diff --git a/app/src/main/java/eu/faircode/email/EntityMessage.java b/app/src/main/java/eu/faircode/email/EntityMessage.java index 76580a0165..c3b97a25b3 100644 --- a/app/src/main/java/eu/faircode/email/EntityMessage.java +++ b/app/src/main/java/eu/faircode/email/EntityMessage.java @@ -452,10 +452,10 @@ public class EntityMessage implements Serializable { } static void snooze(Context context, long id, Long wakeup) { - Intent snoozed = new Intent(context, ServiceUI.class); - snoozed.setAction("wakeup:" + id); - PendingIntent pi = PendingIntentCompat.getService( - context, ServiceUI.PI_WAKEUP, snoozed, PendingIntent.FLAG_UPDATE_CURRENT); + Intent snoozed = new Intent(context, ServiceSynchronize.class); + snoozed.setAction("unsnooze:" + id); + PendingIntent pi = PendingIntentCompat.getForegroundService( + context, ServiceSynchronize.PI_UNSNOOZE, snoozed, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); if (wakeup == null || wakeup == Long.MAX_VALUE) { diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 5c7bb52de4..917cb50dab 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -56,6 +56,8 @@ import com.sun.mail.imap.IMAPStore; import com.sun.mail.imap.protocol.IMAPProtocol; import com.sun.mail.imap.protocol.IMAPResponse; +import java.io.File; +import java.io.IOException; import java.text.DateFormat; import java.util.ArrayList; import java.util.Arrays; @@ -156,6 +158,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences static final int PI_ENABLE = 4; static final int PI_POLL = 5; static final int PI_WATCHDOG = 6; + static final int PI_UNSNOOZE = 7; @Override public void onCreate() { @@ -815,6 +818,10 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences onWakeup(intent); break; + case "unsnooze": + onUnsnooze(intent); + break; + case "state": onState(intent); break; @@ -883,6 +890,99 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences } } + private void onUnsnooze(Intent intent) { + String action = intent.getAction(); + long id = Long.parseLong(action.split(":")[1]); + + executor.submit(new Runnable() { + @Override + public void run() { + try { + EntityFolder folder; + + DB db = DB.getInstance(ServiceSynchronize.this); + try { + db.beginTransaction(); + + EntityMessage message = db.message().getMessage(id); + if (message == null) + return; + + folder = db.folder().getFolder(message.folder); + if (folder == null) + return; + + if (EntityFolder.OUTBOX.equals(folder.type)) { + Log.i("Delayed send id=" + message.id); + if (message.ui_snoozed != null) { + db.message().setMessageSnoozed(message.id, null); + EntityOperation.queue(ServiceSynchronize.this, message, EntityOperation.SEND); + } + } else { + if (folder.notify) { + List attachments = db.attachment().getAttachments(id); + + // A new message ID is needed for a new (wearable) notification + db.message().deleteMessage(id); + + message.id = null; + message.fts = false; + message.id = db.message().insertMessage(message); + + if (message.content) { + File source = EntityMessage.getFile(ServiceSynchronize.this, id); + File target = message.getFile(ServiceSynchronize.this); + try { + Helper.copy(source, target); + } catch (IOException ex) { + Log.e(ex); + db.message().resetMessageContent(message.id); + } + } + + for (EntityAttachment attachment : attachments) { + File source = attachment.getFile(ServiceSynchronize.this); + + attachment.id = null; + attachment.message = message.id; + attachment.progress = null; + attachment.id = db.attachment().insertAttachment(attachment); + + if (attachment.available) { + File target = attachment.getFile(ServiceSynchronize.this); + try { + Helper.copy(source, target); + } catch (IOException ex) { + Log.e(ex); + db.attachment().setError(attachment.id, Log.formatThrowable(ex, false)); + } + } + } + } + + db.message().setMessageSnoozed(message.id, null); + if (!message.ui_ignored) { + db.message().setMessageUnsnoozed(message.id, true); + EntityOperation.queue(ServiceSynchronize.this, message, EntityOperation.SEEN, false, false); + } + } + + db.setTransactionSuccessful(); + } finally { + db.endTransaction(); + } + + if (EntityFolder.OUTBOX.equals(folder.type)) + ServiceSend.start(ServiceSynchronize.this); + else + ServiceSynchronize.eval(ServiceSynchronize.this, "unsnooze"); + } catch (Throwable ex) { + Log.e(ex); + } + } + }); + } + private void onState(Intent intent) { foreground = intent.getBooleanExtra("foreground", false); } diff --git a/app/src/main/java/eu/faircode/email/ServiceUI.java b/app/src/main/java/eu/faircode/email/ServiceUI.java index 2bb7d1acbc..7e378f45ec 100644 --- a/app/src/main/java/eu/faircode/email/ServiceUI.java +++ b/app/src/main/java/eu/faircode/email/ServiceUI.java @@ -153,11 +153,7 @@ public class ServiceUI extends IntentService { break; case "wakeup": - // AlarmManager.RTC_WAKEUP - // When the alarm is dispatched, the app will also be added to the system's temporary whitelist - // for approximately 10 seconds to allow that application to acquire further wake locks in which to complete its work. - // https://developer.android.com/reference/android/app/AlarmManager - onWakeup(id); + // ignore break; case "sync": @@ -446,87 +442,6 @@ public class ServiceUI extends IntentService { } } - private void onWakeup(long id) { - EntityFolder folder; - - DB db = DB.getInstance(this); - try { - db.beginTransaction(); - - EntityMessage message = db.message().getMessage(id); - if (message == null) - return; - - folder = db.folder().getFolder(message.folder); - if (folder == null) - return; - - if (EntityFolder.OUTBOX.equals(folder.type)) { - Log.i("Delayed send id=" + message.id); - if (message.ui_snoozed != null) { - db.message().setMessageSnoozed(message.id, null); - EntityOperation.queue(this, message, EntityOperation.SEND); - } - } else { - if (folder.notify) { - List attachments = db.attachment().getAttachments(id); - - // A new message ID is needed for a new (wearable) notification - db.message().deleteMessage(id); - - message.id = null; - message.fts = false; - message.id = db.message().insertMessage(message); - - if (message.content) { - File source = EntityMessage.getFile(this, id); - File target = message.getFile(this); - try { - Helper.copy(source, target); - } catch (IOException ex) { - Log.e(ex); - db.message().resetMessageContent(message.id); - } - } - - for (EntityAttachment attachment : attachments) { - File source = attachment.getFile(this); - - attachment.id = null; - attachment.message = message.id; - attachment.progress = null; - attachment.id = db.attachment().insertAttachment(attachment); - - if (attachment.available) { - File target = attachment.getFile(this); - try { - Helper.copy(source, target); - } catch (IOException ex) { - Log.e(ex); - db.attachment().setError(attachment.id, Log.formatThrowable(ex, false)); - } - } - } - } - - db.message().setMessageSnoozed(message.id, null); - if (!message.ui_ignored) { - db.message().setMessageUnsnoozed(message.id, true); - EntityOperation.queue(this, message, EntityOperation.SEEN, false, false); - } - } - - db.setTransactionSuccessful(); - } finally { - db.endTransaction(); - } - - if (EntityFolder.OUTBOX.equals(folder.type)) - ServiceSend.start(this); - else - ServiceSynchronize.eval(this, "unsnooze"); - } - private void onSync(long aid) { DB db = DB.getInstance(this); try {