aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/RichInputMethodManager.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/src/com/android/inputmethod/latin/RichInputMethodManager.java')
-rw-r--r--java/src/com/android/inputmethod/latin/RichInputMethodManager.java106
1 files changed, 72 insertions, 34 deletions
diff --git a/java/src/com/android/inputmethod/latin/RichInputMethodManager.java b/java/src/com/android/inputmethod/latin/RichInputMethodManager.java
index 6b6bbf3a7..7cf4eff92 100644
--- a/java/src/com/android/inputmethod/latin/RichInputMethodManager.java
+++ b/java/src/com/android/inputmethod/latin/RichInputMethodManager.java
@@ -20,6 +20,7 @@ import static com.android.inputmethod.latin.Constants.Subtype.KEYBOARD_MODE;
import android.content.Context;
import android.content.SharedPreferences;
+import android.os.Build;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
@@ -30,7 +31,6 @@ import android.view.inputmethod.InputMethodSubtype;
import com.android.inputmethod.compat.InputMethodManagerCompatWrapper;
import com.android.inputmethod.latin.settings.Settings;
import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils;
-import com.android.inputmethod.latin.utils.CollectionUtils;
import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
import java.util.Collections;
@@ -50,11 +50,11 @@ public final class RichInputMethodManager {
private static final RichInputMethodManager sInstance = new RichInputMethodManager();
private InputMethodManagerCompatWrapper mImmWrapper;
- private InputMethodInfo mInputMethodInfoOfThisIme;
+ private InputMethodInfoCache mInputMethodInfoCache;
final HashMap<InputMethodInfo, List<InputMethodSubtype>>
- mSubtypeListCacheWithImplicitlySelectedSubtypes = CollectionUtils.newHashMap();
+ mSubtypeListCacheWithImplicitlySelectedSubtypes = new HashMap<>();
final HashMap<InputMethodInfo, List<InputMethodSubtype>>
- mSubtypeListCacheWithoutImplicitlySelectedSubtypes = CollectionUtils.newHashMap();
+ mSubtypeListCacheWithoutImplicitlySelectedSubtypes = new HashMap<>();
private static final int INDEX_NOT_FOUND = -1;
@@ -64,8 +64,7 @@ public final class RichInputMethodManager {
}
public static void init(final Context context) {
- final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
- sInstance.initInternal(context, prefs);
+ sInstance.initInternal(context);
}
private boolean isInitialized() {
@@ -78,20 +77,26 @@ public final class RichInputMethodManager {
}
}
- private void initInternal(final Context context, final SharedPreferences prefs) {
+ private void initInternal(final Context context) {
if (isInitialized()) {
return;
}
mImmWrapper = new InputMethodManagerCompatWrapper(context);
- mInputMethodInfoOfThisIme = getInputMethodInfoOfThisIme(context);
+ mInputMethodInfoCache = new InputMethodInfoCache(
+ mImmWrapper.mImm, context.getPackageName());
// Initialize additional subtypes.
SubtypeLocaleUtils.init(context);
+ final InputMethodSubtype[] additionalSubtypes = getAdditionalSubtypes(context);
+ setAdditionalInputMethodSubtypes(additionalSubtypes);
+ }
+
+ public InputMethodSubtype[] getAdditionalSubtypes(final Context context) {
+ SubtypeLocaleUtils.init(context);
+ final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
final String prefAdditionalSubtypes = Settings.readPrefAdditionalSubtypes(
prefs, context.getResources());
- final InputMethodSubtype[] additionalSubtypes =
- AdditionalSubtypeUtils.createAdditionalSubtypesArray(prefAdditionalSubtypes);
- setAdditionalInputMethodSubtypes(additionalSubtypes);
+ return AdditionalSubtypeUtils.createAdditionalSubtypesArray(prefAdditionalSubtypes);
}
public InputMethodManager getInputMethodManager() {
@@ -99,20 +104,10 @@ public final class RichInputMethodManager {
return mImmWrapper.mImm;
}
- private InputMethodInfo getInputMethodInfoOfThisIme(final Context context) {
- final String packageName = context.getPackageName();
- for (final InputMethodInfo imi : mImmWrapper.mImm.getInputMethodList()) {
- if (imi.getPackageName().equals(packageName)) {
- return imi;
- }
- }
- throw new RuntimeException("Input method id for " + packageName + " not found.");
- }
-
public List<InputMethodSubtype> getMyEnabledInputMethodSubtypeList(
boolean allowsImplicitlySelectedSubtypes) {
- return getEnabledInputMethodSubtypeList(mInputMethodInfoOfThisIme,
- allowsImplicitlySelectedSubtypes);
+ return getEnabledInputMethodSubtypeList(
+ getInputMethodInfoOfThisIme(), allowsImplicitlySelectedSubtypes);
}
public boolean switchToNextInputMethod(final IBinder token, final boolean onlyCurrentIme) {
@@ -153,10 +148,10 @@ public final class RichInputMethodManager {
private boolean switchToNextInputMethodAndSubtype(final IBinder token) {
final InputMethodManager imm = mImmWrapper.mImm;
final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList();
- final int currentIndex = getImiIndexInList(mInputMethodInfoOfThisIme, enabledImis);
+ final int currentIndex = getImiIndexInList(getInputMethodInfoOfThisIme(), enabledImis);
if (currentIndex == INDEX_NOT_FOUND) {
Log.w(TAG, "Can't find current IME in enabled IMEs: IME package="
- + mInputMethodInfoOfThisIme.getPackageName());
+ + getInputMethodInfoOfThisIme().getPackageName());
return false;
}
final InputMethodInfo nextImi = getNextNonAuxiliaryIme(currentIndex, enabledImis);
@@ -213,16 +208,45 @@ public final class RichInputMethodManager {
return true;
}
+ private static class InputMethodInfoCache {
+ private final InputMethodManager mImm;
+ private final String mImePackageName;
+
+ private InputMethodInfo mCachedValue;
+
+ public InputMethodInfoCache(final InputMethodManager imm, final String imePackageName) {
+ mImm = imm;
+ mImePackageName = imePackageName;
+ }
+
+ public synchronized InputMethodInfo get() {
+ if (mCachedValue != null) {
+ return mCachedValue;
+ }
+ for (final InputMethodInfo imi : mImm.getInputMethodList()) {
+ if (imi.getPackageName().equals(mImePackageName)) {
+ mCachedValue = imi;
+ return imi;
+ }
+ }
+ throw new RuntimeException("Input method id for " + mImePackageName + " not found.");
+ }
+
+ public synchronized void clear() {
+ mCachedValue = null;
+ }
+ }
+
public InputMethodInfo getInputMethodInfoOfThisIme() {
- return mInputMethodInfoOfThisIme;
+ return mInputMethodInfoCache.get();
}
public String getInputMethodIdOfThisIme() {
- return mInputMethodInfoOfThisIme.getId();
+ return getInputMethodInfoOfThisIme().getId();
}
public boolean checkIfSubtypeBelongsToThisImeAndEnabled(final InputMethodSubtype subtype) {
- return checkIfSubtypeBelongsToImeAndEnabled(mInputMethodInfoOfThisIme, subtype);
+ return checkIfSubtypeBelongsToImeAndEnabled(getInputMethodInfoOfThisIme(), subtype);
}
public boolean checkIfSubtypeBelongsToThisImeAndImplicitlyEnabled(
@@ -258,7 +282,7 @@ public final class RichInputMethodManager {
}
public boolean checkIfSubtypeBelongsToThisIme(final InputMethodSubtype subtype) {
- return getSubtypeIndexInIme(subtype, mInputMethodInfoOfThisIme) != INDEX_NOT_FOUND;
+ return getSubtypeIndexInIme(subtype, getInputMethodInfoOfThisIme()) != INDEX_NOT_FOUND;
}
private static int getSubtypeIndexInIme(final InputMethodSubtype subtype,
@@ -286,7 +310,8 @@ public final class RichInputMethodManager {
public boolean hasMultipleEnabledSubtypesInThisIme(
final boolean shouldIncludeAuxiliarySubtypes) {
- final List<InputMethodInfo> imiList = Collections.singletonList(mInputMethodInfoOfThisIme);
+ final List<InputMethodInfo> imiList = Collections.singletonList(
+ getInputMethodInfoOfThisIme());
return hasMultipleEnabledSubtypes(shouldIncludeAuxiliarySubtypes, imiList);
}
@@ -340,7 +365,7 @@ public final class RichInputMethodManager {
public InputMethodSubtype findSubtypeByLocaleAndKeyboardLayoutSet(final String localeString,
final String keyboardLayoutSetName) {
- final InputMethodInfo myImi = mInputMethodInfoOfThisIme;
+ final InputMethodInfo myImi = getInputMethodInfoOfThisIme();
final int count = myImi.getSubtypeCount();
for (int i = 0; i < count; i++) {
final InputMethodSubtype subtype = myImi.getSubtypeAt(i);
@@ -355,13 +380,14 @@ public final class RichInputMethodManager {
public void setInputMethodAndSubtype(final IBinder token, final InputMethodSubtype subtype) {
mImmWrapper.mImm.setInputMethodAndSubtype(
- token, mInputMethodInfoOfThisIme.getId(), subtype);
+ token, getInputMethodIdOfThisIme(), subtype);
}
public void setAdditionalInputMethodSubtypes(final InputMethodSubtype[] subtypes) {
mImmWrapper.mImm.setAdditionalInputMethodSubtypes(
- mInputMethodInfoOfThisIme.getId(), subtypes);
- // Clear the cache so that we go read the subtypes again next time.
+ getInputMethodIdOfThisIme(), subtypes);
+ // Clear the cache so that we go read the {@link InputMethodInfo} of this IME and list of
+ // subtypes again next time.
clearSubtypeCaches();
}
@@ -382,5 +408,17 @@ public final class RichInputMethodManager {
public void clearSubtypeCaches() {
mSubtypeListCacheWithImplicitlySelectedSubtypes.clear();
mSubtypeListCacheWithoutImplicitlySelectedSubtypes.clear();
+ mInputMethodInfoCache.clear();
+ }
+
+ public boolean shouldOfferSwitchingToNextInputMethod(final IBinder binder,
+ boolean defaultValue) {
+ // Use the default value instead on Jelly Bean MR2 and previous where
+ // {@link InputMethodManager#shouldOfferSwitchingToNextInputMethod} isn't yet available
+ // and on KitKat where the API is still just a stub to return true always.
+ if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
+ return defaultValue;
+ }
+ return mImmWrapper.shouldOfferSwitchingToNextInputMethod(binder);
}
}