From 0d7aff8ab22b25e4215bee1a1f1da830a4d8fbfe Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Fri, 10 Feb 2017 00:40:51 -0800 Subject: [PATCH] Inline IMMS#resetAllInternalStateLocked() Currently IMMS#resetAllInternalStateLocked(boolean, boolean) has only two call sites, and the first parameter is the constant for each call site. We can get much simpler code by inlining the method in question into those two call sites. It actually revealed that UNBIND_REASON_RESET_IME should have been called UNBIND_REASON_SWITCH_USER. Test: no behavior change Test: adb shell dumpsys input_method | grep "mMethodMapUpdateCount=" to make sure that switching users can cause the same count increase. Test: adb shell dumpsys input_method | grep "mMethodMapUpdateCount=" to make sure that switching the device locale can cause the same count increase. Bug: 35079353 Change-Id: I63388402369f58d11fdb21b508eb2051ff39fa5b --- .../internal/view/InputMethodClient.java | 8 +- .../server/InputMethodManagerService.java | 93 +++++++++---------- 2 files changed, 47 insertions(+), 54 deletions(-) diff --git a/core/java/com/android/internal/view/InputMethodClient.java b/core/java/com/android/internal/view/InputMethodClient.java index 802ce3e734d74..cea030a3d489b 100644 --- a/core/java/com/android/internal/view/InputMethodClient.java +++ b/core/java/com/android/internal/view/InputMethodClient.java @@ -75,12 +75,12 @@ public final class InputMethodClient { public static final int UNBIND_REASON_DISCONNECT_IME = 3; public static final int UNBIND_REASON_NO_IME = 4; public static final int UNBIND_REASON_SWITCH_IME_FAILED = 5; - public static final int UNBIND_REASON_RESET_IME = 6; + public static final int UNBIND_REASON_SWITCH_USER = 6; @Retention(SOURCE) @IntDef({UNBIND_REASON_UNSPECIFIED, UNBIND_REASON_SWITCH_CLIENT, UNBIND_REASON_SWITCH_IME, UNBIND_REASON_DISCONNECT_IME, UNBIND_REASON_NO_IME, UNBIND_REASON_SWITCH_IME_FAILED, - UNBIND_REASON_RESET_IME}) + UNBIND_REASON_SWITCH_USER}) public @interface UnbindReason {} public static String getUnbindReason(@UnbindReason final int reason) { @@ -97,8 +97,8 @@ public final class InputMethodClient { return "NO_IME"; case UNBIND_REASON_SWITCH_IME_FAILED: return "SWITCH_IME_FAILED"; - case UNBIND_REASON_RESET_IME: - return "RESET_IME"; + case UNBIND_REASON_SWITCH_USER: + return "SWITCH_USER"; default: return "Unknown=" + reason; } diff --git a/services/core/java/com/android/server/InputMethodManagerService.java b/services/core/java/com/android/server/InputMethodManagerService.java index 121bc3c02d523..2b3d8d4e1fb14 100644 --- a/services/core/java/com/android/server/InputMethodManagerService.java +++ b/services/core/java/com/android/server/InputMethodManagerService.java @@ -600,15 +600,35 @@ public class InputMethodManagerService extends IInputMethodManager.Stub restoreEnabledInputMethods(mContext, prevValue, newValue); } } else if (Intent.ACTION_LOCALE_CHANGED.equals(action)) { - synchronized (mMethodMap) { - resetStateIfCurrentLocaleChangedLocked(); - } + onActionLocaleChanged(); } else { Slog.w(TAG, "Unexpected intent " + intent); } } } + /** + * Handles {@link Intent#ACTION_LOCALE_CHANGED}. + * + *

