From dc06eedcfdffb7bc12a8198fc2f9b4b21f9e1269 Mon Sep 17 00:00:00 2001 From: Nathan Freitas Date: Fri, 26 Jun 2015 09:52:42 -0400 Subject: [PATCH] final fixes for AppsVPN mode on boot and Android 4.x support --- AndroidManifest.xml | 7 +- external/appcompat/project.properties | 2 +- .../torproject/android/OrbotMainActivity.java | 47 +----- .../android/service/OnBootReceiver.java | 28 ++-- .../android/service/TorService.java | 1 - .../android/vpn/VPNEnableActivity.java | 137 ++++++++++++++++++ 6 files changed, 164 insertions(+), 58 deletions(-) create mode 100644 src/org/torproject/android/vpn/VPNEnableActivity.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 328d57e5..3b5ba412 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -73,7 +73,12 @@ android:finishOnTaskLaunch="true" /> - + + + diff --git a/external/appcompat/project.properties b/external/appcompat/project.properties index 93c8c3c0..362a0a30 100644 --- a/external/appcompat/project.properties +++ b/external/appcompat/project.properties @@ -11,5 +11,5 @@ #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt # Project target. -target=android-21 +target=android-22 android.library=true diff --git a/src/org/torproject/android/OrbotMainActivity.java b/src/org/torproject/android/OrbotMainActivity.java index 7ff60684..c88f1298 100644 --- a/src/org/torproject/android/OrbotMainActivity.java +++ b/src/org/torproject/android/OrbotMainActivity.java @@ -59,6 +59,7 @@ import org.torproject.android.settings.SettingsPreferences; import org.torproject.android.ui.ImageProgressView; import org.torproject.android.ui.PromoAppsActivity; import org.torproject.android.ui.Rotate3dAnimation; +import org.torproject.android.vpn.VPNEnableActivity; import java.io.IOException; import java.io.UnsupportedEncodingException; @@ -252,10 +253,11 @@ public class OrbotMainActivity extends Activity boolean useVPN = Prefs.useVpn(); mBtnVPN.setChecked(useVPN); + /** if (useVPN) { - startVpnService (); - } + startActivity(new Intent(OrbotMainActivity.this,VPNEnableActivity.class)); + }*/ mBtnVPN.setOnClickListener(new View.OnClickListener () { @@ -264,7 +266,7 @@ public class OrbotMainActivity extends Activity public void onClick(View v) { if (mBtnVPN.isChecked()) - promptStartVpnService(); + startActivity(new Intent(OrbotMainActivity.this,VPNEnableActivity.class)); else stopVpnService(); @@ -943,45 +945,12 @@ public class OrbotMainActivity extends Activity sendIntentToService(TorServiceConstants.CMD_SIGNAL_HUP); } - public void promptStartVpnService () - { - LayoutInflater li = LayoutInflater.from(this); - View view = li.inflate(R.layout.layout_diag, null); - - TextView versionName = (TextView)view.findViewById(R.id.diaglog); - versionName.setText(R.string.you_can_enable_all_apps_on_your_device_to_run_through_the_tor_network_using_the_vpn_feature_of_android_); - - new AlertDialog.Builder(this) - .setTitle(R.string.apps_mode) - .setView(view) - .setPositiveButton(R.string.activate, new Dialog.OnClickListener () - { - - @Override - public void onClick(DialogInterface dialog, int which) { - Prefs.putUseVpn(true); - startVpnService(); - } - - - }) - .setNegativeButton(R.string.btn_cancel, new Dialog.OnClickListener () - { - - @Override - public void onClick(DialogInterface dialog, int which) { - - mBtnVPN.setChecked(false); - } - - }) - .show(); - } + /** @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) public void startVpnService () { - Intent intent = VpnService.prepare(this); + Intent intent = VpnService.prepare(getApplicationContext()); if (intent != null) { startActivityForResult(intent,REQUEST_VPN); } @@ -989,7 +958,7 @@ public class OrbotMainActivity extends Activity { sendIntentToService(TorServiceConstants.CMD_VPN); } - } + }*/ public void stopVpnService () { diff --git a/src/org/torproject/android/service/OnBootReceiver.java b/src/org/torproject/android/service/OnBootReceiver.java index 2429c4d6..36891e86 100644 --- a/src/org/torproject/android/service/OnBootReceiver.java +++ b/src/org/torproject/android/service/OnBootReceiver.java @@ -1,13 +1,12 @@ package org.torproject.android.service; -import android.annotation.SuppressLint; +import org.torproject.android.Prefs; +import org.torproject.android.vpn.VPNEnableActivity; + import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; -import android.net.VpnService; - -import org.torproject.android.Prefs; public class OnBootReceiver extends BroadcastReceiver { @@ -15,24 +14,21 @@ public class OnBootReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { Prefs.setContext(context); if (Prefs.startOnBoot()) - { - startService(TorServiceConstants.ACTION_START, context); + { if (Prefs.useVpn()) - startVpnService(context); + startVpnService(context); //VPN will start Tor once it is done + else + startService(TorServiceConstants.ACTION_START, context); + } } - @SuppressLint("NewApi") - public void startVpnService (Context context) + public void startVpnService (final Context context) { - Intent intent = VpnService.prepare(context); - - if (intent != null) { - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - context.startActivity(intent); - } - + Intent intent = new Intent(context,VPNEnableActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(intent); } private void startService (String action, Context context) diff --git a/src/org/torproject/android/service/TorService.java b/src/org/torproject/android/service/TorService.java index a515333b..2ee02bdb 100644 --- a/src/org/torproject/android/service/TorService.java +++ b/src/org/torproject/android/service/TorService.java @@ -93,7 +93,6 @@ public class TorService extends Service implements TorServiceConstants, OrbotCon private Socket torConnSocket = null; private int mLastProcessId = -1; - private int mPortHTTP = HTTP_PROXY_PORT_DEFAULT; private int mPortSOCKS = SOCKS_PROXY_PORT_DEFAULT; diff --git a/src/org/torproject/android/vpn/VPNEnableActivity.java b/src/org/torproject/android/vpn/VPNEnableActivity.java new file mode 100644 index 00000000..273a1cdf --- /dev/null +++ b/src/org/torproject/android/vpn/VPNEnableActivity.java @@ -0,0 +1,137 @@ +package org.torproject.android.vpn; + +import org.torproject.android.Prefs; +import org.torproject.android.R; +import org.torproject.android.service.TorService; +import org.torproject.android.service.TorServiceConstants; + +import android.annotation.TargetApi; +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Dialog; +import android.content.DialogInterface; +import android.content.Intent; +import android.net.VpnService; +import android.os.Build; +import android.os.Bundle; +import android.os.Handler; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.TextView; + +/* + * To combat background service being stopped/swiped + */ +public class VPNEnableActivity extends Activity { + + private final static int REQUEST_VPN = 7777; + private Intent intent = null; + + @Override + public void onCreate( Bundle icicle ) { + super.onCreate( icicle ); + + Log.d("VPNEnableActivity","prompting user to start Orbot VPN"); + + intent = VpnService.prepare(this); + + if (intent != null) + promptStartVpnService(); + else + startVpnService (); + + } + + public void promptStartVpnService () + { + LayoutInflater li = LayoutInflater.from(this); + View view = li.inflate(R.layout.layout_diag, null); + + TextView versionName = (TextView)view.findViewById(R.id.diaglog); + versionName.setText(R.string.you_can_enable_all_apps_on_your_device_to_run_through_the_tor_network_using_the_vpn_feature_of_android_); + + new AlertDialog.Builder(this) + .setTitle(getString(R.string.app_name) + ' ' + getString(R.string.apps_mode)) + .setView(view) + .setPositiveButton(R.string.activate, new Dialog.OnClickListener () + { + + @Override + public void onClick(DialogInterface dialog, int which) { + Prefs.putUseVpn(true); + + startVpnService(); + + } + + + }) + .setNegativeButton(R.string.btn_cancel, new Dialog.OnClickListener () + { + + @Override + public void onClick(DialogInterface dialog, int which) { + + finish(); + } + + }) + .show(); + } + + private void startVpnService () + { + if (intent == null) + { + Log.d("VPNEnableActivity","VPN enabled, starting Tor..."); + sendIntentToService(TorServiceConstants.CMD_VPN); + + Handler h = new Handler(); + h.postDelayed(new Runnable () { + + public void run () + { + sendIntentToService(TorServiceConstants.ACTION_START); + finish(); + } + }, 1000); + + } + else + { + Log.w("VPNEnableActivity","prompt for VPN"); + startActivityForResult(intent,REQUEST_VPN); + + } + + } + + @Override + protected void onActivityResult(int request, int response, Intent data) { + super.onActivityResult(request, response, data); + + if (request == REQUEST_VPN && response == RESULT_OK) + { + sendIntentToService(TorServiceConstants.CMD_VPN); + Handler h = new Handler(); + h.postDelayed(new Runnable () { + + public void run () + { + sendIntentToService(TorServiceConstants.ACTION_START); + finish(); + } + }, 1000); + + } + } + + + private void sendIntentToService(String action) { + Intent torService = new Intent(this, TorService.class); + torService.setAction(action); + startService(torService); + } + +} \ No newline at end of file