Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [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 | |
| 17 | package com.android.systemui.pip.phone; |
| 18 | |
Winson Chung | b502690 | 2017-05-03 12:45:13 -0700 | [diff] [blame^] | 19 | import static android.app.ActivityManager.StackId.PINNED_STACK_ID; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 20 | import static android.view.Display.DEFAULT_DISPLAY; |
| 21 | |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 22 | import android.app.ActivityManager; |
Winson Chung | b502690 | 2017-05-03 12:45:13 -0700 | [diff] [blame^] | 23 | import android.app.ActivityManager.StackInfo; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 24 | import android.app.IActivityManager; |
Winson Chung | 3535df2 | 2017-01-17 18:43:39 -0800 | [diff] [blame] | 25 | import android.content.ComponentName; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 27 | import android.content.pm.ParceledListSlice; |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 28 | import android.graphics.Rect; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 29 | import android.os.Handler; |
| 30 | import android.os.RemoteException; |
| 31 | import android.util.Log; |
| 32 | import android.view.IPinnedStackController; |
| 33 | import android.view.IPinnedStackListener; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 34 | import android.view.IWindowManager; |
| 35 | import android.view.WindowManagerGlobal; |
| 36 | |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 37 | import com.android.systemui.pip.BasePipManager; |
Winson Chung | b502690 | 2017-05-03 12:45:13 -0700 | [diff] [blame^] | 38 | import com.android.systemui.recents.events.EventBus; |
| 39 | import com.android.systemui.recents.events.component.ExpandPipEvent; |
Winson Chung | 929d4f7 | 2017-01-13 10:21:33 -0800 | [diff] [blame] | 40 | import com.android.systemui.recents.misc.SystemServicesProxy; |
| 41 | import com.android.systemui.recents.misc.SystemServicesProxy.TaskStackListener; |
Winson Chung | ac52f28 | 2017-03-30 14:44:52 -0700 | [diff] [blame] | 42 | import com.android.systemui.statusbar.CommandQueue; |
Winson Chung | 929d4f7 | 2017-01-13 10:21:33 -0800 | [diff] [blame] | 43 | |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 44 | import java.io.PrintWriter; |
| 45 | |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 46 | /** |
| 47 | * Manages the picture-in-picture (PIP) UI and states for Phones. |
| 48 | */ |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 49 | public class PipManager implements BasePipManager { |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 50 | private static final String TAG = "PipManager"; |
| 51 | |
| 52 | private static PipManager sPipController; |
| 53 | |
| 54 | private Context mContext; |
| 55 | private IActivityManager mActivityManager; |
| 56 | private IWindowManager mWindowManager; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 57 | private Handler mHandler = new Handler(); |
| 58 | |
| 59 | private final PinnedStackListener mPinnedStackListener = new PinnedStackListener(); |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 60 | |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 61 | private InputConsumerController mInputConsumerController; |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 62 | private PipMenuActivityController mMenuController; |
Winson Chung | 97a60d9 | 2017-01-18 16:01:53 -0800 | [diff] [blame] | 63 | private PipMediaController mMediaController; |
Winson Chung | c81c0ce | 2017-03-17 12:27:30 -0700 | [diff] [blame] | 64 | private PipNotificationController mNotificationController; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 65 | private PipTouchHandler mTouchHandler; |
| 66 | |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 67 | /** |
Winson Chung | 929d4f7 | 2017-01-13 10:21:33 -0800 | [diff] [blame] | 68 | * Handler for system task stack changes. |
| 69 | */ |
| 70 | TaskStackListener mTaskStackListener = new TaskStackListener() { |
| 71 | @Override |
Winson Chung | b502690 | 2017-05-03 12:45:13 -0700 | [diff] [blame^] | 72 | public void onActivityPinned(String packageName, int taskId) { |
Winson Chung | 3535df2 | 2017-01-17 18:43:39 -0800 | [diff] [blame] | 73 | if (!checkCurrentUserId(false /* debug */)) { |
| 74 | return; |
| 75 | } |
Winson Chung | c81c0ce | 2017-03-17 12:27:30 -0700 | [diff] [blame] | 76 | |
Winson Chung | 929d4f7 | 2017-01-13 10:21:33 -0800 | [diff] [blame] | 77 | mTouchHandler.onActivityPinned(); |
Winson Chung | 97a60d9 | 2017-01-18 16:01:53 -0800 | [diff] [blame] | 78 | mMediaController.onActivityPinned(); |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 79 | mMenuController.onActivityPinned(); |
Winson Chung | 6b991b6 | 2017-05-11 11:30:36 -0700 | [diff] [blame] | 80 | mNotificationController.onActivityPinned(packageName, |
| 81 | true /* deferUntilAnimationEnds */); |
Winson Chung | ac52f28 | 2017-03-30 14:44:52 -0700 | [diff] [blame] | 82 | |
| 83 | SystemServicesProxy.getInstance(mContext).setPipVisibility(true); |
Winson Chung | c81c0ce | 2017-03-17 12:27:30 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public void onActivityUnpinned() { |
| 88 | if (!checkCurrentUserId(false /* debug */)) { |
| 89 | return; |
| 90 | } |
| 91 | |
Winson Chung | ac52f28 | 2017-03-30 14:44:52 -0700 | [diff] [blame] | 92 | ComponentName topPipActivity = PipUtils.getTopPinnedActivity(mContext, |
| 93 | mActivityManager); |
Winson Chung | 32c566f | 2017-04-11 18:31:21 -0700 | [diff] [blame] | 94 | mMenuController.hideMenu(); |
Winson Chung | ac52f28 | 2017-03-30 14:44:52 -0700 | [diff] [blame] | 95 | mNotificationController.onActivityUnpinned(topPipActivity); |
| 96 | |
| 97 | SystemServicesProxy.getInstance(mContext).setPipVisibility(topPipActivity != null); |
Winson Chung | 929d4f7 | 2017-01-13 10:21:33 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
Winson Chung | 85d3998 | 2017-02-24 15:21:25 -0800 | [diff] [blame] | 101 | public void onPinnedStackAnimationStarted() { |
| 102 | // Disable touches while the animation is running |
| 103 | mTouchHandler.setTouchEnabled(false); |
| 104 | } |
| 105 | |
| 106 | @Override |
Winson Chung | 929d4f7 | 2017-01-13 10:21:33 -0800 | [diff] [blame] | 107 | public void onPinnedStackAnimationEnded() { |
Winson Chung | 85d3998 | 2017-02-24 15:21:25 -0800 | [diff] [blame] | 108 | // Re-enable touches after the animation completes |
| 109 | mTouchHandler.setTouchEnabled(true); |
Winson Chung | ac52f28 | 2017-03-30 14:44:52 -0700 | [diff] [blame] | 110 | mTouchHandler.onPinnedStackAnimationEnded(); |
Winson Chung | 3448824 | 2017-04-26 15:53:51 -0700 | [diff] [blame] | 111 | mMenuController.onPinnedStackAnimationEnded(); |
Winson Chung | 6b991b6 | 2017-05-11 11:30:36 -0700 | [diff] [blame] | 112 | mNotificationController.onPinnedStackAnimationEnded(); |
Winson Chung | 929d4f7 | 2017-01-13 10:21:33 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Override |
Winson Chung | e6385a2 | 2017-05-02 18:15:16 -0700 | [diff] [blame] | 116 | public void onPinnedActivityRestartAttempt(boolean clearedTask) { |
Winson Chung | 3535df2 | 2017-01-17 18:43:39 -0800 | [diff] [blame] | 117 | if (!checkCurrentUserId(false /* debug */)) { |
| 118 | return; |
| 119 | } |
| 120 | |
Winson Chung | e6385a2 | 2017-05-02 18:15:16 -0700 | [diff] [blame] | 121 | mTouchHandler.getMotionHelper().expandPip(clearedTask /* skipAnimation */); |
Winson Chung | 929d4f7 | 2017-01-13 10:21:33 -0800 | [diff] [blame] | 122 | } |
| 123 | }; |
| 124 | |
| 125 | /** |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 126 | * Handler for messages from the PIP controller. |
| 127 | */ |
| 128 | private class PinnedStackListener extends IPinnedStackListener.Stub { |
| 129 | |
| 130 | @Override |
| 131 | public void onListenerRegistered(IPinnedStackController controller) { |
| 132 | mHandler.post(() -> { |
| 133 | mTouchHandler.setPinnedStackController(controller); |
| 134 | }); |
| 135 | } |
| 136 | |
| 137 | @Override |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 138 | public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) { |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 139 | mHandler.post(() -> { |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 140 | mTouchHandler.onImeVisibilityChanged(imeVisible, imeHeight); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 141 | }); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public void onMinimizedStateChanged(boolean isMinimized) { |
| 146 | mHandler.post(() -> { |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 147 | mTouchHandler.setMinimizedState(isMinimized, true /* fromController */); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 148 | }); |
| 149 | } |
| 150 | |
| 151 | @Override |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 152 | public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds, |
Winson Chung | ef4dc81 | 2017-04-11 13:31:44 -0700 | [diff] [blame] | 153 | Rect animatingBounds, boolean fromImeAdjustement, int displayRotation) { |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 154 | mHandler.post(() -> { |
Winson Chung | baa7b72 | 2017-03-03 21:33:44 -0800 | [diff] [blame] | 155 | mTouchHandler.onMovementBoundsChanged(insetBounds, normalBounds, animatingBounds, |
Winson Chung | ef4dc81 | 2017-04-11 13:31:44 -0700 | [diff] [blame] | 156 | fromImeAdjustement, displayRotation); |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 157 | }); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public void onActionsChanged(ParceledListSlice actions) { |
| 162 | mHandler.post(() -> { |
| 163 | mMenuController.setAppActions(actions); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 164 | }); |
| 165 | } |
| 166 | } |
| 167 | |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 168 | private PipManager() {} |
| 169 | |
| 170 | /** |
| 171 | * Initializes {@link PipManager}. |
| 172 | */ |
| 173 | public void initialize(Context context) { |
| 174 | mContext = context; |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 175 | mActivityManager = ActivityManager.getService(); |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 176 | mWindowManager = WindowManagerGlobal.getWindowManagerService(); |
| 177 | |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 178 | try { |
| 179 | mWindowManager.registerPinnedStackListener(DEFAULT_DISPLAY, mPinnedStackListener); |
| 180 | } catch (RemoteException e) { |
| 181 | Log.e(TAG, "Failed to register pinned stack listener", e); |
| 182 | } |
Winson Chung | 929d4f7 | 2017-01-13 10:21:33 -0800 | [diff] [blame] | 183 | SystemServicesProxy.getInstance(mContext).registerTaskStackListener(mTaskStackListener); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 184 | |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 185 | mInputConsumerController = new InputConsumerController(mWindowManager); |
Winson Chung | 97a60d9 | 2017-01-18 16:01:53 -0800 | [diff] [blame] | 186 | mMediaController = new PipMediaController(context, mActivityManager); |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 187 | mMenuController = new PipMenuActivityController(context, mActivityManager, mMediaController, |
| 188 | mInputConsumerController); |
| 189 | mTouchHandler = new PipTouchHandler(context, mActivityManager, mMenuController, |
| 190 | mInputConsumerController); |
Winson Chung | 664758c | 2017-03-28 21:03:52 -0700 | [diff] [blame] | 191 | mNotificationController = new PipNotificationController(context, mActivityManager, |
| 192 | mTouchHandler.getMotionHelper()); |
Winson Chung | b502690 | 2017-05-03 12:45:13 -0700 | [diff] [blame^] | 193 | EventBus.getDefault().register(this); |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Updates the PIP per configuration changed. |
| 198 | */ |
Winson Chung | 303c6b7 | 2016-10-24 17:12:49 -0700 | [diff] [blame] | 199 | public void onConfigurationChanged() { |
| 200 | mTouchHandler.onConfigurationChanged(); |
| 201 | } |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 202 | |
| 203 | /** |
Winson Chung | b502690 | 2017-05-03 12:45:13 -0700 | [diff] [blame^] | 204 | * Expands the PIP. |
| 205 | */ |
| 206 | public final void onBusEvent(ExpandPipEvent event) { |
| 207 | if (event.clearThumbnailWindows) { |
| 208 | try { |
| 209 | StackInfo stackInfo = mActivityManager.getStackInfo(PINNED_STACK_ID); |
| 210 | if (stackInfo != null && stackInfo.taskIds != null) { |
| 211 | SystemServicesProxy ssp = SystemServicesProxy.getInstance(mContext); |
| 212 | for (int taskId : stackInfo.taskIds) { |
| 213 | ssp.cancelThumbnailTransition(taskId); |
| 214 | } |
| 215 | } |
| 216 | } catch (RemoteException e) { |
| 217 | // Do nothing |
| 218 | } |
| 219 | } |
| 220 | mTouchHandler.getMotionHelper().expandPip(false /* skipAnimation */); |
| 221 | } |
| 222 | |
| 223 | /** |
Winson Chung | ac52f28 | 2017-03-30 14:44:52 -0700 | [diff] [blame] | 224 | * Sent from KEYCODE_WINDOW handler in PhoneWindowManager, to request the menu to be shown. |
| 225 | */ |
| 226 | public void showPictureInPictureMenu() { |
| 227 | mTouchHandler.showPictureInPictureMenu(); |
| 228 | } |
| 229 | |
| 230 | /** |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 231 | * Gets an instance of {@link PipManager}. |
| 232 | */ |
| 233 | public static PipManager getInstance() { |
| 234 | if (sPipController == null) { |
| 235 | sPipController = new PipManager(); |
| 236 | } |
| 237 | return sPipController; |
| 238 | } |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 239 | |
| 240 | public void dump(PrintWriter pw) { |
| 241 | final String innerPrefix = " "; |
| 242 | pw.println(TAG); |
Winson Chung | d2d9097 | 2017-02-28 11:40:41 -0800 | [diff] [blame] | 243 | mInputConsumerController.dump(pw, innerPrefix); |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame] | 244 | mMenuController.dump(pw, innerPrefix); |
| 245 | mTouchHandler.dump(pw, innerPrefix); |
| 246 | } |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 247 | } |