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 12:02:38 +00:00
|
|
|
import org.strongswan.android.R;
|
2012-08-07 12:11:27 +00:00
|
|
|
import org.strongswan.android.data.VpnProfile;
|
|
|
|
import org.strongswan.android.data.VpnProfileDataSource;
|
|
|
|
import org.strongswan.android.logic.CharonVpnService;
|
2012-07-18 11:43:34 +00:00
|
|
|
import org.strongswan.android.logic.TrustedCertificateManager;
|
2012-08-07 12:11:27 +00:00
|
|
|
import org.strongswan.android.ui.VpnProfileListFragment.OnVpnProfileSelectedListener;
|
2012-07-18 11:43:34 +00:00
|
|
|
|
2012-07-17 17:40:03 +00:00
|
|
|
import android.app.ActionBar;
|
2012-02-17 15:59:23 +00:00
|
|
|
import android.app.Activity;
|
2012-08-07 16:44:06 +00:00
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.app.AlertDialog.Builder;
|
|
|
|
import android.app.Dialog;
|
|
|
|
import android.app.DialogFragment;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.DialogInterface;
|
2012-02-17 16:16:07 +00:00
|
|
|
import android.content.Intent;
|
|
|
|
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-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-02-17 15:59:23 +00:00
|
|
|
|
2012-08-07 12:11:27 +00:00
|
|
|
public class MainActivity extends Activity implements OnVpnProfileSelectedListener
|
2012-02-22 14:36:51 +00:00
|
|
|
{
|
2012-08-07 12:11:27 +00:00
|
|
|
private static final int PREPARE_VPN_SERVICE = 0;
|
|
|
|
private VpnProfile activeProfile;
|
|
|
|
|
2012-02-17 16:16:07 +00:00
|
|
|
@Override
|
2012-02-22 14:36:51 +00:00
|
|
|
public void onCreate(Bundle savedInstanceState)
|
|
|
|
{
|
2012-02-17 16:16:07 +00:00
|
|
|
super.onCreate(savedInstanceState);
|
2012-08-07 12:00:16 +00:00
|
|
|
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
|
2012-02-17 16:16:07 +00:00
|
|
|
setContentView(R.layout.main);
|
2012-07-17 17:40:03 +00:00
|
|
|
|
|
|
|
ActionBar bar = getActionBar();
|
|
|
|
bar.setDisplayShowTitleEnabled(false);
|
2012-07-18 11:43:34 +00:00
|
|
|
|
2012-08-07 15:52:10 +00:00
|
|
|
/* load CA certificates in a background task */
|
|
|
|
new CertificateLoadTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, false);
|
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())
|
|
|
|
{
|
|
|
|
case R.id.menu_reload_certs:
|
2012-08-07 15:52:10 +00:00
|
|
|
new CertificateLoadTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, true);
|
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.
|
|
|
|
*/
|
2012-08-07 12:02:38 +00:00
|
|
|
protected void prepareVpnService()
|
2012-02-22 14:36:51 +00:00
|
|
|
{
|
2012-02-17 16:16:07 +00:00
|
|
|
Intent intent = VpnService.prepare(this);
|
2012-02-22 14:36:51 +00:00
|
|
|
if (intent != null)
|
|
|
|
{
|
2012-08-07 12:11:27 +00:00
|
|
|
startActivityForResult(intent, PREPARE_VPN_SERVICE);
|
2012-02-22 14:36:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
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:
|
|
|
|
if (resultCode == RESULT_OK && activeProfile != null)
|
|
|
|
{
|
|
|
|
Intent intent = new Intent(this, CharonVpnService.class);
|
|
|
|
intent.putExtra(VpnProfileDataSource.KEY_ID, activeProfile.getId());
|
2012-08-07 16:44:06 +00:00
|
|
|
/* submit the password as the profile might not store one */
|
|
|
|
intent.putExtra(VpnProfileDataSource.KEY_PASSWORD, activeProfile.getPassword());
|
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)
|
|
|
|
{
|
|
|
|
activeProfile = profile;
|
2012-08-07 16:44:06 +00:00
|
|
|
if (activeProfile.getPassword() == null)
|
|
|
|
{
|
|
|
|
new LoginDialog().show(getFragmentManager(), "LoginDialog");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prepareVpnService();
|
|
|
|
}
|
2012-08-07 12:11:27 +00:00
|
|
|
}
|
2012-08-07 15:52:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class that loads or reloads the cached CA certificates.
|
|
|
|
*/
|
|
|
|
private class CertificateLoadTask extends AsyncTask<Boolean, Void, TrustedCertificateManager>
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
protected void onPreExecute()
|
|
|
|
{
|
|
|
|
setProgressBarIndeterminateVisibility(true);
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
protected TrustedCertificateManager doInBackground(Boolean... params)
|
|
|
|
{
|
|
|
|
if (params.length > 0 && params[0])
|
|
|
|
{ /* force a reload of the certificates */
|
|
|
|
return TrustedCertificateManager.getInstance().reload();
|
|
|
|
}
|
|
|
|
return TrustedCertificateManager.getInstance().load();
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(TrustedCertificateManager result)
|
|
|
|
{
|
|
|
|
setProgressBarIndeterminateVisibility(false);
|
|
|
|
}
|
|
|
|
}
|
2012-08-07 16:44:06 +00:00
|
|
|
|
|
|
|
private class LoginDialog extends DialogFragment
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public Dialog onCreateDialog(Bundle savedInstanceState)
|
|
|
|
{
|
|
|
|
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
|
|
|
View view = inflater.inflate(R.layout.login_dialog, null);
|
|
|
|
EditText username = (EditText)view.findViewById(R.id.username);
|
|
|
|
username.setText(activeProfile.getUsername());
|
|
|
|
final EditText password = (EditText)view.findViewById(R.id.password);
|
|
|
|
|
|
|
|
Builder adb = new AlertDialog.Builder(MainActivity.this);
|
|
|
|
adb.setView(view);
|
|
|
|
adb.setTitle(getString(R.string.login_title));
|
|
|
|
adb.setPositiveButton(R.string.login_confirm, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int whichButton)
|
|
|
|
{
|
2012-08-07 16:45:03 +00:00
|
|
|
/* let's work on a clone of the profile when updating the password */
|
|
|
|
activeProfile = activeProfile.clone();
|
2012-08-07 16:44:06 +00:00
|
|
|
activeProfile.setPassword(password.getText().toString().trim());
|
|
|
|
prepareVpnService();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
adb.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which)
|
|
|
|
{
|
|
|
|
dismiss();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return adb.create();
|
|
|
|
}
|
|
|
|
}
|
2012-02-22 14:36:51 +00:00
|
|
|
}
|