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