]>
Commit | Line | Data |
---|---|---|
58d139da TB |
1 | /* |
2 | * Copyright (C) 2012-2018 Tobias Brunner | |
3 | * HSR Hochschule fuer Technik Rapperswil | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License as published by the | |
7 | * Free Software Foundation; either version 2 of the License, or (at your | |
8 | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
12 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
13 | * for more details. | |
14 | */ | |
15 | ||
16 | package org.strongswan.android.ui; | |
17 | ||
18 | import android.app.Dialog; | |
19 | import android.app.Service; | |
20 | import android.content.ActivityNotFoundException; | |
21 | import android.content.ComponentName; | |
22 | import android.content.DialogInterface; | |
23 | import android.content.Intent; | |
24 | import android.content.ServiceConnection; | |
25 | import android.net.VpnService; | |
26 | import android.os.Bundle; | |
27 | import android.os.IBinder; | |
28 | import android.support.v4.app.Fragment; | |
29 | import android.support.v4.app.FragmentManager; | |
30 | import android.support.v4.app.FragmentTransaction; | |
31 | import android.support.v7.app.AlertDialog; | |
32 | import android.support.v7.app.AppCompatActivity; | |
33 | import android.support.v7.app.AppCompatDialogFragment; | |
34 | import android.view.LayoutInflater; | |
35 | import android.view.View; | |
36 | import android.widget.EditText; | |
37 | import android.widget.Toast; | |
38 | ||
39 | import org.strongswan.android.R; | |
40 | import org.strongswan.android.data.VpnProfile; | |
41 | import org.strongswan.android.data.VpnProfileDataSource; | |
42 | import org.strongswan.android.data.VpnType.VpnTypeFeature; | |
58d139da TB |
43 | import org.strongswan.android.logic.VpnStateService; |
44 | import org.strongswan.android.logic.VpnStateService.State; | |
45 | ||
58d139da TB |
46 | public class VpnProfileControlActivity extends AppCompatActivity |
47 | { | |
48 | public static final String START_PROFILE = "org.strongswan.android.action.START_PROFILE"; | |
49 | public static final String DISCONNECT = "org.strongswan.android.action.DISCONNECT"; | |
50 | public static final String EXTRA_VPN_PROFILE_ID = "org.strongswan.android.VPN_PROFILE_ID"; | |
51 | ||
52 | private static final int PREPARE_VPN_SERVICE = 0; | |
baf2f474 | 53 | private static final String WAITING_FOR_RESULT = "WAITING_FOR_RESULT"; |
58d139da TB |
54 | private static final String PROFILE_NAME = "PROFILE_NAME"; |
55 | private static final String PROFILE_REQUIRES_PASSWORD = "REQUIRES_PASSWORD"; | |
56 | private static final String PROFILE_RECONNECT = "RECONNECT"; | |
57 | private static final String PROFILE_DISCONNECT = "DISCONNECT"; | |
58 | private static final String DIALOG_TAG = "Dialog"; | |
59 | ||
60 | private Bundle mProfileInfo; | |
baf2f474 | 61 | private boolean mWaitingForResult; |
58d139da TB |
62 | private VpnStateService mService; |
63 | private final ServiceConnection mServiceConnection = new ServiceConnection() | |
64 | { | |
65 | @Override | |
66 | public void onServiceDisconnected(ComponentName name) | |
67 | { | |
68 | mService = null; | |
69 | } | |
70 | ||
71 | @Override | |
72 | public void onServiceConnected(ComponentName name, IBinder service) | |
73 | { | |
74 | mService = ((VpnStateService.LocalBinder)service).getService(); | |
baf2f474 | 75 | handleIntent(); |
58d139da TB |
76 | } |
77 | }; | |
78 | ||
79 | @Override | |
80 | public void onCreate(Bundle savedInstanceState) | |
81 | { | |
82 | super.onCreate(savedInstanceState); | |
83 | ||
baf2f474 TB |
84 | if (savedInstanceState != null) |
85 | { | |
86 | mWaitingForResult = savedInstanceState.getBoolean(WAITING_FOR_RESULT, false); | |
87 | } | |
58d139da TB |
88 | this.bindService(new Intent(this, VpnStateService.class), |
89 | mServiceConnection, Service.BIND_AUTO_CREATE); | |
90 | } | |
91 | ||
baf2f474 TB |
92 | @Override |
93 | protected void onSaveInstanceState(Bundle outState) | |
94 | { | |
95 | super.onSaveInstanceState(outState); | |
96 | outState.putBoolean(WAITING_FOR_RESULT, mWaitingForResult); | |
97 | } | |
98 | ||
58d139da TB |
99 | @Override |
100 | protected void onDestroy() | |
101 | { | |
102 | super.onDestroy(); | |
103 | if (mService != null) | |
104 | { | |
105 | this.unbindService(mServiceConnection); | |
106 | } | |
107 | } | |
108 | ||
109 | /** | |
110 | * Due to launchMode=singleTop this is called if the Activity already exists | |
111 | */ | |
112 | @Override | |
113 | protected void onNewIntent(Intent intent) | |
114 | { | |
115 | super.onNewIntent(intent); | |
116 | ||
baf2f474 TB |
117 | /* store this intent in case the service is not yet connected or the activity is restarted */ |
118 | setIntent(intent); | |
119 | ||
120 | if (mService != null) | |
58d139da | 121 | { |
baf2f474 | 122 | handleIntent(); |
58d139da TB |
123 | } |
124 | } | |
125 | ||
126 | /** | |
127 | * Prepare the VpnService. If this succeeds the current VPN profile is | |
128 | * started. | |
129 | * | |
130 | * @param profileInfo a bundle containing the information about the profile to be started | |
131 | */ | |
132 | protected void prepareVpnService(Bundle profileInfo) | |
133 | { | |
134 | Intent intent; | |
baf2f474 TB |
135 | |
136 | if (mWaitingForResult) | |
137 | { | |
138 | mProfileInfo = profileInfo; | |
139 | return; | |
140 | } | |
141 | ||
58d139da TB |
142 | try |
143 | { | |
144 | intent = VpnService.prepare(this); | |
145 | } | |
146 | catch (IllegalStateException ex) | |
147 | { | |
148 | /* this happens if the always-on VPN feature (Android 4.2+) is activated */ | |
149 | VpnNotSupportedError.showWithMessage(this, R.string.vpn_not_supported_during_lockdown); | |
150 | return; | |
151 | } | |
152 | catch (NullPointerException ex) | |
153 | { | |
154 | /* not sure when this happens exactly, but apparently it does */ | |
155 | VpnNotSupportedError.showWithMessage(this, R.string.vpn_not_supported); | |
156 | return; | |
157 | } | |
158 | /* store profile info until the user grants us permission */ | |
159 | mProfileInfo = profileInfo; | |
160 | if (intent != null) | |
161 | { | |
162 | try | |
163 | { | |
baf2f474 | 164 | mWaitingForResult = true; |
58d139da TB |
165 | startActivityForResult(intent, PREPARE_VPN_SERVICE); |
166 | } | |
167 | catch (ActivityNotFoundException ex) | |
168 | { | |
169 | /* it seems some devices, even though they come with Android 4, | |
170 | * don't have the VPN components built into the system image. | |
171 | * com.android.vpndialogs/com.android.vpndialogs.ConfirmDialog | |
172 | * will not be found then */ | |
173 | VpnNotSupportedError.showWithMessage(this, R.string.vpn_not_supported); | |
baf2f474 | 174 | mWaitingForResult = false; |
58d139da TB |
175 | } |
176 | } | |
177 | else | |
178 | { /* user already granted permission to use VpnService */ | |
179 | onActivityResult(PREPARE_VPN_SERVICE, RESULT_OK, null); | |
180 | } | |
181 | } | |
182 | ||
183 | @Override | |
184 | protected void onActivityResult(int requestCode, int resultCode, Intent data) | |
185 | { | |
186 | switch (requestCode) | |
187 | { | |
188 | case PREPARE_VPN_SERVICE: | |
baf2f474 | 189 | mWaitingForResult = false; |
58d139da TB |
190 | if (resultCode == RESULT_OK && mProfileInfo != null) |
191 | { | |
1350ee1e TB |
192 | if (mService != null) |
193 | { | |
194 | mService.connect(mProfileInfo, true); | |
195 | } | |
58d139da TB |
196 | finish(); |
197 | } | |
198 | else | |
199 | { /* this happens if the always-on VPN feature is activated by a different app or the user declined */ | |
4db3bf0c TB |
200 | if (getSupportFragmentManager().isStateSaved()) |
201 | { /* onActivityResult() might be called when we aren't active anymore e.g. if the | |
202 | * user pressed the home button, if the activity is started again we land here | |
203 | * before onNewIntent() is called */ | |
204 | return; | |
205 | } | |
58d139da TB |
206 | VpnNotSupportedError.showWithMessage(this, R.string.vpn_not_supported_no_permission); |
207 | } | |
208 | break; | |
209 | default: | |
210 | super.onActivityResult(requestCode, resultCode, data); | |
211 | } | |
212 | } | |
213 | ||
be893107 TB |
214 | /** |
215 | * Check if we are currently connected to a VPN connection | |
216 | * | |
217 | * @return true if currently connected | |
218 | */ | |
219 | private boolean isConnected() | |
220 | { | |
221 | if (mService == null) | |
222 | { | |
223 | return false; | |
224 | } | |
225 | if (mService.getErrorState() != VpnStateService.ErrorState.NO_ERROR) | |
226 | { /* allow reconnecting (even to a different profile) without confirmation if there is an error */ | |
227 | return false; | |
228 | } | |
229 | return (mService.getState() == State.CONNECTED || mService.getState() == State.CONNECTING); | |
230 | } | |
231 | ||
58d139da TB |
232 | /** |
233 | * Start the given VPN profile | |
234 | * | |
235 | * @param profile VPN profile | |
236 | */ | |
237 | public void startVpnProfile(VpnProfile profile) | |
238 | { | |
239 | Bundle profileInfo = new Bundle(); | |
6f9b96ac | 240 | profileInfo.putString(VpnProfileDataSource.KEY_UUID, profile.getUUID().toString()); |
58d139da TB |
241 | profileInfo.putString(VpnProfileDataSource.KEY_USERNAME, profile.getUsername()); |
242 | profileInfo.putString(VpnProfileDataSource.KEY_PASSWORD, profile.getPassword()); | |
243 | profileInfo.putBoolean(PROFILE_REQUIRES_PASSWORD, profile.getVpnType().has(VpnTypeFeature.USER_PASS)); | |
244 | profileInfo.putString(PROFILE_NAME, profile.getName()); | |
245 | ||
246 | removeFragmentByTag(DIALOG_TAG); | |
247 | ||
be893107 | 248 | if (isConnected()) |
58d139da | 249 | { |
be893107 | 250 | profileInfo.putBoolean(PROFILE_RECONNECT, mService.getProfile().getUUID().equals(profile.getUUID())); |
58d139da TB |
251 | |
252 | ConfirmationDialog dialog = new ConfirmationDialog(); | |
253 | dialog.setArguments(profileInfo); | |
254 | dialog.show(this.getSupportFragmentManager(), DIALOG_TAG); | |
255 | return; | |
256 | } | |
257 | startVpnProfile(profileInfo); | |
258 | } | |
259 | ||
260 | /** | |
261 | * Start the given VPN profile asking the user for a password if required. | |
262 | * | |
263 | * @param profileInfo data about the profile | |
264 | */ | |
265 | private void startVpnProfile(Bundle profileInfo) | |
266 | { | |
267 | if (profileInfo.getBoolean(PROFILE_REQUIRES_PASSWORD) && | |
268 | profileInfo.getString(VpnProfileDataSource.KEY_PASSWORD) == null) | |
269 | { | |
270 | LoginDialog login = new LoginDialog(); | |
271 | login.setArguments(profileInfo); | |
272 | login.show(getSupportFragmentManager(), DIALOG_TAG); | |
273 | return; | |
274 | } | |
275 | prepareVpnService(profileInfo); | |
276 | } | |
277 | ||
278 | /** | |
279 | * Start the VPN profile referred to by the given intent. Displays an error | |
280 | * if the profile doesn't exist. | |
281 | * | |
282 | * @param intent Intent that caused us to start this | |
283 | */ | |
284 | private void startVpnProfile(Intent intent) | |
285 | { | |
286 | VpnProfile profile = null; | |
287 | ||
288 | VpnProfileDataSource dataSource = new VpnProfileDataSource(this); | |
289 | dataSource.open(); | |
290 | String profileUUID = intent.getStringExtra(EXTRA_VPN_PROFILE_ID); | |
291 | if (profileUUID != null) | |
292 | { | |
acdac148 | 293 | profile = dataSource.getVpnProfile(profileUUID); |
58d139da TB |
294 | } |
295 | else | |
296 | { | |
297 | long profileId = intent.getLongExtra(EXTRA_VPN_PROFILE_ID, 0); | |
298 | if (profileId > 0) | |
299 | { | |
300 | profile = dataSource.getVpnProfile(profileId); | |
301 | } | |
302 | } | |
303 | dataSource.close(); | |
304 | ||
305 | if (profile != null) | |
306 | { | |
307 | startVpnProfile(profile); | |
308 | } | |
309 | else | |
310 | { | |
311 | Toast.makeText(this, R.string.profile_not_found, Toast.LENGTH_LONG).show(); | |
312 | finish(); | |
313 | } | |
314 | } | |
315 | ||
316 | /** | |
317 | * Disconnect the current connection, if any (silently ignored if there is no connection). | |
318 | */ | |
319 | private void disconnect() | |
320 | { | |
321 | removeFragmentByTag(DIALOG_TAG); | |
322 | ||
323 | if (mService != null) | |
324 | { | |
325 | if (mService.getState() == State.CONNECTED || mService.getState() == State.CONNECTING) | |
326 | { | |
327 | Bundle args = new Bundle(); | |
328 | args.putBoolean(PROFILE_DISCONNECT, true); | |
329 | ||
330 | ConfirmationDialog dialog = new ConfirmationDialog(); | |
331 | dialog.setArguments(args); | |
332 | dialog.show(this.getSupportFragmentManager(), DIALOG_TAG); | |
333 | } | |
334 | else | |
335 | { | |
336 | finish(); | |
337 | } | |
338 | } | |
339 | } | |
340 | ||
baf2f474 TB |
341 | /** |
342 | * Handle the Intent of this Activity depending on its action | |
343 | */ | |
344 | private void handleIntent() | |
345 | { | |
346 | if (START_PROFILE.equals(getIntent().getAction())) | |
347 | { | |
348 | startVpnProfile(getIntent()); | |
349 | } | |
350 | else if (DISCONNECT.equals(getIntent().getAction())) | |
351 | { | |
352 | disconnect(); | |
353 | } | |
354 | } | |
355 | ||
58d139da TB |
356 | /** |
357 | * Dismiss dialog if shown | |
358 | */ | |
359 | public void removeFragmentByTag(String tag) | |
360 | { | |
361 | FragmentManager fm = getSupportFragmentManager(); | |
362 | Fragment login = fm.findFragmentByTag(tag); | |
363 | if (login != null) | |
364 | { | |
365 | FragmentTransaction ft = fm.beginTransaction(); | |
366 | ft.remove(login); | |
367 | ft.commit(); | |
368 | } | |
369 | } | |
370 | ||
371 | /** | |
372 | * Class that displays a confirmation dialog if a VPN profile is already connected | |
373 | * and then initiates the selected VPN profile if the user confirms the dialog. | |
374 | */ | |
375 | public static class ConfirmationDialog extends AppCompatDialogFragment | |
376 | { | |
377 | @Override | |
378 | public Dialog onCreateDialog(Bundle savedInstanceState) | |
379 | { | |
380 | final Bundle profileInfo = getArguments(); | |
381 | int icon = android.R.drawable.ic_dialog_alert; | |
382 | int title = R.string.connect_profile_question; | |
383 | int message = R.string.replaces_active_connection; | |
384 | int button = R.string.connect; | |
385 | ||
386 | if (profileInfo.getBoolean(PROFILE_RECONNECT)) | |
387 | { | |
388 | icon = android.R.drawable.ic_dialog_info; | |
389 | title = R.string.vpn_connected; | |
390 | message = R.string.vpn_profile_connected; | |
391 | button = R.string.reconnect; | |
392 | } | |
393 | else if (profileInfo.getBoolean(PROFILE_DISCONNECT)) | |
394 | { | |
395 | title = R.string.disconnect_question; | |
396 | message = R.string.disconnect_active_connection; | |
397 | button = R.string.disconnect; | |
398 | } | |
399 | ||
400 | DialogInterface.OnClickListener connectListener = new DialogInterface.OnClickListener() | |
401 | { | |
402 | @Override | |
403 | public void onClick(DialogInterface dialog, int which) | |
404 | { | |
405 | VpnProfileControlActivity activity = (VpnProfileControlActivity)getActivity(); | |
406 | activity.startVpnProfile(profileInfo); | |
407 | } | |
408 | }; | |
409 | DialogInterface.OnClickListener disconnectListener = new DialogInterface.OnClickListener() | |
410 | { | |
411 | @Override | |
412 | public void onClick(DialogInterface dialog, int which) | |
413 | { | |
414 | VpnProfileControlActivity activity = (VpnProfileControlActivity)getActivity(); | |
415 | if (activity.mService != null) | |
416 | { | |
417 | activity.mService.disconnect(); | |
418 | } | |
419 | getActivity().finish(); | |
420 | } | |
421 | }; | |
422 | DialogInterface.OnClickListener cancelListener = new DialogInterface.OnClickListener() | |
423 | { | |
424 | @Override | |
425 | public void onClick(DialogInterface dialog, int which) | |
426 | { | |
427 | getActivity().finish(); | |
428 | } | |
429 | }; | |
430 | ||
431 | AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()) | |
432 | .setIcon(icon) | |
433 | .setTitle(String.format(getString(title), profileInfo.getString(PROFILE_NAME))) | |
434 | .setMessage(message); | |
435 | ||
436 | if (profileInfo.getBoolean(PROFILE_DISCONNECT)) | |
437 | { | |
438 | builder.setPositiveButton(button, disconnectListener); | |
439 | } | |
440 | else | |
441 | { | |
442 | builder.setPositiveButton(button, connectListener); | |
443 | } | |
444 | ||
445 | if (profileInfo.getBoolean(PROFILE_RECONNECT)) | |
446 | { | |
447 | builder.setNegativeButton(R.string.disconnect, disconnectListener); | |
448 | builder.setNeutralButton(android.R.string.cancel, cancelListener); | |
449 | } | |
450 | else | |
451 | { | |
452 | builder.setNegativeButton(android.R.string.cancel, cancelListener); | |
453 | } | |
454 | return builder.create(); | |
455 | } | |
456 | ||
457 | @Override | |
458 | public void onCancel(DialogInterface dialog) | |
459 | { | |
460 | getActivity().finish(); | |
461 | } | |
462 | } | |
463 | ||
464 | /** | |
465 | * Class that displays a login dialog and initiates the selected VPN | |
466 | * profile if the user confirms the dialog. | |
467 | */ | |
468 | public static class LoginDialog extends AppCompatDialogFragment | |
469 | { | |
470 | @Override | |
471 | public Dialog onCreateDialog(Bundle savedInstanceState) | |
472 | { | |
473 | final Bundle profileInfo = getArguments(); | |
474 | LayoutInflater inflater = getActivity().getLayoutInflater(); | |
475 | View view = inflater.inflate(R.layout.login_dialog, null); | |
476 | EditText username = (EditText)view.findViewById(R.id.username); | |
477 | username.setText(profileInfo.getString(VpnProfileDataSource.KEY_USERNAME)); | |
478 | final EditText password = (EditText)view.findViewById(R.id.password); | |
479 | ||
480 | AlertDialog.Builder adb = new AlertDialog.Builder(getActivity()); | |
481 | adb.setView(view); | |
482 | adb.setTitle(getString(R.string.login_title)); | |
483 | adb.setPositiveButton(R.string.login_confirm, new DialogInterface.OnClickListener() | |
484 | { | |
485 | @Override | |
486 | public void onClick(DialogInterface dialog, int whichButton) | |
487 | { | |
488 | VpnProfileControlActivity activity = (VpnProfileControlActivity)getActivity(); | |
489 | profileInfo.putString(VpnProfileDataSource.KEY_PASSWORD, password.getText().toString().trim()); | |
490 | activity.prepareVpnService(profileInfo); | |
491 | } | |
492 | }); | |
493 | adb.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() | |
494 | { | |
495 | @Override | |
496 | public void onClick(DialogInterface dialog, int which) | |
497 | { | |
498 | getActivity().finish(); | |
499 | } | |
500 | }); | |
501 | return adb.create(); | |
502 | } | |
503 | ||
504 | @Override | |
505 | public void onCancel(DialogInterface dialog) | |
506 | { | |
507 | getActivity().finish(); | |
508 | } | |
509 | } | |
510 | ||
511 | /** | |
512 | * Class representing an error message which is displayed if VpnService is | |
513 | * not supported on the current device. | |
514 | */ | |
515 | public static class VpnNotSupportedError extends AppCompatDialogFragment | |
516 | { | |
517 | static final String ERROR_MESSAGE_ID = "org.strongswan.android.VpnNotSupportedError.MessageId"; | |
518 | ||
519 | public static void showWithMessage(AppCompatActivity activity, int messageId) | |
520 | { | |
521 | Bundle bundle = new Bundle(); | |
522 | bundle.putInt(ERROR_MESSAGE_ID, messageId); | |
523 | VpnNotSupportedError dialog = new VpnNotSupportedError(); | |
524 | dialog.setArguments(bundle); | |
525 | dialog.show(activity.getSupportFragmentManager(), DIALOG_TAG); | |
526 | } | |
527 | ||
528 | @Override | |
529 | public Dialog onCreateDialog(Bundle savedInstanceState) | |
530 | { | |
531 | final Bundle arguments = getArguments(); | |
532 | final int messageId = arguments.getInt(ERROR_MESSAGE_ID); | |
533 | return new AlertDialog.Builder(getActivity()) | |
534 | .setTitle(R.string.vpn_not_supported_title) | |
535 | .setMessage(messageId) | |
536 | .setCancelable(false) | |
537 | .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() | |
538 | { | |
539 | @Override | |
540 | public void onClick(DialogInterface dialog, int id) | |
541 | { | |
542 | getActivity().finish(); | |
543 | } | |
544 | }).create(); | |
545 | } | |
546 | ||
547 | @Override | |
548 | public void onCancel(DialogInterface dialog) | |
549 | { | |
550 | getActivity().finish(); | |
551 | } | |
552 | } | |
553 | } |