diff options
author | cyBerta <cyberta@riseup.net> | 2021-11-23 14:51:33 +0100 |
---|---|---|
committer | cyBerta <cyberta@riseup.net> | 2021-11-23 14:51:33 +0100 |
commit | f29ee8ac7378408e070be1130c9cadc8e947ddb3 (patch) | |
tree | 5b6cf94738065434a41ca5a69be049d3b261d3cf /app/src/main/java/se/leap/bitmaskclient/base | |
parent | 438b2b866b64f63dc1346ca89cf3e56847def6da (diff) |
sort locations by transport
Diffstat (limited to 'app/src/main/java/se/leap/bitmaskclient/base')
-rw-r--r-- | app/src/main/java/se/leap/bitmaskclient/base/fragments/GatewaySelectionFragment.java | 10 | ||||
-rw-r--r-- | app/src/main/java/se/leap/bitmaskclient/base/models/Location.java | 43 |
2 files changed, 38 insertions, 15 deletions
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<LocationListAdapter.ViewHolder> { private static final String TAG = LocationListAdapter.class.getSimpleName(); private Connection.TransportType transport; - private final List<Location> values; + private List<Location> values; private final WeakReference<LocationListSelectionListener> 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<Connection.TransportType, Double> averageLoad = new HashMap<>(); - @NonNull private HashMap<Connection.TransportType, Integer> numberOfGateways = new HashMap<>(); + @NonNull private HashMap<TransportType, Double> averageLoad = new HashMap<>(); + @NonNull private HashMap<TransportType, Integer> numberOfGateways = new HashMap<>(); public boolean selected; public Location() {} public Location(@NonNull String name, - @NonNull HashMap<Connection.TransportType, Double> averageLoad, - @NonNull HashMap<Connection.TransportType, Integer> numberOfGateways, + @NonNull HashMap<TransportType, Double> averageLoad, + @NonNull HashMap<TransportType, Integer> 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<Connection.TransportType, Integer>) this.numberOfGateways.clone(); - copy.averageLoad = (HashMap<Connection.TransportType, Double>) this.averageLoad.clone(); + copy.numberOfGateways = (HashMap<TransportType, Integer>) this.numberOfGateways.clone(); + copy.averageLoad = (HashMap<TransportType, Double>) this.averageLoad.clone(); return copy; } + + public static class SortByAverageLoad implements Comparator<Location> { + 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; + } + } + } } |