summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/blinkt/openvpn/core/connection
diff options
context:
space:
mode:
authorcyBerta <cyberta@riseup.net>2021-01-18 12:32:47 +0100
committercyBerta <cyberta@riseup.net>2021-01-18 12:32:47 +0100
commit71fcf8577611d3163ea81307b04db6ba57950bb7 (patch)
treecdfc1f9d2cd92e1a8bd67f87bcb0266a9fdcae1e /app/src/main/java/de/blinkt/openvpn/core/connection
parent7e03f3066127d73270baa0fcba3a01a40d802feb (diff)
fix de-/serialization of Connection objects. Fixes VPN auto-restart on reboot due to always-on system settings or if system killed app due to low memory
Diffstat (limited to 'app/src/main/java/de/blinkt/openvpn/core/connection')
-rw-r--r--app/src/main/java/de/blinkt/openvpn/core/connection/Connection.java3
-rw-r--r--app/src/main/java/de/blinkt/openvpn/core/connection/ConnectionAdapter.java35
2 files changed, 38 insertions, 0 deletions
diff --git a/app/src/main/java/de/blinkt/openvpn/core/connection/Connection.java b/app/src/main/java/de/blinkt/openvpn/core/connection/Connection.java
index a318e55d..4cb9c0c7 100644
--- a/app/src/main/java/de/blinkt/openvpn/core/connection/Connection.java
+++ b/app/src/main/java/de/blinkt/openvpn/core/connection/Connection.java
@@ -7,9 +7,12 @@ package de.blinkt.openvpn.core.connection;
import android.text.TextUtils;
+import com.google.gson.annotations.JsonAdapter;
+
import java.io.Serializable;
import java.util.Locale;
+@JsonAdapter(ConnectionAdapter.class)
public abstract class Connection implements Serializable, Cloneable {
private String mServerName = "openvpn.example.com";
private String mServerPort = "1194";
diff --git a/app/src/main/java/de/blinkt/openvpn/core/connection/ConnectionAdapter.java b/app/src/main/java/de/blinkt/openvpn/core/connection/ConnectionAdapter.java
new file mode 100644
index 00000000..335ef34c
--- /dev/null
+++ b/app/src/main/java/de/blinkt/openvpn/core/connection/ConnectionAdapter.java
@@ -0,0 +1,35 @@
+package de.blinkt.openvpn.core.connection;
+
+import com.google.gson.JsonDeserializationContext;
+import com.google.gson.JsonDeserializer;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParseException;
+import com.google.gson.JsonSerializationContext;
+import com.google.gson.JsonSerializer;
+
+import java.lang.reflect.Type;
+
+// Adapter for Gson used to serialize and deserialize abstract Connection class, adds a property about the implemented class
+public class ConnectionAdapter implements JsonSerializer<Connection>, JsonDeserializer<Connection> {
+
+ public final static String META_TYPE = ConnectionAdapter.class.getSimpleName() + ".META_TYPE";
+ @Override
+ public Connection deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
+ JsonObject jsonObject = json.getAsJsonObject();
+ String className = jsonObject.get(META_TYPE).getAsString();
+ try {
+ Class<?> clz = Class.forName(className);
+ return context.deserialize(json, clz);
+ } catch (ClassNotFoundException e) {
+ throw new JsonParseException(e);
+ }
+ }
+
+ @Override
+ public JsonElement serialize(Connection src, Type typeOfSrc, JsonSerializationContext context) {
+ JsonElement json = context.serialize(src, src.getClass());
+ json.getAsJsonObject().addProperty(META_TYPE, src.getClass().getCanonicalName());
+ return json;
+ }
+}