Evan Rosky | af9f27c | 2020-02-18 18:58:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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.stackdivider; |
| 18 | |
| 19 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME; |
| 20 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_RECENTS; |
| 21 | import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED; |
| 22 | import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY; |
| 23 | import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY; |
| 24 | import static android.view.Display.DEFAULT_DISPLAY; |
| 25 | |
| 26 | import android.app.ActivityManager.RunningTaskInfo; |
Evan Rosky | af9f27c | 2020-02-18 18:58:35 +0000 | [diff] [blame] | 27 | import android.app.WindowConfiguration; |
Louis Chang | a009c76 | 2020-02-26 11:21:31 +0800 | [diff] [blame] | 28 | import android.graphics.Rect; |
Evan Rosky | af9f27c | 2020-02-18 18:58:35 +0000 | [diff] [blame] | 29 | import android.os.RemoteException; |
| 30 | import android.util.Log; |
| 31 | import android.view.Display; |
Evan Rosky | af9f27c | 2020-02-18 18:58:35 +0000 | [diff] [blame] | 32 | import android.view.SurfaceControl; |
| 33 | import android.view.SurfaceSession; |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 34 | import android.window.TaskOrganizer; |
Louis Chang | a009c76 | 2020-02-26 11:21:31 +0800 | [diff] [blame] | 35 | |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 36 | class SplitScreenTaskOrganizer extends TaskOrganizer { |
Evan Rosky | a5858ed | 2020-04-21 18:47:37 -0700 | [diff] [blame] | 37 | private static final String TAG = "SplitScreenTaskOrg"; |
Evan Rosky | af9f27c | 2020-02-18 18:58:35 +0000 | [diff] [blame] | 38 | private static final boolean DEBUG = Divider.DEBUG; |
| 39 | |
| 40 | RunningTaskInfo mPrimary; |
| 41 | RunningTaskInfo mSecondary; |
| 42 | SurfaceControl mPrimarySurface; |
| 43 | SurfaceControl mSecondarySurface; |
| 44 | SurfaceControl mPrimaryDim; |
| 45 | SurfaceControl mSecondaryDim; |
Louis Chang | a009c76 | 2020-02-26 11:21:31 +0800 | [diff] [blame] | 46 | Rect mHomeBounds = new Rect(); |
Evan Rosky | af9f27c | 2020-02-18 18:58:35 +0000 | [diff] [blame] | 47 | final Divider mDivider; |
Evan Rosky | b8540a0 | 2020-03-25 16:30:24 -0700 | [diff] [blame] | 48 | private boolean mSplitScreenSupported = false; |
Evan Rosky | af9f27c | 2020-02-18 18:58:35 +0000 | [diff] [blame] | 49 | |
chaviw | da7b3c2 | 2020-04-24 11:25:08 -0700 | [diff] [blame] | 50 | final SurfaceSession mSurfaceSession = new SurfaceSession(); |
| 51 | |
Evan Rosky | af9f27c | 2020-02-18 18:58:35 +0000 | [diff] [blame] | 52 | SplitScreenTaskOrganizer(Divider divider) { |
| 53 | mDivider = divider; |
| 54 | } |
| 55 | |
chaviw | da7b3c2 | 2020-04-24 11:25:08 -0700 | [diff] [blame] | 56 | void init() throws RemoteException { |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 57 | registerOrganizer(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY); |
| 58 | registerOrganizer(WINDOWING_MODE_SPLIT_SCREEN_SECONDARY); |
chaviw | da7b3c2 | 2020-04-24 11:25:08 -0700 | [diff] [blame] | 59 | synchronized (this) { |
| 60 | try { |
| 61 | mPrimary = TaskOrganizer.createRootTask(Display.DEFAULT_DISPLAY, |
| 62 | WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY); |
| 63 | mSecondary = TaskOrganizer.createRootTask(Display.DEFAULT_DISPLAY, |
| 64 | WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_SECONDARY); |
| 65 | } catch (Exception e) { |
| 66 | // teardown to prevent callbacks |
| 67 | unregisterOrganizer(); |
| 68 | throw e; |
| 69 | } |
Evan Rosky | b8540a0 | 2020-03-25 16:30:24 -0700 | [diff] [blame] | 70 | } |
Evan Rosky | af9f27c | 2020-02-18 18:58:35 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Evan Rosky | b8540a0 | 2020-03-25 16:30:24 -0700 | [diff] [blame] | 73 | boolean isSplitScreenSupported() { |
| 74 | return mSplitScreenSupported; |
| 75 | } |
| 76 | |
Evan Rosky | af9f27c | 2020-02-18 18:58:35 +0000 | [diff] [blame] | 77 | SurfaceControl.Transaction getTransaction() { |
| 78 | return mDivider.mTransactionPool.acquire(); |
| 79 | } |
| 80 | |
| 81 | void releaseTransaction(SurfaceControl.Transaction t) { |
| 82 | mDivider.mTransactionPool.release(t); |
| 83 | } |
| 84 | |
| 85 | @Override |
chaviw | 7de5000 | 2020-04-27 12:33:30 -0700 | [diff] [blame] | 86 | public void onTaskAppeared(RunningTaskInfo taskInfo, SurfaceControl leash) { |
chaviw | da7b3c2 | 2020-04-24 11:25:08 -0700 | [diff] [blame] | 87 | synchronized (this) { |
| 88 | if (mPrimary == null || mSecondary == null) { |
| 89 | Log.w(TAG, "Received onTaskAppeared before creating root tasks " + taskInfo); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | if (taskInfo.token.equals(mPrimary.token)) { |
chaviw | 7de5000 | 2020-04-27 12:33:30 -0700 | [diff] [blame] | 94 | mPrimarySurface = leash; |
chaviw | da7b3c2 | 2020-04-24 11:25:08 -0700 | [diff] [blame] | 95 | } else if (taskInfo.token.equals(mSecondary.token)) { |
chaviw | 7de5000 | 2020-04-27 12:33:30 -0700 | [diff] [blame] | 96 | mSecondarySurface = leash; |
chaviw | da7b3c2 | 2020-04-24 11:25:08 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | if (!mSplitScreenSupported && mPrimarySurface != null && mSecondarySurface != null) { |
| 100 | mSplitScreenSupported = true; |
| 101 | |
| 102 | // Initialize dim surfaces: |
| 103 | mPrimaryDim = new SurfaceControl.Builder(mSurfaceSession) |
| 104 | .setParent(mPrimarySurface).setColorLayer() |
| 105 | .setName("Primary Divider Dim").build(); |
| 106 | mSecondaryDim = new SurfaceControl.Builder(mSurfaceSession) |
| 107 | .setParent(mSecondarySurface).setColorLayer() |
| 108 | .setName("Secondary Divider Dim").build(); |
| 109 | SurfaceControl.Transaction t = getTransaction(); |
| 110 | t.setLayer(mPrimaryDim, Integer.MAX_VALUE); |
| 111 | t.setColor(mPrimaryDim, new float[]{0f, 0f, 0f}); |
| 112 | t.setLayer(mSecondaryDim, Integer.MAX_VALUE); |
| 113 | t.setColor(mSecondaryDim, new float[]{0f, 0f, 0f}); |
| 114 | t.apply(); |
| 115 | releaseTransaction(t); |
Tony Huang | 3dd2b93 | 2020-06-15 05:40:40 +0000 | [diff] [blame] | 116 | |
| 117 | mDivider.onTasksReady(); |
chaviw | da7b3c2 | 2020-04-24 11:25:08 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void onTaskVanished(RunningTaskInfo taskInfo) { |
| 124 | synchronized (this) { |
| 125 | final boolean isPrimaryTask = mPrimary != null |
| 126 | && taskInfo.token.equals(mPrimary.token); |
| 127 | final boolean isSecondaryTask = mSecondary != null |
| 128 | && taskInfo.token.equals(mSecondary.token); |
| 129 | |
| 130 | if (mSplitScreenSupported && (isPrimaryTask || isSecondaryTask)) { |
| 131 | mSplitScreenSupported = false; |
| 132 | |
| 133 | SurfaceControl.Transaction t = getTransaction(); |
| 134 | t.remove(mPrimaryDim); |
| 135 | t.remove(mSecondaryDim); |
| 136 | t.remove(mPrimarySurface); |
| 137 | t.remove(mSecondarySurface); |
| 138 | t.apply(); |
| 139 | releaseTransaction(t); |
| 140 | |
| 141 | mDivider.onTaskVanished(); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | @Override |
Evan Rosky | af9f27c | 2020-02-18 18:58:35 +0000 | [diff] [blame] | 147 | public void onTaskInfoChanged(RunningTaskInfo taskInfo) { |
| 148 | if (taskInfo.displayId != DEFAULT_DISPLAY) { |
| 149 | return; |
| 150 | } |
| 151 | mDivider.getHandler().post(() -> handleTaskInfoChanged(taskInfo)); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * This is effectively a finite state machine which moves between the various split-screen |
| 156 | * presentations based on the contents of the split regions. |
| 157 | */ |
| 158 | private void handleTaskInfoChanged(RunningTaskInfo info) { |
Evan Rosky | a5858ed | 2020-04-21 18:47:37 -0700 | [diff] [blame] | 159 | if (!mSplitScreenSupported) { |
| 160 | // This shouldn't happen; but apparently there is a chance that SysUI crashes without |
| 161 | // system server receiving binder-death (or maybe it receives binder-death too late?). |
| 162 | // In this situation, when sys-ui restarts, the split root-tasks will still exist so |
| 163 | // there is a small window of time during init() where WM might send messages here |
| 164 | // before init() fails. So, avoid a cycle of crashes by returning early. |
| 165 | Log.e(TAG, "Got handleTaskInfoChanged when not initialized: " + info); |
| 166 | return; |
| 167 | } |
Evan Rosky | 89c285e | 2020-06-17 21:21:53 -0700 | [diff] [blame] | 168 | final boolean secondaryImpliedMinimize = mSecondary.topActivityType == ACTIVITY_TYPE_HOME |
| 169 | || (mSecondary.topActivityType == ACTIVITY_TYPE_RECENTS |
| 170 | && mDivider.isHomeStackResizable()); |
Evan Rosky | af9f27c | 2020-02-18 18:58:35 +0000 | [diff] [blame] | 171 | final boolean primaryWasEmpty = mPrimary.topActivityType == ACTIVITY_TYPE_UNDEFINED; |
| 172 | final boolean secondaryWasEmpty = mSecondary.topActivityType == ACTIVITY_TYPE_UNDEFINED; |
| 173 | if (info.token.asBinder() == mPrimary.token.asBinder()) { |
| 174 | mPrimary = info; |
| 175 | } else if (info.token.asBinder() == mSecondary.token.asBinder()) { |
| 176 | mSecondary = info; |
| 177 | } |
| 178 | final boolean primaryIsEmpty = mPrimary.topActivityType == ACTIVITY_TYPE_UNDEFINED; |
| 179 | final boolean secondaryIsEmpty = mSecondary.topActivityType == ACTIVITY_TYPE_UNDEFINED; |
Evan Rosky | 89c285e | 2020-06-17 21:21:53 -0700 | [diff] [blame] | 180 | final boolean secondaryImpliesMinimize = mSecondary.topActivityType == ACTIVITY_TYPE_HOME |
| 181 | || (mSecondary.topActivityType == ACTIVITY_TYPE_RECENTS |
| 182 | && mDivider.isHomeStackResizable()); |
Evan Rosky | af9f27c | 2020-02-18 18:58:35 +0000 | [diff] [blame] | 183 | if (DEBUG) { |
| 184 | Log.d(TAG, "onTaskInfoChanged " + mPrimary + " " + mSecondary); |
| 185 | } |
Evan Rosky | f64f5da | 2020-03-16 13:47:48 -0700 | [diff] [blame] | 186 | if (primaryIsEmpty == primaryWasEmpty && secondaryWasEmpty == secondaryIsEmpty |
Evan Rosky | 89c285e | 2020-06-17 21:21:53 -0700 | [diff] [blame] | 187 | && secondaryImpliedMinimize == secondaryImpliesMinimize) { |
Evan Rosky | f64f5da | 2020-03-16 13:47:48 -0700 | [diff] [blame] | 188 | // No relevant changes |
| 189 | return; |
| 190 | } |
Evan Rosky | af9f27c | 2020-02-18 18:58:35 +0000 | [diff] [blame] | 191 | if (primaryIsEmpty || secondaryIsEmpty) { |
| 192 | // At-least one of the splits is empty which means we are currently transitioning |
| 193 | // into or out-of split-screen mode. |
| 194 | if (DEBUG) { |
| 195 | Log.d(TAG, " at-least one split empty " + mPrimary.topActivityType |
| 196 | + " " + mSecondary.topActivityType); |
| 197 | } |
Evan Rosky | 90d5b6d | 2020-05-13 19:39:05 -0700 | [diff] [blame] | 198 | if (mDivider.isDividerVisible()) { |
Evan Rosky | af9f27c | 2020-02-18 18:58:35 +0000 | [diff] [blame] | 199 | // Was in split-mode, which means we are leaving split, so continue that. |
| 200 | // This happens when the stack in the primary-split is dismissed. |
| 201 | if (DEBUG) { |
| 202 | Log.d(TAG, " was in split, so this means leave it " |
| 203 | + mPrimary.topActivityType + " " + mSecondary.topActivityType); |
| 204 | } |
Evan Rosky | 3613854 | 2020-05-01 18:02:11 -0700 | [diff] [blame] | 205 | mDivider.startDismissSplit(); |
Evan Rosky | af9f27c | 2020-02-18 18:58:35 +0000 | [diff] [blame] | 206 | } else if (!primaryIsEmpty && primaryWasEmpty && secondaryWasEmpty) { |
| 207 | // Wasn't in split-mode (both were empty), but now that the primary split is |
| 208 | // populated, we should fully enter split by moving everything else into secondary. |
| 209 | // This just tells window-manager to reparent things, the UI will respond |
| 210 | // when it gets new task info for the secondary split. |
| 211 | if (DEBUG) { |
| 212 | Log.d(TAG, " was not in split, but primary is populated, so enter it"); |
| 213 | } |
| 214 | mDivider.startEnterSplit(); |
| 215 | } |
Evan Rosky | 89c285e | 2020-06-17 21:21:53 -0700 | [diff] [blame] | 216 | } else if (secondaryImpliesMinimize) { |
Evan Rosky | af9f27c | 2020-02-18 18:58:35 +0000 | [diff] [blame] | 217 | // Both splits are populated but the secondary split has a home/recents stack on top, |
| 218 | // so enter minimized mode. |
| 219 | mDivider.ensureMinimizedSplit(); |
| 220 | } else { |
| 221 | // Both splits are populated by normal activities, so make sure we aren't minimized. |
| 222 | mDivider.ensureNormalSplit(); |
| 223 | } |
| 224 | } |
| 225 | } |