Replace getNoteTag() with getNoteId(). Replace some stray hardcoded IDs with getNoteId()

This commit is contained in:
Phillip Hsu 2016-09-16 18:28:08 -07:00
parent e586c58c6b
commit ce1c56e979
4 changed files with 17 additions and 41 deletions

View File

@ -44,23 +44,10 @@ public abstract class ChronometerNotificationService extends Service {
*/ */
protected abstract boolean isCountDown(); protected abstract boolean isCountDown();
/**
* @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 * @return the id for the foreground notification
*/ */
protected int getNoteId() { protected abstract 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)} * The intent received in {@link #onStartCommand(Intent, int, int)}
@ -172,10 +159,9 @@ public abstract class ChronometerNotificationService extends Service {
* Instantiates a new HandlerThread and calls its {@link Thread#start() start()}. * Instantiates a new HandlerThread and calls its {@link Thread#start() start()}.
* The calling thread will be blocked until the HandlerThread created here finishes * The calling thread will be blocked until the HandlerThread created here finishes
* initializing its looper. * initializing its looper.
* @param noteId the id with which the thread created here will be posting notifications.
* @param base the new base time of the chronometer * @param base the new base time of the chronometer
*/ */
public void startNewThread(int noteId/*TODO remove*/, long base) { public void startNewThread(long base) {
// An instance of Thread cannot be started more than once. You must create // 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. // a new instance if you want to start the Thread's work again.
mThread = new ChronometerNotificationThread( mThread = new ChronometerNotificationThread(
@ -183,7 +169,6 @@ public abstract class ChronometerNotificationService extends Service {
mNotificationManager, mNotificationManager,
mNoteBuilder, mNoteBuilder,
getResources(), getResources(),
getNoteTag(),
getNoteId()); getNoteId());
// 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.
@ -207,7 +192,7 @@ public abstract class ChronometerNotificationService extends Service {
* @param running whether the chronometer is running * @param running whether the chronometer is running
* @param requestCode Used to create the PendingIntent that is fired when this action is clicked. * @param requestCode Used to create the PendingIntent that is fired when this action is clicked.
*/ */
protected final void addStartPauseAction(boolean running, int requestCode) { protected final void addStartPauseAction(boolean running, int requestCode/*TODO remove*/) {
addAction(ACTION_START_PAUSE, addAction(ACTION_START_PAUSE,
running ? R.drawable.ic_pause_24dp : R.drawable.ic_start_24dp, running ? R.drawable.ic_pause_24dp : R.drawable.ic_start_24dp,
getString(running ? R.string.pause : R.string.resume), getString(running ? R.string.pause : R.string.resume),
@ -218,7 +203,7 @@ public abstract class ChronometerNotificationService extends Service {
* Helper method to add the stop action to the notification's builder. * Helper method to add the stop action to the notification's builder.
* @param requestCode Used to create the PendingIntent that is fired when this action is clicked. * @param requestCode Used to create the PendingIntent that is fired when this action is clicked.
*/ */
protected final void addStopAction(int requestCode) { protected final void addStopAction(int requestCode/*TODO remove*/) {
addAction(ACTION_STOP, R.drawable.ic_stop_24dp, getString(R.string.stop), requestCode); addAction(ACTION_STOP, R.drawable.ic_stop_24dp, getString(R.string.stop), requestCode);
} }
@ -251,7 +236,7 @@ public abstract class ChronometerNotificationService extends Service {
/** /**
* Adds the specified action to the notification's Builder. * Adds the specified action to the notification's Builder.
*/ */
protected final void addAction(String action, @DrawableRes int icon, String actionTitle, int requestCode) { protected final void addAction(String action, @DrawableRes int icon, String actionTitle, int requestCode/*TODO remove*/) {
Intent intent = new Intent(this, getClass()) Intent intent = new Intent(this, getClass())
.setAction(action); .setAction(action);
// TODO: We can put the requestCode as an extra to this intent, and then retrieve that extra // TODO: We can put the requestCode as an extra to this intent, and then retrieve that extra
@ -265,7 +250,7 @@ public abstract class ChronometerNotificationService extends Service {
/** /**
* Cancels the foreground notification. * Cancels the foreground notification.
*/ */
protected final void cancelNotification(int id/*TODO remove*/) { protected final void cancelNotification() {
mNotificationManager.cancel(getNoteId()); mNotificationManager.cancel(getNoteId());
} }

View File

@ -29,7 +29,6 @@ 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;
@ -42,24 +41,17 @@ public class ChronometerNotificationThread extends HandlerThread {
* 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. * becomes negative? If so, this param is rendered useless.
* @param noteTag A tag associated with the client service, used for posting
* the notification. We require this because we want to avoid
* using a tag associated with this thread, or else the client
* service won't be able to manipulate the notifications that
* are posted from this thread.
*/ */
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,
@NonNull String noteTag, // TODO: remove param
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;
} }

View File

@ -75,12 +75,6 @@ public class StopwatchNotificationService extends ChronometerNotificationService
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
// After being cancelled due to time being up, sometimes the active timer notification posts again
// with a static 00:00 text, along with the Time's up notification. My theory is
// 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);
} }
@Override @Override
@ -201,7 +195,7 @@ public class StopwatchNotificationService extends ChronometerNotificationService
quitCurrentThread(); quitCurrentThread();
if (running) { if (running) {
long startTime = mPrefs.getLong(StopwatchFragment.KEY_START_TIME, SystemClock.elapsedRealtime()); long startTime = mPrefs.getLong(StopwatchFragment.KEY_START_TIME, SystemClock.elapsedRealtime());
startNewThread(0, startTime); startNewThread(startTime);
} }
} }
} }

View File

@ -88,6 +88,11 @@ public class TimerNotificationService extends ChronometerNotificationService {
return true; return true;
} }
@Override
protected int getNoteId() {
return R.id.timer_notification_service;
}
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
@ -96,7 +101,7 @@ public class TimerNotificationService extends ChronometerNotificationService {
// our thread has enough leeway to sneak in a final call to post the notification before it // our thread has enough leeway to sneak in a final call to post the notification before it
// is actually quit(). // is actually quit().
// As such, try cancelling the notification with this (tag, id) pair again. // As such, try cancelling the notification with this (tag, id) pair again.
cancelNotification(mTimer.getIntId()); cancelNotification();
} }
@Override @Override
@ -158,7 +163,7 @@ public class TimerNotificationService extends ChronometerNotificationService {
quitCurrentThread(); quitCurrentThread();
if (running) { if (running) {
startNewThread(timerId, SystemClock.elapsedRealtime() + mTimer.timeRemaining()); startNewThread(SystemClock.elapsedRealtime() + mTimer.timeRemaining());
} }
} }
} }