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