Robert Carr | 9d431e1 | 2018-12-17 13:11:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.view; |
| 18 | |
Vishnu Nair | 5cf25319 | 2019-11-07 15:33:20 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Robert Carr | 8833ae8 | 2019-09-16 13:20:02 -0700 | [diff] [blame] | 21 | import android.annotation.TestApi; |
Vishnu Nair | 5cf25319 | 2019-11-07 15:33:20 -0800 | [diff] [blame] | 22 | import android.content.Context; |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 23 | import android.graphics.PixelFormat; |
Vishnu Nair | 5cf25319 | 2019-11-07 15:33:20 -0800 | [diff] [blame] | 24 | import android.os.IBinder; |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 25 | import android.os.Parcel; |
| 26 | import android.os.Parcelable; |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 27 | import android.view.accessibility.IAccessibilityEmbeddedConnection; |
Robert Carr | 8833ae8 | 2019-09-16 13:20:02 -0700 | [diff] [blame] | 28 | |
Robert Carr | 271d9c7 | 2020-03-12 12:39:24 -0700 | [diff] [blame] | 29 | import java.util.Objects; |
| 30 | |
Robert Carr | 9d431e1 | 2018-12-17 13:11:48 -0800 | [diff] [blame] | 31 | /** |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 32 | * Utility class for adding a View hierarchy to a {@link SurfaceControl}. The View hierarchy |
| 33 | * will render in to a root SurfaceControl, and receive input based on the SurfaceControl's |
| 34 | * placement on-screen. The primary usage of this class is to embed a View hierarchy from |
| 35 | * one process in to another. After the SurfaceControlViewHost has been set up in the embedded |
| 36 | * content provider, we can send the {@link SurfaceControlViewHost.SurfacePackage} |
| 37 | * to the host process. The host process can then attach the hierarchy to a SurfaceView within |
| 38 | * its own by calling |
| 39 | * {@link SurfaceView#setChildSurfacePackage}. |
Robert Carr | 9d431e1 | 2018-12-17 13:11:48 -0800 | [diff] [blame] | 40 | */ |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 41 | public class SurfaceControlViewHost { |
Vishnu Nair | 5cf25319 | 2019-11-07 15:33:20 -0800 | [diff] [blame] | 42 | private ViewRootImpl mViewRoot; |
| 43 | private WindowlessWindowManager mWm; |
Evan Rosky | 3f09bb3 | 2019-10-09 19:27:52 -0700 | [diff] [blame] | 44 | |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 45 | private SurfaceControl mSurfaceControl; |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 46 | private IAccessibilityEmbeddedConnection mAccessibilityEmbeddedConnection; |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 47 | |
| 48 | /** |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 49 | * Package encapsulating a Surface hierarchy which contains interactive view |
| 50 | * elements. It's expected to get this object from |
| 51 | * {@link SurfaceControlViewHost#getSurfacePackage} afterwards it can be embedded within |
| 52 | * a SurfaceView by calling {@link SurfaceView#setChildSurfacePackage}. |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 53 | */ |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 54 | public static final class SurfacePackage implements Parcelable { |
| 55 | private final SurfaceControl mSurfaceControl; |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 56 | private final IAccessibilityEmbeddedConnection mAccessibilityEmbeddedConnection; |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 57 | |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 58 | SurfacePackage(SurfaceControl sc, IAccessibilityEmbeddedConnection connection) { |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 59 | mSurfaceControl = sc; |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 60 | mAccessibilityEmbeddedConnection = connection; |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 63 | private SurfacePackage(Parcel in) { |
| 64 | mSurfaceControl = new SurfaceControl(); |
| 65 | mSurfaceControl.readFromParcel(in); |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 66 | mAccessibilityEmbeddedConnection = IAccessibilityEmbeddedConnection.Stub.asInterface( |
| 67 | in.readStrongBinder()); |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Use {@link SurfaceView#setChildSurfacePackage} or manually fix |
| 72 | * accessibility (see SurfaceView implementation). |
| 73 | * @hide |
| 74 | */ |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 75 | public @NonNull SurfaceControl getSurfaceControl() { |
| 76 | return mSurfaceControl; |
| 77 | } |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 78 | |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 79 | /** |
| 80 | * Gets an accessibility embedded connection interface for this SurfaceControlViewHost. |
| 81 | * |
| 82 | * @return {@link IAccessibilityEmbeddedConnection} interface. |
| 83 | * @hide |
| 84 | */ |
| 85 | public IAccessibilityEmbeddedConnection getAccessibilityEmbeddedConnection() { |
| 86 | return mAccessibilityEmbeddedConnection; |
| 87 | } |
| 88 | |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 89 | @Override |
| 90 | public int describeContents() { |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void writeToParcel(@NonNull Parcel out, int flags) { |
| 96 | mSurfaceControl.writeToParcel(out, flags); |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 97 | out.writeStrongBinder(mAccessibilityEmbeddedConnection.asBinder()); |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | public static final @NonNull Creator<SurfacePackage> CREATOR |
| 101 | = new Creator<SurfacePackage>() { |
| 102 | public SurfacePackage createFromParcel(Parcel in) { |
| 103 | return new SurfacePackage(in); |
| 104 | } |
| 105 | public SurfacePackage[] newArray(int size) { |
| 106 | return new SurfacePackage[size]; |
| 107 | } |
| 108 | }; |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Evan Rosky | 3f09bb3 | 2019-10-09 19:27:52 -0700 | [diff] [blame] | 111 | /** @hide */ |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 112 | public SurfaceControlViewHost(@NonNull Context c, @NonNull Display d, |
Evan Rosky | 3f09bb3 | 2019-10-09 19:27:52 -0700 | [diff] [blame] | 113 | @NonNull WindowlessWindowManager wwm) { |
| 114 | mWm = wwm; |
| 115 | mViewRoot = new ViewRootImpl(c, d, mWm); |
Robert Carr | 6d16c8c | 2020-02-21 12:04:31 -0800 | [diff] [blame] | 116 | mViewRoot.forceDisableBLAST(); |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 117 | mAccessibilityEmbeddedConnection = mViewRoot.getAccessibilityEmbeddedConnection(); |
Evan Rosky | 3f09bb3 | 2019-10-09 19:27:52 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 120 | /** |
| 121 | * Construct a new SurfaceControlViewHost. The root Surface will be |
| 122 | * allocated internally and is accessible via getSurfacePackage(). |
| 123 | * |
| 124 | * The {@param hostToken} parameter, primarily used for ANR reporting, |
| 125 | * must be obtained from whomever will be hosting the embedded hierarchy. |
| 126 | * It's accessible from {@link SurfaceView#getHostToken}. |
| 127 | * |
| 128 | * @param context The Context object for your activity or application. |
| 129 | * @param display The Display the hierarchy will be placed on. |
| 130 | * @param hostToken The host token, as discussed above. |
| 131 | */ |
| 132 | public SurfaceControlViewHost(@NonNull Context context, @NonNull Display display, |
| 133 | @Nullable IBinder hostToken) { |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 134 | mSurfaceControl = new SurfaceControl.Builder() |
| 135 | .setContainerLayer() |
| 136 | .setName("SurfaceControlViewHost") |
| 137 | .build(); |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 138 | mWm = new WindowlessWindowManager(context.getResources().getConfiguration(), |
| 139 | mSurfaceControl, hostToken); |
| 140 | mViewRoot = new ViewRootImpl(context, display, mWm); |
Robert Carr | 6d16c8c | 2020-02-21 12:04:31 -0800 | [diff] [blame] | 141 | mViewRoot.forceDisableBLAST(); |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 142 | mAccessibilityEmbeddedConnection = mViewRoot.getAccessibilityEmbeddedConnection(); |
Robert Carr | 9d431e1 | 2018-12-17 13:11:48 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 145 | /** |
| 146 | * Return a SurfacePackage for the root SurfaceControl of the embedded hierarchy. |
| 147 | * Rather than be directly reparented using {@link SurfaceControl.Transaction} this |
| 148 | * SurfacePackage should be passed to {@link SurfaceView#setChildSurfacePackage} |
| 149 | * which will not only reparent the Surface, but ensure the accessibility hierarchies |
| 150 | * are linked. |
| 151 | */ |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 152 | public @Nullable SurfacePackage getSurfacePackage() { |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 153 | if (mSurfaceControl != null && mAccessibilityEmbeddedConnection != null) { |
| 154 | return new SurfacePackage(mSurfaceControl, mAccessibilityEmbeddedConnection); |
Robert Carr | 59b1888 | 2019-12-18 00:38:40 -0800 | [diff] [blame] | 155 | } else { |
| 156 | return null; |
| 157 | } |
| 158 | } |
| 159 | |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 160 | /** |
| 161 | * @hide |
| 162 | */ |
Evan Rosky | 680377e | 2020-01-10 19:12:10 -0800 | [diff] [blame] | 163 | @TestApi |
Robert Carr | 271d9c7 | 2020-03-12 12:39:24 -0700 | [diff] [blame] | 164 | public void setView(@NonNull View view, @NonNull WindowManager.LayoutParams attrs) { |
| 165 | Objects.requireNonNull(view); |
Robert Carr | 9d431e1 | 2018-12-17 13:11:48 -0800 | [diff] [blame] | 166 | mViewRoot.setView(view, attrs, null); |
| 167 | } |
Robert Carr | df28946 | 2019-09-16 13:20:02 -0700 | [diff] [blame] | 168 | |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 169 | /** |
| 170 | * Set the root view of the SurfaceControlViewHost. This view will render in to |
| 171 | * the SurfaceControl, and receive input based on the SurfaceControls positioning on |
| 172 | * screen. It will be laid as if it were in a window of the passed in width and height. |
| 173 | * |
| 174 | * @param view The View to add |
| 175 | * @param width The width to layout the View within, in pixels. |
| 176 | * @param height The height to layout the View within, in pixels. |
| 177 | */ |
Robert Carr | 271d9c7 | 2020-03-12 12:39:24 -0700 | [diff] [blame] | 178 | public void setView(@NonNull View view, int width, int height) { |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 179 | final WindowManager.LayoutParams lp = |
| 180 | new WindowManager.LayoutParams(width, height, |
| 181 | WindowManager.LayoutParams.TYPE_APPLICATION, 0, PixelFormat.TRANSPARENT); |
Robert Carr | 271d9c7 | 2020-03-12 12:39:24 -0700 | [diff] [blame] | 182 | setView(view, lp); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * @return The view passed to setView, or null if none has been passed. |
| 187 | */ |
| 188 | public @Nullable View getView() { |
| 189 | return mViewRoot.getView(); |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /** |
| 193 | * @hide |
| 194 | */ |
Evan Rosky | 680377e | 2020-01-10 19:12:10 -0800 | [diff] [blame] | 195 | @TestApi |
Robert Carr | df28946 | 2019-09-16 13:20:02 -0700 | [diff] [blame] | 196 | public void relayout(WindowManager.LayoutParams attrs) { |
| 197 | mViewRoot.setLayoutParams(attrs, false); |
| 198 | mViewRoot.setReportNextDraw(); |
| 199 | mWm.setCompletionCallback(mViewRoot.mWindow.asBinder(), (SurfaceControl.Transaction t) -> { |
| 200 | t.apply(); |
| 201 | }); |
| 202 | } |
Vishnu Nair | 5cf25319 | 2019-11-07 15:33:20 -0800 | [diff] [blame] | 203 | |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 204 | /** |
| 205 | * Modify the size of the root view. |
| 206 | * |
| 207 | * @param width Width in pixels |
| 208 | * @param height Height in pixels |
| 209 | */ |
| 210 | public void relayout(int width, int height) { |
| 211 | final WindowManager.LayoutParams lp = |
| 212 | new WindowManager.LayoutParams(width, height, |
| 213 | WindowManager.LayoutParams.TYPE_APPLICATION, 0, PixelFormat.TRANSPARENT); |
Robert Carr | 7fdf5d1 | 2020-02-27 12:11:16 -0800 | [diff] [blame] | 214 | relayout(lp); |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Trigger the tear down of the embedded view hierarchy and release the SurfaceControl. |
| 219 | * This will result in onDispatchedFromWindow being dispatched to the embedded view hierarchy |
| 220 | * and render the object unusable. |
| 221 | */ |
| 222 | public void release() { |
Vishnu Nair | 5cf25319 | 2019-11-07 15:33:20 -0800 | [diff] [blame] | 223 | mViewRoot.dispatchDetachedFromWindow(); |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 224 | mSurfaceControl.release(); |
Vishnu Nair | 5cf25319 | 2019-11-07 15:33:20 -0800 | [diff] [blame] | 225 | } |
Evan Rosky | 3f09bb3 | 2019-10-09 19:27:52 -0700 | [diff] [blame] | 226 | |
| 227 | /** |
| 228 | * Tell this viewroot to clean itself up. |
| 229 | * @hide |
| 230 | */ |
| 231 | public void die() { |
| 232 | mViewRoot.die(false /* immediate */); |
| 233 | } |
Robert Carr | 9d431e1 | 2018-12-17 13:11:48 -0800 | [diff] [blame] | 234 | } |