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 gvCookies = new LinkedHashMap<>(); final List 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 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 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); } }