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();
/**
* @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;
}
protected abstract int getNoteId();
/**
* 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()}.
* The calling thread will be blocked until the HandlerThread created here finishes
* 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
*/
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
// a new instance if you want to start the Thread's work again.
mThread = new ChronometerNotificationThread(
@ -183,7 +169,6 @@ public abstract class ChronometerNotificationService extends Service {
mNotificationManager,
mNoteBuilder,
getResources(),
getNoteTag(),
getNoteId());
// Initializes this thread as a looper. HandlerThread.run() will be executed
// in this thread.
@ -207,7 +192,7 @@ public abstract class ChronometerNotificationService extends Service {
* @param running whether the chronometer is running
* @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,
running ? R.drawable.ic_pause_24dp : R.drawable.ic_start_24dp,
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.
* @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);
}
@ -251,7 +236,7 @@ public abstract class ChronometerNotificationService extends Service {
/**
* 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())
.setAction(action);
// 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.
*/
protected final void cancelNotification(int id/*TODO remove*/) {
protected final void cancelNotification() {
mNotificationManager.cancel(getNoteId());
}

View File

@ -29,7 +29,6 @@ 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;
@ -39,27 +38,20 @@ 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.
* @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.
* 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.
*/
public ChronometerNotificationThread(@NonNull ChronometerDelegate delegate,
@NonNull NotificationManager manager,
@NonNull NotificationCompat.Builder builder,
@Nullable Resources resources,
@NonNull String noteTag, // TODO: remove param
int noteId) {
super(TAG);
mDelegate = delegate;
mNotificationManager = manager;
mNoteBuilder = builder;
mResources = resources;
mNoteTag = noteTag;
mNoteId = noteId;
}

View File

@ -75,12 +75,6 @@ public class StopwatchNotificationService extends ChronometerNotificationService
@Override
public void 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
@ -201,7 +195,7 @@ public class StopwatchNotificationService extends ChronometerNotificationService
quitCurrentThread();
if (running) {
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;
}
@Override
protected int getNoteId() {
return R.id.timer_notification_service;
}
@Override
public void 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
// is actually quit().
// As such, try cancelling the notification with this (tag, id) pair again.
cancelNotification(mTimer.getIntId());
cancelNotification();
}
@Override
@ -158,7 +163,7 @@ public class TimerNotificationService extends ChronometerNotificationService {
quitCurrentThread();
if (running) {
startNewThread(timerId, SystemClock.elapsedRealtime() + mTimer.timeRemaining());
startNewThread(SystemClock.elapsedRealtime() + mTimer.timeRemaining());
}
}
}