From: Tobias Brunner Date: Wed, 10 Sep 2014 09:25:03 +0000 (+0200) Subject: android: Change how features of VPN types are stored and checked X-Git-Tag: 5.2.2dr1~41^2~6 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a64089738d3e1711c3afa781a0078a1ea7275f3f;p=thirdparty%2Fstrongswan.git android: Change how features of VPN types are stored and checked --- diff --git a/src/frontends/android/src/org/strongswan/android/data/VpnType.java b/src/frontends/android/src/org/strongswan/android/data/VpnType.java index f62c96d76a..8b2fd59870 100644 --- a/src/frontends/android/src/org/strongswan/android/data/VpnType.java +++ b/src/frontends/android/src/org/strongswan/android/data/VpnType.java @@ -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 @@ -15,45 +15,43 @@ 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 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 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); } /** diff --git a/src/frontends/android/src/org/strongswan/android/logic/CharonVpnService.java b/src/frontends/android/src/org/strongswan/android/logic/CharonVpnService.java index c8f3b344fd..7cdaee7358 100644 --- a/src/frontends/android/src/org/strongswan/android/logic/CharonVpnService.java +++ b/src/frontends/android/src/org/strongswan/android/logic/CharonVpnService.java @@ -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(), diff --git a/src/frontends/android/src/org/strongswan/android/ui/MainActivity.java b/src/frontends/android/src/org/strongswan/android/ui/MainActivity.java index a2ad80e829..e1b3e07839 100644 --- a/src/frontends/android/src/org/strongswan/android/ui/MainActivity.java +++ b/src/frontends/android/src/org/strongswan/android/ui/MainActivity.java @@ -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); diff --git a/src/frontends/android/src/org/strongswan/android/ui/VpnProfileDetailActivity.java b/src/frontends/android/src/org/strongswan/android/ui/VpnProfileDetailActivity.java index 39d37005d6..41cd6e98c2 100644 --- a/src/frontends/android/src/org/strongswan/android/ui/VpnProfileDetailActivity.java +++ b/src/frontends/android/src/org/strongswan/android/ui/VpnProfileDetailActivity.java @@ -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()); } diff --git a/src/frontends/android/src/org/strongswan/android/ui/adapter/VpnProfileAdapter.java b/src/frontends/android/src/org/strongswan/android/ui/adapter/VpnProfileAdapter.java index 85dc8370a3..f3bb271bc2 100644 --- a/src/frontends/android/src/org/strongswan/android/ui/adapter/VpnProfileAdapter.java +++ b/src/frontends/android/src/org/strongswan/android/ui/adapter/VpnProfileAdapter.java @@ -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 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 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);