Bug 1516665 - Prevent MediaControlService NPE. r=JanH, a=RyanVM
authorVlad Baicu <vlad.baicu@softvision.ro>
Fri, 01 Feb 2019 15:07:29 +0000
changeset 509635 8ec5f920524b
parent 509634 55b9be64fd88
child 509636 1499c0543d3c
push id1935
push userryanvm@gmail.com
push dateMon, 11 Feb 2019 16:39:10 +0000
treeherdermozilla-release@be2b97abeeff [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersJanH, RyanVM
bugs1516665
milestone65.0.1
Bug 1516665 - Prevent MediaControlService NPE. r=JanH, a=RyanVM Use MediaControlService's context when creating the notification in order to prevent a NPE. Differential Revision: https://phabricator.services.mozilla.com/D17391
mobile/android/base/java/org/mozilla/gecko/media/GeckoMediaControlAgent.java
mobile/android/base/java/org/mozilla/gecko/media/MediaControlService.java
--- a/mobile/android/base/java/org/mozilla/gecko/media/GeckoMediaControlAgent.java
+++ b/mobile/android/base/java/org/mozilla/gecko/media/GeckoMediaControlAgent.java
@@ -108,21 +108,21 @@ public class GeckoMediaControlAgent {
 
     private GeckoMediaControlAgent() {}
 
     public void attachToContext(Context context) {
         if (isAttachedToContext()) {
             return;
         }
 
-        mContext = context;
+        mContext = context.getApplicationContext();
         initialize();
     }
 
-    private boolean isAttachedToContext() {
+    public boolean isAttachedToContext() {
         return (mContext != null);
     }
 
     private void initialize() {
         // Need to make sure the receiver is registered and mInitialized has it's value set
         // serially, on the main thread.
         ThreadUtils.assertOnUiThread();
 
@@ -317,17 +317,17 @@ public class GeckoMediaControlAgent {
         if (!mInitialized && action.equals(ACTION_TAB_STATE_PLAYING)) {
             initialize();
         }
 
         if (!mInitialized) {
             return;
         }
 
-        Log.d(LOGTAG, "HandleAction, action = " + action + ", mediaState = " + sMediaState);
+        Log.d(LOGTAG, "handleAction, action = " + action + ", mediaState = " + sMediaState);
         switch (action) {
             case ACTION_RESUME :
                 mController.getTransportControls().play();
                 break;
             case ACTION_PAUSE :
                 mController.getTransportControls().pause();
                 break;
             case ACTION_STOP :
--- a/mobile/android/base/java/org/mozilla/gecko/media/MediaControlService.java
+++ b/mobile/android/base/java/org/mozilla/gecko/media/MediaControlService.java
@@ -34,30 +34,35 @@ public class MediaControlService extends
         }
     }
 
     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {
         Log.d(LOGTAG, "onStartCommand");
 
         if (intent.hasExtra(GeckoMediaControlAgent.EXTRA_NOTIFICATION_DATA)) {
-            currentNotification = GeckoMediaControlAgent.getInstance().createNotification(
-                    (MediaNotification) intent.getParcelableExtra(GeckoMediaControlAgent.EXTRA_NOTIFICATION_DATA));
+            if (GeckoMediaControlAgent.getInstance().isAttachedToContext()) {
+                    currentNotification = GeckoMediaControlAgent.getInstance().createNotification(
+                            (MediaNotification) intent.getParcelableExtra(GeckoMediaControlAgent.EXTRA_NOTIFICATION_DATA));
+            } else {
+                intent.setAction(GeckoMediaControlAgent.ACTION_SHUTDOWN);
+            }
         }
 
         startForeground(R.id.mediaControlNotification, currentNotification);
 
         handleAction(intent, startId);
 
         return START_NOT_STICKY;
     }
 
     private void handleAction(Intent intent, int startId) {
         if (intent.getAction() != null) {
             final String action = intent.getAction();
+            Log.d(LOGTAG, "handleAction, action = " + action);
 
             switch (action) {
                 case GeckoMediaControlAgent.ACTION_SHUTDOWN:
                     stopForeground(true);
                     stopSelfResult(startId);
                     break;
 
                 case GeckoMediaControlAgent.ACTION_STOP_FOREGROUND: