Config/Interface/Peer: Make Parcelable

This allows saving the editor state across restarts.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Samuel Holland 2017-08-23 23:47:44 -05:00
parent 95384851cd
commit b2357e58e3
3 changed files with 123 additions and 9 deletions

View File

@ -5,6 +5,8 @@ import android.databinding.Bindable;
import android.databinding.Observable;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import com.wireguard.android.BR;
@ -14,6 +16,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;
/**
@ -21,7 +25,18 @@ import java.util.regex.Pattern;
*/
public class Config extends BaseObservable
implements Comparable<Config>, Copyable<Config>, Observable {
implements Comparable<Config>, Copyable<Config>, Observable, Parcelable {
public static final Parcelable.Creator<Config> CREATOR = new Parcelable.Creator<Config>() {
@Override
public Config createFromParcel(final Parcel in) {
return new Config(in);
}
@Override
public Config[] newArray(final int size) {
return new Config[size];
}
};
public static final int NAME_MAX_LENGTH = 16;
private static final Pattern PATTERN = Pattern.compile("^[a-zA-Z0-9_=+.-]{1,16}$");
@ -29,12 +44,26 @@ public class Config extends BaseObservable
return name.length() <= NAME_MAX_LENGTH && PATTERN.matcher(name).matches();
}
private final Interface iface = new Interface();
private final Interface iface;
private boolean isEnabled;
private boolean isPrimary;
private String name;
private final ObservableList<Peer> peers = new ObservableArrayList<>();
public Config() {
iface = new Interface();
}
protected Config(final Parcel in) {
iface = in.readParcelable(Interface.class.getClassLoader());
name = in.readString();
// The flattened peers must be recreated to associate them with this config.
final List<Peer> flattenedPeers = new LinkedList<>();
in.readTypedList(flattenedPeers, Peer.CREATOR);
for (final Peer peer : flattenedPeers)
addPeer(peer);
}
public Peer addPeer() {
final Peer peer = new Peer(this);
peers.add(peer);
@ -63,22 +92,23 @@ public class Config extends BaseObservable
public void copyFrom(final Config source) {
if (source != null) {
iface.copyFrom(source.iface);
isEnabled = source.isEnabled;
isPrimary = source.isPrimary;
name = source.name;
peers.clear();
for (final Peer peer : source.peers)
addPeer(peer);
} else {
iface.copyFrom(null);
isEnabled = false;
isPrimary = false;
name = null;
peers.clear();
}
notifyChange();
}
@Override
public int describeContents() {
return 0;
}
public Interface getInterface() {
return iface;
}
@ -157,4 +187,11 @@ public class Config extends BaseObservable
return "This configuration does not have a valid keypair.";
return null;
}
@Override
public void writeToParcel(final Parcel dest, final int flags) {
dest.writeParcelable(iface, flags);
dest.writeString(name);
dest.writeTypedList(peers);
}
}

View File

@ -3,16 +3,32 @@ package com.wireguard.config;
import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.databinding.Observable;
import android.os.Parcel;
import android.os.Parcelable;
import com.wireguard.android.BR;
import com.wireguard.crypto.Keypair;
import com.wireguard.crypto.KeyEncoding;
import com.wireguard.crypto.Keypair;
/**
* Represents the configuration for a WireGuard interface (an [Interface] block).
*/
public class Interface extends BaseObservable implements Copyable<Interface>, Observable {
public class Interface extends BaseObservable
implements Copyable<Interface>, Observable, Parcelable {
public static final Parcelable.Creator<Interface> CREATOR
= new Parcelable.Creator<Interface>() {
@Override
public Interface createFromParcel(final Parcel in) {
return new Interface(in);
}
@Override
public Interface[] newArray(final int size) {
return new Interface[size];
}
};
private String address;
private String dns;
private String listenPort;
@ -20,6 +36,18 @@ public class Interface extends BaseObservable implements Copyable<Interface>, Ob
private String mtu;
private String privateKey;
public Interface() {
// Do nothing.
}
protected Interface(final Parcel in) {
address = in.readString();
dns = in.readString();
listenPort = in.readString();
mtu = in.readString();
setPrivateKey(in.readString());
}
@Override
public Interface copy() {
final Interface copy = new Interface();
@ -45,6 +73,11 @@ public class Interface extends BaseObservable implements Copyable<Interface>, Ob
notifyChange();
}
@Override
public int describeContents() {
return 0;
}
public void generateKeypair() {
keypair = new Keypair();
privateKey = keypair.getPrivateKey();
@ -158,4 +191,13 @@ public class Interface extends BaseObservable implements Copyable<Interface>, Ob
sb.append(Attribute.PRIVATE_KEY.composeWith(privateKey));
return sb.toString();
}
@Override
public void writeToParcel(final Parcel dest, final int flags) {
dest.writeString(address);
dest.writeString(dns);
dest.writeString(listenPort);
dest.writeString(mtu);
dest.writeString(privateKey);
}
}

View File

@ -3,6 +3,8 @@ package com.wireguard.config;
import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.databinding.Observable;
import android.os.Parcel;
import android.os.Parcelable;
import com.android.databinding.library.baseAdapters.BR;
@ -10,7 +12,19 @@ import com.android.databinding.library.baseAdapters.BR;
* Represents the configuration for a WireGuard peer (a [Peer] block).
*/
public class Peer extends BaseObservable implements Copyable<Peer>, Observable {
public class Peer extends BaseObservable implements Copyable<Peer>, Observable, Parcelable {
public static final Parcelable.Creator<Peer> CREATOR = new Parcelable.Creator<Peer>() {
@Override
public Peer createFromParcel(final Parcel in) {
return new Peer(in);
}
@Override
public Peer[] newArray(final int size) {
return new Peer[size];
}
};
private String allowedIPs;
private final Config config;
private String endpoint;
@ -21,6 +35,14 @@ public class Peer extends BaseObservable implements Copyable<Peer>, Observable {
this.config = config;
}
protected Peer(final Parcel in) {
allowedIPs = in.readString();
config = null;
endpoint = in.readString();
persistentKeepalive = in.readString();
publicKey = in.readString();
}
@Override
public Peer copy() {
return copy(config);
@ -41,6 +63,11 @@ public class Peer extends BaseObservable implements Copyable<Peer>, Observable {
notifyChange();
}
@Override
public int describeContents() {
return 0;
}
@Bindable
public String getAllowedIPs() {
return allowedIPs;
@ -120,4 +147,12 @@ public class Peer extends BaseObservable implements Copyable<Peer>, Observable {
sb.append(Attribute.PUBLIC_KEY.composeWith(publicKey));
return sb.toString();
}
@Override
public void writeToParcel(final Parcel dest, final int flags) {
dest.writeString(allowedIPs);
dest.writeString(endpoint);
dest.writeString(persistentKeepalive);
dest.writeString(publicKey);
}
}