-
-
Notifications
You must be signed in to change notification settings - Fork 325
/
VPNEnableActivity.java
119 lines (95 loc) · 2.86 KB
/
VPNEnableActivity.java
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package org.torproject.android.mini.vpn;
import android.content.Intent;
import android.net.VpnService;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Window;
import org.torproject.android.service.TorService;
import org.torproject.android.service.TorServiceConstants;
import org.torproject.android.service.util.Prefs;
/**
* To combat background service being stopped/swiped
*/
public class VPNEnableActivity extends AppCompatActivity {
private final static int REQUEST_VPN = 7777;
private Intent intent = null;
private boolean checkVpn = true;
private Handler h = new Handler();
@Override
public void onCreate(Bundle icicle ) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate( icicle );
Log.d("VPNEnableActivity","prompting user to start Orbot VPN");
}
public void onResume() {
super.onResume();
if (checkVpn)
{
intent = VpnService.prepare(this);
if (intent != null)
promptStartVpnService();
else
startVpnService ();
checkVpn = false;
}
}
public void promptStartVpnService () {
// todo no actual prompting happens here and this should be refactored
startVpnService();
}
private void startVpnService ()
{
if (intent == null)
{
Prefs.putUseVpn(true);
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();
}
}, 100);
}
else
{
Log.w("VPNEnableActivity","prompt for VPN");
startActivityForResult(intent,REQUEST_VPN);
}
}
public static final int ACTIVITY_RESULT_VPN_DENIED = 63;
@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);
h.postDelayed(new Runnable () {
@Override
public void run () {
sendIntentToService(TorServiceConstants.ACTION_START);
finish();
}
}, 1000);
}
else if (request == REQUEST_VPN && response == RESULT_CANCELED) {
setResult(ACTIVITY_RESULT_VPN_DENIED);
finish();
}
}
private void sendIntentToService(String action) {
Intent torService = new Intent(this, TorService.class);
torService.setAction(action);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(torService);
}
else
{
startService(torService);
}
}
}