Bug 1311245 - part2 : make sure the mActionState would be modified everytime the UI interface changed. draft
authorAlastor Wu <alwu@mozilla.com>
Thu, 27 Oct 2016 10:12:23 +0800
changeset 430052 76ae47e87f8e0178d390efb9bc281449579e0fce
parent 430051 0c6e4ea002e67daeb190cda3039df21bd9484873
child 430053 0466619fca39b3617105255dd077dd3d78c172a5
push id33722
push useralwu@mozilla.com
push dateThu, 27 Oct 2016 02:13:11 +0000
bugs1311245
milestone52.0a1
Bug 1311245 - part2 : make sure the mActionState would be modified everytime the UI interface changed. Make sure the value of |mActionState| is correct after we modified the media control's interface. MozReview-Commit-ID: 6Nhu1gqCFrD
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
@@ -235,17 +235,16 @@ public class MediaControlService extends
                                           mSession.getSessionToken());
 
         mSession.setCallback(new MediaSession.Callback() {
             @Override
             public void onCustomAction(String action, Bundle extras) {
                 if (action.equals(ACTION_START)) {
                     Log.d(LOGTAG, "Controller, onStart");
                     notifyControlInterfaceChanged(ACTION_PAUSE);
-                    mActionState = ACTION_RESUME;
                 } else if (action.equals(ACTION_PAUSE_BY_AUDIO_FOCUS)) {
                     Log.d(LOGTAG, "Controller, pause by audio focus changed");
                     notifyControlInterfaceChanged(ACTION_RESUME);
                     mActionState = ACTION_PAUSE_BY_AUDIO_FOCUS;
                 } else if (action.equals(ACTION_RESUME_BY_AUDIO_FOCUS)) {
                     Log.d(LOGTAG, "Controller, resume by audio focus changed");
                     notifyControlInterfaceChanged(ACTION_PAUSE);
                     mActionState = ACTION_RESUME_BY_AUDIO_FOCUS;
@@ -253,61 +252,59 @@ public class MediaControlService extends
             }
 
             @Override
             public void onPlay() {
                 Log.d(LOGTAG, "Controller, onPlay");
                 super.onPlay();
                 notifyControlInterfaceChanged(ACTION_PAUSE);
                 notifyObservers("MediaControl", "resumeMedia");
-                mActionState = ACTION_RESUME;
                 // To make sure we always own audio focus during playing.
                 AudioFocusAgent.notifyStartedPlaying();
             }
 
             @Override
             public void onPause() {
                 Log.d(LOGTAG, "Controller, onPause");
                 super.onPause();
                 notifyControlInterfaceChanged(ACTION_RESUME);
                 notifyObservers("MediaControl", "mediaControlPaused");
-                mActionState = ACTION_PAUSE;
                 AudioFocusAgent.notifyStoppedPlaying();
             }
 
             @Override
             public void onStop() {
                 Log.d(LOGTAG, "Controller, onStop");
                 super.onStop();
                 notifyControlInterfaceChanged(ACTION_STOP);
                 notifyObservers("MediaControl", "mediaControlStopped");
-                mActionState = ACTION_STOP;
                 mTabReference = new WeakReference<>(null);
             }
         });
     }
 
     private void notifyObservers(String topic, String data) {
         GeckoAppShell.notifyObservers(topic, data);
     }
 
     private boolean isNeedToRemoveControlInterface(String action) {
         return action.equals(ACTION_STOP);
     }
 
-    private void notifyControlInterfaceChanged(final String action) {
+    private void notifyControlInterfaceChanged(final String uiAction) {
         if (!mInitialize) {
             return;
         }
 
-        Log.d(LOGTAG, "notifyControlInterfaceChanged, action = " + action);
+        Log.d(LOGTAG, "notifyControlInterfaceChanged, action = " + uiAction);
 
-        if (isNeedToRemoveControlInterface(action)) {
+        if (isNeedToRemoveControlInterface(uiAction)) {
             stopForeground(false);
             NotificationManagerCompat.from(this).cancel(MEDIA_CONTROL_ID);
+            setActionState(uiAction);
             return;
         }
 
         if (!mIsMediaControlPrefOn) {
             return;
         }
 
         // TODO : remove this checking when the media control is ready to ship,
@@ -317,24 +314,40 @@ public class MediaControlService extends
         }
 
         final Tab tab = mTabReference.get();
 
         if (tab == null) {
             return;
         }
 
+        setActionState(uiAction);
+
         ThreadUtils.postToBackgroundThread(new Runnable() {
             @Override
             public void run() {
-                updateNotification(tab, action);
+                updateNotification(tab, uiAction);
             }
         });
     }
 
+    private void setActionState(final String uiAction) {
+        switch (uiAction) {
+            case ACTION_PAUSE:
+                mActionState = ACTION_RESUME;
+                break;
+            case ACTION_RESUME:
+                mActionState = ACTION_PAUSE;
+                break;
+            case ACTION_STOP:
+                mActionState = ACTION_STOP;
+                break;
+        }
+    }
+
     private void updateNotification(Tab tab, String action) {
         ThreadUtils.assertNotOnUiThread();
 
         final Notification.MediaStyle style = new Notification.MediaStyle();
         style.setShowActionsInCompactView(0);
 
         final boolean isPlaying = isMediaPlaying();
         final int visibility = tab.isPrivate() ?