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 | |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 19 | import static com.android.systemui.util.SysuiLifecycle.viewAttachLifecycle; |
| 20 | |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 21 | import android.app.PendingIntent; |
| 22 | import android.content.ComponentName; |
| 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; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 29 | import android.graphics.drawable.Drawable; |
| 30 | import android.graphics.drawable.GradientDrawable; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 31 | import android.graphics.drawable.RippleDrawable; |
| 32 | import android.media.MediaMetadata; |
| 33 | import android.media.session.MediaController; |
Robert Snoeberger | c981dc9 | 2020-04-27 15:00:50 -0400 | [diff] [blame] | 34 | import android.media.session.MediaController.PlaybackInfo; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 35 | import android.media.session.MediaSession; |
| 36 | import android.media.session.PlaybackState; |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 37 | import android.service.media.MediaBrowserService; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 38 | import android.util.Log; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 39 | import android.view.LayoutInflater; |
| 40 | import android.view.View; |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 41 | import android.view.View.OnAttachStateChangeListener; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 42 | import android.view.ViewGroup; |
| 43 | import android.widget.ImageButton; |
| 44 | import android.widget.ImageView; |
| 45 | import android.widget.LinearLayout; |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 46 | import android.widget.SeekBar; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 47 | import android.widget.TextView; |
| 48 | |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 49 | import androidx.annotation.Nullable; |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame^] | 50 | import androidx.constraintlayout.motion.widget.Key; |
| 51 | import androidx.constraintlayout.motion.widget.KeyAttributes; |
| 52 | import androidx.constraintlayout.motion.widget.KeyFrames; |
Selim Cinek | d835792 | 2020-04-10 15:06:53 -0700 | [diff] [blame] | 53 | import androidx.constraintlayout.motion.widget.MotionLayout; |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 54 | import androidx.constraintlayout.widget.ConstraintSet; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 55 | |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 56 | import com.android.settingslib.media.LocalMediaManager; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 57 | import com.android.settingslib.media.MediaDevice; |
| 58 | import com.android.settingslib.media.MediaOutputSliceConstants; |
| 59 | import com.android.settingslib.widget.AdaptiveIcon; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 60 | import com.android.systemui.R; |
| 61 | import com.android.systemui.plugins.ActivityStarter; |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 62 | import com.android.systemui.qs.QSMediaBrowser; |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 63 | import com.android.systemui.util.Assert; |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 64 | import com.android.systemui.util.concurrency.DelayableExecutor; |
| 65 | |
| 66 | import org.jetbrains.annotations.NotNull; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 67 | |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame^] | 68 | import java.util.ArrayList; |
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 | /** |
Selim Cinek | d835792 | 2020-04-10 15:06:53 -0700 | [diff] [blame] | 73 | * A view controller used for Media Playback. |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 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; |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 78 | |
| 79 | // Button IDs for QS controls |
| 80 | static final int[] ACTION_IDS = { |
| 81 | R.id.action0, |
| 82 | R.id.action1, |
| 83 | R.id.action2, |
| 84 | R.id.action3, |
| 85 | R.id.action4 |
| 86 | }; |
| 87 | |
| 88 | private final SeekBarViewModel mSeekBarViewModel; |
| 89 | private final SeekBarObserver mSeekBarObserver; |
Beth Thibodeau | a51c314 | 2020-03-17 17:27:04 -0400 | [diff] [blame] | 90 | private final Executor mForegroundExecutor; |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 91 | protected final Executor mBackgroundExecutor; |
Beth Thibodeau | e561c00 | 2020-04-23 17:33:00 -0400 | [diff] [blame] | 92 | private final ActivityStarter mActivityStarter; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 93 | |
| 94 | private Context mContext; |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame^] | 95 | private MotionLayout mMediaNotifView; |
| 96 | private final View mBackground; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 97 | private View mSeamless; |
| 98 | private MediaSession.Token mToken; |
| 99 | private MediaController mController; |
| 100 | private int mForegroundColor; |
| 101 | private int mBackgroundColor; |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 102 | private MediaDevice mDevice; |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 103 | protected ComponentName mServiceComponent; |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 104 | private boolean mIsRegistered = false; |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame^] | 105 | private final List<KeyFrames> mKeyFrames; |
Beth Thibodeau | a3d9098 | 2020-04-13 23:42:48 -0400 | [diff] [blame] | 106 | private String mKey; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 107 | |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 108 | public static final String MEDIA_PREFERENCES = "media_control_prefs"; |
| 109 | public static final String MEDIA_PREFERENCE_KEY = "browser_components"; |
| 110 | private SharedPreferences mSharedPrefs; |
| 111 | private boolean mCheckedForResumption = false; |
Robert Snoeberger | c981dc9 | 2020-04-27 15:00:50 -0400 | [diff] [blame] | 112 | private boolean mIsRemotePlayback; |
Beth Thibodeau | ee42e8c | 2020-04-30 01:09:29 -0400 | [diff] [blame] | 113 | private QSMediaBrowser mQSMediaBrowser; |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 114 | |
Beth Thibodeau | d3ad81d | 2020-04-20 23:26:25 -0400 | [diff] [blame] | 115 | // URI fields to try loading album art from |
| 116 | private static final String[] ART_URIS = { |
| 117 | MediaMetadata.METADATA_KEY_ALBUM_ART_URI, |
| 118 | MediaMetadata.METADATA_KEY_ART_URI, |
| 119 | MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI |
| 120 | }; |
| 121 | |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 122 | private final MediaController.Callback mSessionCallback = new MediaController.Callback() { |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 123 | @Override |
| 124 | public void onSessionDestroyed() { |
| 125 | Log.d(TAG, "session destroyed"); |
| 126 | mController.unregisterCallback(mSessionCallback); |
| 127 | clearControls(); |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 128 | makeInactive(); |
| 129 | } |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 130 | @Override |
Robert Snoeberger | 445d441 | 2020-04-15 00:03:13 -0400 | [diff] [blame] | 131 | public void onPlaybackStateChanged(PlaybackState state) { |
| 132 | final int s = state != null ? state.getState() : PlaybackState.STATE_NONE; |
Beth Thibodeau | d664de2 | 2020-04-28 16:29:36 -0400 | [diff] [blame] | 133 | if (s == PlaybackState.STATE_NONE) { |
Robert Snoeberger | 445d441 | 2020-04-15 00:03:13 -0400 | [diff] [blame] | 134 | Log.d(TAG, "playback state change will trigger resumption, state=" + state); |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 135 | clearControls(); |
| 136 | makeInactive(); |
| 137 | } |
| 138 | } |
| 139 | }; |
| 140 | |
| 141 | private final OnAttachStateChangeListener mStateListener = new OnAttachStateChangeListener() { |
| 142 | @Override |
| 143 | public void onViewAttachedToWindow(View unused) { |
| 144 | makeActive(); |
| 145 | } |
| 146 | @Override |
| 147 | public void onViewDetachedFromWindow(View unused) { |
| 148 | makeInactive(); |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 149 | } |
| 150 | }; |
| 151 | |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 152 | private final LocalMediaManager.DeviceCallback mDeviceCallback = |
| 153 | new LocalMediaManager.DeviceCallback() { |
| 154 | @Override |
| 155 | public void onDeviceListUpdate(List<MediaDevice> devices) { |
| 156 | if (mLocalMediaManager == null) { |
| 157 | return; |
| 158 | } |
| 159 | MediaDevice currentDevice = mLocalMediaManager.getCurrentConnectedDevice(); |
| 160 | // Check because this can be called several times while changing devices |
| 161 | if (mDevice == null || !mDevice.equals(currentDevice)) { |
| 162 | mDevice = currentDevice; |
| 163 | updateDevice(mDevice); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public void onSelectedDeviceStateChanged(MediaDevice device, int state) { |
| 169 | if (mDevice == null || !mDevice.equals(device)) { |
| 170 | mDevice = device; |
| 171 | updateDevice(mDevice); |
| 172 | } |
| 173 | } |
| 174 | }; |
| 175 | |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 176 | /** |
| 177 | * Initialize a new control panel |
| 178 | * @param context |
| 179 | * @param parent |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 180 | * @param routeManager Manager used to listen for device change events. |
Beth Thibodeau | a51c314 | 2020-03-17 17:27:04 -0400 | [diff] [blame] | 181 | * @param foregroundExecutor foreground executor |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 182 | * @param backgroundExecutor background executor, used for processing artwork |
Beth Thibodeau | e561c00 | 2020-04-23 17:33:00 -0400 | [diff] [blame] | 183 | * @param activityStarter activity starter |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 184 | */ |
Robert Snoeberger | 445d441 | 2020-04-15 00:03:13 -0400 | [diff] [blame] | 185 | public MediaControlPanel(Context context, ViewGroup parent, |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 186 | @Nullable LocalMediaManager routeManager, Executor foregroundExecutor, |
| 187 | DelayableExecutor backgroundExecutor, ActivityStarter activityStarter) { |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 188 | mContext = context; |
| 189 | LayoutInflater inflater = LayoutInflater.from(mContext); |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 190 | mMediaNotifView = (MotionLayout) inflater.inflate(R.layout.qs_media_panel, parent, false); |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame^] | 191 | mBackground = mMediaNotifView.findViewById(R.id.media_background); |
| 192 | mKeyFrames = mMediaNotifView.getDefinedTransitions().get(0).getKeyFrameList(); |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 193 | // TODO(b/150854549): removeOnAttachStateChangeListener when this doesn't inflate views |
| 194 | // mStateListener shouldn't need to be unregistered since this object shares the same |
| 195 | // lifecycle with the inflated view. It would be better, however, if this controller used an |
| 196 | // attach/detach of views instead of inflating them in the constructor, which would allow |
| 197 | // mStateListener to be unregistered in detach. |
| 198 | mMediaNotifView.addOnAttachStateChangeListener(mStateListener); |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 199 | mLocalMediaManager = routeManager; |
Beth Thibodeau | a51c314 | 2020-03-17 17:27:04 -0400 | [diff] [blame] | 200 | mForegroundExecutor = foregroundExecutor; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 201 | mBackgroundExecutor = backgroundExecutor; |
Beth Thibodeau | e561c00 | 2020-04-23 17:33:00 -0400 | [diff] [blame] | 202 | mActivityStarter = activityStarter; |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 203 | mSeekBarViewModel = new SeekBarViewModel(backgroundExecutor); |
| 204 | mSeekBarObserver = new SeekBarObserver(getView()); |
| 205 | // Can't use the viewAttachLifecycle of media player because remove/add is used to adjust |
| 206 | // priority of players. As soon as it is removed, the lifecycle will end and the seek bar |
| 207 | // will stop updating. So, use the lifecycle of the parent instead. |
| 208 | // TODO: this parent is also detached, need to fix that |
| 209 | mSeekBarViewModel.getProgress().observe(viewAttachLifecycle(parent), mSeekBarObserver); |
| 210 | SeekBar bar = getView().findViewById(R.id.media_progress_bar); |
| 211 | bar.setOnSeekBarChangeListener(mSeekBarViewModel.getSeekBarListener()); |
| 212 | bar.setOnTouchListener(mSeekBarViewModel.getSeekBarTouchListener()); |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Get the view used to display media controls |
| 217 | * @return the view |
| 218 | */ |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 219 | public MotionLayout getView() { |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 220 | return mMediaNotifView; |
| 221 | } |
| 222 | |
| 223 | /** |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 224 | * Sets the listening state of the player. |
| 225 | * |
| 226 | * Should be set to true when the QS panel is open. Otherwise, false. This is a signal to avoid |
| 227 | * unnecessary work when the QS panel is closed. |
| 228 | * |
| 229 | * @param listening True when player should be active. Otherwise, false. |
| 230 | */ |
| 231 | public void setListening(boolean listening) { |
| 232 | mSeekBarViewModel.setListening(listening); |
| 233 | } |
| 234 | |
| 235 | /** |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 236 | * Get the context |
| 237 | * @return context |
| 238 | */ |
| 239 | public Context getContext() { |
| 240 | return mContext; |
| 241 | } |
| 242 | |
| 243 | /** |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 244 | * Bind this view based on the data given |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 245 | */ |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 246 | public void bind(@NotNull MediaData data) { |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame^] | 247 | MediaSession.Token token = data.getToken(); |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 248 | mForegroundColor = data.getForegroundColor(); |
| 249 | mBackgroundColor = data.getBackgroundColor(); |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 250 | if (mToken == null || !mToken.equals(token)) { |
Beth Thibodeau | ee42e8c | 2020-04-30 01:09:29 -0400 | [diff] [blame] | 251 | if (mQSMediaBrowser != null) { |
| 252 | Log.d(TAG, "Disconnecting old media browser"); |
| 253 | mQSMediaBrowser.disconnect(); |
| 254 | mQSMediaBrowser = null; |
| 255 | } |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 256 | mToken = token; |
| 257 | mServiceComponent = null; |
| 258 | mCheckedForResumption = false; |
| 259 | } |
| 260 | |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 261 | mController = new MediaController(mContext, mToken); |
| 262 | |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 263 | // Try to find a browser service component for this app |
| 264 | // TODO also check for a media button receiver intended for restarting (b/154127084) |
| 265 | // 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] | 266 | final String pkgName = mController.getPackageName(); |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 267 | if (mServiceComponent == null && !mCheckedForResumption) { |
| 268 | Log.d(TAG, "Checking for service component"); |
| 269 | PackageManager pm = mContext.getPackageManager(); |
| 270 | Intent resumeIntent = new Intent(MediaBrowserService.SERVICE_INTERFACE); |
| 271 | List<ResolveInfo> resumeInfo = pm.queryIntentServices(resumeIntent, 0); |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 272 | // TODO: look into this resumption |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 273 | if (resumeInfo != null) { |
| 274 | for (ResolveInfo inf : resumeInfo) { |
| 275 | if (inf.serviceInfo.packageName.equals(mController.getPackageName())) { |
| 276 | mBackgroundExecutor.execute(() -> |
| 277 | tryUpdateResumptionList(inf.getComponentInfo().getComponentName())); |
| 278 | break; |
| 279 | } |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 280 | } |
| 281 | } |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 282 | mCheckedForResumption = true; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | mController.registerCallback(mSessionCallback); |
| 286 | |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame^] | 287 | mMediaNotifView.requireViewById(R.id.media_background).setBackgroundTintList( |
| 288 | ColorStateList.valueOf(mBackgroundColor)); |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 289 | |
| 290 | // Click action |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 291 | PendingIntent clickIntent = data.getClickIntent(); |
| 292 | if (clickIntent != null) { |
Beth Thibodeau | a51c314 | 2020-03-17 17:27:04 -0400 | [diff] [blame] | 293 | mMediaNotifView.setOnClickListener(v -> { |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 294 | mActivityStarter.postStartActivityDismissingKeyguard(clickIntent); |
Beth Thibodeau | a51c314 | 2020-03-17 17:27:04 -0400 | [diff] [blame] | 295 | }); |
| 296 | } |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 297 | |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame^] | 298 | ImageView albumView = mMediaNotifView.findViewById(R.id.album_art); |
| 299 | albumView.setImageDrawable(data.getArtwork()); |
| 300 | |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 301 | // App icon |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 302 | ImageView appIcon = mMediaNotifView.requireViewById(R.id.icon); |
| 303 | // TODO: look at iconDrawable |
| 304 | Drawable iconDrawable = data.getAppIcon(); |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 305 | iconDrawable.setTint(mForegroundColor); |
| 306 | appIcon.setImageDrawable(iconDrawable); |
| 307 | |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame^] | 308 | // Song name |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 309 | TextView titleText = mMediaNotifView.requireViewById(R.id.header_title); |
| 310 | titleText.setText(data.getSong()); |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame^] | 311 | titleText.setTextColor(data.getForegroundColor()); |
| 312 | |
| 313 | // App title |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 314 | TextView appName = mMediaNotifView.requireViewById(R.id.app_name); |
| 315 | appName.setText(data.getApp()); |
| 316 | appName.setTextColor(mForegroundColor); |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame^] | 317 | |
| 318 | // Artist name |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 319 | TextView artistText = mMediaNotifView.requireViewById(R.id.header_artist); |
| 320 | artistText.setText(data.getArtist()); |
| 321 | artistText.setTextColor(mForegroundColor); |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame^] | 322 | |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 323 | // Transfer chip |
| 324 | mSeamless = mMediaNotifView.findViewById(R.id.media_seamless); |
Robert Snoeberger | 4429917 | 2020-04-24 22:22:21 -0400 | [diff] [blame] | 325 | if (mSeamless != null) { |
| 326 | if (mLocalMediaManager != null) { |
| 327 | mSeamless.setVisibility(View.VISIBLE); |
| 328 | updateDevice(mLocalMediaManager.getCurrentConnectedDevice()); |
| 329 | mSeamless.setOnClickListener(v -> { |
| 330 | final Intent intent = new Intent() |
| 331 | .setAction(MediaOutputSliceConstants.ACTION_MEDIA_OUTPUT) |
| 332 | .putExtra(MediaOutputSliceConstants.EXTRA_PACKAGE_NAME, |
| 333 | mController.getPackageName()) |
| 334 | .putExtra(MediaOutputSliceConstants.KEY_MEDIA_SESSION_TOKEN, mToken); |
| 335 | mActivityStarter.startActivity(intent, false, true /* dismissShade */, |
| 336 | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| 337 | }); |
| 338 | } else { |
| 339 | Log.d(TAG, "LocalMediaManager is null. Not binding output chip for pkg=" + pkgName); |
| 340 | } |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 341 | } |
Robert Snoeberger | c981dc9 | 2020-04-27 15:00:50 -0400 | [diff] [blame] | 342 | PlaybackInfo playbackInfo = mController.getPlaybackInfo(); |
| 343 | if (playbackInfo != null) { |
| 344 | mIsRemotePlayback = playbackInfo.getPlaybackType() == PlaybackInfo.PLAYBACK_TYPE_REMOTE; |
| 345 | } else { |
| 346 | Log.d(TAG, "PlaybackInfo was null. Defaulting to local playback."); |
| 347 | mIsRemotePlayback = false; |
| 348 | } |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 349 | |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 350 | ConstraintSet expandedSet = mMediaNotifView.getConstraintSet(R.id.expanded); |
| 351 | ConstraintSet collapsedSet = mMediaNotifView.getConstraintSet(R.id.collapsed); |
| 352 | List<Integer> actionsWhenCollapsed = data.getActionsToShowInCompact(); |
| 353 | // Media controls |
| 354 | int i = 0; |
| 355 | List<MediaAction> actionIcons = data.getActions(); |
| 356 | for (; i < actionIcons.size() && i < ACTION_IDS.length; i++) { |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame^] | 357 | int actionId = ACTION_IDS[i]; |
| 358 | final ImageButton button = mMediaNotifView.findViewById(actionId); |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 359 | MediaAction mediaAction = actionIcons.get(i); |
| 360 | button.setImageDrawable(mediaAction.getDrawable()); |
| 361 | button.setContentDescription(mediaAction.getContentDescription()); |
| 362 | button.setImageTintList(ColorStateList.valueOf(mForegroundColor)); |
| 363 | PendingIntent actionIntent = mediaAction.getIntent(); |
| 364 | |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame^] | 365 | if (mBackground.getBackground() instanceof IlluminationDrawable) { |
| 366 | ((IlluminationDrawable) mBackground.getBackground()) |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 367 | .setupTouch(button, mMediaNotifView); |
| 368 | } |
| 369 | |
| 370 | button.setOnClickListener(v -> { |
| 371 | if (actionIntent != null) { |
| 372 | try { |
| 373 | actionIntent.send(); |
| 374 | } catch (PendingIntent.CanceledException e) { |
| 375 | e.printStackTrace(); |
| 376 | } |
| 377 | } |
| 378 | }); |
| 379 | boolean visibleInCompat = actionsWhenCollapsed.contains(i); |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame^] | 380 | updateKeyFrameVisibility(actionId, visibleInCompat); |
| 381 | collapsedSet.setVisibility(actionId, |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 382 | visibleInCompat ? ConstraintSet.VISIBLE : ConstraintSet.GONE); |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame^] | 383 | expandedSet.setVisibility(actionId, ConstraintSet.VISIBLE); |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | // Hide any unused buttons |
| 387 | for (; i < ACTION_IDS.length; i++) { |
| 388 | expandedSet.setVisibility(ACTION_IDS[i], ConstraintSet.GONE); |
| 389 | collapsedSet.setVisibility(ACTION_IDS[i], ConstraintSet.GONE); |
| 390 | } |
| 391 | |
| 392 | // Seek Bar |
| 393 | final MediaController controller = new MediaController(getContext(), data.getToken()); |
| 394 | mBackgroundExecutor.execute( |
| 395 | () -> mSeekBarViewModel.updateController(controller, data.getForegroundColor())); |
| 396 | |
| 397 | // Set up long press menu |
| 398 | // TODO: b/156036025 bring back media guts |
| 399 | |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 400 | makeActive(); |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame^] | 401 | } |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 402 | |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame^] | 403 | /** |
| 404 | * Updates the keyframe visibility such that only views that are not visible actually go |
| 405 | * through a transition and fade in. |
| 406 | * |
| 407 | * @param actionId the id to change |
| 408 | * @param visible is the view visible |
| 409 | */ |
| 410 | private void updateKeyFrameVisibility(int actionId, boolean visible) { |
| 411 | for (int i = 0; i < mKeyFrames.size(); i++) { |
| 412 | KeyFrames keyframe = mKeyFrames.get(i); |
| 413 | ArrayList<Key> viewKeyFrames = keyframe.getKeyFramesForView(actionId); |
| 414 | for (int j = 0; j < viewKeyFrames.size(); j++) { |
| 415 | Key key = viewKeyFrames.get(j); |
| 416 | if (key instanceof KeyAttributes) { |
| 417 | KeyAttributes attributes = (KeyAttributes) key; |
| 418 | attributes.setValue("alpha", visible ? 1.0f : 0.0f); |
| 419 | } |
Beth Thibodeau | 4b5ec83 | 2020-04-30 19:43:37 -0400 | [diff] [blame] | 420 | } |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 421 | } |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Return the token for the current media session |
| 426 | * @return the token |
| 427 | */ |
| 428 | public MediaSession.Token getMediaSessionToken() { |
| 429 | return mToken; |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Get the current media controller |
| 434 | * @return the controller |
| 435 | */ |
| 436 | public MediaController getController() { |
| 437 | return mController; |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * Get the name of the package associated with the current media controller |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 442 | * @return the package name, or null if no controller |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 443 | */ |
| 444 | public String getMediaPlayerPackage() { |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 445 | if (mController == null) { |
| 446 | return null; |
| 447 | } |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 448 | return mController.getPackageName(); |
| 449 | } |
| 450 | |
| 451 | /** |
Beth Thibodeau | a3d9098 | 2020-04-13 23:42:48 -0400 | [diff] [blame] | 452 | * Return the original notification's key |
| 453 | * @return The notification key |
| 454 | */ |
| 455 | public String getKey() { |
| 456 | return mKey; |
| 457 | } |
| 458 | |
| 459 | /** |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 460 | * Check whether this player has an attached media session. |
| 461 | * @return whether there is a controller with a current media session. |
| 462 | */ |
| 463 | public boolean hasMediaSession() { |
| 464 | return mController != null && mController.getPlaybackState() != null; |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Check whether the media controlled by this player is currently playing |
| 469 | * @return whether it is playing, or false if no controller information |
| 470 | */ |
| 471 | public boolean isPlaying() { |
| 472 | return isPlaying(mController); |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * Check whether the given controller is currently playing |
| 477 | * @param controller media controller to check |
| 478 | * @return whether it is playing, or false if no controller information |
| 479 | */ |
| 480 | protected boolean isPlaying(MediaController controller) { |
| 481 | if (controller == null) { |
| 482 | return false; |
| 483 | } |
| 484 | |
| 485 | PlaybackState state = controller.getPlaybackState(); |
| 486 | if (state == null) { |
| 487 | return false; |
| 488 | } |
| 489 | |
| 490 | return (state.getState() == PlaybackState.STATE_PLAYING); |
| 491 | } |
| 492 | |
| 493 | /** |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 494 | * Update the current device information |
| 495 | * @param device device information to display |
| 496 | */ |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 497 | private void updateDevice(MediaDevice device) { |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 498 | if (mSeamless == null) { |
| 499 | return; |
| 500 | } |
Beth Thibodeau | a51c314 | 2020-03-17 17:27:04 -0400 | [diff] [blame] | 501 | mForegroundExecutor.execute(() -> { |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 502 | updateChipInternal(device); |
| 503 | }); |
| 504 | } |
| 505 | |
| 506 | private void updateChipInternal(MediaDevice device) { |
| 507 | ColorStateList fgTintList = ColorStateList.valueOf(mForegroundColor); |
| 508 | |
| 509 | // Update the outline color |
| 510 | LinearLayout viewLayout = (LinearLayout) mSeamless; |
| 511 | RippleDrawable bkgDrawable = (RippleDrawable) viewLayout.getBackground(); |
| 512 | GradientDrawable rect = (GradientDrawable) bkgDrawable.getDrawable(0); |
| 513 | rect.setStroke(2, mForegroundColor); |
| 514 | rect.setColor(mBackgroundColor); |
| 515 | |
| 516 | ImageView iconView = mSeamless.findViewById(R.id.media_seamless_image); |
| 517 | TextView deviceName = mSeamless.findViewById(R.id.media_seamless_text); |
| 518 | deviceName.setTextColor(fgTintList); |
| 519 | |
Robert Snoeberger | c981dc9 | 2020-04-27 15:00:50 -0400 | [diff] [blame] | 520 | if (mIsRemotePlayback) { |
| 521 | mSeamless.setEnabled(false); |
| 522 | mSeamless.setAlpha(0.38f); |
| 523 | iconView.setImageResource(R.drawable.ic_hardware_speaker); |
| 524 | iconView.setVisibility(View.VISIBLE); |
| 525 | iconView.setImageTintList(fgTintList); |
| 526 | deviceName.setText(R.string.media_seamless_remote_device); |
| 527 | } else if (device != null) { |
| 528 | mSeamless.setEnabled(true); |
| 529 | mSeamless.setAlpha(1f); |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 530 | Drawable icon = device.getIcon(); |
| 531 | iconView.setVisibility(View.VISIBLE); |
| 532 | iconView.setImageTintList(fgTintList); |
| 533 | |
| 534 | if (icon instanceof AdaptiveIcon) { |
| 535 | AdaptiveIcon aIcon = (AdaptiveIcon) icon; |
| 536 | aIcon.setBackgroundColor(mBackgroundColor); |
| 537 | iconView.setImageDrawable(aIcon); |
| 538 | } else { |
| 539 | iconView.setImageDrawable(icon); |
| 540 | } |
| 541 | deviceName.setText(device.getName()); |
| 542 | } else { |
| 543 | // Reset to default |
Robert Snoeberger | 4429917 | 2020-04-24 22:22:21 -0400 | [diff] [blame] | 544 | Log.d(TAG, "device is null. Not binding output chip."); |
Robert Snoeberger | c981dc9 | 2020-04-27 15:00:50 -0400 | [diff] [blame] | 545 | mSeamless.setEnabled(true); |
| 546 | mSeamless.setAlpha(1f); |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 547 | iconView.setVisibility(View.GONE); |
| 548 | deviceName.setText(com.android.internal.R.string.ext_media_seamless_action); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | /** |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 553 | * Puts controls into a resumption state if possible, or calls removePlayer if no component was |
| 554 | * found that could resume playback |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 555 | */ |
| 556 | public void clearControls() { |
Robert Snoeberger | 445d441 | 2020-04-15 00:03:13 -0400 | [diff] [blame] | 557 | Log.d(TAG, "clearControls to resumption state package=" + getMediaPlayerPackage()); |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 558 | if (mServiceComponent == null) { |
| 559 | // If we don't have a way to resume, just remove the player altogether |
| 560 | Log.d(TAG, "Removing unresumable controls"); |
| 561 | removePlayer(); |
| 562 | return; |
| 563 | } |
| 564 | resetButtons(); |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * Hide the media buttons and show only a restart button |
| 569 | */ |
| 570 | protected void resetButtons() { |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 571 | // Hide all the old buttons |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 572 | |
| 573 | ConstraintSet expandedSet = mMediaNotifView.getConstraintSet(R.id.expanded); |
| 574 | ConstraintSet collapsedSet = mMediaNotifView.getConstraintSet(R.id.collapsed); |
| 575 | for (int i = 1; i < ACTION_IDS.length; i++) { |
| 576 | expandedSet.setVisibility(ACTION_IDS[i], ConstraintSet.GONE); |
| 577 | collapsedSet.setVisibility(ACTION_IDS[i], ConstraintSet.GONE); |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | // Add a restart button |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 581 | ImageButton btn = mMediaNotifView.findViewById(ACTION_IDS[0]); |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 582 | btn.setOnClickListener(v -> { |
| 583 | Log.d(TAG, "Attempting to restart session"); |
Beth Thibodeau | ee42e8c | 2020-04-30 01:09:29 -0400 | [diff] [blame] | 584 | if (mQSMediaBrowser != null) { |
| 585 | mQSMediaBrowser.disconnect(); |
| 586 | } |
| 587 | mQSMediaBrowser = new QSMediaBrowser(mContext, new QSMediaBrowser.Callback(){ |
| 588 | @Override |
| 589 | public void onConnected() { |
| 590 | Log.d(TAG, "Successfully restarted"); |
| 591 | } |
| 592 | @Override |
| 593 | public void onError() { |
| 594 | Log.e(TAG, "Error restarting"); |
| 595 | mQSMediaBrowser.disconnect(); |
| 596 | mQSMediaBrowser = null; |
| 597 | } |
| 598 | }, mServiceComponent); |
| 599 | mQSMediaBrowser.restart(); |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 600 | }); |
| 601 | btn.setImageDrawable(mContext.getResources().getDrawable(R.drawable.lb_ic_play)); |
| 602 | btn.setImageTintList(ColorStateList.valueOf(mForegroundColor)); |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 603 | expandedSet.setVisibility(ACTION_IDS[0], ConstraintSet.VISIBLE); |
| 604 | collapsedSet.setVisibility(ACTION_IDS[0], ConstraintSet.VISIBLE); |
| 605 | |
| 606 | mSeekBarViewModel.clearController(); |
| 607 | // TODO: fix guts |
| 608 | // View guts = mMediaNotifView.findViewById(R.id.media_guts); |
| 609 | View options = mMediaNotifView.findViewById(R.id.qs_media_controls_options); |
| 610 | |
| 611 | mMediaNotifView.setOnLongClickListener(v -> { |
| 612 | // Replace player view with close/cancel view |
| 613 | // guts.setVisibility(View.GONE); |
| 614 | options.setVisibility(View.VISIBLE); |
| 615 | return true; // consumed click |
| 616 | }); |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 617 | } |
| 618 | |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 619 | private void makeActive() { |
| 620 | Assert.isMainThread(); |
| 621 | if (!mIsRegistered) { |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 622 | if (mLocalMediaManager != null) { |
| 623 | mLocalMediaManager.registerCallback(mDeviceCallback); |
| 624 | mLocalMediaManager.startScan(); |
| 625 | } |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 626 | mIsRegistered = true; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 627 | } |
| 628 | } |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 629 | |
| 630 | private void makeInactive() { |
| 631 | Assert.isMainThread(); |
| 632 | if (mIsRegistered) { |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 633 | if (mLocalMediaManager != null) { |
| 634 | mLocalMediaManager.stopScan(); |
| 635 | mLocalMediaManager.unregisterCallback(mDeviceCallback); |
| 636 | } |
Robert Snoeberger | 3cc2222 | 2020-03-25 15:36:31 -0400 | [diff] [blame] | 637 | mIsRegistered = false; |
| 638 | } |
| 639 | } |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 640 | /** |
| 641 | * Verify that we can connect to the given component with a MediaBrowser, and if so, add that |
| 642 | * component to the list of resumption components |
| 643 | */ |
| 644 | private void tryUpdateResumptionList(ComponentName componentName) { |
| 645 | Log.d(TAG, "Testing if we can connect to " + componentName); |
Beth Thibodeau | ee42e8c | 2020-04-30 01:09:29 -0400 | [diff] [blame] | 646 | if (mQSMediaBrowser != null) { |
| 647 | mQSMediaBrowser.disconnect(); |
| 648 | } |
| 649 | mQSMediaBrowser = new QSMediaBrowser(mContext, |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 650 | new QSMediaBrowser.Callback() { |
| 651 | @Override |
| 652 | public void onConnected() { |
| 653 | Log.d(TAG, "yes we can resume with " + componentName); |
| 654 | mServiceComponent = componentName; |
| 655 | updateResumptionList(componentName); |
Beth Thibodeau | ee42e8c | 2020-04-30 01:09:29 -0400 | [diff] [blame] | 656 | mQSMediaBrowser.disconnect(); |
| 657 | mQSMediaBrowser = null; |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | @Override |
| 661 | public void onError() { |
| 662 | Log.d(TAG, "Cannot resume with " + componentName); |
| 663 | mServiceComponent = null; |
Beth Thibodeau | 89f5c76 | 2020-04-21 13:09:55 -0400 | [diff] [blame] | 664 | if (!hasMediaSession()) { |
| 665 | // If it's not active and we can't resume, remove |
| 666 | removePlayer(); |
| 667 | } |
Beth Thibodeau | ee42e8c | 2020-04-30 01:09:29 -0400 | [diff] [blame] | 668 | mQSMediaBrowser.disconnect(); |
| 669 | mQSMediaBrowser = null; |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 670 | } |
| 671 | }, |
| 672 | componentName); |
Beth Thibodeau | ee42e8c | 2020-04-30 01:09:29 -0400 | [diff] [blame] | 673 | mQSMediaBrowser.testConnection(); |
Beth Thibodeau | 23a33ab | 2020-04-07 20:51:57 -0400 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /** |
| 677 | * Add the component to the saved list of media browser services, checking for duplicates and |
| 678 | * removing older components that exceed the maximum limit |
| 679 | * @param componentName |
| 680 | */ |
| 681 | private synchronized void updateResumptionList(ComponentName componentName) { |
| 682 | // Add to front of saved list |
| 683 | if (mSharedPrefs == null) { |
| 684 | mSharedPrefs = mContext.getSharedPreferences(MEDIA_PREFERENCES, 0); |
| 685 | } |
| 686 | String componentString = componentName.flattenToString(); |
| 687 | String listString = mSharedPrefs.getString(MEDIA_PREFERENCE_KEY, null); |
| 688 | if (listString == null) { |
| 689 | listString = componentString; |
| 690 | } else { |
| 691 | String[] components = listString.split(QSMediaBrowser.DELIMITER); |
| 692 | StringBuilder updated = new StringBuilder(componentString); |
| 693 | int nBrowsers = 1; |
| 694 | for (int i = 0; i < components.length |
| 695 | && nBrowsers < QSMediaBrowser.MAX_RESUMPTION_CONTROLS; i++) { |
| 696 | if (componentString.equals(components[i])) { |
| 697 | continue; |
| 698 | } |
| 699 | updated.append(QSMediaBrowser.DELIMITER).append(components[i]); |
| 700 | nBrowsers++; |
| 701 | } |
| 702 | listString = updated.toString(); |
| 703 | } |
| 704 | mSharedPrefs.edit().putString(MEDIA_PREFERENCE_KEY, listString).apply(); |
| 705 | } |
| 706 | |
| 707 | /** |
| 708 | * Called when a player can't be resumed to give it an opportunity to hide or remove itself |
| 709 | */ |
| 710 | protected void removePlayer() { } |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 711 | } |