AsyncItemChangeHandler now checks if alarm is enabled before scheduling it
This commit is contained in:
parent
3a86aaf3ba
commit
4eb27df911
@ -43,8 +43,10 @@ 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);
|
||||||
// Prepare to scroll to the newly added alarm
|
if (mScrollHandler != null) {
|
||||||
mScrollHandler.setScrollToStableId(aLong);
|
// Prepare to scroll to the newly added alarm
|
||||||
|
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,11 +78,13 @@ 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);
|
||||||
// The new alarm could have a different sort order from the old alarm.
|
if (mScrollHandler != null) {
|
||||||
// TODO: Sometimes this won't scrolls to the new alarm if the old alarm is
|
// The new alarm could have a different sort order from the old alarm.
|
||||||
// towards the bottom and the new alarm is ordered towards the top. This
|
// TODO: Sometimes this won't scrolls to the new alarm if the old alarm is
|
||||||
// may have something to do with us breaking the stable id guarantee?
|
// towards the bottom and the new alarm is ordered towards the top. This
|
||||||
mScrollHandler.setScrollToStableId(newAlarm.id());
|
// may have something to do with us breaking the stable id guarantee?
|
||||||
|
mScrollHandler.setScrollToStableId(newAlarm.id());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}.execute();
|
}.execute();
|
||||||
}
|
}
|
||||||
@ -92,9 +106,7 @@ 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();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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);
|
||||||
|
|||||||
@ -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;
|
||||||
|
|||||||
@ -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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user