Pass alarm directly to AlarmActivity and AlarmRingtoneService
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
package com.philliphsu.clock2.alarms;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.content.Loader;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
|
||||
import com.philliphsu.clock2.Alarm;
|
||||
import com.philliphsu.clock2.R;
|
||||
import com.philliphsu.clock2.model.AlarmLoader;
|
||||
import com.philliphsu.clock2.ringtone.RingtoneActivity;
|
||||
import com.philliphsu.clock2.ringtone.RingtoneService;
|
||||
import com.philliphsu.clock2.util.AlarmController;
|
||||
|
||||
public class AlarmActivity extends RingtoneActivity<Alarm> {
|
||||
@@ -20,7 +19,13 @@ public class AlarmActivity extends RingtoneActivity<Alarm> {
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
if ((mAlarm = getIntent().getParcelableExtra(EXTRA_ITEM)) == null) {
|
||||
throw new IllegalStateException("Cannot start AlarmActivity without an Alarm");
|
||||
}
|
||||
mAlarmController = new AlarmController(this, null);
|
||||
// TODO: If the upcoming alarm notification isn't present, verify other notifications aren't affected.
|
||||
// This could be the case if we're starting a new instance of this activity after leaving the first launch.
|
||||
mAlarmController.removeUpcomingAlarmNotification(mAlarm);
|
||||
// TODO: Butterknife binding
|
||||
Button snooze = (Button) findViewById(R.id.btn_snooze);
|
||||
snooze.setOnClickListener(new View.OnClickListener() {
|
||||
@@ -38,27 +43,32 @@ public class AlarmActivity extends RingtoneActivity<Alarm> {
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Loader<Alarm> onCreateLoader(long id) {
|
||||
return new AlarmLoader(this, id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadFinished(Loader<Alarm> loader, Alarm data) {
|
||||
super.onLoadFinished(loader, data);
|
||||
mAlarm = data;
|
||||
if (data != null) {
|
||||
// TODO: If the upcoming alarm notification isn't present, verify other notifications aren't affected.
|
||||
// This could be the case if we're starting a new instance of this activity after leaving the first launch.
|
||||
mAlarmController.removeUpcomingAlarmNotification(data);
|
||||
}
|
||||
}
|
||||
// @Override
|
||||
// public Loader<Alarm> onCreateLoader(long id) {
|
||||
// return new AlarmLoader(this, id);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onLoadFinished(Loader<Alarm> loader, Alarm data) {
|
||||
// super.onLoadFinished(loader, data);
|
||||
// mAlarm = data;
|
||||
// if (data != null) {
|
||||
// // TODO: If the upcoming alarm notification isn't present, verify other notifications aren't affected.
|
||||
// // This could be the case if we're starting a new instance of this activity after leaving the first launch.
|
||||
// mAlarmController.removeUpcomingAlarmNotification(data);
|
||||
// }
|
||||
// }
|
||||
|
||||
@Override
|
||||
public int layoutResource() {
|
||||
return R.layout.activity_ringtone;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<? extends RingtoneService> getRingtoneServiceClass() {
|
||||
return AlarmRingtoneService.class;
|
||||
}
|
||||
|
||||
private void snooze() {
|
||||
if (mAlarm != null) {
|
||||
mAlarmController.snoozeAlarm(mAlarm);
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
package com.philliphsu.clock2.alarms;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationManager;
|
||||
import android.content.Intent;
|
||||
import android.media.Ringtone;
|
||||
import android.media.RingtoneManager;
|
||||
import android.net.Uri;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
|
||||
import com.philliphsu.clock2.Alarm;
|
||||
import com.philliphsu.clock2.R;
|
||||
import com.philliphsu.clock2.ringtone.RingtoneService;
|
||||
import com.philliphsu.clock2.util.AlarmController;
|
||||
import com.philliphsu.clock2.util.AlarmUtils;
|
||||
|
||||
import static com.philliphsu.clock2.util.DateFormatUtils.formatTime;
|
||||
|
||||
public class AlarmRingtoneService extends RingtoneService<Alarm> {
|
||||
private static final String TAG = "AlarmRingtoneService";
|
||||
/* TOneverDO: not private */
|
||||
private static final String ACTION_SNOOZE = "com.philliphsu.clock2.ringtone.action.SNOOZE";
|
||||
private static final String ACTION_DISMISS = "com.philliphsu.clock2.ringtone.action.DISMISS";
|
||||
|
||||
private String mNormalRingTime;
|
||||
private AlarmController mAlarmController;
|
||||
private Alarm mAlarm;
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
// TOneverDO: Call super before our custom logic
|
||||
if (intent.getAction() == null) {
|
||||
// final long id = intent.getLongExtra(EXTRA_ITEM_ID, -1);
|
||||
// if (id < 0)
|
||||
// throw new IllegalStateException("No item id set");
|
||||
// http://stackoverflow.com/q/8696146/5055032
|
||||
// Start our own thread to load the alarm instead of using a loader,
|
||||
// because Services do not have a built-in LoaderManager (because they have no need for one since
|
||||
// their lifecycle is not complex like in Activities/Fragments) and our
|
||||
// work is simple enough that getting loaders to work here is not
|
||||
// worth the effort.
|
||||
// // TODO: Will using the Runnable like this cause a memory leak?
|
||||
// new Thread(new Runnable() {
|
||||
// @Override
|
||||
// public void run() {
|
||||
// // TODO: We don't actually need the exact same Alarm instance as the
|
||||
// // one from our calling component, because we won't mutate any of its
|
||||
// // fields. Since we only read values, we could just pass in the Alarm
|
||||
// // to the intent as a Parcelable.
|
||||
// AlarmCursor cursor = new AlarmsTableManager(AlarmRingtoneService.this).queryItem(id);
|
||||
// mAlarm = checkNotNull(cursor.getItem());
|
||||
// }
|
||||
// }).start();
|
||||
if ((mAlarm = intent.getParcelableExtra(EXTRA_ITEM)) == null) {
|
||||
throw new IllegalStateException("Cannot start AlarmRingtoneService without an Alarm");
|
||||
}
|
||||
} else {
|
||||
if (ACTION_SNOOZE.equals(intent.getAction())) {
|
||||
mAlarmController.snoozeAlarm(mAlarm);
|
||||
} else if (ACTION_DISMISS.equals(intent.getAction())) {
|
||||
mAlarmController.cancelAlarm(mAlarm, false); // TODO do we really need to cancel the intent and alarm?
|
||||
} else {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
// ==========================================================================
|
||||
stopSelf(startId);
|
||||
finishActivity();
|
||||
}
|
||||
return super.onStartCommand(intent, flags, startId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
mAlarmController = new AlarmController(this, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onAutoSilenced() {
|
||||
// TODO do we really need to cancel the alarm and intent?
|
||||
mAlarmController.cancelAlarm(mAlarm, false);
|
||||
// Post notification that alarm was missed
|
||||
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
||||
Notification note = new NotificationCompat.Builder(this)
|
||||
.setContentTitle(getString(R.string.missed_alarm))
|
||||
.setContentText(mNormalRingTime)
|
||||
.setSmallIcon(R.mipmap.ic_launcher)
|
||||
.build();
|
||||
nm.notify(TAG, mAlarm.intId(), note);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Ringtone getRingtone() {
|
||||
Uri ringtone = Uri.parse(mAlarm.ringtone());
|
||||
return RingtoneManager.getRingtone(this, ringtone);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Notification getForegroundNotification() {
|
||||
String title = mAlarm.label().isEmpty()
|
||||
? getString(R.string.alarm)
|
||||
: mAlarm.label();
|
||||
mNormalRingTime = formatTime(this, System.currentTimeMillis()); // now
|
||||
return new NotificationCompat.Builder(this)
|
||||
// Required contents
|
||||
.setSmallIcon(R.mipmap.ic_launcher) // TODO: alarm icon
|
||||
.setContentTitle(title)
|
||||
.setContentText(mNormalRingTime)
|
||||
.addAction(R.mipmap.ic_launcher,
|
||||
getString(R.string.snooze),
|
||||
getPendingIntent(ACTION_SNOOZE, mAlarm))
|
||||
.addAction(R.mipmap.ic_launcher,
|
||||
getString(R.string.dismiss),
|
||||
getPendingIntent(ACTION_DISMISS, mAlarm))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean doesVibrate() {
|
||||
return mAlarm.vibrates();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int minutesToAutoSilence() {
|
||||
return AlarmUtils.minutesToSilenceAfter(this);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user