diff options
| author | 2023-12-04 13:18:19 +0000 | |
|---|---|---|
| committer | 2023-12-04 13:18:19 +0000 | |
| commit | 6ea41f315c98e068f67be6ebc839074814024ffb (patch) | |
| tree | 574072845a0011d89ce0e5401bc9dbc20a855468 | |
| parent | ee29988423fcf75482d36ba6dcabfd065d1a7ce7 (diff) | |
| parent | 44912ff9b1e492d67adc32ff23f6018306d7da0f (diff) | |
Merge "Get WindowManager only when needed" into main
| -rw-r--r-- | core/java/android/widget/ToastPresenter.java | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/core/java/android/widget/ToastPresenter.java b/core/java/android/widget/ToastPresenter.java index 6884e639f9c7..6963237fc19b 100644 --- a/core/java/android/widget/ToastPresenter.java +++ b/core/java/android/widget/ToastPresenter.java @@ -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; } |