Alarms now scheduled after saving in edit screen

This commit is contained in:
Phillip Hsu
2016-06-03 14:58:57 -07:00
parent 49b7d80185
commit 5fb8448ce3
6 changed files with 35 additions and 9 deletions
@@ -39,8 +39,10 @@ public final class AlarmUtils {
// We use a WAKEUP alarm to send the upcoming alarm notification so it goes off even if the
// device is asleep. Otherwise, it will not go off until the device is turned back on.
// todo: read shared prefs for number of hours to be notified in advance
am.set(AlarmManager.RTC_WAKEUP, alarm.ringsAt() - 2*3600000, notifyUpcomingAlarmIntent(context, alarm, false));
am.setExact(AlarmManager.RTC_WAKEUP, alarm.ringsAt(), alarmIntent(context, alarm, false));
long ringAt = alarm.isSnoozed() ? alarm.snoozingUntil() : alarm.ringsAt();
// If snoozed, upcoming note posted immediately.
am.set(AlarmManager.RTC_WAKEUP, ringAt - 2*3600000, notifyUpcomingAlarmIntent(context, alarm, false));
am.setExact(AlarmManager.RTC_WAKEUP, ringAt, alarmIntent(context, alarm, false));
}
public static void unscheduleAlarm(Context c, Alarm a) {
@@ -79,6 +81,12 @@ public final class AlarmUtils {
private static PendingIntent notifyUpcomingAlarmIntent(Context context, Alarm alarm, boolean retrievePrevious) {
Intent intent = new Intent(context, UpcomingAlarmReceiver.class)
.putExtra(UpcomingAlarmReceiver.EXTRA_ALARM_ID, alarm.id());
if (alarm.isSnoozed()) {
// TODO: Will this affect retrieving a previous instance? Say if the previous instance
// didn't have this action set initially, but at a later time we made a new instance
// with it set.
intent.setAction(UpcomingAlarmReceiver.ACTION_SHOW_SNOOZING);
}
int flag = retrievePrevious ? FLAG_NO_CREATE : FLAG_CANCEL_CURRENT;
PendingIntent pi = PendingIntent.getBroadcast(context, alarm.intId(), intent, flag);
if (retrievePrevious) {
@@ -21,6 +21,7 @@ import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import com.philliphsu.clock2.Alarm;
import com.philliphsu.clock2.BaseActivity;
import com.philliphsu.clock2.DaysOfWeek;
import com.philliphsu.clock2.R;
@@ -176,7 +177,6 @@ public class EditAlarmActivity extends BaseActivity implements
@OnClick(R.id.save)
void save() {
mPresenter.save();
}
@OnClick(R.id.delete)
@@ -387,4 +387,9 @@ public class EditAlarmActivity extends BaseActivity implements
mSwitch.setChecked(false);
}
}
@Override
public void scheduleAlarm(Alarm alarm) {
AlarmUtils.scheduleAlarm(this, alarm);
}
}
@@ -1,5 +1,7 @@
package com.philliphsu.clock2.editalarm;
import com.philliphsu.clock2.Alarm;
/**
* Created by Phillip Hsu on 6/2/2016.
*/
@@ -16,6 +18,7 @@ public interface EditAlarmContract {
void setTimeTextHint();
void showTimeText(String timeText);
void showTimeTextPostBackspace(String newStr);
void scheduleAlarm(Alarm alarm);
int getHour();
int getMinutes();
boolean isEnabled();
@@ -74,6 +74,13 @@ public class EditAlarmPresenter implements EditAlarmContract.Presenter {
mRepository.addItem(a);
}
if (a.isEnabled()) {
// TODO: Consider passing in some interface during construction that abstracts away the
// Context required to call AlarmUtils.scheduleAlarm(), so we can call it here instead.
// It doesn't seem right that this task is delegated to the View.
mView.scheduleAlarm(a);
}
mView.showEditorClosed();
}