1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-26 14:21:16 +00:00

Disable Syncthing if battery saving mode is active (fixes #603).

This commit is contained in:
Felix Ableitner 2016-08-02 21:44:51 +02:00
parent 2daa601e36
commit c1d5bcc4cd
2 changed files with 29 additions and 4 deletions

View file

@ -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() {

View file

@ -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() {