Move initialization from onCreate to attachBaseContext

Previously we initialize WindowManager and associate DisplayArea in onCreate,
but apps may use WindowManager before WindowProviderService#onCreate.
The code may be like:
```
public void onCreate() {
    WindowManager wm = getSystemService(Windowmanager.class);
    wm.getCurrentWindowMetrics().getBounds(); // <--- crash here
    super.onCreate()
    ...
}
```
This CL move the initialization step to attachBaseContext to prevent
the crash.

Test: atest WindowContextTests
Test: run gsi_avd/boot_health/vendor_img_rvc on ABTD
Bug: 191635763
Change-Id: Id8bad73a749813a90b307ac2b4d020f740631ea3
This commit is contained in:
Charles Chen
2021-06-21 20:37:47 +08:00
parent 0c8ec087f9
commit dd4a748af0

View File

@@ -53,6 +53,7 @@ public abstract class WindowProviderService extends Service {
private final WindowTokenClient mWindowToken = new WindowTokenClient();
private final WindowContextController mController = new WindowContextController(mWindowToken);
private WindowManager mWindowManager;
private boolean mInitialized;
/**
* Returns the type of this {@link WindowProviderService}.
@@ -122,13 +123,17 @@ public abstract class WindowProviderService extends Service {
return context.createTokenContext(mWindowToken, display);
}
@CallSuper
/** @hide */
@Override
public void onCreate() {
super.onCreate();
mWindowToken.attachContext(this);
mController.attachToDisplayArea(getWindowType(), getDisplayId(), getWindowContextOptions());
mWindowManager = WindowManagerImpl.createWindowContextWindowManager(this);
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(newBase);
if (!mInitialized) {
mWindowToken.attachContext(this);
mController.attachToDisplayArea(getWindowType(), getDisplayId(),
getWindowContextOptions());
mWindowManager = WindowManagerImpl.createWindowContextWindowManager(this);
mInitialized = true;
}
}
@SuppressLint("OnNameExpected")