Reschedule alarms after booting up

This commit is contained in:
Phillip Hsu 2016-06-14 21:41:14 -07:00
parent 1edd0ce026
commit d19d32fa86
5 changed files with 159 additions and 10 deletions

View File

@ -3,6 +3,7 @@
xmlns:android="http://schemas.android.com/apk/res/android"> xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.VIBRATE"/> <uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application <application
android:allowBackup="true" android:allowBackup="true"
@ -63,6 +64,20 @@
android:enabled="true" android:enabled="true"
android:exported="false"> android:exported="false">
</receiver> </receiver>
<receiver
android:name=".OnBootUpReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service
android:name=".OnBootUpAlarmScheduler"
android:enabled="true"
android:exported="false">
</service>
</application> </application>
</manifest> </manifest>

View File

@ -0,0 +1,101 @@
package com.philliphsu.clock2;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import com.philliphsu.clock2.model.AlarmsRepository;
import com.philliphsu.clock2.util.AlarmUtils;
import java.util.List;
/**
* An {@link IntentService} subclass for handling asynchronous task requests in
* a service on a separate handler thread.
*/
public class OnBootUpAlarmScheduler extends IntentService {
// TODO: Rename actions, choose action names that describe tasks that this
// IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS
private static final String ACTION_FOO = "com.philliphsu.clock2.action.FOO";
private static final String ACTION_BAZ = "com.philliphsu.clock2.action.BAZ";
// TODO: Rename parameters
private static final String EXTRA_PARAM1 = "com.philliphsu.clock2.extra.PARAM1";
private static final String EXTRA_PARAM2 = "com.philliphsu.clock2.extra.PARAM2";
public OnBootUpAlarmScheduler() {
super("OnBootUpAlarmScheduler");
}
/**
* Starts this service to perform action Foo with the given parameters. If
* the service is already performing a task this action will be queued.
*
* @see IntentService
*/
// TODO: Customize helper method
public static void startActionFoo(Context context, String param1, String param2) {
Intent intent = new Intent(context, OnBootUpAlarmScheduler.class);
intent.setAction(ACTION_FOO);
intent.putExtra(EXTRA_PARAM1, param1);
intent.putExtra(EXTRA_PARAM2, param2);
context.startService(intent);
}
/**
* Starts this service to perform action Baz with the given parameters. If
* the service is already performing a task this action will be queued.
*
* @see IntentService
*/
// TODO: Customize helper method
public static void startActionBaz(Context context, String param1, String param2) {
Intent intent = new Intent(context, OnBootUpAlarmScheduler.class);
intent.setAction(ACTION_BAZ);
intent.putExtra(EXTRA_PARAM1, param1);
intent.putExtra(EXTRA_PARAM2, param2);
context.startService(intent);
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
List<Alarm> alarms = AlarmsRepository.getInstance(this).getItems();
for (Alarm a : alarms) {
if (a.isEnabled()) {
AlarmUtils.scheduleAlarm(this, a, false);
}
}
/* final String action = intent.getAction();
if (ACTION_FOO.equals(action)) {
final String param1 = intent.getStringExtra(EXTRA_PARAM1);
final String param2 = intent.getStringExtra(EXTRA_PARAM2);
handleActionFoo(param1, param2);
} else if (ACTION_BAZ.equals(action)) {
final String param1 = intent.getStringExtra(EXTRA_PARAM1);
final String param2 = intent.getStringExtra(EXTRA_PARAM2);
handleActionBaz(param1, param2);
}
*/
}
}
/**
* Handle action Foo in the provided background thread with the provided
* parameters.
*/
private void handleActionFoo(String param1, String param2) {
// TODO: Handle action Foo
throw new UnsupportedOperationException("Not yet implemented");
}
/**
* Handle action Baz in the provided background thread with the provided
* parameters.
*/
private void handleActionBaz(String param1, String param2) {
// TODO: Handle action Baz
throw new UnsupportedOperationException("Not yet implemented");
}
}

View File

@ -0,0 +1,18 @@
package com.philliphsu.clock2;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
* If the device is turned off, all alarms scheduled will be cancelled, and they will not be automatically
* rescheduled when it is turned on again.
*/
public class OnBootUpReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Note that this will be called when the device boots up, not when the app first launches.
// We may have a lot of alarms to reschedule, so do this in the background using an IntentService.
context.startService(new Intent(context, OnBootUpAlarmScheduler.class));
}
}

View File

