Android Open Source - DroidBallet Abstract Motion Event Resolver






From Project

Back to project page DroidBallet.

License

The source code is released under:

Apache License

If you think the Android project DroidBallet listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.lonepulse.droidballet.resolver;
/*from  w  w w.j ava2s  . com*/
/*
 * #%L
 * DroidBallet
 * %%
 * Copyright (C) 2013 Lonepulse
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.util.HashSet;
import java.util.Set;

import android.hardware.SensorEvent;
import android.util.Log;

import com.lonepulse.droidballet.detector.MotionDetector;
import com.lonepulse.droidballet.detector.MotionDetectorException;
import com.lonepulse.droidballet.listener.MotionEvent;
import com.lonepulse.droidballet.listener.MotionListener;


/**
 * <p>An abstract implementation of {@link MotionEventResolver} which performs the 
 * generic tasks.
 * 
 * @version 1.0.0
 * 
 * @author <a href="mailto:lahiru@lonepulse.com">Lahiru Sahan Jayasinghe</a>
 */
public abstract class AbstractMotionEventResolver<L extends MotionListener, 
                          E extends MotionEvent, 
                          D extends MotionDetector<E>> implements MotionEventResolver {

  
  /**
   * <p>the {@link MotionDetector} of the motion type handled by this 
   * {@link AbstractMotionEventResolver} implementation.
   * 
   * @since 1.0.0
   */
  private D motionDetector;
  
  /**
   * <p>The {@link Class} of the {@link MotionListener} sub-type which this implementation of 
   * {@link AbstractMotionEventResolver} is designated to handle. 
   * 
   * @since 1.0.0
   */
  private Class<L> motionListenerClass;
  
  
  /**
   * <p>A parameterized constructor which takes the {@link MotionDetector} of the 
   * associated with the motion type handled by this implementation of 
   * {@link AbstractMotionEventResolver}. 
   * 
   * @param motionListenerClass
   *       the {@link #motionListenerClass} used by this instance implementation 
   *       of {@link AbstractMotionEventResolver}
   *  
   * @param motionDetector
   *       the {@link #motionDetector} used by this implementation of 
   *       {@link AbstractMotionEventResolver}
   * 
   * @since 1.0.0 
   */
  public AbstractMotionEventResolver(Class<L> motionListenerClass, D motionDetector) {
    
    this.motionListenerClass = motionListenerClass;
    this.motionDetector = motionDetector;
  }

  /**
   * {@inheritDoc}
   */
  public void resolve(SensorEvent sensorEvent, Set<MotionListener> motionListeners) {
  
    Set<L> filteredMotionListeners = filterMotionListeners(motionListeners);
    
    E motionEvent = generateMotionEvent(sensorEvent);
    
    if(motionEvent != null) {
      
      fireMotionEvent(motionEvent, filteredMotionListeners);
    }
  }
  
  /**
   * <p>Takes a {@link Set} of {@link MotionListener}s and filter out those that are of the type 
   * handled by this implementation of {@link AbstractMotionEventResolver}.
   * 
   * @param motionListeners
   *       the {@link Set} of {@link MotionListener}s which are to be filtered
   * 
   * @return  the filtered {@link Set} of {@link MotionListener}s
   * 
   * @since 1.0.0
   */
  protected Set<L> filterMotionListeners(Set<MotionListener> motionListeners) {
    
    Set<L> filteredMotionListeners = new HashSet<L>();
    
    for (MotionListener motionListener : motionListeners) {
      
      if(motionListenerClass.isAssignableFrom(motionListener.getClass()))
        filteredMotionListeners.add(motionListenerClass.cast(motionListener));
    }
    
    return filteredMotionListeners;
  }
  
  
  /**
   * <p>Takes the {@link SensorEvent} generated by the motion sensor and creates the 
   * associated {@link MotionEvent} using the {@link #motionDetector}.
   * 
   * @param sensorEvent
   *       the {@link SensorEvent} generated by the motion sensor
   * 
   * @return the generated {@link MotionEvent}, else {@code null} if motion detection 
   *        failed with a {@link MotionDetectorException}
   * 
   * @since 1.0.0
   */
  protected E generateMotionEvent(SensorEvent sensorEvent) {
    
    try {
    
      return motionDetector.getMotionEvent(sensorEvent);
    }
    catch (MotionDetectorException mde) {
      
      Log.w(getClass().getSimpleName(), mde);
      return null;
    }
  }
  
  
  /**
   * <p>Takes the {@link Set} of filtered {@link MotionListener}s and fires 
   * the generated {@link MotionEvent} on each of them. The way in which this 
   * is performed is different depending on the {@link MotionListener} contract.</p>
   * 
   * <p>The information within the generated {@link MotionEvent} will be used to 
   * judge which service will be invoked on the {@link MotionListener}'s contract.</p> 
   * 
   * <p>All implementations of {@link AbstractMotionEventResolver} should override 
   * this method handle the event firing scenario respective to it's associated 
   * {@link MotionListener}.
   * 
   * @param motionEvent
   *       the {@link MotionEvent} generated via the {@link #motionDetector}
   * 
   * @param filteredMotionListeners
   *       the {@link Set} of filtered motion listeners of the relevant type 
   *       upon which the generated {@link MotionEvent} should be fired
   *       
   * @since 1.0.0
   */
  protected abstract void fireMotionEvent(E motionEvent, Set<L> filteredMotionListeners);
}




