Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3057)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/media/ui/MediaSessionTabHelper.java

Issue 1159113006: [Android] A prototype of the interactive media notification. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed Min's nits Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/media/ui/MediaSessionTabHelper.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/media/ui/MediaSessionTabHelper.java b/chrome/android/java/src/org/chromium/chrome/browser/media/ui/MediaSessionTabHelper.java
new file mode 100644
index 0000000000000000000000000000000000000000..ed4bcfa734f77c9fc289658f81835c32bfb0f576
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/media/ui/MediaSessionTabHelper.java
@@ -0,0 +1,124 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.chrome.browser.media.ui;
+
+import org.chromium.base.ApplicationStatus;
+import org.chromium.base.Log;
+import org.chromium.chrome.browser.EmptyTabObserver;
+import org.chromium.chrome.browser.Tab;
+import org.chromium.chrome.browser.TabObserver;
+import org.chromium.chrome.browser.UrlUtilities;
+import org.chromium.content_public.browser.WebContents;
+import org.chromium.content_public.browser.WebContentsObserver;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+/**
+ * A tab helper responsible for enabling/disabling media controls and passing
+ * media actions from the controls to the {@link org.chromium.content.browser.MediaSession}
+ */
+public class MediaSessionTabHelper {
+ private static final String TAG = "cr.MediaSession";
+
+ private Tab mTab;
+ private WebContents mWebContents;
+ private WebContentsObserver mWebContentsObserver;
+
+ private MediaPlaybackListener mControlsListener = new MediaPlaybackListener() {
+ @Override
+ public void onPlay() {
+ assert mWebContents != null;
+ mWebContents.resumeMediaSession();
+ }
+
+ @Override
+ public void onPause() {
+ assert mWebContents != null;
+ mWebContents.suspendMediaSession();
+ }
+ };
+
+ private WebContentsObserver createWebContentsObserver(WebContents webContents) {
+ return new WebContentsObserver(webContents) {
+ @Override
+ public void destroy() {
+ int tabId = mTab == null ? Tab.INVALID_TAB_ID : mTab.getId();
Ted C 2015/07/06 22:18:28 you want to call super.destroy() as well.
whywhat 2015/07/07 15:44:57 Oops, didn't expect any of the WebContentsObserver
+ NotificationMediaPlaybackControls.hide(tabId);
+ }
+
+ @Override
+ public void mediaSessionStateChanged(boolean isControllable, boolean isPaused) {
+ assert mTab != null;
+ if (!isControllable) {
+ NotificationMediaPlaybackControls.hide(mTab.getId());
+ return;
+ }
+ String origin = mTab.getUrl();
+ try {
+ origin = UrlUtilities.getOriginForDisplay(new URI(origin), true);
+ } catch (URISyntaxException e) {
+ Log.e(TAG, "Wrong syntax for the tab URL when playing media. Showing the URL.");
Ted C 2015/07/06 22:18:28 I would say, "Unable to parse the origin from the
whywhat 2015/07/07 15:44:57 Done.
+ }
+ NotificationMediaPlaybackControls.show(
+ ApplicationStatus.getApplicationContext(),
+ new MediaInfo(
+ mTab.getTitle(),
+ isPaused,
+ origin,
+ mTab.getId(),
+ mControlsListener));
+ }
+ };
+
+ }
+
+ private void cleanupWebContents() {
+ if (mWebContents != null && mWebContentsObserver != null) {
Ted C 2015/07/06 22:18:28 here, just check mWebContentsObserver != null and
whywhat 2015/07/07 15:44:57 Done.
+ mWebContents.removeObserver(mWebContentsObserver);
+ }
+ mWebContentsObserver = null;
+ mWebContents = null;
+ }
+
+ private final TabObserver mTabObserver = new EmptyTabObserver() {
+ @Override
+ public void onContentChanged(Tab tab) {
+ assert tab == mTab;
+
+ // Usually means that tab's web contents changed.
Ted C 2015/07/06 22:18:28 this also happens when you navigate to a native pa
whywhat 2015/07/07 15:44:57 Isn't this included in "web contents changed" as i
Ted C 2015/07/07 17:11:19 No, the webcontents only changes for a tab when yo
whywhat 2015/07/07 19:19:04 I think I understand now but just to be clear - th
Ted C 2015/07/07 20:04:12 Correct, nothing to change. Just wanted to fully
+ cleanupWebContents();
+ mWebContents = tab.getWebContents();
+ if (mWebContents != null) {
+ mWebContentsObserver = createWebContentsObserver(mWebContents);
+ mWebContents.addObserver(mWebContentsObserver);
whywhat 2015/07/07 15:44:57 Also wasn't needed - WebContentsObserver adds itse
+ }
+ }
+
+ @Override
+ public void onDestroyed(Tab tab) {
+ assert mTab == tab;
+
+ cleanupWebContents();
+
+ NotificationMediaPlaybackControls.hide(mTab.getId());
+ mTab.removeObserver(this);
+ mTab = null;
+ }
+ };
+
+ private MediaSessionTabHelper(Tab tab) {
+ mTab = tab;
+ mTab.addObserver(mTabObserver);
Ted C 2015/07/06 22:18:28 For safety sake, I would do if (tab.getWebContent
whywhat 2015/07/07 15:44:57 Done.
+ }
+
+ /**
+ * Creates the {@link MediaSessionTabHelper} for the given {@link Tab}.
+ * @param tab the tab to attach the helper to.
+ */
+ public static void createForTab(Tab tab) {
+ new MediaSessionTabHelper(tab);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698