1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-12-23 03:11:30 +00:00

Implement mPendingRunConditions in SettingsActivity to queue run condition changes until the user leaves the preferences screen after making changes. (fixes #1196)

This commit is contained in:
Catfriend1 2018-07-27 00:52:36 +02:00 committed by Audrius Butkevicius
parent 20922055db
commit d0dbb695eb
3 changed files with 51 additions and 34 deletions

View file

@ -88,6 +88,7 @@ public class SettingsActivity extends SyncthingActivity {
@Inject NotificationHandler mNotificationHandler; @Inject NotificationHandler mNotificationHandler;
@Inject SharedPreferences mPreferences; @Inject SharedPreferences mPreferences;
private Preference mCategoryRunConditions;
private CheckBoxPreference mAlwaysRunInBackground; private CheckBoxPreference mAlwaysRunInBackground;
private CheckBoxPreference mSyncOnlyCharging; private CheckBoxPreference mSyncOnlyCharging;
private CheckBoxPreference mSyncOnlyWifi; private CheckBoxPreference mSyncOnlyWifi;
@ -126,6 +127,12 @@ public class SettingsActivity extends SyncthingActivity {
private Boolean mPendingConfig = false; 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 @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -210,10 +217,11 @@ public class SettingsActivity extends SyncthingActivity {
Preference appVersion = screen.findPreference("app_version"); Preference appVersion = screen.findPreference("app_version");
mSyncOnlyOnSSIDs.setEnabled(mSyncOnlyWifi.isChecked()); mSyncOnlyOnSSIDs.setEnabled(mSyncOnlyWifi.isChecked());
setPreferenceCategoryChangeListener(findPreference("category_run_conditions"), this);
mCategorySyncthingOptions = findPreference("category_syncthing_options"); mCategorySyncthingOptions = findPreference("category_syncthing_options");
setPreferenceCategoryChangeListener(mCategorySyncthingOptions, this::onSyncthingPreferenceChange); setPreferenceCategoryChangeListener(mCategorySyncthingOptions, this::onSyncthingPreferenceChange);
mCategoryRunConditions = findPreference("category_run_conditions");
setPreferenceCategoryChangeListener(mCategoryRunConditions, this::onRunConditionPreferenceChange);
exportConfig.setOnPreferenceClickListener(this); exportConfig.setOnPreferenceClickListener(this);
importConfig.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) { public boolean onSyncthingPreferenceChange(Preference preference, Object o) {
Splitter splitter = Splitter.on(",").trimResults().omitEmptyStrings(); Splitter splitter = Splitter.on(",").trimResults().omitEmptyStrings();
switch (preference.getKey()) { switch (preference.getKey()) {
@ -385,6 +417,11 @@ public class SettingsActivity extends SyncthingActivity {
mPendingConfig = false; mPendingConfig = false;
} }
} }
if (mPendingRunConditions) {
if (mSyncthingService != null) {
mSyncthingService.evaluateRunConditions();
}
}
super.onStop(); super.onStop();
} }
@ -395,23 +432,6 @@ public class SettingsActivity extends SyncthingActivity {
@Override @Override
public boolean onPreferenceChange(Preference preference, Object o) { public boolean onPreferenceChange(Preference preference, Object o) {
switch (preference.getKey()) { 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: case Constants.PREF_DEBUG_FACILITIES_ENABLED:
mPendingConfig = true; mPendingConfig = true;
break; break;

View file

@ -13,7 +13,6 @@ import android.net.wifi.WifiManager;
import android.os.BatteryManager; import android.os.BatteryManager;
import android.os.Build; import android.os.Build;
import android.os.PowerManager; import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
import android.util.Log; 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 * This information is actively read on instance creation, and then updated from intents
* that are passed with {@link #ACTION_DEVICE_STATE_CHANGED}. * that are passed with {@link #ACTION_DEVICE_STATE_CHANGED}.
*/ */
public class RunConditionMonitor implements SharedPreferences.OnSharedPreferenceChangeListener { public class RunConditionMonitor {
private static final String TAG = "RunConditionMonitor"; private static final String TAG = "RunConditionMonitor";
@ -60,7 +59,6 @@ public class RunConditionMonitor implements SharedPreferences.OnSharedPreference
Log.v(TAG, "Created new instance"); Log.v(TAG, "Created new instance");
((SyncthingApp) context.getApplicationContext()).component().inject(this); ((SyncthingApp) context.getApplicationContext()).component().inject(this);
mContext = context; mContext = context;
mPreferences.registerOnSharedPreferenceChangeListener(this);
mOnRunConditionChangedListener = listener; mOnRunConditionChangedListener = listener;
/** /**
@ -88,22 +86,9 @@ public class RunConditionMonitor implements SharedPreferences.OnSharedPreference
public void shutdown() { public void shutdown() {
Log.v(TAG, "Shutting down"); Log.v(TAG, "Shutting down");
mPreferences.unregisterOnSharedPreferenceChangeListener(this);
mReceiverManager.unregisterAllReceivers(mContext); mReceiverManager.unregisterAllReceivers(mContext);
} }
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
List<String> 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 { private class BatteryReceiver extends BroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {

View file

@ -519,6 +519,18 @@ public class SyncthingService extends Service {
return mApi; 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. * Register a listener for the syncthing API state changing.
* *