Bug 1373887 - catch exception to handle creating media session fail. draft
authorAlastor Wu <alwu@mozilla.com>
Tue, 11 Jul 2017 14:01:53 +0800
changeset 606644 4893ae9d0965f4ab534e507aaca2aaa94bd15b42
parent 603204 6f8f10f48ace5692256efd91f011bd23054ee2ec
child 636821 5898cf736900c1628b307b59e2e0cf1f7b8cb625
push id67760
push useralwu@mozilla.com
push dateTue, 11 Jul 2017 07:56:19 +0000
bugs1373887
milestone56.0a1
Bug 1373887 - catch exception to handle creating media session fail. 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);