final fixes for AppsVPN mode on boot and Android 4.x support
This commit is contained in:
		
							parent
							
								
									f98f7b4a33
								
							
						
					
					
						commit
						dc06eedcfd
					
				| 
						 | 
				
			
			@ -73,7 +73,12 @@
 | 
			
		|||
			android:finishOnTaskLaunch="true"
 | 
			
		||||
			
 | 
			
		||||
			/> 
 | 
			
		||||
			      	
 | 
			
		||||
		
 | 
			
		||||
      	<activity
 | 
			
		||||
			android:name=".vpn.VPNEnableActivity" android:label="@string/app_name" android:exported="false"
 | 
			
		||||
			android:theme="@android:style/Theme.NoDisplay"			
 | 
			
		||||
			/> 
 | 
			
		||||
		
 | 
			
		||||
			      	
 | 
			
		||||
      	<activity android:name="org.torproject.android.ui.PromoAppsActivity" android:exported="false"/>
 | 
			
		||||
      	
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 ()
 | 
			
		||||
    {    	
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
    
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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);
 | 
			
		||||
		}
 | 
			
		||||
    
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue