blob: 60c2ed2fa2be85420d5a64ded4f682cf5c7b89b6 [file] [log] [blame]
Beth Thibodeau7b6c1782020-03-05 11:43:51 -05001/*
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
17package com.android.systemui.media;
18
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050019import android.app.PendingIntent;
20import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
Beth Thibodeau23a33ab2020-04-07 20:51:57 -040023import android.content.SharedPreferences;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050024import android.content.pm.PackageManager;
25import android.content.pm.ResolveInfo;
26import android.content.res.ColorStateList;
Selim Cinekc5436712020-04-27 15:15:44 -070027import android.graphics.Bitmap;
28import android.graphics.Canvas;
29import android.graphics.Rect;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050030import android.graphics.drawable.Drawable;
31import android.graphics.drawable.GradientDrawable;
Selim Cinekc5436712020-04-27 15:15:44 -070032import android.graphics.drawable.Icon;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050033import android.graphics.drawable.RippleDrawable;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050034import android.media.session.MediaController;
Robert Snoebergerc981dc92020-04-27 15:00:50 -040035import android.media.session.MediaController.PlaybackInfo;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050036import android.media.session.MediaSession;
37import android.media.session.PlaybackState;
Beth Thibodeau23a33ab2020-04-07 20:51:57 -040038import android.service.media.MediaBrowserService;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050039import android.util.Log;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050040import android.view.LayoutInflater;
41import android.view.View;
42import android.view.ViewGroup;
43import android.widget.ImageButton;
44import android.widget.ImageView;
45import android.widget.LinearLayout;
Selim Cinek5dbef2d2020-05-07 17:44:38 -070046import android.widget.SeekBar;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050047import android.widget.TextView;
48
Robert Snoeberger9a7409b2020-04-09 18:12:27 -040049import androidx.annotation.Nullable;
Selim Cinekf418bb02020-05-04 17:16:58 -070050import androidx.annotation.UiThread;
Selim Cinekf0f74952020-04-21 11:45:16 -070051import androidx.constraintlayout.motion.widget.Key;
52import androidx.constraintlayout.motion.widget.KeyAttributes;
53import androidx.constraintlayout.motion.widget.KeyFrames;
Selim Cinekd8357922020-04-10 15:06:53 -070054import androidx.constraintlayout.motion.widget.MotionLayout;
Selim Cinek5dbef2d2020-05-07 17:44:38 -070055import androidx.constraintlayout.widget.ConstraintSet;
Selim Cinekc5436712020-04-27 15:15:44 -070056import androidx.core.graphics.drawable.RoundedBitmapDrawable;
57import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050058
Selim Cinekc5436712020-04-27 15:15:44 -070059import com.android.settingslib.Utils;
Robert Snoeberger9a7409b2020-04-09 18:12:27 -040060import com.android.settingslib.media.LocalMediaManager;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050061import com.android.settingslib.media.MediaDevice;
62import com.android.settingslib.media.MediaOutputSliceConstants;
63import com.android.settingslib.widget.AdaptiveIcon;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050064import com.android.systemui.R;
65import com.android.systemui.plugins.ActivityStarter;
Beth Thibodeau23a33ab2020-04-07 20:51:57 -040066import com.android.systemui.qs.QSMediaBrowser;
Robert Snoeberger3cc22222020-03-25 15:36:31 -040067import com.android.systemui.util.Assert;
Selim Cinek5dbef2d2020-05-07 17:44:38 -070068import com.android.systemui.util.concurrency.DelayableExecutor;
69
70import org.jetbrains.annotations.NotNull;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050071
Selim Cinekf0f74952020-04-21 11:45:16 -070072import java.util.ArrayList;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050073import java.util.List;
74import java.util.concurrent.Executor;
75
76/**
Selim Cinekd8357922020-04-10 15:06:53 -070077 * A view controller used for Media Playback.
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050078 */
Robert Snoeberger3cc22222020-03-25 15:36:31 -040079public class MediaControlPanel {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050080 private static final String TAG = "MediaControlPanel";
Robert Snoeberger9a7409b2020-04-09 18:12:27 -040081 @Nullable private final LocalMediaManager mLocalMediaManager;
Selim Cinek5dbef2d2020-05-07 17:44:38 -070082
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 Thibodeaua51c3142020-03-17 17:27:04 -040094 private final Executor mForegroundExecutor;
Beth Thibodeau23a33ab2020-04-07 20:51:57 -040095 protected final Executor mBackgroundExecutor;
Beth Thibodeaue561c002020-04-23 17:33:00 -040096 private final ActivityStarter mActivityStarter;
Selim Cinek3df592e2020-04-28 13:51:43 -070097 private final LayoutAnimationHelper mLayoutAnimationHelper;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050098
99 private Context mContext;
Selim Cinekf0f74952020-04-21 11:45:16 -0700100 private MotionLayout mMediaNotifView;
101 private final View mBackground;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500102 private View mSeamless;
103 private MediaSession.Token mToken;
104 private MediaController mController;
105 private int mForegroundColor;
106 private int mBackgroundColor;
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400107 private MediaDevice mDevice;
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400108 protected ComponentName mServiceComponent;
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400109 private boolean mIsRegistered = false;
Selim Cinekf0f74952020-04-21 11:45:16 -0700110 private final List<KeyFrames> mKeyFrames;
Beth Thibodeaua3d90982020-04-13 23:42:48 -0400111 private String mKey;
Selim Cinekc5436712020-04-27 15:15:44 -0700112 private int mAlbumArtSize;
113 private int mAlbumArtRadius;
Selim Cinek3df592e2020-04-28 13:51:43 -0700114 private int mViewWidth;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500115
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400116 public static final String MEDIA_PREFERENCES = "media_control_prefs";
117 public static final String MEDIA_PREFERENCE_KEY = "browser_components";
118 private SharedPreferences mSharedPrefs;
119 private boolean mCheckedForResumption = false;
Robert Snoebergerc981dc92020-04-27 15:00:50 -0400120 private boolean mIsRemotePlayback;
Beth Thibodeauee42e8c2020-04-30 01:09:29 -0400121 private QSMediaBrowser mQSMediaBrowser;
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400122
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400123 private final MediaController.Callback mSessionCallback = new MediaController.Callback() {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500124 @Override
125 public void onSessionDestroyed() {
126 Log.d(TAG, "session destroyed");
127 mController.unregisterCallback(mSessionCallback);
128 clearControls();
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400129 makeInactive();
130 }
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400131 @Override
Robert Snoeberger445d4412020-04-15 00:03:13 -0400132 public void onPlaybackStateChanged(PlaybackState state) {
133 final int s = state != null ? state.getState() : PlaybackState.STATE_NONE;
Beth Thibodeaud664de22020-04-28 16:29:36 -0400134 if (s == PlaybackState.STATE_NONE) {
Robert Snoeberger445d4412020-04-15 00:03:13 -0400135 Log.d(TAG, "playback state change will trigger resumption, state=" + state);
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400136 clearControls();
137 makeInactive();
138 }
139 }
140 };
141
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400142 private final LocalMediaManager.DeviceCallback mDeviceCallback =
143 new LocalMediaManager.DeviceCallback() {
144 @Override
145 public void onDeviceListUpdate(List<MediaDevice> devices) {
146 if (mLocalMediaManager == null) {
147 return;
148 }
149 MediaDevice currentDevice = mLocalMediaManager.getCurrentConnectedDevice();
150 // Check because this can be called several times while changing devices
151 if (mDevice == null || !mDevice.equals(currentDevice)) {
152 mDevice = currentDevice;
153 updateDevice(mDevice);
154 }
155 }
156
157 @Override
158 public void onSelectedDeviceStateChanged(MediaDevice device, int state) {
159 if (mDevice == null || !mDevice.equals(device)) {
160 mDevice = device;
161 updateDevice(mDevice);
162 }
163 }
164 };
165
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500166 /**
167 * Initialize a new control panel
168 * @param context
169 * @param parent
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400170 * @param routeManager Manager used to listen for device change events.
Beth Thibodeaua51c3142020-03-17 17:27:04 -0400171 * @param foregroundExecutor foreground executor
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500172 * @param backgroundExecutor background executor, used for processing artwork
Beth Thibodeaue561c002020-04-23 17:33:00 -0400173 * @param activityStarter activity starter
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500174 */
Robert Snoeberger445d4412020-04-15 00:03:13 -0400175 public MediaControlPanel(Context context, ViewGroup parent,
Selim Cinek5dbef2d2020-05-07 17:44:38 -0700176 @Nullable LocalMediaManager routeManager, Executor foregroundExecutor,
177 DelayableExecutor backgroundExecutor, ActivityStarter activityStarter) {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500178 mContext = context;
179 LayoutInflater inflater = LayoutInflater.from(mContext);
Selim Cinek5dbef2d2020-05-07 17:44:38 -0700180 mMediaNotifView = (MotionLayout) inflater.inflate(R.layout.qs_media_panel, parent, false);
Selim Cinekf0f74952020-04-21 11:45:16 -0700181 mBackground = mMediaNotifView.findViewById(R.id.media_background);
Selim Cinek3df592e2020-04-28 13:51:43 -0700182 mLayoutAnimationHelper = new LayoutAnimationHelper(mMediaNotifView);
Selim Cinek8081f092020-05-01 21:11:13 -0700183 GoneChildrenHideHelper.clipGoneChildrenOnLayout(mMediaNotifView);
Selim Cinekf0f74952020-04-21 11:45:16 -0700184 mKeyFrames = mMediaNotifView.getDefinedTransitions().get(0).getKeyFrameList();
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400185 mLocalMediaManager = routeManager;
Beth Thibodeaua51c3142020-03-17 17:27:04 -0400186 mForegroundExecutor = foregroundExecutor;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500187 mBackgroundExecutor = backgroundExecutor;
Beth Thibodeaue561c002020-04-23 17:33:00 -0400188 mActivityStarter = activityStarter;
Selim Cinek5dbef2d2020-05-07 17:44:38 -0700189 mSeekBarViewModel = new SeekBarViewModel(backgroundExecutor);
190 mSeekBarObserver = new SeekBarObserver(getView());
Selim Cinek098baf42020-04-27 19:02:06 -0700191 mSeekBarViewModel.getProgress().observeForever(mSeekBarObserver);
Selim Cinek5dbef2d2020-05-07 17:44:38 -0700192 SeekBar bar = getView().findViewById(R.id.media_progress_bar);
193 bar.setOnSeekBarChangeListener(mSeekBarViewModel.getSeekBarListener());
194 bar.setOnTouchListener(mSeekBarViewModel.getSeekBarTouchListener());
Selim Cinekc5436712020-04-27 15:15:44 -0700195 loadDimens();
196 }
197
Selim Cinek098baf42020-04-27 19:02:06 -0700198 public void onDestroy() {
199 mSeekBarViewModel.getProgress().removeObserver(mSeekBarObserver);
200 makeInactive();
201 }
202
Selim Cinekc5436712020-04-27 15:15:44 -0700203 private void loadDimens() {
204 mAlbumArtRadius = mContext.getResources().getDimensionPixelSize(
205 Utils.getThemeAttr(mContext, android.R.attr.dialogCornerRadius));
206 mAlbumArtSize = mContext.getResources().getDimensionPixelSize(R.dimen.qs_media_album_size);
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500207 }
208
209 /**
210 * Get the view used to display media controls
211 * @return the view
212 */
Selim Cinek5dbef2d2020-05-07 17:44:38 -0700213 public MotionLayout getView() {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500214 return mMediaNotifView;
215 }
216
217 /**
Selim Cinek5dbef2d2020-05-07 17:44:38 -0700218 * Sets the listening state of the player.
219 *
220 * Should be set to true when the QS panel is open. Otherwise, false. This is a signal to avoid
221 * unnecessary work when the QS panel is closed.
222 *
223 * @param listening True when player should be active. Otherwise, false.
224 */
225 public void setListening(boolean listening) {
226 mSeekBarViewModel.setListening(listening);
227 }
228
229 /**
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500230 * Get the context
231 * @return context
232 */
233 public Context getContext() {
234 return mContext;
235 }
236
237 /**
Selim Cinek5dbef2d2020-05-07 17:44:38 -0700238 * Bind this view based on the data given
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500239 */
Selim Cinek5dbef2d2020-05-07 17:44:38 -0700240 public void bind(@NotNull MediaData data) {
Selim Cinekf0f74952020-04-21 11:45:16 -0700241 MediaSession.Token token = data.getToken();
Selim Cinek5dbef2d2020-05-07 17:44:38 -0700242 mForegroundColor = data.getForegroundColor();
243 mBackgroundColor = data.getBackgroundColor();
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400244 if (mToken == null || !mToken.equals(token)) {
Beth Thibodeauee42e8c2020-04-30 01:09:29 -0400245 if (mQSMediaBrowser != null) {
246 Log.d(TAG, "Disconnecting old media browser");
247 mQSMediaBrowser.disconnect();
248 mQSMediaBrowser = null;
249 }
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400250 mToken = token;
251 mServiceComponent = null;
252 mCheckedForResumption = false;
253 }
254
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500255 mController = new MediaController(mContext, mToken);
256
Selim Cinek8081f092020-05-01 21:11:13 -0700257 ConstraintSet expandedSet = mMediaNotifView.getConstraintSet(R.id.expanded);
258 ConstraintSet collapsedSet = mMediaNotifView.getConstraintSet(R.id.collapsed);
259
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400260 // Try to find a browser service component for this app
261 // TODO also check for a media button receiver intended for restarting (b/154127084)
262 // Only check if we haven't tried yet or the session token changed
Selim Cinekf418bb02020-05-04 17:16:58 -0700263 final String pkgName = data.getPackageName();
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400264 if (mServiceComponent == null && !mCheckedForResumption) {
265 Log.d(TAG, "Checking for service component");
266 PackageManager pm = mContext.getPackageManager();
267 Intent resumeIntent = new Intent(MediaBrowserService.SERVICE_INTERFACE);
268 List<ResolveInfo> resumeInfo = pm.queryIntentServices(resumeIntent, 0);
Selim Cinek5dbef2d2020-05-07 17:44:38 -0700269 // TODO: look into this resumption
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400270 if (resumeInfo != null) {
271 for (ResolveInfo inf : resumeInfo) {
272 if (inf.serviceInfo.packageName.equals(mController.getPackageName())) {
273 mBackgroundExecutor.execute(() ->
274 tryUpdateResumptionList(inf.getComponentInfo().getComponentName()));
275 break;
276 }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500277 }
278 }
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400279 mCheckedForResumption = true;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500280 }
281
282 mController.registerCallback(mSessionCallback);
283
Selim Cinekf0f74952020-04-21 11:45:16 -0700284 mMediaNotifView.requireViewById(R.id.media_background).setBackgroundTintList(
285 ColorStateList.valueOf(mBackgroundColor));
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500286
287 // Click action
Selim Cinek5dbef2d2020-05-07 17:44:38 -0700288 PendingIntent clickIntent = data.getClickIntent();
289 if (clickIntent != null) {
Beth Thibodeaua51c3142020-03-17 17:27:04 -0400290 mMediaNotifView.setOnClickListener(v -> {
Selim Cinek5dbef2d2020-05-07 17:44:38 -0700291 mActivityStarter.postStartActivityDismissingKeyguard(clickIntent);
Beth Thibodeaua51c3142020-03-17 17:27:04 -0400292 });
293 }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500294
Selim Cinekf0f74952020-04-21 11:45:16 -0700295 ImageView albumView = mMediaNotifView.findViewById(R.id.album_art);
Selim Cinekc5436712020-04-27 15:15:44 -0700296 // TODO: migrate this to a view with rounded corners instead of baking the rounding
297 // into the bitmap
298 Drawable artwork = createRoundedBitmap(data.getArtwork());
299 albumView.setImageDrawable(artwork);
Selim Cinekf0f74952020-04-21 11:45:16 -0700300
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500301 // App icon
Selim Cinek5dbef2d2020-05-07 17:44:38 -0700302 ImageView appIcon = mMediaNotifView.requireViewById(R.id.icon);
Selim Cinekf418bb02020-05-04 17:16:58 -0700303 Drawable iconDrawable = data.getAppIcon().mutate();
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500304 iconDrawable.setTint(mForegroundColor);
305 appIcon.setImageDrawable(iconDrawable);
306
Selim Cinekf0f74952020-04-21 11:45:16 -0700307 // Song name
Selim Cinek5dbef2d2020-05-07 17:44:38 -0700308 TextView titleText = mMediaNotifView.requireViewById(R.id.header_title);
309 titleText.setText(data.getSong());
Selim Cinekf0f74952020-04-21 11:45:16 -0700310 titleText.setTextColor(data.getForegroundColor());
311
312 // App title
Selim Cinek5dbef2d2020-05-07 17:44:38 -0700313 TextView appName = mMediaNotifView.requireViewById(R.id.app_name);
314 appName.setText(data.getApp());
315 appName.setTextColor(mForegroundColor);
Selim Cinekf0f74952020-04-21 11:45:16 -0700316
317 // Artist name
Selim Cinek5dbef2d2020-05-07 17:44:38 -0700318 TextView artistText = mMediaNotifView.requireViewById(R.id.header_artist);
319 artistText.setText(data.getArtist());
320 artistText.setTextColor(mForegroundColor);
Selim Cinekf0f74952020-04-21 11:45:16 -0700321
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500322 // Transfer chip
323 mSeamless = mMediaNotifView.findViewById(R.id.media_seamless);
Robert Snoeberger44299172020-04-24 22:22:21 -0400324 if (mSeamless != null) {
325 if (mLocalMediaManager != null) {
326 mSeamless.setVisibility(View.VISIBLE);
Selim Cinek8081f092020-05-01 21:11:13 -0700327 setVisibleAndAlpha(collapsedSet, R.id.media_seamless, true /*visible */);
328 setVisibleAndAlpha(expandedSet, R.id.media_seamless, true /*visible */);
Robert Snoeberger44299172020-04-24 22:22:21 -0400329 updateDevice(mLocalMediaManager.getCurrentConnectedDevice());
330 mSeamless.setOnClickListener(v -> {
331 final Intent intent = new Intent()
332 .setAction(MediaOutputSliceConstants.ACTION_MEDIA_OUTPUT)
333 .putExtra(MediaOutputSliceConstants.EXTRA_PACKAGE_NAME,
334 mController.getPackageName())
335 .putExtra(MediaOutputSliceConstants.KEY_MEDIA_SESSION_TOKEN, mToken);
336 mActivityStarter.startActivity(intent, false, true /* dismissShade */,
337 Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
338 });
339 } else {
340 Log.d(TAG, "LocalMediaManager is null. Not binding output chip for pkg=" + pkgName);
341 }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500342 }
Robert Snoebergerc981dc92020-04-27 15:00:50 -0400343 PlaybackInfo playbackInfo = mController.getPlaybackInfo();
344 if (playbackInfo != null) {
345 mIsRemotePlayback = playbackInfo.getPlaybackType() == PlaybackInfo.PLAYBACK_TYPE_REMOTE;
346 } else {
347 Log.d(TAG, "PlaybackInfo was null. Defaulting to local playback.");
348 mIsRemotePlayback = false;
349 }
Selim Cinek5dbef2d2020-05-07 17:44:38 -0700350 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 Cinekf0f74952020-04-21 11:45:16 -0700355 int actionId = ACTION_IDS[i];
356 final ImageButton button = mMediaNotifView.findViewById(actionId);
Selim Cinek5dbef2d2020-05-07 17:44:38 -0700357 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 Cinekf0f74952020-04-21 11:45:16 -0700363 if (mBackground.getBackground() instanceof IlluminationDrawable) {
364 ((IlluminationDrawable) mBackground.getBackground())
Selim Cinek5dbef2d2020-05-07 17:44:38 -0700365 .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 Cinekf0f74952020-04-21 11:45:16 -0700378 updateKeyFrameVisibility(actionId, visibleInCompat);
Selim Cinek8081f092020-05-01 21:11:13 -0700379 setVisibleAndAlpha(collapsedSet, actionId, visibleInCompat);
380 setVisibleAndAlpha(expandedSet, actionId, true /*visible */);
Selim Cinek5dbef2d2020-05-07 17:44:38 -0700381 }
382
383 // Hide any unused buttons
384 for (; i < ACTION_IDS.length; i++) {
Selim Cinek8081f092020-05-01 21:11:13 -0700385 setVisibleAndAlpha(expandedSet, ACTION_IDS[i], false /*visible */);
386 setVisibleAndAlpha(collapsedSet, ACTION_IDS[i], false /*visible */);
Selim Cinek5dbef2d2020-05-07 17:44:38 -0700387 }
388
389 // Seek Bar
Selim Cinekf418bb02020-05-04 17:16:58 -0700390 final MediaController controller = getController();
Selim Cinek5dbef2d2020-05-07 17:44:38 -0700391 mBackgroundExecutor.execute(
392 () -> mSeekBarViewModel.updateController(controller, data.getForegroundColor()));
393
394 // Set up long press menu
395 // TODO: b/156036025 bring back media guts
396
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400397 makeActive();
Selim Cinekf418bb02020-05-04 17:16:58 -0700398
399 // Update both constraint sets to regenerate the animation.
Selim Cinekb28ec0a2020-05-01 15:07:42 -0700400 mMediaNotifView.updateState(R.id.collapsed, collapsedSet);
401 mMediaNotifView.updateState(R.id.expanded, expandedSet);
Selim Cinekf0f74952020-04-21 11:45:16 -0700402 }
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400403
Selim Cinekf418bb02020-05-04 17:16:58 -0700404 @UiThread
Selim Cinekc5436712020-04-27 15:15:44 -0700405 private Drawable createRoundedBitmap(Icon icon) {
406 if (icon == null) {
407 return null;
408 }
409 // Let's scale down the View, such that the content always nicely fills the view.
410 // ThumbnailUtils actually scales it down such that it may not be filled for odd aspect
411 // ratios
412 Drawable drawable = icon.loadDrawable(mContext);
413 float aspectRatio = drawable.getIntrinsicHeight() / (float) drawable.getIntrinsicWidth();
414 Rect bounds;
415 if (aspectRatio > 1.0f) {
416 bounds = new Rect(0, 0, mAlbumArtSize, (int) (mAlbumArtSize * aspectRatio));
417 } else {
418 bounds = new Rect(0, 0, (int) (mAlbumArtSize / aspectRatio), mAlbumArtSize);
419 }
420 if (bounds.width() > mAlbumArtSize || bounds.height() > mAlbumArtSize) {
421 float offsetX = (bounds.width() - mAlbumArtSize) / 2.0f;
422 float offsetY = (bounds.height() - mAlbumArtSize) / 2.0f;
423 bounds.offset((int) -offsetX,(int) -offsetY);
424 }
425 drawable.setBounds(bounds);
426 Bitmap scaled = Bitmap.createBitmap(mAlbumArtSize, mAlbumArtSize,
427 Bitmap.Config.ARGB_8888);
428 Canvas canvas = new Canvas(scaled);
429 drawable.draw(canvas);
430 RoundedBitmapDrawable artwork = RoundedBitmapDrawableFactory.create(
431 mContext.getResources(), scaled);
432 artwork.setCornerRadius(mAlbumArtRadius);
433 return artwork;
434 }
435
Selim Cinekf0f74952020-04-21 11:45:16 -0700436 /**
437 * Updates the keyframe visibility such that only views that are not visible actually go
438 * through a transition and fade in.
439 *
440 * @param actionId the id to change
441 * @param visible is the view visible
442 */
443 private void updateKeyFrameVisibility(int actionId, boolean visible) {
444 for (int i = 0; i < mKeyFrames.size(); i++) {
445 KeyFrames keyframe = mKeyFrames.get(i);
446 ArrayList<Key> viewKeyFrames = keyframe.getKeyFramesForView(actionId);
447 for (int j = 0; j < viewKeyFrames.size(); j++) {
448 Key key = viewKeyFrames.get(j);
449 if (key instanceof KeyAttributes) {
450 KeyAttributes attributes = (KeyAttributes) key;
451 attributes.setValue("alpha", visible ? 1.0f : 0.0f);
452 }
Beth Thibodeau4b5ec832020-04-30 19:43:37 -0400453 }
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400454 }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500455 }
456
457 /**
458 * Return the token for the current media session
459 * @return the token
460 */
461 public MediaSession.Token getMediaSessionToken() {
462 return mToken;
463 }
464
465 /**
466 * Get the current media controller
467 * @return the controller
468 */
469 public MediaController getController() {
470 return mController;
471 }
472
473 /**
474 * Get the name of the package associated with the current media controller
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400475 * @return the package name, or null if no controller
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500476 */
477 public String getMediaPlayerPackage() {
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400478 if (mController == null) {
479 return null;
480 }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500481 return mController.getPackageName();
482 }
483
484 /**
Beth Thibodeaua3d90982020-04-13 23:42:48 -0400485 * Return the original notification's key
486 * @return The notification key
487 */
488 public String getKey() {
489 return mKey;
490 }
491
492 /**
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500493 * Check whether this player has an attached media session.
494 * @return whether there is a controller with a current media session.
495 */
496 public boolean hasMediaSession() {
497 return mController != null && mController.getPlaybackState() != null;
498 }
499
500 /**
501 * Check whether the media controlled by this player is currently playing
502 * @return whether it is playing, or false if no controller information
503 */
504 public boolean isPlaying() {
505 return isPlaying(mController);
506 }
507
508 /**
509 * Check whether the given controller is currently playing
510 * @param controller media controller to check
511 * @return whether it is playing, or false if no controller information
512 */
513 protected boolean isPlaying(MediaController controller) {
514 if (controller == null) {
515 return false;
516 }
517
518 PlaybackState state = controller.getPlaybackState();
519 if (state == null) {
520 return false;
521 }
522
523 return (state.getState() == PlaybackState.STATE_PLAYING);
524 }
525
526 /**
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500527 * Update the current device information
528 * @param device device information to display
529 */
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400530 private void updateDevice(MediaDevice device) {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500531 if (mSeamless == null) {
532 return;
533 }
Beth Thibodeaua51c3142020-03-17 17:27:04 -0400534 mForegroundExecutor.execute(() -> {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500535 updateChipInternal(device);
536 });
537 }
538
539 private void updateChipInternal(MediaDevice device) {
540 ColorStateList fgTintList = ColorStateList.valueOf(mForegroundColor);
541
542 // Update the outline color
543 LinearLayout viewLayout = (LinearLayout) mSeamless;
544 RippleDrawable bkgDrawable = (RippleDrawable) viewLayout.getBackground();
545 GradientDrawable rect = (GradientDrawable) bkgDrawable.getDrawable(0);
546 rect.setStroke(2, mForegroundColor);
547 rect.setColor(mBackgroundColor);
548
549 ImageView iconView = mSeamless.findViewById(R.id.media_seamless_image);
550 TextView deviceName = mSeamless.findViewById(R.id.media_seamless_text);
551 deviceName.setTextColor(fgTintList);
552
Robert Snoebergerc981dc92020-04-27 15:00:50 -0400553 if (mIsRemotePlayback) {
554 mSeamless.setEnabled(false);
555 mSeamless.setAlpha(0.38f);
556 iconView.setImageResource(R.drawable.ic_hardware_speaker);
557 iconView.setVisibility(View.VISIBLE);
558 iconView.setImageTintList(fgTintList);
559 deviceName.setText(R.string.media_seamless_remote_device);
560 } else if (device != null) {
561 mSeamless.setEnabled(true);
562 mSeamless.setAlpha(1f);
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500563 Drawable icon = device.getIcon();
564 iconView.setVisibility(View.VISIBLE);
565 iconView.setImageTintList(fgTintList);
566
567 if (icon instanceof AdaptiveIcon) {
568 AdaptiveIcon aIcon = (AdaptiveIcon) icon;
569 aIcon.setBackgroundColor(mBackgroundColor);
570 iconView.setImageDrawable(aIcon);
571 } else {
572 iconView.setImageDrawable(icon);
573 }
574 deviceName.setText(device.getName());
575 } else {
576 // Reset to default
Robert Snoeberger44299172020-04-24 22:22:21 -0400577 Log.d(TAG, "device is null. Not binding output chip.");
Robert Snoebergerc981dc92020-04-27 15:00:50 -0400578 mSeamless.setEnabled(true);
579 mSeamless.setAlpha(1f);
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500580 iconView.setVisibility(View.GONE);
581 deviceName.setText(com.android.internal.R.string.ext_media_seamless_action);
582 }
583 }
584
585 /**
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400586 * Puts controls into a resumption state if possible, or calls removePlayer if no component was
587 * found that could resume playback
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500588 */
589 public void clearControls() {
Robert Snoeberger445d4412020-04-15 00:03:13 -0400590 Log.d(TAG, "clearControls to resumption state package=" + getMediaPlayerPackage());
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400591 if (mServiceComponent == null) {
592 // If we don't have a way to resume, just remove the player altogether
593 Log.d(TAG, "Removing unresumable controls");
594 removePlayer();
595 return;
596 }
597 resetButtons();
598 }
599
600 /**
601 * Hide the media buttons and show only a restart button
602 */
603 protected void resetButtons() {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500604 // Hide all the old buttons
Selim Cinek5dbef2d2020-05-07 17:44:38 -0700605
606 ConstraintSet expandedSet = mMediaNotifView.getConstraintSet(R.id.expanded);
607 ConstraintSet collapsedSet = mMediaNotifView.getConstraintSet(R.id.collapsed);
608 for (int i = 1; i < ACTION_IDS.length; i++) {
Selim Cinek8081f092020-05-01 21:11:13 -0700609 setVisibleAndAlpha(expandedSet, ACTION_IDS[i], false /*visible */);
610 setVisibleAndAlpha(collapsedSet, ACTION_IDS[i], false /*visible */);
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500611 }
612
613 // Add a restart button
Selim Cinek5dbef2d2020-05-07 17:44:38 -0700614 ImageButton btn = mMediaNotifView.findViewById(ACTION_IDS[0]);
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500615 btn.setOnClickListener(v -> {
616 Log.d(TAG, "Attempting to restart session");
Beth Thibodeauee42e8c2020-04-30 01:09:29 -0400617 if (mQSMediaBrowser != null) {
618 mQSMediaBrowser.disconnect();
619 }
620 mQSMediaBrowser = new QSMediaBrowser(mContext, new QSMediaBrowser.Callback(){
621 @Override
622 public void onConnected() {
623 Log.d(TAG, "Successfully restarted");
624 }
625 @Override
626 public void onError() {
627 Log.e(TAG, "Error restarting");
628 mQSMediaBrowser.disconnect();
629 mQSMediaBrowser = null;
630 }
631 }, mServiceComponent);
632 mQSMediaBrowser.restart();
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500633 });
634 btn.setImageDrawable(mContext.getResources().getDrawable(R.drawable.lb_ic_play));
635 btn.setImageTintList(ColorStateList.valueOf(mForegroundColor));
Selim Cinek8081f092020-05-01 21:11:13 -0700636 setVisibleAndAlpha(expandedSet, ACTION_IDS[0], true /*visible */);
637 setVisibleAndAlpha(collapsedSet, ACTION_IDS[0], true /*visible */);
Selim Cinek5dbef2d2020-05-07 17:44:38 -0700638
639 mSeekBarViewModel.clearController();
640 // TODO: fix guts
641 // View guts = mMediaNotifView.findViewById(R.id.media_guts);
642 View options = mMediaNotifView.findViewById(R.id.qs_media_controls_options);
643
644 mMediaNotifView.setOnLongClickListener(v -> {
645 // Replace player view with close/cancel view
646// guts.setVisibility(View.GONE);
647 options.setVisibility(View.VISIBLE);
648 return true; // consumed click
649 });
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500650 }
651
Selim Cinek8081f092020-05-01 21:11:13 -0700652 private void setVisibleAndAlpha(ConstraintSet set, int actionId, boolean visible) {
653 set.setVisibility(actionId, visible? ConstraintSet.VISIBLE : ConstraintSet.GONE);
654 set.setAlpha(actionId, visible ? 1.0f : 0.0f);
655 }
656
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400657 private void makeActive() {
658 Assert.isMainThread();
659 if (!mIsRegistered) {
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400660 if (mLocalMediaManager != null) {
661 mLocalMediaManager.registerCallback(mDeviceCallback);
662 mLocalMediaManager.startScan();
663 }
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400664 mIsRegistered = true;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500665 }
666 }
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400667
668 private void makeInactive() {
669 Assert.isMainThread();
670 if (mIsRegistered) {
Robert Snoeberger9a7409b2020-04-09 18:12:27 -0400671 if (mLocalMediaManager != null) {
672 mLocalMediaManager.stopScan();
673 mLocalMediaManager.unregisterCallback(mDeviceCallback);
674 }
Robert Snoeberger3cc22222020-03-25 15:36:31 -0400675 mIsRegistered = false;
676 }
677 }
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400678 /**
679 * Verify that we can connect to the given component with a MediaBrowser, and if so, add that
680 * component to the list of resumption components
681 */
682 private void tryUpdateResumptionList(ComponentName componentName) {
683 Log.d(TAG, "Testing if we can connect to " + componentName);
Beth Thibodeauee42e8c2020-04-30 01:09:29 -0400684 if (mQSMediaBrowser != null) {
685 mQSMediaBrowser.disconnect();
686 }
687 mQSMediaBrowser = new QSMediaBrowser(mContext,
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400688 new QSMediaBrowser.Callback() {
689 @Override
690 public void onConnected() {
691 Log.d(TAG, "yes we can resume with " + componentName);
692 mServiceComponent = componentName;
693 updateResumptionList(componentName);
Beth Thibodeauee42e8c2020-04-30 01:09:29 -0400694 mQSMediaBrowser.disconnect();
695 mQSMediaBrowser = null;
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400696 }
697
698 @Override
699 public void onError() {
700 Log.d(TAG, "Cannot resume with " + componentName);
701 mServiceComponent = null;
Beth Thibodeau89f5c762020-04-21 13:09:55 -0400702 if (!hasMediaSession()) {
703 // If it's not active and we can't resume, remove
704 removePlayer();
705 }
Beth Thibodeauee42e8c2020-04-30 01:09:29 -0400706 mQSMediaBrowser.disconnect();
707 mQSMediaBrowser = null;
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400708 }
709 },
710 componentName);
Beth Thibodeauee42e8c2020-04-30 01:09:29 -0400711 mQSMediaBrowser.testConnection();
Beth Thibodeau23a33ab2020-04-07 20:51:57 -0400712 }
713
714 /**
715 * Add the component to the saved list of media browser services, checking for duplicates and
716 * removing older components that exceed the maximum limit
717 * @param componentName
718 */
719 private synchronized void updateResumptionList(ComponentName componentName) {
720 // Add to front of saved list
721 if (mSharedPrefs == null) {
722 mSharedPrefs = mContext.getSharedPreferences(MEDIA_PREFERENCES, 0);
723 }
724 String componentString = componentName.flattenToString();
725 String listString = mSharedPrefs.getString(MEDIA_PREFERENCE_KEY, null);
726 if (listString == null) {
727 listString = componentString;
728 } else {
729 String[] components = listString.split(QSMediaBrowser.DELIMITER);
730 StringBuilder updated = new StringBuilder(componentString);
731 int nBrowsers = 1;
732 for (int i = 0; i < components.length
733 && nBrowsers < QSMediaBrowser.MAX_RESUMPTION_CONTROLS; i++) {
734 if (componentString.equals(components[i])) {
735 continue;
736 }
737 updated.append(QSMediaBrowser.DELIMITER).append(components[i]);
738 nBrowsers++;
739 }
740 listString = updated.toString();
741 }
742 mSharedPrefs.edit().putString(MEDIA_PREFERENCE_KEY, listString).apply();
743 }
744
745 /**
746 * Called when a player can't be resumed to give it an opportunity to hide or remove itself
747 */
748 protected void removePlayer() { }
Selim Cinek3df592e2020-04-28 13:51:43 -0700749
Selim Cinekf418bb02020-05-04 17:16:58 -0700750 public void measure(@Nullable MediaMeasurementInput input) {
751 if (input != null) {
752 int width = input.getWidth();
753 setPlayerWidth(width);
754 mMediaNotifView.measure(input.getWidthMeasureSpec(), input.getHeightMeasureSpec());
Selim Cinek3df592e2020-04-28 13:51:43 -0700755 }
756 }
757
Selim Cinekb28ec0a2020-05-01 15:07:42 -0700758 public void setPlayerWidth(int width) {
Selim Cinek3df592e2020-04-28 13:51:43 -0700759 ConstraintSet expandedSet = mMediaNotifView.getConstraintSet(R.id.expanded);
760 ConstraintSet collapsedSet = mMediaNotifView.getConstraintSet(R.id.collapsed);
Selim Cinek54809622020-04-30 19:04:44 -0700761 collapsedSet.setGuidelineBegin(R.id.view_width, width);
762 expandedSet.setGuidelineBegin(R.id.view_width, width);
Selim Cinekb28ec0a2020-05-01 15:07:42 -0700763 mMediaNotifView.updateState(R.id.collapsed, collapsedSet);
764 mMediaNotifView.updateState(R.id.expanded, expandedSet);
Selim Cinek3df592e2020-04-28 13:51:43 -0700765 }
Selim Cinekf418bb02020-05-04 17:16:58 -0700766
767 public void animatePendingSizeChange(long duration, long startDelay) {
768 mLayoutAnimationHelper.animatePendingSizeChange(duration, startDelay);
769 }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500770}