use LocaleKeyTexts instead of KeyboardTextsSet

This commit is contained in:
Helium314 2023-12-19 23:49:24 +01:00
parent b4d0349d59
commit 1cc331387f
3 changed files with 15 additions and 11 deletions

View file

@ -13,6 +13,8 @@ import android.content.res.TypedArray;
import android.content.res.XmlResourceParser; import android.content.res.XmlResourceParser;
import android.os.Build; import android.os.Build;
import android.text.InputType; import android.text.InputType;
import org.dslul.openboard.inputmethod.keyboard.internal.keyboard_parser.LocaleKeyTexts;
import org.dslul.openboard.inputmethod.latin.utils.Log; import org.dslul.openboard.inputmethod.latin.utils.Log;
import android.util.SparseArray; import android.util.SparseArray;
import android.util.Xml; import android.util.Xml;
@ -62,6 +64,7 @@ public final class KeyboardLayoutSet {
private final Context mContext; private final Context mContext;
@NonNull @NonNull
private final Params mParams; private final Params mParams;
public final LocaleKeyTexts mLocaleKeyTexts;
// How many layouts we forcibly keep in cache. This only includes ALPHABET (default) and // How many layouts we forcibly keep in cache. This only includes ALPHABET (default) and
// ALPHABET_AUTOMATIC_SHIFTED layouts - other layouts may stay in memory in the map of // ALPHABET_AUTOMATIC_SHIFTED layouts - other layouts may stay in memory in the map of
@ -122,8 +125,7 @@ public final class KeyboardLayoutSet {
// whether the user has enabled it, and the keyboard layout supports it. // whether the user has enabled it, and the keyboard layout supports it.
boolean mIsSplitLayoutEnabled; boolean mIsSplitLayoutEnabled;
// Sparse array of KeyboardLayoutSet element parameters indexed by element's id. // Sparse array of KeyboardLayoutSet element parameters indexed by element's id.
final SparseArray<ElementParams> mKeyboardLayoutSetElementIdToParamsMap = final SparseArray<ElementParams> mKeyboardLayoutSetElementIdToParamsMap = new SparseArray<>();
new SparseArray<>();
} }
public static void onSystemLocaleChanged() { public static void onSystemLocaleChanged() {
@ -154,6 +156,7 @@ public final class KeyboardLayoutSet {
KeyboardLayoutSet(final Context context, @NonNull final Params params) { KeyboardLayoutSet(final Context context, @NonNull final Params params) {
mContext = context; mContext = context;
mParams = params; mParams = params;
mLocaleKeyTexts = LocaleKeyTextsKt.getOrCreate(context, params.mSubtype.getLocale());
} }
@NonNull @NonNull

View file

@ -23,7 +23,6 @@ import org.dslul.openboard.inputmethod.keyboard.KeyboardLayoutSet.KeyboardLayout
import org.dslul.openboard.inputmethod.keyboard.clipboard.ClipboardHistoryView; import org.dslul.openboard.inputmethod.keyboard.clipboard.ClipboardHistoryView;
import org.dslul.openboard.inputmethod.keyboard.emoji.EmojiPalettesView; import org.dslul.openboard.inputmethod.keyboard.emoji.EmojiPalettesView;
import org.dslul.openboard.inputmethod.keyboard.internal.KeyboardState; import org.dslul.openboard.inputmethod.keyboard.internal.KeyboardState;
import org.dslul.openboard.inputmethod.keyboard.internal.KeyboardTextsSet;
import org.dslul.openboard.inputmethod.latin.InputView; import org.dslul.openboard.inputmethod.latin.InputView;
import org.dslul.openboard.inputmethod.latin.KeyboardWrapperView; import org.dslul.openboard.inputmethod.latin.KeyboardWrapperView;
import org.dslul.openboard.inputmethod.latin.LatinIME; import org.dslul.openboard.inputmethod.latin.LatinIME;
@ -55,8 +54,6 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
private KeyboardState mState; private KeyboardState mState;
private KeyboardLayoutSet mKeyboardLayoutSet; private KeyboardLayoutSet mKeyboardLayoutSet;
// TODO: The following {@link KeyboardTextsSet} should be in {@link KeyboardLayoutSet}.
private final KeyboardTextsSet mKeyboardTextsSet = new KeyboardTextsSet();
private KeyboardTheme mKeyboardTheme; private KeyboardTheme mKeyboardTheme;
private Context mThemeContext; private Context mThemeContext;
@ -133,7 +130,6 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
.build(); .build();
try { try {
mState.onLoadKeyboard(currentAutoCapsState, currentRecapitalizeState, oneHandedModeEnabled); mState.onLoadKeyboard(currentAutoCapsState, currentRecapitalizeState, oneHandedModeEnabled);
mKeyboardTextsSet.setLocale(mRichImm.getCurrentSubtypeLocale(), mThemeContext);
} catch (KeyboardLayoutSetException e) { } catch (KeyboardLayoutSetException e) {
Log.w(TAG, "loading keyboard failed: " + e.mKeyboardId, e.getCause()); Log.w(TAG, "loading keyboard failed: " + e.mKeyboardId, e.getCause());
} }
@ -302,7 +298,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
// @see LatinIME#onComputeInset(android.inputmethodservice.InputMethodService.Insets) // @see LatinIME#onComputeInset(android.inputmethodservice.InputMethodService.Insets)
mKeyboardView.setVisibility(View.GONE); mKeyboardView.setVisibility(View.GONE);
mEmojiPalettesView.startEmojiPalettes( mEmojiPalettesView.startEmojiPalettes(
mKeyboardTextsSet.getText(KeyboardTextsSet.SWITCH_TO_ALPHA_KEY_LABEL), mKeyboardLayoutSet.mLocaleKeyTexts.getLabelAlphabet(),
mKeyboardView.getKeyVisualAttribute(), keyboard.mIconsSet); mKeyboardView.getKeyVisualAttribute(), keyboard.mIconsSet);
mEmojiPalettesView.setVisibility(View.VISIBLE); mEmojiPalettesView.setVisibility(View.VISIBLE);
} }
@ -321,7 +317,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
mKeyboardView.setVisibility(View.GONE); mKeyboardView.setVisibility(View.GONE);
mClipboardHistoryView.startClipboardHistory( mClipboardHistoryView.startClipboardHistory(
mLatinIME.getClipboardHistoryManager(), mLatinIME.getClipboardHistoryManager(),
mKeyboardTextsSet.getText(KeyboardTextsSet.SWITCH_TO_ALPHA_KEY_LABEL), mKeyboardLayoutSet.mLocaleKeyTexts.getLabelAlphabet(),
mKeyboardView.getKeyVisualAttribute(), keyboard.mIconsSet); mKeyboardView.getKeyVisualAttribute(), keyboard.mIconsSet);
mClipboardHistoryView.setVisibility(View.VISIBLE); mClipboardHistoryView.setVisibility(View.VISIBLE);
} }

