Bug 1313601 - Disable presentation mode when taps disconnect from Chromecast on Android Notification Bar. r=sebastian
authorKuoE0 <kuoe0.tw@gmail.com>
Fri, 28 Oct 2016 16:26:03 +0800
changeset 347471 fe218ca720fc285071583f5f5607542bf7c25c4c
parent 347470 ae743ad1f3260fc23be090535ff86f7097e144bb
child 347472 4a4d45357bc7a1e77be4f3796a78005cce43655f
push id10298
push userraliiev@mozilla.com
push dateMon, 14 Nov 2016 12:33:03 +0000
treeherdermozilla-aurora@7e29173b1641 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssebastian
bugs1313601
milestone52.0a1
Bug 1313601 - Disable presentation mode when taps disconnect from Chromecast on Android Notification Bar. r=sebastian MozReview-Commit-ID: Ge83JjnPSJg
mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
mobile/android/base/java/org/mozilla/gecko/MediaPlayerManager.java
mobile/android/base/java/org/mozilla/gecko/RemotePresentationService.java
--- a/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
+++ b/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
@@ -2036,17 +2036,17 @@ public class BrowserApp extends GeckoApp
 
                         if (mediaManagerClass != null) {
                             try {
                                 final String tag = "";
                                 mediaManagerClass.getDeclaredField("MEDIA_PLAYER_TAG").get(tag);
                                 Log.i(LOGTAG, "Found tag " + tag);
                                 final Fragment frag = getSupportFragmentManager().findFragmentByTag(tag);
                                 if (frag == null) {
-                                    final Method getInstance = mediaManagerClass.getMethod("newInstance", (Class[]) null);
+                                    final Method getInstance = mediaManagerClass.getMethod("getInstance", (Class[]) null);
                                     final Fragment mpm = (Fragment) getInstance.invoke(null);
                                     getSupportFragmentManager().beginTransaction().disallowAddToBackStack().add(mpm, tag).commit();
                                 }
                             } catch (Exception ex) {
                                 Log.e(LOGTAG, "Error initializing media manager", ex);
                             }
                         }
                     }
--- a/mobile/android/base/java/org/mozilla/gecko/MediaPlayerManager.java
+++ b/mobile/android/base/java/org/mozilla/gecko/MediaPlayerManager.java
@@ -30,23 +30,30 @@ import java.util.Map;
  * Manages a list of GeckoMediaPlayers methods (i.e. Chromecast/Miracast). Routes messages
  * from Gecko to the correct caster based on the id of the display
  */
 public class MediaPlayerManager extends Fragment implements NativeEventListener {
     /**
      * Create a new instance of DetailsFragment, initialized to
      * show the text at 'index'.
      */
+
+    private static MediaPlayerManager instance = null;
+
     @ReflectionTarget
-    public static MediaPlayerManager newInstance() {
+    public static MediaPlayerManager getInstance() {
+        if (instance != null) {
+            return instance;
+        }
         if (Versions.feature17Plus) {
-            return new PresentationMediaPlayerManager();
+            instance = (MediaPlayerManager) new PresentationMediaPlayerManager();
         } else {
-            return new MediaPlayerManager();
+            instance = new MediaPlayerManager();
         }
+        return instance;
     }
 
     private static final String LOGTAG = "GeckoMediaPlayerManager";
     protected boolean isPresentationMode = false; // Used to prevent mirroring when Presentation API is used.
 
     @ReflectionTarget
     public static final String MEDIA_PLAYER_TAG = "MPManagerFragment";
 
@@ -141,20 +148,18 @@ public class MediaPlayerManager extends 
             final GeckoPresentationDisplay display = displays.get(message.getString("id"));
             if (display == null) {
                 Log.e(LOGTAG, "Couldn't find a display for this id: " + message.getString("id") + " for message: " + event);
                 return;
             }
 
             if ("AndroidCastDevice:Start".equals(event)) {
                 display.start(callback);
-                isPresentationMode = true;
             } else if ("AndroidCastDevice:Stop".equals(event)) {
                 display.stop(callback);
-                isPresentationMode = false;
             }
         }
     }
 
     private final MediaRouter.Callback callback =
         new MediaRouter.Callback() {
             @Override
             public void onRouteRemoved(MediaRouter router, RouteInfo route) {
@@ -289,10 +294,14 @@ public class MediaPlayerManager extends 
             .addControlCategory(MediaControlIntent.CATEGORY_LIVE_VIDEO)
             .addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)
             .addControlCategory(CastMediaControlIntent.categoryForCast(ChromeCastPlayer.MIRROR_RECEIVER_APP_ID))
             .addControlCategory(CastMediaControlIntent.categoryForCast(ChromeCastDisplay.REMOTE_DISPLAY_APP_ID))
             .build();
         mediaRouter.addCallback(selectorBuilder, callback, MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
     }
 
+    public void setPresentationMode(boolean isPresentationMode) {
+        this.isPresentationMode = isPresentationMode;
+    }
+
     protected void updatePresentation() { /* Overridden in sub-classes. */ }
 }
--- a/mobile/android/base/java/org/mozilla/gecko/RemotePresentationService.java
+++ b/mobile/android/base/java/org/mozilla/gecko/RemotePresentationService.java
@@ -75,22 +75,25 @@ public class RemotePresentationService e
         dismissPresentation();
     }
 
     private void dismissPresentation() {
         if (presentation != null) {
             presentation.dismiss();
             presentation = null;
             ScreenManagerHelper.removeDisplay(screenId);
+            MediaPlayerManager.getInstance().setPresentationMode(false);
         }
     }
 
     private void createPresentation() {
         dismissPresentation();
 
+        MediaPlayerManager.getInstance().setPresentationMode(true);
+
         DisplayMetrics metrics = new DisplayMetrics();
         getDisplay().getMetrics(metrics);
         screenId = ScreenManagerHelper.addDisplay(ScreenManagerHelper.DISPLAY_VIRTUAL,
                                                   metrics.widthPixels,
                                                   metrics.heightPixels,
                                                   metrics.density);
 
         VirtualPresentation virtualPresentation = new VirtualPresentation(this, getDisplay());