implemented basic resume functionlity (see #33) - needs testing

master
y20k 2018-03-16 16:34:13 +01:00
parent 77479aa60d
commit b737408090
4 changed files with 45 additions and 28 deletions

View File

@ -308,13 +308,6 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
} }
/* Loads state of Floating Action Button from preferences */
private void loadFloatingActionButtonState(Context context) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
mFloatingActionButtonState = settings.getInt(PREFS_FAB_STATE, FAB_STATE_DEFAULT);
}
/* Handles tap on the button "save" */ /* Handles tap on the button "save" */
private void handleSaveButtonClick() { private void handleSaveButtonClick() {
// save button click is handled by onActivityResult in MainActivityMapFragment // save button click is handled by onActivityResult in MainActivityMapFragment
@ -340,18 +333,26 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
/* Handles tap on the button "resume" */ /* Handles tap on the button "resume" */
private void handleResumeButtonClick() { private void handleResumeButtonClick(View view) {
// todo implement // change state
mTrackerServiceRunning = true;
mFloatingActionButtonState = FAB_STATE_RECORDING;
setFloatingActionButtonState();
showFloatingActionButtonMenu(false);
// show snackbar
Snackbar.make(view, R.string.snackbar_message_tracking_resumed, Snackbar.LENGTH_SHORT).setAction("Action", null).show();
// resume tracking
startTrackerService(ACTION_RESUME, null);
} }
// /* Saves state of Floating Action Button */ // not needed tracker service saves state /* Loads state of Floating Action Button from preferences */
// private void saveFloatingActionButtonState(Context context) { private void loadFloatingActionButtonState(Context context) {
// SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
// SharedPreferences.Editor editor = settings.edit(); mFloatingActionButtonState = settings.getInt(PREFS_FAB_STATE, FAB_STATE_DEFAULT);
// editor.putInt(PREFS_FAB_STATE, mFloatingActionButtonState); }
// editor.apply();
// }
/* Set up main layout */ /* Set up main layout */
@ -455,13 +456,13 @@ public class MainActivity extends AppCompatActivity implements TrackbookKeys {
mFloatingActionButtonSubResume.setOnClickListener(new View.OnClickListener() { mFloatingActionButtonSubResume.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
handleResumeButtonClick(); handleResumeButtonClick(view);
} }
}); });
mFloatingActionButtonSubResumeLabel.setOnClickListener(new View.OnClickListener() { mFloatingActionButtonSubResumeLabel.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
handleResumeButtonClick(); handleResumeButtonClick(view);
} }
}); });

View File

