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