Bug 1290467 - part4 : create helper function.
wrap some code into initialize() and shutdown().
MozReview-Commit-ID: AiyABlyDEME
--- a/mobile/android/base/java/org/mozilla/gecko/media/MediaControlService.java
+++ b/mobile/android/base/java/org/mozilla/gecko/media/MediaControlService.java
@@ -44,41 +44,31 @@ public class MediaControlService extends
private String mActionState = ACTION_STOP;
private MediaSession mSession;
private MediaController mController;
private PrefsHelper.PrefHandler mPrefsObserver;
private final String[] mPrefs = { MEDIA_CONTROL_PREF };
- private boolean mIsInitMediaSession = false;
+ private boolean mInitialize = false;
private boolean mIsMediaControlPrefOn = true;
- private static WeakReference<Tab> mTabReference = null;
+ private static WeakReference<Tab> mTabReference = new WeakReference<>(null);
private int coverSize;
@Override
public void onCreate() {
- mTabReference = new WeakReference<>(null);
-
- getGeckoPreference();
- initMediaSession();
-
- coverSize = (int) getResources().getDimension(R.dimen.notification_media_cover);
-
- Tabs.registerOnTabsChangedListener(this);
+ initialize();
}
@Override
public void onDestroy() {
- notifyControlInterfaceChanged(ACTION_REMOVE_CONTROL);
- PrefsHelper.removeObserver(mPrefsObserver);
-
- Tabs.unregisterOnTabsChangedListener(this);
+ shutdown();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleIntent(intent);
return START_NOT_STICKY;
}
@@ -90,22 +80,22 @@ public class MediaControlService extends
@Override
public boolean onUnbind(Intent intent) {
mSession.release();
return super.onUnbind(intent);
}
@Override
public void onTaskRemoved(Intent rootIntent) {
- stopSelf();
+ shutdown();
}
@Override
public void onTabChanged(Tab tab, Tabs.TabEvents msg, String data) {
- if (!mIsInitMediaSession) {
+ if (!mInitialize) {
return;
}
final Tab playingTab = mTabReference.get();
switch (msg) {
case MEDIA_PLAYING_CHANGE:
if (playingTab != tab && tab.isMediaPlaying()) {
mTabReference = new WeakReference<>(tab);
@@ -119,23 +109,52 @@ public class MediaControlService extends
if (playingTab == null || playingTab == tab) {
// Remove the controls when the playing tab disappeared or was closed.
mController.getTransportControls().stop();
}
break;
}
}
+ private void initialize() {
+ if (mInitialize ||
+ !isAndroidVersionLollopopOrHigher()) {
+ return;
+ }
+
+ Log.d(LOGTAG, "initialize");
+ getGeckoPreference();
+ initMediaSession();
+
+ coverSize = (int) getResources().getDimension(R.dimen.notification_media_cover);
+
+ Tabs.registerOnTabsChangedListener(this);
+ mInitialize = true;
+ }
+
+ private void shutdown() {
+ if (!mInitialize) {
+ return;
+ }
+
+ Log.d(LOGTAG, "shutdown");
+ notifyControlInterfaceChanged(ACTION_REMOVE_CONTROL);
+ PrefsHelper.removeObserver(mPrefsObserver);
+
+ Tabs.unregisterOnTabsChangedListener(this);
+ mInitialize = false;
+ stopSelf();
+ }
+
private boolean isAndroidVersionLollopopOrHigher() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
}
private void handleIntent(Intent intent) {
- if (intent == null || intent.getAction() == null ||
- !mIsInitMediaSession) {
+ if (intent == null || intent.getAction() == null || !mInitialize) {
return;
}
Log.d(LOGTAG, "HandleIntent, action = " + intent.getAction() + ", actionState = " + mActionState);
switch (intent.getAction()) {
case ACTION_START :
mController.getTransportControls().sendCustomAction(ACTION_START, null);
break;
@@ -182,20 +201,16 @@ public class MediaControlService extends
}
}
}
};
PrefsHelper.addObserver(mPrefs, mPrefsObserver);
}
private void initMediaSession() {
- if (!isAndroidVersionLollopopOrHigher() || mIsInitMediaSession) {
- return;
- }
-
// Android MediaSession is introduced since version L.
mSession = new MediaSession(getApplicationContext(),
"fennec media session");
mController = new MediaController(getApplicationContext(),
mSession.getSessionToken());
mSession.setCallback(new MediaSession.Callback() {
@Override
@@ -231,29 +246,32 @@ public class MediaControlService extends
super.onStop();
notifyControlInterfaceChanged(ACTION_STOP);
notifyObservers("MediaControl", "mediaControlStopped");
mActionState = ACTION_STOP;
mTabReference = new WeakReference<>(null);
stopSelf();
}
});
- mIsInitMediaSession = true;
}
private void notifyObservers(String topic, String data) {
GeckoAppShell.notifyObservers(topic, data);
}
private boolean isNeedToRemoveControlInterface(String action) {
return (action.equals(ACTION_STOP) ||
action.equals(ACTION_REMOVE_CONTROL));
}
private void notifyControlInterfaceChanged(final String action) {
+ if (!mInitialize) {
+ return;
+ }
+
Log.d(LOGTAG, "notifyControlInterfaceChanged, action = " + action);
if (isNeedToRemoveControlInterface(action)) {
NotificationManagerCompat.from(this).cancel(MEDIA_CONTROL_ID);
return;
}
if (!mIsMediaControlPrefOn) {