Back to project page RazorRunner-AndroidProject.
The source code is released under:
Apache License
If you think the Android project RazorRunner-AndroidProject 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 edu.uark.csce.razorrunner; //from w w w. j a v a 2 s .c o m import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Binder; import android.os.IBinder; import android.os.PowerManager; import android.preference.PreferenceManager; import android.util.Log; import android.widget.Toast; /** * Created by Kai Tribble on 11/29/2014. */ public class UserStepRecognition extends Service { public String STATE = "state"; public String STEPS = "steps"; public String DISTANCE_TRAVELED = "distance"; public String DISTANCE = "distance"; private SharedPreferences mSettings; private PedometerSettings mPedometerSettings; private SharedPreferences mState; private SharedPreferences.Editor mStateEditor; private SensorManager mSensorManager; private Sensor mSensor; private ActivityUtils mActivityUtils; private StepDetector mStepDetector; private StepDisplayer mStepDisplayer; private DistanceNotifier mDistanceNotifier; private SensorManager sensorManager; private long lastUpdate; private Boolean aBoolean = false, bBoolean = false; private Context context; private PowerManager.WakeLock wakeLock; private NotificationManager mNM; private int mSteps; private float mDistance; public class StepBinder extends Binder { UserStepRecognition getService() { return UserStepRecognition.this; } } @Override public void onCreate() { Log.i(ActivityUtils.APPTAG, "[SERVICE] onCreate"); super.onCreate(); mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); showNotification(); // Load settings mSettings = PreferenceManager.getDefaultSharedPreferences(this); mPedometerSettings = new PedometerSettings(mSettings); mState = getSharedPreferences("state", 0); mActivityUtils = ActivityUtils.getInstance(); mActivityUtils.setService(this); acquireWakeLock(); // Start detecting mStepDetector = new StepDetector(); mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); registerDetector(); // Register our receiver for the ACTION_SCREEN_OFF action. This will make our receiver // code be called whenever the phone enters standby mode. IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF); registerReceiver(mReceiver, filter); mStepDisplayer = new StepDisplayer(mPedometerSettings, mActivityUtils); mStepDisplayer.setSteps(mSteps = mState.getInt("steps", 0)); mStepDisplayer.addListener(mStepListener); mStepDetector.addStepListener(mStepDisplayer); mDistanceNotifier = new DistanceNotifier(mDistanceListener, mPedometerSettings, mActivityUtils); mDistanceNotifier.setDistance(mDistance = mState.getFloat("distance", 0)); mStepDetector.addStepListener(mDistanceNotifier); // Tell the user we started. // Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show(); } private StepDisplayer.Listener mStepListener = new StepDisplayer.Listener() { public void stepsChanged(int value) { mSteps = value; passValue(); } public void passValue() { if (mCallback != null) { mCallback.stepsChanged(mSteps); } } }; @Override public void onStart(Intent intent, int startId) { Log.i(ActivityUtils.APPTAG, "[SERVICE] onStart"); super.onStart(intent, startId); } @Override public void onDestroy() { Log.i(ActivityUtils.APPTAG, "[SERVICE] onDestroy"); // Unregister our receiver. unregisterReceiver(mReceiver); unregisterDetector(); mStateEditor = mState.edit(); mStateEditor.putInt("steps", mSteps); mStateEditor.putFloat("distance", mDistance); mStateEditor.commit(); mNM.cancel(R.string.app_name); wakeLock.release(); super.onDestroy(); // Stop detecting mSensorManager.unregisterListener(mStepDetector); // Tell the user we stopped. // Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show(); } private void registerDetector() { mSensor = mSensorManager.getDefaultSensor( Sensor.TYPE_ACCELEROMETER /*| Sensor.TYPE_MAGNETIC_FIELD | Sensor.TYPE_ORIENTATION*/); mSensorManager.registerListener(mStepDetector, mSensor, SensorManager.SENSOR_DELAY_FASTEST); } private void unregisterDetector() { mSensorManager.unregisterListener(mStepDetector); } @Override public IBinder onBind(Intent intent) { Log.i(ActivityUtils.APPTAG, "[SERVICE] onBind"); return mBinder; } /** * Receives messages from activity. */ private final IBinder mBinder = new StepBinder(); public interface ICallback { public void stepsChanged(int value); public void distanceChanged(float value); } private ICallback mCallback; public void registerCallback(ICallback cb) { mCallback = cb; //mStepDisplayer.passValue(); //mPaceListener.passValue(); } public void reloadSettings() { mSettings = PreferenceManager.getDefaultSharedPreferences(this); String activityType = mSettings.getString(ActivityUtils.KEY_PREVIOUS_ACTIVITY_TYPE, ""); if (mStepDetector != null) { if(activityType == "on_foot"){ mStepDetector.setSensitivity( Float.valueOf(mSettings.getString("sensitivity", "50")) ); } else{ mStepDetector.setSensitivity(Float.valueOf(mSettings.getString("sensitivity", "5"))); } } if (mStepDisplayer != null) mStepDisplayer.reloadSettings(); if (mDistanceNotifier != null) mDistanceNotifier.reloadSettings(); } public void resetValues() { mStepDisplayer.setSteps(0); mDistanceNotifier.setDistance(0); } /** * Forwards distance values from DistanceNotifier to the activity. */ private DistanceNotifier.Listener mDistanceListener = new DistanceNotifier.Listener() { public void valueChanged(float value) { mDistance = value; passValue(); } public void passValue() { if (mCallback != null) { mCallback.distanceChanged(mDistance); } } }; /** * Show a notification while this service is running. */ private void showNotification() { CharSequence text = getText(R.string.app_name); Notification notification = new Notification(R.drawable.ic_notification, null, System.currentTimeMillis()); notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT; Intent pedometerIntent = new Intent(); pedometerIntent.setComponent(new ComponentName(this, WorkoutActivity.class)); pedometerIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, pedometerIntent, 0); notification.setLatestEventInfo(this, text, "Counting your steps", contentIntent); mNM.notify(R.string.app_name, notification); } // BroadcastReceiver for handling ACTION_SCREEN_OFF. private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // Check action just to be on the safe side. if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { // Unregisters the listener and registers it again. UserStepRecognition.this.unregisterDetector(); UserStepRecognition.this.registerDetector(); if (mPedometerSettings.wakeAggressively()) { wakeLock.release(); acquireWakeLock(); } } } }; private void acquireWakeLock() { PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); int wakeFlags; if (mPedometerSettings.wakeAggressively()) { wakeFlags = PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP; } else if (mPedometerSettings.keepScreenOn()) { wakeFlags = PowerManager.SCREEN_DIM_WAKE_LOCK; } else { wakeFlags = PowerManager.PARTIAL_WAKE_LOCK; } wakeLock = pm.newWakeLock(wakeFlags, ActivityUtils.APPTAG); wakeLock.acquire(); } }