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

View File

@ -104,6 +104,10 @@ public abstract class Alarm implements JsonSerializable {
return true; return true;
} }
public void stopSnoozing() {
snoozingUntilMillis = 0;
}
public void setEnabled(boolean enabled) { public void setEnabled(boolean enabled) {
this.enabled = enabled; this.enabled = enabled;
} }

View File

@ -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 // 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. // 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 // 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)); long ringAt = alarm.isSnoozed() ? alarm.snoozingUntil() : alarm.ringsAt();
am.setExact(AlarmManager.RTC_WAKEUP, alarm.ringsAt(), alarmIntent(context, alarm, false)); // 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) { 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) { private static PendingIntent notifyUpcomingAlarmIntent(Context context, Alarm alarm, boolean retrievePrevious) {
Intent intent = new Intent(context, UpcomingAlarmReceiver.class) Intent intent = new Intent(context, UpcomingAlarmReceiver.class)
.putExtra(UpcomingAlarmReceiver.EXTRA_ALARM_ID, alarm.id()); .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; int flag = retrievePrevious ? FLAG_NO_CREATE : FLAG_CANCEL_CURRENT;
PendingIntent pi = PendingIntent.getBroadcast(context, alarm.intId(), intent, flag); PendingIntent pi = PendingIntent.getBroadcast(context, alarm.intId(), intent, flag);
if (retrievePrevious) { if (retrievePrevious) {

View File

@ -21,6 +21,7 @@ import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import android.widget.ToggleButton; import android.widget.ToggleButton;
import com.philliphsu.clock2.Alarm;
import com.philliphsu.clock2.BaseActivity; import com.philliphsu.clock2.BaseActivity;
import com.philliphsu.clock2.DaysOfWeek; import com.philliphsu.clock2.DaysOfWeek;
import com.philliphsu.clock2.R; import com.philliphsu.clock2.R;
@ -176,7 +177,6 @@ public class EditAlarmActivity extends BaseActivity implements
@OnClick(R.id.save) @OnClick(R.id.save)
void save() { void save() {
mPresenter.save(); mPresenter.save();
} }
@OnClick(R.id.delete) @OnClick(R.id.delete)
@ -387,4 +387,9 @@ public class EditAlarmActivity extends BaseActivity implements
mSwitch.setChecked(false); mSwitch.setChecked(false);
} }
} }
@Override
public void scheduleAlarm(Alarm alarm) {
AlarmUtils.scheduleAlarm(this, alarm);
}
} }

View File

@ -1,5 +1,7 @@
package com.philliphsu.clock2.editalarm; package com.philliphsu.clock2.editalarm;
import com.philliphsu.clock2.Alarm;
/** /**
* Created by Phillip Hsu on 6/2/2016. * Created by Phillip Hsu on 6/2/2016.
*/ */
@ -16,6 +18,7 @@ public interface EditAlarmContract {
void setTimeTextHint(); void setTimeTextHint();
void showTimeText(String timeText); void showTimeText(String timeText);
void showTimeTextPostBackspace(String newStr); void showTimeTextPostBackspace(String newStr);
void scheduleAlarm(Alarm alarm);
int getHour(); int getHour();
int getMinutes(); int getMinutes();
boolean isEnabled(); boolean isEnabled();

View File

@ -74,6 +74,13 @@ public class EditAlarmPresenter implements EditAlarmContract.Presenter {
mRepository.addItem(a); 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(); mView.showEditorClosed();
} }

View File

@ -1,9 +1,6 @@
package com.philliphsu.clock2.ringtone; package com.philliphsu.clock2.ringtone;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent; import android.content.Intent;
import android.media.RingtoneManager;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.view.View; import android.view.View;
@ -11,7 +8,6 @@ import android.widget.Button;
import com.philliphsu.clock2.Alarm; import com.philliphsu.clock2.Alarm;
import com.philliphsu.clock2.R; import com.philliphsu.clock2.R;
import com.philliphsu.clock2.UpcomingAlarmReceiver;
import com.philliphsu.clock2.editalarm.AlarmUtils; import com.philliphsu.clock2.editalarm.AlarmUtils;
import com.philliphsu.clock2.model.AlarmsRepository; import com.philliphsu.clock2.model.AlarmsRepository;
@ -41,13 +37,13 @@ public class RingtoneActivity extends AppCompatActivity {
} }
mAlarm = checkNotNull(AlarmsRepository.getInstance(this).getItem(id)); mAlarm = checkNotNull(AlarmsRepository.getInstance(this).getItem(id));
AlarmUtils.removeUpcomingAlarmNotification(this, mAlarm);
// Play the ringtone // Play the ringtone
Intent intent = new Intent(this, RingtoneService.class) Intent intent = new Intent(this, RingtoneService.class)
.putExtra(EXTRA_ITEM_ID, mAlarm.id()); .putExtra(EXTRA_ITEM_ID, mAlarm.id());
startService(intent); startService(intent);
AlarmUtils.removeUpcomingAlarmNotification(this, mAlarm);
Button snooze = (Button) findViewById(R.id.btn_snooze); Button snooze = (Button) findViewById(R.id.btn_snooze);
snooze.setOnClickListener(new View.OnClickListener() { snooze.setOnClickListener(new View.OnClickListener() {
@Override @Override
@ -90,6 +86,8 @@ public class RingtoneActivity extends AppCompatActivity {
private void snooze() { private void snooze() {
// Schedule another launch // Schedule another launch
AlarmUtils.scheduleAlarm(this, mAlarm);
/*
Intent intent = new Intent(this, RingtoneActivity.class) Intent intent = new Intent(this, RingtoneActivity.class)
.setData(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)); .setData(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM));
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
@ -99,6 +97,7 @@ public class RingtoneActivity extends AppCompatActivity {
Intent intent2 = new Intent(this, UpcomingAlarmReceiver.class) Intent intent2 = new Intent(this, UpcomingAlarmReceiver.class)
.setAction(UpcomingAlarmReceiver.ACTION_SHOW_SNOOZING); .setAction(UpcomingAlarmReceiver.ACTION_SHOW_SNOOZING);
sendBroadcast(intent2); sendBroadcast(intent2);
*/
dismiss(); dismiss();
} }