mirror of
https://gitlab.winehq.org/wine/wine-gecko.git
synced 2024-09-13 09:24:08 -07:00
Bug 845080 - Extract BackgroundService superclass. r=rnewman
This commit is contained in:
parent
3c1825ec7d
commit
7c0f56b9ab
@ -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 \
|
||||
|
73
mobile/android/base/background/BackgroundService.java
Normal file
73
mobile/android/base/background/BackgroundService.java
Normal file
@ -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<? extends BroadcastReceiver> 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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user