Bug 1454709 - Don't run MediaControlService on unsupported Android versions. r=snorp
authorJan Henning <jh+bugzilla@buttercookie.de>
Tue, 17 Apr 2018 20:11:35 +0200
changeset 414352 13f330a9dba13b5aad22721adae31023693b483a
parent 414351 9923d93eddd88deb8a071b6cdf9e506b5a3152f2
child 414353 924aabc981b6136cf09cfc8df170ffd882dde8a3
push id62854
push usermozilla@buttercookie.de
push dateWed, 18 Apr 2018 20:18:49 +0000
treeherderautoland@13f330a9dba1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp
bugs1454709
milestone61.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1454709 - Don't run MediaControlService on unsupported Android versions. r=snorp Since we're currently not using the support library versions of MediaSession and the MediaStyle notification, our playback notification only works from Lollipop onward, so starting the MediaControlService on previous Android versions is unnecessary. Right now the AudioFocusAgent is the only place that is directly communicating with the MediaControlService, so we simply block starting of the service there when running on an unsupported OS version. As an additional safeguard however, we also make sure that if somebody accidentally manages to start the service on Android 4.x anyway, we not only skip initialisation and ignore any further intents delivered to the service like we currently do, but also actually stop the service again. MozReview-Commit-ID: 6TiV6og74NQ
mobile/android/base/java/org/mozilla/gecko/media/AudioFocusAgent.java
mobile/android/base/java/org/mozilla/gecko/media/MediaControlService.java
--- a/mobile/android/base/java/org/mozilla/gecko/media/AudioFocusAgent.java
+++ b/mobile/android/base/java/org/mozilla/gecko/media/AudioFocusAgent.java
@@ -11,16 +11,18 @@ import android.content.Context;
 import android.content.Intent;
 import android.media.AudioManager;
 import android.media.AudioManager.OnAudioFocusChangeListener;
 import android.support.annotation.VisibleForTesting;
 import android.util.Log;
 
 import java.lang.ref.WeakReference;
 
+import static org.mozilla.gecko.AppConstants.Versions;
+
 public class AudioFocusAgent implements Tabs.OnTabsChangedListener {
     private static final String LOGTAG = "AudioFocusAgent";
 
     // We're referencing the *application* context, so this is in fact okay.
     @SuppressLint("StaticFieldLeak")
     private static Context mContext;
     private AudioManager mAudioManager;
     private OnAudioFocusChangeListener mAfChangeListener;
@@ -196,16 +198,22 @@ public class AudioFocusAgent implements 
                 if (playingTab == tab) {
                     notifyMediaControlService(MediaControlService.ACTION_TAB_STATE_FAVICON);
                 }
                 break;
         }
     }
 
     private void notifyMediaControlService(String action) {
+        if (Versions.preLollipop) {
+            // The notification only works from Lollipop onwards (at least until we try using
+            // the support library version), so there's no point in starting the service.
+            return;
+        }
+
         Intent intent = new Intent(mContext, MediaControlService.class);
         intent.setAction(action);
         mContext.startService(intent);
     }
 
     @VisibleForTesting
     @RobocopTarget
     public State getAudioFocusState() {
--- a/mobile/android/base/java/org/mozilla/gecko/media/MediaControlService.java
+++ b/mobile/android/base/java/org/mozilla/gecko/media/MediaControlService.java
@@ -76,17 +76,16 @@ public class MediaControlService extends
         PLAYING,
         PAUSED,
         STOPPED
     }
 
     @Override
     public void onCreate() {
         initialize();
-        mHeadSetStateReceiver = new HeadSetStateReceiver().registerReceiver(getApplicationContext());
     }
 
     @Override
     public void onDestroy() {
         shutdown();
     }
 
     @Override
@@ -105,32 +104,38 @@ public class MediaControlService extends
         shutdown();
     }
 
     private boolean isMediaPlaying() {
         return mMediaState.equals(State.PLAYING);
     }
 
     private void initialize() {
-        if (mInitialize ||
-            !isAndroidVersionLollipopOrHigher()) {
+        if (mInitialize) {
+            return;
+        }
+
+        if (!isAndroidVersionLollipopOrHigher()) {
+            stopSelf();
             return;
         }
 
         Log.d(LOGTAG, "initialize");
         getGeckoPreference();
         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);
 
+        mHeadSetStateReceiver = new HeadSetStateReceiver().registerReceiver(getApplicationContext());
+
         mInitialize = true;
     }
 
     private void shutdown() {
         if (!mInitialize) {
             return;
         }
         mInitialize = false;