Reset alarm on reboot and app upgrade

This commit is contained in:
daniel oeh 2013-03-14 11:47:40 +01:00
parent fbb4772132
commit d0bfd7c02d
3 changed files with 64 additions and 19 deletions

View File

@ -23,7 +23,9 @@
<uses-feature
android:name="android.hardware.screen.portrait"
android:required="false" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name="de.danoeh.antennapod.PodcastApp"
@ -348,11 +350,23 @@
android:configChanges="orientation"
android:label="@string/organize_queue_label" >
</activity>
<receiver android:name=".receiver.ConnectivityActionReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<receiver android:name=".receiver.AlarmUpdateReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data
android:path="de.danoeh.antennapod"
android:scheme="package" />
</intent-filter>
</receiver>
</application>
</manifest>

View File

@ -198,11 +198,9 @@ public class UserPreferences implements
followQueue = sp.getBoolean(PREF_FOLLOW_QUEUE, false);
} else if (key.equals(PREF_UPDATE_INTERVAL)) {
int hours = Integer.parseInt(sp
.getString(PREF_UPDATE_INTERVAL, "0"));
updateInterval = readUpdateInterval(sp
.getString(PREF_UPDATE_INTERVAL, "0"));
restartUpdateAlarm(hours);
updateInterval = readUpdateInterval(sp.getString(
PREF_UPDATE_INTERVAL, "0"));
restartUpdateAlarm(updateInterval);
} else if (key.equals(PREF_AUTO_DELETE)) {
autoDelete = sp.getBoolean(PREF_AUTO_DELETE, false);
@ -347,20 +345,23 @@ public class UserPreferences implements
/**
* Updates alarm registered with the AlarmManager service or deactivates it.
*
* @param hours
* new value to register with AlarmManager. If hours is 0, the
* @param millis
* new value to register with AlarmManager. If millis is 0, the
* alarm is deactivated.
* */
public void restartUpdateAlarm(int hours) {
AlarmManager alarmManager = (AlarmManager) context
public static void restartUpdateAlarm(long millis) {
instanceAvailable();
if (AppConfig.DEBUG)
Log.d(TAG, "Restarting update alarm. New value: " + millis);
AlarmManager alarmManager = (AlarmManager) instance.context
.getSystemService(Context.ALARM_SERVICE);
PendingIntent updateIntent = PendingIntent.getBroadcast(context, 0,
new Intent(FeedUpdateReceiver.ACTION_REFRESH_FEEDS), 0);
PendingIntent updateIntent = PendingIntent.getBroadcast(
instance.context, 0, new Intent(
FeedUpdateReceiver.ACTION_REFRESH_FEEDS), 0);
alarmManager.cancel(updateIntent);
if (hours != 0) {
long newIntervall = TimeUnit.HOURS.toMillis(hours);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, newIntervall,
newIntervall, updateIntent);
if (millis != 0) {
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, millis, millis,
updateIntent);
if (AppConfig.DEBUG)
Log.d(TAG, "Changed alarm to new intervall");
} else {

View File

@ -0,0 +1,30 @@
package de.danoeh.antennapod.receiver;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.preferences.UserPreferences;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/** Listens for events that make it necessary to reset the update alarm. */
public class AlarmUpdateReceiver extends BroadcastReceiver {
private static final String TAG = "AlarmUpdateReceiver";
@Override
public void onReceive(Context context, Intent intent) {
if (AppConfig.DEBUG)
Log.d(TAG, "Received intent");
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
if (AppConfig.DEBUG)
Log.d(TAG, "Resetting update alarm after reboot");
} else if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
if (AppConfig.DEBUG)
Log.d(TAG, "Resetting update alarm after app upgrade");
}
UserPreferences.restartUpdateAlarm(UserPreferences.getUpdateInterval());
}
}