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