blob: f6bd948fe11bbd22edd077004d2d6e5c859b1e4a [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;
39import android.media.session.MediaSession;
40import android.media.session.PlaybackState;
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -040041import android.net.Uri;
Beth Thibodeau23a33ab2020-04-07 20:51:57 -040042import android.service.media.MediaBrowserService;
Beth Thibodeaudba74bc2020-04-27 14:17:08 -040043import android.text.TextUtils;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050044import android.util.Log;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050045import android.view.LayoutInflater;
46import android.view.View;
Robert Snoeberger3cc22222020-03-25 15:36:31 -040047import android.view.View.OnAttachStateChangeListener;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050048import android.view.ViewGroup;
49import android.widget.ImageButton;
50import android.widget.ImageView;
51import android.widget.LinearLayout;
52import android.widget.TextView;
53
Robert Snoeberger9a7409b2020-04-09 18:12:27 -040054import androidx.annotation.Nullable;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050055import androidx.core.graphics.drawable.RoundedBitmapDrawable;
56import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
57
Robert Snoeberger9a7409b2020-04-09 18:12:27 -040058import com.android.settingslib.media.LocalMediaManager;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050059import com.android.settingslib.media.MediaDevice;
60import com.android.settingslib.media.MediaOutputSliceConstants;
61import com.android.settingslib.widget.AdaptiveIcon;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050062import com.android.systemui.R;
63import com.android.systemui.plugins.ActivityStarter;
Beth Thibodeau23a33ab2020-04-07 20:51:57 -040064import com.android.systemui.qs.QSMediaBrowser;
Robert Snoeberger3cc22222020-03-25 15:36:31 -040065import com.android.systemui.util.Assert;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050066
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -040067import java.io.IOException;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050068import java.util.List;
69import java.util.concurrent.Executor;
70
71/**
72 * Base media control panel for System UI
73 */
Robert Snoeberger3cc22222020-03-25 15:36:31 -040074public class MediaControlPanel {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050075 private static final String TAG = "MediaControlPanel";
Robert Snoeberger9a7409b2020-04-09 18:12:27 -040076 @Nullable private final LocalMediaManager mLocalMediaManager;
Beth Thibodeaua51c3142020-03-17 17:27:04 -040077 private final Executor mForegroundExecutor;
Beth Thibodeau23a33ab2020-04-07 20:51:57 -040078 protected final Executor mBackgroundExecutor;
Beth Thibodeaue561c002020-04-23 17:33:00 -040079 private final ActivityStarter mActivityStarter;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050080
81 private Context mContext;
82 protected LinearLayout mMediaNotifView;
83 private View mSeamless;
84 private MediaSession.Token mToken;
85 private MediaController mController;
86 private int mForegroundColor;
87 private int mBackgroundColor;
Robert Snoeberger9a7409b2020-04-09 18:12:27 -040088 private MediaDevice mDevice;
Beth Thibodeau23a33ab2020-04-07 20:51:57 -040089 protected ComponentName mServiceComponent;
Robert Snoeberger3cc22222020-03-25 15:36:31 -040090 private boolean mIsRegistered = false;
Beth Thibodeaua3d90982020-04-13 23:42:48 -040091 private String mKey;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050092
93 private final int[] mActionIds;
94
Beth Thibodeau23a33ab2020-04-07 20:51:57 -040095 public static final String MEDIA_PREFERENCES = "media_control_prefs";
96 public static final String MEDIA_PREFERENCE_KEY = "browser_components";
97 private SharedPreferences mSharedPrefs;
98 private boolean mCheckedForResumption = false;
99
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500100 // Button IDs used in notifications
101 protected static final int[] NOTIF_ACTION_IDS = {
102 com.android.internal.R.id.action0,
103 com.android.internal.R.id.action1,
104 com.android.internal.R.id.action2,
105 com.android.internal.R.id.action3,
106 com.android.internal.R.id.action4
107 };
108
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400109 // URI fields to try loading album art from
110 private static final String[] ART_URIS = {
111 MediaMetadata.METADATA_KEY_ALBUM_ART_URI,
112 MediaMetadata.METADATA_KEY_ART_URI,
113 MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI
114 };
115
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400116 private final MediaController.Callback mSessionCallback = new MediaController.Callback() {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500117 @Override
118 public void onSessionDestroyed() {
119 Log.d(TAG, "session destroyed");
120 mController.unregisterCallback(mSessionCallback);
121 clearControls();
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400122 makeInactive();
123 }
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400124 @Override
Robert Snoeberger445d4412020-04-15 00:03:13 -0400125 public void onPlaybackStateChanged(PlaybackState state) {
126 final int s = state != null ? state.getState() : PlaybackState.STATE_NONE;
127 // When the playback state is NONE or CONNECTING, transition the player to the
128 // resumption state. State CONNECTING needs to be considered for Cast sessions. Ending
129 // a cast session in YT results in the CONNECTING state, which makes sense if you
130 // thinking of the session as waiting to connect to another cast device.
131 if (s == PlaybackState.STATE_NONE || s == PlaybackState.STATE_CONNECTING) {
132 Log.d(TAG, "playback state change will trigger resumption, state=" + state);
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400133 clearControls();
134 makeInactive();
135 }
136 }
137 };
138
139 private final OnAttachStateChangeListener mStateListener = new OnAttachStateChangeListener() {
140 @Override
141 public void onViewAttachedToWindow(View unused) {
142 makeActive();
143 }
144 @Override
145 public void onViewDetachedFromWindow(View unused) {
146 makeInactive();
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500147 }
148 };
149
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400150 private final LocalMediaManager.DeviceCallback mDeviceCallback =
151 new LocalMediaManager.DeviceCallback() {
152 @Override
153 public void onDeviceListUpdate(List<MediaDevice> devices) {
154 if (mLocalMediaManager == null) {
155 return;
156 }
157 MediaDevice currentDevice = mLocalMediaManager.getCurrentConnectedDevice();
158 // Check because this can be called several times while changing devices
159 if (mDevice == null || !mDevice.equals(currentDevice)) {
160 mDevice = currentDevice;
161 updateDevice(mDevice);
162 }
163 }
164
165 @Override
166 public void onSelectedDeviceStateChanged(MediaDevice device, int state) {
167 if (mDevice == null || !mDevice.equals(device)) {
168 mDevice = device;
169 updateDevice(mDevice);
170 }
171 }
172 };
173
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500174 /**
175 * Initialize a new control panel
176 * @param context
177 * @param parent
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400178 * @param routeManager Manager used to listen for device change events.
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500179 * @param layoutId layout resource to use for this control panel
180 * @param actionIds resource IDs for action buttons in the layout
Beth Thibodeaua51c3142020-03-17 17:27:04 -0400181 * @param foregroundExecutor foreground executor
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500182 * @param backgroundExecutor background executor, used for processing artwork
Beth Thibodeaue561c002020-04-23 17:33:00 -0400183 * @param activityStarter activity starter
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500184 */
Robert Snoeberger445d4412020-04-15 00:03:13 -0400185 public MediaControlPanel(Context context, ViewGroup parent,
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400186 @Nullable LocalMediaManager routeManager, @LayoutRes int layoutId, int[] actionIds,
Beth Thibodeaue561c002020-04-23 17:33:00 -0400187 Executor foregroundExecutor, Executor backgroundExecutor,
188 ActivityStarter activityStarter) {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500189 mContext = context;
190 LayoutInflater inflater = LayoutInflater.from(mContext);
191 mMediaNotifView = (LinearLayout) inflater.inflate(layoutId, parent, false);
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400192 // TODO(b/150854549): removeOnAttachStateChangeListener when this doesn't inflate views
193 // mStateListener shouldn't need to be unregistered since this object shares the same
194 // lifecycle with the inflated view. It would be better, however, if this controller used an
195 // attach/detach of views instead of inflating them in the constructor, which would allow
196 // mStateListener to be unregistered in detach.
197 mMediaNotifView.addOnAttachStateChangeListener(mStateListener);
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400198 mLocalMediaManager = routeManager;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500199 mActionIds = actionIds;
Beth Thibodeaua51c3142020-03-17 17:27:04 -0400200 mForegroundExecutor = foregroundExecutor;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500201 mBackgroundExecutor = backgroundExecutor;
Beth Thibodeaue561c002020-04-23 17:33:00 -0400202 mActivityStarter = activityStarter;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500203 }
204
205 /**
206 * Get the view used to display media controls
207 * @return the view
208 */
209 public View getView() {
210 return mMediaNotifView;
211 }
212
213 /**
214 * Get the context
215 * @return context
216 */
217 public Context getContext() {
218 return mContext;
219 }
220
221 /**
222 * Update the media panel view for the given media session
223 * @param token
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400224 * @param iconDrawable
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400225 * @param largeIcon
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500226 * @param iconColor
227 * @param bgColor
228 * @param contentIntent
229 * @param appNameString
Beth Thibodeaua3d90982020-04-13 23:42:48 -0400230 * @param key
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500231 */
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400232 public void setMediaSession(MediaSession.Token token, Drawable iconDrawable, Icon largeIcon,
233 int iconColor, int bgColor, PendingIntent contentIntent, String appNameString,
234 String key) {
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400235 // Ensure that component names are updated if token has changed
236 if (mToken == null || !mToken.equals(token)) {
237 mToken = token;
238 mServiceComponent = null;
239 mCheckedForResumption = false;
240 }
241
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500242 mForegroundColor = iconColor;
243 mBackgroundColor = bgColor;
244 mController = new MediaController(mContext, mToken);
Beth Thibodeaua3d90982020-04-13 23:42:48 -0400245 mKey = key;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500246
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400247 // Try to find a browser service component for this app
248 // TODO also check for a media button receiver intended for restarting (b/154127084)
249 // Only check if we haven't tried yet or the session token changed
250 String pkgName = mController.getPackageName();
251 if (mServiceComponent == null && !mCheckedForResumption) {
252 Log.d(TAG, "Checking for service component");
253 PackageManager pm = mContext.getPackageManager();
254 Intent resumeIntent = new Intent(MediaBrowserService.SERVICE_INTERFACE);
255 List<ResolveInfo> resumeInfo = pm.queryIntentServices(resumeIntent, 0);
256 if (resumeInfo != null) {
257 for (ResolveInfo inf : resumeInfo) {
258 if (inf.serviceInfo.packageName.equals(mController.getPackageName())) {
259 mBackgroundExecutor.execute(() ->
260 tryUpdateResumptionList(inf.getComponentInfo().getComponentName()));
261 break;
262 }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500263 }
264 }
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400265 mCheckedForResumption = true;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500266 }
267
268 mController.registerCallback(mSessionCallback);
269
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500270 mMediaNotifView.setBackgroundTintList(ColorStateList.valueOf(mBackgroundColor));
271
272 // Click action
Beth Thibodeaua51c3142020-03-17 17:27:04 -0400273 if (contentIntent != null) {
274 mMediaNotifView.setOnClickListener(v -> {
Beth Thibodeaue561c002020-04-23 17:33:00 -0400275 mActivityStarter.postStartActivityDismissingKeyguard(contentIntent);
Beth Thibodeaua51c3142020-03-17 17:27:04 -0400276 });
277 }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500278
279 // App icon
280 ImageView appIcon = mMediaNotifView.findViewById(R.id.icon);
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500281 iconDrawable.setTint(mForegroundColor);
282 appIcon.setImageDrawable(iconDrawable);
283
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500284 // Transfer chip
285 mSeamless = mMediaNotifView.findViewById(R.id.media_seamless);
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400286 if (mSeamless != null && mLocalMediaManager != null) {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500287 mSeamless.setVisibility(View.VISIBLE);
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400288 updateDevice(mLocalMediaManager.getCurrentConnectedDevice());
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500289 mSeamless.setOnClickListener(v -> {
290 final Intent intent = new Intent()
291 .setAction(MediaOutputSliceConstants.ACTION_MEDIA_OUTPUT)
292 .putExtra(MediaOutputSliceConstants.EXTRA_PACKAGE_NAME,
293 mController.getPackageName())
294 .putExtra(MediaOutputSliceConstants.KEY_MEDIA_SESSION_TOKEN, mToken);
295 mActivityStarter.startActivity(intent, false, true /* dismissShade */,
296 Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
297 });
298 }
299
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400300 makeActive();
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400301
302 // App title (not in mini player)
303 TextView appName = mMediaNotifView.findViewById(R.id.app_name);
304 if (appName != null) {
305 appName.setText(appNameString);
306 appName.setTextColor(mForegroundColor);
307 }
308
309 MediaMetadata mediaMetadata = mController.getMetadata();
310 if (mediaMetadata == null) {
311 Log.e(TAG, "Media metadata was null");
312 return;
313 }
314
315 ImageView albumView = mMediaNotifView.findViewById(R.id.album_art);
316 if (albumView != null) {
317 // Resize art in a background thread
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400318 mBackgroundExecutor.execute(() -> processAlbumArt(mediaMetadata, largeIcon, albumView));
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400319 }
320
321 // Song name
322 TextView titleText = mMediaNotifView.findViewById(R.id.header_title);
323 String songName = mediaMetadata.getString(MediaMetadata.METADATA_KEY_TITLE);
324 titleText.setText(songName);
325 titleText.setTextColor(mForegroundColor);
326
327 // Artist name (not in mini player)
328 TextView artistText = mMediaNotifView.findViewById(R.id.header_artist);
329 if (artistText != null) {
330 String artistName = mediaMetadata.getString(MediaMetadata.METADATA_KEY_ARTIST);
331 artistText.setText(artistName);
332 artistText.setTextColor(mForegroundColor);
333 }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500334 }
335
336 /**
337 * Return the token for the current media session
338 * @return the token
339 */
340 public MediaSession.Token getMediaSessionToken() {
341 return mToken;
342 }
343
344 /**
345 * Get the current media controller
346 * @return the controller
347 */
348 public MediaController getController() {
349 return mController;
350 }
351
352 /**
353 * Get the name of the package associated with the current media controller
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400354 * @return the package name, or null if no controller
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500355 */
356 public String getMediaPlayerPackage() {
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400357 if (mController == null) {
358 return null;
359 }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500360 return mController.getPackageName();
361 }
362
363 /**
Beth Thibodeaua3d90982020-04-13 23:42:48 -0400364 * Return the original notification's key
365 * @return The notification key
366 */
367 public String getKey() {
368 return mKey;
369 }
370
371 /**
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500372 * Check whether this player has an attached media session.
373 * @return whether there is a controller with a current media session.
374 */
375 public boolean hasMediaSession() {
376 return mController != null && mController.getPlaybackState() != null;
377 }
378
379 /**
380 * Check whether the media controlled by this player is currently playing
381 * @return whether it is playing, or false if no controller information
382 */
383 public boolean isPlaying() {
384 return isPlaying(mController);
385 }
386
387 /**
388 * Check whether the given controller is currently playing
389 * @param controller media controller to check
390 * @return whether it is playing, or false if no controller information
391 */
392 protected boolean isPlaying(MediaController controller) {
393 if (controller == null) {
394 return false;
395 }
396
397 PlaybackState state = controller.getPlaybackState();
398 if (state == null) {
399 return false;
400 }
401
402 return (state.getState() == PlaybackState.STATE_PLAYING);
403 }
404
405 /**
406 * Process album art for layout
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400407 * @param description media description
408 * @param albumView view to hold the album art
409 */
410 protected void processAlbumArt(MediaDescription description, ImageView albumView) {
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400411 Bitmap albumArt = null;
412
413 // First try loading from URI
414 albumArt = loadBitmapFromUri(description.getIconUri());
415
416 // Then check bitmap
417 if (albumArt == null) {
418 albumArt = description.getIconBitmap();
419 }
420
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400421 processAlbumArtInternal(albumArt, albumView);
422 }
423
424 /**
425 * Process album art for layout
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500426 * @param metadata media metadata
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400427 * @param largeIcon from notification, checked as a fallback if metadata does not have art
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500428 * @param albumView view to hold the album art
429 */
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400430 private void processAlbumArt(MediaMetadata metadata, Icon largeIcon, ImageView albumView) {
431 Bitmap albumArt = null;
432
433 // First look in URI fields
434 for (String field : ART_URIS) {
435 String uriString = metadata.getString(field);
Beth Thibodeaudba74bc2020-04-27 14:17:08 -0400436 if (!TextUtils.isEmpty(uriString)) {
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400437 albumArt = loadBitmapFromUri(Uri.parse(uriString));
438 if (albumArt != null) {
439 Log.d(TAG, "loaded art from " + field);
440 break;
441 }
442 }
443 }
444
445 // Then check bitmap field
446 if (albumArt == null) {
447 albumArt = metadata.getBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART);
448 }
449
450 // Finally try the notification's largeIcon
451 if (albumArt == null && largeIcon != null) {
452 albumArt = largeIcon.getBitmap();
453 }
454
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400455 processAlbumArtInternal(albumArt, albumView);
456 }
457
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400458 /**
459 * Load a bitmap from a URI
460 * @param uri
461 * @return bitmap, or null if couldn't be loaded
462 */
463 private Bitmap loadBitmapFromUri(Uri uri) {
Beth Thibodeaudba74bc2020-04-27 14:17:08 -0400464 // ImageDecoder requires a scheme of the following types
465 if (uri.getScheme() == null) {
466 return null;
467 }
468
469 if (!uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)
470 && !uri.getScheme().equals(ContentResolver.SCHEME_ANDROID_RESOURCE)
471 && !uri.getScheme().equals(ContentResolver.SCHEME_FILE)) {
472 return null;
473 }
474
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400475 ImageDecoder.Source source = ImageDecoder.createSource(mContext.getContentResolver(), uri);
476 try {
477 return ImageDecoder.decodeBitmap(source);
478 } catch (IOException e) {
479 e.printStackTrace();
480 return null;
481 }
482 }
483
484 /**
485 * Resize and crop the image if provided and update the control view
486 * @param albumArt Bitmap of art to display, or null to hide view
487 * @param albumView View that will hold the art
488 */
489 private void processAlbumArtInternal(@Nullable Bitmap albumArt, ImageView albumView) {
490 // Resize
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500491 RoundedBitmapDrawable roundedDrawable = null;
492 if (albumArt != null) {
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400493 float radius = mContext.getResources().getDimension(R.dimen.qs_media_corner_radius);
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500494 Bitmap original = albumArt.copy(Bitmap.Config.ARGB_8888, true);
495 int albumSize = (int) mContext.getResources().getDimension(
496 R.dimen.qs_media_album_size);
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400497 Bitmap scaled = ThumbnailUtils.extractThumbnail(original, albumSize, albumSize);
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500498 roundedDrawable = RoundedBitmapDrawableFactory.create(mContext.getResources(), scaled);
499 roundedDrawable.setCornerRadius(radius);
500 } else {
501 Log.e(TAG, "No album art available");
502 }
503
504 // Now that it's resized, update the UI
505 final RoundedBitmapDrawable result = roundedDrawable;
Beth Thibodeaua51c3142020-03-17 17:27:04 -0400506 mForegroundExecutor.execute(() -> {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500507 if (result != null) {
508 albumView.setImageDrawable(result);
509 albumView.setVisibility(View.VISIBLE);
510 } else {
511 albumView.setImageDrawable(null);
512 albumView.setVisibility(View.GONE);
513 }
514 });
515 }
516
517 /**
518 * Update the current device information
519 * @param device device information to display
520 */
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400521 private void updateDevice(MediaDevice device) {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500522 if (mSeamless == null) {
523 return;
524 }
Beth Thibodeaua51c3142020-03-17 17:27:04 -0400525 mForegroundExecutor.execute(() -> {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500526 updateChipInternal(device);
527 });
528 }
529
530 private void updateChipInternal(MediaDevice device) {
531 ColorStateList fgTintList = ColorStateList.valueOf(mForegroundColor);
532
533 // Update the outline color
534 LinearLayout viewLayout = (LinearLayout) mSeamless;
535 RippleDrawable bkgDrawable = (RippleDrawable) viewLayout.getBackground();
536 GradientDrawable rect = (GradientDrawable) bkgDrawable.getDrawable(0);
537 rect.setStroke(2, mForegroundColor);
538 rect.setColor(mBackgroundColor);
539
540 ImageView iconView = mSeamless.findViewById(R.id.media_seamless_image);
541 TextView deviceName = mSeamless.findViewById(R.id.media_seamless_text);
542 deviceName.setTextColor(fgTintList);
543
544 if (device != null) {
545 Drawable icon = device.getIcon();
546 iconView.setVisibility(View.VISIBLE);
547 iconView.setImageTintList(fgTintList);
548
549 if (icon instanceof AdaptiveIcon) {
550 AdaptiveIcon aIcon = (AdaptiveIcon) icon;
551 aIcon.setBackgroundColor(mBackgroundColor);
552 iconView.setImageDrawable(aIcon);
553 } else {
554 iconView.setImageDrawable(icon);
555 }
556 deviceName.setText(device.getName());
557 } else {
558 // Reset to default
559 iconView.setVisibility(View.GONE);
560 deviceName.setText(com.android.internal.R.string.ext_media_seamless_action);
561 }
562 }
563
564 /**
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400565 * Puts controls into a resumption state if possible, or calls removePlayer if no component was
566 * found that could resume playback
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500567 */
568 public void clearControls() {
Robert Snoeberger445d4412020-04-15 00:03:13 -0400569 Log.d(TAG, "clearControls to resumption state package=" + getMediaPlayerPackage());
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400570 if (mServiceComponent == null) {
571 // If we don't have a way to resume, just remove the player altogether
572 Log.d(TAG, "Removing unresumable controls");
573 removePlayer();
574 return;
575 }
576 resetButtons();
577 }
578
579 /**
580 * Hide the media buttons and show only a restart button
581 */
582 protected void resetButtons() {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500583 // Hide all the old buttons
584 for (int i = 0; i < mActionIds.length; i++) {
585 ImageButton thisBtn = mMediaNotifView.findViewById(mActionIds[i]);
586 if (thisBtn != null) {
587 thisBtn.setVisibility(View.GONE);
588 }
589 }
590
591 // Add a restart button
592 ImageButton btn = mMediaNotifView.findViewById(mActionIds[0]);
593 btn.setOnClickListener(v -> {
594 Log.d(TAG, "Attempting to restart session");
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400595 QSMediaBrowser browser = new QSMediaBrowser(mContext, null, mServiceComponent);
596 browser.restart();
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500597 });
598 btn.setImageDrawable(mContext.getResources().getDrawable(R.drawable.lb_ic_play));
599 btn.setImageTintList(ColorStateList.valueOf(mForegroundColor));
600 btn.setVisibility(View.VISIBLE);
601 }
602
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400603 private void makeActive() {
604 Assert.isMainThread();
605 if (!mIsRegistered) {
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400606 if (mLocalMediaManager != null) {
607 mLocalMediaManager.registerCallback(mDeviceCallback);
608 mLocalMediaManager.startScan();
609 }
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400610 mIsRegistered = true;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500611 }
612 }
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400613
614 private void makeInactive() {
615 Assert.isMainThread();
616 if (mIsRegistered) {
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400617 if (mLocalMediaManager != null) {
618 mLocalMediaManager.stopScan();
619 mLocalMediaManager.unregisterCallback(mDeviceCallback);
620 }
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400621 mIsRegistered = false;
622 }
623 }
624
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400625 /**
626 * Verify that we can connect to the given component with a MediaBrowser, and if so, add that
627 * component to the list of resumption components
628 */
629 private void tryUpdateResumptionList(ComponentName componentName) {
630 Log.d(TAG, "Testing if we can connect to " + componentName);
631 QSMediaBrowser.testConnection(mContext,
632 new QSMediaBrowser.Callback() {
633 @Override
634 public void onConnected() {
635 Log.d(TAG, "yes we can resume with " + componentName);
636 mServiceComponent = componentName;
637 updateResumptionList(componentName);
638 }
639
640 @Override
641 public void onError() {
642 Log.d(TAG, "Cannot resume with " + componentName);
643 mServiceComponent = null;
Beth Thibodeau89f5c762020-04-21 13:09:55 -0400644 if (!hasMediaSession()) {
645 // If it's not active and we can't resume, remove
646 removePlayer();
647 }
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400648 }
649 },
650 componentName);
651 }
652
653 /**
654 * Add the component to the saved list of media browser services, checking for duplicates and
655 * removing older components that exceed the maximum limit
656 * @param componentName
657 */
658 private synchronized void updateResumptionList(ComponentName componentName) {
659 // Add to front of saved list
660 if (mSharedPrefs == null) {
661 mSharedPrefs = mContext.getSharedPreferences(MEDIA_PREFERENCES, 0);
662 }
663 String componentString = componentName.flattenToString();
664 String listString = mSharedPrefs.getString(MEDIA_PREFERENCE_KEY, null);
665 if (listString == null) {
666 listString = componentString;
667 } else {
668 String[] components = listString.split(QSMediaBrowser.DELIMITER);
669 StringBuilder updated = new StringBuilder(componentString);
670 int nBrowsers = 1;
671 for (int i = 0; i < components.length
672 && nBrowsers < QSMediaBrowser.MAX_RESUMPTION_CONTROLS; i++) {
673 if (componentString.equals(components[i])) {
674 continue;
675 }
676 updated.append(QSMediaBrowser.DELIMITER).append(components[i]);
677 nBrowsers++;
678 }
679 listString = updated.toString();
680 }
681 mSharedPrefs.edit().putString(MEDIA_PREFERENCE_KEY, listString).apply();
682 }
683
684 /**
685 * Called when a player can't be resumed to give it an opportunity to hide or remove itself
686 */
687 protected void removePlayer() { }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500688}