View file

@ -234,9 +234,14 @@ private fun addFixedColumnOrder(moreKeys: Array<String>): Array<String> {
} }
} }
fun getOrCreate(context: Context, locale: Locale): LocaleKeyTexts =
localeKeyTextsCache.getOrPut(locale.toString()) {
LocaleKeyTexts(getStreamForLocale(locale, context), locale)
}
fun addLocaleKeyTextsToParams(context: Context, params: KeyboardParams, moreKeysSetting: Int) { fun addLocaleKeyTextsToParams(context: Context, params: KeyboardParams, moreKeysSetting: Int) {
val locales = params.mSecondaryLocales + params.mId.locale val locales = params.mSecondaryLocales + params.mId.locale
params.mLocaleKeyTexts = moreKeysAndLabels.getOrPut(locales.joinToString { it.toString() }) { params.mLocaleKeyTexts = localeKeyTextsCache.getOrPut(locales.joinToString { it.toString() }) {
createLocaleKeyTexts(context, params, moreKeysSetting) createLocaleKeyTexts(context, params, moreKeysSetting)
} }
} }
@ -266,10 +271,10 @@ private fun getStreamForLocale(locale: Locale, context: Context) =
} }
} }
fun clearCache() = moreKeysAndLabels.clear() fun clearCache() = localeKeyTextsCache.clear()
// cache the texts, so they don't need to be read over and over // cache the texts, so they don't need to be read over and over
private val moreKeysAndLabels = hashMapOf<String, LocaleKeyTexts>() private val localeKeyTextsCache = hashMapOf<String, LocaleKeyTexts>()
private const val READER_MODE_NONE = 0 private const val READER_MODE_NONE = 0
private const val READER_MODE_MORE_KEYS = 1 private const val READER_MODE_MORE_KEYS = 1