2 * Copyright (C) 2012-2018 Tobias Brunner
3 * HSR Hochschule fuer Technik Rapperswil
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>.
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
16 package org
.strongswan
.android
.ui
;
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
;
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
;
43 import org
.strongswan
.android
.logic
.VpnStateService
;
44 import org
.strongswan
.android
.logic
.VpnStateService
.State
;
46 public class VpnProfileControlActivity
extends AppCompatActivity
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";
52 private static final int PREPARE_VPN_SERVICE
= 0;
53 private static final String WAITING_FOR_RESULT
= "WAITING_FOR_RESULT";
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";
60 private Bundle mProfileInfo
;
61 private boolean mWaitingForResult
;
62 private VpnStateService mService
;
63 private final ServiceConnection mServiceConnection
= new ServiceConnection()
66 public void onServiceDisconnected(ComponentName name
)
72 public void onServiceConnected(ComponentName name
, IBinder service
)
74 mService
= ((VpnStateService
.LocalBinder
)service
).getService();
80 public void onCreate(Bundle savedInstanceState
)
82 super.onCreate(savedInstanceState
);
84 if (savedInstanceState
!= null)
86 mWaitingForResult
= savedInstanceState
.getBoolean(WAITING_FOR_RESULT
, false);
88 this.bindService(new Intent(this, VpnStateService
.class),
89 mServiceConnection
, Service
.BIND_AUTO_CREATE
);
93 protected void onSaveInstanceState(Bundle outState
)
95 super.onSaveInstanceState(outState
);
96 outState
.putBoolean(WAITING_FOR_RESULT
, mWaitingForResult
);
100 protected void onDestroy()
103 if (mService
!= null)
105 this.unbindService(mServiceConnection
);
110 * Due to launchMode=singleTop this is called if the Activity already exists
113 protected void onNewIntent(Intent intent
)
115 super.onNewIntent(intent
);
117 /* store this intent in case the service is not yet connected or the activity is restarted */
120 if (mService
!= null)
127 * Prepare the VpnService. If this succeeds the current VPN profile is
130 * @param profileInfo a bundle containing the information about the profile to be started
132 protected void prepareVpnService(Bundle profileInfo
)
136 if (mWaitingForResult
)
138 mProfileInfo
= profileInfo
;
144 intent
= VpnService
.prepare(this);
146 catch (IllegalStateException ex
)
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
);
152 catch (NullPointerException ex
)
154 /* not sure when this happens exactly, but apparently it does */
155 VpnNotSupportedError
.showWithMessage(this, R
.string
.vpn_not_supported
);
158 /* store profile info until the user grants us permission */
159 mProfileInfo
= profileInfo
;
164 mWaitingForResult
= true;
165 startActivityForResult(intent
, PREPARE_VPN_SERVICE
);
167 catch (ActivityNotFoundException ex
)
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
);
174 mWaitingForResult
= false;
178 { /* user already granted permission to use VpnService */
179 onActivityResult(PREPARE_VPN_SERVICE
, RESULT_OK
, null);
184 protected void onActivityResult(int requestCode
, int resultCode
, Intent data
)
188 case PREPARE_VPN_SERVICE
:
189 mWaitingForResult
= false;
190 if (resultCode
== RESULT_OK
&& mProfileInfo
!= null)
192 if (mService
!= null)
194 mService
.connect(mProfileInfo
, true);
199 { /* this happens if the always-on VPN feature is activated by a different app or the user declined */
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 */
206 VpnNotSupportedError
.showWithMessage(this, R
.string
.vpn_not_supported_no_permission
);
210 super.onActivityResult(requestCode
, resultCode
, data
);
215 * Check if we are currently connected to a VPN connection
217 * @return true if currently connected
219 private boolean isConnected()
221 if (mService
== null)
225 if (mService
.getErrorState() != VpnStateService
.ErrorState
.NO_ERROR
)
226 { /* allow reconnecting (even to a different profile) without confirmation if there is an error */
229 return (mService
.getState() == State
.CONNECTED
|| mService
.getState() == State
.CONNECTING
);
233 * Start the given VPN profile
235 * @param profile VPN profile
237 public void startVpnProfile(VpnProfile profile
)
239 Bundle profileInfo
= new Bundle();
240 profileInfo
.putString(VpnProfileDataSource
.KEY_UUID
, profile
.getUUID().toString());
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());
246 removeFragmentByTag(DIALOG_TAG
);
250 profileInfo
.putBoolean(PROFILE_RECONNECT
, mService
.getProfile().getUUID().equals(profile
.getUUID()));
252 ConfirmationDialog dialog
= new ConfirmationDialog();
253 dialog
.setArguments(profileInfo
);
254 dialog
.show(this.getSupportFragmentManager(), DIALOG_TAG
);
257 startVpnProfile(profileInfo
);
261 * Start the given VPN profile asking the user for a password if required.
263 * @param profileInfo data about the profile
265 private void startVpnProfile(Bundle profileInfo
)
267 if (profileInfo
.getBoolean(PROFILE_REQUIRES_PASSWORD
) &&
268 profileInfo
.getString(VpnProfileDataSource
.KEY_PASSWORD
) == null)
270 LoginDialog login
= new LoginDialog();
271 login
.setArguments(profileInfo
);
272 login
.show(getSupportFragmentManager(), DIALOG_TAG
);
275 prepareVpnService(profileInfo
);
279 * Start the VPN profile referred to by the given intent. Displays an error
280 * if the profile doesn't exist.
282 * @param intent Intent that caused us to start this
284 private void startVpnProfile(Intent intent
)
286 VpnProfile profile
= null;
288 VpnProfileDataSource dataSource
= new VpnProfileDataSource(this);
290 String profileUUID
= intent
.getStringExtra(EXTRA_VPN_PROFILE_ID
);
291 if (profileUUID
!= null)
293 profile
= dataSource
.getVpnProfile(profileUUID
);
297 long profileId
= intent
.getLongExtra(EXTRA_VPN_PROFILE_ID
, 0);
300 profile
= dataSource
.getVpnProfile(profileId
);
307 startVpnProfile(profile
);
311 Toast
.makeText(this, R
.string
.profile_not_found
, Toast
.LENGTH_LONG
).show();
317 * Disconnect the current connection, if any (silently ignored if there is no connection).
319 private void disconnect()
321 removeFragmentByTag(DIALOG_TAG
);
323 if (mService
!= null)
325 if (mService
.getState() == State
.CONNECTED
|| mService
.getState() == State
.CONNECTING
)
327 Bundle args
= new Bundle();
328 args
.putBoolean(PROFILE_DISCONNECT
, true);
330 ConfirmationDialog dialog
= new ConfirmationDialog();
331 dialog
.setArguments(args
);
332 dialog
.show(this.getSupportFragmentManager(), DIALOG_TAG
);
342 * Handle the Intent of this Activity depending on its action
344 private void handleIntent()
346 if (START_PROFILE
.equals(getIntent().getAction()))
348 startVpnProfile(getIntent());
350 else if (DISCONNECT
.equals(getIntent().getAction()))
357 * Dismiss dialog if shown
359 public void removeFragmentByTag(String tag
)
361 FragmentManager fm
= getSupportFragmentManager();
362 Fragment login
= fm
.findFragmentByTag(tag
);
365 FragmentTransaction ft
= fm
.beginTransaction();
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.
375 public static class ConfirmationDialog
extends AppCompatDialogFragment
378 public Dialog
onCreateDialog(Bundle savedInstanceState
)
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
;
386 if (profileInfo
.getBoolean(PROFILE_RECONNECT
))
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
;
393 else if (profileInfo
.getBoolean(PROFILE_DISCONNECT
))
395 title
= R
.string
.disconnect_question
;
396 message
= R
.string
.disconnect_active_connection
;
397 button
= R
.string
.disconnect
;
400 DialogInterface
.OnClickListener connectListener
= new DialogInterface
.OnClickListener()
403 public void onClick(DialogInterface dialog
, int which
)
405 VpnProfileControlActivity activity
= (VpnProfileControlActivity
)getActivity();
406 activity
.startVpnProfile(profileInfo
);
409 DialogInterface
.OnClickListener disconnectListener
= new DialogInterface
.OnClickListener()
412 public void onClick(DialogInterface dialog
, int which
)
414 VpnProfileControlActivity activity
= (VpnProfileControlActivity
)getActivity();
415 if (activity
.mService
!= null)
417 activity
.mService
.disconnect();
419 getActivity().finish();
422 DialogInterface
.OnClickListener cancelListener
= new DialogInterface
.OnClickListener()
425 public void onClick(DialogInterface dialog
, int which
)
427 getActivity().finish();
431 AlertDialog
.Builder builder
= new AlertDialog
.Builder(getActivity())
433 .setTitle(String
.format(getString(title
), profileInfo
.getString(PROFILE_NAME
)))
434 .setMessage(message
);
436 if (profileInfo
.getBoolean(PROFILE_DISCONNECT
))
438 builder
.setPositiveButton(button
, disconnectListener
);
442 builder
.setPositiveButton(button
, connectListener
);
445 if (profileInfo
.getBoolean(PROFILE_RECONNECT
))
447 builder
.setNegativeButton(R
.string
.disconnect
, disconnectListener
);
448 builder
.setNeutralButton(android
.R
.string
.cancel
, cancelListener
);
452 builder
.setNegativeButton(android
.R
.string
.cancel
, cancelListener
);
454 return builder
.create();
458 public void onCancel(DialogInterface dialog
)
460 getActivity().finish();
465 * Class that displays a login dialog and initiates the selected VPN
466 * profile if the user confirms the dialog.
468 public static class LoginDialog
extends AppCompatDialogFragment
471 public Dialog
onCreateDialog(Bundle savedInstanceState
)
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
);
480 AlertDialog
.Builder adb
= new AlertDialog
.Builder(getActivity());
482 adb
.setTitle(getString(R
.string
.login_title
));
483 adb
.setPositiveButton(R
.string
.login_confirm
, new DialogInterface
.OnClickListener()
486 public void onClick(DialogInterface dialog
, int whichButton
)
488 VpnProfileControlActivity activity
= (VpnProfileControlActivity
)getActivity();
489 profileInfo
.putString(VpnProfileDataSource
.KEY_PASSWORD
, password
.getText().toString().trim());
490 activity
.prepareVpnService(profileInfo
);
493 adb
.setNegativeButton(android
.R
.string
.cancel
, new DialogInterface
.OnClickListener()
496 public void onClick(DialogInterface dialog
, int which
)
498 getActivity().finish();
505 public void onCancel(DialogInterface dialog
)
507 getActivity().finish();
512 * Class representing an error message which is displayed if VpnService is
513 * not supported on the current device.
515 public static class VpnNotSupportedError
extends AppCompatDialogFragment
517 static final String ERROR_MESSAGE_ID
= "org.strongswan.android.VpnNotSupportedError.MessageId";
519 public static void showWithMessage(AppCompatActivity activity
, int messageId
)
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
);
529 public Dialog
onCreateDialog(Bundle savedInstanceState
)
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()
540 public void onClick(DialogInterface dialog
, int id
)
542 getActivity().finish();
548 public void onCancel(DialogInterface dialog
)
550 getActivity().finish();