Cleanup InputMethodInfoCache

Change-Id: I40afeb4557c3b1ad32bd90f67502a625df2bc3fb
This commit is contained in:
Tadashi G. Takaoka 2014-11-07 12:48:59 -08:00
parent 8cfad7ed64
commit afd52dfc60

View file

@ -69,10 +69,6 @@ public class RichInputMethodManager {
private RichInputMethodSubtype mCurrentRichInputMethodSubtype; private RichInputMethodSubtype mCurrentRichInputMethodSubtype;
private InputMethodInfo mShortcutInputMethodInfo; private InputMethodInfo mShortcutInputMethodInfo;
private InputMethodSubtype mShortcutSubtype; private InputMethodSubtype mShortcutSubtype;
final HashMap<InputMethodInfo, List<InputMethodSubtype>>
mSubtypeListCacheWithImplicitlySelectedSubtypes = new HashMap<>();
final HashMap<InputMethodInfo, List<InputMethodSubtype>>
mSubtypeListCacheWithoutImplicitlySelectedSubtypes = new HashMap<>();
private static final int INDEX_NOT_FOUND = -1; private static final int INDEX_NOT_FOUND = -1;
@ -234,33 +230,57 @@ public class RichInputMethodManager {
private final InputMethodManager mImm; private final InputMethodManager mImm;
private final String mImePackageName; private final String mImePackageName;
private InputMethodInfo mCachedValue; private InputMethodInfo mCachedThisImeInfo;
private final HashMap<InputMethodInfo, List<InputMethodSubtype>>
mCachedSubtypeListWithImplicitlySelected;
private final HashMap<InputMethodInfo, List<InputMethodSubtype>>
mCachedSubtypeListOnlyExplicitlySelected;
public InputMethodInfoCache(final InputMethodManager imm, final String imePackageName) { public InputMethodInfoCache(final InputMethodManager imm, final String imePackageName) {
mImm = imm; mImm = imm;
mImePackageName = imePackageName; mImePackageName = imePackageName;
mCachedSubtypeListWithImplicitlySelected = new HashMap<>();
mCachedSubtypeListOnlyExplicitlySelected = new HashMap<>();
} }
public synchronized InputMethodInfo get() { public synchronized InputMethodInfo getInputMethodOfThisIme() {
if (mCachedValue != null) { if (mCachedThisImeInfo != null) {
return mCachedValue; return mCachedThisImeInfo;
} }
for (final InputMethodInfo imi : mImm.getInputMethodList()) { for (final InputMethodInfo imi : mImm.getInputMethodList()) {
if (imi.getPackageName().equals(mImePackageName)) { if (imi.getPackageName().equals(mImePackageName)) {
mCachedValue = imi; mCachedThisImeInfo = imi;
return imi; return imi;
} }
} }
throw new RuntimeException("Input method id for " + mImePackageName + " not found."); throw new RuntimeException("Input method id for " + mImePackageName + " not found.");
} }
public synchronized List<InputMethodSubtype> getEnabledInputMethodSubtypeList(
final InputMethodInfo imi, final boolean allowsImplicitlySelectedSubtypes) {
final HashMap<InputMethodInfo, List<InputMethodSubtype>> cache =
allowsImplicitlySelectedSubtypes
? mCachedSubtypeListWithImplicitlySelected
: mCachedSubtypeListOnlyExplicitlySelected;
final List<InputMethodSubtype> cachedList = cache.get(imi);
if (cachedList != null) {
return cachedList;
}
final List<InputMethodSubtype> result = mImm.getEnabledInputMethodSubtypeList(
imi, allowsImplicitlySelectedSubtypes);
cache.put(imi, result);
return result;
}
public synchronized void clear() { public synchronized void clear() {
mCachedValue = null; mCachedThisImeInfo = null;
mCachedSubtypeListWithImplicitlySelected.clear();
mCachedSubtypeListOnlyExplicitlySelected.clear();
} }
} }
public InputMethodInfo getInputMethodInfoOfThisIme() { public InputMethodInfo getInputMethodInfoOfThisIme() {
return mInputMethodInfoCache.get(); return mInputMethodInfoCache.getInputMethodOfThisIme();
} }
public String getInputMethodIdOfThisIme() { public String getInputMethodIdOfThisIme() {
@ -445,21 +465,11 @@ public class RichInputMethodManager {
private List<InputMethodSubtype> getEnabledInputMethodSubtypeList(final InputMethodInfo imi, private List<InputMethodSubtype> getEnabledInputMethodSubtypeList(final InputMethodInfo imi,
final boolean allowsImplicitlySelectedSubtypes) { final boolean allowsImplicitlySelectedSubtypes) {
final HashMap<InputMethodInfo, List<InputMethodSubtype>> cache = return mInputMethodInfoCache.getEnabledInputMethodSubtypeList(
allowsImplicitlySelectedSubtypes
? mSubtypeListCacheWithImplicitlySelectedSubtypes
: mSubtypeListCacheWithoutImplicitlySelectedSubtypes;
final List<InputMethodSubtype> cachedList = cache.get(imi);
if (null != cachedList) return cachedList;
final List<InputMethodSubtype> result = mImmWrapper.mImm.getEnabledInputMethodSubtypeList(
imi, allowsImplicitlySelectedSubtypes); imi, allowsImplicitlySelectedSubtypes);
cache.put(imi, result);
return result;
} }
public void refreshSubtypeCaches() { public void refreshSubtypeCaches() {
mSubtypeListCacheWithImplicitlySelectedSubtypes.clear();
mSubtypeListCacheWithoutImplicitlySelectedSubtypes.clear();
mInputMethodInfoCache.clear(); mInputMethodInfoCache.clear();
updateCurrentSubtype(mImmWrapper.mImm.getCurrentInputMethodSubtype()); updateCurrentSubtype(mImmWrapper.mImm.getCurrentInputMethodSubtype());
updateShortcutIme(); updateShortcutIme();