From 11e64649e77c942c3483026aa50ae93ae7e9cc51 Mon Sep 17 00:00:00 2001 From: Phillip Hsu Date: Sat, 17 Sep 2016 20:57:19 -0700 Subject: [PATCH] Restore getNoteTag() method --- .../clock2/ChronometerNotificationService.java | 15 ++++++++++++--- .../clock2/ChronometerNotificationThread.java | 11 +++++++---- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/philliphsu/clock2/ChronometerNotificationService.java b/app/src/main/java/com/philliphsu/clock2/ChronometerNotificationService.java index d4b1f4d..91b4aea 100644 --- a/app/src/main/java/com/philliphsu/clock2/ChronometerNotificationService.java +++ b/app/src/main/java/com/philliphsu/clock2/ChronometerNotificationService.java @@ -60,6 +60,14 @@ public abstract class ChronometerNotificationService extends Service { */ protected abstract int getNoteId(); + /** + * @return an optional tag associated with the notification(s). The default implementation + * returns null if {@link #isForeground()} returns true; otherwise, it returns the class's name. + */ + protected String getNoteTag() { + return isForeground() ? null : getClass().getName(); + } + /** * @return whether this service should run in the foreground. The default is true. */ @@ -208,7 +216,8 @@ public abstract class ChronometerNotificationService extends Service { mNotificationManager, mNoteBuilders.get(id), getResources(), - getNoteId()); + getNoteTag(), + (int) id); mThreads.put(id, thread); // Initializes this thread as a looper. HandlerThread.run() will be executed // in this thread. @@ -307,10 +316,10 @@ public abstract class ChronometerNotificationService extends Service { } /** - * Cancels the notification associated with the ID. + * Cancels the notification with the pair ({@link #getNoteTag() tag}, id) */ protected final void cancelNotification(long id/*TODO: change to int noteId?*/) { - mNotificationManager.cancel((int) id); + mNotificationManager.cancel(getNoteTag(), (int) id); } /** diff --git a/app/src/main/java/com/philliphsu/clock2/ChronometerNotificationThread.java b/app/src/main/java/com/philliphsu/clock2/ChronometerNotificationThread.java index 5b745db..83429fc 100644 --- a/app/src/main/java/com/philliphsu/clock2/ChronometerNotificationThread.java +++ b/app/src/main/java/com/philliphsu/clock2/ChronometerNotificationThread.java @@ -29,6 +29,7 @@ public class ChronometerNotificationThread extends HandlerThread { private final NotificationManager mNotificationManager; private final NotificationCompat.Builder mNoteBuilder; private final Resources mResources; + private final String mNoteTag; private final int mNoteId; private Handler mHandler; @@ -38,20 +39,22 @@ public class ChronometerNotificationThread extends HandlerThread { * @param builder A preconfigured Builder from the client service whose content * text will be updated and eventually built from. * @param resources Required only if the ChronometerDelegate is configured to count down. - * Used to retrieve a String resource if/when the countdown reaches negative. - * TODO: Will the notification be cancelled fast enough before the countdown - * becomes negative? If so, this param is rendered useless. +* Used to retrieve a String resource if/when the countdown reaches negative. +* TODO: Will the notification be cancelled fast enough before the countdown + * @param noteTag An optional tag for posting notifications. */ public ChronometerNotificationThread(@NonNull ChronometerDelegate delegate, @NonNull NotificationManager manager, @NonNull NotificationCompat.Builder builder, @Nullable Resources resources, + @Nullable String noteTag, int noteId) { super(TAG); mDelegate = delegate; mNotificationManager = manager; mNoteBuilder = builder; mResources = resources; + mNoteTag = noteTag; mNoteId = noteId; } @@ -85,7 +88,7 @@ public class ChronometerNotificationThread extends HandlerThread { CharSequence text = mDelegate.formatElapsedTime(SystemClock.elapsedRealtime(), mResources); mNoteBuilder.setContentText(text); } - mNotificationManager.notify(mNoteId, mNoteBuilder.build()); + mNotificationManager.notify(mNoteTag, mNoteId, mNoteBuilder.build()); } @Override