mirror of
https://github.com/syncthing/syncthing-android.git
synced 2025-01-11 04:25:53 +00:00
WIP - Add mobile data run condition
This commit is contained in:
parent
4232f7f468
commit
947ff691f2
5 changed files with 128 additions and 84 deletions
|
@ -17,6 +17,7 @@ import android.preference.PreferenceManager;
|
|||
import android.preference.PreferenceScreen;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
|
@ -38,6 +39,8 @@ import com.nutomic.syncthingandroid.views.WifiSsidPreference;
|
|||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.security.InvalidParameterException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
|
@ -90,8 +93,9 @@ public class SettingsActivity extends SyncthingActivity {
|
|||
|
||||
private CheckBoxPreference mAlwaysRunInBackground;
|
||||
private ListPreference mPowerSource;
|
||||
private CheckBoxPreference mSyncOnlyWifi;
|
||||
private WifiSsidPreference mSyncOnlyOnSSIDs;
|
||||
private CheckBoxPreference mRunOnMobileData;
|
||||
private CheckBoxPreference mRunOnWifi;
|
||||
private WifiSsidPreference mWifiSsidWhitelist;
|
||||
|
||||
private Preference mCategorySyncthingOptions;
|
||||
private EditTextPreference mDeviceName;
|
||||
|
@ -149,12 +153,12 @@ public class SettingsActivity extends SyncthingActivity {
|
|||
(CheckBoxPreference) findPreference(Constants.PREF_ALWAYS_RUN_IN_BACKGROUND);
|
||||
mPowerSource =
|
||||
(ListPreference) findPreference(Constants.PREF_POWER_SOURCE);
|
||||
mSyncOnlyWifi =
|
||||
(CheckBoxPreference) findPreference(Constants.PREF_SYNC_ONLY_WIFI);
|
||||
mSyncOnlyOnSSIDs =
|
||||
(WifiSsidPreference) findPreference(Constants.PREF_SYNC_ONLY_WIFI_SSIDS);
|
||||
|
||||
mSyncOnlyOnSSIDs.setEnabled(mSyncOnlyWifi.isChecked());
|
||||
mRunOnMobileData =
|
||||
(CheckBoxPreference) findPreference(Constants.PREF_RUN_ON_WIFI);
|
||||
mRunOnWifi =
|
||||
(CheckBoxPreference) findPreference(Constants.PREF_RUN_ON_WIFI);
|
||||
mWifiSsidWhitelist =
|
||||
(WifiSsidPreference) findPreference(Constants.PREF_WIFI_SSID_WHITELIST);
|
||||
|
||||
ListPreference languagePref = (ListPreference) findPreference(Languages.PREFERENCE_LANGUAGE);
|
||||
PreferenceScreen categoryBehaviour = (PreferenceScreen) findPreference("category_behaviour");
|
||||
|
@ -207,7 +211,7 @@ public class SettingsActivity extends SyncthingActivity {
|
|||
mSyncthingVersion = findPreference("syncthing_version");
|
||||
Preference appVersion = screen.findPreference("app_version");
|
||||
|
||||
mSyncOnlyOnSSIDs.setEnabled(mSyncOnlyWifi.isChecked());
|
||||
mWifiSsidWhitelist.setEnabled(mRunOnWifi.isChecked());
|
||||
setPreferenceCategoryChangeListener(findPreference("category_run_conditions"), this);
|
||||
|
||||
mCategorySyncthingOptions = findPreference("category_syncthing_options");
|
||||
|
@ -235,6 +239,11 @@ public class SettingsActivity extends SyncthingActivity {
|
|||
/* Initialize summaries */
|
||||
mPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
|
||||
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) ?
|
||||
getString(R.string.run_on_all_wifi_networks) :
|
||||
getString(R.string.run_on_whitelisted_wifi_networks, wifiSsidSummary)
|
||||
);
|
||||
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, ""));
|
||||
|
||||
|
@ -398,6 +407,16 @@ public class SettingsActivity extends SyncthingActivity {
|
|||
mPowerSource.setValue(o.toString());
|
||||
preference.setSummary(mPowerSource.getEntry());
|
||||
break;
|
||||
case Constants.PREF_RUN_ON_WIFI:
|
||||
mWifiSsidWhitelist.setEnabled((Boolean) o);
|
||||
break;
|
||||
case Constants.PREF_WIFI_SSID_WHITELIST:
|
||||
String wifiSsidSummary = TextUtils.join(", ", (Set<String>) o);
|
||||
preference.setSummary(TextUtils.isEmpty(wifiSsidSummary) ?
|
||||
getString(R.string.run_on_all_wifi_networks) :
|
||||
getString(R.string.run_on_whitelisted_wifi_networks, wifiSsidSummary)
|
||||
);
|
||||
break;
|
||||
case Constants.PREF_DEBUG_FACILITIES_ENABLED:
|
||||
mPendingConfig = true;
|
||||
break;
|
||||
|
|
|
@ -11,8 +11,11 @@ public class Constants {
|
|||
public static final String FILENAME_SYNCTHING_BINARY = "libsyncthing.so";
|
||||
|
||||
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_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_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_RESPECT_BATTERY_SAVING = "respect_battery_saving";
|
||||
|
|
|
@ -100,8 +100,10 @@ public class RunConditionMonitor implements SharedPreferences.OnSharedPreference
|
|||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||
List<String> watched = Lists.newArrayList(
|
||||
Constants.PREF_POWER_SOURCE,
|
||||
Constants.PREF_SYNC_ONLY_WIFI, Constants.PREF_RESPECT_BATTERY_SAVING,
|
||||
Constants.PREF_SYNC_ONLY_WIFI_SSIDS);
|
||||
Constants.PREF_RUN_ON_WIFI,
|
||||
Constants.PREF_WIFI_SSID_WHITELIST,
|
||||
Constants.PREF_RESPECT_BATTERY_SAVING
|
||||
);
|
||||
if (watched.contains(key)) {
|
||||
// Force a re-evaluation of which run conditions apply according to the changed prefs.
|
||||
updateShouldRunDecision();
|
||||
|
@ -155,18 +157,12 @@ public class RunConditionMonitor implements SharedPreferences.OnSharedPreference
|
|||
// Get run conditions preferences.
|
||||
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 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);
|
||||
Set<String> whitelistedWifiSsids = mPreferences.getStringSet(Constants.PREF_SYNC_ONLY_WIFI_SSIDS, new HashSet<>());
|
||||
Set<String> whitelistedWifiSsids = mPreferences.getStringSet(Constants.PREF_WIFI_SSID_WHITELIST, new HashSet<>());
|
||||
boolean prefWifiWhitelistEnabled = !whitelistedWifiSsids.isEmpty();
|
||||
|
||||
// Power saving
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
if (prefRespectPowerSaving && isPowerSaving()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// PREF_POWER_SOURCE
|
||||
switch (prefPowerSource) {
|
||||
case POWER_SOURCE_AC:
|
||||
|
@ -186,28 +182,37 @@ public class RunConditionMonitor implements SharedPreferences.OnSharedPreference
|
|||
break;
|
||||
}
|
||||
|
||||
// Run only on wifi.
|
||||
if (prefRunOnlyOnWifi && !isWifiOrEthernetConnection()) {
|
||||
// Not on wifi.
|
||||
Log.v(TAG, "decideShouldRun: prefRunOnlyOnWifi && !isWifiOrEthernetConnection");
|
||||
return false;
|
||||
// Power saving
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
if (prefRespectPowerSaving && isPowerSaving()) {
|
||||
Log.v(TAG, "decideShouldRun: prefRespectPowerSaving && isPowerSaving");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Run only on whitelisted wifi ssids.
|
||||
if (prefRunOnlyOnWifi && prefWifiWhitelistEnabled) {
|
||||
// Wifi connection detected. Wifi ssid whitelist enabled.
|
||||
Log.v(TAG, "decideShouldRun: prefRunOnlyOnWifi && prefWifiWhitelistEnabled");
|
||||
return isWifiConnectionWhitelisted(whitelistedWifiSsids);
|
||||
// Run on mobile data or tethered connection that is marked as metered.
|
||||
if (prefRunOnMobileData && (isMobileDataConnection() || isMeteredNetworkConnection())) {
|
||||
Log.v(TAG, "decideShouldRun: prefRunOnMobileData && (isMobileDataConnection || isMeteredNetworkConnection");
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Respect power saving, device is not in power-save mode.
|
||||
* Always run in background is the only pref that is enabled.
|
||||
* Run only when charging, charging.
|
||||
* Run only on wifi, wifi connection detected, wifi ssid whitelist disabled.
|
||||
* If none of the above run conditions matched, don't run.
|
||||
*/
|
||||
Log.v(TAG, "decideShouldRun: return true");
|
||||
return true;
|
||||
Log.v(TAG, "decideShouldRun: return false");
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -290,15 +290,29 @@ Please report any problems you encounter via Github.</string>
|
|||
<string name="always_run_in_background_enabled">to_be_deleted</string>
|
||||
<string name="always_run_in_background_disabled">to_be_deleted</string>
|
||||
<string name="sync_only_charging">to_be_deleted</string>
|
||||
|
||||
<string name="service_settings_title">Service setting</string>
|
||||
<string name="service_settings_summary">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.</string>
|
||||
|
||||
<string name="run_as_background_service">Run as background service</string>
|
||||
<string name="sync_only_wifi">to_be_deleted</string>
|
||||
<string name="sync_only_wifi_ssids">to_be_deleted</string>
|
||||
|
||||
<string name="run_conditions_title">Run Conditions</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_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_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>
|
||||
|
||||
<string name="sync_only_wifi_ssids_wifi_turn_on_wifi">Please turn on WiFi to select networks.</string>
|
||||
|
||||
<string name="sync_only_wifi_ssids_need_to_grant_location_permission">You need to grant LOCATION permission to use this feature.</string>
|
||||
|
||||
<string name="sync_only_wifi_ssids_location_permission_rejected_dialog_title">Permission required</string>
|
||||
<string name="sync_only_wifi_ssids_location_permission_rejected_dialog_content">Starting with Android 8.1, location access is required to be able to read the WiFi\'s name. You can use this feature only if you grant this permission.</string>
|
||||
|
||||
<string name="power_source_title">Run when device powered by</string>
|
||||
|
||||
<string-array name="power_source_entries">
|
||||
|
@ -307,20 +321,15 @@ Please report any problems you encounter via Github.</string>
|
|||
<item>Battery power</item>
|
||||
</string-array>
|
||||
|
||||
<string name="sync_only_wifi">Run only on wifi</string>
|
||||
|
||||
<string name="sync_only_wifi_ssids">Restrict to certain wifi networks</string>
|
||||
|
||||
<string name="sync_only_wifi_ssids_wifi_turn_on_wifi">Please turn on WiFi to select networks.</string>
|
||||
|
||||
<string name="sync_only_wifi_ssids_need_to_grant_location_permission">You need to grant LOCATION permission to use this feature.</string>
|
||||
|
||||
<string name="sync_only_wifi_ssids_location_permission_rejected_dialog_title">Permission required</string>
|
||||
<string name="sync_only_wifi_ssids_location_permission_rejected_dialog_content">Starting with Android 8.1, location access is required to be able to read the WiFi\'s name. You can use this feature only if you grant this permission.</string>
|
||||
|
||||
<string name="respect_battery_saving_title">Respect Android battery saving setting</string>
|
||||
<string name="respect_battery_saving_summary">Disable Syncthing if battery saving is active</string>
|
||||
|
||||
<string name="service_settings_title">Service setting</string>
|
||||
<string name="service_settings_summary">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.</string>
|
||||
|
||||
<string name="run_as_background_service">Run as background service</string>
|
||||
|
||||
<!-- Preferences - Behaviour -->
|
||||
<string name="advanced_folder_picker">Use advanced Folder Picker</string>
|
||||
|
||||
<string name="advanced_folder_picker_summary">Select any folder on the device for syncing</string>
|
||||
|
|
|
@ -5,6 +5,44 @@
|
|||
android:title="@string/category_run_conditions"
|
||||
android:key="category_run_conditions">
|
||||
|
||||
<Preference
|
||||
android:key="static_run_conditions"
|
||||
android:persistent="false"
|
||||
android:selectable="false"
|
||||
android:title="@string/run_conditions_title"
|
||||
android:summary="@string/run_conditions_summary"/>
|
||||
|
||||
<CheckBoxPreference
|
||||
android:key="run_on_mobile_data"
|
||||
android:title="@string/run_on_mobile_data_title"
|
||||
android:summary="@string/run_on_mobile_data_summary"
|
||||
android:defaultValue="false" />
|
||||
|
||||
<CheckBoxPreference
|
||||
android:key="run_on_wifi"
|
||||
android:title="@string/run_on_wifi_title"
|
||||
android:summary="@string/run_on_wifi_summary"
|
||||
android:defaultValue="true" />
|
||||
|
||||
<com.nutomic.syncthingandroid.views.WifiSsidPreference
|
||||
android:key="wifi_ssid_whitelist"
|
||||
android:title="@string/run_on_whitelisted_wifi_title"
|
||||
android:summary="@null" />
|
||||
|
||||
<ListPreference
|
||||
android:key="power_source"
|
||||
android:title="@string/power_source_title"
|
||||
android:entryValues="@array/power_source_values"
|
||||
android:entries="@array/power_source_entries"
|
||||
android:summary="@null"
|
||||
android:defaultValue="ac_and_battery_power" />
|
||||
|
||||
<CheckBoxPreference
|
||||
android:key="respect_battery_saving"
|
||||
android:title="@string/respect_battery_saving_title"
|
||||
android:summary="@string/respect_battery_saving_summary"
|
||||
android:defaultValue="true" />
|
||||
|
||||
<Preference
|
||||
android:key="static_service_settings"
|
||||
android:persistent="false"
|
||||
|
@ -17,36 +55,6 @@
|
|||
android:title="@string/run_as_background_service"
|
||||
android:defaultValue="false" />
|
||||
|
||||
<Preference
|
||||
android:key="static_run_conditions"
|
||||
android:persistent="false"
|
||||
android:selectable="false"
|
||||
android:title="@string/run_conditions_title"
|
||||
android:summary="@string/run_conditions_summary"/>
|
||||
|
||||
<ListPreference
|
||||
android:key="power_source"
|
||||
android:title="@string/power_source_title"
|
||||
android:entryValues="@array/power_source_values"
|
||||
android:entries="@array/power_source_entries"
|
||||
android:summary="@null"
|
||||
android:defaultValue="ac_and_battery_power" />
|
||||
|
||||
<CheckBoxPreference
|
||||
android:key="sync_only_wifi"
|
||||
android:title="@string/sync_only_wifi"
|
||||
android:defaultValue="false" />
|
||||
|
||||
<com.nutomic.syncthingandroid.views.WifiSsidPreference
|
||||
android:key="sync_only_wifi_ssids_set"
|
||||
android:title="@string/sync_only_wifi_ssids" />
|
||||
|
||||
<CheckBoxPreference
|
||||
android:key="respect_battery_saving"
|
||||
android:title="@string/respect_battery_saving_title"
|
||||
android:summary="@string/respect_battery_saving_summary"
|
||||
android:defaultValue="true" />
|
||||
|
||||
</PreferenceScreen>
|
||||
|
||||
<PreferenceScreen
|
||||
|
|
Loading…
Reference in a new issue