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
--- 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);