feat: SettingsStore shared prefs; Main/session/event read persisted config

This commit is contained in:
2026-09-01 19:03:16 +08:00
parent 4cb53a1f0b
commit fd62ea9d95
5 changed files with 50 additions and 7 deletions
@@ -65,7 +65,10 @@ public class EventLogActivity extends AppCompatActivity {
}
});
client.connect("ws://10.0.2.2:8787/ws", "dshwatch", "change-me");
client.connect(
SettingsStore.serverUrl(EventLogActivity.this),
SettingsStore.userId(EventLogActivity.this),
SettingsStore.token(EventLogActivity.this));
}
@Override
@@ -122,8 +122,10 @@ public class MainActivity extends AppCompatActivity {
@Override
public void onClick(View v) {
status.setText("连接中...");
// 空壳:默认连本机模拟器 server;真实地址/用户/Token 从设置读取
serverClient.connect("ws://10.0.2.2:8787/ws", "dshwatch", "change-me");
String serverUrl = SettingsStore.serverUrl(MainActivity.this);
String userId = SettingsStore.userId(MainActivity.this);
String token = SettingsStore.token(MainActivity.this);
serverClient.connect(serverUrl, userId, token);
watchBridge.connect();
}
});
@@ -83,8 +83,10 @@ public class SessionListActivity extends AppCompatActivity {
}
});
// 空壳:默认连本机模拟器 server,后续从设置读取
client.connect("ws://10.0.2.2:8787/ws", "dshwatch", "change-me");
client.connect(
SettingsStore.serverUrl(SessionListActivity.this),
SettingsStore.userId(SessionListActivity.this),
SettingsStore.token(SessionListActivity.this));
}
@Override
@@ -27,8 +27,12 @@ public class SettingsActivity extends AppCompatActivity {
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 空壳:后续持久化到 SharedPreferences / 安全存储
Toast.makeText(SettingsActivity.this, "保存(空壳): " + serverUrl.getText(), Toast.LENGTH_SHORT).show();
SettingsStore.save(
SettingsActivity.this,
serverUrl.getText().toString().trim(),
userId.getText().toString().trim(),
token.getText().toString().trim());
Toast.makeText(SettingsActivity.this, "已保存", Toast.LENGTH_SHORT).show();
}
});
}
@@ -0,0 +1,32 @@
package com.dshwatch.app;
import android.content.Context;
import android.content.SharedPreferences;
public class SettingsStore {
private static final String PREFS = "dshwatch";
private static final String KEY_SERVER = "serverUrl";
private static final String KEY_USER = "userId";
private static final String KEY_TOKEN = "token";
public static void save(Context c, String serverUrl, String userId, String token) {
SharedPreferences p = c.getSharedPreferences(PREFS, Context.MODE_PRIVATE);
p.edit()
.putString(KEY_SERVER, serverUrl == null ? "" : serverUrl)
.putString(KEY_USER, userId == null ? "" : userId)
.putString(KEY_TOKEN, token == null ? "" : token)
.apply();
}
public static String serverUrl(Context c) {
return c.getSharedPreferences(PREFS, Context.MODE_PRIVATE).getString(KEY_SERVER, "ws://10.0.2.2:8787/ws");
}
public static String userId(Context c) {
return c.getSharedPreferences(PREFS, Context.MODE_PRIVATE).getString(KEY_USER, "dshwatch");
}
public static String token(Context c) {
return c.getSharedPreferences(PREFS, Context.MODE_PRIVATE).getString(KEY_TOKEN, "change-me");
}
}