Show sender/subject/time in new messages notification

This commit is contained in:
M66B 2018-08-24 18:06:44 +00:00
parent 873e3e6f47
commit 07194fb664
4 changed files with 41 additions and 19 deletions

View File

@ -57,13 +57,6 @@ public interface DaoAccount {
" JOIN account ON account.id = message.account" +
" WHERE synchronize) AS operations" +
", (SELECT COUNT(message.id) FROM message" +
" JOIN account ON account.id = message.account" +
" JOIN folder ON folder.id = message.folder" +
" WHERE account.`synchronize`" +
" AND NOT message.ui_seen AND NOT message.ui_hide" +
" AND (account.seen_until IS NULL OR message.stored > account.seen_until)" +
" AND folder.type = '" + EntityFolder.INBOX + "') AS unseen" +
", (SELECT COUNT(message.id) FROM message" +
" JOIN folder ON folder.id = message.folder" +
" JOIN operation ON operation.message = message.id AND operation.name = '" + EntityOperation.SEND + "'" +
" WHERE NOT message.ui_seen" +

View File

@ -35,15 +35,14 @@ public interface DaoMessage {
// all bare columns in the result set take values from the input row which also contains the minimum or maximum."
// https://www.sqlite.org/lang_select.html
@Query("SELECT message.*, maccount.name AS accountName, folder.name as folderName, folder.type as folderType" +
@Query("SELECT message.*, account.name AS accountName, folder.name as folderName, folder.type as folderType" +
", COUNT(message.id) as count" +
", SUM(CASE WHEN message.ui_seen THEN 0 ELSE 1 END) as unseen" +
", (SELECT COUNT(a.id) FROM attachment a WHERE a.message = message.id) AS attachments" +
", MAX(CASE WHEN folder.type = '" + EntityFolder.INBOX + "' THEN message.id ELSE 0 END) as dummy" +
" FROM message" +
" LEFT JOIN account maccount ON maccount.id = message.account" +
" JOIN account ON account.id = message.account" +
" JOIN folder ON folder.id = message.folder" +
" JOIN account ON account.id = folder.account" +
" WHERE account.`synchronize`" +
" AND (NOT message.ui_hide OR :debug)" +
" GROUP BY CASE WHEN message.thread IS NULL THEN message.id ELSE message.thread END" +
@ -118,6 +117,16 @@ public interface DaoMessage {
" WHERE message.id = :id")
LiveData<TupleMessageEx> liveMessage(long id);
@Query("SELECT message.* FROM message" +
" JOIN account ON account.id = message.account" +
" JOIN folder ON folder.id = message.folder" +
" WHERE account.`synchronize`" +
" AND folder.type = '" + EntityFolder.INBOX + "'" +
" AND NOT message.ui_seen AND NOT message.ui_hide" +
" AND (account.seen_until IS NULL OR message.stored > account.seen_until)" +
" ORDER BY message.received")
LiveData<List<EntityMessage>> liveUnseenUnified();
@Query("SELECT uid FROM message WHERE folder = :folder AND received >= :received AND NOT uid IS NULL")
List<Long> getUids(long folder, long received);

View File

@ -59,6 +59,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketTimeoutException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
@ -141,24 +143,32 @@ public class ServiceSynchronize extends LifecycleService {
// builder.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
cm.registerNetworkCallback(builder.build(), serviceManager);
DB.getInstance(this).account().liveStats().observe(this, new Observer<TupleAccountStats>() {
private int prev_unseen = -1;
DB db = DB.getInstance(this);
db.account().liveStats().observe(this, new Observer<TupleAccountStats>() {
@Override
public void onChanged(@Nullable TupleAccountStats stats) {
NotificationManager nm = getSystemService(NotificationManager.class);
nm.notify(NOTIFICATION_SYNCHRONIZE,
getNotificationService(stats.accounts, stats.operations, stats.unsent).build());
}
});
if (stats.unseen > 0) {
if (stats.unseen > prev_unseen) {
db.message().liveUnseenUnified().observe(this, new Observer<List<EntityMessage>>() {
private int prev_unseen = -1;
@Override
public void onChanged(List<EntityMessage> messages) {
NotificationManager nm = getSystemService(NotificationManager.class);
if (messages.size() > 0) {
if (messages.size() > prev_unseen) {
nm.cancel(NOTIFICATION_UNSEEN);
nm.notify(NOTIFICATION_UNSEEN, getNotificationUnseen(stats.unseen).build());
nm.notify(NOTIFICATION_UNSEEN, getNotificationUnseen(messages).build());
}
} else
nm.cancel(NOTIFICATION_UNSEEN);
prev_unseen = stats.unseen;
prev_unseen = messages.size();
}
});
}
@ -257,7 +267,7 @@ public class ServiceSynchronize extends LifecycleService {
return builder;
}
private Notification.Builder getNotificationUnseen(int unseen) {
private Notification.Builder getNotificationUnseen(List<EntityMessage> messages) {
// Build pending intent
Intent intent = new Intent(this, ActivityView.class);
intent.setAction("unseen");
@ -271,6 +281,16 @@ public class ServiceSynchronize extends LifecycleService {
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
DateFormat df = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.SHORT);
StringBuilder sb = new StringBuilder();
for (EntityMessage message : messages) {
sb.append(MessageHelper.getFormattedAddresses(message.from, false));
if (!TextUtils.isEmpty(message.subject))
sb.append(": ").append(message.subject);
sb.append(" ").append(df.format(new Date(message.sent)));
sb.append("\n");
}
// Build notification
Notification.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
@ -280,8 +300,9 @@ public class ServiceSynchronize extends LifecycleService {
builder
.setSmallIcon(R.drawable.baseline_mail_24)
.setContentTitle(getResources().getQuantityString(R.plurals.title_notification_unseen, unseen, unseen))
.setContentTitle(getResources().getQuantityString(R.plurals.title_notification_unseen, messages.size(), messages.size()))
.setContentIntent(pi)
.setStyle(new Notification.BigTextStyle().bigText(sb.toString()))
.setSound(uri)
.setShowWhen(false)
.setPriority(Notification.PRIORITY_DEFAULT)

View File

@ -22,6 +22,5 @@ package eu.faircode.email;
public class TupleAccountStats {
public Integer accounts;
public Integer operations;
public Integer unseen;
public Integer unsent;
}