1
0
Fork 0

automatic language selection on startup

This commit is contained in:
sspanak 2024-02-11 11:56:50 +02:00 committed by Dimo Karaivanov
parent 7ae9a6f8e3
commit 756e2cdaec
10 changed files with 64 additions and 62 deletions

View file

@ -1,4 +1,4 @@
locale: fr
locale: fr-FR
dictionaryFile: fr-utf8.csv
layout:
- [SPECIAL] # 0

View file

@ -1,4 +1,4 @@
locale: de
locale: de-DE
dictionaryFile: de-utf8.csv
layout:
- [SPECIAL] # 0

View file

@ -1,4 +1,4 @@
locale: it
locale: it-IT
dictionaryFile: it-utf8.csv
layout:
- [SPECIAL] # 0

View file

@ -1,27 +0,0 @@
package io.github.sspanak.tt9.ime.helpers;
import android.content.Context;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
public class GlobalKeyboardSettings {
private final InputMethodManager inputManager;
private final String packageName;
public GlobalKeyboardSettings(Context context, InputMethodManager inputManager) {
this.inputManager = inputManager;
packageName = context.getPackageName();
}
public boolean isTT9Enabled() {
for (final InputMethodInfo imeInfo : inputManager.getEnabledInputMethodList()) {
if (packageName.equals(imeInfo.getPackageName())) {
return true;
}
}
return false;
}
}

View file

@ -0,0 +1,40 @@
package io.github.sspanak.tt9.ime.helpers;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.LocaleList;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import java.util.Locale;
public class SystemSettings {
private static InputMethodManager inputManager;
private static String packageName;
public static boolean isTT9Enabled(Activity context) {
inputManager = inputManager == null ? (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE) : inputManager;
packageName = packageName == null ? context.getPackageName() : packageName;
for (final InputMethodInfo imeInfo : inputManager.getEnabledInputMethodList()) {
if (packageName.equals(imeInfo.getPackageName())) {
return true;
}
}
return false;
}
public static String getLocale() {
Locale locale = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? LocaleList.getDefault().get(0) : Locale.getDefault();
String country = locale.getCountry();
String language = locale.getLanguage();
if (language.equals(Locale.ENGLISH.getLanguage())) {
country = "";
}
return country.isEmpty() ? language : language + "_" + country;
}
}

View file

