Bug 1373887 - Catch exception to handle creating media session fail. r=sebastian, a=jcristau
authorAlastor Wu <alwu@mozilla.com>
Tue, 11 Jul 2017 14:01:53 +0800
changeset 411978 589b643648db507f01d4c68f3234138567bdcb58
parent 411977 dc00e73c06e10974b8e88e8ed1a7b918e256e2d0
child 411979 60fdd29667adb68cbe84c7a2dbe53dbfee63e7e6
push id7514
push userryanvm@gmail.com
push dateMon, 17 Jul 2017 13:37:24 +0000
treeherdermozilla-beta@e26b1f5d635e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssebastian, jcristau
bugs1373887
milestone55.0
Bug 1373887 - Catch exception to handle creating media session fail. r=sebastian, a=jcristau The crash reason is that our package name [1] is not the same as the name from packageManager's getPackagesForUid(uid) [2]. Not sure what causes it and how it happen, but we can catch this exception first to avoid the crash. [1] https://goo.gl/ezJvWB [2] https://goo.gl/6Q8b6M MozReview-Commit-ID: HrTfubxHNSk
mobile/android/base/java/org/mozilla/gecko/media/MediaControlService.java
--- a/mobile/android/base/java/org/mozilla/gecko/media/MediaControlService.java
+++ b/mobile/android/base/java/org/mozilla/gecko/media/MediaControlService.java
@@ -162,17 +162,21 @@ public class MediaControlService extends
     private void initialize() {
         if (mInitialize ||
             !isAndroidVersionLollipopOrHigher()) {
             return;
         }
 
         Log.d(LOGTAG, "initialize");
         getGeckoPreference();
-        initMediaSession();
+        if (!initMediaSession()) {
+             Log.e(LOGTAG, "initialization fail!");
+             stopSelf();
+             return;
+        }
 
         coverSize = (int) getResources().getDimension(R.dimen.notification_media_cover);
         minCoverSize = getResources().getDimensionPixelSize(R.dimen.favicon_bg);
 
         Tabs.registerOnTabsChangedListener(this);
         mInitialize = true;
     }
 
@@ -275,22 +279,27 @@ public class MediaControlService extends
                         handleIntent(intent);
                     }
                 }
             }
         };
         PrefsHelper.addObserver(mPrefs, mPrefsObserver);
     }
 
-    private void initMediaSession() {
+    private boolean initMediaSession() {
         // Android MediaSession is introduced since version L.
-        mSession = new MediaSession(getApplicationContext(),
-                "fennec media session");
-        mController = new MediaController(getApplicationContext(),
-                mSession.getSessionToken());
+        try {
+            mSession = new MediaSession(getApplicationContext(),
+                                        "fennec media session");
+            mController = new MediaController(getApplicationContext(),
+                                              mSession.getSessionToken());
+        } catch (IllegalStateException e) {
+            Log.e(LOGTAG, "can't create MediaSession and MediaController!");
+            return false;
+        }
 
         int volumeControl = mController.getPlaybackInfo().getVolumeControl();
         if (volumeControl == VolumeProvider.VOLUME_CONTROL_ABSOLUTE ||
                 volumeControl == VolumeProvider.VOLUME_CONTROL_RELATIVE) {
             mSupportsDucking = true;
         } else {
             Log.w(LOGTAG, "initMediaSession, Session does not support volume absolute or relative volume control");
         }
@@ -327,17 +336,17 @@ public class MediaControlService extends
             public void onStop() {
                 Log.d(LOGTAG, "Controller, onStop");
                 super.onStop();
                 setState(State.STOPPED);
                 notifyObservers("mediaControl", "mediaControlStopped");
                 mTabReference = new WeakReference<>(null);
             }
         });
-
+        return true;
     }
 
     private void setMediaStateForTab(boolean isTabPlaying) {
         final Tab tab = mTabReference.get();
         if (tab == null) {
             return;
         }
         tab.setIsMediaPlaying(isTabPlaying);