summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorSamuel Holland <samuel@sholland.org>2017-08-07 19:55:11 -0500
committerSamuel Holland <samuel@sholland.org>2017-08-07 19:55:11 -0500
commit01524c0dbfe944015be59f76e6dcbf0f76e44931 (patch)
treedc15b1f7bdbf699b196b319097c5aa9f55a6d934
parent7d3e79684204e8df60eaf8d3985c22317f6594ef (diff)
ProfileService: Use a map to store profile data
This has no visible changes at the moment, but will allow most functions to pass around strings instead of Profile objects, obviating the need to implement serialization for them. It also trades some naive linear searches for the binary search in SimpleArrayMap. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r--app/src/main/java/com/wireguard/android/ProfileService.java29
-rw-r--r--app/src/main/java/com/wireguard/android/ProfileServiceInterface.java4
-rw-r--r--app/src/main/res/layout/profile_list_fragment.xml2
3 files changed, 17 insertions, 18 deletions
diff --git a/app/src/main/java/com/wireguard/android/ProfileService.java b/app/src/main/java/com/wireguard/android/ProfileService.java
index aca47c1c..ea97a613 100644
--- a/app/src/main/java/com/wireguard/android/ProfileService.java
+++ b/app/src/main/java/com/wireguard/android/ProfileService.java
@@ -2,8 +2,7 @@ package com.wireguard.android;
import android.app.Service;
import android.content.Intent;
-import android.databinding.ObservableArrayList;
-import android.databinding.ObservableList;
+import android.databinding.ObservableArrayMap;
import android.os.AsyncTask;
import android.os.Binder;
import android.os.IBinder;
@@ -28,7 +27,7 @@ public class ProfileService extends Service {
private static final String TAG = "ProfileService";
private final IBinder binder = new ProfileServiceBinder();
- private final ObservableList<Profile> profiles = new ObservableArrayList<>();
+ private final ObservableArrayMap<String, Profile> profiles = new ObservableArrayMap<>();
private RootShell rootShell;
@Override
@@ -58,9 +57,8 @@ public class ProfileService extends Service {
private ProfileAdder(Profile profile, boolean shouldConnect) {
super();
- for (Profile p : profiles)
- if (p.getName().equals(profile.getName()))
- throw new IllegalStateException("Profile already exists: " + profile.getName());
+ if (profiles.get(profile.getName()) != null)
+ throw new IllegalStateException("Profile already exists: " + profile.getName());
this.profile = profile;
this.shouldConnect = shouldConnect;
}
@@ -87,7 +85,7 @@ public class ProfileService extends Service {
if (!result)
return;
profile.setIsConnected(false);
- profiles.add(profile);
+ profiles.put(profile.getName(), profile);
if (shouldConnect)
new ProfileConnecter(profile).execute();
}
@@ -176,7 +174,8 @@ public class ProfileService extends Service {
protected void onPostExecute(List<Profile> loadedProfiles) {
if (loadedProfiles == null)
return;
- profiles.addAll(loadedProfiles);
+ for (Profile profile : loadedProfiles)
+ profiles.put(profile.getName(), profile);
}
}
@@ -208,7 +207,7 @@ public class ProfileService extends Service {
protected void onPostExecute(Boolean result) {
if (!result)
return;
- profiles.remove(profile);
+ profiles.remove(profile.getName());
if (replaceWith != null)
new ProfileAdder(replaceWith, shouldConnect).execute();
}
@@ -217,7 +216,7 @@ public class ProfileService extends Service {
private class ProfileServiceBinder extends Binder implements ProfileServiceInterface {
@Override
public void connectProfile(Profile profile) {
- if (!profiles.contains(profile))
+ if (profiles.get(profile.getName()) != profile)
return;
if (profile.getIsConnected())
return;
@@ -226,14 +225,14 @@ public class ProfileService extends Service {
@Override
public Profile copyProfileForEditing(Profile profile) {
- if (!profiles.contains(profile))
+ if (profiles.get(profile.getName()) != profile)
return null;
return profile.copy();
}
@Override
public void disconnectProfile(Profile profile) {
- if (!profiles.contains(profile))
+ if (profiles.get(profile.getName()) != profile)
return;
if (!profile.getIsConnected())
return;
@@ -241,13 +240,13 @@ public class ProfileService extends Service {
}
@Override
- public ObservableList<Profile> getProfiles() {
+ public ObservableArrayMap<String, Profile> getProfiles() {
return profiles;
}
@Override
public void removeProfile(Profile profile) {
- if (!profiles.contains(profile))
+ if (profiles.get(profile.getName()) != profile)
return;
if (profile.getIsConnected())
new ProfileDisconnecter(profile).execute();
@@ -257,7 +256,7 @@ public class ProfileService extends Service {
@Override
public void saveProfile(Profile oldProfile, Profile newProfile) {
if (oldProfile != null) {
- if (!profiles.contains(oldProfile))
+ if (profiles.get(oldProfile.getName()) != oldProfile)
return;
final boolean wasConnected = oldProfile.getIsConnected();
if (wasConnected)
diff --git a/app/src/main/java/com/wireguard/android/ProfileServiceInterface.java b/app/src/main/java/com/wireguard/android/ProfileServiceInterface.java
index 272059bc..0e94727b 100644
--- a/app/src/main/java/com/wireguard/android/ProfileServiceInterface.java
+++ b/app/src/main/java/com/wireguard/android/ProfileServiceInterface.java
@@ -1,6 +1,6 @@
package com.wireguard.android;
-import android.databinding.ObservableList;
+import android.databinding.ObservableArrayMap;
import com.wireguard.config.Profile;
@@ -44,7 +44,7 @@ public interface ProfileServiceInterface {
*
* @return The list of known profiles.
*/
- ObservableList<Profile> getProfiles();
+ ObservableArrayMap<String, Profile> getProfiles();
/**
* Remove a profile from being managed by this service. If the profile is currently connected,
diff --git a/app/src/main/res/layout/profile_list_fragment.xml b/app/src/main/res/layout/profile_list_fragment.xml
index 29ce62e7..c0edd444 100644
--- a/app/src/main/res/layout/profile_list_fragment.xml
+++ b/app/src/main/res/layout/profile_list_fragment.xml
@@ -6,7 +6,7 @@
<variable
name="profiles"
- type="android.databinding.ObservableList&lt;com.wireguard.config.Profile&gt;" />
+ type="android.databinding.ObservableArrayMap&lt;String, com.wireguard.config.Profile&gt;" />
</data>
<ListView