Restore getNoteTag() method

This commit is contained in:
Phillip Hsu 2016-09-17 20:57:19 -07:00
parent ced47b7fbe
commit 11e64649e7
2 changed files with 19 additions and 7 deletions

View File

@ -60,6 +60,14 @@ public abstract class ChronometerNotificationService extends Service {
*/ */
protected abstract int getNoteId(); 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. * @return whether this service should run in the foreground. The default is true.
*/ */
@ -208,7 +216,8 @@ public abstract class ChronometerNotificationService extends Service {
mNotificationManager, mNotificationManager,
mNoteBuilders.get(id), mNoteBuilders.get(id),
getResources(), getResources(),
getNoteId()); getNoteTag(),
(int) id);
mThreads.put(id, thread); mThreads.put(id, thread);
// Initializes this thread as a looper. HandlerThread.run() will be executed // Initializes this thread as a looper. HandlerThread.run() will be executed
// in this thread. // 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?*/) { protected final void cancelNotification(long id/*TODO: change to int noteId?*/) {
mNotificationManager.cancel((int) id); mNotificationManager.cancel(getNoteTag(), (int) id);
} }
/** /**

View File

@ -29,6 +29,7 @@ public class ChronometerNotificationThread extends HandlerThread {
private final NotificationManager mNotificationManager; private final NotificationManager mNotificationManager;
private final NotificationCompat.Builder mNoteBuilder; private final NotificationCompat.Builder mNoteBuilder;
private final Resources mResources; private final Resources mResources;
private final String mNoteTag;
private final int mNoteId; private final int mNoteId;
private Handler mHandler; private Handler mHandler;
@ -40,18 +41,20 @@ public class ChronometerNotificationThread extends HandlerThread {
* @param resources Required only if the ChronometerDelegate is configured to count down. * @param resources Required only if the ChronometerDelegate is configured to count down.
* Used to retrieve a String resource if/when the countdown reaches negative. * Used to retrieve a String resource if/when the countdown reaches negative.
* TODO: Will the notification be cancelled fast enough before the countdown * TODO: Will the notification be cancelled fast enough before the countdown
* becomes negative? If so, this param is rendered useless. * @param noteTag An optional tag for posting notifications.
*/ */
public ChronometerNotificationThread(@NonNull ChronometerDelegate delegate, public ChronometerNotificationThread(@NonNull ChronometerDelegate delegate,
@NonNull NotificationManager manager, @NonNull NotificationManager manager,
@NonNull NotificationCompat.Builder builder, @NonNull NotificationCompat.Builder builder,
@Nullable Resources resources, @Nullable Resources resources,
@Nullable String noteTag,
int noteId) { int noteId) {
super(TAG); super(TAG);
mDelegate = delegate; mDelegate = delegate;
mNotificationManager = manager; mNotificationManager = manager;
mNoteBuilder = builder; mNoteBuilder = builder;
mResources = resources; mResources = resources;
mNoteTag = noteTag;
mNoteId = noteId; mNoteId = noteId;
} }
@ -85,7 +88,7 @@ public class ChronometerNotificationThread extends HandlerThread {
CharSequence text = mDelegate.formatElapsedTime(SystemClock.elapsedRealtime(), mResources); CharSequence text = mDelegate.formatElapsedTime(SystemClock.elapsedRealtime(), mResources);
mNoteBuilder.setContentText(text); mNoteBuilder.setContentText(text);
} }
mNotificationManager.notify(mNoteId, mNoteBuilder.build()); mNotificationManager.notify(mNoteTag, mNoteId, mNoteBuilder.build());
} }
@Override @Override