Jaewan Kim | 26c6356 | 2017-04-26 15:41:43 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.pip.tv; |
| 18 | |
| 19 | import android.app.Notification; |
| 20 | import android.app.NotificationManager; |
| 21 | import android.app.PendingIntent; |
| 22 | import android.content.BroadcastReceiver; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.IntentFilter; |
Winson Chung | b6de872 | 2017-06-02 12:45:51 -0700 | [diff] [blame] | 26 | import android.content.pm.ParceledListSlice; |
Jaewan Kim | 26c6356 | 2017-04-26 15:41:43 +0900 | [diff] [blame] | 27 | import android.content.res.Resources; |
| 28 | import android.graphics.Bitmap; |
Jaewan Kim | 26c6356 | 2017-04-26 15:41:43 +0900 | [diff] [blame] | 29 | import android.media.MediaMetadata; |
| 30 | import android.media.session.MediaController; |
| 31 | import android.media.session.PlaybackState; |
| 32 | import android.text.TextUtils; |
| 33 | import android.util.Log; |
Jaewan Kim | 26c6356 | 2017-04-26 15:41:43 +0900 | [diff] [blame] | 34 | |
Jaewan Kim | 26c6356 | 2017-04-26 15:41:43 +0900 | [diff] [blame] | 35 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 36 | import com.android.systemui.R; |
| 37 | import com.android.systemui.util.NotificationChannels; |
Jaewan Kim | 26c6356 | 2017-04-26 15:41:43 +0900 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * A notification that informs users that PIP is running and also provides PIP controls. |
| 41 | * <p>Once it's created, it will manage the PIP notification UI by itself except for handling |
| 42 | * configuration changes. |
| 43 | */ |
| 44 | public class PipNotification { |
| 45 | private static final String TAG = "PipNotification"; |
Winson Chung | facf213 | 2019-04-15 17:39:49 -0700 | [diff] [blame] | 46 | private static final String NOTIFICATION_TAG = PipNotification.class.getSimpleName(); |
Jaewan Kim | 26c6356 | 2017-04-26 15:41:43 +0900 | [diff] [blame] | 47 | private static final boolean DEBUG = PipManager.DEBUG; |
| 48 | |
| 49 | private static final String ACTION_MENU = "PipNotification.menu"; |
| 50 | private static final String ACTION_CLOSE = "PipNotification.close"; |
| 51 | |
| 52 | private final PipManager mPipManager = PipManager.getInstance(); |
| 53 | |
| 54 | private final NotificationManager mNotificationManager; |
| 55 | private final Notification.Builder mNotificationBuilder; |
| 56 | |
| 57 | private MediaController mMediaController; |
| 58 | private String mDefaultTitle; |
Youngsang Cho | bad2669 | 2017-05-10 17:09:10 -0700 | [diff] [blame] | 59 | private int mDefaultIconResId; |
Jaewan Kim | 26c6356 | 2017-04-26 15:41:43 +0900 | [diff] [blame] | 60 | |
| 61 | private boolean mNotified; |
| 62 | private String mTitle; |
| 63 | private Bitmap mArt; |
| 64 | |
| 65 | private PipManager.Listener mPipListener = new PipManager.Listener() { |
| 66 | @Override |
| 67 | public void onPipEntered() { |
| 68 | updateMediaControllerMetadata(); |
| 69 | notifyPipNotification(); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void onPipActivityClosed() { |
| 74 | dismissPipNotification(); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public void onShowPipMenu() { |
| 79 | // no-op. |
| 80 | } |
| 81 | |
| 82 | @Override |
Winson Chung | b6de872 | 2017-06-02 12:45:51 -0700 | [diff] [blame] | 83 | public void onPipMenuActionsChanged(ParceledListSlice actions) { |
| 84 | // no-op. |
| 85 | } |
| 86 | |
| 87 | @Override |
Jaewan Kim | 26c6356 | 2017-04-26 15:41:43 +0900 | [diff] [blame] | 88 | public void onMoveToFullscreen() { |
| 89 | dismissPipNotification(); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void onPipResizeAboutToStart() { |
| 94 | // no-op. |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | private MediaController.Callback mMediaControllerCallback = new MediaController.Callback() { |
| 99 | @Override |
| 100 | public void onPlaybackStateChanged(PlaybackState state) { |
| 101 | if (updateMediaControllerMetadata() && mNotified) { |
| 102 | // update notification |
| 103 | notifyPipNotification(); |
| 104 | } |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | private final PipManager.MediaListener mPipMediaListener = new PipManager.MediaListener() { |
| 109 | @Override |
| 110 | public void onMediaControllerChanged() { |
| 111 | MediaController newController = mPipManager.getMediaController(); |
| 112 | if (mMediaController == newController) { |
| 113 | return; |
| 114 | } |
| 115 | if (mMediaController != null) { |
| 116 | mMediaController.unregisterCallback(mMediaControllerCallback); |
| 117 | } |
| 118 | mMediaController = newController; |
| 119 | if (mMediaController != null) { |
| 120 | mMediaController.registerCallback(mMediaControllerCallback); |
| 121 | } |
| 122 | if (updateMediaControllerMetadata() && mNotified) { |
| 123 | // update notification |
| 124 | notifyPipNotification(); |
| 125 | } |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | private final BroadcastReceiver mEventReceiver = new BroadcastReceiver() { |
| 130 | @Override |
| 131 | public void onReceive(Context context, Intent intent) { |
| 132 | if (DEBUG) { |
| 133 | Log.d(TAG, "Received " + intent.getAction() + " from the notification UI"); |
| 134 | } |
| 135 | switch (intent.getAction()) { |
| 136 | case ACTION_MENU: |
| 137 | mPipManager.showPictureInPictureMenu(); |
| 138 | break; |
| 139 | case ACTION_CLOSE: |
| 140 | mPipManager.closePip(); |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | public PipNotification(Context context) { |
| 147 | mNotificationManager = (NotificationManager) context.getSystemService( |
| 148 | Context.NOTIFICATION_SERVICE); |
| 149 | |
| 150 | mNotificationBuilder = new Notification.Builder(context, NotificationChannels.TVPIP) |
| 151 | .setLocalOnly(true) |
| 152 | .setOngoing(false) |
| 153 | .setCategory(Notification.CATEGORY_SYSTEM) |
| 154 | .extend(new Notification.TvExtender() |
| 155 | .setContentIntent(createPendingIntent(context, ACTION_MENU)) |
| 156 | .setDeleteIntent(createPendingIntent(context, ACTION_CLOSE))); |
| 157 | |
| 158 | mPipManager.addListener(mPipListener); |
| 159 | mPipManager.addMediaListener(mPipMediaListener); |
| 160 | |
| 161 | IntentFilter intentFilter = new IntentFilter(); |
| 162 | intentFilter.addAction(ACTION_MENU); |
| 163 | intentFilter.addAction(ACTION_CLOSE); |
| 164 | context.registerReceiver(mEventReceiver, intentFilter); |
| 165 | |
| 166 | onConfigurationChanged(context); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Called by {@link PipManager} when the configuration is changed. |
| 171 | */ |
| 172 | void onConfigurationChanged(Context context) { |
| 173 | Resources res = context.getResources(); |
| 174 | mDefaultTitle = res.getString(R.string.pip_notification_unknown_title); |
Shubang | 856493b | 2017-06-21 19:15:26 -0700 | [diff] [blame] | 175 | mDefaultIconResId = R.drawable.pip_icon; |
Jaewan Kim | 26c6356 | 2017-04-26 15:41:43 +0900 | [diff] [blame] | 176 | if (mNotified) { |
| 177 | // update notification |
| 178 | notifyPipNotification(); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | private void notifyPipNotification() { |
| 183 | mNotified = true; |
| 184 | mNotificationBuilder |
| 185 | .setShowWhen(true) |
| 186 | .setWhen(System.currentTimeMillis()) |
Youngsang Cho | bad2669 | 2017-05-10 17:09:10 -0700 | [diff] [blame] | 187 | .setSmallIcon(mDefaultIconResId) |
Jaewan Kim | 26c6356 | 2017-04-26 15:41:43 +0900 | [diff] [blame] | 188 | .setContentTitle(!TextUtils.isEmpty(mTitle) ? mTitle : mDefaultTitle); |
Youngsang Cho | bad2669 | 2017-05-10 17:09:10 -0700 | [diff] [blame] | 189 | if (mArt != null) { |
| 190 | mNotificationBuilder.setStyle(new Notification.BigPictureStyle() |
| 191 | .bigPicture(mArt)); |
| 192 | } else { |
| 193 | mNotificationBuilder.setStyle(null); |
| 194 | } |
| 195 | mNotificationManager.notify(NOTIFICATION_TAG, SystemMessage.NOTE_TV_PIP, |
| 196 | mNotificationBuilder.build()); |
Jaewan Kim | 26c6356 | 2017-04-26 15:41:43 +0900 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | private void dismissPipNotification() { |
| 200 | mNotified = false; |
Jaewan Kim | b7db9f9 | 2017-05-23 18:34:07 +0900 | [diff] [blame] | 201 | mNotificationManager.cancel(NOTIFICATION_TAG, SystemMessage.NOTE_TV_PIP); |
Jaewan Kim | 26c6356 | 2017-04-26 15:41:43 +0900 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | private boolean updateMediaControllerMetadata() { |
| 205 | String title = null; |
| 206 | Bitmap art = null; |
| 207 | if (mPipManager.getMediaController() != null) { |
| 208 | MediaMetadata metadata = mPipManager.getMediaController().getMetadata(); |
| 209 | if (metadata != null) { |
| 210 | title = metadata.getString(MediaMetadata.METADATA_KEY_DISPLAY_TITLE); |
| 211 | if (TextUtils.isEmpty(title)) { |
| 212 | title = metadata.getString(MediaMetadata.METADATA_KEY_TITLE); |
| 213 | } |
| 214 | art = metadata.getBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART); |
| 215 | if (art == null) { |
| 216 | art = metadata.getBitmap(MediaMetadata.METADATA_KEY_ART); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | if (!TextUtils.equals(title, mTitle) || art != mArt) { |
| 221 | mTitle = title; |
| 222 | mArt = art; |
| 223 | return true; |
| 224 | } |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | private static PendingIntent createPendingIntent(Context context, String action) { |
| 229 | return PendingIntent.getBroadcast(context, 0, |
| 230 | new Intent(action), PendingIntent.FLAG_CANCEL_CURRENT); |
| 231 | } |
| 232 | } |