Android Open Source - DroidBallet Higgs Field






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.core;
//w w  w. j ava2 s.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.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorManager;
import android.view.ViewGroup;

import com.lonepulse.droidballet.queue.EventQueue;
import com.lonepulse.droidballet.registry.MotionViewRegistry;

/**
 * <p>"<i>The Higgs-Field is a possibly discovered, ubiquitous quantum field 
 * supposed to be responsible for giving elementary particles their masses.</i>"</p>
 * 
 * <p>This enum is responsible for maintaining a single instance of itself which 
 * can be used by Widgets  and {@link ViewGroup}s to register themselves for changes 
 * in device orientation and respond to the associated motion.</p>
 * 
 * @version 1.0.0
 * <br><br>
 * @author <a href="mailto:lahiru@lonepulse.com">Lahiru Sahan Jayasinghe</a>
 */
public enum HiggsField implements HiggsMechanism {
  
  
  /**
   * <p>The {@link HiggsField} singleton.
   * 
   * @since 1.0.0
   */
  INSTANCE;

  
  /**
   * <p>A flag which determines if the HiggsField has already been 
   * initialized via {@link #initialize(HiggsFieldConfiguration)}.
   */
  private volatile AtomicBoolean initialized = new AtomicBoolean(false);
  
  /**
   * <p>An {@link Executors#newSingleThreadExecutor()} instance which accepts motion event 
   * producers for changes in sensor readings fired on {@link #onSensorChanged(SensorEvent)}.
   */
  private ExecutorService PRODUCER_EXECUTOR_SERVICE;
  
  /**
   * <p>The instance of {@link HIGGS_FIELD_STATE} which indicates the <b>current</b> 
   * state of the {@link HiggsField#INSTANCE}.</p>
   * 
   * <p>Do not mutate directly - always use {@link #setState(HIGGS_FIELD_STATE)}.</p>
   */
  private volatile HIGGS_FIELD_STATE state = HIGGS_FIELD_STATE.INACTIVE;

  /**
   * <p>The <i>configuration</i> which was used to activate the {@link HiggsField#INSTANCE}. 
   * This is available only after activation via {@link #activate(HiggsFieldConfiguration)} 
   * has succeeded.
   */
  private HiggsFieldConfiguration config;
  
  /**
   * <p>The system {@link SensorManager} which can be used to manage all the 
   * <b>motion sensors</b>.
   */
  private SensorManager sensorManager;
  
  /**
   * <p>The <i>default</i> instance of the <b>Accelerometer</b> found on the device.
   */
  private Sensor accelerometer;
  
  
  /**
   * <p>Accessor for {@link #state}.
   * 
   * @return {@link #state}
   */
  public synchronized HIGGS_FIELD_STATE getState() {
    
    return state;
  }

  /**
   * <p>Updates the {@link HIGGS_FIELD_STATE} of the {@link HiggsField}.
   * 
   * @param state
   *       the {@link HIGGS_FIELD_STATE} to update {@link HiggsField#state}
   */
  private synchronized void setState(HIGGS_FIELD_STATE state) {
    
    this.state = state;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public synchronized void initialize(final HiggsFieldConfiguration config) {
    
    if(!initialized.get()) {
    
      this.config = config;
  
      Context context = this.config.getApplication();
      
      this.sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
      this.accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
      
      initialized.set(true);
    }
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public synchronized void activate() {
    
    if(!initialized.get())
      throw new HiggsFieldUninitializedException();
    
    if(getState().equals(HIGGS_FIELD_STATE.INACTIVE)) {
      
      setState(HIGGS_FIELD_STATE.ACTIVE);
      
      PRODUCER_EXECUTOR_SERVICE = Executors.newSingleThreadExecutor();
      sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
      EventQueue.INSTANCE.startConsuming();
    }
  }
  
  /**
   * {@inheritDoc}
   */
  @Override
  public synchronized void deactivate() {
    
    if(!initialized.get())
      throw new HiggsFieldUninitializedException();
    
    if(getState().equals(HIGGS_FIELD_STATE.ACTIVE)) {
    
      setState(HIGGS_FIELD_STATE.INACTIVE);
      
      sensorManager.unregisterListener(this);
      PRODUCER_EXECUTOR_SERVICE.shutdownNow();
      EventQueue.INSTANCE.stopConsuming();
    }
  }
  
  /**
   * {@inheritDoc}
   */
  @Override
  public void onSensorChanged(final SensorEvent sensorEvent) {

    if(getState().equals(HIGGS_FIELD_STATE.INACTIVE)) return;
    
    PRODUCER_EXECUTOR_SERVICE.submit(new Runnable() {
      
      @Override
      public void run() {
        
        boolean aquired = EventQueue.CONSUMER_LOCK.tryLock();
        
        if(aquired) {
          
          try {
            
            MotionViewRegistry.INSTANCE.notify(sensorEvent);
            EventQueue.ENQUEUED.signal();
          }
          finally {
            
            EventQueue.CONSUMER_LOCK.unlock();
          }
        }
        else {
          
          MotionViewRegistry.INSTANCE.notify(sensorEvent);
        }
      }
    });
  }
  
  /**
   * {@inheritDoc}
   */
  @Override
  public void onAccuracyChanged(Sensor sensor, int accuracy) {} 
}




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