OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.media.ui; |
| 6 |
| 7 import org.chromium.base.ApplicationStatus; |
| 8 import org.chromium.base.Log; |
| 9 import org.chromium.chrome.browser.EmptyTabObserver; |
| 10 import org.chromium.chrome.browser.Tab; |
| 11 import org.chromium.chrome.browser.TabObserver; |
| 12 import org.chromium.chrome.browser.UrlUtilities; |
| 13 import org.chromium.content_public.browser.WebContents; |
| 14 import org.chromium.content_public.browser.WebContentsObserver; |
| 15 |
| 16 import java.net.URI; |
| 17 import java.net.URISyntaxException; |
| 18 |
| 19 /** |
| 20 * A tab helper responsible for enabling/disabling media controls and passing |
| 21 * media actions from the controls to the {@link org.chromium.content.browser.Me
diaSession} |
| 22 */ |
| 23 public class MediaSessionTabHelper { |
| 24 private static final String TAG = "cr.MediaSession"; |
| 25 |
| 26 private Tab mTab; |
| 27 private WebContents mWebContents; |
| 28 private WebContentsObserver mWebContentsObserver; |
| 29 |
| 30 private MediaPlaybackListener mControlsListener = new MediaPlaybackListener(
) { |
| 31 @Override |
| 32 public void onPlay() { |
| 33 assert mWebContents != null; |
| 34 mWebContents.resumeMediaSession(); |
| 35 } |
| 36 |
| 37 @Override |
| 38 public void onPause() { |
| 39 assert mWebContents != null; |
| 40 mWebContents.suspendMediaSession(); |
| 41 } |
| 42 }; |
| 43 |
| 44 private WebContentsObserver createWebContentsObserver(WebContents webContent
s) { |
| 45 return new WebContentsObserver(webContents) { |
| 46 @Override |
| 47 public void destroy() { |
| 48 if (mTab == null) { |
| 49 NotificationMediaPlaybackControls.clear(); |
| 50 } else { |
| 51 NotificationMediaPlaybackControls.hide(mTab.getId()); |
| 52 } |
| 53 super.destroy(); |
| 54 } |
| 55 |
| 56 @Override |
| 57 public void mediaSessionStateChanged(boolean isControllable, boolean
isPaused) { |
| 58 assert mTab != null; |
| 59 if (!isControllable) { |
| 60 NotificationMediaPlaybackControls.hide(mTab.getId()); |
| 61 return; |
| 62 } |
| 63 String origin = mTab.getUrl(); |
| 64 try { |
| 65 origin = UrlUtilities.getOriginForDisplay(new URI(origin), t
rue); |
| 66 } catch (URISyntaxException e) { |
| 67 Log.e(TAG, "Unable to parse the origin from the URL. " |
| 68 + "Showing the full URL instead."); |
| 69 } |
| 70 NotificationMediaPlaybackControls.show( |
| 71 ApplicationStatus.getApplicationContext(), |
| 72 new MediaInfo( |
| 73 mTab.getTitle(), |
| 74 isPaused, |
| 75 origin, |
| 76 mTab.getId(), |
| 77 mControlsListener)); |
| 78 } |
| 79 }; |
| 80 |
| 81 } |
| 82 |
| 83 private void setWebContents(WebContents webContents) { |
| 84 if (mWebContents == webContents) return; |
| 85 |
| 86 cleanupWebContents(); |
| 87 mWebContents = webContents; |
| 88 if (mWebContents != null) mWebContentsObserver = createWebContentsObserv
er(mWebContents); |
| 89 } |
| 90 |
| 91 private void cleanupWebContents() { |
| 92 if (mWebContentsObserver != null) mWebContentsObserver.destroy(); |
| 93 mWebContentsObserver = null; |
| 94 mWebContents = null; |
| 95 } |
| 96 |
| 97 private final TabObserver mTabObserver = new EmptyTabObserver() { |
| 98 @Override |
| 99 public void onContentChanged(Tab tab) { |
| 100 assert tab == mTab; |
| 101 setWebContents(tab.getWebContents()); |
| 102 } |
| 103 |
| 104 @Override |
| 105 public void onDestroyed(Tab tab) { |
| 106 assert mTab == tab; |
| 107 |
| 108 cleanupWebContents(); |
| 109 |
| 110 NotificationMediaPlaybackControls.hide(mTab.getId()); |
| 111 mTab.removeObserver(this); |
| 112 mTab = null; |
| 113 } |
| 114 }; |
| 115 |
| 116 private MediaSessionTabHelper(Tab tab) { |
| 117 mTab = tab; |
| 118 mTab.addObserver(mTabObserver); |
| 119 if (mTab.getWebContents() != null) setWebContents(tab.getWebContents()); |
| 120 } |
| 121 |
| 122 /** |
| 123 * Creates the {@link MediaSessionTabHelper} for the given {@link Tab}. |
| 124 * @param tab the tab to attach the helper to. |
| 125 */ |
| 126 public static void createForTab(Tab tab) { |
| 127 new MediaSessionTabHelper(tab); |
| 128 } |
| 129 } |
OLD | NEW |