saving the duration in shared preferences introduced problems - now using duration within track object (see #29)

This commit is contained in:
y20k 2018-01-21 22:54:34 +01:00
parent 9e46a09401
commit adc68605a4
2 changed files with 18 additions and 20 deletions

View file

@ -275,9 +275,9 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
intent.putExtra(EXTRA_LAST_LOCATION, lastLocation); intent.putExtra(EXTRA_LAST_LOCATION, lastLocation);
} }
// start service // communicate with service
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && intentAction.equals(ACTION_START)) {
// ... in foreground to prevent it being killed on Oreo // ... start service in foreground to prevent it being killed on Oreo
startForegroundService(intent); startForegroundService(intent);
} else { } else {
startService(intent); startService(intent);

View file

@ -115,7 +115,7 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
// RESTART CHECK: checking for empty intent - try to get saved track // RESTART CHECK: checking for empty intent - try to get saved track
if (intent == null || intent.getAction() == null) { if (intent == null || intent.getAction() == null) {
LogHelper.e(LOG_TAG, "Null-Intent received. Are we being restarted?"); LogHelper.w(LOG_TAG, "Null-Intent received. Trying to restart tracking.");
StorageHelper storageHelper = new StorageHelper(this); StorageHelper storageHelper = new StorageHelper(this);
if (storageHelper.tempFileExists()) { if (storageHelper.tempFileExists()) {
mTrack = storageHelper.loadTrack(FILE_TEMP_TRACK); mTrack = storageHelper.loadTrack(FILE_TEMP_TRACK);
@ -136,14 +136,14 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
stopTracking(); stopTracking();
} else { } else {
// handle error - save state // handle error - save state
saveTrackerServiceState(mTrackerServiceRunning, 0, FAB_STATE_DEFAULT); saveTrackerServiceState(mTrackerServiceRunning, FAB_STATE_DEFAULT);
} }
} }
// ACTION DISMISS // ACTION DISMISS
else if (intent.getAction().equals(ACTION_DISMISS)) { else if (intent.getAction().equals(ACTION_DISMISS)) {
// save state // save state
saveTrackerServiceState(mTrackerServiceRunning, mTrack.getTrackDuration(), FAB_STATE_DEFAULT); saveTrackerServiceState(mTrackerServiceRunning, FAB_STATE_DEFAULT);
// dismiss notification // dismiss notification
mNotificationManager.cancel(TRACKER_SERVICE_NOTIFICATION_ID); // todo check if necessary? mNotificationManager.cancel(TRACKER_SERVICE_NOTIFICATION_ID); // todo check if necessary?
stopForeground(true); stopForeground(true);
@ -230,19 +230,23 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
mNotificationManager.notify(TRACKER_SERVICE_NOTIFICATION_ID, mNotification); // todo check if necessary in pre Android O mNotificationManager.notify(TRACKER_SERVICE_NOTIFICATION_ID, mNotification); // todo check if necessary in pre Android O
// set timer to retrieve new locations and to prevent endless tracking // set timer to retrieve new locations and to prevent endless tracking
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); // SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
final long previouslyRecorded = settings.getLong(PREFS_CURRENT_TRACK_DURATION, 0);// todo describe // 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) { mTimer = new CountDownTimer(EIGHT_HOURS_IN_MILLISECONDS, FIFTEEN_SECONDS_IN_MILLISECONDS) {
@Override @Override
public void onTick(long millisUntilFinished) { public void onTick(long millisUntilFinished) {
// update track duration // update track duration
long duration = EIGHT_HOURS_IN_MILLISECONDS - millisUntilFinished + previouslyRecorded; long duration = EIGHT_HOURS_IN_MILLISECONDS - millisUntilFinished + previouslyRecordedDuration;
mTrack.setDuration(duration); mTrack.setDuration(duration);
// try to add WayPoint to Track // try to add WayPoint to Track
addWayPointToTrack(); addWayPointToTrack();
// update notification // update notification
mNotification = NotificationHelper.getUpdatedNotification(TrackerService.this, mNotificationBuilder, mTrack); mNotification = NotificationHelper.getUpdatedNotification(TrackerService.this, mNotificationBuilder, mTrack);
mNotificationManager.notify(TRACKER_SERVICE_NOTIFICATION_ID, mNotification); mNotificationManager.notify(TRACKER_SERVICE_NOTIFICATION_ID, mNotification);
// save a temp file in case the service has been killed by the system
SaveTempTrackAsyncHelper saveTempTrackAsyncHelper = new SaveTempTrackAsyncHelper();
saveTempTrackAsyncHelper.execute();
} }
@Override @Override
@ -358,18 +362,11 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
// if new, add current best location to track // if new, add current best location to track
newWayPoint = mTrack.addWayPoint(mCurrentBestLocation); newWayPoint = mTrack.addWayPoint(mCurrentBestLocation);
} }
// save state
saveTrackerServiceState(mTrackerServiceRunning, mTrack.getTrackDuration(), FAB_STATE_RECORDING);
} }
// send local broadcast if new WayPoint added // send local broadcast if new WayPoint added
if (newWayPoint != null) { if (newWayPoint != null) {
sendTrackUpdate(); sendTrackUpdate();
// save a temp file in case the service has been killed by the system
SaveTempTrackAsyncHelper saveTempTrackAsyncHelper = new SaveTempTrackAsyncHelper();
saveTempTrackAsyncHelper.execute();
} }
} }
@ -427,7 +424,7 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
mTrackerServiceRunning = true; mTrackerServiceRunning = true;
} }
LocationHelper.registerLocationListeners(mLocationManager, mGPSListener, mNetworkListener); LocationHelper.registerLocationListeners(mLocationManager, mGPSListener, mNetworkListener);
saveTrackerServiceState(mTrackerServiceRunning, mTrack.getTrackDuration(), FAB_STATE_RECORDING); saveTrackerServiceState(mTrackerServiceRunning, FAB_STATE_RECORDING);
} }
@ -436,7 +433,7 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
// remove listeners // remove listeners
LocationHelper.removeLocationListeners(mLocationManager, mGPSListener, mNetworkListener); LocationHelper.removeLocationListeners(mLocationManager, mGPSListener, mNetworkListener);
mTrackerServiceRunning = false; mTrackerServiceRunning = false;
saveTrackerServiceState(mTrackerServiceRunning, mTrack.getTrackDuration(),FAB_STATE_SAVE); saveTrackerServiceState(mTrackerServiceRunning, FAB_STATE_SAVE);
// notify MainActivityMapFragment // notify MainActivityMapFragment
Intent i = new Intent(); Intent i = new Intent();
@ -448,11 +445,12 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
/* Saves state of Tracker Service and floating Action Button */ /* Saves state of Tracker Service and floating Action Button */
private void saveTrackerServiceState(boolean trackerServiceRunning, long currentTrackDuration, int fabState) { private void saveTrackerServiceState(boolean trackerServiceRunning, int fabState) {
// private void saveTrackerServiceState(boolean trackerServiceRunning, long currentTrackDuration, int fabState) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = settings.edit(); SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(PREFS_TRACKER_SERVICE_RUNNING, trackerServiceRunning); editor.putBoolean(PREFS_TRACKER_SERVICE_RUNNING, trackerServiceRunning);
editor.putLong(PREFS_CURRENT_TRACK_DURATION, currentTrackDuration); // editor.putLong(PREFS_CURRENT_TRACK_DURATION, currentTrackDuration); // todo remove
editor.putInt(PREFS_FAB_STATE, fabState); editor.putInt(PREFS_FAB_STATE, fabState);
editor.apply(); editor.apply();
} }