automatic language selection on startup
This commit is contained in:
parent
7ae9a6f8e3
commit
756e2cdaec
10 changed files with 64 additions and 62 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
locale: fr
|
locale: fr-FR
|
||||||
dictionaryFile: fr-utf8.csv
|
dictionaryFile: fr-utf8.csv
|
||||||
layout:
|
layout:
|
||||||
- [SPECIAL] # 0
|
- [SPECIAL] # 0
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
locale: de
|
locale: de-DE
|
||||||
dictionaryFile: de-utf8.csv
|
dictionaryFile: de-utf8.csv
|
||||||
layout:
|
layout:
|
||||||
- [SPECIAL] # 0
|
- [SPECIAL] # 0
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
locale: it
|
locale: it-IT
|
||||||
dictionaryFile: it-utf8.csv
|
dictionaryFile: it-utf8.csv
|
||||||
layout:
|
layout:
|
||||||
- [SPECIAL] # 0
|
- [SPECIAL] # 0
|
||||||
|
|
|
||||||
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -28,28 +28,17 @@ public class Language {
|
||||||
}
|
}
|
||||||
|
|
||||||
Locale definitionLocale;
|
Locale definitionLocale;
|
||||||
switch (definition.locale) {
|
if (definition.locale.equals("en")) {
|
||||||
case "de":
|
definitionLocale = Locale.ENGLISH;
|
||||||
definitionLocale = Locale.GERMAN;
|
} else {
|
||||||
break;
|
String[] parts = definition.locale.split("-", 2);
|
||||||
case "en":
|
if (parts.length == 2) {
|
||||||
definitionLocale = Locale.ENGLISH;
|
definitionLocale = new Locale(parts[0], parts[1]);
|
||||||
break;
|
} else if (parts.length == 1) {
|
||||||
case "fr":
|
definitionLocale = new Locale(parts[0]);
|
||||||
definitionLocale = Locale.FRENCH;
|
} else {
|
||||||
break;
|
throw new Exception("Unrecognized locale format: '" + definition.locale + "'.");
|
||||||
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 + "'.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Language lang = new Language();
|
Language lang = new Language();
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import java.util.Comparator;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
import io.github.sspanak.tt9.Logger;
|
import io.github.sspanak.tt9.Logger;
|
||||||
|
import io.github.sspanak.tt9.ime.helpers.SystemSettings;
|
||||||
|
|
||||||
public class LanguageCollection {
|
public class LanguageCollection {
|
||||||
private static LanguageCollection self;
|
private static LanguageCollection self;
|
||||||
|
|
@ -46,7 +47,8 @@ public class LanguageCollection {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Language getDefault(Context context) {
|
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;
|
return language == null ? new NullLanguage(context) : language;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package io.github.sspanak.tt9.preferences;
|
package io.github.sspanak.tt9.preferences;
|
||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.inputmethod.InputMethodManager;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.appcompat.app.ActionBar;
|
import androidx.appcompat.app.ActionBar;
|
||||||
|
|
@ -15,10 +14,9 @@ import androidx.preference.PreferenceFragmentCompat;
|
||||||
|
|
||||||
import io.github.sspanak.tt9.Logger;
|
import io.github.sspanak.tt9.Logger;
|
||||||
import io.github.sspanak.tt9.R;
|
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.DictionaryLoader;
|
||||||
import io.github.sspanak.tt9.db.LegacyDb;
|
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.ime.helpers.InputModeValidator;
|
||||||
import io.github.sspanak.tt9.preferences.helpers.Hotkeys;
|
import io.github.sspanak.tt9.preferences.helpers.Hotkeys;
|
||||||
import io.github.sspanak.tt9.preferences.screens.AppearanceScreen;
|
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 class PreferencesActivity extends AppCompatActivity implements PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {
|
||||||
public SettingsStore settings;
|
public SettingsStore settings;
|
||||||
public GlobalKeyboardSettings globalKeyboardSettings;
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
globalKeyboardSettings = new GlobalKeyboardSettings(this, (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE));
|
|
||||||
settings = new SettingsStore(this);
|
settings = new SettingsStore(this);
|
||||||
applyTheme();
|
applyTheme();
|
||||||
Logger.enableDebugLevel(settings.getDebugLogsEnabled());
|
Logger.enableDebugLevel(settings.getDebugLogsEnabled());
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ import java.util.regex.Pattern;
|
||||||
import io.github.sspanak.tt9.BuildConfig;
|
import io.github.sspanak.tt9.BuildConfig;
|
||||||
import io.github.sspanak.tt9.Logger;
|
import io.github.sspanak.tt9.Logger;
|
||||||
import io.github.sspanak.tt9.R;
|
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.PreferencesActivity;
|
||||||
|
|
||||||
public class MainSettingsScreen extends BaseScreenFragment {
|
public class MainSettingsScreen extends BaseScreenFragment {
|
||||||
|
|
@ -80,11 +81,11 @@ public class MainSettingsScreen extends BaseScreenFragment {
|
||||||
|
|
||||||
|
|
||||||
private void createSettingsSection() {
|
private void createSettingsSection() {
|
||||||
boolean isTT9Enabled = activity.globalKeyboardSettings.isTT9Enabled();
|
boolean isTT9On = SystemSettings.isTT9Enabled(activity);
|
||||||
|
|
||||||
Preference gotoSetup = findPreference("screen_setup");
|
Preference gotoSetup = findPreference("screen_setup");
|
||||||
if (gotoSetup != null) {
|
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(
|
ArrayList<Preference> screens = new ArrayList<>(Arrays.asList(
|
||||||
|
|
@ -95,7 +96,7 @@ public class MainSettingsScreen extends BaseScreenFragment {
|
||||||
|
|
||||||
for (Preference goToScreen : screens) {
|
for (Preference goToScreen : screens) {
|
||||||
if (goToScreen != null) {
|
if (goToScreen != null) {
|
||||||
goToScreen.setEnabled(isTT9Enabled);
|
goToScreen.setEnabled(isTT9On);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package io.github.sspanak.tt9.preferences.screens;
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
|
|
||||||
import io.github.sspanak.tt9.R;
|
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.PreferencesActivity;
|
||||||
import io.github.sspanak.tt9.preferences.items.ItemSelectGlobalKeyboard;
|
import io.github.sspanak.tt9.preferences.items.ItemSelectGlobalKeyboard;
|
||||||
import io.github.sspanak.tt9.preferences.items.ItemSetDefaultGlobalKeyboard;
|
import io.github.sspanak.tt9.preferences.items.ItemSetDefaultGlobalKeyboard;
|
||||||
|
|
@ -26,7 +27,7 @@ public class SetupScreen extends BaseScreenFragment {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createKeyboardSection() {
|
private void createKeyboardSection() {
|
||||||
boolean isTT9On = activity.globalKeyboardSettings.isTT9Enabled();
|
boolean isTT9On = SystemSettings.isTT9Enabled(activity);
|
||||||
|
|
||||||
Preference statusItem = findPreference("global_tt9_status");
|
Preference statusItem = findPreference("global_tt9_status");
|
||||||
if (statusItem != null) {
|
if (statusItem != null) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue