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