From d0dbb695eba7918f0bfa14dcb2de93db06101f30 Mon Sep 17 00:00:00 2001 From: Catfriend1 Date: Fri, 27 Jul 2018 00:52:36 +0200 Subject: [PATCH] Implement mPendingRunConditions in SettingsActivity to queue run condition changes until the user leaves the preferences screen after making changes. (fixes #1196) --- .../activities/SettingsActivity.java | 56 +++++++++++++------ .../service/RunConditionMonitor.java | 17 +----- .../service/SyncthingService.java | 12 ++++ 3 files changed, 51 insertions(+), 34 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 d980004d..1fa12d9a 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java @@ -88,6 +88,7 @@ public class SettingsActivity extends SyncthingActivity { @Inject NotificationHandler mNotificationHandler; @Inject SharedPreferences mPreferences; + private Preference mCategoryRunConditions; private CheckBoxPreference mAlwaysRunInBackground; private CheckBoxPreference mSyncOnlyCharging; private CheckBoxPreference mSyncOnlyWifi; @@ -126,6 +127,12 @@ public class SettingsActivity extends SyncthingActivity { private Boolean mPendingConfig = false; + /** + * Indicates if run conditions were changed and need to be + * re-evaluated when the user leaves the preferences screen. + */ + private Boolean mPendingRunConditions = false; + @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -210,10 +217,11 @@ public class SettingsActivity extends SyncthingActivity { Preference appVersion = screen.findPreference("app_version"); mSyncOnlyOnSSIDs.setEnabled(mSyncOnlyWifi.isChecked()); - setPreferenceCategoryChangeListener(findPreference("category_run_conditions"), this); mCategorySyncthingOptions = findPreference("category_syncthing_options"); setPreferenceCategoryChangeListener(mCategorySyncthingOptions, this::onSyncthingPreferenceChange); + mCategoryRunConditions = findPreference("category_run_conditions"); + setPreferenceCategoryChangeListener(mCategoryRunConditions, this::onRunConditionPreferenceChange); exportConfig.setOnPreferenceClickListener(this); importConfig.setOnPreferenceClickListener(this); @@ -307,6 +315,30 @@ public class SettingsActivity extends SyncthingActivity { } } + public boolean onRunConditionPreferenceChange(Preference preference, Object o) { + switch (preference.getKey()) { + case Constants.PREF_ALWAYS_RUN_IN_BACKGROUND: + boolean value = (Boolean) o; + mAlwaysRunInBackground.setSummary((value) + ? R.string.always_run_in_background_enabled + : R.string.always_run_in_background_disabled); + mSyncOnlyCharging.setEnabled(value); + mSyncOnlyWifi.setEnabled(value); + mSyncOnlyOnSSIDs.setEnabled(false); + // Uncheck items when disabled, so it is clear they have no effect. + if (!value) { + mSyncOnlyCharging.setChecked(false); + mSyncOnlyWifi.setChecked(false); + } + break; + case Constants.PREF_SYNC_ONLY_WIFI: + mSyncOnlyOnSSIDs.setEnabled((Boolean) o); + break; + } + mPendingRunConditions = true; + return true; + } + public boolean onSyncthingPreferenceChange(Preference preference, Object o) { Splitter splitter = Splitter.on(",").trimResults().omitEmptyStrings(); switch (preference.getKey()) { @@ -385,6 +417,11 @@ public class SettingsActivity extends SyncthingActivity { mPendingConfig = false; } } + if (mPendingRunConditions) { + if (mSyncthingService != null) { + mSyncthingService.evaluateRunConditions(); + } + } super.onStop(); } @@ -395,23 +432,6 @@ public class SettingsActivity extends SyncthingActivity { @Override public boolean onPreferenceChange(Preference preference, Object o) { switch (preference.getKey()) { - case Constants.PREF_ALWAYS_RUN_IN_BACKGROUND: - boolean value = (Boolean) o; - mAlwaysRunInBackground.setSummary((value) - ? R.string.always_run_in_background_enabled - : R.string.always_run_in_background_disabled); - mSyncOnlyCharging.setEnabled(value); - mSyncOnlyWifi.setEnabled(value); - mSyncOnlyOnSSIDs.setEnabled(false); - // Uncheck items when disabled, so it is clear they have no effect. - if (!value) { - mSyncOnlyCharging.setChecked(false); - mSyncOnlyWifi.setChecked(false); - } - break; - case Constants.PREF_SYNC_ONLY_WIFI: - mSyncOnlyOnSSIDs.setEnabled((Boolean) o); - break; case Constants.PREF_DEBUG_FACILITIES_ENABLED: mPendingConfig = true; break; 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 883b8b6e..a30a6ab3 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/service/RunConditionMonitor.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/service/RunConditionMonitor.java @@ -13,7 +13,6 @@ import android.net.wifi.WifiManager; import android.os.BatteryManager; import android.os.Build; import android.os.PowerManager; -import android.preference.PreferenceManager; import android.support.annotation.Nullable; import android.support.v4.content.LocalBroadcastManager; import android.util.Log; @@ -34,7 +33,7 @@ import javax.inject.Inject; * This information is actively read on instance creation, and then updated from intents * that are passed with {@link #ACTION_DEVICE_STATE_CHANGED}. */ -public class RunConditionMonitor implements SharedPreferences.OnSharedPreferenceChangeListener { +public class RunConditionMonitor { private static final String TAG = "RunConditionMonitor"; @@ -60,7 +59,6 @@ public class RunConditionMonitor implements SharedPreferences.OnSharedPreference Log.v(TAG, "Created new instance"); ((SyncthingApp) context.getApplicationContext()).component().inject(this); mContext = context; - mPreferences.registerOnSharedPreferenceChangeListener(this); mOnRunConditionChangedListener = listener; /** @@ -88,22 +86,9 @@ public class RunConditionMonitor implements SharedPreferences.OnSharedPreference public void shutdown() { Log.v(TAG, "Shutting down"); - mPreferences.unregisterOnSharedPreferenceChangeListener(this); mReceiverManager.unregisterAllReceivers(mContext); } - @Override - public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - List watched = Lists.newArrayList( - Constants.PREF_SYNC_ONLY_CHARGING, - Constants.PREF_SYNC_ONLY_WIFI, Constants.PREF_RESPECT_BATTERY_SAVING, - Constants.PREF_SYNC_ONLY_WIFI_SSIDS); - if (watched.contains(key)) { - // Force a re-evaluation of which run conditions apply according to the changed prefs. - updateShouldRunDecision(); - } - } - private class BatteryReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { diff --git a/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java b/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java index 12100335..53ea6da4 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java @@ -519,6 +519,18 @@ public class SyncthingService extends Service { return mApi; } + /** + * Force re-evaluating run conditions immediately e.g. after + * preferences were modified by {@link SettingsActivity}. + */ + public void evaluateRunConditions() { + if (mRunConditionMonitor == null) { + return; + } + Log.v(TAG, "Forced re-evaluating run conditions ..."); + mRunConditionMonitor.updateShouldRunDecision(); + } + /** * Register a listener for the syncthing API state changing. *