From 892fdea2ea359830980d598b4fd2a016829906d4 Mon Sep 17 00:00:00 2001 From: "fps.io" Date: Tue, 23 Aug 2022 21:46:38 +0200 Subject: [PATCH] Add preference for enabling run conditions (workaround for #1822) (#1823) --- .../activities/SettingsActivity.java | 24 +++++++++++++++++-- .../syncthingandroid/service/Constants.java | 1 + .../service/RunConditionMonitor.java | 6 +++++ app/src/main/res/values/strings.xml | 2 +- app/src/main/res/xml/app_settings.xml | 7 +++--- 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java b/app/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java index a24158c8..fe356a62 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java @@ -11,6 +11,7 @@ import android.preference.CheckBoxPreference; import android.preference.EditTextPreference; import android.preference.ListPreference; import android.preference.Preference; +import android.preference.PreferenceGroup; import android.preference.PreferenceFragment; import android.preference.PreferenceManager; import android.preference.PreferenceScreen; @@ -101,7 +102,8 @@ public class SettingsActivity extends SyncthingActivity { @Inject NotificationHandler mNotificationHandler; @Inject SharedPreferences mPreferences; - private Preference mCategoryRunConditions; + private PreferenceGroup mCategoryRunConditions; + private CheckBoxPreference mRunConditions; private CheckBoxPreference mStartServiceOnBoot; private ListPreference mPowerSource; private CheckBoxPreference mRunOnMobileData; @@ -167,6 +169,8 @@ public class SettingsActivity extends SyncthingActivity { addPreferencesFromResource(R.xml.app_settings); PreferenceScreen screen = getPreferenceScreen(); + mRunConditions = + (CheckBoxPreference) findPreference(Constants.PREF_RUN_CONDITIONS); mStartServiceOnBoot = (CheckBoxPreference) findPreference(Constants.PREF_START_SERVICE_ON_BOOT); mPowerSource = @@ -234,9 +238,15 @@ public class SettingsActivity extends SyncthingActivity { mCategorySyncthingOptions = findPreference("category_syncthing_options"); setPreferenceCategoryChangeListener(mCategorySyncthingOptions, this::onSyncthingPreferenceChange); - mCategoryRunConditions = findPreference("category_run_conditions"); + mCategoryRunConditions = (PreferenceGroup) findPreference("category_run_conditions"); setPreferenceCategoryChangeListener(mCategoryRunConditions, this::onRunConditionPreferenceChange); + if (!mRunConditions.isChecked()) { + for (int index = 1; index < mCategoryRunConditions.getPreferenceCount(); ++index) { + mCategoryRunConditions.getPreference(index).setEnabled(false); + } + } + exportConfig.setOnPreferenceClickListener(this); importConfig.setOnPreferenceClickListener(this); @@ -363,6 +373,16 @@ public class SettingsActivity extends SyncthingActivity { public boolean onRunConditionPreferenceChange(Preference preference, Object o) { switch (preference.getKey()) { + case Constants.PREF_RUN_CONDITIONS: + boolean enabled = (Boolean) o; + for (int index = 1; index < mCategoryRunConditions.getPreferenceCount(); ++index) { + mCategoryRunConditions.getPreference(index).setEnabled(enabled); + } + if (enabled) { + mRunOnMeteredWifi.setEnabled(mRunOnWifi.isChecked()); + mWifiSsidWhitelist.setEnabled(mRunOnWifi.isChecked()); + } + break; case Constants.PREF_POWER_SOURCE: mPowerSource.setValue(o.toString()); preference.setSummary(mPowerSource.getEntry()); diff --git a/app/src/main/java/com/nutomic/syncthingandroid/service/Constants.java b/app/src/main/java/com/nutomic/syncthingandroid/service/Constants.java index 8284740f..75b52aed 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/service/Constants.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/service/Constants.java @@ -14,6 +14,7 @@ public class Constants { // Preferences - Run conditions public static final String PREF_START_SERVICE_ON_BOOT = "always_run_in_background"; + public static final String PREF_RUN_CONDITIONS = "static_run_conditions"; public static final String PREF_RUN_ON_MOBILE_DATA = "run_on_mobile_data"; public static final String PREF_RUN_ON_WIFI = "run_on_wifi"; public static final String PREF_RUN_ON_METERED_WIFI = "run_on_metered_wifi"; diff --git a/app/src/main/java/com/nutomic/syncthingandroid/service/RunConditionMonitor.java b/app/src/main/java/com/nutomic/syncthingandroid/service/RunConditionMonitor.java index c561c8de..0a6b286f 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/service/RunConditionMonitor.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/service/RunConditionMonitor.java @@ -169,6 +169,7 @@ public class RunConditionMonitor { */ private RunConditionCheckResult decideShouldRun() { // Get run conditions preferences. + boolean prefRunConditions= mPreferences.getBoolean(Constants.PREF_RUN_CONDITIONS, false); boolean prefRunOnMobileData= mPreferences.getBoolean(Constants.PREF_RUN_ON_MOBILE_DATA, false); boolean prefRunOnWifi= mPreferences.getBoolean(Constants.PREF_RUN_ON_WIFI, true); boolean prefRunOnMeteredWifi= mPreferences.getBoolean(Constants.PREF_RUN_ON_METERED_WIFI, false); @@ -179,6 +180,11 @@ public class RunConditionMonitor { boolean prefRespectPowerSaving = mPreferences.getBoolean(Constants.PREF_RESPECT_BATTERY_SAVING, true); boolean prefRespectMasterSync = mPreferences.getBoolean(Constants.PREF_RESPECT_MASTER_SYNC, false); + if (!prefRunConditions) { + Log.v(TAG, "decideShouldRun: !runConditions"); + return SHOULD_RUN; + } + List blockerReasons = new ArrayList<>(); // PREF_POWER_SOURCE diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 86b5ca7a..6dccc15a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -315,7 +315,7 @@ Please report any problems you encounter via Github. Experimental - Run Conditions + Enable run conditions Use the following options to decide when Syncthing will run. Run on mobile data diff --git a/app/src/main/res/xml/app_settings.xml b/app/src/main/res/xml/app_settings.xml index 13b870b7..fcba73be 100644 --- a/app/src/main/res/xml/app_settings.xml +++ b/app/src/main/res/xml/app_settings.xml @@ -12,12 +12,11 @@ android:title="@string/category_run_conditions" android:key="category_run_conditions"> - + android:summary="@string/run_conditions_summary" + android:defaultValue="true" />