Fix NPE on rootViewImpl.getView
We have two instances which can (very rarely) trigger an NPE, when we
check if we have a non-null rootViewImpl with a non-null view, and use
that view later. This code is in a synchronized block, but that lock
does not protect the viewRootImpl's getView call, instead we should just
save a final reference to it.
Bug: 313399081
Test: n/a
Change-Id: I047b50d26972e6407b04d7cc09ef9110c5fd4b41
diff --git a/core/java/android/view/inputmethod/InputMethodManager.java b/core/java/android/view/inputmethod/InputMethodManager.java
index 6d7a543..ac9ad2d 100644
--- a/core/java/android/view/inputmethod/InputMethodManager.java
+++ b/core/java/android/view/inputmethod/InputMethodManager.java
@@ -2198,7 +2198,8 @@
Log.w(TAG, "showSoftInputUnchecked() is a hidden method, which will be"
+ " removed soon. If you are using androidx.appcompat.widget.SearchView,"
+ " please update to version 26.0 or newer version.");
- if (mCurRootView == null || mCurRootView.getView() == null) {
+ final View rootView = mCurRootView != null ? mCurRootView.getView() : null;
+ if (rootView == null) {
ImeTracker.forLogging().onFailed(statsToken, ImeTracker.PHASE_CLIENT_VIEW_SERVED);
Log.w(TAG, "No current root view, ignoring showSoftInputUnchecked()");
return;
@@ -2211,7 +2212,7 @@
mH.executeOrSendMessage(Message.obtain(mH, MSG_ON_SHOW_REQUESTED));
IInputMethodManagerGlobalInvoker.showSoftInput(
mClient,
- mCurRootView.getView().getWindowToken(),
+ rootView.getWindowToken(),
statsToken,
flags,
mCurRootView.getLastClickToolType(),
@@ -3121,7 +3122,8 @@
ActivityThread::currentApplication);
synchronized (mH) {
- if (mCurRootView == null || mCurRootView.getView() == null) {
+ final View rootView = mCurRootView != null ? mCurRootView.getView() : null;
+ if (rootView == null) {
ImeTracker.forLogging().onFailed(statsToken, ImeTracker.PHASE_CLIENT_VIEW_SERVED);
ImeTracker.forLatency().onHideFailed(statsToken,
ImeTracker.PHASE_CLIENT_VIEW_SERVED, ActivityThread::currentApplication);
@@ -3133,7 +3135,7 @@
IInputMethodManagerGlobalInvoker.hideSoftInput(
mClient,
- mCurRootView.getView().getWindowToken(),
+ rootView.getWindowToken(),
statsToken,
HIDE_NOT_ALWAYS,
null,