aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/invalid/lena/scrcpy/Devices.java
diff options
context:
space:
mode:
authorLena <lena@omega>2026-01-01 00:00:00 +0000
committerLena <lena@omega>2026-06-24 22:14:50 +0300
commiteb0c8951196c637e44daf3c0617b131b997d5d2c (patch)
tree2f83b6a41cee745467e53fc401942b82cea286ea /app/src/main/java/invalid/lena/scrcpy/Devices.java
downloadscrcpy-android-eb0c8951196c637e44daf3c0617b131b997d5d2c.tar.gz
scrcpy-android: mirror an Android device over wireless ADB0.1
Native Java app for Android 12+ that mirrors another Android device over wireless ADB, forwarding video, audio, touch input, and clipboard. Bundles a pinned scrcpy-server.jar and the vendored libadb-android stack. Supports h264/h265/av1 video and raw/opus audio with in-app codec selection. No NDK, no Kotlin. Includes JVM unit tests and a Docker-based emulator e2e rig.
Diffstat (limited to 'app/src/main/java/invalid/lena/scrcpy/Devices.java')
-rw-r--r--app/src/main/java/invalid/lena/scrcpy/Devices.java126
1 files changed, 126 insertions, 0 deletions
diff --git a/app/src/main/java/invalid/lena/scrcpy/Devices.java b/app/src/main/java/invalid/lena/scrcpy/Devices.java
new file mode 100644
index 0000000..2883025
--- /dev/null
+++ b/app/src/main/java/invalid/lena/scrcpy/Devices.java
@@ -0,0 +1,126 @@
+package invalid.lena.scrcpy;
+
+import android.content.Context;
+
+import org.json.JSONArray;
+import org.json.JSONObject;
+
+import java.io.File;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.ArrayList;
+import java.util.List;
+
+// Tiny JSON-backed list of paired targets, stored at filesDir/devices.json.
+// No SQLite, no Room. Read on demand, written whole-file on add/remove.
+//
+// parse() / serialize() are pure-java and android-free for unit tests.
+// load() / save() add the Context-rooted file I/O.
+public final class Devices {
+
+ private static final String FILE = "devices.json";
+
+ public static final class Device {
+ public final String host;
+ public final int port;
+
+ public Device(String host, int port) {
+ this.host = host;
+ this.port = port;
+ }
+
+ @Override
+ public String toString() {
+ return host + ":" + port;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (!(o instanceof Device d)) return false;
+ return port == d.port && host.equals(d.host);
+ }
+
+ @Override
+ public int hashCode() {
+ return host.hashCode() * 31 + port;
+ }
+ }
+
+ private Devices() {}
+
+ // Pure-java parse: returns whatever rows are well-formed; logs and
+ // skips anything malformed instead of nuking the list.
+ public static List<Device> parse(String json) {
+ List<Device> out = new ArrayList<>();
+ if (json == null) return out;
+ json = json.trim();
+ if (json.isEmpty()) return out;
+ JSONArray arr;
+ try { arr = new JSONArray(json); }
+ catch (Exception e) { Log.e(e, "devices: not a json array"); return out; }
+
+ for (int i = 0; i < arr.length(); i++) {
+ try {
+ JSONObject o = arr.getJSONObject(i);
+ out.add(new Device(o.getString("host"), o.getInt("port")));
+ } catch (Exception e) {
+ Log.w("devices: skipping malformed row %d: %s", i, e);
+ }
+ }
+ return out;
+ }
+
+ public static String serialize(List<Device> devices) {
+ try {
+ JSONArray arr = new JSONArray();
+ for (Device d : devices) {
+ JSONObject o = new JSONObject();
+ o.put("host", d.host);
+ o.put("port", d.port);
+ arr.put(o);
+ }
+ return arr.toString(2);
+ } catch (Exception e) {
+ Log.e(e, "devices: serialize failed");
+ return "[]";
+ }
+ }
+
+ public static List<Device> load(Context ctx) {
+ File f = new File(ctx.getFilesDir(), FILE);
+ if (!f.exists()) return new ArrayList<>();
+ try {
+ return parse(new String(Files.readAllBytes(f.toPath()), StandardCharsets.UTF_8));
+ } catch (Exception e) {
+ Log.e(e, "devices: load failed");
+ return new ArrayList<>();
+ }
+ }
+
+ public static void save(Context ctx, List<Device> devices) {
+ File f = new File(ctx.getFilesDir(), FILE);
+ try {
+ AtomicFiles.write(f, serialize(devices).getBytes(StandardCharsets.UTF_8));
+ } catch (Exception e) {
+ Log.e(e, "devices: save failed");
+ }
+ }
+
+ // Add or replace by host+port. Context-rooted; the in-place helper
+ // is the test seam.
+ public static List<Device> upsert(Context ctx, Device d) {
+ List<Device> list = load(ctx);
+ list.removeIf(d::equals);
+ list.add(d);
+ save(ctx, list);
+ return list;
+ }
+
+ // Remove the matching device (by host+port). Returns the updated list.
+ public static List<Device> remove(Context ctx, Device d) {
+ List<Device> list = load(ctx);
+ list.removeIf(d::equals);
+ save(ctx, list);
+ return list;
+ }
+}