1
0
mirror of https://github.com/etesync/android synced 2024-12-23 07:08:16 +00:00

Add debug option to force language.

Resolves #3 Add option to force a language
This commit is contained in:
Tal Hacohen 2017-09-12 18:29:20 +03:00 committed by Tom Hacohen
parent f631fe4452
commit d1fa0114e6
5 changed files with 170 additions and 5 deletions

View File

@ -48,6 +48,7 @@ import com.etesync.syncadapter.resource.LocalAddressBook;
import com.etesync.syncadapter.resource.LocalCalendar;
import com.etesync.syncadapter.ui.AccountsActivity;
import com.etesync.syncadapter.utils.HintManager;
import com.etesync.syncadapter.utils.LanguageUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
@ -55,6 +56,7 @@ import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
@ -81,11 +83,15 @@ public class App extends Application {
LOG_TO_EXTERNAL_STORAGE = "logToExternalStorage",
OVERRIDE_PROXY = "overrideProxy",
OVERRIDE_PROXY_HOST = "overrideProxyHost",
OVERRIDE_PROXY_PORT = "overrideProxyPort";
OVERRIDE_PROXY_PORT = "overrideProxyPort",
FORCE_LANGUAGE = "forceLanguage";
public static final String OVERRIDE_PROXY_HOST_DEFAULT = "localhost";
public static final int OVERRIDE_PROXY_PORT_DEFAULT = 8118;
public static final String DEFAULT_LANGUAGE = "default";
public static Locale sDefaultLocacle = Locale.getDefault();
@Getter
private static String appName;
@ -123,6 +129,16 @@ public class App extends Application {
accountType = getString(R.string.account_type);
addressBookAccountType = getString(R.string.account_type_address_book);
addressBooksAuthority = getString(R.string.address_books_authority);
loadLanguage();
}
private void loadLanguage() {
String lang = new Settings(new ServiceDB.OpenHelper(this).
getReadableDatabase()).getString(App.FORCE_LANGUAGE, null);
if (lang != null && !lang.equals(DEFAULT_LANGUAGE)) {
LanguageUtils.setLanguage(this, lang);
}
}
public void reinitCertManager() {

View File

@ -9,9 +9,11 @@
package com.etesync.syncadapter.ui;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.preference.EditTextPreference;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.support.v7.preference.SwitchPreferenceCompat;
@ -21,9 +23,11 @@ import com.etesync.syncadapter.R;
import com.etesync.syncadapter.model.ServiceDB;
import com.etesync.syncadapter.model.Settings;
import com.etesync.syncadapter.utils.HintManager;
import com.etesync.syncadapter.utils.LanguageUtils;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
public class AppSettingsActivity extends BaseActivity {
@ -125,13 +129,20 @@ public class AppSettingsActivity extends BaseActivity {
}
});
prefDistrustSystemCerts = (SwitchPreferenceCompat)findPreference("distrust_system_certs");
prefDistrustSystemCerts = (SwitchPreferenceCompat) findPreference("distrust_system_certs");
prefDistrustSystemCerts.setChecked(settings.getBoolean(App.DISTRUST_SYSTEM_CERTIFICATES, false));
prefResetCertificates = findPreference("reset_certificates");
prefLogToExternalStorage = (SwitchPreferenceCompat)findPreference("log_to_external_storage");
prefLogToExternalStorage = (SwitchPreferenceCompat) findPreference("log_to_external_storage");
prefLogToExternalStorage.setChecked(settings.getBoolean(App.LOG_TO_EXTERNAL_STORAGE, false));
initSelectLanguageList();
}
private void initSelectLanguageList() {
ListPreference listPreference = (ListPreference) findPreference("select_language");
new LanguageTask(listPreference).execute();
}
@Override
@ -174,12 +185,52 @@ public class AppSettingsActivity extends BaseActivity {
settings.putBoolean(App.LOG_TO_EXTERNAL_STORAGE, externalLogging);
// reinitialize logger of default process
App app = (App)getContext().getApplicationContext();
App app = (App) getContext().getApplicationContext();
app.reinitLogger();
// reinitialize logger of :sync process
getContext().sendBroadcast(new Intent(App.ReinitSettingsReceiver.ACTION_REINIT_SETTINGS));
}
}
private class LanguageTask extends AsyncTask<Void, Void, LanguageUtils.LocaleList> {
private ListPreference mListPreference;
LanguageTask(ListPreference listPreference) {
mListPreference = listPreference;
}
@Override
protected LanguageUtils.LocaleList doInBackground(Void... voids) {
return LanguageUtils.getAppLanguages(getContext());
}
@Override
protected void onPostExecute(LanguageUtils.LocaleList locales) {
mListPreference.setEntries(locales.getDisplayNames());
mListPreference.setEntryValues(locales.getLocaleData());
mListPreference.setValue(settings.getString(App.FORCE_LANGUAGE,
App.DEFAULT_LANGUAGE));
mListPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String value = newValue.toString();
if (value.equals(((ListPreference) preference).getValue())) return true;
LanguageUtils.setLanguage(getContext(), value);
settings.putString(App.FORCE_LANGUAGE, newValue.toString());
Intent intent = new Intent(getContext(), AccountsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
return false;
}
});
}
}
}
}

