Skip to content
Snippets Groups Projects
Commit 3dca77ec authored by Tadashi G. Takaoka's avatar Tadashi G. Takaoka Committed by Android (Google) Code Review
Browse files

Merge "Refactor KeysCache to UniqueKeysCache"

parents 5ac8b040 01d54002
No related branches found
No related tags found
No related merge requests found
...@@ -34,7 +34,7 @@ import com.android.inputmethod.compat.EditorInfoCompatUtils; ...@@ -34,7 +34,7 @@ import com.android.inputmethod.compat.EditorInfoCompatUtils;
import com.android.inputmethod.compat.InputMethodSubtypeCompatUtils; import com.android.inputmethod.compat.InputMethodSubtypeCompatUtils;
import com.android.inputmethod.keyboard.internal.KeyboardBuilder; import com.android.inputmethod.keyboard.internal.KeyboardBuilder;
import com.android.inputmethod.keyboard.internal.KeyboardParams; import com.android.inputmethod.keyboard.internal.KeyboardParams;
import com.android.inputmethod.keyboard.internal.KeysCache; import com.android.inputmethod.keyboard.internal.UniqueKeysCache;
import com.android.inputmethod.latin.InputAttributes; import com.android.inputmethod.latin.InputAttributes;
import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.RichInputMethodSubtype; import com.android.inputmethod.latin.RichInputMethodSubtype;
...@@ -86,7 +86,7 @@ public final class KeyboardLayoutSet { ...@@ -86,7 +86,7 @@ public final class KeyboardLayoutSet {
private static final HashMap<KeyboardId, SoftReference<Keyboard>> sKeyboardCache = private static final HashMap<KeyboardId, SoftReference<Keyboard>> sKeyboardCache =
new HashMap<>(); new HashMap<>();
@Nonnull @Nonnull
private static final KeysCache sKeysCache = new KeysCache(); private static final UniqueKeysCache sUniqueKeysCache = UniqueKeysCache.newInstance();
private final static HashMap<InputMethodSubtype, Integer> sScriptIdsForSubtypes = private final static HashMap<InputMethodSubtype, Integer> sScriptIdsForSubtypes =
new HashMap<>(); new HashMap<>();
...@@ -144,7 +144,7 @@ public final class KeyboardLayoutSet { ...@@ -144,7 +144,7 @@ public final class KeyboardLayoutSet {
private static void clearKeyboardCache() { private static void clearKeyboardCache() {
sKeyboardCache.clear(); sKeyboardCache.clear();
sKeysCache.clear(); sUniqueKeysCache.clear();
} }
public static int getScriptId(final Resources resources, public static int getScriptId(final Resources resources,
...@@ -219,10 +219,8 @@ public final class KeyboardLayoutSet { ...@@ -219,10 +219,8 @@ public final class KeyboardLayoutSet {
} }
final KeyboardBuilder<KeyboardParams> builder = final KeyboardBuilder<KeyboardParams> builder =
new KeyboardBuilder<>(mContext, new KeyboardParams()); new KeyboardBuilder<>(mContext, new KeyboardParams(sUniqueKeysCache));
if (id.isAlphabetKeyboard()) { sUniqueKeysCache.setEnabled(id.isAlphabetKeyboard());
builder.setAutoGenerate(sKeysCache);
}
builder.setAllowRedundantMoreKes(elementParams.mAllowRedundantMoreKeys); builder.setAllowRedundantMoreKes(elementParams.mAllowRedundantMoreKeys);
final int keyboardXmlId = elementParams.mKeyboardXmlId; final int keyboardXmlId = elementParams.mKeyboardXmlId;
builder.load(keyboardXmlId, id); builder.load(keyboardXmlId, id);
......
...@@ -161,10 +161,6 @@ public class KeyboardBuilder<KP extends KeyboardParams> { ...@@ -161,10 +161,6 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
params.GRID_HEIGHT = res.getInteger(R.integer.config_keyboard_grid_height); params.GRID_HEIGHT = res.getInteger(R.integer.config_keyboard_grid_height);
} }
public void setAutoGenerate(final KeysCache keysCache) {
mParams.mKeysCache = keysCache;
}
public void setAllowRedundantMoreKes(final boolean enabled) { public void setAllowRedundantMoreKes(final boolean enabled) {
mParams.mAllowRedundantMoreKeys = enabled; mParams.mAllowRedundantMoreKeys = enabled;
} }
......
...@@ -77,9 +77,8 @@ public class KeyboardParams { ...@@ -77,9 +77,8 @@ public class KeyboardParams {
@Nonnull @Nonnull
public final KeyStylesSet mKeyStyles = new KeyStylesSet(mTextsSet); public final KeyStylesSet mKeyStyles = new KeyStylesSet(mTextsSet);
// TODO: Make this @Nonnull @Nonnull
@Nullable private final UniqueKeysCache mUniqueKeysCache;
public KeysCache mKeysCache;
public boolean mAllowRedundantMoreKeys; public boolean mAllowRedundantMoreKeys;
public int mMostCommonKeyHeight = 0; public int mMostCommonKeyHeight = 0;
...@@ -103,6 +102,14 @@ public class KeyboardParams { ...@@ -103,6 +102,14 @@ public class KeyboardParams {
} }
}; };
public KeyboardParams() {
this(UniqueKeysCache.NO_CACHE);
}
public KeyboardParams(@Nonnull final UniqueKeysCache keysCache) {
mUniqueKeysCache = keysCache;
}
protected void clearKeys() { protected void clearKeys() {
mSortedKeys.clear(); mSortedKeys.clear();
mShiftKeys.clear(); mShiftKeys.clear();
...@@ -110,9 +117,7 @@ public class KeyboardParams { ...@@ -110,9 +117,7 @@ public class KeyboardParams {
} }
public void onAddKey(@Nonnull final Key newKey) { public void onAddKey(@Nonnull final Key newKey) {
// To avoid possible null pointer access. final Key key = mUniqueKeysCache.getUniqueKey(newKey);
final KeysCache keysCache = mKeysCache;
final Key key = (keysCache != null) ? keysCache.get(newKey) : newKey;
final boolean isSpacer = key.isSpacer(); final boolean isSpacer = key.isSpacer();
if (isSpacer && key.getWidth() == 0) { if (isSpacer && key.getWidth() == 0) {
// Ignore zero width {@link Spacer}. // Ignore zero width {@link Spacer}.
...@@ -140,16 +145,11 @@ public class KeyboardParams { ...@@ -140,16 +145,11 @@ public class KeyboardParams {
for (final Key key : mSortedKeys) { for (final Key key : mSortedKeys) {
lettersOnBaseLayout.addLetter(key); lettersOnBaseLayout.addLetter(key);
} }
// To avoid possible null pointer access.
final KeysCache keysCache = mKeysCache;
final ArrayList<Key> allKeys = new ArrayList<>(mSortedKeys); final ArrayList<Key> allKeys = new ArrayList<>(mSortedKeys);
mSortedKeys.clear(); mSortedKeys.clear();
for (final Key key : allKeys) { for (final Key key : allKeys) {
final Key filteredKey = Key.removeRedundantMoreKeys(key, lettersOnBaseLayout); final Key filteredKey = Key.removeRedundantMoreKeys(key, lettersOnBaseLayout);
if (keysCache != null) { mSortedKeys.add(mUniqueKeysCache.getUniqueKey(filteredKey));
keysCache.replace(key, filteredKey);
}
mSortedKeys.add(filteredKey);
} }
} }
......
/* /*
* Copyright (C) 2012 The Android Open Source Project * Copyright (C) 2014 The Android Open Source Project
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -20,31 +20,62 @@ import com.android.inputmethod.keyboard.Key; ...@@ -20,31 +20,62 @@ import com.android.inputmethod.keyboard.Key;
import java.util.HashMap; import java.util.HashMap;
// TODO: Rename more appropriate name. import javax.annotation.Nonnull;
public final class KeysCache {
private final HashMap<Key, Key> mMap = new HashMap<>();
public void clear() { public abstract class UniqueKeysCache {
mMap.clear(); public abstract void setEnabled(boolean enabled);
public abstract void clear();
public abstract @Nonnull Key getUniqueKey(@Nonnull Key key);
@Nonnull
public static final UniqueKeysCache NO_CACHE = new UniqueKeysCache() {
@Override
public void setEnabled(boolean enabled) {}
@Override
public void clear() {}
@Override
public Key getUniqueKey(Key key) { return key; }
};
@Nonnull
public static UniqueKeysCache newInstance() {
return new UniqueKeysCacheImpl();
} }
// TODO: Rename more descriptive name. private static final class UniqueKeysCacheImpl extends UniqueKeysCache {
public Key get(final Key key) { private final HashMap<Key, Key> mCache;
final Key existingKey = mMap.get(key);
if (existingKey != null) { private boolean mEnabled;
// Reuse the existing element that equals to "key" without adding "key" to the map.
return existingKey; UniqueKeysCacheImpl() {
mCache = new HashMap<>();
}
@Override
public void setEnabled(final boolean enabled) {
mEnabled = enabled;
}
@Override
public void clear() {
mCache.clear();
} }
mMap.put(key, key);
return key;
}
// TODO: Rename more descriptive name. @Override
public Key replace(final Key oldKey, final Key newKey) { public Key getUniqueKey(final Key key) {
if (oldKey.equals(newKey)) { if (!mEnabled) {
return oldKey; return key;
}
final Key existingKey = mCache.get(key);
if (existingKey != null) {
// Reuse the existing object that equals to "key" without adding "key" to
// the cache.
return existingKey;
}
mCache.put(key, key);
return key;
} }
mMap.remove(oldKey);
return get(newKey);
} }
} }
...@@ -120,31 +120,13 @@ abstract class LayoutTestsBase extends KeyboardLayoutSetTestsBase { ...@@ -120,31 +120,13 @@ abstract class LayoutTestsBase extends KeyboardLayoutSetTestsBase {
// TODO: Add phone, phone symbols, number, number password layout tests. // TODO: Add phone, phone symbols, number, number password layout tests.
public final void testAlphabet() { public final void testLayouts() {
doKeyboardTests(KeyboardId.ELEMENT_ALPHABET); doKeyboardTests(KeyboardId.ELEMENT_ALPHABET);
}
public final void testAlphabetAutomaticShifted() {
doKeyboardTests(KeyboardId.ELEMENT_ALPHABET_AUTOMATIC_SHIFTED); doKeyboardTests(KeyboardId.ELEMENT_ALPHABET_AUTOMATIC_SHIFTED);
}
public final void testAlphabetManualShifted() {
doKeyboardTests(KeyboardId.ELEMENT_ALPHABET_MANUAL_SHIFTED); doKeyboardTests(KeyboardId.ELEMENT_ALPHABET_MANUAL_SHIFTED);
}
public final void testAlphabetShiftLocked() {
doKeyboardTests(KeyboardId.ELEMENT_ALPHABET_SHIFT_LOCKED); doKeyboardTests(KeyboardId.ELEMENT_ALPHABET_SHIFT_LOCKED);
}
public final void testAlphabetShiftLockShifted() {
doKeyboardTests(KeyboardId.ELEMENT_ALPHABET_SHIFT_LOCK_SHIFTED); doKeyboardTests(KeyboardId.ELEMENT_ALPHABET_SHIFT_LOCK_SHIFTED);
}
public final void testSymbols() {
doKeyboardTests(KeyboardId.ELEMENT_SYMBOLS); doKeyboardTests(KeyboardId.ELEMENT_SYMBOLS);
}
public final void testSymbolsShifted() {
doKeyboardTests(KeyboardId.ELEMENT_SYMBOLS_SHIFTED); doKeyboardTests(KeyboardId.ELEMENT_SYMBOLS_SHIFTED);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment