Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "roaming" option to run conditions (fixes #177) #441

Merged
merged 7 commits into from
Jul 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public static class SettingsFragment extends PreferenceFragment
private PreferenceScreen mCategoryRunConditions;
private ListPreference mPowerSource;
private CheckBoxPreference mRunOnMobileData;
private CheckBoxPreference mRunOnRoaming;
private CheckBoxPreference mRunOnWifi;
private CheckBoxPreference mRunOnMeteredWifi;
private CheckBoxPreference mUseWifiWhitelist;
Expand Down Expand Up @@ -285,7 +286,9 @@ public void onActivityCreated(Bundle savedInstanceState) {
mWifiSsidWhitelist =
(WifiSsidPreference) findPreference(Constants.PREF_WIFI_SSID_WHITELIST);
mRunOnMobileData =
(CheckBoxPreference) findPreference(Constants.PREF_RUN_ON_WIFI);
(CheckBoxPreference) findPreference(Constants.PREF_RUN_ON_MOBILE_DATA);
mRunOnRoaming =
(CheckBoxPreference) findPreference(Constants.PREF_RUN_ON_ROAMING);
mPowerSource =
(ListPreference) findPreference(Constants.PREF_POWER_SOURCE);
mRunInFlightMode =
Expand All @@ -295,6 +298,8 @@ public void onActivityCreated(Bundle savedInstanceState) {
mUseWifiWhitelist.setEnabled(mRunOnWifi.isChecked());
mWifiSsidWhitelist.setEnabled(mRunOnWifi.isChecked() && mUseWifiWhitelist.isChecked());

mRunOnRoaming.setEnabled(mRunOnMobileData.isChecked());

screen.findPreference(Constants.PREF_POWER_SOURCE).setSummary(mPowerSource.getEntry());
String wifiSsidSummary = TextUtils.join(", ", mPreferences.getStringSet(Constants.PREF_WIFI_SSID_WHITELIST, new HashSet<>()));
screen.findPreference(Constants.PREF_WIFI_SSID_WHITELIST).setSummary(TextUtils.isEmpty(wifiSsidSummary) ?
Expand Down Expand Up @@ -593,6 +598,9 @@ public boolean onRunConditionPreferenceChange(Preference preference, Object o) {
getString(R.string.run_on_whitelisted_wifi_networks, wifiSsidSummary)
);
break;
case Constants.PREF_RUN_ON_MOBILE_DATA:
mRunOnRoaming.setEnabled((Boolean) o);
break;
}
mPendingRunConditions = true;
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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_ON_MOBILE_DATA = "run_on_mobile_data";
public static final String PREF_RUN_ON_ROAMING = "run_on_roaming";
public static final String PREF_RUN_ON_WIFI = "run_on_wifi";
public static final String PREF_RUN_ON_METERED_WIFI = "run_on_metered_wifi";
public static final String PREF_USE_WIFI_SSID_WHITELIST = "use_wifi_whitelist";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,25 @@ private SyncConditionResult checkConditionSyncOnMobileData(String prefNameSyncOn
return new SyncConditionResult(false, res.getString(R.string.reason_not_on_mobile_data));
}

/**
* Constants.PREF_RUN_ON_ROAMING
*/
private SyncConditionResult checkConditionSyncOnRoaming(String prefNameSyncOnRoaming) {
boolean prefSyncOnRoaming = mPreferences.getBoolean(prefNameSyncOnRoaming, false);
if (prefSyncOnRoaming) {
// Condition is always met as we allow both types of mobile data networks - roaming and non-roaming.
return new SyncConditionResult(true, "\n" + res.getString(R.string.reason_on_roaming_nonroaming_mobile_data));
}

// Check if we are on a non-roaming mobile data network.
if (!isRoamingNetworkConnection()) {
return new SyncConditionResult(true, "\n" + res.getString(R.string.reason_on_nonroaming_mobile_data));
}

// We disallowed non-roaming mobile data and are connected to a mobile data network in roaming mode.
return new SyncConditionResult(false, "\n" + res.getString(R.string.reason_not_nonroaming_mobile_data));
}

/**
* Determines if Syncthing should currently run.
* Updates mRunDecisionExplanation.
Expand Down Expand Up @@ -439,7 +458,14 @@ private boolean decideShouldRun() {
if (scr.conditionMet) {
// Mobile data is connected.
LogV("decideShouldRun: checkConditionSyncOnMobileData");
return true;

scr = checkConditionSyncOnRoaming(Constants.PREF_RUN_ON_ROAMING);
mRunDecisionExplanation += scr.explanation;
if (scr.conditionMet) {
// Mobile data connection type is allowed.
LogV("decideShouldRun: checkConditionSyncOnMobileData && checkConditionSyncOnRoaming");
return true;
}
}

// Run on WiFi?
Expand Down Expand Up @@ -628,6 +654,20 @@ private boolean isMobileDataConnection() {
}
}

private boolean isRoamingNetworkConnection() {
ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null) {
// In flight mode.
return false;
}
if (!ni.isConnected()) {
// No network connection.
return false;
}
return ni.isRoaming();
}

private boolean isWifiOrEthernetConnection() {
ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
Expand Down
13 changes: 12 additions & 1 deletion app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,9 @@ Bitte melden Sie auftretende Probleme via GitHub.</string>
<string name="run_on_mobile_data_title">Starte bei mobiler Datenverbindung</string>
<string name="run_on_mobile_data_summary">Starte, wenn das Gerät über das mobile Datennetzwerk verbunden ist. Warnung: Wenn Du große Datenmengen synchronisierst, kann dies einen hohen Verbrauch deines mobilen Datentarifs verursachen.</string>

<string name="run_on_roaming_title">Starte während Roaming</string>
<string name="run_on_roaming_summary">Ermögliche Syncthing, mobile Daten während dem Roaming zu verwenden. (!) Bitte hinterlasse ein Feedback bei GitHub, wenn dies korrekt funktioniert.</string>

<string name="run_on_wifi_title">Starte bei WLAN-Verbindung</string>
<string name="run_on_wifi_summary">Starte, wenn das Gerät mit einem WLAN- oder Kabel-Netzwerk verbunden ist.</string>

Expand Down Expand Up @@ -779,9 +782,17 @@ Bitte melden Sie auftretende Probleme via GitHub.</string>
<string name="reason_not_on_battery_power">Telefon wird nicht batteriebetrieben</string>
<string name="reason_not_while_power_saving">Syncthing läuft nicht, weil das Telefon im Energiesparmodus ist.</string>
<string name="reason_not_while_auto_sync_data_disabled">Syncthing läuft nicht, weil die Android-Kachel \"Automatische Datensynchronisation\" ausgeschaltet ist.</string>

<!-- ... Mobile data -->
<string name="reason_mobile_data_disallowed">Syncthing wurde nicht konfiguriert, die mobile Datenverbindung zu nutzen.</string>
<string name="reason_on_mobile_data">Syncthing läuft, weil die mobile Datenverbindung aufgebaut ist.</string>
<string name="reason_on_mobile_data">Syncthing darf die mobile Datenverbindung nutzen.</string>
<string name="reason_not_on_mobile_data">Syncthing darf bei mobiler Datenverbindung laufen, jedoch sind mobile Daten nicht verbunden.</string>

<!-- ... Roaming -->
<string name="reason_on_roaming_nonroaming_mobile_data">Syncthing darf bei mobilen Roaming- und Nicht-Roaming-Datenverbindungen ausgeführt werden.</string>
<string name="reason_on_nonroaming_mobile_data">Syncthing darf auf mobilen Datenverbindungen ohne Roaming ausgeführt werden. Die aktive Verbindung ist ohne Roaming.</string>
<string name="reason_not_nonroaming_mobile_data">Syncthing läuft nicht, da verboten wurde, dass es auf mobilen Datenverbindungen mit Roaming läuft.</string>

<string name="reason_wifi_disallowed">Syncthing wurde verboten, die WLAN- oder Kabel-Verbindung zu nutzen.</string>
<string name="reason_on_wifi">Syncthing darf bei WLAN-Verbindung laufen und WLAN ist gerade verbunden.</string>
<string name="reason_not_on_wifi">Syncthing darf bei WLAN-Verbindung laufen, jedoch ist WLAN gerade nicht verbunden oder das Telefon befindet sich im Flugzeugmodus.</string>
Expand Down
13 changes: 12 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,9 @@ Please report any problems you encounter via Github.</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 via the mobile data network. Warning: This can consume a lot of data from your mobile operator data plan if you sync large amounts of data.</string>

<string name="run_on_roaming_title">Run while roaming</string>
<string name="run_on_roaming_summary">Allow Syncthing to use mobile data while roaming. (!) Please post feedback to GitHub if this works correctly.</string>

<string name="run_on_wifi_title">Run on Wi-Fi</string>
<string name="run_on_wifi_summary">Run when device is connected to a Wi-Fi or ethernet network.</string>

Expand Down Expand Up @@ -800,9 +803,17 @@ Please report any problems you encounter via Github.</string>
<string name="reason_not_on_battery_power">Phone is not running on battery power.</string>
<string name="reason_not_while_power_saving">Syncthing is not running as the phone is currently power saving.</string>
<string name="reason_not_while_auto_sync_data_disabled">Syncthing is not running as Android currently has \'Auto-sync data\' disabled.</string>

<!-- ... Mobile data -->
<string name="reason_mobile_data_disallowed">Syncthing is not configured to run on mobile data connection.</string>
<string name="reason_on_mobile_data">Syncthing is running as mobile data is currently connected.</string>
<string name="reason_on_mobile_data">Syncthing is allowed to run on mobile data.</string>
<string name="reason_not_on_mobile_data">Syncthing is allowed to run on mobile data connection but mobile data isn\'t connected.</string>

<!-- ... Roaming -->
<string name="reason_on_roaming_nonroaming_mobile_data">Syncthing is allowed to run on roaming and non-roaming mobile data connections.</string>
<string name="reason_on_nonroaming_mobile_data">Syncthing is allowed to run on non-roaming mobile data connections. The active connection is non-roaming.</string>
<string name="reason_not_nonroaming_mobile_data">Syncthing is not running as you disallowed it to run on roaming mobile data connections.</string>

<string name="reason_wifi_disallowed">Syncthing is not allowed to run on WiFi or ethernet.</string>
<string name="reason_on_wifi">Syncthing is allowed to run on WiFi and WiFi is currently connected.</string>
<string name="reason_not_on_wifi">Syncthing is allowed to run on WiFi but WiFi isn\'t connected or the phone is in flight mode.</string>
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/xml/app_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@
android:summary="@string/run_on_mobile_data_summary"
android:defaultValue="false" />

<!-- Sync while roaming -->
<CheckBoxPreference
android:key="run_on_roaming"
android:title="@string/run_on_roaming_title"
android:summary="@string/run_on_roaming_summary"
android:defaultValue="false" />

<ListPreference
android:key="power_source"
android:title="@string/power_source_title"
Expand Down