Java Code Examples for android.net.VpnService#prepare()

The following examples show how to use android.net.VpnService#prepare() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Shadowsocks.java    From ShadowsocksRR with Apache License 2.0 6 votes vote down vote up
private void prepareStartService() {
    if (app.isNatEnabled()) {
        serviceLoad();
    } else {
        Intent intent = VpnService.prepare(mServiceBoundContext);
        if (intent != null) {
            startActivityForResult(intent, REQUEST_CONNECT);
        } else {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    onActivityResult(REQUEST_CONNECT, RESULT_OK, null);
                }
            });
        }
    }
}
 
Example 2
Source File: ShadowsocksRunnerActivity.java    From ShadowsocksRR with Apache License 2.0 6 votes vote down vote up
private void startBackgroundService() {
    if (app.isNatEnabled()) {
        try {
            mServiceBoundContext.bgService.use(app.profileId());
            finish();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    } else {
        Intent intent = VpnService.prepare(ShadowsocksRunnerActivity.this);
        if (intent != null) {
            startActivityForResult(intent, REQUEST_CONNECT);
        } else {
            onActivityResult(REQUEST_CONNECT, Activity.RESULT_OK, null);
        }
    }
}
 
Example 3
Source File: VPNActivity.java    From SimpleOpenVpn-Android with Apache License 2.0 6 votes vote down vote up
/**
 * 开始连接
 */
public void connectVpn() {
    Intent intent = VpnService.prepare(this);
    if (intent != null) {
        VpnStatus.updateStateString("USER_VPN_PERMISSION", "", R.string.state_user_vpn_permission,
                LEVEL_WAITING_FOR_USER_INPUT);
        // Start the query
        try {
            startActivityForResult(intent, START_VPN_PROFILE);
        } catch (ActivityNotFoundException ane) {
            // Shame on you Sony! At least one user reported that
            // an official Sony Xperia Arc S image triggers this exception
            VpnStatus.logError(R.string.no_vpn_support_image);
        }
    } else {
        onActivityResult(START_VPN_PROFILE, Activity.RESULT_OK, null);
    }
}
 
Example 4
Source File: Shadowsocks.java    From Maying with Apache License 2.0 6 votes vote down vote up
private void prepareStartService() {
    if (ShadowsocksApplication.app.isNatEnabled()) {
        serviceLoad();
    } else {
        Intent intent = VpnService.prepare(mServiceBoundContext);
        if (intent != null) {
            startActivityForResult(intent, REQUEST_CONNECT);
        } else {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    onActivityResult(REQUEST_CONNECT, RESULT_OK, null);
                }
            });
        }
    }
}
 
Example 5
Source File: ModulesStateLoop.java    From InviZible with GNU General Public License v3.0 6 votes vote down vote up
private void startVPNService() {

        //Start VPN service if it is not started by modules presenters

        final Intent prepareIntent = VpnService.prepare(modulesService);

        if (handler != null && prepareIntent == null) {
            handler.postDelayed(() -> {
                if (modulesService != null && modulesStatus != null && sharedPreferences != null
                        && !sharedPreferences.getBoolean("VPNServiceEnabled", false)
                        && (modulesStatus.getDnsCryptState() == RUNNING || modulesStatus.getTorState() == RUNNING)) {
                    sharedPreferences.edit().putBoolean("VPNServiceEnabled", true).apply();
                    ServiceVPNHelper.start("ModulesStateLoop start VPN service", modulesService);
                }
            }, 10000);
        }
    }
 
Example 6
Source File: IntraTileService.java    From Intra with Apache License 2.0 6 votes vote down vote up
@Override
public void onClick() {
    VpnState vpnState = VpnController.getInstance().getState(this);

    if (vpnState.activationRequested) {
        VpnController.getInstance().stop(this);
    } else {
        if (VpnService.prepare(this) == null) {
            // Start VPN service when VPN permission has been granted.
            VpnController.getInstance().start(this);
        } else {
            // Open Main activity when VPN permission has not been granted.
            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivityAndCollapse(intent);
        }
    }
}
 
Example 7
Source File: Daedalus.java    From Daedalus with GNU General Public License v3.0 5 votes vote down vote up
public static boolean prepareAndActivateService(Context context) {
    Intent intent = VpnService.prepare(context);
    if (intent != null) {
        return false;
    } else {
        activateService(context);
        return true;
    }
}
 
