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

Commit 44912ff9 authored by Valentin Iftime's avatar Valentin Iftime
Browse files

Get WindowManager only when needed

 Do not keep any WM references in ToastPresenter, only get on demand.

Test: atest WindowUntrustedTouchTest
 atest ToastWindowTest
 atest ToastPresenterTest
 atest ToastUITest
 Manual run: the stress-test app attached in the bug

Bug: 309562471
Change-Id: I30d048b01c0890d2fff3bc7684a20defbf589ffb
parent 0da9b54f
Loading
Loading
Loading
Loading
+13 −4
Original line number Diff line number Diff line
@@ -91,7 +91,6 @@ public class ToastPresenter {

    private final WeakReference<Context> mContext;
    private final Resources mResources;
    private final WeakReference<WindowManager> mWindowManager;
    private final IAccessibilityManager mAccessibilityManagerService;
    private final INotificationManager mNotificationManager;
    private final String mPackageName;
@@ -104,7 +103,6 @@ public class ToastPresenter {
            INotificationManager notificationManager, String packageName) {
        mContext = new WeakReference<>(context);
        mResources = context.getResources();
        mWindowManager = new WeakReference<>(context.getSystemService(WindowManager.class));
        mNotificationManager = notificationManager;
        mPackageName = packageName;
        mContextPackageName = context.getPackageName();
@@ -274,7 +272,7 @@ public class ToastPresenter {
    public void hide(@Nullable ITransientNotificationCallback callback) {
        checkState(mView != null, "No toast to hide.");

        final WindowManager windowManager = mWindowManager.get();
        final WindowManager windowManager = getWindowManager(mView);
        if (mView.getParent() != null && windowManager != null) {
            windowManager.removeViewImmediate(mView);
        }
@@ -295,6 +293,17 @@ public class ToastPresenter {
        mToken = null;
    }

    private WindowManager getWindowManager(View view) {
        Context context = mContext.get();
        if (context == null && view != null) {
            context = view.getContext();
        }
        if (context != null) {
            return context.getSystemService(WindowManager.class);
        }
        return null;
    }

    /**
     * Sends {@link AccessibilityEvent#TYPE_NOTIFICATION_STATE_CHANGED} event if accessibility is
     * enabled.
@@ -331,7 +340,7 @@ public class ToastPresenter {
    }

    private void addToastView() {
        final WindowManager windowManager = mWindowManager.get();
        final WindowManager windowManager = getWindowManager(mView);
        if (windowManager == null) {
            return;
        }