From f29ee8ac7378408e070be1130c9cadc8e947ddb3 Mon Sep 17 00:00:00 2001 From: cyBerta Date: Tue, 23 Nov 2021 14:51:33 +0100 Subject: sort locations by transport --- .../base/fragments/GatewaySelectionFragment.java | 10 +++-- .../leap/bitmaskclient/base/models/Location.java | 43 ++++++++++++++++------ 2 files changed, 38 insertions(+), 15 deletions(-) (limited to 'app/src/main/java/se/leap/bitmaskclient/base') diff --git a/app/src/main/java/se/leap/bitmaskclient/base/fragments/GatewaySelectionFragment.java b/app/src/main/java/se/leap/bitmaskclient/base/fragments/GatewaySelectionFragment.java index 51ebe359..e3845164 100644 --- a/app/src/main/java/se/leap/bitmaskclient/base/fragments/GatewaySelectionFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/base/fragments/GatewaySelectionFragment.java @@ -117,7 +117,7 @@ public class GatewaySelectionFragment extends Fragment implements Observer, Loca recyclerView.setHasFixedSize(true); LinearLayoutManager layoutManager = new LinearLayoutManager(this.getContext()); recyclerView.setLayoutManager(layoutManager); - locationListAdapter = new LocationListAdapter(gatewaysManager.getGatewayLocations(), this, selectedTransport); + locationListAdapter = new LocationListAdapter(gatewaysManager.getSortedGatewayLocations(selectedTransport), this, selectedTransport); recyclerView.setAdapter(locationListAdapter); recyclerView.setVisibility(VISIBLE); } @@ -203,14 +203,15 @@ public class GatewaySelectionFragment extends Fragment implements Observer, Loca public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if (key.equals(USE_BRIDGES)) { selectedTransport = PreferenceHelper.getUseBridges(sharedPreferences) ? OBFS4 : OPENVPN; - locationListAdapter.updateTransport(selectedTransport); + gatewaysManager.updateTransport(selectedTransport); + locationListAdapter.updateTransport(selectedTransport, gatewaysManager); } } static class LocationListAdapter extends RecyclerView.Adapter { private static final String TAG = LocationListAdapter.class.getSimpleName(); private Connection.TransportType transport; - private final List values; + private List values; private final WeakReference callback; static class ViewHolder extends RecyclerView.ViewHolder { @@ -232,8 +233,9 @@ public class GatewaySelectionFragment extends Fragment implements Observer, Loca notifyItemRemoved(position); } - public void updateTransport(Connection.TransportType transportType) { + public void updateTransport(Connection.TransportType transportType, GatewaysManager gatewaysManager) { transport = transportType; + values = gatewaysManager.getSortedGatewayLocations(transportType); notifyDataSetChanged(); } diff --git a/app/src/main/java/se/leap/bitmaskclient/base/models/Location.java b/app/src/main/java/se/leap/bitmaskclient/base/models/Location.java index e0ce9e8b..064f25c0 100644 --- a/app/src/main/java/se/leap/bitmaskclient/base/models/Location.java +++ b/app/src/main/java/se/leap/bitmaskclient/base/models/Location.java @@ -19,22 +19,25 @@ package se.leap.bitmaskclient.base.models; import androidx.annotation.NonNull; +import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; +import java.util.function.ToDoubleFunction; import de.blinkt.openvpn.core.connection.Connection; +import de.blinkt.openvpn.core.connection.Connection.TransportType; public class Location implements Cloneable { @NonNull private String name = ""; - @NonNull private HashMap averageLoad = new HashMap<>(); - @NonNull private HashMap numberOfGateways = new HashMap<>(); + @NonNull private HashMap averageLoad = new HashMap<>(); + @NonNull private HashMap numberOfGateways = new HashMap<>(); public boolean selected; public Location() {} public Location(@NonNull String name, - @NonNull HashMap averageLoad, - @NonNull HashMap numberOfGateways, + @NonNull HashMap averageLoad, + @NonNull HashMap numberOfGateways, boolean selected) { this.name = name; this.averageLoad = averageLoad; @@ -46,26 +49,26 @@ public class Location implements Cloneable { return !numberOfGateways.isEmpty() && !averageLoad.isEmpty() && !name.isEmpty(); } - public boolean supportsTransport(Connection.TransportType transportType) { + public boolean supportsTransport(TransportType transportType) { return numberOfGateways.containsKey(transportType); } - public void setAverageLoad(Connection.TransportType transportType, double load) { + public void setAverageLoad(TransportType transportType, double load) { averageLoad.put(transportType, load); } - public double getAverageLoad(Connection.TransportType transportType) { + public double getAverageLoad(TransportType transportType) { if (averageLoad.containsKey(transportType)) { return averageLoad.get(transportType); } return 0; } - public void setNumberOfGateways(Connection.TransportType transportType, int numbers) { + public void setNumberOfGateways(TransportType transportType, int numbers) { numberOfGateways.put(transportType, numbers); } - public int getNumberOfGateways(Connection.TransportType transportType) { + public int getNumberOfGateways(TransportType transportType) { if (numberOfGateways.containsKey(transportType)) { return numberOfGateways.get(transportType); } @@ -101,8 +104,26 @@ public class Location implements Cloneable { public Location clone() throws CloneNotSupportedException { Location copy = (Location) super.clone(); copy.name = this.name; - copy.numberOfGateways = (HashMap) this.numberOfGateways.clone(); - copy.averageLoad = (HashMap) this.averageLoad.clone(); + copy.numberOfGateways = (HashMap) this.numberOfGateways.clone(); + copy.averageLoad = (HashMap) this.averageLoad.clone(); return copy; } + + public static class SortByAverageLoad implements Comparator { + TransportType transportType; + public SortByAverageLoad(TransportType transportType) { + this.transportType = transportType; + } + + @Override + public int compare(Location location1, Location location2) { + if (location1.supportsTransport(transportType) && location2.supportsTransport(transportType)) { + return (int) (location1.getAverageLoad(transportType) * 100) - (int) (location2.getAverageLoad(transportType) * 100); + } else if (location1.supportsTransport(transportType) && !location2.supportsTransport(transportType)) { + return -1; + } else { + return 1; + } + } + } } -- cgit v1.2.3