Ejemplo n.º 1
0
 @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;
 }
Ejemplo n.º 2
0
 @Nullable
 @Override
 public IBinder onBind(Intent intent) {
   Logger.d(this, "onBind");
   cancelCurrentNotification();
   return (IBinder) mBinder;
 }
Ejemplo n.º 3
0
 private void unregisterSensors() {
   Logger.d(this, "unregisterSensors");
   final SensorManager sensorManager = getSensorManager();
   if (sensorManager != null) {
     sensorManager.unregisterListener(this);
   }
 }
Ejemplo n.º 4
0
 @Override
 public void stopTracking() {
   Logger.d(this, "stopTracking");
   cancelCurrentNotification();
   stopSensors();
   stopSelf();
 }
Ejemplo n.º 5
0
 @Override
 public void startTracking() {
   Logger.d(this, "startTracking");
   if (isNotRunning()) {
     SensorService.startService(SensorService.this);
   }
   startSensors();
 }
Ejemplo n.º 6
0
  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);
  }
Ejemplo n.º 7
0
 @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);
   }
 }
Ejemplo n.º 8
0
 @Override
 public boolean onUnbind(Intent intent) {
   Logger.d(this, "onUnbind");
   mListener = null;
   if (isNotRunning()) {
     stopSelf();
   } else {
     displayNotification();
   }
   return true;
 }
Ejemplo n.º 9
0
 private void startSensors() {
   Logger.d(this, "startSensors");
   // Clock.
   mSessionStartTimeMillis = SystemClock.elapsedRealtime();
   Preferences.storeStartTime(this, mSessionStartTimeMillis);
   // Sensors.
   registerSensors();
   if (mListener != null) {
     mListener.onSensorStart(mSessionStartTimeMillis);
   }
 }
Ejemplo n.º 10
0
 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();
   }
 }
Ejemplo n.º 11
0
 @Override
 public void onRebind(Intent intent) {
   Logger.d(this, "onRebind");
   cancelCurrentNotification();
 }
Ejemplo n.º 12
0
 @Override
 public void onDestroy() {
   Logger.d(this, "onDestroy");
   Preferences.clearStoredValues(this);
 }