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