diff --git a/src/main/java/com/nutomic/syncthingandroid/service/DeviceStateHolder.java b/src/main/java/com/nutomic/syncthingandroid/service/DeviceStateHolder.java index 133480ee..54700b83 100644 --- a/src/main/java/com/nutomic/syncthingandroid/service/DeviceStateHolder.java +++ b/src/main/java/com/nutomic/syncthingandroid/service/DeviceStateHolder.java @@ -95,7 +95,8 @@ public class DeviceStateHolder extends BroadcastReceiver { public void updateWifiSsid() { mWifiSsid = null; - WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); + WifiManager wifiManager = + (WifiManager) mContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); // may be null, if WiFi has been turned off in meantime if (wifiInfo != null) { @@ -112,12 +113,14 @@ public class DeviceStateHolder extends BroadcastReceiver { */ @TargetApi(21) public boolean shouldRun() { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext); PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); - if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP && pm.isPowerSaveMode()) { + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP && + prefs.getBoolean("respect_battery_saving", true) && + pm.isPowerSaveMode()) { return false; } else if (SyncthingService.alwaysRunInBackground(mContext)) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext); // Check wifi/charging state against preferences and start if ok. boolean prefStopMobileData = prefs.getBoolean(SyncthingService.PREF_SYNC_ONLY_WIFI, false); boolean prefStopNotCharging = prefs.getBoolean(SyncthingService.PREF_SYNC_ONLY_CHARGING, false); diff --git a/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java b/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java index 413757bc..36b12e95 100644 --- a/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java +++ b/src/main/java/com/nutomic/syncthingandroid/service/SyncthingService.java @@ -89,6 +89,7 @@ public class SyncthingService extends Service implements 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"; + public static final String PREF_RESPECT_BATTERY_SAVING = "respect_battery_saving"; public static final String PREF_USE_ROOT = "use_root"; private static final String PREF_NOTIFICATION_TYPE = "notification_type"; public static final String PREF_USE_WAKE_LOCK = "wakelock_while_binary_running"; @@ -197,7 +198,7 @@ public class SyncthingService extends Service implements * Depending on the result, syncthing is started or stopped, and {@link #onApiChange()} is * called. */ - public void updateState() { + private void updateState() { // Start syncthing. if (mDeviceStateHolder.shouldRun()) { if (mCurrentState == State.ACTIVE || mCurrentState == State.STARTING) { @@ -291,8 +292,9 @@ public class SyncthingService extends Service implements if (key.equals(PREF_NOTIFICATION_TYPE) || key.equals(PREF_FOREGROUND_SERVICE)) updateNotification(); else if (key.equals(PREF_SYNC_ONLY_CHARGING) || key.equals(PREF_SYNC_ONLY_WIFI) - || key.equals(PREF_SYNC_ONLY_WIFI_SSIDS)) + || key.equals(PREF_SYNC_ONLY_WIFI_SSIDS) || key.equals(PREF_RESPECT_BATTERY_SAVING)) { updateState(); + } } /** diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index a06eb1dd..5c09e596 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -250,6 +250,9 @@ Please report any problems you encounter via Github. Please turn on WiFi to select networks. + Respect Android battery saving setting + Disable Syncthing if battery saving is active + Use advanced Folder Picker Select any folder on the device for syncing diff --git a/src/main/res/xml/app_settings.xml b/src/main/res/xml/app_settings.xml index 6779ea65..ec6eb92b 100644 --- a/src/main/res/xml/app_settings.xml +++ b/src/main/res/xml/app_settings.xml @@ -24,6 +24,12 @@ android:key="sync_only_wifi_ssids_set" android:title="@string/sync_only_wifi_ssids" /> + +