Note: For historical reasons, {@link Intent#ACTION_LOCALE_CHANGED} has been sent to all + * the users. We should ignore this event if this is about any background user's locale.

+ * + *

Caution: This method must not be called when system is not ready.

+ */ + void onActionLocaleChanged() { + synchronized (mMethodMap) { + final LocaleList possibleNewLocale = mRes.getConfiguration().getLocales(); + if (possibleNewLocale != null && possibleNewLocale.equals(mLastSystemLocales)) { + return; + } + buildInputMethodListLocked(true); + // If the locale is changed, needs to reset the default ime + resetDefaultImeLocked(mContext); + updateFromSettingsLocked(true); + mLastSystemLocales = possibleNewLocale; + } + } + // Apply the results of a restore operation to the set of enabled IMEs. Note that this // does not attempt to validate on the fly with any installed device policy, so must only // be run in the context of initial device setup. @@ -980,51 +1000,6 @@ public class InputMethodManagerService extends IInputMethodManager.Stub setSelectedInputMethodAndSubtypeLocked(defIm, NOT_A_SUBTYPE_ID, false); } - private void resetAllInternalStateLocked(final boolean updateOnlyWhenLocaleChanged, - final boolean resetDefaultEnabledIme) { - if (!mSystemReady) { - // not system ready - return; - } - final LocaleList newLocales = mRes.getConfiguration().getLocales(); - if (!updateOnlyWhenLocaleChanged - || (newLocales != null && !newLocales.equals(mLastSystemLocales))) { - if (!updateOnlyWhenLocaleChanged) { - hideCurrentInputLocked(0, null); - resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_RESET_IME); - } - if (DEBUG) { - Slog.i(TAG, "LocaleList has been changed to " + newLocales); - } - buildInputMethodListLocked(resetDefaultEnabledIme); - if (!updateOnlyWhenLocaleChanged) { - final String selectedImiId = mSettings.getSelectedInputMethod(); - if (TextUtils.isEmpty(selectedImiId)) { - // This is the first time of the user switch and - // set the current ime to the proper one. - resetDefaultImeLocked(mContext); - } - } else { - // If the locale is changed, needs to reset the default ime - resetDefaultImeLocked(mContext); - } - updateFromSettingsLocked(true); - mLastSystemLocales = newLocales; - if (!updateOnlyWhenLocaleChanged) { - try { - startInputInnerLocked(); - } catch (RuntimeException e) { - Slog.w(TAG, "Unexpected exception", e); - } - } - } - } - - private void resetStateIfCurrentLocaleChangedLocked() { - resetAllInternalStateLocked(true /* updateOnlyWhenLocaleChanged */, - true /* resetDefaultImeLocked */); - } - private void switchUserLocked(int newUserId) { if (DEBUG) Slog.d(TAG, "Switching user stage 1/3. newUserId=" + newUserId + " currentUserId=" + mSettings.getCurrentUserId()); @@ -1051,8 +1026,26 @@ public class InputMethodManagerService extends IInputMethodManager.Stub // Even in such cases, IMMS works fine because it will find the most applicable // IME for that user. final boolean initialUserSwitch = TextUtils.isEmpty(defaultImiId); - resetAllInternalStateLocked(false /* updateOnlyWhenLocaleChanged */, - initialUserSwitch /* needsToResetDefaultIme */); + mLastSystemLocales = mRes.getConfiguration().getLocales(); + + // TODO: Is it really possible that switchUserLocked() happens before system ready? + if (mSystemReady) { + hideCurrentInputLocked(0, null); + resetCurrentMethodAndClient(InputMethodClient.UNBIND_REASON_SWITCH_USER); + buildInputMethodListLocked(initialUserSwitch); + if (TextUtils.isEmpty(mSettings.getSelectedInputMethod())) { + // This is the first time of the user switch and + // set the current ime to the proper one. + resetDefaultImeLocked(mContext); + } + updateFromSettingsLocked(true); + try { + startInputInnerLocked(); + } catch (RuntimeException e) { + Slog.w(TAG, "Unexpected exception", e); + } + } + if (initialUserSwitch) { InputMethodUtils.setNonSelectedSystemImesDisabledUntilUsed(mIPackageManager, mSettings.getEnabledInputMethodListLocked(), newUserId,