Android Open Source - DroidBallet Motion View Registry






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.registry;
/*  w ww . ja  v  a2  s .co m*/
/*
 * #%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.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.util.HashSet;
import java.util.Set;

import android.hardware.SensorEvent;

import com.lonepulse.droidballet.detector.MotionDetector;
import com.lonepulse.droidballet.listener.MotionEvent;
import com.lonepulse.droidballet.listener.MotionListener;
import com.lonepulse.droidballet.queue.EventQueue;
import com.lonepulse.droidballet.queue.MotionEventResolutionJob;
import com.lonepulse.droidballet.resolver.HorizontalMotionEventResolver;
import com.lonepulse.droidballet.resolver.MotionEventResolver;
import com.lonepulse.droidballet.resolver.VerticalMotionEventResolver;

/**
 * <p>A basic implementation of {@link MotionListenerRegistry} which maintains 
 * all motion widgets and view groups.
 * 
 * @version 1.0.0
 * 
 * @author <a href="mailto:lahiru@lonepulse.com">Lahiru Sahan Jayasinghe</a>
 */
public enum MotionViewRegistry implements MotionListenerRegistry, MotionEventResolverRegistry {

  /**
   * <p>The {@link MotionViewRegistry} singleton.
   * 
   * @since 1.0.0
   */
  INSTANCE;

  
  /**
   * <p>Maintains the {@link Set} of {@link MotionListener}s. 
   */
  private volatile Set<MotionListener> motionListeners; 
  {
    motionListeners = new HashSet<MotionListener>();
  }
  
  /**
   * <p>Maintains the {@link Set} of {@link MotionEventResolver}s.
   */
  private volatile Set<MotionEventResolver> motionEventResolvers; 
  {
    
    motionEventResolvers = new HashSet<MotionEventResolver>();
    
    motionEventResolvers.add(new VerticalMotionEventResolver());
    motionEventResolvers.add(new HorizontalMotionEventResolver());
  }
  
  /**
   * <p>This is a {@link ReferenceQueue} with {@link PhantomReference}s 
   * of all registered {@link MotionListener}s. 
     *
   * TODO complete this reference queue implementation
   */
  @SuppressWarnings("unused")
  private ReferenceQueue<MotionListener> referenceQueue; 
  {
    referenceQueue = new ReferenceQueue<MotionListener>();  
  }

  
  /**
   * <p>Initializes the {@link #motionListeners} and the set of {@link MotionEventResolver}s. 
   */
  private MotionViewRegistry() {
  }
  
  /**
   * {@inheritDoc}
   * 
   * <p>This service is {@code synchronized} over {@link MotionViewRegistry#INSTANCE}. 
   */
  @Override
  public synchronized <T extends MotionListener> void register(T motionListener) {
    
    motionListeners.add(motionListener);
  }

  /**
   * {@inheritDoc}
   * 
   * <p>This service is {@code synchronized} over {@link MotionViewRegistry#INSTANCE}.
   */
  @Override
  public synchronized <T extends MotionListener> void unregister(T motionListener) {
    
    motionListeners.remove(motionListener);
  }
  
  /**
   * {@inheritDoc}
   * 
   * <p>This service is {@code synchronized} over {@link MotionViewRegistry#INSTANCE}.
   */
  @Override
  public synchronized <T extends MotionEventResolver> void register(T motionEventResolver) {
    
    motionEventResolvers.add(motionEventResolver);
  }
  
  /**
   * {@inheritDoc}
   * 
   * <p>This service is {@code synchronized} over {@link MotionViewRegistry#INSTANCE}.
   */
  @Override
  public synchronized <T extends MotionEventResolver> void unregister(T motionEventResolver) {
    
    motionEventResolvers.remove(motionEventResolver);
  }

  /**
   * <p>Takes the {@link SensorEvent} which was fired from the motion 
   * sensor and runs it through the {@link MotionDetector}s to generate 
   * the associated {@link MotionEvent}s. Theses events are then fired 
   * on the {@link MotionView}s which are registered under the respective 
   * {@link MotionEvent}s.
   * 
   * @param sensorEvent
   *       the {@link SensorEvent} which was fired from the <b>motion sensor</b>
   * 
   * @since 1.0.0
   */
  public void notify(SensorEvent sensorEvent) {

    for (MotionEventResolver motionEventResolver : motionEventResolvers) {

      MotionEventResolutionJob.Builder builder 
        = new MotionEventResolutionJob.Builder(sensorEvent, motionListeners, motionEventResolver);
      
      EventQueue.INSTANCE.enqueue(builder.build());
      
      motionEventResolver.resolve(sensorEvent, motionListeners);
    }
  }
}




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