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 | 3535df2 | 2017-01-17 18:43:39 -0800 | [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 | 3535df2 | 2017-01-17 18:43:39 -0800 | [diff] [blame^] | 23 | import android.app.ActivityManager.StackInfo; |
| 24 | import android.app.ActivityOptions; |
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 | 3535df2 | 2017-01-17 18:43:39 -0800 | [diff] [blame^] | 28 | import android.content.Intent; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 29 | import android.content.pm.ParceledListSlice; |
| 30 | import android.os.Handler; |
| 31 | import android.os.RemoteException; |
Winson Chung | 3535df2 | 2017-01-17 18:43:39 -0800 | [diff] [blame^] | 32 | import android.os.UserHandle; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 33 | import android.util.Log; |
| 34 | import android.view.IPinnedStackController; |
| 35 | import android.view.IPinnedStackListener; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 36 | import android.view.IWindowManager; |
| 37 | import android.view.WindowManagerGlobal; |
| 38 | |
Winson Chung | 929d4f7 | 2017-01-13 10:21:33 -0800 | [diff] [blame] | 39 | import com.android.systemui.recents.misc.SystemServicesProxy; |
| 40 | import com.android.systemui.recents.misc.SystemServicesProxy.TaskStackListener; |
| 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 | */ |
| 45 | public class PipManager { |
| 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 | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 57 | private PipMenuActivityController mMenuController; |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 58 | private PipTouchHandler mTouchHandler; |
| 59 | |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 60 | /** |
Winson Chung | 929d4f7 | 2017-01-13 10:21:33 -0800 | [diff] [blame] | 61 | * Handler for system task stack changes. |
| 62 | */ |
| 63 | TaskStackListener mTaskStackListener = new TaskStackListener() { |
| 64 | @Override |
| 65 | public void onActivityPinned() { |
Winson Chung | 3535df2 | 2017-01-17 18:43:39 -0800 | [diff] [blame^] | 66 | if (!checkCurrentUserId(false /* debug */)) { |
| 67 | return; |
| 68 | } |
Winson Chung | 929d4f7 | 2017-01-13 10:21:33 -0800 | [diff] [blame] | 69 | mTouchHandler.onActivityPinned(); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void onPinnedStackAnimationEnded() { |
| 74 | // TODO(winsonc): Disable touch interaction with the PiP until the animation ends |
| 75 | } |
| 76 | |
| 77 | @Override |
Winson Chung | 3535df2 | 2017-01-17 18:43:39 -0800 | [diff] [blame^] | 78 | public void onPinnedActivityRestartAttempt(ComponentName sourceComponent) { |
| 79 | if (!checkCurrentUserId(false /* debug */)) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | // Expand the activity back to fullscreen only if it was attempted to be restarted from |
| 84 | // another package than the top activity in the stack |
| 85 | boolean expandPipToFullscreen = true; |
| 86 | if (sourceComponent != null) { |
| 87 | try { |
| 88 | StackInfo pinnedStackInfo = mActivityManager.getStackInfo(PINNED_STACK_ID); |
| 89 | if (pinnedStackInfo != null && pinnedStackInfo.taskIds != null && |
| 90 | pinnedStackInfo.taskIds.length > 0) { |
| 91 | expandPipToFullscreen = |
| 92 | !pinnedStackInfo.topActivity.getPackageName().equals( |
| 93 | sourceComponent.getPackageName()); |
| 94 | } |
| 95 | } catch (RemoteException e) { |
| 96 | Log.w(TAG, "Unable to get pinned stack."); |
| 97 | } |
| 98 | } |
| 99 | if (expandPipToFullscreen) { |
| 100 | mTouchHandler.expandPinnedStackToFullscreen(); |
| 101 | } else { |
| 102 | Log.w(TAG, "Can not expand PiP to fullscreen via intent from the same package."); |
| 103 | } |
Winson Chung | 929d4f7 | 2017-01-13 10:21:33 -0800 | [diff] [blame] | 104 | } |
| 105 | }; |
| 106 | |
| 107 | /** |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 108 | * Handler for messages from the PIP controller. |
| 109 | */ |
| 110 | private class PinnedStackListener extends IPinnedStackListener.Stub { |
| 111 | |
| 112 | @Override |
| 113 | public void onListenerRegistered(IPinnedStackController controller) { |
| 114 | mHandler.post(() -> { |
| 115 | mTouchHandler.setPinnedStackController(controller); |
| 116 | }); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void onBoundsChanged(boolean adjustedForIme) { |
| 121 | // Do nothing |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void onActionsChanged(ParceledListSlice actions) { |
| 126 | mHandler.post(() -> { |
| 127 | mMenuController.setActions(actions); |
| 128 | }); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void onMinimizedStateChanged(boolean isMinimized) { |
| 133 | mHandler.post(() -> { |
| 134 | mTouchHandler.onMinimizedStateChanged(isMinimized); |
| 135 | }); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void onSnapToEdgeStateChanged(boolean isSnapToEdge) { |
| 140 | mHandler.post(() -> { |
| 141 | mTouchHandler.onSnapToEdgeStateChanged(isSnapToEdge); |
| 142 | }); |
| 143 | } |
| 144 | } |
| 145 | |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 146 | private PipManager() {} |
| 147 | |
| 148 | /** |
| 149 | * Initializes {@link PipManager}. |
| 150 | */ |
| 151 | public void initialize(Context context) { |
| 152 | mContext = context; |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 153 | mActivityManager = ActivityManager.getService(); |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 154 | mWindowManager = WindowManagerGlobal.getWindowManagerService(); |
| 155 | |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 156 | try { |
| 157 | mWindowManager.registerPinnedStackListener(DEFAULT_DISPLAY, mPinnedStackListener); |
| 158 | } catch (RemoteException e) { |
| 159 | Log.e(TAG, "Failed to register pinned stack listener", e); |
| 160 | } |
Winson Chung | 929d4f7 | 2017-01-13 10:21:33 -0800 | [diff] [blame] | 161 | SystemServicesProxy.getInstance(mContext).registerTaskStackListener(mTaskStackListener); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 162 | |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 163 | mMenuController = new PipMenuActivityController(context, mActivityManager, mWindowManager); |
| 164 | mTouchHandler = new PipTouchHandler(context, mMenuController, mActivityManager, |
| 165 | mWindowManager); |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Updates the PIP per configuration changed. |
| 170 | */ |
Winson Chung | 303c6b7 | 2016-10-24 17:12:49 -0700 | [diff] [blame] | 171 | public void onConfigurationChanged() { |
| 172 | mTouchHandler.onConfigurationChanged(); |
| 173 | } |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 174 | |
| 175 | /** |
| 176 | * Gets an instance of {@link PipManager}. |
| 177 | */ |
| 178 | public static PipManager getInstance() { |
| 179 | if (sPipController == null) { |
| 180 | sPipController = new PipManager(); |
| 181 | } |
| 182 | return sPipController; |
| 183 | } |
| 184 | } |