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