blob: 0ba4cb159024bfa48deba49f08055aad84259b01 [file] [log] [blame]
Beth Thibodeau07d20c32019-10-16 13:45:56 -04001/*
2 * Copyright (C) 2019 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.qs;
18
Beth Thibodeaufd51bb22019-11-25 13:57:06 -050019import android.app.PendingIntent;
Beth Thibodeau07d20c32019-10-16 13:45:56 -040020import android.content.Context;
Beth Thibodeau07d20c32019-10-16 13:45:56 -040021import android.graphics.drawable.Drawable;
22import android.graphics.drawable.Icon;
Beth Thibodeau07d20c32019-10-16 13:45:56 -040023import android.media.session.MediaController;
24import android.media.session.MediaSession;
Beth Thibodeau07d20c32019-10-16 13:45:56 -040025import android.view.View;
26import android.view.ViewGroup;
27import android.widget.ImageButton;
Beth Thibodeau07d20c32019-10-16 13:45:56 -040028import android.widget.LinearLayout;
Beth Thibodeau07d20c32019-10-16 13:45:56 -040029
Beth Thibodeau07d20c32019-10-16 13:45:56 -040030import com.android.systemui.R;
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050031import com.android.systemui.media.MediaControlPanel;
32import com.android.systemui.statusbar.NotificationMediaManager;
Beth Thibodeau07d20c32019-10-16 13:45:56 -040033
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050034import java.util.concurrent.Executor;
Beth Thibodeaufd51bb22019-11-25 13:57:06 -050035
Beth Thibodeau07d20c32019-10-16 13:45:56 -040036/**
37 * QQS mini media player
38 */
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050039public class QuickQSMediaPlayer extends MediaControlPanel {
Beth Thibodeau07d20c32019-10-16 13:45:56 -040040
41 private static final String TAG = "QQSMediaPlayer";
42
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050043 // Button IDs for QS controls
44 private static final int[] QQS_ACTION_IDS = {R.id.action0, R.id.action1, R.id.action2};
Beth Thibodeau07d20c32019-10-16 13:45:56 -040045
46 /**
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050047 * Initialize mini media player for QQS
Beth Thibodeau07d20c32019-10-16 13:45:56 -040048 * @param context
49 * @param parent
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050050 * @param manager
Beth Thibodeaua51c3142020-03-17 17:27:04 -040051 * @param foregroundExecutor
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050052 * @param backgroundExecutor
Beth Thibodeau07d20c32019-10-16 13:45:56 -040053 */
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050054 public QuickQSMediaPlayer(Context context, ViewGroup parent, NotificationMediaManager manager,
Beth Thibodeaua51c3142020-03-17 17:27:04 -040055 Executor foregroundExecutor, Executor backgroundExecutor) {
Robert Snoeberger9a7409b2020-04-09 18:12:27 -040056 super(context, parent, manager, null, R.layout.qqs_media_panel, QQS_ACTION_IDS,
Beth Thibodeaua51c3142020-03-17 17:27:04 -040057 foregroundExecutor, backgroundExecutor);
Beth Thibodeau07d20c32019-10-16 13:45:56 -040058 }
59
60 /**
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050061 * Update media panel view for the given media session
Beth Thibodeau07d20c32019-10-16 13:45:56 -040062 * @param token token for this media session
63 * @param icon app notification icon
64 * @param iconColor foreground color (for text, icons)
65 * @param bgColor background color
66 * @param actionsContainer a LinearLayout containing the media action buttons
Beth Thibodeau69b1dfd2019-11-08 15:39:25 -050067 * @param actionsToShow indices of which actions to display in the mini player
68 * (max 3: Notification.MediaStyle.MAX_MEDIA_BUTTONS_IN_COMPACT)
Beth Thibodeau899b5952020-01-07 17:50:07 -050069 * @param contentIntent Intent to send when user taps on the view
Beth Thibodeau07d20c32019-10-16 13:45:56 -040070 */
71 public void setMediaSession(MediaSession.Token token, Icon icon, int iconColor, int bgColor,
Beth Thibodeau899b5952020-01-07 17:50:07 -050072 View actionsContainer, int[] actionsToShow, PendingIntent contentIntent) {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050073 // Only update if this is a different session and currently playing
Beth Thibodeau899b5952020-01-07 17:50:07 -050074 String oldPackage = "";
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050075 if (getController() != null) {
76 oldPackage = getController().getPackageName();
Beth Thibodeau899b5952020-01-07 17:50:07 -050077 }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050078 MediaController controller = new MediaController(getContext(), token);
79 MediaSession.Token currentToken = getMediaSessionToken();
80 boolean samePlayer = currentToken != null
81 && currentToken.equals(token)
82 && oldPackage.equals(controller.getPackageName());
83 if (getController() != null && !samePlayer && !isPlaying(controller)) {
Beth Thibodeau4e69f4c2019-10-24 14:13:49 -040084 return;
85 }
86
Robert Snoeberger9a7409b2020-04-09 18:12:27 -040087 super.setMediaSession(token, icon, iconColor, bgColor, contentIntent, null);
Beth Thibodeau899b5952020-01-07 17:50:07 -050088
Beth Thibodeau69b1dfd2019-11-08 15:39:25 -050089 LinearLayout parentActionsLayout = (LinearLayout) actionsContainer;
Beth Thibodeau69b1dfd2019-11-08 15:39:25 -050090 int i = 0;
91 if (actionsToShow != null) {
92 int maxButtons = Math.min(actionsToShow.length, parentActionsLayout.getChildCount());
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050093 maxButtons = Math.min(maxButtons, QQS_ACTION_IDS.length);
Beth Thibodeau69b1dfd2019-11-08 15:39:25 -050094 for (; i < maxButtons; i++) {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050095 ImageButton thisBtn = mMediaNotifView.findViewById(QQS_ACTION_IDS[i]);
96 int thatId = NOTIF_ACTION_IDS[actionsToShow[i]];
Beth Thibodeau69b1dfd2019-11-08 15:39:25 -050097 ImageButton thatBtn = parentActionsLayout.findViewById(thatId);
98 if (thatBtn == null || thatBtn.getDrawable() == null
99 || thatBtn.getVisibility() != View.VISIBLE) {
100 thisBtn.setVisibility(View.GONE);
101 continue;
102 }
103
104 Drawable thatIcon = thatBtn.getDrawable();
105 thisBtn.setImageDrawable(thatIcon.mutate());
106 thisBtn.setVisibility(View.VISIBLE);
107 thisBtn.setOnClickListener(v -> {
108 thatBtn.performClick();
109 });
Beth Thibodeau07d20c32019-10-16 13:45:56 -0400110 }
Beth Thibodeau69b1dfd2019-11-08 15:39:25 -0500111 }
Beth Thibodeau07d20c32019-10-16 13:45:56 -0400112
Beth Thibodeau69b1dfd2019-11-08 15:39:25 -0500113 // Hide any unused buttons
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500114 for (; i < QQS_ACTION_IDS.length; i++) {
115 ImageButton thisBtn = mMediaNotifView.findViewById(QQS_ACTION_IDS[i]);
Beth Thibodeau69b1dfd2019-11-08 15:39:25 -0500116 thisBtn.setVisibility(View.GONE);
Beth Thibodeau07d20c32019-10-16 13:45:56 -0400117 }
118 }
Beth Thibodeau07d20c32019-10-16 13:45:56 -0400119}