summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnLauncher.java
blob: a8494e2b45b18dd6fb8585407eeb7fea41ff0e4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package se.leap.bitmaskclient.eip;

import android.app.Activity;
import android.content.Intent;
import android.net.VpnService;
import android.os.Bundle;

import static se.leap.bitmaskclient.base.models.Constants.EIP_ACTION_PREPARE_VPN;
import static se.leap.bitmaskclient.eip.EipResultBroadcast.tellToReceiverOrBroadcast;

public class VoidVpnLauncher extends Activity {

    private static final int VPN_USER_PERMISSION = 71;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setUp();
    }

    public void setUp() {
        Intent blockingIntent = null;
        try {
            blockingIntent = VpnService.prepare(getApplicationContext()); // stops the VPN connection created by another application.
        } catch (NullPointerException npe) {
            tellToReceiverOrBroadcast(this.getApplicationContext(), EIP_ACTION_PREPARE_VPN, RESULT_CANCELED);
            finish();
        }
        if (blockingIntent != null) {
            startActivityForResult(blockingIntent, VPN_USER_PERMISSION);
        }
        else {
            EipCommand.startBlockingVPN(this);
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == VPN_USER_PERMISSION) {
            if (resultCode == RESULT_OK) {
                EipCommand.launchVoidVPN(this);
            }
        }
        finish();
    }
}