View File

@ -0,0 +1,92 @@
package com.etesync.syncadapter.utils;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import com.etesync.syncadapter.App;
import com.etesync.syncadapter.R;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Locale;
/**
* Created by tal on 11/09/17.
*/
public class LanguageUtils {
public static LocaleList getAppLanguages(Context context) {
Locale[] locales = Locale.getAvailableLocales();
Arrays.sort(locales, new Comparator<Locale>() {
@Override
public int compare(Locale aLocale, Locale aT1) {
return aLocale.getDisplayName().compareTo(aT1.getDisplayName());
}
});
String[] localeData = new String[locales.length + 1];
String[] displayNames = new String[locales.length + 1];
localeData[0] = App.DEFAULT_LANGUAGE;
displayNames[0] = context.getString(R.string.app_settings_force_language_default);
int index = 1;
for (Locale locale : locales) {
localeData[index] = encodeLocale(locale);
displayNames[index] = locale.getDisplayName();
index++;
}
return new LocaleList(localeData, displayNames);
}
public static void setLanguage(Context context, String locale) {
if (locale.equals(App.DEFAULT_LANGUAGE)) setLanguage(context, App.sDefaultLocacle);
else setLanguage(context, decodeLocale(locale));
}
@SuppressWarnings("deprecation")
public static void setLanguage(Context context, Locale locale) {
context = context.getApplicationContext();
Locale.setDefault(locale);
Configuration config = new Configuration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLocale(locale);
context.createConfigurationContext(config);
} else {
config.locale = locale;
context.getResources().updateConfiguration(
config, context.getResources().getDisplayMetrics());
}
context.getApplicationContext().getResources().updateConfiguration(config,
context.getResources().getDisplayMetrics());
}
private static String encodeLocale(Locale locale) {
return String.format("%s;%s;%s",
locale.getLanguage(), locale.getCountry(), locale.getVariant());
}
private static Locale decodeLocale(String encodedLocale) {
String[] data = encodedLocale.split(";", -1);
return new Locale(data[0], data[1], data[2]);
}
public static class LocaleList {
private final String[] mLocaleData;
private final String[] mDisplayNames;
LocaleList(String[] localeData, String[] displayName) {
mLocaleData = localeData;
mDisplayNames = displayName;
}
public String[] getDisplayNames() {
return mDisplayNames;
}
public String[] getLocaleData() {
return mLocaleData;
}
}
}

View File

@ -89,6 +89,8 @@
<string name="app_settings_log_to_external_storage_off">External file logging is disabled</string>
<string name="app_settings_show_debug_info">Show debug info</string>
<string name="app_settings_show_debug_info_details">View/share software and configuration details</string>
<string name="app_settings_force_language">Force Language</string>
<string name="app_settings_force_language_default">Auto</string>
<!-- AccountActivity -->
<string name="account_synchronize_now">Synchronize now</string>

View File

@ -63,6 +63,10 @@
android:summaryOn="@string/app_settings_log_to_external_storage_on"
android:summaryOff="@string/app_settings_log_to_external_storage_off"/>
<ListPreference
android:key="select_language"
android:title="@string/app_settings_select_language"/>
<Preference
android:title="@string/app_settings_show_debug_info"
android:summary="@string/app_settings_show_debug_info_details">