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..6d7288cd 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java @@ -89,7 +89,7 @@ public class SettingsActivity extends SyncthingActivity { @Inject SharedPreferences mPreferences; private CheckBoxPreference mAlwaysRunInBackground; - private CheckBoxPreference mSyncOnlyCharging; + private ListPreference mPowerSource; private CheckBoxPreference mSyncOnlyWifi; private WifiSsidPreference mSyncOnlyOnSSIDs; @@ -147,15 +147,13 @@ public class SettingsActivity extends SyncthingActivity { PreferenceScreen screen = getPreferenceScreen(); mAlwaysRunInBackground = (CheckBoxPreference) findPreference(Constants.PREF_ALWAYS_RUN_IN_BACKGROUND); - mSyncOnlyCharging = - (CheckBoxPreference) findPreference(Constants.PREF_SYNC_ONLY_CHARGING); + mPowerSource = + (ListPreference) findPreference(Constants.PREF_POWER_SOURCE); mSyncOnlyWifi = (CheckBoxPreference) findPreference(Constants.PREF_SYNC_ONLY_WIFI); mSyncOnlyOnSSIDs = (WifiSsidPreference) findPreference(Constants.PREF_SYNC_ONLY_WIFI_SSIDS); - mSyncOnlyCharging.setEnabled(mAlwaysRunInBackground.isChecked()); - mSyncOnlyWifi.setEnabled(mAlwaysRunInBackground.isChecked()); mSyncOnlyOnSSIDs.setEnabled(mSyncOnlyWifi.isChecked()); ListPreference languagePref = (ListPreference) findPreference(Languages.PREFERENCE_LANGUAGE); @@ -236,6 +234,7 @@ public class SettingsActivity extends SyncthingActivity { /* Initialize summaries */ mPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); + screen.findPreference(Constants.PREF_POWER_SOURCE).setSummary(mPowerSource.getEntry()); handleSocksProxyPreferenceChange(screen.findPreference(Constants.PREF_SOCKS_PROXY_ADDRESS), mPreferences.getString(Constants.PREF_SOCKS_PROXY_ADDRESS, "")); handleHttpProxyPreferenceChange(screen.findPreference(Constants.PREF_HTTP_PROXY_ADDRESS), mPreferences.getString(Constants.PREF_HTTP_PROXY_ADDRESS, "")); @@ -395,22 +394,9 @@ 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); + case Constants.PREF_POWER_SOURCE: + mPowerSource.setValue(o.toString()); + preference.setSummary(mPowerSource.getEntry()); break; case Constants.PREF_DEBUG_FACILITIES_ENABLED: mPendingConfig = true; diff --git a/app/src/main/java/com/nutomic/syncthingandroid/service/Constants.java b/app/src/main/java/com/nutomic/syncthingandroid/service/Constants.java index 54e7dfbe..d8894911 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/service/Constants.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/service/Constants.java @@ -13,7 +13,8 @@ public class Constants { public static final String PREF_ALWAYS_RUN_IN_BACKGROUND = "always_run_in_background"; public static final String PREF_SYNC_ONLY_WIFI = "sync_only_wifi"; public static final String PREF_SYNC_ONLY_WIFI_SSIDS = "sync_only_wifi_ssids_set"; - public static final String PREF_SYNC_ONLY_CHARGING = "sync_only_charging"; + // to_be_deleted public static final String PREF_SYNC_ONLY_CHARGING = "sync_only_charging"; + public static final String PREF_POWER_SOURCE = "power_source"; public static final String PREF_RESPECT_BATTERY_SAVING = "respect_battery_saving"; public static final String PREF_USE_ROOT = "use_root"; public static final String PREF_NOTIFICATION_TYPE = "notification_type"; 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 18fe8521..75cc75b1 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/service/RunConditionMonitor.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/service/RunConditionMonitor.java @@ -38,6 +38,10 @@ public class RunConditionMonitor implements SharedPreferences.OnSharedPreference private static final String TAG = "RunConditionMonitor"; + private static final String POWER_SOURCE_AC_BATTERY = "ac_and_battery_power"; + private static final String POWER_SOURCE_AC = "ac_power"; + private static final String POWER_SOURCE_BATTERY = "battery_power"; + public interface OnRunConditionChangedListener { void onRunConditionChanged(boolean shouldRun); } @@ -95,7 +99,7 @@ public class RunConditionMonitor implements SharedPreferences.OnSharedPreference @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { List watched = Lists.newArrayList( - Constants.PREF_SYNC_ONLY_CHARGING, + Constants.PREF_POWER_SOURCE, Constants.PREF_SYNC_ONLY_WIFI, Constants.PREF_RESPECT_BATTERY_SAVING, Constants.PREF_SYNC_ONLY_WIFI_SSIDS); if (watched.contains(key)) { @@ -152,7 +156,7 @@ public class RunConditionMonitor implements SharedPreferences.OnSharedPreference boolean prefAlwaysRunInBackground = mPreferences.getBoolean(Constants.PREF_ALWAYS_RUN_IN_BACKGROUND, false); boolean prefRespectPowerSaving = mPreferences.getBoolean(Constants.PREF_RESPECT_BATTERY_SAVING, true); boolean prefRunOnlyOnWifi= mPreferences.getBoolean(Constants.PREF_SYNC_ONLY_WIFI, false); - boolean prefRunOnlyWhenCharging = mPreferences.getBoolean(Constants.PREF_SYNC_ONLY_CHARGING, false); + String prefPowerSource = mPreferences.getString(Constants.PREF_POWER_SOURCE, POWER_SOURCE_AC_BATTERY); Set whitelistedWifiSsids = mPreferences.getStringSet(Constants.PREF_SYNC_ONLY_WIFI_SSIDS, new HashSet<>()); boolean prefWifiWhitelistEnabled = !whitelistedWifiSsids.isEmpty(); @@ -173,10 +177,23 @@ public class RunConditionMonitor implements SharedPreferences.OnSharedPreference return true; } - // Run only when charging. - if (prefRunOnlyWhenCharging && !isCharging()) { - Log.v(TAG, "decideShouldRun: prefRunOnlyWhenCharging && !isCharging"); - return false; + // PREF_POWER_SOURCE + switch (prefPowerSource) { + case POWER_SOURCE_AC: + if (!isOnAcPower()) { + Log.v(TAG, "decideShouldRun: POWER_SOURCE_AC && !isOnAcPower"); + return false; + } + break; + case POWER_SOURCE_BATTERY: + if (isOnAcPower()) { + Log.v(TAG, "decideShouldRun: POWER_SOURCE_BATTERY && isOnAcPower"); + return false; + } + break; + case POWER_SOURCE_AC_BATTERY: + default: + break; } // Run only on wifi. @@ -206,7 +223,7 @@ public class RunConditionMonitor implements SharedPreferences.OnSharedPreference /** * Functions for run condition information retrieval. */ - private boolean isCharging() { + private boolean isOnAcPower() { Intent batteryIntent = mContext.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); int status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); return status == BatteryManager.BATTERY_STATUS_CHARGING || diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 1377931f..feb79c9a 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -1,12 +1,20 @@ + never metadata always + + + ac_and_battery_power + ac_power + battery_power + + normal low_priority diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 10b07804..455524d5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -285,15 +285,27 @@ Please report any problems you encounter via Github. Debug Experimental - Always run in background + + to_be_deleted + to_be_deleted + to_be_deleted + to_be_deleted - - Syncthing always runs in the background, according to preferences below. + Service setting + Running Syncthing as a service adds a persistent notification to prevent it from being ended by android. Checking this option starts syncthing automatically when the phone boots. - - Syncthing only runs when explicitly started, and can be stopped by menu button. + Run as background service - Run only when charging + Run Conditions + Use the following options to decide when the syncthing UI and syncing will be available. + + Run when device powered by + + + AC and battery power + AC power + Battery power + Run only on wifi diff --git a/app/src/main/res/xml/app_settings.xml b/app/src/main/res/xml/app_settings.xml index 1cc3d319..3b257e39 100644 --- a/app/src/main/res/xml/app_settings.xml +++ b/app/src/main/res/xml/app_settings.xml @@ -5,16 +5,33 @@ android:title="@string/category_run_conditions" android:key="category_run_conditions"> - + + + + +