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 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 poll(Context c) { Config cfg = Config.get(c); Store store = Store.get(c); StateSync.importPending(c); List 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 wanted = new HashSet<>(); for (Config.Did d : cfg.dids) if (!d.gv) wanted.add(d.did); List 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 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 pollGv(Config cfg, Store store) throws GVoice.Failure { GVoice gv = new GVoice(cfg); List added = new ArrayList<>(); List threads = (List) 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 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 storeThread(Store store, String did, String contact, GVoice.Thread t, List messages) { store.setGvThread(did, contact, t.id); boolean group = Phone.isGroup(contact); List msgs = new ArrayList<>(); for (Pb m : messages) { Store.Msg x = GVoice.toMsg(m, did, contact, group); if (x != null) msgs.add(x); } List 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; } }