android: Change how features of VPN types are stored and checked

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