Bug 1215353 - Move TabReceivedBroadcastReceiver notifications to IntentService. r=nalexander
authorMichael Comella <michael.l.comella@gmail.com>
Wed, 21 Oct 2015 14:19:49 -0400
changeset 304761 b404b452db2bbc78f132e5d265c18441ec137b9a
parent 304760 b842ef4420f1b247ab9706ce8d5028c8bca52ecd
child 304762 81d08a1039202a9966009ea282dea10a69bf68d2
push id1001
push userraliiev@mozilla.com
push dateMon, 18 Jan 2016 19:06:03 +0000
treeherdermozilla-release@8b89261f3ac4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnalexander
bugs1215353
milestone44.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1215353 - Move TabReceivedBroadcastReceiver notifications to IntentService. r=nalexander
mobile/android/base/AndroidManifest.xml.in
mobile/android/base/moz.build
mobile/android/base/sync/CommandProcessor.java
mobile/android/base/sync/TabReceivedBroadcastReceiver.java
mobile/android/base/sync/TabReceivedService.java
--- a/mobile/android/base/AndroidManifest.xml.in
+++ b/mobile/android/base/AndroidManifest.xml.in
@@ -494,17 +494,17 @@
             android:name="org.mozilla.gecko.NotificationService">
         </service>
 
 
 #include ../services/manifests/FxAccountAndroidManifest_services.xml.in
 #include ../services/manifests/HealthReportAndroidManifest_services.xml.in
 #include ../services/manifests/SyncAndroidManifest_services.xml.in
 
-        <receiver android:name="org.mozilla.gecko.sync.TabReceivedBroadcastReceiver"
+        <service android:name="org.mozilla.gecko.sync.TabReceivedService"
                   android:exported="false"/>
 
 #ifdef MOZ_ANDROID_SEARCH_ACTIVITY
 #include ../search/manifests/SearchAndroidManifest_services.xml.in
 #endif
 #ifdef MOZ_ANDROID_MLS_STUMBLER
 #include ../stumbler/manifests/StumblerManifest_services.xml.in
 #endif
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -467,17 +467,17 @@ gbjar.sources += [
     'SiteIdentity.java',
     'SmsManager.java',
     'sqlite/ByteBufferInputStream.java',
     'sqlite/MatrixBlobCursor.java',
     'sqlite/SQLiteBridge.java',
     'sqlite/SQLiteBridgeException.java',
     'SuggestClient.java',
     'SurfaceBits.java',
-    'sync/TabReceivedBroadcastReceiver.java',
+    'sync/TabReceivedService.java',
     'Tab.java',
     'tabqueue/TabQueueDispatcher.java',
     'tabqueue/TabQueueHelper.java',
     'tabqueue/TabQueuePrompt.java',
     'tabqueue/TabQueueService.java',
     'Tabs.java',
     'tabs/PrivateTabsPanel.java',
     'tabs/TabCurve.java',
--- a/mobile/android/base/sync/CommandProcessor.java
+++ b/mobile/android/base/sync/CommandProcessor.java
@@ -7,17 +7,16 @@ package org.mozilla.gecko.sync;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 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;
 import org.mozilla.gecko.sync.repositories.domain.ClientRecord;
 
 import android.content.Context;
 import android.content.Intent;
 import android.net.Uri;
@@ -248,14 +247,14 @@ public class CommandProcessor {
       return;
     }
 
     String title = null;
     if (args.size() == 3) {
       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);
   }
 }
rename from mobile/android/base/sync/TabReceivedBroadcastReceiver.java
rename to mobile/android/base/sync/TabReceivedService.java
--- a/mobile/android/base/sync/TabReceivedBroadcastReceiver.java
+++ b/mobile/android/base/sync/TabReceivedService.java
@@ -4,81 +4,82 @@
 
 package org.mozilla.gecko.sync;
 
 import org.mozilla.gecko.BrowserLocaleManager;
 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;
 import android.support.v4.app.NotificationCompat;
 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";
 
     public static final String EXTRA_TITLE = "org.mozilla.gecko.extra.TITLE";
 
     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) {
             Log.d(LOGTAG, "Received null uri – ignoring");
             return;
         }
 
         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());
         builder.setAutoCancel(true);
         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();
     }
 
     /**
      * Notification IDs must be unique else a notification
      * will be overwritten so we cycle them.
      */
     private int getNextNotificationId(final int currentId) {
         if (currentId > MAX_NOTIFICATION_COUNT) {