2011-10-28 04:31:45 +00:00
|
|
|
/* Copyright (c) 2009, Nathan Freitas, Orbot / The Guardian Project - http://openideals.com/guardian */
|
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
package org.torproject.android.settings;
|
|
|
|
|
2012-02-24 05:13:27 +00:00
|
|
|
import java.util.Locale;
|
|
|
|
|
2011-10-28 04:31:45 +00:00
|
|
|
import org.torproject.android.R;
|
|
|
|
import org.torproject.android.R.xml;
|
2012-01-13 02:53:42 +00:00
|
|
|
import org.torproject.android.TorConstants;
|
2011-10-28 04:31:45 +00:00
|
|
|
import org.torproject.android.service.TorServiceUtils;
|
|
|
|
import org.torproject.android.service.TorTransProxy;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.content.SharedPreferences.Editor;
|
2012-02-24 05:13:27 +00:00
|
|
|
import android.content.res.Configuration;
|
2011-10-28 04:31:45 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.preference.CheckBoxPreference;
|
|
|
|
import android.preference.Preference;
|
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
import android.preference.Preference.OnPreferenceClickListener;
|
|
|
|
import android.preference.PreferenceActivity;
|
|
|
|
import android.preference.PreferenceCategory;
|
2012-01-13 02:53:42 +00:00
|
|
|
import android.util.Log;
|
2012-01-12 20:46:54 +00:00
|
|
|
import android.widget.Toast;
|
2011-10-28 04:31:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
public class SettingsPreferences
|
|
|
|
extends PreferenceActivity implements OnPreferenceClickListener {
|
|
|
|
|
|
|
|
private CheckBoxPreference prefCBTransProxy = null;
|
|
|
|
private CheckBoxPreference prefcBTransProxyAll = null;
|
|
|
|
private Preference prefTransProxyApps = null;
|
|
|
|
private CheckBoxPreference prefHiddenServices = null;
|
2012-01-12 20:46:54 +00:00
|
|
|
private CheckBoxPreference prefRequestRoot = null;
|
2012-02-24 05:13:27 +00:00
|
|
|
private Preference prefLocale = null;
|
2011-10-28 04:31:45 +00:00
|
|
|
|
|
|
|
private boolean hasRoot = false;
|
|
|
|
|
|
|
|
|
|
|
|
private final static int HIDDEN_SERVICE_PREF_IDX = 6;
|
2012-01-12 20:46:54 +00:00
|
|
|
private final static int TRANSPROXY_GROUP_IDX = 1;
|
2011-10-28 04:31:45 +00:00
|
|
|
|
|
|
|
protected void onCreate(Bundle savedInstanceState)
|
|
|
|
{
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
addPreferencesFromResource(R.xml.preferences);
|
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
|
|
|
|
hasRoot = prefs.getBoolean("has_root",false);
|
|
|
|
|
|
|
|
if (!hasRoot)
|
|
|
|
{
|
|
|
|
hasRoot = prefs.getBoolean("use_whispercore", false);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
|
|
|
|
super.onResume();
|
|
|
|
|
2012-01-12 20:46:54 +00:00
|
|
|
int REQUEST_ROOT_IDX = 1;
|
2012-02-24 05:13:27 +00:00
|
|
|
int SET_LOCALE_IDX = 3;
|
|
|
|
|
2012-01-12 20:46:54 +00:00
|
|
|
int GENERAL_GROUP_IDX = 0;
|
|
|
|
|
|
|
|
prefRequestRoot = ((CheckBoxPreference)((PreferenceCategory)getPreferenceScreen().getPreference(GENERAL_GROUP_IDX)).getPreference(REQUEST_ROOT_IDX));
|
|
|
|
prefRequestRoot.setOnPreferenceClickListener(this);
|
|
|
|
|
2012-02-24 05:13:27 +00:00
|
|
|
prefLocale = (((PreferenceCategory)getPreferenceScreen().getPreference(GENERAL_GROUP_IDX)).getPreference(SET_LOCALE_IDX));
|
|
|
|
prefLocale.setOnPreferenceClickListener(this);
|
|
|
|
|
2012-01-12 20:46:54 +00:00
|
|
|
prefCBTransProxy = ((CheckBoxPreference)((PreferenceCategory)this.getPreferenceScreen().getPreference(TRANSPROXY_GROUP_IDX)).getPreference(0));
|
|
|
|
prefcBTransProxyAll = (CheckBoxPreference)((PreferenceCategory)this.getPreferenceScreen().getPreference(TRANSPROXY_GROUP_IDX)).getPreference(1);
|
|
|
|
prefTransProxyApps = ((PreferenceCategory)this.getPreferenceScreen().getPreference(TRANSPROXY_GROUP_IDX)).getPreference(2);
|
|
|
|
|
2011-10-28 04:31:45 +00:00
|
|
|
|
2012-01-12 20:46:54 +00:00
|
|
|
prefCBTransProxy.setOnPreferenceClickListener(this);
|
|
|
|
prefcBTransProxyAll.setOnPreferenceClickListener(this);
|
|
|
|
prefTransProxyApps.setOnPreferenceClickListener(this);
|
2011-10-28 04:31:45 +00:00
|
|
|
|
|
|
|
if (!hasRoot)
|
|
|
|
{
|
2012-01-12 20:46:54 +00:00
|
|
|
getPreferenceScreen().getPreference(TRANSPROXY_GROUP_IDX).setEnabled(false);
|
2011-10-28 04:31:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (non-Javadoc)
|
|
|
|
* @see android.app.Activity#onStop()
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
protected void onStop() {
|
|
|
|
super.onStop();
|
|
|
|
|
|
|
|
//Log.d(getClass().getName(),"Exiting Preferences");
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean onPreferenceClick(Preference preference) {
|
|
|
|
|
|
|
|
setResult(1010);
|
|
|
|
|
2012-01-12 20:46:54 +00:00
|
|
|
if (preference == prefRequestRoot)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (prefRequestRoot.isChecked())
|
|
|
|
{
|
2012-02-12 03:41:25 +00:00
|
|
|
//boolean canRoot = TorServiceUtils.isRootPossible();
|
|
|
|
boolean canRoot;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
StringBuilder res = new StringBuilder();
|
|
|
|
String[] cmd = {"ls /data/data"}; //only root can do this!
|
|
|
|
int code = TorServiceUtils.doShellCommand(cmd, res, true, true);
|
|
|
|
canRoot = code > -1;
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
//probably not root
|
|
|
|
canRoot = false;
|
|
|
|
}
|
2012-01-12 20:46:54 +00:00
|
|
|
|
|
|
|
getPreferenceScreen().getPreference(TRANSPROXY_GROUP_IDX).setEnabled(canRoot);
|
|
|
|
prefRequestRoot.setChecked(canRoot);
|
|
|
|
|
|
|
|
if (!canRoot)
|
|
|
|
{
|
|
|
|
Toast.makeText(this, R.string.wizard_permissions_no_root_msg, Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (preference == prefTransProxyApps)
|
2011-10-28 04:31:45 +00:00
|
|
|
{
|
|
|
|
startActivity(new Intent(this, AppManager.class));
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (preference == prefHiddenServices)
|
|
|
|
{
|
|
|
|
|
|
|
|
((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());
|
|
|
|
|
2012-02-24 05:13:27 +00:00
|
|
|
}
|
|
|
|
else if (preference == prefLocale)
|
|
|
|
{
|
|
|
|
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
|
|
|
|
Configuration config = getResources().getConfiguration();
|
|
|
|
|
|
|
|
String lang = settings.getString("pref_default_locale", "");
|
2012-02-27 03:48:30 +00:00
|
|
|
|
|
|
|
Locale locale;
|
|
|
|
|
|
|
|
if (lang.equals("xx"))
|
|
|
|
{
|
|
|
|
locale = Locale.getDefault();
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
locale = new Locale(lang);
|
|
|
|
|
2012-02-24 05:13:27 +00:00
|
|
|
Locale.setDefault(locale);
|
|
|
|
config.locale = locale;
|
|
|
|
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
|
|
|
|
|
2011-10-28 04:31:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prefcBTransProxyAll.setEnabled(prefCBTransProxy.isChecked());
|
|
|
|
prefTransProxyApps.setEnabled(prefCBTransProxy.isChecked() && (!prefcBTransProxyAll.isChecked()));
|
|
|
|
|
2012-01-13 02:53:42 +00:00
|
|
|
if (!prefCBTransProxy.isChecked())
|
|
|
|
clearTransProxyState ();
|
|
|
|
|
2011-10-28 04:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-13 02:53:42 +00:00
|
|
|
private void clearTransProxyState ()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
new TorTransProxy().flushIptables(this);
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(TorConstants.TAG,"error flushing iptables",e);
|
|
|
|
}
|
|
|
|
}
|
2011-10-28 04:31:45 +00:00
|
|
|
|
|
|
|
}
|