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