rework setting locale from pref to have a lighter touch and be dynamic

This leaves the default Locale unchanged, i.e. Locale.setDefault().  This
also will immediately change the language after the user selects it in the
pref.
This commit is contained in:
Hans-Christoph Steiner 2015-06-05 17:09:10 -04:00
parent 807b51fb43
commit fbf9932be3
3 changed files with 97 additions and 113 deletions

View File

@ -1,72 +1,87 @@
package org.torproject.android; package org.torproject.android;
import java.util.Locale; import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Configuration;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import info.guardianproject.util.Languages;
import org.torproject.android.service.TorServiceUtils; import org.torproject.android.service.TorServiceUtils;
import android.app.Activity; import java.util.Locale;
import android.app.Application;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.preference.PreferenceManager;
import info.guardianproject.util.Languages;
public class OrbotApp extends Application implements OrbotConstants public class OrbotApp extends Application implements OrbotConstants
{ {
private Locale locale; private Locale locale;
private final static String DEFAULT_LOCALE = "en"; private SharedPreferences prefs;
private SharedPreferences settings;
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
settings = TorServiceUtils.getSharedPrefs(getApplicationContext()); prefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
setNewLocale(prefs.getString(PREF_DEFAULT_LOCALE, Locale.getDefault().getLanguage()));
Configuration config = getResources().getConfiguration();
String lang = settings.getString(PREF_DEFAULT_LOCALE, DEFAULT_LOCALE);
if (! "".equals(lang) && ! config.locale.getLanguage().equals(lang))
{
if (lang.equals("xx"))
{
locale = Locale.getDefault();
}
else
locale = new Locale(lang);
Locale.setDefault(locale);
Configuration myConfig = new Configuration(config);
myConfig.locale = locale;
getResources().updateConfiguration(myConfig, getResources().getDisplayMetrics());
}
} }
@Override @Override
public void onConfigurationChanged(Configuration newConfig) public void onConfigurationChanged(Configuration newConfig) {
{
super.onConfigurationChanged(newConfig); super.onConfigurationChanged(newConfig);
Log.i(TAG, "onConfigurationChanged " + newConfig.locale.getLanguage());
setNewLocale(prefs.getString(PREF_DEFAULT_LOCALE, Locale.getDefault().getLanguage()));
}
String lang = settings.getString(PREF_DEFAULT_LOCALE, DEFAULT_LOCALE); @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public void setNewLocale(String language) {
if (TextUtils.isEmpty(language))
return;
if (! "".equals(lang) && ! newConfig.locale.getLanguage().equals(lang)) if (locale != null && TextUtils.equals(locale.getLanguage(), language))
{ return; // already configured
locale = new Locale(lang);
Locale.setDefault(locale);
Configuration myConfig = new Configuration(newConfig); /* handle locales with the country in it, i.e. zh_CN, zh_TW, etc */
myConfig.locale = locale; String localeSplit[] = language.split("_");
if (localeSplit.length > 1)
locale = new Locale(localeSplit[0], localeSplit[1]);
else
locale = new Locale(language);
Configuration config = getResources().getConfiguration();
if (Build.VERSION.SDK_INT >= 17)
config.setLocale(locale);
else
config.locale = locale;
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
getResources().updateConfiguration(myConfig, getResources().getDisplayMetrics()); /*
} * Set the preference after setting the locale in case something goes
* wrong. If setting the locale causes an Exception, it should be set in
* the preferences, otherwise ChatSecure will be stuck in a crash loop.
*/
SharedPreferences prefs = TorServiceUtils.getSharedPrefs(this);
Editor prefEdit = prefs.edit();
prefEdit.putString(PREF_DEFAULT_LOCALE, language);
prefEdit.apply();
Log.i(TAG, "setNewLocale complete: locale: " + locale.getLanguage()
+ " Locale.getDefault: " + Locale.getDefault().getLanguage());
}
public static void forceChangeLanguage(Activity activity) {
Intent intent = activity.getIntent();
if (intent == null) // when launched as LAUNCHER
intent = new Intent(activity, OrbotMainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
activity.finish();
activity.overridePendingTransition(0, 0);
activity.startActivity(intent);
activity.overridePendingTransition(0, 0);
} }
public static Languages getLanguages(Activity activity) { public static Languages getLanguages(Activity activity) {

View File

@ -95,6 +95,7 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
private final static long INIT_DELAY = 100; private final static long INIT_DELAY = 100;
private final static int REQUEST_VPN = 8888; private final static int REQUEST_VPN = 8888;
private final static int REQUEST_SETTINGS = 0x9874;
/** Called when the activity is first created. */ /** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
@ -103,8 +104,6 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
mPrefs = TorServiceUtils.getSharedPrefs(getApplicationContext()); mPrefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
mPrefs.registerOnSharedPreferenceChangeListener(this); mPrefs.registerOnSharedPreferenceChangeListener(this);
setLocale();
doLayout(); doLayout();
// Register to receive messages. // Register to receive messages.
@ -390,7 +389,8 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
if (item.getItemId() == R.id.menu_settings) if (item.getItemId() == R.id.menu_settings)
{ {
showSettings(); Intent intent = new Intent(OrbotMainActivity.this, SettingsPreferences.class);
startActivityForResult(intent, REQUEST_SETTINGS);
} }
else if (item.getItemId() == R.id.menu_wizard) else if (item.getItemId() == R.id.menu_wizard)
{ {
@ -806,23 +806,13 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
} }
} }
/*
* Load the basic settings application to display torrc
*/
private void showSettings ()
{
startActivityForResult(new Intent(this, SettingsPreferences.class), 1);
}
@Override @Override
protected void onActivityResult(int request, int response, Intent data) { protected void onActivityResult(int request, int response, Intent data) {
super.onActivityResult(request, response, data); super.onActivityResult(request, response, data);
if (request == REQUEST_SETTINGS && response == RESULT_OK)
if (request == 1 && response == RESULT_OK)
{ {
OrbotApp.forceChangeLanguage(this);
if (data != null && data.getBooleanExtra("transproxywipe", false)) if (data != null && data.getBooleanExtra("transproxywipe", false))
{ {
@ -1166,8 +1156,6 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
public void run () public void run ()
{ {
setLocale();
handleIntents(); handleIntents();
} }
@ -1428,24 +1416,6 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
}; };
// this is the connection that gets called back when a successfull bind occurs
// we should use this to activity monitor unbind so that we don't have to call
// bindService() a million times
private void setLocale ()
{
Configuration config = getResources().getConfiguration();
String lang = mPrefs.getString(PREF_DEFAULT_LOCALE, "");
if (! "".equals(lang) && ! config.locale.getLanguage().equals(lang))
{
Locale locale = new Locale(lang);
Locale.setDefault(locale);
config.locale = locale;
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
}
}
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();

View File

@ -6,12 +6,12 @@ package org.torproject.android.settings;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle; import android.os.Bundle;
import android.preference.CheckBoxPreference; import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference; import android.preference.EditTextPreference;
import android.preference.ListPreference; import android.preference.ListPreference;
import android.preference.Preference; import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener; import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.widget.Toast; import android.widget.Toast;
@ -20,6 +20,7 @@ import info.guardianproject.util.Languages;
import org.sufficientlysecure.rootcommands.RootCommands; import org.sufficientlysecure.rootcommands.RootCommands;
import org.sufficientlysecure.rootcommands.Shell; import org.sufficientlysecure.rootcommands.Shell;
import org.torproject.android.OrbotApp;
import org.torproject.android.R; import org.torproject.android.R;
import org.torproject.android.service.TorServiceUtils; import org.torproject.android.service.TorServiceUtils;
@ -56,6 +57,26 @@ public class SettingsPreferences
Languages languages = Languages.get(this, R.string.menu_settings, "Settings"); Languages languages = Languages.get(this, R.string.menu_settings, "Settings");
prefLocale.setEntries(languages.getAllNames()); prefLocale.setEntries(languages.getAllNames());
prefLocale.setEntryValues(languages.getSupportedLocales()); prefLocale.setEntryValues(languages.getSupportedLocales());
prefLocale.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String language = (String) newValue;
if (preference == prefLocale) {
SharedPreferences settings = TorServiceUtils
.getSharedPrefs(getApplicationContext());
String lang = settings.getString("pref_default_locale",
Locale.getDefault().getLanguage());
OrbotApp app = (OrbotApp) getApplication();
app.setNewLocale(language);
lang = settings.getString("pref_default_locale",
Locale.getDefault().getLanguage());
OrbotApp.forceChangeLanguage(SettingsPreferences.this);
}
return false;
}
});
prefCBTransProxy = (CheckBoxPreference) findPreference("pref_transparent"); prefCBTransProxy = (CheckBoxPreference) findPreference("pref_transparent");
prefcBTransProxyAll = (CheckBoxPreference) findPreference("pref_transparent_all"); prefcBTransProxyAll = (CheckBoxPreference) findPreference("pref_transparent_all");
@ -131,28 +152,6 @@ public class SettingsPreferences
{ {
prefHiddenServicesPorts.setEnabled(prefHiddenServices.isChecked()); prefHiddenServicesPorts.setEnabled(prefHiddenServices.isChecked());
prefHiddenServicesHostname.setEnabled(prefHiddenServices.isChecked()); prefHiddenServicesHostname.setEnabled(prefHiddenServices.isChecked());
}
else if (preference == prefLocale)
{
SharedPreferences settings = TorServiceUtils.getSharedPrefs(getApplicationContext());
Configuration config = getResources().getConfiguration();
String lang = settings.getString("pref_default_locale", "en");
Locale locale;
if (lang.equals("xx"))
{
locale = Locale.getDefault();
}
else
locale = new Locale(lang);
Locale.setDefault(locale);
config.locale = locale;
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
} }
else else
{ {