mirror of
https://gitea.winboll.cc/ZhanGSKen/MyWinBoLL.git
synced 2026-08-14 05:27:10 +08:00
feat: 为绿色通道开关添加持久化存储
- SettingsBean新增isEnableGreenChannel属性及JSON序列化支持 - LimitedTimeGreenChannelView新增loadSavedGreenChannelState()加载持久状态 - LimitedTimeGreenChannelView新增saveGreenChannelState()保存开关状态 - App重启后绿色通道开关状态自动恢复
This commit is contained in:
@@ -28,6 +28,7 @@ public class SettingsBean extends BaseBean {
|
||||
private static final String JSON_KEY_DUN_RESUME_COUNT = "dunResumeCount";
|
||||
private static final String JSON_KEY_DUN_ENABLE = "isEnableDun";
|
||||
private static final String JSON_KEY_URL = "szBoBullToon_URL";
|
||||
private static final String JSON_KEY_GREEN_CHANNEL_ENABLE = "isEnableGreenChannel";
|
||||
|
||||
// ====================== 成员变量区 ======================
|
||||
// 云盾防御层数量
|
||||
@@ -40,6 +41,8 @@ public class SettingsBean extends BaseBean {
|
||||
private int dunResumeCount;
|
||||
// 是否启用云盾
|
||||
private boolean isEnableDun;
|
||||
// 是否启用绿色通道
|
||||
private boolean isEnableGreenChannel;
|
||||
// BoBullToon 应用模块数据请求地址
|
||||
private String szBoBullToon_URL;
|
||||
|
||||
@@ -53,6 +56,7 @@ public class SettingsBean extends BaseBean {
|
||||
this.dunResumeSecondCount = 60;
|
||||
this.dunResumeCount = 1;
|
||||
this.isEnableDun = false;
|
||||
this.isEnableGreenChannel = false;
|
||||
this.szBoBullToon_URL = "";
|
||||
LogUtils.d(TAG, "SettingsModel: 默认构造初始化完成 | 云盾默认配置加载完毕");
|
||||
}
|
||||
@@ -61,17 +65,20 @@ public class SettingsBean extends BaseBean {
|
||||
* 带参构造,初始化自定义配置并校验数值范围
|
||||
*/
|
||||
public SettingsBean(int dunTotalCount, int dunCurrentCount, int dunResumeSecondCount,
|
||||
int dunResumeCount, boolean isEnableDun, String szBoBullToon_URL) {
|
||||
int dunResumeCount, boolean isEnableDun, boolean isEnableGreenChannel,
|
||||
String szBoBullToon_URL) {
|
||||
this.dunTotalCount = getSettingsModelRangeInt(dunTotalCount);
|
||||
this.dunCurrentCount = getSettingsModelRangeInt(dunCurrentCount);
|
||||
this.dunResumeSecondCount = getSettingsModelRangeInt(dunResumeSecondCount);
|
||||
this.dunResumeCount = getSettingsModelRangeInt(dunResumeCount);
|
||||
this.isEnableDun = isEnableDun;
|
||||
this.isEnableGreenChannel = isEnableGreenChannel;
|
||||
this.szBoBullToon_URL = szBoBullToon_URL == null ? "" : szBoBullToon_URL;
|
||||
|
||||
LogUtils.d(TAG, "SettingsModel: 带参构造初始化完成 | 总层数=" + this.dunTotalCount
|
||||
+ " | 当前层数=" + this.dunCurrentCount + " | 恢复间隔=" + this.dunResumeSecondCount
|
||||
+ " | 恢复层数=" + this.dunResumeCount + " | 云盾启用=" + this.isEnableDun);
|
||||
+ " | 恢复层数=" + this.dunResumeCount + " | 云盾启用=" + this.isEnableDun
|
||||
+ " | 绿色通道启用=" + this.isEnableGreenChannel);
|
||||
}
|
||||
|
||||
// ====================== 私有工具方法区 ======================
|
||||
@@ -136,6 +143,15 @@ public class SettingsBean extends BaseBean {
|
||||
this.isEnableDun = isEnableDun;
|
||||
}
|
||||
|
||||
public boolean isEnableGreenChannel() {
|
||||
return isEnableGreenChannel;
|
||||
}
|
||||
|
||||
public void setIsEnableGreenChannel(boolean isEnableGreenChannel) {
|
||||
LogUtils.d(TAG, "setIsEnableGreenChannel: 绿色通道启用状态更新为" + isEnableGreenChannel);
|
||||
this.isEnableGreenChannel = isEnableGreenChannel;
|
||||
}
|
||||
|
||||
public String getBoBullToon_URL() {
|
||||
return szBoBullToon_URL;
|
||||
}
|
||||
@@ -164,6 +180,7 @@ public class SettingsBean extends BaseBean {
|
||||
jsonWriter.name(JSON_KEY_DUN_RESUME_SECOND).value(getDunResumeSecondCount());
|
||||
jsonWriter.name(JSON_KEY_DUN_RESUME_COUNT).value(getDunResumeCount());
|
||||
jsonWriter.name(JSON_KEY_DUN_ENABLE).value(isEnableDun());
|
||||
jsonWriter.name(JSON_KEY_GREEN_CHANNEL_ENABLE).value(isEnableGreenChannel());
|
||||
jsonWriter.name(JSON_KEY_URL).value(getBoBullToon_URL());
|
||||
LogUtils.d(TAG, "writeThisToJsonWriter: JSON序列化完成");
|
||||
}
|
||||
@@ -187,6 +204,8 @@ public class SettingsBean extends BaseBean {
|
||||
setDunResumeCount(getSettingsModelRangeInt(jsonReader.nextInt()));
|
||||
} else if (JSON_KEY_DUN_ENABLE.equals(name)) {
|
||||
setIsEnableDun(jsonReader.nextBoolean());
|
||||
} else if (JSON_KEY_GREEN_CHANNEL_ENABLE.equals(name)) {
|
||||
setIsEnableGreenChannel(jsonReader.nextBoolean());
|
||||
} else if (JSON_KEY_URL.equals(name)) {
|
||||
setBoBullToon_URL(jsonReader.nextString());
|
||||
} else {
|
||||
|
||||
+63
-1
@@ -13,6 +13,8 @@ import android.widget.LinearLayout;
|
||||
import android.widget.Switch;
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
||||
import cc.winboll.studio.contacts.R;
|
||||
import cc.winboll.studio.contacts.dun.Rules;
|
||||
import cc.winboll.studio.contacts.model.SettingsBean;
|
||||
import cc.winboll.studio.contacts.services.LimitedTimeGreenChannelService;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.libappbase.ToastUtils;
|
||||
@@ -78,12 +80,15 @@ public class LimitedTimeGreenChannelView extends LinearLayout {
|
||||
bindViews();
|
||||
// 初始化所有按钮点击事件
|
||||
initButtons();
|
||||
// 初始化开关状态
|
||||
// 初始化开关状态(从持久化存储加载)
|
||||
initSwitchState();
|
||||
// 注册广播监听
|
||||
registerServiceDestroyedListener();
|
||||
registerCountdownTickListener();
|
||||
|
||||
// 加载保存的绿色通道启用状态并设置Switch
|
||||
loadSavedGreenChannelState();
|
||||
|
||||
LogUtils.i(TAG, "视图初始化流程结束");
|
||||
}
|
||||
|
||||
@@ -191,6 +196,9 @@ public class LimitedTimeGreenChannelView extends LinearLayout {
|
||||
// 根据开关状态更新UI可操作状态
|
||||
updateUIState(!isChecked);
|
||||
|
||||
// 保存绿色通道启用状态到持久化存储
|
||||
saveGreenChannelState(isChecked);
|
||||
|
||||
if (isChecked) {
|
||||
startLimitedTimeGreenChannelService();
|
||||
} else {
|
||||
@@ -200,6 +208,60 @@ public class LimitedTimeGreenChannelView extends LinearLayout {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 从持久化存储加载绿色通道启用状态并设置Switch
|
||||
*/
|
||||
private void loadSavedGreenChannelState() {
|
||||
Rules rules = Rules.getInstance(mContext);
|
||||
if (rules == null) {
|
||||
LogUtils.e(TAG, "loadSavedGreenChannelState: Rules实例获取失败");
|
||||
return;
|
||||
}
|
||||
SettingsBean settings = rules.getSettingsModel();
|
||||
if (settings == null) {
|
||||
LogUtils.e(TAG, "loadSavedGreenChannelState: SettingsBean获取失败");
|
||||
return;
|
||||
}
|
||||
|
||||
boolean savedState = settings.isEnableGreenChannel();
|
||||
LogUtils.i(TAG, "loadSavedGreenChannelState: 加载保存的绿色通道状态=" + savedState);
|
||||
|
||||
// 设置Switch状态(不会触发监听器,因为监听器在initSwitchState中已设置)
|
||||
if (mSwEnable != null && mSwEnable.isChecked() != savedState) {
|
||||
mSwEnable.setChecked(savedState);
|
||||
// 手动更新UI状态
|
||||
updateUIState(!savedState);
|
||||
LogUtils.i(TAG, "loadSavedGreenChannelState: Switch状态已恢复为" + (savedState ? "开启" : "关闭"));
|
||||
}
|
||||
|
||||
// 如果保存的状态是开启,启动服务
|
||||
if (savedState && !LimitedTimeGreenChannelService.isServiceRunning()) {
|
||||
LogUtils.i(TAG, "loadSavedGreenChannelState: 启动绿色通道服务");
|
||||
startLimitedTimeGreenChannelService();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存绿色通道启用状态到持久化存储
|
||||
* @param isEnabled 是否启用
|
||||
*/
|
||||
private void saveGreenChannelState(boolean isEnabled) {
|
||||
Rules rules = Rules.getInstance(mContext);
|
||||
if (rules == null) {
|
||||
LogUtils.e(TAG, "saveGreenChannelState: Rules实例获取失败,保存失败");
|
||||
return;
|
||||
}
|
||||
SettingsBean settings = rules.getSettingsModel();
|
||||
if (settings == null) {
|
||||
LogUtils.e(TAG, "saveGreenChannelState: SettingsBean获取失败,保存失败");
|
||||
return;
|
||||
}
|
||||
|
||||
settings.setIsEnableGreenChannel(isEnabled);
|
||||
rules.saveDun();
|
||||
LogUtils.i(TAG, "saveGreenChannelState: 绿色通道启用状态已保存为" + isEnabled);
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一更新UI可操作状态
|
||||
* @param enabled 可用状态
|
||||
|
||||
Reference in New Issue
Block a user