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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
package se.leap.bitmaskclient;
import se.leap.bitmaskclient.R;
import se.leap.bitmaskclient.ProviderAPIResultReceiver;
import se.leap.bitmaskclient.ProviderAPIResultReceiver.Receiver;
import se.leap.bitmaskclient.Dashboard;
import de.blinkt.openvpn.activities.LogWindow;
import de.blinkt.openvpn.core.VpnStatus;
import de.blinkt.openvpn.core.VpnStatus.ConnectionStatus;
import de.blinkt.openvpn.core.VpnStatus.StateListener;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CompoundButton;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
public class EipServiceFragment extends Fragment implements StateListener, OnCheckedChangeListener {
protected static final String IS_EIP_PENDING = "is_eip_pending";
public static final String START_ON_BOOT = "start on boot";
private View eipFragment;
private static Switch eipSwitch;
private View eipDetail;
private TextView eipStatus;
private static EIPReceiver mEIPReceiver;
public static String TAG = "se.leap.bitmask.EipServiceFragment";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
eipFragment = inflater.inflate(R.layout.eip_service_fragment, container, false);
eipDetail = ((RelativeLayout) eipFragment.findViewById(R.id.eipDetail));
eipDetail.setVisibility(View.VISIBLE);
View eipSettings = eipFragment.findViewById(R.id.eipSettings);
eipSettings.setVisibility(View.GONE); // FIXME too!
if (EIP.mIsStarting)
eipFragment.findViewById(R.id.eipProgress).setVisibility(View.VISIBLE);
eipStatus = (TextView) eipFragment.findViewById(R.id.eipStatus);
eipSwitch = (Switch) eipFragment.findViewById(R.id.eipSwitch);
eipSwitch.setOnCheckedChangeListener(this);
if(getArguments() != null && getArguments().containsKey(START_ON_BOOT) && getArguments().getBoolean(START_ON_BOOT))
startEipFromScratch();
return eipFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mEIPReceiver = new EIPReceiver(new Handler());
if (savedInstanceState != null)
EIP.mIsStarting = savedInstanceState.getBoolean(IS_EIP_PENDING);
}
@Override
public void onResume() {
super.onResume();
VpnStatus.addStateListener(this);
eipCommand(EIP.ACTION_CHECK_CERT_VALIDITY);
}
@Override
public void onPause() {
super.onPause();
VpnStatus.removeStateListener(this);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(IS_EIP_PENDING, EIP.mIsStarting);
}
protected void saveEipStatus() {
boolean eip_is_on = false;
Log.d("bitmask", "saveEipStatus");
if(eipSwitch.isChecked()) {
eip_is_on = true;
}
if(getActivity() != null)
Dashboard.preferences.edit().putBoolean(Dashboard.START_ON_BOOT, eip_is_on).commit();
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.equals(eipSwitch)){
handleSwitch(isChecked);
}
}
private void handleSwitch(boolean isChecked) {
if(isChecked)
handleSwitchOn();
else
handleSwitchOff();
saveEipStatus();
}
private void handleSwitchOn() {
if(canStartEIP())
startEipFromScratch();
else if(canLogInToStartEIP()) {
Log.d(TAG, "Can Log In to start EIP");
Dashboard dashboard = (Dashboard) getActivity();
dashboard.logInDialog(Bundle.EMPTY);
}
}
private boolean canStartEIP() {
boolean certificateExists = !Dashboard.preferences.getString(EIP.CERTIFICATE, "").isEmpty();
boolean isAllowedAnon = Dashboard.preferences.getBoolean(EIP.ALLOWED_ANON, false);
return (isAllowedAnon || certificateExists) && !EIP.mIsStarting && !EIP.isConnected();
}
private boolean canLogInToStartEIP() {
boolean isAllowedRegistered = Dashboard.preferences.getBoolean(EIP.ALLOWED_REGISTERED, false);
boolean isLoggedIn = Dashboard.preferences.getBoolean(EIP.AUTHED_EIP, false);
Log.d(TAG, "Allow registered? " + isAllowedRegistered);
Log.d(TAG, "Is logged in? " + isLoggedIn);
return isAllowedRegistered && !isLoggedIn && !EIP.mIsStarting && !EIP.isConnected();
}
private void handleSwitchOff() {
if(EIP.mIsStarting) {
askPendingStartCancellation();
} else if(EIP.isConnected()) {
Log.d(TAG, "Stopping EIP");
stopEIP();
}
}
private void askPendingStartCancellation() {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getActivity());
alertBuilder.setTitle(getResources().getString(R.string.eip_cancel_connect_title))
.setMessage(getResources().getString(R.string.eip_cancel_connect_text))
.setPositiveButton((R.string.yes), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
stopEIP();
}
})
.setNegativeButton(getResources().getString(R.string.no), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d(TAG, "askPendingStartCancellation checks the switch to true");
eipSwitch.setChecked(true);
}
})
.show();
}
public void startEipFromScratch() {
EIP.mIsStarting = true;
eipFragment.findViewById(R.id.eipProgress).setVisibility(View.VISIBLE);
String status = getResources().getString(R.string.eip_status_start_pending);
setEipStatus(status);
if(!eipSwitch.isChecked()) {
Log.d(TAG, "startEipFromScratch checks the switch to true");
eipSwitch.setChecked(true);
saveEipStatus();
}
eipCommand(EIP.ACTION_START_EIP);
}
private void stopEIP() {
EIP.mIsStarting = false;
View eipProgressBar = getActivity().findViewById(R.id.eipProgress);
if(eipProgressBar != null)
eipProgressBar.setVisibility(View.GONE);
String status = getResources().getString(R.string.eip_state_not_connected);
setEipStatus(status);
eipCommand(EIP.ACTION_STOP_EIP);
}
/**
* Send a command to EIP
*
* @param action A valid String constant from EIP class representing an Intent
* filter for the EIP class
*/
private void eipCommand(String action){
// TODO validate "action"...how do we get the list of intent-filters for a class via Android API?
Intent vpn_intent = new Intent(getActivity().getApplicationContext(), EIP.class);
vpn_intent.setAction(action);
vpn_intent.putExtra(EIP.RECEIVER_TAG, mEIPReceiver);
getActivity().startService(vpn_intent);
}
@Override
public void updateState(final String state, final String logmessage, final int localizedResId, final ConnectionStatus level) {
boolean isNewLevel = EIP.lastConnectionStatusLevel != level;
boolean justDecidedOnDisconnect = EIP.lastConnectionStatusLevel == ConnectionStatus.UNKNOWN_LEVEL;
Log.d(TAG, "update state with level " + level);
if(!justDecidedOnDisconnect && (isNewLevel || level == ConnectionStatus.LEVEL_CONNECTED)) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
EIP.lastConnectionStatusLevel = level;
handleNewState(state, logmessage, localizedResId, level);
}
});
} else if(justDecidedOnDisconnect && level == ConnectionStatus.LEVEL_CONNECTED) {
EIP.lastConnectionStatusLevel = ConnectionStatus.LEVEL_NOTCONNECTED;
updateState(state, logmessage, localizedResId, level);
}
}
private void handleNewState(final String state, final String logmessage, final int localizedResId, final ConnectionStatus level) {
if (level == ConnectionStatus.LEVEL_CONNECTED)
setConnectedUI();
else if (isDisconnectedLevel(level) && !EIP.mIsStarting)
setDisconnectedUI();
else if (level == ConnectionStatus.LEVEL_CONNECTING_NO_SERVER_REPLY_YET)
setNoServerReplyUI(localizedResId, logmessage);
else if (level == ConnectionStatus.LEVEL_CONNECTING_SERVER_REPLIED)
setServerReplyUI(state, localizedResId, logmessage);
}
private boolean isDisconnectedLevel(final ConnectionStatus level) {
return level == ConnectionStatus.LEVEL_NOTCONNECTED || level == ConnectionStatus.LEVEL_AUTH_FAILED;
}
private void setConnectedUI() {
hideProgressBar();
Log.d(TAG, "mIsDisconnecting = false in setConnectedUI");
EIP.mIsStarting = false; //TODO This should be done in the onReceiveResult from START_EIP command, but right now LaunchVPN isn't notifying anybody the resultcode of the request so we need to listen the states with this listener.
EIP.mIsDisconnecting = false; //TODO See comment above
String status = getString(R.string.eip_state_connected);
setEipStatus(status);
adjustSwitch();
}
private void setDisconnectedUI(){
hideProgressBar();
EIP.mIsStarting = false; //TODO See comment in setConnectedUI()
Log.d(TAG, "mIsDisconnecting = false in setDisconnectedUI");
EIP.mIsDisconnecting = false; //TODO See comment in setConnectedUI()
String status = getString(R.string.eip_state_not_connected);
setEipStatus(status);
adjustSwitch();
}
private void adjustSwitch() {
if(EIP.isConnected()) {
if(!eipSwitch.isChecked()) {
eipSwitch.setChecked(true);
}
} else {
if(eipSwitch.isChecked()) {
eipSwitch.setChecked(false);
}
}
}
private void setNoServerReplyUI(int localizedResId, String logmessage) {
if(eipStatus != null) {
String prefix = getString(localizedResId);
setEipStatus(prefix + " " + logmessage);
}
}
private void setServerReplyUI(String state, int localizedResId, String logmessage) {
if(eipStatus != null)
if(state.equals("AUTH") || state.equals("GET_CONFIG")) {
String prefix = getString(localizedResId);
setEipStatus(prefix + " " + logmessage);
}
}
protected void setEipStatus(String status) {
if(eipStatus == null)
eipStatus = (TextView) getActivity().findViewById(R.id.eipStatus);
eipStatus.setText(status);
}
private void hideProgressBar() {
if(getActivity() != null && getActivity().findViewById(R.id.eipProgress) != null)
getActivity().findViewById(R.id.eipProgress).setVisibility(View.GONE);
}
/**
* Inner class for handling messages related to EIP status and control requests
*
* @author Sean Leonard <meanderingcode@aetherislands.net>
*/
protected class EIPReceiver extends ResultReceiver {
protected EIPReceiver(Handler handler){
super(handler);
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
String request = resultData.getString(EIP.REQUEST_TAG);
boolean checked = false;
if (request == EIP.ACTION_IS_EIP_RUNNING) {
switch (resultCode){
case Activity.RESULT_OK:
checked = true;
break;
case Activity.RESULT_CANCELED:
checked = false;
break;
}
} else if (request == EIP.ACTION_START_EIP) {
switch (resultCode){
case Activity.RESULT_OK:
Log.d(TAG, "Action start eip = Result OK");
checked = true;
eipFragment.findViewById(R.id.eipProgress).setVisibility(View.VISIBLE);
EIP.mIsStarting = false;
break;
case Activity.RESULT_CANCELED:
checked = false;
eipFragment.findViewById(R.id.eipProgress).setVisibility(View.GONE);
break;
}
} else if (request == EIP.ACTION_STOP_EIP) {
switch (resultCode){
case Activity.RESULT_OK:
checked = false;
break;
case Activity.RESULT_CANCELED:
checked = true;
break;
}
} else if (request == EIP.EIP_NOTIFICATION) {
switch (resultCode){
case Activity.RESULT_OK:
checked = true;
break;
case Activity.RESULT_CANCELED:
checked = false;
break;
}
} else if (request == EIP.ACTION_CHECK_CERT_VALIDITY) {
checked = eipSwitch.isChecked();
switch (resultCode) {
case Activity.RESULT_OK:
break;
case Activity.RESULT_CANCELED:
Dashboard dashboard = (Dashboard) getActivity();
dashboard.setProgressBarVisibility(ProgressBar.VISIBLE);
String status = getResources().getString(R.string.updating_certificate_message);
setEipStatus(status);
Intent provider_API_command = new Intent(getActivity(), ProviderAPI.class);
if(dashboard.providerAPI_result_receiver == null) {
dashboard.providerAPI_result_receiver = new ProviderAPIResultReceiver(new Handler());
dashboard.providerAPI_result_receiver.setReceiver(dashboard);
}
provider_API_command.setAction(ProviderAPI.DOWNLOAD_CERTIFICATE);
provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, dashboard.providerAPI_result_receiver);
getActivity().startService(provider_API_command);
break;
}
}
}
}
public static EIPReceiver getReceiver() {
return mEIPReceiver;
}
public static boolean isEipSwitchChecked() {
return eipSwitch.isChecked();
}
public void checkEipSwitch(boolean checked) {
eipSwitch.setChecked(checked);
// Log.d(TAG, "checkEipSwitch");
// onCheckedChanged(eipSwitch, checked);
}
}
|