diff --git a/src/org/torproject/android/service/StartTorReceiver.java b/src/org/torproject/android/service/StartTorReceiver.java index c236484b..89df5964 100644 --- a/src/org/torproject/android/service/StartTorReceiver.java +++ b/src/org/torproject/android/service/StartTorReceiver.java @@ -11,10 +11,14 @@ public class StartTorReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { /* sanitize the Intent before forwarding it to TorService */ - if (TextUtils.equals(intent.getAction(), TorServiceConstants.ACTION_START)) { - Intent startTorService = new Intent(context, TorService.class); - startTorService.setAction(intent.getAction()); - context.startService(startTorService); + String action = intent.getAction(); + if (TextUtils.equals(action, TorServiceConstants.ACTION_START)) { + Intent startTorIntent = new Intent(context, TorService.class); + startTorIntent.setAction(action); + String packageName = intent.getStringExtra(TorServiceConstants.EXTRA_PACKAGE_NAME); + if (packageName != null) + startTorIntent.putExtra(TorServiceConstants.EXTRA_PACKAGE_NAME, packageName); + context.startService(startTorIntent); } } } diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java index efde5c66..ccfc4c84 100644 --- a/src/org/torproject/android/service/TorService.java +++ b/src/org/torproject/android/service/TorService.java @@ -28,6 +28,7 @@ import android.os.RemoteException; import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat.Builder; import android.support.v4.content.LocalBroadcastManager; +import android.text.TextUtils; import android.util.Log; import android.widget.RemoteViews; @@ -342,6 +343,7 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon if (action != null) { if (action.equals(ACTION_START)) { + replyWithStatus(mIntent); startTor(); // stopTor() is called when the Service is destroyed } else if (action.equals(CMD_SIGNAL_HUP)) { @@ -748,6 +750,25 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon return success; } + /** + * Send Orbot's status in reply to an + * {@link TorServiceConstants#ACTION_START} {@link Intent}, targeted only to + * the app that sent the initial request. + */ + private void replyWithStatus(Intent startRequest) { + String packageName = startRequest.getStringExtra(EXTRA_PACKAGE_NAME); + if (TextUtils.isEmpty(packageName)) { + return; + } + Intent reply = new Intent(ACTION_STATUS); + reply.putExtra(EXTRA_STATUS, mCurrentStatus); + reply.setPackage(packageName); + sendBroadcast(reply); + } + + /** + * The entire process for starting tor and related services is run from this method. + */ private void startTor() { if (mCurrentStatus == STATUS_STARTING || mCurrentStatus == STATUS_STOPPING) { // these states should probably be handled better diff --git a/src/org/torproject/android/service/TorServiceConstants.java b/src/org/torproject/android/service/TorServiceConstants.java index 96715b6e..7921ddcc 100644 --- a/src/org/torproject/android/service/TorServiceConstants.java +++ b/src/org/torproject/android/service/TorServiceConstants.java @@ -1,7 +1,10 @@ /* Copyright (c) 2009, Nathan Freitas, Orbot / The Guardian Project - http://openideals.com/guardian */ /* See LICENSE for licensing information */ + package org.torproject.android.service; +import android.content.Intent; + public interface TorServiceConstants { public final static String TOR_APP_USERNAME = "org.torproject.android"; @@ -67,19 +70,42 @@ public interface TorServiceConstants { //control port public final static String TOR_CONTROL_PORT_MSG_BOOTSTRAP_DONE = "Bootstrapped 100%"; + /** + * A request to Orbot to transparently start Tor services + */ public final static String ACTION_START = "org.torproject.android.intent.action.START"; + /** + * {@link Intent} send by Orbot with {@code ON/OFF/STARTING/STOPPING} status + */ public final static String ACTION_STATUS = "org.torproject.android.intent.action.STATUS"; + /** + * {@code String} that contains a status constant: {@link #STATUS_ON}, + * {@link #STATUS_OFF}, {@link #STATUS_STARTING}, or + * {@link #STATUS_STOPPING} + */ public final static String EXTRA_STATUS = "org.torproject.android.intent.extra.STATUS"; + /** + * A {@link String} {@code packageName} for Orbot to direct its status reply + * to, used in {@link #ACTION_START} {@link Intent}s sent to Orbot + */ + public final static String EXTRA_PACKAGE_NAME = "org.torproject.android.intent.extra.PACKAGE_NAME"; public final static String LOCAL_ACTION_LOG = "log"; public final static String LOCAL_ACTION_BANDWIDTH = "bandwidth"; public final static String LOCAL_EXTRA_LOG = "log"; + /** + * All tor-related services and daemons are stopped + */ public final static String STATUS_OFF = "OFF"; + /** + * All tor-related services and daemons have completed starting + */ public final static String STATUS_ON = "ON"; public final static String STATUS_STARTING = "STARTING"; public final static String STATUS_STOPPING = "STOPPING"; + // actions for internal command Intents public static final String CMD_SIGNAL_HUP = "signal_hup"; public static final String CMD_FLUSH = "flush"; public static final String CMD_NEWNYM = "newnym";