From 9d0b00d5e75b3909d9880061f59f11f5a0c1f64e Mon Sep 17 00:00:00 2001 From: Phillip Hsu Date: Mon, 12 Sep 2016 03:13:37 -0700 Subject: [PATCH] Send intent with stop action from StopwatchFragment to StopwatchNotificationService to remove notification when stopwatch is stopped --- .../clock2/stopwatch/StopwatchFragment.java | 31 +++++++++++++++++-- .../StopwatchNotificationService.java | 3 -- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/philliphsu/clock2/stopwatch/StopwatchFragment.java b/app/src/main/java/com/philliphsu/clock2/stopwatch/StopwatchFragment.java index 6468463..b02e118 100644 --- a/app/src/main/java/com/philliphsu/clock2/stopwatch/StopwatchFragment.java +++ b/app/src/main/java/com/philliphsu/clock2/stopwatch/StopwatchFragment.java @@ -314,7 +314,6 @@ public class StopwatchFragment extends RecyclerViewFragment< // ---------------------------------------------------------------------- mCurrentLap = null; mPreviousLap = null; - mUpdateHandler.asyncClear(); // Clear laps // No issues controlling the animator here, because onLoadFinished() can't // call through to startNewProgressBarAnimator(), because by that point // the chronometer won't be running. @@ -322,10 +321,37 @@ public class StopwatchFragment extends RecyclerViewFragment< mProgressAnimator.end(); } mProgressAnimator = null; + + // --------------------------------------------------------------------------------------- + // TOneverDO: Leave these up to StopwatchNotificationService for ACTION_STOP. + // Even though the service calls these for us, we MUST still keep these here; + // otherwise, updateAllFabs() won't restore the activity FAB icon back to the start icon. + // Possible reasons for why this happens: + // * BOTH SharedPreferences.Editor#apply() and #commit() return before the prefs are actually + // written to file, so updateAllFabs() is still reading the old value of KEY_CHRONOMETER_RUNNING. + // But doesn't #commit() block until the write is complete? + // * The SharedPreferences instances between this Fragment and the Service were created with + // different Contexts, so editing an instance applies the changes to that Context only. It + // is only until a "refresh" occurs (e.g. app restart) that the values are synced between + // Contexts? + // * Different thread execution orders between the thread used by the Service and the main + // thread here? But isn't the Service's handleStopAction() being called by the main thread? + // The only worker thread is for updating the notification periodically... + mUpdateHandler.asyncClear(); savePrefs(); + // --------------------------------------------------------------------------------------- + + // --------------------------------------------------------------------------------------- // TOneverDO: Precede savePrefs(), or else we don't save false to KEY_CHRONOMETER_RUNNING - /// and updateFab will update the wrong icon. + // and updateFab will update the wrong icon. updateAllFabs(); + // --------------------------------------------------------------------------------------- + + // Remove the notification. This will also write to prefs and clear the laps table again, + // but we probably won't notice a performance penalty... + Intent stop = new Intent(getActivity(), StopwatchNotificationService.class) + .setAction(StopwatchNotificationService.ACTION_STOP); + getActivity().startService(stop); } private void updateAllFabs() { @@ -349,6 +375,7 @@ public class StopwatchFragment extends RecyclerViewFragment< } private void updateFab() { + Log.d(TAG, "Running? " + isStopwatchRunning()); mActivityFab.get().setImageDrawable(isStopwatchRunning() ? mPauseDrawable : mStartDrawable); } diff --git a/app/src/main/java/com/philliphsu/clock2/stopwatch/StopwatchNotificationService.java b/app/src/main/java/com/philliphsu/clock2/stopwatch/StopwatchNotificationService.java index ca1457d..455ca3e 100644 --- a/app/src/main/java/com/philliphsu/clock2/stopwatch/StopwatchNotificationService.java +++ b/app/src/main/java/com/philliphsu/clock2/stopwatch/StopwatchNotificationService.java @@ -101,9 +101,6 @@ public class StopwatchNotificationService extends ChronometerNotificationService // says and tell StopwatchFragment to stop itself. The latter would also stop the // chronometer view if the fragment is still in view (i.e. app is still open). mLapsTableUpdateHandler.asyncClear(); - // TODO: When stopping the stopwatch from the fragment, send an intent to this service - // with the stop action specified. We will end up making the above calls twice, since - // they would be called in the fragment already, but that's not an issue to worry about too much. stopSelf(); }