1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-22 12:21:15 +00:00

Add preference for enabling run conditions (workaround for #1822) (#1823)

This commit is contained in:
fps.io 2022-08-23 21:46:38 +02:00 committed by GitHub
parent 5c04dab7b2
commit 892fdea2ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 7 deletions

View file

@ -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());

View file

@ -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";

View file

@ -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<BlockerReason> blockerReasons = new ArrayList<>();
// PREF_POWER_SOURCE

View file

@ -315,7 +315,7 @@ Please report any problems you encounter via Github.</string>
<string name="category_experimental">Experimental</string>
<!-- Preference screen - Run conditions -->
<string name="run_conditions_title">Run Conditions</string>
<string name="run_conditions_title">Enable run conditions</string>
<string name="run_conditions_summary">Use the following options to decide when Syncthing will run.</string>
<string name="run_on_mobile_data_title">Run on mobile data</string>

View file

@ -12,12 +12,11 @@
android:title="@string/category_run_conditions"
android:key="category_run_conditions">
<Preference
<CheckBoxPreference
android:key="static_run_conditions"
android:persistent="false"
android:selectable="false"
android:title="@string/run_conditions_title"
android:summary="@string/run_conditions_summary"/>
android:summary="@string/run_conditions_summary"
android:defaultValue="true" />
<CheckBoxPreference
android:key="run_on_wifi"