2012-07-17 17:23:21 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.strongswan.android.ui;
|
|
|
|
|
|
|
|
import org.strongswan.android.R;
|
|
|
|
import org.strongswan.android.data.VpnProfile;
|
|
|
|
import org.strongswan.android.data.VpnProfileDataSource;
|
2012-08-07 16:03:51 +00:00
|
|
|
import org.strongswan.android.logic.TrustedCertificateManager;
|
2012-07-17 17:23:21 +00:00
|
|
|
|
|
|
|
import android.app.Activity;
|
2012-08-07 16:03:51 +00:00
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.content.DialogInterface;
|
2012-07-17 17:45:23 +00:00
|
|
|
import android.content.Intent;
|
2012-08-07 16:03:51 +00:00
|
|
|
import android.os.AsyncTask;
|
2012-07-17 17:23:21 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.util.Log;
|
2012-07-17 17:45:23 +00:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
2012-08-07 16:03:51 +00:00
|
|
|
import android.view.View;
|
2012-07-17 17:45:23 +00:00
|
|
|
import android.view.Window;
|
2012-08-07 16:03:51 +00:00
|
|
|
import android.widget.CheckBox;
|
|
|
|
import android.widget.CompoundButton;
|
|
|
|
import android.widget.CompoundButton.OnCheckedChangeListener;
|
2012-07-17 17:23:21 +00:00
|
|
|
import android.widget.EditText;
|
|
|
|
|
|
|
|
public class VpnProfileDetailActivity extends Activity
|
|
|
|
{
|
|
|
|
private VpnProfileDataSource mDataSource;
|
|
|
|
private Long mId;
|
|
|
|
private VpnProfile mProfile;
|
2012-08-07 16:03:51 +00:00
|
|
|
private boolean mCertsLoaded;
|
|
|
|
private String mCertAlias;
|
2012-07-17 17:23:21 +00:00
|
|
|
private EditText mName;
|
|
|
|
private EditText mGateway;
|
|
|
|
private EditText mUsername;
|
|
|
|
private EditText mPassword;
|
2012-08-07 16:03:51 +00:00
|
|
|
private CheckBox mCheckAuto;
|
2012-07-17 17:23:21 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState)
|
|
|
|
{
|
|
|
|
super.onCreate(savedInstanceState);
|
2012-08-07 16:03:51 +00:00
|
|
|
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
|
2012-07-17 17:23:21 +00:00
|
|
|
|
|
|
|
/* the title is set when we load the profile, if any */
|
|
|
|
getActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
|
|
|
|
mDataSource = new VpnProfileDataSource(this);
|
|
|
|
mDataSource.open();
|
|
|
|
|
|
|
|
setContentView(R.layout.profile_detail_view);
|
|
|
|
|
|
|
|
mName = (EditText)findViewById(R.id.name);
|
|
|
|
mPassword = (EditText)findViewById(R.id.password);
|
|
|
|
mGateway = (EditText)findViewById(R.id.gateway);
|
|
|
|
mUsername = (EditText)findViewById(R.id.username);
|
|
|
|
|
2012-08-07 16:03:51 +00:00
|
|
|
mCheckAuto = (CheckBox)findViewById(R.id.ca_auto);
|
|
|
|
|
|
|
|
mCheckAuto.setOnCheckedChangeListener(new OnCheckedChangeListener() {
|
|
|
|
@Override
|
|
|
|
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-07-17 17:23:21 +00:00
|
|
|
mId = savedInstanceState == null ? null : savedInstanceState.getLong(VpnProfileDataSource.KEY_ID);
|
|
|
|
if (mId == null)
|
|
|
|
{
|
|
|
|
Bundle extras = getIntent().getExtras();
|
|
|
|
mId = extras == null ? null : extras.getLong(VpnProfileDataSource.KEY_ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
loadProfileData();
|
2012-08-07 16:03:51 +00:00
|
|
|
|
|
|
|
new CertificateLoadTask().execute();
|
2012-07-17 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDestroy()
|
|
|
|
{
|
|
|
|
super.onDestroy();
|
|
|
|
mDataSource.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onSaveInstanceState(Bundle outState)
|
|
|
|
{
|
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
outState.putLong(VpnProfileDataSource.KEY_ID, mId);
|
|
|
|
}
|
|
|
|
|
2012-07-17 17:45:23 +00:00
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu)
|
|
|
|
{
|
|
|
|
MenuInflater inflater = getMenuInflater();
|
|
|
|
inflater.inflate(R.menu.profile_edit, menu);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item)
|
|
|
|
{
|
|
|
|
switch (item.getItemId())
|
|
|
|
{
|
|
|
|
case android.R.id.home:
|
|
|
|
case R.id.menu_cancel:
|
|
|
|
finish();
|
|
|
|
return true;
|
|
|
|
case R.id.menu_accept:
|
|
|
|
saveProfile();
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-07 16:03:51 +00:00
|
|
|
/**
|
|
|
|
* Show an alert in case the previously selected certificate is not found anymore
|
|
|
|
* or the user did not select a certificate in the spinner.
|
|
|
|
*/
|
|
|
|
private void showCertificateAlert()
|
|
|
|
{
|
|
|
|
AlertDialog.Builder adb = new AlertDialog.Builder(VpnProfileDetailActivity.this);
|
|
|
|
adb.setTitle(R.string.alert_text_nocertfound_title);
|
|
|
|
adb.setMessage(R.string.alert_text_nocertfound);
|
|
|
|
adb.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int id)
|
|
|
|
{
|
|
|
|
dialog.cancel();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
adb.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously executed task which confirms that the certificates are loaded.
|
|
|
|
* They are loaded from the main Activity already but might not be ready yet, or
|
|
|
|
* unloaded again.
|
|
|
|
*
|
|
|
|
* Once loaded the CA certificate spinner and checkboxes are updated
|
|
|
|
* accordingly.
|
|
|
|
*/
|
|
|
|
private class CertificateLoadTask extends AsyncTask<Void, Void, TrustedCertificateManager>
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
protected void onPreExecute()
|
|
|
|
{
|
|
|
|
setProgressBarIndeterminateVisibility(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected TrustedCertificateManager doInBackground(Void... params)
|
|
|
|
{
|
|
|
|
return TrustedCertificateManager.getInstance().load();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(TrustedCertificateManager result)
|
|
|
|
{
|
|
|
|
setProgressBarIndeterminateVisibility(false);
|
|
|
|
mCertsLoaded = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-17 17:45:23 +00:00
|
|
|
/**
|
|
|
|
* Save or update the profile depending on whether we actually have a
|
|
|
|
* profile object or not (this was created in updateProfileData)
|
|
|
|
*/
|
|
|
|
private void saveProfile()
|
|
|
|
{
|
|
|
|
if (verifyInput())
|
|
|
|
{
|
|
|
|
if (mProfile != null)
|
|
|
|
{
|
|
|
|
updateProfileData();
|
|
|
|
mDataSource.updateVpnProfile(mProfile);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mProfile = new VpnProfile();
|
|
|
|
updateProfileData();
|
|
|
|
mDataSource.insertProfile(mProfile);
|
|
|
|
}
|
|
|
|
setResult(RESULT_OK, new Intent().putExtra(VpnProfileDataSource.KEY_ID, mProfile.getId()));
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify the user input and display error messages.
|
|
|
|
* @return true if the input is valid
|
|
|
|
*/
|
|
|
|
private boolean verifyInput()
|
|
|
|
{
|
|
|
|
boolean valid = true;
|
|
|
|
if (mGateway.getText().toString().trim().isEmpty())
|
|
|
|
{
|
|
|
|
mGateway.setError(getString(R.string.alert_text_no_input_gateway));
|
|
|
|
valid = false;
|
|
|
|
}
|
|
|
|
if (mUsername.getText().toString().trim().isEmpty())
|
|
|
|
{
|
|
|
|
mUsername.setError(getString(R.string.alert_text_no_input_username));
|
|
|
|
valid = false;
|
|
|
|
}
|
2012-08-14 07:12:29 +00:00
|
|
|
if (!mCheckAuto.isChecked())
|
2012-08-07 16:03:51 +00:00
|
|
|
{
|
|
|
|
showCertificateAlert();
|
|
|
|
valid = false;
|
|
|
|
}
|
2012-07-17 17:45:23 +00:00
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the profile object with the data entered by the user
|
|
|
|
*/
|
|
|
|
private void updateProfileData()
|
|
|
|
{
|
|
|
|
/* the name is optional, we default to the gateway if none is given */
|
|
|
|
String name = mName.getText().toString().trim();
|
|
|
|
String gateway = mGateway.getText().toString().trim();
|
|
|
|
mProfile.setName(name.isEmpty() ? gateway : name);
|
|
|
|
mProfile.setGateway(gateway);
|
|
|
|
mProfile.setUsername(mUsername.getText().toString().trim());
|
|
|
|
String password = mPassword.getText().toString().trim();
|
|
|
|
password = password.isEmpty() ? null : password;
|
|
|
|
mProfile.setPassword(password);
|
|
|
|
}
|
|
|
|
|
2012-07-17 17:23:21 +00:00
|
|
|
/**
|
|
|
|
* Load an existing profile if we got an ID
|
|
|
|
*/
|
|
|
|
private void loadProfileData()
|
|
|
|
{
|
|
|
|
getActionBar().setTitle(R.string.add_profile);
|
|
|
|
if (mId != null)
|
|
|
|
{
|
|
|
|
mProfile = mDataSource.getVpnProfile(mId);
|
|
|
|
if (mProfile != null)
|
|
|
|
{
|
|
|
|
mName.setText(mProfile.getName());
|
|
|
|
mGateway.setText(mProfile.getGateway());
|
|
|
|
mUsername.setText(mProfile.getUsername());
|
|
|
|
mPassword.setText(mProfile.getPassword());
|
2012-08-07 16:03:51 +00:00
|
|
|
mCertAlias = mProfile.getCertificateAlias();
|
2012-07-17 17:23:21 +00:00
|
|
|
getActionBar().setTitle(mProfile.getName());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log.e(VpnProfileDetailActivity.class.getSimpleName(),
|
|
|
|
"VPN profile with id " + mId + " not found");
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}
|
2012-08-07 16:03:51 +00:00
|
|
|
mCheckAuto.setChecked(mCertAlias == null);
|
2012-07-17 17:23:21 +00:00
|
|
|
}
|
|
|
|
}
|