OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.media.ui; | 5 package org.chromium.chrome.browser.media.ui; |
6 | 6 |
7 import android.app.Activity; | 7 import android.app.Activity; |
8 import android.content.Intent; | 8 import android.content.Intent; |
9 import android.graphics.Bitmap; | 9 import android.graphics.Bitmap; |
10 import android.media.AudioManager; | 10 import android.media.AudioManager; |
11 import android.text.TextUtils; | 11 import android.text.TextUtils; |
12 | 12 |
13 import org.chromium.base.ContextUtils; | 13 import org.chromium.base.ContextUtils; |
14 import org.chromium.base.Log; | 14 import org.chromium.base.Log; |
15 import org.chromium.chrome.R; | 15 import org.chromium.chrome.R; |
16 import org.chromium.chrome.browser.metrics.MediaNotificationUma; | 16 import org.chromium.chrome.browser.metrics.MediaNotificationUma; |
17 import org.chromium.chrome.browser.metrics.MediaSessionUMA; | 17 import org.chromium.chrome.browser.metrics.MediaSessionUMA; |
18 import org.chromium.chrome.browser.tab.EmptyTabObserver; | 18 import org.chromium.chrome.browser.tab.EmptyTabObserver; |
19 import org.chromium.chrome.browser.tab.Tab; | 19 import org.chromium.chrome.browser.tab.Tab; |
20 import org.chromium.chrome.browser.tab.TabObserver; | 20 import org.chromium.chrome.browser.tab.TabObserver; |
21 import org.chromium.components.url_formatter.UrlFormatter; | 21 import org.chromium.components.url_formatter.UrlFormatter; |
22 import org.chromium.content_public.browser.MediaSessionDelegate; | |
22 import org.chromium.content_public.browser.WebContents; | 23 import org.chromium.content_public.browser.WebContents; |
23 import org.chromium.content_public.browser.WebContentsObserver; | |
24 import org.chromium.content_public.common.MediaMetadata; | 24 import org.chromium.content_public.common.MediaMetadata; |
25 import org.chromium.ui.base.WindowAndroid; | 25 import org.chromium.ui.base.WindowAndroid; |
26 | 26 |
27 import java.net.URI; | 27 import java.net.URI; |
28 import java.net.URISyntaxException; | 28 import java.net.URISyntaxException; |
29 | 29 |
30 /** | 30 /** |
31 * A tab helper responsible for enabling/disabling media controls and passing | 31 * A tab helper responsible for enabling/disabling media controls and passing |
32 * media actions from the controls to the {@link org.chromium.content.browser.Me diaSession} | 32 * media actions from the controls to the {@link org.chromium.content.browser.Me diaSession} |
33 */ | 33 */ |
34 public class MediaSessionTabHelper implements MediaImageCallback { | 34 public class MediaSessionTabHelper implements MediaImageCallback { |
35 private static final String TAG = "MediaSession"; | 35 private static final String TAG = "MediaSession"; |
36 | 36 |
37 private static final String UNICODE_PLAY_CHARACTER = "\u25B6"; | 37 private static final String UNICODE_PLAY_CHARACTER = "\u25B6"; |
38 private static final int MINIMAL_FAVICON_SIZE = 114; | 38 private static final int MINIMAL_FAVICON_SIZE = 114; |
39 | 39 |
40 private Tab mTab; | 40 private Tab mTab; |
41 private Bitmap mPageMediaImage = null; | 41 private Bitmap mPageMediaImage = null; |
42 private Bitmap mFavicon = null; | 42 private Bitmap mFavicon = null; |
43 private Bitmap mCurrentMediaImage = null; | 43 private Bitmap mCurrentMediaImage = null; |
44 private String mOrigin = null; | 44 private String mOrigin = null; |
45 private WebContents mWebContents; | 45 private WebContents mWebContents; |
whywhat
2016/10/20 15:38:33
this seems to be unused, I guess destroy() is hand
Zhiqiang Zhang (Slow)
2016/10/20 16:22:32
destroy() is now MediaSessionDelegate.mediaSession
| |
46 private WebContentsObserver mWebContentsObserver; | 46 private MediaSessionDelegate mMediaSessionDelegate; |
47 private int mPreviousVolumeControlStream = AudioManager.USE_DEFAULT_STREAM_T YPE; | 47 private int mPreviousVolumeControlStream = AudioManager.USE_DEFAULT_STREAM_T YPE; |
48 private MediaNotificationInfo.Builder mNotificationInfoBuilder = null; | 48 private MediaNotificationInfo.Builder mNotificationInfoBuilder = null; |
49 // The fallback title if |mPageMetadata| is null or its title is empty. | 49 // The fallback title if |mPageMetadata| is null or its title is empty. |
50 private String mFallbackTitle = null; | 50 private String mFallbackTitle = null; |
51 // The metadata set by the page. | 51 // The metadata set by the page. |
52 private MediaMetadata mPageMetadata = null; | 52 private MediaMetadata mPageMetadata = null; |
53 // The currently showing metadata. | 53 // The currently showing metadata. |
54 private MediaMetadata mCurrentMetadata = null; | 54 private MediaMetadata mCurrentMetadata = null; |
55 private MediaImageManager mMediaImageManager = null; | 55 private MediaImageManager mMediaImageManager = null; |
56 | 56 |
57 private MediaNotificationListener mControlsListener = new MediaNotificationL istener() { | 57 private MediaNotificationListener mControlsListener = new MediaNotificationL istener() { |
58 @Override | 58 @Override |
59 public void onPlay(int actionSource) { | 59 public void onPlay(int actionSource) { |
60 MediaSessionUMA | 60 MediaSessionUMA |
61 .recordPlay(MediaSessionTabHelper.convertMediaActionSourceTo UMA(actionSource)); | 61 .recordPlay(MediaSessionTabHelper.convertMediaActionSourceTo UMA(actionSource)); |
62 | 62 if (mMediaSessionDelegate != null) mMediaSessionDelegate.resumeMedia Session(); |
63 mWebContents.resumeMediaSession(); | |
64 } | 63 } |
65 | 64 |
66 @Override | 65 @Override |
67 public void onPause(int actionSource) { | 66 public void onPause(int actionSource) { |
68 MediaSessionUMA.recordPause( | 67 MediaSessionUMA.recordPause( |
69 MediaSessionTabHelper.convertMediaActionSourceToUMA(actionSo urce)); | 68 MediaSessionTabHelper.convertMediaActionSourceToUMA(actionSo urce)); |
70 | 69 if (mMediaSessionDelegate != null) mMediaSessionDelegate.suspendMedi aSession(); |
71 mWebContents.suspendMediaSession(); | |
72 } | 70 } |
73 | 71 |
74 @Override | 72 @Override |
75 public void onStop(int actionSource) { | 73 public void onStop(int actionSource) { |
76 MediaSessionUMA | 74 MediaSessionUMA |
77 .recordStop(MediaSessionTabHelper.convertMediaActionSourceTo UMA(actionSource)); | 75 .recordStop(MediaSessionTabHelper.convertMediaActionSourceTo UMA(actionSource)); |
78 | 76 if (mMediaSessionDelegate != null) mMediaSessionDelegate.stopMediaSe ssion(); |
79 mWebContents.stopMediaSession(); | |
80 } | 77 } |
81 }; | 78 }; |
82 | 79 |
83 void hideNotification() { | 80 void hideNotification() { |
84 if (mTab == null) { | 81 if (mTab == null) { |
85 return; | 82 return; |
86 } | 83 } |
87 MediaNotificationManager.hide(mTab.getId(), R.id.media_playback_notifica tion); | 84 MediaNotificationManager.hide(mTab.getId(), R.id.media_playback_notifica tion); |
88 Activity activity = getActivityFromTab(mTab); | 85 Activity activity = getActivityFromTab(mTab); |
89 if (activity != null) { | 86 if (activity != null) { |
90 activity.setVolumeControlStream(mPreviousVolumeControlStream); | 87 activity.setVolumeControlStream(mPreviousVolumeControlStream); |
91 } | 88 } |
92 mNotificationInfoBuilder = null; | 89 mNotificationInfoBuilder = null; |
93 } | 90 } |
94 | 91 |
95 private WebContentsObserver createWebContentsObserver(WebContents webContent s) { | 92 private MediaSessionDelegate createMediaSessionDelegate(WebContents webConte nts) { |
96 return new WebContentsObserver(webContents) { | 93 MediaSessionDelegate delegate = new MediaSessionDelegate() { |
97 @Override | 94 @Override |
98 public void destroy() { | 95 public void mediaSessionDisconnected() { |
99 hideNotification(); | 96 hideNotification(); |
100 super.destroy(); | |
101 } | 97 } |
102 | 98 |
103 @Override | 99 @Override |
104 public void mediaSessionStateChanged(boolean isControllable, boolean isPaused) { | 100 public void mediaSessionStateChanged(boolean isControllable, boolean isPaused) { |
101 Log.i(TAG, "Android MediaSessionStateChanged()"); | |
105 if (!isControllable) { | 102 if (!isControllable) { |
106 hideNotification(); | 103 hideNotification(); |
107 return; | 104 return; |
108 } | 105 } |
109 | 106 |
110 Intent contentIntent = Tab.createBringTabToFrontIntent(mTab.getI d()); | 107 Intent contentIntent = Tab.createBringTabToFrontIntent(mTab.getI d()); |
111 if (contentIntent != null) { | 108 if (contentIntent != null) { |
112 contentIntent.putExtra(MediaNotificationUma.INTENT_EXTRA_NAM E, | 109 contentIntent.putExtra(MediaNotificationUma.INTENT_EXTRA_NAM E, |
113 MediaNotificationUma.SOURCE_MEDIA); | 110 MediaNotificationUma.SOURCE_MEDIA); |
114 } | 111 } |
(...skipping 28 matching lines...) Expand all Loading... | |
143 @Override | 140 @Override |
144 public void mediaSessionMetadataChanged(MediaMetadata metadata) { | 141 public void mediaSessionMetadataChanged(MediaMetadata metadata) { |
145 mPageMetadata = metadata; | 142 mPageMetadata = metadata; |
146 if (mPageMetadata != null) { | 143 if (mPageMetadata != null) { |
147 mMediaImageManager.downloadImage(mPageMetadata.getArtwork(), | 144 mMediaImageManager.downloadImage(mPageMetadata.getArtwork(), |
148 MediaSessionTabHelper.this); | 145 MediaSessionTabHelper.this); |
149 } | 146 } |
150 updateNotificationMetadata(); | 147 updateNotificationMetadata(); |
151 } | 148 } |
152 }; | 149 }; |
150 Log.i(TAG, "createMediaSessionDelegate()"); | |
151 webContents.addMediaSessionDelegate(delegate); | |
152 return delegate; | |
153 } | 153 } |
154 | 154 |
155 private void setWebContents(WebContents webContents) { | 155 private void setWebContents(WebContents webContents) { |
156 if (mWebContents == webContents) return; | 156 if (mWebContents == webContents) return; |
157 | 157 |
158 cleanupWebContents(); | 158 cleanupWebContents(); |
159 mWebContents = webContents; | 159 mWebContents = webContents; |
160 if (mWebContents != null) mWebContentsObserver = createWebContentsObserv er(mWebContents); | 160 if (mWebContents != null) { |
161 if (mMediaSessionDelegate != null) mMediaSessionDelegate.unlinkMedia Session(); | |
162 | |
163 mMediaSessionDelegate = createMediaSessionDelegate(mWebContents); | |
164 } | |
161 mMediaImageManager.setWebContents(mWebContents); | 165 mMediaImageManager.setWebContents(mWebContents); |
162 } | 166 } |
163 | 167 |
164 private void cleanupWebContents() { | 168 private void cleanupWebContents() { |
165 if (mWebContentsObserver != null) mWebContentsObserver.destroy(); | 169 if (mMediaSessionDelegate != null) mMediaSessionDelegate.unlinkMediaSess ion(); |
166 mWebContentsObserver = null; | 170 mMediaSessionDelegate = null; |
167 mWebContents = null; | 171 mWebContents = null; |
168 } | 172 } |
169 | 173 |
170 private final TabObserver mTabObserver = new EmptyTabObserver() { | 174 private final TabObserver mTabObserver = new EmptyTabObserver() { |
171 @Override | 175 @Override |
172 public void onContentChanged(Tab tab) { | 176 public void onContentChanged(Tab tab) { |
173 assert tab == mTab; | 177 assert tab == mTab; |
174 setWebContents(tab.getWebContents()); | 178 setWebContents(tab.getWebContents()); |
175 } | 179 } |
176 | 180 |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
364 if (mNotificationInfoBuilder == null) return; | 368 if (mNotificationInfoBuilder == null) return; |
365 mNotificationInfoBuilder.setLargeIcon(mCurrentMediaImage); | 369 mNotificationInfoBuilder.setLargeIcon(mCurrentMediaImage); |
366 MediaNotificationManager.show( | 370 MediaNotificationManager.show( |
367 ContextUtils.getApplicationContext(), mNotificationInfoBuilder.b uild()); | 371 ContextUtils.getApplicationContext(), mNotificationInfoBuilder.b uild()); |
368 } | 372 } |
369 | 373 |
370 private Bitmap getNotificationImage() { | 374 private Bitmap getNotificationImage() { |
371 return (mPageMediaImage != null) ? mPageMediaImage : mFavicon; | 375 return (mPageMediaImage != null) ? mPageMediaImage : mFavicon; |
372 } | 376 } |
373 } | 377 } |
OLD | NEW |