Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit dd4a748a authored by Charles Chen's avatar Charles Chen
Browse files

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
parent 0c8ec087
Loading
Loading
Loading
Loading
+11 −6
Original line number Diff line number Diff line
@@ -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();
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
        if (!mInitialized) {
            mWindowToken.attachContext(this);
        mController.attachToDisplayArea(getWindowType(), getDisplayId(), getWindowContextOptions());
            mController.attachToDisplayArea(getWindowType(), getDisplayId(),
                    getWindowContextOptions());
            mWindowManager = WindowManagerImpl.createWindowContextWindowManager(this);
            mInitialized = true;
        }
    }

    @SuppressLint("OnNameExpected")