Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2017 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 android.app; |
| 18 | |
chaviw | da4c694 | 2018-11-07 15:52:56 -0800 | [diff] [blame] | 19 | import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_DESTROY_CONTENT_ON_REMOVAL; |
| 20 | import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY; |
| 21 | import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC; |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 22 | import static android.view.Display.INVALID_DISPLAY; |
chaviw | da4c694 | 2018-11-07 15:52:56 -0800 | [diff] [blame] | 23 | |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 24 | import android.annotation.NonNull; |
Wale Ogunwale | 691af68 | 2019-02-11 03:09:10 -0800 | [diff] [blame] | 25 | import android.annotation.TestApi; |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 26 | import android.app.ActivityManager.StackInfo; |
Mark Renouf | 7b71a5d | 2019-01-23 17:12:34 -0500 | [diff] [blame] | 27 | import android.content.ComponentName; |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
Issei Suzuki | a5dbf52 | 2019-02-01 17:58:15 +0100 | [diff] [blame] | 30 | import android.graphics.Insets; |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 31 | import android.graphics.Matrix; |
Tiger Huang | 2b210c2 | 2019-03-18 21:21:26 +0800 | [diff] [blame] | 32 | import android.graphics.Region; |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 33 | import android.hardware.display.DisplayManager; |
| 34 | import android.hardware.display.VirtualDisplay; |
Mark Renouf | 041d726 | 2019-02-06 12:09:41 -0500 | [diff] [blame] | 35 | import android.hardware.input.InputManager; |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 36 | import android.os.RemoteException; |
Mark Renouf | 041d726 | 2019-02-06 12:09:41 -0500 | [diff] [blame] | 37 | import android.os.SystemClock; |
Brad Stenning | a1dbe9c | 2018-05-02 08:29:28 -0700 | [diff] [blame] | 38 | import android.os.UserHandle; |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 39 | import android.util.AttributeSet; |
| 40 | import android.util.DisplayMetrics; |
| 41 | import android.util.Log; |
Andrii Kulian | f0379de | 2018-03-14 16:24:07 -0700 | [diff] [blame] | 42 | import android.view.IWindowManager; |
Mark Renouf | 041d726 | 2019-02-06 12:09:41 -0500 | [diff] [blame] | 43 | import android.view.InputDevice; |
| 44 | import android.view.KeyCharacterMap; |
| 45 | import android.view.KeyEvent; |
chaviw | ff2e7d8 | 2018-11-02 11:11:27 -0700 | [diff] [blame] | 46 | import android.view.SurfaceControl; |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 47 | import android.view.SurfaceHolder; |
chaviw | ff2e7d8 | 2018-11-02 11:11:27 -0700 | [diff] [blame] | 48 | import android.view.SurfaceSession; |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 49 | import android.view.SurfaceView; |
chaviw | d44449d | 2019-01-02 16:33:12 -0800 | [diff] [blame] | 50 | import android.view.View; |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 51 | import android.view.ViewGroup; |
Tiger Huang | 2b210c2 | 2019-03-18 21:21:26 +0800 | [diff] [blame] | 52 | import android.view.ViewParent; |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 53 | import android.view.WindowManager; |
Andrii Kulian | 4b6599e | 2018-01-15 17:24:08 -0800 | [diff] [blame] | 54 | import android.view.WindowManagerGlobal; |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 55 | import android.view.inputmethod.InputMethodManager; |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 56 | |
| 57 | import dalvik.system.CloseGuard; |
| 58 | |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 59 | import java.util.List; |
| 60 | |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 61 | /** |
Tiger Huang | 04dc4cc | 2019-01-17 18:41:41 +0800 | [diff] [blame] | 62 | * Activity container that allows launching activities into itself. |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 63 | * <p>Activity launching into this container is restricted by the same rules that apply to launching |
| 64 | * on VirtualDisplays. |
| 65 | * @hide |
| 66 | */ |
Wale Ogunwale | 691af68 | 2019-02-11 03:09:10 -0800 | [diff] [blame] | 67 | @TestApi |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 68 | public class ActivityView extends ViewGroup { |
| 69 | |
| 70 | private static final String DISPLAY_NAME = "ActivityViewVirtualDisplay"; |
| 71 | private static final String TAG = "ActivityView"; |
| 72 | |
| 73 | private VirtualDisplay mVirtualDisplay; |
| 74 | private final SurfaceView mSurfaceView; |
chaviw | ff2e7d8 | 2018-11-02 11:11:27 -0700 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * This is the root surface for the VirtualDisplay. The VirtualDisplay child surfaces will be |
| 78 | * re-parented to this surface. This will also be a child of the SurfaceView's SurfaceControl. |
| 79 | */ |
| 80 | private SurfaceControl mRootSurfaceControl; |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 81 | |
| 82 | private final SurfaceCallback mSurfaceCallback; |
| 83 | private StateCallback mActivityViewCallback; |
| 84 | |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 85 | private IActivityTaskManager mActivityTaskManager; |
Tiger Huang | 04dc4cc | 2019-01-17 18:41:41 +0800 | [diff] [blame] | 86 | // Temp container to store view coordinates in window. |
| 87 | private final int[] mLocationInWindow = new int[2]; |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 88 | |
Tiger Huang | 2b210c2 | 2019-03-18 21:21:26 +0800 | [diff] [blame] | 89 | // The latest tap exclude region that we've sent to WM. |
| 90 | private final Region mTapExcludeRegion = new Region(); |
| 91 | |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 92 | private TaskStackListener mTaskStackListener; |
| 93 | |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 94 | private final CloseGuard mGuard = CloseGuard.get(); |
| 95 | private boolean mOpened; // Protected by mGuard. |
| 96 | |
chaviw | ff2e7d8 | 2018-11-02 11:11:27 -0700 | [diff] [blame] | 97 | private final SurfaceControl.Transaction mTmpTransaction = new SurfaceControl.Transaction(); |
chaviw | ff2e7d8 | 2018-11-02 11:11:27 -0700 | [diff] [blame] | 98 | |
Wale Ogunwale | 9e737db | 2018-12-17 15:42:37 -0800 | [diff] [blame] | 99 | /** The ActivityView is only allowed to contain one task. */ |
| 100 | private final boolean mSingleTaskInstance; |
| 101 | |
Issei Suzuki | a5dbf52 | 2019-02-01 17:58:15 +0100 | [diff] [blame] | 102 | private Insets mForwardedInsets; |
| 103 | |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 104 | public ActivityView(Context context) { |
| 105 | this(context, null /* attrs */); |
| 106 | } |
| 107 | |
| 108 | public ActivityView(Context context, AttributeSet attrs) { |
| 109 | this(context, attrs, 0 /* defStyle */); |
| 110 | } |
| 111 | |
| 112 | public ActivityView(Context context, AttributeSet attrs, int defStyle) { |
Wale Ogunwale | 9e737db | 2018-12-17 15:42:37 -0800 | [diff] [blame] | 113 | this(context, attrs, defStyle, false /*singleTaskInstance*/); |
| 114 | } |
| 115 | |
| 116 | public ActivityView( |
| 117 | Context context, AttributeSet attrs, int defStyle, boolean singleTaskInstance) { |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 118 | super(context, attrs, defStyle); |
Wale Ogunwale | 9e737db | 2018-12-17 15:42:37 -0800 | [diff] [blame] | 119 | mSingleTaskInstance = singleTaskInstance; |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 120 | |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 121 | mActivityTaskManager = ActivityTaskManager.getService(); |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 122 | mSurfaceView = new SurfaceView(context); |
| 123 | mSurfaceCallback = new SurfaceCallback(); |
| 124 | mSurfaceView.getHolder().addCallback(mSurfaceCallback); |
| 125 | addView(mSurfaceView); |
| 126 | |
| 127 | mOpened = true; |
| 128 | mGuard.open("release"); |
| 129 | } |
| 130 | |
| 131 | /** Callback that notifies when the container is ready or destroyed. */ |
| 132 | public abstract static class StateCallback { |
Mark Renouf | 7b71a5d | 2019-01-23 17:12:34 -0500 | [diff] [blame] | 133 | |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 134 | /** |
| 135 | * Called when the container is ready for launching activities. Calling |
| 136 | * {@link #startActivity(Intent)} prior to this callback will result in an |
| 137 | * {@link IllegalStateException}. |
| 138 | * |
| 139 | * @see #startActivity(Intent) |
| 140 | */ |
| 141 | public abstract void onActivityViewReady(ActivityView view); |
Mark Renouf | 7b71a5d | 2019-01-23 17:12:34 -0500 | [diff] [blame] | 142 | |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 143 | /** |
| 144 | * Called when the container can no longer launch activities. Calling |
| 145 | * {@link #startActivity(Intent)} after this callback will result in an |
| 146 | * {@link IllegalStateException}. |
| 147 | * |
| 148 | * @see #startActivity(Intent) |
| 149 | */ |
| 150 | public abstract void onActivityViewDestroyed(ActivityView view); |
Mark Renouf | 7b71a5d | 2019-01-23 17:12:34 -0500 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * Called when a task is created inside the container. |
| 154 | * This is a filtered version of {@link TaskStackListener} |
| 155 | */ |
| 156 | public void onTaskCreated(int taskId, ComponentName componentName) { } |
| 157 | |
Brad Stenning | a1dbe9c | 2018-05-02 08:29:28 -0700 | [diff] [blame] | 158 | /** |
| 159 | * Called when a task is moved to the front of the stack inside the container. |
| 160 | * This is a filtered version of {@link TaskStackListener} |
| 161 | */ |
Wale Ogunwale | 691af68 | 2019-02-11 03:09:10 -0800 | [diff] [blame] | 162 | public void onTaskMovedToFront(int taskId) { } |
Mark Renouf | 7b71a5d | 2019-01-23 17:12:34 -0500 | [diff] [blame] | 163 | |
| 164 | /** |
| 165 | * Called when a task is about to be removed from the stack inside the container. |
| 166 | * This is a filtered version of {@link TaskStackListener} |
| 167 | */ |
| 168 | public void onTaskRemovalStarted(int taskId) { } |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Set the callback to be notified about state changes. |
| 173 | * <p>This class must finish initializing before {@link #startActivity(Intent)} can be called. |
| 174 | * <p>Note: If the instance was ready prior to this call being made, then |
| 175 | * {@link StateCallback#onActivityViewReady(ActivityView)} will be called from within |
| 176 | * this method call. |
| 177 | * |
| 178 | * @param callback The callback to report events to. |
| 179 | * |
| 180 | * @see StateCallback |
| 181 | * @see #startActivity(Intent) |
| 182 | */ |
| 183 | public void setCallback(StateCallback callback) { |
| 184 | mActivityViewCallback = callback; |
| 185 | |
| 186 | if (mVirtualDisplay != null && mActivityViewCallback != null) { |
| 187 | mActivityViewCallback.onActivityViewReady(this); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Launch a new activity into this container. |
| 193 | * <p>Activity resolved by the provided {@link Intent} must have |
| 194 | * {@link android.R.attr#resizeableActivity} attribute set to {@code true} in order to be |
| 195 | * launched here. Also, if activity is not owned by the owner of this container, it must allow |
| 196 | * embedding and the caller must have permission to embed. |
| 197 | * <p>Note: This class must finish initializing and |
| 198 | * {@link StateCallback#onActivityViewReady(ActivityView)} callback must be triggered before |
| 199 | * this method can be called. |
| 200 | * |
| 201 | * @param intent Intent used to launch an activity. |
| 202 | * |
| 203 | * @see StateCallback |
| 204 | * @see #startActivity(PendingIntent) |
| 205 | */ |
| 206 | public void startActivity(@NonNull Intent intent) { |
| 207 | final ActivityOptions options = prepareActivityOptions(); |
| 208 | getContext().startActivity(intent, options.toBundle()); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Launch a new activity into this container. |
Brad Stenning | a1dbe9c | 2018-05-02 08:29:28 -0700 | [diff] [blame] | 213 | * <p>Activity resolved by the provided {@link Intent} must have |
| 214 | * {@link android.R.attr#resizeableActivity} attribute set to {@code true} in order to be |
| 215 | * launched here. Also, if activity is not owned by the owner of this container, it must allow |
| 216 | * embedding and the caller must have permission to embed. |
| 217 | * <p>Note: This class must finish initializing and |
| 218 | * {@link StateCallback#onActivityViewReady(ActivityView)} callback must be triggered before |
| 219 | * this method can be called. |
| 220 | * |
| 221 | * @param intent Intent used to launch an activity. |
| 222 | * @param user The UserHandle of the user to start this activity for. |
| 223 | * |
| 224 | * |
| 225 | * @see StateCallback |
| 226 | * @see #startActivity(PendingIntent) |
| 227 | */ |
| 228 | public void startActivity(@NonNull Intent intent, UserHandle user) { |
| 229 | final ActivityOptions options = prepareActivityOptions(); |
| 230 | getContext().startActivityAsUser(intent, options.toBundle(), user); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Launch a new activity into this container. |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 235 | * <p>Activity resolved by the provided {@link PendingIntent} must have |
| 236 | * {@link android.R.attr#resizeableActivity} attribute set to {@code true} in order to be |
| 237 | * launched here. Also, if activity is not owned by the owner of this container, it must allow |
| 238 | * embedding and the caller must have permission to embed. |
| 239 | * <p>Note: This class must finish initializing and |
| 240 | * {@link StateCallback#onActivityViewReady(ActivityView)} callback must be triggered before |
| 241 | * this method can be called. |
| 242 | * |
| 243 | * @param pendingIntent Intent used to launch an activity. |
| 244 | * |
| 245 | * @see StateCallback |
| 246 | * @see #startActivity(Intent) |
| 247 | */ |
| 248 | public void startActivity(@NonNull PendingIntent pendingIntent) { |
| 249 | final ActivityOptions options = prepareActivityOptions(); |
| 250 | try { |
| 251 | pendingIntent.send(null /* context */, 0 /* code */, null /* intent */, |
| 252 | null /* onFinished */, null /* handler */, null /* requiredPermission */, |
| 253 | options.toBundle()); |
| 254 | } catch (PendingIntent.CanceledException e) { |
| 255 | throw new RuntimeException(e); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /** |
Mady Mellor | 60101c9 | 2019-04-11 19:04:00 -0700 | [diff] [blame] | 260 | * Launch a new activity into this container. |
| 261 | * <p>Activity resolved by the provided {@link PendingIntent} must have |
| 262 | * {@link android.R.attr#resizeableActivity} attribute set to {@code true} in order to be |
| 263 | * launched here. Also, if activity is not owned by the owner of this container, it must allow |
| 264 | * embedding and the caller must have permission to embed. |
| 265 | * <p>Note: This class must finish initializing and |
| 266 | * {@link StateCallback#onActivityViewReady(ActivityView)} callback must be triggered before |
| 267 | * this method can be called. |
| 268 | * |
| 269 | * @param pendingIntent Intent used to launch an activity. |
| 270 | * @param options options for the activity |
| 271 | * |
| 272 | * @see StateCallback |
| 273 | * @see #startActivity(Intent) |
| 274 | */ |
| 275 | public void startActivity(@NonNull PendingIntent pendingIntent, |
| 276 | @NonNull ActivityOptions options) { |
| 277 | options.setLaunchDisplayId(mVirtualDisplay.getDisplay().getDisplayId()); |
| 278 | try { |
| 279 | pendingIntent.send(null /* context */, 0 /* code */, null /* intent */, |
| 280 | null /* onFinished */, null /* handler */, null /* requiredPermission */, |
| 281 | options.toBundle()); |
| 282 | } catch (PendingIntent.CanceledException e) { |
| 283 | throw new RuntimeException(e); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /** |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 288 | * Check if container is ready to launch and create {@link ActivityOptions} to target the |
| 289 | * virtual display. |
| 290 | */ |
| 291 | private ActivityOptions prepareActivityOptions() { |
| 292 | if (mVirtualDisplay == null) { |
| 293 | throw new IllegalStateException( |
| 294 | "Trying to start activity before ActivityView is ready."); |
| 295 | } |
| 296 | final ActivityOptions options = ActivityOptions.makeBasic(); |
| 297 | options.setLaunchDisplayId(mVirtualDisplay.getDisplay().getDisplayId()); |
| 298 | return options; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Release this container. Activity launching will no longer be permitted. |
| 303 | * <p>Note: Calling this method is allowed after |
| 304 | * {@link StateCallback#onActivityViewReady(ActivityView)} callback was triggered and before |
| 305 | * {@link StateCallback#onActivityViewDestroyed(ActivityView)}. |
| 306 | * |
| 307 | * @see StateCallback |
| 308 | */ |
| 309 | public void release() { |
| 310 | if (mVirtualDisplay == null) { |
| 311 | throw new IllegalStateException( |
| 312 | "Trying to release container that is not initialized."); |
| 313 | } |
| 314 | performRelease(); |
| 315 | } |
| 316 | |
Andrii Kulian | 4b6599e | 2018-01-15 17:24:08 -0800 | [diff] [blame] | 317 | /** |
Tiger Huang | 2b210c2 | 2019-03-18 21:21:26 +0800 | [diff] [blame] | 318 | * Triggers an update of {@link ActivityView}'s location in window to properly set tap exclude |
Andrii Kulian | 4b6599e | 2018-01-15 17:24:08 -0800 | [diff] [blame] | 319 | * regions and avoid focus switches by touches on this view. |
| 320 | */ |
| 321 | public void onLocationChanged() { |
Tiger Huang | d8ec938 | 2019-04-18 14:35:09 -0700 | [diff] [blame] | 322 | updateLocationAndTapExcludeRegion(); |
Andrii Kulian | 4b6599e | 2018-01-15 17:24:08 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 325 | private void clearActivityViewGeometryForIme() { |
| 326 | if (mVirtualDisplay == null) { |
| 327 | return; |
| 328 | } |
| 329 | final int displayId = mVirtualDisplay.getDisplay().getDisplayId(); |
| 330 | mContext.getSystemService(InputMethodManager.class).reportActivityView(displayId, null); |
| 331 | } |
| 332 | |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 333 | @Override |
| 334 | public void onLayout(boolean changed, int l, int t, int r, int b) { |
| 335 | mSurfaceView.layout(0 /* left */, 0 /* top */, r - l /* right */, b - t /* bottom */); |
| 336 | } |
| 337 | |
Tiger Huang | 2b210c2 | 2019-03-18 21:21:26 +0800 | [diff] [blame] | 338 | @Override |
| 339 | public boolean gatherTransparentRegion(Region region) { |
| 340 | // The tap exclude region may be affected by any view on top of it, so we detect the |
| 341 | // possible change by monitoring this function. |
Tiger Huang | d8ec938 | 2019-04-18 14:35:09 -0700 | [diff] [blame] | 342 | updateLocationAndTapExcludeRegion(); |
Tiger Huang | 2b210c2 | 2019-03-18 21:21:26 +0800 | [diff] [blame] | 343 | return super.gatherTransparentRegion(region); |
| 344 | } |
| 345 | |
Tiger Huang | d8ec938 | 2019-04-18 14:35:09 -0700 | [diff] [blame] | 346 | /** |
| 347 | * Sends current location in window and tap exclude region to WM for this view. |
| 348 | */ |
| 349 | private void updateLocationAndTapExcludeRegion() { |
| 350 | if (mVirtualDisplay == null || !isAttachedToWindow()) { |
Mark Renouf | 3856b4f | 2019-02-13 16:43:18 -0500 | [diff] [blame] | 351 | return; |
| 352 | } |
Tiger Huang | d8ec938 | 2019-04-18 14:35:09 -0700 | [diff] [blame] | 353 | try { |
| 354 | int x = mLocationInWindow[0]; |
| 355 | int y = mLocationInWindow[1]; |
| 356 | getLocationInWindow(mLocationInWindow); |
| 357 | if (x != mLocationInWindow[0] || y != mLocationInWindow[1]) { |
| 358 | x = mLocationInWindow[0]; |
| 359 | y = mLocationInWindow[1]; |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 360 | final int displayId = mVirtualDisplay.getDisplay().getDisplayId(); |
Tiger Huang | d8ec938 | 2019-04-18 14:35:09 -0700 | [diff] [blame] | 361 | WindowManagerGlobal.getWindowSession().updateDisplayContentLocation( |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 362 | getWindow(), x, y, displayId); |
| 363 | |
| 364 | // Also report this geometry information to InputMethodManagerService. |
| 365 | // TODO(b/115693908): Unify this logic into the above WMS-based one. |
| 366 | final Matrix matrix = new Matrix(); |
| 367 | matrix.set(getMatrix()); |
| 368 | matrix.postTranslate(x, y); |
| 369 | mContext.getSystemService(InputMethodManager.class) |
| 370 | .reportActivityView(displayId, matrix); |
Tiger Huang | d8ec938 | 2019-04-18 14:35:09 -0700 | [diff] [blame] | 371 | } |
| 372 | updateTapExcludeRegion(x, y); |
| 373 | } catch (RemoteException e) { |
| 374 | e.rethrowAsRuntimeException(); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | /** Computes and sends current tap exclude region to WM for this view. */ |
| 379 | private void updateTapExcludeRegion(int x, int y) throws RemoteException { |
Tiger Huang | 2b210c2 | 2019-03-18 21:21:26 +0800 | [diff] [blame] | 380 | if (!canReceivePointerEvents()) { |
| 381 | cleanTapExcludeRegion(); |
| 382 | return; |
| 383 | } |
Tiger Huang | d8ec938 | 2019-04-18 14:35:09 -0700 | [diff] [blame] | 384 | mTapExcludeRegion.set(x, y, x + getWidth(), y + getHeight()); |
Tiger Huang | 2b210c2 | 2019-03-18 21:21:26 +0800 | [diff] [blame] | 385 | |
Tiger Huang | d8ec938 | 2019-04-18 14:35:09 -0700 | [diff] [blame] | 386 | // There might be views on top of us. We need to subtract those areas from the tap |
| 387 | // exclude region. |
| 388 | final ViewParent parent = getParent(); |
| 389 | if (parent != null) { |
| 390 | parent.subtractObscuredTouchableRegion(mTapExcludeRegion, this); |
Andrii Kulian | 4b6599e | 2018-01-15 17:24:08 -0800 | [diff] [blame] | 391 | } |
Tiger Huang | d8ec938 | 2019-04-18 14:35:09 -0700 | [diff] [blame] | 392 | |
| 393 | WindowManagerGlobal.getWindowSession().updateTapExcludeRegion(getWindow(), hashCode(), |
| 394 | mTapExcludeRegion); |
Andrii Kulian | 4b6599e | 2018-01-15 17:24:08 -0800 | [diff] [blame] | 395 | } |
| 396 | |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 397 | private class SurfaceCallback implements SurfaceHolder.Callback { |
| 398 | @Override |
| 399 | public void surfaceCreated(SurfaceHolder surfaceHolder) { |
| 400 | if (mVirtualDisplay == null) { |
Robert Carr | 5fea55b | 2018-12-10 13:05:52 -0800 | [diff] [blame] | 401 | initVirtualDisplay(new SurfaceSession()); |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 402 | if (mVirtualDisplay != null && mActivityViewCallback != null) { |
| 403 | mActivityViewCallback.onActivityViewReady(ActivityView.this); |
| 404 | } |
| 405 | } else { |
chaviw | ff2e7d8 | 2018-11-02 11:11:27 -0700 | [diff] [blame] | 406 | mTmpTransaction.reparent(mRootSurfaceControl, |
Robert Carr | 10584fa | 2019-01-14 15:55:19 -0800 | [diff] [blame] | 407 | mSurfaceView.getSurfaceControl()).apply(); |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 408 | } |
chaviw | da4c694 | 2018-11-07 15:52:56 -0800 | [diff] [blame] | 409 | |
| 410 | if (mVirtualDisplay != null) { |
| 411 | mVirtualDisplay.setDisplayState(true); |
| 412 | } |
| 413 | |
Tiger Huang | d8ec938 | 2019-04-18 14:35:09 -0700 | [diff] [blame] | 414 | updateLocationAndTapExcludeRegion(); |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | @Override |
| 418 | public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) { |
| 419 | if (mVirtualDisplay != null) { |
| 420 | mVirtualDisplay.resize(width, height, getBaseDisplayDensity()); |
| 421 | } |
Tiger Huang | d8ec938 | 2019-04-18 14:35:09 -0700 | [diff] [blame] | 422 | updateLocationAndTapExcludeRegion(); |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | @Override |
| 426 | public void surfaceDestroyed(SurfaceHolder surfaceHolder) { |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 427 | if (mVirtualDisplay != null) { |
chaviw | da4c694 | 2018-11-07 15:52:56 -0800 | [diff] [blame] | 428 | mVirtualDisplay.setDisplayState(false); |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 429 | } |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 430 | clearActivityViewGeometryForIme(); |
Andrii Kulian | 4b6599e | 2018-01-15 17:24:08 -0800 | [diff] [blame] | 431 | cleanTapExcludeRegion(); |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
chaviw | d44449d | 2019-01-02 16:33:12 -0800 | [diff] [blame] | 435 | @Override |
| 436 | protected void onVisibilityChanged(View changedView, int visibility) { |
| 437 | super.onVisibilityChanged(changedView, visibility); |
| 438 | mSurfaceView.setVisibility(visibility); |
| 439 | } |
| 440 | |
Mark Renouf | 041d726 | 2019-02-06 12:09:41 -0500 | [diff] [blame] | 441 | /** |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 442 | * @return the display id of the virtual display. |
| 443 | */ |
| 444 | public int getVirtualDisplayId() { |
| 445 | if (mVirtualDisplay != null) { |
| 446 | return mVirtualDisplay.getDisplay().getDisplayId(); |
| 447 | } |
| 448 | return INVALID_DISPLAY; |
| 449 | } |
| 450 | |
| 451 | /** |
Mark Renouf | 041d726 | 2019-02-06 12:09:41 -0500 | [diff] [blame] | 452 | * Injects a pair of down/up key events with keycode {@link KeyEvent#KEYCODE_BACK} to the |
| 453 | * virtual display. |
| 454 | */ |
| 455 | public void performBackPress() { |
| 456 | if (mVirtualDisplay == null) { |
| 457 | return; |
| 458 | } |
| 459 | final int displayId = mVirtualDisplay.getDisplay().getDisplayId(); |
| 460 | final InputManager im = InputManager.getInstance(); |
| 461 | im.injectInputEvent(createKeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK, displayId), |
| 462 | InputManager.INJECT_INPUT_EVENT_MODE_ASYNC); |
| 463 | im.injectInputEvent(createKeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK, displayId), |
| 464 | InputManager.INJECT_INPUT_EVENT_MODE_ASYNC); |
| 465 | } |
| 466 | |
| 467 | private static KeyEvent createKeyEvent(int action, int code, int displayId) { |
| 468 | long when = SystemClock.uptimeMillis(); |
| 469 | final KeyEvent ev = new KeyEvent(when, when, action, code, 0 /* repeat */, |
| 470 | 0 /* metaState */, KeyCharacterMap.VIRTUAL_KEYBOARD, 0 /* scancode */, |
| 471 | KeyEvent.FLAG_FROM_SYSTEM | KeyEvent.FLAG_VIRTUAL_HARD_KEY, |
| 472 | InputDevice.SOURCE_KEYBOARD); |
| 473 | ev.setDisplayId(displayId); |
| 474 | return ev; |
| 475 | } |
| 476 | |
chaviw | ff2e7d8 | 2018-11-02 11:11:27 -0700 | [diff] [blame] | 477 | private void initVirtualDisplay(SurfaceSession surfaceSession) { |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 478 | if (mVirtualDisplay != null) { |
| 479 | throw new IllegalStateException("Trying to initialize for the second time."); |
| 480 | } |
| 481 | |
| 482 | final int width = mSurfaceView.getWidth(); |
| 483 | final int height = mSurfaceView.getHeight(); |
| 484 | final DisplayManager displayManager = mContext.getSystemService(DisplayManager.class); |
chaviw | ff2e7d8 | 2018-11-02 11:11:27 -0700 | [diff] [blame] | 485 | |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 486 | mVirtualDisplay = displayManager.createVirtualDisplay( |
chaviw | da4c694 | 2018-11-07 15:52:56 -0800 | [diff] [blame] | 487 | DISPLAY_NAME + "@" + System.identityHashCode(this), width, height, |
| 488 | getBaseDisplayDensity(), null, |
| 489 | VIRTUAL_DISPLAY_FLAG_PUBLIC | VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY |
| 490 | | VIRTUAL_DISPLAY_FLAG_DESTROY_CONTENT_ON_REMOVAL); |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 491 | if (mVirtualDisplay == null) { |
| 492 | Log.e(TAG, "Failed to initialize ActivityView"); |
| 493 | return; |
| 494 | } |
| 495 | |
Andrii Kulian | f0379de | 2018-03-14 16:24:07 -0700 | [diff] [blame] | 496 | final int displayId = mVirtualDisplay.getDisplay().getDisplayId(); |
| 497 | final IWindowManager wm = WindowManagerGlobal.getWindowManagerService(); |
chaviw | ff2e7d8 | 2018-11-02 11:11:27 -0700 | [diff] [blame] | 498 | |
| 499 | mRootSurfaceControl = new SurfaceControl.Builder(surfaceSession) |
Chavi Weingarten | 6ef9cc6 | 2019-02-07 16:28:45 +0000 | [diff] [blame] | 500 | .setContainerLayer() |
Robert Carr | 5fea55b | 2018-12-10 13:05:52 -0800 | [diff] [blame] | 501 | .setParent(mSurfaceView.getSurfaceControl()) |
chaviw | ff2e7d8 | 2018-11-02 11:11:27 -0700 | [diff] [blame] | 502 | .setName(DISPLAY_NAME) |
| 503 | .build(); |
| 504 | |
Andrii Kulian | f0379de | 2018-03-14 16:24:07 -0700 | [diff] [blame] | 505 | try { |
Issei Suzuki | a5dbf52 | 2019-02-01 17:58:15 +0100 | [diff] [blame] | 506 | // TODO: Find a way to consolidate these calls to the server. |
Tiger Huang | d8ec938 | 2019-04-18 14:35:09 -0700 | [diff] [blame] | 507 | WindowManagerGlobal.getWindowSession().reparentDisplayContent( |
| 508 | getWindow(), mRootSurfaceControl, displayId); |
Andrii Kulian | f0379de | 2018-03-14 16:24:07 -0700 | [diff] [blame] | 509 | wm.dontOverrideDisplayInfo(displayId); |
Wale Ogunwale | 9e737db | 2018-12-17 15:42:37 -0800 | [diff] [blame] | 510 | if (mSingleTaskInstance) { |
| 511 | mActivityTaskManager.setDisplayToSingleTaskInstance(displayId); |
| 512 | } |
Issei Suzuki | a5dbf52 | 2019-02-01 17:58:15 +0100 | [diff] [blame] | 513 | wm.setForwardedInsets(displayId, mForwardedInsets); |
Andrii Kulian | f0379de | 2018-03-14 16:24:07 -0700 | [diff] [blame] | 514 | } catch (RemoteException e) { |
| 515 | e.rethrowAsRuntimeException(); |
| 516 | } |
chaviw | ff2e7d8 | 2018-11-02 11:11:27 -0700 | [diff] [blame] | 517 | |
| 518 | mTmpTransaction.show(mRootSurfaceControl).apply(); |
Brad Stenning | a1dbe9c | 2018-05-02 08:29:28 -0700 | [diff] [blame] | 519 | mTaskStackListener = new TaskStackListenerImpl(); |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 520 | try { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 521 | mActivityTaskManager.registerTaskStackListener(mTaskStackListener); |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 522 | } catch (RemoteException e) { |
| 523 | Log.e(TAG, "Failed to register task stack listener", e); |
| 524 | } |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | private void performRelease() { |
| 528 | if (!mOpened) { |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | mSurfaceView.getHolder().removeCallback(mSurfaceCallback); |
| 533 | |
Andrii Kulian | 4b6599e | 2018-01-15 17:24:08 -0800 | [diff] [blame] | 534 | cleanTapExcludeRegion(); |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 535 | |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 536 | if (mTaskStackListener != null) { |
| 537 | try { |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 538 | mActivityTaskManager.unregisterTaskStackListener(mTaskStackListener); |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 539 | } catch (RemoteException e) { |
| 540 | Log.e(TAG, "Failed to unregister task stack listener", e); |
| 541 | } |
| 542 | mTaskStackListener = null; |
| 543 | } |
| 544 | |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 545 | final boolean displayReleased; |
| 546 | if (mVirtualDisplay != null) { |
| 547 | mVirtualDisplay.release(); |
| 548 | mVirtualDisplay = null; |
| 549 | displayReleased = true; |
| 550 | } else { |
| 551 | displayReleased = false; |
| 552 | } |
| 553 | |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 554 | if (displayReleased && mActivityViewCallback != null) { |
| 555 | mActivityViewCallback.onActivityViewDestroyed(this); |
| 556 | } |
| 557 | |
| 558 | mGuard.close(); |
| 559 | mOpened = false; |
| 560 | } |
| 561 | |
Andrii Kulian | 4b6599e | 2018-01-15 17:24:08 -0800 | [diff] [blame] | 562 | /** Report to server that tap exclude region on hosting display should be cleared. */ |
| 563 | private void cleanTapExcludeRegion() { |
Tiger Huang | 2b210c2 | 2019-03-18 21:21:26 +0800 | [diff] [blame] | 564 | if (!isAttachedToWindow() || mTapExcludeRegion.isEmpty()) { |
Mark Renouf | 3856b4f | 2019-02-13 16:43:18 -0500 | [diff] [blame] | 565 | return; |
| 566 | } |
Tiger Huang | 2b210c2 | 2019-03-18 21:21:26 +0800 | [diff] [blame] | 567 | // Update tap exclude region with a null region to clean the state on server. |
Andrii Kulian | 4b6599e | 2018-01-15 17:24:08 -0800 | [diff] [blame] | 568 | try { |
| 569 | WindowManagerGlobal.getWindowSession().updateTapExcludeRegion(getWindow(), hashCode(), |
Tiger Huang | 2b210c2 | 2019-03-18 21:21:26 +0800 | [diff] [blame] | 570 | null /* region */); |
| 571 | mTapExcludeRegion.setEmpty(); |
Andrii Kulian | 4b6599e | 2018-01-15 17:24:08 -0800 | [diff] [blame] | 572 | } catch (RemoteException e) { |
| 573 | e.rethrowAsRuntimeException(); |
| 574 | } |
| 575 | } |
| 576 | |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 577 | /** Get density of the hosting display. */ |
| 578 | private int getBaseDisplayDensity() { |
| 579 | final WindowManager wm = mContext.getSystemService(WindowManager.class); |
| 580 | final DisplayMetrics metrics = new DisplayMetrics(); |
| 581 | wm.getDefaultDisplay().getMetrics(metrics); |
| 582 | return metrics.densityDpi; |
| 583 | } |
| 584 | |
| 585 | @Override |
| 586 | protected void finalize() throws Throwable { |
| 587 | try { |
| 588 | if (mGuard != null) { |
| 589 | mGuard.warnIfOpen(); |
| 590 | performRelease(); |
| 591 | } |
| 592 | } finally { |
| 593 | super.finalize(); |
| 594 | } |
| 595 | } |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 596 | |
| 597 | /** |
Issei Suzuki | a5dbf52 | 2019-02-01 17:58:15 +0100 | [diff] [blame] | 598 | * Set forwarded insets on the virtual display. |
| 599 | * |
| 600 | * @see IWindowManager#setForwardedInsets |
| 601 | */ |
| 602 | public void setForwardedInsets(Insets insets) { |
| 603 | mForwardedInsets = insets; |
| 604 | if (mVirtualDisplay == null) { |
| 605 | return; |
| 606 | } |
| 607 | try { |
| 608 | final IWindowManager wm = WindowManagerGlobal.getWindowManagerService(); |
| 609 | wm.setForwardedInsets(mVirtualDisplay.getDisplay().getDisplayId(), mForwardedInsets); |
| 610 | } catch (RemoteException e) { |
| 611 | e.rethrowAsRuntimeException(); |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | /** |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 616 | * A task change listener that detects background color change of the topmost stack on our |
| 617 | * virtual display and updates the background of the surface view. This background will be shown |
| 618 | * when surface view is resized, but the app hasn't drawn its content in new size yet. |
Brad Stenning | a1dbe9c | 2018-05-02 08:29:28 -0700 | [diff] [blame] | 619 | * It also calls StateCallback.onTaskMovedToFront to notify interested parties that the stack |
| 620 | * associated with the {@link ActivityView} has had a Task moved to the front. This is useful |
| 621 | * when needing to also bring the host Activity to the foreground at the same time. |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 622 | */ |
Brad Stenning | a1dbe9c | 2018-05-02 08:29:28 -0700 | [diff] [blame] | 623 | private class TaskStackListenerImpl extends TaskStackListener { |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 624 | |
| 625 | @Override |
Mark Renouf | c808f06 | 2019-02-07 15:20:37 -0500 | [diff] [blame] | 626 | public void onTaskDescriptionChanged(ActivityManager.RunningTaskInfo taskInfo) |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 627 | throws RemoteException { |
Mark Renouf | c808f06 | 2019-02-07 15:20:37 -0500 | [diff] [blame] | 628 | if (mVirtualDisplay == null |
| 629 | || taskInfo.displayId != mVirtualDisplay.getDisplay().getDisplayId()) { |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 630 | return; |
| 631 | } |
| 632 | |
Brad Stenning | a1dbe9c | 2018-05-02 08:29:28 -0700 | [diff] [blame] | 633 | StackInfo stackInfo = getTopMostStackInfo(); |
| 634 | if (stackInfo == null) { |
| 635 | return; |
| 636 | } |
| 637 | // Found the topmost stack on target display. Now check if the topmost task's |
| 638 | // description changed. |
Mark Renouf | c808f06 | 2019-02-07 15:20:37 -0500 | [diff] [blame] | 639 | if (taskInfo.taskId == stackInfo.taskIds[stackInfo.taskIds.length - 1]) { |
| 640 | mSurfaceView.setResizeBackgroundColor( |
| 641 | taskInfo.taskDescription.getBackgroundColor()); |
Brad Stenning | a1dbe9c | 2018-05-02 08:29:28 -0700 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
| 645 | @Override |
Mark Renouf | c808f06 | 2019-02-07 15:20:37 -0500 | [diff] [blame] | 646 | public void onTaskMovedToFront(ActivityManager.RunningTaskInfo taskInfo) |
| 647 | throws RemoteException { |
| 648 | if (mActivityViewCallback == null || mVirtualDisplay == null |
| 649 | || taskInfo.displayId != mVirtualDisplay.getDisplay().getDisplayId()) { |
Mark Renouf | 7b71a5d | 2019-01-23 17:12:34 -0500 | [diff] [blame] | 650 | return; |
| 651 | } |
| 652 | |
| 653 | StackInfo stackInfo = getTopMostStackInfo(); |
| 654 | // if StackInfo was null or unrelated to the "move to front" then there's no use |
| 655 | // notifying the callback |
| 656 | if (stackInfo != null |
Mark Renouf | c808f06 | 2019-02-07 15:20:37 -0500 | [diff] [blame] | 657 | && taskInfo.taskId == stackInfo.taskIds[stackInfo.taskIds.length - 1]) { |
Wale Ogunwale | 691af68 | 2019-02-11 03:09:10 -0800 | [diff] [blame] | 658 | mActivityViewCallback.onTaskMovedToFront(taskInfo.taskId); |
Mark Renouf | 7b71a5d | 2019-01-23 17:12:34 -0500 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
| 662 | @Override |
| 663 | public void onTaskCreated(int taskId, ComponentName componentName) throws RemoteException { |
Mark Renouf | c808f06 | 2019-02-07 15:20:37 -0500 | [diff] [blame] | 664 | if (mActivityViewCallback == null || mVirtualDisplay == null) { |
Mark Renouf | 7b71a5d | 2019-01-23 17:12:34 -0500 | [diff] [blame] | 665 | return; |
| 666 | } |
| 667 | |
| 668 | StackInfo stackInfo = getTopMostStackInfo(); |
| 669 | // if StackInfo was null or unrelated to the task creation then there's no use |
| 670 | // notifying the callback |
| 671 | if (stackInfo != null |
| 672 | && taskId == stackInfo.taskIds[stackInfo.taskIds.length - 1]) { |
| 673 | mActivityViewCallback.onTaskCreated(taskId, componentName); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | @Override |
Mark Renouf | c808f06 | 2019-02-07 15:20:37 -0500 | [diff] [blame] | 678 | public void onTaskRemovalStarted(ActivityManager.RunningTaskInfo taskInfo) |
| 679 | throws RemoteException { |
| 680 | if (mActivityViewCallback == null || mVirtualDisplay == null |
| 681 | || taskInfo.displayId != mVirtualDisplay.getDisplay().getDisplayId()) { |
Mark Renouf | 7b71a5d | 2019-01-23 17:12:34 -0500 | [diff] [blame] | 682 | return; |
| 683 | } |
Mark Renouf | c808f06 | 2019-02-07 15:20:37 -0500 | [diff] [blame] | 684 | mActivityViewCallback.onTaskRemovalStarted(taskInfo.taskId); |
Brad Stenning | a1dbe9c | 2018-05-02 08:29:28 -0700 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | private StackInfo getTopMostStackInfo() throws RemoteException { |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 688 | // Find the topmost task on our virtual display - it will define the background |
| 689 | // color of the surface view during resizing. |
| 690 | final int displayId = mVirtualDisplay.getDisplay().getDisplayId(); |
Wale Ogunwale | 04d9cb5 | 2018-04-30 13:55:07 -0700 | [diff] [blame] | 691 | final List<StackInfo> stackInfoList = mActivityTaskManager.getAllStackInfos(); |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 692 | |
| 693 | // Iterate through stacks from top to bottom. |
| 694 | final int stackCount = stackInfoList.size(); |
| 695 | for (int i = 0; i < stackCount; i++) { |
| 696 | final StackInfo stackInfo = stackInfoList.get(i); |
| 697 | // Only look for stacks on our virtual display. |
| 698 | if (stackInfo.displayId != displayId) { |
| 699 | continue; |
| 700 | } |
Brad Stenning | a1dbe9c | 2018-05-02 08:29:28 -0700 | [diff] [blame] | 701 | // Found the topmost stack on target display. |
| 702 | return stackInfo; |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 703 | } |
Brad Stenning | a1dbe9c | 2018-05-02 08:29:28 -0700 | [diff] [blame] | 704 | return null; |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 705 | } |
| 706 | } |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 707 | } |