From cefd9dcfc062693980693801ea65c6b3c30580ba Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 9 Oct 2017 13:11:15 +0900 Subject: [PATCH] Don't disable all settings when Syncthing is disabled --- .../activities/SettingsActivity.java | 40 ++++++++++--------- .../service/SyncthingService.java | 4 ++ src/main/res/xml/app_settings.xml | 1 + 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java b/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java index 068ddbff..50d5fb9e 100644 --- a/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java @@ -59,6 +59,7 @@ public class SettingsActivity extends SyncthingActivity { private CheckBoxPreference mSyncOnlyWifi; private WifiSsidPreference mSyncOnlyOnSSIDs; + private Preference mCategorySyncthingOptions; private EditTextPreference mDeviceName; private EditTextPreference mListenAddresses; private EditTextPreference mMaxRecvKbps; @@ -71,6 +72,8 @@ public class SettingsActivity extends SyncthingActivity { private EditTextPreference mAddress; private CheckBoxPreference mUrAccepted; + private Preference mCategoryBackup; + private CheckBoxPreference mUseRoot; private Preference mSyncthingVersion; @@ -134,6 +137,7 @@ public class SettingsActivity extends SyncthingActivity { mAddress = (EditTextPreference) findPreference("address"); mUrAccepted = (CheckBoxPreference) findPreference("urAccepted"); + mCategoryBackup = findPreference("category_backup"); Preference exportConfig = findPreference("export_config"); Preference importConfig = findPreference("import_config"); @@ -152,8 +156,8 @@ public class SettingsActivity extends SyncthingActivity { mSyncOnlyOnSSIDs.setEnabled(mSyncOnlyWifi.isChecked()); setPreferenceCategoryChangeListener(findPreference("category_run_conditions"), this); - setPreferenceCategoryChangeListener( - findPreference("category_syncthing_options"), this::onSyncthingPreferenceChange); + mCategorySyncthingOptions = findPreference("category_syncthing_options"); + setPreferenceCategoryChangeListener(mCategorySyncthingOptions, this::onSyncthingPreferenceChange); exportConfig.setOnPreferenceClickListener(this); importConfig.setOnPreferenceClickListener(this); @@ -163,9 +167,9 @@ public class SettingsActivity extends SyncthingActivity { stReset.setOnPreferenceClickListener(this); mUseRoot.setOnPreferenceClickListener(this); - useWakelock.setOnPreferenceChangeListener(this::onRequireRestart); - foregroundService.setOnPreferenceChangeListener(this::onRequireRestart); - useTor.setOnPreferenceChangeListener(this::onRequireRestart); + useWakelock.setOnPreferenceChangeListener((p, o) -> requireRestart()); + foregroundService.setOnPreferenceChangeListener((p, o) -> requireRestart()); + useTor.setOnPreferenceChangeListener((p, o) -> requireRestart()); try { appVersion.setSummary(getActivity().getPackageManager() @@ -195,14 +199,11 @@ public class SettingsActivity extends SyncthingActivity { @Override public void onApiChange(SyncthingService.State currentState) { boolean syncthingActive = currentState == SyncthingService.State.ACTIVE; - boolean enableAllPrefs = syncthingActive && mSyncthingService.getApi().isConfigLoaded(); - PreferenceScreen ps = getPreferenceScreen(); - for (int i = 0; i < ps.getPreferenceCount(); i++) { - Preference p = ps.getPreference(i); - p.setEnabled(enableAllPrefs || "category_run_conditions".equals(p.getKey())); - } + boolean isSyncthingRunning = syncthingActive && mSyncthingService.getApi().isConfigLoaded(); + mCategorySyncthingOptions.setEnabled(isSyncthingRunning); + mCategoryBackup.setEnabled(isSyncthingRunning); - if (!enableAllPrefs) + if (!isSyncthingRunning) return; mApi = mSyncthingService.getApi(); @@ -273,8 +274,11 @@ public class SettingsActivity extends SyncthingActivity { return true; } - public boolean onRequireRestart(Preference preference, Object o) { - mSyncthingService.getApi().showRestartDialog(getActivity()); + public boolean requireRestart() { + if (mSyncthingService.getCurrentState() != SyncthingService.State.DISABLED && + mSyncthingService.getApi() != null) { + mSyncthingService.getApi().showRestartDialog(getActivity()); + } return true; } @@ -304,7 +308,7 @@ public class SettingsActivity extends SyncthingActivity { break; case KEY_STTRACE: if (((String) o).matches("[0-9a-z, ]*")) - mSyncthingService.getApi().showRestartDialog(getActivity()); + requireRestart(); else { Toast.makeText(getActivity(), R.string.toast_invalid_sttrace, Toast.LENGTH_SHORT) .show(); @@ -313,7 +317,7 @@ public class SettingsActivity extends SyncthingActivity { break; case "environment_variables": if (((String) o).matches("^(\\w+=[\\w:/\\.]+)?( \\w+=[\\w:/\\.]+)*$")) { - mSyncthingService.getApi().showRestartDialog(getActivity()); + requireRestart(); } else { Toast.makeText(getActivity(), R.string.toast_invalid_environment_variables, Toast.LENGTH_SHORT) @@ -336,7 +340,7 @@ public class SettingsActivity extends SyncthingActivity { new TestRootTask().execute(); } else { new Thread(() -> Util.fixAppDataPermissions(getActivity())).start(); - mSyncthingService.getApi().showRestartDialog(getActivity()); + requireRestart(); } return true; case KEY_EXPORT_CONFIG: @@ -402,7 +406,7 @@ public class SettingsActivity extends SyncthingActivity { @Override protected void onPostExecute(Boolean haveRoot) { if (haveRoot) { - mSyncthingService.getApi().showRestartDialog(getActivity()); + requireRestart(); mUseRoot.setChecked(true); } else { Toast.makeText(getActivity(), R.string.toast_root_denied, Toast.LENGTH_SHORT) diff --git a/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java b/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java index 880f990a..d1d77713 100644 --- a/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java +++ b/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java @@ -425,6 +425,10 @@ public class SyncthingService extends Service implements return sp.getBoolean(Constants.PREF_ALWAYS_RUN_IN_BACKGROUND, false); } + public State getCurrentState() { + return mCurrentState; + } + /** * Exports the local config and keys to {@link Constants#EXPORT_PATH}. */ diff --git a/src/main/res/xml/app_settings.xml b/src/main/res/xml/app_settings.xml index 606a06ca..8de4ba89 100644 --- a/src/main/res/xml/app_settings.xml +++ b/src/main/res/xml/app_settings.xml @@ -133,6 +133,7 @@