update SettingsActivity to use simpler findPreferences() method

This commit is contained in:
Hans-Christoph Steiner 2015-06-05 15:16:08 -04:00
parent 237366cd27
commit fba09263b4
1 changed files with 45 additions and 71 deletions

View File

@ -16,10 +16,11 @@ import android.content.SharedPreferences;
import android.content.res.Configuration; 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.ListPreference;
import android.preference.Preference; import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener; import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.widget.Toast; import android.widget.Toast;
@ -31,85 +32,60 @@ public class SettingsPreferences
private Preference prefTransProxyFlush = null; private Preference prefTransProxyFlush = null;
private Preference prefTransProxyApps = null; private Preference prefTransProxyApps = null;
private CheckBoxPreference prefHiddenServices = null; private CheckBoxPreference prefHiddenServices = null;
private EditTextPreference prefHiddenServicesPorts;
private EditTextPreference prefHiddenServicesHostname;
private CheckBoxPreference prefRequestRoot = null; private CheckBoxPreference prefRequestRoot = null;
private Preference prefLocale = null; private ListPreference prefLocale = null;
private boolean hasRoot = false; protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
private final static int HIDDEN_SERVICE_PREF_IDX = 6; addPreferencesFromResource(R.xml.preferences);
private final static int TRANSPROXY_GROUP_IDX = 1; getPreferenceManager().setSharedPreferencesMode(Context.MODE_MULTI_PROCESS);
private final static int DEBUG_GROUP_IDX = 8; SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
prefRequestRoot = (CheckBoxPreference) findPreference("has_root");
prefRequestRoot.setOnPreferenceClickListener(this);
protected void onCreate(Bundle savedInstanceState) prefLocale = (ListPreference) findPreference("pref_default_locale");
{ prefLocale.setOnPreferenceClickListener(this);
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences); prefCBTransProxy = (CheckBoxPreference) findPreference("pref_transparent");
getPreferenceManager().setSharedPreferencesMode(Context.MODE_MULTI_PROCESS); prefcBTransProxyAll = (CheckBoxPreference) findPreference("pref_transparent_all");
SharedPreferences prefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
hasRoot = prefs.getBoolean("has_root",false); prefTransProxyFlush = (Preference) findPreference("pref_transproxy_flush");
prefTransProxyFlush.setOnPreferenceClickListener(new OnPreferenceClickListener() {
init(); @Override
} public boolean onPreferenceClick(Preference arg0) {
private void init ()
{
int SET_LOCALE_IDX = 3;
int GENERAL_GROUP_IDX = 0;
prefRequestRoot = ((CheckBoxPreference)((PreferenceCategory)getPreferenceScreen().getPreference(TRANSPROXY_GROUP_IDX)).getPreference(0));
prefRequestRoot.setOnPreferenceClickListener(this);
prefLocale = (((PreferenceCategory)getPreferenceScreen().getPreference(GENERAL_GROUP_IDX)).getPreference(SET_LOCALE_IDX));
prefLocale.setOnPreferenceClickListener(this);
prefCBTransProxy = ((CheckBoxPreference)((PreferenceCategory)this.getPreferenceScreen().getPreference(TRANSPROXY_GROUP_IDX)).getPreference(1));
prefcBTransProxyAll = (CheckBoxPreference)((PreferenceCategory)this.getPreferenceScreen().getPreference(TRANSPROXY_GROUP_IDX)).getPreference(2);
prefTransProxyFlush = (Preference)((PreferenceCategory)this.getPreferenceScreen().getPreference(DEBUG_GROUP_IDX)).getPreference(8);
prefTransProxyFlush.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference arg0) {
Intent data = new Intent();
data.putExtra("transproxywipe", true);
setResult(RESULT_OK,data);
finish();
return false;
}
});
prefTransProxyApps = ((PreferenceCategory)this.getPreferenceScreen().getPreference(TRANSPROXY_GROUP_IDX)).getPreference(3);
prefCBTransProxy.setOnPreferenceClickListener(this);
prefcBTransProxyAll.setOnPreferenceClickListener(this);
prefTransProxyApps.setOnPreferenceClickListener(this);
prefcBTransProxyAll.setEnabled(prefCBTransProxy.isChecked());
prefTransProxyApps.setEnabled(prefCBTransProxy.isChecked() && (!prefcBTransProxyAll.isChecked()));
prefHiddenServices = ((CheckBoxPreference)((PreferenceCategory)this.getPreferenceScreen().getPreference(HIDDEN_SERVICE_PREF_IDX)).getPreference(0));
prefHiddenServices.setOnPreferenceClickListener(this);
((PreferenceCategory)this.getPreferenceScreen().getPreference(HIDDEN_SERVICE_PREF_IDX)).getPreference(1).setEnabled(prefHiddenServices.isChecked());
((PreferenceCategory)this.getPreferenceScreen().getPreference(HIDDEN_SERVICE_PREF_IDX)).getPreference(2).setEnabled(prefHiddenServices.isChecked());
};
Intent data = new Intent();
data.putExtra("transproxywipe", true);
setResult(RESULT_OK, data);
finish();
return false;
}
});
prefTransProxyApps = findPreference("pref_transparent_app_list");
prefTransProxyApps.setOnPreferenceClickListener(this);
prefTransProxyApps.setEnabled(prefCBTransProxy.isChecked()
&& (!prefcBTransProxyAll.isChecked()));
prefCBTransProxy.setOnPreferenceClickListener(this);
prefcBTransProxyAll.setOnPreferenceClickListener(this);
prefcBTransProxyAll.setEnabled(prefCBTransProxy.isChecked());
prefHiddenServices = (CheckBoxPreference) findPreference("pref_hs_enable");
prefHiddenServices.setOnPreferenceClickListener(this);
prefHiddenServicesPorts = (EditTextPreference) findPreference("pref_hs_ports");
prefHiddenServicesPorts.setEnabled(prefHiddenServices.isChecked());
prefHiddenServicesHostname = (EditTextPreference) findPreference("pref_hs_hostname");
prefHiddenServicesHostname.setEnabled(prefHiddenServices.isChecked());
}
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
@ -147,10 +123,8 @@ public class SettingsPreferences
} }
else if (preference == prefHiddenServices) else if (preference == prefHiddenServices)
{ {
prefHiddenServicesPorts.setEnabled(prefHiddenServices.isChecked());
((PreferenceCategory)this.getPreferenceScreen().getPreference(HIDDEN_SERVICE_PREF_IDX)).getPreference(1).setEnabled(prefHiddenServices.isChecked()); prefHiddenServicesHostname.setEnabled(prefHiddenServices.isChecked());
((PreferenceCategory)this.getPreferenceScreen().getPreference(HIDDEN_SERVICE_PREF_IDX)).getPreference(2).setEnabled(prefHiddenServices.isChecked());
} }
else if (preference == prefLocale) else if (preference == prefLocale)
{ {