1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2025-01-11 04:25:53 +00:00

Add run condition "Run on metered wifi" (fixes #937)

This commit is contained in:
Catfriend1 2018-07-25 16:02:37 +02:00
parent 35d4421cbf
commit 13ab9b6599
5 changed files with 51 additions and 15 deletions

View file

@ -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:

View file

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

View file

@ -101,6 +101,7 @@ public class RunConditionMonitor implements SharedPreferences.OnSharedPreference
List<String> 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<String> 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,23 +195,28 @@ 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");
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;
}
if (isWifiConnectionWhitelisted(whitelistedWifiSsids)) {
Log.v(TAG, "decideShouldRun: prefRunOnWifi && isWifiOrEthernetConnection && prefWifiWhitelistEnabled && isWifiConnectionWhitelisted");
} 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;
}
}
}
// Run in flight mode.
if (prefRunInFlightMode && isFlightMode()) {
@ -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<String> 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.
*/

View file

@ -297,11 +297,14 @@ Please report any problems you encounter via Github.</string>
<string name="run_conditions_summary">Use the following options to decide when the syncthing UI and syncing will be available.</string>
<string name="run_on_mobile_data_title">Run on mobile data</string>
<string name="run_on_mobile_data_summary">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.</string>
<string name="run_on_mobile_data_summary">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.</string>
<string name="run_on_wifi_title">Run on wifi</string>
<string name="run_on_wifi_summary">Run when device is connected to a wifi network.</string>
<string name="run_on_metered_wifi_title">Run on metered wifi</string>
<string name="run_on_metered_wifi_summary">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.</string>
<string name="run_on_whitelisted_wifi_title">Run on specified wifi networks</string>
<string name="run_on_whitelisted_wifi_networks">Run on selected wifi networks: %1$s</string>
<string name="run_on_all_wifi_networks">Run on all wifi networks.</string>

View file

@ -24,6 +24,12 @@
android:summary="@string/run_on_wifi_summary"
android:defaultValue="true" />
<CheckBoxPreference
android:key="run_on_metered_wifi"
android:title="@string/run_on_metered_wifi_title"
android:summary="@string/run_on_metered_wifi_summary"
android:defaultValue="false" />
<com.nutomic.syncthingandroid.views.WifiSsidPreference
android:key="wifi_ssid_whitelist"
android:title="@string/run_on_whitelisted_wifi_title"