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>
This commit is contained in:
parent
7d3e796842
commit
01524c0dbf
@ -2,8 +2,7 @@ package com.wireguard.android;
|
|||||||
|
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.databinding.ObservableArrayList;
|
import android.databinding.ObservableArrayMap;
|
||||||
import android.databinding.ObservableList;
|
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
@ -28,7 +27,7 @@ public class ProfileService extends Service {
|
|||||||
private static final String TAG = "ProfileService";
|
private static final String TAG = "ProfileService";
|
||||||
|
|
||||||
private final IBinder binder = new ProfileServiceBinder();
|
private final IBinder binder = new ProfileServiceBinder();
|
||||||
private final ObservableList<Profile> profiles = new ObservableArrayList<>();
|
private final ObservableArrayMap<String, Profile> profiles = new ObservableArrayMap<>();
|
||||||
private RootShell rootShell;
|
private RootShell rootShell;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -58,9 +57,8 @@ public class ProfileService extends Service {
|
|||||||
|
|
||||||
private ProfileAdder(Profile profile, boolean shouldConnect) {
|
private ProfileAdder(Profile profile, boolean shouldConnect) {
|
||||||
super();
|
super();
|
||||||
for (Profile p : profiles)
|
if (profiles.get(profile.getName()) != null)
|
||||||
if (p.getName().equals(profile.getName()))
|
throw new IllegalStateException("Profile already exists: " + profile.getName());
|
||||||
throw new IllegalStateException("Profile already exists: " + profile.getName());
|
|
||||||
this.profile = profile;
|
this.profile = profile;
|
||||||
this.shouldConnect = shouldConnect;
|
this.shouldConnect = shouldConnect;
|
||||||
}
|
}
|
||||||
@ -87,7 +85,7 @@ public class ProfileService extends Service {
|
|||||||
if (!result)
|
if (!result)
|
||||||
return;
|
return;
|
||||||
profile.setIsConnected(false);
|
profile.setIsConnected(false);
|
||||||
profiles.add(profile);
|
profiles.put(profile.getName(), profile);
|
||||||
if (shouldConnect)
|
if (shouldConnect)
|
||||||
new ProfileConnecter(profile).execute();
|
new ProfileConnecter(profile).execute();
|
||||||
}
|
}
|
||||||
@ -176,7 +174,8 @@ public class ProfileService extends Service {
|
|||||||
protected void onPostExecute(List<Profile> loadedProfiles) {
|
protected void onPostExecute(List<Profile> loadedProfiles) {
|
||||||
if (loadedProfiles == null)
|
if (loadedProfiles == null)
|
||||||
return;
|
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) {
|
protected void onPostExecute(Boolean result) {
|
||||||
if (!result)
|
if (!result)
|
||||||
return;
|
return;
|
||||||
profiles.remove(profile);
|
profiles.remove(profile.getName());
|
||||||
if (replaceWith != null)
|
if (replaceWith != null)
|
||||||
new ProfileAdder(replaceWith, shouldConnect).execute();
|
new ProfileAdder(replaceWith, shouldConnect).execute();
|
||||||
}
|
}
|
||||||
@ -217,7 +216,7 @@ public class ProfileService extends Service {
|
|||||||
private class ProfileServiceBinder extends Binder implements ProfileServiceInterface {
|
private class ProfileServiceBinder extends Binder implements ProfileServiceInterface {
|
||||||
@Override
|
@Override
|
||||||
public void connectProfile(Profile profile) {
|
public void connectProfile(Profile profile) {
|
||||||
if (!profiles.contains(profile))
|
if (profiles.get(profile.getName()) != profile)
|
||||||
return;
|
return;
|
||||||
if (profile.getIsConnected())
|
if (profile.getIsConnected())
|
||||||
return;
|
return;
|
||||||
@ -226,14 +225,14 @@ public class ProfileService extends Service {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Profile copyProfileForEditing(Profile profile) {
|
public Profile copyProfileForEditing(Profile profile) {
|
||||||
if (!profiles.contains(profile))
|
if (profiles.get(profile.getName()) != profile)
|
||||||
return null;
|
return null;
|
||||||
return profile.copy();
|
return profile.copy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void disconnectProfile(Profile profile) {
|
public void disconnectProfile(Profile profile) {
|
||||||
if (!profiles.contains(profile))
|
if (profiles.get(profile.getName()) != profile)
|
||||||
return;
|
return;
|
||||||
if (!profile.getIsConnected())
|
if (!profile.getIsConnected())
|
||||||
return;
|
return;
|
||||||
@ -241,13 +240,13 @@ public class ProfileService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ObservableList<Profile> getProfiles() {
|
public ObservableArrayMap<String, Profile> getProfiles() {
|
||||||
return profiles;
|
return profiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void removeProfile(Profile profile) {
|
public void removeProfile(Profile profile) {
|
||||||
if (!profiles.contains(profile))
|
if (profiles.get(profile.getName()) != profile)
|
||||||
return;
|
return;
|
||||||
if (profile.getIsConnected())
|
if (profile.getIsConnected())
|
||||||
new ProfileDisconnecter(profile).execute();
|
new ProfileDisconnecter(profile).execute();
|
||||||
@ -257,7 +256,7 @@ public class ProfileService extends Service {
|
|||||||
@Override
|
@Override
|
||||||
public void saveProfile(Profile oldProfile, Profile newProfile) {
|
public void saveProfile(Profile oldProfile, Profile newProfile) {
|
||||||
if (oldProfile != null) {
|
if (oldProfile != null) {
|
||||||
if (!profiles.contains(oldProfile))
|
if (profiles.get(oldProfile.getName()) != oldProfile)
|
||||||
return;
|
return;
|
||||||
final boolean wasConnected = oldProfile.getIsConnected();
|
final boolean wasConnected = oldProfile.getIsConnected();
|
||||||
if (wasConnected)
|
if (wasConnected)
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package com.wireguard.android;
|
package com.wireguard.android;
|
||||||
|
|
||||||
import android.databinding.ObservableList;
|
import android.databinding.ObservableArrayMap;
|
||||||
|
|
||||||
import com.wireguard.config.Profile;
|
import com.wireguard.config.Profile;
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ public interface ProfileServiceInterface {
|
|||||||
*
|
*
|
||||||
* @return The list of known profiles.
|
* @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,
|
* Remove a profile from being managed by this service. If the profile is currently connected,
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
<variable
|
<variable
|
||||||
name="profiles"
|
name="profiles"
|
||||||
type="android.databinding.ObservableList<com.wireguard.config.Profile>" />
|
type="android.databinding.ObservableArrayMap<String, com.wireguard.config.Profile>" />
|
||||||
</data>
|
</data>
|
||||||
|
|
||||||
<ListView
|
<ListView
|
||||||
|
Loading…
Reference in New Issue
Block a user