From 2ed92f310ed85e02c96c351697a2cfb1eba64e25 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 19 Sep 2018 09:43:57 +0000 Subject: [PATCH] Added notification action to mark messages read Fixes #126 --- .../java/eu/faircode/email/DaoMessage.java | 6 +++ .../eu/faircode/email/ServiceSynchronize.java | 46 ++++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/DaoMessage.java b/app/src/main/java/eu/faircode/email/DaoMessage.java index 1aeef4f228..b937b8c22f 100644 --- a/app/src/main/java/eu/faircode/email/DaoMessage.java +++ b/app/src/main/java/eu/faircode/email/DaoMessage.java @@ -167,6 +167,12 @@ public interface DaoMessage { " AND NOT ui_found" /* keep found messages */) List getUids(long folder, long received); + @Query("SELECT message.* FROM message" + + " JOIN folder ON folder.id = message.folder" + + " WHERE NOT message.ui_seen" + + " AND folder.unified") + List getUnseenUnifiedMessages(); + @Insert long insertMessage(EntityMessage message); diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 11681f9637..40432c7de1 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -27,6 +27,7 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; +import android.graphics.drawable.Icon; import android.media.RingtoneManager; import android.net.ConnectivityManager; import android.net.Network; @@ -107,6 +108,7 @@ import javax.mail.search.ReceivedDateTerm; import javax.net.ssl.SSLException; import androidx.annotation.Nullable; +import androidx.core.app.NotificationCompat; import androidx.core.content.ContextCompat; import androidx.lifecycle.LifecycleService; import androidx.lifecycle.Observer; @@ -229,8 +231,38 @@ public class ServiceSynchronize extends LifecycleService { protected void onLoaded(Bundle args, Void data) { Log.i(Helper.TAG, "Updated seen until"); } - }.load(ServiceSynchronize.this, args); + }.load(this, args); + } else if ("seen".equals(intent.getAction())) { + Bundle args = new Bundle(); + + new SimpleTask() { + @Override + protected Void onLoad(Context context, Bundle args) { + DB db = DB.getInstance(context); + try { + db.beginTransaction(); + + for (EntityMessage message : db.message().getUnseenUnifiedMessages()) { + db.message().setMessageUiSeen(message.id, true); + EntityOperation.queue(db, message, EntityOperation.SEEN, true); + } + + db.setTransactionSuccessful(); + } finally { + db.endTransaction(); + } + + EntityOperation.process(context); + + return null; + } + + @Override + protected void onLoaded(Bundle args, Void data) { + Log.i(Helper.TAG, "Set seen"); + } + }.load(this, args); } return START_STICKY; @@ -284,6 +316,15 @@ public class ServiceSynchronize extends LifecycleService { delete.setAction("unseen"); PendingIntent pid = PendingIntent.getService(this, 1, delete, PendingIntent.FLAG_UPDATE_CURRENT); + Intent seen = new Intent(this, ServiceSynchronize.class); + seen.setAction("seen"); + PendingIntent pis = PendingIntent.getService(this, 2, seen, PendingIntent.FLAG_UPDATE_CURRENT); + + Notification.Action.Builder actionBuilder = new Notification.Action.Builder( + Icon.createWithResource(this, R.drawable.baseline_mail_outline_24), + getString(R.string.title_seen), + pis); + Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); // Build notification @@ -302,7 +343,8 @@ public class ServiceSynchronize extends LifecycleService { .setPriority(Notification.PRIORITY_DEFAULT) .setCategory(Notification.CATEGORY_STATUS) .setVisibility(Notification.VISIBILITY_PUBLIC) - .setDeleteIntent(pid); + .setDeleteIntent(pid) + .addAction(actionBuilder.build()); if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O && prefs.getBoolean("light", false)) {