Java Source Code List

com.lonepulse.droidballet.DroidBalletException.java
com.lonepulse.droidballet.DroidBalletRuntimeException.java
com.lonepulse.droidballet.app.MotionApplication.java
com.lonepulse.droidballet.core.HiggsFieldConfiguration.java
com.lonepulse.droidballet.core.HiggsFieldException.java
com.lonepulse.droidballet.core.HiggsFieldUninitializedException.java
com.lonepulse.droidballet.core.HiggsField.java
com.lonepulse.droidballet.core.HiggsMechanism.java
com.lonepulse.droidballet.core.SensorType.java
com.lonepulse.droidballet.detector.HorizontalMotionDetector.java
com.lonepulse.droidballet.detector.MotionDetectorException.java
com.lonepulse.droidballet.detector.MotionDetector.java
com.lonepulse.droidballet.detector.VerticalMotionDetector.java
com.lonepulse.droidballet.filter.AbstractSmoothingFilter.java
com.lonepulse.droidballet.filter.LowPassFilter.java
com.lonepulse.droidballet.filter.SmoothingFilterException.java
com.lonepulse.droidballet.filter.SmoothingFilter.java
com.lonepulse.droidballet.listener.AbstractMotionEvent.java
com.lonepulse.droidballet.listener.HorizontalMotionEvent.java
com.lonepulse.droidballet.listener.HorizontalMotionListener.java
com.lonepulse.droidballet.listener.MotionEvent.java
com.lonepulse.droidballet.listener.MotionListener.java
com.lonepulse.droidballet.listener.VerticalMotionEvent.java
com.lonepulse.droidballet.listener.VerticalMotionListener.java
com.lonepulse.droidballet.queue.EventQueue.java
com.lonepulse.droidballet.queue.MotionEventResolutionJob.java
com.lonepulse.droidballet.queue.QueueController.java
com.lonepulse.droidballet.registry.MotionEventResolverRegistry.java
com.lonepulse.droidballet.registry.MotionListenerRegistry.java
com.lonepulse.droidballet.registry.MotionViewRegistry.java
com.lonepulse.droidballet.resolver.AbstractMotionEventResolver.java
com.lonepulse.droidballet.resolver.HorizontalMotionEventResolver.java
com.lonepulse.droidballet.resolver.MotionEventResolver.java
com.lonepulse.droidballet.resolver.VerticalMotionEventResolver.java
com.lonepulse.droidballet.widget.LinearMotionListView.java
com.lonepulse.droidballet.widget.MotionView.java