Back to project page yousense-android-tracker.
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.
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(); } }