allow country exit node select to persist

This commit is contained in:
Nathan Freitas 2016-01-07 00:59:34 -05:00
parent b2081789f5
commit 3b41365708
4 changed files with 97 additions and 71 deletions

View File

@ -72,6 +72,7 @@ import java.net.URLEncoder;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
@ -306,38 +307,46 @@ public class OrbotMainActivity extends Activity
}); });
Locale[] locale = Locale.getAvailableLocales();
ArrayList<String> countries = new ArrayList<String>();
countries.add("World (best)");
countries.add("US");
countries.add("DE");
countries.add("CA");
countries.add("FR");
String currentExit = Prefs.getExitNodes();
int selIdx = -1;
ArrayList<String> cList = new ArrayList<String>();
cList.add(0, "WORLD");
for (int i = 0; i < TorServiceConstants.COUNTRY_CODES.length; i++)
{
Locale locale = new Locale("",TorServiceConstants.COUNTRY_CODES[i]);
cList.add(locale.getDisplayCountry());
if (currentExit.contains(TorServiceConstants.COUNTRY_CODES[i]))
selIdx = i+1;
}
spnCountries = (Spinner)findViewById(R.id.spinnerCountry); spnCountries = (Spinner)findViewById(R.id.spinnerCountry);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, countries); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, cList);
spnCountries.setAdapter(adapter); spnCountries.setAdapter(adapter);
if (selIdx != -1)
spnCountries.setSelection(selIdx);
spnCountries.setOnItemSelectedListener(new OnItemSelectedListener() { spnCountries.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override @Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here // your code here
String country = (String)spnCountries.getItemAtPosition(position); String country = null;
if (position == 0) if (position == 0)
country = ""; country = "";
else else
country = '{' + country.toLowerCase() + '}'; country = '{' + TorServiceConstants.COUNTRY_CODES[position-1] + '}';
Intent torService = new Intent(OrbotMainActivity.this, TorService.class); Intent torService = new Intent(OrbotMainActivity.this, TorService.class);
torService.setAction(TorServiceConstants.CMD_SET_EXIT); torService.setAction(TorServiceConstants.CMD_SET_EXIT);
torService.putExtra("exit",country); torService.putExtra("exit",country);
startService(torService); startService(torService);
} }
@Override @Override

View File

@ -3,6 +3,8 @@ package org.torproject.android;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.text.TextUtils;
import org.torproject.android.service.TorServiceUtils; import org.torproject.android.service.TorServiceUtils;
import java.util.Locale; import java.util.Locale;
@ -23,7 +25,8 @@ public class Prefs {
private final static String PREF_TRANSPROXY_REFRESH = "pref_transproxy_refresh"; private final static String PREF_TRANSPROXY_REFRESH = "pref_transproxy_refresh";
private final static String PREF_USE_SYSTEM_IPTABLES = "pref_use_sys_iptables"; private final static String PREF_USE_SYSTEM_IPTABLES = "pref_use_sys_iptables";
private final static String PREF_USE_VPN = "pref_vpn"; private final static String PREF_USE_VPN = "pref_vpn";
private final static String PREF_EXIT_NODES = "pref_exit_nodes";
private static SharedPreferences prefs; private static SharedPreferences prefs;
public static void setContext(Context context) { public static void setContext(Context context) {
@ -118,4 +121,14 @@ public class Prefs {
public static void putStartOnBoot(boolean value) { public static void putStartOnBoot(boolean value) {
putBoolean(PREF_START_ON_BOOT, value); putBoolean(PREF_START_ON_BOOT, value);
} }
public static String getExitNodes ()
{
return prefs.getString(PREF_EXIT_NODES, "");
}
public static void setExitNodes (String exits)
{
putString(PREF_EXIT_NODES,exits);
}
} }

View File

@ -1414,71 +1414,71 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon
if (mCurrentStatus == STATUS_STARTING && TextUtils.equals(status, "BUILT")) if (mCurrentStatus == STATUS_STARTING && TextUtils.equals(status, "BUILT"))
sendCallbackStatus(STATUS_ON); sendCallbackStatus(STATUS_ON);
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("Circuit ("); sb.append("Circuit (");
sb.append((circID)); sb.append((circID));
sb.append(") "); sb.append(") ");
sb.append(status); sb.append(status);
sb.append(": "); sb.append(": ");
StringTokenizer st = new StringTokenizer(path,",");
Node node = null;
while (st.hasMoreTokens())
{
String nodePath = st.nextToken();
node = new Node();
StringTokenizer st = new StringTokenizer(path,","); String[] nodeParts;
Node node = null;
while (st.hasMoreTokens()) if (nodePath.contains("="))
nodeParts = nodePath.split("=");
else
nodeParts = nodePath.split("~");
if (nodeParts.length == 1)
{ {
String nodePath = st.nextToken(); node.id = nodeParts[0].substring(1);
node = new Node(); node.name = node.id;
}
String[] nodeParts; else if (nodeParts.length == 2)
{
if (nodePath.contains("=")) node.id = nodeParts[0].substring(1);
nodeParts = nodePath.split("="); node.name = nodeParts[1];
else
nodeParts = nodePath.split("~");
if (nodeParts.length == 1)
{
node.id = nodeParts[0].substring(1);
node.name = node.id;
}
else if (nodeParts.length == 2)
{
node.id = nodeParts[0].substring(1);
node.name = nodeParts[1];
}
node.status = status;
sb.append(node.name);
if (st.hasMoreTokens())
sb.append (" > ");
} }
if (Prefs.useDebugLogging()) node.status = status;
debug(sb.toString());
else if(status.equals("BUILT")) sb.append(node.name);
logNotice(sb.toString());
else if (status.equals("CLOSED")) if (st.hasMoreTokens())
logNotice(sb.toString()); sb.append (" > ");
}
if (Prefs.useDebugLogging())
debug(sb.toString());
else if(status.equals("BUILT"))
logNotice(sb.toString());
else if (status.equals("CLOSED"))
logNotice(sb.toString());
if (Prefs.expandedNotifications()) if (Prefs.expandedNotifications())
{ {
//get IP from last nodename //get IP from last nodename
if(status.equals("BUILT")){ if(status.equals("BUILT")){
if (node.ipAddress == null)
mExecutor.execute(new ExternalIPFetcher(node));
hmBuiltNodes.put(node.id, node);
}
if (status.equals("CLOSED")) if (node.ipAddress == null)
{ mExecutor.execute(new ExternalIPFetcher(node));
hmBuiltNodes.remove(node.id);
hmBuiltNodes.put(node.id, node);
}
} }
if (status.equals("CLOSED"))
{
hmBuiltNodes.remove(node.id);
}
}
} }

View File

@ -148,5 +148,9 @@ public interface TorServiceConstants {
//DNS daemon for TCP DNS over TOr //DNS daemon for TCP DNS over TOr
public final static String PDNSD_ASSET_KEY = "pdnsd"; public final static String PDNSD_ASSET_KEY = "pdnsd";
//EXIT COUNTRY CODES
public final static String[] COUNTRY_CODES = {"DE","AT","SE","CH","IS","CA","US","FR","BG","AU","BR","CZ","DK","FI","GB","HU","NL","JP","HK","RO","RU","SG","SK","CN"};
} }