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 | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 56 | private PipMenuActivityController mMenuController; |
Winson Chung | 97a60d9 | 2017-01-18 16:01:53 -0800 | [diff] [blame] | 57 | private PipMediaController mMediaController; |
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(); |
Winson Chung | 97a60d9 | 2017-01-18 16:01:53 -0800 | [diff] [blame] | 70 | mMediaController.onActivityPinned(); |
Winson Chung | 929d4f7 | 2017-01-13 10:21:33 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void onPinnedStackAnimationEnded() { |
| 75 | // TODO(winsonc): Disable touch interaction with the PiP until the animation ends |
| 76 | } |
| 77 | |
| 78 | @Override |
Winson Chung | 3535df2 | 2017-01-17 18:43:39 -0800 | [diff] [blame] | 79 | public void onPinnedActivityRestartAttempt(ComponentName sourceComponent) { |
| 80 | if (!checkCurrentUserId(false /* debug */)) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | // Expand the activity back to fullscreen only if it was attempted to be restarted from |
| 85 | // another package than the top activity in the stack |
| 86 | boolean expandPipToFullscreen = true; |
| 87 | if (sourceComponent != null) { |
Winson Chung | 734c9c0 | 2017-01-23 19:08:37 -0800 | [diff] [blame] | 88 | ComponentName topActivity = PipUtils.getTopPinnedActivity(mContext, |
| 89 | mActivityManager); |
| 90 | if (topActivity != null && topActivity.getPackageName().equals( |
| 91 | sourceComponent.getPackageName())) { |
| 92 | expandPipToFullscreen = false; |
| 93 | } |
Winson Chung | 3535df2 | 2017-01-17 18:43:39 -0800 | [diff] [blame] | 94 | } |
| 95 | if (expandPipToFullscreen) { |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 96 | mTouchHandler.getMotionHelper().expandPip(); |
Winson Chung | 3535df2 | 2017-01-17 18:43:39 -0800 | [diff] [blame] | 97 | } else { |
| 98 | Log.w(TAG, "Can not expand PiP to fullscreen via intent from the same package."); |
| 99 | } |
Winson Chung | 929d4f7 | 2017-01-13 10:21:33 -0800 | [diff] [blame] | 100 | } |
| 101 | }; |
| 102 | |
| 103 | /** |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 104 | * Handler for messages from the PIP controller. |
| 105 | */ |
| 106 | private class PinnedStackListener extends IPinnedStackListener.Stub { |
| 107 | |
| 108 | @Override |
| 109 | public void onListenerRegistered(IPinnedStackController controller) { |
| 110 | mHandler.post(() -> { |
| 111 | mTouchHandler.setPinnedStackController(controller); |
| 112 | }); |
| 113 | } |
| 114 | |
| 115 | @Override |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 116 | public void onImeVisibilityChanged(boolean imeVisible, int imeHeight) { |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 117 | mHandler.post(() -> { |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 118 | mTouchHandler.onImeVisibilityChanged(imeVisible, imeHeight); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 119 | }); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void onMinimizedStateChanged(boolean isMinimized) { |
| 124 | mHandler.post(() -> { |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 125 | mTouchHandler.setMinimizedState(isMinimized, true /* fromController */); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 126 | }); |
| 127 | } |
| 128 | |
| 129 | @Override |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 130 | public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds, |
| 131 | boolean fromImeAdjustement) { |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 132 | mHandler.post(() -> { |
Winson Chung | 2a82fe5 | 2017-02-02 14:43:34 -0800 | [diff] [blame] | 133 | mTouchHandler.onMovementBoundsChanged(insetBounds, normalBounds, fromImeAdjustement); |
| 134 | }); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void onActionsChanged(ParceledListSlice actions) { |
| 139 | mHandler.post(() -> { |
| 140 | mMenuController.setAppActions(actions); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 141 | }); |
| 142 | } |
| 143 | } |
| 144 | |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 145 | private PipManager() {} |
| 146 | |
| 147 | /** |
| 148 | * Initializes {@link PipManager}. |
| 149 | */ |
| 150 | public void initialize(Context context) { |
| 151 | mContext = context; |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 152 | mActivityManager = ActivityManager.getService(); |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 153 | mWindowManager = WindowManagerGlobal.getWindowManagerService(); |
| 154 | |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 155 | try { |
| 156 | mWindowManager.registerPinnedStackListener(DEFAULT_DISPLAY, mPinnedStackListener); |
| 157 | } catch (RemoteException e) { |
| 158 | Log.e(TAG, "Failed to register pinned stack listener", e); |
| 159 | } |
Winson Chung | 929d4f7 | 2017-01-13 10:21:33 -0800 | [diff] [blame] | 160 | SystemServicesProxy.getInstance(mContext).registerTaskStackListener(mTaskStackListener); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 161 | |
Winson Chung | 97a60d9 | 2017-01-18 16:01:53 -0800 | [diff] [blame] | 162 | mMediaController = new PipMediaController(context, mActivityManager); |
| 163 | mMenuController = new PipMenuActivityController(context, mActivityManager, mWindowManager, |
| 164 | mMediaController); |
Winson Chung | 15504af | 2016-11-02 18:11:36 -0700 | [diff] [blame] | 165 | mTouchHandler = new PipTouchHandler(context, mMenuController, mActivityManager, |
| 166 | mWindowManager); |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Updates the PIP per configuration changed. |
| 171 | */ |
Winson Chung | 303c6b7 | 2016-10-24 17:12:49 -0700 | [diff] [blame] | 172 | public void onConfigurationChanged() { |
| 173 | mTouchHandler.onConfigurationChanged(); |
| 174 | } |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 175 | |
| 176 | /** |
| 177 | * Gets an instance of {@link PipManager}. |
| 178 | */ |
| 179 | public static PipManager getInstance() { |
| 180 | if (sPipController == null) { |
| 181 | sPipController = new PipManager(); |
| 182 | } |
| 183 | return sPipController; |
| 184 | } |
Winson Chung | 29a7865 | 2017-02-09 18:35:26 -0800 | [diff] [blame^] | 185 | |
| 186 | public void dump(PrintWriter pw) { |
| 187 | final String innerPrefix = " "; |
| 188 | pw.println(TAG); |
| 189 | mMenuController.dump(pw, innerPrefix); |
| 190 | mTouchHandler.dump(pw, innerPrefix); |
| 191 | } |
Winson | 73bc159 | 2016-10-18 18:47:43 -0700 | [diff] [blame] | 192 | } |