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