Bug 1290467 - part5 : change service's life time.
Now the life time of the MediaControlService would be as same as the Fennec app.
To make code flow more easily, requesting/abandoning the audio focus wouldn't
affect the media control.
We would mainly communicate with the media control via TabEvents.
MozReview-Commit-ID: KT59bII0HuN
--- a/mobile/android/base/java/org/mozilla/gecko/media/AudioFocusAgent.java
+++ b/mobile/android/base/java/org/mozilla/gecko/media/AudioFocusAgent.java
@@ -73,16 +73,17 @@ public class AudioFocusAgent {
notifyObservers("AudioFocusChanged", "gainAudioFocus");
notifyMediaControlService(MediaControlService.ACTION_PLAY);
mAudioFocusState = OWN_FOCUS;
break;
default:
}
}
};
+ notifyMediaControlService(MediaControlService.ACTION_INIT);
}
@RobocopTarget
public static AudioFocusAgent getInstance() {
return AudioFocusAgent.SingletonHolder.INSTANCE;
}
private static class SingletonHolder {
@@ -108,29 +109,27 @@ public class AudioFocusAgent {
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
String focusMsg = (result == AudioManager.AUDIOFOCUS_GAIN) ?
"AudioFocus request granted" : "AudioFoucs request failed";
Log.d(LOGTAG, focusMsg);
if (result == AudioManager.AUDIOFOCUS_GAIN) {
mAudioFocusState = OWN_FOCUS;
- notifyMediaControlService(MediaControlService.ACTION_START);
}
}
private void abandonAudioFocusIfNeeded() {
if (!mAudioFocusState.equals(OWN_FOCUS)) {
return;
}
Log.d(LOGTAG, "Abandon AudioFocus");
mAudioManager.abandonAudioFocus(mAfChangeListener);
mAudioFocusState = LOST_FOCUS;
- notifyMediaControlService(MediaControlService.ACTION_STOP);
}
private void notifyMediaControlService(String action) {
Intent intent = new Intent(mContext, MediaControlService.class);
intent.setAction(action);
mContext.startService(intent);
}
}
--- a/mobile/android/base/java/org/mozilla/gecko/media/MediaControlService.java
+++ b/mobile/android/base/java/org/mozilla/gecko/media/MediaControlService.java
@@ -27,16 +27,17 @@ import org.mozilla.gecko.Tab;
import org.mozilla.gecko.Tabs;
import org.mozilla.gecko.util.ThreadUtils;
import java.lang.ref.WeakReference;
public class MediaControlService extends Service implements Tabs.OnTabsChangedListener {
private static final String LOGTAG = "MediaControlService";
+ public static final String ACTION_INIT = "action_init";
public static final String ACTION_START = "action_start";
public static final String ACTION_PLAY = "action_play";
public static final String ACTION_PAUSE = "action_pause";
public static final String ACTION_STOP = "action_stop";
public static final String ACTION_REMOVE_CONTROL = "action_remove_control";
private static final int MEDIA_CONTROL_ID = 1;
private static final String MEDIA_CONTROL_PREF = "dom.audiochannel.mediaControl";
@@ -150,16 +151,20 @@ public class MediaControlService extends
private void handleIntent(Intent intent) {
if (intent == null || intent.getAction() == null || !mInitialize) {
return;
}
Log.d(LOGTAG, "HandleIntent, action = " + intent.getAction() + ", actionState = " + mActionState);
switch (intent.getAction()) {
+ case ACTION_INIT :
+ // This action is used to create a service and do the initialization,
+ // the actual operation would be executed via tab events.
+ break;
case ACTION_START :
mController.getTransportControls().sendCustomAction(ACTION_START, null);
break;
case ACTION_PLAY :
mController.getTransportControls().play();
break;
case ACTION_PAUSE :
mController.getTransportControls().pause();
@@ -243,17 +248,16 @@ public class MediaControlService extends
@Override
public void onStop() {
Log.d(LOGTAG, "Controller, onStop");
super.onStop();
notifyControlInterfaceChanged(ACTION_STOP);
notifyObservers("MediaControl", "mediaControlStopped");
mActionState = ACTION_STOP;
mTabReference = new WeakReference<>(null);
- stopSelf();
}
});
}
private void notifyObservers(String topic, String data) {
GeckoAppShell.notifyObservers(topic, data);
}
@@ -265,16 +269,17 @@ public class MediaControlService extends
private void notifyControlInterfaceChanged(final String action) {
if (!mInitialize) {
return;
}
Log.d(LOGTAG, "notifyControlInterfaceChanged, action = " + action);
if (isNeedToRemoveControlInterface(action)) {
+ stopForeground(false);
NotificationManagerCompat.from(this).cancel(MEDIA_CONTROL_ID);
return;
}
if (!mIsMediaControlPrefOn) {
return;
}