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; |
Robert Snoeberger | c981dc9 | 2020-04-27 15:00:50 -0400 | [diff] [blame] | 39 | import android.media.session.MediaController.PlaybackInfo; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 40 | import android.media.session.MediaSession; |
| 41 | import android.media.session.PlaybackState; |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 42 | import android.net.Uri; |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 43 | import android.service.media.MediaBrowserService; |
Beth Thibodeau | dba74bc | 2020-04-27 14:17:08 -0400 | [diff] [blame] | 44 | import android.text.TextUtils; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 45 | import android.util.Log; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 46 | import android.view.LayoutInflater; |
| 47 | import android.view.View; |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 48 | import android.view.View.OnAttachStateChangeListener; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 49 | import android.view.ViewGroup; |
| 50 | import android.widget.ImageButton; |
| 51 | import android.widget.ImageView; |
| 52 | import android.widget.LinearLayout; |
| 53 | import android.widget.TextView; |
| 54 | |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 55 | import androidx.annotation.Nullable; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 56 | import androidx.core.graphics.drawable.RoundedBitmapDrawable; |
| 57 | import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory; |
| 58 | |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 59 | import com.android.settingslib.media.LocalMediaManager; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 60 | import com.android.settingslib.media.MediaDevice; |
| 61 | import com.android.settingslib.media.MediaOutputSliceConstants; |
| 62 | import com.android.settingslib.widget.AdaptiveIcon; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 63 | import com.android.systemui.R; |
| 64 | import com.android.systemui.plugins.ActivityStarter; |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 65 | import com.android.systemui.qs.QSMediaBrowser; |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 66 | import com.android.systemui.util.Assert; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 67 | |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 68 | import java.io.IOException; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 69 | import java.util.List; |
| 70 | import java.util.concurrent.Executor; |
| 71 | |
| 72 | /** |
| 73 | * Base media control panel for System UI |
| 74 | */ |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 75 | public class MediaControlPanel { |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 76 | private static final String TAG = "MediaControlPanel"; |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 77 | @Nullable private final LocalMediaManager mLocalMediaManager; |
Beth Thibodeau | a51c314 | 2020-03-17 17:27:04 -0400 | [diff] [blame] | 78 | private final Executor mForegroundExecutor; |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 79 | protected final Executor mBackgroundExecutor; |
Beth Thibodeau | e561c00 | 2020-04-23 17:33:00 -0400 | [diff] [blame] | 80 | private final ActivityStarter mActivityStarter; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 81 | |
| 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 Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 89 | private MediaDevice mDevice; |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 90 | protected ComponentName mServiceComponent; |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 91 | private boolean mIsRegistered = false; |
Beth Thibodeau | a3d9098 | 2020-04-13 23:42:48 -0400 | [diff] [blame] | 92 | private String mKey; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 93 | |
| 94 | private final int[] mActionIds; |
| 95 | |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 96 | 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 Snoeberger | c981dc9 | 2020-04-27 15:00:50 -0400 | [diff] [blame] | 100 | private boolean mIsRemotePlayback; |
Beth Thibodeau | ee42e8c | 2020-04-30 01:09:29 -0400 | [diff] [blame] | 101 | private QSMediaBrowser mQSMediaBrowser; |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 102 | |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 103 | // Button IDs used in notifications |
| 104 | protected static final int[] NOTIF_ACTION_IDS = { |
| 105 | com.android.internal.R.id.action0, |
| 106 | com.android.internal.R.id.action1, |
| 107 | com.android.internal.R.id.action2, |
| 108 | com.android.internal.R.id.action3, |
| 109 | com.android.internal.R.id.action4 |
| 110 | }; |
| 111 | |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 112 | // URI fields to try loading album art from |
| 113 | private static final String[] ART_URIS = { |
| 114 | MediaMetadata.METADATA_KEY_ALBUM_ART_URI, |
| 115 | MediaMetadata.METADATA_KEY_ART_URI, |
| 116 | MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI |
| 117 | }; |
| 118 | |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 119 | private final MediaController.Callback mSessionCallback = new MediaController.Callback() { |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 120 | @Override |
| 121 | public void onSessionDestroyed() { |
| 122 | Log.d(TAG, "session destroyed"); |
| 123 | mController.unregisterCallback(mSessionCallback); |
| 124 | clearControls(); |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 125 | makeInactive(); |
| 126 | } |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 127 | @Override |
Robert Snoeberger | 445d441 | 2020-04-15 00:03:13 -0400 | [diff] [blame] | 128 | public void onPlaybackStateChanged(PlaybackState state) { |
| 129 | final int s = state != null ? state.getState() : PlaybackState.STATE_NONE; |
Beth Thibodeau | d664de2 | 2020-04-28 16:29:36 -0400 | [diff] [blame] | 130 | if (s == PlaybackState.STATE_NONE) { |
Robert Snoeberger | 445d441 | 2020-04-15 00:03:13 -0400 | [diff] [blame] | 131 | Log.d(TAG, "playback state change will trigger resumption, state=" + state); |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 132 | clearControls(); |
| 133 | makeInactive(); |
| 134 | } |
| 135 | } |
| 136 | }; |
| 137 | |
| 138 | private final OnAttachStateChangeListener mStateListener = new OnAttachStateChangeListener() { |
| 139 | @Override |
| 140 | public void onViewAttachedToWindow(View unused) { |
| 141 | makeActive(); |
| 142 | } |
| 143 | @Override |
| 144 | public void onViewDetachedFromWindow(View unused) { |
| 145 | makeInactive(); |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 146 | } |
| 147 | }; |
| 148 | |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 149 | private final LocalMediaManager.DeviceCallback mDeviceCallback = |
| 150 | new LocalMediaManager.DeviceCallback() { |
| 151 | @Override |
| 152 | public void onDeviceListUpdate(List<MediaDevice> devices) { |
| 153 | if (mLocalMediaManager == null) { |
| 154 | return; |
| 155 | } |
| 156 | MediaDevice currentDevice = mLocalMediaManager.getCurrentConnectedDevice(); |
| 157 | // Check because this can be called several times while changing devices |
| 158 | if (mDevice == null || !mDevice.equals(currentDevice)) { |
| 159 | mDevice = currentDevice; |
| 160 | updateDevice(mDevice); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public void onSelectedDeviceStateChanged(MediaDevice device, int state) { |
| 166 | if (mDevice == null || !mDevice.equals(device)) { |
| 167 | mDevice = device; |
| 168 | updateDevice(mDevice); |
| 169 | } |
| 170 | } |
| 171 | }; |
| 172 | |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 173 | /** |
| 174 | * Initialize a new control panel |
| 175 | * @param context |
| 176 | * @param parent |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 177 | * @param routeManager Manager used to listen for device change events. |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 178 | * @param layoutId layout resource to use for this control panel |
| 179 | * @param actionIds resource IDs for action buttons in the layout |
Beth Thibodeau | a51c314 | 2020-03-17 17:27:04 -0400 | [diff] [blame] | 180 | * @param foregroundExecutor foreground executor |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 181 | * @param backgroundExecutor background executor, used for processing artwork |
Beth Thibodeau | e561c00 | 2020-04-23 17:33:00 -0400 | [diff] [blame] | 182 | * @param activityStarter activity starter |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 183 | */ |
Robert Snoeberger | 445d441 | 2020-04-15 00:03:13 -0400 | [diff] [blame] | 184 | public MediaControlPanel(Context context, ViewGroup parent, |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 185 | @Nullable LocalMediaManager routeManager, @LayoutRes int layoutId, int[] actionIds, |
Beth Thibodeau | e561c00 | 2020-04-23 17:33:00 -0400 | [diff] [blame] | 186 | Executor foregroundExecutor, Executor backgroundExecutor, |
| 187 | ActivityStarter activityStarter) { |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 188 | mContext = context; |
| 189 | LayoutInflater inflater = LayoutInflater.from(mContext); |
| 190 | mMediaNotifView = (LinearLayout) inflater.inflate(layoutId, parent, false); |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 191 | // TODO(b/150854549): removeOnAttachStateChangeListener when this doesn't inflate views |
| 192 | // mStateListener shouldn't need to be unregistered since this object shares the same |
| 193 | // lifecycle with the inflated view. It would be better, however, if this controller used an |
| 194 | // attach/detach of views instead of inflating them in the constructor, which would allow |
| 195 | // mStateListener to be unregistered in detach. |
| 196 | mMediaNotifView.addOnAttachStateChangeListener(mStateListener); |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 197 | mLocalMediaManager = routeManager; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 198 | mActionIds = actionIds; |
Beth Thibodeau | a51c314 | 2020-03-17 17:27:04 -0400 | [diff] [blame] | 199 | mForegroundExecutor = foregroundExecutor; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 200 | mBackgroundExecutor = backgroundExecutor; |
Beth Thibodeau | e561c00 | 2020-04-23 17:33:00 -0400 | [diff] [blame] | 201 | mActivityStarter = activityStarter; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Get the view used to display media controls |
| 206 | * @return the view |
| 207 | */ |
| 208 | public View getView() { |
| 209 | return mMediaNotifView; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Get the context |
| 214 | * @return context |
| 215 | */ |
| 216 | public Context getContext() { |
| 217 | return mContext; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Update the media panel view for the given media session |
| 222 | * @param token |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 223 | * @param iconDrawable |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 224 | * @param largeIcon |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 225 | * @param iconColor |
| 226 | * @param bgColor |
| 227 | * @param contentIntent |
| 228 | * @param appNameString |
Beth Thibodeau | a3d9098 | 2020-04-13 23:42:48 -0400 | [diff] [blame] | 229 | * @param key |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 230 | */ |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 231 | public void setMediaSession(MediaSession.Token token, Drawable iconDrawable, Icon largeIcon, |
| 232 | int iconColor, int bgColor, PendingIntent contentIntent, String appNameString, |
| 233 | String key) { |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 234 | // Ensure that component names are updated if token has changed |
| 235 | if (mToken == null || !mToken.equals(token)) { |
Beth Thibodeau | ee42e8c | 2020-04-30 01:09:29 -0400 | [diff] [blame] | 236 | if (mQSMediaBrowser != null) { |
| 237 | Log.d(TAG, "Disconnecting old media browser"); |
| 238 | mQSMediaBrowser.disconnect(); |
| 239 | mQSMediaBrowser = null; |
| 240 | } |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 241 | mToken = token; |
| 242 | mServiceComponent = null; |
| 243 | mCheckedForResumption = false; |
| 244 | } |
| 245 | |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 246 | mForegroundColor = iconColor; |
| 247 | mBackgroundColor = bgColor; |
| 248 | mController = new MediaController(mContext, mToken); |
Beth Thibodeau | a3d9098 | 2020-04-13 23:42:48 -0400 | [diff] [blame] | 249 | mKey = key; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 250 | |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 251 | // Try to find a browser service component for this app |
| 252 | // TODO also check for a media button receiver intended for restarting (b/154127084) |
| 253 | // 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] | 254 | final String pkgName = mController.getPackageName(); |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 255 | if (mServiceComponent == null && !mCheckedForResumption) { |
| 256 | Log.d(TAG, "Checking for service component"); |
| 257 | PackageManager pm = mContext.getPackageManager(); |
| 258 | Intent resumeIntent = new Intent(MediaBrowserService.SERVICE_INTERFACE); |
| 259 | List<ResolveInfo> resumeInfo = pm.queryIntentServices(resumeIntent, 0); |
| 260 | if (resumeInfo != null) { |
| 261 | for (ResolveInfo inf : resumeInfo) { |
| 262 | if (inf.serviceInfo.packageName.equals(mController.getPackageName())) { |
| 263 | mBackgroundExecutor.execute(() -> |
| 264 | tryUpdateResumptionList(inf.getComponentInfo().getComponentName())); |
| 265 | break; |
| 266 | } |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 267 | } |
| 268 | } |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 269 | mCheckedForResumption = true; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | mController.registerCallback(mSessionCallback); |
| 273 | |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 274 | mMediaNotifView.setBackgroundTintList(ColorStateList.valueOf(mBackgroundColor)); |
| 275 | |
| 276 | // Click action |
Beth Thibodeau | a51c314 | 2020-03-17 17:27:04 -0400 | [diff] [blame] | 277 | if (contentIntent != null) { |
| 278 | mMediaNotifView.setOnClickListener(v -> { |
Beth Thibodeau | e561c00 | 2020-04-23 17:33:00 -0400 | [diff] [blame] | 279 | mActivityStarter.postStartActivityDismissingKeyguard(contentIntent); |
Beth Thibodeau | a51c314 | 2020-03-17 17:27:04 -0400 | [diff] [blame] | 280 | }); |
| 281 | } |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 282 | |
| 283 | // App icon |
| 284 | ImageView appIcon = mMediaNotifView.findViewById(R.id.icon); |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 285 | iconDrawable.setTint(mForegroundColor); |
| 286 | appIcon.setImageDrawable(iconDrawable); |
| 287 | |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 288 | // Transfer chip |
| 289 | mSeamless = mMediaNotifView.findViewById(R.id.media_seamless); |
Robert Snoeberger | 4429917 | 2020-04-24 22:22:21 -0400 | [diff] [blame] | 290 | if (mSeamless != null) { |
| 291 | if (mLocalMediaManager != null) { |
| 292 | mSeamless.setVisibility(View.VISIBLE); |
| 293 | updateDevice(mLocalMediaManager.getCurrentConnectedDevice()); |
| 294 | mSeamless.setOnClickListener(v -> { |
| 295 | final Intent intent = new Intent() |
| 296 | .setAction(MediaOutputSliceConstants.ACTION_MEDIA_OUTPUT) |
| 297 | .putExtra(MediaOutputSliceConstants.EXTRA_PACKAGE_NAME, |
| 298 | mController.getPackageName()) |
| 299 | .putExtra(MediaOutputSliceConstants.KEY_MEDIA_SESSION_TOKEN, mToken); |
| 300 | mActivityStarter.startActivity(intent, false, true /* dismissShade */, |
| 301 | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| 302 | }); |
| 303 | } else { |
| 304 | Log.d(TAG, "LocalMediaManager is null. Not binding output chip for pkg=" + pkgName); |
| 305 | } |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 306 | } |
Robert Snoeberger | c981dc9 | 2020-04-27 15:00:50 -0400 | [diff] [blame] | 307 | PlaybackInfo playbackInfo = mController.getPlaybackInfo(); |
| 308 | if (playbackInfo != null) { |
| 309 | mIsRemotePlayback = playbackInfo.getPlaybackType() == PlaybackInfo.PLAYBACK_TYPE_REMOTE; |
| 310 | } else { |
| 311 | Log.d(TAG, "PlaybackInfo was null. Defaulting to local playback."); |
| 312 | mIsRemotePlayback = false; |
| 313 | } |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 314 | |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 315 | makeActive(); |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 316 | |
| 317 | // App title (not in mini player) |
| 318 | TextView appName = mMediaNotifView.findViewById(R.id.app_name); |
| 319 | if (appName != null) { |
| 320 | appName.setText(appNameString); |
| 321 | appName.setTextColor(mForegroundColor); |
| 322 | } |
| 323 | |
Beth Thibodeau | 4b5ec83 | 2020-04-30 19:43:37 -0400 | [diff] [blame] | 324 | // Can be null! |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 325 | MediaMetadata mediaMetadata = mController.getMetadata(); |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 326 | |
| 327 | ImageView albumView = mMediaNotifView.findViewById(R.id.album_art); |
| 328 | if (albumView != null) { |
| 329 | // Resize art in a background thread |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 330 | mBackgroundExecutor.execute(() -> processAlbumArt(mediaMetadata, largeIcon, albumView)); |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | // Song name |
| 334 | TextView titleText = mMediaNotifView.findViewById(R.id.header_title); |
Beth Thibodeau | 4b5ec83 | 2020-04-30 19:43:37 -0400 | [diff] [blame] | 335 | String songName = ""; |
| 336 | if (mediaMetadata != null) { |
| 337 | songName = mediaMetadata.getString(MediaMetadata.METADATA_KEY_TITLE); |
| 338 | } |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 339 | titleText.setText(songName); |
| 340 | titleText.setTextColor(mForegroundColor); |
| 341 | |
| 342 | // Artist name (not in mini player) |
| 343 | TextView artistText = mMediaNotifView.findViewById(R.id.header_artist); |
| 344 | if (artistText != null) { |
Beth Thibodeau | 4b5ec83 | 2020-04-30 19:43:37 -0400 | [diff] [blame] | 345 | String artistName = ""; |
| 346 | if (mediaMetadata != null) { |
| 347 | artistName = mediaMetadata.getString(MediaMetadata.METADATA_KEY_ARTIST); |
| 348 | } |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 349 | artistText.setText(artistName); |
| 350 | artistText.setTextColor(mForegroundColor); |
| 351 | } |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Return the token for the current media session |
| 356 | * @return the token |
| 357 | */ |
| 358 | public MediaSession.Token getMediaSessionToken() { |
| 359 | return mToken; |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Get the current media controller |
| 364 | * @return the controller |
| 365 | */ |
| 366 | public MediaController getController() { |
| 367 | return mController; |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Get the name of the package associated with the current media controller |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 372 | * @return the package name, or null if no controller |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 373 | */ |
| 374 | public String getMediaPlayerPackage() { |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 375 | if (mController == null) { |
| 376 | return null; |
| 377 | } |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 378 | return mController.getPackageName(); |
| 379 | } |
| 380 | |
| 381 | /** |
Beth Thibodeau | a3d9098 | 2020-04-13 23:42:48 -0400 | [diff] [blame] | 382 | * Return the original notification's key |
| 383 | * @return The notification key |
| 384 | */ |
| 385 | public String getKey() { |
| 386 | return mKey; |
| 387 | } |
| 388 | |
| 389 | /** |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 390 | * Check whether this player has an attached media session. |
| 391 | * @return whether there is a controller with a current media session. |
| 392 | */ |
| 393 | public boolean hasMediaSession() { |
| 394 | return mController != null && mController.getPlaybackState() != null; |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Check whether the media controlled by this player is currently playing |
| 399 | * @return whether it is playing, or false if no controller information |
| 400 | */ |
| 401 | public boolean isPlaying() { |
| 402 | return isPlaying(mController); |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Check whether the given controller is currently playing |
| 407 | * @param controller media controller to check |
| 408 | * @return whether it is playing, or false if no controller information |
| 409 | */ |
| 410 | protected boolean isPlaying(MediaController controller) { |
| 411 | if (controller == null) { |
| 412 | return false; |
| 413 | } |
| 414 | |
| 415 | PlaybackState state = controller.getPlaybackState(); |
| 416 | if (state == null) { |
| 417 | return false; |
| 418 | } |
| 419 | |
| 420 | return (state.getState() == PlaybackState.STATE_PLAYING); |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * Process album art for layout |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 425 | * @param description media description |
| 426 | * @param albumView view to hold the album art |
| 427 | */ |
| 428 | protected void processAlbumArt(MediaDescription description, ImageView albumView) { |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 429 | Bitmap albumArt = null; |
| 430 | |
| 431 | // First try loading from URI |
| 432 | albumArt = loadBitmapFromUri(description.getIconUri()); |
| 433 | |
| 434 | // Then check bitmap |
| 435 | if (albumArt == null) { |
| 436 | albumArt = description.getIconBitmap(); |
| 437 | } |
| 438 | |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 439 | processAlbumArtInternal(albumArt, albumView); |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Process album art for layout |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 444 | * @param metadata media metadata |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 445 | * @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] | 446 | * @param albumView view to hold the album art |
| 447 | */ |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 448 | private void processAlbumArt(MediaMetadata metadata, Icon largeIcon, ImageView albumView) { |
| 449 | Bitmap albumArt = null; |
| 450 | |
Beth Thibodeau | 4b5ec83 | 2020-04-30 19:43:37 -0400 | [diff] [blame] | 451 | if (metadata != null) { |
| 452 | // First look in URI fields |
| 453 | for (String field : ART_URIS) { |
| 454 | String uriString = metadata.getString(field); |
| 455 | if (!TextUtils.isEmpty(uriString)) { |
| 456 | albumArt = loadBitmapFromUri(Uri.parse(uriString)); |
| 457 | if (albumArt != null) { |
| 458 | Log.d(TAG, "loaded art from " + field); |
| 459 | break; |
| 460 | } |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 461 | } |
| 462 | } |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 463 | |
Beth Thibodeau | 4b5ec83 | 2020-04-30 19:43:37 -0400 | [diff] [blame] | 464 | // Then check bitmap field |
| 465 | if (albumArt == null) { |
| 466 | albumArt = metadata.getBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART); |
| 467 | } |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | // Finally try the notification's largeIcon |
| 471 | if (albumArt == null && largeIcon != null) { |
| 472 | albumArt = largeIcon.getBitmap(); |
| 473 | } |
| 474 | |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 475 | processAlbumArtInternal(albumArt, albumView); |
| 476 | } |
| 477 | |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 478 | /** |
| 479 | * Load a bitmap from a URI |
| 480 | * @param uri |
| 481 | * @return bitmap, or null if couldn't be loaded |
| 482 | */ |
| 483 | private Bitmap loadBitmapFromUri(Uri uri) { |
Beth Thibodeau | dba74bc | 2020-04-27 14:17:08 -0400 | [diff] [blame] | 484 | // ImageDecoder requires a scheme of the following types |
| 485 | if (uri.getScheme() == null) { |
| 486 | return null; |
| 487 | } |
| 488 | |
| 489 | if (!uri.getScheme().equals(ContentResolver.SCHEME_CONTENT) |
| 490 | && !uri.getScheme().equals(ContentResolver.SCHEME_ANDROID_RESOURCE) |
| 491 | && !uri.getScheme().equals(ContentResolver.SCHEME_FILE)) { |
| 492 | return null; |
| 493 | } |
| 494 | |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 495 | ImageDecoder.Source source = ImageDecoder.createSource(mContext.getContentResolver(), uri); |
| 496 | try { |
| 497 | return ImageDecoder.decodeBitmap(source); |
| 498 | } catch (IOException e) { |
| 499 | e.printStackTrace(); |
| 500 | return null; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * Resize and crop the image if provided and update the control view |
| 506 | * @param albumArt Bitmap of art to display, or null to hide view |
| 507 | * @param albumView View that will hold the art |
| 508 | */ |
| 509 | private void processAlbumArtInternal(@Nullable Bitmap albumArt, ImageView albumView) { |
| 510 | // Resize |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 511 | RoundedBitmapDrawable roundedDrawable = null; |
| 512 | if (albumArt != null) { |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 513 | float radius = mContext.getResources().getDimension(R.dimen.qs_media_corner_radius); |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 514 | Bitmap original = albumArt.copy(Bitmap.Config.ARGB_8888, true); |
| 515 | int albumSize = (int) mContext.getResources().getDimension( |
| 516 | R.dimen.qs_media_album_size); |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 517 | Bitmap scaled = ThumbnailUtils.extractThumbnail(original, albumSize, albumSize); |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 518 | roundedDrawable = RoundedBitmapDrawableFactory.create(mContext.getResources(), scaled); |
| 519 | roundedDrawable.setCornerRadius(radius); |
| 520 | } else { |
| 521 | Log.e(TAG, "No album art available"); |
| 522 | } |
| 523 | |
| 524 | // Now that it's resized, update the UI |
| 525 | final RoundedBitmapDrawable result = roundedDrawable; |
Beth Thibodeau | a51c314 | 2020-03-17 17:27:04 -0400 | [diff] [blame] | 526 | mForegroundExecutor.execute(() -> { |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 527 | if (result != null) { |
| 528 | albumView.setImageDrawable(result); |
| 529 | albumView.setVisibility(View.VISIBLE); |
| 530 | } else { |
| 531 | albumView.setImageDrawable(null); |
| 532 | albumView.setVisibility(View.GONE); |
| 533 | } |
| 534 | }); |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Update the current device information |
| 539 | * @param device device information to display |
| 540 | */ |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 541 | private void updateDevice(MediaDevice device) { |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 542 | if (mSeamless == null) { |
| 543 | return; |
| 544 | } |
Beth Thibodeau | a51c314 | 2020-03-17 17:27:04 -0400 | [diff] [blame] | 545 | mForegroundExecutor.execute(() -> { |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 546 | updateChipInternal(device); |
| 547 | }); |
| 548 | } |
| 549 | |
| 550 | private void updateChipInternal(MediaDevice device) { |
| 551 | ColorStateList fgTintList = ColorStateList.valueOf(mForegroundColor); |
| 552 | |
| 553 | // Update the outline color |
| 554 | LinearLayout viewLayout = (LinearLayout) mSeamless; |
| 555 | RippleDrawable bkgDrawable = (RippleDrawable) viewLayout.getBackground(); |
| 556 | GradientDrawable rect = (GradientDrawable) bkgDrawable.getDrawable(0); |
| 557 | rect.setStroke(2, mForegroundColor); |
| 558 | rect.setColor(mBackgroundColor); |
| 559 | |
| 560 | ImageView iconView = mSeamless.findViewById(R.id.media_seamless_image); |
| 561 | TextView deviceName = mSeamless.findViewById(R.id.media_seamless_text); |
| 562 | deviceName.setTextColor(fgTintList); |
| 563 | |
Robert Snoeberger | c981dc9 | 2020-04-27 15:00:50 -0400 | [diff] [blame] | 564 | if (mIsRemotePlayback) { |
| 565 | mSeamless.setEnabled(false); |
| 566 | mSeamless.setAlpha(0.38f); |
| 567 | iconView.setImageResource(R.drawable.ic_hardware_speaker); |
| 568 | iconView.setVisibility(View.VISIBLE); |
| 569 | iconView.setImageTintList(fgTintList); |
| 570 | deviceName.setText(R.string.media_seamless_remote_device); |
| 571 | } else if (device != null) { |
| 572 | mSeamless.setEnabled(true); |
| 573 | mSeamless.setAlpha(1f); |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 574 | Drawable icon = device.getIcon(); |
| 575 | iconView.setVisibility(View.VISIBLE); |
| 576 | iconView.setImageTintList(fgTintList); |
| 577 | |
| 578 | if (icon instanceof AdaptiveIcon) { |
| 579 | AdaptiveIcon aIcon = (AdaptiveIcon) icon; |
| 580 | aIcon.setBackgroundColor(mBackgroundColor); |
| 581 | iconView.setImageDrawable(aIcon); |
| 582 | } else { |
| 583 | iconView.setImageDrawable(icon); |
| 584 | } |
| 585 | deviceName.setText(device.getName()); |
| 586 | } else { |
| 587 | // Reset to default |
Robert Snoeberger | 4429917 | 2020-04-24 22:22:21 -0400 | [diff] [blame] | 588 | Log.d(TAG, "device is null. Not binding output chip."); |
Robert Snoeberger | c981dc9 | 2020-04-27 15:00:50 -0400 | [diff] [blame] | 589 | mSeamless.setEnabled(true); |
| 590 | mSeamless.setAlpha(1f); |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 591 | iconView.setVisibility(View.GONE); |
| 592 | deviceName.setText(com.android.internal.R.string.ext_media_seamless_action); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | /** |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 597 | * Puts controls into a resumption state if possible, or calls removePlayer if no component was |
| 598 | * found that could resume playback |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 599 | */ |
| 600 | public void clearControls() { |
Robert Snoeberger | 445d441 | 2020-04-15 00:03:13 -0400 | [diff] [blame] | 601 | Log.d(TAG, "clearControls to resumption state package=" + getMediaPlayerPackage()); |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 602 | if (mServiceComponent == null) { |
| 603 | // If we don't have a way to resume, just remove the player altogether |
| 604 | Log.d(TAG, "Removing unresumable controls"); |
| 605 | removePlayer(); |
| 606 | return; |
| 607 | } |
| 608 | resetButtons(); |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * Hide the media buttons and show only a restart button |
| 613 | */ |
| 614 | protected void resetButtons() { |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 615 | // Hide all the old buttons |
| 616 | for (int i = 0; i < mActionIds.length; i++) { |
| 617 | ImageButton thisBtn = mMediaNotifView.findViewById(mActionIds[i]); |
| 618 | if (thisBtn != null) { |
| 619 | thisBtn.setVisibility(View.GONE); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | // Add a restart button |
| 624 | ImageButton btn = mMediaNotifView.findViewById(mActionIds[0]); |
| 625 | btn.setOnClickListener(v -> { |
| 626 | Log.d(TAG, "Attempting to restart session"); |
Beth Thibodeau | ee42e8c | 2020-04-30 01:09:29 -0400 | [diff] [blame] | 627 | if (mQSMediaBrowser != null) { |
| 628 | mQSMediaBrowser.disconnect(); |
| 629 | } |
| 630 | mQSMediaBrowser = new QSMediaBrowser(mContext, new QSMediaBrowser.Callback(){ |
| 631 | @Override |
| 632 | public void onConnected() { |
| 633 | Log.d(TAG, "Successfully restarted"); |
| 634 | } |
| 635 | @Override |
| 636 | public void onError() { |
| 637 | Log.e(TAG, "Error restarting"); |
| 638 | mQSMediaBrowser.disconnect(); |
| 639 | mQSMediaBrowser = null; |
| 640 | } |
| 641 | }, mServiceComponent); |
| 642 | mQSMediaBrowser.restart(); |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 643 | }); |
| 644 | btn.setImageDrawable(mContext.getResources().getDrawable(R.drawable.lb_ic_play)); |
| 645 | btn.setImageTintList(ColorStateList.valueOf(mForegroundColor)); |
| 646 | btn.setVisibility(View.VISIBLE); |
| 647 | } |
| 648 | |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 649 | private void makeActive() { |
| 650 | Assert.isMainThread(); |
| 651 | if (!mIsRegistered) { |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 652 | if (mLocalMediaManager != null) { |
| 653 | mLocalMediaManager.registerCallback(mDeviceCallback); |
| 654 | mLocalMediaManager.startScan(); |
| 655 | } |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 656 | mIsRegistered = true; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 657 | } |
| 658 | } |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 659 | |
| 660 | private void makeInactive() { |
| 661 | Assert.isMainThread(); |
| 662 | if (mIsRegistered) { |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 663 | if (mLocalMediaManager != null) { |
| 664 | mLocalMediaManager.stopScan(); |
| 665 | mLocalMediaManager.unregisterCallback(mDeviceCallback); |
| 666 | } |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 667 | mIsRegistered = false; |
| 668 | } |
| 669 | } |
| 670 | |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 671 | /** |
| 672 | * Verify that we can connect to the given component with a MediaBrowser, and if so, add that |
| 673 | * component to the list of resumption components |
| 674 | */ |
| 675 | private void tryUpdateResumptionList(ComponentName componentName) { |
| 676 | Log.d(TAG, "Testing if we can connect to " + componentName); |
Beth Thibodeau | ee42e8c | 2020-04-30 01:09:29 -0400 | [diff] [blame] | 677 | if (mQSMediaBrowser != null) { |
| 678 | mQSMediaBrowser.disconnect(); |
| 679 | } |
| 680 | mQSMediaBrowser = new QSMediaBrowser(mContext, |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 681 | new QSMediaBrowser.Callback() { |
| 682 | @Override |
| 683 | public void onConnected() { |
| 684 | Log.d(TAG, "yes we can resume with " + componentName); |
| 685 | mServiceComponent = componentName; |
| 686 | updateResumptionList(componentName); |
Beth Thibodeau | ee42e8c | 2020-04-30 01:09:29 -0400 | [diff] [blame] | 687 | mQSMediaBrowser.disconnect(); |
| 688 | mQSMediaBrowser = null; |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | @Override |
| 692 | public void onError() { |
| 693 | Log.d(TAG, "Cannot resume with " + componentName); |
| 694 | mServiceComponent = null; |
Beth Thibodeau | 89f5c76 | 2020-04-21 13:09:55 -0400 | [diff] [blame] | 695 | if (!hasMediaSession()) { |
| 696 | // If it's not active and we can't resume, remove |
| 697 | removePlayer(); |
| 698 | } |
Beth Thibodeau | ee42e8c | 2020-04-30 01:09:29 -0400 | [diff] [blame] | 699 | mQSMediaBrowser.disconnect(); |
| 700 | mQSMediaBrowser = null; |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 701 | } |
| 702 | }, |
| 703 | componentName); |
Beth Thibodeau | ee42e8c | 2020-04-30 01:09:29 -0400 | [diff] [blame] | 704 | mQSMediaBrowser.testConnection(); |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | /** |
| 708 | * Add the component to the saved list of media browser services, checking for duplicates and |
| 709 | * removing older components that exceed the maximum limit |
| 710 | * @param componentName |
| 711 | */ |
| 712 | private synchronized void updateResumptionList(ComponentName componentName) { |
| 713 | // Add to front of saved list |
| 714 | if (mSharedPrefs == null) { |
| 715 | mSharedPrefs = mContext.getSharedPreferences(MEDIA_PREFERENCES, 0); |
| 716 | } |
| 717 | String componentString = componentName.flattenToString(); |
| 718 | String listString = mSharedPrefs.getString(MEDIA_PREFERENCE_KEY, null); |
| 719 | if (listString == null) { |
| 720 | listString = componentString; |
| 721 | } else { |
| 722 | String[] components = listString.split(QSMediaBrowser.DELIMITER); |
| 723 | StringBuilder updated = new StringBuilder(componentString); |
| 724 | int nBrowsers = 1; |
| 725 | for (int i = 0; i < components.length |
| 726 | && nBrowsers < QSMediaBrowser.MAX_RESUMPTION_CONTROLS; i++) { |
| 727 | if (componentString.equals(components[i])) { |
| 728 | continue; |
| 729 | } |
| 730 | updated.append(QSMediaBrowser.DELIMITER).append(components[i]); |
| 731 | nBrowsers++; |
| 732 | } |
| 733 | listString = updated.toString(); |
| 734 | } |
| 735 | mSharedPrefs.edit().putString(MEDIA_PREFERENCE_KEY, listString).apply(); |
| 736 | } |
| 737 | |
| 738 | /** |
| 739 | * Called when a player can't be resumed to give it an opportunity to hide or remove itself |
| 740 | */ |
| 741 | protected void removePlayer() { } |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 742 | } |