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