package org.strongswan.android.ui;
import java.security.cert.X509Certificate;
-import java.util.Hashtable;
import org.strongswan.android.R;
+import org.strongswan.android.data.TrustedCertificateEntry;
import org.strongswan.android.data.VpnProfile;
import org.strongswan.android.data.VpnProfileDataSource;
+import org.strongswan.android.data.VpnType;
import org.strongswan.android.logic.TrustedCertificateManager;
-import org.strongswan.android.ui.adapter.TrustedCertificateAdapter;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
-import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
-import android.view.Window;
+import android.view.View.OnClickListener;
+import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
+import android.widget.RelativeLayout;
import android.widget.Spinner;
+import android.widget.TextView;
public class VpnProfileDetailActivity extends Activity
{
+ private static final int SELECT_TRUSTED_CERTIFICATE = 0;
+
private VpnProfileDataSource mDataSource;
private Long mId;
+ private TrustedCertificateEntry mCertEntry;
+ private VpnType mVpnType = VpnType.IKEV2_EAP;
private VpnProfile mProfile;
- private boolean mCertsLoaded;
- private String mCertAlias;
- private Spinner mCertSpinner;
- private TrustedCertificateAdapter.CertEntry mSelectedCert;
private EditText mName;
private EditText mGateway;
+ private Spinner mSelectVpnType;
+ private ViewGroup mUsernamePassword;
private EditText mUsername;
private EditText mPassword;
- private CheckBox mCheckAll;
private CheckBox mCheckAuto;
+ private RelativeLayout mSelectCert;
+ private TextView mCertTitle;
+ private TextView mCertSubtitle;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
/* the title is set when we load the profile, if any */
getActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.profile_detail_view);
mName = (EditText)findViewById(R.id.name);
- mPassword = (EditText)findViewById(R.id.password);
mGateway = (EditText)findViewById(R.id.gateway);
+ mSelectVpnType = (Spinner)findViewById(R.id.vpn_type);
+
+ mUsernamePassword = (ViewGroup)findViewById(R.id.username_password_group);
mUsername = (EditText)findViewById(R.id.username);
+ mPassword = (EditText)findViewById(R.id.password);
- mCheckAll = (CheckBox)findViewById(R.id.ca_show_all);
mCheckAuto = (CheckBox)findViewById(R.id.ca_auto);
- mCertSpinner = (Spinner)findViewById(R.id.ca_spinner);
+ mSelectCert = (RelativeLayout)findViewById(R.id.select_certificate);
+ mCertTitle = (TextView)findViewById(R.id.select_certificate_title);
+ mCertSubtitle = (TextView)findViewById(R.id.select_certificate_subtitle);
- mCheckAuto.setOnCheckedChangeListener(new OnCheckedChangeListener() {
+
+ mSelectVpnType.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
+ public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
- updateCertSpinner();
+ mVpnType = VpnType.values()[position];
+ updateClientCredentialView();
}
- });
- mCheckAll.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
- {
- Hashtable<String, X509Certificate> certs;
- certs = isChecked ? TrustedCertificateManager.getInstance().getAllCACertificates()
- : TrustedCertificateManager.getInstance().getUserCACertificates();
- mCertSpinner.setAdapter(new TrustedCertificateAdapter(VpnProfileDetailActivity.this, certs));
- mSelectedCert = (TrustedCertificateAdapter.CertEntry)mCertSpinner.getSelectedItem();
+ public void onNothingSelected(AdapterView<?> parent)
+ { /* should not happen */
+ mVpnType = VpnType.IKEV2_EAP;
+ updateClientCredentialView();
}
});
- mCertSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
+ mCheckAuto.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
- public void onItemSelected(AdapterView<?> parent, View view,
- int pos, long id)
+ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
- mSelectedCert = (TrustedCertificateAdapter.CertEntry)parent.getSelectedItem();
+ updateCertificateSelector();
}
+ });
+ mSelectCert.setOnClickListener(new OnClickListener() {
@Override
- public void onNothingSelected(AdapterView<?> arg0)
+ public void onClick(View v)
{
- mSelectedCert = null;
+ Intent intent = new Intent(VpnProfileDetailActivity.this, TrustedCertificatesActivity.class);
+ startActivityForResult(intent, SELECT_TRUSTED_CERTIFICATE);
}
});
mId = extras == null ? null : extras.getLong(VpnProfileDataSource.KEY_ID);
}
- loadProfileData();
+ loadProfileData(savedInstanceState);
- new CertificateLoadTask().execute();
+ updateClientCredentialView();
+ updateCertificateSelector();
}
@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
- outState.putLong(VpnProfileDataSource.KEY_ID, mId);
+ if (mId != null)
+ {
+ outState.putLong(VpnProfileDataSource.KEY_ID, mId);
+ }
+ if (mCertEntry != null)
+ {
+ outState.putString(VpnProfileDataSource.KEY_CERTIFICATE, mCertEntry.getAlias());
+ }
}
@Override
}
}
+ @Override
+ protected void onActivityResult(int requestCode, int resultCode, Intent data)
+ {
+ switch (requestCode)
+ {
+ case SELECT_TRUSTED_CERTIFICATE:
+ if (resultCode == RESULT_OK)
+ {
+ String alias = data.getStringExtra(VpnProfileDataSource.KEY_CERTIFICATE);
+ X509Certificate certificate = TrustedCertificateManager.getInstance().getCACertificateFromAlias(alias);
+ mCertEntry = certificate == null ? null : new TrustedCertificateEntry(alias, certificate);
+ updateCertificateSelector();
+ }
+ break;
+ default:
+ super.onActivityResult(requestCode, resultCode, data);
+ }
+ }
+
+ /**
+ * Update the UI to enter client credentials depending on the type of VPN currently selected
+ */
+ private void updateClientCredentialView()
+ {
+ mUsernamePassword.setVisibility(mVpnType.getRequiresUsernamePassword() ? View.VISIBLE : View.GONE);
+ }
+
/**
* Show an alert in case the previously selected certificate is not found anymore
* or the user did not select a certificate in the spinner.
}
/**
- * 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.
+ * Update the CA certificate selection UI depending on whether the
+ * certificate should be automatically selected or not.
*/
- private class CertificateLoadTask extends AsyncTask<Void, Void, TrustedCertificateManager>
+ private void updateCertificateSelector()
{
- @Override
- protected void onPreExecute()
- {
- setProgressBarIndeterminateVisibility(true);
- }
-
- @Override
- protected TrustedCertificateManager doInBackground(Void... params)
+ if (!mCheckAuto.isChecked())
{
- return TrustedCertificateManager.getInstance().load();
- }
+ mSelectCert.setEnabled(true);
+ mSelectCert.setVisibility(View.VISIBLE);
- @Override
- protected void onPostExecute(TrustedCertificateManager result)
- {
- TrustedCertificateAdapter adapter;
- if (mCertAlias != null && mCertAlias.startsWith("system:"))
+ if (mCertEntry != null)
{
- mCheckAll.setChecked(true);
- adapter = new TrustedCertificateAdapter(VpnProfileDetailActivity.this,
- result.getAllCACertificates());
+ mCertTitle.setText(mCertEntry.getSubjectPrimary());
+ mCertSubtitle.setText(mCertEntry.getSubjectSecondary());
}
else
{
- mCheckAll.setChecked(false);
- adapter = new TrustedCertificateAdapter(VpnProfileDetailActivity.this,
- result.getUserCACertificates());
- }
- mCertSpinner.setAdapter(adapter);
-
- if (mCertAlias != null)
- {
- int position = adapter.getItemPosition(mCertAlias);
- if (position == -1)
- { /* previously selected certificate is not here anymore */
- showCertificateAlert();
- }
- else
- {
- mCertSpinner.setSelection(position);
- }
- }
-
- mSelectedCert = (TrustedCertificateAdapter.CertEntry)mCertSpinner.getSelectedItem();
-
- setProgressBarIndeterminateVisibility(false);
- mCertsLoaded = true;
- updateCertSpinner();
- }
- }
-
- /**
- * Update the CA certificate selection UI depending on whether the
- * certificate should be automatically selected or not.
- */
- private void updateCertSpinner()
- {
- if (!mCheckAuto.isChecked())
- {
- if (mCertsLoaded)
- {
- mCertSpinner.setEnabled(true);
- mCertSpinner.setVisibility(View.VISIBLE);
- mCheckAll.setEnabled(true);
- mCheckAll.setVisibility(View.VISIBLE);
+ mCertTitle.setText(R.string.profile_ca_select_certificate_label);
+ mCertSubtitle.setText(R.string.profile_ca_select_certificate);
}
}
else
{
- mCertSpinner.setEnabled(false);
- mCertSpinner.setVisibility(View.GONE);
- mCheckAll.setEnabled(false);
- mCheckAll.setVisibility(View.GONE);
+ mSelectCert.setEnabled(false);
+ mSelectCert.setVisibility(View.GONE);
}
}
mGateway.setError(getString(R.string.alert_text_no_input_gateway));
valid = false;
}
- if (mUsername.getText().toString().trim().isEmpty())
+ if (mVpnType.getRequiresUsernamePassword())
{
- mUsername.setError(getString(R.string.alert_text_no_input_username));
- valid = false;
+ if (mUsername.getText().toString().trim().isEmpty())
+ {
+ mUsername.setError(getString(R.string.alert_text_no_input_username));
+ valid = false;
+ }
}
- if (!mCheckAuto.isChecked() && mSelectedCert == null)
+ if (!mCheckAuto.isChecked() && mCertEntry == null)
{
showCertificateAlert();
valid = false;
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);
- String certAlias = mCheckAuto.isChecked() ? null : mSelectedCert.mAlias;
+ mProfile.setVpnType(mVpnType);
+ if (mVpnType.getRequiresUsernamePassword())
+ {
+ mProfile.setUsername(mUsername.getText().toString().trim());
+ String password = mPassword.getText().toString().trim();
+ password = password.isEmpty() ? null : password;
+ mProfile.setPassword(password);
+ }
+ String certAlias = mCheckAuto.isChecked() ? null : mCertEntry.getAlias();
mProfile.setCertificateAlias(certAlias);
}
/**
* Load an existing profile if we got an ID
+ *
+ * @param savedInstanceState previously saved state
*/
- private void loadProfileData()
+ private void loadProfileData(Bundle savedInstanceState)
{
+ String alias = null;
+
getActionBar().setTitle(R.string.add_profile);
if (mId != null)
{
{
mName.setText(mProfile.getName());
mGateway.setText(mProfile.getGateway());
+ mVpnType = mProfile.getVpnType();
mUsername.setText(mProfile.getUsername());
mPassword.setText(mProfile.getPassword());
- mCertAlias = mProfile.getCertificateAlias();
+ alias = mProfile.getCertificateAlias();
getActionBar().setTitle(mProfile.getName());
}
else
finish();
}
}
- mCheckAll.setChecked(false);
- mCheckAuto.setChecked(mCertAlias == null);
- updateCertSpinner();
+
+ mSelectVpnType.setSelection(mVpnType.ordinal());
+
+ /* check if the user selected a certificate previously */
+ alias = savedInstanceState == null ? alias : savedInstanceState.getString(VpnProfileDataSource.KEY_CERTIFICATE);
+ mCheckAuto.setChecked(alias == null);
+ if (alias != null)
+ {
+ X509Certificate certificate = TrustedCertificateManager.getInstance().getCACertificateFromAlias(alias);
+ if (certificate != null)
+ {
+ mCertEntry = new TrustedCertificateEntry(alias, certificate);
+ }
+ else
+ { /* previously selected certificate is not here anymore */
+ showCertificateAlert();
+ mCertEntry = null;
+ }
+ }
}
}