Fix NPE in IMM setInteractive

This fixes a null pointer exception in InputMethodManager when handling
a setInteractive call, which was triggered when the root view of the
focused window has been removed between posting this call on the
UI thread and actually handling it.

This also improves the performance of this call by first checking if we
are already on the UI thread, in which case we can handle it directly.

Bug: 283146523
Test: n/a
Change-Id: I1a0734bbb7e5b4b28f1dbe73f7e7a3e8e4441396
diff --git a/core/java/android/view/inputmethod/InputMethodManager.java b/core/java/android/view/inputmethod/InputMethodManager.java
index eeab005..589b7a3 100644
--- a/core/java/android/view/inputmethod/InputMethodManager.java
+++ b/core/java/android/view/inputmethod/InputMethodManager.java
@@ -1176,24 +1176,33 @@
                         mActive = interactive;
                         mFullscreenMode = fullscreen;
                         if (interactive) {
+                            // Find the next view focus to start the input connection when the
+                            // device was interactive.
                             final View rootView =
                                     mCurRootView != null ? mCurRootView.getView() : null;
                             if (rootView == null) {
+                                // No window focused or view was removed, ignore request.
                                 return;
                             }
-                            // Find the next view focus to start the input connection when the
-                            // device was interactive.
                             final ViewRootImpl currentViewRootImpl = mCurRootView;
+                            // Post this on UI thread as required for view focus code.
                             rootView.post(() -> {
                                 synchronized (mH) {
                                     if (mCurRootView != currentViewRootImpl) {
+                                        // Focused window changed since posting, ignore request.
                                         return;
                                     }
                                 }
-                                final View focusedView = currentViewRootImpl.getView().findFocus();
+                                final View curRootView = currentViewRootImpl.getView();
+                                if (curRootView == null) {
+                                    // View was removed, ignore request.
+                                    return;
+                                }
+                                final View focusedView = curRootView.findFocus();
                                 onViewFocusChangedInternal(focusedView, focusedView != null);
                             });
                         } else {
+                            // Finish input connection when device becomes non-interactive.
                             finishInputLocked();
                             if (isImeSessionAvailableLocked()) {
                                 mCurBindState.mImeSession.finishInput();