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