From 3cf4ca52de6331902b3257f781f261bf7c0f9b66 Mon Sep 17 00:00:00 2001 From: Michael Comella Date: Wed, 21 Oct 2015 14:19:49 -0400 Subject: [PATCH] Bug 1215353 - Move TabReceivedBroadcastReceiver notifications to IntentService. r=nalexander --HG-- rename : mobile/android/base/sync/TabReceivedBroadcastReceiver.java => mobile/android/base/sync/TabReceivedService.java extra : commitid : BiorrdiQT71 extra : rebase_source : aa51743b45fd9cfaa9b837a2ffbab8e8d6beef14 --- mobile/android/base/AndroidManifest.xml.in | 2 +- mobile/android/base/moz.build | 2 +- .../android/base/sync/CommandProcessor.java | 7 ++-- ...tReceiver.java => TabReceivedService.java} | 35 ++++++++++--------- 4 files changed, 23 insertions(+), 23 deletions(-) rename mobile/android/base/sync/{TabReceivedBroadcastReceiver.java => TabReceivedService.java} (74%) diff --git a/mobile/android/base/AndroidManifest.xml.in b/mobile/android/base/AndroidManifest.xml.in index e7cc877b8a8f..d88537eebfaf 100644 --- a/mobile/android/base/AndroidManifest.xml.in +++ b/mobile/android/base/AndroidManifest.xml.in @@ -499,7 +499,7 @@ #include ../services/manifests/HealthReportAndroidManifest_services.xml.in #include ../services/manifests/SyncAndroidManifest_services.xml.in - #ifdef MOZ_ANDROID_SEARCH_ACTIVITY diff --git a/mobile/android/base/moz.build b/mobile/android/base/moz.build index 432a6e79aa61..19af5b0f411f 100644 --- a/mobile/android/base/moz.build +++ b/mobile/android/base/moz.build @@ -472,7 +472,7 @@ gbjar.sources += [ 'sqlite/SQLiteBridgeException.java', 'SuggestClient.java', 'SurfaceBits.java', - 'sync/TabReceivedBroadcastReceiver.java', + 'sync/TabReceivedService.java', 'Tab.java', 'tabqueue/TabQueueDispatcher.java', 'tabqueue/TabQueueHelper.java', diff --git a/mobile/android/base/sync/CommandProcessor.java b/mobile/android/base/sync/CommandProcessor.java index 60060e3727d3..0d129dc4b2cb 100644 --- a/mobile/android/base/sync/CommandProcessor.java +++ b/mobile/android/base/sync/CommandProcessor.java @@ -12,7 +12,6 @@ import java.util.concurrent.atomic.AtomicInteger; import org.json.simple.JSONArray; import org.json.simple.JSONObject; -import org.mozilla.gecko.R; import org.mozilla.gecko.background.common.log.Logger; import org.mozilla.gecko.sync.repositories.NullCursorException; import org.mozilla.gecko.sync.repositories.android.ClientsDatabaseAccessor; @@ -253,9 +252,9 @@ public class CommandProcessor { title = args.get(2); } - final Intent sendTabNotificationIntent = new Intent(context, TabReceivedBroadcastReceiver.class); + final Intent sendTabNotificationIntent = new Intent(context, TabReceivedService.class); sendTabNotificationIntent.setData(Uri.parse(uri)); - sendTabNotificationIntent.putExtra(TabReceivedBroadcastReceiver.EXTRA_TITLE, title); - context.sendBroadcast(sendTabNotificationIntent); + sendTabNotificationIntent.putExtra(TabReceivedService.EXTRA_TITLE, title); + context.startService(sendTabNotificationIntent); } } diff --git a/mobile/android/base/sync/TabReceivedBroadcastReceiver.java b/mobile/android/base/sync/TabReceivedService.java similarity index 74% rename from mobile/android/base/sync/TabReceivedBroadcastReceiver.java rename to mobile/android/base/sync/TabReceivedService.java index a761ff6ccad6..87b9ca13de9e 100644 --- a/mobile/android/base/sync/TabReceivedBroadcastReceiver.java +++ b/mobile/android/base/sync/TabReceivedService.java @@ -9,9 +9,8 @@ import org.mozilla.gecko.GeckoSharedPrefs; import org.mozilla.gecko.R; import org.mozilla.gecko.tabqueue.TabQueueDispatcher; +import android.app.IntentService; import android.app.PendingIntent; -import android.content.BroadcastReceiver; -import android.content.Context; import android.content.Intent; import android.content.res.Resources; import android.content.SharedPreferences; @@ -20,14 +19,14 @@ import android.support.v4.app.NotificationManagerCompat; import android.util.Log; /** - * A BroadcastReceiver that displays a notification for a tab sent to this device. + * An IntentService that displays a notification for a tab sent to this device. * * The expected Intent should contain: * * Data: URI to open in the notification * * EXTRA_TITLE: Page title of the URI to open */ -public class TabReceivedBroadcastReceiver extends BroadcastReceiver { - private static final String LOGTAG = TabReceivedBroadcastReceiver.class.getSimpleName(); +public class TabReceivedService extends IntentService { + private static final String LOGTAG = "Gecko" + TabReceivedService.class.getSimpleName(); private static final String PREF_NOTIFICATION_ID = "tab_received_notification_id"; @@ -35,12 +34,16 @@ public class TabReceivedBroadcastReceiver extends BroadcastReceiver { private static final int MAX_NOTIFICATION_COUNT = 1000; + public TabReceivedService() { + super(LOGTAG); + } + @Override - public void onReceive(final Context context, final Intent intent) { - // BroadcastReceivers don't keep the process alive so + protected void onHandleIntent(final Intent intent) { + // IntentServices don't keep the process alive so // we need to do this every time. Ideally, we wouldn't. - final Resources res = context.getResources(); - BrowserLocaleManager.getInstance().correctLocale(context, res, res.getConfiguration()); + final Resources res = getResources(); + BrowserLocaleManager.getInstance().correctLocale(this, res, res.getConfiguration()); final String uri = intent.getDataString(); if (uri == null) { @@ -49,16 +52,16 @@ public class TabReceivedBroadcastReceiver extends BroadcastReceiver { } final String title = intent.getStringExtra(EXTRA_TITLE); - String notificationTitle = context.getString(R.string.sync_new_tab); + String notificationTitle = this.getString(R.string.sync_new_tab); if (title != null) { notificationTitle = notificationTitle.concat(": " + title); } final Intent notificationIntent = new Intent(Intent.ACTION_VIEW, intent.getData()); notificationIntent.putExtra(TabQueueDispatcher.SKIP_TAB_QUEUE_FLAG, true); - final PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); + final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); - final NotificationCompat.Builder builder = new NotificationCompat.Builder(context); + final NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.flat_icon); builder.setContentTitle(notificationTitle); builder.setWhen(System.currentTimeMillis()); @@ -66,14 +69,12 @@ public class TabReceivedBroadcastReceiver extends BroadcastReceiver { builder.setContentText(uri); builder.setContentIntent(contentIntent); - final SharedPreferences prefs = GeckoSharedPrefs.forApp(context); + final SharedPreferences prefs = GeckoSharedPrefs.forApp(this); final int notificationId = getNextNotificationId(prefs.getInt(PREF_NOTIFICATION_ID, 0)); - final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); + final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(notificationId, builder.build()); - // BroadcastReceivers do not continue async methods - // once onReceive returns so we must commit synchronously. - prefs.edit().putInt(PREF_NOTIFICATION_ID, notificationId).commit(); + prefs.edit().putInt(PREF_NOTIFICATION_ID, notificationId).apply(); } /**