Skip to content

Commit

Permalink
rename subtype settings and clean up when renaming in shared prefs
Browse files Browse the repository at this point in the history
  • Loading branch information
Helium314 committed Mar 6, 2024
1 parent 2a13ae1 commit aa94c84
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
18 changes: 17 additions & 1 deletion app/src/main/java/helium314/keyboard/latin/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,28 @@ fun checkVersionUpgrade(context: Context) {
}
if (oldVersion == 0) // new install or restoring settings from old app name
upgradesWhenComingFromOldAppName(context)
if (oldVersion == 1000) { // upgrade old custom layouts name
if (oldVersion <= 1000) { // upgrade old custom layouts name
val layoutsDir = Settings.getLayoutsDir(context)
val oldShiftSymbolsFile = File(layoutsDir, "${CUSTOM_LAYOUT_PREFIX}shift_symbols")
if (oldShiftSymbolsFile.exists()) {
oldShiftSymbolsFile.renameTo(File(layoutsDir, "${CUSTOM_LAYOUT_PREFIX}symbols_shifted"))
}

// rename subtype setting, and clean old subtypes that might remain in some cases
val subtypesPref = prefs.getString("enabled_input_styles", "")!!
.split(";").filter { it.isNotEmpty() }
.map {
val localeAndLayout = it.split(":").toMutableList()
localeAndLayout[0] = localeAndLayout[0].constructLocale().toLanguageTag()
localeAndLayout.joinToString(":")
}.toSet().joinToString(";")
val selectedSubtype = prefs.getString("selected_input_style", "")
prefs.edit {
remove("enabled_input_styles")
putString(Settings.PREF_ENABLED_SUBTYPES, subtypesPref)
remove("selected_input_style")
putString(Settings.PREF_SELECTED_SUBTYPE, selectedSubtype)
}
}
prefs.edit { putInt(Settings.PREF_VERSION_CODE, BuildConfig.VERSION_CODE) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import helium314.keyboard.latin.utils.Log;
import helium314.keyboard.latin.utils.ResourceUtils;
import helium314.keyboard.latin.utils.RunInLocaleKt;
import helium314.keyboard.latin.utils.ScriptUtils;
import helium314.keyboard.latin.utils.StatsUtils;
import helium314.keyboard.latin.utils.SubtypeSettingsKt;
import helium314.keyboard.latin.utils.ToolbarKey;
Expand Down Expand Up @@ -139,8 +138,8 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
public static final String PREF_ADD_TO_PERSONAL_DICTIONARY = "add_to_personal_dictionary";
public static final String PREF_NAVBAR_COLOR = "navbar_color";
public static final String PREF_NARROW_KEY_GAPS = "narrow_key_gaps";
public static final String PREF_ENABLED_INPUT_STYLES = "enabled_input_styles";
public static final String PREF_SELECTED_INPUT_STYLE = "selected_input_style";
public static final String PREF_ENABLED_SUBTYPES = "enabled_subtypes";
public static final String PREF_SELECTED_SUBTYPE = "selected_subtype";
public static final String PREF_USE_SYSTEM_LOCALES = "use_system_locales";
public static final String PREF_URL_DETECTION = "url_detection";
public static final String PREF_DONT_SHOW_MISSING_DICTIONARY_DIALOG = "dont_show_missing_dict_dialog";
Expand Down Expand Up @@ -179,7 +178,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
add(PREF_EMOJI_RECENT_KEYS);
add(PREF_DONT_SHOW_MISSING_DICTIONARY_DIALOG);
add(PREF_SHOW_ALL_COLORS);
add(PREF_SELECTED_INPUT_STYLE);
add(PREF_SELECTED_SUBTYPE);
}};

