From c1d5bcc4cdb02eb40ab60030f304515682ba4ba3 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Tue, 2 Aug 2016 21:44:51 +0200 Subject: [PATCH] Disable Syncthing if battery saving mode is active (fixes #603). --- .../syncthing/DeviceStateHolder.java | 15 +++++++++++---- .../syncthing/SyncthingService.java | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/DeviceStateHolder.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/DeviceStateHolder.java index 924b4793..b8cbea77 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/DeviceStateHolder.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/DeviceStateHolder.java @@ -10,6 +10,8 @@ import android.net.ConnectivityManager; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.BatteryManager; +import android.os.Build; +import android.os.PowerManager; import android.preference.PreferenceManager; import android.util.Log; @@ -107,14 +109,16 @@ public class DeviceStateHolder extends BroadcastReceiver { /** * Determines if Syncthing should currently run. */ + @TargetApi(21) public boolean shouldRun() { - if (!ContentResolver.getMasterSyncAutomatically()) { + PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB && pm.isPowerSaveMode()) { + return false; + } + else if (!ContentResolver.getMasterSyncAutomatically()) { return false; } else if (SyncthingService.alwaysRunInBackground(mContext)) { - return true; - } - else { 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); @@ -123,6 +127,9 @@ public class DeviceStateHolder extends BroadcastReceiver { return (isCharging() || !prefStopNotCharging) && (!prefStopMobileData || isAllowedWifiConnected()); } + else { + return true; + } } private boolean isAllowedWifiConnected() { diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java index b90d4ec8..e5960a4b 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java @@ -1,8 +1,10 @@ package com.nutomic.syncthingandroid.syncthing; +import android.annotation.TargetApi; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; +import android.content.BroadcastReceiver; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; @@ -13,6 +15,7 @@ import android.os.AsyncTask; import android.os.Build; import android.os.Environment; import android.os.IBinder; +import android.os.PowerManager; import android.preference.PreferenceManager; import android.support.v4.app.NotificationCompat; import android.util.Log; @@ -133,6 +136,13 @@ public class SyncthingService extends Service implements } }; + private final BroadcastReceiver mPowerSaveModeChangedReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + updateState(); + } + }; + /** * INIT: Service is starting up and initializing. * STARTING: Syncthing binary is starting (but the API is not yet ready). @@ -301,6 +311,7 @@ public class SyncthingService extends Service implements * Starts the native binary. */ @Override + @TargetApi(21) public void onCreate() { PRNGFixes.apply(); SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); @@ -321,6 +332,11 @@ public class SyncthingService extends Service implements mDeviceStateHolder = new DeviceStateHolder(SyncthingService.this); registerReceiver(mDeviceStateHolder, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) { + registerReceiver(mPowerSaveModeChangedReceiver, + new IntentFilter(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED)); + } + new StartupTask(sp.getString("gui_user",""), sp.getString("gui_password","")).execute(); sp.registerOnSharedPreferenceChangeListener(this); ContentResolver.addStatusChangeListener(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, @@ -432,6 +448,8 @@ public class SyncthingService extends Service implements SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); sp.unregisterOnSharedPreferenceChangeListener(this); ContentResolver.removeStatusChangeListener(mSyncStatusObserver); + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) + unregisterReceiver(mPowerSaveModeChangedReceiver); } private void shutdown() {