Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Aug 3rd, 2011  |  syntax: Java  |  size: 5.93 KB  |  views: 56  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package com.sonaur.sonaur;
  2.  
  3. import java.lang.reflect.*;
  4. import java.util.List;
  5.  
  6. import android.content.Context;
  7. import android.hardware.Sensor;
  8. import android.hardware.SensorEvent;
  9. import android.hardware.SensorEventListener;
  10. import android.hardware.SensorManager;
  11.  
  12.  
  13. /**
  14.  * Android Accelerometer Sensor Manager Archetype
  15.  * @author antoine vianey
  16.  * under GPL v3 : http://www.gnu.org/licenses/gpl-3.0.html
  17.  */
  18. public class AccelerometerManager {
  19.   /** Accuracy configuration */
  20.   private float threshold = 0.2f;
  21.   private int interval = 1000;
  22.  
  23.   private Sensor sensor;
  24.   private SensorManager sensorManager;
  25.   // you could use an OrientationListener array instead
  26.   // if you plans to use more than one listener
  27. //  private AccelerometerListener listener;
  28.  
  29.   Method shakeEventMethod;
  30.   Method accelerationEventMethod;
  31.  
  32.   /** indicates whether or not Accelerometer Sensor is supported */
  33.   private Boolean supported;
  34.   /** indicates whether or not Accelerometer Sensor is running */
  35.   private boolean running = false;
  36.  
  37.   Context context;
  38.  
  39.  
  40.   public AccelerometerManager(Context parent) {
  41.     this.context = parent;
  42.    
  43.     try {
  44.       shakeEventMethod =
  45.         parent.getClass().getMethod("shakeEvent", new Class[] { Float.TYPE });
  46.     } catch (Exception e) {
  47.       // no such method, or an error.. which is fine, just ignore
  48.     }
  49.  
  50.     try {
  51.       accelerationEventMethod =
  52.         parent.getClass().getMethod("accelerationEvent", new Class[] { Float.TYPE, Float.TYPE, Float.TYPE });
  53.     } catch (Exception e) {
  54.       // no such method, or an error.. which is fine, just ignore
  55.     }
  56. //    System.out.println("shakeEventMethod is " + shakeEventMethod);
  57. //    System.out.println("accelerationEventMethod is " + accelerationEventMethod);
  58.     resume();
  59.   }
  60.  
  61.  
  62.   public AccelerometerManager(Context context, int threshold, int interval) {
  63.     this(context);
  64.     this.threshold = threshold;
  65.     this.interval = interval;
  66.   }
  67.  
  68.  
  69.   public void resume() {
  70.     if (isSupported()) {
  71.       startListening();
  72.     }
  73.   }
  74.  
  75.  
  76.   public void pause() {
  77.     if (isListening()) {
  78.       stopListening();
  79.     }
  80.   }
  81.  
  82.  
  83.   /**
  84.    * Returns true if the manager is listening to orientation changes
  85.    */
  86.   public boolean isListening() {
  87.     return running;
  88.   }
  89.  
  90.  
  91.   /**
  92.    * Unregisters listeners
  93.    */
  94.   public void stopListening() {
  95.     running = false;
  96.     try {
  97.       if (sensorManager != null && sensorEventListener != null) {
  98.         sensorManager.unregisterListener(sensorEventListener);
  99.       }
  100.     }
  101.     catch (Exception e) {
  102.     }
  103.   }
  104.  
  105.  
  106.   /**
  107.    * Returns true if at least one Accelerometer sensor is available
  108.    */
  109.   public boolean isSupported() {
  110.     if (supported == null) {
  111.       sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
  112.       List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
  113.       supported = new Boolean(sensors.size() > 0);
  114.     }
  115.     return supported;
  116.   }
  117.  
  118.  
  119. //  /**
  120. //   * Configure the listener for shaking
  121. //   * @param threshold
  122. //   *                  minimum acceleration variation for considering shaking
  123. //   * @param interval
  124. //   *                  minimum interval between to shake events
  125. //   */
  126. //  public static void configure(int threshold, int interval) {
  127. //    AccelerometerManager.threshold = threshold;
  128. //    AccelerometerManager.interval = interval;
  129. //  }
  130.  
  131.  
  132.   /**
  133.    * Registers a listener and start listening
  134.    * @param accelerometerListener callback for accelerometer events
  135.    */
  136.   public void startListening() {
  137. //    AccelerometerListener accelerometerListener = (AccelerometerListener) context;
  138.     sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
  139.     List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
  140.     if (sensors.size() > 0) {
  141.       sensor = sensors.get(0);
  142.       running = sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_GAME);
  143. //      listener = accelerometerListener;
  144.     }
  145.   }
  146.  
  147.  
  148. //  /**
  149. //   * Configures threshold and interval
  150. //   * And registers a listener and start listening
  151. //   * @param accelerometerListener
  152. //   *                  callback for accelerometer events
  153. //   * @param threshold
  154. //   *                  minimum acceleration variation for considering shaking
  155. //   * @param interval
  156. //   *                  minimum interval between to shake events
  157. //   */
  158. //  public void startListening(int threshold, int interval) {
  159. //    configure(threshold, interval);
  160. //    startListening();
  161. //  }
  162.  
  163.  
  164.   /**
  165.    * The listener that listen to events from the accelerometer listener
  166.    */
  167.   //private static SensorEventListener sensorEventListener = new SensorEventListener() {
  168.   private SensorEventListener sensorEventListener = new SensorEventListener() {
  169.     private long now = 0;
  170.     private long timeDiff = 0;
  171.     private long lastUpdate = 0;
  172.     private long lastShake = 0;
  173.  
  174.     private float x = 0;
  175.     private float y = 0;
  176.     private float z = 0;
  177.     private float lastX = 0;
  178.     private float lastY = 0;
  179.     private float lastZ = 0;
  180.     private float force = 0;
  181.  
  182.     public void onAccuracyChanged(Sensor sensor, int accuracy) {
  183.     }
  184.  
  185.     public void onSensorChanged(SensorEvent event) {
  186.       // use the event timestamp as reference
  187.       // so the manager precision won't depends
  188.       // on the AccelerometerListener implementation
  189.       // processing time
  190.       now = event.timestamp;
  191.  
  192.       x = event.values[0];
  193.       y = event.values[1];
  194.       z = event.values[2];
  195.  
  196.       // if not interesting in shake events
  197.       // just remove the whole if then else bloc
  198.      
  199.       // trigger change event
  200. //      listener.onAccelerationChanged(x, y, z);
  201.       if (accelerationEventMethod != null) {
  202.         try {
  203.           accelerationEventMethod.invoke(context, new Object[] { x, y, z });
  204.         } catch (Exception e) {
  205.           e.printStackTrace();
  206.           accelerationEventMethod = null;
  207.         }
  208.       }
  209.     }
  210.   };
  211. }
clone this paste RAW Paste Data