This repository has been archived by the owner on Feb 27, 2021. It is now read-only.
forked from thunderbird/thunderbird-android
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'org_k9mail_k-9/5.2-MAINT' into 5.2-MAINT
- Loading branch information
Showing
8 changed files
with
197 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 14 additions & 22 deletions
36
k9mail/src/main/java/com/fsck/k9/helper/K9AlarmManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
85
k9mail/src/main/java/com/fsck/k9/power/DeviceIdleManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
k9mail/src/main/java/com/fsck/k9/power/DeviceIdleReceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.