blob: f87605382913c9eac01b76e9910deb1e669edebe [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
Beth Thibodeau4b5ec832020-04-30 19:43:37 -0400318 // Can be null!
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400319 MediaMetadata mediaMetadata = mController.getMetadata();
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400320
321 ImageView albumView = mMediaNotifView.findViewById(R.id.album_art);
322 if (albumView != null) {
323 // Resize art in a background thread
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400324 mBackgroundExecutor.execute(() -> processAlbumArt(mediaMetadata, largeIcon, albumView));
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400325 }
326
327 // Song name
328 TextView titleText = mMediaNotifView.findViewById(R.id.header_title);
Beth Thibodeau4b5ec832020-04-30 19:43:37 -0400329 String songName = "";
330 if (mediaMetadata != null) {
331 songName = mediaMetadata.getString(MediaMetadata.METADATA_KEY_TITLE);
332 }
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400333 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) {
Beth Thibodeau4b5ec832020-04-30 19:43:37 -0400339 String artistName = "";
340 if (mediaMetadata != null) {
341 artistName = mediaMetadata.getString(MediaMetadata.METADATA_KEY_ARTIST);
342 }
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400343 artistText.setText(artistName);
344 artistText.setTextColor(mForegroundColor);
345 }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500346 }
347
348 /**
349 * Return the token for the current media session
350 * @return the token
351 */
352 public MediaSession.Token getMediaSessionToken() {
353 return mToken;
354 }
355
356 /**
357 * Get the current media controller
358 * @return the controller
359 */
360 public MediaController getController() {
361 return mController;
362 }
363
364 /**
365 * Get the name of the package associated with the current media controller
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400366 * @return the package name, or null if no controller
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500367 */
368 public String getMediaPlayerPackage() {
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400369 if (mController == null) {
370 return null;
371 }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500372 return mController.getPackageName();
373 }
374
375 /**
Beth Thibodeaua3d90982020-04-13 23:42:48 -0400376 * Return the original notification's key
377 * @return The notification key
378 */
379 public String getKey() {
380 return mKey;
381 }
382
383 /**
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500384 * Check whether this player has an attached media session.
385 * @return whether there is a controller with a current media session.
386 */
387 public boolean hasMediaSession() {
388 return mController != null && mController.getPlaybackState() != null;
389 }
390
391 /**
392 * Check whether the media controlled by this player is currently playing
393 * @return whether it is playing, or false if no controller information
394 */
395 public boolean isPlaying() {
396 return isPlaying(mController);
397 }
398
399 /**
400 * Check whether the given controller is currently playing
401 * @param controller media controller to check
402 * @return whether it is playing, or false if no controller information
403 */
404 protected boolean isPlaying(MediaController controller) {
405 if (controller == null) {
406 return false;
407 }
408
409 PlaybackState state = controller.getPlaybackState();
410 if (state == null) {
411 return false;
412 }
413
414 return (state.getState() == PlaybackState.STATE_PLAYING);
415 }
416
417 /**
418 * Process album art for layout
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400419 * @param description media description
420 * @param albumView view to hold the album art
421 */
422 protected void processAlbumArt(MediaDescription description, ImageView albumView) {
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400423 Bitmap albumArt = null;
424
425 // First try loading from URI
426 albumArt = loadBitmapFromUri(description.getIconUri());
427
428 // Then check bitmap
429 if (albumArt == null) {
430 albumArt = description.getIconBitmap();
431 }
432
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400433 processAlbumArtInternal(albumArt, albumView);
434 }
435
436 /**
437 * Process album art for layout
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500438 * @param metadata media metadata
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400439 * @param largeIcon from notification, checked as a fallback if metadata does not have art
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500440 * @param albumView view to hold the album art
441 */
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400442 private void processAlbumArt(MediaMetadata metadata, Icon largeIcon, ImageView albumView) {
443 Bitmap albumArt = null;
444
Beth Thibodeau4b5ec832020-04-30 19:43:37 -0400445 if (metadata != null) {
446 // First look in URI fields
447 for (String field : ART_URIS) {
448 String uriString = metadata.getString(field);
449 if (!TextUtils.isEmpty(uriString)) {
450 albumArt = loadBitmapFromUri(Uri.parse(uriString));
451 if (albumArt != null) {
452 Log.d(TAG, "loaded art from " + field);
453 break;
454 }
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400455 }
456 }
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400457
Beth Thibodeau4b5ec832020-04-30 19:43:37 -0400458 // Then check bitmap field
459 if (albumArt == null) {
460 albumArt = metadata.getBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART);
461 }
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400462 }
463
464 // Finally try the notification's largeIcon
465 if (albumArt == null && largeIcon != null) {
466 albumArt = largeIcon.getBitmap();
467 }
468
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400469 processAlbumArtInternal(albumArt, albumView);
470 }
471
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400472 /**
473 * Load a bitmap from a URI
474 * @param uri
475 * @return bitmap, or null if couldn't be loaded
476 */
477 private Bitmap loadBitmapFromUri(Uri uri) {
Beth Thibodeaudba74bc2020-04-27 14:17:08 -0400478 // ImageDecoder requires a scheme of the following types
479 if (uri.getScheme() == null) {
480 return null;
481 }
482
483 if (!uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)
484 && !uri.getScheme().equals(ContentResolver.SCHEME_ANDROID_RESOURCE)
485 && !uri.getScheme().equals(ContentResolver.SCHEME_FILE)) {
486 return null;
487 }
488
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400489 ImageDecoder.Source source = ImageDecoder.createSource(mContext.getContentResolver(), uri);
490 try {
491 return ImageDecoder.decodeBitmap(source);
492 } catch (IOException e) {
493 e.printStackTrace();
494 return null;
495 }
496 }
497
498 /**
499 * Resize and crop the image if provided and update the control view
500 * @param albumArt Bitmap of art to display, or null to hide view
501 * @param albumView View that will hold the art
502 */
503 private void processAlbumArtInternal(@Nullable Bitmap albumArt, ImageView albumView) {
504 // Resize
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500505 RoundedBitmapDrawable roundedDrawable = null;
506 if (albumArt != null) {
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400507 float radius = mContext.getResources().getDimension(R.dimen.qs_media_corner_radius);
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500508 Bitmap original = albumArt.copy(Bitmap.Config.ARGB_8888, true);
509 int albumSize = (int) mContext.getResources().getDimension(
510 R.dimen.qs_media_album_size);
Beth Thibodeaud3ad81d2020-04-20 23:26:25 -0400511 Bitmap scaled = ThumbnailUtils.extractThumbnail(original, albumSize, albumSize);
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500512 roundedDrawable = RoundedBitmapDrawableFactory.create(mContext.getResources(), scaled);
513 roundedDrawable.setCornerRadius(radius);
514 } else {
515 Log.e(TAG, "No album art available");
516 }
517
518 // Now that it's resized, update the UI
519 final RoundedBitmapDrawable result = roundedDrawable;
Beth Thibodeaua51c3142020-03-17 17:27:04 -0400520 mForegroundExecutor.execute(() -> {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500521 if (result != null) {
522 albumView.setImageDrawable(result);
523 albumView.setVisibility(View.VISIBLE);
524 } else {
525 albumView.setImageDrawable(null);
526 albumView.setVisibility(View.GONE);
527 }
528 });
529 }
530
531 /**
532 * Update the current device information
533 * @param device device information to display
534 */
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400535 private void updateDevice(MediaDevice device) {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500536 if (mSeamless == null) {
537 return;
538 }
Beth Thibodeaua51c3142020-03-17 17:27:04 -0400539 mForegroundExecutor.execute(() -> {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500540 updateChipInternal(device);
541 });
542 }
543
544 private void updateChipInternal(MediaDevice device) {
545 ColorStateList fgTintList = ColorStateList.valueOf(mForegroundColor);
546
547 // Update the outline color
548 LinearLayout viewLayout = (LinearLayout) mSeamless;
549 RippleDrawable bkgDrawable = (RippleDrawable) viewLayout.getBackground();
550 GradientDrawable rect = (GradientDrawable) bkgDrawable.getDrawable(0);
551 rect.setStroke(2, mForegroundColor);
552 rect.setColor(mBackgroundColor);
553
554 ImageView iconView = mSeamless.findViewById(R.id.media_seamless_image);
555 TextView deviceName = mSeamless.findViewById(R.id.media_seamless_text);
556 deviceName.setTextColor(fgTintList);
557
Robert Snoebergerc981dc92020-04-27 15:00:50 -0400558 if (mIsRemotePlayback) {
559 mSeamless.setEnabled(false);
560 mSeamless.setAlpha(0.38f);
561 iconView.setImageResource(R.drawable.ic_hardware_speaker);
562 iconView.setVisibility(View.VISIBLE);
563 iconView.setImageTintList(fgTintList);
564 deviceName.setText(R.string.media_seamless_remote_device);
565 } else if (device != null) {
566 mSeamless.setEnabled(true);
567 mSeamless.setAlpha(1f);
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500568 Drawable icon = device.getIcon();
569 iconView.setVisibility(View.VISIBLE);
570 iconView.setImageTintList(fgTintList);
571
572 if (icon instanceof AdaptiveIcon) {
573 AdaptiveIcon aIcon = (AdaptiveIcon) icon;
574 aIcon.setBackgroundColor(mBackgroundColor);
575 iconView.setImageDrawable(aIcon);
576 } else {
577 iconView.setImageDrawable(icon);
578 }
579 deviceName.setText(device.getName());
580 } else {
581 // Reset to default
Robert Snoeberger44299172020-04-24 22:22:21 -0400582 Log.d(TAG, "device is null. Not binding output chip.");
Robert Snoebergerc981dc92020-04-27 15:00:50 -0400583 mSeamless.setEnabled(true);
584 mSeamless.setAlpha(1f);
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500585 iconView.setVisibility(View.GONE);
586 deviceName.setText(com.android.internal.R.string.ext_media_seamless_action);
587 }
588 }
589
590 /**
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400591 * Puts controls into a resumption state if possible, or calls removePlayer if no component was
592 * found that could resume playback
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500593 */
594 public void clearControls() {
Robert Snoeberger445d4412020-04-15 00:03:13 -0400595 Log.d(TAG, "clearControls to resumption state package=" + getMediaPlayerPackage());
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400596 if (mServiceComponent == null) {
597 // If we don't have a way to resume, just remove the player altogether
598 Log.d(TAG, "Removing unresumable controls");
599 removePlayer();
600 return;
601 }
602 resetButtons();
603 }
604
605 /**
606 * Hide the media buttons and show only a restart button
607 */
608 protected void resetButtons() {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500609 // Hide all the old buttons
610 for (int i = 0; i < mActionIds.length; i++) {
611 ImageButton thisBtn = mMediaNotifView.findViewById(mActionIds[i]);
612 if (thisBtn != null) {
613 thisBtn.setVisibility(View.GONE);
614 }
615 }
616
617 // Add a restart button
618 ImageButton btn = mMediaNotifView.findViewById(mActionIds[0]);
619 btn.setOnClickListener(v -> {
620 Log.d(TAG, "Attempting to restart session");
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400621 QSMediaBrowser browser = new QSMediaBrowser(mContext, null, mServiceComponent);
622 browser.restart();
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500623 });
624 btn.setImageDrawable(mContext.getResources().getDrawable(R.drawable.lb_ic_play));
625 btn.setImageTintList(ColorStateList.valueOf(mForegroundColor));
626 btn.setVisibility(View.VISIBLE);
627 }
628
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400629 private void makeActive() {
630 Assert.isMainThread();
631 if (!mIsRegistered) {
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400632 if (mLocalMediaManager != null) {
633 mLocalMediaManager.registerCallback(mDeviceCallback);
634 mLocalMediaManager.startScan();
635 }
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400636 mIsRegistered = true;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500637 }
638 }
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400639
640 private void makeInactive() {
641 Assert.isMainThread();
642 if (mIsRegistered) {
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400643 if (mLocalMediaManager != null) {
644 mLocalMediaManager.stopScan();
645 mLocalMediaManager.unregisterCallback(mDeviceCallback);
646 }
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400647 mIsRegistered = false;
648 }
649 }
650
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400651 /**
652 * Verify that we can connect to the given component with a MediaBrowser, and if so, add that
653 * component to the list of resumption components
654 */
655 private void tryUpdateResumptionList(ComponentName componentName) {
656 Log.d(TAG, "Testing if we can connect to " + componentName);
657 QSMediaBrowser.testConnection(mContext,
658 new QSMediaBrowser.Callback() {
659 @Override
660 public void onConnected() {
661 Log.d(TAG, "yes we can resume with " + componentName);
662 mServiceComponent = componentName;
663 updateResumptionList(componentName);
664 }
665
666 @Override
667 public void onError() {
668 Log.d(TAG, "Cannot resume with " + componentName);
669 mServiceComponent = null;
Beth Thibodeau89f5c762020-04-21 13:09:55 -0400670 if (!hasMediaSession()) {
671 // If it's not active and we can't resume, remove
672 removePlayer();
673 }
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400674 }
675 },
676 componentName);
677 }
678
679 /**
680 * Add the component to the saved list of media browser services, checking for duplicates and
681 * removing older components that exceed the maximum limit
682 * @param componentName
683 */
684 private synchronized void updateResumptionList(ComponentName componentName) {
685 // Add to front of saved list
686 if (mSharedPrefs == null) {
687 mSharedPrefs = mContext.getSharedPreferences(MEDIA_PREFERENCES, 0);
688 }
689 String componentString = componentName.flattenToString();
690 String listString = mSharedPrefs.getString(MEDIA_PREFERENCE_KEY, null);
691 if (listString == null) {
692 listString = componentString;
693 } else {
694 String[] components = listString.split(QSMediaBrowser.DELIMITER);
695 StringBuilder updated = new StringBuilder(componentString);
696 int nBrowsers = 1;
697 for (int i = 0; i < components.length
698 && nBrowsers < QSMediaBrowser.MAX_RESUMPTION_CONTROLS; i++) {
699 if (componentString.equals(components[i])) {
700 continue;
701 }
702 updated.append(QSMediaBrowser.DELIMITER).append(components[i]);
703 nBrowsers++;
704 }
705 listString = updated.toString();
706 }
707 mSharedPrefs.edit().putString(MEDIA_PREFERENCE_KEY, listString).apply();
708 }
709
710 /**
711 * Called when a player can't be resumed to give it an opportunity to hide or remove itself
712 */
713 protected void removePlayer() { }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500714}