blob: 683c7934908ba243e7b234508d99ff74a9cc2a60 [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;
22import android.content.Context;
23import android.content.Intent;
Beth Thibodeau23a33ab2020-04-07 20:51:57 -040024import android.content.SharedPreferences;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050025import android.content.pm.PackageManager;
26import android.content.pm.ResolveInfo;
27import android.content.res.ColorStateList;
28import android.graphics.Bitmap;
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -040029import android.graphics.ImageDecoder;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050030import android.graphics.drawable.Drawable;
31import android.graphics.drawable.GradientDrawable;
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -040032import android.graphics.drawable.Icon;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050033import android.graphics.drawable.RippleDrawable;
Beth Thibodeau23a33ab2020-04-07 20:51:57 -040034import android.media.MediaDescription;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050035import android.media.MediaMetadata;
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -040036import android.media.ThumbnailUtils;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050037import android.media.session.MediaController;
38import android.media.session.MediaSession;
39import android.media.session.PlaybackState;
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -040040import android.net.Uri;
Beth Thibodeau23a33ab2020-04-07 20:51:57 -040041import android.service.media.MediaBrowserService;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050042import android.util.Log;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050043import android.view.LayoutInflater;
44import android.view.View;
Robert Snoeberger3cc22222020-03-25 15:36:31 -040045import android.view.View.OnAttachStateChangeListener;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050046import android.view.ViewGroup;
47import android.widget.ImageButton;
48import android.widget.ImageView;
49import android.widget.LinearLayout;
50import android.widget.TextView;
51
Robert Snoeberger9a7409b2020-04-09 18:12:27 -040052import androidx.annotation.Nullable;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050053import androidx.core.graphics.drawable.RoundedBitmapDrawable;
54import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
55
Robert Snoeberger9a7409b2020-04-09 18:12:27 -040056import com.android.settingslib.media.LocalMediaManager;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050057import com.android.settingslib.media.MediaDevice;
58import com.android.settingslib.media.MediaOutputSliceConstants;
59import com.android.settingslib.widget.AdaptiveIcon;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050060import com.android.systemui.R;
61import com.android.systemui.plugins.ActivityStarter;
Beth Thibodeau23a33ab2020-04-07 20:51:57 -040062import com.android.systemui.qs.QSMediaBrowser;
Robert Snoeberger3cc22222020-03-25 15:36:31 -040063import com.android.systemui.util.Assert;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050064
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -040065import java.io.IOException;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050066import java.util.List;
67import java.util.concurrent.Executor;
68
69/**
70 * Base media control panel for System UI
71 */
Robert Snoeberger3cc22222020-03-25 15:36:31 -040072public class MediaControlPanel {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050073 private static final String TAG = "MediaControlPanel";
Robert Snoeberger9a7409b2020-04-09 18:12:27 -040074 @Nullable private final LocalMediaManager mLocalMediaManager;
Beth Thibodeaua51c3142020-03-17 17:27:04 -040075 private final Executor mForegroundExecutor;
Beth Thibodeau23a33ab2020-04-07 20:51:57 -040076 protected final Executor mBackgroundExecutor;
Beth Thibodeaue561c002020-04-23 17:33:00 -040077 private final ActivityStarter mActivityStarter;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050078
79 private Context mContext;
80 protected LinearLayout mMediaNotifView;
81 private View mSeamless;
82 private MediaSession.Token mToken;
83 private MediaController mController;
84 private int mForegroundColor;
85 private int mBackgroundColor;
Robert Snoeberger9a7409b2020-04-09 18:12:27 -040086 private MediaDevice mDevice;
Beth Thibodeau23a33ab2020-04-07 20:51:57 -040087 protected ComponentName mServiceComponent;
Robert Snoeberger3cc22222020-03-25 15:36:31 -040088 private boolean mIsRegistered = false;
Beth Thibodeaua3d90982020-04-13 23:42:48 -040089 private String mKey;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050090
91 private final int[] mActionIds;
92
Beth Thibodeau23a33ab2020-04-07 20:51:57 -040093 public static final String MEDIA_PREFERENCES = "media_control_prefs";
94 public static final String MEDIA_PREFERENCE_KEY = "browser_components";
95 private SharedPreferences mSharedPrefs;
96 private boolean mCheckedForResumption = false;
97
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050098 // Button IDs used in notifications
99 protected static final int[] NOTIF_ACTION_IDS = {
100 com.android.internal.R.id.action0,
101 com.android.internal.R.id.action1,
102 com.android.internal.R.id.action2,
103 com.android.internal.R.id.action3,
104 com.android.internal.R.id.action4
105 };
106
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400107 // URI fields to try loading album art from
108 private static final String[] ART_URIS = {
109 MediaMetadata.METADATA_KEY_ALBUM_ART_URI,
110 MediaMetadata.METADATA_KEY_ART_URI,
111 MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI
112 };
113
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400114 private final MediaController.Callback mSessionCallback = new MediaController.Callback() {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500115 @Override
116 public void onSessionDestroyed() {
117 Log.d(TAG, "session destroyed");
118 mController.unregisterCallback(mSessionCallback);
119 clearControls();
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400120 makeInactive();
121 }
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400122 @Override
Robert Snoeberger445d4412020-04-15 00:03:13 -0400123 public void onPlaybackStateChanged(PlaybackState state) {
124 final int s = state != null ? state.getState() : PlaybackState.STATE_NONE;
125 // When the playback state is NONE or CONNECTING, transition the player to the
126 // resumption state. State CONNECTING needs to be considered for Cast sessions. Ending
127 // a cast session in YT results in the CONNECTING state, which makes sense if you
128 // thinking of the session as waiting to connect to another cast device.
129 if (s == PlaybackState.STATE_NONE || s == PlaybackState.STATE_CONNECTING) {
130 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
248 String pkgName = mController.getPackageName();
249 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 Snoeberger9a7409b2020-04-09 18:12:27 -0400284 if (mSeamless != null && mLocalMediaManager != null) {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500285 mSeamless.setVisibility(View.VISIBLE);
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400286 updateDevice(mLocalMediaManager.getCurrentConnectedDevice());
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500287 mSeamless.setOnClickListener(v -> {
288 final Intent intent = new Intent()
289 .setAction(MediaOutputSliceConstants.ACTION_MEDIA_OUTPUT)
290 .putExtra(MediaOutputSliceConstants.EXTRA_PACKAGE_NAME,
291 mController.getPackageName())
292 .putExtra(MediaOutputSliceConstants.KEY_MEDIA_SESSION_TOKEN, mToken);
293 mActivityStarter.startActivity(intent, false, true /* dismissShade */,
294 Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
295 });
296 }
297
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400298 makeActive();
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400299
300 // App title (not in mini player)
301 TextView appName = mMediaNotifView.findViewById(R.id.app_name);
302 if (appName != null) {
303 appName.setText(appNameString);
304 appName.setTextColor(mForegroundColor);
305 }
306
307 MediaMetadata mediaMetadata = mController.getMetadata();
308 if (mediaMetadata == null) {
309 Log.e(TAG, "Media metadata was null");
310 return;
311 }
312
313 ImageView albumView = mMediaNotifView.findViewById(R.id.album_art);
314 if (albumView != null) {
315 // Resize art in a background thread
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400316 mBackgroundExecutor.execute(() -> processAlbumArt(mediaMetadata, largeIcon, albumView));
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400317 }
318
319 // Song name
320 TextView titleText = mMediaNotifView.findViewById(R.id.header_title);
321 String songName = mediaMetadata.getString(MediaMetadata.METADATA_KEY_TITLE);
322 titleText.setText(songName);
323 titleText.setTextColor(mForegroundColor);
324
325 // Artist name (not in mini player)
326 TextView artistText = mMediaNotifView.findViewById(R.id.header_artist);
327 if (artistText != null) {
328 String artistName = mediaMetadata.getString(MediaMetadata.METADATA_KEY_ARTIST);
329 artistText.setText(artistName);
330 artistText.setTextColor(mForegroundColor);
331 }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500332 }
333
334 /**
335 * Return the token for the current media session
336 * @return the token
337 */
338 public MediaSession.Token getMediaSessionToken() {
339 return mToken;
340 }
341
342 /**
343 * Get the current media controller
344 * @return the controller
345 */
346 public MediaController getController() {
347 return mController;
348 }
349
350 /**
351 * Get the name of the package associated with the current media controller
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400352 * @return the package name, or null if no controller
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500353 */
354 public String getMediaPlayerPackage() {
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400355 if (mController == null) {
356 return null;
357 }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500358 return mController.getPackageName();
359 }
360
361 /**
Beth Thibodeaua3d90982020-04-13 23:42:48 -0400362 * Return the original notification's key
363 * @return The notification key
364 */
365 public String getKey() {
366 return mKey;
367 }
368
369 /**
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500370 * Check whether this player has an attached media session.
371 * @return whether there is a controller with a current media session.
372 */
373 public boolean hasMediaSession() {
374 return mController != null && mController.getPlaybackState() != null;
375 }
376
377 /**
378 * Check whether the media controlled by this player is currently playing
379 * @return whether it is playing, or false if no controller information
380 */
381 public boolean isPlaying() {
382 return isPlaying(mController);
383 }
384
385 /**
386 * Check whether the given controller is currently playing
387 * @param controller media controller to check
388 * @return whether it is playing, or false if no controller information
389 */
390 protected boolean isPlaying(MediaController controller) {
391 if (controller == null) {
392 return false;
393 }
394
395 PlaybackState state = controller.getPlaybackState();
396 if (state == null) {
397 return false;
398 }
399
400 return (state.getState() == PlaybackState.STATE_PLAYING);
401 }
402
403 /**
404 * Process album art for layout
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400405 * @param description media description
406 * @param albumView view to hold the album art
407 */
408 protected void processAlbumArt(MediaDescription description, ImageView albumView) {
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400409 Bitmap albumArt = null;
410
411 // First try loading from URI
412 albumArt = loadBitmapFromUri(description.getIconUri());
413
414 // Then check bitmap
415 if (albumArt == null) {
416 albumArt = description.getIconBitmap();
417 }
418
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400419 processAlbumArtInternal(albumArt, albumView);
420 }
421
422 /**
423 * Process album art for layout
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500424 * @param metadata media metadata
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400425 * @param largeIcon from notification, checked as a fallback if metadata does not have art
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500426 * @param albumView view to hold the album art
427 */
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400428 private void processAlbumArt(MediaMetadata metadata, Icon largeIcon, ImageView albumView) {
429 Bitmap albumArt = null;
430
431 // First look in URI fields
432 for (String field : ART_URIS) {
433 String uriString = metadata.getString(field);
434 if (uriString != null) {
435 albumArt = loadBitmapFromUri(Uri.parse(uriString));
436 if (albumArt != null) {
437 Log.d(TAG, "loaded art from " + field);
438 break;
439 }
440 }
441 }
442
443 // Then check bitmap field
444 if (albumArt == null) {
445 albumArt = metadata.getBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART);
446 }
447
448 // Finally try the notification's largeIcon
449 if (albumArt == null && largeIcon != null) {
450 albumArt = largeIcon.getBitmap();
451 }
452
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400453 processAlbumArtInternal(albumArt, albumView);
454 }
455
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400456 /**
457 * Load a bitmap from a URI
458 * @param uri
459 * @return bitmap, or null if couldn't be loaded
460 */
461 private Bitmap loadBitmapFromUri(Uri uri) {
462 ImageDecoder.Source source = ImageDecoder.createSource(mContext.getContentResolver(), uri);
463 try {
464 return ImageDecoder.decodeBitmap(source);
465 } catch (IOException e) {
466 e.printStackTrace();
467 return null;
468 }
469 }
470
471 /**
472 * Resize and crop the image if provided and update the control view
473 * @param albumArt Bitmap of art to display, or null to hide view
474 * @param albumView View that will hold the art
475 */
476 private void processAlbumArtInternal(@Nullable Bitmap albumArt, ImageView albumView) {
477 // Resize
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500478 RoundedBitmapDrawable roundedDrawable = null;
479 if (albumArt != null) {
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400480 float radius = mContext.getResources().getDimension(R.dimen.qs_media_corner_radius);
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500481 Bitmap original = albumArt.copy(Bitmap.Config.ARGB_8888, true);
482 int albumSize = (int) mContext.getResources().getDimension(
483 R.dimen.qs_media_album_size);
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400484 Bitmap scaled = ThumbnailUtils.extractThumbnail(original, albumSize, albumSize);
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500485 roundedDrawable = RoundedBitmapDrawableFactory.create(mContext.getResources(), scaled);
486 roundedDrawable.setCornerRadius(radius);
487 } else {
488 Log.e(TAG, "No album art available");
489 }
490
491 // Now that it's resized, update the UI
492 final RoundedBitmapDrawable result = roundedDrawable;
Beth Thibodeaua51c3142020-03-17 17:27:04 -0400493 mForegroundExecutor.execute(() -> {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500494 if (result != null) {
495 albumView.setImageDrawable(result);
496 albumView.setVisibility(View.VISIBLE);
497 } else {
498 albumView.setImageDrawable(null);
499 albumView.setVisibility(View.GONE);
500 }
501 });
502 }
503
504 /**
505 * Update the current device information
506 * @param device device information to display
507 */
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400508 private void updateDevice(MediaDevice device) {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500509 if (mSeamless == null) {
510 return;
511 }
Beth Thibodeaua51c3142020-03-17 17:27:04 -0400512 mForegroundExecutor.execute(() -> {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500513 updateChipInternal(device);
514 });
515 }
516
517 private void updateChipInternal(MediaDevice device) {
518 ColorStateList fgTintList = ColorStateList.valueOf(mForegroundColor);
519
520 // Update the outline color
521 LinearLayout viewLayout = (LinearLayout) mSeamless;
522 RippleDrawable bkgDrawable = (RippleDrawable) viewLayout.getBackground();
523 GradientDrawable rect = (GradientDrawable) bkgDrawable.getDrawable(0);
524 rect.setStroke(2, mForegroundColor);
525 rect.setColor(mBackgroundColor);
526
527 ImageView iconView = mSeamless.findViewById(R.id.media_seamless_image);
528 TextView deviceName = mSeamless.findViewById(R.id.media_seamless_text);
529 deviceName.setTextColor(fgTintList);
530
531 if (device != null) {
532 Drawable icon = device.getIcon();
533 iconView.setVisibility(View.VISIBLE);
534 iconView.setImageTintList(fgTintList);
535
536 if (icon instanceof AdaptiveIcon) {
537 AdaptiveIcon aIcon = (AdaptiveIcon) icon;
538 aIcon.setBackgroundColor(mBackgroundColor);
539 iconView.setImageDrawable(aIcon);
540 } else {
541 iconView.setImageDrawable(icon);
542 }
543 deviceName.setText(device.getName());
544 } else {
545 // Reset to default
546 iconView.setVisibility(View.GONE);
547 deviceName.setText(com.android.internal.R.string.ext_media_seamless_action);
548 }
549 }
550
551 /**
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400552 * Puts controls into a resumption state if possible, or calls removePlayer if no component was
553 * found that could resume playback
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500554 */
555 public void clearControls() {
Robert Snoeberger445d4412020-04-15 00:03:13 -0400556 Log.d(TAG, "clearControls to resumption state package=" + getMediaPlayerPackage());
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400557 if (mServiceComponent == null) {
558 // If we don't have a way to resume, just remove the player altogether
559 Log.d(TAG, "Removing unresumable controls");
560 removePlayer();
561 return;
562 }
563 resetButtons();
564 }
565
566 /**
567 * Hide the media buttons and show only a restart button
568 */
569 protected void resetButtons() {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500570 // Hide all the old buttons
571 for (int i = 0; i < mActionIds.length; i++) {
572 ImageButton thisBtn = mMediaNotifView.findViewById(mActionIds[i]);
573 if (thisBtn != null) {
574 thisBtn.setVisibility(View.GONE);
575 }
576 }
577
578 // Add a restart button
579 ImageButton btn = mMediaNotifView.findViewById(mActionIds[0]);
580 btn.setOnClickListener(v -> {
581 Log.d(TAG, "Attempting to restart session");
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400582 QSMediaBrowser browser = new QSMediaBrowser(mContext, null, mServiceComponent);
583 browser.restart();
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500584 });
585 btn.setImageDrawable(mContext.getResources().getDrawable(R.drawable.lb_ic_play));
586 btn.setImageTintList(ColorStateList.valueOf(mForegroundColor));
587 btn.setVisibility(View.VISIBLE);
588 }
589
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400590 private void makeActive() {
591 Assert.isMainThread();
592 if (!mIsRegistered) {
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400593 if (mLocalMediaManager != null) {
594 mLocalMediaManager.registerCallback(mDeviceCallback);
595 mLocalMediaManager.startScan();
596 }
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400597 mIsRegistered = true;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500598 }
599 }
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400600
601 private void makeInactive() {
602 Assert.isMainThread();
603 if (mIsRegistered) {
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400604 if (mLocalMediaManager != null) {
605 mLocalMediaManager.stopScan();
606 mLocalMediaManager.unregisterCallback(mDeviceCallback);
607 }
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400608 mIsRegistered = false;
609 }
610 }
611
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400612 /**
613 * Verify that we can connect to the given component with a MediaBrowser, and if so, add that
614 * component to the list of resumption components
615 */
616 private void tryUpdateResumptionList(ComponentName componentName) {
617 Log.d(TAG, "Testing if we can connect to " + componentName);
618 QSMediaBrowser.testConnection(mContext,
619 new QSMediaBrowser.Callback() {
620 @Override
621 public void onConnected() {
622 Log.d(TAG, "yes we can resume with " + componentName);
623 mServiceComponent = componentName;
624 updateResumptionList(componentName);
625 }
626
627 @Override
628 public void onError() {
629 Log.d(TAG, "Cannot resume with " + componentName);
630 mServiceComponent = null;
Beth Thibodeau89f5c762020-04-21 13:09:55 -0400631 if (!hasMediaSession()) {
632 // If it's not active and we can't resume, remove
633 removePlayer();
634 }
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400635 }
636 },
637 componentName);
638 }
639
640 /**
641 * Add the component to the saved list of media browser services, checking for duplicates and
642 * removing older components that exceed the maximum limit
643 * @param componentName
644 */
645 private synchronized void updateResumptionList(ComponentName componentName) {
646 // Add to front of saved list
647 if (mSharedPrefs == null) {
648 mSharedPrefs = mContext.getSharedPreferences(MEDIA_PREFERENCES, 0);
649 }
650 String componentString = componentName.flattenToString();
651 String listString = mSharedPrefs.getString(MEDIA_PREFERENCE_KEY, null);
652 if (listString == null) {
653 listString = componentString;
654 } else {
655 String[] components = listString.split(QSMediaBrowser.DELIMITER);
656 StringBuilder updated = new StringBuilder(componentString);
657 int nBrowsers = 1;
658 for (int i = 0; i < components.length
659 && nBrowsers < QSMediaBrowser.MAX_RESUMPTION_CONTROLS; i++) {
660 if (componentString.equals(components[i])) {
661 continue;
662 }
663 updated.append(QSMediaBrowser.DELIMITER).append(components[i]);
664 nBrowsers++;
665 }
666 listString = updated.toString();
667 }
668 mSharedPrefs.edit().putString(MEDIA_PREFERENCE_KEY, listString).apply();
669 }
670
671 /**
672 * Called when a player can't be resumed to give it an opportunity to hide or remove itself
673 */
674 protected void removePlayer() { }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500675}