@ -28,28 +28,17 @@ public class Language {
}
Locale definitionLocale;
switch (definition.locale) {
case "de":
definitionLocale = Locale.GERMAN;
break;
case "en":
definitionLocale = Locale.ENGLISH;
break;
case "fr":
definitionLocale = Locale.FRENCH;
break;
case "it":
definitionLocale = Locale.ITALIAN;
break;
default:
String[] parts = definition.locale.split("-", 2);
if (parts.length == 2) {
definitionLocale = new Locale(parts[0], parts[1]);
} else if (parts.length == 1) {
definitionLocale = new Locale(parts[0]);
} else {
throw new Exception("Unrecognized locale format: '" + definition.locale + "'.");
}
if (definition.locale.equals("en")) {
definitionLocale = Locale.ENGLISH;
} else {
String[] parts = definition.locale.split("-", 2);
if (parts.length == 2) {
definitionLocale = new Locale(parts[0], parts[1]);
} else if (parts.length == 1) {
definitionLocale = new Locale(parts[0]);
} else {
throw new Exception("Unrecognized locale format: '" + definition.locale + "'.");
}
}
Language lang = new Language();

View file

@ -10,6 +10,7 @@ import java.util.Comparator;
import java.util.HashMap;
import io.github.sspanak.tt9.Logger;
import io.github.sspanak.tt9.ime.helpers.SystemSettings;
public class LanguageCollection {
private static LanguageCollection self;
@ -46,7 +47,8 @@ public class LanguageCollection {
}
public static Language getDefault(Context context) {
Language language = getByLocale(context, "en");
Language language = getByLocale(context, SystemSettings.getLocale());
language = language == null ? getByLocale(context, "en") : language;
return language == null ? new NullLanguage(context) : language;
}

View file

@ -1,7 +1,6 @@
package io.github.sspanak.tt9.preferences;
import android.os.Bundle;
import android.view.inputmethod.InputMethodManager;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
@ -15,10 +14,9 @@ import androidx.preference.PreferenceFragmentCompat;
import io.github.sspanak.tt9.Logger;
import io.github.sspanak.tt9.R;
import io.github.sspanak.tt9.db.WordStoreAsync;
import io.github.sspanak.tt9.db.DictionaryLoader;
import io.github.sspanak.tt9.db.LegacyDb;
import io.github.sspanak.tt9.ime.helpers.GlobalKeyboardSettings;
import io.github.sspanak.tt9.db.WordStoreAsync;
import io.github.sspanak.tt9.ime.helpers.InputModeValidator;
import io.github.sspanak.tt9.preferences.helpers.Hotkeys;
import io.github.sspanak.tt9.preferences.screens.AppearanceScreen;
@ -33,12 +31,10 @@ import io.github.sspanak.tt9.ui.DictionaryLoadingBar;
public class PreferencesActivity extends AppCompatActivity implements PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {
public SettingsStore settings;
public GlobalKeyboardSettings globalKeyboardSettings;
@Override
protected void onCreate(Bundle savedInstanceState) {
globalKeyboardSettings = new GlobalKeyboardSettings(this, (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE));
settings = new SettingsStore(this);
applyTheme();
Logger.enableDebugLevel(settings.getDebugLogsEnabled());

View file

@ -12,6 +12,7 @@ import java.util.regex.Pattern;
import io.github.sspanak.tt9.BuildConfig;
import io.github.sspanak.tt9.Logger;
import io.github.sspanak.tt9.R;
import io.github.sspanak.tt9.ime.helpers.SystemSettings;
import io.github.sspanak.tt9.preferences.PreferencesActivity;
public class MainSettingsScreen extends BaseScreenFragment {
@ -80,11 +81,11 @@ public class MainSettingsScreen extends BaseScreenFragment {
private void createSettingsSection() {
boolean isTT9Enabled = activity.globalKeyboardSettings.isTT9Enabled();
boolean isTT9On = SystemSettings.isTT9Enabled(activity);
Preference gotoSetup = findPreference("screen_setup");
if (gotoSetup != null) {
gotoSetup.setSummary(isTT9Enabled ? "" : activity.getString(R.string.setup_click_here_to_enable));
gotoSetup.setSummary(isTT9On ? "" : activity.getString(R.string.setup_click_here_to_enable));
}
ArrayList<Preference> screens = new ArrayList<>(Arrays.asList(
@ -95,7 +96,7 @@ public class MainSettingsScreen extends BaseScreenFragment {
for (Preference goToScreen : screens) {
if (goToScreen != null) {
goToScreen.setEnabled(isTT9Enabled);
goToScreen.setEnabled(isTT9On);
}
}
}

View file

@ -3,6 +3,7 @@ package io.github.sspanak.tt9.preferences.screens;
import androidx.preference.Preference;
import io.github.sspanak.tt9.R;
import io.github.sspanak.tt9.ime.helpers.SystemSettings;
import io.github.sspanak.tt9.preferences.PreferencesActivity;
import io.github.sspanak.tt9.preferences.items.ItemSelectGlobalKeyboard;
import io.github.sspanak.tt9.preferences.items.ItemSetDefaultGlobalKeyboard;
@ -26,7 +27,7 @@ public class SetupScreen extends BaseScreenFragment {
}
private void createKeyboardSection() {
boolean isTT9On = activity.globalKeyboardSettings.isTT9Enabled();
boolean isTT9On = SystemSettings.isTT9Enabled(activity);
Preference statusItem = findPreference("global_tt9_status");
if (statusItem != null) {