Example 8
Source File: ExternalOpenVPNService.java    From android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Intent prepareVPNService() throws RemoteException {
    checkOpenVPNPermission();

    if (VpnService.prepare(ExternalOpenVPNService.this) == null)
        return null;
    else
        return new Intent(getBaseContext(), GrantPermissionsActivity.class);
}
 
Example 9
Source File: MainActivity.java    From star-dns-changer with MIT License 5 votes vote down vote up
private void startDNS() {
    if (presenter.isWorking()) {
        presenter.stopService();
    } else if (isValid()) {
        Intent intent = VpnService.prepare(this);
        if (intent != null) {
            startActivityForResult(intent, REQUEST_CONNECT);
        } else {
            onActivityResult(REQUEST_CONNECT, RESULT_OK, null);
        }
    } else {
        makeSnackbar(getString(R.string.enter_valid_dns));
    }
}
 
Example 10
Source File: MainActivity.java    From VpnServiceDemo with GNU General Public License v2.0 5 votes vote down vote up
private void startVPN()
{
    Intent vpnIntent = VpnService.prepare(this);
    if (vpnIntent != null)
    {
        startActivityForResult(vpnIntent, VPN_REQUEST_CODE);
    }
    else
    {
        onActivityResult(VPN_REQUEST_CODE, RESULT_OK, null);
    }
}
 
Example 11
Source File: LaunchVPN.java    From android with GNU General Public License v3.0 5 votes vote down vote up
void launchVPN() {
    int vpnok = mSelectedProfile.checkProfile(this);
    if (vpnok != R.string.no_error_found) {
        showConfigErrorDialog(vpnok);
        return;
    }

    Intent intent = VpnService.prepare(this);
    // Check if we want to fix /dev/tun
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    boolean usecm9fix = prefs.getBoolean("useCM9Fix", false);
    boolean loadTunModule = prefs.getBoolean("loadTunModule", false);

    if (loadTunModule)
        execeuteSUcmd("insmod /system/lib/modules/tun.ko");

    if (usecm9fix && !mCmfixed) {
        execeuteSUcmd("chown system /dev/tun");
    }


    if (intent != null) {
        VpnStatus.updateStateString("USER_VPN_PERMISSION", "", R.string.state_user_vpn_permission, ConnectionStatus.LEVEL_WAITING_FOR_USER_INPUT);
        // Start the query
        startActivityForResult(new Intent(this, GrantPermissionsActivity.class), START_VPN_PROFILE);
    } else {
        onActivityResult(START_VPN_PROFILE, Activity.RESULT_OK, null);
    }

}
 
Example 12
Source File: VpnAuthActivity.java    From Cybernet-VPN with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mConfig = getIntent().getStringExtra(KEY_CONFIG);
    mUsername = getIntent().getStringExtra(KEY_USERNAME);
    mPw = getIntent().getStringExtra(KEY_PASSWORD);
    Intent intent = VpnService.prepare(this);
    if (intent != null) {
        startActivityForResult(intent, 0);
    } else {
        startVpn();
        finish();
    }
}
 
Example 13
Source File: VoidVpnLauncher.java    From bitmask_android with GNU General Public License v3.0 5 votes vote down vote up
public void setUp() {
    Intent blocking_intent = VpnService.prepare(getApplicationContext()); // stops the VPN connection created by another application.
    if (blocking_intent != null)
        startActivityForResult(blocking_intent, VPN_USER_PERMISSION);
    else {
        onActivityResult(VPN_USER_PERMISSION, RESULT_OK, null);
    }
}
 
Example 14
Source File: MainActivity.java    From InviZible with GNU General Public License v3.0 5 votes vote down vote up
public void prepareVPNService() {
    Log.i(LOG_TAG, "MainActivity prepare VPN Service");

    final Intent prepareIntent = VpnService.prepare(this);

    if (prepareIntent == null) {
        startVPNService(RESULT_OK);
    } else if (!vpnRequested){
        vpnRequested = true;
        startActivityForResult(prepareIntent, CODE_IS_VPN_ALLOWED);
    }

}
 
Example 15
Source File: AlertActivity.java    From product-emm with Apache License 2.0 5 votes vote down vote up
/**
 * This method starts a VPN connection.
 */
