fix handle intent for hidden service API request

This commit is contained in:
Nathan Freitas 2015-05-11 15:41:43 -04:00
parent 84c6645220
commit fa533e9d52
1 changed files with 69 additions and 42 deletions

View File

@ -499,45 +499,67 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
} }
private void enableHiddenServicePort (int hsPort) private void enableHiddenServicePort (int hsPort) throws RemoteException, InterruptedException
{ {
Editor pEdit = mPrefs.edit(); Editor pEdit = mPrefs.edit();
String hsPortString = mPrefs.getString("pref_hs_ports", ""); String hsPortString = mPrefs.getString("pref_hs_ports", "");
if (hsPortString.length() > 0 && hsPortString.indexOf(hsPort+"")==-1)
hsPortString += ',' + hsPort;
else
hsPortString = hsPort + "";
pEdit.putString("pref_hs_ports", hsPortString);
pEdit.putBoolean("pref_hs_enable", true);
pEdit.commit();
String onionHostname = mPrefs.getString("pref_hs_hostname",""); String onionHostname = mPrefs.getString("pref_hs_hostname","");
while (onionHostname.length() == 0) if (hsPortString.indexOf(hsPort+"")==-1)
{ {
//we need to stop and start Tor if (hsPortString.length() > 0 && hsPortString.indexOf(hsPort+"")==-1)
try { hsPortString += ',' + hsPort;
stopTor(); else
hsPortString = hsPort + "";
Thread.sleep(3000); //wait three seconds pEdit.putString("pref_hs_ports", hsPortString);
pEdit.putBoolean("pref_hs_enable", true);
startTor(); pEdit.commit();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
onionHostname = mPrefs.getString("pref_hs_hostname","");
} }
Intent nResult = new Intent(); if (onionHostname == null || onionHostname.length() == 0)
nResult.putExtra("hs_host", onionHostname); {
setResult(RESULT_OK, nResult); stopTor();
startTor();
new Thread () {
public void run ()
{
String onionHostname = mPrefs.getString("pref_hs_hostname","");
while (onionHostname.length() == 0)
{
//we need to stop and start Tor
try {
Thread.sleep(3000); //wait three seconds
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
onionHostname = mPrefs.getString("pref_hs_hostname","");
}
Intent nResult = new Intent();
nResult.putExtra("hs_host", onionHostname);
setResult(RESULT_OK, nResult);
finish();
}
}.start();
}
else
{
Intent nResult = new Intent();
nResult.putExtra("hs_host", onionHostname);
setResult(RESULT_OK, nResult);
finish();
}
} }
@ -557,6 +579,7 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
if (action.equals("org.torproject.android.REQUEST_HS_PORT")) if (action.equals("org.torproject.android.REQUEST_HS_PORT"))
{ {
final int hiddenServicePortRequest = getIntent().getIntExtra("hs_port", -1);
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@ -564,11 +587,16 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
switch (which){ switch (which){
case DialogInterface.BUTTON_POSITIVE: case DialogInterface.BUTTON_POSITIVE:
int hsPort = getIntent().getIntExtra("hs_port", -1); try {
enableHiddenServicePort (hiddenServicePortRequest);
enableHiddenServicePort (hsPort); } catch (RemoteException e) {
// TODO Auto-generated catch block
finish(); e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break; break;
@ -581,14 +609,13 @@ public class OrbotMainActivity extends Activity implements OrbotConstants, OnLon
} }
}; };
int hsPort = getIntent().getIntExtra("hs_port", -1);
String requestMsg = getString(R.string.hidden_service_request, hsPort); String requestMsg = getString(R.string.hidden_service_request, hiddenServicePortRequest);
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(requestMsg).setPositiveButton("Allow", dialogClickListener) builder.setMessage(requestMsg).setPositiveButton("Allow", dialogClickListener)
.setNegativeButton("Deny", dialogClickListener).show(); .setNegativeButton("Deny", dialogClickListener).show();
return; //don't null the setIntent() as we need it later
} }
else if (action.equals("org.torproject.android.START_TOR")) else if (action.equals("org.torproject.android.START_TOR"))
{ {