mirror of
https://github.com/Helium314/HeliBoard.git
synced 2025-04-24 08:36:26 +00:00
replace RunInLocale with less deprecated functions
This commit is contained in:
parent
8a7a41764c
commit
4a277b8c32
7 changed files with 43 additions and 83 deletions
|
@ -3,8 +3,6 @@ package org.dslul.openboard.inputmethod.keyboard.internal.keyboard_parser
|
|||
|
||||
import android.content.Context
|
||||
import android.content.res.Configuration
|
||||
import android.content.res.Resources
|
||||
import android.os.Build
|
||||
import org.dslul.openboard.inputmethod.latin.utils.Log
|
||||
import android.view.inputmethod.EditorInfo
|
||||
import androidx.annotation.StringRes
|
||||
|
@ -28,7 +26,7 @@ import org.dslul.openboard.inputmethod.latin.utils.CUSTOM_LAYOUT_PREFIX
|
|||
import org.dslul.openboard.inputmethod.latin.utils.InputTypeUtils
|
||||
import org.dslul.openboard.inputmethod.latin.utils.MORE_KEYS_LAYOUT
|
||||
import org.dslul.openboard.inputmethod.latin.utils.MORE_KEYS_NUMBER
|
||||
import org.dslul.openboard.inputmethod.latin.utils.RunInLocale
|
||||
import org.dslul.openboard.inputmethod.latin.utils.runInLocale
|
||||
import org.dslul.openboard.inputmethod.latin.utils.sumOf
|
||||
import java.io.File
|
||||
import java.util.Locale
|
||||
|
@ -667,17 +665,13 @@ abstract class KeyboardParser(private val params: KeyboardParams, private val co
|
|||
}
|
||||
|
||||
private fun getInLocale(@StringRes id: Int): String {
|
||||
val ril = object : RunInLocale<String>() {
|
||||
override fun job(res: Resources) = res.getString(id)
|
||||
}
|
||||
// crappy workaround...
|
||||
val locale = when (params.mId.locale.toString().lowercase()) {
|
||||
// todo: improve this when switching (rich input) subtype to use language tag
|
||||
"hi_zz" -> Locale("en", "IN")
|
||||
"sr_zz" -> Locale.forLanguageTag("sr-Latn")
|
||||
else -> params.mId.locale
|
||||
}
|
||||
return ril.runInLocale(context.resources, locale)
|
||||
return runInLocale(context, locale) { it.getString(id) }
|
||||
}
|
||||
|
||||
private fun getToSymbolLabel() =
|
||||
|
|
|
@ -37,7 +37,7 @@ import org.dslul.openboard.inputmethod.latin.utils.ColorUtilKt;
|
|||
import org.dslul.openboard.inputmethod.latin.utils.DeviceProtectedUtils;
|
||||
import org.dslul.openboard.inputmethod.latin.utils.JniUtils;
|
||||
import org.dslul.openboard.inputmethod.latin.utils.ResourceUtils;
|
||||
import org.dslul.openboard.inputmethod.latin.utils.RunInLocale;
|
||||
import org.dslul.openboard.inputmethod.latin.utils.RunInLocaleKt;
|
||||
import org.dslul.openboard.inputmethod.latin.utils.ScriptUtils;
|
||||
import org.dslul.openboard.inputmethod.latin.utils.StatsUtils;
|
||||
import org.dslul.openboard.inputmethod.latin.utils.SubtypeSettingsKt;
|
||||
|
@ -51,6 +51,8 @@ import java.util.List;
|
|||
import java.util.Locale;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import kotlin.jvm.functions.Function1;
|
||||
|
||||
public final class Settings implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
private static final String TAG = Settings.class.getSimpleName();
|
||||
// Settings screens
|
||||
|
@ -228,13 +230,8 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
|
|||
try {
|
||||
final SharedPreferences prefs = mPrefs;
|
||||
Log.i(TAG, "loadSettings");
|
||||
final RunInLocale<SettingsValues> job = new RunInLocale<>() {
|
||||
@Override
|
||||
protected SettingsValues job(final Resources res) {
|
||||
return new SettingsValues(context, prefs, res, inputAttributes);
|
||||
}
|
||||
};
|
||||
mSettingsValues = job.runInLocale(mRes, locale);
|
||||
mSettingsValues = RunInLocaleKt.runInLocale(context, locale,
|
||||
(Function1<Context, SettingsValues>) ctx -> new SettingsValues(ctx, prefs, ctx.getResources(), inputAttributes));
|
||||
} finally {
|
||||
mSettingsValuesLock.unlock();
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ import java.util.Locale;
|
|||
|
||||
/**
|
||||
* When you call the constructor of this class, you may want to change the current system locale by
|
||||
* using {@link org.dslul.openboard.inputmethod.latin.utils.RunInLocale}.
|
||||
* using {@link org.dslul.openboard.inputmethod.latin.utils.RunInLocaleKt}.
|
||||
*/
|
||||
// Non-final for testing via mock library.
|
||||
public class SettingsValues {
|
||||
|
|
|
@ -13,11 +13,13 @@ import android.view.textservice.TextInfo;
|
|||
|
||||
import org.dslul.openboard.inputmethod.latin.common.Constants;
|
||||
import org.dslul.openboard.inputmethod.latin.settings.SpacingAndPunctuations;
|
||||
import org.dslul.openboard.inputmethod.latin.utils.RunInLocale;
|
||||
import org.dslul.openboard.inputmethod.latin.utils.RunInLocaleKt;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Locale;
|
||||
|
||||
import kotlin.jvm.functions.Function1;
|
||||
|
||||
/**
|
||||
* This code is mostly lifted directly from android.service.textservice.SpellCheckerService in
|
||||
* the framework; maybe that should be protected instead, so that implementers don't have to
|
||||
|
@ -65,13 +67,8 @@ public class SentenceLevelAdapter {
|
|||
private static class WordIterator {
|
||||
private final SpacingAndPunctuations mSpacingAndPunctuations;
|
||||
public WordIterator(final Resources res, final Locale locale) {
|
||||
final RunInLocale<SpacingAndPunctuations> job = new RunInLocale<>() {
|
||||
@Override
|
||||
protected SpacingAndPunctuations job(final Resources r) {
|
||||
return new SpacingAndPunctuations(r, false);
|
||||
}
|
||||
};
|
||||
mSpacingAndPunctuations = job.runInLocale(res, locale);
|
||||
mSpacingAndPunctuations = RunInLocaleKt.runInLocale(res, locale,
|
||||
(Function1<Resources, SpacingAndPunctuations>) r -> new SpacingAndPunctuations(r, false));
|
||||
}
|
||||
|
||||
public int getEndOfWord(final CharSequence sequence, final int fromIndex) {
|
||||
|
|
|
@ -1,43 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2013 The Android Open Source Project
|
||||
* modified
|
||||
* SPDX-License-Identifier: Apache-2.0 AND GPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.dslul.openboard.inputmethod.latin.utils;
|
||||
|
||||
import android.content.res.Configuration;
|
||||
import android.content.res.Resources;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public abstract class RunInLocale<T> {
|
||||
private static final Object sLockForRunInLocale = new Object();
|
||||
|
||||
protected abstract T job(final Resources res);
|
||||
|
||||
/**
|
||||
* Execute {@link #job(Resources)} method in specified system locale exclusively.
|
||||
*
|
||||
* @param res the resources to use.
|
||||
* @param newLocale the locale to change to. Run in system locale if null.
|
||||
* @return the value returned from {@link #job(Resources)}.
|
||||
*/
|
||||
public T runInLocale(final Resources res, final Locale newLocale) {
|
||||
synchronized (sLockForRunInLocale) {
|
||||
final Configuration conf = res.getConfiguration();
|
||||
if (newLocale == null || newLocale.equals(conf.locale)) {
|
||||
return job(res);
|
||||
}
|
||||
final Locale savedLocale = conf.locale;
|
||||
try {
|
||||
conf.locale = newLocale;
|
||||
res.updateConfiguration(conf, null);
|
||||
return job(res);
|
||||
} finally {
|
||||
conf.locale = savedLocale;
|
||||
res.updateConfiguration(conf, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
package org.dslul.openboard.inputmethod.latin.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.content.res.Configuration
|
||||
import android.content.res.Resources
|
||||
import java.util.Locale
|
||||
|
||||
fun <T> runInLocale(context: Context, locale: Locale, run: (Context) -> T): T {
|
||||
val config = Configuration(context.resources.configuration)
|
||||
config.setLocale(locale)
|
||||
val localeContext = context.createConfigurationContext(config)
|
||||
return run(localeContext)
|
||||
}
|
||||
|
||||
// slower than context and deprecated, but still better than original style
|
||||
fun <T> runInLocale(resources: Resources, locale: Locale, run: (Resources) -> T): T {
|
||||
val config = Configuration(resources.configuration)
|
||||
config.setLocale(locale)
|
||||
@Suppress("deprecation") val res = Resources(resources.assets, resources.displayMetrics, config)
|
||||
return run(res)
|
||||
}
|
|
@ -24,6 +24,8 @@ import static org.dslul.openboard.inputmethod.latin.common.Constants.Subtype.Ext
|
|||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import kotlin.jvm.functions.Function1;
|
||||
|
||||
/**
|
||||
* A helper class to deal with subtype locales.
|
||||
*/
|
||||
|
@ -186,13 +188,8 @@ public final class SubtypeLocaleUtils {
|
|||
|
||||
final String displayName;
|
||||
if (exceptionalNameResId != null) {
|
||||
final RunInLocale<String> getExceptionalName = new RunInLocale<String>() {
|
||||
@Override
|
||||
protected String job(final Resources res) {
|
||||
return res.getString(exceptionalNameResId);
|
||||
}
|
||||
};
|
||||
displayName = getExceptionalName.runInLocale(sResources, displayLocale);
|
||||
displayName = RunInLocaleKt.runInLocale(sResources, displayLocale,
|
||||
(Function1<Resources, String>) res -> res.getString(exceptionalNameResId));
|
||||
} else {
|
||||
displayName = LocaleUtils.constructLocaleFromString(localeString)
|
||||
.getDisplayName(displayLocale);
|
||||
|
@ -248,11 +245,10 @@ public final class SubtypeLocaleUtils {
|
|||
final String replacementString = getReplacementString(subtype, displayLocale);
|
||||
// TODO: rework this for multi-lingual subtypes
|
||||
final int nameResId = subtype.getNameResId();
|
||||
final RunInLocale<String> getSubtypeName = new RunInLocale<String>() {
|
||||
@Override
|
||||
protected String job(final Resources res) {
|
||||
return RunInLocaleKt.runInLocale(sResources, displayLocale,
|
||||
(Function1<Resources, String>) res -> {
|
||||
try {
|
||||
return res.getString(nameResId, replacementString);
|
||||
return StringUtils.capitalizeFirstCodePoint(res.getString(nameResId, replacementString), displayLocale);
|
||||
} catch (Resources.NotFoundException e) {
|
||||
// TODO: Remove this catch when InputMethodManager.getCurrentInputMethodSubtype
|
||||
// is fixed.
|
||||
|
@ -263,10 +259,7 @@ public final class SubtypeLocaleUtils {
|
|||
+ "\n" + DebugLogUtils.getStackTrace());
|
||||
return "";
|
||||
}
|
||||
}
|
||||
};
|
||||
return StringUtils.capitalizeFirstCodePoint(
|
||||
getSubtypeName.runInLocale(sResources, displayLocale), displayLocale);
|
||||
});
|
||||
}
|
||||
|
||||
@NonNull
|
||||
|
|
Loading…
Add table
Reference in a new issue