From f2071be81c37d7c40611ba3d43275d76f7e254d7 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 25 Aug 2017 20:12:14 +0900 Subject: [PATCH] Allow setting arbitary environment variables (fixes #788) --- .../activities/SettingsActivity.java | 18 +++++++++++++++--- .../service/SyncthingRunnable.java | 12 ++++++++++++ src/main/res/values/strings.xml | 4 ++++ src/main/res/xml/app_settings.xml | 6 ++++++ 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java b/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java index 6b5ff217..89300648 100644 --- a/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java @@ -130,10 +130,11 @@ public class SettingsActivity extends SyncthingActivity { Preference exportConfig = findPreference("export_config"); Preference importConfig = findPreference("import_config"); - Preference stTrace = findPreference("sttrace"); - Preference stReset = findPreference("streset"); + Preference stTrace = findPreference("sttrace"); + Preference environmentVariables = findPreference("environment_variables"); + Preference stReset = findPreference("streset"); - mUseRoot = (CheckBoxPreference) findPreference(SyncthingService.PREF_USE_ROOT); + mUseRoot = (CheckBoxPreference) findPreference(SyncthingService.PREF_USE_ROOT); Preference useWakelock = findPreference(SyncthingService.PREF_USE_WAKE_LOCK); Preference foregroundService = findPreference("run_as_foreground_service"); Preference useTor = findPreference("use_tor"); @@ -151,6 +152,7 @@ public class SettingsActivity extends SyncthingActivity { importConfig.setOnPreferenceClickListener(this); stTrace.setOnPreferenceChangeListener(this); + environmentVariables.setOnPreferenceChangeListener(this); stReset.setOnPreferenceClickListener(this); mUseRoot.setOnPreferenceChangeListener(this); @@ -298,6 +300,16 @@ public class SettingsActivity extends SyncthingActivity { return false; } break; + case "environment_variables": + if (((String) o).matches("^(\\w+=[\\w:/\\.]+)?( \\w+=[\\w:/\\.]+)*$")) { + mSyncthingService.getApi().showRestartDialog(getActivity()); + } + else { + Toast.makeText(getActivity(), R.string.toast_invalid_environment_variables, Toast.LENGTH_SHORT) + .show(); + return false; + } + break; } return true; diff --git a/src/main/java/com/nutomic/syncthingandroid/service/SyncthingRunnable.java b/src/main/java/com/nutomic/syncthingandroid/service/SyncthingRunnable.java index ac9e65ae..42dab85b 100644 --- a/src/main/java/com/nutomic/syncthingandroid/service/SyncthingRunnable.java +++ b/src/main/java/com/nutomic/syncthingandroid/service/SyncthingRunnable.java @@ -142,6 +142,7 @@ public class SyncthingRunnable implements Runnable { } if (sp.getBoolean("use_legacy_hashing", false)) env.put("STHASHING", "standard"); + putCustomEnvironmentVariables(env, sp); process = pb.start(); mSyncthing.set(process); @@ -200,6 +201,17 @@ public class SyncthingRunnable implements Runnable { } } + private void putCustomEnvironmentVariables(Map environment, SharedPreferences sp) { + String customEnvironment = sp.getString("environment_variables", null); + if (TextUtils.isEmpty(customEnvironment)) + return; + + for (String e : customEnvironment.split(" ")) { + String[] e2 = e.split("="); + environment.put(e2[0], e2[1]); + } + } + /** * Returns true if root is available and enabled in settings. */ diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index c88534b9..7e532cae 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -347,9 +347,13 @@ Please report any problems you encounter via Github. Debug Options + Environment variables + Only 0-9, a-z and \',\' are allowed in STTRACE options + Value is not a valid environment variable string + Reset Database diff --git a/src/main/res/xml/app_settings.xml b/src/main/res/xml/app_settings.xml index ee70fd67..45cf3499 100644 --- a/src/main/res/xml/app_settings.xml +++ b/src/main/res/xml/app_settings.xml @@ -159,6 +159,12 @@ android:singleLine="true" android:inputType="textNoSuggestions"/> + +