From ee689cbb3c0abc7392d68ac5f9e902d85275eac7 Mon Sep 17 00:00:00 2001 From: Phillip Hsu Date: Fri, 16 Sep 2016 16:00:59 -0700 Subject: [PATCH] Change ChronometerNotificationService to be a foreground service --- .../ChronometerNotificationService.java | 21 ++++++++++++++----- .../clock2/ChronometerNotificationThread.java | 4 ++-- .../StopwatchNotificationService.java | 20 +++++++----------- app/src/main/res/values/ids.xml | 2 ++ 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/philliphsu/clock2/ChronometerNotificationService.java b/app/src/main/java/com/philliphsu/clock2/ChronometerNotificationService.java index 9c1f102..42364c7 100644 --- a/app/src/main/java/com/philliphsu/clock2/ChronometerNotificationService.java +++ b/app/src/main/java/com/philliphsu/clock2/ChronometerNotificationService.java @@ -48,10 +48,20 @@ public abstract class ChronometerNotificationService extends Service { * @return a tag associated with the notification. The default implementation returns the * name of this class. */ + @Deprecated protected String getNoteTag() { return getClass().getName(); } + /** + * @return the id for the foreground notification + */ + protected int getNoteId() { + // TODO: Abstract this and override in TimerNotificationService. Currently only + // StopwatchNotificationService implements this. + return 0; + } + /** * The intent received in {@link #onStartCommand(Intent, int, int)} * has no {@link Intent#getAction() action} set. At this point, you @@ -83,6 +93,7 @@ public abstract class ChronometerNotificationService extends Service { .setShowWhen(false) .setOngoing(true) .setContentIntent(getContentIntent()); + startForeground(getNoteId(), mNoteBuilder.build()); mDelegate.init(); mDelegate.setCountDown(isCountDown()); } @@ -149,7 +160,7 @@ public abstract class ChronometerNotificationService extends Service { * @param noteId the id with which the thread created here will be posting notifications. * @param base the new base time of the chronometer */ - public void startNewThread(int noteId, long base) { + public void startNewThread(int noteId/*TODO remove*/, long base) { // An instance of Thread cannot be started more than once. You must create // a new instance if you want to start the Thread's work again. mThread = new ChronometerNotificationThread( @@ -158,7 +169,7 @@ public abstract class ChronometerNotificationService extends Service { mNoteBuilder, getResources(), getNoteTag(), - noteId); + getNoteId()); // Initializes this thread as a looper. HandlerThread.run() will be executed // in this thread. // This gives you a chance to create handlers that then reference this looper, @@ -237,10 +248,10 @@ public abstract class ChronometerNotificationService extends Service { } /** - * Cancels the notification with the pair ({@link #getNoteTag() tag}, id) + * Cancels the foreground notification. */ - protected final void cancelNotification(int id) { - mNotificationManager.cancel(getNoteTag(), id); + protected final void cancelNotification(int id/*TODO remove*/) { + mNotificationManager.cancel(getNoteId()); } /** diff --git a/app/src/main/java/com/philliphsu/clock2/ChronometerNotificationThread.java b/app/src/main/java/com/philliphsu/clock2/ChronometerNotificationThread.java index fb5eb0b..542c2e9 100644 --- a/app/src/main/java/com/philliphsu/clock2/ChronometerNotificationThread.java +++ b/app/src/main/java/com/philliphsu/clock2/ChronometerNotificationThread.java @@ -52,7 +52,7 @@ public class ChronometerNotificationThread extends HandlerThread { @NonNull NotificationManager manager, @NonNull NotificationCompat.Builder builder, @Nullable Resources resources, - @NonNull String noteTag, + @NonNull String noteTag, // TODO: remove param int noteId) { super(TAG); mDelegate = delegate; @@ -93,7 +93,7 @@ public class ChronometerNotificationThread extends HandlerThread { CharSequence text = mDelegate.formatElapsedTime(SystemClock.elapsedRealtime(), mResources); mNoteBuilder.setContentText(text); } - mNotificationManager.notify(mNoteTag, mNoteId, mNoteBuilder.build()); + mNotificationManager.notify(mNoteId, mNoteBuilder.build()); } @Override 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 5ee0046..a749703 100644 --- a/app/src/main/java/com/philliphsu/clock2/stopwatch/StopwatchNotificationService.java +++ b/app/src/main/java/com/philliphsu/clock2/stopwatch/StopwatchNotificationService.java @@ -30,13 +30,11 @@ public class StopwatchNotificationService extends ChronometerNotificationService @Override public void onCreate() { super.onCreate(); + setContentTitle(getString(R.string.stopwatch)); mUpdateHandler = new AsyncLapsTableUpdateHandler(this, null); mPrefs = PreferenceManager.getDefaultSharedPreferences(this); mDelegate.init(); mDelegate.setShowCentiseconds(true, false); - setContentTitle(getString(R.string.stopwatch)); - // TODO: I think we can make this a foreground service so even - // if the process is killed, this service remains alive. } @Override @@ -82,7 +80,7 @@ public class StopwatchNotificationService extends ChronometerNotificationService // our thread has enough leeway to sneak in a final call to post the notification before it // is actually quit(). // As such, try cancelling the notification with this (tag, id) pair again. - cancelNotification(0); +// cancelNotification(0); } @Override @@ -103,6 +101,11 @@ public class StopwatchNotificationService extends ChronometerNotificationService return false; } + @Override + protected int getNoteId() { + return R.id.stopwatch_notification_service; + } + @Override protected void handleDefaultAction(Intent intent, int flags, long startId) { // TODO: Why do we need this check? Won't KEY_START_TIME always have a value of 0 here? @@ -197,17 +200,8 @@ public class StopwatchNotificationService extends ChronometerNotificationService quitCurrentThread(); if (running) { -// startChronometer(); long startTime = mPrefs.getLong(StopwatchFragment.KEY_START_TIME, SystemClock.elapsedRealtime()); startNewThread(0, startTime); } } - -// /** -// * Reads the value of KEY_START_TIME and passes it to {@link #startNewThread(int, long)} for you. -// */ -// private void startChronometer() { -// long startTime = mPrefs.getLong(StopwatchFragment.KEY_START_TIME, SystemClock.elapsedRealtime()); -// startNewThread(0, startTime); -// } } diff --git a/app/src/main/res/values/ids.xml b/app/src/main/res/values/ids.xml index 9d8efd4..ffba741 100644 --- a/app/src/main/res/values/ids.xml +++ b/app/src/main/res/values/ids.xml @@ -1,4 +1,6 @@ + + \ No newline at end of file