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();
/**
* @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);
}
/**

View File

@ -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