package me.teafry.voipmessages; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.service.notification.StatusBarNotification; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; /** One notification per conversation with new incoming messages. */ final class Notifier { private Notifier() {} private static final String CHANNEL = "messages"; // Only messages this recent notify (not a big catch-up after being offline). private static final long RECENT = 6 * 60 * 60_000L; /** Screens in front; while one is, new messages don't notify. */ static int visible; static void notifyNew(Context c, List added) { if (!Config.notifications(c) || visible > 0) return; NotificationManager nm = c.getSystemService(NotificationManager.class); nm.createNotificationChannel(new NotificationChannel(CHANNEL, "Messages", NotificationManager.IMPORTANCE_HIGH)); long now = System.currentTimeMillis(); Map> byConv = new LinkedHashMap<>(); for (Store.Msg m : added) if (m.in() && m.ts > now - RECENT) byConv.computeIfAbsent(m.did + "\n" + m.contact, k -> new ArrayList<>()).add(m); Store store = Store.get(c); for (List list : byConv.values()) { Store.Msg last = list.get(0); for (Store.Msg m : list) if (m.ts > last.ts) last = m; String title = Contacts.title(c, last.contact, store.getName(last.did, last.contact)); String reaction = Reactions.preview(last.body); String body = reaction != null ? reaction : last.body.trim().isEmpty() && last.mediaJson != null ? "📷 Photo" : last.body.trim(); if (Phone.isGroup(last.contact) && last.sender != null) body = Contacts.firstName(c, last.sender) + ": " + body; if (list.size() > 1) body += " (+" + (list.size() - 1) + " more)"; Intent open = ChatActivity.intent(c, last.did, last.contact); int id = (last.did + last.contact).hashCode(); PendingIntent pi = PendingIntent.getActivity(c, id, open, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); Notification n = new Notification.Builder(c, CHANNEL) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle(title) .setContentText(body) .setStyle(new Notification.BigTextStyle().bigText(body)) .setWhen(last.ts) .setShowWhen(true) .setAutoCancel(true) .setContentIntent(pi) .addExtras(conversation(last.did, last.contact)) .build(); nm.notify(id, n); } } static void clear(Context c, String did, String contact) { c.getSystemService(NotificationManager.class).cancel((did + contact).hashCode()); } private static Bundle conversation(String did, String contact) { Bundle b = new Bundle(); b.putString("did", did); b.putString("contact", contact); return b; } /** * Take down the notification of every conversation that's been read since, * here or anywhere else (the desktop, the phone, the web), so the shade * only holds what's still unread. Call after read state changes. */ static void sweep(Context c) { NotificationManager nm = c.getSystemService(NotificationManager.class); Store store = Store.get(c); for (StatusBarNotification sbn : nm.getActiveNotifications()) { Bundle x = sbn.getNotification().extras; String did = x.getString("did"), contact = x.getString("contact"); if (did != null && contact != null && !store.hasUnread(did, contact)) nm.cancel(sbn.getId()); } } }