From 4eb27df911465e23df1a048cac9a0891cc53c1b2 Mon Sep 17 00:00:00 2001 From: Phillip Hsu Date: Thu, 7 Jul 2016 01:18:52 -0700 Subject: [PATCH] AsyncItemChangeHandler now checks if alarm is enabled before scheduling it --- .../clock2/AsyncItemChangeHandler.java | 32 +++++++++++++------ .../clock2/PendingAlarmScheduler.java | 3 ++ .../clock2/UpcomingAlarmReceiver.java | 1 + .../clock2/alarms/AlarmsFragment.java | 5 +-- .../philliphsu/clock2/util/AlarmUtils.java | 17 +++++++--- 5 files changed, 41 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/philliphsu/clock2/AsyncItemChangeHandler.java b/app/src/main/java/com/philliphsu/clock2/AsyncItemChangeHandler.java index cbe9de0..8286c39 100644 --- a/app/src/main/java/com/philliphsu/clock2/AsyncItemChangeHandler.java +++ b/app/src/main/java/com/philliphsu/clock2/AsyncItemChangeHandler.java @@ -43,8 +43,10 @@ public final class AsyncItemChangeHandler { // TODO: Snackbar/Toast here? If so, remove the code in AlarmUtils.scheduleAlarm() that does it. // Then, consider scheduling the alarm in the background. AlarmUtils.scheduleAlarm(mContext, alarm, true); - // Prepare to scroll to the newly added alarm - mScrollHandler.setScrollToStableId(aLong); + if (mScrollHandler != null) { + // Prepare to scroll to the newly added alarm + mScrollHandler.setScrollToStableId(aLong); + } } }.execute(); } @@ -54,6 +56,16 @@ public final class AsyncItemChangeHandler { * when we were in the edit activity. * TODO: Consider changing the signature of updateAlarm() in DatabaseManager and * AlarmDatabaseHelper to only require one Alarm param. + * TODO: The AsyncTask employed here is very similar to the one employed in + * asyncAddAlarm(). Figure out a way to refactor the code in common. Possible + * starts are to: + * * Change the Result type to Long, and then the onPostExecute() can be + * expressed the same between the two methods. + * * Similar to what you did in AlarmsFragment with the static + * inner Runnables, write a static inner abstract class that extends + * AsyncTask that takes in an Alarm; leave doInBackground() unimplemented + * in this base class. Then, define methods in this base class that subclasses + * can call to do their desired CRUD task in their doInBackground(). */ public void asyncUpdateAlarm(final Alarm newAlarm) { new AsyncTask() { @@ -66,11 +78,13 @@ public final class AsyncItemChangeHandler { protected void onPostExecute(Integer integer) { // TODO: Snackbar/Toast here? If so, remove the code in AlarmUtils.scheduleAlarm() that does it. AlarmUtils.scheduleAlarm(mContext, newAlarm, true); - // The new alarm could have a different sort order from the old alarm. - // TODO: Sometimes this won't scrolls to the new alarm if the old alarm is - // towards the bottom and the new alarm is ordered towards the top. This - // may have something to do with us breaking the stable id guarantee? - mScrollHandler.setScrollToStableId(newAlarm.id()); + if (mScrollHandler != null) { + // The new alarm could have a different sort order from the old alarm. + // TODO: Sometimes this won't scrolls to the new alarm if the old alarm is + // towards the bottom and the new alarm is ordered towards the top. This + // may have something to do with us breaking the stable id guarantee? + mScrollHandler.setScrollToStableId(newAlarm.id()); + } } }.execute(); } @@ -92,9 +106,7 @@ public final class AsyncItemChangeHandler { @Override public void onClick(View v) { DatabaseManager.getInstance(mContext).insertAlarm(alarm); - if (alarm.isEnabled()) { - AlarmUtils.scheduleAlarm(mContext, alarm, true); - } + AlarmUtils.scheduleAlarm(mContext, alarm, true); } }).show(); } diff --git a/app/src/main/java/com/philliphsu/clock2/PendingAlarmScheduler.java b/app/src/main/java/com/philliphsu/clock2/PendingAlarmScheduler.java index 5d0f129..e896f91 100644 --- a/app/src/main/java/com/philliphsu/clock2/PendingAlarmScheduler.java +++ b/app/src/main/java/com/philliphsu/clock2/PendingAlarmScheduler.java @@ -28,6 +28,9 @@ public class PendingAlarmScheduler extends BroadcastReceiver { } // TODO: Do this in the background. AsyncTask? Alarm alarm = checkNotNull(DatabaseManager.getInstance(context).getAlarm(id)); + if (!alarm.isEnabled()) { + throw new IllegalStateException("Alarm must be enabled!"); + } AlarmUtils.scheduleAlarm(context, alarm, false); } } diff --git a/app/src/main/java/com/philliphsu/clock2/UpcomingAlarmReceiver.java b/app/src/main/java/com/philliphsu/clock2/UpcomingAlarmReceiver.java index 388d8b2..6d1384a 100644 --- a/app/src/main/java/com/philliphsu/clock2/UpcomingAlarmReceiver.java +++ b/app/src/main/java/com/philliphsu/clock2/UpcomingAlarmReceiver.java @@ -35,6 +35,7 @@ public class UpcomingAlarmReceiver extends BroadcastReceiver { if (ACTION_CANCEL_NOTIFICATION.equals(intent.getAction())) { nm.cancel(getClass().getName(), (int) id); } else { + // TODO: AsyncTask/Loader Alarm alarm = checkNotNull(DatabaseManager.getInstance(context).getAlarm(id)); if (ACTION_DISMISS_NOW.equals(intent.getAction())) { AlarmUtils.cancelAlarm(context, alarm, true); diff --git a/app/src/main/java/com/philliphsu/clock2/alarms/AlarmsFragment.java b/app/src/main/java/com/philliphsu/clock2/alarms/AlarmsFragment.java index dad752f..fcb5bdd 100644 --- a/app/src/main/java/com/philliphsu/clock2/alarms/AlarmsFragment.java +++ b/app/src/main/java/com/philliphsu/clock2/alarms/AlarmsFragment.java @@ -30,9 +30,10 @@ import butterknife.ButterKnife; // TODO: Use native LoaderCallbacks. public class AlarmsFragment extends Fragment implements LoaderCallbacks, OnListItemInteractionListener, ScrollHandler { - private static final int REQUEST_EDIT_ALARM = 0; - public static final int REQUEST_CREATE_ALARM = 1; private static final String TAG = "AlarmsFragment"; + private static final int REQUEST_EDIT_ALARM = 0; + // Public because MainActivity needs to use it. + public static final int REQUEST_CREATE_ALARM = 1; private AlarmsCursorAdapter mAdapter; private AsyncItemChangeHandler mAsyncItemChangeHandler; diff --git a/app/src/main/java/com/philliphsu/clock2/util/AlarmUtils.java b/app/src/main/java/com/philliphsu/clock2/util/AlarmUtils.java index 059b0df..6ca0641 100644 --- a/app/src/main/java/com/philliphsu/clock2/util/AlarmUtils.java +++ b/app/src/main/java/com/philliphsu/clock2/util/AlarmUtils.java @@ -10,10 +10,10 @@ import android.util.Log; import android.widget.Toast; import com.philliphsu.clock2.Alarm; +import com.philliphsu.clock2.AsyncItemChangeHandler; import com.philliphsu.clock2.PendingAlarmScheduler; import com.philliphsu.clock2.R; import com.philliphsu.clock2.UpcomingAlarmReceiver; -import com.philliphsu.clock2.model.DatabaseManager; import com.philliphsu.clock2.ringtone.RingtoneActivity; import com.philliphsu.clock2.ringtone.RingtoneService; @@ -36,7 +36,17 @@ public final class AlarmUtils { private AlarmUtils() {} + /** + * Schedules the alarm with the {@link AlarmManager}. If + * {@code alarm.}{@link Alarm#isEnabled() isEnabled()} returns false, + * this does nothing and returns immediately. + */ public static void scheduleAlarm(Context context, Alarm alarm, boolean showToast) { + if (!alarm.isEnabled()) { + Log.i(TAG, "Skipped scheduling an alarm because it was not enabled"); + return; + } + Log.d(TAG, "Scheduling alarm " + alarm); AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); // If there is already an alarm for this Intent scheduled (with the equality of two @@ -194,9 +204,6 @@ public final class AlarmUtils { } private static void save(Context c, Alarm alarm) { -// AlarmsRepository.getInstance(c).saveItems(); - // Update the same alarm - // TODO: Do this in the background. AsyncTask? - DatabaseManager.getInstance(c).updateAlarm(alarm.id(), alarm); + new AsyncItemChangeHandler(c, null, null).asyncUpdateAlarm(alarm); } }