mirror of
https://github.com/moparisthebest/k-9
synced 2024-12-25 00:58:50 -05:00
. More reliable background sync by using wake lock
--> Background sync now work even when the device is asleep (Thanks danapple0)
This commit is contained in:
parent
d55e335f48
commit
1e67bbee6b
@ -8,6 +8,7 @@
|
|||||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
|
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
|
||||||
<uses-permission android:name="android.permission.INTERNET"/>
|
<uses-permission android:name="android.permission.INTERNET"/>
|
||||||
<uses-permission android:name="android.permission.VIBRATE"/>
|
<uses-permission android:name="android.permission.VIBRATE"/>
|
||||||
|
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||||
<permission android:name="com.android.email.permission.READ_ATTACHMENT"
|
<permission android:name="com.android.email.permission.READ_ATTACHMENT"
|
||||||
android:permissionGroup="android.permission-group.MESSAGES"
|
android:permissionGroup="android.permission-group.MESSAGES"
|
||||||
android:protectionLevel="dangerous"
|
android:protectionLevel="dangerous"
|
||||||
|
@ -100,6 +100,11 @@ public class Email extends Application {
|
|||||||
*/
|
*/
|
||||||
public static final int MAX_ATTACHMENT_DOWNLOAD_SIZE = (5 * 1024 * 1024);
|
public static final int MAX_ATTACHMENT_DOWNLOAD_SIZE = (5 * 1024 * 1024);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Max time (in millis) the wake lock will be held for when background sync is happening
|
||||||
|
*/
|
||||||
|
public static final int WAKE_LOCK_TIMEOUT = 30000;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called throughout the application when the number of accounts has changed. This method
|
* Called throughout the application when the number of accounts has changed. This method
|
||||||
* enables or disables the Compose activity, the boot receiver and the service based on
|
* enables or disables the Compose activity, the boot receiver and the service based on
|
||||||
|
@ -11,6 +11,8 @@ import android.app.Service;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
|
import android.os.PowerManager;
|
||||||
|
import android.os.PowerManager.WakeLock;
|
||||||
import android.os.SystemClock;
|
import android.os.SystemClock;
|
||||||
import android.util.Config;
|
import android.util.Config;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
@ -61,6 +63,7 @@ public class MailService extends Service {
|
|||||||
if (Config.LOGV) {
|
if (Config.LOGV) {
|
||||||
Log.v(Email.LOG_TAG, "***** MailService *****: checking mail");
|
Log.v(Email.LOG_TAG, "***** MailService *****: checking mail");
|
||||||
}
|
}
|
||||||
|
mListener.wakeLockAcquire();
|
||||||
MessagingController.getInstance(getApplication()).checkMail(this, null, mListener);
|
MessagingController.getInstance(getApplication()).checkMail(this, null, mListener);
|
||||||
}
|
}
|
||||||
else if (ACTION_CANCEL.equals(intent.getAction())) {
|
else if (ACTION_CANCEL.equals(intent.getAction())) {
|
||||||
@ -124,6 +127,25 @@ public class MailService extends Service {
|
|||||||
|
|
||||||
class Listener extends MessagingListener {
|
class Listener extends MessagingListener {
|
||||||
HashMap<Account, Integer> accountsWithNewMail = new HashMap<Account, Integer>();
|
HashMap<Account, Integer> accountsWithNewMail = new HashMap<Account, Integer>();
|
||||||
|
private WakeLock wakeLock = null;
|
||||||
|
|
||||||
|
// wakelock strategy is to be very conservative. If there is any reason to release, then release
|
||||||
|
// don't want to take the chance of running wild
|
||||||
|
public synchronized void wakeLockAcquire() {
|
||||||
|
if (wakeLock == null) {
|
||||||
|
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
||||||
|
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Email");
|
||||||
|
wakeLock.setReferenceCounted(false);
|
||||||
|
wakeLock.acquire(Email.WAKE_LOCK_TIMEOUT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void wakeLockRelease() {
|
||||||
|
if (wakeLock != null) {
|
||||||
|
wakeLock.release();
|
||||||
|
wakeLock = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void checkMailStarted(Context context, Account account) {
|
public void checkMailStarted(Context context, Account account) {
|
||||||
@ -133,6 +155,7 @@ public class MailService extends Service {
|
|||||||
@Override
|
@Override
|
||||||
public void checkMailFailed(Context context, Account account, String reason) {
|
public void checkMailFailed(Context context, Account account, String reason) {
|
||||||
reschedule();
|
reschedule();
|
||||||
|
wakeLockRelease();
|
||||||
stopSelf(mStartId);
|
stopSelf(mStartId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,6 +210,7 @@ public class MailService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
reschedule();
|
reschedule();
|
||||||
|
wakeLockRelease();
|
||||||
stopSelf(mStartId);
|
stopSelf(mStartId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user