blob: 794677912626c4d74c21a03d9bac1ba0e6434c3f [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 Thibodeaua3d90982020-04-13 23:42:48 -040070 * @param key original notification's key
Beth Thibodeau07d20c32019-10-16 13:45:56 -040071 */
72 public void setMediaSession(MediaSession.Token token, Icon icon, int iconColor, int bgColor,
Beth Thibodeaua3d90982020-04-13 23:42:48 -040073 View actionsContainer, int[] actionsToShow, PendingIntent contentIntent, String key) {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050074 // Only update if this is a different session and currently playing
Beth Thibodeau899b5952020-01-07 17:50:07 -050075 String oldPackage = "";
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050076 if (getController() != null) {
77 oldPackage = getController().getPackageName();
Beth Thibodeau899b5952020-01-07 17:50:07 -050078 }
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050079 MediaController controller = new MediaController(getContext(), token);
80 MediaSession.Token currentToken = getMediaSessionToken();
81 boolean samePlayer = currentToken != null
82 && currentToken.equals(token)
83 && oldPackage.equals(controller.getPackageName());
84 if (getController() != null && !samePlayer && !isPlaying(controller)) {
Beth Thibodeau4e69f4c2019-10-24 14:13:49 -040085 return;
86 }
87
Beth Thibodeaua3d90982020-04-13 23:42:48 -040088 super.setMediaSession(token, icon, iconColor, bgColor, contentIntent, null, key);
Beth Thibodeau899b5952020-01-07 17:50:07 -050089
Beth Thibodeau69b1dfd2019-11-08 15:39:25 -050090 LinearLayout parentActionsLayout = (LinearLayout) actionsContainer;
Beth Thibodeau69b1dfd2019-11-08 15:39:25 -050091 int i = 0;
92 if (actionsToShow != null) {
93 int maxButtons = Math.min(actionsToShow.length, parentActionsLayout.getChildCount());
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050094 maxButtons = Math.min(maxButtons, QQS_ACTION_IDS.length);
Beth Thibodeau69b1dfd2019-11-08 15:39:25 -050095 for (; i < maxButtons; i++) {
Beth Thibodeau7b6c1782020-03-05 11:43:51 -050096 ImageButton thisBtn = mMediaNotifView.findViewById(QQS_ACTION_IDS[i]);
97 int thatId = NOTIF_ACTION_IDS[actionsToShow[i]];
Beth Thibodeau69b1dfd2019-11-08 15:39:25 -050098 ImageButton thatBtn = parentActionsLayout.findViewById(thatId);
99 if (thatBtn == null || thatBtn.getDrawable() == null
100 || thatBtn.getVisibility() != View.VISIBLE) {
101 thisBtn.setVisibility(View.GONE);
102 continue;
103 }
104
105 Drawable thatIcon = thatBtn.getDrawable();
106 thisBtn.setImageDrawable(thatIcon.mutate());
107 thisBtn.setVisibility(View.VISIBLE);
108 thisBtn.setOnClickListener(v -> {
109 thatBtn.performClick();
110 });
Beth Thibodeau07d20c32019-10-16 13:45:56 -0400111 }
Beth Thibodeau69b1dfd2019-11-08 15:39:25 -0500112 }
Beth Thibodeau07d20c32019-10-16 13:45:56 -0400113
Beth Thibodeau69b1dfd2019-11-08 15:39:25 -0500114 // Hide any unused buttons
Beth Thibodeau7b6c1782020-03-05 11:43:51 -0500115 for (; i < QQS_ACTION_IDS.length; i++) {
116 ImageButton thisBtn = mMediaNotifView.findViewById(QQS_ACTION_IDS[i]);
Beth Thibodeau69b1dfd2019-11-08 15:39:25 -0500117 thisBtn.setVisibility(View.GONE);
Beth Thibodeau07d20c32019-10-16 13:45:56 -0400118 }
119 }
Beth Thibodeau07d20c32019-10-16 13:45:56 -0400120}