Allow selecting language

This commit is contained in:
M66B 2020-10-05 21:09:13 +02:00
parent 7ac16d007e
commit 81105e7055
6 changed files with 104 additions and 33 deletions

View File

@ -39,14 +39,24 @@ import android.webkit.CookieManager;
import androidx.preference.PreferenceManager;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public class ApplicationEx extends Application {
public class ApplicationEx extends Application implements SharedPreferences.OnSharedPreferenceChangeListener {
private Thread.UncaughtExceptionHandler prev = null;
private static final List<String> OPTIONS_RESTART = Collections.unmodifiableList(Arrays.asList(
"secure", // privacy
"shortcuts", // misc
"language", // misc
"query_threads" // misc
));
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(getLocalizedContext(base));
@ -54,14 +64,26 @@ public class ApplicationEx extends Application {
static Context getLocalizedContext(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean english = prefs.getBoolean("english", false);
if (english) {
if (prefs.contains("english")) {
boolean english = prefs.getBoolean("english", false);
if (english)
prefs.edit()
.remove("english")
.putString("language", Locale.US.toLanguageTag())
.commit();
}
String language = prefs.getString("language", null);
if (language != null) {
Locale locale = Locale.forLanguageTag(language);
Locale.setDefault(locale);
Configuration config = new Configuration(context.getResources().getConfiguration());
config.setLocale(Locale.US);
config.setLocale(locale);
return context.createConfigurationContext(config);
} else
return context;
}
return context;
}
@Override
@ -81,6 +103,7 @@ public class ApplicationEx extends Application {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
final boolean crash_reports = prefs.getBoolean("crash_reports", false);
prefs.registerOnSharedPreferenceChangeListener(this);
prev = Thread.getDefaultUncaughtExceptionHandler();
@ -136,6 +159,19 @@ public class ApplicationEx extends Application {
Log.i("App created " + (end - start) + " ms");
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (OPTIONS_RESTART.contains(key))
restart();
}
void restart() {
Intent intent = new Intent(this, ActivityMain.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Runtime.getRuntime().exit(0);
}
@Override
public void onTrimMemory(int level) {
Log.logMemory(this, "Trim memory level=" + level);

View File

@ -176,13 +176,6 @@ public class FragmentBase extends Fragment {
finish = true;
}
protected void restart() {
Intent intent = new Intent(getContext(), ActivityMain.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Runtime.getRuntime().exit(0);
}
@Override
public void onSaveInstanceState(Bundle outState) {
Log.d("Save instance " + this);

View File

@ -39,8 +39,11 @@ import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
@ -59,6 +62,7 @@ import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.SortedMap;
import javax.net.ssl.SSLSocket;
@ -74,7 +78,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
private SwitchCompat swFts;
private TextView tvFtsIndexed;
private TextView tvFtsPro;
private SwitchCompat swEnglish;
private Spinner spLanguage;
private SwitchCompat swWatchdog;
private SwitchCompat swUpdates;
private SwitchCompat swExperiments;
@ -105,7 +109,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
private Group grpDebug;
private final static String[] RESET_OPTIONS = new String[]{
"shortcuts", "fts", "english", "watchdog", "updates",
"shortcuts", "fts", "language", "watchdog", "updates",
"experiments", "query_threads", "crash_reports", "cleanup_attachments",
"protocol", "debug", "auth_plain", "auth_login", "auth_sasl"
};
@ -135,7 +139,7 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
swFts = view.findViewById(R.id.swFts);
tvFtsIndexed = view.findViewById(R.id.tvFtsIndexed);
tvFtsPro = view.findViewById(R.id.tvFtsPro);
swEnglish = view.findViewById(R.id.swEnglish);
spLanguage = view.findViewById(R.id.spLanguage);
swWatchdog = view.findViewById(R.id.swWatchdog);
swUpdates = view.findViewById(R.id.swUpdates);
swExperiments = view.findViewById(R.id.swExperiments);
@ -182,7 +186,6 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("shortcuts", checked).commit(); // apply won't work here
restart();
}
});
@ -225,11 +228,20 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
Helper.linkPro(tvFtsPro);
swEnglish.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
spLanguage.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("english", checked).commit(); // apply won't work here
restart();
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
if (position == 0)
onNothingSelected(adapterView);
else {
String tag = getResources().getAssets().getLocales()[position - 1];
prefs.edit().putString("language", tag).commit(); // apply won't work here
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
prefs.edit().remove("language").commit(); // apply won't work here
}
});
@ -274,7 +286,6 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
prefs.edit().putInt("query_threads", 2).commit(); // apply won't work here
else
prefs.edit().remove("query_threads").commit(); // apply won't work here
restart();
}
});
@ -593,7 +604,27 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
swExternalSearch.setChecked(state != PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
swShortcuts.setChecked(prefs.getBoolean("shortcuts", true));
swFts.setChecked(prefs.getBoolean("fts", false));
swEnglish.setChecked(prefs.getBoolean("english", false));
String language = prefs.getString("language", null);
String[] languages = getResources().getAssets().getLocales();
int selected = -1;
List<String> display = new ArrayList<>();
display.add(getString(R.string.title_advanced_language_system));
for (int pos = 0; pos < languages.length; pos++) {
String lang = languages[pos];
Locale loc = Locale.forLanguageTag(lang);
display.add(loc.getDisplayName() + " [" + lang + "]");
if (lang.equals(language))
selected = pos + 1;
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, android.R.id.text1, display);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spLanguage.setAdapter(adapter);
if (selected >= 0)
spLanguage.setSelection(selected);
swWatchdog.setChecked(prefs.getBoolean("watchdog", true));
swUpdates.setChecked(prefs.getBoolean("updates", true));
swUpdates.setVisibility(

View File

@ -230,7 +230,6 @@ public class FragmentOptionsPrivacy extends FragmentBase implements SharedPrefer
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("secure", checked).commit(); // apply won't work here
restart();
}
});

View File

@ -112,28 +112,39 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvFtsIndexed" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/swEnglish"
<eu.faircode.email.FixedTextView
android:id="@+id/tvLanguage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/title_advanced_english"
android:layout_marginEnd="48dp"
android:text="@string/title_advanced_language"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="?android:attr/textColorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvFtsPro"
app:switchPadding="12dp" />
app:layout_constraintTop_toBottomOf="@id/tvFtsPro" />
<Spinner
android:id="@+id/spLanguage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvLanguage" />
<eu.faircode.email.FixedTextView
android:id="@+id/tvEnglishHint"
android:id="@+id/tvLanguageHint"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_marginEnd="48dp"
android:text="@string/title_advanced_english_hint"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swEnglish" />
app:layout_constraintTop_toBottomOf="@id/spLanguage" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/swWatchdog"
@ -144,7 +155,7 @@
android:text="@string/title_advanced_watchdog"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvEnglishHint"
app:layout_constraintTop_toBottomOf="@id/tvLanguageHint"
app:switchPadding="12dp" />
<androidx.appcompat.widget.SwitchCompat

View File

@ -494,7 +494,8 @@
<string name="title_advanced_language_detection">Detect message text language</string>
<string name="title_advanced_fts">Build search index</string>
<string name="title_advanced_fts_indexed">%1$d / %2$d messages indexed (%3$s)</string>
<string name="title_advanced_english">Use US country settings</string>
<string name="title_advanced_language">Language</string>
<string name="title_advanced_language_system">System</string>
<string name="title_advanced_watchdog">Periodically check if FairEmail is still active</string>
<string name="title_advanced_auth_plain" translatable="false">PLAIN</string>
<string name="title_advanced_auth_login" translatable="false">LOGIN</string>