Restart push/poll after device idle mode (Doze) has been left
This commit is contained in:
parent
949902a860
commit
0ada7fd58c
3 changed files with 132 additions and 2 deletions
|
@ -38,6 +38,7 @@ import com.fsck.k9.mail.Message;
|
||||||
import com.fsck.k9.mail.internet.BinaryTempFileBody;
|
import com.fsck.k9.mail.internet.BinaryTempFileBody;
|
||||||
import com.fsck.k9.mail.ssl.LocalKeyStore;
|
import com.fsck.k9.mail.ssl.LocalKeyStore;
|
||||||
import com.fsck.k9.mailstore.LocalStore;
|
import com.fsck.k9.mailstore.LocalStore;
|
||||||
|
import com.fsck.k9.power.DeviceIdleManager;
|
||||||
import com.fsck.k9.preferences.Storage;
|
import com.fsck.k9.preferences.Storage;
|
||||||
import com.fsck.k9.preferences.StorageEditor;
|
import com.fsck.k9.preferences.StorageEditor;
|
||||||
import com.fsck.k9.provider.UnreadWidgetProvider;
|
import com.fsck.k9.provider.UnreadWidgetProvider;
|
||||||
|
@ -353,10 +354,22 @@ public class K9 extends Application {
|
||||||
* whether any accounts are configured.
|
* whether any accounts are configured.
|
||||||
*/
|
*/
|
||||||
public static void setServicesEnabled(Context context) {
|
public static void setServicesEnabled(Context context) {
|
||||||
int acctLength = Preferences.getPreferences(context).getAvailableAccounts().size();
|
Context appContext = context.getApplicationContext();
|
||||||
|
int acctLength = Preferences.getPreferences(appContext).getAvailableAccounts().size();
|
||||||
|
boolean enable = acctLength > 0;
|
||||||
|
|
||||||
setServicesEnabled(context, acctLength > 0, null);
|
setServicesEnabled(appContext, enable, null);
|
||||||
|
|
||||||
|
updateDeviceIdleReceiver(appContext, enable);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void updateDeviceIdleReceiver(Context context, boolean enable) {
|
||||||
|
DeviceIdleManager deviceIdleManager = DeviceIdleManager.getInstance(context);
|
||||||
|
if (enable) {
|
||||||
|
deviceIdleManager.registerReceiver();
|
||||||
|
} else {
|
||||||
|
deviceIdleManager.unregisterReceiver();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void setServicesEnabled(Context context, boolean enabled, Integer wakeLockId) {
|
private static void setServicesEnabled(Context context, boolean enabled, Integer wakeLockId) {
|
||||||
|
|
|
@ -0,0 +1,84 @@
|
||||||
|
package com.fsck.k9.power;
|
||||||
|
|
||||||
|
|
||||||
|
import android.annotation.TargetApi;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.IntentFilter;
|
||||||
|
import android.os.Build;
|
||||||
|
import android.os.PowerManager;
|
||||||
|
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
|
||||||
|
public abstract class DeviceIdleManager {
|
||||||
|
private static DeviceIdleManager instance;
|
||||||
|
|
||||||
|
|
||||||
|
public static synchronized DeviceIdleManager getInstance(Context context) {
|
||||||
|
if (instance == null) {
|
||||||
|
DozeChecker dozeChecker = new DozeChecker(context);
|
||||||
|
if (dozeChecker.isDeviceIdleModeSupported() && !dozeChecker.isAppWhitelisted()) {
|
||||||
|
instance = RealDeviceIdleManager.newInstance(context);
|
||||||
|
} else {
|
||||||
|
instance = new NoOpDeviceIdleManager();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
private DeviceIdleManager() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void registerReceiver();
|
||||||
|
public abstract void unregisterReceiver();
|
||||||
|
|
||||||
|
|
||||||
|
static class NoOpDeviceIdleManager extends DeviceIdleManager {
|
||||||
|
@Override
|
||||||
|
public void registerReceiver() {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void unregisterReceiver() {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@TargetApi(Build.VERSION_CODES.M)
|
||||||
|
static class RealDeviceIdleManager extends DeviceIdleManager {
|
||||||
|
private final Context context;
|
||||||
|
private final DeviceIdleReceiver deviceIdleReceiver;
|
||||||
|
private final IntentFilter intentFilter;
|
||||||
|
private boolean registered;
|
||||||
|
|
||||||
|
|
||||||
|
static RealDeviceIdleManager newInstance(Context context) {
|
||||||
|
Context appContext = context.getApplicationContext();
|
||||||
|
return new RealDeviceIdleManager(appContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
private RealDeviceIdleManager(Context context) {
|
||||||
|
this.context = context;
|
||||||
|
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
||||||
|
deviceIdleReceiver = new DeviceIdleReceiver(powerManager);
|
||||||
|
intentFilter = new IntentFilter(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerReceiver() {
|
||||||
|
Timber.v("Registering DeviceIdleReceiver");
|
||||||
|
registered = true;
|
||||||
|
context.registerReceiver(deviceIdleReceiver, intentFilter);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void unregisterReceiver() {
|
||||||
|
Timber.v("Unregistering DeviceIdleReceiver");
|
||||||
|
if (registered) {
|
||||||
|
context.unregisterReceiver(deviceIdleReceiver);
|
||||||
|
registered = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
package com.fsck.k9.power;
|
||||||
|
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.os.Build;
|
||||||
|
import android.os.PowerManager;
|
||||||
|
import android.support.annotation.RequiresApi;
|
||||||
|
|
||||||
|
import com.fsck.k9.service.MailService;
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
|
||||||
|
@RequiresApi(api = Build.VERSION_CODES.M)
|
||||||
|
class DeviceIdleReceiver extends BroadcastReceiver {
|
||||||
|
private final PowerManager powerManager;
|
||||||
|
|
||||||
|
|
||||||
|
DeviceIdleReceiver(PowerManager powerManager) {
|
||||||
|
this.powerManager = powerManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
boolean deviceInIdleMode = powerManager.isDeviceIdleMode();
|
||||||
|
Timber.v("Device idle mode changed. Idle: %b", deviceInIdleMode);
|
||||||
|
|
||||||
|
if (!deviceInIdleMode) {
|
||||||
|
MailService.actionReset(context, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue