android: Spinner added to select the VPN type
[strongswan.git] / src / frontends / android / src / org / strongswan / android / ui / VpnProfileDetailActivity.java
index 0a8dc16..73fa301 100644 (file)
 
 package org.strongswan.android.ui;
 
+import java.security.cert.X509Certificate;
+
 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 android.app.Activity;
+import android.app.AlertDialog;
+import android.content.DialogInterface;
+import android.content.Intent;
 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.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 EditText mName;
        private EditText mGateway;
+       private Spinner mSelectVpnType;
+       private ViewGroup mUsernamePassword;
        private EditText mUsername;
        private EditText mPassword;
+       private CheckBox mCheckAuto;
+       private RelativeLayout mSelectCert;
+       private TextView mCertTitle;
+       private TextView mCertSubtitle;
 
        @Override
        public void onCreate(Bundle savedInstanceState)
@@ -50,9 +82,51 @@ public class VpnProfileDetailActivity extends Activity
                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);
+
+               mCheckAuto = (CheckBox)findViewById(R.id.ca_auto);
+               mSelectCert = (RelativeLayout)findViewById(R.id.select_certificate);
+               mCertTitle = (TextView)findViewById(R.id.select_certificate_title);
+               mCertSubtitle = (TextView)findViewById(R.id.select_certificate_subtitle);
+
+
+               mSelectVpnType.setOnItemSelectedListener(new OnItemSelectedListener() {
+                       @Override
+                       public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
+                       {
+                               mVpnType = VpnType.values()[position];
+                               updateClientCredentialView();
+                       }
+
+                       @Override
+                       public void onNothingSelected(AdapterView<?> parent)
+                       {       /* should not happen */
+                               mVpnType = VpnType.IKEV2_EAP;
+                               updateClientCredentialView();
+                       }
+               });
+
+               mCheckAuto.setOnCheckedChangeListener(new OnCheckedChangeListener() {
+                       @Override
+                       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
+                       {
+                               updateCertificateSelector();
+                       }
+               });
+
+               mSelectCert.setOnClickListener(new OnClickListener() {
+                       @Override
+                       public void onClick(View v)
+                       {
+                               Intent intent = new Intent(VpnProfileDetailActivity.this, TrustedCertificatesActivity.class);
+                               startActivityForResult(intent, SELECT_TRUSTED_CERTIFICATE);
+                       }
+               });
 
                mId = savedInstanceState == null ? null : savedInstanceState.getLong(VpnProfileDataSource.KEY_ID);
                if (mId == null)
@@ -61,7 +135,10 @@ public class VpnProfileDetailActivity extends Activity
                        mId = extras == null ? null : extras.getLong(VpnProfileDataSource.KEY_ID);
                }
 
-               loadProfileData();
+               loadProfileData(savedInstanceState);
+
+               updateClientCredentialView();
+               updateCertificateSelector();
        }
 
        @Override
@@ -75,14 +152,199 @@ public class VpnProfileDetailActivity extends Activity
        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
+       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);
+               }
+       }
+
+       @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.
+        */
+       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();
+       }
+
+       /**
+        * Update the CA certificate selection UI depending on whether the
+        * certificate should be automatically selected or not.
+        */
+       private void updateCertificateSelector()
+       {
+               if (!mCheckAuto.isChecked())
+               {
+                       mSelectCert.setEnabled(true);
+                       mSelectCert.setVisibility(View.VISIBLE);
+
+                       if (mCertEntry != null)
+                       {
+                               mCertTitle.setText(mCertEntry.getSubjectPrimary());
+                               mCertSubtitle.setText(mCertEntry.getSubjectSecondary());
+                       }
+                       else
+                       {
+                               mCertTitle.setText(R.string.profile_ca_select_certificate_label);
+                               mCertSubtitle.setText(R.string.profile_ca_select_certificate);
+                       }
+               }
+               else
+               {
+                       mSelectCert.setEnabled(false);
+                       mSelectCert.setVisibility(View.GONE);
+               }
+       }
+
+       /**
+        * 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 (mVpnType.getRequiresUsernamePassword())
+               {
+                       if (mUsername.getText().toString().trim().isEmpty())
+                       {
+                               mUsername.setError(getString(R.string.alert_text_no_input_username));
+                               valid = false;
+                       }
+               }
+               if (!mCheckAuto.isChecked() && mCertEntry == null)
+               {
+                       showCertificateAlert();
+                       valid = false;
+               }
+               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.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)
                {
@@ -91,8 +353,10 @@ public class VpnProfileDetailActivity extends Activity
                        {
                                mName.setText(mProfile.getName());
                                mGateway.setText(mProfile.getGateway());
+                               mVpnType = mProfile.getVpnType();
                                mUsername.setText(mProfile.getUsername());
                                mPassword.setText(mProfile.getPassword());
+                               alias = mProfile.getCertificateAlias();
                                getActionBar().setTitle(mProfile.getName());
                        }
                        else
@@ -102,5 +366,24 @@ public class VpnProfileDetailActivity extends Activity
                                finish();
                        }
                }
+
+               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;
+                       }
+               }
        }
 }