@ -16,6 +16,7 @@ import com.philliphsu.clock2.Alarm;
import com.philliphsu.clock2.R; import com.philliphsu.clock2.R;
import com.philliphsu.clock2.model.AlarmsRepository; import com.philliphsu.clock2.model.AlarmsRepository;
import com.philliphsu.clock2.util.AlarmUtils; import com.philliphsu.clock2.util.AlarmUtils;
import com.philliphsu.clock2.util.LocalBroadcastHelper;
import static com.philliphsu.clock2.util.Preconditions.checkNotNull; import static com.philliphsu.clock2.util.Preconditions.checkNotNull;
@ -97,9 +98,7 @@ public class RingtoneActivity extends AppCompatActivity {
//super.onNewIntent(intent); // Not needed since no fragments hosted? //super.onNewIntent(intent); // Not needed since no fragments hosted?
// Notifies alarm missed and stops the service // Notifies alarm missed and stops the service
// TODO: Find a better, cleaner way? Starting the service just to set a flag and stop itself LocalBroadcastHelper.sendBroadcast(this, RingtoneService.ACTION_NOTIFY_MISSED);
// is not very clear from this code. Perhaps the same Broadcast concept as done here.
startService(new Intent(this, RingtoneService.class).setAction(RingtoneService.ACTION_NOTIFY_MISSED));
finish(); finish();
startActivity(intent); startActivity(intent);
} }

View File

@ -4,7 +4,10 @@ import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager; import android.media.AudioManager;
import android.media.Ringtone; import android.media.Ringtone;
import android.media.RingtoneManager; import android.media.RingtoneManager;
@ -15,6 +18,7 @@ import android.os.Vibrator;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log; import android.util.Log;
import com.philliphsu.clock2.Alarm; import com.philliphsu.clock2.Alarm;
@ -65,6 +69,15 @@ public class RingtoneService extends Service { // TODO: abstract this, make subc
stopSelf(); stopSelf();
} }
}; };
private final BroadcastReceiver mNotifyMissedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
mAutoSilenced = true;
// TODO: Do we need to call AlarmUtils.cancelAlarm()?
stopSelf();
// Activity finishes itself
}
};
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
@ -73,13 +86,8 @@ public class RingtoneService extends Service { // TODO: abstract this, make subc
throw new IllegalStateException("No item id set"); throw new IllegalStateException("No item id set");
Alarm alarm = checkNotNull(AlarmsRepository.getInstance(this).getItem(id)); Alarm alarm = checkNotNull(AlarmsRepository.getInstance(this).getItem(id));
// TODO: Refactor to use switch block if (intent.getAction() == null) {
if (intent.getAction() == null || intent.getAction().isEmpty()) {
playRingtone(alarm); playRingtone(alarm);
} else if (ACTION_NOTIFY_MISSED.equals(intent.getAction())) {
mAutoSilenced = true;
stopSelf(startId);
// Activity finishes itself
} else { } else {
if (ACTION_SNOOZE.equals(intent.getAction())) { if (ACTION_SNOOZE.equals(intent.getAction())) {
AlarmUtils.snoozeAlarm(this, alarm); AlarmUtils.snoozeAlarm(this, alarm);
@ -96,6 +104,13 @@ public class RingtoneService extends Service { // TODO: abstract this, make subc
return START_NOT_STICKY; // If killed while started, don't recreate. Should be sufficient. return START_NOT_STICKY; // If killed while started, don't recreate. Should be sufficient.
} }
@Override
public void onCreate() {
super.onCreate();
LocalBroadcastManager.getInstance(this).registerReceiver(
mNotifyMissedReceiver, new IntentFilter(ACTION_NOTIFY_MISSED));
}
@Override @Override
public void onDestroy() { public void onDestroy() {
Log.d(TAG, "onDestroy()"); Log.d(TAG, "onDestroy()");
@ -120,6 +135,7 @@ public class RingtoneService extends Service { // TODO: abstract this, make subc
nm.notify(getClass().getName(), mAlarm.intId(), note); nm.notify(getClass().getName(), mAlarm.intId(), note);
} }
stopForeground(true); stopForeground(true);
LocalBroadcastManager.getInstance(this).unregisterReceiver(mNotifyMissedReceiver);
} }
@Override @Override
@ -187,7 +203,7 @@ public class RingtoneService extends Service { // TODO: abstract this, make subc
// doing this in the respective subclass of this service. // doing this in the respective subclass of this service.
private void scheduleAutoSilence() { private void scheduleAutoSilence() {
int minutes = AlarmUtils.minutesToSilenceAfter(this); int minutes = AlarmUtils.minutesToSilenceAfter(this);
mSilenceHandler.postDelayed(mSilenceRunnable, /*minutes * 60000*/10000); // TODO: uncomment mSilenceHandler.postDelayed(mSilenceRunnable, /*minutes * 60000*/70000); // TODO: uncomment
} }
private void finishActivity() { private void finishActivity() {