1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
|
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.view;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.TestApi;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.os.IBinder;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import android.util.Log;
import android.view.accessibility.IAccessibilityEmbeddedConnection;
import android.window.WindowTokenClient;
import java.util.Objects;
/**
* Utility class for adding a View hierarchy to a {@link SurfaceControl}. The View hierarchy
* will render in to a root SurfaceControl, and receive input based on the SurfaceControl's
* placement on-screen. The primary usage of this class is to embed a View hierarchy from
* one process in to another. After the SurfaceControlViewHost has been set up in the embedded
* content provider, we can send the {@link SurfaceControlViewHost.SurfacePackage}
* to the host process. The host process can then attach the hierarchy to a SurfaceView within
* its own by calling
* {@link SurfaceView#setChildSurfacePackage}.
*/
public class SurfaceControlViewHost {
private final static String TAG = "SurfaceControlViewHost";
private final ViewRootImpl mViewRoot;
private WindowlessWindowManager mWm;
private SurfaceControl mSurfaceControl;
private IAccessibilityEmbeddedConnection mAccessibilityEmbeddedConnection;
private boolean mReleased = false;
private final class ISurfaceControlViewHostImpl extends ISurfaceControlViewHost.Stub {
@Override
public void onConfigurationChanged(Configuration configuration) {
if (mViewRoot == null) {
return;
}
mViewRoot.mHandler.post(() -> {
if (mWm != null) {
mWm.setConfiguration(configuration);
}
if (mViewRoot != null) {
mViewRoot.forceWmRelayout();
}
});
}
@Override
public void onDispatchDetachedFromWindow() {
if (mViewRoot == null) {
return;
}
mViewRoot.mHandler.post(() -> {
release();
});
}
@Override
public void onInsetsChanged(InsetsState state, Rect frame) {
if (mViewRoot != null) {
mViewRoot.mHandler.post(() -> {
mViewRoot.setOverrideInsetsFrame(frame);
});
}
mWm.setInsetsState(state);
}
}
private ISurfaceControlViewHost mRemoteInterface = new ISurfaceControlViewHostImpl();
/**
* Package encapsulating a Surface hierarchy which contains interactive view
* elements. It's expected to get this object from
* {@link SurfaceControlViewHost#getSurfacePackage} afterwards it can be embedded within
* a SurfaceView by calling {@link SurfaceView#setChildSurfacePackage}.
*
* Note that each {@link SurfacePackage} must be released by calling
* {@link SurfacePackage#release}. However, if you use the recommended flow,
* the framework will automatically handle the lifetime for you.
*
* 1. When sending the package to the remote process, return it from an AIDL method
* or manually use FLAG_WRITE_RETURN_VALUE in writeToParcel. This will automatically
* release the package in the local process.
* 2. In the remote process, consume the package using SurfaceView. This way the
* SurfaceView will take over the lifetime and call {@link SurfacePackage#release}
* for the user.
*
* One final note: The {@link SurfacePackage} lifetime is totally de-coupled
* from the lifetime of the underlying {@link SurfaceControlViewHost}. Regardless
* of the lifetime of the package the user should still call
* {@link SurfaceControlViewHost#release} when finished.
*/
public static final class SurfacePackage implements Parcelable {
private SurfaceControl mSurfaceControl;
private final IAccessibilityEmbeddedConnection mAccessibilityEmbeddedConnection;
private final IBinder mInputToken;
private final ISurfaceControlViewHost mRemoteInterface;
SurfacePackage(SurfaceControl sc, IAccessibilityEmbeddedConnection connection,
IBinder inputToken, ISurfaceControlViewHost ri) {
mSurfaceControl = sc;
mAccessibilityEmbeddedConnection = connection;
mInputToken = inputToken;
mRemoteInterface = ri;
}
/**
* Constructs a copy of {@code SurfacePackage} with an independent lifetime.
*
* The caller can use this to create an independent copy in situations where ownership of
* the {@code SurfacePackage} would be transferred elsewhere, such as attaching to a
* {@code SurfaceView}, returning as {@code Binder} result value, etc. The caller is
* responsible for releasing this copy when its done.
*
* @param other {@code SurfacePackage} to create a copy of.
*/
public SurfacePackage(@NonNull SurfacePackage other) {
SurfaceControl otherSurfaceControl = other.mSurfaceControl;
if (otherSurfaceControl != null && otherSurfaceControl.isValid()) {
mSurfaceControl = new SurfaceControl();
mSurfaceControl.copyFrom(otherSurfaceControl, "SurfacePackage");
}
mAccessibilityEmbeddedConnection = other.mAccessibilityEmbeddedConnection;
mInputToken = other.mInputToken;
mRemoteInterface = other.mRemoteInterface;
}
private SurfacePackage(Parcel in) {
mSurfaceControl = new SurfaceControl();
mSurfaceControl.readFromParcel(in);
mAccessibilityEmbeddedConnection = IAccessibilityEmbeddedConnection.Stub.asInterface(
in.readStrongBinder());
mInputToken = in.readStrongBinder();
mRemoteInterface = ISurfaceControlViewHost.Stub.asInterface(
in.readStrongBinder());
}
/**
* Use {@link SurfaceView#setChildSurfacePackage} or manually fix
* accessibility (see SurfaceView implementation).
* @hide
*/
public @NonNull SurfaceControl getSurfaceControl() {
return mSurfaceControl;
}
/**
* Gets an accessibility embedded connection interface for this SurfaceControlViewHost.
*
* @return {@link IAccessibilityEmbeddedConnection} interface.
* @hide
*/
public IAccessibilityEmbeddedConnection getAccessibilityEmbeddedConnection() {
return mAccessibilityEmbeddedConnection;
}
/**
* @hide
*/
public ISurfaceControlViewHost getRemoteInterface() {
return mRemoteInterface;
}
/**
* Forward a configuration to the remote SurfaceControlViewHost.
* This will cause View#onConfigurationChanged to be invoked on the remote
* end. This does not automatically cause the SurfaceControlViewHost
* to be resized. The root View of a SurfaceControlViewHost
* is more akin to a PopupWindow in that the size is user specified
* independent of configuration width and height.
*
* In order to receive the configuration change via
* {@link View#onConfigurationChanged}, the context used with the
* SurfaceControlViewHost and it's embedded view hierarchy must
* be a WindowContext obtained from {@link Context#createWindowContext}.
*
* If a regular service context is used, then your embedded view hierarchy
* will always perceive the global configuration.
*
* @param c The configuration to forward
*/
public void notifyConfigurationChanged(@NonNull Configuration c) {
try {
getRemoteInterface().onConfigurationChanged(c);
} catch (RemoteException e) {
e.rethrowAsRuntimeException();
}
}
/**
* Tear down the remote SurfaceControlViewHost and cause
* View#onDetachedFromWindow to be invoked on the other side.
*/
public void notifyDetachedFromWindow() {
try {
getRemoteInterface().onDispatchDetachedFromWindow();
} catch (RemoteException e) {
e.rethrowAsRuntimeException();
}
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(@NonNull Parcel out, int flags) {
mSurfaceControl.writeToParcel(out, flags);
out.writeStrongBinder(mAccessibilityEmbeddedConnection.asBinder());
out.writeStrongBinder(mInputToken);
out.writeStrongBinder(mRemoteInterface.asBinder());
}
/**
* Release the {@link SurfaceControl} associated with this package.
* It's not necessary to call this if you pass the package to
* {@link SurfaceView#setChildSurfacePackage} as {@link SurfaceView} will
* take ownership in that case.
*/
public void release() {
if (mSurfaceControl != null) {
mSurfaceControl.release();
}
mSurfaceControl = null;
}
/**
* Returns an input token used which can be used to request focus on the embedded surface.
*
* @hide
*/
public IBinder getInputToken() {
return mInputToken;
}
public static final @NonNull Creator<SurfacePackage> CREATOR
= new Creator<SurfacePackage>() {
public SurfacePackage createFromParcel(Parcel in) {
return new SurfacePackage(in);
}
public SurfacePackage[] newArray(int size) {
return new SurfacePackage[size];
}
};
}
/** @hide */
public SurfaceControlViewHost(@NonNull Context c, @NonNull Display d,
@NonNull WindowlessWindowManager wwm) {
mWm = wwm;
mViewRoot = new ViewRootImpl(c, d, mWm, new WindowlessWindowLayout());
addConfigCallback(c, d);
WindowManagerGlobal.getInstance().addWindowlessRoot(mViewRoot);
mAccessibilityEmbeddedConnection = mViewRoot.getAccessibilityEmbeddedConnection();
}
/**
* Construct a new SurfaceControlViewHost. The root Surface will be
* allocated internally and is accessible via getSurfacePackage().
*
* The {@param hostToken} parameter, primarily used for ANR reporting,
* must be obtained from whomever will be hosting the embedded hierarchy.
* It's accessible from {@link SurfaceView#getHostToken}.
*
* @param context The Context object for your activity or application.
* @param display The Display the hierarchy will be placed on.
* @param hostToken The host token, as discussed above.
*/
public SurfaceControlViewHost(@NonNull Context context, @NonNull Display display,
@Nullable IBinder hostToken) {
mSurfaceControl = new SurfaceControl.Builder()
.setContainerLayer()
.setName("SurfaceControlViewHost")
.setCallsite("SurfaceControlViewHost")
.build();
mWm = new WindowlessWindowManager(context.getResources().getConfiguration(),
mSurfaceControl, hostToken);
mViewRoot = new ViewRootImpl(context, display, mWm, new WindowlessWindowLayout());
addConfigCallback(context, display);
WindowManagerGlobal.getInstance().addWindowlessRoot(mViewRoot);
mAccessibilityEmbeddedConnection = mViewRoot.getAccessibilityEmbeddedConnection();
}
private void addConfigCallback(Context c, Display d) {
final IBinder token = c.getWindowContextToken();
mViewRoot.addConfigCallback((conf) -> {
if (token instanceof WindowTokenClient) {
final WindowTokenClient w = (WindowTokenClient) token;
w.onConfigurationChanged(conf, d.getDisplayId(), true);
}
});
}
/**
* @hide
*/
@Override
protected void finalize() throws Throwable {
if (mReleased) {
return;
}
Log.e(TAG, "SurfaceControlViewHost finalized without being released: " + this);
// We aren't on the UI thread here so we need to pass false to doDie
mViewRoot.die(false /* immediate */);
WindowManagerGlobal.getInstance().removeWindowlessRoot(mViewRoot);
}
/**
* Return a SurfacePackage for the root SurfaceControl of the embedded hierarchy.
* Rather than be directly reparented using {@link SurfaceControl.Transaction} this
* SurfacePackage should be passed to {@link SurfaceView#setChildSurfacePackage}
* which will not only reparent the Surface, but ensure the accessibility hierarchies
* are linked.
*/
public @Nullable SurfacePackage getSurfacePackage() {
if (mSurfaceControl != null && mAccessibilityEmbeddedConnection != null) {
return new SurfacePackage(new SurfaceControl(mSurfaceControl, "getSurfacePackage"),
mAccessibilityEmbeddedConnection,
mWm.getFocusGrantToken(), mRemoteInterface);
} else {
return null;
}
}
/**
* Set the root view of the SurfaceControlViewHost. This view will render in to
* the SurfaceControl, and receive input based on the SurfaceControls positioning on
* screen. It will be laid as if it were in a window of the passed in width and height.
*
* @param view The View to add
* @param width The width to layout the View within, in pixels.
* @param height The height to layout the View within, in pixels.
*/
public void setView(@NonNull View view, int width, int height) {
final WindowManager.LayoutParams lp =
new WindowManager.LayoutParams(width, height,
WindowManager.LayoutParams.TYPE_APPLICATION, 0, PixelFormat.TRANSPARENT);
setView(view, lp);
}
/**
* @hide
*/
@TestApi
public void setView(@NonNull View view, @NonNull WindowManager.LayoutParams attrs) {
Objects.requireNonNull(view);
attrs.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
view.setLayoutParams(attrs);
mViewRoot.setView(view, attrs, null);
}
/**
* @return The view passed to setView, or null if none has been passed.
*/
public @Nullable View getView() {
return mViewRoot.getView();
}
/**
* @return the ViewRootImpl wrapped by this host.
* @hide
*/
public IWindow getWindowToken() {
return mViewRoot.mWindow;
}
/**
* @return the WindowlessWindowManager instance that this host is attached to.
* @hide
*/
public @NonNull WindowlessWindowManager getWindowlessWM() {
return mWm;
}
/**
* @hide
*/
@TestApi
public void relayout(WindowManager.LayoutParams attrs) {
relayout(attrs, SurfaceControl.Transaction::apply);
}
/**
* Forces relayout and draw and allows to set a custom callback when it is finished
* @hide
*/
public void relayout(WindowManager.LayoutParams attrs,
WindowlessWindowManager.ResizeCompleteCallback callback) {
mViewRoot.setLayoutParams(attrs, false);
mViewRoot.setReportNextDraw(true /* syncBuffer */, "scvh_relayout");
mWm.setCompletionCallback(mViewRoot.mWindow.asBinder(), callback);
}
/**
* Modify the size of the root view.
*
* @param width Width in pixels
* @param height Height in pixels
*/
public void relayout(int width, int height) {
final WindowManager.LayoutParams lp =
new WindowManager.LayoutParams(width, height,
WindowManager.LayoutParams.TYPE_APPLICATION, 0, PixelFormat.TRANSPARENT);
relayout(lp);
}
/**
* Trigger the tear down of the embedded view hierarchy and release the SurfaceControl.
* This will result in onDispatchedFromWindow being dispatched to the embedded view hierarchy
* and render the object unusable.
*/
public void release() {
// ViewRoot will release mSurfaceControl for us.
mViewRoot.die(true /* immediate */);
WindowManagerGlobal.getInstance().removeWindowlessRoot(mViewRoot);
mReleased = true;
}
/**
* @hide
*/
public IBinder getFocusGrantToken() {
return mWm.getFocusGrantToken();
}
}
|