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

147 lines
5.0 KiB
Java

package me.teafry.voipmessages;
import android.content.Context;
import android.content.SharedPreferences;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Accounts, copied over by the desktop's "Install to Rabbit" into the app's
* private files dir (files/config.json, which no other app can read):
*
* {"voipms": {"username", "password"} | null,
* "gvoice": {"number", "cookies": {name: value}} | null,
* "dids": [{"did", "description"}], "defaultDid": "..."}
*
* Google rotates some cookies as we use them; those go back into the file.
*/
final class Config {
static final class Did {
final String did, description;
final boolean gv;
Did(String did, String description, boolean gv) {
this.did = did;
this.description = description;
this.gv = gv;
}
}
String voipUser, voipPass;
String gvNumber;
final Map<String, String> gvCookies = new LinkedHashMap<>();
final List<Did> dids = new ArrayList<>();
String defaultDid;
/** Made-up data for testing (android/tools/push_sandbox.py): never goes online. */
boolean sandbox;
private final File file;
private JSONObject raw;
private Config(File file) {
this.file = file;
}
private static Config cached;
static synchronized Config get(Context c) {
File f = new File(c.getFilesDir(), "config.json");
if (cached == null || cached.stale()) cached = load(f);
return cached;
}
private long loadedAt;
private boolean stale() {
return file.lastModified() > loadedAt;
}
private static Config load(File f) {
Config cfg = new Config(f);
cfg.loadedAt = f.lastModified();
try {
JSONObject o = new JSONObject(new String(Files.readAllBytes(f.toPath()), StandardCharsets.UTF_8));
cfg.raw = o;
JSONObject v = o.optJSONObject("voipms");
if (v != null) {
cfg.voipUser = v.optString("username", null);
cfg.voipPass = v.optString("password", null);
}
JSONObject g = o.optJSONObject("gvoice");
if (g != null) {
cfg.gvNumber = Phone.normalize(g.optString("number"));
JSONObject ck = g.optJSONObject("cookies");
if (ck != null)
for (Iterator<String> it = ck.keys(); it.hasNext(); ) {
String k = it.next();
cfg.gvCookies.put(k, ck.getString(k));
}
}
JSONArray ds = o.optJSONArray("dids");
if (ds != null)
for (int i = 0; i < ds.length(); i++) {
JSONObject d = ds.getJSONObject(i);
cfg.dids.add(new Did(Phone.normalize(d.getString("did")), d.optString("description"), false));
}
if (cfg.gvNumber != null && !cfg.gvNumber.isEmpty()) cfg.dids.add(new Did(cfg.gvNumber, "Google Voice", true));
cfg.defaultDid = o.optString("defaultDid", null);
cfg.sandbox = o.optBoolean("sandbox", false);
} catch (IOException | JSONException e) {
cfg.raw = new JSONObject();
}
if (cfg.defaultDid == null && !cfg.dids.isEmpty()) cfg.defaultDid = cfg.dids.get(0).did;
return cfg;
}
boolean configured() {
return voipUser != null || gvNumber != null;
}
boolean isGv(String did) {
return gvNumber != null && gvNumber.equals(did);
}
/** Persist cookies Google rotated. */
synchronized void saveCookies() {
try {
JSONObject g = raw.optJSONObject("gvoice");
if (g == null) return;
JSONObject ck = new JSONObject();
for (Map.Entry<String, String> e : gvCookies.entrySet()) ck.put(e.getKey(), e.getValue());
g.put("cookies", ck);
File tmp = new File(file.getPath() + ".tmp");
try (FileOutputStream out = new FileOutputStream(tmp)) {
out.write(raw.toString().getBytes(StandardCharsets.UTF_8));
}
if (tmp.renameTo(file)) loadedAt = file.lastModified();
} catch (IOException | JSONException ignored) {
}
}
// ---- settings that belong to this device ----
static SharedPreferences prefs(Context c) {
return c.getSharedPreferences("settings", Context.MODE_PRIVATE);
}
/** Minutes between background checks; 0 = never (pull to refresh only). */
static int pollMinutes(Context c) {
return prefs(c).getInt("pollMinutes", 15);
}
static boolean notifications(Context c) {
return prefs(c).getBoolean("notifications", true);
}
}