commit 6f5d48dff413b68daca79ca1a4814fa6a8194d61 Author: Hans-Christoph Steiner hans@eds.org Date: Mon Oct 7 22:26:06 2019 +0200
send TorVpnService Intents directly, do not go through TorService
This follows the standard pattern as documented in the Android docs for starting and stopping a Service with Intents. --- .../torproject/android/mini/MiniMainActivity.java | 13 ++++------- .../android/mini/vpn/VPNEnableActivity.java | 6 +++-- .../org/torproject/android/OrbotMainActivity.java | 12 ++++------ .../torproject/android/ui/VPNEnableActivity.java | 5 ++-- .../org/torproject/android/service/TorService.java | 26 --------------------- .../android/service/TorServiceConstants.java | 2 -- .../android/service/vpn/TorVpnService.java | 27 ++++++++++++++++++++-- 7 files changed, 41 insertions(+), 50 deletions(-)
diff --git a/app-mini/src/main/java/org/torproject/android/mini/MiniMainActivity.java b/app-mini/src/main/java/org/torproject/android/mini/MiniMainActivity.java index e8612ed5..52148113 100644 --- a/app-mini/src/main/java/org/torproject/android/mini/MiniMainActivity.java +++ b/app-mini/src/main/java/org/torproject/android/mini/MiniMainActivity.java @@ -58,6 +58,7 @@ import org.torproject.android.mini.ui.Rotate3dAnimation; import org.torproject.android.mini.ui.onboarding.OnboardingActivity; import org.torproject.android.mini.vpn.VPNEnableActivity; import org.torproject.android.service.OrbotConstants; +import org.torproject.android.service.vpn.TorVpnService; import org.torproject.android.service.TorService; import org.torproject.android.service.TorServiceConstants; import org.torproject.android.service.util.Prefs; @@ -466,7 +467,7 @@ public class MiniMainActivity extends AppCompatActivity
private void refreshVPNApps () { - stopVpnService(); + TorVpnService.start(this); startActivity(new Intent(MiniMainActivity.this, VPNEnableActivity.class)); }
@@ -482,7 +483,7 @@ public class MiniMainActivity extends AppCompatActivity if (enable) { startActivityForResult(new Intent(MiniMainActivity.this, VPNEnableActivity.class), REQUEST_VPN); } else { - stopVpnService(); + TorVpnService.start(this); stopTor(); } } @@ -610,7 +611,8 @@ public class MiniMainActivity extends AppCompatActivity else if (request == REQUEST_VPN) { if (response == RESULT_OK) { - sendIntentToService(TorServiceConstants.CMD_VPN); + TorVpnService.start(this); + } else if (response == VPNEnableActivity.ACTIVITY_RESULT_VPN_DENIED) { @@ -710,11 +712,6 @@ public class MiniMainActivity extends AppCompatActivity private void requestTorRereadConfig() { sendIntentToService(TorServiceConstants.CMD_SIGNAL_HUP); } - - public void stopVpnService () - { - sendIntentToService(TorServiceConstants.CMD_VPN_CLEAR); - }
@Override protected void onResume() { diff --git a/app-mini/src/main/java/org/torproject/android/mini/vpn/VPNEnableActivity.java b/app-mini/src/main/java/org/torproject/android/mini/vpn/VPNEnableActivity.java index c252852b..bb263372 100644 --- a/app-mini/src/main/java/org/torproject/android/mini/vpn/VPNEnableActivity.java +++ b/app-mini/src/main/java/org/torproject/android/mini/vpn/VPNEnableActivity.java @@ -11,6 +11,7 @@ import android.view.Window; import org.torproject.android.service.TorService; import org.torproject.android.service.TorServiceConstants; import org.torproject.android.service.util.Prefs; +import org.torproject.android.service.vpn.TorVpnService;
/** * To combat background service being stopped/swiped @@ -59,7 +60,7 @@ public class VPNEnableActivity extends AppCompatActivity { Prefs.putUseVpn(true);
Log.d("VPNEnableActivity","VPN enabled, starting Tor..."); - sendIntentToService(TorServiceConstants.CMD_VPN); + TorVpnService.start(this);
Handler h = new Handler(); h.postDelayed(new Runnable () { @@ -89,7 +90,8 @@ public class VPNEnableActivity extends AppCompatActivity { super.onActivityResult(request, response, data); if (request == REQUEST_VPN && response == RESULT_OK) { - sendIntentToService(TorServiceConstants.CMD_VPN); + TorVpnService.start(this); + h.postDelayed(new Runnable () { @Override public void run () { diff --git a/app/src/main/java/org/torproject/android/OrbotMainActivity.java b/app/src/main/java/org/torproject/android/OrbotMainActivity.java index 947f3a76..4ff3e850 100644 --- a/app/src/main/java/org/torproject/android/OrbotMainActivity.java +++ b/app/src/main/java/org/torproject/android/OrbotMainActivity.java @@ -57,6 +57,7 @@ import org.torproject.android.service.OrbotConstants; import org.torproject.android.service.TorService; import org.torproject.android.service.TorServiceConstants; import org.torproject.android.service.util.Prefs; +import org.torproject.android.service.vpn.TorVpnService; import org.torproject.android.service.vpn.VpnConstants; import org.torproject.android.service.vpn.VpnPrefs; import org.torproject.android.settings.Languages; @@ -600,7 +601,7 @@ public class OrbotMainActivity extends AppCompatActivity
private void refreshVPNApps () { - stopVpnService(); + TorVpnService.stop(this); startActivity(new Intent(OrbotMainActivity.this, VPNEnableActivity.class)); }
@@ -611,7 +612,7 @@ public class OrbotMainActivity extends AppCompatActivity if (enable) { startActivityForResult(new Intent(OrbotMainActivity.this, VPNEnableActivity.class), REQUEST_VPN); } else - stopVpnService(); + TorVpnService.stop(this);
addAppShortcuts(); } @@ -903,7 +904,7 @@ public class OrbotMainActivity extends AppCompatActivity else if (request == REQUEST_VPN) { if (response == RESULT_OK) { - sendIntentToService(TorServiceConstants.CMD_VPN); + TorVpnService.start(this); } else if (response == VPNEnableActivity.ACTIVITY_RESULT_VPN_DENIED) { @@ -997,11 +998,6 @@ public class OrbotMainActivity extends AppCompatActivity private void requestTorRereadConfig() { sendIntentToService(TorServiceConstants.CMD_SIGNAL_HUP); } - - public void stopVpnService () - { - sendIntentToService(TorServiceConstants.CMD_VPN_CLEAR); - }
@Override protected void onResume() { diff --git a/app/src/main/java/org/torproject/android/ui/VPNEnableActivity.java b/app/src/main/java/org/torproject/android/ui/VPNEnableActivity.java index 09bdf84a..e910be32 100644 --- a/app/src/main/java/org/torproject/android/ui/VPNEnableActivity.java +++ b/app/src/main/java/org/torproject/android/ui/VPNEnableActivity.java @@ -11,6 +11,7 @@ import android.view.Window; import org.torproject.android.service.TorService; import org.torproject.android.service.TorServiceConstants; import org.torproject.android.service.util.Prefs; +import org.torproject.android.service.vpn.TorVpnService;
/** * To combat background service being stopped/swiped @@ -59,7 +60,7 @@ public class VPNEnableActivity extends AppCompatActivity { Prefs.putUseVpn(true);
Log.d("VPNEnableActivity","VPN enabled, starting Tor..."); - sendIntentToService(TorServiceConstants.CMD_VPN); + TorVpnService.start(this);
Handler h = new Handler(); h.postDelayed(new Runnable () { @@ -89,7 +90,7 @@ public class VPNEnableActivity extends AppCompatActivity { super.onActivityResult(request, response, data); if (request == REQUEST_VPN && response == RESULT_OK) { - sendIntentToService(TorServiceConstants.CMD_VPN); + TorVpnService.start(this); h.postDelayed(new Runnable () { @Override public void run () { diff --git a/orbotservice/src/main/java/org/torproject/android/service/TorService.java b/orbotservice/src/main/java/org/torproject/android/service/TorService.java index f8137632..08c15abf 100644 --- a/orbotservice/src/main/java/org/torproject/android/service/TorService.java +++ b/orbotservice/src/main/java/org/torproject/android/service/TorService.java @@ -50,7 +50,6 @@ import org.torproject.android.service.util.Prefs; import org.torproject.android.service.util.TorServiceUtils; import org.torproject.android.service.util.Utils; import org.torproject.android.service.vpn.OrbotVpnManager; -import org.torproject.android.service.vpn.TorVpnService; import org.torproject.android.service.vpn.VpnPrefs;
import java.io.BufferedReader; @@ -381,10 +380,6 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon requestTorRereadConfig(); } else if (action.equals(CMD_NEWNYM)) { newIdentity(); - } else if (action.equals(CMD_VPN)) { - startVPNService(); - } else if (action.equals(CMD_VPN_CLEAR)) { - clearVpnProxy(); } else if (action.equals(CMD_SET_EXIT)) { setExitNode(mIntent.getStringExtra("exit")); @@ -1961,27 +1956,6 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon return NOTIFY_ID; }
- private void startVPNService () - { - Intent intentVpn = new Intent(this, TorVpnService.class); - intentVpn.setAction("start"); - startService(intentVpn); - - } - - - public void clearVpnProxy () - { - debug ("clearing VPN Proxy"); - Prefs.putUseVpn(false); - - Intent intentVpn = new Intent(this,TorVpnService.class); - intentVpn.setAction("stop"); - startService(intentVpn); - - } - - // for bridge loading from the assets default bridges.txt file class Bridge { diff --git a/orbotservice/src/main/java/org/torproject/android/service/TorServiceConstants.java b/orbotservice/src/main/java/org/torproject/android/service/TorServiceConstants.java index b805cd6a..ffcdefab 100644 --- a/orbotservice/src/main/java/org/torproject/android/service/TorServiceConstants.java +++ b/orbotservice/src/main/java/org/torproject/android/service/TorServiceConstants.java @@ -96,8 +96,6 @@ public interface TorServiceConstants { String CMD_STATUS = "status"; String CMD_FLUSH = "flush"; String CMD_NEWNYM = "newnym"; - String CMD_VPN = "vpn"; - String CMD_VPN_CLEAR = "vpnclear"; String CMD_UPDATE_TRANS_PROXY = "update"; String CMD_SET_EXIT = "setexit";
diff --git a/orbotservice/src/main/java/org/torproject/android/service/vpn/TorVpnService.java b/orbotservice/src/main/java/org/torproject/android/service/vpn/TorVpnService.java index ceae17e2..13ff2c49 100644 --- a/orbotservice/src/main/java/org/torproject/android/service/vpn/TorVpnService.java +++ b/orbotservice/src/main/java/org/torproject/android/service/vpn/TorVpnService.java @@ -7,8 +7,10 @@ import android.content.Intent; import android.content.IntentFilter; import android.net.VpnService; import android.support.v4.content.LocalBroadcastManager; +import android.util.Log; import org.torproject.android.service.TorService; import org.torproject.android.service.TorServiceConstants; +import org.torproject.android.service.util.Prefs;
import java.io.IOException; import java.util.concurrent.TimeoutException; @@ -17,6 +19,24 @@ import java.util.concurrent.TimeoutException; * Created by n8fr8 on 9/26/16. */ public class TorVpnService extends VpnService { + + public static final String TAG = "TorVpnService"; + + private static final String ACTION_START = "start"; + private static final String ACTION_STOP = "stop"; + + public static void start(Context context) { + Intent intent = new Intent(context, TorVpnService.class); + intent.setAction(ACTION_START); + context.startService(intent); + } + + public static void stop(Context context) { + Intent intent = new Intent(context, TorVpnService.class); + intent.setAction(ACTION_STOP); + context.startService(intent); + } + OrbotVpnManager mVpnManager;
@Override @@ -38,14 +58,17 @@ public class TorVpnService extends VpnService { */ public int onStartCommand(Intent intent, int flags, int startId) {
- if (intent.getAction().equals("start")) + if (ACTION_START.equals(intent.getAction())) { LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this); lbm.registerReceiver(mLocalBroadcastReceiver, new IntentFilter(TorServiceConstants.LOCAL_ACTION_PORTS)); } - else if (intent.getAction().equals("stop")) + else if (ACTION_STOP.equals(intent.getAction())) { + Log.d(TAG, "clearing VPN Proxy"); + Prefs.putUseVpn(false); + LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this); lbm.unregisterReceiver(mLocalBroadcastReceiver); }