private void startVpn() {

	try {
		JSONObject vpnData = new JSONObject(payload);
		if (!vpnData.isNull(resources.getString(R.string.intent_extra_server))) {
			serverAddress = (String) vpnData.get(resources.getString(R.string.intent_extra_server));
		}
		if (!vpnData.isNull(resources.getString(R.string.intent_extra_server_port))) {
			serverPort = (String) vpnData.get(resources.getString(R.string.intent_extra_server_port));
		}

		if (!vpnData.isNull(resources.getString(R.string.intent_extra_shared_secret))) {
			sharedSecret = (String) vpnData.get(resources.getString(R.string.intent_extra_shared_secret));
		}

		if (!vpnData.isNull(resources.getString(R.string.intent_extra_dns))) {
			dnsServer = (String) vpnData.get(resources.getString(R.string.intent_extra_dns));
		}
	} catch (JSONException e) {
		Log.e(TAG, "Invalid VPN payload " + e);
	}

	Intent intent = VpnService.prepare(this);
	if (intent != null) {
		startActivityForResult(intent, VPN_REQUEST_CODE);
	} else {
		onActivityResult(VPN_REQUEST_CODE, RESULT_OK, null);
	}
}
 
Example 16
Source File: Receiver.java    From Android-Firewall with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onReceive(final Context context, Intent intent) {
    Log.i(TAG, "Received " + intent);
    Util.logExtras(TAG, intent);

    // Start service
    if (VpnService.prepare(context) == null)
        BlackHoleService.start(context);
}
 
Example 17
Source File: ShadowsocksRunnerService.java    From Maying with Apache License 2.0 5 votes vote down vote up
@Override
protected void onServiceConnected() {
    if (bgService != null) {
        if (ShadowsocksApplication.app.isNatEnabled()) {
            startBackgroundService();
        } else if (VpnService.prepare(ShadowsocksRunnerService.this) == null) {
            startBackgroundService();
        } else {
            handler.postDelayed(mStopSelfRunnable, 10000);
        }
    }
}
 
Example 18
Source File: VpnFragment.java    From block-this with GNU General Public License v3.0 5 votes vote down vote up
public void startVpn(){
    Intent intent = VpnService.prepare(getActivity());
    if (intent != null) {
        startActivityForResult(intent, 0);
    } else {
        onActivityResult(0, Activity.RESULT_OK, null);
    }
}
 
Example 19
Source File: ShadowsocksRunnerService.java    From ShadowsocksRR with Apache License 2.0 5 votes vote down vote up
@Override
protected void onServiceConnected() {
    if (bgService != null) {
        if (app.isNatEnabled()) {
            startBackgroundService();
        } else if (VpnService.prepare(ShadowsocksRunnerService.this) == null) {
            startBackgroundService();
        } else {
            handler.postDelayed(mStopSelfRunnable, 10000);
        }
    }
}
 
Example 20
Source File: LaunchVPN.java    From bitmask_android with GNU General Public License v3.0 4 votes vote down vote up
void launchVPN() {
    int vpnok = mSelectedProfile.checkProfile(this);
    if (vpnok != R.string.no_error_found) {
        showConfigErrorDialog(vpnok);
        return;
    }

    Intent intent = null;
    try {
        intent = VpnService.prepare(this.getApplicationContext());
    } catch (NullPointerException npe) {
        tellToReceiverOrBroadcast(this.getApplicationContext(), EIP_ACTION_PREPARE_VPN, RESULT_CANCELED);
        finish();
        return;
    }

    // Check if we want to fix /dev/tun
    SharedPreferences prefs = Preferences.getDefaultSharedPreferences(this);
    boolean usecm9fix = prefs.getBoolean("useCM9Fix", false);
    boolean loadTunModule = prefs.getBoolean("loadTunModule", false);

    if (loadTunModule)
        execeuteSUcmd("insmod /system/lib/modules/tun.ko");

    if (usecm9fix && !mCmfixed) {
        execeuteSUcmd("chown system /dev/tun");
    }

    if (intent != null) {
        VpnStatus.updateStateString("USER_VPN_PERMISSION", "", R.string.state_user_vpn_permission,
                ConnectionStatus.LEVEL_WAITING_FOR_USER_INPUT);
        // Start the query
        try {
            startActivityForResult(intent, START_VPN_PROFILE);
        } catch (ActivityNotFoundException ane) {
            // Shame on you Sony! At least one user reported that
            // an official Sony Xperia Arc S image triggers this exception
            VpnStatus.logError(R.string.no_vpn_support_image);
            showLogWindow();
        }
    } else {
        onActivityResult(START_VPN_PROFILE, Activity.RESULT_OK, null);
    }

}