package com.lanremote.watch; import android.content.Context; import android.content.SharedPreferences; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.UUID; /** * 设备与连接信息持久化。 * * 配对成功后保存令牌,下次打开直接从设备列表一键连接,无需再输配对码。 */ public class DeviceStore { private static final String PREFS = "lanremote"; private static final String KEY_DEVICES = "devices"; private static final String KEY_DEVICE_ID = "device_id"; private static final String KEY_DEVICE_NAME = "device_name"; private static final String KEY_LAST_HOST = "last_host"; private static final String KEY_LAST_PORT = "last_port"; private final SharedPreferences prefs; public DeviceStore(Context context) { this.prefs = context.getApplicationContext() .getSharedPreferences(PREFS, Context.MODE_PRIVATE); } // ---------------- 本机标识 ---------------- public String deviceId() { String id = prefs.getString(KEY_DEVICE_ID, ""); if (id == null || id.isEmpty()) { id = "watch-" + UUID.randomUUID().toString().replace("-", "").substring(0, 16); prefs.edit().putString(KEY_DEVICE_ID, id).apply(); } return id; } public String deviceName() { String name = prefs.getString(KEY_DEVICE_NAME, ""); if (name == null || name.isEmpty()) { name = android.os.Build.MODEL; if (name == null || name.trim().isEmpty()) { name = "手表"; } prefs.edit().putString(KEY_DEVICE_NAME, name).apply(); } return name; } public void setDeviceName(String name) { prefs.edit().putString(KEY_DEVICE_NAME, name == null ? "手表" : name.trim()).apply(); } // ---------------- 上次连接 ---------------- public String lastHost() { return prefs.getString(KEY_LAST_HOST, ""); } public int lastPort() { return prefs.getInt(KEY_LAST_PORT, RemoteClient.DEFAULT_PORT); } public void setLastEndpoint(String host, int port) { prefs.edit().putString(KEY_LAST_HOST, host).putInt(KEY_LAST_PORT, port).apply(); } // ---------------- 已配对设备 ---------------- public List> devices() { return readDevices(); } private List> readDevices() { String raw = prefs.getString(KEY_DEVICES, ""); if (raw == null || raw.isEmpty()) { return new ArrayList>(); } try { Object parsed = Json.parse(raw); if (parsed instanceof List) { List> result = new ArrayList>(); for (Object item : (List) parsed) { Map map = Json.asMap(item); if (map != null) { result.add(map); } } return result; } } catch (RuntimeException ignored) { // 存储损坏时当作空列表,不崩溃 } return new ArrayList>(); } private void writeDevices(List> devices) { prefs.edit().putString(KEY_DEVICES, Json.write(devices)).apply(); } /** 保存或更新一台设备(按 key = 地址:端口:设备ID 去重)。 */ public void saveDevice(String key, String label, String host, int port, String token) { List> devices = readDevices(); List> updated = new ArrayList>(); for (Map device : devices) { if (!key.equals(Json.str(device, "key", ""))) { updated.add(device); } } updated.add(0, Json.obj( "key", key, "label", label, "host", host, "port", port, "token", token, "savedAt", System.currentTimeMillis())); while (updated.size() > 12) { updated.remove(updated.size() - 1); } writeDevices(updated); } public void removeDevice(String key) { List> devices = readDevices(); List> updated = new ArrayList>(); for (Map device : devices) { if (!key.equals(Json.str(device, "key", ""))) { updated.add(device); } } writeDevices(updated); } /** 清空所有已配对设备(设置页的清空用)。 */ public void clearDevices() { prefs.edit().remove(KEY_DEVICES).apply(); } public static String deviceKey(String host, int port, String deviceId) { return host + ":" + port + ":" + deviceId; } }