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

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: Moved OnMediaSessionStateChanged to WebContentsImpl 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..c68e582f7508fc9467d596ea824cec5a5974fd60
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/media/ui/MediaSessionTabHelper.java
@@ -0,0 +1,129 @@
+// 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() {
+ if (mTab == null) {
+ NotificationMediaPlaybackControls.clear();
+ } else {
+ NotificationMediaPlaybackControls.hide(mTab.getId());
+ }
+ super.destroy();
+ }
+
+ @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, "Unable to parse the origin from the URL. "
+ + "Showing the full URL instead.");
+ }
+ NotificationMediaPlaybackControls.show(
+ ApplicationStatus.getApplicationContext(),
+ new MediaInfo(
+ mTab.getTitle(),
+ isPaused,
+ origin,
+ mTab.getId(),
+ mControlsListener));
+ }
+ };
+
+ }
+
+ private void setWebContents(WebContents webContents) {
+ if (mWebContents == webContents) return;
+
+ cleanupWebContents();
+ mWebContents = webContents;
+ if (mWebContents != null) mWebContentsObserver = createWebContentsObserver(mWebContents);
+ }
+
+ private void cleanupWebContents() {
+ if (mWebContentsObserver != null) mWebContentsObserver.destroy();
+ mWebContentsObserver = null;
+ mWebContents = null;
+ }
+
+ private final TabObserver mTabObserver = new EmptyTabObserver() {
+ @Override
+ public void onContentChanged(Tab tab) {
+ assert tab == mTab;
+ setWebContents(tab.getWebContents());
+ }
+
+ @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);
+ if (mTab.getWebContents() != null) setWebContents(tab.getWebContents());
+ }
+
+ /**
+ * 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