Android Open Source - yousense-android-tracker Duty Cycled Accelerometer






From Project

Back to project page yousense-android-tracker.

License

The source code is released under:

Energy-efficent motion and location tracker for Android. Based on Mattias's power and tracking work. I plan to release it as GPL, once I have a paper published that goes with it. Might also release i...

If you think the Android project yousense-android-tracker 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.linnap.locationtracker.movement;
/*from   ww  w  .  j  ava2  s  .  c o m*/
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Handler;
import android.util.FloatMath;

import com.linnap.locationtracker.LocationTrackerService;
import com.linnap.locationtracker.SensorConfig;
import com.linnap.locationtracker.StateChange;

public class DutyCycledAccelerometer implements SensorEventListener {
  
  LocationTrackerService service;
  MovementDetectedListener listener;
  Handler handler;
  SensorManager sensorManager;  
  Sensor accelerometer;
  StatsAccumulator magnitude;
  BooleanState state;
  
  public DutyCycledAccelerometer(LocationTrackerService service, Handler handler, MovementDetectedListener listener) {
    this.service = service;
    this.listener = listener;
    this.handler = handler;
    this.sensorManager = (SensorManager)service.getSystemService(Context.SENSOR_SERVICE);
    this.accelerometer = this.sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    this.magnitude = new StatsAccumulator(SensorConfig.ACCELEROMETER_ACCUMULATOR_SIZE);
    this.state = BooleanState.OFF;
  }
  
  public synchronized void start() {
    service.event("dutycycledaccelerometer_state", new StateChange(state, BooleanState.ON));
    state = BooleanState.ON;
    startAccelerometer.run();
  }
  
  public synchronized void stop() {
    service.event("dutycycledaccelerometer_state", new StateChange(state, BooleanState.OFF));
    state = BooleanState.OFF;
    sensorManager.unregisterListener(DutyCycledAccelerometer.this);
    handler.removeCallbacks(startAccelerometer);
    handler.removeCallbacks(stopAccelerometer);
  }
  
  /// Implementation.
  
  Runnable startAccelerometer = new Runnable() {
    public void run() {
      synchronized (DutyCycledAccelerometer.this) {
        if (state == BooleanState.ON) {
          magnitude.clear();
          sensorManager.registerListener(DutyCycledAccelerometer.this, accelerometer, SensorConfig.ACCELEROMETER_RATE, handler);
          handler.postDelayed(stopAccelerometer, SensorConfig.ACCELEROMETER_ACTIVE_MILLIS);
        }
      }
    }
  };
  
  Runnable stopAccelerometer = new Runnable() {
    public void run() {
      synchronized (DutyCycledAccelerometer.this) {
        if (state == BooleanState.ON) {
          sensorManager.unregisterListener(DutyCycledAccelerometer.this);          
          float variance = magnitude.variance();
          if (variance >= SensorConfig.ACCELEROMETER_MAGNITUDE_VARIANCE_FOR_MOVEMENT) {
            service.event("accelerometer_movement", new AccelerometerData(variance));
            listener.movementDetected();            
          }
          handler.postDelayed(startAccelerometer, SensorConfig.ACCELEROMETER_SLEEP_MILLIS);
        }
      }
    }
  };

  public void onAccuracyChanged(Sensor sensor, int accuracy) {
  }

  public void onSensorChanged(SensorEvent event) {
    magnitude.add(FloatMath.sqrt(event.values[0] * event.values[0] + event.values[1] * event.values[1] + event.values[2] * event.values[2]));
  }
  
  
  /// Callbacks
  
  public interface MovementDetectedListener {
    public void movementDetected();
  }
}




Java Source Code List

com.linnap.locationtracker.EventBindings.java
com.linnap.locationtracker.ExpectedState.java
com.linnap.locationtracker.LocationTrackerService.java
com.linnap.locationtracker.SensorConfig.java
com.linnap.locationtracker.Sets.java
com.linnap.locationtracker.StateChange.java
com.linnap.locationtracker.gps.DistanceCycledGps.java
com.linnap.locationtracker.gps.GpsHistory.java
com.linnap.locationtracker.gps.LocationFix.java
com.linnap.locationtracker.movement.AccelerometerData.java
com.linnap.locationtracker.movement.BooleanState.java
com.linnap.locationtracker.movement.DutyCycledAccelerometer.java
com.linnap.locationtracker.movement.SmallMovement.java
com.linnap.locationtracker.movement.StatsAccumulator.java
com.linnap.locationtracker.schedule.SensorScheduler.java
com.linnap.locationtracker.wifi.ScanResultsData.java
com.linnap.locationtracker.wifi.ScanStartedData.java
com.linnap.locationtracker.wifi.TimeoutScan.java
com.linnap.locationtracker.wifi.WifiFingerprint.java
com.linnap.locationtracker.wifi.WifiPlaceChange.java