Beth Thibodeau | 07d20c3 | 2019-10-16 13:45:56 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.qs; |
| 18 | |
Beth Thibodeau | fd51bb2 | 2019-11-25 13:57:06 -0500 | [diff] [blame] | 19 | import android.app.PendingIntent; |
Beth Thibodeau | 07d20c3 | 2019-10-16 13:45:56 -0400 | [diff] [blame] | 20 | import android.content.Context; |
Beth Thibodeau | 07d20c3 | 2019-10-16 13:45:56 -0400 | [diff] [blame] | 21 | import android.graphics.drawable.Drawable; |
| 22 | import android.graphics.drawable.Icon; |
Beth Thibodeau | 07d20c3 | 2019-10-16 13:45:56 -0400 | [diff] [blame] | 23 | import android.media.session.MediaController; |
| 24 | import android.media.session.MediaSession; |
Beth Thibodeau | 07d20c3 | 2019-10-16 13:45:56 -0400 | [diff] [blame] | 25 | import android.view.View; |
| 26 | import android.view.ViewGroup; |
| 27 | import android.widget.ImageButton; |
Beth Thibodeau | 07d20c3 | 2019-10-16 13:45:56 -0400 | [diff] [blame] | 28 | import android.widget.LinearLayout; |
Beth Thibodeau | 07d20c3 | 2019-10-16 13:45:56 -0400 | [diff] [blame] | 29 | |
Beth Thibodeau | 07d20c3 | 2019-10-16 13:45:56 -0400 | [diff] [blame] | 30 | import com.android.systemui.R; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 31 | import com.android.systemui.media.MediaControlPanel; |
| 32 | import com.android.systemui.statusbar.NotificationMediaManager; |
Beth Thibodeau | 07d20c3 | 2019-10-16 13:45:56 -0400 | [diff] [blame] | 33 | |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 34 | import java.util.concurrent.Executor; |
Beth Thibodeau | fd51bb2 | 2019-11-25 13:57:06 -0500 | [diff] [blame] | 35 | |
Beth Thibodeau | 07d20c3 | 2019-10-16 13:45:56 -0400 | [diff] [blame] | 36 | /** |
| 37 | * QQS mini media player |
| 38 | */ |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 39 | public class QuickQSMediaPlayer extends MediaControlPanel { |
Beth Thibodeau | 07d20c3 | 2019-10-16 13:45:56 -0400 | [diff] [blame] | 40 | |
| 41 | private static final String TAG = "QQSMediaPlayer"; |
| 42 | |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 43 | // Button IDs for QS controls |
| 44 | private static final int[] QQS_ACTION_IDS = {R.id.action0, R.id.action1, R.id.action2}; |
Beth Thibodeau | 07d20c3 | 2019-10-16 13:45:56 -0400 | [diff] [blame] | 45 | |
| 46 | /** |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 47 | * Initialize mini media player for QQS |
Beth Thibodeau | 07d20c3 | 2019-10-16 13:45:56 -0400 | [diff] [blame] | 48 | * @param context |
| 49 | * @param parent |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 50 | * @param manager |
Beth Thibodeau | a51c314 | 2020-03-17 17:27:04 -0400 | [diff] [blame] | 51 | * @param foregroundExecutor |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 52 | * @param backgroundExecutor |
Beth Thibodeau | 07d20c3 | 2019-10-16 13:45:56 -0400 | [diff] [blame] | 53 | */ |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 54 | public QuickQSMediaPlayer(Context context, ViewGroup parent, NotificationMediaManager manager, |
Beth Thibodeau | a51c314 | 2020-03-17 17:27:04 -0400 | [diff] [blame] | 55 | Executor foregroundExecutor, Executor backgroundExecutor) { |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 56 | super(context, parent, manager, null, R.layout.qqs_media_panel, QQS_ACTION_IDS, |
Beth Thibodeau | a51c314 | 2020-03-17 17:27:04 -0400 | [diff] [blame] | 57 | foregroundExecutor, backgroundExecutor); |
Beth Thibodeau | 07d20c3 | 2019-10-16 13:45:56 -0400 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 61 | * Update media panel view for the given media session |
Beth Thibodeau | 07d20c3 | 2019-10-16 13:45:56 -0400 | [diff] [blame] | 62 | * @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 Thibodeau | 69b1dfd | 2019-11-08 15:39:25 -0500 | [diff] [blame] | 67 | * @param actionsToShow indices of which actions to display in the mini player |
| 68 | * (max 3: Notification.MediaStyle.MAX_MEDIA_BUTTONS_IN_COMPACT) |
Beth Thibodeau | 899b595 | 2020-01-07 17:50:07 -0500 | [diff] [blame] | 69 | * @param contentIntent Intent to send when user taps on the view |
Beth Thibodeau | 07d20c3 | 2019-10-16 13:45:56 -0400 | [diff] [blame] | 70 | */ |
| 71 | public void setMediaSession(MediaSession.Token token, Icon icon, int iconColor, int bgColor, |
Beth Thibodeau | 899b595 | 2020-01-07 17:50:07 -0500 | [diff] [blame] | 72 | View actionsContainer, int[] actionsToShow, PendingIntent contentIntent) { |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 73 | // Only update if this is a different session and currently playing |
Beth Thibodeau | 899b595 | 2020-01-07 17:50:07 -0500 | [diff] [blame] | 74 | String oldPackage = ""; |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 75 | if (getController() != null) { |
| 76 | oldPackage = getController().getPackageName(); |
Beth Thibodeau | 899b595 | 2020-01-07 17:50:07 -0500 | [diff] [blame] | 77 | } |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 78 | 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 Thibodeau | 4e69f4c | 2019-10-24 14:13:49 -0400 | [diff] [blame] | 84 | return; |
| 85 | } |
| 86 | |
Robert Snoeberger | 9a7409b | 2020-04-09 18:12:27 -0400 | [diff] [blame] | 87 | super.setMediaSession(token, icon, iconColor, bgColor, contentIntent, null); |
Beth Thibodeau | 899b595 | 2020-01-07 17:50:07 -0500 | [diff] [blame] | 88 | |
Beth Thibodeau | 69b1dfd | 2019-11-08 15:39:25 -0500 | [diff] [blame] | 89 | LinearLayout parentActionsLayout = (LinearLayout) actionsContainer; |
Beth Thibodeau | 69b1dfd | 2019-11-08 15:39:25 -0500 | [diff] [blame] | 90 | int i = 0; |
| 91 | if (actionsToShow != null) { |
| 92 | int maxButtons = Math.min(actionsToShow.length, parentActionsLayout.getChildCount()); |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 93 | maxButtons = Math.min(maxButtons, QQS_ACTION_IDS.length); |
Beth Thibodeau | 69b1dfd | 2019-11-08 15:39:25 -0500 | [diff] [blame] | 94 | for (; i < maxButtons; i++) { |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 95 | ImageButton thisBtn = mMediaNotifView.findViewById(QQS_ACTION_IDS[i]); |
| 96 | int thatId = NOTIF_ACTION_IDS[actionsToShow[i]]; |
Beth Thibodeau | 69b1dfd | 2019-11-08 15:39:25 -0500 | [diff] [blame] | 97 | 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 Thibodeau | 07d20c3 | 2019-10-16 13:45:56 -0400 | [diff] [blame] | 110 | } |
Beth Thibodeau | 69b1dfd | 2019-11-08 15:39:25 -0500 | [diff] [blame] | 111 | } |
Beth Thibodeau | 07d20c3 | 2019-10-16 13:45:56 -0400 | [diff] [blame] | 112 | |
Beth Thibodeau | 69b1dfd | 2019-11-08 15:39:25 -0500 | [diff] [blame] | 113 | // Hide any unused buttons |
Beth Thibodeau | 7b6c178 | 2020-03-05 11:43:51 -0500 | [diff] [blame] | 114 | for (; i < QQS_ACTION_IDS.length; i++) { |
| 115 | ImageButton thisBtn = mMediaNotifView.findViewById(QQS_ACTION_IDS[i]); |
Beth Thibodeau | 69b1dfd | 2019-11-08 15:39:25 -0500 | [diff] [blame] | 116 | thisBtn.setVisibility(View.GONE); |
Beth Thibodeau | 07d20c3 | 2019-10-16 13:45:56 -0400 | [diff] [blame] | 117 | } |
| 118 | } |
Beth Thibodeau | 07d20c3 | 2019-10-16 13:45:56 -0400 | [diff] [blame] | 119 | } |