AsyncItemChangeHandler now checks if alarm is enabled before scheduling it

This commit is contained in:
Phillip Hsu 2016-07-07 01:18:52 -07:00
parent 3a86aaf3ba
commit 4eb27df911
5 changed files with 41 additions and 17 deletions

View File

@ -43,9 +43,11 @@ public final class AsyncItemChangeHandler {
// TODO: Snackbar/Toast here? If so, remove the code in AlarmUtils.scheduleAlarm() that does it. // TODO: Snackbar/Toast here? If so, remove the code in AlarmUtils.scheduleAlarm() that does it.
// Then, consider scheduling the alarm in the background. // Then, consider scheduling the alarm in the background.
AlarmUtils.scheduleAlarm(mContext, alarm, true); AlarmUtils.scheduleAlarm(mContext, alarm, true);
if (mScrollHandler != null) {
// Prepare to scroll to the newly added alarm // Prepare to scroll to the newly added alarm
mScrollHandler.setScrollToStableId(aLong); mScrollHandler.setScrollToStableId(aLong);
} }
}
}.execute(); }.execute();
} }
@ -54,6 +56,16 @@ public final class AsyncItemChangeHandler {
* when we were in the edit activity. * when we were in the edit activity.
* TODO: Consider changing the signature of updateAlarm() in DatabaseManager and * TODO: Consider changing the signature of updateAlarm() in DatabaseManager and
* AlarmDatabaseHelper to only require one Alarm param. * 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) { public void asyncUpdateAlarm(final Alarm newAlarm) {
new AsyncTask<Void, Void, Integer>() { new AsyncTask<Void, Void, Integer>() {
@ -66,12 +78,14 @@ public final class AsyncItemChangeHandler {
protected void onPostExecute(Integer integer) { protected void onPostExecute(Integer integer) {
// TODO: Snackbar/Toast here? If so, remove the code in AlarmUtils.scheduleAlarm() that does it. // TODO: Snackbar/Toast here? If so, remove the code in AlarmUtils.scheduleAlarm() that does it.
AlarmUtils.scheduleAlarm(mContext, newAlarm, true); AlarmUtils.scheduleAlarm(mContext, newAlarm, true);
if (mScrollHandler != null) {
// The new alarm could have a different sort order from the old alarm. // 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 // 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 // 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? // may have something to do with us breaking the stable id guarantee?
mScrollHandler.setScrollToStableId(newAlarm.id()); mScrollHandler.setScrollToStableId(newAlarm.id());
} }
}
}.execute(); }.execute();
} }
@ -92,10 +106,8 @@ public final class AsyncItemChangeHandler {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
DatabaseManager.getInstance(mContext).insertAlarm(alarm); DatabaseManager.getInstance(mContext).insertAlarm(alarm);
if (alarm.isEnabled()) {
AlarmUtils.scheduleAlarm(mContext, alarm, true); AlarmUtils.scheduleAlarm(mContext, alarm, true);
} }
}
}).show(); }).show();
} }
} }

View File

@ -28,6 +28,9 @@ public class PendingAlarmScheduler extends BroadcastReceiver {
} }
// TODO: Do this in the background. AsyncTask? // TODO: Do this in the background. AsyncTask?
Alarm alarm = checkNotNull(DatabaseManager.getInstance(context).getAlarm(id)); Alarm alarm = checkNotNull(DatabaseManager.getInstance(context).getAlarm(id));
if (!alarm.isEnabled()) {
throw new IllegalStateException("Alarm must be enabled!");
}
AlarmUtils.scheduleAlarm(context, alarm, false); AlarmUtils.scheduleAlarm(context, alarm, false);
} }
} }

View File

@ -35,6 +35,7 @@ public class UpcomingAlarmReceiver extends BroadcastReceiver {
if (ACTION_CANCEL_NOTIFICATION.equals(intent.getAction())) { if (ACTION_CANCEL_NOTIFICATION.equals(intent.getAction())) {
nm.cancel(getClass().getName(), (int) id); nm.cancel(getClass().getName(), (int) id);
} else { } else {
// TODO: AsyncTask/Loader
Alarm alarm = checkNotNull(DatabaseManager.getInstance(context).getAlarm(id)); Alarm alarm = checkNotNull(DatabaseManager.getInstance(context).getAlarm(id));
if (ACTION_DISMISS_NOW.equals(intent.getAction())) { if (ACTION_DISMISS_NOW.equals(intent.getAction())) {
AlarmUtils.cancelAlarm(context, alarm, true); AlarmUtils.cancelAlarm(context, alarm, true);

View File

@ -30,9 +30,10 @@ import butterknife.ButterKnife;
// TODO: Use native LoaderCallbacks. // TODO: Use native LoaderCallbacks.
public class AlarmsFragment extends Fragment implements LoaderCallbacks<Cursor>, public class AlarmsFragment extends Fragment implements LoaderCallbacks<Cursor>,
OnListItemInteractionListener<Alarm>, ScrollHandler { OnListItemInteractionListener<Alarm>, 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 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 AlarmsCursorAdapter mAdapter;
private AsyncItemChangeHandler mAsyncItemChangeHandler; private AsyncItemChangeHandler mAsyncItemChangeHandler;

View File

@ -10,10 +10,10 @@ import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import com.philliphsu.clock2.Alarm; import com.philliphsu.clock2.Alarm;
import com.philliphsu.clock2.AsyncItemChangeHandler;
import com.philliphsu.clock2.PendingAlarmScheduler; import com.philliphsu.clock2.PendingAlarmScheduler;
import com.philliphsu.clock2.R; import com.philliphsu.clock2.R;
import com.philliphsu.clock2.UpcomingAlarmReceiver; import com.philliphsu.clock2.UpcomingAlarmReceiver;
import com.philliphsu.clock2.model.DatabaseManager;
import com.philliphsu.clock2.ringtone.RingtoneActivity; import com.philliphsu.clock2.ringtone.RingtoneActivity;
import com.philliphsu.clock2.ringtone.RingtoneService; import com.philliphsu.clock2.ringtone.RingtoneService;
@ -36,7 +36,17 @@ public final class AlarmUtils {
private 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) { 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); Log.d(TAG, "Scheduling alarm " + alarm);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// If there is already an alarm for this Intent scheduled (with the equality of two // 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) { private static void save(Context c, Alarm alarm) {
// AlarmsRepository.getInstance(c).saveItems(); new AsyncItemChangeHandler(c, null, null).asyncUpdateAlarm(alarm);
// Update the same alarm
// TODO: Do this in the background. AsyncTask?
DatabaseManager.getInstance(c).updateAlarm(alarm.id(), alarm);
} }
} }