keep step count when restarting trackerservice (see #29)
This commit is contained in:
		
							parent
							
								
									adc68605a4
								
							
						
					
					
						commit
						1584eac152
					
				
					 5 changed files with 19 additions and 60 deletions
				
			
		|  | @ -294,10 +294,6 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys { | |||
|         // dismiss notification | ||||
|         startTrackerService(ACTION_DISMISS, null); | ||||
| 
 | ||||
| //        Intent intent = new Intent(this, TrackerService.class); // todo remove | ||||
| //        intent.setAction(ACTION_DISMISS); | ||||
| //        startService(intent); | ||||
| 
 | ||||
|         // hide Floating Action Button sub menu | ||||
|         showFloatingActionButtonMenu(false); | ||||
| 
 | ||||
|  | @ -474,15 +470,6 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys { | |||
|                     // start tracker service | ||||
|                     startTrackerService(ACTION_START, lastLocation); | ||||
| 
 | ||||
| //                    Intent intent = new Intent(this, TrackerService.class); // todo remove | ||||
| //                    intent.setAction(ACTION_START); | ||||
| //                    intent.putExtra(EXTRA_LAST_LOCATION, lastLocation); | ||||
| //                    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) { | ||||
| //                        startForegroundService(intent); | ||||
| //                    } else { | ||||
| //                        startService(intent); | ||||
| //                    } | ||||
| 
 | ||||
|                 } else { | ||||
|                     Toast.makeText(this, getString(R.string.toast_message_location_services_not_ready), Toast.LENGTH_LONG).show(); | ||||
|                     // change state back | ||||
|  | @ -502,19 +489,11 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys { | |||
|                 // stop tracker service | ||||
|                 startTrackerService(ACTION_STOP, null); | ||||
| 
 | ||||
| //                Intent intent = new Intent(this, TrackerService.class); // todo remove | ||||
| //                intent.setAction(ACTION_STOP); | ||||
| //                startService(intent); | ||||
| 
 | ||||
|                 break; | ||||
| 
 | ||||
|             case FAB_STATE_SAVE: | ||||
|                 // toggle floating action button sub menu | ||||
|                 if (!mFloatingActionButtonSubMenuVisible) { | ||||
|                     showFloatingActionButtonMenu(true); | ||||
|                 } else { | ||||
|                     showFloatingActionButtonMenu(false); | ||||
|                 } | ||||
|                 // toggle visibility floating action button sub menu | ||||
|                 showFloatingActionButtonMenu(!mFloatingActionButtonSubMenuVisible); | ||||
| 
 | ||||
|                 break; | ||||
| 
 | ||||
|  |  | |||
|  | @ -294,7 +294,6 @@ public class MainActivityMapFragment extends Fragment implements TrackbookKeys { | |||
|     } | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|     @Override | ||||
|     public void onActivityResult(int requestCode, int resultCode, Intent data) { | ||||
|         super.onActivityResult(requestCode, resultCode, data); | ||||
|  |  | |||
|  | @ -16,7 +16,6 @@ | |||
| 
 | ||||
| package org.y20k.trackbook; | ||||
| 
 | ||||
| import android.app.ActivityManager; | ||||
| import android.app.Notification; | ||||
| import android.app.NotificationManager; | ||||
| import android.app.Service; | ||||
|  | @ -50,9 +49,10 @@ import org.y20k.trackbook.helpers.NotificationHelper; | |||
| import org.y20k.trackbook.helpers.StorageHelper; | ||||
| import org.y20k.trackbook.helpers.TrackbookKeys; | ||||
| 
 | ||||
| import java.util.Iterator; | ||||
| import java.util.List; | ||||
| 
 | ||||
| import static android.hardware.Sensor.TYPE_STEP_COUNTER; | ||||
| 
 | ||||
| 
 | ||||
| /** | ||||
|  * TrackerService class | ||||
|  | @ -184,9 +184,9 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven | |||
| 
 | ||||
|     @Override | ||||
|     public void onSensorChanged(SensorEvent sensorEvent) { | ||||
|         // save the step count offset / previously recorded steps | ||||
|         // save the step count offset (steps previously recorded by the system) and add any steps recorded during this session in case the app was killed | ||||
|         if (mStepCountOffset == 0) { | ||||
|             mStepCountOffset = sensorEvent.values[0] - 1; | ||||
|             mStepCountOffset = sensorEvent.values[0] - 1 + mTrack.getStepCount(); | ||||
|         } | ||||
| 
 | ||||
|         // calculate step count | ||||
|  | @ -229,14 +229,14 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven | |||
|         mNotification = NotificationHelper.getNotification(this, mNotificationBuilder, mTrack, true); | ||||
|         mNotificationManager.notify(TRACKER_SERVICE_NOTIFICATION_ID, mNotification); // todo check if necessary in pre Android O | ||||
| 
 | ||||
|         // get duration of previously recorded track - in case this service has been restarted | ||||
|         final long previouslyRecordedDuration = mTrack.getTrackDuration(); | ||||
| 
 | ||||
|         // set timer to retrieve new locations and to prevent endless tracking | ||||
| //        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); | ||||
| //        final long previouslyRecordedDuration = settings.getLong(PREFS_CURRENT_TRACK_DURATION, 0);// todo describe | ||||
|         final long previouslyRecordedDuration = mTrack.getTrackDuration();// todo describe | ||||
|         mTimer = new CountDownTimer(EIGHT_HOURS_IN_MILLISECONDS, FIFTEEN_SECONDS_IN_MILLISECONDS) { | ||||
|             @Override | ||||
|             public void onTick(long millisUntilFinished) { | ||||
|                 // update track duration | ||||
|                 // update track duration - and add duration from previously interrupted session | ||||
|                 long duration = EIGHT_HOURS_IN_MILLISECONDS - millisUntilFinished + previouslyRecordedDuration; | ||||
|                 mTrack.setDuration(duration); | ||||
|                 // try to add WayPoint to Track | ||||
|  | @ -259,11 +259,13 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven | |||
| 
 | ||||
|         // initialize step counter | ||||
|         mStepCountOffset = 0; | ||||
|         Sensor stepCounter = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER); | ||||
|         if (stepCounter != null) { | ||||
|             mSensorManager.registerListener(this, stepCounter, SensorManager.SENSOR_DELAY_UI); | ||||
| 
 | ||||
|         boolean stepCounterAvailable; | ||||
|         stepCounterAvailable = mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(TYPE_STEP_COUNTER), SensorManager.SENSOR_DELAY_UI); | ||||
|         if (stepCounterAvailable) { | ||||
|             LogHelper.v(LOG_TAG, "Pedometer sensor available: Registering listener."); | ||||
|         } else { | ||||
|             LogHelper.i(LOG_TAG, "Pedometer Sensor not available"); | ||||
|             LogHelper.i(LOG_TAG, "Pedometer sensor not available."); | ||||
|             mTrack.setStepCount(-1); | ||||
|         } | ||||
| 
 | ||||
|  | @ -315,25 +317,6 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven | |||
|     /* Adds a new WayPoint to current track */ | ||||
|     private void addWayPointToTrack() { | ||||
| 
 | ||||
|         // TODO REMOVE | ||||
|         ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE); | ||||
|         List<ActivityManager.RunningServiceInfo> l = am.getRunningServices(50); | ||||
|         Iterator<ActivityManager.RunningServiceInfo> i = l.iterator(); | ||||
|         while (i.hasNext()) { | ||||
|             ActivityManager.RunningServiceInfo runningServiceInfo = i | ||||
|                     .next(); | ||||
| 
 | ||||
|             if(runningServiceInfo.service.getClassName().contains("TrackerService")){ | ||||
| 
 | ||||
|                 if(runningServiceInfo.foreground) | ||||
|                 { | ||||
|                     LogHelper.e(LOG_TAG, "Foreground State? YES"); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|         // TODO REMOVE | ||||
| 
 | ||||
| 
 | ||||
|         // create new WayPoint | ||||
|         WayPoint newWayPoint = null; | ||||
| 
 | ||||
|  | @ -446,11 +429,9 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven | |||
| 
 | ||||
|     /* Saves state of Tracker Service and floating Action Button */ | ||||
|     private void saveTrackerServiceState(boolean trackerServiceRunning, int fabState) { | ||||
| //    private void saveTrackerServiceState(boolean trackerServiceRunning, long currentTrackDuration, int fabState) { | ||||
|         SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); | ||||
|         SharedPreferences.Editor editor = settings.edit(); | ||||
|         editor.putBoolean(PREFS_TRACKER_SERVICE_RUNNING, trackerServiceRunning); | ||||
| //        editor.putLong(PREFS_CURRENT_TRACK_DURATION, currentTrackDuration); // todo remove | ||||
|         editor.putInt(PREFS_FAB_STATE, fabState); | ||||
|         editor.apply(); | ||||
|     } | ||||
|  |  | |||
|  | @ -26,7 +26,7 @@ import android.util.Log; | |||
|  */ | ||||
| public final class LogHelper { | ||||
| 
 | ||||
|     private final static boolean mTesting = true; | ||||
|     private final static boolean mTesting = false; | ||||
| 
 | ||||
|     public static void d(final String tag, String message) { | ||||
|         // include logging only in debug versions | ||||
|  |  | |||
|  | @ -21,8 +21,8 @@ allprojects { | |||
|     } | ||||
|     project.ext { | ||||
|         applicationId = 'org.y20k.trackbook' | ||||
|         versionCode = 16 | ||||
|         versionName = '1.1.0' | ||||
|         versionCode = 17 | ||||
|         versionName = '1.1.1' | ||||
|         minSdkVersion = 22 | ||||
|         compileSdkVersion = 27 | ||||
|         targetSdkVersion = 27 | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue
	
	 y20k
						y20k