@Override public int onStartCommand(Intent intent, int flags, int startId) { if (intent != null && Notification.STOP_SERVICE.equals(intent.getAction())) { Logger.d(this, "Stopping service."); mBinder.stopTracking(); } else { Logger.d(this, "onStartCommand"); restoreServiceState(); } return START_STICKY; }
@Nullable @Override public IBinder onBind(Intent intent) { Logger.d(this, "onBind"); cancelCurrentNotification(); return (IBinder) mBinder; }
private void unregisterSensors() { Logger.d(this, "unregisterSensors"); final SensorManager sensorManager = getSensorManager(); if (sensorManager != null) { sensorManager.unregisterListener(this); } }
@Override public void stopTracking() { Logger.d(this, "stopTracking"); cancelCurrentNotification(); stopSensors(); stopSelf(); }
@Override public void startTracking() { Logger.d(this, "startTracking"); if (isNotRunning()) { SensorService.startService(SensorService.this); } startSensors(); }
private void saveSessionData() { Logger.d(this, "saveSessionData"); Logger.d( this, "Start time: " + DateFormatUtils.formatDateTimeNoYear(new Date(getSessionStartTime()))); Logger.d(this, "Duration: " + DateFormatUtils.formatTimeNoZone(new Date(getSessionDuration()))); Logger.d(this, "Steps: + " + mSensorInfo.getSteps()); Logger.d(this, "Distance: " + DistanceUtils.distanceFromSteps(mSensorInfo.getSteps())); final ContentValues contentValues = new ContentValues(); contentValues.put(SessionEntry.COLUMN_NAME_START_TIME, getSessionStartTime()); contentValues.put(SessionEntry.COLUMN_NAME_DURATION, getSessionDuration()); contentValues.put(SessionEntry.COLUMN_NAME_STEPS, mSensorInfo.getSteps()); contentValues.put( SessionEntry.COLUMN_NAME_DISTANCE, DistanceUtils.distanceFromSteps(mSensorInfo.getSteps())); getContentResolver().insert(PlaygroundContentProvider.Service.SESSIONS.getUri(), contentValues); }
@Override public void onSensorChanged(@NonNull SensorEvent event) { if (mSensorInfo == null) { return; } switch (event.sensor.getType()) { case Sensor.TYPE_STEP_COUNTER: final long totalSteps = (long) event.values[0]; if (mSessionInitialSteps == Integer.MIN_VALUE) { mSessionInitialSteps = totalSteps; Preferences.storeSensorInitialStepCount(this, mSessionInitialSteps); } mSensorInfo.setSteps(totalSteps - mSessionInitialSteps); Logger.d(this, "Step count: " + mSensorInfo.getSteps()); break; case Sensor.TYPE_LINEAR_ACCELERATION: final float x = event.values[0]; final float y = event.values[1]; final float z = event.values[2]; //noinspection SuspiciousNameCombination final double totalAcc = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2) + Math.pow(z, 2)); float filteredAcc = filterAcceleration((float) totalAcc); SensorInfo.ActivityType activityType = SensorInfo.ActivityType.STILL; if (filteredAcc < WALKING_THRESHOLD) { Logger.d(this, "Still: " + filteredAcc); activityType = SensorInfo.ActivityType.STILL; } else if (filteredAcc > WALKING_THRESHOLD && filteredAcc < RUNNING_THRESHOLD) { Logger.d(this, "Walking: " + filteredAcc); activityType = SensorInfo.ActivityType.WALKING; } else if (filteredAcc > RUNNING_THRESHOLD) { Logger.d(this, "Running: " + filteredAcc); activityType = SensorInfo.ActivityType.RUNNING; } if (!activityType.equals(mSensorInfo.getActivityType())) { mSensorInfo.setActivityType(activityType); } break; default: } if (mListener != null) { mListener.onSensorUpdate(mSensorInfo); } }
@Override public boolean onUnbind(Intent intent) { Logger.d(this, "onUnbind"); mListener = null; if (isNotRunning()) { stopSelf(); } else { displayNotification(); } return true; }
private void startSensors() { Logger.d(this, "startSensors"); // Clock. mSessionStartTimeMillis = SystemClock.elapsedRealtime(); Preferences.storeStartTime(this, mSessionStartTimeMillis); // Sensors. registerSensors(); if (mListener != null) { mListener.onSensorStart(mSessionStartTimeMillis); } }
private void stopSensors() { Logger.d(this, "stopSensors"); // Store data. saveSessionData(); // Clear state. Preferences.clearStoredValues(this); mSessionStartTimeMillis = Integer.MIN_VALUE; mSessionInitialSteps = Integer.MIN_VALUE; mSensorInfo = null; // Sensors. unregisterSensors(); if (mListener != null) { mListener.onSensorStop(); } }
@Override public void onRebind(Intent intent) { Logger.d(this, "onRebind"); cancelCurrentNotification(); }
@Override public void onDestroy() { Logger.d(this, "onDestroy"); Preferences.clearStoredValues(this); }