Skip to content
This repository has been archived by the owner on Feb 27, 2021. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'org_k9mail_k-9/5.2-MAINT' into 5.2-MAINT
Browse files Browse the repository at this point in the history
  • Loading branch information
G00fY2 committed Oct 3, 2017
2 parents f25fa3e + cd0f48b commit 6cab2c7
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 80 deletions.
4 changes: 2 additions & 2 deletions k9mail/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ android {
applicationId "com.fsck.k9.materialfork"
testApplicationId "com.fsck.k9.materialfork.tests"

versionCode 232703
versionName '5.207.3'
versionCode 232711
versionName '5.208.1'

minSdkVersion 15
targetSdkVersion 25
Expand Down
17 changes: 15 additions & 2 deletions k9mail/src/main/java/com/fsck/k9/K9.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.fsck.k9.mail.internet.BinaryTempFileBody;
import com.fsck.k9.mail.ssl.LocalKeyStore;
import com.fsck.k9.mailstore.LocalStore;
import com.fsck.k9.power.DeviceIdleManager;
import com.fsck.k9.preferences.Storage;
import com.fsck.k9.preferences.StorageEditor;
import com.fsck.k9.provider.UnreadWidgetProvider;
Expand Down Expand Up @@ -346,10 +347,22 @@ public static class Share {
* whether any accounts are configured.
*/
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) {
Expand Down
36 changes: 14 additions & 22 deletions k9mail/src/main/java/com/fsck/k9/helper/K9AlarmManager.java
Original file line number Diff line number Diff line change
@@ -1,56 +1,48 @@
package com.fsck.k9.helper;


import android.annotation.TargetApi;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.os.Build;
import android.os.PowerManager;
import android.support.annotation.VisibleForTesting;

import com.fsck.k9.power.DozeChecker;


public class K9AlarmManager {
private final AlarmManager alarmManager;
private final PowerManager powerManager;
private final String packageName;
private final DozeChecker dozeChecker;


@VisibleForTesting
K9AlarmManager(Context context) {
alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
packageName = context.getPackageName();
public static K9AlarmManager getAlarmManager(Context context) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
DozeChecker dozeChecker = new DozeChecker(context);
return new K9AlarmManager(alarmManager, dozeChecker);
}

public static K9AlarmManager getAlarmManager(Context context) {
return new K9AlarmManager(context);
@VisibleForTesting
K9AlarmManager(AlarmManager alarmManager, DozeChecker dozeChecker) {
this.alarmManager = alarmManager;
this.dozeChecker = dozeChecker;
}

public void set(int type, long triggerAtMillis, PendingIntent operation) {
if (isDozeSupported() && isDozeWhiteListed()) {
if (dozeChecker.isDeviceIdleModeSupported() && dozeChecker.isAppWhitelisted()) {
setAndAllowWhileIdle(type, triggerAtMillis, operation);
} else {
alarmManager.set(type, triggerAtMillis, operation);
}
}

@TargetApi(Build.VERSION_CODES.M)
public void setAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation) {
private void setAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation) {
alarmManager.setAndAllowWhileIdle(type, triggerAtMillis, operation);
}

public void cancel(PendingIntent operation) {
alarmManager.cancel(operation);
}

@VisibleForTesting
protected boolean isDozeSupported() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
}

@TargetApi(Build.VERSION_CODES.M)
private boolean isDozeWhiteListed() {
return powerManager.isIgnoringBatteryOptimizations(packageName);
}
}

85 changes: 85 additions & 0 deletions k9mail/src/main/java/com/fsck/k9/power/DeviceIdleManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
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 android.util.Log;

import com.fsck.k9.K9;


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() {
Log.v(K9.LOG_TAG, "Registering DeviceIdleReceiver");
registered = true;
context.registerReceiver(deviceIdleReceiver, intentFilter);
}

@Override
public void unregisterReceiver() {
Log.v(K9.LOG_TAG, "Unregistering DeviceIdleReceiver");
if (registered) {
context.unregisterReceiver(deviceIdleReceiver);
registered = false;
}
}
}
}
34 changes: 34 additions & 0 deletions k9mail/src/main/java/com/fsck/k9/power/DeviceIdleReceiver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.fsck.k9.power;


import android.annotation.TargetApi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.PowerManager;
import android.util.Log;

import com.fsck.k9.K9;
import com.fsck.k9.service.MailService;


@TargetApi(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();
Log.v(K9.LOG_TAG, "Device idle mode changed. Idle: " + deviceInIdleMode);

if (!deviceInIdleMode) {
MailService.actionReset(context, null);
}
}
}
28 changes: 28 additions & 0 deletions k9mail/src/main/java/com/fsck/k9/power/DozeChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.fsck.k9.power;


import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.PowerManager;


public class DozeChecker {
private final PowerManager powerManager;
private final String packageName;


public DozeChecker(Context context) {
powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
packageName = context.getPackageName();
}

public boolean isDeviceIdleModeSupported() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
}

@TargetApi(Build.VERSION_CODES.M)
public boolean isAppWhitelisted() {
return powerManager.isIgnoringBatteryOptimizations(packageName);
}
}
3 changes: 3 additions & 0 deletions k9mail/src/main/res/xml/changelog_master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
Locale specific versions are kept in res/xml-<locale qualifier>/changelog.xml.
-->
<changelog>
<release version="5.208" versioncode="23271">
<change>Fixed bug where automatic synchronization wouldn't restart after the device exited doze mode</change>
</release>
<release version="5.207" versioncode="23270">
<change>Improved speed of local message search</change>
</release>
Expand Down
Loading

0 comments on commit 6cab2c7

Please sign in to comment.