blob: 9509e6d479e37c10acde78fa0f3913a2e0adee64 [file] [log] [blame]
Beth Thibodeau7b6c1782020-03-05 11:43:51 -05001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.systemui.media;
18
19import android.annotation.LayoutRes;
20import android.app.PendingIntent;
21import android.content.ComponentName;
Beth Thibodeaudba74bc2020-04-27 14:17:08 -040022import android.content.ContentResolver;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050023import android.content.Context;
24import android.content.Intent;
Beth Thibodeau23a33ab2020-04-07 20:51:57 -040025import android.content.SharedPreferences;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050026import android.content.pm.PackageManager;
27import android.content.pm.ResolveInfo;
28import android.content.res.ColorStateList;
29import android.graphics.Bitmap;
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -040030import android.graphics.ImageDecoder;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050031import android.graphics.drawable.Drawable;
32import android.graphics.drawable.GradientDrawable;
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -040033import android.graphics.drawable.Icon;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050034import android.graphics.drawable.RippleDrawable;
Beth Thibodeau23a33ab2020-04-07 20:51:57 -040035import android.media.MediaDescription;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050036import android.media.MediaMetadata;
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -040037import android.media.ThumbnailUtils;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050038import android.media.session.MediaController;
Robert Snoebergerc981dc92020-04-27 15:00:50 -040039import android.media.session.MediaController.PlaybackInfo;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050040import android.media.session.MediaSession;
41import android.media.session.PlaybackState;
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -040042import android.net.Uri;
Beth Thibodeau23a33ab2020-04-07 20:51:57 -040043import android.service.media.MediaBrowserService;
Beth Thibodeaudba74bc2020-04-27 14:17:08 -040044import android.text.TextUtils;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050045import android.util.Log;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050046import android.view.LayoutInflater;
47import android.view.View;
Robert Snoeberger3cc22222020-03-25 15:36:31 -040048import android.view.View.OnAttachStateChangeListener;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050049import android.view.ViewGroup;
50import android.widget.ImageButton;
51import android.widget.ImageView;
52import android.widget.LinearLayout;
53import android.widget.TextView;
54
Robert Snoeberger9a7409b2020-04-09 18:12:27 -040055import androidx.annotation.Nullable;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050056import androidx.core.graphics.drawable.RoundedBitmapDrawable;
57import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
58
Robert Snoeberger9a7409b2020-04-09 18:12:27 -040059import com.android.settingslib.media.LocalMediaManager;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050060import com.android.settingslib.media.MediaDevice;
61import com.android.settingslib.media.MediaOutputSliceConstants;
62import com.android.settingslib.widget.AdaptiveIcon;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050063import com.android.systemui.R;
64import com.android.systemui.plugins.ActivityStarter;
Beth Thibodeau23a33ab2020-04-07 20:51:57 -040065import com.android.systemui.qs.QSMediaBrowser;
Robert Snoeberger3cc22222020-03-25 15:36:31 -040066import com.android.systemui.util.Assert;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050067
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -040068import java.io.IOException;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050069import java.util.List;
70import java.util.concurrent.Executor;
71
72/**
73 * Base media control panel for System UI
74 */
Robert Snoeberger3cc22222020-03-25 15:36:31 -040075public class MediaControlPanel {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050076 private static final String TAG = "MediaControlPanel";
Robert Snoeberger9a7409b2020-04-09 18:12:27 -040077 @Nullable private final LocalMediaManager mLocalMediaManager;
Beth Thibodeaua51c3142020-03-17 17:27:04 -040078 private final Executor mForegroundExecutor;
Beth Thibodeau23a33ab2020-04-07 20:51:57 -040079 protected final Executor mBackgroundExecutor;
Beth Thibodeaue561c002020-04-23 17:33:00 -040080 private final ActivityStarter mActivityStarter;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050081
82 private Context mContext;
83 protected LinearLayout mMediaNotifView;
84 private View mSeamless;
85 private MediaSession.Token mToken;
86 private MediaController mController;
87 private int mForegroundColor;
88 private int mBackgroundColor;
Robert Snoeberger9a7409b2020-04-09 18:12:27 -040089 private MediaDevice mDevice;
Beth Thibodeau23a33ab2020-04-07 20:51:57 -040090 protected ComponentName mServiceComponent;
Robert Snoeberger3cc22222020-03-25 15:36:31 -040091 private boolean mIsRegistered = false;
Beth Thibodeaua3d90982020-04-13 23:42:48 -040092 private String mKey;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050093
94 private final int[] mActionIds;
95
Beth Thibodeau23a33ab2020-04-07 20:51:57 -040096 public static final String MEDIA_PREFERENCES = "media_control_prefs";
97 public static final String MEDIA_PREFERENCE_KEY = "browser_components";
98 private SharedPreferences mSharedPrefs;
99 private boolean mCheckedForResumption = false;
Robert Snoebergerc981dc92020-04-27 15:00:50 -0400100 private boolean mIsRemotePlayback;
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400101
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500102 // Button IDs used in notifications
103 protected static final int[] NOTIF_ACTION_IDS = {
104 com.android.internal.R.id.action0,
105 com.android.internal.R.id.action1,
106 com.android.internal.R.id.action2,
107 com.android.internal.R.id.action3,
108 com.android.internal.R.id.action4
109 };
110
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400111 // URI fields to try loading album art from
112 private static final String[] ART_URIS = {
113 MediaMetadata.METADATA_KEY_ALBUM_ART_URI,
114 MediaMetadata.METADATA_KEY_ART_URI,
115 MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI
116 };
117
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400118 private final MediaController.Callback mSessionCallback = new MediaController.Callback() {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500119 @Override
120 public void onSessionDestroyed() {
121 Log.d(TAG, "session destroyed");
122 mController.unregisterCallback(mSessionCallback);
123 clearControls();
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400124 makeInactive();
125 }
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400126 @Override
Robert Snoeberger445d4412020-04-15 00:03:13 -0400127 public void onPlaybackStateChanged(PlaybackState state) {
128 final int s = state != null ? state.getState() : PlaybackState.STATE_NONE;
Beth Thibodeaud664de22020-04-28 16:29:36 -0400129 if (s == PlaybackState.STATE_NONE) {
Robert Snoeberger445d4412020-04-15 00:03:13 -0400130 Log.d(TAG, "playback state change will trigger resumption, state=" + state);
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400131 clearControls();
132 makeInactive();
133 }
134 }
135 };
136
137 private final OnAttachStateChangeListener mStateListener = new OnAttachStateChangeListener() {
138 @Override
139 public void onViewAttachedToWindow(View unused) {
140 makeActive();
141 }
142 @Override
143 public void onViewDetachedFromWindow(View unused) {
144 makeInactive();
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500145 }
146 };
147
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400148 private final LocalMediaManager.DeviceCallback mDeviceCallback =
149 new LocalMediaManager.DeviceCallback() {
150 @Override
151 public void onDeviceListUpdate(List<MediaDevice> devices) {
152 if (mLocalMediaManager == null) {
153 return;
154 }
155 MediaDevice currentDevice = mLocalMediaManager.getCurrentConnectedDevice();
156 // Check because this can be called several times while changing devices
157 if (mDevice == null || !mDevice.equals(currentDevice)) {
158 mDevice = currentDevice;
159 updateDevice(mDevice);
160 }
161 }
162
163 @Override
164 public void onSelectedDeviceStateChanged(MediaDevice device, int state) {
165 if (mDevice == null || !mDevice.equals(device)) {
166 mDevice = device;
167 updateDevice(mDevice);
168 }
169 }
170 };
171
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500172 /**
173 * Initialize a new control panel
174 * @param context
175 * @param parent
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400176 * @param routeManager Manager used to listen for device change events.
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500177 * @param layoutId layout resource to use for this control panel
178 * @param actionIds resource IDs for action buttons in the layout
Beth Thibodeaua51c3142020-03-17 17:27:04 -0400179 * @param foregroundExecutor foreground executor
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500180 * @param backgroundExecutor background executor, used for processing artwork
Beth Thibodeaue561c002020-04-23 17:33:00 -0400181 * @param activityStarter activity starter
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500182 */
Robert Snoeberger445d4412020-04-15 00:03:13 -0400183 public MediaControlPanel(Context context, ViewGroup parent,
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400184 @Nullable LocalMediaManager routeManager, @LayoutRes int layoutId, int[] actionIds,
Beth Thibodeaue561c002020-04-23 17:33:00 -0400185 Executor foregroundExecutor, Executor backgroundExecutor,
186 ActivityStarter activityStarter) {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500187 mContext = context;
188 LayoutInflater inflater = LayoutInflater.from(mContext);
189 mMediaNotifView = (LinearLayout) inflater.inflate(layoutId, parent, false);
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400190 // TODO(b/150854549): removeOnAttachStateChangeListener when this doesn't inflate views
191 // mStateListener shouldn't need to be unregistered since this object shares the same
192 // lifecycle with the inflated view. It would be better, however, if this controller used an
193 // attach/detach of views instead of inflating them in the constructor, which would allow
194 // mStateListener to be unregistered in detach.
195 mMediaNotifView.addOnAttachStateChangeListener(mStateListener);
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400196 mLocalMediaManager = routeManager;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500197 mActionIds = actionIds;
Beth Thibodeaua51c3142020-03-17 17:27:04 -0400198 mForegroundExecutor = foregroundExecutor;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500199 mBackgroundExecutor = backgroundExecutor;
Beth Thibodeaue561c002020-04-23 17:33:00 -0400200 mActivityStarter = activityStarter;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500201 }
202
203 /**
204 * Get the view used to display media controls
205 * @return the view
206 */
207 public View getView() {
208 return mMediaNotifView;
209 }
210
211 /**
212 * Get the context
213 * @return context
214 */
215 public Context getContext() {
216 return mContext;
217 }
218
219 /**
220 * Update the media panel view for the given media session
221 * @param token
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400222 * @param iconDrawable
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400223 * @param largeIcon
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500224 * @param iconColor
225 * @param bgColor
226 * @param contentIntent
227 * @param appNameString
Beth Thibodeaua3d90982020-04-13 23:42:48 -0400228 * @param key
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500229 */
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400230 public void setMediaSession(MediaSession.Token token, Drawable iconDrawable, Icon largeIcon,
231 int iconColor, int bgColor, PendingIntent contentIntent, String appNameString,
232 String key) {
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400233 // Ensure that component names are updated if token has changed
234 if (mToken == null || !mToken.equals(token)) {
235 mToken = token;
236 mServiceComponent = null;
237 mCheckedForResumption = false;
238 }
239
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500240 mForegroundColor = iconColor;
241 mBackgroundColor = bgColor;
242 mController = new MediaController(mContext, mToken);
Beth Thibodeaua3d90982020-04-13 23:42:48 -0400243 mKey = key;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500244
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400245 // Try to find a browser service component for this app
246 // TODO also check for a media button receiver intended for restarting (b/154127084)
247 // Only check if we haven't tried yet or the session token changed
Robert Snoeberger44299172020-04-24 22:22:21 -0400248 final String pkgName = mController.getPackageName();
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400249 if (mServiceComponent == null && !mCheckedForResumption) {
250 Log.d(TAG, "Checking for service component");
251 PackageManager pm = mContext.getPackageManager();
252 Intent resumeIntent = new Intent(MediaBrowserService.SERVICE_INTERFACE);
253 List<ResolveInfo> resumeInfo = pm.queryIntentServices(resumeIntent, 0);
254 if (resumeInfo != null) {
255 for (ResolveInfo inf : resumeInfo) {
256 if (inf.serviceInfo.packageName.equals(mController.getPackageName())) {
257 mBackgroundExecutor.execute(() ->
258 tryUpdateResumptionList(inf.getComponentInfo().getComponentName()));
259 break;
260 }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500261 }
262 }
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400263 mCheckedForResumption = true;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500264 }
265
266 mController.registerCallback(mSessionCallback);
267
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500268 mMediaNotifView.setBackgroundTintList(ColorStateList.valueOf(mBackgroundColor));
269
270 // Click action
Beth Thibodeaua51c3142020-03-17 17:27:04 -0400271 if (contentIntent != null) {
272 mMediaNotifView.setOnClickListener(v -> {
Beth Thibodeaue561c002020-04-23 17:33:00 -0400273 mActivityStarter.postStartActivityDismissingKeyguard(contentIntent);
Beth Thibodeaua51c3142020-03-17 17:27:04 -0400274 });
275 }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500276
277 // App icon
278 ImageView appIcon = mMediaNotifView.findViewById(R.id.icon);
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500279 iconDrawable.setTint(mForegroundColor);
280 appIcon.setImageDrawable(iconDrawable);
281
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500282 // Transfer chip
283 mSeamless = mMediaNotifView.findViewById(R.id.media_seamless);
Robert Snoeberger44299172020-04-24 22:22:21 -0400284 if (mSeamless != null) {
285 if (mLocalMediaManager != null) {
286 mSeamless.setVisibility(View.VISIBLE);
287 updateDevice(mLocalMediaManager.getCurrentConnectedDevice());
288 mSeamless.setOnClickListener(v -> {
289 final Intent intent = new Intent()
290 .setAction(MediaOutputSliceConstants.ACTION_MEDIA_OUTPUT)
291 .putExtra(MediaOutputSliceConstants.EXTRA_PACKAGE_NAME,
292 mController.getPackageName())
293 .putExtra(MediaOutputSliceConstants.KEY_MEDIA_SESSION_TOKEN, mToken);
294 mActivityStarter.startActivity(intent, false, true /* dismissShade */,
295 Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
296 });
297 } else {
298 Log.d(TAG, "LocalMediaManager is null. Not binding output chip for pkg=" + pkgName);
299 }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500300 }
Robert Snoebergerc981dc92020-04-27 15:00:50 -0400301 PlaybackInfo playbackInfo = mController.getPlaybackInfo();
302 if (playbackInfo != null) {
303 mIsRemotePlayback = playbackInfo.getPlaybackType() == PlaybackInfo.PLAYBACK_TYPE_REMOTE;
304 } else {
305 Log.d(TAG, "PlaybackInfo was null. Defaulting to local playback.");
306 mIsRemotePlayback = false;
307 }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500308
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400309 makeActive();
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400310
311 // App title (not in mini player)
312 TextView appName = mMediaNotifView.findViewById(R.id.app_name);
313 if (appName != null) {
314 appName.setText(appNameString);
315 appName.setTextColor(mForegroundColor);
316 }
317
318 MediaMetadata mediaMetadata = mController.getMetadata();
319 if (mediaMetadata == null) {
320 Log.e(TAG, "Media metadata was null");
321 return;
322 }
323
324 ImageView albumView = mMediaNotifView.findViewById(R.id.album_art);
325 if (albumView != null) {
326 // Resize art in a background thread
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400327 mBackgroundExecutor.execute(() -> processAlbumArt(mediaMetadata, largeIcon, albumView));
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400328 }
329
330 // Song name
331 TextView titleText = mMediaNotifView.findViewById(R.id.header_title);
332 String songName = mediaMetadata.getString(MediaMetadata.METADATA_KEY_TITLE);
333 titleText.setText(songName);
334 titleText.setTextColor(mForegroundColor);
335
336 // Artist name (not in mini player)
337 TextView artistText = mMediaNotifView.findViewById(R.id.header_artist);
338 if (artistText != null) {
339 String artistName = mediaMetadata.getString(MediaMetadata.METADATA_KEY_ARTIST);
340 artistText.setText(artistName);
341 artistText.setTextColor(mForegroundColor);
342 }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500343 }
344
345 /**
346 * Return the token for the current media session
347 * @return the token
348 */
349 public MediaSession.Token getMediaSessionToken() {
350 return mToken;
351 }
352
353 /**
354 * Get the current media controller
355 * @return the controller
356 */
357 public MediaController getController() {
358 return mController;
359 }
360
361 /**
362 * Get the name of the package associated with the current media controller
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400363 * @return the package name, or null if no controller
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500364 */
365 public String getMediaPlayerPackage() {
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400366 if (mController == null) {
367 return null;
368 }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500369 return mController.getPackageName();
370 }
371
372 /**
Beth Thibodeaua3d90982020-04-13 23:42:48 -0400373 * Return the original notification's key
374 * @return The notification key
375 */
376 public String getKey() {
377 return mKey;
378 }
379
380 /**
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500381 * Check whether this player has an attached media session.
382 * @return whether there is a controller with a current media session.
383 */
384 public boolean hasMediaSession() {
385 return mController != null && mController.getPlaybackState() != null;
386 }
387
388 /**
389 * Check whether the media controlled by this player is currently playing
390 * @return whether it is playing, or false if no controller information
391 */
392 public boolean isPlaying() {
393 return isPlaying(mController);
394 }
395
396 /**
397 * Check whether the given controller is currently playing
398 * @param controller media controller to check
399 * @return whether it is playing, or false if no controller information
400 */
401 protected boolean isPlaying(MediaController controller) {
402 if (controller == null) {
403 return false;
404 }
405
406 PlaybackState state = controller.getPlaybackState();
407 if (state == null) {
408 return false;
409 }
410
411 return (state.getState() == PlaybackState.STATE_PLAYING);
412 }
413
414 /**
415 * Process album art for layout
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400416 * @param description media description
417 * @param albumView view to hold the album art
418 */
419 protected void processAlbumArt(MediaDescription description, ImageView albumView) {
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400420 Bitmap albumArt = null;
421
422 // First try loading from URI
423 albumArt = loadBitmapFromUri(description.getIconUri());
424
425 // Then check bitmap
426 if (albumArt == null) {
427 albumArt = description.getIconBitmap();
428 }
429
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400430 processAlbumArtInternal(albumArt, albumView);
431 }
432
433 /**
434 * Process album art for layout
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500435 * @param metadata media metadata
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400436 * @param largeIcon from notification, checked as a fallback if metadata does not have art
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500437 * @param albumView view to hold the album art
438 */
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400439 private void processAlbumArt(MediaMetadata metadata, Icon largeIcon, ImageView albumView) {
440 Bitmap albumArt = null;
441
442 // First look in URI fields
443 for (String field : ART_URIS) {
444 String uriString = metadata.getString(field);
Beth Thibodeaudba74bc2020-04-27 14:17:08 -0400445 if (!TextUtils.isEmpty(uriString)) {
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400446 albumArt = loadBitmapFromUri(Uri.parse(uriString));
447 if (albumArt != null) {
448 Log.d(TAG, "loaded art from " + field);
449 break;
450 }
451 }
452 }
453
454 // Then check bitmap field
455 if (albumArt == null) {
456 albumArt = metadata.getBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART);
457 }
458
459 // Finally try the notification's largeIcon
460 if (albumArt == null && largeIcon != null) {
461 albumArt = largeIcon.getBitmap();
462 }
463
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400464 processAlbumArtInternal(albumArt, albumView);
465 }
466
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400467 /**
468 * Load a bitmap from a URI
469 * @param uri
470 * @return bitmap, or null if couldn't be loaded
471 */
472 private Bitmap loadBitmapFromUri(Uri uri) {
Beth Thibodeaudba74bc2020-04-27 14:17:08 -0400473 // ImageDecoder requires a scheme of the following types
474 if (uri.getScheme() == null) {
475 return null;
476 }
477
478 if (!uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)
479 && !uri.getScheme().equals(ContentResolver.SCHEME_ANDROID_RESOURCE)
480 && !uri.getScheme().equals(ContentResolver.SCHEME_FILE)) {
481 return null;
482 }
483
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400484 ImageDecoder.Source source = ImageDecoder.createSource(mContext.getContentResolver(), uri);
485 try {
486 return ImageDecoder.decodeBitmap(source);
487 } catch (IOException e) {
488 e.printStackTrace();
489 return null;
490 }
491 }
492
493 /**
494 * Resize and crop the image if provided and update the control view
495 * @param albumArt Bitmap of art to display, or null to hide view
496 * @param albumView View that will hold the art
497 */
498 private void processAlbumArtInternal(@Nullable Bitmap albumArt, ImageView albumView) {
499 // Resize
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500500 RoundedBitmapDrawable roundedDrawable = null;
501 if (albumArt != null) {
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400502 float radius = mContext.getResources().getDimension(R.dimen.qs_media_corner_radius);
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500503 Bitmap original = albumArt.copy(Bitmap.Config.ARGB_8888, true);
504 int albumSize = (int) mContext.getResources().getDimension(
505 R.dimen.qs_media_album_size);
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400506 Bitmap scaled = ThumbnailUtils.extractThumbnail(original, albumSize, albumSize);
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500507 roundedDrawable = RoundedBitmapDrawableFactory.create(mContext.getResources(), scaled);
508 roundedDrawable.setCornerRadius(radius);
509 } else {
510 Log.e(TAG, "No album art available");
511 }
512
513 // Now that it's resized, update the UI
514 final RoundedBitmapDrawable result = roundedDrawable;
Beth Thibodeaua51c3142020-03-17 17:27:04 -0400515 mForegroundExecutor.execute(() -> {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500516 if (result != null) {
517 albumView.setImageDrawable(result);
518 albumView.setVisibility(View.VISIBLE);
519 } else {
520 albumView.setImageDrawable(null);
521 albumView.setVisibility(View.GONE);
522 }
523 });
524 }
525
526 /**
527 * Update the current device information
528 * @param device device information to display
529 */
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400530 private void updateDevice(MediaDevice device) {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500531 if (mSeamless == null) {
532 return;
533 }
Beth Thibodeaua51c3142020-03-17 17:27:04 -0400534 mForegroundExecutor.execute(() -> {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500535 updateChipInternal(device);
536 });
537 }
538
539 private void updateChipInternal(MediaDevice device) {
540 ColorStateList fgTintList = ColorStateList.valueOf(mForegroundColor);
541
542 // Update the outline color
543 LinearLayout viewLayout = (LinearLayout) mSeamless;
544 RippleDrawable bkgDrawable = (RippleDrawable) viewLayout.getBackground();
545 GradientDrawable rect = (GradientDrawable) bkgDrawable.getDrawable(0);
546 rect.setStroke(2, mForegroundColor);
547 rect.setColor(mBackgroundColor);
548
549 ImageView iconView = mSeamless.findViewById(R.id.media_seamless_image);
550 TextView deviceName = mSeamless.findViewById(R.id.media_seamless_text);
551 deviceName.setTextColor(fgTintList);
552
Robert Snoebergerc981dc92020-04-27 15:00:50 -0400553 if (mIsRemotePlayback) {
554 mSeamless.setEnabled(false);
555 mSeamless.setAlpha(0.38f);
556 iconView.setImageResource(R.drawable.ic_hardware_speaker);
557 iconView.setVisibility(View.VISIBLE);
558 iconView.setImageTintList(fgTintList);
559 deviceName.setText(R.string.media_seamless_remote_device);
560 } else if (device != null) {
561 mSeamless.setEnabled(true);
562 mSeamless.setAlpha(1f);
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500563 Drawable icon = device.getIcon();
564 iconView.setVisibility(View.VISIBLE);
565 iconView.setImageTintList(fgTintList);
566
567 if (icon instanceof AdaptiveIcon) {
568 AdaptiveIcon aIcon = (AdaptiveIcon) icon;
569 aIcon.setBackgroundColor(mBackgroundColor);
570 iconView.setImageDrawable(aIcon);
571 } else {
572 iconView.setImageDrawable(icon);
573 }
574 deviceName.setText(device.getName());
575 } else {
576 // Reset to default
Robert Snoeberger44299172020-04-24 22:22:21 -0400577 Log.d(TAG, "device is null. Not binding output chip.");
Robert Snoebergerc981dc92020-04-27 15:00:50 -0400578 mSeamless.setEnabled(true);
579 mSeamless.setAlpha(1f);
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500580 iconView.setVisibility(View.GONE);
581 deviceName.setText(com.android.internal.R.string.ext_media_seamless_action);
582 }
583 }
584
585 /**
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400586 * Puts controls into a resumption state if possible, or calls removePlayer if no component was
587 * found that could resume playback
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500588 */
589 public void clearControls() {
Robert Snoeberger445d4412020-04-15 00:03:13 -0400590 Log.d(TAG, "clearControls to resumption state package=" + getMediaPlayerPackage());
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400591 if (mServiceComponent == null) {
592 // If we don't have a way to resume, just remove the player altogether
593 Log.d(TAG, "Removing unresumable controls");
594 removePlayer();
595 return;
596 }
597 resetButtons();
598 }
599
600 /**
601 * Hide the media buttons and show only a restart button
602 */
603 protected void resetButtons() {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500604 // Hide all the old buttons
605 for (int i = 0; i < mActionIds.length; i++) {
606 ImageButton thisBtn = mMediaNotifView.findViewById(mActionIds[i]);
607 if (thisBtn != null) {
608 thisBtn.setVisibility(View.GONE);
609 }
610 }
611
612 // Add a restart button
613 ImageButton btn = mMediaNotifView.findViewById(mActionIds[0]);
614 btn.setOnClickListener(v -> {
615 Log.d(TAG, "Attempting to restart session");
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400616 QSMediaBrowser browser = new QSMediaBrowser(mContext, null, mServiceComponent);
617 browser.restart();
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500618 });
619 btn.setImageDrawable(mContext.getResources().getDrawable(R.drawable.lb_ic_play));
620 btn.setImageTintList(ColorStateList.valueOf(mForegroundColor));
621 btn.setVisibility(View.VISIBLE);
622 }
623
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400624 private void makeActive() {
625 Assert.isMainThread();
626 if (!mIsRegistered) {
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400627 if (mLocalMediaManager != null) {
628 mLocalMediaManager.registerCallback(mDeviceCallback);
629 mLocalMediaManager.startScan();
630 }
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400631 mIsRegistered = true;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500632 }
633 }
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400634
635 private void makeInactive() {
636 Assert.isMainThread();
637 if (mIsRegistered) {
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400638 if (mLocalMediaManager != null) {
639 mLocalMediaManager.stopScan();
640 mLocalMediaManager.unregisterCallback(mDeviceCallback);
641 }
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400642 mIsRegistered = false;
643 }
644 }
645
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400646 /**
647 * Verify that we can connect to the given component with a MediaBrowser, and if so, add that
648 * component to the list of resumption components
649 */
650 private void tryUpdateResumptionList(ComponentName componentName) {
651 Log.d(TAG, "Testing if we can connect to " + componentName);
652 QSMediaBrowser.testConnection(mContext,
653 new QSMediaBrowser.Callback() {
654 @Override
655 public void onConnected() {
656 Log.d(TAG, "yes we can resume with " + componentName);
657 mServiceComponent = componentName;
658 updateResumptionList(componentName);
659 }
660
661 @Override
662 public void onError() {
663 Log.d(TAG, "Cannot resume with " + componentName);
664 mServiceComponent = null;
Beth Thibodeau89f5c762020-04-21 13:09:55 -0400665 if (!hasMediaSession()) {
666 // If it's not active and we can't resume, remove
667 removePlayer();
668 }
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400669 }
670 },
671 componentName);
672 }
673
674 /**
675 * Add the component to the saved list of media browser services, checking for duplicates and
676 * removing older components that exceed the maximum limit
677 * @param componentName
678 */
679 private synchronized void updateResumptionList(ComponentName componentName) {
680 // Add to front of saved list
681 if (mSharedPrefs == null) {
682 mSharedPrefs = mContext.getSharedPreferences(MEDIA_PREFERENCES, 0);
683 }
684 String componentString = componentName.flattenToString();
685 String listString = mSharedPrefs.getString(MEDIA_PREFERENCE_KEY, null);
686 if (listString == null) {
687 listString = componentString;
688 } else {
689 String[] components = listString.split(QSMediaBrowser.DELIMITER);
690 StringBuilder updated = new StringBuilder(componentString);
691 int nBrowsers = 1;
692 for (int i = 0; i < components.length
693 && nBrowsers < QSMediaBrowser.MAX_RESUMPTION_CONTROLS; i++) {
694 if (componentString.equals(components[i])) {
695 continue;
696 }
697 updated.append(QSMediaBrowser.DELIMITER).append(components[i]);
698 nBrowsers++;
699 }
700 listString = updated.toString();
701 }
702 mSharedPrefs.edit().putString(MEDIA_PREFERENCE_KEY, listString).apply();
703 }
704
705 /**
706 * Called when a player can't be resumed to give it an opportunity to hide or remove itself
707 */
708 protected void removePlayer() { }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500709}