VpnService: Use a string to remember the primary config
This allows simplifies the code a bit. Also, a few other minor changes. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
c2189a78b9
commit
27241d074e
@ -3,7 +3,6 @@ package com.wireguard.android;
|
|||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.databinding.ObservableArrayMap;
|
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
@ -29,9 +28,9 @@ import java.util.Set;
|
|||||||
|
|
||||||
public class VpnService extends Service
|
public class VpnService extends Service
|
||||||
implements SharedPreferences.OnSharedPreferenceChangeListener {
|
implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||||
private static final String KEY_ENABLED_CONFIGS = "enabled_configs";
|
public static final String KEY_ENABLED_CONFIGS = "enabled_configs";
|
||||||
private static final String KEY_PRIMARY_CONFIG = "primary_config";
|
public static final String KEY_PRIMARY_CONFIG = "primary_config";
|
||||||
private static final String KEY_RESTORE_ON_BOOT = "restore_on_boot";
|
public static final String KEY_RESTORE_ON_BOOT = "restore_on_boot";
|
||||||
private static final String TAG = "VpnService";
|
private static final String TAG = "VpnService";
|
||||||
|
|
||||||
private static VpnService instance;
|
private static VpnService instance;
|
||||||
@ -44,7 +43,7 @@ public class VpnService extends Service
|
|||||||
private final ObservableTreeMap<String, Config> configurations = new ObservableTreeMap<>();
|
private final ObservableTreeMap<String, Config> configurations = new ObservableTreeMap<>();
|
||||||
private final Set<String> enabledConfigs = new HashSet<>();
|
private final Set<String> enabledConfigs = new HashSet<>();
|
||||||
private SharedPreferences preferences;
|
private SharedPreferences preferences;
|
||||||
private Config primaryConfig;
|
private String primaryName;
|
||||||
private RootShell rootShell;
|
private RootShell rootShell;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -128,22 +127,30 @@ public class VpnService extends Service
|
|||||||
preferences.registerOnSharedPreferenceChangeListener(this);
|
preferences.registerOnSharedPreferenceChangeListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDestroy() {
|
||||||
|
preferences.unregisterOnSharedPreferenceChangeListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSharedPreferenceChanged(final SharedPreferences preferences,
|
public void onSharedPreferenceChanged(final SharedPreferences preferences,
|
||||||
final String key) {
|
final String key) {
|
||||||
Log.i(TAG, "Preference change trigger!");
|
|
||||||
if (!KEY_PRIMARY_CONFIG.equals(key))
|
if (!KEY_PRIMARY_CONFIG.equals(key))
|
||||||
return;
|
return;
|
||||||
final String name = preferences.getString(key, null);
|
final String newName = preferences.getString(key, null);
|
||||||
if (primaryConfig != null && !primaryConfig.getName().equals(name)) {
|
if (primaryName != null && !primaryName.equals(newName)) {
|
||||||
primaryConfig.setIsPrimary(false);
|
final Config oldConfig = configurations.get(primaryName);
|
||||||
primaryConfig = null;
|
if (oldConfig != null)
|
||||||
|
oldConfig.setIsPrimary(false);
|
||||||
}
|
}
|
||||||
if (primaryConfig == null && name != null) {
|
if (newName != null && !newName.equals(primaryName)) {
|
||||||
primaryConfig = configurations.get(name);
|
final Config newConfig = configurations.get(newName);
|
||||||
if (primaryConfig != null)
|
if (newConfig != null)
|
||||||
primaryConfig.setIsPrimary(true);
|
newConfig.setIsPrimary(true);
|
||||||
|
else
|
||||||
|
preferences.edit().remove(KEY_PRIMARY_CONFIG).apply();
|
||||||
}
|
}
|
||||||
|
primaryName = newName;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -279,12 +286,8 @@ public class VpnService extends Service
|
|||||||
return;
|
return;
|
||||||
for (final Config config : configs)
|
for (final Config config : configs)
|
||||||
configurations.put(config.getName(), config);
|
configurations.put(config.getName(), config);
|
||||||
final String primaryName = preferences.getString(KEY_PRIMARY_CONFIG, null);
|
// Run the handler to avoid duplicating the code here.
|
||||||
if (primaryName != null) {
|
onSharedPreferenceChanged(preferences, KEY_PRIMARY_CONFIG);
|
||||||
primaryConfig = configurations.get(primaryName);
|
|
||||||
if (primaryConfig != null)
|
|
||||||
primaryConfig.setIsPrimary(true);
|
|
||||||
}
|
|
||||||
if (preferences.getBoolean(KEY_RESTORE_ON_BOOT, false)) {
|
if (preferences.getBoolean(KEY_RESTORE_ON_BOOT, false)) {
|
||||||
final Set<String> configsToEnable =
|
final Set<String> configsToEnable =
|
||||||
preferences.getStringSet(KEY_ENABLED_CONFIGS, null);
|
preferences.getStringSet(KEY_ENABLED_CONFIGS, null);
|
||||||
@ -323,9 +326,10 @@ public class VpnService extends Service
|
|||||||
if (!result)
|
if (!result)
|
||||||
return;
|
return;
|
||||||
configurations.remove(config.getName());
|
configurations.remove(config.getName());
|
||||||
// This will get picked up by the preference change listener.
|
if (config.getName().equals(primaryName)) {
|
||||||
if (primaryConfig == config)
|
// This will get picked up by the preference change listener.
|
||||||
preferences.edit().putString(KEY_PRIMARY_CONFIG, null).apply();
|
preferences.edit().remove(KEY_PRIMARY_CONFIG).apply();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -391,6 +395,8 @@ public class VpnService extends Service
|
|||||||
}
|
}
|
||||||
newConfig.setIsEnabled(false);
|
newConfig.setIsEnabled(false);
|
||||||
configurations.put(newName, newConfig);
|
configurations.put(newName, newConfig);
|
||||||
|
if (isRename() && oldName.equals(primaryName))
|
||||||
|
preferences.edit().putString(KEY_PRIMARY_CONFIG, newName).apply();
|
||||||
if (shouldConnect)
|
if (shouldConnect)
|
||||||
new ConfigEnabler(newConfig).execute();
|
new ConfigEnabler(newConfig).execute();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user