| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.annotation.TestApi; |
| 22 |
import android.content.Context; |
| 23 |
import android.content.res.Configuration; |
| 24 |
import android.graphics.PixelFormat; |
| 25 |
import android.graphics.Rect; |
| 26 |
import android.os.IBinder; |
| 27 |
import android.os.Parcel; |
| 28 |
import android.os.Parcelable; |
| 29 |
import android.os.RemoteException; |
| 30 |
import android.util.Log; |
| 31 |
import android.view.accessibility.IAccessibilityEmbeddedConnection; |
| 32 |
import android.window.WindowTokenClient; |
| 33 |
|
| 34 |
import java.util.Objects; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class SurfaceControlViewHost { |
| 47 |
private final static String TAG = "SurfaceControlViewHost"; |
| 48 |
private final ViewRootImpl mViewRoot; |
| 49 |
private WindowlessWindowManager mWm; |
| 50 |
|
| 51 |
private SurfaceControl mSurfaceControl; |
| 52 |
private IAccessibilityEmbeddedConnection mAccessibilityEmbeddedConnection; |
| 53 |
private boolean mReleased = false; |
| 54 |
|
| 55 |
private final class ISurfaceControlViewHostImpl extends ISurfaceControlViewHost.Stub { |
| 56 |
@Override |
| 57 |
public void onConfigurationChanged(Configuration configuration) { |
| 58 |
if (mViewRoot == null) { |
| 59 |
return; |
| 60 |
} |
| 61 |
mViewRoot.mHandler.post(() -> { |
| 62 |
if (mWm != null) { |
| 63 |
mWm.setConfiguration(configuration); |
| 64 |
} |
| 65 |
if (mViewRoot != null) { |
| 66 |
mViewRoot.forceWmRelayout(); |
| 67 |
} |
| 68 |
}); |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public void onDispatchDetachedFromWindow() { |
| 73 |
if (mViewRoot == null) { |
| 74 |
return; |
| 75 |
} |
| 76 |
mViewRoot.mHandler.post(() -> { |
| 77 |
release(); |
| 78 |
}); |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public void onInsetsChanged(InsetsState state, Rect frame) { |
| 83 |
if (mViewRoot != null) { |
| 84 |
mViewRoot.mHandler.post(() -> { |
| 85 |
mViewRoot.setOverrideInsetsFrame(frame); |
| 86 |
}); |
| 87 |
} |
| 88 |
mWm.setInsetsState(state); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
private ISurfaceControlViewHost mRemoteInterface = new ISurfaceControlViewHostImpl(); |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public static final class SurfacePackage implements Parcelable { |
| 117 |
private SurfaceControl mSurfaceControl; |
| 118 |
private final IAccessibilityEmbeddedConnection mAccessibilityEmbeddedConnection; |
| 119 |
private final IBinder mInputToken; |
| 120 |
private final ISurfaceControlViewHost mRemoteInterface; |
| 121 |
|
| 122 |
SurfacePackage(SurfaceControl sc, IAccessibilityEmbeddedConnection connection, |
| 123 |
IBinder inputToken, ISurfaceControlViewHost ri) { |
| 124 |
mSurfaceControl = sc; |
| 125 |
mAccessibilityEmbeddedConnection = connection; |
| 126 |
mInputToken = inputToken; |
| 127 |
mRemoteInterface = ri; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public SurfacePackage(@NonNull SurfacePackage other) { |
| 141 |
SurfaceControl otherSurfaceControl = other.mSurfaceControl; |
| 142 |
if (otherSurfaceControl != null && otherSurfaceControl.isValid()) { |
| 143 |
mSurfaceControl = new SurfaceControl(); |
| 144 |
mSurfaceControl.copyFrom(otherSurfaceControl, "SurfacePackage"); |
| 145 |
} |
| 146 |
mAccessibilityEmbeddedConnection = other.mAccessibilityEmbeddedConnection; |
| 147 |
mInputToken = other.mInputToken; |
| 148 |
mRemoteInterface = other.mRemoteInterface; |
| 149 |
} |
| 150 |
|
| 151 |
private SurfacePackage(Parcel in) { |
| 152 |
mSurfaceControl = new SurfaceControl(); |
| 153 |
mSurfaceControl.readFromParcel(in); |
| 154 |
mAccessibilityEmbeddedConnection = IAccessibilityEmbeddedConnection.Stub.asInterface( |
| 155 |
in.readStrongBinder()); |
| 156 |
mInputToken = in.readStrongBinder(); |
| 157 |
mRemoteInterface = ISurfaceControlViewHost.Stub.asInterface( |
| 158 |
in.readStrongBinder()); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public @NonNull SurfaceControl getSurfaceControl() { |
| 167 |
return mSurfaceControl; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public IAccessibilityEmbeddedConnection getAccessibilityEmbeddedConnection() { |
| 177 |
return mAccessibilityEmbeddedConnection; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public ISurfaceControlViewHost getRemoteInterface() { |
| 184 |
return mRemoteInterface; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public void notifyConfigurationChanged(@NonNull Configuration c) { |
| 206 |
try { |
| 207 |
getRemoteInterface().onConfigurationChanged(c); |
| 208 |
} catch (RemoteException e) { |
| 209 |
e.rethrowAsRuntimeException(); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public void notifyDetachedFromWindow() { |
| 218 |
try { |
| 219 |
getRemoteInterface().onDispatchDetachedFromWindow(); |
| 220 |
} catch (RemoteException e) { |
| 221 |
e.rethrowAsRuntimeException(); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
@Override |
| 226 |
public int describeContents() { |
| 227 |
return 0; |
| 228 |
} |
| 229 |
|
| 230 |
@Override |
| 231 |
public void writeToParcel(@NonNull Parcel out, int flags) { |
| 232 |
mSurfaceControl.writeToParcel(out, flags); |
| 233 |
out.writeStrongBinder(mAccessibilityEmbeddedConnection.asBinder()); |
| 234 |
out.writeStrongBinder(mInputToken); |
| 235 |
out.writeStrongBinder(mRemoteInterface.asBinder()); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public void release() { |
| 245 |
if (mSurfaceControl != null) { |
| 246 |
mSurfaceControl.release(); |
| 247 |
} |
| 248 |
mSurfaceControl = null; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public IBinder getInputToken() { |
| 257 |
return mInputToken; |
| 258 |
} |
| 259 |
|
| 260 |
public static final @NonNull Creator<SurfacePackage> CREATOR |
| 261 |
= new Creator<SurfacePackage>() { |
| 262 |
public SurfacePackage createFromParcel(Parcel in) { |
| 263 |
return new SurfacePackage(in); |
| 264 |
} |
| 265 |
public SurfacePackage[] newArray(int size) { |
| 266 |
return new SurfacePackage[size]; |
| 267 |
} |
| 268 |
}; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
public SurfaceControlViewHost(@NonNull Context c, @NonNull Display d, |
| 273 |
@NonNull WindowlessWindowManager wwm) { |
| 274 |
mWm = wwm; |
| 275 |
mViewRoot = new ViewRootImpl(c, d, mWm, new WindowlessWindowLayout()); |
| 276 |
addConfigCallback(c, d); |
| 277 |
|
| 278 |
WindowManagerGlobal.getInstance().addWindowlessRoot(mViewRoot); |
| 279 |
|
| 280 |
mAccessibilityEmbeddedConnection = mViewRoot.getAccessibilityEmbeddedConnection(); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
public SurfaceControlViewHost(@NonNull Context context, @NonNull Display display, |
| 296 |
@Nullable IBinder hostToken) { |
| 297 |
mSurfaceControl = new SurfaceControl.Builder() |
| 298 |
.setContainerLayer() |
| 299 |
.setName("SurfaceControlViewHost") |
| 300 |
.setCallsite("SurfaceControlViewHost") |
| 301 |
.build(); |
| 302 |
mWm = new WindowlessWindowManager(context.getResources().getConfiguration(), |
| 303 |
mSurfaceControl, hostToken); |
| 304 |
|
| 305 |
mViewRoot = new ViewRootImpl(context, display, mWm, new WindowlessWindowLayout()); |
| 306 |
addConfigCallback(context, display); |
| 307 |
|
| 308 |
WindowManagerGlobal.getInstance().addWindowlessRoot(mViewRoot); |
| 309 |
|
| 310 |
mAccessibilityEmbeddedConnection = mViewRoot.getAccessibilityEmbeddedConnection(); |
| 311 |
} |
| 312 |
|
| 313 |
private void addConfigCallback(Context c, Display d) { |
| 314 |
final IBinder token = c.getWindowContextToken(); |
| 315 |
mViewRoot.addConfigCallback((conf) -> { |
| 316 |
if (token instanceof WindowTokenClient) { |
| 317 |
final WindowTokenClient w = (WindowTokenClient) token; |
| 318 |
w.onConfigurationChanged(conf, d.getDisplayId(), true); |
| 319 |
} |
| 320 |
}); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
@Override |
| 327 |
protected void finalize() throws Throwable { |
| 328 |
if (mReleased) { |
| 329 |
return; |
| 330 |
} |
| 331 |
Log.e(TAG, "SurfaceControlViewHost finalized without being released: " + this); |
| 332 |
|
| 333 |
mViewRoot.die(false ); |
| 334 |
WindowManagerGlobal.getInstance().removeWindowlessRoot(mViewRoot); |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
public @Nullable SurfacePackage getSurfacePackage() { |
| 345 |
if (mSurfaceControl != null && mAccessibilityEmbeddedConnection != null) { |
| 346 |
return new SurfacePackage(new SurfaceControl(mSurfaceControl, "getSurfacePackage"), |
| 347 |
mAccessibilityEmbeddedConnection, |
| 348 |
mWm.getFocusGrantToken(), mRemoteInterface); |
| 349 |
} else { |
| 350 |
return null; |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
public void setView(@NonNull View view, int width, int height) { |
| 364 |
final WindowManager.LayoutParams lp = |
| 365 |
new WindowManager.LayoutParams(width, height, |
| 366 |
WindowManager.LayoutParams.TYPE_APPLICATION, 0, PixelFormat.TRANSPARENT); |
| 367 |
setView(view, lp); |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
@TestApi |
| 374 |
public void setView(@NonNull View view, @NonNull WindowManager.LayoutParams attrs) { |
| 375 |
Objects.requireNonNull(view); |
| 376 |
attrs.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED; |
| 377 |
view.setLayoutParams(attrs); |
| 378 |
mViewRoot.setView(view, attrs, null); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
public @Nullable View getView() { |
| 385 |
return mViewRoot.getView(); |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
public IWindow getWindowToken() { |
| 393 |
return mViewRoot.mWindow; |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
public @NonNull WindowlessWindowManager getWindowlessWM() { |
| 401 |
return mWm; |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
@TestApi |
| 408 |
public void relayout(WindowManager.LayoutParams attrs) { |
| 409 |
relayout(attrs, SurfaceControl.Transaction::apply); |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
public void relayout(WindowManager.LayoutParams attrs, |
| 417 |
WindowlessWindowManager.ResizeCompleteCallback callback) { |
| 418 |
mViewRoot.setLayoutParams(attrs, false); |
| 419 |
mViewRoot.setReportNextDraw(true , "scvh_relayout"); |
| 420 |
mWm.setCompletionCallback(mViewRoot.mWindow.asBinder(), callback); |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
public void relayout(int width, int height) { |
| 430 |
final WindowManager.LayoutParams lp = |
| 431 |
new WindowManager.LayoutParams(width, height, |
| 432 |
WindowManager.LayoutParams.TYPE_APPLICATION, 0, PixelFormat.TRANSPARENT); |
| 433 |
relayout(lp); |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
public void release() { |
| 442 |
|
| 443 |
mViewRoot.die(true ); |
| 444 |
WindowManagerGlobal.getInstance().removeWindowlessRoot(mViewRoot); |
| 445 |
mReleased = true; |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
public IBinder getFocusGrantToken() { |
| 452 |
return mWm.getFocusGrantToken(); |
| 453 |
} |
| 454 |
} |