android: Add convenience methods to get/set selected apps to/from a sorted set
authorTobias Brunner <tobias@strongswan.org>
Mon, 26 Jun 2017 14:19:08 +0000 (16:19 +0200)
committerTobias Brunner <tobias@strongswan.org>
Mon, 3 Jul 2017 08:27:53 +0000 (10:27 +0200)
src/frontends/android/app/src/main/java/org/strongswan/android/data/VpnProfile.java
src/frontends/android/app/src/main/java/org/strongswan/android/logic/CharonVpnService.java

index 92e5906..0b552f4 100644 (file)
 package org.strongswan.android.data;
 
 
+import android.text.TextUtils;
+
+import java.util.Arrays;
+import java.util.SortedSet;
+import java.util.TreeSet;
 import java.util.UUID;
 
 public class VpnProfile implements Cloneable
@@ -202,6 +207,7 @@ public class VpnProfile implements Cloneable
        {
                this.mIncludedSubnets = includedSubnets;
        }
+
        public String getIncludedSubnets()
        {
                return mIncludedSubnets;
@@ -212,11 +218,26 @@ public class VpnProfile implements Cloneable
                this.mSelectedApps = selectedApps;
        }
 
+       public void setSelectedApps(SortedSet<String> selectedApps)
+       {
+               this.mSelectedApps = selectedApps.size() > 0 ? TextUtils.join(" ", selectedApps) : null;
+       }
+
        public String getSelectedApps()
        {
                return mSelectedApps;
        }
 
+       public SortedSet<String> getSelectedAppsSet()
+       {
+               TreeSet<String> set = new TreeSet<>();
+               if (!TextUtils.isEmpty(mSelectedApps))
+               {
+                       set.addAll(Arrays.asList(mSelectedApps.split("\\s+")));
+               }
+               return set;
+       }
+
        public void setSelectedAppsHandling(SelectedAppsHandling selectedAppsHandling)
        {
                this.mSelectedAppsHandling = selectedAppsHandling;
index ffb2045..965edfb 100644 (file)
@@ -64,6 +64,7 @@ import java.security.cert.X509Certificate;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Locale;
+import java.util.SortedSet;
 
 public class CharonVpnService extends VpnService implements Runnable, VpnStateService.VpnStateListener
 {
@@ -811,7 +812,7 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
                private final IPRangeSet mExcludedSubnets;
                private final int mSplitTunneling;
                private final SelectedAppsHandling mAppHandling;
-               private final String[] mSelectedApps;
+               private final SortedSet<String> mSelectedApps;
                private int mMtu;
                private boolean mIPv4Seen, mIPv6Seen;
 
@@ -833,8 +834,7 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
                        Integer splitTunneling = profile.getSplitTunneling();
                        mSplitTunneling = splitTunneling != null ? splitTunneling : 0;
                        mAppHandling = profile.getSelectedAppsHandling();
-                       String selectedApps = profile.getSelectedApps();
-                       mSelectedApps = selectedApps != null ? selectedApps.split("\\s+") : null;
+                       mSelectedApps = profile.getSelectedAppsSet();
                }
 
                public void addAddress(String address, int prefixLength)
@@ -961,7 +961,7 @@ public class CharonVpnService extends VpnService implements Runnable, VpnStateSe
                                builder.addRoute("::", 0);
                        }
                        /* apply selected applications */
-                       if (mSelectedApps != null && mSelectedApps.length > 0)
+                       if (mSelectedApps.size() > 0)
                        {
                                switch (mAppHandling)
                                {