summaryrefslogtreecommitdiff
path: root/app/src/main/java/se
diff options
context:
space:
mode:
authorcyBerta <cyberta@riseup.net>2018-12-27 19:10:46 +0100
committercyBerta <cyberta@riseup.net>2018-12-27 19:10:46 +0100
commit678024d9be61e6b0bb55b826c4c488521e16de8d (patch)
treea86d8a37cd82f5cd825686ee1fcb0ca05519c761 /app/src/main/java/se
parent3609af508119f4d27b148e6f6f296c112943eafe (diff)
add test and minor refactoring for gateway selection
Diffstat (limited to 'app/src/main/java/se')
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java20
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/utils/ConfigHelper.java5
2 files changed, 17 insertions, 8 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java
index 078e3fd5..2bd666bf 100644
--- a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java
+++ b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaySelector.java
@@ -2,7 +2,13 @@ package se.leap.bitmaskclient.eip;
import android.util.Log;
-import java.util.*;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+
+import static se.leap.bitmaskclient.utils.ConfigHelper.getCurrentTimezone;
public class GatewaySelector {
private final static String TAG = GatewaySelector.class.getSimpleName();
@@ -19,16 +25,14 @@ public class GatewaySelector {
return closestGateway();
}
- public Gateway select(int nClosest) throws IndexOutOfBoundsException{
+ public Gateway select(int nClosest) {
int i = 0;
for (Map.Entry<Integer,Set<Gateway>> entrySet : offsets.entrySet()) {
- Iterator<Gateway> iterator = entrySet.getValue().iterator();
- while (iterator.hasNext()) {
- Gateway gateway = iterator.next();
- if (i == nClosest) {
+ for (Gateway gateway : entrySet.getValue()) {
+ if (i == nClosest) {
return gateway;
}
- i = i+1;
+ i = i + 1;
}
}
@@ -42,7 +46,7 @@ public class GatewaySelector {
private TreeMap<Integer, Set<Gateway>> calculateOffsets() {
TreeMap<Integer, Set<Gateway>> offsets = new TreeMap<Integer, Set<Gateway>>();
- int localOffset = Calendar.getInstance().get(Calendar.ZONE_OFFSET) / 3600000;
+ int localOffset = getCurrentTimezone();
for (Gateway gateway : gateways) {
int dist = timezoneDistance(localOffset, gateway.getTimezone());
Set<Gateway> set = (offsets.get(dist) != null) ?
diff --git a/app/src/main/java/se/leap/bitmaskclient/utils/ConfigHelper.java b/app/src/main/java/se/leap/bitmaskclient/utils/ConfigHelper.java
index 326139c0..d1ac0eb3 100644
--- a/app/src/main/java/se/leap/bitmaskclient/utils/ConfigHelper.java
+++ b/app/src/main/java/se/leap/bitmaskclient/utils/ConfigHelper.java
@@ -39,6 +39,7 @@ import java.security.cert.X509Certificate;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
+import java.util.Calendar;
import se.leap.bitmaskclient.BuildConfig;
import se.leap.bitmaskclient.ProviderAPI;
@@ -177,4 +178,8 @@ public class ConfigHelper {
public static boolean preferAnonymousUsage() {
return BuildConfig.priotize_anonymous_usage;
}
+
+ public static int getCurrentTimezone() {
+ return Calendar.getInstance().get(Calendar.ZONE_OFFSET) / 3600000;
+ }
}