2012-07-17 17:40:03 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 Tobias Brunner
|
|
|
|
* Copyright (C) 2012 Giuliano Grassi
|
|
|
|
* Copyright (C) 2012 Ralf Sager
|
|
|
|
* Hochschule fuer Technik Rapperswil
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
2012-07-14 15:12:07 +00:00
|
|
|
package org.strongswan.android.ui;
|
2012-02-17 15:59:23 +00:00
|
|
|
|
2012-08-07 16:44:06 +00:00
|
|
|
import android.app.Dialog;
|
2012-11-20 14:47:54 +00:00
|
|
|
import android.app.Fragment;
|
|
|
|
import android.app.FragmentManager;
|
|
|
|
import android.app.FragmentTransaction;
|
2012-11-20 14:37:04 +00:00
|
|
|
import android.app.Service;
|
2012-08-15 08:51:30 +00:00
|
|
|
import android.content.ActivityNotFoundException;
|
2012-11-20 14:37:04 +00:00
|
|
|
import android.content.ComponentName;
|
2012-08-07 16:44:06 +00:00
|
|
|
import android.content.DialogInterface;
|
2012-02-17 16:16:07 +00:00
|
|
|
import android.content.Intent;
|
2012-11-20 14:37:04 +00:00
|
|
|
import android.content.ServiceConnection;
|
2012-02-17 16:16:07 +00:00
|
|
|
import android.net.VpnService;
|
2012-08-07 15:52:10 +00:00
|
|
|
import android.os.AsyncTask;
|
2012-02-17 15:59:23 +00:00
|
|
|
import android.os.Bundle;
|
2012-11-20 14:37:04 +00:00
|
|
|
import android.os.IBinder;
|
2015-11-25 16:05:56 +00:00
|
|
|
import android.support.v7.app.ActionBar;
|
2016-04-15 15:36:31 +00:00
|
|
|
import android.support.v7.app.AlertDialog;
|
2015-11-25 16:05:56 +00:00
|
|
|
import android.support.v7.app.AppCompatActivity;
|
2016-04-15 15:36:31 +00:00
|
|
|
import android.support.v7.app.AppCompatDialogFragment;
|
2012-08-07 16:44:06 +00:00
|
|
|
import android.view.LayoutInflater;
|
2012-08-07 12:02:38 +00:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
2012-08-07 16:44:06 +00:00
|
|
|
import android.view.View;
|
2012-08-07 12:00:16 +00:00
|
|
|
import android.view.Window;
|
2012-08-07 16:44:06 +00:00
|
|
|
import android.widget.EditText;
|
2012-11-07 15:06:30 +00:00
|
|
|
import android.widget.Toast;
|
2012-02-17 15:59:23 +00:00
|
|
|
|
2015-11-25 16:05:56 +00:00
|
|
|
import org.strongswan.android.R;
|
|
|
|
import org.strongswan.android.data.VpnProfile;
|
|
|
|
import org.strongswan.android.data.VpnProfileDataSource;
|
|
|
|
import org.strongswan.android.data.VpnType.VpnTypeFeature;
|
|
|
|
import org.strongswan.android.logic.CharonVpnService;
|
|
|
|
import org.strongswan.android.logic.TrustedCertificateManager;
|
|
|
|
import org.strongswan.android.logic.VpnStateService;
|
|
|
|
import org.strongswan.android.logic.VpnStateService.State;
|
|
|
|
import org.strongswan.android.ui.VpnProfileListFragment.OnVpnProfileSelectedListener;
|
|
|
|
|
|
|
|
public class MainActivity extends AppCompatActivity implements OnVpnProfileSelectedListener
|
2012-02-22 14:36:51 +00:00
|
|
|
{
|
2012-08-10 14:37:39 +00:00
|
|
|
public static final String CONTACT_EMAIL = "android@strongswan.org";
|
2012-11-07 15:06:30 +00:00
|
|
|
public static final String START_PROFILE = "org.strongswan.android.action.START_PROFILE";
|
|
|
|
public static final String EXTRA_VPN_PROFILE_ID = "org.strongswan.android.VPN_PROFILE_ID";
|
2015-11-25 16:05:56 +00:00
|
|
|
/**
|
|
|
|
* Use "bring your own device" (BYOD) features
|
|
|
|
*/
|
2012-10-11 11:50:18 +00:00
|
|
|
public static final boolean USE_BYOD = true;
|
2012-08-07 12:11:27 +00:00
|
|
|
private static final int PREPARE_VPN_SERVICE = 0;
|
2012-11-20 14:37:04 +00:00
|
|
|
private static final String PROFILE_NAME = "org.strongswan.android.MainActivity.PROFILE_NAME";
|
|
|
|
private static final String PROFILE_REQUIRES_PASSWORD = "org.strongswan.android.MainActivity.REQUIRES_PASSWORD";
|
|
|
|
private static final String PROFILE_RECONNECT = "org.strongswan.android.MainActivity.RECONNECT";
|
2012-11-20 15:06:39 +00:00
|
|
|
private static final String DIALOG_TAG = "Dialog";
|
2012-08-15 08:51:30 +00:00
|
|
|
|
2012-08-24 12:55:31 +00:00
|
|
|
private Bundle mProfileInfo;
|
2012-11-20 14:37:04 +00:00
|
|
|
private VpnStateService mService;
|
2015-11-25 16:05:56 +00:00
|
|
|
private final ServiceConnection mServiceConnection = new ServiceConnection()
|
|
|
|
{
|
2012-11-20 14:37:04 +00:00
|
|
|
@Override
|
|
|
|
public void onServiceDisconnected(ComponentName name)
|
|
|
|
{
|
|
|
|
mService = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onServiceConnected(ComponentName name, IBinder service)
|
|
|
|
{
|
|
|
|
mService = ((VpnStateService.LocalBinder)service).getService();
|
|
|
|
|
|
|
|
if (START_PROFILE.equals(getIntent().getAction()))
|
|
|
|
{
|
|
|
|
startVpnProfile(getIntent());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2012-08-07 12:11:27 +00:00
|
|
|
|
2012-02-17 16:16:07 +00:00
|
|
|
@Override
|
2012-02-22 14:36:51 +00:00
|
|
|
public void onCreate(Bundle savedInstanceState)
|
|
|
|
{
|
2012-08-07 12:00:16 +00:00
|
|
|
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
|
2015-11-25 16:05:56 +00:00
|
|
|
super.onCreate(savedInstanceState);
|
2012-02-17 16:16:07 +00:00
|
|
|
setContentView(R.layout.main);
|
2012-07-17 17:40:03 +00:00
|
|
|
|
2015-11-25 16:05:56 +00:00
|
|
|
ActionBar bar = getSupportActionBar();
|
|
|
|
bar.setDisplayShowHomeEnabled(true);
|
|
|
|
bar.setDisplayShowTitleEnabled(false);
|
|
|
|
bar.setIcon(R.drawable.ic_launcher);
|
|
|
|
|
2012-11-20 14:37:04 +00:00
|
|
|
this.bindService(new Intent(this, VpnStateService.class),
|
|
|
|
mServiceConnection, Service.BIND_AUTO_CREATE);
|
|
|
|
|
2012-08-07 15:52:10 +00:00
|
|
|
/* load CA certificates in a background task */
|
2014-05-30 15:46:15 +00:00
|
|
|
new LoadCertificatesTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2012-11-20 14:37:04 +00:00
|
|
|
}
|
2012-11-07 15:06:30 +00:00
|
|
|
|
2012-11-20 14:37:04 +00:00
|
|
|
@Override
|
|
|
|
protected void onDestroy()
|
|
|
|
{
|
|
|
|
super.onDestroy();
|
|
|
|
if (mService != null)
|
2012-11-07 15:06:30 +00:00
|
|
|
{
|
2012-11-20 14:37:04 +00:00
|
|
|
this.unbindService(mServiceConnection);
|
2012-11-07 15:06:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Due to launchMode=singleTop this is called if the Activity already exists
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
protected void onNewIntent(Intent intent)
|
|
|
|
{
|
|
|
|
super.onNewIntent(intent);
|
|
|
|
|
|
|
|
if (START_PROFILE.equals(intent.getAction()))
|
|
|
|
{
|
|
|
|
startVpnProfile(intent);
|
|
|
|
}
|
2012-02-17 16:16:07 +00:00
|
|
|
}
|
|
|
|
|
2012-08-07 12:02:38 +00:00
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu)
|
|
|
|
{
|
2012-08-10 07:58:56 +00:00
|
|
|
getMenuInflater().inflate(R.menu.main, menu);
|
2012-08-07 12:02:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item)
|
|
|
|
{
|
|
|
|
switch (item.getItemId())
|
|
|
|
{
|
2014-05-30 15:46:15 +00:00
|
|
|
case R.id.menu_manage_certs:
|
|
|
|
Intent certIntent = new Intent(this, TrustedCertificatesActivity.class);
|
|
|
|
startActivity(certIntent);
|
2012-08-07 12:02:38 +00:00
|
|
|
return true;
|
2012-08-10 07:58:56 +00:00
|
|
|
case R.id.menu_show_log:
|
|
|
|
Intent logIntent = new Intent(this, LogActivity.class);
|
|
|
|
startActivity(logIntent);
|
|
|
|
return true;
|
2012-08-07 12:02:38 +00:00
|
|
|
default:
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-07 12:11:27 +00:00
|
|
|
/**
|
|
|
|
* Prepare the VpnService. If this succeeds the current VPN profile is
|
|
|
|
* started.
|
2015-11-25 16:05:56 +00:00
|
|
|
*
|
2012-08-24 12:55:31 +00:00
|
|
|
* @param profileInfo a bundle containing the information about the profile to be started
|
2012-08-07 12:11:27 +00:00
|
|
|
*/
|
2012-08-24 12:55:31 +00:00
|
|
|
protected void prepareVpnService(Bundle profileInfo)
|
2012-02-22 14:36:51 +00:00
|
|
|
{
|
2012-11-15 17:52:18 +00:00
|
|
|
Intent intent;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
intent = VpnService.prepare(this);
|
|
|
|
}
|
|
|
|
catch (IllegalStateException ex)
|
|
|
|
{
|
|
|
|
/* this happens if the always-on VPN feature (Android 4.2+) is activated */
|
|
|
|
VpnNotSupportedError.showWithMessage(this, R.string.vpn_not_supported_during_lockdown);
|
|
|
|
return;
|
|
|
|
}
|
2012-08-24 12:55:31 +00:00
|
|
|
/* store profile info until the user grants us permission */
|
|
|
|
mProfileInfo = profileInfo;
|
2012-02-22 14:36:51 +00:00
|
|
|
if (intent != null)
|
|
|
|
{
|
2012-08-15 08:51:30 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
startActivityForResult(intent, PREPARE_VPN_SERVICE);
|
|
|
|
}
|
|
|
|
catch (ActivityNotFoundException ex)
|
|
|
|
{
|
|
|
|
/* it seems some devices, even though they come with Android 4,
|
|
|
|
* don't have the VPN components built into the system image.
|
|
|
|
* com.android.vpndialogs/com.android.vpndialogs.ConfirmDialog
|
|
|
|
* will not be found then */
|
2012-11-15 17:51:24 +00:00
|
|
|
VpnNotSupportedError.showWithMessage(this, R.string.vpn_not_supported);
|
2012-08-15 08:51:30 +00:00
|
|
|
}
|
2012-02-22 14:36:51 +00:00
|
|
|
}
|
|
|
|
else
|
2012-08-24 12:55:31 +00:00
|
|
|
{ /* user already granted permission to use VpnService */
|
2012-08-07 12:11:27 +00:00
|
|
|
onActivityResult(PREPARE_VPN_SERVICE, RESULT_OK, null);
|
2012-02-17 16:16:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-02-22 14:36:51 +00:00
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data)
|
|
|
|
{
|
2012-08-07 12:11:27 +00:00
|
|
|
switch (requestCode)
|
2012-02-22 14:36:51 +00:00
|
|
|
{
|
2012-08-07 12:11:27 +00:00
|
|
|
case PREPARE_VPN_SERVICE:
|
2012-08-24 12:55:31 +00:00
|
|
|
if (resultCode == RESULT_OK && mProfileInfo != null)
|
2012-08-07 12:11:27 +00:00
|
|
|
{
|
|
|
|
Intent intent = new Intent(this, CharonVpnService.class);
|
2012-08-24 12:55:31 +00:00
|
|
|
intent.putExtras(mProfileInfo);
|
2012-08-07 12:11:27 +00:00
|
|
|
this.startService(intent);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
2012-02-17 16:16:07 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-07 12:11:27 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onVpnProfileSelected(VpnProfile profile)
|
|
|
|
{
|
2012-08-24 12:55:31 +00:00
|
|
|
Bundle profileInfo = new Bundle();
|
|
|
|
profileInfo.putLong(VpnProfileDataSource.KEY_ID, profile.getId());
|
|
|
|
profileInfo.putString(VpnProfileDataSource.KEY_USERNAME, profile.getUsername());
|
2012-11-20 14:37:04 +00:00
|
|
|
profileInfo.putString(VpnProfileDataSource.KEY_PASSWORD, profile.getPassword());
|
2014-09-10 09:25:03 +00:00
|
|
|
profileInfo.putBoolean(PROFILE_REQUIRES_PASSWORD, profile.getVpnType().has(VpnTypeFeature.USER_PASS));
|
2012-11-20 14:37:04 +00:00
|
|
|
profileInfo.putString(PROFILE_NAME, profile.getName());
|
|
|
|
|
2012-11-20 15:06:39 +00:00
|
|
|
removeFragmentByTag(DIALOG_TAG);
|
2012-11-20 14:47:54 +00:00
|
|
|
|
2012-11-20 14:37:04 +00:00
|
|
|
if (mService != null && mService.getState() == State.CONNECTED)
|
|
|
|
{
|
|
|
|
profileInfo.putBoolean(PROFILE_RECONNECT, mService.getProfile().getId() == profile.getId());
|
|
|
|
|
|
|
|
ConfirmationDialog dialog = new ConfirmationDialog();
|
|
|
|
dialog.setArguments(profileInfo);
|
2016-04-15 15:36:31 +00:00
|
|
|
dialog.show(this.getSupportFragmentManager(), DIALOG_TAG);
|
2012-11-20 14:37:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
startVpnProfile(profileInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the given VPN profile asking the user for a password if required.
|
2015-11-25 16:05:56 +00:00
|
|
|
*
|
2012-11-20 14:37:04 +00:00
|
|
|
* @param profileInfo data about the profile
|
|
|
|
*/
|
|
|
|
private void startVpnProfile(Bundle profileInfo)
|
|
|
|
{
|
|
|
|
if (profileInfo.getBoolean(PROFILE_REQUIRES_PASSWORD) &&
|
|
|
|
profileInfo.getString(VpnProfileDataSource.KEY_PASSWORD) == null)
|
2012-08-07 16:44:06 +00:00
|
|
|
{
|
2012-08-24 12:55:31 +00:00
|
|
|
LoginDialog login = new LoginDialog();
|
|
|
|
login.setArguments(profileInfo);
|
2016-04-15 15:36:31 +00:00
|
|
|
login.show(getSupportFragmentManager(), DIALOG_TAG);
|
2012-11-20 14:37:04 +00:00
|
|
|
return;
|
2012-08-07 16:44:06 +00:00
|
|
|
}
|
2012-11-20 14:37:04 +00:00
|
|
|
prepareVpnService(profileInfo);
|
2012-08-07 12:11:27 +00:00
|
|
|
}
|
2012-08-07 15:52:10 +00:00
|
|
|
|
2012-11-07 15:06:30 +00:00
|
|
|
/**
|
|
|
|
* Start the VPN profile referred to by the given intent. Displays an error
|
|
|
|
* if the profile doesn't exist.
|
2015-11-25 16:05:56 +00:00
|
|
|
*
|
2012-11-07 15:06:30 +00:00
|
|
|
* @param intent Intent that caused us to start this
|
|
|
|
*/
|
|
|
|
private void startVpnProfile(Intent intent)
|
|
|
|
{
|
|
|
|
long profileId = intent.getLongExtra(EXTRA_VPN_PROFILE_ID, 0);
|
|
|
|
if (profileId <= 0)
|
|
|
|
{ /* invalid invocation */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
VpnProfileDataSource dataSource = new VpnProfileDataSource(this);
|
|
|
|
dataSource.open();
|
|
|
|
VpnProfile profile = dataSource.getVpnProfile(profileId);
|
|
|
|
dataSource.close();
|
|
|
|
|
|
|
|
if (profile != null)
|
|
|
|
{
|
|
|
|
onVpnProfileSelected(profile);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Toast.makeText(this, R.string.profile_not_found, Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-07 15:52:10 +00:00
|
|
|
/**
|
2014-05-30 15:46:15 +00:00
|
|
|
* Class that loads the cached CA certificates.
|
2012-08-07 15:52:10 +00:00
|
|
|
*/
|
2014-05-30 15:46:15 +00:00
|
|
|
private class LoadCertificatesTask extends AsyncTask<Void, Void, TrustedCertificateManager>
|
2012-08-07 15:52:10 +00:00
|
|
|
{
|
|
|
|
@Override
|
|
|
|
protected void onPreExecute()
|
|
|
|
{
|
|
|
|
setProgressBarIndeterminateVisibility(true);
|
|
|
|
}
|
2015-11-25 16:05:56 +00:00
|
|
|
|
2012-08-07 15:52:10 +00:00
|
|
|
@Override
|
2014-05-30 15:46:15 +00:00
|
|
|
protected TrustedCertificateManager doInBackground(Void... params)
|
2012-08-07 15:52:10 +00:00
|
|
|
{
|
|
|
|
return TrustedCertificateManager.getInstance().load();
|
|
|
|
}
|
2015-11-25 16:05:56 +00:00
|
|
|
|
2012-08-07 15:52:10 +00:00
|
|
|
@Override
|
|
|
|
protected void onPostExecute(TrustedCertificateManager result)
|
|
|
|
{
|
|
|
|
setProgressBarIndeterminateVisibility(false);
|
|
|
|
}
|
|
|
|
}
|
2012-08-07 16:44:06 +00:00
|
|
|
|
2012-11-20 14:47:54 +00:00
|
|
|
/**
|
|
|
|
* Dismiss dialog if shown
|
|
|
|
*/
|
|
|
|
public void removeFragmentByTag(String tag)
|
|
|
|
{
|
|
|
|
FragmentManager fm = getFragmentManager();
|
|
|
|
Fragment login = fm.findFragmentByTag(tag);
|
|
|
|
if (login != null)
|
|
|
|
{
|
|
|
|
FragmentTransaction ft = fm.beginTransaction();
|
|
|
|
ft.remove(login);
|
|
|
|
ft.commit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-20 14:37:04 +00:00
|
|
|
/**
|
|
|
|
* Class that displays a confirmation dialog if a VPN profile is already connected
|
|
|
|
* and then initiates the selected VPN profile if the user confirms the dialog.
|
|
|
|
*/
|
2016-04-15 15:36:31 +00:00
|
|
|
public static class ConfirmationDialog extends AppCompatDialogFragment
|
2012-11-20 14:37:04 +00:00
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public Dialog onCreateDialog(Bundle savedInstanceState)
|
|
|
|
{
|
|
|
|
final Bundle profileInfo = getArguments();
|
|
|
|
int icon = android.R.drawable.ic_dialog_alert;
|
|
|
|
int title = R.string.connect_profile_question;
|
|
|
|
int message = R.string.replaces_active_connection;
|
|
|
|
int button = R.string.connect;
|
|
|
|
|
|
|
|
if (profileInfo.getBoolean(PROFILE_RECONNECT))
|
|
|
|
{
|
|
|
|
icon = android.R.drawable.ic_dialog_info;
|
|
|
|
title = R.string.vpn_connected;
|
|
|
|
message = R.string.vpn_profile_connected;
|
|
|
|
button = R.string.reconnect;
|
|
|
|
}
|
|
|
|
|
2016-04-15 15:36:31 +00:00
|
|
|
return new AlertDialog.Builder(getActivity())
|
2012-11-20 14:37:04 +00:00
|
|
|
.setIcon(icon)
|
|
|
|
.setTitle(String.format(getString(title), profileInfo.getString(PROFILE_NAME)))
|
|
|
|
.setMessage(message)
|
2015-11-25 16:05:56 +00:00
|
|
|
.setPositiveButton(button, new DialogInterface.OnClickListener()
|
|
|
|
{
|
2012-11-20 14:37:04 +00:00
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int whichButton)
|
|
|
|
{
|
|
|
|
MainActivity activity = (MainActivity)getActivity();
|
|
|
|
activity.startVpnProfile(profileInfo);
|
|
|
|
}
|
|
|
|
})
|
2015-11-25 16:05:56 +00:00
|
|
|
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener()
|
|
|
|
{
|
2012-11-20 14:37:04 +00:00
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which)
|
|
|
|
{
|
|
|
|
dismiss();
|
|
|
|
}
|
|
|
|
}).create();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-24 12:55:31 +00:00
|
|
|
/**
|
|
|
|
* Class that displays a login dialog and initiates the selected VPN
|
|
|
|
* profile if the user confirms the dialog.
|
|
|
|
*/
|
2016-04-15 15:36:31 +00:00
|
|
|
public static class LoginDialog extends AppCompatDialogFragment
|
2012-08-07 16:44:06 +00:00
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public Dialog onCreateDialog(Bundle savedInstanceState)
|
|
|
|
{
|
2012-08-24 12:55:31 +00:00
|
|
|
final Bundle profileInfo = getArguments();
|
|
|
|
LayoutInflater inflater = getActivity().getLayoutInflater();
|
2012-08-07 16:44:06 +00:00
|
|
|
View view = inflater.inflate(R.layout.login_dialog, null);
|
|
|
|
EditText username = (EditText)view.findViewById(R.id.username);
|
2012-08-24 12:55:31 +00:00
|
|
|
username.setText(profileInfo.getString(VpnProfileDataSource.KEY_USERNAME));
|
2012-08-07 16:44:06 +00:00
|
|
|
final EditText password = (EditText)view.findViewById(R.id.password);
|
|
|
|
|
2016-04-15 15:36:31 +00:00
|
|
|
AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());
|
2012-08-07 16:44:06 +00:00
|
|
|
adb.setView(view);
|
|
|
|
adb.setTitle(getString(R.string.login_title));
|
2015-11-25 16:05:56 +00:00
|
|
|
adb.setPositiveButton(R.string.login_confirm, new DialogInterface.OnClickListener()
|
|
|
|
{
|
2012-08-07 16:44:06 +00:00
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int whichButton)
|
|
|
|
{
|
2012-08-24 12:55:31 +00:00
|
|
|
MainActivity activity = (MainActivity)getActivity();
|
|
|
|
profileInfo.putString(VpnProfileDataSource.KEY_PASSWORD, password.getText().toString().trim());
|
|
|
|
activity.prepareVpnService(profileInfo);
|
2012-08-07 16:44:06 +00:00
|
|
|
}
|
|
|
|
});
|
2015-11-25 16:05:56 +00:00
|
|
|
adb.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener()
|
|
|
|
{
|
2012-08-07 16:44:06 +00:00
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which)
|
|
|
|
{
|
|
|
|
dismiss();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return adb.create();
|
|
|
|
}
|
|
|
|
}
|
2012-08-24 13:06:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class representing an error message which is displayed if VpnService is
|
|
|
|
* not supported on the current device.
|
|
|
|
*/
|
2016-04-15 15:36:31 +00:00
|
|
|
public static class VpnNotSupportedError extends AppCompatDialogFragment
|
2012-08-24 13:06:39 +00:00
|
|
|
{
|
2012-11-15 17:51:24 +00:00
|
|
|
static final String ERROR_MESSAGE_ID = "org.strongswan.android.VpnNotSupportedError.MessageId";
|
|
|
|
|
2016-04-15 15:36:31 +00:00
|
|
|
public static void showWithMessage(AppCompatActivity activity, int messageId)
|
2012-11-15 17:51:24 +00:00
|
|
|
{
|
|
|
|
Bundle bundle = new Bundle();
|
|
|
|
bundle.putInt(ERROR_MESSAGE_ID, messageId);
|
|
|
|
VpnNotSupportedError dialog = new VpnNotSupportedError();
|
|
|
|
dialog.setArguments(bundle);
|
2016-04-15 15:36:31 +00:00
|
|
|
dialog.show(activity.getSupportFragmentManager(), DIALOG_TAG);
|
2012-11-15 17:51:24 +00:00
|
|
|
}
|
|
|
|
|
2012-08-24 13:06:39 +00:00
|
|
|
@Override
|
|
|
|
public Dialog onCreateDialog(Bundle savedInstanceState)
|
|
|
|
{
|
2012-11-15 17:51:24 +00:00
|
|
|
final Bundle arguments = getArguments();
|
|
|
|
final int messageId = arguments.getInt(ERROR_MESSAGE_ID);
|
2016-04-15 15:36:31 +00:00
|
|
|
return new AlertDialog.Builder(getActivity())
|
2012-08-24 13:06:39 +00:00
|
|
|
.setTitle(R.string.vpn_not_supported_title)
|
2012-11-15 17:51:24 +00:00
|
|
|
.setMessage(messageId)
|
2012-08-24 13:06:39 +00:00
|
|
|
.setCancelable(false)
|
2015-11-25 16:05:56 +00:00
|
|
|
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener()
|
|
|
|
{
|
2012-08-24 13:06:39 +00:00
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int id)
|
|
|
|
{
|
|
|
|
dialog.dismiss();
|
|
|
|
}
|
|
|
|
}).create();
|
|
|
|
}
|
|
|
|
}
|
2012-02-22 14:36:51 +00:00
|
|
|
}
|