Files
bubble/android/app/src/main/java/me/teafry/voipmessages/Notifier.java
T
trav 8c84feaa91 v0.2.0: bubble for Android 0.2.0 — chat search, swipe for unread, faster chat opening
Desktop:
- Search lists chats whose own name matches first, so a named group
  isn't pushed out of the top 8 by chats that merely include a member.

Android (Rabbit R1) app, 0.2.0:
- Pull the chat list past its top for a search box (name, person,
  number, then group members; archived chats too; Back clears it).
- Swipe right to left marks a chat unread / read over a magenta band;
  left to right still archives (SwipeActions replaces SwipeArchive).
- Google Voice chats don't offer voice notes in the paperclip bubble.
- Chats open with their newest 60 messages, then the full history;
  marking read (and the Google Voice network call) waits until they're
  on screen. Cached pictures show at once, and one slow download no
  longer blocks the other images. Fixed a contacts reload race.
- Sandbox data gains filler chats, a named group and a 4,000-message chat.

Also: the Android app, Google Voice support, media, export, emoji and
the other work since the first commit, and a CLAUDE.md carrying the
privacy rule.
2026-11-06 02:19:56 -08:00

89 lines
4.0 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);
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());
}
}
}