Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.media; |
| 18 | |
| 19 | import android.annotation.LayoutRes; |
| 20 | import android.app.PendingIntent; |
| 21 | import android.content.ComponentName; |
Beth Thibodeau | dba74bc | 2020-04-27 14:17:08 -0400 | [diff] [blame] | 22 | import android.content.ContentResolver; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 25 | import android.content.SharedPreferences; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 26 | import android.content.pm.PackageManager; |
| 27 | import android.content.pm.ResolveInfo; |
| 28 | import android.content.res.ColorStateList; |
| 29 | import android.graphics.Bitmap; |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 30 | import android.graphics.ImageDecoder; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 31 | import android.graphics.drawable.Drawable; |
| 32 | import android.graphics.drawable.GradientDrawable; |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 33 | import android.graphics.drawable.Icon; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 34 | import android.graphics.drawable.RippleDrawable; |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 35 | import android.media.MediaDescription; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 36 | import android.media.MediaMetadata; |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 37 | import android.media.ThumbnailUtils; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 38 | import android.media.session.MediaController; |
| 39 | import android.media.session.MediaSession; |
| 40 | import android.media.session.PlaybackState; |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 41 | import android.net.Uri; |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 42 | import android.service.media.MediaBrowserService; |
Beth Thibodeau | dba74bc | 2020-04-27 14:17:08 -0400 | [diff] [blame] | 43 | import android.text.TextUtils; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 44 | import android.util.Log; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 45 | import android.view.LayoutInflater; |
| 46 | import android.view.View; |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 47 | import android.view.View.OnAttachStateChangeListener; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 48 | import android.view.ViewGroup; |
| 49 | import android.widget.ImageButton; |
| 50 | import android.widget.ImageView; |
| 51 | import android.widget.LinearLayout; |
| 52 | import android.widget.TextView; |
| 53 | |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 54 | import androidx.annotation.Nullable; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 55 | import androidx.core.graphics.drawable.RoundedBitmapDrawable; |
| 56 | import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory; |
| 57 | |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 58 | import com.android.settingslib.media.LocalMediaManager; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 59 | import com.android.settingslib.media.MediaDevice; |
| 60 | import com.android.settingslib.media.MediaOutputSliceConstants; |
| 61 | import com.android.settingslib.widget.AdaptiveIcon; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 62 | import com.android.systemui.R; |
| 63 | import com.android.systemui.plugins.ActivityStarter; |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 64 | import com.android.systemui.qs.QSMediaBrowser; |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 65 | import com.android.systemui.util.Assert; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 66 | |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 67 | import java.io.IOException; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 68 | import java.util.List; |
| 69 | import java.util.concurrent.Executor; |
| 70 | |
| 71 | /** |
| 72 | * Base media control panel for System UI |
| 73 | */ |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 74 | public class MediaControlPanel { |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 75 | private static final String TAG = "MediaControlPanel"; |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 76 | @Nullable private final LocalMediaManager mLocalMediaManager; |
Beth Thibodeau | a51c314 | 2020-03-17 17:27:04 -0400 | [diff] [blame] | 77 | private final Executor mForegroundExecutor; |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 78 | protected final Executor mBackgroundExecutor; |
Beth Thibodeau | e561c00 | 2020-04-23 17:33:00 -0400 | [diff] [blame] | 79 | private final ActivityStarter mActivityStarter; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 80 | |
| 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 Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 88 | private MediaDevice mDevice; |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 89 | protected ComponentName mServiceComponent; |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 90 | private boolean mIsRegistered = false; |
Beth Thibodeau | a3d9098 | 2020-04-13 23:42:48 -0400 | [diff] [blame] | 91 | private String mKey; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 92 | |
| 93 | private final int[] mActionIds; |
| 94 | |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 95 | 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 Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 100 | // 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 Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 109 | // 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 Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 116 | private final MediaController.Callback mSessionCallback = new MediaController.Callback() { |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 117 | @Override |
| 118 | public void onSessionDestroyed() { |
| 119 | Log.d(TAG, "session destroyed"); |
| 120 | mController.unregisterCallback(mSessionCallback); |
| 121 | clearControls(); |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 122 | makeInactive(); |
| 123 | } |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 124 | @Override |
Robert Snoeberger | 445d441 | 2020-04-15 00:03:13 -0400 | [diff] [blame] | 125 | public void onPlaybackStateChanged(PlaybackState state) { |
| 126 | final int s = state != null ? state.getState() : PlaybackState.STATE_NONE; |
Beth Thibodeau | d664de2 | 2020-04-28 16:29:36 -0400 | [diff] [blame^] | 127 | if (s == PlaybackState.STATE_NONE) { |
Robert Snoeberger | 445d441 | 2020-04-15 00:03:13 -0400 | [diff] [blame] | 128 | Log.d(TAG, "playback state change will trigger resumption, state=" + state); |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 129 | clearControls(); |
| 130 | makeInactive(); |
| 131 | } |
| 132 | } |
| 133 | }; |
| 134 | |
| 135 | private final OnAttachStateChangeListener mStateListener = new OnAttachStateChangeListener() { |
| 136 | @Override |
| 137 | public void onViewAttachedToWindow(View unused) { |
| 138 | makeActive(); |
| 139 | } |
| 140 | @Override |
| 141 | public void onViewDetachedFromWindow(View unused) { |
| 142 | makeInactive(); |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 143 | } |
| 144 | }; |
| 145 | |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 146 | private final LocalMediaManager.DeviceCallback mDeviceCallback = |
| 147 | new LocalMediaManager.DeviceCallback() { |
| 148 | @Override |
| 149 | public void onDeviceListUpdate(List<MediaDevice> devices) { |
| 150 | if (mLocalMediaManager == null) { |
| 151 | return; |
| 152 | } |
| 153 | MediaDevice currentDevice = mLocalMediaManager.getCurrentConnectedDevice(); |
| 154 | // Check because this can be called several times while changing devices |
| 155 | if (mDevice == null || !mDevice.equals(currentDevice)) { |
| 156 | mDevice = currentDevice; |
| 157 | updateDevice(mDevice); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public void onSelectedDeviceStateChanged(MediaDevice device, int state) { |
| 163 | if (mDevice == null || !mDevice.equals(device)) { |
| 164 | mDevice = device; |
| 165 | updateDevice(mDevice); |
| 166 | } |
| 167 | } |
| 168 | }; |
| 169 | |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 170 | /** |
| 171 | * Initialize a new control panel |
| 172 | * @param context |
| 173 | * @param parent |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 174 | * @param routeManager Manager used to listen for device change events. |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 175 | * @param layoutId layout resource to use for this control panel |
| 176 | * @param actionIds resource IDs for action buttons in the layout |
Beth Thibodeau | a51c314 | 2020-03-17 17:27:04 -0400 | [diff] [blame] | 177 | * @param foregroundExecutor foreground executor |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 178 | * @param backgroundExecutor background executor, used for processing artwork |
Beth Thibodeau | e561c00 | 2020-04-23 17:33:00 -0400 | [diff] [blame] | 179 | * @param activityStarter activity starter |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 180 | */ |
Robert Snoeberger | 445d441 | 2020-04-15 00:03:13 -0400 | [diff] [blame] | 181 | public MediaControlPanel(Context context, ViewGroup parent, |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 182 | @Nullable LocalMediaManager routeManager, @LayoutRes int layoutId, int[] actionIds, |
Beth Thibodeau | e561c00 | 2020-04-23 17:33:00 -0400 | [diff] [blame] | 183 | Executor foregroundExecutor, Executor backgroundExecutor, |
| 184 | ActivityStarter activityStarter) { |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 185 | mContext = context; |
| 186 | LayoutInflater inflater = LayoutInflater.from(mContext); |
| 187 | mMediaNotifView = (LinearLayout) inflater.inflate(layoutId, parent, false); |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 188 | // TODO(b/150854549): removeOnAttachStateChangeListener when this doesn't inflate views |
| 189 | // mStateListener shouldn't need to be unregistered since this object shares the same |
| 190 | // lifecycle with the inflated view. It would be better, however, if this controller used an |
| 191 | // attach/detach of views instead of inflating them in the constructor, which would allow |
| 192 | // mStateListener to be unregistered in detach. |
| 193 | mMediaNotifView.addOnAttachStateChangeListener(mStateListener); |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 194 | mLocalMediaManager = routeManager; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 195 | mActionIds = actionIds; |
Beth Thibodeau | a51c314 | 2020-03-17 17:27:04 -0400 | [diff] [blame] | 196 | mForegroundExecutor = foregroundExecutor; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 197 | mBackgroundExecutor = backgroundExecutor; |
Beth Thibodeau | e561c00 | 2020-04-23 17:33:00 -0400 | [diff] [blame] | 198 | mActivityStarter = activityStarter; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Get the view used to display media controls |
| 203 | * @return the view |
| 204 | */ |
| 205 | public View getView() { |
| 206 | return mMediaNotifView; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Get the context |
| 211 | * @return context |
| 212 | */ |
| 213 | public Context getContext() { |
| 214 | return mContext; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Update the media panel view for the given media session |
| 219 | * @param token |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 220 | * @param iconDrawable |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 221 | * @param largeIcon |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 222 | * @param iconColor |
| 223 | * @param bgColor |
| 224 | * @param contentIntent |
| 225 | * @param appNameString |
Beth Thibodeau | a3d9098 | 2020-04-13 23:42:48 -0400 | [diff] [blame] | 226 | * @param key |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 227 | */ |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 228 | public void setMediaSession(MediaSession.Token token, Drawable iconDrawable, Icon largeIcon, |
| 229 | int iconColor, int bgColor, PendingIntent contentIntent, String appNameString, |
| 230 | String key) { |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 231 | // Ensure that component names are updated if token has changed |
| 232 | if (mToken == null || !mToken.equals(token)) { |
| 233 | mToken = token; |
| 234 | mServiceComponent = null; |
| 235 | mCheckedForResumption = false; |
| 236 | } |
| 237 | |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 238 | mForegroundColor = iconColor; |
| 239 | mBackgroundColor = bgColor; |
| 240 | mController = new MediaController(mContext, mToken); |
Beth Thibodeau | a3d9098 | 2020-04-13 23:42:48 -0400 | [diff] [blame] | 241 | mKey = key; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 242 | |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 243 | // Try to find a browser service component for this app |
| 244 | // TODO also check for a media button receiver intended for restarting (b/154127084) |
| 245 | // Only check if we haven't tried yet or the session token changed |
Robert Snoeberger | 4429917 | 2020-04-24 22:22:21 -0400 | [diff] [blame] | 246 | final String pkgName = mController.getPackageName(); |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 247 | if (mServiceComponent == null && !mCheckedForResumption) { |
| 248 | Log.d(TAG, "Checking for service component"); |
| 249 | PackageManager pm = mContext.getPackageManager(); |
| 250 | Intent resumeIntent = new Intent(MediaBrowserService.SERVICE_INTERFACE); |
| 251 | List<ResolveInfo> resumeInfo = pm.queryIntentServices(resumeIntent, 0); |
| 252 | if (resumeInfo != null) { |
| 253 | for (ResolveInfo inf : resumeInfo) { |
| 254 | if (inf.serviceInfo.packageName.equals(mController.getPackageName())) { |
| 255 | mBackgroundExecutor.execute(() -> |
| 256 | tryUpdateResumptionList(inf.getComponentInfo().getComponentName())); |
| 257 | break; |
| 258 | } |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 259 | } |
| 260 | } |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 261 | mCheckedForResumption = true; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | mController.registerCallback(mSessionCallback); |
| 265 | |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 266 | mMediaNotifView.setBackgroundTintList(ColorStateList.valueOf(mBackgroundColor)); |
| 267 | |
| 268 | // Click action |
Beth Thibodeau | a51c314 | 2020-03-17 17:27:04 -0400 | [diff] [blame] | 269 | if (contentIntent != null) { |
| 270 | mMediaNotifView.setOnClickListener(v -> { |
Beth Thibodeau | e561c00 | 2020-04-23 17:33:00 -0400 | [diff] [blame] | 271 | mActivityStarter.postStartActivityDismissingKeyguard(contentIntent); |
Beth Thibodeau | a51c314 | 2020-03-17 17:27:04 -0400 | [diff] [blame] | 272 | }); |
| 273 | } |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 274 | |
| 275 | // App icon |
| 276 | ImageView appIcon = mMediaNotifView.findViewById(R.id.icon); |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 277 | iconDrawable.setTint(mForegroundColor); |
| 278 | appIcon.setImageDrawable(iconDrawable); |
| 279 | |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 280 | // Transfer chip |
| 281 | mSeamless = mMediaNotifView.findViewById(R.id.media_seamless); |
Robert Snoeberger | 4429917 | 2020-04-24 22:22:21 -0400 | [diff] [blame] | 282 | if (mSeamless != null) { |
| 283 | if (mLocalMediaManager != null) { |
| 284 | mSeamless.setVisibility(View.VISIBLE); |
| 285 | updateDevice(mLocalMediaManager.getCurrentConnectedDevice()); |
| 286 | mSeamless.setOnClickListener(v -> { |
| 287 | final Intent intent = new Intent() |
| 288 | .setAction(MediaOutputSliceConstants.ACTION_MEDIA_OUTPUT) |
| 289 | .putExtra(MediaOutputSliceConstants.EXTRA_PACKAGE_NAME, |
| 290 | mController.getPackageName()) |
| 291 | .putExtra(MediaOutputSliceConstants.KEY_MEDIA_SESSION_TOKEN, mToken); |
| 292 | mActivityStarter.startActivity(intent, false, true /* dismissShade */, |
| 293 | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| 294 | }); |
| 295 | } else { |
| 296 | Log.d(TAG, "LocalMediaManager is null. Not binding output chip for pkg=" + pkgName); |
| 297 | } |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 298 | } |
| 299 | |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 300 | makeActive(); |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 301 | |
| 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 Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 318 | mBackgroundExecutor.execute(() -> processAlbumArt(mediaMetadata, largeIcon, albumView)); |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 319 | } |
| 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 Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 334 | } |
| 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 Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 354 | * @return the package name, or null if no controller |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 355 | */ |
| 356 | public String getMediaPlayerPackage() { |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 357 | if (mController == null) { |
| 358 | return null; |
| 359 | } |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 360 | return mController.getPackageName(); |
| 361 | } |
| 362 | |
| 363 | /** |
Beth Thibodeau | a3d9098 | 2020-04-13 23:42:48 -0400 | [diff] [blame] | 364 | * Return the original notification's key |
| 365 | * @return The notification key |
| 366 | */ |
| 367 | public String getKey() { |
| 368 | return mKey; |
| 369 | } |
| 370 | |
| 371 | /** |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 372 | * 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 Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 407 | * @param description media description |
| 408 | * @param albumView view to hold the album art |
| 409 | */ |
| 410 | protected void processAlbumArt(MediaDescription description, ImageView albumView) { |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 411 | 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 Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 421 | processAlbumArtInternal(albumArt, albumView); |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Process album art for layout |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 426 | * @param metadata media metadata |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 427 | * @param largeIcon from notification, checked as a fallback if metadata does not have art |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 428 | * @param albumView view to hold the album art |
| 429 | */ |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 430 | 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 Thibodeau | dba74bc | 2020-04-27 14:17:08 -0400 | [diff] [blame] | 436 | if (!TextUtils.isEmpty(uriString)) { |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 437 | 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 Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 455 | processAlbumArtInternal(albumArt, albumView); |
| 456 | } |
| 457 | |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 458 | /** |
| 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 Thibodeau | dba74bc | 2020-04-27 14:17:08 -0400 | [diff] [blame] | 464 | // 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 Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 475 | 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 Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 491 | RoundedBitmapDrawable roundedDrawable = null; |
| 492 | if (albumArt != null) { |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 493 | float radius = mContext.getResources().getDimension(R.dimen.qs_media_corner_radius); |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 494 | Bitmap original = albumArt.copy(Bitmap.Config.ARGB_8888, true); |
| 495 | int albumSize = (int) mContext.getResources().getDimension( |
| 496 | R.dimen.qs_media_album_size); |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 497 | Bitmap scaled = ThumbnailUtils.extractThumbnail(original, albumSize, albumSize); |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 498 | 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 Thibodeau | a51c314 | 2020-03-17 17:27:04 -0400 | [diff] [blame] | 506 | mForegroundExecutor.execute(() -> { |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 507 | 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 Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 521 | private void updateDevice(MediaDevice device) { |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 522 | if (mSeamless == null) { |
| 523 | return; |
| 524 | } |
Beth Thibodeau | a51c314 | 2020-03-17 17:27:04 -0400 | [diff] [blame] | 525 | mForegroundExecutor.execute(() -> { |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 526 | 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 |
Robert Snoeberger | 4429917 | 2020-04-24 22:22:21 -0400 | [diff] [blame] | 559 | Log.d(TAG, "device is null. Not binding output chip."); |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 560 | iconView.setVisibility(View.GONE); |
| 561 | deviceName.setText(com.android.internal.R.string.ext_media_seamless_action); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | /** |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 566 | * Puts controls into a resumption state if possible, or calls removePlayer if no component was |
| 567 | * found that could resume playback |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 568 | */ |
| 569 | public void clearControls() { |
Robert Snoeberger | 445d441 | 2020-04-15 00:03:13 -0400 | [diff] [blame] | 570 | Log.d(TAG, "clearControls to resumption state package=" + getMediaPlayerPackage()); |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 571 | if (mServiceComponent == null) { |
| 572 | // If we don't have a way to resume, just remove the player altogether |
| 573 | Log.d(TAG, "Removing unresumable controls"); |
| 574 | removePlayer(); |
| 575 | return; |
| 576 | } |
| 577 | resetButtons(); |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Hide the media buttons and show only a restart button |
| 582 | */ |
| 583 | protected void resetButtons() { |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 584 | // Hide all the old buttons |
| 585 | for (int i = 0; i < mActionIds.length; i++) { |
| 586 | ImageButton thisBtn = mMediaNotifView.findViewById(mActionIds[i]); |
| 587 | if (thisBtn != null) { |
| 588 | thisBtn.setVisibility(View.GONE); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | // Add a restart button |
| 593 | ImageButton btn = mMediaNotifView.findViewById(mActionIds[0]); |
| 594 | btn.setOnClickListener(v -> { |
| 595 | Log.d(TAG, "Attempting to restart session"); |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 596 | QSMediaBrowser browser = new QSMediaBrowser(mContext, null, mServiceComponent); |
| 597 | browser.restart(); |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 598 | }); |
| 599 | btn.setImageDrawable(mContext.getResources().getDrawable(R.drawable.lb_ic_play)); |
| 600 | btn.setImageTintList(ColorStateList.valueOf(mForegroundColor)); |
| 601 | btn.setVisibility(View.VISIBLE); |
| 602 | } |
| 603 | |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 604 | private void makeActive() { |
| 605 | Assert.isMainThread(); |
| 606 | if (!mIsRegistered) { |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 607 | if (mLocalMediaManager != null) { |
| 608 | mLocalMediaManager.registerCallback(mDeviceCallback); |
| 609 | mLocalMediaManager.startScan(); |
| 610 | } |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 611 | mIsRegistered = true; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 612 | } |
| 613 | } |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 614 | |
| 615 | private void makeInactive() { |
| 616 | Assert.isMainThread(); |
| 617 | if (mIsRegistered) { |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 618 | if (mLocalMediaManager != null) { |
| 619 | mLocalMediaManager.stopScan(); |
| 620 | mLocalMediaManager.unregisterCallback(mDeviceCallback); |
| 621 | } |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 622 | mIsRegistered = false; |
| 623 | } |
| 624 | } |
| 625 | |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 626 | /** |
| 627 | * Verify that we can connect to the given component with a MediaBrowser, and if so, add that |
| 628 | * component to the list of resumption components |
| 629 | */ |
| 630 | private void tryUpdateResumptionList(ComponentName componentName) { |
| 631 | Log.d(TAG, "Testing if we can connect to " + componentName); |
| 632 | QSMediaBrowser.testConnection(mContext, |
| 633 | new QSMediaBrowser.Callback() { |
| 634 | @Override |
| 635 | public void onConnected() { |
| 636 | Log.d(TAG, "yes we can resume with " + componentName); |
| 637 | mServiceComponent = componentName; |
| 638 | updateResumptionList(componentName); |
| 639 | } |
| 640 | |
| 641 | @Override |
| 642 | public void onError() { |
| 643 | Log.d(TAG, "Cannot resume with " + componentName); |
| 644 | mServiceComponent = null; |
Beth Thibodeau | 89f5c76 | 2020-04-21 13:09:55 -0400 | [diff] [blame] | 645 | if (!hasMediaSession()) { |
| 646 | // If it's not active and we can't resume, remove |
| 647 | removePlayer(); |
| 648 | } |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 649 | } |
| 650 | }, |
| 651 | componentName); |
| 652 | } |
| 653 | |
| 654 | /** |
| 655 | * Add the component to the saved list of media browser services, checking for duplicates and |
| 656 | * removing older components that exceed the maximum limit |
| 657 | * @param componentName |
| 658 | */ |
| 659 | private synchronized void updateResumptionList(ComponentName componentName) { |
| 660 | // Add to front of saved list |
| 661 | if (mSharedPrefs == null) { |
| 662 | mSharedPrefs = mContext.getSharedPreferences(MEDIA_PREFERENCES, 0); |
| 663 | } |
| 664 | String componentString = componentName.flattenToString(); |
| 665 | String listString = mSharedPrefs.getString(MEDIA_PREFERENCE_KEY, null); |
| 666 | if (listString == null) { |
| 667 | listString = componentString; |
| 668 | } else { |
| 669 | String[] components = listString.split(QSMediaBrowser.DELIMITER); |
| 670 | StringBuilder updated = new StringBuilder(componentString); |
| 671 | int nBrowsers = 1; |
| 672 | for (int i = 0; i < components.length |
| 673 | && nBrowsers < QSMediaBrowser.MAX_RESUMPTION_CONTROLS; i++) { |
| 674 | if (componentString.equals(components[i])) { |
| 675 | continue; |
| 676 | } |
| 677 | updated.append(QSMediaBrowser.DELIMITER).append(components[i]); |
| 678 | nBrowsers++; |
| 679 | } |
| 680 | listString = updated.toString(); |
| 681 | } |
| 682 | mSharedPrefs.edit().putString(MEDIA_PREFERENCE_KEY, listString).apply(); |
| 683 | } |
| 684 | |
| 685 | /** |
| 686 | * Called when a player can't be resumed to give it an opportunity to hide or remove itself |
| 687 | */ |
| 688 | protected void removePlayer() { } |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 689 | } |