android: Change how features of VPN types are stored and checked
authorTobias Brunner <tobias@strongswan.org>
Wed, 10 Sep 2014 09:25:03 +0000 (11:25 +0200)
committerTobias Brunner <tobias@strongswan.org>
Thu, 6 Nov 2014 15:28:40 +0000 (16:28 +0100)
src/frontends/android/src/org/strongswan/android/data/VpnType.java
src/frontends/android/src/org/strongswan/android/logic/CharonVpnService.java
src/frontends/android/src/org/strongswan/android/ui/MainActivity.java
src/frontends/android/src/org/strongswan/android/ui/VpnProfileDetailActivity.java
src/frontends/android/src/org/strongswan/android/ui/adapter/VpnProfileAdapter.java

index f62c96d..8b2fd59 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012-2013 Tobias Brunner
+ * Copyright (C) 2012-2014 Tobias Brunner
  * Hochschule fuer Technik Rapperswil
  *
  * This program is free software; you can redistribute it and/or modify it
 
 package org.strongswan.android.data;
 
+import java.util.EnumSet;
+
 public enum VpnType
 {
        /* the order here must match the items in R.array.vpn_types */
-       IKEV2_EAP("ikev2-eap", true, false),
-       IKEV2_CERT("ikev2-cert", false, true),
-       IKEV2_CERT_EAP("ikev2-cert-eap", true, true),
-       IKEV2_BYOD_EAP("ikev2-byod-eap", true, false, true);
-
-       private String mIdentifier;
-       private boolean mCertificate;
-       private boolean mUsernamePassword;
-       private boolean mBYOD;
+       IKEV2_EAP("ikev2-eap", EnumSet.of(VpnTypeFeature.USER_PASS)),
+       IKEV2_CERT("ikev2-cert", EnumSet.of(VpnTypeFeature.CERTIFICATE)),
+       IKEV2_CERT_EAP("ikev2-cert-eap", EnumSet.of(VpnTypeFeature.USER_PASS, VpnTypeFeature.CERTIFICATE)),
+       IKEV2_BYOD_EAP("ikev2-byod-eap", EnumSet.of(VpnTypeFeature.USER_PASS, VpnTypeFeature.CERTIFICATE, VpnTypeFeature.BYOD));
 
        /**
-        * Enum which provides additional information about the supported VPN types.
-        *
-        * @param id identifier used to store and transmit this specific type
-        * @param userpass true if username and password are required
-        * @param certificate true if a client certificate is required
+        * Features of a VPN type.
         */
-       VpnType(String id, boolean userpass, boolean certificate)
+       public enum VpnTypeFeature
        {
-               this(id, userpass, certificate, false);
+               /** client certificate is required */
+               CERTIFICATE,
+               /** username and password are required */
+               USER_PASS,
+               /** enable BYOD features */
+               BYOD;
        }
 
+       private String mIdentifier;
+       private EnumSet<VpnTypeFeature> mFeatures;
+
        /**
         * Enum which provides additional information about the supported VPN types.
         *
         * @param id identifier used to store and transmit this specific type
-        * @param userpass true if username and password are required
+        * @param features of the given VPN type
         * @param certificate true if a client certificate is required
-        * @param byod true to enable BYOD features
         */
-       VpnType(String id, boolean userpass, boolean certificate, boolean byod)
+       VpnType(String id, EnumSet<VpnTypeFeature> features)
        {
                mIdentifier = id;
-               mUsernamePassword = userpass;
-               mCertificate = certificate;
-               mBYOD = byod;
+               mFeatures = features;
        }
 
        /**
@@ -66,33 +64,13 @@ public enum VpnType
        }
 
        /**
-        * Whether username and password are required for this type of VPN.
-        *
-        * @return true if username and password are required
-        */
-       public boolean getRequiresUsernamePassword()
-       {
-               return mUsernamePassword;
-       }
-
-       /**
-        * Whether a certificate is required for this type of VPN.
-        *
-        * @return true if a certificate is required
-        */
-       public boolean getRequiresCertificate()
-       {
-               return mCertificate;
-       }
-
-       /**
-        * Whether BYOD features should be enabled.
+        * Checks whether a feature is supported/required by this type of VPN.
         *
-        * @return true if BYOD features are to be enabled
+        * @return true if the feature is supported/required
         */
-       public boolean getEnableBYOD()
+       public boolean has(VpnTypeFeature feature)
        {
-               return mBYOD;
+               return mFeatures.contains(feature);
        }
 
        /**
index c8f3b34..7cdaee7 100644 (file)
@@ -26,6 +26,7 @@ import java.util.List;
 
 import org.strongswan.android.data.VpnProfile;
 import org.strongswan.android.data.VpnProfileDataSource;
+import org.strongswan.android.data.VpnType.VpnTypeFeature;
 import org.strongswan.android.logic.VpnStateService.ErrorState;
 import org.strongswan.android.logic.VpnStateService.State;
 import org.strongswan.android.logic.imc.ImcState;
@@ -211,7 +212,7 @@ public class CharonVpnService extends VpnService implements Runnable
                                                mIsDisconnecting = false;
 
                                                BuilderAdapter builder = new BuilderAdapter(mCurrentProfile.getName());
-                                               if (initializeCharon(builder, mLogFile, mCurrentProfile.getVpnType().getEnableBYOD()))
+                                               if (initializeCharon(builder, mLogFile, mCurrentProfile.getVpnType().has(VpnTypeFeature.BYOD)))
                                                {
                                                        Log.i(TAG, "charon started");
                                                        initiate(mCurrentProfile.getVpnType().getIdentifier(),
index a2ad80e..e1b3e07 100644 (file)
@@ -20,6 +20,7 @@ package org.strongswan.android.ui;
 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;
@@ -219,7 +220,7 @@ public class MainActivity extends Activity implements OnVpnProfileSelectedListen
                profileInfo.putLong(VpnProfileDataSource.KEY_ID, profile.getId());
                profileInfo.putString(VpnProfileDataSource.KEY_USERNAME, profile.getUsername());
                profileInfo.putString(VpnProfileDataSource.KEY_PASSWORD, profile.getPassword());
-               profileInfo.putBoolean(PROFILE_REQUIRES_PASSWORD, profile.getVpnType().getRequiresUsernamePassword());
+               profileInfo.putBoolean(PROFILE_REQUIRES_PASSWORD, profile.getVpnType().has(VpnTypeFeature.USER_PASS));
                profileInfo.putString(PROFILE_NAME, profile.getName());
 
                removeFragmentByTag(DIALOG_TAG);
index 39d3700..41cd6e9 100644 (file)
@@ -23,6 +23,7 @@ import org.strongswan.android.R;
 import org.strongswan.android.data.VpnProfile;
 import org.strongswan.android.data.VpnProfileDataSource;
 import org.strongswan.android.data.VpnType;
+import org.strongswan.android.data.VpnType.VpnTypeFeature;
 import org.strongswan.android.logic.TrustedCertificateManager;
 import org.strongswan.android.security.TrustedCertificateEntry;
 
@@ -240,11 +241,11 @@ public class VpnProfileDetailActivity extends Activity
         */
        private void updateCredentialView()
        {
-               mUsernamePassword.setVisibility(mVpnType.getRequiresUsernamePassword() ? View.VISIBLE : View.GONE);
-               mUserCertificate.setVisibility(mVpnType.getRequiresCertificate() ? View.VISIBLE : View.GONE);
-               mTncNotice.setVisibility(mVpnType.getEnableBYOD() ? View.VISIBLE : View.GONE);
+               mUsernamePassword.setVisibility(mVpnType.has(VpnTypeFeature.USER_PASS) ? View.VISIBLE : View.GONE);
+               mUserCertificate.setVisibility(mVpnType.has(VpnTypeFeature.CERTIFICATE) ? View.VISIBLE : View.GONE);
+               mTncNotice.setVisibility(mVpnType.has(VpnTypeFeature.BYOD) ? View.VISIBLE : View.GONE);
 
-               if (mVpnType.getRequiresCertificate())
+               if (mVpnType.has(VpnTypeFeature.CERTIFICATE))
                {
                        if (mUserCertLoading != null)
                        {
@@ -349,7 +350,7 @@ public class VpnProfileDetailActivity extends Activity
                        mGateway.setError(getString(R.string.alert_text_no_input_gateway));
                        valid = false;
                }
-               if (mVpnType.getRequiresUsernamePassword())
+               if (mVpnType.has(VpnTypeFeature.USER_PASS))
                {
                        if (mUsername.getText().toString().trim().isEmpty())
                        {
@@ -357,7 +358,7 @@ public class VpnProfileDetailActivity extends Activity
                                valid = false;
                        }
                }
-               if (mVpnType.getRequiresCertificate() && mUserCertEntry == null)
+               if (mVpnType.has(VpnTypeFeature.CERTIFICATE) && mUserCertEntry == null)
                {       /* let's show an error icon */
                        ((TextView)mSelectUserCert.findViewById(android.R.id.text1)).setError("");
                        valid = false;
@@ -381,14 +382,14 @@ public class VpnProfileDetailActivity extends Activity
                mProfile.setName(name.isEmpty() ? gateway : name);
                mProfile.setGateway(gateway);
                mProfile.setVpnType(mVpnType);
-               if (mVpnType.getRequiresUsernamePassword())
+               if (mVpnType.has(VpnTypeFeature.USER_PASS))
                {
                        mProfile.setUsername(mUsername.getText().toString().trim());
                        String password = mPassword.getText().toString().trim();
                        password = password.isEmpty() ? null : password;
                        mProfile.setPassword(password);
                }
-               if (mVpnType.getRequiresCertificate())
+               if (mVpnType.has(VpnTypeFeature.CERTIFICATE))
                {
                        mProfile.setUserCertificateAlias(mUserCertEntry.getAlias());
                }
index 85dc837..f3bb271 100644 (file)
@@ -23,6 +23,7 @@ import java.util.List;
 
 import org.strongswan.android.R;
 import org.strongswan.android.data.VpnProfile;
+import org.strongswan.android.data.VpnType.VpnTypeFeature;
 
 import android.content.Context;
 import android.view.LayoutInflater;
@@ -64,7 +65,7 @@ public class VpnProfileAdapter extends ArrayAdapter<VpnProfile>
                tv = (TextView)vpnProfileView.findViewById(R.id.profile_item_gateway);
                tv.setText(getContext().getString(R.string.profile_gateway_label) + " " + profile.getGateway());
                tv = (TextView)vpnProfileView.findViewById(R.id.profile_item_username);
-               if (profile.getVpnType().getRequiresUsernamePassword())
+               if (profile.getVpnType().has(VpnTypeFeature.USER_PASS))
                {       /* if the view is reused we make sure it is visible */
                        tv.setVisibility(View.VISIBLE);
                        tv.setText(getContext().getString(R.string.profile_username_label) + " " + profile.getUsername());
@@ -74,7 +75,7 @@ public class VpnProfileAdapter extends ArrayAdapter<VpnProfile>
                        tv.setVisibility(View.GONE);
                }
                tv = (TextView)vpnProfileView.findViewById(R.id.profile_item_certificate);
-               if (profile.getVpnType().getRequiresCertificate())
+               if (profile.getVpnType().has(VpnTypeFeature.CERTIFICATE))
                {
                        tv.setText(getContext().getString(R.string.profile_user_certificate_label) + " " + profile.getUserCertificateAlias());
                        tv.setVisibility(View.VISIBLE);