From b753ae09ca88e60891e2b6da7022e0059b33baf6 Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Wed, 16 Aug 2017 04:36:12 -0500 Subject: VpnService: Remember and optionally restore enabled configs Signed-off-by: Jason A. Donenfeld --- .../main/java/com/wireguard/android/VpnService.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/app/src/main/java/com/wireguard/android/VpnService.java b/app/src/main/java/com/wireguard/android/VpnService.java index 057f1906..fd2aadc3 100644 --- a/app/src/main/java/com/wireguard/android/VpnService.java +++ b/app/src/main/java/com/wireguard/android/VpnService.java @@ -18,8 +18,10 @@ import java.io.FilenameFilter; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Collections; +import java.util.HashSet; import java.util.LinkedList; import java.util.List; +import java.util.Set; /** * Service that handles config state coordination and all background processing for the application. @@ -27,7 +29,9 @@ import java.util.List; public class VpnService extends Service implements SharedPreferences.OnSharedPreferenceChangeListener { + private static final String KEY_ENABLED_CONFIGS = "enabled_configs"; private static final String KEY_PRIMARY_CONFIG = "primary_config"; + private static final String KEY_RESTORE_ON_BOOT = "restore_on_boot"; private static final String TAG = "VpnService"; private static VpnService instance; @@ -38,6 +42,7 @@ public class VpnService extends Service private final IBinder binder = new Binder(); private final ObservableArrayMap configurations = new ObservableArrayMap<>(); + private final Set enabledConfigs = new HashSet<>(); private SharedPreferences preferences; private Config primaryConfig; private RootShell rootShell; @@ -205,6 +210,8 @@ public class VpnService extends Service if (!result) return; config.setIsEnabled(false); + enabledConfigs.remove(config.getName()); + preferences.edit().putStringSet(KEY_ENABLED_CONFIGS, enabledConfigs).apply(); } } @@ -227,6 +234,8 @@ public class VpnService extends Service if (!result) return; config.setIsEnabled(true); + enabledConfigs.add(config.getName()); + preferences.edit().putStringSet(KEY_ENABLED_CONFIGS, enabledConfigs).apply(); } } @@ -276,6 +285,17 @@ public class VpnService extends Service if (primaryConfig != null) primaryConfig.setIsPrimary(true); } + if (preferences.getBoolean(KEY_RESTORE_ON_BOOT, false)) { + final Set configsToEnable = + preferences.getStringSet(KEY_ENABLED_CONFIGS, null); + if (configsToEnable != null) { + for (final String name : configsToEnable) { + final Config config = configurations.get(name); + if (config != null && !config.isEnabled()) + new ConfigEnabler(config).execute(); + } + } + } } } -- cgit v1.2.3