From f09ab4036fe309fa78d4756eb3bd472b03e8247f Mon Sep 17 00:00:00 2001 From: MiMo Date: Mon, 20 Jul 2026 13:46:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=BA=E7=BB=BF=E8=89=B2=E9=80=9A?= =?UTF-8?q?=E9=81=93=E5=BC=80=E5=85=B3=E6=B7=BB=E5=8A=A0=E6=8C=81=E4=B9=85?= =?UTF-8?q?=E5=8C=96=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - SettingsBean新增isEnableGreenChannel属性及JSON序列化支持 - LimitedTimeGreenChannelView新增loadSavedGreenChannelState()加载持久状态 - LimitedTimeGreenChannelView新增saveGreenChannelState()保存开关状态 - App重启后绿色通道开关状态自动恢复 --- .../studio/contacts/model/SettingsBean.java | 23 ++++++- .../views/LimitedTimeGreenChannelView.java | 64 ++++++++++++++++++- 2 files changed, 84 insertions(+), 3 deletions(-) diff --git a/contacts/src/main/java/cc/winboll/studio/contacts/model/SettingsBean.java b/contacts/src/main/java/cc/winboll/studio/contacts/model/SettingsBean.java index cf4254e..5fa278f 100644 --- a/contacts/src/main/java/cc/winboll/studio/contacts/model/SettingsBean.java +++ b/contacts/src/main/java/cc/winboll/studio/contacts/model/SettingsBean.java @@ -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 { diff --git a/contacts/src/main/java/cc/winboll/studio/contacts/views/LimitedTimeGreenChannelView.java b/contacts/src/main/java/cc/winboll/studio/contacts/views/LimitedTimeGreenChannelView.java index e2442f8..fd4e851 100644 --- a/contacts/src/main/java/cc/winboll/studio/contacts/views/LimitedTimeGreenChannelView.java +++ b/contacts/src/main/java/cc/winboll/studio/contacts/views/LimitedTimeGreenChannelView.java @@ -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 可用状态