Alarms now scheduled after saving in edit screen
This commit is contained in:
parent
49b7d80185
commit
5fb8448ce3
@ -104,6 +104,10 @@ public abstract class Alarm implements JsonSerializable {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void stopSnoozing() {
|
||||
snoozingUntilMillis = 0;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
|
||||
@ -1,9 +1,6 @@
|
||||
package com.philliphsu.clock2.ringtone;
|
||||
|
||||
import android.app.AlarmManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Intent;
|
||||
import android.media.RingtoneManager;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.view.View;
|
||||
@ -11,7 +8,6 @@ import android.widget.Button;
|
||||
|
||||
import com.philliphsu.clock2.Alarm;
|
||||
import com.philliphsu.clock2.R;
|
||||
import com.philliphsu.clock2.UpcomingAlarmReceiver;
|
||||
import com.philliphsu.clock2.editalarm.AlarmUtils;
|
||||
import com.philliphsu.clock2.model.AlarmsRepository;
|
||||
|
||||
@ -41,13 +37,13 @@ public class RingtoneActivity extends AppCompatActivity {
|
||||
}
|
||||
mAlarm = checkNotNull(AlarmsRepository.getInstance(this).getItem(id));
|
||||
|
||||
AlarmUtils.removeUpcomingAlarmNotification(this, mAlarm);
|
||||
|
||||
// Play the ringtone
|
||||
Intent intent = new Intent(this, RingtoneService.class)
|
||||
.putExtra(EXTRA_ITEM_ID, mAlarm.id());
|
||||
startService(intent);
|
||||
|
||||
AlarmUtils.removeUpcomingAlarmNotification(this, mAlarm);
|
||||
|
||||
Button snooze = (Button) findViewById(R.id.btn_snooze);
|
||||
snooze.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
@ -90,6 +86,8 @@ public class RingtoneActivity extends AppCompatActivity {
|
||||
|
||||
private void snooze() {
|
||||
// Schedule another launch
|
||||
AlarmUtils.scheduleAlarm(this, mAlarm);
|
||||
/*
|
||||
Intent intent = new Intent(this, RingtoneActivity.class)
|
||||
.setData(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM));
|
||||
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)
|
||||
.setAction(UpcomingAlarmReceiver.ACTION_SHOW_SNOOZING);
|
||||
sendBroadcast(intent2);
|
||||
*/
|
||||
dismiss();
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user