Files
bubble/android/app/src/main/java/me/teafry/voipmessages/Notifier.java
T
trav 09a46aa35c v0.7.0: bubble for Android 0.7.0 — react to messages, reactions in other languages, and edits
Android, ported from the desktop's reactions.ts / edits.ts (same readings:
checked side by side on the same 32 cases):
- Long-press a message: the quick reaction row (yours lit, tap again to take
  it back), + for every emoji (EmojiPicker, the desktop's emoji list as
  res/raw/emoji_list.txt, minus any the font can't draw), and Copy text.
- Settings → Reactions → Quick reactions: pick a spot, then its emoji; Reset.
- Tapbacks in other languages fold like the desktop's; "Edited to “…”" edits
  its message, which says "Edited · time"; tap it for the earlier texts.
- Chat list and notifications preview an edit as "✏️ new text".
- push_sandbox.py has a Spanish tapback and an edit to try it on.

Desktop: version 0.7.0; gen-emoji also writes the Android list.
2026-11-10 17:38:09 -08:00

90 lines
4.1 KiB
Java

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<Store.Msg> 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<String, List<Store.Msg>> 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<Store.Msg> 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);
if (reaction == null) reaction = Edits.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());
}
}
}