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:
parent
20922055db
commit
d0dbb695eb
3 changed files with 51 additions and 34 deletions
|
@ -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;
|
||||
|
|
|
@ -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<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 {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue