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