Back to project page DroidBallet.
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.
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); }