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 1041b09f..0512a309 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/activities/SettingsActivity.java @@ -103,6 +103,7 @@ public class SettingsActivity extends SyncthingActivity { private ListPreference mPowerSource; private CheckBoxPreference mRunOnMobileData; private CheckBoxPreference mRunOnWifi; + private CheckBoxPreference mRunOnMeteredWifi; private CheckBoxPreference mRunInFlightMode; private WifiSsidPreference mWifiSsidWhitelist; @@ -166,6 +167,8 @@ public class SettingsActivity extends SyncthingActivity { (CheckBoxPreference) findPreference(Constants.PREF_RUN_ON_WIFI); mRunOnWifi = (CheckBoxPreference) findPreference(Constants.PREF_RUN_ON_WIFI); + mRunOnMeteredWifi = + (CheckBoxPreference) findPreference(Constants.PREF_RUN_ON_METERED_WIFI); mWifiSsidWhitelist = (WifiSsidPreference) findPreference(Constants.PREF_WIFI_SSID_WHITELIST); mRunInFlightMode = @@ -222,6 +225,7 @@ public class SettingsActivity extends SyncthingActivity { mSyncthingVersion = findPreference("syncthing_version"); Preference appVersion = screen.findPreference("app_version"); + mRunOnMeteredWifi.setEnabled(mRunOnWifi.isChecked()); mWifiSsidWhitelist.setEnabled(mRunOnWifi.isChecked()); setPreferenceCategoryChangeListener(findPreference("category_run_conditions"), this); @@ -443,6 +447,7 @@ public class SettingsActivity extends SyncthingActivity { preference.setSummary(mPowerSource.getEntry()); break; case Constants.PREF_RUN_ON_WIFI: + mRunOnMeteredWifi.setEnabled((Boolean) o); mWifiSsidWhitelist.setEnabled((Boolean) o); break; case Constants.PREF_WIFI_SSID_WHITELIST: 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 96d841fe..76aeec15 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/service/Constants.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/service/Constants.java @@ -13,10 +13,8 @@ public class Constants { public static final String PREF_ALWAYS_RUN_IN_BACKGROUND = "always_run_in_background"; public static final String PREF_RUN_ON_MOBILE_DATA = "run_on_mobile_data"; public static final String PREF_RUN_ON_WIFI = "run_on_wifi"; - // to_be_deleted public static final String PREF_SYNC_ONLY_WIFI = "sync_only_wifi"; + public static final String PREF_RUN_ON_METERED_WIFI = "run_on_metered_wifi"; public static final String PREF_WIFI_SSID_WHITELIST = "wifi_ssid_whitelist"; - // to_be_deleted public static final String PREF_SYNC_ONLY_WIFI_SSIDS = "sync_only_wifi_ssids_set"; - // 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_RUN_IN_FLIGHT_MODE = "run_in_flight_mode"; public static final String PREF_RESPECT_BATTERY_SAVING = "respect_battery_saving"; 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 c8a26247..4baef653 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/service/RunConditionMonitor.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/service/RunConditionMonitor.java @@ -101,6 +101,7 @@ public class RunConditionMonitor implements SharedPreferences.OnSharedPreference List watched = Lists.newArrayList( Constants.PREF_RUN_ON_MOBILE_DATA, Constants.PREF_RUN_ON_WIFI, + Constants.PREF_RUN_ON_METERED_WIFI, Constants.PREF_WIFI_SSID_WHITELIST, Constants.PREF_POWER_SOURCE, Constants.PREF_RUN_IN_FLIGHT_MODE, @@ -159,10 +160,11 @@ public class RunConditionMonitor implements SharedPreferences.OnSharedPreference // Get run conditions preferences. boolean prefRunOnMobileData= mPreferences.getBoolean(Constants.PREF_RUN_ON_MOBILE_DATA, false); boolean prefRunOnWifi= mPreferences.getBoolean(Constants.PREF_RUN_ON_WIFI, true); - String prefPowerSource = mPreferences.getString(Constants.PREF_POWER_SOURCE, POWER_SOURCE_AC_BATTERY); + boolean prefRunOnMeteredWifi= mPreferences.getBoolean(Constants.PREF_RUN_ON_METERED_WIFI, false); Set whitelistedWifiSsids = mPreferences.getStringSet(Constants.PREF_WIFI_SSID_WHITELIST, new HashSet<>()); boolean prefWifiWhitelistEnabled = !whitelistedWifiSsids.isEmpty(); boolean prefRunInFlightMode = mPreferences.getBoolean(Constants.PREF_RUN_IN_FLIGHT_MODE, false); + String prefPowerSource = mPreferences.getString(Constants.PREF_POWER_SOURCE, POWER_SOURCE_AC_BATTERY); boolean prefRespectPowerSaving = mPreferences.getBoolean(Constants.PREF_RESPECT_BATTERY_SAVING, true); boolean prefAlwaysRunInBackground = mPreferences.getBoolean(Constants.PREF_ALWAYS_RUN_IN_BACKGROUND, false); @@ -193,21 +195,26 @@ public class RunConditionMonitor implements SharedPreferences.OnSharedPreference } } - // Run on mobile data or tethered connection that is marked as metered. - if (prefRunOnMobileData && (isMobileDataConnection() || isMeteredNetworkConnection())) { - Log.v(TAG, "decideShouldRun: prefRunOnMobileData && (isMobileDataConnection || isMeteredNetworkConnection"); + // Run on mobile data. + if (prefRunOnMobileData && isMobileDataConnection()) { + Log.v(TAG, "decideShouldRun: prefRunOnMobileData && isMobileDataConnection"); return true; } // Run on wifi. if (prefRunOnWifi && isWifiOrEthernetConnection()) { - if (!prefWifiWhitelistEnabled) { - Log.v(TAG, "decideShouldRun: prefRunOnWifi && isWifiOrEthernetConnection && !prefWifiWhitelistEnabled"); - return true; - } - if (isWifiConnectionWhitelisted(whitelistedWifiSsids)) { - Log.v(TAG, "decideShouldRun: prefRunOnWifi && isWifiOrEthernetConnection && prefWifiWhitelistEnabled && isWifiConnectionWhitelisted"); - return true; + if (prefRunOnMeteredWifi) { + // Check if we are on metered wifi and if wifi whitelist run condition is met. + if (isMeteredNetworkConnection() && wifiWhitelistConditionMet(prefWifiWhitelistEnabled, whitelistedWifiSsids)) { + Log.v(TAG, "decideShouldRun: prefRunOnWifi && isWifiOrEthernetConnection && prefRunOnMeteredWifi && isMeteredNetworkConnection && wifiWhitelistConditionMet"); + return true; + } + } else { + // Check if we are on a non-metered wifi and if wifi whitelist run condition is met. + if (!isMeteredNetworkConnection() && wifiWhitelistConditionMet(prefWifiWhitelistEnabled, whitelistedWifiSsids)) { + Log.v(TAG, "decideShouldRun: prefRunOnWifi && isWifiOrEthernetConnection && !prefRunOnMeteredWifi && !isMeteredNetworkConnection && wifiWhitelistConditionMet"); + return true; + } } } @@ -224,6 +231,23 @@ public class RunConditionMonitor implements SharedPreferences.OnSharedPreference return false; } + /** + * Return whether the wifi whitelist run condition is met. + * Precondition: An active wifi connection has been detected. + */ + private boolean wifiWhitelistConditionMet(boolean prefWifiWhitelistEnabled, + Set whitelistedWifiSsids) { + if (!prefWifiWhitelistEnabled) { + Log.v(TAG, "handleWifiWhitelist: !prefWifiWhitelistEnabled"); + return true; + } + if (isWifiConnectionWhitelisted(whitelistedWifiSsids)) { + Log.v(TAG, "handleWifiWhitelist: isWifiConnectionWhitelisted"); + return true; + } + return false; + } + /** * Functions for run condition information retrieval. */ diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 61b34e93..54be57db 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -297,11 +297,14 @@ Please report any problems you encounter via Github. Use the following options to decide when the syncthing UI and syncing will be available. Run on mobile data - Run when device is connected to a mobile data or tethered network. Attention: This can fastly consume your data plan if you sync a large amount of data. + Run when device is connected to the mobile data network. Attention: This can fastly consume your data plan if you sync a large amount of data. Run on wifi Run when device is connected to a wifi network. + Run on metered wifi + Run when device is connected to a metered wifi network e.g. a hotspot or tethered network. Attention: This can fastly consume your data plan if you sync a large amount of data. + Run on specified wifi networks Run on selected wifi networks: %1$s Run on all wifi networks. diff --git a/app/src/main/res/xml/app_settings.xml b/app/src/main/res/xml/app_settings.xml index f36e588b..8f880860 100644 --- a/app/src/main/res/xml/app_settings.xml +++ b/app/src/main/res/xml/app_settings.xml @@ -24,6 +24,12 @@ android:summary="@string/run_on_wifi_summary" android:defaultValue="true" /> + +