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 | |
| 19 | import android.annotation.NonNull; |
Mady Mellor | c41ed32 | 2019-09-25 11:19:26 -0700 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Wale Ogunwale | 691af68 | 2019-02-11 03:09:10 -0800 | [diff] [blame] | 21 | import android.annotation.TestApi; |
Mark Renouf | 7b71a5d | 2019-01-23 17:12:34 -0500 | [diff] [blame] | 22 | import android.content.ComponentName; |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
Mady Mellor | 1bd1675 | 2019-10-25 12:30:09 -0700 | [diff] [blame] | 25 | import android.content.pm.LauncherApps; |
| 26 | import android.content.pm.ShortcutInfo; |
Issei Suzuki | a5dbf52 | 2019-02-01 17:58:15 +0100 | [diff] [blame] | 27 | import android.graphics.Insets; |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 28 | import android.graphics.Matrix; |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 29 | import android.graphics.Point; |
Mark Renouf | e574ad0 | 2019-08-12 16:22:15 -0400 | [diff] [blame] | 30 | import android.graphics.Rect; |
Tiger Huang | 2b210c2 | 2019-03-18 21:21:26 +0800 | [diff] [blame] | 31 | import android.graphics.Region; |
Yunfan Chen | b83940c | 2020-04-06 16:43:09 +0900 | [diff] [blame] | 32 | import android.hardware.display.VirtualDisplay; |
Mady Mellor | 1bd1675 | 2019-10-25 12:30:09 -0700 | [diff] [blame] | 33 | import android.os.Bundle; |
Brad Stenning | a1dbe9c | 2018-05-02 08:29:28 -0700 | [diff] [blame] | 34 | import android.os.UserHandle; |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 35 | import android.util.AttributeSet; |
Issei Suzuki | ea5bec7 | 2020-06-19 18:14:33 +0200 | [diff] [blame] | 36 | import android.util.DisplayMetrics; |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 37 | import android.util.Log; |
Issei Suzuki | ea5bec7 | 2020-06-19 18:14:33 +0200 | [diff] [blame] | 38 | import android.view.Display; |
| 39 | import android.view.DisplayInfo; |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 40 | import android.view.IWindow; |
Andrii Kulian | f0379de | 2018-03-14 16:24:07 -0700 | [diff] [blame] | 41 | import android.view.IWindowManager; |
Mark Renouf | 041d726 | 2019-02-06 12:09:41 -0500 | [diff] [blame] | 42 | import android.view.KeyEvent; |
chaviw | ff2e7d8 | 2018-11-02 11:11:27 -0700 | [diff] [blame] | 43 | import android.view.SurfaceControl; |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 44 | import android.view.SurfaceHolder; |
| 45 | import android.view.SurfaceView; |
chaviw | d44449d | 2019-01-02 16:33:12 -0800 | [diff] [blame] | 46 | import android.view.View; |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 47 | import android.view.ViewGroup; |
Tiger Huang | 2b210c2 | 2019-03-18 21:21:26 +0800 | [diff] [blame] | 48 | import android.view.ViewParent; |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 49 | import android.window.TaskEmbedder; |
| 50 | import android.window.TaskOrganizerTaskEmbedder; |
| 51 | import android.window.VirtualDisplayTaskEmbedder; |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 52 | |
| 53 | import dalvik.system.CloseGuard; |
| 54 | |
| 55 | /** |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 56 | * Task container that allows launching activities into itself. |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 57 | * <p>Activity launching into this container is restricted by the same rules that apply to launching |
| 58 | * on VirtualDisplays. |
| 59 | * @hide |
| 60 | */ |
Wale Ogunwale | 691af68 | 2019-02-11 03:09:10 -0800 | [diff] [blame] | 61 | @TestApi |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 62 | public class ActivityView extends ViewGroup implements android.window.TaskEmbedder.Host { |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 63 | |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 64 | private static final String TAG = "ActivityView"; |
| 65 | |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 66 | private android.window.TaskEmbedder mTaskEmbedder; |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 67 | |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 68 | private final SurfaceView mSurfaceView; |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 69 | private final SurfaceCallback mSurfaceCallback; |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 70 | |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 71 | private final CloseGuard mGuard = CloseGuard.get(); |
| 72 | private boolean mOpened; // Protected by mGuard. |
| 73 | |
chaviw | ff2e7d8 | 2018-11-02 11:11:27 -0700 | [diff] [blame] | 74 | private final SurfaceControl.Transaction mTmpTransaction = new SurfaceControl.Transaction(); |
chaviw | ff2e7d8 | 2018-11-02 11:11:27 -0700 | [diff] [blame] | 75 | |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 76 | // For Host |
| 77 | private final Point mWindowPosition = new Point(); |
| 78 | private final int[] mTmpArray = new int[2]; |
Winson Chung | a1f869d | 2020-03-21 23:02:48 -0700 | [diff] [blame] | 79 | private final Rect mTmpRect = new Rect(); |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 80 | private final Matrix mScreenSurfaceMatrix = new Matrix(); |
| 81 | private final Region mTapExcludeRegion = new Region(); |
Mark Renouf | e574ad0 | 2019-08-12 16:22:15 -0400 | [diff] [blame] | 82 | |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 83 | public ActivityView(Context context) { |
| 84 | this(context, null /* attrs */); |
| 85 | } |
| 86 | |
| 87 | public ActivityView(Context context, AttributeSet attrs) { |
| 88 | this(context, attrs, 0 /* defStyle */); |
| 89 | } |
| 90 | |
| 91 | public ActivityView(Context context, AttributeSet attrs, int defStyle) { |
Wale Ogunwale | 9e737db | 2018-12-17 15:42:37 -0800 | [diff] [blame] | 92 | this(context, attrs, defStyle, false /*singleTaskInstance*/); |
| 93 | } |
| 94 | |
Winson Chung | a1f869d | 2020-03-21 23:02:48 -0700 | [diff] [blame] | 95 | public ActivityView(Context context, AttributeSet attrs, int defStyle, |
| 96 | boolean singleTaskInstance) { |
Linus Tufvesson | db1f2ec | 2020-04-09 17:04:32 +0100 | [diff] [blame] | 97 | this(context, attrs, defStyle, singleTaskInstance, false /* usePublicVirtualDisplay */); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * This constructor let's the caller explicitly request a public virtual display as the backing |
| 102 | * display. Using a public display is not recommended as it exposes it to other applications, |
| 103 | * but it might be needed for backwards compatibility. |
| 104 | */ |
| 105 | public ActivityView( |
| 106 | @NonNull Context context, @NonNull AttributeSet attrs, int defStyle, |
| 107 | boolean singleTaskInstance, boolean usePublicVirtualDisplay) { |
shawnlin | ba532af | 2020-06-02 17:48:37 +0800 | [diff] [blame] | 108 | this(context, attrs, defStyle, singleTaskInstance, usePublicVirtualDisplay, false); |
| 109 | } |
| 110 | |
| 111 | /** @hide */ |
| 112 | public ActivityView( |
| 113 | @NonNull Context context, @NonNull AttributeSet attrs, int defStyle, |
| 114 | boolean singleTaskInstance, boolean usePublicVirtualDisplay, |
| 115 | boolean disableSurfaceViewBackgroundLayer) { |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 116 | super(context, attrs, defStyle); |
Winson Chung | a1f869d | 2020-03-21 23:02:48 -0700 | [diff] [blame] | 117 | if (useTaskOrganizer()) { |
| 118 | mTaskEmbedder = new TaskOrganizerTaskEmbedder(context, this); |
| 119 | } else { |
Linus Tufvesson | db1f2ec | 2020-04-09 17:04:32 +0100 | [diff] [blame] | 120 | mTaskEmbedder = new VirtualDisplayTaskEmbedder(context, this, singleTaskInstance, |
| 121 | usePublicVirtualDisplay); |
Winson Chung | a1f869d | 2020-03-21 23:02:48 -0700 | [diff] [blame] | 122 | } |
shawnlin | ba532af | 2020-06-02 17:48:37 +0800 | [diff] [blame] | 123 | mSurfaceView = new SurfaceView(context, null, 0, 0, disableSurfaceViewBackgroundLayer); |
lumark | 3f51e3c | 2019-07-23 21:18:17 +0800 | [diff] [blame] | 124 | // Since ActivityView#getAlpha has been overridden, we should use parent class's alpha |
| 125 | // as master to synchronize surface view's alpha value. |
| 126 | mSurfaceView.setAlpha(super.getAlpha()); |
Issei Suzuki | 006b71f | 2019-06-17 15:56:57 +0200 | [diff] [blame] | 127 | mSurfaceView.setUseAlpha(); |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 128 | mSurfaceCallback = new SurfaceCallback(); |
| 129 | mSurfaceView.getHolder().addCallback(mSurfaceCallback); |
| 130 | addView(mSurfaceView); |
| 131 | |
| 132 | mOpened = true; |
| 133 | mGuard.open("release"); |
| 134 | } |
| 135 | |
| 136 | /** Callback that notifies when the container is ready or destroyed. */ |
| 137 | public abstract static class StateCallback { |
Mark Renouf | 7b71a5d | 2019-01-23 17:12:34 -0500 | [diff] [blame] | 138 | |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 139 | /** |
| 140 | * Called when the container is ready for launching activities. Calling |
| 141 | * {@link #startActivity(Intent)} prior to this callback will result in an |
| 142 | * {@link IllegalStateException}. |
| 143 | * |
| 144 | * @see #startActivity(Intent) |
| 145 | */ |
| 146 | public abstract void onActivityViewReady(ActivityView view); |
Mark Renouf | 7b71a5d | 2019-01-23 17:12:34 -0500 | [diff] [blame] | 147 | |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 148 | /** |
| 149 | * Called when the container can no longer launch activities. Calling |
| 150 | * {@link #startActivity(Intent)} after this callback will result in an |
| 151 | * {@link IllegalStateException}. |
| 152 | * |
| 153 | * @see #startActivity(Intent) |
| 154 | */ |
| 155 | public abstract void onActivityViewDestroyed(ActivityView view); |
Mark Renouf | 7b71a5d | 2019-01-23 17:12:34 -0500 | [diff] [blame] | 156 | |
| 157 | /** |
| 158 | * Called when a task is created inside the container. |
| 159 | * This is a filtered version of {@link TaskStackListener} |
| 160 | */ |
| 161 | public void onTaskCreated(int taskId, ComponentName componentName) { } |
| 162 | |
Brad Stenning | a1dbe9c | 2018-05-02 08:29:28 -0700 | [diff] [blame] | 163 | /** |
Winson Chung | a1f869d | 2020-03-21 23:02:48 -0700 | [diff] [blame] | 164 | * Called when a task visibility changes. |
| 165 | * @hide |
| 166 | */ |
| 167 | public void onTaskVisibilityChanged(int taskId, boolean visible) { } |
| 168 | |
| 169 | /** |
Brad Stenning | a1dbe9c | 2018-05-02 08:29:28 -0700 | [diff] [blame] | 170 | * Called when a task is moved to the front of the stack inside the container. |
| 171 | * This is a filtered version of {@link TaskStackListener} |
| 172 | */ |
Wale Ogunwale | 691af68 | 2019-02-11 03:09:10 -0800 | [diff] [blame] | 173 | public void onTaskMovedToFront(int taskId) { } |
Mark Renouf | 7b71a5d | 2019-01-23 17:12:34 -0500 | [diff] [blame] | 174 | |
| 175 | /** |
| 176 | * Called when a task is about to be removed from the stack inside the container. |
| 177 | * This is a filtered version of {@link TaskStackListener} |
| 178 | */ |
| 179 | public void onTaskRemovalStarted(int taskId) { } |
Winson Chung | a1f869d | 2020-03-21 23:02:48 -0700 | [diff] [blame] | 180 | |
| 181 | /** |
| 182 | * Called when back is pressed on the root activity of the task. |
| 183 | * @hide |
| 184 | */ |
| 185 | public void onBackPressedOnTaskRoot(int taskId) { } |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Set the callback to be notified about state changes. |
| 190 | * <p>This class must finish initializing before {@link #startActivity(Intent)} can be called. |
| 191 | * <p>Note: If the instance was ready prior to this call being made, then |
| 192 | * {@link StateCallback#onActivityViewReady(ActivityView)} will be called from within |
| 193 | * this method call. |
| 194 | * |
| 195 | * @param callback The callback to report events to. |
| 196 | * |
| 197 | * @see StateCallback |
| 198 | * @see #startActivity(Intent) |
| 199 | */ |
| 200 | public void setCallback(StateCallback callback) { |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 201 | if (callback == null) { |
| 202 | mTaskEmbedder.setListener(null); |
| 203 | return; |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 204 | } |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 205 | mTaskEmbedder.setListener(new StateCallbackAdapter(callback)); |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /** |
Mark Renouf | 34d04f3 | 2019-05-13 15:53:18 -0400 | [diff] [blame] | 209 | * Sets the corner radius for the Activity displayed here. The corners will be |
| 210 | * cropped from the window painted by the contained Activity. |
| 211 | * |
| 212 | * @param cornerRadius the radius for the corners, in pixels |
| 213 | * @hide |
| 214 | */ |
| 215 | public void setCornerRadius(float cornerRadius) { |
| 216 | mSurfaceView.setCornerRadius(cornerRadius); |
| 217 | } |
| 218 | |
| 219 | /** |
Mark Renouf | e574ad0 | 2019-08-12 16:22:15 -0400 | [diff] [blame] | 220 | * @hide |
| 221 | */ |
| 222 | public float getCornerRadius() { |
| 223 | return mSurfaceView.getCornerRadius(); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Control whether the surface is clipped to the same bounds as the View. If true, then |
| 228 | * the bounds set by {@link #setSurfaceClipBounds(Rect)} are applied to the surface as |
| 229 | * window-crop. |
| 230 | * |
| 231 | * @param clippingEnabled whether to enable surface clipping |
| 232 | * @hide |
| 233 | */ |
| 234 | public void setSurfaceClippingEnabled(boolean clippingEnabled) { |
| 235 | mSurfaceView.setEnableSurfaceClipping(clippingEnabled); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Sets an area on the contained surface to which it will be clipped |
| 240 | * when it is drawn. Setting the value to null will remove the clip bounds |
| 241 | * and the surface will draw normally, using its full bounds. |
| 242 | * |
| 243 | * @param clipBounds The rectangular area, in the local coordinates of |
| 244 | * this view, to which future drawing operations will be clipped. |
| 245 | * @hide |
| 246 | */ |
| 247 | public void setSurfaceClipBounds(Rect clipBounds) { |
| 248 | mSurfaceView.setClipBounds(clipBounds); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * @hide |
| 253 | */ |
| 254 | public boolean getSurfaceClipBounds(Rect outRect) { |
| 255 | return mSurfaceView.getClipBounds(outRect); |
| 256 | } |
| 257 | |
| 258 | /** |
Mady Mellor | 1bd1675 | 2019-10-25 12:30:09 -0700 | [diff] [blame] | 259 | * Launch an activity represented by {@link ShortcutInfo} into this container. |
| 260 | * <p>The owner of this container must be allowed to access the shortcut information, |
| 261 | * as defined in {@link LauncherApps#hasShortcutHostPermission()} to use this method. |
| 262 | * <p>Activity resolved by the provided {@link ShortcutInfo} must have |
| 263 | * {@link android.R.attr#resizeableActivity} attribute set to {@code true} in order to be |
| 264 | * launched here. Also, if activity is not owned by the owner of this container, it must allow |
| 265 | * embedding and the caller must have permission to embed. |
| 266 | * <p>Note: This class must finish initializing and |
| 267 | * {@link StateCallback#onActivityViewReady(ActivityView)} callback must be triggered before |
| 268 | * this method can be called. |
| 269 | * |
| 270 | * @param shortcut the shortcut used to launch the activity. |
| 271 | * @param options for the activity. |
| 272 | * @param sourceBounds the rect containing the source bounds of the clicked icon to open |
| 273 | * this shortcut. |
| 274 | * @see StateCallback |
| 275 | * @see LauncherApps#startShortcut(ShortcutInfo, Rect, Bundle) |
| 276 | * |
| 277 | * @hide |
| 278 | */ |
| 279 | public void startShortcutActivity(@NonNull ShortcutInfo shortcut, |
| 280 | @NonNull ActivityOptions options, @Nullable Rect sourceBounds) { |
| 281 | mTaskEmbedder.startShortcutActivity(shortcut, options, sourceBounds); |
| 282 | } |
| 283 | |
| 284 | /** |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 285 | * Launch a new activity into this container. |
| 286 | * <p>Activity resolved by the provided {@link Intent} must have |
| 287 | * {@link android.R.attr#resizeableActivity} attribute set to {@code true} in order to be |
| 288 | * launched here. Also, if activity is not owned by the owner of this container, it must allow |
| 289 | * embedding and the caller must have permission to embed. |
| 290 | * <p>Note: This class must finish initializing and |
| 291 | * {@link StateCallback#onActivityViewReady(ActivityView)} callback must be triggered before |
| 292 | * this method can be called. |
| 293 | * |
| 294 | * @param intent Intent used to launch an activity. |
| 295 | * |
| 296 | * @see StateCallback |
| 297 | * @see #startActivity(PendingIntent) |
| 298 | */ |
| 299 | public void startActivity(@NonNull Intent intent) { |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 300 | mTaskEmbedder.startActivity(intent); |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Launch a new activity into this container. |
Brad Stenning | a1dbe9c | 2018-05-02 08:29:28 -0700 | [diff] [blame] | 305 | * <p>Activity resolved by the provided {@link Intent} must have |
| 306 | * {@link android.R.attr#resizeableActivity} attribute set to {@code true} in order to be |
| 307 | * launched here. Also, if activity is not owned by the owner of this container, it must allow |
| 308 | * embedding and the caller must have permission to embed. |
| 309 | * <p>Note: This class must finish initializing and |
| 310 | * {@link StateCallback#onActivityViewReady(ActivityView)} callback must be triggered before |
| 311 | * this method can be called. |
| 312 | * |
| 313 | * @param intent Intent used to launch an activity. |
| 314 | * @param user The UserHandle of the user to start this activity for. |
| 315 | * |
| 316 | * |
| 317 | * @see StateCallback |
| 318 | * @see #startActivity(PendingIntent) |
| 319 | */ |
| 320 | public void startActivity(@NonNull Intent intent, UserHandle user) { |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 321 | mTaskEmbedder.startActivity(intent, user); |
Brad Stenning | a1dbe9c | 2018-05-02 08:29:28 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Launch a new activity into this container. |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 326 | * <p>Activity resolved by the provided {@link PendingIntent} must have |
| 327 | * {@link android.R.attr#resizeableActivity} attribute set to {@code true} in order to be |
| 328 | * launched here. Also, if activity is not owned by the owner of this container, it must allow |
| 329 | * embedding and the caller must have permission to embed. |
| 330 | * <p>Note: This class must finish initializing and |
| 331 | * {@link StateCallback#onActivityViewReady(ActivityView)} callback must be triggered before |
| 332 | * this method can be called. |
| 333 | * |
| 334 | * @param pendingIntent Intent used to launch an activity. |
| 335 | * |
| 336 | * @see StateCallback |
| 337 | * @see #startActivity(Intent) |
| 338 | */ |
| 339 | public void startActivity(@NonNull PendingIntent pendingIntent) { |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 340 | mTaskEmbedder.startActivity(pendingIntent); |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /** |
Mady Mellor | 60101c9 | 2019-04-11 19:04:00 -0700 | [diff] [blame] | 344 | * Launch a new activity into this container. |
| 345 | * <p>Activity resolved by the provided {@link PendingIntent} must have |
| 346 | * {@link android.R.attr#resizeableActivity} attribute set to {@code true} in order to be |
| 347 | * launched here. Also, if activity is not owned by the owner of this container, it must allow |
| 348 | * embedding and the caller must have permission to embed. |
| 349 | * <p>Note: This class must finish initializing and |
| 350 | * {@link StateCallback#onActivityViewReady(ActivityView)} callback must be triggered before |
| 351 | * this method can be called. |
| 352 | * |
| 353 | * @param pendingIntent Intent used to launch an activity. |
Mady Mellor | c41ed32 | 2019-09-25 11:19:26 -0700 | [diff] [blame] | 354 | * @param fillInIntent Additional Intent data, see {@link Intent#fillIn Intent.fillIn()}. |
Mady Mellor | 60101c9 | 2019-04-11 19:04:00 -0700 | [diff] [blame] | 355 | * @param options options for the activity |
| 356 | * |
| 357 | * @see StateCallback |
| 358 | * @see #startActivity(Intent) |
| 359 | */ |
Mady Mellor | c41ed32 | 2019-09-25 11:19:26 -0700 | [diff] [blame] | 360 | public void startActivity(@NonNull PendingIntent pendingIntent, @Nullable Intent fillInIntent, |
Mady Mellor | 60101c9 | 2019-04-11 19:04:00 -0700 | [diff] [blame] | 361 | @NonNull ActivityOptions options) { |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 362 | mTaskEmbedder.startActivity(pendingIntent, fillInIntent, options); |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /** |
shawnlin | 2806269 | 2020-06-15 13:55:51 +0800 | [diff] [blame] | 366 | * Release this container if it is initialized. Activity launching will no longer be permitted. |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 367 | */ |
| 368 | public void release() { |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 369 | performRelease(); |
| 370 | } |
| 371 | |
Andrii Kulian | 4b6599e | 2018-01-15 17:24:08 -0800 | [diff] [blame] | 372 | /** |
Tiger Huang | 2b210c2 | 2019-03-18 21:21:26 +0800 | [diff] [blame] | 373 | * 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] | 374 | * regions and avoid focus switches by touches on this view. |
| 375 | */ |
| 376 | public void onLocationChanged() { |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 377 | mTaskEmbedder.notifyBoundsChanged(); |
Yohei Yukawa | b4f328a | 2019-05-02 08:41:27 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 380 | @Override |
| 381 | public void onLayout(boolean changed, int l, int t, int r, int b) { |
| 382 | mSurfaceView.layout(0 /* left */, 0 /* top */, r - l /* right */, b - t /* bottom */); |
| 383 | } |
| 384 | |
lumark | 3f51e3c | 2019-07-23 21:18:17 +0800 | [diff] [blame] | 385 | /** |
| 386 | * Sets the alpha value when the content of {@link SurfaceView} needs to show or hide. |
| 387 | * <p>Note: The surface view may ignore the alpha value in some cases. Refer to |
| 388 | * {@link SurfaceView#setAlpha} for more details. |
| 389 | * |
| 390 | * @param alpha The opacity of the view. |
| 391 | */ |
Tiger Huang | 2b210c2 | 2019-03-18 21:21:26 +0800 | [diff] [blame] | 392 | @Override |
Issei Suzuki | cac2a50 | 2019-04-16 16:52:50 +0200 | [diff] [blame] | 393 | public void setAlpha(float alpha) { |
lumark | 3f51e3c | 2019-07-23 21:18:17 +0800 | [diff] [blame] | 394 | super.setAlpha(alpha); |
| 395 | |
| 396 | if (mSurfaceView != null) { |
| 397 | mSurfaceView.setAlpha(alpha); |
| 398 | } |
Issei Suzuki | cac2a50 | 2019-04-16 16:52:50 +0200 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | @Override |
| 402 | public float getAlpha() { |
| 403 | return mSurfaceView.getAlpha(); |
| 404 | } |
| 405 | |
| 406 | @Override |
Tiger Huang | 2b210c2 | 2019-03-18 21:21:26 +0800 | [diff] [blame] | 407 | public boolean gatherTransparentRegion(Region region) { |
Winson Chung | a1f869d | 2020-03-21 23:02:48 -0700 | [diff] [blame] | 408 | return mTaskEmbedder.gatherTransparentRegion(region) |
| 409 | || super.gatherTransparentRegion(region); |
Tiger Huang | 2b210c2 | 2019-03-18 21:21:26 +0800 | [diff] [blame] | 410 | } |
| 411 | |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 412 | private class SurfaceCallback implements SurfaceHolder.Callback { |
Issei Suzuki | ea5bec7 | 2020-06-19 18:14:33 +0200 | [diff] [blame] | 413 | private final DisplayInfo mTempDisplayInfo = new DisplayInfo(); |
| 414 | private final DisplayMetrics mTempMetrics = new DisplayMetrics(); |
| 415 | |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 416 | @Override |
| 417 | public void surfaceCreated(SurfaceHolder surfaceHolder) { |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 418 | if (!mTaskEmbedder.isInitialized()) { |
| 419 | initTaskEmbedder(mSurfaceView.getSurfaceControl()); |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 420 | } else { |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 421 | mTmpTransaction.reparent(mTaskEmbedder.getSurfaceControl(), |
Robert Carr | 10584fa | 2019-01-14 15:55:19 -0800 | [diff] [blame] | 422 | mSurfaceView.getSurfaceControl()).apply(); |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 423 | } |
Issei Suzuki | ea5bec7 | 2020-06-19 18:14:33 +0200 | [diff] [blame] | 424 | mTaskEmbedder.resizeTask(getWidth(), getHeight()); |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 425 | mTaskEmbedder.start(); |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | @Override |
| 429 | public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) { |
Issei Suzuki | ea5bec7 | 2020-06-19 18:14:33 +0200 | [diff] [blame] | 430 | final Display display = getVirtualDisplay().getDisplay(); |
| 431 | if (!display.getDisplayInfo(mTempDisplayInfo)) { |
| 432 | return; |
| 433 | } |
| 434 | mTempDisplayInfo.getAppMetrics(mTempMetrics); |
| 435 | if (width != mTempMetrics.widthPixels || height != mTempMetrics.heightPixels) { |
| 436 | mTaskEmbedder.resizeTask(width, height); |
| 437 | mTaskEmbedder.notifyBoundsChanged(); |
| 438 | } |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | @Override |
| 442 | public void surfaceDestroyed(SurfaceHolder surfaceHolder) { |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 443 | mTaskEmbedder.stop(); |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
chaviw | d44449d | 2019-01-02 16:33:12 -0800 | [diff] [blame] | 447 | @Override |
| 448 | protected void onVisibilityChanged(View changedView, int visibility) { |
| 449 | super.onVisibilityChanged(changedView, visibility); |
| 450 | mSurfaceView.setVisibility(visibility); |
| 451 | } |
| 452 | |
Mark Renouf | 041d726 | 2019-02-06 12:09:41 -0500 | [diff] [blame] | 453 | /** |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 454 | * @return the display id of the virtual display. |
| 455 | */ |
| 456 | public int getVirtualDisplayId() { |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 457 | return mTaskEmbedder.getDisplayId(); |
Mady Mellor | 390bff4 | 2019-04-05 15:09:01 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | /** |
Yunfan Chen | b83940c | 2020-04-06 16:43:09 +0900 | [diff] [blame] | 461 | * @hide |
| 462 | * @return virtual display. |
| 463 | */ |
| 464 | public VirtualDisplay getVirtualDisplay() { |
| 465 | return mTaskEmbedder.getVirtualDisplay(); |
| 466 | } |
| 467 | |
| 468 | /** |
Mark Renouf | 041d726 | 2019-02-06 12:09:41 -0500 | [diff] [blame] | 469 | * Injects a pair of down/up key events with keycode {@link KeyEvent#KEYCODE_BACK} to the |
| 470 | * virtual display. |
| 471 | */ |
| 472 | public void performBackPress() { |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 473 | mTaskEmbedder.performBackPress(); |
Mark Renouf | 041d726 | 2019-02-06 12:09:41 -0500 | [diff] [blame] | 474 | } |
| 475 | |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 476 | /** |
| 477 | * Initializes the task embedder. |
| 478 | * |
| 479 | * @param parent control for the surface to parent to |
| 480 | * @return true if the task embedder has been initialized |
| 481 | */ |
| 482 | private boolean initTaskEmbedder(SurfaceControl parent) { |
| 483 | if (!mTaskEmbedder.initialize(parent)) { |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 484 | Log.e(TAG, "Failed to initialize ActivityView"); |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 485 | return false; |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 486 | } |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 487 | return true; |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | private void performRelease() { |
| 491 | if (!mOpened) { |
| 492 | return; |
| 493 | } |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 494 | mSurfaceView.getHolder().removeCallback(mSurfaceCallback); |
shawnlin | 2806269 | 2020-06-15 13:55:51 +0800 | [diff] [blame] | 495 | if (mTaskEmbedder.isInitialized()) { |
| 496 | mTaskEmbedder.release(); |
| 497 | } |
Yuncheol Heo | 9076eac | 2019-11-10 11:32:27 -0800 | [diff] [blame] | 498 | mTaskEmbedder.setListener(null); |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 499 | |
| 500 | mGuard.close(); |
| 501 | mOpened = false; |
| 502 | } |
| 503 | |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 504 | @Override |
| 505 | protected void finalize() throws Throwable { |
| 506 | try { |
| 507 | if (mGuard != null) { |
| 508 | mGuard.warnIfOpen(); |
| 509 | performRelease(); |
| 510 | } |
| 511 | } finally { |
| 512 | super.finalize(); |
| 513 | } |
| 514 | } |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 515 | |
| 516 | /** |
Issei Suzuki | a5dbf52 | 2019-02-01 17:58:15 +0100 | [diff] [blame] | 517 | * Set forwarded insets on the virtual display. |
| 518 | * |
| 519 | * @see IWindowManager#setForwardedInsets |
| 520 | */ |
| 521 | public void setForwardedInsets(Insets insets) { |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 522 | mTaskEmbedder.setForwardedInsets(insets); |
| 523 | } |
| 524 | |
| 525 | // Host |
| 526 | |
| 527 | /** @hide */ |
| 528 | @Override |
Wale Ogunwale | adf116e | 2020-03-27 16:36:01 -0700 | [diff] [blame] | 529 | public void onTaskBackgroundColorChanged(android.window.TaskEmbedder ts, int bgColor) { |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 530 | if (mSurfaceView != null) { |
| 531 | mSurfaceView.setResizeBackgroundColor(bgColor); |
Issei Suzuki | a5dbf52 | 2019-02-01 17:58:15 +0100 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 535 | /** @hide */ |
| 536 | @Override |
| 537 | public Region getTapExcludeRegion() { |
| 538 | if (isAttachedToWindow() && canReceivePointerEvents()) { |
| 539 | Point windowPos = getPositionInWindow(); |
| 540 | mTapExcludeRegion.set( |
| 541 | windowPos.x, |
| 542 | windowPos.y, |
| 543 | windowPos.x + getWidth(), |
| 544 | windowPos.y + getHeight()); |
| 545 | // There might be views on top of us. We need to subtract those areas from the tap |
| 546 | // exclude region. |
| 547 | final ViewParent parent = getParent(); |
| 548 | if (parent != null) { |
| 549 | parent.subtractObscuredTouchableRegion(mTapExcludeRegion, this); |
| 550 | } |
| 551 | } else { |
| 552 | mTapExcludeRegion.setEmpty(); |
| 553 | } |
| 554 | return mTapExcludeRegion; |
| 555 | } |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 556 | |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 557 | /** @hide */ |
| 558 | @Override |
| 559 | public Matrix getScreenToTaskMatrix() { |
| 560 | getLocationOnScreen(mTmpArray); |
| 561 | mScreenSurfaceMatrix.set(getMatrix()); |
| 562 | mScreenSurfaceMatrix.postTranslate(mTmpArray[0], mTmpArray[1]); |
| 563 | return mScreenSurfaceMatrix; |
| 564 | } |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 565 | |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 566 | /** @hide */ |
| 567 | @Override |
| 568 | public Point getPositionInWindow() { |
| 569 | getLocationInWindow(mTmpArray); |
| 570 | mWindowPosition.set(mTmpArray[0], mTmpArray[1]); |
| 571 | return mWindowPosition; |
| 572 | } |
| 573 | |
| 574 | /** @hide */ |
| 575 | @Override |
Winson Chung | a1f869d | 2020-03-21 23:02:48 -0700 | [diff] [blame] | 576 | public Rect getScreenBounds() { |
| 577 | getBoundsOnScreen(mTmpRect); |
| 578 | return mTmpRect; |
| 579 | } |
| 580 | |
| 581 | /** @hide */ |
| 582 | @Override |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 583 | public IWindow getWindow() { |
| 584 | return super.getWindow(); |
| 585 | } |
| 586 | |
| 587 | /** @hide */ |
| 588 | @Override |
| 589 | public boolean canReceivePointerEvents() { |
| 590 | return super.canReceivePointerEvents(); |
| 591 | } |
| 592 | |
Winson Chung | a1f869d | 2020-03-21 23:02:48 -0700 | [diff] [blame] | 593 | /** |
| 594 | * Overridden by instances that require the use of the task organizer implementation instead of |
| 595 | * the virtual display implementation. Not for general use. |
| 596 | * @hide |
| 597 | */ |
| 598 | protected boolean useTaskOrganizer() { |
| 599 | return false; |
| 600 | } |
| 601 | |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 602 | private final class StateCallbackAdapter implements TaskEmbedder.Listener { |
| 603 | private final StateCallback mCallback; |
| 604 | |
| 605 | private StateCallbackAdapter(ActivityView.StateCallback cb) { |
| 606 | mCallback = cb; |
Brad Stenning | a1dbe9c | 2018-05-02 08:29:28 -0700 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | @Override |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 610 | public void onInitialized() { |
| 611 | mCallback.onActivityViewReady(ActivityView.this); |
Mark Renouf | 7b71a5d | 2019-01-23 17:12:34 -0500 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | @Override |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 615 | public void onReleased() { |
| 616 | mCallback.onActivityViewDestroyed(ActivityView.this); |
Mark Renouf | 7b71a5d | 2019-01-23 17:12:34 -0500 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | @Override |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 620 | public void onTaskCreated(int taskId, ComponentName name) { |
| 621 | mCallback.onTaskCreated(taskId, name); |
Brad Stenning | a1dbe9c | 2018-05-02 08:29:28 -0700 | [diff] [blame] | 622 | } |
| 623 | |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 624 | @Override |
Winson Chung | a1f869d | 2020-03-21 23:02:48 -0700 | [diff] [blame] | 625 | public void onTaskVisibilityChanged(int taskId, boolean visible) { |
| 626 | mCallback.onTaskVisibilityChanged(taskId, visible); |
| 627 | } |
| 628 | |
| 629 | @Override |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 630 | public void onTaskMovedToFront(int taskId) { |
| 631 | mCallback.onTaskMovedToFront(taskId); |
| 632 | } |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 633 | |
Mark Renouf | 89ac988 | 2019-10-07 16:28:18 -0400 | [diff] [blame] | 634 | @Override |
| 635 | public void onTaskRemovalStarted(int taskId) { |
| 636 | mCallback.onTaskRemovalStarted(taskId); |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 637 | } |
Winson Chung | a1f869d | 2020-03-21 23:02:48 -0700 | [diff] [blame] | 638 | |
| 639 | @Override |
| 640 | public void onBackPressedOnTaskRoot(int taskId) { |
| 641 | mCallback.onBackPressedOnTaskRoot(taskId); |
| 642 | } |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 643 | } |
Andrii Kulian | d313469 | 2017-06-26 14:57:02 -0700 | [diff] [blame] | 644 | } |