Skip to content

Commit

Permalink
Reset the logger alarm on phone reboot.
Browse files Browse the repository at this point in the history
  • Loading branch information
JayThomason committed May 28, 2014
1 parent a408691 commit ef11bc7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
18 changes: 18 additions & 0 deletions src/com/stanford/tutti/BootReceiver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.stanford.tutti;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BootReceiver extends BroadcastReceiver {

LoggerAlarmReceiver alarm = new LoggerAlarmReceiver();

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
alarm.setAlarm(context, true);
}
}
}
2 changes: 1 addition & 1 deletion src/com/stanford/tutti/Globals.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void onCreate() {
discoveryManager = new DiscoveryManager(this);
logger = new Logger(this);
loggerAlarm = new LoggerAlarmReceiver();
loggerAlarm.setAlarm(context);
loggerAlarm.setAlarm(context, false);
}

public static Context getAppContext() {
Expand Down
18 changes: 10 additions & 8 deletions src/com/stanford/tutti/LoggerAlarmReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.preference.PreferenceManager;
import android.support.v4.content.WakefulBroadcastReceiver;

Expand Down Expand Up @@ -71,17 +73,17 @@ public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Thr

}

public void setAlarm(Context context) {
public void setAlarm(Context context, boolean forceAlarmReset) {
System.out.println("set alarm called in logger alarm");
alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, LoggerAlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 1234, intent, 0);

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
preferences.edit().putBoolean(ALARM_SET_FLAG, false).apply();

// only set alarm if not already set!
// note: this call still returns not null even if the alarm has been canceled -- bad for testing
if (!preferences.getBoolean(ALARM_SET_FLAG, false)) {

if (!preferences.getBoolean(ALARM_SET_FLAG, false) || forceAlarmReset) {
Random random = new Random();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
Expand All @@ -96,15 +98,15 @@ public void setAlarm(Context context) {

preferences.edit().putBoolean(ALARM_SET_FLAG, true).apply();

/*
// Enable {@code SampleBootReceiver} to automatically restart the alarm when the

// Enable {@code BootReceiver} to automatically restart the alarm when the
// device is rebooted.
ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
*/

}
else {
alarmManager.cancel(alarmIntent);
Expand Down

0 comments on commit ef11bc7

Please sign in to comment.