diff --git a/mobile/android/base/android-services-files.mk b/mobile/android/base/android-services-files.mk index 375fab4a220..47d814f44a1 100644 --- a/mobile/android/base/android-services-files.mk +++ b/mobile/android/base/android-services-files.mk @@ -20,6 +20,7 @@ SYNC_JAVA_FILES := \ background/announcements/AnnouncementsService.java \ background/announcements/AnnouncementsStartReceiver.java \ background/BackgroundConstants.java \ + background/BackgroundService.java \ sync/AlreadySyncingException.java \ sync/CollectionKeys.java \ sync/CommandProcessor.java \ diff --git a/mobile/android/base/background/BackgroundService.java b/mobile/android/base/background/BackgroundService.java new file mode 100644 index 00000000000..5391b4ec60e --- /dev/null +++ b/mobile/android/base/background/BackgroundService.java @@ -0,0 +1,73 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.gecko.background; + +import org.mozilla.gecko.sync.Logger; + +import android.app.AlarmManager; +import android.app.IntentService; +import android.app.PendingIntent; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; +import android.os.Build; + +public abstract class BackgroundService extends IntentService { + private static final String LOG_TAG = BackgroundService.class.getSimpleName(); + + protected BackgroundService() { + super(LOG_TAG); + } + + protected BackgroundService(String threadName) { + super(threadName); + } + + /** + * Returns true if the OS will allow us to perform background + * data operations. This logic varies by OS version. + */ + protected boolean backgroundDataIsEnabled() { + ConnectivityManager connectivity = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) { + return connectivity.getBackgroundDataSetting(); + } + NetworkInfo networkInfo = connectivity.getActiveNetworkInfo(); + if (networkInfo == null) { + return false; + } + return networkInfo.isAvailable(); + } + + protected static PendingIntent createPendingIntent(Context context, Class broadcastReceiverClass) { + final Intent service = new Intent(context, broadcastReceiverClass); + return PendingIntent.getBroadcast(context, 0, service, PendingIntent.FLAG_CANCEL_CURRENT); + } + + protected AlarmManager getAlarmManager() { + return getAlarmManager(this.getApplicationContext()); + } + + protected static AlarmManager getAlarmManager(Context context) { + return (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); + } + + protected void scheduleAlarm(long pollInterval, PendingIntent pendingIntent) { + Logger.info(LOG_TAG, "Setting inexact repeating alarm for interval " + pollInterval); + if (pollInterval <= 0) { + throw new IllegalArgumentException("pollInterval " + pollInterval + " must be positive"); + } + final AlarmManager alarm = getAlarmManager(); + final long firstEvent = System.currentTimeMillis(); + alarm.setInexactRepeating(AlarmManager.RTC, firstEvent, pollInterval, pendingIntent); + } + + protected void cancelAlarm(PendingIntent pendingIntent) { + final AlarmManager alarm = getAlarmManager(); + alarm.cancel(pendingIntent); + } +} diff --git a/mobile/android/base/background/announcements/AnnouncementsBroadcastService.java b/mobile/android/base/background/announcements/AnnouncementsBroadcastService.java index 1e5786df7af..5ad6d49d70f 100644 --- a/mobile/android/base/background/announcements/AnnouncementsBroadcastService.java +++ b/mobile/android/base/background/announcements/AnnouncementsBroadcastService.java @@ -8,11 +8,11 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import org.mozilla.gecko.background.BackgroundConstants; +import org.mozilla.gecko.background.BackgroundService; import org.mozilla.gecko.sync.GlobalConstants; import org.mozilla.gecko.sync.Logger; import android.app.AlarmManager; -import android.app.IntentService; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; @@ -24,7 +24,7 @@ import android.content.SharedPreferences.Editor; * browser, registering or unregistering the main * {@link AnnouncementsStartReceiver} with the {@link AlarmManager}. */ -public class AnnouncementsBroadcastService extends IntentService { +public class AnnouncementsBroadcastService extends BackgroundService { private static final String WORKER_THREAD_NAME = "AnnouncementsBroadcastServiceWorker"; private static final String LOG_TAG = "AnnounceBrSvc"; @@ -34,24 +34,16 @@ public class AnnouncementsBroadcastService extends IntentService { private void toggleAlarm(final Context context, boolean enabled) { Logger.info(LOG_TAG, (enabled ? "R" : "Unr") + "egistering announcements broadcast receiver..."); - final AlarmManager alarm = getAlarmManager(context); - final Intent service = new Intent(context, AnnouncementsStartReceiver.class); - final PendingIntent pending = PendingIntent.getBroadcast(context, 0, service, PendingIntent.FLAG_CANCEL_CURRENT); + final PendingIntent pending = createPendingIntent(context, AnnouncementsStartReceiver.class); if (!enabled) { - alarm.cancel(pending); + cancelAlarm(pending); return; } - final long firstEvent = System.currentTimeMillis(); final long pollInterval = getPollInterval(context); - Logger.info(LOG_TAG, "Setting inexact repeating alarm for interval " + pollInterval); - alarm.setInexactRepeating(AlarmManager.RTC, firstEvent, pollInterval, pending); - } - - private static AlarmManager getAlarmManager(Context context) { - return (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); + scheduleAlarm(pollInterval, pending); } /** @@ -198,4 +190,4 @@ public class AnnouncementsBroadcastService extends IntentService { editor.commit(); } } -} \ No newline at end of file +} diff --git a/mobile/android/base/background/announcements/AnnouncementsService.java b/mobile/android/base/background/announcements/AnnouncementsService.java index d15fd9b487c..5b60c1f335d 100644 --- a/mobile/android/base/background/announcements/AnnouncementsService.java +++ b/mobile/android/base/background/announcements/AnnouncementsService.java @@ -11,15 +11,11 @@ import java.util.List; import java.util.Locale; import org.mozilla.gecko.background.BackgroundConstants; +import org.mozilla.gecko.background.BackgroundService; import org.mozilla.gecko.sync.Logger; -import android.app.IntentService; -import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; -import android.net.ConnectivityManager; -import android.net.NetworkInfo; -import android.os.Build; import android.os.IBinder; /** @@ -52,7 +48,7 @@ import android.os.IBinder; * * Persisting of multiple announcements. * * Prioritization. */ -public class AnnouncementsService extends IntentService implements AnnouncementsFetchDelegate { +public class AnnouncementsService extends BackgroundService implements AnnouncementsFetchDelegate { private static final String WORKER_THREAD_NAME = "AnnouncementsServiceWorker"; private static final String LOG_TAG = "AnnounceService"; @@ -141,22 +137,6 @@ public class AnnouncementsService extends IntentService implements Announcements return null; } - /** - * Returns true if the OS will allow us to perform background - * data operations. This logic varies by OS version. - */ - protected boolean backgroundDataIsEnabled() { - ConnectivityManager connectivity = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) { - return connectivity.getBackgroundDataSetting(); - } - NetworkInfo networkInfo = connectivity.getActiveNetworkInfo(); - if (networkInfo == null) { - return false; - } - return networkInfo.isAvailable(); - } - protected long getLastLaunch() { return getSharedPreferences().getLong(AnnouncementsConstants.PREF_LAST_LAUNCH, 0); } diff --git a/mobile/android/services/java-sources.mn b/mobile/android/services/java-sources.mn index 80d883b45c3..9688169e07a 100644 --- a/mobile/android/services/java-sources.mn +++ b/mobile/android/services/java-sources.mn @@ -8,6 +8,7 @@ background/announcements/AnnouncementsFetchResourceDelegate.java background/announcements/AnnouncementsService.java background/announcements/AnnouncementsStartReceiver.java background/BackgroundConstants.java +background/BackgroundService.java sync/AlreadySyncingException.java sync/CollectionKeys.java sync/CommandProcessor.java