diff --git a/app/src/main/java/eu/faircode/email/PendingIntentCompat.java b/app/src/main/java/eu/faircode/email/PendingIntentCompat.java new file mode 100644 index 0000000000..1d9466bd8e --- /dev/null +++ b/app/src/main/java/eu/faircode/email/PendingIntentCompat.java @@ -0,0 +1,39 @@ +package eu.faircode.email; + +/* + This file is part of FairEmail. + + FairEmail is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + FairEmail is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with FairEmail. If not, see . + + Copyright 2018-2019 by Marcel Bokhorst (M66B) +*/ + +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; +import android.os.Build; + +import androidx.annotation.NonNull; + +public class PendingIntentCompat { + private PendingIntentCompat() { + } + + static PendingIntent getForegroundService(Context context, int requestCode, @NonNull Intent intent, int flags) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) + return PendingIntent.getService(context, requestCode, intent, flags); + else + return PendingIntent.getForegroundService(context, requestCode, intent, flags); + } +} diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 1bbfb96ae7..45c7629703 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -119,7 +119,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences )); static final int PI_ALARM = 1; - static final int PI_WAKEUP = 2; + static final int PI_BACKOFF = 2; + static final int PI_KEEPALIVE = 2; @Override public void onCreate() { @@ -562,7 +563,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences onReload(intent); break; - case "wakeup": + case "backoff": + case "keepalive": onWakeup(intent); break; @@ -708,14 +710,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences try { wlAccount.acquire(); - Intent wakeup = new Intent(ServiceSynchronize.this, ServiceSynchronize.class); - wakeup.setAction("wakeup:" + account.id); - PendingIntent piWakeup; - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) - piWakeup = PendingIntent.getService(this, PI_WAKEUP, wakeup, PendingIntent.FLAG_UPDATE_CURRENT); - else - piWakeup = PendingIntent.getForegroundService(this, PI_WAKEUP, wakeup, PendingIntent.FLAG_UPDATE_CURRENT); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (account.notify) account.createNotificationChannel(ServiceSynchronize.this); @@ -1177,13 +1171,18 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences nm.cancel("alert:" + account.id, 1); // Schedule keep alive alarm + Intent intent = new Intent(ServiceSynchronize.this, ServiceSynchronize.class); + intent.setAction("keepalive:" + account.id); + PendingIntent pi = PendingIntentCompat.getForegroundService( + this, PI_KEEPALIVE, intent, PendingIntent.FLAG_UPDATE_CURRENT); + AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); try { long duration = account.poll_interval * 60 * 1000L; long trigger = System.currentTimeMillis() + duration; EntityLog.log(this, "### " + account.name + " keep alive" + " wait=" + account.poll_interval + " until=" + new Date(trigger)); - AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, trigger, piWakeup); + AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, trigger, pi); try { wlAccount.release(); @@ -1195,7 +1194,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences wlAccount.acquire(); } } finally { - am.cancel(piWakeup); + am.cancel(pi); } } @@ -1264,13 +1263,18 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences } } else { // Long back-off period, let device sleep + Intent intent = new Intent(ServiceSynchronize.this, ServiceSynchronize.class); + intent.setAction("backoff:" + account.id); + PendingIntent pi = PendingIntentCompat.getForegroundService( + this, PI_BACKOFF, intent, PendingIntent.FLAG_UPDATE_CURRENT); + AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); try { long duration = CONNECT_BACKOFF_AlARM * 60 * 1000L; long trigger = System.currentTimeMillis() + duration; EntityLog.log(this, "### " + account.name + " backoff" + " alarm=" + CONNECT_BACKOFF_AlARM + " until=" + new Date(trigger)); - AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, trigger, piWakeup); + AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, trigger, pi); try { wlAccount.release(); @@ -1282,7 +1286,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences wlAccount.acquire(); } } finally { - am.cancel(piWakeup); + am.cancel(pi); } } @@ -1463,16 +1467,13 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences } private static void schedule(Context context) { - Intent alarm = new Intent(context, ServiceSynchronize.class); - alarm.setAction("alarm"); - PendingIntent piAlarm; - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) - piAlarm = PendingIntent.getService(context, PI_ALARM, alarm, PendingIntent.FLAG_UPDATE_CURRENT); - else - piAlarm = PendingIntent.getForegroundService(context, PI_ALARM, alarm, PendingIntent.FLAG_UPDATE_CURRENT); + Intent intent = new Intent(context, ServiceSynchronize.class); + intent.setAction("alarm"); + PendingIntent pi = PendingIntentCompat.getForegroundService( + context, PI_ALARM, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); - am.cancel(piAlarm); + am.cancel(pi); long[] schedule = getSchedule(context); if (schedule == null) @@ -1488,7 +1489,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences Log.i("Schedule next=" + new Date(next)); Log.i("Schedule enabled=" + enabled); - AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, next, piAlarm); + AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, next, pi); WorkerPoll.init(context); }