diff --git a/app/src/main/java/com/etesync/syncadapter/App.java b/app/src/main/java/com/etesync/syncadapter/App.java index 6e88e631..d2d3b55d 100644 --- a/app/src/main/java/com/etesync/syncadapter/App.java +++ b/app/src/main/java/com/etesync/syncadapter/App.java @@ -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() { diff --git a/app/src/main/java/com/etesync/syncadapter/ui/AppSettingsActivity.java b/app/src/main/java/com/etesync/syncadapter/ui/AppSettingsActivity.java index e85104a9..0162ecbd 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/AppSettingsActivity.java +++ b/app/src/main/java/com/etesync/syncadapter/ui/AppSettingsActivity.java @@ -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 { + 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; + } + }); + } + } + } } diff --git a/app/src/main/java/com/etesync/syncadapter/utils/LanguageUtils.java b/app/src/main/java/com/etesync/syncadapter/utils/LanguageUtils.java new file mode 100644 index 00000000..518bc3e3 --- /dev/null +++ b/app/src/main/java/com/etesync/syncadapter/utils/LanguageUtils.java @@ -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() { + @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; + } + + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0d3fd53c..2b1cd573 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -89,6 +89,8 @@ External file logging is disabled Show debug info View/share software and configuration details + Force Language + Auto Synchronize now diff --git a/app/src/main/res/xml/settings_app.xml b/app/src/main/res/xml/settings_app.xml index 31cfd3c8..44d7bddb 100644 --- a/app/src/main/res/xml/settings_app.xml +++ b/app/src/main/res/xml/settings_app.xml @@ -63,6 +63,10 @@ android:summaryOn="@string/app_settings_log_to_external_storage_on" android:summaryOff="@string/app_settings_log_to_external_storage_off"/> + +