public static Settings getInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ fun getMatchingLayoutSetNameForLocale(locale: Locale): String {
fun addEnabledSubtype(prefs: SharedPreferences, newSubtype: InputMethodSubtype) {
require(initialized)
val subtypeString = newSubtype.prefString()
val oldSubtypeStrings = prefs.getString(Settings.PREF_ENABLED_INPUT_STYLES, "")!!.split(SUBTYPE_SEPARATOR)
val oldSubtypeStrings = prefs.getString(Settings.PREF_ENABLED_SUBTYPES, "")!!.split(SUBTYPE_SEPARATOR)
val newString = (oldSubtypeStrings + subtypeString).filter { it.isNotBlank() }.toSortedSet().joinToString(SUBTYPE_SEPARATOR)
prefs.edit { putString(Settings.PREF_ENABLED_INPUT_STYLES, newString) }
prefs.edit { putString(Settings.PREF_ENABLED_SUBTYPES, newString) }

if (newSubtype !in enabledSubtypes) {
enabledSubtypes.add(newSubtype)
Expand Down Expand Up @@ -94,7 +94,7 @@ fun removeAdditionalSubtype(prefs: SharedPreferences, resources: Resources, subt

fun getSelectedSubtype(prefs: SharedPreferences): InputMethodSubtype {
require(initialized)
val localeAndLayout = prefs.getString(Settings.PREF_SELECTED_INPUT_STYLE, "")!!.toLocaleAndLayout()
val localeAndLayout = prefs.getString(Settings.PREF_SELECTED_SUBTYPE, "")!!.toLocaleAndLayout()
val subtypes = if (prefs.getBoolean(Settings.PREF_USE_SYSTEM_LOCALES, true)) getDefaultEnabledSubtypes()
else enabledSubtypes
val subtype = subtypes.firstOrNull { localeAndLayout.first == it.locale() && localeAndLayout.second == SubtypeLocaleUtils.getKeyboardLayoutSetName(it) }
Expand All @@ -113,9 +113,9 @@ fun getSelectedSubtype(prefs: SharedPreferences): InputMethodSubtype {

fun setSelectedSubtype(prefs: SharedPreferences, subtype: InputMethodSubtype) {
val subtypeString = subtype.prefString()
if (subtype.locale().toLanguageTag().isEmpty() || prefs.getString(Settings.PREF_SELECTED_INPUT_STYLE, "") == subtypeString)
if (subtype.locale().toLanguageTag().isEmpty() || prefs.getString(Settings.PREF_SELECTED_SUBTYPE, "") == subtypeString)
return
prefs.edit { putString(Settings.PREF_SELECTED_INPUT_STYLE, subtypeString) }
prefs.edit { putString(Settings.PREF_SELECTED_SUBTYPE, subtypeString) }
}

fun isAdditionalSubtype(subtype: InputMethodSubtype): Boolean {
Expand Down Expand Up @@ -267,7 +267,7 @@ private fun loadAdditionalSubtypes(context: Context) {
// requires loadResourceSubtypes to be called before
private fun loadEnabledSubtypes(context: Context) {
val prefs = DeviceProtectedUtils.getSharedPreferences(context)
val subtypeStrings = prefs.getString(Settings.PREF_ENABLED_INPUT_STYLES, "")!!
val subtypeStrings = prefs.getString(Settings.PREF_ENABLED_SUBTYPES, "")!!
.split(SUBTYPE_SEPARATOR).filter { it.isNotEmpty() }.map { it.toLocaleAndLayout() }

for (localeAndLayout in subtypeStrings) {
Expand Down Expand Up @@ -299,12 +299,12 @@ private fun loadEnabledSubtypes(context: Context) {
}

private fun removeEnabledSubtype(prefs: SharedPreferences, subtypeString: String) {
val oldSubtypeString = prefs.getString(Settings.PREF_ENABLED_INPUT_STYLES, "")!!
val oldSubtypeString = prefs.getString(Settings.PREF_ENABLED_SUBTYPES, "")!!
val newString = (oldSubtypeString.split(SUBTYPE_SEPARATOR) - subtypeString).joinToString(SUBTYPE_SEPARATOR)
if (newString == oldSubtypeString)
return // already removed
prefs.edit { putString(Settings.PREF_ENABLED_INPUT_STYLES, newString) }
if (subtypeString == prefs.getString(Settings.PREF_SELECTED_INPUT_STYLE, "")) {
prefs.edit { putString(Settings.PREF_ENABLED_SUBTYPES, newString) }
if (subtypeString == prefs.getString(Settings.PREF_SELECTED_SUBTYPE, "")) {
// switch subtype if the currently used one has been disabled
try {
val nextSubtype = RichInputMethodManager.getInstance().getNextSubtypeInThisIme(true)
Expand Down

0 comments on commit aa94c84

Please sign in to comment.