Files
bubble/android/app/src/main/java/me/teafry/voipmessages/Sync.java
T
trav cde8cc6c55 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

135 lines
5.1 KiB
Java

package me.teafry.voipmessages;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* Checking both providers for new messages. History arrives with the install
* (the desktop copies its database), so this only ever looks at what's recent,
* like the desktop's polling.
*/
final class Sync {
private static final String TAG = "Sync";
private static final long DAY = 86_400_000L;
/** Screens listen for changes to redraw. Called on the main thread. */
interface Listener {
void changed();
}
private static final List<Listener> listeners = new CopyOnWriteArrayList<>();
private static final Handler main = new Handler(Looper.getMainLooper());
static void listen(Listener l) {
listeners.add(l);
}
static void unlisten(Listener l) {
listeners.remove(l);
}
static void changed() {
main.post(() -> {
for (Listener l : listeners) l.changed();
});
}
/** Last problem per provider, shown in Settings and the list header. */
static String voipError, gvError;
static boolean gvSignedOut;
/** Polls both; returns the messages that were new. Call off the main thread. */
static synchronized List<Store.Msg> poll(Context c) {
Config cfg = Config.get(c);
Store store = Store.get(c);
StateSync.importPending(c);
List<Store.Msg> added = new ArrayList<>();
if (cfg.sandbox) {
store.set("last_poll", String.valueOf(System.currentTimeMillis()));
changed();
return added;
}
if (cfg.voipUser != null) {
try {
long now = System.currentTimeMillis();
Set<String> wanted = new HashSet<>();
for (Config.Did d : cfg.dids) if (!d.gv) wanted.add(d.did);
List<Store.Msg> rows = new ArrayList<>();
for (Store.Msg m : new VoipMs(cfg.voipUser, cfg.voipPass).messages(VoipMs.ymd(now - DAY), VoipMs.ymd(now + DAY)))
if (wanted.isEmpty() || wanted.contains(m.did)) rows.add(m);
List<Store.Msg> a = store.upsert(rows);
store.reconcileSent(a);
added.addAll(a);
voipError = null;
} catch (Exception e) {
Log.w(TAG, "voip.ms poll failed", e);
voipError = e.getMessage();
}
}
if (cfg.gvNumber != null) {
try {
added.addAll(pollGv(cfg, store));
gvError = null;
gvSignedOut = false;
} catch (GVoice.Failure e) {
Log.w(TAG, "GV poll failed", e);
gvError = e.getMessage();
gvSignedOut = e.signedOut();
} catch (Exception e) {
Log.w(TAG, "GV poll failed", e);
gvError = e.getMessage();
}
}
store.set("last_poll", String.valueOf(System.currentTimeMillis()));
if (!added.isEmpty()) StateSync.export(c);
// Threads read on the phone or the web came back read (see storeThread).
Notifier.sweep(c);
changed();
return added;
}
@SuppressWarnings("unchecked")
private static List<Store.Msg> pollGv(Config cfg, Store store) throws GVoice.Failure {
GVoice gv = new GVoice(cfg);
List<Store.Msg> added = new ArrayList<>();
List<GVoice.Thread> threads = (List<GVoice.Thread>) gv.listThreads("")[0];
for (GVoice.Thread t : threads) {
if (t.messages.isEmpty()) continue;
String contact = GVoice.threadContact(t, cfg.gvNumber);
if (contact.isEmpty()) continue;
boolean known = false;
for (Pb m : t.messages) if (store.getMessage(GVoice.messageId(m.str(1))) != null) known = true;
boolean hasHistory = t.id.equals(store.gvThread(cfg.gvNumber, contact));
List<Store.Msg> a = storeThread(store, cfg.gvNumber, contact, t, t.messages);
// The list only carries each thread's latest few; fetch the rest of a gap.
if (!known && hasHistory && !t.paginationToken.isEmpty())
a.addAll(storeThread(store, cfg.gvNumber, contact, t, gv.getThread(t.id, 100, "").messages));
added.addAll(a);
}
return added;
}
private static List<Store.Msg> storeThread(Store store, String did, String contact, GVoice.Thread t, List<Pb> messages) {
store.setGvThread(did, contact, t.id);
boolean group = Phone.isGroup(contact);
List<Store.Msg> msgs = new ArrayList<>();
for (Pb m : messages) {
Store.Msg x = GVoice.toMsg(m, did, contact, group);
if (x != null) msgs.add(x);
}
List<Store.Msg> added = store.upsert(msgs);
store.reconcileSent(added);
// Already read on the phone or the web: not unread here either.
if (!added.isEmpty() && t.read) store.markRead(did, contact);
return added;
}
}