@ -116,12 +116,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.w(LOG_TAG, "Null-Intent received. Trying to restart tracking."); LogHelper.w(LOG_TAG, "Null-Intent received. Trying to restart tracking.");
StorageHelper storageHelper = new StorageHelper(this); startTracking(intent, false);
if (storageHelper.tempFileExists()) {
mTrack = storageHelper.loadTrack(FILE_TEMP_TRACK);
// restart tracking
startTracking(intent, false);
}
} }
// ACTION START // ACTION START
@ -129,6 +124,11 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
startTracking(intent, true); startTracking(intent, true);
} }
// ACTION START
else if (intent.getAction().equals(ACTION_RESUME) && mLocationSystemSetting) {
startTracking(intent, false);
}
// ACTION STOP // ACTION STOP
else if (intent.getAction().equals(ACTION_STOP) || !mLocationSystemSetting) { else if (intent.getAction().equals(ACTION_STOP) || !mLocationSystemSetting) {
mTrackerServiceRunning = false; mTrackerServiceRunning = false;
@ -210,12 +210,27 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
// create a new track -- if necessary // create a new track -- if necessary
if (createNewTrack) { if (createNewTrack) {
mTrack = new Track(); mTrack = new Track();
} else {
StorageHelper storageHelper = new StorageHelper(this);
if (storageHelper.tempFileExists()) {
// load temp track file
mTrack = storageHelper.loadTrack(FILE_TEMP_TRACK);
} else {
// fallback, if temfile did not exist
LogHelper.e(LOG_TAG, "Unable to find previously saved track temp file.");
mTrack = new Track();
}
} }
// get last location // get last location
if (intent != null && intent.hasExtra(EXTRA_LAST_LOCATION)) { if (intent != null && ACTION_START.equals(intent.getAction()) && intent.hasExtra(EXTRA_LAST_LOCATION)) {
// received START intent and last location - unpack last location
mCurrentBestLocation = intent.getParcelableExtra(EXTRA_LAST_LOCATION); mCurrentBestLocation = intent.getParcelableExtra(EXTRA_LAST_LOCATION);
} else if (ACTION_RESUME.equals(intent.getAction()) && mTrack.getSize() > 0) {
// received RESUME intent - use last waypoint
mCurrentBestLocation = mTrack.getWayPointLocation(mTrack.getSize() -1);
} }
// get last location - fallback // get last location - fallback
if (mCurrentBestLocation == null) { if (mCurrentBestLocation == null) {
mCurrentBestLocation = LocationHelper.determineLastKnownLocation(mLocationManager); mCurrentBestLocation = LocationHelper.determineLastKnownLocation(mLocationManager);
@ -229,14 +244,14 @@ public class TrackerService extends Service implements TrackbookKeys, SensorEven
mNotification = NotificationHelper.getNotification(this, mNotificationBuilder, mTrack, true); mNotification = NotificationHelper.getNotification(this, mNotificationBuilder, mTrack, true);
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
// get duration of previously recorded track - in case this service has been restarted // get duration of previously recorded track - in case this service has been restarted / resumed
final long previouslyRecordedDuration = mTrack.getTrackDuration(); final long previouslyRecordedDuration = mTrack.getTrackDuration();
// set timer to retrieve new locations and to prevent endless tracking // set timer to retrieve new locations and to prevent endless tracking
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 - and add duration from previously interrupted session // update track duration - and add duration from previously interrupted / paused session
long duration = EIGHT_HOURS_IN_MILLISECONDS - millisUntilFinished + previouslyRecordedDuration; 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

View File

@ -26,6 +26,7 @@ public interface TrackbookKeys {
String ACTION_START = "org.y20k.trackbook.action.START"; String ACTION_START = "org.y20k.trackbook.action.START";
String ACTION_STOP = "org.y20k.trackbook.action.STOP"; String ACTION_STOP = "org.y20k.trackbook.action.STOP";
String ACTION_DISMISS = "org.y20k.transistor.action.DISMISS"; String ACTION_DISMISS = "org.y20k.transistor.action.DISMISS";
String ACTION_RESUME = "org.y20k.transistor.action.RESUME";
String ACTION_DEFAULT = "DEFAULT"; String ACTION_DEFAULT = "DEFAULT";
String ACTION_SHOW_MAP = "SHOW_MAP"; String ACTION_SHOW_MAP = "SHOW_MAP";
String ACTION_TRACK_UPDATED = "TRACK_UPDATED"; String ACTION_TRACK_UPDATED = "TRACK_UPDATED";

View File

@ -52,6 +52,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_gravity="bottom|end"
android:layout_marginEnd="8dp"
android:clickable="true" android:clickable="true"
android:contentDescription="@string/descr_fab_sub_menu_label_save" android:contentDescription="@string/descr_fab_sub_menu_label_save"
android:focusable="true" android:focusable="true"
@ -90,7 +91,6 @@
app:fabSize="mini" app:fabSize="mini"
app:layout_constraintBottom_toTopOf="@+id/fabSubMenuButtonResume" app:layout_constraintBottom_toTopOf="@+id/fabSubMenuButtonResume"
app:layout_constraintEnd_toEndOf="@+id/fabSubMenuButtonResume" app:layout_constraintEnd_toEndOf="@+id/fabSubMenuButtonResume"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/fabSubMenuButtonResume" app:layout_constraintStart_toStartOf="@+id/fabSubMenuButtonResume"
app:srcCompat="@drawable/ic_clear_white_24dp" /> app:srcCompat="@drawable/ic_clear_white_24dp" />