Winson Chung | 97a60d9 | 2017-01-18 16:01:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 17 | package com.android.systemui.pip.phone; |
| 18 | |
Wale Ogunwale | 6827856 | 2017-09-23 17:13:55 -0700 | [diff] [blame] | 19 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED; |
| 20 | import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED; |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 21 | |
| 22 | import android.app.ActivityManager.StackInfo; |
| 23 | import android.app.ActivityOptions; |
Winson Chung | 2dbcf09 | 2018-10-24 13:00:41 -0700 | [diff] [blame] | 24 | import android.app.ActivityTaskManager; |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 25 | import android.app.IActivityManager; |
Winson Chung | 97a60d9 | 2017-01-18 16:01:53 -0800 | [diff] [blame] | 26 | import android.app.RemoteAction; |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 29 | import android.content.pm.ParceledListSlice; |
Winson Chung | e7a3d22 | 2017-03-09 13:26:45 -0800 | [diff] [blame] | 30 | import android.graphics.Rect; |
Winson Chung | 853c99a | 2017-03-21 22:16:42 -0700 | [diff] [blame] | 31 | import android.os.Bundle; |
Winson Chung | bb23376 | 2017-05-15 14:20:46 -0700 | [diff] [blame] | 32 | import android.os.Debug; |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 33 | import android.os.Handler; |
| 34 | import android.os.Message; |
| 35 | import android.os.Messenger; |
| 36 | import android.os.RemoteException; |
Winson Chung | 947ca48 | 2017-07-25 14:28:03 -0700 | [diff] [blame] | 37 | import android.os.SystemClock; |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 38 | import android.os.UserHandle; |
| 39 | import android.util.Log; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 40 | |
Winson Chung | 97a60d9 | 2017-01-18 16:01:53 -0800 | [diff] [blame] | 41 | import com.android.systemui.pip.phone.PipMediaController.ActionListener; |
Winson Chung | cbb15a9 | 2018-01-25 17:46:16 +0000 | [diff] [blame] | 42 | import com.android.systemui.shared.system.InputConsumerController; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 43 | |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 44 | import java.io.PrintWriter; |
Winson Chung | 97a60d9 | 2017-01-18 16:01:53 -0800 | [diff] [blame] | 45 | import java.util.ArrayList; |
| 46 | import java.util.List; |
| 47 | |
| 48 | /** |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 49 | * Manages the PiP menu activity which can show menu options or a scrim. |
Winson Chung | 97a60d9 | 2017-01-18 16:01:53 -0800 | [diff] [blame] | 50 | * |
| 51 | * The current media session provides actions whenever there are no valid actions provided by the |
| 52 | * current PiP activity. Otherwise, those actions always take precedence. |
| 53 | */ |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 54 | public class PipMenuActivityController { |
| 55 | |
Winson Chung | 97a60d9 | 2017-01-18 16:01:53 -0800 | [diff] [blame] | 56 | private static final String TAG = "PipMenuActController"; |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 57 | private static final boolean DEBUG = false; |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 58 | |
| 59 | public static final String EXTRA_CONTROLLER_MESSENGER = "messenger"; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 60 | public static final String EXTRA_ACTIONS = "actions"; |
Winson Chung | e7a3d22 | 2017-03-09 13:26:45 -0800 | [diff] [blame] | 61 | public static final String EXTRA_STACK_BOUNDS = "stack_bounds"; |
| 62 | public static final String EXTRA_MOVEMENT_BOUNDS = "movement_bounds"; |
Winson Chung | 0f873de | 2017-03-30 10:26:48 -0700 | [diff] [blame] | 63 | public static final String EXTRA_ALLOW_TIMEOUT = "allow_timeout"; |
Winson Chung | bb78744 | 2017-09-01 11:33:47 -0700 | [diff] [blame] | 64 | public static final String EXTRA_WILL_RESIZE_MENU = "resize_menu_on_show"; |
Winson Chung | 853c99a | 2017-03-21 22:16:42 -0700 | [diff] [blame] | 65 | public static final String EXTRA_DISMISS_FRACTION = "dismiss_fraction"; |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 66 | public static final String EXTRA_MENU_STATE = "menu_state"; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 67 | |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 68 | public static final int MESSAGE_MENU_STATE_CHANGED = 100; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 69 | public static final int MESSAGE_EXPAND_PIP = 101; |
| 70 | public static final int MESSAGE_MINIMIZE_PIP = 102; |
| 71 | public static final int MESSAGE_DISMISS_PIP = 103; |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 72 | public static final int MESSAGE_UPDATE_ACTIVITY_CALLBACK = 104; |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 73 | public static final int MESSAGE_REGISTER_INPUT_CONSUMER = 105; |
Winson Chung | 2824d7c | 2017-03-15 19:43:00 -0700 | [diff] [blame] | 74 | public static final int MESSAGE_UNREGISTER_INPUT_CONSUMER = 106; |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 75 | public static final int MESSAGE_SHOW_MENU = 107; |
| 76 | |
| 77 | public static final int MENU_STATE_NONE = 0; |
| 78 | public static final int MENU_STATE_CLOSE = 1; |
| 79 | public static final int MENU_STATE_FULL = 2; |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 80 | |
Winson Chung | 947ca48 | 2017-07-25 14:28:03 -0700 | [diff] [blame] | 81 | // The duration to wait before we consider the start activity as having timed out |
| 82 | private static final long START_ACTIVITY_REQUEST_TIMEOUT_MS = 300; |
| 83 | |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 84 | /** |
| 85 | * A listener interface to receive notification on changes in PIP. |
| 86 | */ |
| 87 | public interface Listener { |
| 88 | /** |
| 89 | * Called when the PIP menu visibility changes. |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 90 | * |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 91 | * @param menuState the current state of the menu |
| 92 | * @param resize whether or not to resize the PiP with the state change |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 93 | */ |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 94 | void onPipMenuStateChanged(int menuState, boolean resize); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * Called when the PIP requested to be expanded. |
| 98 | */ |
| 99 | void onPipExpand(); |
| 100 | |
| 101 | /** |
| 102 | * Called when the PIP requested to be minimized. |
| 103 | */ |
| 104 | void onPipMinimize(); |
| 105 | |
| 106 | /** |
Winson Chung | 14fbe14 | 2016-12-19 16:18:24 -0800 | [diff] [blame] | 107 | * Called when the PIP requested to be dismissed. |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 108 | */ |
| 109 | void onPipDismiss(); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Called when the PIP requested to show the menu. |
| 113 | */ |
| 114 | void onPipShowMenu(); |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | private Context mContext; |
| 118 | private IActivityManager mActivityManager; |
Winson Chung | 97a60d9 | 2017-01-18 16:01:53 -0800 | [diff] [blame] | 119 | private PipMediaController mMediaController; |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 120 | private InputConsumerController mInputConsumerController; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 121 | |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 122 | private ArrayList<Listener> mListeners = new ArrayList<>(); |
Winson Chung | 97a60d9 | 2017-01-18 16:01:53 -0800 | [diff] [blame] | 123 | private ParceledListSlice mAppActions; |
| 124 | private ParceledListSlice mMediaActions; |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 125 | private int mMenuState; |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 126 | |
Winson Chung | 0f873de | 2017-03-30 10:26:48 -0700 | [diff] [blame] | 127 | // The dismiss fraction update is sent frequently, so use a temporary bundle for the message |
| 128 | private Bundle mTmpDismissFractionData = new Bundle(); |
Winson Chung | 853c99a | 2017-03-21 22:16:42 -0700 | [diff] [blame] | 129 | |
Winson Chung | 2dbcf09 | 2018-10-24 13:00:41 -0700 | [diff] [blame] | 130 | private Runnable mOnAnimationEndRunnable; |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 131 | private boolean mStartActivityRequested; |
Winson Chung | 947ca48 | 2017-07-25 14:28:03 -0700 | [diff] [blame] | 132 | private long mStartActivityRequestedTime; |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 133 | private Messenger mToActivityMessenger; |
Winson Chung | 947ca48 | 2017-07-25 14:28:03 -0700 | [diff] [blame] | 134 | private Handler mHandler = new Handler() { |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 135 | @Override |
| 136 | public void handleMessage(Message msg) { |
| 137 | switch (msg.what) { |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 138 | case MESSAGE_MENU_STATE_CHANGED: { |
| 139 | int menuState = msg.arg1; |
| 140 | onMenuStateChanged(menuState, true /* resize */); |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 141 | break; |
| 142 | } |
| 143 | case MESSAGE_EXPAND_PIP: { |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 144 | mListeners.forEach(l -> l.onPipExpand()); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 145 | break; |
| 146 | } |
| 147 | case MESSAGE_MINIMIZE_PIP: { |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 148 | mListeners.forEach(l -> l.onPipMinimize()); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 149 | break; |
| 150 | } |
| 151 | case MESSAGE_DISMISS_PIP: { |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 152 | mListeners.forEach(l -> l.onPipDismiss()); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 153 | break; |
| 154 | } |
| 155 | case MESSAGE_SHOW_MENU: { |
| 156 | mListeners.forEach(l -> l.onPipShowMenu()); |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 157 | break; |
| 158 | } |
| 159 | case MESSAGE_REGISTER_INPUT_CONSUMER: { |
| 160 | mInputConsumerController.registerInputConsumer(); |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 161 | break; |
| 162 | } |
Winson Chung | 2824d7c | 2017-03-15 19:43:00 -0700 | [diff] [blame] | 163 | case MESSAGE_UNREGISTER_INPUT_CONSUMER: { |
| 164 | mInputConsumerController.unregisterInputConsumer(); |
| 165 | break; |
| 166 | } |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 167 | case MESSAGE_UPDATE_ACTIVITY_CALLBACK: { |
| 168 | mToActivityMessenger = msg.replyTo; |
Winson Chung | 947ca48 | 2017-07-25 14:28:03 -0700 | [diff] [blame] | 169 | setStartActivityRequested(false); |
Winson Chung | 2dbcf09 | 2018-10-24 13:00:41 -0700 | [diff] [blame] | 170 | if (mOnAnimationEndRunnable != null) { |
| 171 | mOnAnimationEndRunnable.run(); |
| 172 | mOnAnimationEndRunnable = null; |
Winson Chung | b502690 | 2017-05-03 12:45:13 -0700 | [diff] [blame] | 173 | } |
Winson Chung | 929d4f7 | 2017-01-13 10:21:33 -0800 | [diff] [blame] | 174 | // Mark the menu as invisible once the activity finishes as well |
| 175 | if (mToActivityMessenger == null) { |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 176 | onMenuStateChanged(MENU_STATE_NONE, true /* resize */); |
Winson Chung | 929d4f7 | 2017-01-13 10:21:33 -0800 | [diff] [blame] | 177 | } |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 178 | break; |
| 179 | } |
| 180 | } |
| 181 | } |
Winson Chung | 947ca48 | 2017-07-25 14:28:03 -0700 | [diff] [blame] | 182 | }; |
| 183 | private Messenger mMessenger = new Messenger(mHandler); |
| 184 | |
| 185 | private Runnable mStartActivityRequestedTimeoutRunnable = () -> { |
| 186 | setStartActivityRequested(false); |
Winson Chung | 2dbcf09 | 2018-10-24 13:00:41 -0700 | [diff] [blame] | 187 | if (mOnAnimationEndRunnable != null) { |
| 188 | mOnAnimationEndRunnable.run(); |
| 189 | mOnAnimationEndRunnable = null; |
Winson Chung | 947ca48 | 2017-07-25 14:28:03 -0700 | [diff] [blame] | 190 | } |
| 191 | Log.e(TAG, "Expected start menu activity request timed out"); |
| 192 | }; |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 193 | |
Winson Chung | 97a60d9 | 2017-01-18 16:01:53 -0800 | [diff] [blame] | 194 | private ActionListener mMediaActionListener = new ActionListener() { |
| 195 | @Override |
| 196 | public void onMediaActionsChanged(List<RemoteAction> mediaActions) { |
| 197 | mMediaActions = new ParceledListSlice<>(mediaActions); |
| 198 | updateMenuActions(); |
| 199 | } |
| 200 | }; |
| 201 | |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 202 | public PipMenuActivityController(Context context, IActivityManager activityManager, |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 203 | PipMediaController mediaController, InputConsumerController inputConsumerController) { |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 204 | mContext = context; |
| 205 | mActivityManager = activityManager; |
Winson Chung | 97a60d9 | 2017-01-18 16:01:53 -0800 | [diff] [blame] | 206 | mMediaController = mediaController; |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 207 | mInputConsumerController = inputConsumerController; |
| 208 | } |
| 209 | |
Winson Chung | bca0311 | 2017-08-16 10:38:15 -0700 | [diff] [blame] | 210 | public boolean isMenuActivityVisible() { |
| 211 | return mToActivityMessenger != null; |
| 212 | } |
| 213 | |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 214 | public void onActivityPinned() { |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 215 | if (mMenuState == MENU_STATE_NONE) { |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 216 | // If the menu is not visible, then re-register the input consumer if it is not already |
| 217 | // registered |
| 218 | mInputConsumerController.registerInputConsumer(); |
| 219 | } |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Winson Chung | 85d3c8a | 2017-09-15 15:41:00 -0700 | [diff] [blame] | 222 | public void onActivityUnpinned() { |
Winson Chung | 947ca48 | 2017-07-25 14:28:03 -0700 | [diff] [blame] | 223 | hideMenu(); |
| 224 | setStartActivityRequested(false); |
| 225 | } |
| 226 | |
Winson Chung | 3448824 | 2017-04-26 15:53:51 -0700 | [diff] [blame] | 227 | public void onPinnedStackAnimationEnded() { |
| 228 | // Note: Only active menu activities care about this event |
| 229 | if (mToActivityMessenger != null) { |
| 230 | Message m = Message.obtain(); |
| 231 | m.what = PipMenuActivity.MESSAGE_ANIMATION_ENDED; |
| 232 | try { |
| 233 | mToActivityMessenger.send(m); |
| 234 | } catch (RemoteException e) { |
| 235 | Log.e(TAG, "Could not notify menu pinned animation ended", e); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 240 | /** |
| 241 | * Adds a new menu activity listener. |
| 242 | */ |
| 243 | public void addListener(Listener listener) { |
| 244 | if (!mListeners.contains(listener)) { |
| 245 | mListeners.add(listener); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /** |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 250 | * Updates the appearance of the menu and scrim on top of the PiP while dismissing. |
| 251 | */ |
| 252 | public void setDismissFraction(float fraction) { |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 253 | if (DEBUG) { |
| 254 | Log.d(TAG, "setDismissFraction() hasActivity=" + (mToActivityMessenger != null) |
| 255 | + " fraction=" + fraction); |
| 256 | } |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 257 | if (mToActivityMessenger != null) { |
Winson Chung | 0f873de | 2017-03-30 10:26:48 -0700 | [diff] [blame] | 258 | mTmpDismissFractionData.clear(); |
| 259 | mTmpDismissFractionData.putFloat(EXTRA_DISMISS_FRACTION, fraction); |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 260 | Message m = Message.obtain(); |
| 261 | m.what = PipMenuActivity.MESSAGE_UPDATE_DISMISS_FRACTION; |
Winson Chung | 0f873de | 2017-03-30 10:26:48 -0700 | [diff] [blame] | 262 | m.obj = mTmpDismissFractionData; |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 263 | try { |
| 264 | mToActivityMessenger.send(m); |
| 265 | } catch (RemoteException e) { |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 266 | Log.e(TAG, "Could not notify menu to update dismiss fraction", e); |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 267 | } |
Winson Chung | 947ca48 | 2017-07-25 14:28:03 -0700 | [diff] [blame] | 268 | } else if (!mStartActivityRequested || isStartActivityRequestedElapsed()) { |
| 269 | // If we haven't requested the start activity, or if it previously took too long to |
| 270 | // start, then start it |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 271 | startMenuActivity(MENU_STATE_NONE, null /* stackBounds */, |
Winson Chung | bb78744 | 2017-09-01 11:33:47 -0700 | [diff] [blame] | 272 | null /* movementBounds */, false /* allowMenuTimeout */, |
| 273 | false /* resizeMenuOnShow */); |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | /** |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 278 | * Shows the menu activity. |
| 279 | */ |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 280 | public void showMenu(int menuState, Rect stackBounds, Rect movementBounds, |
Winson Chung | bb78744 | 2017-09-01 11:33:47 -0700 | [diff] [blame] | 281 | boolean allowMenuTimeout, boolean willResizeMenu) { |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 282 | if (DEBUG) { |
Winson Chung | bb23376 | 2017-05-15 14:20:46 -0700 | [diff] [blame] | 283 | Log.d(TAG, "showMenu() state=" + menuState |
| 284 | + " hasActivity=" + (mToActivityMessenger != null) |
| 285 | + " callers=\n" + Debug.getCallers(5, " ")); |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 286 | } |
Winson Chung | bb78744 | 2017-09-01 11:33:47 -0700 | [diff] [blame] | 287 | |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 288 | if (mToActivityMessenger != null) { |
Winson Chung | 0f873de | 2017-03-30 10:26:48 -0700 | [diff] [blame] | 289 | Bundle data = new Bundle(); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 290 | data.putInt(EXTRA_MENU_STATE, menuState); |
Winson Chung | 0f873de | 2017-03-30 10:26:48 -0700 | [diff] [blame] | 291 | data.putParcelable(EXTRA_STACK_BOUNDS, stackBounds); |
| 292 | data.putParcelable(EXTRA_MOVEMENT_BOUNDS, movementBounds); |
| 293 | data.putBoolean(EXTRA_ALLOW_TIMEOUT, allowMenuTimeout); |
Winson Chung | bb78744 | 2017-09-01 11:33:47 -0700 | [diff] [blame] | 294 | data.putBoolean(EXTRA_WILL_RESIZE_MENU, willResizeMenu); |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 295 | Message m = Message.obtain(); |
| 296 | m.what = PipMenuActivity.MESSAGE_SHOW_MENU; |
Winson Chung | 0f873de | 2017-03-30 10:26:48 -0700 | [diff] [blame] | 297 | m.obj = data; |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 298 | try { |
| 299 | mToActivityMessenger.send(m); |
| 300 | } catch (RemoteException e) { |
| 301 | Log.e(TAG, "Could not notify menu to show", e); |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 302 | } |
Winson Chung | 947ca48 | 2017-07-25 14:28:03 -0700 | [diff] [blame] | 303 | } else if (!mStartActivityRequested || isStartActivityRequestedElapsed()) { |
| 304 | // If we haven't requested the start activity, or if it previously took too long to |
| 305 | // start, then start it |
Winson Chung | bb78744 | 2017-09-01 11:33:47 -0700 | [diff] [blame] | 306 | startMenuActivity(menuState, stackBounds, movementBounds, allowMenuTimeout, |
| 307 | willResizeMenu); |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
| 311 | /** |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 312 | * Pokes the menu, indicating that the user is interacting with it. |
| 313 | */ |
| 314 | public void pokeMenu() { |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 315 | if (DEBUG) { |
| 316 | Log.d(TAG, "pokeMenu() hasActivity=" + (mToActivityMessenger != null)); |
| 317 | } |
Mady Mellor | a7f6974 | 2017-02-03 11:00:20 -0800 | [diff] [blame] | 318 | if (mToActivityMessenger != null) { |
| 319 | Message m = Message.obtain(); |
| 320 | m.what = PipMenuActivity.MESSAGE_POKE_MENU; |
| 321 | try { |
| 322 | mToActivityMessenger.send(m); |
| 323 | } catch (RemoteException e) { |
| 324 | Log.e(TAG, "Could not notify poke menu", e); |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | /** |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 330 | * Hides the menu activity. |
| 331 | */ |
| 332 | public void hideMenu() { |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 333 | if (DEBUG) { |
Winson Chung | bb23376 | 2017-05-15 14:20:46 -0700 | [diff] [blame] | 334 | Log.d(TAG, "hideMenu() state=" + mMenuState |
| 335 | + " hasActivity=" + (mToActivityMessenger != null) |
| 336 | + " callers=\n" + Debug.getCallers(5, " ")); |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 337 | } |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 338 | if (mToActivityMessenger != null) { |
| 339 | Message m = Message.obtain(); |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 340 | m.what = PipMenuActivity.MESSAGE_HIDE_MENU; |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 341 | try { |
| 342 | mToActivityMessenger.send(m); |
| 343 | } catch (RemoteException e) { |
Winson Chung | c75ffe8 | 2016-12-16 16:20:16 -0800 | [diff] [blame] | 344 | Log.e(TAG, "Could not notify menu to hide", e); |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 345 | } |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 346 | } |
| 347 | } |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 348 | |
| 349 | /** |
Winson Chung | 2dbcf09 | 2018-10-24 13:00:41 -0700 | [diff] [blame] | 350 | * Hides the menu activity. |
| 351 | */ |
| 352 | public void hideMenu(Runnable onStartCallback, Runnable onEndCallback) { |
| 353 | if (mStartActivityRequested) { |
| 354 | // If the menu has been start-requested, but not actually started, then we defer the |
| 355 | // trigger callback until the menu has started and called back to the controller. |
| 356 | mOnAnimationEndRunnable = onEndCallback; |
| 357 | onStartCallback.run(); |
| 358 | |
| 359 | // Fallback for b/63752800, we have started the PipMenuActivity but it has not made any |
| 360 | // callbacks. Don't continue to wait for the menu to show past some timeout. |
| 361 | mHandler.removeCallbacks(mStartActivityRequestedTimeoutRunnable); |
| 362 | mHandler.postDelayed(mStartActivityRequestedTimeoutRunnable, |
| 363 | START_ACTIVITY_REQUEST_TIMEOUT_MS); |
| 364 | } else if (mMenuState != MENU_STATE_NONE && mToActivityMessenger != null) { |
| 365 | // If the menu is visible in either the closed or full state, then hide the menu and |
| 366 | // trigger the animation trigger afterwards |
| 367 | onStartCallback.run(); |
| 368 | Message m = Message.obtain(); |
| 369 | m.what = PipMenuActivity.MESSAGE_HIDE_MENU; |
| 370 | m.obj = onEndCallback; |
| 371 | try { |
| 372 | mToActivityMessenger.send(m); |
| 373 | } catch (RemoteException e) { |
| 374 | Log.e(TAG, "Could not notify hide menu", e); |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | /** |
Winson Chung | 79f852e | 2017-05-04 15:06:18 -0700 | [diff] [blame] | 380 | * Preemptively mark the menu as invisible, used when we are directly manipulating the pinned |
| 381 | * stack and don't want to trigger a resize which can animate the stack in a conflicting way |
| 382 | * (ie. when manually expanding or dismissing). |
| 383 | */ |
| 384 | public void hideMenuWithoutResize() { |
| 385 | onMenuStateChanged(MENU_STATE_NONE, false /* resize */); |
| 386 | } |
| 387 | |
| 388 | /** |
Winson Chung | 97a60d9 | 2017-01-18 16:01:53 -0800 | [diff] [blame] | 389 | * Sets the menu actions to the actions provided by the current PiP activity. |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 390 | */ |
Winson Chung | 97a60d9 | 2017-01-18 16:01:53 -0800 | [diff] [blame] | 391 | public void setAppActions(ParceledListSlice appActions) { |
| 392 | mAppActions = appActions; |
| 393 | updateMenuActions(); |
| 394 | } |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 395 | |
Winson Chung | 97a60d9 | 2017-01-18 16:01:53 -0800 | [diff] [blame] | 396 | /** |
| 397 | * @return the best set of actions to show in the PiP menu. |
| 398 | */ |
| 399 | private ParceledListSlice resolveMenuActions() { |
| 400 | if (isValidActions(mAppActions)) { |
| 401 | return mAppActions; |
| 402 | } |
| 403 | return mMediaActions; |
| 404 | } |
| 405 | |
| 406 | /** |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 407 | * Starts the menu activity on the top task of the pinned stack. |
| 408 | */ |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 409 | private void startMenuActivity(int menuState, Rect stackBounds, Rect movementBounds, |
Winson Chung | bb78744 | 2017-09-01 11:33:47 -0700 | [diff] [blame] | 410 | boolean allowMenuTimeout, boolean willResizeMenu) { |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 411 | try { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 412 | StackInfo pinnedStackInfo = ActivityTaskManager.getService().getStackInfo( |
Wale Ogunwale | 6827856 | 2017-09-23 17:13:55 -0700 | [diff] [blame] | 413 | WINDOWING_MODE_PINNED, ACTIVITY_TYPE_UNDEFINED); |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 414 | if (pinnedStackInfo != null && pinnedStackInfo.taskIds != null && |
| 415 | pinnedStackInfo.taskIds.length > 0) { |
| 416 | Intent intent = new Intent(mContext, PipMenuActivity.class); |
| 417 | intent.putExtra(EXTRA_CONTROLLER_MESSENGER, mMessenger); |
| 418 | intent.putExtra(EXTRA_ACTIONS, resolveMenuActions()); |
| 419 | if (stackBounds != null) { |
Winson Chung | 853c99a | 2017-03-21 22:16:42 -0700 | [diff] [blame] | 420 | intent.putExtra(EXTRA_STACK_BOUNDS, stackBounds); |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 421 | } |
| 422 | if (movementBounds != null) { |
Winson Chung | 853c99a | 2017-03-21 22:16:42 -0700 | [diff] [blame] | 423 | intent.putExtra(EXTRA_MOVEMENT_BOUNDS, movementBounds); |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 424 | } |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 425 | intent.putExtra(EXTRA_MENU_STATE, menuState); |
Winson Chung | 0f873de | 2017-03-30 10:26:48 -0700 | [diff] [blame] | 426 | intent.putExtra(EXTRA_ALLOW_TIMEOUT, allowMenuTimeout); |
Winson Chung | bb78744 | 2017-09-01 11:33:47 -0700 | [diff] [blame] | 427 | intent.putExtra(EXTRA_WILL_RESIZE_MENU, willResizeMenu); |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 428 | ActivityOptions options = ActivityOptions.makeCustomAnimation(mContext, 0, 0); |
| 429 | options.setLaunchTaskId( |
| 430 | pinnedStackInfo.taskIds[pinnedStackInfo.taskIds.length - 1]); |
| 431 | options.setTaskOverlay(true, true /* canResume */); |
| 432 | mContext.startActivityAsUser(intent, options.toBundle(), UserHandle.CURRENT); |
Winson Chung | 947ca48 | 2017-07-25 14:28:03 -0700 | [diff] [blame] | 433 | setStartActivityRequested(true); |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 434 | } else { |
| 435 | Log.e(TAG, "No PIP tasks found"); |
| 436 | } |
| 437 | } catch (RemoteException e) { |
Winson Chung | 947ca48 | 2017-07-25 14:28:03 -0700 | [diff] [blame] | 438 | setStartActivityRequested(false); |
Mady Mellor | 81d4061 | 2017-03-10 15:14:10 -0800 | [diff] [blame] | 439 | Log.e(TAG, "Error showing PIP menu activity", e); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | /** |
Winson Chung | 97a60d9 | 2017-01-18 16:01:53 -0800 | [diff] [blame] | 444 | * Updates the PiP menu activity with the best set of actions provided. |
| 445 | */ |
| 446 | private void updateMenuActions() { |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 447 | if (mToActivityMessenger != null) { |
Winson Chung | e7a3d22 | 2017-03-09 13:26:45 -0800 | [diff] [blame] | 448 | // Fetch the pinned stack bounds |
| 449 | Rect stackBounds = null; |
| 450 | try { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 451 | StackInfo pinnedStackInfo = ActivityTaskManager.getService().getStackInfo( |
Wale Ogunwale | 6827856 | 2017-09-23 17:13:55 -0700 | [diff] [blame] | 452 | WINDOWING_MODE_PINNED, ACTIVITY_TYPE_UNDEFINED); |
Winson Chung | e7a3d22 | 2017-03-09 13:26:45 -0800 | [diff] [blame] | 453 | if (pinnedStackInfo != null) { |
| 454 | stackBounds = pinnedStackInfo.bounds; |
| 455 | } |
| 456 | } catch (RemoteException e) { |
| 457 | Log.e(TAG, "Error showing PIP menu activity", e); |
| 458 | } |
| 459 | |
Winson Chung | 0f873de | 2017-03-30 10:26:48 -0700 | [diff] [blame] | 460 | Bundle data = new Bundle(); |
| 461 | data.putParcelable(EXTRA_STACK_BOUNDS, stackBounds); |
| 462 | data.putParcelable(EXTRA_ACTIONS, resolveMenuActions()); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 463 | Message m = Message.obtain(); |
| 464 | m.what = PipMenuActivity.MESSAGE_UPDATE_ACTIONS; |
Winson Chung | 0f873de | 2017-03-30 10:26:48 -0700 | [diff] [blame] | 465 | m.obj = data; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 466 | try { |
| 467 | mToActivityMessenger.send(m); |
| 468 | } catch (RemoteException e) { |
| 469 | Log.e(TAG, "Could not notify menu activity to update actions", e); |
| 470 | } |
| 471 | } |
| 472 | } |
Winson Chung | 97a60d9 | 2017-01-18 16:01:53 -0800 | [diff] [blame] | 473 | |
| 474 | /** |
| 475 | * Returns whether the set of actions are valid. |
| 476 | */ |
| 477 | private boolean isValidActions(ParceledListSlice actions) { |
| 478 | return actions != null && actions.getList().size() > 0; |
| 479 | } |
| 480 | |
| 481 | /** |
Winson Chung | 947ca48 | 2017-07-25 14:28:03 -0700 | [diff] [blame] | 482 | * @return whether the time of the activity request has exceeded the timeout. |
| 483 | */ |
| 484 | private boolean isStartActivityRequestedElapsed() { |
| 485 | return (SystemClock.uptimeMillis() - mStartActivityRequestedTime) |
| 486 | >= START_ACTIVITY_REQUEST_TIMEOUT_MS; |
| 487 | } |
| 488 | |
| 489 | /** |
Winson Chung | 97a60d9 | 2017-01-18 16:01:53 -0800 | [diff] [blame] | 490 | * Handles changes in menu visibility. |
| 491 | */ |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 492 | private void onMenuStateChanged(int menuState, boolean resize) { |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 493 | if (DEBUG) { |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 494 | Log.d(TAG, "onMenuStateChanged() mMenuState=" + mMenuState |
| 495 | + " menuState=" + menuState + " resize=" + resize); |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 496 | } |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 497 | if (menuState == MENU_STATE_NONE) { |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 498 | mInputConsumerController.registerInputConsumer(); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 499 | } else { |
| 500 | mInputConsumerController.unregisterInputConsumer(); |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 501 | } |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 502 | if (menuState != mMenuState) { |
| 503 | mListeners.forEach(l -> l.onPipMenuStateChanged(menuState, resize)); |
| 504 | if (menuState == MENU_STATE_FULL) { |
Winson Chung | 97a60d9 | 2017-01-18 16:01:53 -0800 | [diff] [blame] | 505 | // Once visible, start listening for media action changes. This call will trigger |
| 506 | // the menu actions to be updated again. |
| 507 | mMediaController.addListener(mMediaActionListener); |
| 508 | } else { |
| 509 | // Once hidden, stop listening for media action changes. This call will trigger |
| 510 | // the menu actions to be updated again. |
| 511 | mMediaController.removeListener(mMediaActionListener); |
| 512 | } |
| 513 | } |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 514 | mMenuState = menuState; |
Winson Chung | 97a60d9 | 2017-01-18 16:01:53 -0800 | [diff] [blame] | 515 | } |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 516 | |
Winson Chung | 947ca48 | 2017-07-25 14:28:03 -0700 | [diff] [blame] | 517 | private void setStartActivityRequested(boolean requested) { |
| 518 | mHandler.removeCallbacks(mStartActivityRequestedTimeoutRunnable); |
| 519 | mStartActivityRequested = requested; |
| 520 | mStartActivityRequestedTime = requested ? SystemClock.uptimeMillis() : 0; |
| 521 | } |
| 522 | |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 523 | public void dump(PrintWriter pw, String prefix) { |
| 524 | final String innerPrefix = prefix + " "; |
| 525 | pw.println(prefix + TAG); |
Mady Mellor | 637cd48 | 2017-03-21 10:39:42 -0700 | [diff] [blame] | 526 | pw.println(innerPrefix + "mMenuState=" + mMenuState); |
Winson Chung | 87e5d55 | 2017-04-05 11:49:38 -0700 | [diff] [blame] | 527 | pw.println(innerPrefix + "mToActivityMessenger=" + mToActivityMessenger); |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 528 | pw.println(innerPrefix + "mListeners=" + mListeners.size()); |
Winson Chung | 947ca48 | 2017-07-25 14:28:03 -0700 | [diff] [blame] | 529 | pw.println(innerPrefix + "mStartActivityRequested=" + mStartActivityRequested); |
| 530 | pw.println(innerPrefix + "mStartActivityRequestedTime=" + mStartActivityRequestedTime); |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 531 | } |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 532 | } |