Compare commits

..

256 Commits

Author SHA1 Message Date
ac206ba58d <powerbell>APK 15.14.49 release Publish. 2026-01-07 18:09:34 +08:00
1d36d69e52 更新基础类库版本 2026-01-07 18:08:01 +08:00
d03c105ccd <powerbell>APK 15.14.48 release Publish. 2026-01-06 19:10:13 +08:00
5d58b3b20d 更新基础类库 2026-01-06 19:08:26 +08:00
32864c72b1 <powerbell>APK 15.14.47 release Publish. 2026-01-06 15:13:36 +08:00
a490ab3ff5 调整应用设置窗口显示信息 2026-01-06 15:12:05 +08:00
a5c5cc91e1 更新编译配置 2026-01-06 15:09:09 +08:00
5a9d068fc0 Merge branch 'winboll' into powerbell 2026-01-06 14:56:29 +08:00
cce4a643e7 编译环境参数调整 2026-01-06 14:08:30 +08:00
8405097573 Merge branch 'winboll' into powerbell 2026-01-06 12:52:47 +08:00
3d69d4da09 <powerbell>APK 15.14.46 release Publish. 2026-01-05 19:22:25 +08:00
c98685440d 添加云宝物语小店广告。 2026-01-05 19:20:19 +08:00
7183338c97 更新广支持,添加云宝小店广告。 2026-01-05 19:10:02 +08:00
61cfa7f3ff 添加微信支付测试窗口 2026-01-04 17:05:12 +08:00
cb9ad1dc57 添加企业微信测试码 2026-01-03 11:16:35 +08:00
e18ed1b0fd Merge branch 'winboll' into powerbell 2026-01-03 10:20:49 +08:00
98c334f442 移除GitHub工作流配置文件 2026-01-03 10:19:43 +08:00
9d42a3e1e9 源码整理 2026-01-02 19:08:57 +08:00
411af44303 源码整理 2026-01-02 18:53:54 +08:00
9afb072351 源码整理 2026-01-02 18:34:05 +08:00
b0c91f3ee4 <powerbell>APK 15.14.45 release Publish. 2025-12-31 20:53:46 +08:00
b21d5ecdb5 Merge remote-tracking branch 'origin/winboll' into powerbell 2025-12-31 20:47:20 +08:00
47c328cd25 <winboll>APK 15.11.8 release Publish. 2025-12-31 20:28:06 +08:00
7134d4e1c8 源码整理 2025-12-31 20:25:15 +08:00
a98c9e4214 <powerbell>APK 15.14.44 release Publish. 2025-12-30 20:07:05 +08:00
cc7bbcf2a6 <powerbell>Start New Stage Version. 2025-12-30 20:06:28 +08:00
8da6162632 应用设置UI界面优化。 2025-12-30 20:05:28 +08:00
02c135fd8c <powerbell>APK 15.14.43 release Publish. 2025-12-30 18:46:42 +08:00
2f42334f19 修复TTS语音服务在应用重启时,会播放的问题,TTS语音服务设定在充电状态与放电状态切换时播放。 2025-12-30 18:45:10 +08:00
10348d2c8d <powerbell>APK 15.14.42 release Publish. 2025-12-30 18:16:29 +08:00
21cdc219c8 更新TTS语音服务朗读文本。 2025-12-30 18:14:27 +08:00
3ebf87c642 <powerbell>APK 15.14.41 release Publish. 2025-12-29 21:40:10 +08:00
347a4040cd <powerbell>Start New Stage Version. 2025-12-29 21:37:48 +08:00
6fd86a2742 添加TTS贴心服务,以免在充电时设置了服务提醒却不知道。 2025-12-29 21:36:34 +08:00
798357aedd 源码整理 2025-12-29 09:45:01 +08:00
9124303fd3 <powerbell>APK 15.14.40 release Publish. 2025-12-28 20:43:38 +08:00
414541093a <powerbell>Start New Stage Version. 2025-12-28 20:42:24 +08:00
13265be66e 添加点阵能量块风格。 2025-12-28 20:41:19 +08:00
ebd32adb68 <powerbell>APK 15.14.39 release Publish. 2025-12-28 13:54:20 +08:00
56a65cd10a 能量风格绘图方法切换回老式算法,修复能量条间隔缝隙问题。 2025-12-28 13:52:51 +08:00
e379684002 <powerbell>APK 15.14.38 release Publish. 2025-12-28 13:16:31 +08:00
4077ac18f6 <powerbell>Start New Stage Version. 2025-12-28 13:15:17 +08:00
278e690795 能量条绘图风格调试完成。 2025-12-28 13:14:24 +08:00
e81fc65b90 BatteryStyleView控件调试中。。。 2025-12-27 21:19:20 +08:00
abd956d7d0 添加BatteryStyleView控件,添加能量与斑马绘图风格。 2025-12-27 21:12:54 +08:00
fca17908b2 <powerbell>APK 15.14.37 release Publish. 2025-12-27 14:48:34 +08:00
39a3a5aeb0 更新黑白主题风格。 2025-12-27 14:45:13 +08:00
2e30f577b5 <powerbell>APK 15.14.36 release Publish. 2025-12-26 21:00:10 +08:00
dcb5355233 添加在切换主题时把主题基准色配置为背景图背景像素底色。 2025-12-26 20:58:31 +08:00
d1ced7ac63 <powerbell>APK 15.14.35 release Publish. 2025-12-26 20:13:09 +08:00
bf0cf23144 <powerbell>Start New Stage Version. 2025-12-26 20:11:34 +08:00
890b32ceae 修复像素拾取窗口菜单栏风格未统一问题。 2025-12-26 20:10:28 +08:00
c347d51c84 简化代码使用全局变量 2025-12-26 20:04:25 +08:00
7278e9f22f 源码整理 2025-12-26 19:47:06 +08:00
4e98c8d699 优化函数使用方式 2025-12-26 18:56:24 +08:00
3ec1bbe264 源码整理 2025-12-26 18:31:25 +08:00
f4a2a1585d 源码整理 2025-12-26 18:00:24 +08:00
35f4aa8730 源码整理 2025-12-26 17:42:30 +08:00
5b1d160dac <powerbell>APK 15.14.34 release Publish. 2025-12-26 02:58:19 +08:00
db87ba51e3 修复接收分享图片后的主界面更新问题。 2025-12-26 02:56:11 +08:00
fd6e852061 优化任务进程配置 2025-12-26 02:53:41 +08:00
8ff4b9e1f4 修复全局应用资源缓存不同步问题 2025-12-26 02:20:21 +08:00
78a2d85150 20251226_002506_076 正在调试设置窗口返回时的界面刷新。。。 2025-12-26 00:25:50 +08:00
fd2d1d17ed 注释调试码 2025-12-25 20:15:25 +08:00
37f3091103 <powerbell>APK 15.14.33 release Publish. 2025-12-25 20:10:33 +08:00
6dc5f05702 调整图片分享接收后的窗口切换逻辑。 2025-12-25 20:08:36 +08:00
77df2558b3 <powerbell>APK 15.14.32 release Publish. 2025-12-25 16:51:19 +08:00
315541f2d2 添加权限申请配置属性 2025-12-25 16:50:02 +08:00
f52bb54d22 <powerbell>APK 15.14.31 release Publish. 2025-12-25 16:31:07 +08:00
0742164f65 调整资源进程配置 2025-12-25 16:27:59 +08:00
fc785b9258 <powerbell>APK 15.14.30 release Publish. 2025-12-25 16:08:51 +08:00
0abeb982d2 增加自启成功概率 2025-12-25 16:04:23 +08:00
81f0e5c56e 视图缓存控件调试成功。 2025-12-25 16:00:17 +08:00
4fcce7edb3 20251225_153508_537 2025-12-25 15:35:12 +08:00
59f5eae136 <powerbell>APK 15.14.29 release Publish. 2025-12-24 21:23:33 +08:00
bf9479c53f 源码整理,最新像素背景位图缓存功能调试完成。 2025-12-24 21:21:38 +08:00
f4a485f1ff 线程提醒电量问题调试完成 2025-12-24 21:07:54 +08:00
80da7677f8 源码整理 2025-12-24 20:27:30 +08:00
e1c3c8f072 位图大小设置调试完成 2025-12-24 20:18:59 +08:00
2238d632f7 源码整理 2025-12-24 19:20:17 +08:00
558bc16013 位图平铺方式调试结束 2025-12-24 19:17:56 +08:00
7d50453e34 位图类处理模块重构 2025-12-24 17:28:55 +08:00
f771225830 <powerbell>APK 15.14.28 release Publish. 2025-12-24 12:22:06 +08:00
72794dc6e5 改进图片像素背景的绘图方法。 2025-12-24 12:19:48 +08:00
2ffdb26f69 图片加载逻辑优化,正在查询哪个类在使用背景像素。。。 2025-12-24 00:12:34 +08:00
3c6d269caf <powerbell>APK 15.14.27 release Publish. 2025-12-23 21:02:16 +08:00
2f7f6b62fd 设置应用初始背景颜色为白色,颜色清理按钮点击后,背景颜色改为主题风格的基准色。 2025-12-23 21:01:03 +08:00
db096e716c <powerbell>APK 15.14.26 release Publish. 2025-12-23 19:45:28 +08:00
1ce0a86b53 更新元素类库 2025-12-23 19:39:50 +08:00
1e04bd463f 改用jitpack.io库 2025-12-23 18:26:06 +08:00
7c0e723227 <powerbell>APK 15.14.25 release Publish. 2025-12-23 15:23:07 +08:00
accc276fd8 更新主题元素类库 2025-12-23 15:21:37 +08:00
16e581802f <powerbell>APK 15.14.24 release Publish. 2025-12-23 14:29:30 +08:00
3048963fa9 <powerbell>Start New Stage Version. 2025-12-23 14:28:13 +08:00
34082c49e9 源码整理 2025-12-23 14:16:58 +08:00
eff1822ee5 源码整理 2025-12-23 13:43:30 +08:00
4e84ff493b 源码整理 2025-12-23 13:12:17 +08:00
c2def0bb3b 源码整理 2025-12-22 23:19:50 +08:00
a086a47b2d 源码整理 2025-12-22 23:05:26 +08:00
c38b392e39 <powerbell>APK 15.14.23 release Publish. 2025-12-22 13:00:54 +08:00
0f736c2007 去掉位图压缩功能,尽量保持位图品质。 2025-12-22 12:59:50 +08:00
26b86cd715 <powerbell>APK 15.14.22 release Publish. 2025-12-22 11:21:24 +08:00
7888696e65 改进缓存策略。 2025-12-22 11:20:14 +08:00
8609c2f784 <powerbell>APK 15.14.21 release Publish. 2025-12-22 11:02:54 +08:00
863b743330 改进缓存策略,修复位图绘画时的异常。 2025-12-22 11:01:35 +08:00
61b7afa4b5 <powerbell>APK 15.14.20 release Publish. 2025-12-22 10:25:46 +08:00
8e4c7a6832 添加位图缓存 2025-12-22 10:24:29 +08:00
d29068b029 <powerbell>APK 15.14.19 release Publish. 2025-12-22 10:20:01 +08:00
51963f8e0f 优化启动界面显示 2025-12-22 10:18:28 +08:00
18a5762c15 <powerbell>APK 15.14.18 release Publish. 2025-12-22 10:04:49 +08:00
60de16ab45 <powerbell>Start New Stage Version. 2025-12-22 10:03:27 +08:00
6d60d71991 首页启动窗口改用视图控件缓存替代位图缓存。 2025-12-22 10:01:13 +08:00
0cfbc43acb MemoryCachedBackgroundView测试完成 2025-12-22 08:55:09 +08:00
20227e29ef 添加缓存视图控件类 2025-12-21 20:46:14 +08:00
2b7007f478 <powerbell>APK 15.14.17 release Publish. 2025-12-21 19:13:50 +08:00
212b8185c8 移除白色启动屏。 2025-12-21 19:12:31 +08:00
2ef09e020e <powerbell>APK 15.14.16 release Publish. 2025-12-21 17:57:41 +08:00
6de9b7379b 添加每分钟加载一次位图缓存的功能。预防系统优化内存时位图缓存被清理。 2025-12-21 17:50:40 +08:00
7fba2c8812 <powerbell>APK 15.14.15 release Publish. 2025-12-21 13:40:20 +08:00
20e3a5f974 修复系统分享图片的接收功能。 2025-12-21 13:38:46 +08:00
c8333e1e81 <powerbell>APK 15.14.14 release Publish. 2025-12-21 11:22:23 +08:00
6beb56efae <powerbell>Start New Stage Version. 2025-12-21 11:21:35 +08:00
eb51b2c8f4 网络下载按钮修复完成 2025-12-21 11:20:51 +08:00
36bdd059b0 修复空白图片背景按钮与图片背景按钮。 2025-12-21 09:49:40 +08:00
bf051dcc9f <powerbell>APK 15.14.13 release Publish. 2025-12-21 09:22:26 +08:00
b38a8df462 添加应用内存整理后重新缓存位图的功能。 2025-12-21 09:20:15 +08:00
a8dbe43d4b 源码整理 2025-12-21 02:05:38 +08:00
05a8dc5205 <powerbell>APK 15.14.12 release Publish. 2025-12-20 22:59:40 +08:00
280fc1abd6 <powerbell>Start New Stage Version. 2025-12-20 22:58:26 +08:00
fe2084f5ff 优化RemindThread线程,节约电量。 2025-12-20 22:57:42 +08:00
2a75aa140e 修改背景控件默认背景颜色。 2025-12-20 21:23:52 +08:00
04b597cbe7 <powerbell>APK 15.14.11 release Publish. 2025-12-20 19:28:00 +08:00
35d210c378 添加滚动条拉动时,实时预览数值的功能。 2025-12-20 19:26:32 +08:00
79ae6de6fc <powerbell>APK 15.14.10 release Publish. 2025-12-20 18:45:13 +08:00
328f559d2e 改进背景图片位图缓存方法。 2025-12-20 18:40:29 +08:00
28d340a772 电量提醒线程测试完成。 2025-12-20 17:40:06 +08:00
65acbfcd04 调整电量消息频率以及消息内容。 2025-12-20 16:30:34 +08:00
6af2096b30 修复广播消息发送方法 2025-12-20 15:55:40 +08:00
e539922478 广播消息分开为,电量消息与应用配置更新消息。 2025-12-20 15:44:12 +08:00
1d9a03554c 添加广播接收器的注册与释放。 2025-12-20 13:49:02 +08:00
23beabe99b 源码整理 2025-12-20 13:23:07 +08:00
9c1e9dc75b 函数简化重构。 2025-12-20 03:07:08 +08:00
76c1dee625 MainActivity本地消息发送整理中。。。 2025-12-19 21:05:42 +08:00
e967ce5511 修复服务启动逻辑 2025-12-19 20:42:40 +08:00
bea77409a5 重置通知架构为旧式方法,用线程启动法。过程顺便整理代码。 2025-12-19 20:31:43 +08:00
e584e824c0 恢复2a74fd2c304b571ab5ae349ffc3b7f06c5b4daf7提交点,旧版电量计算方法。 2025-12-19 20:11:10 +08:00
bc6a82af41 电量提醒消息,通路架构基本完成。 2025-12-19 18:48:47 +08:00
9bc71bb3f6 正在修复电量提醒线程。。。 2025-12-19 00:13:03 +08:00
b0dee5e98e 服务线程调试中,目标就是线程放入服务类使用handler与服务通信。 2025-12-18 21:15:24 +08:00
7d796b5c3f 更新基础类库,更新调试工具。 2025-12-18 17:52:47 +08:00
d43ba4bff2 剔除服务锁,正在调试RemindThread调用。。。 2025-12-18 15:20:32 +08:00
796d826331 源码整理。 2025-12-18 13:14:04 +08:00
b3f4571b57 移除提醒线程前台标志 2025-12-18 13:11:00 +08:00
5e6de91430 服务启停调试中。。。 2025-12-17 20:40:04 +08:00
d61d1da5d1 服务自启动修复中。。。 2025-12-17 16:50:38 +08:00
5cf47172f4 服务自启配置持久化 2025-12-17 16:24:49 +08:00
683dc6791e 修复应用初始安装时电量提醒无声音的问题。 2025-12-17 15:17:45 +08:00
1e9a6adc88 修复电量进度条拉动时无响应问题 2025-12-17 14:50:21 +08:00
dbcb5259d9 源码整理 2025-12-17 14:14:07 +08:00
740ab932a4 源码整理 2025-12-17 14:01:57 +08:00
29b9f3c82b 源码整理 2025-12-17 13:50:33 +08:00
47601ef542 编译参数修复 2025-12-17 12:04:08 +08:00
7b17fae798 豆包完美想象版,未调试。 2025-12-17 07:48:32 +08:00
405b914f02 20251217_043730_107 2025-12-17 04:37:34 +08:00
1c7ceebb78 添加软著申请文档生成脚本 2025-12-17 04:03:43 +08:00
493b7e433c <powerbell>APK 15.14.9 release Publish. 2025-12-16 21:18:25 +08:00
d2ddfedc96 调整当前电量初始化时的默认值。处理bean类序列化问题,未调试。 2025-12-16 21:16:34 +08:00
bba48a4458 <powerbell>APK 15.14.8 release Publish. 2025-12-16 20:17:03 +08:00
b7ae6ce190 编译参数修复 2025-12-16 20:15:58 +08:00
ba5470ebcb 更新像素清空按钮的重置颜色值。重置后为黑色,透明度OriginalAlpha值为FF。 2025-12-16 20:14:23 +08:00
78c7763212 优化颜色拾取流程 2025-12-16 20:06:12 +08:00
d051b1f737 <powerbell>APK 15.14.7 release Publish. 2025-12-16 17:52:54 +08:00
d6323bc1ed <powerbell>Start New Stage Version. 2025-12-16 17:50:40 +08:00
dffcc0f8a0 渐变像素对话框调试完成。 2025-12-16 17:48:59 +08:00
9426618b59 20251216_171108_907 2025-12-16 17:11:20 +08:00
68d98d4be3 调色板基本调试完成 2025-12-16 16:24:09 +08:00
4db458dda8 20251216_154557_434 2025-12-16 15:46:01 +08:00
83a8f5dada 初步完成调色板调用流程调试。 2025-12-16 14:12:41 +08:00
8e1d6ba197 添加调色板对话框,未调试。 2025-12-16 12:11:32 +08:00
778a1bc98e 添加应用版本号说明 2025-12-15 12:42:50 +08:00
70a004d9e3 <powerbell>APK 15.14.6 release Publish. 2025-12-14 19:58:14 +08:00
c7f8aea1ce <powerbell>Start New Stage Version. 2025-12-14 19:57:39 +08:00
6d4381d78a 修复固定剪裁时的宽高比例不准确的BUG。 2025-12-14 19:56:36 +08:00
ddcd9a450e 20251214_191909_625 2025-12-14 19:19:13 +08:00
ca2323f534 <powerbell>APK 15.14.5 release Publish. 2025-12-14 18:30:51 +08:00
851800e39a 背景图片第二次无法更换的Bug修复。 2025-12-14 18:29:44 +08:00
f17624048c <powerbell>APK 15.14.4 release Publish. 2025-12-14 18:12:02 +08:00
724fce895f 减去多余应用图标。 2025-12-14 18:10:40 +08:00
5ece532dd4 <powerbell>APK 15.14.3 release Publish. 2025-12-14 17:48:59 +08:00
8b20bc84c8 更新测试数据 2025-12-14 17:47:02 +08:00
634c71dfd4 剪裁图片透明度问题解决 2025-12-14 17:42:12 +08:00
947df2e9b4 20251214_165544_910 2025-12-14 16:55:59 +08:00
08a33365b3 <powerbell>APK 15.14.2 release Publish. 2025-12-14 04:55:45 +08:00
7cffe5c0a5 <powerbell>Start New Stage Version. 2025-12-14 04:54:24 +08:00
5a0c429131 背景像素切换流程测试完成 2025-12-14 04:53:17 +08:00
cff26b3d11 背景像素拾取功能测试完成 2025-12-14 04:46:17 +08:00
e59034e48d 添加权限申请提示框 2025-12-14 04:18:29 +08:00
3d3301064c 必要权限申请设置完成。 2025-12-14 04:08:10 +08:00
2d12397f5e 减少初始化应用相册目录时的多余目录。 2025-12-14 02:28:53 +08:00
f09bb17cbc 修改所有通知栏,点击后都跳转到主窗口。 2025-12-14 02:10:13 +08:00
28d8a5679f <powerbell>APK 15.14.1 release Publish. 2025-12-13 21:16:25 +08:00
b4d9bdf3b3 <powerbell>APK 15.14.0 release Publish. 2025-12-13 21:15:48 +08:00
111cf01f9a <powerbell>Start New Stage Version. 2025-12-13 21:14:54 +08:00
e51d46186a 数据模型有调整,设定次级版本号。同次级版本应用可以自由切换。 2025-12-13 21:11:03 +08:00
8fc6855066 通知类调试完成 2025-12-13 21:04:35 +08:00
4ceaf1e46a 通知类重构 2025-12-13 20:47:00 +08:00
e669bbb04b 命名空间重构 2025-12-13 20:23:48 +08:00
6bf3ebe2fd 20251212_002702_716 2025-12-12 00:27:07 +08:00
a44f7fe6d4 <powerbell>APK 15.12.15 release Publish. 2025-12-11 20:54:28 +08:00
35a34b5b53 <powerbell>Start New Stage Version. 2025-12-11 20:51:08 +08:00
d35d0d0291 图片选择到剪裁选定流程调试完成。 2025-12-11 20:49:58 +08:00
03212c0554 一次不必要的数据清空操作 2025-12-11 19:58:39 +08:00
0c963213df 调试信息准备 2025-12-11 19:40:30 +08:00
10ddca4f73 调试信息准备 2025-12-11 19:34:47 +08:00
f240d9c057 视图控件与全局位图缓存类优先调整。 2025-12-11 19:18:02 +08:00
2c77bf775b 背景图片控件测试通过,现在的问题就剩下,应用全局位图缓存问题与图片路径传递问题。 2025-12-11 18:48:42 +08:00
1db7c9bf80 20251211_130730_716 2025-12-11 13:07:35 +08:00
fd556fd06f 20251211_130438_907 2025-12-11 13:04:44 +08:00
220aa9dbfb 应用权限框架更新,重新调试剪裁文件流程。 2025-12-11 09:02:41 +08:00
ecafd2026f 相册权限申请模块改进中。。。 2025-12-11 06:53:22 +08:00
6ed9bc0d8e <powerbell>APK 15.12.14 release Publish. 2025-12-11 03:22:10 +08:00
bcb5db0a17 移除启动页cache缓存,以及移除savedInstanceState 窗体缓存。 2025-12-11 03:18:03 +08:00
6b69e04706 <powerbell>APK 15.12.13 release Publish. 2025-12-11 03:04:35 +08:00
a2a61bbf0b 添加BitmapCacheUtils应用全局位图缓存工具类。加速位图加载。 2025-12-11 03:02:46 +08:00
9f4211c83e <powerbell>APK 15.12.12 release Publish. 2025-12-10 20:37:42 +08:00
447a786632 图片尺寸调整函数优化 2025-12-10 20:31:10 +08:00
ff0f239ffc <powerbell>APK 15.12.11 release Publish. 2025-12-10 20:11:13 +08:00
7c59a982fc 使用豆包提供的savedInstanceState 窗体缓存技术,仅限于启动页窗体优化。 2025-12-10 20:09:49 +08:00
895cc4630d <powerbell>APK 15.12.10 release Publish. 2025-12-10 19:31:13 +08:00
851a539364 <powerbell>Start New Stage Version. 2025-12-10 19:30:41 +08:00
d79f2937ba 豆包优化,添加窗体缓存技术。代码优化首次窗体加载速度不多。代码理解难度增加。有可能会回退到上一个提交点。 2025-12-10 19:28:39 +08:00
c524a21429 <powerbell>APK 15.12.9 release Publish. 2025-12-10 18:38:00 +08:00
a148de2ab8 加快图片加载速度。 2025-12-10 18:36:49 +08:00
de34e823b6 <powerbell>APK 15.12.8 release Publish. 2025-12-10 18:32:41 +08:00
fd0833476e 简化冗余图片加载步骤。 2025-12-10 18:31:36 +08:00
361b533b0d <powerbell>APK 15.12.7 release Publish. 2025-12-10 18:12:34 +08:00
f277f76468 <powerbell>Start New Stage Version. 2025-12-10 18:12:02 +08:00
ec54865a8e 清理图片加载前的调试色调。优化加载速度。 2025-12-10 18:11:10 +08:00
6b3682994e <powerbell>APK 15.12.6 release Publish. 2025-12-10 17:52:12 +08:00
7145bff552 修复图片加载时出现的画面抖动问题。 2025-12-10 17:51:15 +08:00
4c3b60128f <powerbell>APK 15.12.5 release Publish. 2025-12-10 17:43:17 +08:00
29d30f3831 优化图片加载速度。 2025-12-10 17:42:09 +08:00
c7b0b1bc80 <powerbell>APK 15.12.4 release Publish. 2025-12-10 16:54:47 +08:00
9082b67bbd <powerbell>Start New Stage Version. 2025-12-10 16:53:55 +08:00
2b5447e65f 整合应用相框框架调整,加快启动页加载速度。 2025-12-10 16:52:47 +08:00
6a5c2dbbe8 <powerbell>APK 15.12.3 release Publish. 2025-12-10 16:20:34 +08:00
8cad25bb11 优化启动页加载速度。 2025-12-10 16:19:03 +08:00
e440943992 <powerbell>APK 15.12.2 release Publish. 2025-12-10 14:57:42 +08:00
7c5f8c3cc2 第一条图片选择与剪裁流程调试通过。 2025-12-10 14:52:43 +08:00
016b3b5e48 20251208_212749_544重新调整背景资源图片存储流程。 2025-12-08 21:28:49 +08:00
bb94f87597 更新说明书 2025-12-08 00:56:14 +08:00
489b72b582 <powerbell>APK 15.12.1 release Publish. 2025-12-07 15:19:14 +08:00
918df3dfbe 更新应用介绍中Git项目名称 2025-12-07 15:10:51 +08:00
8fd955af73 应用主题风格化 2025-12-07 15:07:31 +08:00
1db438e231 更新最新类库,编译测试。 2025-12-07 11:27:52 +08:00
8cceac1d03 添加https://gitea.winboll.cc/Studio/APPBase_Bck20251205_123340_337.git最新分支代码 2025-12-07 11:19:56 +08:00
183 changed files with 20591 additions and 7303 deletions

View File

@@ -1,87 +0,0 @@
name: Android CI
# 触发器
on:
push:
tags:
- *-beta
pull_request:
tags:
- *-beta
jobs:
build:
runs-on: ubuntu-latest
# 设置 JDK 环境
steps:
- uses: actions/checkout@v3
- name: set up JDK 11
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
cache: gradle
- name: Grant execute permission for gradlew
run: chmod +x gradlew
# 获取应用打包秘钥库
- name: Checkout Android Keystore
uses: actions/checkout@v3
with:
repository: zhangsken/keystore # 存储应用打包用的 keystore 的仓库(格式:用户名/仓库名)
token: ${{ secrets.APP_SECRET_TOKEN_1 }} # 连接仓库的 token , 需要单独配置
path: keystore # 仓库的根目录名
# 打包 Stage Release 版本应用
- name: Build with Gradle
run: bash ./gradlew assembleBetaRelease
# 创建release
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.APP_SECRET_TOKEN_1 }}
# GitHub 会自动创建 GITHUB_TOKEN 密码以在工作流程中使用。
# 您可以使用 GITHUB_TOKEN 在工作流程运行中进行身份验证。
# 当您启用 GitHub Actions 时GitHub 在您的仓库中安装 GitHub 应用程序。
# GITHUB_TOKEN 密码是一种 GitHub 应用程序 安装访问令牌。
# 您可以使用安装访问令牌代表仓库中安装的 GitHub 应用程序 进行身份验证。
# 令牌的权限仅限于包含您的工作流程的仓库。 更多信息请参阅“GITHUB_TOKEN 的权限”。
# 在每个作业开始之前, GitHub 将为作业提取安装访问令牌。 令牌在作业完成后过期。
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
draft: false
prerelease: false
# 获取 APK 版本号
- name: Get Version Name
uses: actions/github-script@v3
id: get-version
with:
script: |
const str=process.env.GITHUB_REF;
return str.substring(str.indexOf("v"));
result-encoding: string
# 上传至 Release 的资源
- name: Upload Release Asset
id: upload-release-asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.APP_SECRET_TOKEN_1 }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }} # 上传网址,无需改动
#asset_path: app/build/outputs/apk/release/app-release.apk # 上传路径(Release)
asset_path: app/build/outputs/apk/beta/release/app-beta-release.apk # 上传路径(WinBoll Stage Release)
asset_name: WinBoll-${{steps.get-version.outputs.result}}0.apk # 资源名
asset_content_type: application/vnd.android.package-archiv # 资源类型
# 存档打包的文件
- name: Archive production artifacts
uses: actions/upload-artifact@v2
with:
name: build
path: app/build/outputs # 将打包之后的文件全部上传(里面会有混淆的 map 文件)

View File

@@ -1,166 +1,223 @@
#!/usr/bin/bash
# ==============================================================================
# WinBoLL 应用发布脚本
# 功能检查Git源码状态 → 编译Stage Release包 → 添加WinBoLL标签 → 提交并推送源码
# 依赖build.properties、app_update_description.txt项目根目录下
# 使用:./script_name.sh <APP_NAME>
# 作者:豆包&ZhanGSKen<zhangsken@qq.com>
# ==============================================================================
# 检查是否指定了将要发布的应用名称
# 使用 `-z` 命令检查变量是否为空
# ==================== 常量定义 ====================
# 脚本退出码
EXIT_CODE_SUCCESS=0
EXIT_CODE_ERR_NO_APP_NAME=2
EXIT_CODE_ERR_WORK_DIR=1
EXIT_CODE_ERR_GIT_CHECK=1
EXIT_CODE_ERR_ADD_WINBOLL_TAG=1
# Gradle 任务(正式发布)
GRADLE_TASK_PUBLISH="assembleStageRelease"
# Gradle 任务(调试用,注释备用)
# GRADLE_TASK_DEBUG="assembleBetaDebug"
# ==================== 函数定义 ====================
# 检查Git源码是否已完全提交无未提交变更
# 返回值0=已完全提交1=存在未提交变更
function checkGitSources() {
# 配置Git安全目录解决权限问题
git config --global --add safe.directory "$(pwd)"
# 检查是否有未提交的变更
if [[ -n $(git diff --stat) ]]; then
echo "[ERROR] Git源码存在未提交变更请先提交所有修改"
return 1
fi
echo "[INFO] Git源码检查通过所有变更已提交。"
return 0
}
# 询问是否添加GitHub Workflows标签当前逻辑注释保留扩展能力
# 返回值1=用户选择是0=用户选择否
function askAddWorkflowsTag() {
read -p "是否添加GitHub Workflows标签(Y/n) " answer
if [[ $answer =~ ^[Yy]$ ]]; then
return 1
else
return 0
fi
}
# 添加WinBoLL正式标签
# 参数:$1=应用名称(项目根目录名)
# 返回值0=标签添加成功1=标签已存在/添加失败
function addWinBoLLTag() {
local app_name=$1
local build_prop_path="${app_name}/build.properties"
# 从build.properties中提取publishVersion
local publish_version=$(grep -o "publishVersion=.*" "${build_prop_path}" | awk -F '=' '{print $2}')
if [[ -z ${publish_version} ]]; then
echo "[ERROR] 未从${build_prop_path}中提取到publishVersion配置"
return 1
fi
echo "[INFO] 从${build_prop_path}读取到publishVersion${publish_version}"
# 构造WinBoLL标签格式<APP_NAME>-v<publishVersion>
local tag="${app_name}-v${publish_version}"
echo "[INFO] 准备添加WinBoLL标签${tag}"
# 检查标签是否已存在
if [[ "$(git tag -l ${tag})" == "${tag}" ]]; then
echo "[ERROR] WinBoLL标签${tag}已存在!"
return 1
fi
# 添加带注释的标签注释来自app_update_description.txt
git tag -a "${tag}" -F "${app_name}/app_update_description.txt"
echo "[INFO] WinBoLL标签${tag}添加成功!"
return 0
}
# 添加GitHub Workflows Beta标签当前逻辑注释保留扩展能力
# 参数:$1=应用名称(项目根目录名)
# 返回值0=标签添加成功1=标签已存在/添加失败
function addWorkflowsTag() {
local app_name=$1
local build_prop_path="${app_name}/build.properties"
# 从build.properties中提取baseBetaVersion
local base_beta_version=$(grep -o "baseBetaVersion=.*" "${build_prop_path}" | awk -F '=' '{print $2}')
if [[ -z ${base_beta_version} ]]; then
echo "[ERROR] 未从${build_prop_path}中提取到baseBetaVersion配置"
return 1
fi
echo "[INFO] 从${build_prop_path}读取到baseBetaVersion${base_beta_version}"
# 构造Workflows标签格式<APP_NAME>-v<baseBetaVersion>-beta
local tag="${app_name}-v${base_beta_version}-beta"
echo "[INFO] 准备添加Workflows标签${tag}"
# 检查标签是否已存在
if [[ "$(git tag -l ${tag})" == "${tag}" ]]; then
echo "[ERROR] Workflows标签${tag}已存在!"
return 1
fi
# 添加带注释的标签注释来自app_update_description.txt
git tag -a "${tag}" -F "${app_name}/app_update_description.txt"
echo "[INFO] Workflows标签${tag}添加成功!"
return 0
}
# ==================== 主流程开始 ====================
echo "============================================="
echo " WinBoLL 应用发布脚本"
echo "============================================="
# 1. 检查应用名称参数是否指定
if [ -z "$1" ]; then
echo "No APP name specified : $0"
exit 2
echo "[ERROR] 未指定应用名称!使用方式:${0} <APP_NAME>"
exit ${EXIT_CODE_ERR_NO_APP_NAME}
fi
APP_NAME=$1
echo "[INFO] 待发布应用名称:${APP_NAME}"
## 定义相关函数
## 检查 Git 源码是否完全提交了完全提交就返回0
function checkGitSources {
#local input="$1"
#echo "The string is: $input"
git config --global --add safe.directory `pwd`
if [[ -n $(git diff --stat) ]]
then
local result="Source is no commit completely."
echo $result
# 脚本调试时使用
#return 0
# 正式检查源码时使用
return 1
fi
local result="Git Source Check OK."
echo $result
return 0
}
function askAddWorkflowsTag {
read answer
if [[ $answer =~ ^[Yy]$ ]]; then
#echo "You chose yes."
return 1
else
#echo "You chose no."
return 0
fi
}
function addWinBoLLTag {
# 就读取脚本 .winboll/winboll_app_build.gradle 生成的 publishVersion。
# 如果文件中有 publishVersion 这一项,
# 使用grep找到包含"publishVersion="的那一行然后用awk提取其后的值
PUBLISH_VERSION=$(grep -o "publishVersion=.*" $1/build.properties | awk -F '=' '{print $2}')
echo "< $1/build.properties publishVersion : ${PUBLISH_VERSION} >"
## 设新的 WinBoLL 标签
# 脚本调试时使用
#tag="projectname-v7.6.4-test1"
# 正式设置标签时使用
tag=$1"-v"${PUBLISH_VERSION}
echo "< WinBoLL Tag To: $tag >";
# 检查是否已经添加了 WinBoLL Tag
if [ "$(git tag -l ${tag})" == "${tag}" ]; then
echo -e "< WinBoLL Tag ${tag} exist! >"
return 1 # WinBoLL标签重复
fi
# 添加WinBoLL标签
git tag -a ${tag} -F $1/app_update_description.txt
return 0
}
function addWorkflowsTag {
# 就读取脚本 .winboll/winboll_app_build.gradle 生成的 baseBetaVersion。
# 如果文件中有 baseBetaVersion 这一项,
# 使用grep找到包含"baseBetaVersion="的那一行然后用awk提取其后的值
BASE_BETA_VERSION=$(grep -o "baseBetaVersion=.*" $1/build.properties | awk -F '=' '{print $2}')
echo "< $1/build.properties baseBetaVersion : ${BASE_BETA_VERSION} >"
## 设新的 workflows 标签
# 脚本调试时使用
#tag="projectname-v7.6.4-beta"
# 正式设置标签时使用
tag=$1"-v"${BASE_BETA_VERSION}-beta
echo "< Workflows Tag To: $tag >";
# 检查是否已经添加了工作流 Tag
if [ "$(git tag -l ${tag})" == "${tag}" ]; then
echo -e "< Github Workflows Tag ${tag} exist! >"
return 1 # 工作流标签重复
fi
# 添加工作流标签
git tag -a ${tag} -F $1/app_update_description.txt
return 0
}
## 开始执行脚本
echo -e "Current dir : \n"`pwd`
# 检查当前目录是否是项目根目录
if [[ -e $1/build.properties ]]; then
echo "The $1/build.properties file exists."
echo -e "Work dir correctly."
else
echo "The $1/build.properties file does not exist."
echo "尝试进入根目录"
# 进入项目根目录
# 2. 检查并切换到项目根目录确保build.properties存在
echo "[INFO] 当前工作目录:$(pwd)"
if [[ ! -e "${APP_NAME}/build.properties" ]]; then
echo "[WARNING] 当前目录不存在${APP_NAME}/build.properties尝试切换到上级目录..."
cd ..
echo "[INFO] 切换后工作目录:$(pwd)"
fi
## 本脚本需要在项目根目录下执行
echo -e "Current dir : \n"`pwd`
# 检查当前目录是否是项目根目录
if [[ -e $1/build.properties ]]; then
echo "The $1/build.properties file exists."
echo -e "Work dir correctly."
# 验证最终工作目录是否正确
if [[ ! -e "${APP_NAME}/build.properties" ]]; then
echo "[ERROR] 工作目录错误!${APP_NAME}/build.properties 文件不存在。"
exit ${EXIT_CODE_ERR_WORK_DIR}
fi
echo "[INFO] 工作目录验证通过:${APP_NAME}/build.properties 存在。"
# 3. 检查Git源码状态
echo "---------------------------------------------"
echo " 步骤1检查Git源码状态"
echo "---------------------------------------------"
checkGitSources
if [[ $? -ne ${EXIT_CODE_SUCCESS} ]]; then
echo "[ERROR] Git源码检查失败脚本终止"
exit ${EXIT_CODE_ERR_GIT_CHECK}
fi
# 4. 编译Stage Release版本APK
echo "---------------------------------------------"
echo " 步骤2编译Stage Release APK"
echo "---------------------------------------------"
echo "[INFO] 开始执行Gradle任务${GRADLE_TASK_PUBLISH}"
# 调试用(注释正式任务,启用调试任务)
# bash gradlew :${APP_NAME}:${GRADLE_TASK_DEBUG}
bash gradlew :${APP_NAME}:${GRADLE_TASK_PUBLISH}
if [[ $? -ne ${EXIT_CODE_SUCCESS} ]]; then
echo "[ERROR] Gradle编译任务失败"
exit 1
fi
echo "[INFO] Stage Release APK编译成功"
# 5. 添加WinBoLL正式标签
echo "---------------------------------------------"
echo " 步骤3添加WinBoLL标签"
echo "---------------------------------------------"
addWinBoLLTag ${APP_NAME}
if [[ $? -ne ${EXIT_CODE_SUCCESS} ]]; then
echo "[ERROR] WinBoLL标签添加失败脚本终止"
exit ${EXIT_CODE_ERR_ADD_WINBOLL_TAG}
fi
# 6. 可选添加GitHub Workflows标签当前逻辑注释保留扩展能力
# echo "---------------------------------------------"
# echo " 步骤4添加Workflows标签可选"
# echo "---------------------------------------------"
# echo "是否添加GitHub Workflows Beta标签(Y/n) "
# askAddWorkflowsTag
# nAskAddWorkflowsTag=$?
# if [[ ${nAskAddWorkflowsTag} -eq 1 ]]; then
# addWorkflowsTag ${APP_NAME}
# if [[ $? -ne ${EXIT_CODE_SUCCESS} ]]; then
# echo "[ERROR] Workflows标签添加失败脚本终止"
# exit 1
# fi
# fi
# 7. 清理更新描述文件
echo "---------------------------------------------"
echo " 步骤5清理更新描述文件"
echo "---------------------------------------------"
echo "" > "${APP_NAME}/app_update_description.txt"
echo "[INFO] 已清空${APP_NAME}/app_update_description.txt"
# 8. 提交并推送源码与标签
echo "---------------------------------------------"
echo " 步骤6提交并推送源码"
echo "---------------------------------------------"
git add .
git commit -m "<${APP_NAME}> 开始新的Stage版本开发。"
echo "[INFO] 源码提交成功,开始推送..."
# 推送源码到远程仓库
git push origin
# 推送标签到远程仓库
git push origin --tags
if [[ $? -eq ${EXIT_CODE_SUCCESS} ]]; then
echo "[INFO] 源码与标签推送成功!"
else
echo "The $1/build.properties file does not exist."
echo -e "Work dir error."
echo "[ERROR] 源码与标签推送失败!"
exit 1
fi
# 检查源码状态
result=$(checkGitSources)
if [[ $? -eq 0 ]]; then
echo $result
# 如果Git已经提交了所有代码就执行标签和应用发布操作
# ==================== 主流程结束 ====================
echo "============================================="
echo " WinBoLL 应用发布完成!"
echo "============================================="
exit ${EXIT_CODE_SUCCESS}
# 预先询问是否添加工作流标签
#echo "Add Github Workflows Tag? (yes/No)"
#result=$(askAddWorkflowsTag)
#nAskAddWorkflowsTag=$?
#echo $result
# 发布应用
echo "Publishing WinBoLL APK ..."
# 脚本调试时使用
#bash gradlew :$1:assembleBetaDebug
# 正式发布
bash gradlew :$1:assembleStageRelease
echo "Publishing WinBoLL APK OK."
# 添加 WinBoLL 标签
result=$(addWinBoLLTag $1)
echo $result
if [[ $? -eq 0 ]]; then
echo $result
# WinBoLL 标签添加成功
else
echo -e "${0}: addWinBoLLTag $1\n${result}\nAdd WinBoLL tag cancel."
exit 1 # addWinBoLLTag 异常
fi
# 添加 GitHub 工作流标签
#if [[ $nAskAddWorkflowsTag -eq 1 ]]; then
# 如果用户选择添加工作流标签
#result=$(addWorkflowsTag $1)
#if [[ $? -eq 0 ]]; then
# echo $result
# 工作流标签添加成功
#else
#echo -e "${0}: addWorkflowsTag $1\n${result}\nAdd workflows tag cancel."
#exit 1 # addWorkflowsTag 异常
#fi
#fi
## 清理更新描述文件内容
echo "" > $1/app_update_description.txt
# 设置新版本开发参数配置
# 提交配置
git add .
git commit -m "<$1>Start New Stage Version."
echo "Push sources to git repositories ..."
# 推送源码到所有仓库
git push origin && git push origin --tags
else
echo -e "${0}: checkGitSources\n${result}\nShell cancel."
exit 1 # checkGitSources 异常
fi

View File

@@ -64,6 +64,11 @@ android {
dimension "WinBoLLApp"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
// 应用包输出配置
//

View File

@@ -5,10 +5,11 @@
## ☁ ☁ ☁ WinBoLL APP ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁
# ☁ ☁ WinBoLL Studio Android 应用开源项目。☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁
# ☁ ☁ ☁ WinBoLL 网站地址 https://www.winboll.cc/ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁
# ☁ ☁ ☁ WinBoLL 源码地址 <https://gitea.winboll.cc/Studio/APPBase> ☁ ☁ ☁ ☁ ☁ ☁ ☁
# ☁ ☁ ☁ GitHub 源码地址 <https://github.com/ZhanGSKen/APPBase.git> ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁
# ☁ ☁ ☁ 码云 源码地址 <https://gitee.com/zhangsken/appbase.git> ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁
# ☁ ☁ ☁ WinBoLL 源码地址 <https://gitea.winboll.cc/Studio/WinBoLL.git> ☁ ☁ ☁ ☁ ☁ ☁ ☁
# ☁ ☁ ☁ GitHub 源码地址 <https://github.com/ZhanGSKen/WinBoLL.git> ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁
# ☁ ☁ ☁ 码云 源码地址 <https://gitee.com/zhangsken/winboll.git> ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁
# ☁ ☁ ☁ 在 jitpack.io 托管的 APPBase 类库源码<https://github.com/ZhanGSKen/APPBase.git> ☁ ☁ ☁ ☁
# ☁ ☁ ☁ 在 jitpack.io 托管的 AES 类库源码<https://github.com/ZhanGSKen/AES.git> ☁ ☁ ☁ ☁
## WinBoLL 提问
同样是 /sdcard 目录,在开发 Android 应用时,
能否实现手机编译与电脑编译的源码同步。
@@ -154,3 +155,11 @@ $ bash gradlew assembleBetaDebug
$ bash gradlew assembleStageDebug
### 若是 winboll.properties 文件的 [ExtraAPKOutputPath] 属性设置了路径。编译器也会复制一份 APK 到这个路径。
# 应用版本号命名方式
## statge 渠道
V<应用开发环境编号><应用功能变更号><应用调试阶段号>
APPBase_15.7.0
## beta 渠道
V<应用开发环境编号><应用功能变更号><应用调试阶段号>-beta<调试编译计数>_<调试编译时间(分钟+秒钟)>
APPBase_15.9.6-beta8_5413

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >
<application>
<!-- Put flavor specific code here -->
</application>
</manifest>

View File

@@ -100,12 +100,15 @@ allprojects {
}
subprojects {
// 1. 对纯 Java 模块的 JavaCompile 任务配置(升级为 Java 11
tasks.withType(JavaCompile) {
options.compilerArgs << "-parameters"
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
// 可选:确保编码一致
options.encoding = "UTF-8"
}
}
}
}
task clean(type: Delete) {

View File

@@ -0,0 +1,25 @@
Manifest-Version: 1.0
Built-By: Generated-by-ADT
Created-By: Android Gradle 3.5.0
Name: AndroidManifest.xml
SHA1-Digest: U36A0NWthb49+Rxs33tkIuNYFCI=
Name: jni/Android.mk
SHA1-Digest: ZpGSlRJPL0g9OejiWbQorqj40/Y=
Name: jni/Application.mk
SHA1-Digest: TKh2CbRLeKfvgL4cPfmoxcVz+vc=
Name: jni/hello-jni.cpp
SHA1-Digest: 1btXO19SqB6rDvPo5ynG0brDqTk=
Name: project.properties
SHA1-Digest: 0ekOiGTFMVJOWqAFzNFj/1vxPL8=
Name: res/values/strings.xml
SHA1-Digest: FgRO/zbNaC1wuZKVT7h6NSYBmpY=
Name: src/$package_name$/HelloJni.java
SHA1-Digest: p0e9DNKocjRnsOhetb9bnp9s9J4=

View File

@@ -32,28 +32,21 @@ android {
// versionName 更新后需要手动设置
// .winboll/winbollBuildProps.properties 文件的 stageCount=0
// Gradle编译环境下合起来的 versionName 就是 "${versionName}.0"
versionName "15.12"
versionName "15.11"
if(true) {
versionName = genVersionName("${versionName}")
}
}
// 米盟 SDK
packagingOptions {
doNotStrip "*/*/libmimo_1011.so"
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
// 米盟
api 'com.miui.zeus:mimo-ad-sdk:5.3.+'//请使用最新版sdk
//注意以下5个库必须要引入
//api 'androidx.appcompat:appcompat:1.4.1'
api 'androidx.recyclerview:recyclerview:1.0.0'
api 'com.google.code.gson:gson:2.8.5'
api 'com.github.bumptech.glide:glide:4.9.0'
//annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
// https://mvnrepository.com/artifact/com.jzxiang.pickerview/TimePickerDialog
api 'com.jzxiang.pickerview:TimePickerDialog:1.0.1'
@@ -79,14 +72,7 @@ dependencies {
//api 'androidx.vectordrawable:vectordrawable-animated:1.1.0'
//api 'androidx.fragment:fragment:1.1.0'
// WinBoLL库 nexus.winboll.cc 地址
//api 'cc.winboll.studio:libaes:15.12.0'
//api 'cc.winboll.studio:libappbase:15.12.2'
// WinBoLL备用库 jitpack.io 地址
api 'com.github.ZhanGSKen:AES:aes-v15.12.3'
api 'com.github.ZhanGSKen:APPBase:appbase-v15.12.2'
api 'cc.winboll.studio:libaes:15.11.0'
api 'cc.winboll.studio:libappbase:15.11.0'
api fileTree(dir: 'libs', include: ['*.jar'])
}

View File

@@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle
#Mon Dec 08 00:07:16 HKT 2025
stageCount=3
#Sat Nov 15 08:45:48 GMT 2025
stageCount=2
libraryProject=
baseVersion=15.12
publishVersion=15.12.2
buildCount=0
baseBetaVersion=15.12.3
baseVersion=15.11
publishVersion=15.11.1
buildCount=21
baseBetaVersion=15.11.2

View File

@@ -1,143 +1,21 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in C:\tools\adt-bundle-windows-x86_64-20131030\sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# ============================== 基础通用规则 ==============================
# 保留系统组件
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
# 保留 WinBoLL 核心包及子类(合并简化规则)
-keep class cc.winboll.studio.** { *; }
-keepclassmembers class cc.winboll.studio.** { *; }
# 保留所有类中的 public static final String TAG 字段(便于日志定位)
-keepclassmembers class * {
public static final java.lang.String TAG;
}
# 保留序列化类避免Parcelable/Gson解析异常
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
-keepclassmembers class * implements java.io.Serializable {
static final long serialVersionUID;
private static final java.io.ObjectStreamField[] serialPersistentFields;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}
# 保留 R 文件避免资源ID混淆
-keepclassmembers class **.R$* {
public static <fields>;
}
# 保留 native 方法避免JNI调用失败
-keepclasseswithmembernames class * {
native <methods>;
}
# 保留注解和泛型(避免反射/序列化异常)
-keepattributes *Annotation*
-keepattributes Signature
# 屏蔽 Java 8+ 警告(适配 Java 7 语法)
-dontwarn java.lang.invoke.*
-dontwarn android.support.v8.renderscript.*
-dontwarn java.util.function.**
# ============================== 第三方框架专项规则 ==============================
# OkHttp 4.4.1米盟广告请求依赖完善Lambda兼容
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-keep class okhttp3.internal.** { *; }
-keep class okio.** { *; }
-dontwarn okhttp3.internal.platform.**
-dontwarn okio.**
# ============================== 必要补充规则 ==============================
# OkHttp 4.4.1 补充规则Java 7 兼容)
-keep class okhttp3.internal.concurrent.** { *; }
-keep class okhttp3.internal.connection.** { *; }
-dontwarn okhttp3.internal.concurrent.TaskRunner
-dontwarn okhttp3.internal.connection.RealCall
# Glide 4.9.0(米盟广告图片加载依赖)
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.module.AppGlideModule
-keep public enum com.bumptech.glide.load.ImageHeaderParser$ImageType {
**[] $VALUES;
public *;
}
-keepclassmembers class * implements com.bumptech.glide.module.AppGlideModule {
<init>();
}
-dontwarn com.bumptech.glide.**
# Gson 2.8.5(米盟广告数据序列化依赖)
-keep class com.google.gson.** { *; }
-keep interface com.google.gson.** { *; }
-keepclassmembers class * {
@com.google.gson.annotations.SerializedName <fields>;
}
# 米盟 SDK(核心广告组件,完整保留避免加载失败)
-keep class com.miui.zeus.** { *; }
-keep interface com.miui.zeus.** { *; }
# 保留米盟日志字段(便于广告加载失败排查)
-keepclassmembers class com.miui.zeus.mimo.sdk.** {
public static final java.lang.String TAG;
}
# RecyclerView 1.0.0(米盟广告布局渲染依赖)
-keep class androidx.recyclerview.** { *; }
-keep interface androidx.recyclerview.** { *; }
-keepclassmembers class androidx.recyclerview.widget.RecyclerView$Adapter {
public *;
}
# 其他第三方框架(按引入依赖保留,无则可删除)
# XXPermissions 18.63
-keep class com.hjq.permissions.** { *; }
-keep interface com.hjq.permissions.** { *; }
# ZXing 二维码(核心解析组件)
-keep class com.google.zxing.** { *; }
-keep class com.journeyapps.zxing.** { *; }
# Jsoup HTML解析
-keep class org.jsoup.** { *; }
# Pinyin4j 拼音搜索
-keep class net.sourceforge.pinyin4j.** { *; }
# JSch SSH组件
-keep class com.jcraft.jsch.** { *; }
# AndroidX 基础组件
-keep class androidx.appcompat.** { *; }
-keep interface androidx.appcompat.** { *; }
# ============================== 优化与调试配置 ==============================
# 优化级别(平衡混淆效果与性能)
-optimizationpasses 5
-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*
# 调试辅助(保留行号便于崩溃定位)
-verbose
-dontpreverify
-dontusemixedcaseclassnames
-keepattributes SourceFile,LineNumberTable
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

View File

@@ -3,6 +3,9 @@
xmlns:android="http://schemas.android.com/apk/res/android"
package="cc.winboll.studio.positions">
<!-- 只能在前台获取精确的位置信息 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!-- 只有在前台运行时才能获取大致位置信息 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
@@ -27,9 +30,6 @@
<!-- 修改或删除您共享存储空间中的内容 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- 只能在前台获取精确的位置信息 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature
android:name="android.hardware.location.gps"
android:required="false"/>
@@ -65,6 +65,8 @@
</activity>
<activity android:name=".activities.CrashActivity"/>
<activity-alias
android:name=".MainActivityWukong"
android:targetActivity=".MainActivity"
@@ -109,9 +111,15 @@
</activity-alias>
<meta-data
android:name="android.max_aspect"
android:value="4.0"/>
<activity android:name="cc.winboll.studio.positions.activities.LocationActivity"/>
<activity android:name="cc.winboll.studio.positions.activities.ShortcutActionActivity"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<service
android:name=".services.MainService"
@@ -135,14 +143,6 @@
</receiver>
<meta-data
android:name="android.max_aspect"
android:value="4.0"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
@@ -151,11 +151,11 @@
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider"/>
android:resource="@xml/file_paths"/>
</provider>
<activity android:name="cc.winboll.studio.positions.activities.SettingsActivity"/>
<activity android:name="cc.winboll.studio.positions.activities.ShortcutActionActivity"/>
</application>

View File

@@ -14,7 +14,6 @@ import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
@@ -25,6 +24,7 @@ import android.widget.Toast;
import cc.winboll.studio.libaes.utils.WinBoLLActivityManager;
import cc.winboll.studio.libappbase.GlobalApplication;
import cc.winboll.studio.libappbase.ToastUtils;
import cc.winboll.studio.positions.utils.MyActivityLifecycleCallbacks;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
@@ -44,24 +44,36 @@ import java.util.concurrent.atomic.AtomicBoolean;
public class App extends GlobalApplication {
public static volatile AppLevel _mAppLevel = AppLevel.WUKONG;
public static final String COMPONENT_WUKONG = "cc.winboll.studio.positions.MainActivityWukong";
public static final String COMPONENT_LAOJUN = "cc.winboll.studio.positions.MainActivityLaojun";
public static final String ACTION_OPEN_APPPLUS = "cc.winboll.studio.positions.App.ACTION_OPEN_APPPLUS";
public static final String ACTION_CLOSE_APPPLUS = "cc.winboll.studio.positions.App.ACTION_CLOSE_APPPLUS";
private static Handler MAIN_HANDLER = new Handler(Looper.getMainLooper());
MyActivityLifecycleCallbacks mMyActivityLifecycleCallbacks;
@Override
public void onCreate() {
super.onCreate();
setIsDebugging(BuildConfig.DEBUG);
WinBoLLActivityManager.init(this);
// 初始化 Toast 框架
ToastUtils.init(this);
// 设置 Toast 布局样式
//ToastUtils.setView(R.layout.view_toast);
//ToastUtils.setStyle(new WhiteToastStyle());
//ToastUtils.setGravity(Gravity.BOTTOM, 0, 200);
//CrashHandler.getInstance().registerGlobal(this);
//CrashHandler.getInstance().registerPart(this);
mMyActivityLifecycleCallbacks = new MyActivityLifecycleCallbacks();
registerActivityLifecycleCallbacks(mMyActivityLifecycleCallbacks);
}
public static void write(InputStream input, OutputStream output) throws IOException {

View File

@@ -0,0 +1,43 @@
package cc.winboll.studio.positions;
/**
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2025/11/10 07:23
* @Describe 应用级别类型枚举
*/
public enum AppLevel {
WUKONG("wukong", "悟空级别"),
LAOJUN("laojun", "老君级别");
public static final String TAG = "AppLevel";
// 枚举属性
private final String code; // 编码(如 "wukong"
private final String desc; // 描述
// 构造方法Java 7 需显式定义)
AppLevel(String code, String desc) {
this.code = code;
this.desc = desc;
}
// Getter 方法(获取枚举属性)
public String getCode() {
return code;
}
public String getDesc() {
return desc;
}
// 可选:根据 code 获取枚举项(便于业务使用)
public static AppLevel getByCode(String code) {
for (AppLevel level : values()) {
if (level.code.equals(code)) {
return level;
}
}
return null; // 或抛出异常,根据业务需求调整
}
}

View File

@@ -6,8 +6,7 @@ import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
@@ -17,15 +16,14 @@ import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import cc.winboll.studio.libaes.interfaces.IWinBoLLActivity;
import cc.winboll.studio.libaes.utils.AESThemeUtil;
import cc.winboll.studio.libaes.utils.DevelopUtils;
import cc.winboll.studio.libaes.utils.WinBoLLActivityManager;
import cc.winboll.studio.libaes.views.ADsBannerView;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.ToastUtils;
import cc.winboll.studio.positions.activities.LocationActivity;
import cc.winboll.studio.positions.activities.SettingsActivity;
import cc.winboll.studio.positions.activities.WinBoLLActivity;
import cc.winboll.studio.positions.utils.APPPlusUtils;
import cc.winboll.studio.positions.utils.AppConfigsUtil;
import cc.winboll.studio.positions.utils.JsonShareHandler;
import cc.winboll.studio.positions.utils.ServiceUtil;
/**
@@ -36,7 +34,8 @@ import cc.winboll.studio.positions.utils.ServiceUtil;
*/
public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
public static final String TAG = "MainActivity";
// 权限请求码(建议定义为类常量,避免魔法值)
// 权限请求码(建议定义为类常量,避免魔法值)
private static final int REQUEST_LOCATION_PERMISSIONS = 1001;
private static final int REQUEST_BACKGROUND_LOCATION_PERMISSION = 1002;
@@ -46,8 +45,7 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
private Toolbar mToolbar;
// 服务相关:服务实例、绑定状态标记
//private DistanceRefreshService mDistanceService;
private boolean isServiceBound = false;
ADsBannerView mADsBannerView;
//private boolean isServiceBound = false;
@Override
@@ -89,6 +87,10 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 关联主页面布局
// 处理启动时的分享 Intent
handleShareIntent(getIntent());
// 1. 初始化顶部 Toolbar保留原逻辑设置页面标题
initToolbar();
// 2. 初始化其他控件
@@ -99,18 +101,36 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
}
// 4. 绑定服务(仅用于获取服务实时状态,不影响服务独立运行)
//bindDistanceService();
mADsBannerView = findViewById(R.id.adsbanner);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// 处理后续接收的分享 Intent如应用已在后台
handleShareIntent(intent);
}
private void handleShareIntent(Intent intent) {
if (intent != null && Intent.ACTION_SEND.equals(intent.getAction())) {
// 调用工具类,弹出确认对话框
JsonShareHandler.handleSharedJsonWithConfirm(this, intent, new JsonShareHandler.ConfirmCallback() {
@Override
public void onConfirm(boolean isConfirm) {
// 回调处理isConfirm 为 true 表示接收并保存false 表示取消
if (!isConfirm) {
Log.d("MainActivity", "用户取消接收文件");
// 可添加取消后的逻辑(如关闭页面)
// finish();
}
}
});
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mADsBannerView != null) {
mADsBannerView.releaseAdResources();
}
// 页面销毁时解绑服务避免Activity与服务相互引用导致内存泄漏
// if (isServiceBound) {
// unbindService(mServiceConn);
@@ -119,16 +139,6 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
// }
}
@Override
protected void onResume() {
super.onResume();
if (mADsBannerView != null) {
mADsBannerView.resumeADs(MainActivity.this);
}
}
// ---------------------- 核心功能1初始化UI组件Toolbar + 服务开关) ----------------------
/**
* 初始化顶部 Toolbar设置页面标题
@@ -137,9 +147,9 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
mToolbar = (Toolbar) findViewById(R.id.toolbar); // Java 7 显式 findViewById + 强转
setSupportActionBar(mToolbar);
// 给ActionBar设置标题先判断非空避免空指针异常
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(getString(R.string.app_name));
}
AppLevel appLevel = AppConfigsUtil.getInstance(getApplicationContext()).getAppLevel(true);
getSupportActionBar().setTitle(getString(R.string.app_name));
}
/**
@@ -178,39 +188,6 @@ public class MainActivity extends WinBoLLActivity implements IWinBoLLActivity {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// 主题菜单
AESThemeUtil.inflateMenu(this, menu);
// 调试工具菜单
if (App.isDebugging()) {
DevelopUtils.inflateMenu(this, menu);
}
// 应用其他菜单
getMenuInflater().inflate(R.menu.toolbar_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int menuItemId = item.getItemId();
if (AESThemeUtil.onAppThemeItemSelected(this, item)) {
recreate();
} if (DevelopUtils.onDevelopItemSelected(this, item)) {
LogUtils.d(TAG, String.format("onOptionsItemSelected item.getItemId() %d ", item.getItemId()));
} else if (item.getItemId() == R.id.item_settings) {
Intent intent = new Intent();
intent.setClass(this, SettingsActivity.class);
startActivity(intent);
} else {
// 在switch语句中处理每个ID并在处理完后返回true未处理的情况返回false。
return super.onOptionsItemSelected(item);
}
return true;
}
/**
* 绑定服务(仅用于获取服务状态,不启动服务)
*/

View File

@@ -0,0 +1,22 @@
package cc.winboll.studio.positions;
import android.os.Bundle;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.ToastUtils;
/**
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2025/11/13 15:21
* @Describe MainActivityLaojun
*/
public class MainActivityLaojun extends MainActivity {
public static final String TAG = "MainActivityLaojun";
@Override
protected void onCreate(Bundle savedInstanceState) {
ToastUtils.show("道法自然");
LogUtils.d(TAG, "玩法归臻");
super.onCreate(savedInstanceState);
}
}

View File

@@ -0,0 +1,43 @@
package cc.winboll.studio.positions;
/**
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2025/11/15 15:14
* @Describe 应用入口级别类型枚举
*/
public enum PointLevel {
DORAEMON("doraemon", "叮铛级别"),
WUKONG("wukong", "悟空级别"),
LAOJUN("laojun", "老君级别");
public static final String TAG = "PointLevel";
// 枚举属性
private final String code; // 编码(如 "wukong"
private final String desc; // 描述
// 构造方法Java 7 需显式定义)
PointLevel(String code, String desc) {
this.code = code;
this.desc = desc;
}
// Getter 方法(获取枚举属性)
public String getCode() {
return code;
}
public String getDesc() {
return desc;
}
// 可选:根据 code 获取枚举项(便于业务使用)
public static PointLevel getByCode(String code) {
for (PointLevel level : values()) {
if (level.code.equals(code)) {
return level;
}
}
return null; // 或抛出异常,根据业务需求调整
}
}

View File

@@ -16,10 +16,8 @@ import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cc.winboll.studio.libaes.interfaces.IWinBoLLActivity;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.ToastUtils;
import cc.winboll.studio.positions.R;
@@ -35,14 +33,12 @@ import java.util.concurrent.atomic.AtomicBoolean;
* 2. Adapter 初始化传入 MainService 实例,确保数据来源唯一
* 3. 所有位置/任务操作通过 MainService 接口执行
*/
public class LocationActivity extends WinBoLLActivity implements IWinBoLLActivity {
public class LocationActivity extends Activity {
public static final String TAG = "LocationActivity";
private Toolbar mToolbar;
private RecyclerView mRvPosition;
private PositionAdapter mPositionAdapter;
// MainService 引用+绑定状态AtomicBoolean 确保多线程状态可见性)
private MainService mMainService;
private final AtomicBoolean isServiceBound = new AtomicBoolean(false);
@@ -100,34 +96,11 @@ public class LocationActivity extends WinBoLLActivity implements IWinBoLLActivit
}
};
@Override
public Activity getActivity() {
return this;
}
@Override
public String getTag() {
return TAG;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mToolbar.setSubtitle(getTag());
mToolbar.setTitleTextAppearance(this, R.style.Toolbar_TitleText);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "【导航栏】点击返回");
finish();
}
});
// 1. 初始化视图优先执行避免Adapter初始化时视图为空
initView();
// 2. 初始化GPS监听提前创建避免绑定服务后空指针
@@ -194,7 +167,7 @@ public class LocationActivity extends WinBoLLActivity implements IWinBoLLActivit
}
}
LogUtils.d(TAG, "数据同步完成:服务位置数=" + (servicePosList == null ? 0 : servicePosList.size())
+ ",本地缓存数=" + mLocalPosCache.size());
+ ",本地缓存数=" + mLocalPosCache.size());
} catch (Exception e) {
LogUtils.d(TAG, "同步服务数据失败:" + e.getMessage());
@@ -210,9 +183,9 @@ public class LocationActivity extends WinBoLLActivity implements IWinBoLLActivit
// 1. 多重安全校验(避免销毁后初始化/重复初始化/依赖未就绪)
if (isAdapterInited.get() || !isServiceBound.get() || mMainService == null || mRvPosition == null) {
LogUtils.w(TAG, "Adapter初始化跳过"
+ "已初始化=" + isAdapterInited.get()
+ ",服务绑定=" + isServiceBound.get()
+ ",视图就绪=" + (mRvPosition != null));
+ "已初始化=" + isAdapterInited.get()
+ ",服务绑定=" + isServiceBound.get()
+ ",视图就绪=" + (mRvPosition != null));
return;
}
@@ -222,54 +195,54 @@ public class LocationActivity extends WinBoLLActivity implements IWinBoLLActivit
// 3. 设置删除回调(删除时同步服务+本地缓存+Adapter
mPositionAdapter.setOnDeleteClickListener(new PositionAdapter.OnDeleteClickListener() {
@Override
public void onDeleteClick(int position) {
// 安全校验(索引有效+服务绑定+缓存非空)
if (position < 0 || position >= mLocalPosCache.size() || !isServiceBound.get() || mMainService == null) {
LogUtils.w(TAG, "删除位置失败:索引无效/服务未就绪(索引=" + position + ",缓存量=" + mLocalPosCache.size() + "");
return;
}
@Override
public void onDeleteClick(int position) {
// 安全校验(索引有效+服务绑定+缓存非空)
if (position < 0 || position >= mLocalPosCache.size() || !isServiceBound.get() || mMainService == null) {
LogUtils.w(TAG, "删除位置失败:索引无效/服务未就绪(索引=" + position + ",缓存量=" + mLocalPosCache.size() + "");
return;
}
PositionModel deletePos = mLocalPosCache.get(position);
if (deletePos != null && !deletePos.getPositionId().isEmpty()) {
// 步骤1调用服务删除确保服务数据一致性
mMainService.removePosition(deletePos.getPositionId());
// 步骤2删除本地缓存确保缓存与服务同步
synchronized (mLocalPosCache) {
mLocalPosCache.remove(position);
}
// 步骤3通知Adapter刷新基于缓存操作避免空数据
mPositionAdapter.notifyItemRemoved(position);
showToast("删除位置成功:" + deletePos.getMemo());
LogUtils.d(TAG, "删除位置完成ID=" + deletePos.getPositionId() + "(服务+缓存已同步)");
}
}
});
PositionModel deletePos = mLocalPosCache.get(position);
if (deletePos != null && !deletePos.getPositionId().isEmpty()) {
// 步骤1调用服务删除确保服务数据一致性
mMainService.removePosition(deletePos.getPositionId());
// 步骤2删除本地缓存确保缓存与服务同步
synchronized (mLocalPosCache) {
mLocalPosCache.remove(position);
}
// 步骤3通知Adapter刷新基于缓存操作避免空数据
mPositionAdapter.notifyItemRemoved(position);
showToast("删除位置成功:" + deletePos.getMemo());
LogUtils.d(TAG, "删除位置完成ID=" + deletePos.getPositionId() + "(服务+缓存已同步)");
}
}
});
// 4. 设置保存回调(保存时同步服务+本地缓存+Adapter
mPositionAdapter.setOnSavePositionClickListener(new PositionAdapter.OnSavePositionClickListener() {
@Override
public void onSavePositionClick(int position, PositionModel updatedPos) {
// 安全校验(索引有效+服务绑定+数据非空)
if (!isServiceBound.get() || mMainService == null
|| position < 0 || position >= mLocalPosCache.size() || updatedPos == null) {
LogUtils.w(TAG, "保存位置失败:服务未就绪/索引无效/数据空");
showToast("服务未就绪,保存失败");
return;
}
@Override
public void onSavePositionClick(int position, PositionModel updatedPos) {
// 安全校验(索引有效+服务绑定+数据非空)
if (!isServiceBound.get() || mMainService == null
|| position < 0 || position >= mLocalPosCache.size() || updatedPos == null) {
LogUtils.w(TAG, "保存位置失败:服务未就绪/索引无效/数据空");
showToast("服务未就绪,保存失败");
return;
}
// 步骤1调用服务更新确保服务数据一致性
mMainService.updatePosition(updatedPos);
// 步骤2更新本地缓存确保缓存与服务同步
synchronized (mLocalPosCache) {
mLocalPosCache.set(position, updatedPos);
}
// 步骤3通知Adapter刷新基于缓存操作避免空数据
mPositionAdapter.notifyItemChanged(position);
showToast("保存位置成功:" + updatedPos.getMemo());
LogUtils.d(TAG, "保存位置完成ID=" + updatedPos.getPositionId() + "(服务+缓存已同步)");
}
});
// 步骤1调用服务更新确保服务数据一致性
mMainService.updatePosition(updatedPos);
// 步骤2更新本地缓存确保缓存与服务同步
synchronized (mLocalPosCache) {
mLocalPosCache.set(position, updatedPos);
}
// 步骤3通知Adapter刷新基于缓存操作避免空数据
mPositionAdapter.notifyItemChanged(position);
showToast("保存位置成功:" + updatedPos.getMemo());
LogUtils.d(TAG, "保存位置完成ID=" + updatedPos.getPositionId() + "(服务+缓存已同步)");
}
});
// 5. 设置Adapter到RecyclerView最后一步确保Adapter已配置完成
mRvPosition.setAdapter(mPositionAdapter);
@@ -295,7 +268,7 @@ public class LocationActivity extends WinBoLLActivity implements IWinBoLLActivit
}
Toast.makeText(this, content, Toast.LENGTH_SHORT).show();
}
// ---------------------- 页面交互新增位置逻辑保留适配GPS数据 ----------------------
/**
* 新增位置调用服务addPosition()可选用当前GPS位置初始化新位置
@@ -422,7 +395,9 @@ public class LocationActivity extends WinBoLLActivity implements IWinBoLLActivit
LogUtils.d(TAG, "onResume服务已绑定但Adapter未初始化重新同步数据");
syncDataFromMainService();
initPositionAdapter();
} else if (isServiceBound.get() && mMainService != null && isAdapterInited.get() && mPositionAdapter != null) {
}
// 2. 服务已绑定且Adapter已初始化刷新数据确保与服务同步
else if (isServiceBound.get() && mMainService != null && isAdapterInited.get() && mPositionAdapter != null) {
syncDataFromMainService();
mPositionAdapter.notifyDataSetChanged();
LogUtils.d(TAG, "onResume刷新位置数据与服务同步");

View File

@@ -1,51 +0,0 @@
package cc.winboll.studio.positions.activities;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.widget.Toolbar;
import cc.winboll.studio.libaes.interfaces.IWinBoLLActivity;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.ToastUtils;
import cc.winboll.studio.positions.R;
/**
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2025/12/07 23:29
* @Describe 应用设置活动窗口
*/
public class SettingsActivity extends WinBoLLActivity implements IWinBoLLActivity {
public static final String TAG = "SettingsActivity";
private Toolbar mToolbar;
@Override
public Activity getActivity() {
return this;
}
@Override
public String getTag() {
return TAG;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mToolbar.setSubtitle(getTag());
mToolbar.setTitleTextAppearance(this, R.style.Toolbar_TitleText);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "【导航栏】点击返回");
finish();
}
});
}
}

View File

@@ -0,0 +1,61 @@
package cc.winboll.studio.positions.activities;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import cc.winboll.studio.libappbase.ToastUtils;
import cc.winboll.studio.positions.R;
import cc.winboll.studio.positions.utils.APPPlusUtils;
import cc.winboll.studio.positions.utils.AppConfigsUtil;
import cc.winboll.studio.positions.AppLevel;
/**
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2025/11/15 13:45
* @Describe 应用快捷方式活动类
*/
public class ShortcutActionActivity extends Activity {
public static final String TAG = "ShortcutActionActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 处理应用级别的切换请求
handleSwitchRequest();
finish();
}
// @Override
// public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
// super.onPostCreate(savedInstanceState, persistentState);
// finish();
// }
// @Override
// protected void onStart() {
// super.onStart();
// }
/**
* 处理应用图标快捷菜单的请求
*/
private void handleSwitchRequest() {
Intent intent = getIntent();
if (intent != null && "open_appplus".equals(intent.getDataString())) {
ToastUtils.show("已添加" + getString(R.string.app_name) + "附加组件");
AppConfigsUtil.getInstance(getApplicationContext()).setAppLevel(AppLevel.LAOJUN);
APPPlusUtils.openAPPPlus(this);
//moveTaskToBack(true);
}
if (intent != null && "close_appplus".equals(intent.getDataString())) {
ToastUtils.show("已移除" + getString(R.string.app_name) + "附加组件");
AppConfigsUtil.getInstance(getApplicationContext()).setAppLevel(AppLevel.WUKONG);
APPPlusUtils.closeAPPPlus(this);
//moveTaskToBack(true);
}
}
}

View File

@@ -10,16 +10,20 @@ import android.os.Bundle;
import android.view.MenuItem;
import androidx.appcompat.app.AppCompatActivity;
import cc.winboll.studio.libaes.interfaces.IWinBoLLActivity;
import cc.winboll.studio.libaes.models.AESThemeBean;
import cc.winboll.studio.libaes.utils.AESThemeUtil;
import cc.winboll.studio.libaes.utils.WinBoLLActivityManager;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.ToastUtils;
import cc.winboll.studio.positions.App;
import cc.winboll.studio.positions.PointLevel;
import cc.winboll.studio.positions.R;
import cc.winboll.studio.positions.utils.ActivityAliasUtils;
import cc.winboll.studio.positions.utils.AppConfigsUtil;
public class WinBoLLActivity extends AppCompatActivity implements IWinBoLLActivity {
public static final String TAG = "WinBoLLActivity";
protected volatile AESThemeBean.ThemeType mThemeType;
public static volatile PointLevel _mPointLevel = PointLevel.WUKONG;
@Override
public Activity getActivity() {
@@ -31,25 +35,58 @@ public class WinBoLLActivity extends AppCompatActivity implements IWinBoLLActivi
return TAG;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
mThemeType = getThemeType();
setThemeStyle();
super.onCreate(savedInstanceState);
}
AESThemeBean.ThemeType getThemeType() {
return AESThemeBean.getThemeStyleType(AESThemeUtil.getThemeTypeID(getApplicationContext()));
}
void setThemeStyle() {
setTheme(AESThemeUtil.getThemeTypeID(getApplicationContext()));
}
@Override
protected void onResume() {
super.onResume();
//ToastUtils.show("onResume");
// ActivityAliasUtils 工具使用示例
//
// // 获取真实的目标组件名(即使通过 alias 启动,也能拿到 OriginalActivity
// String realTargetName = ActivityAliasUtils.getRealTargetNameFromIntent(this);
// LogUtils.d("AliasActivity", "真实组件名:" + realTargetName);
// 获取真实的目标组件名(即使通过 alias 启动,也能拿到 OriginalActivity
// String realTargetName = ActivityAliasUtils.getRealTargetNameFromIntent(this);
// LogUtils.d(TAG, "真实组件名:" + realTargetName);
// ToastUtils.show(realTargetName);
// // 判断某个组件是否为 alias
// String componentName = "com.winboll.app.AliasActivity";
// boolean isAlias = ActivityAliasUtils.isActivityAlias(getApplicationContext(), componentName);
// LogUtils.d("判断结果", componentName + " 是否为 alias" + isAlias); // true
// // 获取启动当前 Activity 的组件名(兼容 alias 场景)
// String launchComponent = ActivityAliasUtils.getLaunchComponentName(this);
// LogUtils.d("MainActivity", "启动组件名:" + launchComponent);
/*
* 应用入口逻辑模块
*/
//
// 检查当前活动的启动组件名,设置应用入口级别。
String launchComponent = ActivityAliasUtils.getLaunchComponentName(this);
LogUtils.d("MainActivity", "启动组件名:" + launchComponent);
ToastUtils.show(launchComponent);
// 当前应用处于活动暂停的状态时,就检查应用的入口组件名称,设置应用入口级别。
if (WinBoLLActivity._mPointLevel == PointLevel.DORAEMON) {
if (launchComponent.equals(App.COMPONENT_WUKONG)) {
getSupportActionBar().setTitle(getString(R.string.appplus_name));
ToastUtils.show("WUKONG");
_mPointLevel = PointLevel.WUKONG;
} else if (launchComponent.equals(App.COMPONENT_LAOJUN)) {
getSupportActionBar().setTitle(getString(R.string.app_name));
ToastUtils.show("LAOJUN");
_mPointLevel = PointLevel.LAOJUN;
} else {
// 如果是其他应用组件入口,就关闭活动
finish();
}
}
/*
* 应用级别设置模块
*/
// 读取并配置应用级别
App._mAppLevel = AppConfigsUtil.getInstance(getApplicationContext()).getAppLevel(true);
LogUtils.d(TAG, String.format("onResume %s", getTag()));
}

View File

@@ -9,12 +9,14 @@ package cc.winboll.studio.positions.models;
import android.util.JsonWriter;
import android.util.JsonReader;
import java.io.IOException;
import cc.winboll.studio.positions.AppLevel;
public class AppConfigsModel extends BaseBean {
public static final String TAG = "AppConfigsModel";
boolean isEnableMainService;
AppLevel appLevel;
public AppConfigsModel(boolean isEnableMainService) {
this.isEnableMainService = isEnableMainService;
@@ -24,6 +26,14 @@ public class AppConfigsModel extends BaseBean {
this.isEnableMainService = false;
}
public void setAppLevel(AppLevel appLevel) {
this.appLevel = appLevel;
}
public AppLevel getAppLevel() {
return appLevel;
}
public void setIsEnableMainService(boolean isEnableMainService) {
this.isEnableMainService = isEnableMainService;
}
@@ -42,6 +52,7 @@ public class AppConfigsModel extends BaseBean {
public void writeThisToJsonWriter(JsonWriter jsonWriter) throws IOException {
super.writeThisToJsonWriter(jsonWriter);
jsonWriter.name("isEnableDistanceRefreshService").value(isEnableMainService());
jsonWriter.name("appLevel").value(getAppLevel().ordinal());
}
// JSON反序列化加载位置数据校验字段
@@ -52,6 +63,8 @@ public class AppConfigsModel extends BaseBean {
} else {
if (name.equals("isEnableDistanceRefreshService")) {
setIsEnableMainService(jsonReader.nextBoolean());
} else if (name.equals("appLevel")) {
setAppLevel((AppLevel.values()[jsonReader.nextInt()]));
} else {
return false;
}

View File

@@ -0,0 +1,361 @@
package cc.winboll.studio.positions.receivers;
/**
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2025/10/28 19:07
* @Describe MotionStatusReceiver
*/
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.ToastUtils;
import cc.winboll.studio.positions.services.MainService;
import cc.winboll.studio.positions.utils.ServiceUtil;
/**
* 运动状态监听Receiver
* 功能1.持续监听传感器(不关闭) 2.每5秒计算运动状态 3.按状态切换GPS模式实时/30秒定时
*/
public class MotionStatusReceiver extends BroadcastReceiver implements SensorEventListener {
public static final String TAG = "MotionStatusReceiver";
// 广播Action
public static final String ACTION_MOTION_STATUS_RECEIVER = "cc.winboll.studio.positions.receivers.MotionStatusReceiver";
public static final String EXTRA_SENSORS_ENABLE = "EXTRA_SENSORS_ENABLE";
// 传感器启动状态标志位
boolean mIsSensorsEnable = false;
// 运动状态常量
private static final int MOTION_STATUS_STATIC = 0; // 静止/低运动
private static final int MOTION_STATUS_WALKING = 1; // 行走/高速运动
// 配置参数(按需求调整)
private static final float ACCELEROMETER_THRESHOLD = 0.8f; // 加速度阈值
private static final float GYROSCOPE_THRESHOLD = 0.5f; // 陀螺仪阈值
private static final long STATUS_CALC_INTERVAL = 5000; // 运动状态计算间隔5秒
private static final long GPS_STATIC_INTERVAL = 30; // 静止时GPS间隔30秒
// 核心对象
private volatile SensorManager mSensorManager;
private Sensor mAccelerometer;
private Sensor mGyroscope;
private volatile boolean mIsSensorListening = false; // 传感器是否持续监听
private int mCurrentMotionStatus = MOTION_STATUS_STATIC; // 当前运动状态
private Handler mMainHandler; // 主线程Handler用于定时计算
private Context mBroadcastContext; // 广播上下文
// 传感器数据缓存用于5秒内数据汇总避免单次波动误判
private float mAccelMax = 0f; // 5秒内加速度最大值
private float mGyroMax = 0f; // 5秒内陀螺仪最大值
@Override
public void onReceive(Context context, Intent intent) {
LogUtils.d(TAG, "===== 接收器启动onReceive() 开始执行 =====");
this.mBroadcastContext = context;
mMainHandler = new Handler(Looper.getMainLooper());
if (TextUtils.equals(intent.getAction(), ACTION_MOTION_STATUS_RECEIVER)) {
boolean isSettingEnable = intent.getBooleanExtra(EXTRA_SENSORS_ENABLE, false);
if (mIsSensorsEnable == false && isSettingEnable == true) {
mIsSensorsEnable = true;
// 1. 初始化传感器(必执行)
initSensors();
if (mAccelerometer == null || mGyroscope == null) {
LogUtils.e(TAG, "设备缺少加速度/陀螺仪,无法持续监听");
cleanResources(false); // 传感器不可用才清理
return;
}
// 2. 校验参数
if (context == null || intent == null) {
LogUtils.d(TAG, "onReceive():无效参数,终止处理");
cleanResources(false);
return;
}
LogUtils.d(TAG, "onReceive()接收到广播Action=" + intent.getAction());
// 3. 启动持续传感器监听(核心:不关闭,重复调用无影响)
startSensorListening();
// 4. 启动5秒定时计算运动状态核心持续触发状态判断
startStatusCalcTimer();
}
}
// 5. 处理外部广播触发(可选,保留外部控制能力)
// if (TextUtils.equals(intent.getAction(), ACTION_MOTION_STATUS_RECEIVER)) {
// int motionStatus = intent.getIntExtra(EXTRA_MOTION_STATUS, MOTION_STATUS_STATIC);
// String statusDesc = motionStatus == MOTION_STATUS_WALKING ? "高速运动" : "静止/低运动";
// LogUtils.d(TAG, "外部广播触发,强制设置运动状态:" + statusDesc);
// mCurrentMotionStatus = motionStatus;
// handleMotionStatus(mCurrentMotionStatus); // 立即执行GPS切换
// }
}
/**
* 初始化传感器(持续监听,复用实例)
*/
private void initSensors() {
LogUtils.d(TAG, "initSensors():初始化传感器");
if (mSensorManager != null || mBroadcastContext == null) return;
mSensorManager = (SensorManager) mBroadcastContext.getSystemService(Context.SENSOR_SERVICE);
if (mSensorManager == null) {
LogUtils.e(TAG, "设备不支持传感器服务");
return;
}
// 获取传感器实例(持续复用,不销毁)
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
LogUtils.d(TAG, "传感器初始化结果:加速度=" + (mAccelerometer != null) + ",陀螺仪=" + (mGyroscope != null));
}
/**
* 启动传感器持续监听(核心:不关闭,注册一次一直生效)
*/
private void startSensorListening() {
if (mSensorManager == null || mAccelerometer == null || mGyroscope == null) return;
if (!mIsSensorListening) {
// 注册传感器监听(持续生效,直到服务销毁才注销)
mSensorManager.registerListener(
this,
mAccelerometer,
SensorManager.SENSOR_DELAY_NORMAL, // 正常延迟,平衡性能与精度
mMainHandler
);
mSensorManager.registerListener(
this,
mGyroscope,
SensorManager.SENSOR_DELAY_NORMAL,
mMainHandler
);
mIsSensorListening = true;
LogUtils.d(TAG, "startSensorListening():传感器持续监听已启动(不关闭)");
}
}
/**
* 启动5秒定时计算运动状态核心周期性汇总传感器数据
*/
private void startStatusCalcTimer() {
if (mMainHandler == null) return;
// 移除旧任务(避免重复注册)
mMainHandler.removeCallbacks(mStatusCalcRunnable);
// 启动定时任务每5秒执行一次
mMainHandler.postDelayed(mStatusCalcRunnable, STATUS_CALC_INTERVAL);
LogUtils.d(TAG, "startStatusCalcTimer()5秒运动状态计算定时器已启动");
}
/**
* 运动状态计算任务5秒执行一次
*/
private final Runnable mStatusCalcRunnable = new Runnable() {
@Override
public void run() {
// 1. 基于5秒内缓存的最大传感器数据判断状态
boolean isHighMotion = (mAccelMax > ACCELEROMETER_THRESHOLD) && (mGyroMax > GYROSCOPE_THRESHOLD);
int newMotionStatus = isHighMotion ? MOTION_STATUS_WALKING : MOTION_STATUS_STATIC;
// 2. 状态变化时才处理避免频繁切换GPS
if (newMotionStatus != mCurrentMotionStatus) {
mCurrentMotionStatus = newMotionStatus;
String statusDesc = isHighMotion ? "高速运动" : "静止/低运动";
LogUtils.d(TAG, "运动状态更新5秒计算" + statusDesc
+ "(加速度最大值=" + mAccelMax + ",陀螺仪最大值=" + mGyroMax + "");
handleMotionStatus(newMotionStatus); // 切换GPS模式
} else {
LogUtils.d(TAG, "运动状态无变化5秒计算" + (isHighMotion ? "高速运动" : "静止/低运动"));
}
// 3. 重置传感器数据缓存准备下一个5秒周期
mAccelMax = 0f;
mGyroMax = 0f;
// 4. 循环执行定时任务(核心:持续计算)
mMainHandler.postDelayed(this, STATUS_CALC_INTERVAL);
}
};
/**
* 传感器数据变化回调(核心:实时缓存最大数据)
*/
@Override
public void onSensorChanged(SensorEvent event) {
if (event == null) return;
// 实时缓存5秒内的最大传感器数据避免单次波动误判
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
float accelTotal = Math.abs(event.values[0]) + Math.abs(event.values[1]) + Math.abs(event.values[2]);
if (accelTotal > mAccelMax) mAccelMax = accelTotal; // 缓存最大值
LogUtils.d(TAG, "加速度传感器实时数据:合值=" + accelTotal + "当前5秒最大值=" + mAccelMax + "");
break;
case Sensor.TYPE_GYROSCOPE:
float gyroTotal = Math.abs(event.values[0]) + Math.abs(event.values[1]) + Math.abs(event.values[2]);
if (gyroTotal > mGyroMax) mGyroMax = gyroTotal; // 缓存最大值
LogUtils.d(TAG, "陀螺仪实时数据:合值=" + gyroTotal + "当前5秒最大值=" + mGyroMax + "");
break;
}
}
/**
* 处理运动状态核心按状态切换GPS模式
*/
private void handleMotionStatus(int motionStatus) {
LogUtils.d(TAG, "handleMotionStatus()开始处理运动状态切换GPS模式");
if (mBroadcastContext == null) {
LogUtils.w(TAG, "上下文为空无法处理GPS");
return;
}
MainService mainService = getMainService();
if (mainService == null) {
LogUtils.e(TAG, "MainService未启动GPS控制失败");
return;
}
if (motionStatus == MOTION_STATUS_WALKING) {
// 高速运动启动GPS实时更新2秒/1米
handleHighMotionGPS(mainService);
} else {
// 静止/低运动启动GPS30秒定时更新
handleStaticGPS(mainService);
}
}
/**
* 高速运动GPS处理实时更新
*/
private void handleHighMotionGPS(MainService mainService) {
// 动态权限判断Android 6.0+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
mBroadcastContext.checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
sendPermissionRequestBroadcast();
return;
}
// 启动实时GPS已启动则不重复操作
if (!mainService.isGpsListening()) {
mainService.startGpsLocation(); // 实时更新2秒/1米
mainService.stopGpsStaticTimer(); // 停止定时GPS
LogUtils.d(TAG, "高速运动已启动GPS实时更新");
}
}
/**
* 静止/低运动GPS处理30秒定时更新
*/
private void handleStaticGPS(MainService mainService) {
// 停止实时GPS已停止则不重复操作
if (mainService.isGpsListening()) {
mainService.stopGpsLocation(); // 停止实时更新
LogUtils.d(TAG, "静止/低运动已停止GPS实时更新");
}
// 启动30秒定时GPS已启动则不重复操作
mainService.startGpsStaticTimer(GPS_STATIC_INTERVAL); // 30秒一次
LogUtils.d(TAG, "静止/低运动已启动GPS30秒定时更新");
}
/**
* 获取MainService实例复用逻辑
*/
private MainService getMainService() {
if (mBroadcastContext == null) return null;
// 优先获取单例
MainService singleton = MainService.getInstance(mBroadcastContext);
if (singleton != null && singleton.isServiceRunning()) {
return singleton;
}
// 启动服务并重试
if (!ServiceUtil.isServiceAlive(mBroadcastContext, MainService.class.getName())) {
mBroadcastContext.startService(new Intent(mBroadcastContext, MainService.class));
try {
Thread.sleep(500); // 等待服务启动
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
return MainService.getInstance(mBroadcastContext);
}
/**
* 发送GPS权限申请广播Receiver无法直接申请
*/
private void sendPermissionRequestBroadcast() {
Intent permissionIntent = new Intent("cc.winboll.studio.positions.ACTION_REQUEST_GPS_PERMISSION");
permissionIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
mBroadcastContext.sendBroadcast(permissionIntent);
LogUtils.d(TAG, "GPS权限缺失已发送申请广播");
}
/**
* 资源清理核心传感器不关闭仅清理Handler和上下文
* @param isForceStopSensor 是否强制停止传感器仅服务销毁时传true
*/
private void cleanResources(boolean isForceStopSensor) {
// 1. 停止定时计算任务
if (mMainHandler != null) {
mMainHandler.removeCallbacksAndMessages(null);
mMainHandler = null;
LogUtils.d(TAG, "cleanResources():已停止运动状态计算定时器");
}
// 2. 强制停止传感器(仅当外部触发销毁时执行,正常情况不关闭)
if (isForceStopSensor && mSensorManager != null && mIsSensorListening) {
mSensorManager.unregisterListener(this);
mIsSensorListening = false;
LogUtils.d(TAG, "cleanResources():已强制停止传感器监听");
}
// 3. 置空上下文(避免内存泄漏)
mBroadcastContext = null;
}
/**
* 传感器精度变化回调(日志监控)
*/
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
String sensorType = sensor.getType() == Sensor.TYPE_ACCELEROMETER ? "加速度" : "陀螺仪";
String accuracyDesc = getAccuracyDesc(accuracy);
LogUtils.d(TAG, sensorType + "传感器精度变化:" + accuracyDesc);
}
/**
* 传感器精度描述转换
*/
private String getAccuracyDesc(int accuracy) {
switch (accuracy) {
case SensorManager.SENSOR_STATUS_ACCURACY_HIGH: return "";
case SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM: return "";
case SensorManager.SENSOR_STATUS_ACCURACY_LOW: return "";
case SensorManager.SENSOR_STATUS_UNRELIABLE: return "不可靠";
default: return "未知";
}
}
/**
* 补充Receiver销毁时强制清理需在MainService注销时调用
*/
public void forceCleanResources() {
cleanResources(true); // 强制停止传感器
}
}

View File

@@ -0,0 +1,194 @@
package cc.winboll.studio.positions.utils;
/**
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2025/11/10 09:51
* @Describe 应用图标切换工具类(启用组件时创建对应快捷方式)
*/
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.widget.Toast;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.positions.App;
import cc.winboll.studio.positions.MainActivity;
public class APPPlusUtils {
public static final String TAG = "APPPlusUtils";
// 快捷方式配置(名称+图标,需与实际资源匹配)
// private static final String PLUS_SHORTCUT_NAME = "位置服务-Laojun";
// private static final int PLUS_SHORTCUT_ICON = R.mipmap.ic_launcher; // Laojun 图标资源
/**
* 添加Plus组件与图标
*/
public static boolean openAPPPlus(Context context) {
if (context == null) {
LogUtils.d(TAG, "切换失败:上下文为空");
Toast.makeText(context, "图标切换失败", Toast.LENGTH_SHORT).show();
return false;
}
PackageManager pm = context.getPackageManager();
ComponentName plusComponentLaojun = new ComponentName(context, App.COMPONENT_LAOJUN);
//ComponentName plusComponentWuKong = new ComponentName(context, MainActivity.COMPONENT_WUKONG);
try {
//disableComponent(pm, plusComponentWuKong);
enableComponent(pm, plusComponentLaojun);
// 2. 创建 Laojun 组件对应的快捷方式(自动去重)
// boolean shortcutCreated = createComponentShortcut(context, plusComponent, PLUS_SHORTCUT_NAME, PLUS_SHORTCUT_ICON);
//
// // 3. 通知桌面刷新图标
// context.sendBroadcast(new Intent(Intent.ACTION_PACKAGE_CHANGED)
// .setData(android.net.Uri.parse("package:" + context.getPackageName())));
//
// // 4. 反馈结果
// String logMsg = shortcutCreated ? "启用 Laojun + 快捷方式创建成功" : "启用 Laojun 成功,快捷方式创建失败";
// String toastMsg = shortcutCreated ? "图标切换为 Laojun已创建快捷方式" : "图标切换为 Laojun快捷方式创建失败";
// LogUtils.d(TAG, logMsg);
// Toast.makeText(context, toastMsg, Toast.LENGTH_SHORT).show();
//
return true;
} catch (Exception e) {
LogUtils.e(TAG, "Laojun 图标切换失败:" + e.getMessage());
// 失败兜底:启用 Wukong 组件
//enableComponent(pm, wukongComponent);
Toast.makeText(context, "图标切换失败" + e.getMessage(), Toast.LENGTH_SHORT).show();
return false;
}
}
/**
* 移除Plus组件
*/
public static boolean closeAPPPlus(Context context) {
if (context == null) {
LogUtils.d(TAG, "切换失败:上下文为空");
Toast.makeText(context, "图标切换失败", Toast.LENGTH_SHORT).show();
return false;
}
PackageManager pm = context.getPackageManager();
ComponentName plusComponentLaojun = new ComponentName(context, App.COMPONENT_LAOJUN);
//ComponentName plusComponentWuKong = new ComponentName(context, MainActivity.COMPONENT_WUKONG);
disableComponent(pm, plusComponentLaojun);
//enableComponent(pm, plusComponentWuKong);
return true;
}
/**
* 创建指定组件的桌面快捷方式(自动去重,兼容 Android 8.0+
* @param component 目标组件(如 LAOJUN_ACTIVITY
* @param name 快捷方式名称
* @param iconRes 快捷方式图标资源ID
* @return 是否创建成功
*/
private static boolean createComponentShortcut(Context context, ComponentName component, String name, int iconRes) {
if (context == null || component == null || name == null || iconRes == 0) {
LogUtils.d(TAG, "快捷方式创建失败:参数为空");
return false;
}
// Android 8.0+API 26+):使用 ShortcutManager系统推荐
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
try {
PackageManager pm = context.getPackageManager();
android.content.pm.ShortcutManager shortcutManager = context.getSystemService(android.content.pm.ShortcutManager.class);
if (shortcutManager == null || !shortcutManager.isRequestPinShortcutSupported()) {
LogUtils.d(TAG, "系统不支持创建快捷方式");
return false;
}
// 检查是否已存在该组件的快捷方式(去重)
for (android.content.pm.ShortcutInfo info : shortcutManager.getPinnedShortcuts()) {
if (component.getClassName().equals(info.getIntent().getComponent().getClassName())) {
LogUtils.d(TAG, "快捷方式已存在:" + component.getClassName());
return true;
}
}
// 构建启动目标组件的意图
Intent launchIntent = new Intent(Intent.ACTION_MAIN)
.setComponent(component)
.addCategory(Intent.CATEGORY_LAUNCHER)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
// 构建快捷方式信息
android.content.pm.ShortcutInfo shortcutInfo = new android.content.pm.ShortcutInfo.Builder(context, component.getClassName())
.setShortLabel(name)
.setLongLabel(name)
.setIcon(android.graphics.drawable.Icon.createWithResource(context, iconRes))
.setIntent(launchIntent)
.build();
// 请求创建快捷方式(需用户确认)
shortcutManager.requestPinShortcut(shortcutInfo, null);
return true;
} catch (Exception e) {
LogUtils.d(TAG, "Android O+ 快捷方式创建失败:" + e.getMessage());
return false;
}
} else {
// Android 8.0 以下:使用广播(兼容旧机型)
try {
// 构建启动目标组件的意图
Intent launchIntent = new Intent(Intent.ACTION_MAIN)
.setComponent(component)
.addCategory(Intent.CATEGORY_LAUNCHER)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
// 构建创建快捷方式的广播意图
Intent installIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
installIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
installIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
installIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(context, iconRes));
installIntent.putExtra("duplicate", false); // 禁止重复创建
context.sendBroadcast(installIntent);
return true;
} catch (Exception e) {
LogUtils.d(TAG, "Android O- 快捷方式创建失败:" + e.getMessage());
return false;
}
}
}
/**
* 启用组件(带状态检查,避免重复操作)
*/
private static void enableComponent(PackageManager pm, ComponentName component) {
if (pm.getComponentEnabledSetting(component) != PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
pm.setComponentEnabledSetting(
component,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP | PackageManager.SYNCHRONOUS
);
}
}
/**
* 禁用组件(带状态检查,避免重复操作)
*/
private static void disableComponent(PackageManager pm, ComponentName component) {
if (pm.getComponentEnabledSetting(component) != PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
pm.setComponentEnabledSetting(
component,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP | PackageManager.SYNCHRONOUS
);
}
}
}

View File

@@ -0,0 +1,148 @@
package cc.winboll.studio.positions.utils;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import cc.winboll.studio.libappbase.LogUtils;
/**
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2025/11/15 15:23
* @Describe Activity Alias 工具类(兼容 Android 所有版本Java 7 语法)
* 用于获取 activity-alias 对应的原始 Activity 组件名、判断 alias 类型、获取启动组件名等
*/
public class ActivityAliasUtils {
private static final String TAG = "ActivityAliasUtils";
/**
* 获取 activity-alias 指向的原始 Activity 组件名
*
* @param context 上下文(建议用 ApplicationContext
* @param aliasName activity-alias 的组件名(完整路径,如 ".AliasActivity" 或 "com.winboll.app.AliasActivity"
* @return 原始 Activity 的完整组件名(如 "com.winboll.app.OriginalActivity"),失败返回 null
*/
public static String getTargetActivityName(Context context, String aliasName) {
// 校验参数
if (context == null || TextUtils.isEmpty(aliasName)) {
LogUtils.e(TAG, "getTargetActivityName: context is null or aliasName is empty");
return null;
}
// 补全组件名(若传入的是短名,自动拼接包名)
String fullAliasName = aliasName.startsWith(".")
? context.getPackageName() + aliasName
: aliasName;
try {
// 1. 获取 PackageManager
PackageManager packageManager = context.getPackageManager();
// 2. 解析 activity-alias 的 ActivityInfoflag 必须设为 PackageManager.GET_META_DATA否则可能获取不到 targetActivity
ActivityInfo aliasActivityInfo = packageManager.getActivityInfo(
new android.content.ComponentName(context.getPackageName(), fullAliasName),
PackageManager.GET_META_DATA
);
// 3. 获取 targetActivity原始 Activity 组件名)
String targetActivity = aliasActivityInfo.targetActivity;
if (TextUtils.isEmpty(targetActivity)) {
LogUtils.e(TAG, "getTargetActivityName: targetActivity is empty for alias " + fullAliasName);
return null;
}
// 4. 补全原始 Activity 的完整包名(若 targetActivity 是短名)
String fullTargetName = targetActivity.startsWith(".")
? context.getPackageName() + targetActivity
: targetActivity;
LogUtils.d(TAG, "getTargetActivityName: alias=" + fullAliasName + ", target=" + fullTargetName);
return fullTargetName;
} catch (PackageManager.NameNotFoundException e) {
LogUtils.e(TAG, "getTargetActivityName: alias not found - " + fullAliasName, e);
} catch (Exception e) {
LogUtils.e(TAG, "getTargetActivityName: unknown error", e);
}
return null;
}
/**
* 判断某个组件名是否为 activity-alias而非原始 Activity
*
* @param context 上下文
* @param componentName 待判断的组件名(完整路径)
* @return true是 activity-aliasfalse不是或判断失败
*/
public static boolean isActivityAlias(Context context, String componentName) {
// 调用 getTargetActivityName若返回非空则说明是 alias
return !TextUtils.isEmpty(getTargetActivityName(context, componentName));
}
/**
* 从启动的 Intent 中获取实际的目标组件名(处理 alias 场景)
* 适用于 Activity 中获取自身真实组件名(原始 Activity 名)
*
* @param context 当前 Activity 上下文
* @return 真实的目标组件名(原始 Activity 名,若为 alias 启动则返回原始 Activity否则返回自身
*/
public static String getRealTargetNameFromIntent(Context context) {
if (context == null) {
LogUtils.e(TAG, "getRealTargetNameFromIntent: context is null");
return null;
}
// 获取当前 Activity 的组件名(可能是 alias
String currentComponentName = context.getClass().getName();
// 检查是否为 alias若是则返回 target否则返回自身
String targetName = getTargetActivityName(context, currentComponentName);
return TextUtils.isEmpty(targetName) ? currentComponentName : targetName;
}
/**
* 获取当前活动上下文Activity的启动组件名即启动时使用的组件名可能是 alias 或原始 Activity
* 场景:若通过 alias 启动 Activity返回 alias 名;若直接启动原始 Activity返回原始 Activity 名
*
* @param context 当前 Activity 上下文(必须是 Activity 实例,不能是 ApplicationContext
* @return 启动组件的完整名,失败返回 null
*/
public static String getLaunchComponentName(Context context) {
// 1. 校验上下文类型(必须是 Activity否则无法获取启动 Intent
if (context == null) {
LogUtils.e(TAG, "getLaunchComponentName: context is null");
return null;
}
if (!(context instanceof android.app.Activity)) {
LogUtils.e(TAG, "getLaunchComponentName: context must be Activity instance, current is " + context.getClass().getName());
return null;
}
try {
// 2. 获取启动当前 Activity 的 Intent
android.app.Activity activity = (android.app.Activity) context;
Intent launchIntent = activity.getIntent();
if (launchIntent == null) {
LogUtils.e(TAG, "getLaunchComponentName: launch Intent is null");
return null;
}
// 3. 从 Intent 中获取启动组件名ComponentName
android.content.ComponentName componentName = launchIntent.getComponent();
if (componentName == null) {
LogUtils.e(TAG, "getLaunchComponentName: ComponentName is null in launch Intent");
return null;
}
// 4. 获取组件的完整类名(即启动时使用的组件名)
String launchComponentName = componentName.getClassName();
LogUtils.d(TAG, "getLaunchComponentName: current launch component is " + launchComponentName);
return launchComponentName;
} catch (Exception e) {
LogUtils.e(TAG, "getLaunchComponentName: failed to get launch component name", e);
return null;
}
}
}

View File

@@ -1,6 +1,8 @@
package cc.winboll.studio.positions.utils;
import android.content.Context;
import cc.winboll.studio.positions.AppLevel;
import cc.winboll.studio.positions.models.AppConfigsModel;
import cc.winboll.studio.positions.App;
/**
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
@@ -58,11 +60,27 @@ public class AppConfigsUtil {
}
public void setIsEnableMainService(boolean isEnableMainService) {
if(mAppConfigsModel == null) {
if (mAppConfigsModel == null) {
mAppConfigsModel = new AppConfigsModel();
}
mAppConfigsModel.setIsEnableMainService(isEnableMainService);
saveConfigs();
}
public AppLevel getAppLevel(boolean isReloadConfigs) {
if (isReloadConfigs) {
loadConfigs();
}
return (mAppConfigsModel == null) ?AppLevel.WUKONG: mAppConfigsModel.getAppLevel();
}
public void setAppLevel(AppLevel appLevel) {
if (mAppConfigsModel == null) {
mAppConfigsModel = new AppConfigsModel();
}
App._mAppLevel = appLevel;
mAppConfigsModel.setAppLevel(appLevel);
saveConfigs();
}
}

View File

@@ -0,0 +1,241 @@
package cc.winboll.studio.positions.utils;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2025/11/13 15:42
* @Describe JsonShareHandler
* 外部 JSON 文件分享处理工具类
* 功能:接收外部分享的 .json 文件,弹出确认对话框,保存到外部存储 files/BaseBean 目录
*/
public class JsonShareHandler {
private static final String TAG = "JsonShareHandler";
private static final String TARGET_DIR = "BaseBean";
private static final String MIME_TYPE_JSON = "application/json";
private static final String FILE_SUFFIX_JSON = ".json";
// 对话框回调接口Java7 无 Lambda用接口实现
public interface ConfirmCallback {
void onConfirm(boolean isConfirm);
}
/**
* 处理外部分享的 Intent先弹出确认对话框再决定是否接收文件
* @param context 上下文(需为 Activity否则无法弹出对话框
* @param intent 分享 Intent
* @param callback 确认结果回调(用于 Activity 处理后续逻辑)
*/
public static void handleSharedJsonWithConfirm(final Context context, final Intent intent, final ConfirmCallback callback) {
if (context == null || intent == null || callback == null) {
Log.e(TAG, "参数为空,处理失败");
if (callback != null) callback.onConfirm(false);
return;
}
// 1. 先验证 Intent 合法性(提前过滤无效分享)
String action = intent.getAction();
String type = intent.getType();
if (!Intent.ACTION_SEND.equals(action) || type == null) {
Log.e(TAG, "非文件分享 Intent");
Toast.makeText(context, "不支持的分享类型", Toast.LENGTH_SHORT).show();
callback.onConfirm(false);
return;
}
// 2. 弹出确认对话框
new AlertDialog.Builder(context)
.setTitle("接收 JSON 文件")
.setMessage("是否接收并保存该 JSON 文件?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
// 3. 点击 Yes处理文件保存
String savedPath = handleSharedJsonFile(context, intent);
if (savedPath != null) {
Toast.makeText(context, "文件保存成功:" + savedPath, Toast.LENGTH_LONG).show();
callback.onConfirm(true);
} else {
Toast.makeText(context, "文件保存失败", Toast.LENGTH_SHORT).show();
callback.onConfirm(false);
}
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
// 4. 点击 No直接退出处理
callback.onConfirm(false);
}
})
.setCancelable(false) // 不可点击外部取消
.show();
}
/**
* 核心文件处理逻辑(原有功能,无修改)
*/
private static String handleSharedJsonFile(Context context, Intent intent) {
String action = intent.getAction();
String type = intent.getType();
// 验证 JSON 格式
if (!MIME_TYPE_JSON.equals(type) && !type.contains("json")) {
Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (uri == null || !getFileNameFromUri(context, uri).endsWith(FILE_SUFFIX_JSON)) {
Log.e(TAG, "接收的文件不是 JSON 格式");
return null;
}
}
Uri sharedUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (sharedUri == null) {
Log.e(TAG, "未获取到分享的文件 Uri");
return null;
}
try {
// 创建保存目录
File saveDir = getTargetSaveDir(context);
if (!saveDir.exists() && !saveDir.mkdirs()) {
Log.e(TAG, "创建保存目录失败:" + saveDir.getAbsolutePath());
return null;
}
// 解析文件名(兼容低版本)
String fileName = getFileNameFromUri(context, sharedUri);
if (fileName == null || !fileName.endsWith(FILE_SUFFIX_JSON)) {
fileName = "default_" + System.currentTimeMillis() + FILE_SUFFIX_JSON;
Log.w(TAG, "文件名解析失败,使用默认名称:" + fileName);
}
// 复制文件
File targetFile = new File(saveDir, fileName);
boolean copySuccess = copyFileFromUri(context, sharedUri, targetFile);
return copySuccess ? targetFile.getAbsolutePath() : null;
} catch (Exception e) {
Log.e(TAG, "处理分享文件异常:" + e.getMessage());
return null;
}
}
/**
* 获取目标保存目录(兼容 Android 10+ 分区存储)
*/
private static File getTargetSaveDir(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
return new File(context.getExternalFilesDir(null), TARGET_DIR);
} else {
return new File(
Environment.getExternalStorageDirectory() + File.separator +
"Android" + File.separator +
"data" + File.separator +
context.getPackageName() + File.separator +
"files" + File.separator +
TARGET_DIR
);
}
}
/**
* 从 Uri 解析文件名(兼容所有 Android 版本)
*/
private static String getFileNameFromUri(Context context, Uri uri) {
if (uri == null) return null;
// 1. 文件 Urifile:// 开头)
if ("file".equals(uri.getScheme())) {
return new File(uri.getPath()).getName();
}
// 2. 内容 Uricontent:// 开头)
if ("content".equals(uri.getScheme())) {
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(
uri,
new String[]{MediaStore.MediaColumns.DISPLAY_NAME},
null,
null,
null
);
if (cursor != null && cursor.moveToFirst()) {
int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
if (nameIndex != -1) {
return cursor.getString(nameIndex);
}
}
} catch (Exception e) {
Log.e(TAG, "解析内容 Uri 文件名失败:" + e.getMessage());
} finally {
if (cursor != null) cursor.close();
}
}
// 3. 解析失败,返回默认名称
String lastPathSegment = uri.getLastPathSegment();
return lastPathSegment != null ? lastPathSegment : "unknown.json";
}
/**
* 复制 Uri 指向的文件到目标路径
*/
private static boolean copyFileFromUri(Context context, Uri sourceUri, File targetFile) {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = context.getContentResolver().openInputStream(sourceUri);
if (inputStream == null) {
Log.e(TAG, "无法打开源文件输入流");
return false;
}
outputStream = new FileOutputStream(targetFile);
byte[] buffer = new byte[1024 * 4];
int length;
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
outputStream.flush();
Log.d(TAG, "文件保存成功:" + targetFile.getAbsolutePath());
return true;
} catch (IOException e) {
Log.e(TAG, "文件复制异常:" + e.getMessage());
return false;
} finally {
try {
if (inputStream != null) inputStream.close();
if (outputStream != null) outputStream.close();
} catch (IOException e) {
Log.e(TAG, "关闭流异常:" + e.getMessage());
}
}
}
/**
* 检查外部存储是否可用
*/
public static boolean isExternalStorageAvailable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state);
}
}

View File

@@ -0,0 +1,168 @@
package cc.winboll.studio.positions.utils;
/**
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2025/11/05 15:49
* @Describe LocalMotionDetector
*/
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import cc.winboll.studio.libappbase.LogUtils;
/**
* 本机运动状态监测工具(无联网,纯传感器)
*/
public class LocalMotionDetector implements SensorEventListener {
public static final String TAG = "LocalMotionDetector";
// 配置参数(重点修改:调高运动阈值,适配坐立持机场景)
private static final float MOTION_THRESHOLD = 1.8f; // 从0.5f调高到1.8f(过滤坐立轻微晃动)
private static final long STATUS_CHECK_INTERVAL = 3000; // 3秒判断一次状态
private static final int STEP_CHANGE_THRESHOLD = 2; // 3秒≥2步判定行走
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private Sensor mStepCounter;
private Handler mMainHandler;
private MotionStatusCallback mCallback;
private boolean mIsDetecting = false;
private float mLastAccelMagnitude = 0f;
private int mLastStepCount = 0;
private int mCurrentStepCount = 0;
private boolean mIsWalking = false;
// 单例模式
private static LocalMotionDetector sInstance;
public static LocalMotionDetector getInstance() {
if (sInstance == null) {
synchronized (LocalMotionDetector.class) {
if (sInstance == null) {
sInstance = new LocalMotionDetector();
}
}
}
return sInstance;
}
private LocalMotionDetector() {
mMainHandler = new Handler(Looper.getMainLooper());
}
/**
* 开始监测运动状态
*/
public void startDetection(Context context, MotionStatusCallback callback) {
if (mIsDetecting) return;
mCallback = callback;
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
// 初始化传感器
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mStepCounter = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
// 注册传感器监听
if (mAccelerometer != null) {
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL, mMainHandler);
}
if (mStepCounter != null) {
mSensorManager.registerListener(this, mStepCounter, SensorManager.SENSOR_DELAY_NORMAL, mMainHandler);
LogUtils.d(TAG, "计步传感器已启动");
} else {
LogUtils.d(TAG, "设备不支持计步传感器,仅用加速度判断");
}
// 启动定时状态检测
mMainHandler.postDelayed(mStatusCheckRunnable, STATUS_CHECK_INTERVAL);
mIsDetecting = true;
LogUtils.d(TAG, "运动状态监测已启动");
}
/**
* 停止监测
*/
public void stopDetection() {
if (!mIsDetecting) return;
if (mSensorManager != null) {
mSensorManager.unregisterListener(this);
}
mMainHandler.removeCallbacksAndMessages(null);
mIsDetecting = false;
mIsWalking = false;
mCallback = null;
LogUtils.d(TAG, "运动状态监测已停止");
}
@Override
public void onSensorChanged(SensorEvent event) {
if (!mIsDetecting) return;
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
// 计算加速度幅度(保留原逻辑,阈值已调高)
float accelX = Math.abs(event.values[0]);
float accelY = Math.abs(event.values[1]);
float accelZ = Math.abs(event.values[2]);
mLastAccelMagnitude = accelX + accelY + accelZ;
break;
case Sensor.TYPE_STEP_COUNTER:
// 累计步数
mCurrentStepCount = (int) event.values[0];
break;
}
}
/**
* 定时判断运动状态优化逻辑计步为0时即使有轻微加速度也判定为静止
*/
private final Runnable mStatusCheckRunnable = new Runnable() {
@Override
public void run() {
if (!mIsDetecting || mCallback == null) return;
//LogUtils.d(TAG, "mStatusCheckRunnable run");
boolean newIsWalking = false;
// 结合计步器+加速度判断(优化:优先计步,无步数时严格按高阈值判断)
if (mStepCounter != null) {
int stepChange = mCurrentStepCount - mLastStepCount;
// 只有“步数达标” 或 “无步数但加速度远超坐立幅度”,才判定为行走
newIsWalking = (stepChange >= STEP_CHANGE_THRESHOLD)
&& (mLastAccelMagnitude >= MOTION_THRESHOLD); // 增加步数+加速度双重校验
mLastStepCount = mCurrentStepCount;
} else {
// 无计步器时,仅用高阈值判断
newIsWalking = mLastAccelMagnitude >= MOTION_THRESHOLD;
}
// 状态变化时回调
if (newIsWalking != mIsWalking) {
mIsWalking = newIsWalking;
String statusDesc = mIsWalking ? "行走状态" : "静止/低运动状态";
LogUtils.d(TAG, "运动状态变化:" + statusDesc + " | 加速度幅度:" + mLastAccelMagnitude); // 增加日志便于调试
mCallback.onMotionStatusChanged(mIsWalking, statusDesc);
}
LogUtils.d(TAG, String.format("运动状态 newIsWalking %s", newIsWalking));
// 循环检测
mMainHandler.postDelayed(this, STATUS_CHECK_INTERVAL);
}
};
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
/**
* 运动状态回调接口
*/
public interface MotionStatusCallback {
void onMotionStatusChanged(boolean isWalking, String statusDesc);
}
}

View File

@@ -0,0 +1,105 @@
package cc.winboll.studio.positions.utils;
import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.ToastUtils;
import cc.winboll.studio.positions.PointLevel;
import cc.winboll.studio.positions.activities.WinBoLLActivity;
/**
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2025/11/15 15:59
* @Describe 应用活动窗口状态响应类
* 主要用于设置应用级别与组件状态
*/
public class MyActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {
public static final String TAG = "MyActivityLifecycleCallbacks";
public String mInfo = "";
public MyActivityLifecycleCallbacks() {
}
void createActivityeInfo(Activity activity) {
StringBuilder sb = new StringBuilder();
Intent receivedIntent = activity.getIntent();
sb.append("\nCallingActivity : \n");
if (activity.getCallingActivity() != null) {
sb.append(activity.getCallingActivity().getPackageName());
}
sb.append("\nReceived Intent Package : \n");
sb.append(receivedIntent.getPackage());
Bundle extras = receivedIntent.getExtras();
if (extras != null) {
for (String key : extras.keySet()) {
sb.append("\nIntentInfo");
sb.append("\n键: ");
sb.append(key);
sb.append(", 值: ");
sb.append(extras.get(key));
//Log.d("IntentInfo", "键: " + key + ", 值: " + extras.get(key));
}
}
mInfo = sb.toString();
//Log.d("IntentInfo", "发送Intent的应用包名: " + senderPackage);
}
public void showActivityeInfo() {
//ToastUtils.show("ActivityeInfo : " + mInfo);
LogUtils.d(TAG, "ActivityeInfo : " + mInfo);
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
// 在这里可以做一些初始化相关的操作例如记录Activity的创建时间等
//System.out.println(activity.getLocalClassName() + " was created");
LogUtils.d(TAG, activity.getLocalClassName() + " was created");
createActivityeInfo(activity);
}
@Override
public void onActivityStarted(Activity activity) {
//System.out.println(activity.getLocalClassName() + " was started");
LogUtils.d(TAG, activity.getLocalClassName() + " was started");
//createActivityeInfo(activity);
}
@Override
public void onActivityResumed(Activity activity) {
//System.out.println(activity.getLocalClassName() + " was resumed");
LogUtils.d(TAG, activity.getLocalClassName() + " was resumed");
//createActivityeInfo(activity);
}
@Override
public void onActivityPaused(Activity activity) {
ToastUtils.show("Activity Paused");
// 应用从正在活动状态抽离出来时设置应用入口级别状态设置为时空虚幻而不确定的哆啦A梦级别。
WinBoLLActivity._mPointLevel = PointLevel.DORAEMON;
//System.out.println(activity.getLocalClassName() + " was paused");
LogUtils.d(TAG, activity.getLocalClassName() + " was paused");
}
@Override
public void onActivityStopped(Activity activity) {
//System.out.println(activity.getLocalClassName() + " was stopped");
LogUtils.d(TAG, activity.getLocalClassName() + " was stopped");
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
// 可以在这里添加保存状态的自定义逻辑
}
@Override
public void onActivityDestroyed(Activity activity) {
//System.out.println(activity.getLocalClassName() + " was destroyed");
LogUtils.d(TAG, activity.getLocalClassName() + " was destroyed");
}
}

View File

@@ -0,0 +1,282 @@
package cc.winboll.studio.positions.views;
/**
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2025/11/10 08:29
* @Describe 沙漏计时器控件
*/
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.text.InputFilter;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Switch;
import android.widget.TextView;
/**
* 沙漏视图类Java 7语法修复ProgressDrawable和setHeight问题
*/
public class HourglassView extends LinearLayout {
public static final String TAG = "HourglassView";
// 数据模型
private String hourglassId;
private int hour; // 小时
private int minute; // 分钟
private boolean isEnabled; // 开关状态
// 控件引用
private EditText etHour;
private EditText etMinute;
private ProgressBar progressBar;
private Switch switchControl;
// 样式参数
private int textSize = 16;
private int padding = 8;
private int progressColor = 0xFF2196F3; // 进度条颜色
private int progressBgColor = 0xFFE0E0E0; // 进度条背景色
private int textColor = 0xFF333333;
private int editTextWidth = 40; // 输入框宽度dp
private int progressHeight = 8; // 进度条高度dp新增参数
public HourglassView(Context context) {
super(context);
initView();
}
public HourglassView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
/**
* 初始化视图布局
*/
private void initView() {
setOrientation(HORIZONTAL);
setGravity(Gravity.CENTER_VERTICAL);
setPadding(dp2px(padding), dp2px(padding), dp2px(padding), dp2px(padding));
// 1. 左侧时间输入区域(水平布局)
LinearLayout inputLayout = new LinearLayout(getContext());
inputLayout.setOrientation(HORIZONTAL);
inputLayout.setGravity(Gravity.CENTER_VERTICAL);
LayoutParams inputParams = new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
inputParams.setMargins(0, 0, dp2px(padding * 2), 0);
addView(inputLayout, inputParams);
// 小时输入框
etHour = createNumberEditText();
etHour.setHint("");
etHour.setFilters(new InputFilter[]{new InputFilter.LengthFilter(2)});
inputLayout.addView(etHour, getEditTextParams());
// 分隔符
TextView divider = new TextView(getContext());
divider.setText(":");
divider.setTextSize(textSize);
divider.setTextColor(textColor);
LayoutParams dividerParams = new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
dividerParams.setMargins(dp2px(padding / 2), 0, dp2px(padding / 2), 0);
inputLayout.addView(divider, dividerParams);
// 分钟输入框
etMinute = createNumberEditText();
etMinute.setHint("");
etMinute.setFilters(new InputFilter[]{new InputFilter.LengthFilter(2)});
inputLayout.addView(etMinute, getEditTextParams());
// 2. 中间进度条修复通过LayoutParams设置高度替代setHeight
progressBar = new ProgressBar(getContext(), null, android.R.attr.progressBarStyleHorizontal);
progressBar.setProgressDrawable(createProgressDrawable()); // 传入Drawable类型
// 修复核心用LayoutParams设置进度条高度兼容低版本
LayoutParams progressParams = new LayoutParams(
0,
dp2px(progressHeight), // 直接在布局参数中设置高度dp转px
1.0f
);
progressParams.setMargins(0, 0, dp2px(padding * 2), 0);
addView(progressBar, progressParams);
// 3. 右侧开关
switchControl = new Switch(getContext());
switchControl.setOnCheckedChangeListener(new Switch.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(android.widget.CompoundButton buttonView, boolean isChecked) {
isEnabled = isChecked;
// 开关状态控制输入框是否可编辑
etHour.setEnabled(!isChecked);
etMinute.setEnabled(!isChecked);
// 更新进度条(仅在开关开启时生效)
if (isChecked) {
updateProgressBar();
}
}
});
addView(switchControl);
// 初始状态
isEnabled = false;
etHour.setEnabled(true);
etMinute.setEnabled(true);
}
/**
* 创建数字输入框
*/
private EditText createNumberEditText() {
EditText editText = new EditText(getContext());
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
editText.setTextSize(textSize);
editText.setTextColor(textColor);
editText.setGravity(Gravity.CENTER);
editText.setSingleLine(true);
editText.setBackgroundResource(android.R.drawable.edit_text); // 默认输入框背景
return editText;
}
/**
* 获取输入框布局参数
*/
private LayoutParams getEditTextParams() {
LayoutParams params = new LayoutParams(
dp2px(editTextWidth),
ViewGroup.LayoutParams.WRAP_CONTENT
);
params.setMargins(0, 0, dp2px(padding), 0);
return params;
}
/**
* 修复核心创建ProgressDrawable返回Drawable类型而非Paint
* 用LayerDrawable实现「背景+进度」的双层进度条
*/
private Drawable createProgressDrawable() {
// 1. 进度条背景(灰色)
ColorDrawable bgDrawable = new ColorDrawable(progressBgColor);
// 2. 进度条前景(主题色)
ColorDrawable progressDrawable = new ColorDrawable(progressColor);
// 3. 用ClipDrawable包裹前景实现进度裁剪
ClipDrawable clipDrawable = new ClipDrawable(progressDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
// 4. 组合成LayerDrawable顺序背景在下进度在上
Drawable[] layers = new Drawable[]{bgDrawable, clipDrawable};
LayerDrawable layerDrawable = new LayerDrawable(layers);
// 5. 设置进度条的层级ID必须与系统ProgressBar的ID匹配
layerDrawable.setId(0, android.R.id.background);
layerDrawable.setId(1, android.R.id.progress);
return layerDrawable;
}
/**
* 更新进度条(总时间 = 小时*60 + 分钟,单位:分钟)
*/
private void updateProgressBar() {
try {
// 获取输入的时间为空时默认0
int inputHour = TextUtils.isEmpty(etHour.getText().toString().trim())
? 0 : Integer.parseInt(etHour.getText().toString().trim());
int inputMinute = TextUtils.isEmpty(etMinute.getText().toString().trim())
? 0 : Integer.parseInt(etMinute.getText().toString().trim());
// 校验时间合法性小时0-99分钟0-59
inputHour = Math.max(0, Math.min(99, inputHour));
inputMinute = Math.max(0, Math.min(59, inputMinute));
// 计算总分钟数(进度条最大值)
int totalMinutes = inputHour * 60 + inputMinute;
totalMinutes = Math.max(1, totalMinutes); // 最小1分钟避免进度条无长度
// 更新进度条
progressBar.setMax(totalMinutes);
progressBar.setProgress(totalMinutes); // 初始显示满进度,可根据实际需求修改
// 更新数据模型
this.hour = inputHour;
this.minute = inputMinute;
} catch (NumberFormatException e) {
// 输入非法时重置进度条
progressBar.setMax(0);
progressBar.setProgress(0);
}
}
/**
* dp转px适配不同设备
*/
private int dp2px(int dp) {
return (int) (dp * getContext().getResources().getDisplayMetrics().density + 0.5f);
}
// ------------------- 数据模型 getter/setter -------------------
public String getHourglassId() {
return hourglassId;
}
public void setHourglassId(String hourglassId) {
this.hourglassId = hourglassId;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = Math.max(0, Math.min(99, hour)); // 限制范围
etHour.setText(String.valueOf(this.hour));
}
public int getMinute() {
return minute;
}
public void setMinute(int minute) {
this.minute = Math.max(0, Math.min(59, minute)); // 限制范围
etMinute.setText(String.valueOf(this.minute));
}
public boolean isEnabled() {
return isEnabled;
}
public void setEnabled(boolean enabled) {
isEnabled = enabled;
switchControl.setChecked(enabled);
}
/**
* 手动更新进度条(外部调用)
*/
public void refreshProgress() {
if (isEnabled) {
updateProgressBar();
}
}
// 工具类判断字符串是否为空Java7无TextUtils.isEmpty手动实现
private static class TextUtils {
public static boolean isEmpty(CharSequence str) {
return str == null || str.length() == 0;
}
}
}

View File

@@ -33,6 +33,10 @@ import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import cc.winboll.studio.positions.App;
import cc.winboll.studio.positions.AppLevel;
import cc.winboll.studio.positions.activities.WinBoLLActivity;
import cc.winboll.studio.positions.PointLevel;
public class PositionTaskListView extends LinearLayout {
// 视图模式常量
@@ -380,7 +384,7 @@ public class PositionTaskListView extends LinearLayout {
// 步骤3刷新Adapter局部刷新+范围通知,避免列表错乱)
notifyItemRemoved(position);
notifyItemRangeChanged(position, mAdapterData.size());
LogUtils.d(TAG, "Adapter已移除任务刷新列表位置索引=" + position + "");
// 步骤4通知外部如Activity任务已更新
@@ -457,7 +461,7 @@ public class PositionTaskListView extends LinearLayout {
}
});
}
private String genSelectedTimeText(long timeMillis) {
// 2. 格式化时间字符串Java 7 用 SimpleDateFormat需处理 ParseException
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
@@ -483,6 +487,16 @@ public class PositionTaskListView extends LinearLayout {
final EditText etEditDistance = dialogView.findViewById(R.id.et_edit_distance);
Button btnCancel = dialogView.findViewById(R.id.btn_dialog_cancel);
Button btnSave = dialogView.findViewById(R.id.btn_dialog_save);
HourglassView hourglassView = dialogView.findViewById(R.id.hourglassView);
if (WinBoLLActivity._mPointLevel == PointLevel.WUKONG) {
hourglassView.setVisibility(View.GONE);
} else if (WinBoLLActivity._mPointLevel == PointLevel.LAOJUN) {
hourglassView.setHourglassId("hourglass_001");
hourglassView.setHour(1);
hourglassView.setMinute(30);
hourglassView.setEnabled(false); // 开启开关
}
// 绑定外层对话框内的控件

View File

@@ -1,81 +1,67 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:padding="20dp">
<cc.winboll.studio.libaes.views.ASupportToolbar
android:layout_width="match_parent"
android:layout_height="@dimen/toolbar_height"
android:id="@+id/toolbar"
android:gravity="center_vertical"/>
<LinearLayout
android:id="@+id/layout_location_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:orientation="vertical"
android:gravity="center_horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:padding="20dp"
android:layout_weight="1.0">
<LinearLayout
android:id="@+id/layout_location_info"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:orientation="vertical"
android:gravity="center_horizontal">
android:text="实时位置信息"
android:textSize="22sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="实时位置信息"
android:textSize="22sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/tv_longitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="当前经度:等待更新..."
android:textSize="18sp"
android:layout_marginTop="15dp"/>
<TextView
android:id="@+id/tv_longitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="当前度:等待更新..."
android:textSize="18sp"
android:layout_marginTop="15dp"/>
<TextView
android:id="@+id/tv_latitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="当前度:等待更新..."
android:textSize="18sp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/tv_latitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="当前纬度:等待更新..."
android:textSize="18sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_position_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/layout_location_info"
android:layout_above="@id/fab_p_button"
android:layout_marginTop="20dp"
android:paddingBottom="10dp"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_position_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/layout_location_info"
android:layout_above="@id/fab_p_button"
android:layout_marginTop="20dp"
android:paddingBottom="10dp"/>
<Button
android:id="@+id/fab_p_button"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="20dp"
android:background="@drawable/circle_button_bg"
android:text="P"
android:textColor="@android:color/white"
android:textSize="24sp"
android:elevation="6dp"
android:padding="0dp"
android:onClick="addNewPosition"/>
<Button
android:id="@+id/fab_p_button"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="20dp"
android:background="@drawable/circle_button_bg"
android:text="P"
android:textColor="@android:color/white"
android:textSize="24sp"
android:elevation="6dp"
android:padding="0dp"
android:onClick="addNewPosition"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>

View File

@@ -2,51 +2,38 @@
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:orientation="vertical">
<cc.winboll.studio.libaes.views.ASupportToolbar
android:layout_width="match_parent"
android:layout_height="@dimen/toolbar_height"
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:gravity="center_vertical"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="1.0">
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<Switch
android:id="@+id/switch_service_control"
android:layout_margin="16dp"
android:text="GPS服务开关"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Switch
android:id="@+id/switch_service_control"
android:layout_margin="16dp"
android:text="GPS服务开关"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:onClick="onPositions"
android:text="位置与任务管理"
android:id="@+id/btn_manage_positions"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:onClick="onLog"
android:text="查看应用日志"/>
</LinearLayout>
<cc.winboll.studio.libaes.views.ADsBannerView
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/adsbanner"
android:layout_alignParentBottom="true"/>
android:layout_margin="16dp"
android:onClick="onPositions"
android:text="位置与任务管理"
android:id="@+id/btn_manage_positions"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:onClick="onLog"
android:text="查看应用日志"/>
</LinearLayout>

View File

@@ -1,21 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<cc.winboll.studio.libaes.views.ASupportToolbar
android:layout_width="match_parent"
android:layout_height="@dimen/toolbar_height"
android:id="@+id/toolbar"
android:gravity="center_vertical"/>
<cc.winboll.studio.libaes.views.ADsControlView
android:id="@+id/ads_control_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

View File

@@ -65,6 +65,16 @@
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<cc.winboll.studio.positions.views.HourglassView
android:id="@+id/hourglassView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
@@ -77,7 +87,7 @@
android:layout_height="wrap_content"
android:text="开始时间"
android:id="@+id/btn_select_time"/>
<TextView
android:id="@+id/tv_selected_time"
android:layout_width="0dp"
@@ -85,7 +95,6 @@
android:text="Text"
android:layout_weight="1.0"/>
</LinearLayout>
<LinearLayout

View File

@@ -1,9 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/item_settings"
android:title="Settings"/>
</menu>

View File

@@ -1,4 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">悟空笔记</string>
<string name="appplus_name">时空任务</string>
<string name="open_appplus">开疆扩土</string>
<string name="close_appplus">返璞归真</string>
<string name="appplus_open_disabled">余力不足</string>
<string name="appplus_close_disabled">辎重难返</string>
</resources>

View File

@@ -1,7 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="toolbar_height">60dp</dimen>
<dimen name="text_content_size">18dp</dimen>
<dimen name="text_title_size">24dp</dimen>
<dimen name="text_subtitle_size">16dp</dimen>
</resources>

View File

@@ -1,9 +1,8 @@
<resources>
<string name="app_name">Positions</string>
<string name="appplus_name">PositionsPlus</string>
<string name="appplus_name">PositionsPlus</string>
<string name="open_appplus">Open APP Plus</string>
<string name="close_appplus">Close APP Plus</string>
<string name="appplus_open_disabled">APP Plus Open Disable</string>
<string name="appplus_close_disabled">APP Plus Close Disable</string>
</resources>

View File

@@ -14,12 +14,4 @@
</style>
<!-- 设置Toolbar标题字体的大小 -->
<style name="Toolbar.TitleText" parent="@android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Title">
<item name="android:textSize">@dimen/text_title_size</item>
</style>
<style name="Toolbar.SubTitleText" parent="@android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Title">
<item name="android:textSize">@dimen/text_subtitle_size</item>
</style>
</resources>

View File

@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path
name="BaseBean"
path="BaseBean/" />
</paths>

View File

@@ -19,21 +19,21 @@ def genVersionName(def versionName){
android {
// 1. compileSdkVersion必须 ≥ targetSdkVersion建议直接等于 targetSdkVersion30
compileSdkVersion 30
// 2. buildToolsVersion需匹配 compileSdkVersion建议使用 30.x.x 最新稳定版(无需高于 compileSdkVersion
buildToolsVersion "30.0.3" // 这是 30 对应的最新稳定版,避免使用 beta 版
// 关键:改为你已安装的 SDK 32≥ targetSdkVersion 30兼容已安装环境
compileSdkVersion 32
// 直接使用已安装的构建工具 33.0.3(无需修改)
buildToolsVersion "33.0.3"
defaultConfig {
applicationId "cc.winboll.studio.powerbell"
minSdkVersion 23
targetSdkVersion 30
versionCode 6
versionCode 7
// versionName 更新后需要手动设置
// .winboll/winbollBuildProps.properties 文件的 stageCount=0
// Gradle编译环境下合起来的 versionName 就是 "${versionName}.0"
versionName "15.11"
versionName "15.14"
if(true) {
versionName = genVersionName("${versionName}")
}
@@ -56,7 +56,12 @@ dependencies {
api 'com.github.bumptech.glide:glide:4.9.0'
//annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
// uCrop 核心依赖(最新稳定版)
implementation 'com.github.yalantis:ucrop:2.2.8'
// 兼容AndroidX若项目用AndroidX必须添加
//implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.exifinterface:exifinterface:1.3.6'
// 应用介绍页类库
api 'io.github.medyo:android-about-page:2.0.0'
// SSH
@@ -77,8 +82,13 @@ dependencies {
//api 'androidx.vectordrawable:vectordrawable-animated:1.1.0'
//api 'androidx.fragment:fragment:1.1.0'
implementation 'cc.winboll.studio:libaes:15.11.6'
implementation 'cc.winboll.studio:libappbase:15.11.0'
// WinBoLL库 nexus.winboll.cc 地址
api 'cc.winboll.studio:libaes:15.12.13'
api 'cc.winboll.studio:libappbase:15.14.2'
// WinBoLL备用库 jitpack.io 地址
//api 'com.github.ZhanGSKen:AES:aes-v15.12.9'
//api 'com.github.ZhanGSKen:APPBase:appbase-v15.14.1'
//api fileTree(dir: 'libs', include: ['*.aar'])
api fileTree(dir: 'libs', include: ['*.jar'])

View File

@@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle
#Wed Nov 26 16:27:33 HKT 2025
stageCount=9
#Wed Jan 07 18:09:34 HKT 2026
stageCount=50
libraryProject=
baseVersion=15.11
publishVersion=15.11.8
baseVersion=15.14
publishVersion=15.14.49
buildCount=0
baseBetaVersion=15.11.9
baseBetaVersion=15.14.50

View File

@@ -0,0 +1,279 @@
#!/bin/bash
# PowerBell软著版本号快速修改+生成脚本
# 无需手动改主脚本,输入版本号直接运行
# 颜色输出函数
red_echo() { echo -e "\033[31m$1\033[0m"; }
green_echo() { echo -e "\033[32m$1\033[0m"; }
blue_echo() { echo -e "\033[34m$1\033[0m"; }
# 1. 提示用户输入新版本号
blue_echo "==== 请输入软著版本号格式示例V15、V15.0.1 ===="
read -p "输入版本号:" NEW_VERSION
# 校验版本号格式(避免特殊符号)
if [[ ! $NEW_VERSION =~ ^V[0-9]+(\.[0-9]+)*$ ]]; then
red_echo "错误版本号格式无效请遵循「V+数字」格式如V15、V15.0.1),不含特殊符号"
exit 1
fi
# 2. 定义固定配置(仅需修改这里的著作权人,其他无需动)
SOFTWARE_NAME="PowerBell"
COPYRIGHT_OWNER="张绍建陆丰东海镇云宝软件开发工作室"
LINES_PER_PAGE=55
# 3. 生成主脚本(自动替换新版本号)
blue_echo -e "\n==== 生成${NEW_VERSION}版本主脚本 ===="
cat > build_copyright_pdf_temp.sh << EOF
#!/bin/bash
# PowerBell软著PDF生成脚本版本$NEW_VERSION
red_echo() { echo -e "\033[31m\$1\033[0m"; }
green_echo() { echo -e "\033[32m\$1\033[0m"; }
blue_echo() { echo -e "\033[34m\$1\033[0m"; }
# 配置项(已自动替换为${NEW_VERSION}
SOFTWARE_NAME="$SOFTWARE_NAME"
SOFTWARE_VERSION="$NEW_VERSION"
COPYRIGHT_OWNER="$COPYRIGHT_OWNER"
LINES_PER_PAGE=$LINES_PER_PAGE
# 步骤1检查依赖
blue_echo "==== 1/7 检查并安装依赖 ===="
sudo apt update > /dev/null 2>&1
REQUIRED_PKGS=("python3" "wkhtmltopdf" "fonts-wqy-microhei" "pdftk" "poppler-utils")
for pkg in "\${REQUIRED_PKGS[@]}"; do
if ! dpkg -s "\$pkg" > /dev/null 2>&1; then
green_echo "安装依赖:\$pkg"
sudo apt install -y "\$pkg" > /dev/null 2>&1
fi
done
# 步骤2生成纯文本源码
blue_echo -e "\n==== 2/7 生成纯文本核心源码 ===="
cat > generate_source.py << GEN_EOF
import os
PROJECT_PATH = "./"
OUTPUT_TXT = "PowerBell_Core_Source.txt"
INCLUDE_EXT = [".java", ".kt"]
EXCLUDE_DIRS = ["build", "libs", "test", "androidTest", ".git", ".idea", "gradle", "unittest"]
MIN_LINE_COUNT = 3
SOFTWARE_NAME = "$SOFTWARE_NAME"
SOFTWARE_VERSION = "$NEW_VERSION"
COPYRIGHT_OWNER = "$COPYRIGHT_OWNER"
def clean_text(text):
return ''.join(c for c in text if c.isprintable() or c in "\\n\\r\\t")
def generate_source_txt():
valid_files = []
main_dir = os.path.join(PROJECT_PATH, "src", "main")
if not os.path.exists(main_dir):
print("Error: src/main directory not found!")
return
for root, dirs, files in os.walk(main_dir):
dirs[:] = [d for d in dirs if d not in EXCLUDE_DIRS]
for file in files:
if os.path.splitext(file)[1] in INCLUDE_EXT:
file_path = os.path.join(root, file)
try:
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
lines = f.readlines()
code_lines = [l for l in lines if l.strip() and not l.strip().startswith("//")]
if len(code_lines) >= MIN_LINE_COUNT:
valid_files.append(file_path)
except:
continue
valid_files.sort(key=lambda x: os.path.getsize(x), reverse=True)
with open(OUTPUT_TXT, "w", encoding="utf-8-sig") as f:
f.write(f"\{SOFTWARE_NAME} \{SOFTWARE_VERSION} 核心源码 - 著作权人:\{COPYRIGHT_OWNER}\\n\\n")
for idx, file_path in enumerate(valid_files, 1):
f.write(f"\\n{'='*60}\\n")
f.write(f"文件 \{idx}\{file_path.replace(PROJECT_PATH, '')}\\n")
f.write(f"{'='*60}\\n\\n")
try:
try:
with open(file_path, "r", encoding="utf-8") as src_f:
content = clean_text(src_f.read())
except UnicodeDecodeError:
with open(file_path, "r", encoding="gbk") as src_f:
content = clean_text(src_f.read())
f.write(content)
f.write("\\n\\n")
except Exception as e:
f.write(f"文件读取失败:\{str(e)}\\n\\n")
continue
print(f"有效源码文件数:\{len(valid_files)}")
print(f"纯文本文件路径:\{os.path.abspath(OUTPUT_TXT)}")
if __name__ == "__main__":
generate_source_txt()
GEN_EOF
python3 generate_source.py
if [ ! -f "PowerBell_Core_Source.txt" ]; then
red_echo "纯文本源码生成失败!"
exit 1
fi
# 步骤3生成带版本号页眉的HTML
blue_echo -e "\n==== 3/7 生成带${NEW_VERSION}页眉的HTML ===="
cat > txt2html.py << TXT_EOF
import os
TXT_FILE = "PowerBell_Core_Source.txt"
HTML_FILE = "PowerBell_Source.html"
SOFTWARE_NAME = "$SOFTWARE_NAME"
SOFTWARE_VERSION = "$NEW_VERSION"
COPYRIGHT_OWNER = "$COPYRIGHT_OWNER"
LINES_PER_PAGE = $LINES_PER_PAGE
CSS_STYLE = """
<style>
@page {{
size: A4;
margin: 10mm 5mm;
@top-center {{
content: "{} {} - 源代码(著作权人:{}";
font-family: 'WenQuanYi Micro Hei', monospace;
font-size: 9pt;
font-weight: bold;
}}
@bottom-center {{
content: "页码 " counter(page) " / " counter(pages);
font-family: 'WenQuanYi Micro Hei', monospace;
font-size: 9pt;
}}
}}
body {{
font-family: 'WenQuanYi Micro Hei', monospace;
font-size: 9pt;
line-height: 1.1;
margin: 0;
padding: 5mm 0 0 0;
counter-reset: code-line;
}}
.file-header {{
background: #f0f0f0;
padding: 3px;
margin: 6px 0;
font-weight: bold;
font-size: 10pt;
}}
.code-block {{
white-space: pre;
margin-left: 8px;
line-height: 1.1;
counter-increment: code-line;
}}
.code-block:before {{
content: counter(code-line) " ";
color: #888;
display: inline-block;
width: 30px;
text-align: right;
margin-right: 5px;
}}
.page-break {{ page-break-after: always; counter-reset: code-line; }}
</style>
""".format(SOFTWARE_NAME, SOFTWARE_VERSION, COPYRIGHT_OWNER)
def txt_to_html():
with open(TXT_FILE, "r", encoding="utf-8") as f:
content = f.read()
html_content = "<!DOCTYPE html><html><head><meta charset='utf-8'>" + CSS_STYLE + "</head><body>"
content_lines = content.split("\\n")[2:]
content_clean = "\\n".join(content_lines)
blocks = content_clean.split("====")
line_count = 0
for block in blocks:
if not block.strip():
continue
if "文件 " in block and ":" in block:
file_header = block.split("\\n")[0].strip() if "\\n" in block else block.strip()
html_content += f"<div class='file-header'>\{file_header}</div>"
code_part = block.split("\\n")[1:] if "\\n" in block else []
block = "\\n".join(code_part)
code_lines = block.split("\\n")
for line in code_lines:
if line.strip() or line_count > 0:
line_count += 1
html_content += f"<div class='code-block'>\{line}</div>"
if line_count >= LINES_PER_PAGE:
html_content += "<div class='page-break'></div>"
line_count = 0
html_content += "</body></html>"
with open(HTML_FILE, "w", encoding="utf-8") as f:
f.write(html_content)
print(f"HTML文件路径\{os.path.abspath(HTML_FILE)}")
if __name__ == "__main__":
txt_to_html()
TXT_EOF
python3 txt2html.py
if [ ! -f "PowerBell_Source.html" ]; then
red_echo "HTML文件生成失败"
exit 1
fi
# 步骤4生成完整PDF
blue_echo -e "\n==== 4/7 生成完整PDF版本${NEW_VERSION} ===="
wkhtmltopdf --page-size A4 \
--margin-top 15mm --margin-bottom 15mm --margin-left 5mm --margin-right 5mm \
--encoding utf-8 \
--no-images --disable-javascript \
--enable-local-file-access \
--no-stop-slow-scripts \
PowerBell_Source.html PowerBell_soft_full.pdf
if [ ! -f "PowerBell_soft_full.pdf" ]; then
red_echo "完整PDF生成失败"
exit 1
fi
# 步骤5截取60页
blue_echo -e "\n==== 5/7 截取前30+后30页 ===="
TOTAL_PAGES=\$(pdfinfo PowerBell_soft_full.pdf | grep "Pages" | awk '{print \$2}')
green_echo "源码完整PDF总页数\$TOTAL_PAGES 页"
if [ "\$TOTAL_PAGES" -le 60 ]; then
cp PowerBell_soft_full.pdf PowerBell_软著源码_${NEW_VERSION}_60页.pdf
green_echo "源码不足60页直接使用完整PDF"
else
pdftk PowerBell_soft_full.pdf cat 1-30 output PowerBell_前30页.pdf
START_PAGE=\$((TOTAL_PAGES - 29))
pdftk PowerBell_soft_full.pdf cat \$START_PAGE-\$TOTAL_PAGES output PowerBell_后30页.pdf
pdftk PowerBell_前30页.pdf PowerBell_后30页.pdf cat output PowerBell_软著源码_${NEW_VERSION}_60页.pdf
rm -f PowerBell_前30页.pdf PowerBell_后30页.pdf
green_echo "源码超过60页已截取前30页+后30页合并为60页"
fi
# 步骤6验证规范
blue_echo -e "\n==== 6/7 验证${NEW_VERSION}版本PDF规范 ===="
FINAL_PAGES=\$(pdfinfo PowerBell_软著源码_${NEW_VERSION}_60页.pdf | grep "Pages" | awk '{print \$2}')
green_echo "最终PDF页数\$FINAL_PAGES 页"
green_echo "每页代码行数:\$LINES_PER_PAGE 行≥50行"
green_echo "页眉信息:$SOFTWARE_NAME $NEW_VERSION - 源代码(著作权人:$COPYRIGHT_OWNER"
# 步骤7清理临时文件
blue_echo -e "\n==== 7/7 清理临时文件 ===="
rm -f generate_source.py txt2html.py PowerBell_Core_Source.txt PowerBell_Source.html PowerBell_soft_full.pdf
green_echo "临时文件清理完成!"
# 输出结果
green_echo -e "\n====================================="
green_echo "$SOFTWARE_NAME $NEW_VERSION 软著PDF生成成功🎉"
green_echo "📄 最终文件:\$(pwd)/PowerBell_软著源码_${NEW_VERSION}_60页.pdf"
green_echo "💡 可直接提交软著登记,无需手动修改!"
green_echo "====================================="
EOF
# 4. 赋予执行权限并运行
chmod +x build_copyright_pdf_temp.sh
blue_echo -e "\n==== 开始生成${NEW_VERSION}版本PDF ===="
./build_copyright_pdf_temp.sh
# 5. 删除临时主脚本(可选,保留则注释此行)
rm -f build_copyright_pdf_temp.sh
green_echo -e "\n==== 操作完成!${NEW_VERSION}版本PDF已生成 ===="

View File

@@ -4,30 +4,18 @@
xmlns:tools="http://schemas.android.com/tools"
package="cc.winboll.studio.powerbell">
<!-- 只能在前台获取精确的位置信息 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!-- 只有在前台运行时才能获取大致位置信息 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<!-- 拍摄照片和视频 -->
<uses-permission android:name="android.permission.CAMERA"/>
<!-- 此应用可显示在其他应用上方 -->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<!-- 运行前台服务 -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<!-- 读取您共享存储空间中的内容 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<!-- 修改或删除您共享存储空间中的内容 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- 运行“specialUse”类型的前台服务 -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE"/>
<!-- 开机启动 -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<!-- MANAGE_EXTERNAL_STORAGE -->
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<!-- 显示通知 -->
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
@@ -40,22 +28,60 @@
<!-- 计算应用存储空间 -->
<uses-permission android:name="android.permission.GET_PACKAGE_SIZE"/>
<uses-feature android:name="android.hardware.camera"/>
<!-- 请求忽略电池优化 -->
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<uses-feature android:name="android.hardware.camera.autofocus"/>
<!-- 读取您共享存储空间中的内容 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.GET_PACKAGE_SIZE"/>
<!-- 修改或删除您共享存储空间中的内容 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission"/>
<!-- MANAGE_EXTERNAL_STORAGE -->
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<!-- 拍摄照片和视频 -->
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission
android:name="android.permission.ACCESS_PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions"/>
<uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission"/>
<uses-permission android:name="android.permission.BATTERY_STATS"/>
<uses-permission android:name="android.permission.GET_PACKAGE_SIZE"/>
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature
android:name="android.hardware.camera"
android:required="false"/>
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false"/>
<queries>
<package android:name="com.miui.securitycenter"/>
</queries>
<application
android:name=".App"
android:process=":main"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
@@ -64,17 +90,15 @@
android:resizeableActivity="true"
android:requestLegacyExternalStorage="true"
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning">
android:supportsRtl="true"
tools:ignore="GoogleAppIndexingWarning,UnusedAttribute">
<activity
android:process=":main"
android:name=".MainActivity"
android:label="@string/app_name"
android:exported="true"
android:launchMode="singleTask">
</activity>
<activity android:name=".activities.CrashActivity"/>
android:launchMode="singleTask"/>
<activity-alias
android:name=".MainActivityEN1"
@@ -143,14 +167,20 @@
</activity-alias>
<activity
android:name="cc.winboll.studio.powerbell.activities.ClearRecordActivity"
android:parentActivityName="cc.winboll.studio.powerbell.MainActivity"
android:launchMode="singleTask">
</activity>
android:process=":main"
android:name=".activities.CrashActivity"
android:exported="false"/>
<activity
android:name="cc.winboll.studio.powerbell.activities.BackgroundPictureActivity"
android:process=":main"
android:name=".activities.ClearRecordActivity"
android:parentActivityName="cc.winboll.studio.powerbell.MainActivity"
android:launchMode="singleTask"
android:exported="false"/>
<activity
android:process=":main"
android:name=".activities.BackgroundSettingsActivity"
android:parentActivityName="cc.winboll.studio.powerbell.MainActivity"
android:exported="true"
android:launchMode="singleTask">
@@ -175,52 +205,114 @@
</activity>
<activity
android:process=":main"
android:name=".activities.BatteryReporterActivity"
android:exported="false"/>
<activity
android:process=":main"
android:name=".activities.PixelPickerActivity"
android:exported="false"/>
<activity
android:process=":main"
android:name=".activities.BatteryReportActivity"
android:exported="false"/>
<activity
android:process=":main"
android:name=".unittest.MainUnitTestActivity"
android:exported="false"/>
<activity
android:process=":main"
android:name=".activities.ShortcutActionActivity"
android:exported="false"/>
<activity
android:process=":main"
android:name=".activities.SettingsActivity"
android:exported="false"/>
<activity
android:process=":main"
android:name="cc.winboll.studio.powerbell.unittest.MainUnitTest2Activity"
android:exported="false"/>
<activity
android:process=":main"
android:name="com.yalantis.ucrop.UCropActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:exported="true"/>
<receiver
android:process=":main"
android:name=".receivers.MainReceiver"
android:enabled="true"
android:exported="false"
android:exported="true"
android:directBootAware="true">
<intent-filter>
<intent-filter android:priority="1000">
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.POWER_CONNECTED"/>
<action android:name="android.intent.action.USER_PRESENT"/>
</intent-filter>
</receiver>
<service
android:name="cc.winboll.studio.powerbell.services.ControlCenterService"
android:name=".services.ControlCenterService"
android:priority="1000"
android:enabled="true"
android:exported="false"
android:process=".controlcenterservice"/>
android:process=":main"
android:stopWithTask="false"
android:foregroundServiceType="dataSync">
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FOREGROUND_SERVICE"
android:value="后台核心功能运行、持续保活"/>
</service>
<service
android:name="cc.winboll.studio.powerbell.services.AssistantService"
android:name=".services.AssistantService"
android:enabled="true"
android:exported="false"
android:process=".assistantservice"/>
android:process=":assistant"
android:stopWithTask="false"
android:foregroundServiceType="dataSync">
<meta-data
android:name="android.max_aspect"
android:value="4.0"/>
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FOREGROUND_SERVICE"
android:value="辅助核心功能运行"/>
<activity android:name="cc.winboll.studio.powerbell.activities.BatteryReporterActivity"/>
</service>
<activity android:name="cc.winboll.studio.powerbell.activities.AboutActivity"/>
<service
android:name=".services.TTSPlayService"
android:enabled="true"
android:exported="false"
android:process=":main"
android:stopWithTask="false"/>
<activity android:name="cc.winboll.studio.powerbell.activities.PixelPickerActivity"/>
<activity android:name="cc.winboll.studio.powerbell.activities.BatteryReportActivity"/>
<activity android:name="cc.winboll.studio.powerbell.unittest.MainUnitTestActivity"/>
<service android:name=".services.ThoughtfulService"
android:enabled="true"
android:exported="false"
android:process=":main"
android:stopWithTask="false"/>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
android:grantUriPermissions="true"
android:process=":main">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
@@ -228,8 +320,10 @@
</provider>
<activity android:name="cc.winboll.studio.powerbell.activities.ShortcutActionActivity"/>
<meta-data
android:name="android.max_aspect"
android:value="4.0"/>
</application>
</manifest>
</manifest>

Binary file not shown.

After

Width:  |  Height:  |  Size: 517 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

View File

@@ -1,97 +1,320 @@
package cc.winboll.studio.powerbell;
import android.content.Context;
import android.os.Environment;
import android.view.Gravity;
import cc.winboll.studio.libaes.utils.WinBoLLActivityManager;
import cc.winboll.studio.libappbase.GlobalApplication;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.ToastUtils;
import cc.winboll.studio.powerbell.models.NotificationMessage;
import cc.winboll.studio.powerbell.receivers.GlobalApplicationReceiver;
import cc.winboll.studio.powerbell.utils.AppCacheUtils;
import cc.winboll.studio.powerbell.utils.AppConfigUtils;
import java.io.File;
import cc.winboll.studio.powerbell.utils.BackgroundSourceUtils;
import cc.winboll.studio.powerbell.utils.BitmapCacheUtils;
import cc.winboll.studio.powerbell.utils.NotificationManagerUtils;
import cc.winboll.studio.powerbell.views.MemoryCachedBackgroundView;
/**
* 应用全局入口类
* 适配Java7 语法规范 | Android API30 系统版本
* 核心策略:极致强制缓存 - 无论内存紧张程度永不自动清理任何缓存Bitmap/视图控件/路径记录)
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
* @Date 2025-12-29 15:30:00
* @LastModified 2026-01-02 19:01:00
*/
public class App extends GlobalApplication {
public static final String TAG = "GlobalApplication";
public static final String COMPONENT_EN1 = "cc.winboll.studio.powerbell.MainActivityEN1";
public static final String COMPONENT_CN1 = "cc.winboll.studio.powerbell.MainActivityCN1";
public static final String COMPONENT_CN2 = "cc.winboll.studio.powerbell.MainActivityCN2";
public static final String ACTION_SWITCHTO_EN1 = "cc.winboll.studio.powerbell.App.ACTION_SWITCHTO_EN1";
public static final String ACTION_SWITCHTO_CN1 = "cc.winboll.studio.powerbell.App.ACTION_SWITCHTO_CN1";
public static final String ACTION_SWITCHTO_CN2 = "cc.winboll.studio.powerbell.App.ACTION_SWITCHTO_CN2";
// 数据配置存储工具
static AppConfigUtils _mAppConfigUtils;
static AppCacheUtils _mAppCacheUtils;
GlobalApplicationReceiver mReceiver;
static String szTempDir = "";
// ====================================== 常量区 - 置顶排序 (按功能归类) ======================================
// 基础日志TAG
private static final String TAG = "App";
// 缓存保护专用TAG
private static final String CACHE_PROTECT_TAG = "FORCE_CACHE_PROTECT";
// 电池无效值常量修复拼写错误INVALID_BATTERY_VALUE
private static final int INVALID_BATTERY_VALUE = -1;
public static String getTempDirPath() {
return szTempDir;
// 组件跳转常量
public static final String COMPONENT_EN1 = "cc.winboll.studio.powerbell.MainActivityEN1";
public static final String COMPONENT_CN1 = "cc.winboll.studio.powerbell.MainActivityCN1";
public static final String COMPONENT_CN2 = "cc.winboll.studio.powerbell.MainActivityCN2";
// 动作跳转常量
public static final String ACTION_SWITCHTO_EN1 = "cc.winboll.studio.powerbell.App.ACTION_SWITCHTO_EN1";
public static final String ACTION_SWITCHTO_CN1 = "cc.winboll.studio.powerbell.App.ACTION_SWITCHTO_CN1";
public static final String ACTION_SWITCHTO_CN2 = "cc.winboll.studio.powerbell.App.ACTION_SWITCHTO_CN2";
// ====================================== 静态属性区 - 全局单例/状态 (按核心程度排序) ======================================
// 应用单例
private static App sApp;
// 配置与缓存工具 (全局单例)
public static AppConfigUtils sAppConfigUtils;
private static AppCacheUtils sAppCacheUtils;
// 资源与视图缓存 (强制驻留,极致缓存核心)
public static BackgroundSourceUtils sBackgroundSourceUtils;
public static BitmapCacheUtils sBitmapCacheUtils;
private static MemoryCachedBackgroundView sMemoryCachedBackgroundView;
// 系统状态 (电池电量)
public static volatile int sQuantityOfElectricity = INVALID_BATTERY_VALUE;
// 系统工具 (通知管理器)
private static NotificationManagerUtils sNotificationManagerUtils;
// ====================================== 成员属性区 - 非静态成员 (广播接收器) ======================================
private GlobalApplicationReceiver mGlobalReceiver;
// ====================================== 公共静态方法 - 单例/工具获取 (对外入口) ======================================
/**
* 获取应用全局单例实例
* @return 应用单例App实例
*/
public static App getInstance() {
LogUtils.d(TAG, "【getInstance】应用单例获取方法调用 | 当前实例:" + sApp);
return sApp;
}
/**
* 获取配置工具类单例实例
* @param context 上下文对象
* @return 配置工具类AppConfigUtils实例
*/
public static AppConfigUtils getAppConfigUtils(Context context) {
String contextClass = context != null ? context.getClass().getSimpleName() : "null";
LogUtils.d(TAG, "【getAppConfigUtils】配置工具获取方法调用 | 入参Context类型" + contextClass);
if (sAppConfigUtils == null) {
sAppConfigUtils = AppConfigUtils.getInstance(context);
LogUtils.d(TAG, "【getAppConfigUtils】配置工具实例为空已初始化新实例");
}
return sAppConfigUtils;
}
/**
* 获取缓存工具类单例实例
* @param context 上下文对象
* @return 缓存工具类AppCacheUtils实例
*/
public static AppCacheUtils getAppCacheUtils(Context context) {
String contextClass = context != null ? context.getClass().getSimpleName() : "null";
LogUtils.d(TAG, "【getAppCacheUtils】缓存工具获取方法调用 | 入参Context类型" + contextClass);
if (sAppCacheUtils == null) {
sAppCacheUtils = AppCacheUtils.getInstance(context);
LogUtils.d(TAG, "【getAppCacheUtils】缓存工具实例为空已初始化新实例");
}
return sAppCacheUtils;
}
// ====================================== 公共成员方法 - 业务逻辑 (实例方法) ======================================
/**
* 清除电池历史数据
*/
public void clearBatteryHistory() {
LogUtils.d(TAG, "【clearBatteryHistory】清除电池历史数据方法调用");
if (sAppCacheUtils != null) {
sAppCacheUtils.clearBatteryHistory();
LogUtils.d(TAG, "【clearBatteryHistory】电池历史数据清除成功");
} else {
LogUtils.w(TAG, "【clearBatteryHistory】电池历史数据清除失败 | 缓存工具实例sAppCacheUtils为空");
}
}
/**
* 获取视图缓存实例
* @return 视图缓存MemoryCachedBackgroundView实例
*/
public MemoryCachedBackgroundView getMemoryCachedBackgroundView() {
LogUtils.d(TAG, "【getMemoryCachedBackgroundView】视图缓存获取方法调用 | 当前实例:" + sMemoryCachedBackgroundView);
return sMemoryCachedBackgroundView;
}
// ====================================== 公共静态方法 - 业务逻辑 (全局工具方法) ======================================
/**
* 手动清理所有缓存(仅主动调用生效,符合极致缓存策略)
*/
public static void manualClearAllCache() {
LogUtils.w(CACHE_PROTECT_TAG, "【manualClearAllCache】手动清理缓存方法调用 | 仅主动触发生效");
// 清理Bitmap缓存
if (sBitmapCacheUtils != null) {
sBitmapCacheUtils.clearAllCache();
LogUtils.d(CACHE_PROTECT_TAG, "【manualClearAllCache】Bitmap缓存已清理");
}
// 仅置空视图缓存引用,不销毁实例(极致缓存策略)
if (sMemoryCachedBackgroundView != null) {
LogUtils.d(CACHE_PROTECT_TAG, "【manualClearAllCache】视图缓存引用已置空 | 实例保留");
sMemoryCachedBackgroundView = null;
}
LogUtils.w(CACHE_PROTECT_TAG, "【manualClearAllCache】手动清理缓存操作完成");
}
/**
* 发送通知消息(仅调试模式下生效)
* @param title 通知标题
* @param content 通知内容
*/
public static void notifyMessage(String title, String content) {
LogUtils.d(TAG, "【notifyMessage】发送通知消息方法调用 | 标题:" + title + " | 内容:" + content);
boolean canSend = isDebugging() && sApp != null && sNotificationManagerUtils != null;
if (canSend) {
NotificationMessage message = new NotificationMessage(title, content, "");
sNotificationManagerUtils.showMessageNotification(sApp, message);
LogUtils.d(TAG, "【notifyMessage】通知消息发送成功");
} else {
LogUtils.d(TAG, "【notifyMessage】通知消息发送失败 | 条件不满足:调试模式=" + isDebugging() + " | 应用实例=" + (sApp != null) + " | 通知工具=" + (sNotificationManagerUtils != null));
}
}
// ====================================== 生命周期方法 - 应用全局生命周期 (重写父类方法) ======================================
@Override
public void onCreate() {
super.onCreate();
setIsDebugging(BuildConfig.DEBUG);
LogUtils.d(TAG, "【onCreate】应用启动生命周期方法调用 | 开始初始化应用...");
// 临时文件夹方案1
// 获取Pictures文件夹路径Android 10及以上推荐使用MediaStore此处为传统方式
File picturesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
// 定义目标文件路径在Pictures目录下创建"PowerBell"子文件夹及文件)
File powerBellDir = new File(picturesDir, "PowerBell");
// 临时文件夹方案2 <图片保存失败>
// 获取Pictures文件夹路径Android 10及以上推荐使用MediaStore此处为传统方式
//File powerBellDir = getExternalFilesDir("TempDir");
// 初始化应用单例与调试模式
sApp = this;
setIsDebugging(BuildConfig.DEBUG);
LogUtils.d(TAG, "【onCreate】应用单例已初始化 | 调试模式:" + BuildConfig.DEBUG);
// 先创建文件夹(如果不存在)
if (!powerBellDir.exists()) {
powerBellDir.mkdirs();
}
szTempDir = powerBellDir.getAbsolutePath();
// 初始化核心组件
initBaseTools();
initUtils();
initReceiver();
LogUtils.d(TAG, "【onCreate】应用初始化完成 | 极致强制缓存策略已激活");
}
// 初始化 Toast 框架
@Override
public void onTerminate() {
super.onTerminate();
LogUtils.d(TAG, "【onTerminate】应用终止生命周期方法调用 | 开始释放非缓存资源...");
// 释放非缓存资源
ToastUtils.release();
releaseNotificationManager();
releaseReceiver();
// 核心策略:不清理任何缓存
LogUtils.w(CACHE_PROTECT_TAG, "【onTerminate】极致缓存策略生效 | 所有缓存将保留在内存中");
LogUtils.d(TAG, "【onTerminate】非缓存资源释放完成");
}
@Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
LogUtils.w(CACHE_PROTECT_TAG, "【onTrimMemory】系统内存修剪回调 | 内存等级:" + level + " | 忽略修剪,缓存强制保护");
logDetailedCacheStatus();
}
@Override
public void onLowMemory() {
super.onLowMemory();
LogUtils.w(CACHE_PROTECT_TAG, "【onLowMemory】系统低内存回调 | 极致缓存策略生效 | 不执行任何缓存清理操作");
logDetailedCacheStatus();
}
// ====================================== 私有初始化方法 - 组件初始化 (按依赖顺序排序) ======================================
/**
* 初始化基础工具类Activity管理、Toast、通知管理器
*/
private void initBaseTools() {
LogUtils.d(TAG, "【initBaseTools】基础工具类初始化开始...");
WinBoLLActivityManager.init(this);
ToastUtils.init(this);
// 设置 Toast 布局样式
//ToastUtils.setView(R.layout.toast_custom_view);
//ToastUtils.setStyle(new WhiteToastStyle());
//ToastUtils.setGravity(Gravity.BOTTOM, 0, 200);
// 设置数据配置存储工具
_mAppConfigUtils = getAppConfigUtils(this);
_mAppCacheUtils = getAppCacheUtils(this);
mReceiver = new GlobalApplicationReceiver(this);
mReceiver.registerAction();
sNotificationManagerUtils = new NotificationManagerUtils(this);
LogUtils.d(TAG, "【initBaseTools】基础工具类初始化完成");
}
public static AppConfigUtils getAppConfigUtils(Context context) {
if (_mAppConfigUtils == null) {
_mAppConfigUtils = AppConfigUtils.getInstance(context);
/**
* 初始化核心工具与缓存(极致强制驻留,缓存核心)
*/
private void initUtils() {
LogUtils.d(TAG, "【initUtils】核心工具与缓存初始化开始 | 极致缓存策略激活");
// 1. 配置与基础缓存工具初始化
sAppConfigUtils = getAppConfigUtils(this);
sAppCacheUtils = getAppCacheUtils(this);
// 2. 资源与Bitmap缓存工具初始化永久驻留
sBackgroundSourceUtils = BackgroundSourceUtils.getInstance(this);
sBackgroundSourceUtils.loadSettings();
sBitmapCacheUtils = BitmapCacheUtils.getInstance();
LogUtils.d(TAG, "【initUtils】资源与Bitmap缓存工具初始化完成 | 永久驻留内存");
// 3. 视图缓存初始化(永久驻留,无实例则创建)
sMemoryCachedBackgroundView = MemoryCachedBackgroundView.getLastInstance(this);
if (sMemoryCachedBackgroundView == null) {
sMemoryCachedBackgroundView = MemoryCachedBackgroundView.getInstance(this, sBackgroundSourceUtils.getCurrentBackgroundBean(), true);
LogUtils.d(TAG, "【initUtils】视图缓存无现有实例已创建新实例");
}
return _mAppConfigUtils;
LogUtils.d(TAG, "【initUtils】视图缓存初始化完成 | 永久驻留内存");
}
public static AppCacheUtils getAppCacheUtils(Context context) {
if (_mAppCacheUtils == null) {
_mAppCacheUtils = AppCacheUtils.getInstance(context);
/**
* 注册全局广播接收器
*/
private void initReceiver() {
LogUtils.d(TAG, "【initReceiver】全局广播接收器注册开始...");
mGlobalReceiver = new GlobalApplicationReceiver(this);
mGlobalReceiver.registerAction();
LogUtils.d(TAG, "【initReceiver】全局广播接收器注册完成");
}
// ====================================== 私有释放方法 - 资源释放 (按创建逆序排序) ======================================
/**
* 释放全局广播接收器
*/
private void releaseReceiver() {
LogUtils.d(TAG, "【releaseReceiver】全局广播接收器释放开始...");
if (mGlobalReceiver != null) {
mGlobalReceiver.unregisterAction();
mGlobalReceiver = null;
LogUtils.d(TAG, "【releaseReceiver】全局广播接收器释放完成");
}
return _mAppCacheUtils;
}
public void clearBatteryHistory() {
_mAppCacheUtils.clearBatteryHistory();
/**
* 释放通知管理器资源
*/
private void releaseNotificationManager() {
LogUtils.d(TAG, "【releaseNotificationManager】通知管理器资源释放开始...");
if (sNotificationManagerUtils != null) {
sNotificationManagerUtils.release();
sNotificationManagerUtils = null;
LogUtils.d(TAG, "【releaseNotificationManager】通知管理器资源释放完成");
}
}
@Override
public void onTerminate() {
super.onTerminate();
ToastUtils.release();
}
// ====================================== 私有辅助方法 - 日志/工具 (辅助功能) ======================================
/**
* 记录当前缓存详细状态(用于调试监控,极致缓存策略监控)
*/
private void logDetailedCacheStatus() {
LogUtils.d(TAG, "【logDetailedCacheStatus】缓存状态监控日志开始...");
// Bitmap缓存状态
if (sBitmapCacheUtils != null) {
LogUtils.d(CACHE_PROTECT_TAG, "【缓存状态】BitmapCache - 有效");
try {
LogUtils.d(CACHE_PROTECT_TAG, "【缓存详情】Bitmap缓存数量" + sBitmapCacheUtils.getCacheCount());
} catch (Exception e) {
LogUtils.e(CACHE_PROTECT_TAG, "【缓存详情】获取Bitmap缓存数量失败", e);
}
} else {
LogUtils.d(CACHE_PROTECT_TAG, "【缓存状态】BitmapCache - 未初始化");
}
// 视图缓存状态
if (sMemoryCachedBackgroundView != null) {
LogUtils.d(CACHE_PROTECT_TAG, "【缓存状态】ViewCache - 有效");
LogUtils.d(CACHE_PROTECT_TAG, "【缓存详情】视图实例数量:" + MemoryCachedBackgroundView.getInstanceCount());
} else {
LogUtils.d(CACHE_PROTECT_TAG, "【缓存状态】ViewCache - 引用已置空(实例可能保留)");
}
}
}

View File

@@ -1,65 +0,0 @@
package cc.winboll.studio.powerbell.activities;
/**
* @Author ZhanGSKen<zhangsken@qq.com>
* @Date 2025/03/25 01:16:32
* @Describe 应用介绍窗口
*/
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import cc.winboll.studio.libaes.models.APPInfo;
import cc.winboll.studio.libaes.views.AToolbar;
import cc.winboll.studio.libaes.views.AboutView;
import cc.winboll.studio.powerbell.R;
public class AboutActivity extends Activity {
Context mContext;
public static final String TAG = "AboutActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
mContext = this;
// 初始化工具栏
AToolbar mAToolbar = (AToolbar) findViewById(R.id.toolbar);
setActionBar(mAToolbar);
mAToolbar.setSubtitle(getString(R.string.text_about));
//mAToolbar.setTitleTextAppearance(this, R.style.Toolbar_TitleText);
getActionBar().setDisplayHomeAsUpEnabled(true);
AboutView aboutView = CreateAboutView();
// 在 Activity 的 onCreate 或其他生命周期方法中调用
LinearLayout llRoot = findViewById(R.id.root_ll);
//layout.setOrientation(LinearLayout.VERTICAL);
// 创建布局参数(宽度和高度)
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
);
llRoot.addView(aboutView, params);
}
public AboutView CreateAboutView() {
String szBranchName = "powerbell";
APPInfo appInfo = new APPInfo();
appInfo.setAppName(getString(R.string.app_name));
appInfo.setAppIcon(R.drawable.ic_launcher);
appInfo.setAppDescription(getString(R.string.app_description));
appInfo.setAppGitName("APPBase");
appInfo.setAppGitOwner("Studio");
appInfo.setAppGitAPPBranch(szBranchName);
appInfo.setAppGitAPPSubProjectFolder(szBranchName);
appInfo.setAppHomePage("https://www.winboll.cc/apks/index.php?project=PowerBell");
appInfo.setAppAPKName("PowerBell");
appInfo.setAppAPKFolderName("PowerBell");
return new AboutView(mContext, appInfo);
}
}

View File

@@ -1,659 +0,0 @@
package cc.winboll.studio.powerbell.activities;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import cc.winboll.studio.libaes.dialogs.YesNoAlertDialog;
import cc.winboll.studio.libaes.views.AToolbar;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.ToastUtils;
import cc.winboll.studio.powerbell.App;
import cc.winboll.studio.powerbell.R;
import cc.winboll.studio.powerbell.beans.BackgroundPictureBean;
import cc.winboll.studio.powerbell.dialogs.BackgroundPicturePreviewDialog;
import cc.winboll.studio.powerbell.dialogs.NetworkBackgroundDialog;
import cc.winboll.studio.powerbell.utils.BackgroundPictureUtils;
import cc.winboll.studio.powerbell.utils.FileUtils;
import cc.winboll.studio.powerbell.utils.UriUtil;
import cc.winboll.studio.powerbell.views.BackgroundView;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class BackgroundPictureActivity extends WinBoLLActivity implements BackgroundPicturePreviewDialog.IOnRecivedPictureListener {
public static final String TAG = "BackgroundPictureActivity";
public BackgroundPictureUtils mBackgroundPictureUtils;
// 图片选择请求码
public static final int REQUEST_SELECT_PICTURE = 0;
public static final int REQUEST_TAKE_PHOTO = 1;
public static final int REQUEST_CROP_IMAGE = 2;
private static final int STORAGE_PERMISSION_REQUEST = 100;
private AToolbar mAToolbar;
private File mfBackgroundDir; // 背景图片存储文件夹
private File mfPictureDir; // 拍照与剪裁临时文件夹
private File mfTakePhoto; // 拍照文件
private File mfRecivedPicture; // 接收的图片文件
private File mfTempCropPicture; // 剪裁临时文件
private File mfRecivedCropPicture; // 剪裁后的目标文件
private String preViewFileBackgroundView = "";
BackgroundView bvPreviewBackground;
boolean isCommitSettings = false;
// 静态变量
public static String _mszRecivedCropPicture = "RecivedCrop.jpg";
private static String _mszCommonFileType = "jpeg";
private int mnPictureCompress = 100;
private static String _RecivedPictureFileName;
@Override
public Activity getActivity() {
return this;
}
@Override
public String getTag() {
return TAG;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_backgroundpicture);
initEnv();
// 初始化工具类和文件夹
mBackgroundPictureUtils = BackgroundPictureUtils.getInstance(this);
mfBackgroundDir = new File(mBackgroundPictureUtils.getBackgroundDir());
if (!mfBackgroundDir.exists()) {
mfBackgroundDir.mkdirs();
}
mfPictureDir = new File(App.getTempDirPath());
if (!mfPictureDir.exists()) {
mfPictureDir.mkdirs();
}
// 初始化文件对象
mfTakePhoto = new File(mfPictureDir, "TakePhoto.jpg");
mfTempCropPicture = new File(mfPictureDir, "TempCrop.jpg");
mfRecivedPicture = getRecivedPictureFile(this);
mfRecivedCropPicture = new File(mfBackgroundDir, _mszRecivedCropPicture);
// 初始化工具栏
mAToolbar = (AToolbar) findViewById(R.id.toolbar);
setActionBar(mAToolbar);
mAToolbar.setSubtitle(R.string.subtitle_activity_backgroundpicture);
getActionBar().setDisplayHomeAsUpEnabled(true);
mAToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish(); // 点击导航栏返回按钮,触发 finish()
}
});
// 设置按钮点击事件
findViewById(R.id.activitybackgroundpictureAButton5).setOnClickListener(onOriginNullClickListener);
findViewById(R.id.activitybackgroundpictureAButton4).setOnClickListener(onReceivedPictureClickListener);
findViewById(R.id.activitybackgroundpictureAButton1).setOnClickListener(onTakePhotoClickListener);
findViewById(R.id.activitybackgroundpictureAButton2).setOnClickListener(onSelectPictureClickListener);
findViewById(R.id.activitybackgroundpictureAButton3).setOnClickListener(onCropPictureClickListener);
findViewById(R.id.activitybackgroundpictureAButton6).setOnClickListener(onCropFreePictureClickListener);
findViewById(R.id.activitybackgroundpictureAButton7).setOnClickListener(onPixelPickerClickListener);
findViewById(R.id.activitybackgroundpictureAButton8).setOnClickListener(onCleanPixelClickListener);
updatePreviewBackground();
// 处理分享的图片
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null && isImageType(type)) {
BackgroundPicturePreviewDialog dlg = new BackgroundPicturePreviewDialog(this);
dlg.show();
}
}
private void initEnv() {
LogUtils.d(TAG, "initEnv()");
_RecivedPictureFileName = "Recived.data";
}
public static String getBackgroundFileName() {
return _mszRecivedCropPicture;
}
@Override
public void onAcceptRecivedPicture(String szPreRecivedPictureName) {
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(this);
utils.getBackgroundPictureBean().setIsUseBackgroundFile(true);
utils.saveData();
File sourceFile = new File(utils.getBackgroundDir(), szPreRecivedPictureName);
if (FileUtils.copyFile(sourceFile, mfRecivedPicture)) {
startCropImageActivity(false);
} else {
ToastUtils.show("图片复制失败,请重试");
}
}
/**
* 更新背景图片预览
*/
public void updatePreviewBackground() {
LogUtils.d(TAG, "updatePreviewBackground");
//ImageView ivPreviewBackground = (ImageView) findViewById(R.id.activitybackgroundpictureImageView1);
bvPreviewBackground = (BackgroundView) findViewById(R.id.activitybackgroundpictureBackgroundView1);
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(this);
utils.loadBackgroundPictureBean();
boolean isUseBackgroundFile = utils.getBackgroundPictureBean().isUseBackgroundFile();
if (isUseBackgroundFile && mfRecivedCropPicture.exists()) {
//try {
String filePath = utils.getBackgroundDir() + getBackgroundFileName();
preViewFileBackgroundView = filePath;
bvPreviewBackground.previewBackgroundImage(preViewFileBackgroundView);
/*Drawable drawable = FileUtils.getImageDrawable(filePath);
if (drawable != null) {
//drawable.setAlpha(120);
//bvPreviewBackground.setImageDrawable(drawable);
}*/
//ToastUtils.show("背景图片已更新");
// } catch (IOException e) {
// LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
// ToastUtils.show("背景图片加载失败");
// }
} else {
ToastUtils.show("未使用背景图片");
preViewFileBackgroundView = "";
bvPreviewBackground.previewBackgroundImage(preViewFileBackgroundView);
// Drawable drawable = getResources().getDrawable(R.drawable.blank10x10);
// if (drawable != null) {
// drawable.setAlpha(120);
// bvPreviewBackground.setImageDrawable(drawable);
// }
}
}
// 点击事件监听器
private View.OnClickListener onOriginNullClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(BackgroundPictureActivity.this);
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
bean.setIsUseBackgroundFile(false);
utils.saveData();
updatePreviewBackground();
}
};
private View.OnClickListener onSelectPictureClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkAndRequestStoragePermission()) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_SELECT_PICTURE);
}
}
};
private View.OnClickListener onCropPictureClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
File fCheck = new File(mfBackgroundDir, getBackgroundFileName());
if (fCheck.exists()) {
startCropImageActivity(false);
} else {
ToastUtils.show("没有可剪裁的图片");
}
}
};
private View.OnClickListener onCropFreePictureClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
File fCheck = new File(mfBackgroundDir, getBackgroundFileName());
if (fCheck.exists()) {
startCropImageActivity(true);
} else {
ToastUtils.show("没有可剪裁的图片");
}
}
};
private View.OnClickListener onTakePhotoClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "onTakePhotoClickListener");
LogUtils.d(TAG, "mfTakePhoto : " + mfTakePhoto.getPath());
if (mfTakePhoto.exists()) {
mfTakePhoto.delete();
}
try {
mfTakePhoto.createNewFile();
} catch (IOException e) {
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
ToastUtils.show("拍照文件创建失败");
return;
}
if (checkAndRequestStoragePermission()) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
};
private View.OnClickListener onReceivedPictureClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(BackgroundPictureActivity.this);
utils.getBackgroundPictureBean().setIsUseBackgroundFile(true);
utils.saveData();
updatePreviewBackground();
}
};
private View.OnClickListener onPixelPickerClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// 从文件路径启动像素拾取活动
//String imagePath = "/storage/emulated/0/DCIM/Camera/sample.jpg";
String imagePath = mfRecivedCropPicture.toString();
Intent intent = new Intent(getApplicationContext(), PixelPickerActivity.class);
intent.putExtra("imagePath", imagePath);
startActivity(intent);
//App.getWinBoLLActivityManager().startWinBoLLActivity(getActivity(), intent, PixelPickerActivity.class);
}
};
private View.OnClickListener onCleanPixelClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(BackgroundPictureActivity.this);
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
bean.setPixelColor(0);
utils.saveData();
setBackgroundColor();
}
};
/**
* 压缩图片并保存到接收文件
*/
void compressQualityToRecivedPicture(Bitmap bitmap) {
OutputStream outStream = null;
try {
mfRecivedPicture = getRecivedPictureFile(this);
if (!mfRecivedPicture.exists()) {
mfRecivedPicture.createNewFile();
}
FileOutputStream fos = new FileOutputStream(mfRecivedPicture);
outStream = new BufferedOutputStream(fos);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
} catch (IOException e) {
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
ToastUtils.show("图片压缩失败");
} finally {
if (outStream != null) {
try {
outStream.close();
} catch (IOException e) {
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
}
}
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
}
}
}
/**
* 启动图片裁剪活动
* @param isCropFree 是否自由裁剪
*/
public void startCropImageActivity(boolean isCropFree) {
LogUtils.d(TAG, "startCropImageActivity");
BackgroundPictureBean bean = mBackgroundPictureUtils.loadBackgroundPictureBean();
mfRecivedPicture = getRecivedPictureFile(this);
Uri uri = UriUtil.getUriForFile(this, mfRecivedPicture);
LogUtils.d(TAG, "uri : " + uri.toString());
if (mfTempCropPicture.exists()) {
mfTempCropPicture.delete();
}
try {
mfTempCropPicture.createNewFile();
} catch (IOException e) {
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
ToastUtils.show("剪裁临时文件创建失败");
return;
}
Uri cropOutPutUri = Uri.fromFile(mfTempCropPicture);
LogUtils.d(TAG, "mfTempCropPicture : " + mfTempCropPicture.getPath());
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/" + _mszCommonFileType);
intent.putExtra("crop", "true");
intent.putExtra("noFaceDetection", true);
if (!isCropFree) {
intent.putExtra("aspectX", bean.getBackgroundWidth());
intent.putExtra("aspectY", bean.getBackgroundHeight());
}
intent.putExtra("return-data", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, cropOutPutUri);
intent.putExtra("scale", true);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivityForResult(intent, REQUEST_CROP_IMAGE);
}
/**
* 保存剪裁后的Bitmap优化版
*/
private void saveCropBitmap(Bitmap bitmap) {
if (bitmap == null) {
ToastUtils.show("剪裁图片为空");
return;
}
// 内存优化:大图片自动缩放
Bitmap scaledBitmap = bitmap;
if (bitmap.getByteCount() > 10 * 1024 * 1024) { // 超过10MB
float scale = 1.0f;
while (scaledBitmap.getByteCount() > 5 * 1024 * 1024) {
scale -= 0.2f; // 每次缩小20%
if (scale < 0.2f) break; // 最小缩放到20%
scaledBitmap = scaleBitmap(scaledBitmap, scale);
}
if (scaledBitmap != bitmap) {
bitmap.recycle(); // 回收原Bitmap
}
}
// 优化:创建保存目录
File backgroundDir = new File(mBackgroundPictureUtils.getBackgroundDir());
if (!backgroundDir.exists()) {
if (!backgroundDir.mkdirs()) {
ToastUtils.show("无法创建保存目录");
if (scaledBitmap != bitmap) scaledBitmap.recycle();
return;
}
}
File saveFile = new File(backgroundDir, getBackgroundFileName());
// 优化:检查文件是否可写
if (saveFile.exists() && !saveFile.canWrite()) {
if (!saveFile.delete()) {
ToastUtils.show("无法删除旧文件");
if (scaledBitmap != bitmap) scaledBitmap.recycle();
return;
}
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(saveFile);
boolean success = scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
fos.flush();
if (success) {
ToastUtils.show("保存成功");
// 更新数据
mBackgroundPictureUtils.getBackgroundPictureBean().setIsUseBackgroundFile(true);
updatePreviewBackground();
} else {
ToastUtils.show("图片压缩保存失败");
}
} catch (FileNotFoundException e) {
LogUtils.e(TAG, "文件未找到" + e);
ToastUtils.show("保存失败:文件路径错误");
} catch (IOException e) {
LogUtils.e(TAG, "写入异常" + e);
ToastUtils.show("保存失败:磁盘可能已满或路径错误");
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
LogUtils.e(TAG, "流关闭异常" + e);
}
}
if (scaledBitmap != null && !scaledBitmap.isRecycled()) {
scaledBitmap.recycle();
}
}
}
/**
* 缩放Bitmap
*/
private Bitmap scaleBitmap(Bitmap original, float scale) {
if (original == null) {
return null;
}
int width = (int) (original.getWidth() * scale);
int height = (int) (original.getHeight() * scale);
return Bitmap.createScaledBitmap(original, width, height, true);
}
/**
* 分享图片
*/
void sharePicture() {
Uri uri = UriUtil.getUriForFile(this, mfRecivedPicture);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/" + _mszCommonFileType);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share Image"));
}
public static File getRecivedPictureFile(Context context) {
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(context);
utils.loadBackgroundPictureBean();
return new File(utils.getBackgroundDir(), _RecivedPictureFileName);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_SELECT_PICTURE && resultCode == RESULT_OK) {
try {
Uri selectedImage = data.getData();
LogUtils.d(TAG, "Uri is : " + selectedImage.toString());
File fSrcImage = new File(UriUtil.getFilePathFromUri(this, selectedImage));
mfRecivedPicture = getRecivedPictureFile(this);
if (FileUtils.copyFile(fSrcImage, mfRecivedPicture)) {
startCropImageActivity(false);
} else {
ToastUtils.show("图片复制失败,请重试");
}
} catch (Exception e) {
LogUtils.e(TAG, "选择图片异常" + e);
ToastUtils.show("选择图片失败:" + e.getMessage());
}
} else if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
LogUtils.d(TAG, "REQUEST_TAKE_PHOTO");
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap imageBitmap = (Bitmap) extras.get("data");
if (imageBitmap != null) {
compressQualityToRecivedPicture(imageBitmap);
startCropImageActivity(false);
} else {
ToastUtils.show("拍照图片为空");
}
} else {
ToastUtils.show("拍照数据获取失败");
}
} else if (requestCode == REQUEST_CROP_IMAGE && resultCode == RESULT_OK) {
LogUtils.d(TAG, "CROP_IMAGE_REQUEST_CODE");
try {
Bitmap cropBitmap = null;
// 方案1通过Intent获取剪裁后的Bitmap
if (data != null && data.hasExtra("data")) {
cropBitmap = data.getParcelableExtra("data");
} else if (mfTempCropPicture.exists()) {
cropBitmap = BitmapFactory.decodeFile(mfTempCropPicture.getPath());
} else {
ToastUtils.show("剪裁文件不存在");
return;
}
if (cropBitmap != null) {
saveCropBitmap(cropBitmap);
} else {
ToastUtils.show("获取剪裁图片失败");
}
} catch (OutOfMemoryError e) {
LogUtils.e(TAG, "内存溢出" + e);
ToastUtils.show("保存失败:内存不足,请尝试裁剪更小的图片");
} catch (Exception e) {
LogUtils.e(TAG, "剪裁保存异常" + e);
ToastUtils.show("保存失败:" + e.getMessage());
}/* finally {
// 安全删除临时文件
if (mfTempCropPicture.exists()) {
mfTempCropPicture.delete();
}
}*/
} else if (resultCode != RESULT_OK) {
LogUtils.d(TAG, "操作取消或失败requestCode: " + requestCode);
ToastUtils.show("操作已取消");
}
}
/**
* 检查类型是否为图片
*/
private boolean isImageType(String type) {
return type.startsWith("image/") || "image/jpeg".equals(type) ||
"image/jpg".equals(type) || "image/png".equals(type) ||
"image/webp".equals(type);
}
/**
* 检查并申请存储权限
*/
private boolean checkAndRequestStoragePermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
STORAGE_PERMISSION_REQUEST);
return false;
}
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == STORAGE_PERMISSION_REQUEST) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
ToastUtils.show("存储权限已获取");
} else {
ToastUtils.show("需要存储权限才能保存图片");
}
}
}
void setBackgroundColor() {
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(BackgroundPictureActivity.this);
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
int nPixelColor = bean.getPixelColor();
RelativeLayout mainLayout = findViewById(R.id.activitybackgroundpictureRelativeLayout1);
mainLayout.setBackgroundColor(nPixelColor);
}
@Override
protected void onResume() {
super.onResume();
setBackgroundColor();
}
public void onNetworkBackgroundDialog(View view) {
// 在需要显示对话框的地方(如网络状态监听回调中)
NetworkBackgroundDialog dialog = new NetworkBackgroundDialog(this, new NetworkBackgroundDialog.OnDialogClickListener() {
@Override
public void onConfirm() {
ToastUtils.show("onConfirm");
// 处理确认逻辑(如允许后台网络使用)
LogUtils.d("MainActivity", "用户允许后台网络使用");
// 执行具体业务:如开启后台网络请求服务
}
@Override
public void onCancel() {
ToastUtils.show("onCancel");
// 处理取消逻辑(如禁止后台网络使用)
LogUtils.d("MainActivity", "用户禁止后台网络使用");
// 执行具体业务:如关闭后台网络请求
}
});
// 可选:修改对话框标题和内容(适配自定义场景)
dialog.setTitle("网络图片下载对话框");
dialog.setContent("是否下载地址中的图片资源,作为应用背景图片?");
// 显示对话框
dialog.show();
}
/**
* 重写finish方法确保所有退出场景都触发Toast
*/
@Override
public void finish() {
if (!isCommitSettings) {
YesNoAlertDialog.show(this, "应用背景更改提示:", "是否应用预览图片?", new YesNoAlertDialog.OnDialogResultListener(){
@Override
public void onNo() {
isCommitSettings = true;
finish();
}
@Override
public void onYes() {
bvPreviewBackground.saveToBackgroundSources(preViewFileBackgroundView);
isCommitSettings = true;
finish();
}
});
} else {
super.finish();
}
}
}

View File

@@ -0,0 +1,994 @@
package cc.winboll.studio.powerbell.activities;
import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.provider.MediaStore;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;
import cc.winboll.studio.libaes.dialogs.YesNoAlertDialog;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.ToastUtils;
import cc.winboll.studio.powerbell.MainActivity;
import cc.winboll.studio.powerbell.R;
import cc.winboll.studio.powerbell.dialogs.BackgroundPicturePreviewDialog;
import cc.winboll.studio.powerbell.dialogs.ColorPaletteDialog;
import cc.winboll.studio.powerbell.dialogs.NetworkBackgroundDialog;
import cc.winboll.studio.powerbell.models.BackgroundBean;
import cc.winboll.studio.powerbell.utils.AppConfigUtils;
import cc.winboll.studio.powerbell.utils.BackgroundSourceUtils;
import cc.winboll.studio.powerbell.utils.BitmapCacheUtils;
import cc.winboll.studio.powerbell.utils.FileUtils;
import cc.winboll.studio.powerbell.utils.ImageCropUtils;
import cc.winboll.studio.powerbell.utils.ImageUtils;
import cc.winboll.studio.powerbell.utils.UriUtils;
import cc.winboll.studio.powerbell.views.BackgroundView;
import java.io.File;
/**
* 背景设置页面(支持图片选择、拍照、裁剪、像素拾取、调色板等功能)
* 核心:基于强制缓存策略,支持预览与设置提交分离,保留操作状态
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
*/
public class BackgroundSettingsActivity extends WinBoLLActivity {
// ====================== 常量定义(按功能分类排序)======================
public static final String TAG = "BackgroundSettingsActivity";
// 系统版本常量
private static final int SDK_VERSION_TIRAMISU = 33;
// 请求码(按功能分组,从小到大排序)
public static final int REQUEST_SELECT_PICTURE = 0;
public static final int REQUEST_TAKE_PHOTO = 1;
public static final int REQUEST_CROP_IMAGE = 2;
private static final int REQUEST_PIXELPICKER = 1001;
private static final int REQUEST_CAMERA_PERMISSION = 1004;
// Bitmap解析常量
private static final int BITMAP_MAX_SIZE = 2048;
private static final int BITMAP_MAX_SAMPLE_SIZE = 16;
// ====================== 成员变量(按依赖优先级+功能分类)======================
// 工具类实例
private BackgroundSourceUtils mBgSourceUtils;
private BitmapCacheUtils mBitmapCache;
// 视图组件
private Toolbar mToolbar;
private BackgroundView mBackgroundView;
// 状态标记volatile保证多线程可见性
private volatile boolean isCommitSettings = false;
private volatile boolean isPreviewBackgroundChanged = false;
// ====================== 生命周期方法(按执行顺序排列)======================
@Override
public Activity getActivity() {
return this;
}
@Override
public String getTag() {
return TAG;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LogUtils.d(TAG, "onCreate() 开始初始化");
setContentView(R.layout.activity_background_settings);
// 初始化核心组件
initCoreComponents();
// 初始化Toolbar与点击事件
initToolbar();
initClickListeners();
LogUtils.d(TAG, "onCreate() 视图与事件绑定完成");
// 处理分享意图或初始化预览
handleIntentOrPreview();
// 初始化预览环境并刷新
initPreviewEnvironment();
LogUtils.d(TAG, "onCreate() 初始化完成");
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
LogUtils.d(TAG, "onPostCreate() 执行双重刷新预览");
// 监听视图布局完成事件
mBackgroundView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// 移除监听,避免重复回调
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mBackgroundView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
mBackgroundView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
// 此时已获取真实宽高
int width = mBackgroundView.getWidth();
int height = mBackgroundView.getHeight();
LogUtils.d(TAG, String.format("onPostCreate() 获取视图尺寸 | width=%d | height=%d", width, height));
if (width > 0 && height > 0) {
AppConfigUtils appConfigUtils = AppConfigUtils.getInstance(BackgroundSettingsActivity.this);
appConfigUtils.loadAppConfig();
appConfigUtils.mAppConfigBean.setDefaultFrameWidth(width);
appConfigUtils.mAppConfigBean.setDefaultFrameHeight(height);
appConfigUtils.saveAppConfig();
LogUtils.d(TAG, "onPostCreate() 保存默认相框尺寸成功");
doubleRefreshPreview();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
LogUtils.d(TAG, String.format("onActivityResult() | requestCode=%d | resultCode=%d | data=%s",
requestCode, resultCode, data != null ? data.toString() : "null"));
try {
if (resultCode != RESULT_OK) {
LogUtils.d(TAG, String.format("onActivityResult() 操作取消 | requestCode=%d", requestCode));
handleOperationCancelOrFail();
return;
}
handleActivityResult(requestCode, data);
} catch (Exception e) {
LogUtils.e(TAG, String.format("onActivityResult() 异常 | requestCode=%d | 异常信息=%s",
requestCode, e.getMessage()));
ToastUtils.show("操作失败");
}
}
@Override
public void finish() {
LogUtils.d(TAG, String.format("finish() | isCommitSettings=%b | isPreviewBackgroundChanged=%b",
isCommitSettings, isPreviewBackgroundChanged));
if (isCommitSettings) {
super.finish();
} else {
handleFinishConfirmation();
}
}
// ====================== 权限回调方法(单独分类)======================
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
LogUtils.d(TAG, String.format("onRequestPermissionsResult() | requestCode=%d | 权限数量=%d | 结果数量=%d",
requestCode, permissions.length, grantResults.length));
if (requestCode == REQUEST_CAMERA_PERMISSION) {
handleCameraPermissionResult(grantResults);
}
}
// ====================== 界面初始化方法Toolbar + 点击事件)======================
private void initToolbar() {
LogUtils.d(TAG, "initToolbar() 开始初始化");
mToolbar = findViewById(R.id.toolbar);
if (mToolbar == null) {
LogUtils.e(TAG, "initToolbar() | Toolbar未找到");
return;
}
setSupportActionBar(mToolbar);
mToolbar.setSubtitle(getTag());
mToolbar.setTitleTextAppearance(this, R.style.Toolbar_TitleText);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "导航栏 点击返回按钮");
finish();
}
});
LogUtils.d(TAG, "initToolbar() 配置完成");
}
private void initClickListeners() {
LogUtils.d(TAG, "initClickListeners() 开始绑定按钮点击事件");
// 绑定所有按钮点击事件
bindClickListener(R.id.activitybackgroundsettingsAButton1, onOriginNullClickListener);
bindClickListener(R.id.activitybackgroundsettingsAButton2, onReceivedPictureClickListener);
bindClickListener(R.id.activitybackgroundsettingsAButton3, onTakePhotoClickListener);
bindClickListener(R.id.activitybackgroundsettingsAButton4, onSelectPictureClickListener);
bindClickListener(R.id.activitybackgroundsettingsAButton5, onNetworkBackgroundDialog);
bindClickListener(R.id.activitybackgroundsettingsAButton6, onCropPictureClickListener);
bindClickListener(R.id.activitybackgroundsettingsAButton7, onCropFreePictureClickListener);
bindClickListener(R.id.activitybackgroundsettingsAButton8, onPixelPickerClickListener);
bindClickListener(R.id.activitybackgroundsettingsAButton9, onColorPaletteClickListener);
bindClickListener(R.id.activitybackgroundsettingsAButton10, onCleanPixelClickListener);
LogUtils.d(TAG, "initClickListeners() 按钮点击事件绑定完成");
}
// 通用按钮绑定工具方法
private void bindClickListener(int resId, View.OnClickListener listener) {
LogUtils.d(TAG, String.format("bindClickListener() | resId=%d", resId));
View view = findViewById(resId);
if (view != null) {
view.setOnClickListener(listener);
LogUtils.d(TAG, String.format("bindClickListener() | resId=%d 绑定成功", resId));
} else {
LogUtils.e(TAG, String.format("bindClickListener() | 未找到视图:%d", resId));
}
}
// ====================== 按钮点击事件(按功能分类)======================
private View.OnClickListener onOriginNullClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "onOriginNullClickListener() | 取消背景图片");
BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
if (previewBean == null) {
LogUtils.e(TAG, "onOriginNullClickListener() | 预览Bean为空");
return;
}
previewBean.setIsUseBackgroundFile(false);
mBgSourceUtils.saveSettings();
doubleRefreshPreview();
isPreviewBackgroundChanged = true;
}
};
private View.OnClickListener onSelectPictureClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "onSelectPictureClickListener() | 选择图片");
launchImageSelector();
}
};
private View.OnClickListener onNetworkBackgroundDialog = new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "onNetworkBackgroundDialog() | 打开网络背景对话框");
NetworkBackgroundDialog networkBackgroundDialog = new NetworkBackgroundDialog(BackgroundSettingsActivity.this, new NetworkBackgroundDialog.OnDialogClickListener() {
@Override
public void onConfirm(String szConfirmFilePath) {
LogUtils.d(TAG, String.format("网络背景确认 onConfirm() | 文件路径=%s", szConfirmFilePath));
// 拷贝文件到预览数据并启动裁剪
if (putUriFileToPreviewSource(new File(szConfirmFilePath))) {
startImageCrop(false);
}
}
@Override
public void onCancel() {
LogUtils.d(TAG, "网络背景取消 onCancel()");
}
});
networkBackgroundDialog.show();
}
};
private View.OnClickListener onCropPictureClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "onCropPictureClickListener() | 固定比例裁剪");
startImageCrop(false);
}
};
private View.OnClickListener onCropFreePictureClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "onCropFreePictureClickListener() | 自由裁剪");
startImageCrop(true);
}
};
private View.OnClickListener onTakePhotoClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "onTakePhotoClickListener() | 拍照");
// 动态申请相机权限
if (ContextCompat.checkSelfPermission(BackgroundSettingsActivity.this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
LogUtils.d(TAG, "拍照准备 | 相机权限未授予,发起申请");
ActivityCompat.requestPermissions(
BackgroundSettingsActivity.this,
new String[]{Manifest.permission.CAMERA},
REQUEST_CAMERA_PERMISSION);
return;
}
handleTakePhoto();
}
};
private View.OnClickListener onReceivedPictureClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "onReceivedPictureClickListener() | 恢复收到的图片");
BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
if (previewBean == null) {
LogUtils.e(TAG, "onReceivedPictureClickListener() | 预览Bean为空");
return;
}
previewBean.setIsUseBackgroundFile(true);
mBgSourceUtils.saveSettings();
doubleRefreshPreview();
isPreviewBackgroundChanged = true;
}
};
private View.OnClickListener onPixelPickerClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "onPixelPickerClickListener() | 像素拾取");
BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
if (previewBean == null) {
LogUtils.e(TAG, "onPixelPickerClickListener() | 预览Bean为空");
ToastUtils.show("无有效图片可拾取像素");
return;
}
String targetImagePath = previewBean.getBackgroundFilePath();
File targetFile = new File(targetImagePath);
if (targetFile == null || !targetFile.exists() || targetFile.length() <= 0) {
ToastUtils.show("无有效图片可拾取像素");
LogUtils.e(TAG, String.format("像素拾取失败 | 文件无效:%s", targetImagePath));
return;
}
Intent intent = new Intent(getApplicationContext(), PixelPickerActivity.class);
intent.putExtra("imagePath", targetImagePath);
startActivityForResult(intent, REQUEST_PIXELPICKER);
LogUtils.d(TAG, String.format("像素拾取启动 | 路径:%s", targetImagePath));
}
};
private View.OnClickListener onCleanPixelClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "onCleanPixelClickListener() | 清空像素颜色");
BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
if (previewBean == null) {
LogUtils.e(TAG, "onCleanPixelClickListener() | 预览Bean为空");
return;
}
int oldColor = previewBean.getPixelColor();
previewBean.setPixelColor(ImageUtils.getColorAccent(BackgroundSettingsActivity.this));
mBgSourceUtils.saveSettings();
doubleRefreshPreview();
isPreviewBackgroundChanged = true;
ToastUtils.show("像素颜色已清空");
LogUtils.d(TAG, String.format("像素清空 | 旧颜色:#%08X", oldColor));
}
};
private View.OnClickListener onColorPaletteClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "onColorPaletteClickListener() | 调色板按钮");
final BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
if (previewBean == null) {
LogUtils.e(TAG, "onColorPaletteClickListener() | 预览Bean为空");
return;
}
int initialColor = previewBean.getPixelColor();
LogUtils.d(TAG, String.format("调色板 | 初始颜色:#%08X", initialColor));
ColorPaletteDialog dialog = new ColorPaletteDialog(BackgroundSettingsActivity.this, initialColor, new ColorPaletteDialog.OnColorSelectedListener() {
@Override
public void onColorSelected(int color) {
previewBean.setPixelColor(color);
mBgSourceUtils.saveSettings();
doubleRefreshPreview();
isPreviewBackgroundChanged = true;
LogUtils.d(TAG, String.format("颜色选择 | 选中颜色:#%08X", color));
}
});
dialog.show();
LogUtils.d(TAG, "调色板 | 对话框已显示");
}
};
// ====================== 工具方法(通用工具 + 视图工具)======================
/**
* 生成 FileProvider Uri适配 Android 7.0+
* @param file 目标文件
* @return 适配后的Uri失败返回null
*/
public Uri getFileProviderUri(File file) {
LogUtils.d(TAG, String.format("getFileProviderUri() | 文件路径:%s", (file != null ? file.getAbsolutePath() : "null")));
if (file == null) {
LogUtils.e(TAG, "getFileProviderUri() | 文件为空");
return null;
}
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
String FILE_PROVIDER_AUTHORITY = getPackageName() + ".fileprovider";
return FileProvider.getUriForFile(this, FILE_PROVIDER_AUTHORITY, file);
} else {
return Uri.fromFile(file);
}
} catch (Exception e) {
LogUtils.e(TAG, String.format("getFileProviderUri() | 生成Uri失败%s", e.getMessage()));
return null;
}
}
/**
* 校验 Bitmap 是否有效(未被回收且不为空)
* @param bitmap 目标Bitmap
* @return 有效返回true否则false
*/
private boolean isBitmapValid(Bitmap bitmap) {
boolean isValid = bitmap != null && !bitmap.isRecycled();
LogUtils.d(TAG, String.format("isBitmapValid() | Bitmap有效性校验%b", isValid));
return isValid;
}
/**
* 双重刷新预览,确保背景加载最新数据
*/
private void doubleRefreshPreview() {
LogUtils.d(TAG, "doubleRefreshPreview() 开始双重刷新预览");
if (mBgSourceUtils == null || mBackgroundView == null || isFinishing()) {
LogUtils.w(TAG, "双重刷新 跳过对象为空或Activity已结束");
return;
}
// 第一重刷新
try {
mBgSourceUtils.loadSettings();
BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
mBackgroundView.loadByBackgroundBean(previewBean, true);
LogUtils.d(TAG, "双重刷新 第一重完成");
} catch (Exception e) {
LogUtils.e(TAG, String.format("双重刷新 第一重异常:%s", e.getMessage()));
return;
}
// 第二重刷新(延迟执行)
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
if (mBackgroundView != null && !isFinishing() && mBgSourceUtils != null) {
try {
mBgSourceUtils.loadSettings();
BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
mBackgroundView.loadByBackgroundBean(previewBean, true);
LogUtils.d(TAG, "双重刷新 第二重完成");
} catch (Exception e) {
LogUtils.e(TAG, String.format("双重刷新 第二重异常:%s", e.getMessage()));
}
}
}
}, 200);
}
// ====================== 业务逻辑方法(按功能分类)======================
/**
* 初始化核心组件(工具类+视图)
*/
private void initCoreComponents() {
LogUtils.d(TAG, "initCoreComponents() 开始初始化");
// 初始化视图
mBackgroundView = findViewById(R.id.background_view);
if (mBackgroundView == null) {
LogUtils.e(TAG, "initCoreComponents() | BackgroundView未找到");
}
// 初始化工具类
mBgSourceUtils = BackgroundSourceUtils.getInstance(this);
mBgSourceUtils.loadSettings();
mBitmapCache = BitmapCacheUtils.getInstance();
LogUtils.d(TAG, "initCoreComponents() 视图与工具类加载完成");
}
/**
* 处理意图或初始化预览
*/
private void handleIntentOrPreview() {
LogUtils.d(TAG, "handleIntentOrPreview() 开始处理");
if (handleShareIntent()) {
ToastUtils.show("已接收分享图片");
LogUtils.d(TAG, "handleIntentOrPreview() | 处理分享意图成功");
} else {
mBgSourceUtils.setCurrentSourceToPreview();
LogUtils.d(TAG, "handleIntentOrPreview() | 加载当前背景配置");
}
}
/**
* 初始化预览环境
*/
private void initPreviewEnvironment() {
LogUtils.d(TAG, "initPreviewEnvironment() 开始初始化");
BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
mBgSourceUtils.createAndUpdatePreviewEnvironmentForCropping(previewBean);
doubleRefreshPreview();
LogUtils.d(TAG, "initPreviewEnvironment() 初始化完成");
}
/**
* 处理分享意图
* @return 处理成功返回true否则false
*/
private boolean handleShareIntent() {
LogUtils.d(TAG, "handleShareIntent() 开始处理");
Intent intent = getIntent();
if (intent != null) {
String action = intent.getAction();
String type = intent.getType();
LogUtils.d(TAG, String.format("分享处理 | action%stype%s", action, type));
if (Intent.ACTION_SEND.equals(action) && type != null && isImageType(type)) {
showSharePreviewDialog();
return true;
}
}
return false;
}
/**
* 显示分享图片预览对话框
*/
private void showSharePreviewDialog() {
LogUtils.d(TAG, "showSharePreviewDialog() 开始显示");
BackgroundPicturePreviewDialog dlg = new BackgroundPicturePreviewDialog(this, new BackgroundPicturePreviewDialog.IOnRecivedPictureListener() {
@Override
public void onAcceptRecivedPicture(Uri uriRecivedPicture) {
LogUtils.d(TAG, String.format("分享确认 | Uri%s", uriRecivedPicture.toString()));
if (putUriFileToPreviewSource(uriRecivedPicture)) {
startImageCrop(false);
}
}
});
dlg.show();
LogUtils.d(TAG, "分享处理 | 显示图片预览对话框");
}
/**
* 判断是否为图片类型
* @param mimeType MIME类型
* @return 是图片返回true否则false
*/
private boolean isImageType(String mimeType) {
if (mimeType == null) {
return false;
}
String lowerMimeType = mimeType.toLowerCase();
LogUtils.d(TAG, String.format("isImageType() | mimeType: %s, lowerMimeType: %s", mimeType, lowerMimeType));
return lowerMimeType.startsWith("image/");
}
/**
* 启动图片选择器
*/
private void launchImageSelector() {
LogUtils.d(TAG, "launchImageSelector() 启动图片选择器");
Intent[] intents = createImageSelectorIntents();
Intent validIntent = findValidIntent(intents);
if (validIntent != null) {
launchImageChooser(validIntent);
} else {
showNoGalleryDialog();
}
}
/**
* 创建图片选择器意图数组
* @return 意图数组
*/
private Intent[] createImageSelectorIntents() {
LogUtils.d(TAG, "createImageSelectorIntents() 开始创建");
Intent[] intents = new Intent[3];
// ACTION_GET_CONTENT
Intent getContentIntent = new Intent(Intent.ACTION_GET_CONTENT);
getContentIntent.setType("image/*");
getContentIntent.addCategory(Intent.CATEGORY_OPENABLE);
getContentIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intents[0] = getContentIntent;
// ACTION_PICK
Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
pickIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intents[1] = pickIntent;
// ACTION_OPEN_DOCUMENTAPI19+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Intent openDocIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
openDocIntent.setType("image/*");
openDocIntent.addCategory(Intent.CATEGORY_OPENABLE);
openDocIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
intents[2] = openDocIntent;
}
LogUtils.d(TAG, "createImageSelectorIntents() 意图数组创建完成");
return intents;
}
/**
* 查找有效的意图
* @param intents 意图数组
* @return 有效意图无则返回null
*/
private Intent findValidIntent(Intent[] intents) {
LogUtils.d(TAG, "findValidIntent() 开始查找");
for (Intent intent : intents) {
if (intent != null && intent.resolveActivity(getPackageManager()) != null) {
LogUtils.d(TAG, "findValidIntent() | 找到有效意图");
return intent;
}
}
LogUtils.d(TAG, "findValidIntent() | 无有效意图");
return null;
}
/**
* 启动图片选择器
* @param validIntent 有效意图
*/
private void launchImageChooser(Intent validIntent) {
LogUtils.d(TAG, "launchImageChooser() 启动选择器");
Intent chooser = Intent.createChooser(validIntent, "选择图片");
chooser.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
startActivityForResult(chooser, REQUEST_SELECT_PICTURE);
LogUtils.d(TAG, "launchImageChooser() | 启动图片选择");
}
/**
* 显示无相册应用提示对话框
*/
private void showNoGalleryDialog() {
LogUtils.d(TAG, "showNoGalleryDialog() | 无相册应用");
runOnUiThread(new Runnable() {
@Override
public void run() {
ToastUtils.show("未找到相册应用,请安装后重试");
new AlertDialog.Builder(BackgroundSettingsActivity.this)
.setTitle("无图片选择应用")
.setMessage("需要安装相册应用才能选择图片")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
launchGalleryMarket();
}
})
.setNegativeButton("取消", null)
.show();
}
});
}
/**
* 启动应用商店下载相册
*/
private void launchGalleryMarket() {
LogUtils.d(TAG, "launchGalleryMarket() 启动应用商店");
Intent marketIntent = new Intent(Intent.ACTION_VIEW);
marketIntent.setData(Uri.parse("market://details?id=com.android.gallery3d"));
if (marketIntent.resolveActivity(getPackageManager()) != null) {
startActivity(marketIntent);
LogUtils.d(TAG, "launchGalleryMarket() | 启动成功");
} else {
ToastUtils.show("无法打开应用商店");
LogUtils.e(TAG, "launchGalleryMarket() | 启动失败");
}
}
/**
* 处理操作取消或失败
*/
private void handleOperationCancelOrFail() {
LogUtils.d(TAG, "handleOperationCancelOrFail() 操作取消或失败");
mBgSourceUtils.setCurrentSourceToPreview();
ToastUtils.show("操作取消或失败");
doubleRefreshPreview();
}
/**
* 处理拍照逻辑(权限通过后执行)
*/
void handleTakePhoto() {
LogUtils.d(TAG, "handleTakePhoto() 开始处理拍照");
BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
if (previewBean == null) {
LogUtils.e(TAG, "handleTakePhoto() | 预览Bean为空");
ToastUtils.show("拍照文件创建失败");
return;
}
File takePhotoFile = new File(previewBean.getBackgroundFilePath());
if (!takePhotoFile.exists()) {
ToastUtils.show("拍照文件创建失败");
LogUtils.e(TAG, String.format("handleTakePhoto() | 文件不存在:%s", takePhotoFile.getAbsolutePath()));
return;
}
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
Uri photoUri = getFileProviderUri(takePhotoFile);
if (photoUri == null) {
throw new Exception("生成FileProvider Uri失败");
}
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
LogUtils.d(TAG, String.format("handleTakePhoto() | Uri%s", photoUri.toString()));
} catch (Exception e) {
String errMsg = "拍照启动异常:" + e.getMessage();
ToastUtils.show(errMsg.substring(0, 20));
LogUtils.e(TAG, String.format("handleTakePhoto() | %s", e.getMessage()));
}
}
/**
* 处理ActivityResult分发
* @param requestCode 请求码
* @param data 回调数据
*/
private void handleActivityResult(int requestCode, Intent data) {
LogUtils.d(TAG, String.format("handleActivityResult() | 处理请求码:%d", requestCode));
switch (requestCode) {
case REQUEST_SELECT_PICTURE:
handleSelectPictureResult(data);
break;
case REQUEST_TAKE_PHOTO:
handleTakePhotoResult(data);
break;
case REQUEST_CROP_IMAGE:
handleCropImageResult(data);
break;
case REQUEST_PIXELPICKER:
handlePixelPickerResult();
break;
default:
LogUtils.d(TAG, String.format("handleActivityResult() | 未知requestCode%d", requestCode));
break;
}
}
/**
* 处理拍照结果
* @param data 回调数据
*/
private void handleTakePhotoResult(Intent data) {
LogUtils.d(TAG, "handleTakePhotoResult() 处理拍照结果");
BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
if (previewBean == null) {
LogUtils.e(TAG, "handleTakePhotoResult() | 预览Bean为空");
return;
}
previewBean.setIsUseBackgroundFile(true);
previewBean.setIsUseBackgroundScaledCompressFile(false);
mBgSourceUtils.saveSettings();
doubleRefreshPreview();
startImageCrop(false);
LogUtils.d(TAG, "handleTakePhotoResult() | 已启动裁剪");
}
/**
* 处理选图结果
* @param data 回调数据
*/
private void handleSelectPictureResult(Intent data) {
LogUtils.d(TAG, "handleSelectPictureResult() 处理选图结果");
Uri selectedImage = data.getData();
if (selectedImage == null) {
ToastUtils.show("图片Uri为空");
LogUtils.e(TAG, "handleSelectPictureResult() | Uri为空");
return;
}
LogUtils.d(TAG, String.format("handleSelectPictureResult() | 系统返回Uri : %s", selectedImage.toString()));
// 申请持久化权限API33+
if (Build.VERSION.SDK_INT >= SDK_VERSION_TIRAMISU) {
getContentResolver().takePersistableUriPermission(
selectedImage,
Intent.FLAG_GRANT_READ_URI_PERMISSION);
LogUtils.d(TAG, "handleSelectPictureResult() | 已添加持久化权限");
}
// 同步文件并启动裁剪
if (putUriFileToPreviewSource(selectedImage)) {
LogUtils.d(TAG, "handleSelectPictureResult() | 路径绑定完成");
startImageCrop(false);
} else {
ToastUtils.show("图片同步失败");
LogUtils.e(TAG, "handleSelectPictureResult() | 文件复制失败");
}
}
/**
* 将 Uri 文件同步到预览 Bean
* @param srcUriFile 源Uri
* @return 同步成功返回true否则false
*/
private boolean putUriFileToPreviewSource(Uri srcUriFile) {
LogUtils.d(TAG, String.format("putUriFileToPreviewSource() | 源Uri%s", srcUriFile.toString()));
String filePath = UriUtils.getFilePathFromUri(this, srcUriFile);
if (TextUtils.isEmpty(filePath)) {
LogUtils.e(TAG, "putUriFileToPreviewSource() | Uri解析路径为空");
return false;
}
File srcFile = new File(filePath);
return putUriFileToPreviewSource(srcFile);
}
/**
* 将 File 同步到预览 Bean
* @param srcFile 源文件
* @return 同步成功返回true否则false
*/
private boolean putUriFileToPreviewSource(File srcFile) {
LogUtils.d(TAG, String.format("putUriFileToPreviewSource() | 源文件:%s", srcFile.getAbsolutePath()));
mBgSourceUtils.loadSettings();
BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
File dstFile = new File(previewBean.getBackgroundFilePath());
LogUtils.d(TAG, String.format("putUriFileToPreviewSource() | 目标文件:%s", dstFile.getAbsolutePath()));
if (FileUtils.copyFile(srcFile, dstFile)) {
LogUtils.d(TAG, "putUriFileToPreviewSource() | 文件拷贝成功");
return true;
}
LogUtils.d(TAG, "putUriFileToPreviewSource() | 文件无法拷贝");
return false;
}
/**
* 处理裁剪结果
* @param data 回调数据
*/
private void handleCropImageResult(Intent data) {
LogUtils.d(TAG, "handleCropImageResult() 处理裁剪结果");
BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
if (previewBean == null) {
LogUtils.e(TAG, "handleCropImageResult() | 预览Bean为空");
handleOperationCancelOrFail();
return;
}
File cropTempFile = new File(previewBean.getBackgroundScaledCompressFilePath());
boolean isFileExist = cropTempFile.exists();
boolean isFileReadable = isFileExist ? cropTempFile.canRead() : false;
long fileSize = isFileExist ? cropTempFile.length() : 0;
boolean isCropSuccess = isFileExist && isFileReadable && fileSize > 100;
if (isCropSuccess) {
handleCropSuccess(previewBean, fileSize);
} else {
handleCropFailure(isFileExist, isFileReadable, fileSize);
}
}
/**
* 处理裁剪成功
* @param previewBean 预览Bean
* @param fileSize 文件大小
*/
private void handleCropSuccess(BackgroundBean previewBean, long fileSize) {
LogUtils.d(TAG, String.format("handleCropSuccess() | 裁剪成功,文件大小:%d", fileSize));
isPreviewBackgroundChanged = true;
previewBean.setIsUseBackgroundFile(true);
previewBean.setIsUseBackgroundScaledCompressFile(true);
mBgSourceUtils.saveSettings();
doubleRefreshPreview();
}
/**
* 处理裁剪失败
* @param isFileExist 文件是否存在
* @param isFileReadable 文件是否可读
* @param fileSize 文件大小
*/
private void handleCropFailure(boolean isFileExist, boolean isFileReadable, long fileSize) {
LogUtils.e(TAG, String.format("handleCropFailure() | 裁剪失败,文件状态:存在=%b可读=%b大小=%d",
isFileExist, isFileReadable, fileSize));
handleOperationCancelOrFail();
}
/**
* 处理像素拾取结果
*/
private void handlePixelPickerResult() {
LogUtils.d(TAG, "handlePixelPickerResult() 处理像素拾取结果");
doubleRefreshPreview();
isPreviewBackgroundChanged = true;
}
/**
* 处理相机权限申请结果
* @param grantResults 权限结果数组
*/
private void handleCameraPermissionResult(int[] grantResults) {
LogUtils.d(TAG, "handleCameraPermissionResult() 处理相机权限结果");
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
LogUtils.d(TAG, "handleCameraPermissionResult() | 相机权限授予成功");
handleTakePhoto();
} else {
LogUtils.d(TAG, "handleCameraPermissionResult() | 相机权限授予失败");
ToastUtils.show("相机权限被拒绝,无法拍照");
// 引导用户到设置页面开启权限(用户选择不再询问时)
if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
launchAppSettings();
}
}
}
/**
* 启动应用设置页面
*/
private void launchAppSettings() {
LogUtils.d(TAG, "launchAppSettings() 启动应用设置页面");
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
ToastUtils.show("请在设置中开启相机权限");
}
/**
* 处理Finish确认对话框
*/
private void handleFinishConfirmation() {
LogUtils.d(TAG, "handleFinishConfirmation() 处理Finish确认");
if (isPreviewBackgroundChanged) {
YesNoAlertDialog.show(this, "背景更换问题", "是否确定背景图片设置?", new YesNoAlertDialog.OnDialogResultListener() {
@Override
public void onYes() {
mBgSourceUtils.commitPreviewSourceToCurrent();
isCommitSettings = true;
finish();
Intent mainIntent = new Intent(BackgroundSettingsActivity.this, MainActivity.class);
mainIntent.putExtra(MainActivity.EXTRA_ISRELOAD_BACKGROUNDVIEW, true);
startActivity(mainIntent);
LogUtils.d(TAG, "handleFinishConfirmation() | 确认设置启动MainActivity并刷新背景");
}
@Override
public void onNo() {
isCommitSettings = true;
finish();
LogUtils.d(TAG, "handleFinishConfirmation() | 取消设置,关闭页面");
}
});
} else {
isCommitSettings = true;
finish();
}
}
/**
* 启动图片裁剪
* @param isFreeCrop 是否自由裁剪
*/
private void startImageCrop(boolean isFreeCrop) {
LogUtils.d(TAG, String.format("startImageCrop() | 是否自由裁剪:%b", isFreeCrop));
BackgroundBean previewBean = mBgSourceUtils.getPreviewBackgroundBean();
if (previewBean == null) {
LogUtils.e(TAG, "startImageCrop() | 预览Bean为空");
ToastUtils.show("裁剪失败:无有效图片");
return;
}
int width = isFreeCrop ? 0 : mBackgroundView.getWidth();
int height = isFreeCrop ? 0 : mBackgroundView.getHeight();
ImageCropUtils.startImageCrop(BackgroundSettingsActivity.this,
previewBean,
width,
height,
isFreeCrop,
REQUEST_CROP_IMAGE);
LogUtils.d(TAG, String.format("startImageCrop() | 目标尺寸:%dx%d", width, height));
}
}

View File

@@ -1,10 +1,5 @@
package cc.winboll.studio.powerbell.activities;
/**
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2025/10/22 13:21
* @Describe BatteryReportActivity
*/
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
@@ -23,8 +18,11 @@ import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cc.winboll.studio.libaes.interfaces.IWinBoLLActivity;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.powerbell.R;
import java.util.ArrayList;
import java.util.Collections;
@@ -32,88 +30,91 @@ import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cc.winboll.studio.libappbase.LogUtils;
public class BatteryReportActivity extends Activity {
/**
* 电池报告页面统计应用24小时运行时长与电池消耗情况
* 支持应用搜索、累计耗电计算、电池广播监听,适配 API30
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
*/
public class BatteryReportActivity extends WinBoLLActivity implements IWinBoLLActivity {
// ======================== 静态常量(按功能分类) =========================
public static final String TAG = "BatteryReportActivity";
private static final long ONE_DAY_MS = 24 * 3600 * 1000; // 24小时毫秒数
private static final long ONE_MINUTE_MS = 60 * 1000; // 1分钟毫秒数
// ======================== 成员变量(按依赖优先级+功能分类) =========================
// UI组件
private Toolbar mToolbar;
private RecyclerView rvBatteryReport;
private BatteryReportAdapter adapter;
private List<AppBatteryModel> dataList = new ArrayList<AppBatteryModel>();
private List<AppBatteryModel> filteredList = new ArrayList<AppBatteryModel>();
private BroadcastReceiver batteryReceiver;
private int batteryCapacity = 5400; // 电池容量mAh
private float lastBatteryPercent = 100.0f;
private long lastCheckTime = System.currentTimeMillis();
private EditText etSearch;
private Map<String, Long> appRunTimeCache = new HashMap<String, Long>();
private Map<String, String> packageToAppNameCache = new HashMap<String, String>();
// 数据与适配器
private BatteryReportAdapter adapter;
private List<AppBatteryModel> dataList = new ArrayList<>();
private List<AppBatteryModel> filteredList = new ArrayList<>();
// 电池相关
private BroadcastReceiver batteryReceiver;
private int batteryCapacity = 5400; // 电池容量mAh
private float lastBatteryPercent = 100.0f; // 上次电池百分比
private long lastCheckTime = System.currentTimeMillis(); // 上次检查时间戳
// 缓存相关
private Map<String, Long> appRunTimeCache = new HashMap<>();
private Map<String, String> packageToAppNameCache = new HashMap<>();
private PackageManager mPackageManager;
// ======================== 接口实现方法 =========================
@Override
public Activity getActivity() {
return this;
}
@Override
public String getTag() {
return TAG;
}
// ======================== 生命周期方法(按执行顺序排列) =========================
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_battery_report);
LogUtils.d(TAG, "【onCreate】BatteryReportActivity 初始化开始");
// 初始化UI组件
initView();
// 初始化PackageManager
mPackageManager = getPackageManager();
LogUtils.d(TAG, "【onCreate】基础组件初始化完成");
// 权限检查Java7 传统条件判断)
if (!hasUsageStatsPermission(this)) {
Toast.makeText(this, "请进入设置-应用-权限-特殊访问权限-使用情况访问权限,开启本应用的权限", Toast.LENGTH_LONG).show();
startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
LogUtils.w(TAG, "【onCreate】缺少使用情况访问权限引导用户开启");
return;
}
etSearch = (EditText) findViewById(R.id.et_search);
rvBatteryReport = (RecyclerView) findViewById(R.id.rv_battery_report);
rvBatteryReport.setLayoutManager(new LinearLayoutManager(this));
// 初始化流程新增“加载24小时累计耗电”步骤
// 初始化数据流程:加载应用→缓存名称→获取运行时长→计算初始累计耗电
loadAllAppPackage();
preCacheAllAppNames();
appRunTimeCache = getAppRunTime();
updateAppRunTimeToModel();
calculateInitial24hTotalConsumption(); // 初始化时计算24小时累计耗电
calculateInitial24hTotalConsumption();
filteredList.addAll(dataList);
LogUtils.d(TAG, "【onCreate】数据初始化完成原始数据量" + dataList.size());
// 初始化适配器
adapter = new BatteryReportAdapter(this, filteredList, mPackageManager, packageToAppNameCache);
rvBatteryReport.setAdapter(adapter);
LogUtils.d(TAG, "【onCreate】适配器初始化完成过滤后数据量" + filteredList.size());
// 搜索监听(不变)
etSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
// 绑定搜索监听 + 注册电池广播
bindSearchListener();
registerBatteryReceiver();
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
filterAppsByPackageAndName(s.toString());
}
@Override
public void afterTextChanged(Editable s) {}
});
// 电池广播:调用修改后的“单次耗电计算+累计累加”方法
batteryReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra("level", 100);
int scale = intent.getIntExtra("scale", 100);
float currentPercent = (float) level / scale * 100;
LogUtils.d(TAG, "电池百分比变化:" + lastBatteryPercent + " -> " + currentPercent);
if (currentPercent < lastBatteryPercent) {
float dropPercent = lastBatteryPercent - currentPercent;
long duration = System.currentTimeMillis() - lastCheckTime;
LogUtils.d(TAG, "电池消耗:" + dropPercent + "%,时长:" + duration + "ms");
appRunTimeCache = getAppRunTime();
updateAppRunTimeToModel();
calculateSingleConsumptionAndAccumulate(dropPercent, appRunTimeCache); // 单次+累计逻辑
}
lastBatteryPercent = currentPercent;
lastCheckTime = System.currentTimeMillis();
}
};
registerReceiver(batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
LogUtils.d(TAG, "【onCreate】BatteryReportActivity 初始化完成");
}
@Override
@@ -122,82 +123,213 @@ public class BatteryReportActivity extends Activity {
// Java7 显式非空判断
if (batteryReceiver != null) {
unregisterReceiver(batteryReceiver);
LogUtils.d(TAG, "【onDestroy】电池广播已注销");
}
LogUtils.d(TAG, "【onDestroy】BatteryReportActivity 销毁完成");
}
// ======================== UI初始化方法 =========================
private void initView() {
// 初始化Toolbar
mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mToolbar.setSubtitle(getTag());
mToolbar.setTitleTextAppearance(this, R.style.Toolbar_TitleText);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "【导航栏】点击返回");
finish();
}
});
// 初始化RecyclerView与搜索框
etSearch = (EditText) findViewById(R.id.et_search);
rvBatteryReport = (RecyclerView) findViewById(R.id.rv_battery_report);
rvBatteryReport.setLayoutManager(new LinearLayoutManager(this));
LogUtils.d(TAG, "【initView】UI组件初始化完成");
}
// ======================== 搜索监听绑定方法 =========================
private void bindSearchListener() {
etSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String keyword = s.toString().trim();
LogUtils.d(TAG, "【bindSearchListener】搜索关键词变化" + keyword);
filterAppsByPackageAndName(keyword);
}
@Override
public void afterTextChanged(Editable s) {}
});
LogUtils.d(TAG, "【bindSearchListener】搜索监听绑定完成");
}
// ======================== 电池广播注册方法 =========================
private void registerBatteryReceiver() {
batteryReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra("level", 100);
int scale = intent.getIntExtra("scale", 100);
float currentPercent = (float) level / scale * 100;
LogUtils.d(TAG, "【电池广播】电池百分比变化:" + lastBatteryPercent + " -> " + currentPercent);
if (currentPercent < lastBatteryPercent) {
float dropPercent = lastBatteryPercent - currentPercent;
long duration = System.currentTimeMillis() - lastCheckTime;
LogUtils.d(TAG, "【电池广播】电池消耗:" + dropPercent + "%,时长:" + formatRunTime(duration));
// 更新运行时长并计算耗电
appRunTimeCache = getAppRunTime();
updateAppRunTimeToModel();
calculateSingleConsumptionAndAccumulate(dropPercent, appRunTimeCache);
}
// 刷新记录
lastBatteryPercent = currentPercent;
lastCheckTime = System.currentTimeMillis();
}
};
registerReceiver(batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
LogUtils.d(TAG, "【registerBatteryReceiver】电池广播注册完成");
}
// ======================== 权限检查方法 =========================
/**
* 加载所有应用仅获取包名初始化模型时单次耗电、累计耗电均设为0
* 检查是否拥有使用情况访问权限
* @param context 上下文
* @return 拥有权限返回true否则返回false
*/
private boolean hasUsageStatsPermission(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
LogUtils.w(TAG, "【hasUsageStatsPermission】系统版本低于LOLLIPOP不支持使用情况访问权限");
return false;
}
android.app.usage.UsageStatsManager manager =
(android.app.usage.UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
if (manager == null) {
LogUtils.e(TAG, "【hasUsageStatsPermission】获取UsageStatsManager失败");
return false;
}
long endTime = System.currentTimeMillis();
long startTime = endTime - ONE_MINUTE_MS;
List<android.app.usage.UsageStats> statsList = manager.queryUsageStats(
android.app.usage.UsageStatsManager.INTERVAL_DAILY, startTime, endTime);
boolean hasPermission = statsList != null && !statsList.isEmpty();
LogUtils.d(TAG, "【hasUsageStatsPermission】使用情况访问权限检查结果" + hasPermission);
return hasPermission;
}
// ======================== 数据加载与缓存方法 =========================
/**
* 加载所有应用包名,初始化数据模型
*/
private void loadAllAppPackage() {
List<ApplicationInfo> appList = mPackageManager.getInstalledApplications(PackageManager.GET_META_DATA);
dataList.clear();
LogUtils.d(TAG, "开始加载应用包名列表,共找到" + appList.size() + "个应用");
LogUtils.d(TAG, "【loadAllAppPackage】开始加载应用包名列表共找到" + appList.size() + "个应用");
for (ApplicationInfo appInfo : appList) {
String packageName = appInfo.packageName;
// 初始化单次耗电consumption=0累计耗电totalConsumption=0运行时长=0
dataList.add(new AppBatteryModel(packageName, 0.0f, 0.0f, 0));
}
LogUtils.d(TAG, "应用包名列表加载完成,共添加" + dataList.size() + "个包名。");
LogUtils.d(TAG, "【loadAllAppPackage】应用包名列表加载完成共添加" + dataList.size() + "个包名");
}
/**
* 预缓存应用名称(逻辑不变)
* 预缓存所有应用名称减少PackageManager重复调用
*/
private void preCacheAllAppNames() {
packageToAppNameCache.clear();
LogUtils.d(TAG, "开始预缓存包名-应用名称映射");
LogUtils.d(TAG, "【preCacheAllAppNames】开始预缓存包名-应用名称映射");
for (AppBatteryModel model : dataList) {
String packageName = model.getPackageName();
String appName = getAppNameByPackage(packageName);
packageToAppNameCache.put(packageName, appName);
}
LogUtils.d(TAG, "预缓存完成,共缓存" + packageToAppNameCache.size() + "个应用名称");
LogUtils.d(TAG, "【preCacheAllAppNames】预缓存完成共缓存" + packageToAppNameCache.size() + "个应用名称");
}
/**
* 通过包名获取应用名称(逻辑不变)
* 通过包名获取应用名称,带异常处理
* @param packageName 应用包名
* @return 应用名称,获取失败返回包名
*/
private String getAppNameByPackage(String packageName) {
LogUtils.v(TAG, "【getAppNameByPackage】查询包名" + packageName);
try {
ApplicationInfo appInfo = mPackageManager.getApplicationInfo(packageName, 0);
return mPackageManager.getApplicationLabel(appInfo).toString();
} catch (PackageManager.NameNotFoundException e) {
LogUtils.e(TAG, "包名" + packageName + "对应的应用未找到:" + e.getMessage());
LogUtils.e(TAG, "【getAppNameByPackage】包名" + packageName + "对应的应用未找到:" + e.getMessage());
return packageName;
} catch (Exception e) {
LogUtils.e(TAG, "查询应用名称失败(包名:" + packageName + "" + e.getMessage());
LogUtils.e(TAG, "【getAppNameByPackage】查询应用名称失败(包名:" + packageName + "" + e.getMessage());
return packageName;
}
}
/**
* 更新运行时长到模型(逻辑不变)
* 更新运行时长到数据模型
*/
private void updateAppRunTimeToModel() {
int nCount = 0;
int updateCount = 0;
for (AppBatteryModel model : dataList) {
String packageName = model.getPackageName();
Long runTime;
if (appRunTimeCache.containsKey(packageName)) {
runTime = appRunTimeCache.get(packageName);
LogUtils.d(TAG, String.format("应用包 %s 运行时长已更新。", packageName));
nCount++;
} else {
runTime = 0L;
}
Long runTime = appRunTimeCache.containsKey(packageName) ? appRunTimeCache.get(packageName) : 0L;
model.setRunTime(runTime);
if (runTime > 0) {
updateCount++;
}
}
LogUtils.d(TAG, String.format("dataList.size() %d appRunTimeCache.size() %d。", dataList.size(), appRunTimeCache.size()));
LogUtils.d(TAG, String.format("updateAppRunTimeToModel() 更新的数据量为:%d", nCount));
LogUtils.d(TAG, "【updateAppRunTimeToModel】更新完成数据量" + dataList.size() + ",更新运行时长应用数:" + updateCount);
}
/**
* 【新增】初始化时计算24小时累计耗电赋值给totalConsumption
* 获取应用24小时运行时长
* @return 应用包名-运行时长ms映射
*/
private Map<String, Long> getAppRunTime() {
Map<String, Long> runTimeMap = new HashMap<>();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
try {
android.app.usage.UsageStatsManager manager =
(android.app.usage.UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
long endTime = System.currentTimeMillis();
long startTime = endTime - ONE_DAY_MS; // 近24小时
List<android.app.usage.UsageStats> statsList = manager.queryUsageStats(
android.app.usage.UsageStatsManager.INTERVAL_DAILY, startTime, endTime);
for (android.app.usage.UsageStats stats : statsList) {
long runTimeMs = stats.getTotalTimeInForeground();
String packageName = stats.getPackageName();
runTimeMap.put(packageName, runTimeMs);
LogUtils.v(TAG, "【getAppRunTime】包名" + packageName + "24小时运行时长" + formatRunTime(runTimeMs));
if (packageName.equals("aidepro.top")) {
LogUtils.d(TAG, "【getAppRunTime】特殊查询包名" + packageName + "有结果");
}
}
} catch (Exception e) {
LogUtils.e(TAG, "【getAppRunTime】获取应用运行时长失败" + e.getMessage());
}
}
LogUtils.d(TAG, "【getAppRunTime】应用运行时长列表数量" + runTimeMap.size());
return runTimeMap;
}
// ======================== 核心计算方法 =========================
/**
* 初始化时计算24小时累计耗电赋值给totalConsumption
* 逻辑基于24小时运行时长占比分配当前电池容量的理论24小时消耗
*/
private void calculateInitial24hTotalConsumption() {
@@ -206,53 +338,54 @@ public class BatteryReportActivity extends Activity {
for (Map.Entry<String, Long> entry : appRunTimeCache.entrySet()) {
total24hRunTime += entry.getValue();
}
LogUtils.d(TAG, "24小时内所有应用总运行时长" + formatRunTime(total24hRunTime));
LogUtils.d(TAG, "【calculateInitial24hTotalConsumption】24小时内所有应用总运行时长" + formatRunTime(total24hRunTime));
// 2. 按运行时长占比分配24小时累计耗电假设电池满电循环用总容量近似24小时总消耗
// 2. 按运行时长占比分配24小时累计耗电
for (AppBatteryModel model : dataList) {
String packageName = model.getPackageName();
Long app24hRunTime = appRunTimeCache.getOrDefault(packageName, 0L);
// 计算占比与累计耗电
float ratio = (total24hRunTime > 0) ? (float) app24hRunTime / total24hRunTime : 0;
float initialTotalConsumption = batteryCapacity * ratio; // 用电池容量近似24小时总消耗
model.setTotalConsumption(initialTotalConsumption); // 初始化累计耗电
LogUtils.d(TAG, String.format("应用包 %s 24小时累计耗电初始化%.1f mAh", packageName, initialTotalConsumption));
float initialTotalConsumption = batteryCapacity * ratio;
model.setTotalConsumption(initialTotalConsumption);
LogUtils.v(TAG, "【calculateInitial24hTotalConsumption】应用包" + packageName + "24小时累计耗电初始化" + initialTotalConsumption + " mAh");
}
LogUtils.d(TAG, "【calculateInitial24hTotalConsumption】24小时累计耗电初始化完成");
}
/**
* 【核心修改】计算单次耗电赋值给consumption+ 累加至累计耗电totalConsumption = totalConsumption + consumption
* 计算单次耗电赋值给consumption+ 累加至累计耗电
* @param dropPercent 电池下降百分比
* @param runTimeMap 应用运行时长映射
*/
private void calculateSingleConsumptionAndAccumulate(float dropPercent, Map<String, Long> runTimeMap) {
LogUtils.d(TAG, "【calculateSingleConsumptionAndAccumulate】开始计算电池下降百分比" + dropPercent);
long totalSingleRunTime = 0;
// 1. 计算本次电池下降期间的总运行时长
for (Map.Entry<String, Long> entry : runTimeMap.entrySet()) {
totalSingleRunTime += entry.getValue();
}
LogUtils.d(TAG, "【calculateSingleConsumptionAndAccumulate】本次电池下降总运行时长" + formatRunTime(totalSingleRunTime));
// 2. 遍历计算每个应用的单次耗电”并“累加至累计”
// 2. 遍历计算每个应用的单次耗电并累加
for (AppBatteryModel model : dataList) {
String packageName = model.getPackageName();
Long appSingleRunTime = runTimeMap.getOrDefault(packageName, 0L);
// 步骤1计算本次单次耗电赋值给consumption
float ratio = (totalSingleRunTime > 0) ? (float) appSingleRunTime / totalSingleRunTime : 0;
float singleConsumption = batteryCapacity * dropPercent / 100 * ratio; // 单次消耗
model.setConsumption(singleConsumption); // 存储单次耗电
float singleConsumption = batteryCapacity * dropPercent / 100 * ratio;
model.setConsumption(singleConsumption);
// 步骤2累加单次耗电到累计耗电totalConsumption = 原有累计 + 本次单次)
// 累加至累计耗电
float newTotalConsumption = model.getTotalConsumption() + singleConsumption;
model.setTotalConsumption(newTotalConsumption); // 更新累计耗电
// 同步运行时长
model.setTotalConsumption(newTotalConsumption);
model.setRunTime(appSingleRunTime);
LogUtils.d(TAG, String.format("应用包 %s单次耗电%.1f mAh累计耗电%.1f mAh",
LogUtils.v(TAG, String.format("【calculateSingleConsumptionAndAccumulate】应用包%s单次耗电%.1f mAh累计耗电%.1f mAh",
packageName, singleConsumption, newTotalConsumption));
}
// 3. 按累计耗电排序(从高到低)
// 3. 按累计耗电降序排序
Collections.sort(dataList, new Comparator<AppBatteryModel>() {
@Override
public int compare(AppBatteryModel m1, AppBatteryModel m2) {
@@ -260,71 +393,43 @@ public class BatteryReportActivity extends Activity {
}
});
// 4. 重新应用过滤并刷新列表
filterAppsByPackageAndName(etSearch.getText().toString());
// 4. 重新过滤并刷新列表
filterAppsByPackageAndName(etSearch.getText().toString().trim());
LogUtils.d(TAG, "【calculateSingleConsumptionAndAccumulate】单次耗电计算与累加完成列表已刷新");
}
/**
* 双维度过滤(逻辑不变
* 双维度过滤(包名+应用名
* @param keyword 搜索关键词
*/
private void filterAppsByPackageAndName(String keyword) {
filteredList.clear();
if (keyword == null || keyword.isEmpty()) {
filteredList.addAll(dataList);
LogUtils.d(TAG, "【filterAppsByPackageAndName】搜索关键词为空显示全部应用数量" + filteredList.size());
} else {
String lowerKeyword = keyword.toLowerCase();
for (AppBatteryModel model : dataList) {
String packageName = model.getPackageName();
String packageNameLower = packageName.toLowerCase();
String appName = packageToAppNameCache.get(packageName);
String appNameLower = appName.toLowerCase();
boolean isMatched = packageNameLower.contains(lowerKeyword)
|| appNameLower.contains(lowerKeyword);
boolean isMatched = packageNameLower.contains(lowerKeyword) || appNameLower.contains(lowerKeyword);
if (isMatched) {
filteredList.add(model);
}
}
LogUtils.d(TAG, "【filterAppsByPackageAndName】搜索关键词" + keyword + ",匹配应用数量:" + filteredList.size());
}
adapter.notifyDataSetChanged();
}
// ======================== 工具方法 =========================
/**
* 获取应用运行时长逻辑不变返回24小时运行时长
*/
private Map<String, Long> getAppRunTime() {
Map<String, Long> runTimeMap = new HashMap<String, Long>();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
try {
android.app.usage.UsageStatsManager manager =
(android.app.usage.UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
long endTime = System.currentTimeMillis();
long startTime = endTime - 24 * 3600 * 1000; // 近24小时
List<android.app.usage.UsageStats> statsList = manager.queryUsageStats(
android.app.usage.UsageStatsManager.INTERVAL_DAILY, startTime, endTime);
for (android.app.usage.UsageStats stats : statsList) {
long runTimeMs = stats.getTotalTimeInForeground();
String packageName = stats.getPackageName();
LogUtils.d(TAG, "包名" + packageName + "24小时运行时长" + formatRunTime(runTimeMs));
runTimeMap.put(packageName, runTimeMs);
if (packageName.equals("aidepro.top")) {
LogUtils.d(TAG, String.format("runTimeMap.put(packageName, runTimeMs) 特殊查询 %s 查询有结果。", packageName));
}
}
} catch (Exception e) {
LogUtils.e(TAG, "获取应用运行时长失败:" + e.getMessage());
}
}
LogUtils.d(TAG, String.format("应用运行时长列表数量%d。", runTimeMap.size()));
return runTimeMap;
}
/**
* 格式化运行时长(逻辑不变)
* 格式化运行时长
* @param runTimeMs 运行时长ms
* @return 格式化后的运行时长字符串
*/
private String formatRunTime(long runTimeMs) {
if (runTimeMs <= 0) {
@@ -344,66 +449,46 @@ public class BatteryReportActivity extends Activity {
}
}
// ======================== 内部类:数据模型 =========================
/**
* 权限检查(逻辑不变)
*/
private boolean hasUsageStatsPermission(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return false;
}
android.app.usage.UsageStatsManager manager =
(android.app.usage.UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
if (manager == null) {
return false;
}
long endTime = System.currentTimeMillis();
long startTime = endTime - 1000 * 60;
List<android.app.usage.UsageStats> statsList = manager.queryUsageStats(
android.app.usage.UsageStatsManager.INTERVAL_DAILY, startTime, endTime);
return statsList != null && !statsList.isEmpty();
}
/**
* 【核心修改】数据模型:明确字段含义
* - consumption单次耗电两次电池广播间的消耗float类型便于计算
* - totalConsumption累计耗电24小时初始化值+后续单次累加,显示用)
* 应用电池数据模型
* - consumption单次耗电两次电池广播间的消耗
* - totalConsumption累计耗电24小时初始化值+后续单次累加)
* - runTime运行时长ms
*/
public static class AppBatteryModel {
private String packageName; // 应用包名(核心标识)
private float consumption; // 单次耗电mAhfloat类型
private float totalConsumption;// 累计耗电mAh,显示+排序用
private float consumption; // 单次耗电mAh
private float totalConsumption;// 累计耗电mAh
private long runTime; // 运行时长ms
// Java7 显式构造初始化单次耗电、累计耗电为0
// Java7 显式构造
public AppBatteryModel(String packageName, float consumption, float totalConsumption, long runTime) {
this.packageName = packageName;
this.consumption = consumption; // 单次耗电初始为0
this.totalConsumption = totalConsumption; // 累计耗电初始为0后续初始化时赋值
this.consumption = consumption;
this.totalConsumption = totalConsumption;
this.runTime = runTime;
}
// Getter/Setter:覆盖所有字段,确保数据操作正常
// Getter/Setter
public String getPackageName() {
return packageName;
}
public float getConsumption() {
return consumption; // 获取单次耗电
return consumption;
}
public void setConsumption(float consumption) {
this.consumption = consumption; // 设置单次耗电
this.consumption = consumption;
}
public float getTotalConsumption() {
return totalConsumption; // 获取累计耗电(显示用)
return totalConsumption;
}
public void setTotalConsumption(float totalConsumption) {
this.totalConsumption = totalConsumption; // 设置累计耗电(初始化/累加用)
this.totalConsumption = totalConsumption;
}
public long getRunTime() {
@@ -415,8 +500,9 @@ public class BatteryReportActivity extends Activity {
}
}
// ======================== 内部类RecyclerView适配器 =========================
/**
* RecyclerView 适配器仅显示累计耗电totalConsumption逻辑适配模型修改
* 电池报告列表适配器,显示应用名称、累计耗电、运行时长
*/
public static class BatteryReportAdapter extends RecyclerView.Adapter<BatteryReportAdapter.ViewHolder> {
private Context mContext;
@@ -424,29 +510,30 @@ public class BatteryReportActivity extends Activity {
private PackageManager mPm;
private Map<String, String> mPackageToNameCache;
// Java7 显式构造:接收名称缓存,确保显示时高效获取应用名
public BatteryReportAdapter(Context context, List<AppBatteryModel> dataList,
// Java7 显式构造
public BatteryReportAdapter(Context context, List<AppBatteryModel> dataList,
PackageManager pm, Map<String, String> packageToNameCache) {
this.mContext = context;
this.mDataList = dataList;
this.mPm = pm;
this.mPackageToNameCache = packageToNameCache;
LogUtils.d(TAG, "【BatteryReportAdapter】适配器构造完成数据量" + dataList.size());
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// 加载系统列表项布局text1显示应用名text2显示累计耗电+时长)
View itemView = LayoutInflater.from(mContext)
.inflate(android.R.layout.simple_list_item_2, parent, false);
.inflate(android.R.layout.simple_list_item_2, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// Java7 显式非空判断:避免空指针异常
// Java7 显式非空判断
if (mDataList == null || mDataList.isEmpty() || position >= mDataList.size()) {
holder.tvAppName.setText("未知应用");
holder.tvConsumption.setText("累计耗电0.0 mAh | 运行时长0秒");
LogUtils.w(TAG, "【onBindViewHolder】数据异常位置" + position);
return;
}
@@ -454,11 +541,11 @@ public class BatteryReportActivity extends Activity {
String packageName = model.getPackageName();
String appName = "";
// 优先从缓存获取应用名减少PackageManager调用提升性能
// 优先从缓存获取应用名
if (mPackageToNameCache != null && mPackageToNameCache.containsKey(packageName)) {
appName = mPackageToNameCache.get(packageName);
} else {
// 缓存无数据时兜底查询,并同步更新缓存
// 缓存无数据时兜底查询
try {
ApplicationInfo appInfo = mPm.getApplicationInfo(packageName, 0);
appName = mPm.getApplicationLabel(appInfo).toString();
@@ -466,45 +553,40 @@ public class BatteryReportActivity extends Activity {
mPackageToNameCache.put(packageName, appName);
}
} catch (PackageManager.NameNotFoundException e) {
appName = packageName; // 包名不存在时用包名兜底
LogUtils.e("Adapter", "包名" + packageName + "对应的应用未找到:" + e.getMessage());
appName = packageName;
LogUtils.e("BatteryReportAdapter", "【onBindViewHolder】包名" + packageName + "对应的应用未找到:" + e.getMessage());
} catch (Exception e) {
appName = packageName; // 其他异常时用包名兜底
LogUtils.e("Adapter", "查询应用名称失败(包名:" + packageName + "" + e.getMessage());
appName = packageName;
LogUtils.e("BatteryReportAdapter", "【onBindViewHolder】查询应用名称失败(包名:" + packageName + "" + e.getMessage());
}
}
// 显示逻辑:仅展示累计耗电totalConsumption隐藏单次耗电
// 显示逻辑:应用名称 + 累计耗电 + 运行时长
holder.tvAppName.setText(appName);
// 格式化运行时长 + 累计耗电保留1位小数提升可读性
String runTimeStr = ((BatteryReportActivity) mContext).formatRunTime(model.getRunTime());
String totalConsumptionText = String.format("累计耗电:%.1f mAh | 运行时长:%s",
model.getTotalConsumption(), runTimeStr);
holder.tvConsumption.setText(totalConsumptionText);
// 显示优化:文字颜色区分(避免所有应用均标蓝,仅示例可按需修改)
// 显示优化
holder.tvAppName.setTextColor(mContext.getResources().getColor(android.R.color.black));
holder.tvConsumption.setTextColor(mContext.getResources().getColor(android.R.color.darker_gray));
// 调整文字大小:适配手机屏幕,提升可读性
holder.tvAppName.setTextSize(16);
holder.tvConsumption.setTextSize(14);
}
// 获取列表长度Java7 三元运算符判断空值,避免空指针
@Override
public int getItemCount() {
return mDataList == null ? 0 : mDataList.size();
}
/**
* ViewHolder绑定系统布局控件,与显示逻辑对应
* ViewHolder绑定系统布局控件
*/
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView tvAppName; // 显示应用名称
TextView tvConsumption; // 显示累计耗电 + 运行时长
TextView tvAppName; // 应用名称
TextView tvConsumption; // 累计耗电 + 运行时长
// Java7 显式构造绑定控件ID系统布局固定IDtext1、text2
public ViewHolder(View itemView) {
super(itemView);
tvAppName = (TextView) itemView.findViewById(android.R.id.text1);

View File

@@ -6,93 +6,161 @@ import android.os.Bundle;
import android.view.View;
import android.widget.Switch;
import android.widget.TextView;
import androidx.appcompat.widget.Toolbar;
import cc.winboll.studio.libaes.interfaces.IWinBoLLActivity;
import cc.winboll.studio.libaes.views.AOHPCTCSeekBar;
import cc.winboll.studio.libaes.views.AToolbar;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.ToastUtils;
import cc.winboll.studio.powerbell.App;
import cc.winboll.studio.powerbell.R;
import cc.winboll.studio.powerbell.beans.BatteryInfoBean;
import cc.winboll.studio.powerbell.models.BatteryInfoBean;
import cc.winboll.studio.powerbell.receivers.ControlCenterServiceReceiver;
import cc.winboll.studio.powerbell.utils.AppCacheUtils;
import cc.winboll.studio.powerbell.utils.StringUtils;
import java.util.ArrayList;
public class ClearRecordActivity extends Activity {
/**
* 电池记录清理页面,支持滑动清理记录、切换记录显示格式
* 适配 API30基于 Java7 开发
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
*/
public class ClearRecordActivity extends WinBoLLActivity implements IWinBoLLActivity {
// ======================== 静态常量(按功能分类) =========================
public static final String TAG = "ClearRecordActivity";
private static final String TOAST_MSG_CLEAR_SUCCESS = "The APP battery record is cleaned.";
AToolbar mAToolbar;
TextView mtvRecordText;
App mApplication;
boolean mIsShowRecordWithEnter = false;
// ======================== 成员变量(按依赖优先级+功能分类) =========================
// UI组件
private Toolbar mToolbar;
private TextView mtvRecordText;
private TextView tvAOHPCTCSeekBarMSG;
private AOHPCTCSeekBar aOHPCTCSeekBar;
// 应用与配置
private App mApplication;
private boolean mIsShowRecordWithEnter = false; // 记录是否带换行显示
// ======================== 接口实现方法 =========================
@Override
public Activity getActivity() {
return this;
}
@Override
public String getTag() {
return TAG;
}
// ======================== 生命周期方法(按执行顺序排列) =========================
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clearrecord);
LogUtils.d(TAG, "【onCreate】ClearRecordActivity 初始化开始");
// 初始化应用实例
mApplication = (App) getApplication();
LogUtils.d(TAG, "【onCreate】应用实例初始化完成");
// 初始化工具栏
mAToolbar = (AToolbar) findViewById(R.id.toolbar);
setActionBar(mAToolbar);
//mAToolbar.setTitle(getTitle() + " - " + getString(R.string.subtitle_activity_clearrecord));
mAToolbar.setSubtitle(R.string.subtitle_activity_clearrecord);
//mAToolbar.setTitleTextAppearance(this, R.style.Toolbar_TitleText);
//mAToolbar.setSubtitleTextAppearance(this, R.style.Toolbar_SubTitleText);
//mAToolbar.setBackgroundColor(getColor(R.color.colorPrimary));
setActionBar(mAToolbar);
getActionBar().setDisplayHomeAsUpEnabled(true);
mAToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
// 初始化核心逻辑
initView();
initSeekBar();
initRecordText();
// 设置滑动清理控件
//
// 初始化发送拉动控件
final AOHPCTCSeekBar aOHPCTCSeekBar = findViewById(R.id.activityclearrecordAOHPCTCSeekBar1);
LogUtils.d(TAG, "【onCreate】ClearRecordActivity 初始化完成");
}
// ======================== UI初始化方法 =========================
/**
* 初始化Toolbar与显示文本组件
*/
private void initView() {
// 初始化Toolbar
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mToolbar.setSubtitle(getTag());
mToolbar.setTitleTextAppearance(this, R.style.Toolbar_TitleText);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "【导航栏】点击返回按钮,关闭当前页面");
finish();
}
});
// 初始化显示文本组件
tvAOHPCTCSeekBarMSG = (TextView) findViewById(R.id.activityclearrecordTextView1);
mtvRecordText = (TextView) findViewById(R.id.activityclearrecordTextView2);
tvAOHPCTCSeekBarMSG.setText(R.string.msg_AOHPCTCSeekBar_ClearRecord);
LogUtils.d(TAG, "【initView】UI组件初始化完成");
}
/**
* 初始化滑动清理控件,设置回调监听
*/
private void initSeekBar() {
aOHPCTCSeekBar = (AOHPCTCSeekBar) findViewById(R.id.activityclearrecordAOHPCTCSeekBar1);
aOHPCTCSeekBar.setThumb(getDrawable(R.drawable.cursor_pointer));
aOHPCTCSeekBar.setThumbOffset(0);
aOHPCTCSeekBar.setOnOHPCListener(
new AOHPCTCSeekBar.OnOHPCListener(){
aOHPCTCSeekBar.setOnOHPCListener(new AOHPCTCSeekBar.OnOHPCListener() {
@Override
public void onOHPCommit() {
LogUtils.d(TAG, "【onOHPCommit】滑动清理触发开始执行记录清理逻辑");
// 清理电池历史记录
mApplication.clearBatteryHistory();
// 发送广播更新前台通知
sendBroadcast(new Intent(ControlCenterServiceReceiver.ACTION_UPDATE_FOREGROUND_NOTIFICATION));
// 刷新记录显示
initRecordText();
// 提示清理成功
ToastUtils.show(TOAST_MSG_CLEAR_SUCCESS);
LogUtils.d(TAG, "【onOHPCommit】电池记录清理完成已发送前台通知更新广播");
}
});
@Override
public void onOHPCommit() {
mApplication.clearBatteryHistory();
sendBroadcast(new Intent(ControlCenterServiceReceiver.ACTION_UPDATE_SERVICENOTIFICATION));
initRecordText();
String szMSG = "The APP battery record is cleaned.";
LogUtils.d(TAG, szMSG);
ToastUtils.show(szMSG);
}
});
// 初始化提示框
TextView tvAOHPCTCSeekBarMSG = findViewById(R.id.activityclearrecordTextView1);
tvAOHPCTCSeekBarMSG.setText(R.string.msg_AOHPCTCSeekBar_ClearRecord);
mtvRecordText = findViewById(R.id.activityclearrecordTextView2);
initRecordText();
LogUtils.d(TAG, "【initSeekBar】滑动清理控件初始化完成回调监听已绑定");
}
// ======================== 业务逻辑方法 =========================
/**
* 初始化记录显示文本,根据配置切换带换行/不带换行格式
*/
void initRecordText() {
ArrayList<BatteryInfoBean> listBatteryInfo = AppCacheUtils.getInstance(this).getArrayListBatteryInfo();
if (mIsShowRecordWithEnter) {
String szRecordText = StringUtils.formatPCMListStringWithEnter(listBatteryInfo);
mtvRecordText.setText(szRecordText);
} else {
String szRecordText = StringUtils.formatPCMListString(listBatteryInfo);
mtvRecordText.setText(szRecordText);
}
String szRecordText;
// 判空处理:避免空列表导致异常
if (listBatteryInfo == null || listBatteryInfo.isEmpty()) {
szRecordText = getString(R.string.msg_no_battery_record);
LogUtils.d(TAG, "【initRecordText】无电池记录数据显示空记录提示文本");
} else {
// 根据配置切换显示格式
if (mIsShowRecordWithEnter) {
szRecordText = StringUtils.formatPCMListStringWithEnter(listBatteryInfo);
LogUtils.d(TAG, String.format("【initRecordText】使用带换行格式显示记录记录数量%d", listBatteryInfo.size()));
} else {
szRecordText = StringUtils.formatPCMListString(listBatteryInfo);
LogUtils.d(TAG, String.format("【initRecordText】使用无换行格式显示记录记录数量%d", listBatteryInfo.size()));
}
}
mtvRecordText.setText(szRecordText);
LogUtils.d(TAG, "【initRecordText】记录显示文本刷新完成");
}
public void onShowRecordWithEnter(View view) {
Switch swShowRecordWithEnter = (Switch)view;
mIsShowRecordWithEnter = swShowRecordWithEnter.isChecked();
initRecordText();
}
// ======================== 事件回调方法 =========================
/**
* 切换记录显示格式(带换行/不带换行)
* @param view 触发事件的Switch控件
*/
public void onShowRecordWithEnter(View view) {
Switch swShowRecordWithEnter = (Switch) view;
mIsShowRecordWithEnter = swShowRecordWithEnter.isChecked();
LogUtils.d(TAG, String.format("【onShowRecordWithEnter】记录显示格式切换带换行显示%b", mIsShowRecordWithEnter));
// 刷新记录显示
initRecordText();
}
}

View File

@@ -1,9 +1,5 @@
package cc.winboll.studio.powerbell.activities;
/**
* @Author ZhanGSKen<zhangsken@qq.com>
* @Date 2025/06/22 14:15
*/
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
@@ -20,162 +16,200 @@ import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;
import cc.winboll.studio.libaes.interfaces.IWinBoLLActivity;
import cc.winboll.studio.libaes.views.AToolbar;
import cc.winboll.studio.libappbase.GlobalApplication;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.powerbell.R;
import cc.winboll.studio.powerbell.activities.BackgroundPictureActivity;
import cc.winboll.studio.powerbell.activities.PixelPickerActivity;
import cc.winboll.studio.powerbell.beans.BackgroundPictureBean;
import cc.winboll.studio.powerbell.utils.BackgroundPictureUtils;
import cc.winboll.studio.powerbell.models.BackgroundBean;
import cc.winboll.studio.powerbell.utils.BackgroundSourceUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
/**
* 像素拾取页面,支持加载图片并拾取指定位置像素颜色,同步至背景配置
* 适配 API30基于 Java7 开发
* @Author ZhanGSKen<zhangsken@qq.com>
* @Date 2025/06/22 14:15
*/
public class PixelPickerActivity extends WinBoLLActivity implements IWinBoLLActivity {
// ======================== 静态常量 =========================
public static final String TAG = "PixelPickerActivity";
public static final String EXTRA_IMAGE_PATH = "imagePath"; // 图片路径传递键
// 提示文本常量
private static final String MSG_IMAGE_LOADED = "图片已加载,点击获取像素值";
private static final String MSG_NO_IMAGE_PATH = "未找到图片路径";
private static final String MSG_IMAGE_LOAD_FAILED = "图片加载失败";
private static final String MSG_FILE_NOT_EXIST = "图片文件不存在";
private static final String MSG_FILE_NOT_FOUND = "图片文件未找到";
private static final String MSG_PIXEL_OUT_OF_RANGE = "像素坐标超出范围";
private static final String MSG_TOUCH_OUT_OF_IMAGE = "点击位置超出图片显示范围";
private static final String MSG_PIXEL_CALC_FAILED = "计算像素位置失败";
private static final String MSG_PIXEL_RECORDED = "已记录像素值";
public static final String TAG = "PixelPickerActivity";
// ======================== 成员变量 =========================
// UI组件
private Toolbar mToolbar;
private ImageView imageView;
private TextView infoText;
private ViewGroup imageContainer;
private RelativeLayout mainLayout;
// 图片与像素数据
private Bitmap originalBitmap; // 原始图片Bitmap用于像素拾取
@Override
public Activity getActivity() {
return this;
}
// ======================== 接口实现方法 =========================
@Override
public Activity getActivity() {
return this;
}
@Override
public String getTag() {
return TAG;
}
private AToolbar mAToolbar;
private ImageView imageView;
private Bitmap originalBitmap;
private TextView infoText;
private ViewGroup imageContainer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pixelpicker);
@Override
public String getTag() {
return TAG;
}
// ======================== 生命周期方法 =========================
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pixelpicker);
LogUtils.d(TAG, "【onCreate】PixelPickerActivity 初始化开始");
// 初始化UI组件
initView();
// 初始化工具栏
mAToolbar = (AToolbar) findViewById(R.id.toolbar);
setActionBar(mAToolbar);
mAToolbar.setSubtitle(R.string.subtitle_activity_pixelpicker);
getActionBar().setDisplayHomeAsUpEnabled(true);
mAToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
initToolbar();
// 加载传递的图片
loadImageFromIntent();
// 绑定图片触摸事件
bindImageTouchListener();
imageView = findViewById(R.id.imageView);
infoText = findViewById(R.id.infoText);
imageContainer = findViewById(R.id.imageContainer);
LogUtils.d(TAG, "【onCreate】PixelPickerActivity 初始化完成");
}
// 从Intent获取图片路径并加载
String imagePath = getIntent().getStringExtra("imagePath");
if (imagePath != null) {
loadImage(imagePath);
} else {
infoText.setText("未找到图片路径");
}
@Override
protected void onResume() {
super.onResume();
LogUtils.d(TAG, "【onResume】PixelPickerActivity 恢复显示");
// 同步背景颜色
setBackgroundColor();
}
// 设置图片点击事件
imageContainer.setOnTouchListener(new View.OnTouchListener() {
@Override
protected void onDestroy() {
super.onDestroy();
// 回收Bitmap资源避免内存泄漏
if (originalBitmap != null && !originalBitmap.isRecycled()) {
originalBitmap.recycle();
originalBitmap = null;
LogUtils.d(TAG, "【onDestroy】原始图片Bitmap资源已回收");
}
LogUtils.d(TAG, "【onDestroy】PixelPickerActivity 销毁完成");
}
// ======================== UI初始化方法 =========================
/**
* 初始化所有UI组件
*/
private void initView() {
imageView = findViewById(R.id.imageView);
infoText = findViewById(R.id.infoText);
imageContainer = findViewById(R.id.imageContainer);
mainLayout = findViewById(R.id.activitypixelpickerRelativeLayout1);
LogUtils.d(TAG, "【initView】UI组件初始化完成");
}
/**
* 初始化工具栏,设置导航与标题
*/
private void initToolbar() {
LogUtils.d(TAG, "initToolbar() 开始初始化");
mToolbar = findViewById(R.id.toolbar);
if (mToolbar == null) {
LogUtils.e(TAG, "initToolbar() | Toolbar未找到");
return;
}
setSupportActionBar(mToolbar);
mToolbar.setSubtitle(getTag());
mToolbar.setTitleTextAppearance(this, R.style.Toolbar_TitleText);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN && originalBitmap != null) {
// 计算点击位置在图片上的实际坐标
float touchX = event.getX();
float touchY = event.getY();
int pixelX = -1, pixelY = -1;
try {
// 获取图片在容器中的实际位置和尺寸
int[] imageLocation = new int[2];
imageView.getLocationInWindow(imageLocation);
int imageWidth = imageView.getWidth();
int imageHeight = imageView.getHeight();
// 计算缩放比例
float scaleX = (float) originalBitmap.getWidth() / imageWidth;
float scaleY = (float) originalBitmap.getHeight() / imageHeight;
// 调整触摸坐标到图片坐标系
float adjustedX = touchX - imageLocation[0];
float adjustedY = touchY - imageLocation[1];
// 检查是否在图片范围内
if (adjustedX >= 0 && adjustedX <= imageWidth && adjustedY >= 0 && adjustedY <= imageHeight) {
// 计算实际像素坐标
pixelX = (int) (adjustedX * scaleX);
pixelY = (int) (adjustedY * scaleY);
// 再次检查像素坐标是否在有效范围内
if (pixelX >= 0 && pixelX < originalBitmap.getWidth() &&
pixelY >= 0 && pixelY < originalBitmap.getHeight()) {
int pixelColor = originalBitmap.getPixel(pixelX, pixelY);
showPixelDialog(pixelColor, pixelX, pixelY);
} else {
Toast.makeText(PixelPickerActivity.this, "像素坐标超出范围", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(PixelPickerActivity.this, "点击位置超出图片显示范围", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(PixelPickerActivity.this, "计算像素位置失败", Toast.LENGTH_SHORT).show();
}
}
return true;
public void onClick(View v) {
LogUtils.d(TAG, "导航栏 点击返回按钮");
finish();
}
});
}
LogUtils.d(TAG, "initToolbar() 配置完成");
}
/**
* 加载图片
*/
private void loadImage(String imagePath) {
try {
File file = new File(imagePath);
if (file.exists()) {
// 解码图片
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1; // 加载原图
originalBitmap = BitmapFactory.decodeStream(new FileInputStream(file), null, options);
// ======================== 业务逻辑方法 =========================
/**
* 从Intent中获取图片路径并加载图片
*/
private void loadImageFromIntent() {
String imagePath = getIntent().getStringExtra(EXTRA_IMAGE_PATH);
LogUtils.d(TAG, "【loadImageFromIntent】获取到图片路径" + imagePath);
if (originalBitmap != null) {
imageView.setImageBitmap(originalBitmap);
infoText.setText("图片已加载,点击获取像素值");
} else {
infoText.setText("图片加载失败");
}
} else {
infoText.setText("图片文件不存在");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
infoText.setText("图片文件未找到");
}
}
if (imagePath != null) {
loadImage(imagePath);
} else {
infoText.setText(MSG_NO_IMAGE_PATH);
LogUtils.w(TAG, "【loadImageFromIntent】未获取到图片路径");
}
}
/**
* 显示像素对话框
*/
private void showPixelDialog(final int pixelColor, int x, int y) {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_pixel);
dialog.setCancelable(true);
/**
* 加载指定路径的图片
* @param imagePath 图片文件路径
*/
private void loadImage(String imagePath) {
try {
File file = new File(imagePath);
if (file.exists()) {
// 解码图片(加载原图)
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
originalBitmap = BitmapFactory.decodeStream(new FileInputStream(file), null, options);
// 设置像素颜色视图背景
TextView colorView = dialog.findViewById(R.id.pixelColorView);
colorView.setBackgroundColor(pixelColor);
if (originalBitmap != null) {
imageView.setImageBitmap(originalBitmap);
infoText.setText(MSG_IMAGE_LOADED);
LogUtils.d(TAG, "【loadImage】图片加载成功尺寸" + originalBitmap.getWidth() + "x" + originalBitmap.getHeight());
} else {
infoText.setText(MSG_IMAGE_LOAD_FAILED);
LogUtils.e(TAG, "【loadImage】图片解码失败");
}
} else {
infoText.setText(MSG_FILE_NOT_EXIST);
LogUtils.w(TAG, "【loadImage】图片文件不存在" + imagePath);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
infoText.setText(MSG_FILE_NOT_FOUND);
LogUtils.e(TAG, "【loadImage】图片文件未找到" + e.getMessage());
}
}
// 显示颜色信息
TextView infoText = dialog.findViewById(R.id.colorInfoText);
String colorInfo = String.format(
/**
* 显示像素颜色信息对话框
* @param pixelColor 拾取的像素颜色ARGB
* @param x 像素X坐标
* @param y 像素Y坐标
*/
private void showPixelDialog(final int pixelColor, int x, int y) {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_pixel);
dialog.setCancelable(true);
// 设置颜色预览与信息展示
TextView colorView = dialog.findViewById(R.id.pixelColorView);
TextView infoTextView = dialog.findViewById(R.id.colorInfoText);
colorView.setBackgroundColor(pixelColor);
String colorInfo = String.format(
"RGB: (%d, %d, %d)\n" +
"ARGB: #%08X\n" +
"实际像素位置: (%d, %d)",
@@ -184,75 +218,129 @@ public class PixelPickerActivity extends WinBoLLActivity implements IWinBoLLActi
Color.blue(pixelColor),
pixelColor,
x, y);
infoText.setText(colorInfo);
infoTextView.setText(colorInfo);
LogUtils.d(TAG, "【showPixelDialog】显示像素信息" + colorInfo);
// 设置确定按钮点击事件
Button confirmButton = dialog.findViewById(R.id.confirmButton);
confirmButton.setOnClickListener(new View.OnClickListener() {
// 确定按钮点击事件
Button confirmButton = dialog.findViewById(R.id.confirmButton);
confirmButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
// 可以在这里添加确定后的回调逻辑
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(PixelPickerActivity.this);
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
bean.setPixelColor(pixelColor);
utils.saveData();
Toast.makeText(PixelPickerActivity.this, "已记录像素值", Toast.LENGTH_SHORT).show();
// 保存像素颜色到背景配置
savePixelColor(pixelColor);
Toast.makeText(PixelPickerActivity.this, MSG_PIXEL_RECORDED, Toast.LENGTH_SHORT).show();
// 同步背景颜色
setBackgroundColor();
}
});
dialog.show();
}
dialog.show();
LogUtils.d(TAG, "【showPixelDialog】像素对话框已显示");
}
@Override
protected void onDestroy() {
super.onDestroy();
// 回收Bitmap资源
if (originalBitmap != null && !originalBitmap.isRecycled()) {
originalBitmap.recycle();
originalBitmap = null;
}
}
/**
* 保存拾取的像素颜色到背景配置
* @param pixelColor 拾取的像素颜色ARGB
*/
private void savePixelColor(int pixelColor) {
BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(this);
BackgroundBean bean = utils.getPreviewBackgroundBean();
bean.setPixelColor(pixelColor);
utils.saveSettings();
LogUtils.d(TAG, "【savePixelColor】像素颜色已保存#" + Integer.toHexString(pixelColor));
}
/**
* 同步背景颜色为拾取的像素颜色
*/
void setBackgroundColor() {
BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(this);
BackgroundBean bean = utils.getPreviewBackgroundBean();
int pixelColor = bean.getPixelColor();
mainLayout.setBackgroundColor(pixelColor);
LogUtils.d(TAG, "【setBackgroundColor】背景颜色已同步#" + Integer.toHexString(pixelColor));
}
void setBackgroundColor() {
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(PixelPickerActivity.this);
BackgroundPictureBean bean = utils.getBackgroundPictureBean();
int nPixelColor = bean.getPixelColor();
RelativeLayout mainLayout = findViewById(R.id.activitypixelpickerRelativeLayout1);
mainLayout.setBackgroundColor(nPixelColor);
}
// ======================== 事件回调方法 =========================
/**
* 绑定图片容器的触摸事件,处理像素拾取逻辑
*/
private void bindImageTouchListener() {
imageContainer.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN && originalBitmap != null) {
float touchX = event.getX();
float touchY = event.getY();
LogUtils.v(TAG, "【onTouch】触摸坐标(" + touchX + ", " + touchY + ")");
@Override
protected void onResume() {
super.onResume();
setBackgroundColor();
}
try {
// 获取图片在窗口中的位置与尺寸
int[] imageLocation = new int[2];
imageView.getLocationInWindow(imageLocation);
int imageWidth = imageView.getWidth();
int imageHeight = imageView.getHeight();
LogUtils.v(TAG, "【onTouch】图片显示尺寸" + imageWidth + "x" + imageHeight + ",位置:(" + imageLocation[0] + ", " + imageLocation[1] + ")");
// 计算缩放比例
float scaleX = (float) originalBitmap.getWidth() / imageWidth;
float scaleY = (float) originalBitmap.getHeight() / imageHeight;
LogUtils.v(TAG, "【onTouch】图片缩放比例X=" + scaleX + "Y=" + scaleY);
// 调整触摸坐标到图片显示区域坐标系
float adjustedX = touchX - imageLocation[0];
float adjustedY = touchY - imageLocation[1];
LogUtils.v(TAG, "【onTouch】调整后触摸坐标(" + adjustedX + ", " + adjustedY + ")");
// 检查是否在图片显示范围内
if (adjustedX >= 0 && adjustedX <= imageWidth && adjustedY >= 0 && adjustedY <= imageHeight) {
// 计算原始图片的像素坐标
int pixelX = (int) (adjustedX * scaleX);
int pixelY = (int) (adjustedY * scaleY);
LogUtils.v(TAG, "【onTouch】计算后像素坐标(" + pixelX + ", " + pixelY + ")");
// 检查像素坐标是否在原始图片范围内
if (pixelX >= 0 && pixelX < originalBitmap.getWidth() && pixelY >= 0 && pixelY < originalBitmap.getHeight()) {
int pixelColor = originalBitmap.getPixel(pixelX, pixelY);
showPixelDialog(pixelColor, pixelX, pixelY);
} else {
Toast.makeText(PixelPickerActivity.this, MSG_PIXEL_OUT_OF_RANGE, Toast.LENGTH_SHORT).show();
LogUtils.w(TAG, "【onTouch】像素坐标超出原始图片范围");
}
} else {
Toast.makeText(PixelPickerActivity.this, MSG_TOUCH_OUT_OF_IMAGE, Toast.LENGTH_SHORT).show();
LogUtils.w(TAG, "【onTouch】触摸位置超出图片显示范围");
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(PixelPickerActivity.this, MSG_PIXEL_CALC_FAILED, Toast.LENGTH_SHORT).show();
LogUtils.e(TAG, "【onTouch】计算像素位置失败" + e.getMessage());
}
}
return true;
}
});
LogUtils.d(TAG, "【bindImageTouchListener】图片触摸事件已绑定");
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Intent intent = new Intent();
intent.setClass(this, BackgroundPictureActivity.class);
LogUtils.d(TAG, "【onOptionsItemSelected】点击返回菜单");
Intent intent = new Intent(this, BackgroundSettingsActivity.class);
startActivity(intent);
//GlobalApplication.getWinBoLLActivityManager().startWinBoLLActivity(getApplicationContext(), );
return true;
}
// 在switch语句中处理每个ID并在处理完后返回true未处理的情况返回false。
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent();
intent.setClass(this, BackgroundPictureActivity.class);
startActivity(intent);
//GlobalApplication.getWinBoLLActivityManager().startWinBoLLActivity(getApplicationContext(), BackgroundPictureActivity.class);
}
@Override
public void onBackPressed() {
super.onBackPressed();
setResult(RESULT_OK);
finish();
LogUtils.d(TAG, "【onBackPressed】返回键触发页面关闭");
}
}

View File

@@ -0,0 +1,184 @@
package cc.winboll.studio.powerbell.activities;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;
import cc.winboll.studio.libaes.interfaces.IWinBoLLActivity;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.ToastUtils;
import cc.winboll.studio.powerbell.R;
import cc.winboll.studio.powerbell.models.ThoughtfulServiceBean;
import java.lang.reflect.Field;
/**
* 应用设置窗口,提供应用配置项的统一入口
* 适配 API30基于 Java7 开发
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2025/11/27 14:26
* @Describe 应用设置窗口
*/
public class SettingsActivity extends WinBoLLActivity implements IWinBoLLActivity {
// ======================== 静态常量 =========================
public static final String TAG = "SettingsActivity";
// 权限请求常量(为后续读取媒体图片权限预留)
private static final int REQUEST_READ_MEDIA_IMAGES = 1001;
// ======================== 成员变量 =========================
private Toolbar mToolbar; // 顶部工具栏
// ======================== 接口实现方法 =========================
@Override
public Activity getActivity() {
return this;
}
@Override
public String getTag() {
return TAG;
}
// ======================== 生命周期方法 =========================
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
LogUtils.d(TAG, "【onCreate】SettingsActivity 初始化开始");
// 初始化工具栏
initToolbar();
ThoughtfulServiceBean thoughtfulServiceBean = ThoughtfulServiceBean.loadBean(this, ThoughtfulServiceBean.class);
if (thoughtfulServiceBean == null) {
thoughtfulServiceBean = new ThoughtfulServiceBean();
}
((CheckBox)findViewById(R.id.activitysettingsCheckBox1)).setChecked(thoughtfulServiceBean.isEnableUsePowerTts());
((CheckBox)findViewById(R.id.activitysettingsCheckBox2)).setChecked(thoughtfulServiceBean.isEnableChargeTts());
LogUtils.d(TAG, "【onCreate】SettingsActivity 初始化完成");
}
// ======================== UI初始化方法 =========================
/**
* 初始化顶部工具栏,设置导航返回与样式
*/
private void initToolbar() {
mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
// 设置工具栏副标题与标题样式
mToolbar.setSubtitle(getTag());
mToolbar.setTitleTextAppearance(this, R.style.Toolbar_TitleText);
// 显示返回按钮
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// 绑定导航点击事件
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "【导航栏】点击返回");
finish();
}
});
LogUtils.d(TAG, "【initToolbar】工具栏初始化完成");
}
public void onCheckTTSDrawOverlaysPermission(View view) {
canDrawOverlays();
}
public void onEnableChargeTts(View view) {
ThoughtfulServiceBean thoughtfulServiceBean = ThoughtfulServiceBean.loadBean(this, ThoughtfulServiceBean.class);
if (thoughtfulServiceBean == null) {
thoughtfulServiceBean = new ThoughtfulServiceBean();
}
thoughtfulServiceBean.setIsEnableChargeTts(((CheckBox)view).isChecked());
ThoughtfulServiceBean.saveBean(this, thoughtfulServiceBean);
}
public void onEnableUsePowerTts(View view) {
ThoughtfulServiceBean thoughtfulServiceBean = ThoughtfulServiceBean.loadBean(this, ThoughtfulServiceBean.class);
if (thoughtfulServiceBean == null) {
thoughtfulServiceBean = new ThoughtfulServiceBean();
}
thoughtfulServiceBean.setIsEnableUsePowerTts(((CheckBox)view).isChecked());
ThoughtfulServiceBean.saveBean(this, thoughtfulServiceBean);
}
/**
* 悬浮窗权限检查与请求
*/
void canDrawOverlays() {
LogUtils.d(TAG, "onCanDrawOverlays: 检查悬浮窗权限");
// API6.0+校验权限
if (Build.VERSION.SDK_INT >= 23 && !Settings.canDrawOverlays(this)) {
LogUtils.d(TAG, "onCanDrawOverlays: 未开启悬浮窗权限,发起请求");
showDrawOverlayRequestDialog();
} else {
ToastUtils.show("悬浮窗权限已开启");
}
}
/**
* 显示悬浮窗权限请求对话框
*/
private void showDrawOverlayRequestDialog() {
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("权限请求")
.setMessage("为保证通话监听功能正常,需开启悬浮窗权限")
.setPositiveButton("去设置", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
jumpToDrawOverlaySettings();
}
})
.setNegativeButton("稍后", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
// 解决对话框焦点问题
if (dialog.getWindow() != null) {
dialog.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
}
dialog.show();
}
/**
* 跳转悬浮窗权限设置页面(反射适配低版本)
*/
private void jumpToDrawOverlaySettings() {
LogUtils.d(TAG, "jumpToDrawOverlaySettings: 跳转悬浮窗权限设置");
try {
// 反射获取设置页面Action避免高版本API依赖
Class<?> settingsClazz = Settings.class;
Field actionField = settingsClazz.getDeclaredField("ACTION_MANAGE_OVERLAY_PERMISSION");
String action = (String) actionField.get(null);
// 跳转当前应用权限设置页
Intent intent = new Intent(action);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
} catch (Exception e) {
LogUtils.e(TAG, "jumpToDrawOverlaySettings: 跳转权限设置失败", e);
Toast.makeText(this, "请手动在设置中开启悬浮窗权限", Toast.LENGTH_LONG).show();
}
}
}

View File

@@ -3,47 +3,73 @@ package cc.winboll.studio.powerbell.activities;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.ToastUtils;
import cc.winboll.studio.powerbell.App;
import cc.winboll.studio.powerbell.R;
import cc.winboll.studio.powerbell.utils.APPPlusUtils;
import cc.winboll.studio.powerbell.App;
/**
* 应用快捷方式活动类,处理应用图标快捷菜单的切换请求
* 适配 API30基于 Java7 开发
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2025/11/15 13:45
* @Describe 应用快捷方式活动类
*/
public class ShortcutActionActivity extends Activity {
// ======================== 静态常量 =========================
public static final String TAG = "ShortcutActionActivity";
// 快捷指令常量
private static final String ACTION_SWITCH_TO_EN1 = "switchto_en1";
private static final String ACTION_SWITCH_TO_CN1 = "switchto_cn1";
private static final String ACTION_SWITCH_TO_CN2 = "switchto_cn2";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 处理应用级别的切换请求
// ======================== 生命周期方法 =========================
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LogUtils.d(TAG, "【onCreate】ShortcutActionActivity 启动,开始处理快捷方式请求");
// 处理应用图标快捷菜单的切换请求
handleSwitchRequest();
finish();
}
/**
* 处理应用图标快捷菜单的请求
LogUtils.d(TAG, "【onCreate】快捷方式请求处理完成关闭活动");
finish();
}
// ======================== 业务逻辑方法 =========================
/**
* 处理应用图标快捷菜单的请求,根据意图数据切换应用启动组件
*/
private void handleSwitchRequest() {
Intent intent = getIntent();
if (intent != null && "switchto_en1".equals(intent.getDataString())) {
APPPlusUtils.switchAppLauncherToComponent(this, App.COMPONENT_EN1);
ToastUtils.show("切换至" + getString(R.string.app_name) + "图标");
//moveTaskToBack(true);
if (intent == null) {
LogUtils.w(TAG, "【handleSwitchRequest】意图为空无法处理快捷方式请求");
return;
}
if (intent != null && "switchto_cn1".equals(intent.getDataString())) {
APPPlusUtils.switchAppLauncherToComponent(this, App.COMPONENT_CN1);
ToastUtils.show("切换至" + getString(R.string.app_name_cn1) + "图标");
//moveTaskToBack(true);
}
if (intent != null && "switchto_cn2".equals(intent.getDataString())) {
APPPlusUtils.switchAppLauncherToComponent(this, App.COMPONENT_CN2);
ToastUtils.show("切换至" + getString(R.string.app_name_cn2) + "图标");
//moveTaskToBack(true);
String dataString = intent.getDataString();
LogUtils.d(TAG, "【handleSwitchRequest】获取到快捷指令" + dataString);
// 匹配快捷指令并切换组件
if (ACTION_SWITCH_TO_EN1.equals(dataString)) {
APPPlusUtils.switchAppLauncherToComponent(this, App.COMPONENT_EN1);
String toastMsg = "切换至" + getString(R.string.app_name) + "图标";
ToastUtils.show(toastMsg);
LogUtils.d(TAG, "【handleSwitchRequest】已切换至EN1组件" + App.COMPONENT_EN1);
} else if (ACTION_SWITCH_TO_CN1.equals(dataString)) {
APPPlusUtils.switchAppLauncherToComponent(this, App.COMPONENT_CN1);
String toastMsg = "切换至" + getString(R.string.app_name_cn1) + "图标";
ToastUtils.show(toastMsg);
LogUtils.d(TAG, "【handleSwitchRequest】已切换至CN1组件" + App.COMPONENT_CN1);
} else if (ACTION_SWITCH_TO_CN2.equals(dataString)) {
APPPlusUtils.switchAppLauncherToComponent(this, App.COMPONENT_CN2);
String toastMsg = "切换至" + getString(R.string.app_name_cn2) + "图标";
ToastUtils.show(toastMsg);
LogUtils.d(TAG, "【handleSwitchRequest】已切换至CN2组件" + App.COMPONENT_CN2);
} else {
LogUtils.w(TAG, "【handleSwitchRequest】未匹配到有效快捷指令" + dataString);
}
}
}

View File

@@ -1,10 +1,5 @@
package cc.winboll.studio.powerbell.activities;
/**
* @Author ZhanGSKen<zhangsken@qq.com>
* @Date 2025/06/19 20:35
* @Describe 应用窗口基类
*/
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
@@ -21,98 +16,190 @@ import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import cc.winboll.studio.libaes.interfaces.IWinBoLLActivity;
import cc.winboll.studio.libaes.models.AESThemeBean;
import cc.winboll.studio.libaes.utils.AESThemeUtil;
import cc.winboll.studio.libaes.utils.WinBoLLActivityManager;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.powerbell.BuildConfig;
import cc.winboll.studio.powerbell.R;
@SuppressLint("SetTextI18n")
/**
* @Author ZhanGSKen<zhangsken@qq.com>
* @Date 2025/06/19 20:35
* @Describe 应用窗口基类提供主题设置、Activity 管理、工具栏配置、全屏切换、版本标签显示等通用功能
* 适配 API30基于 Java7 开发,所有子类需继承此类实现统一窗口行为
*/
public abstract class WinBoLLActivity extends AppCompatActivity implements IWinBoLLActivity {
// ======================== 静态常量 =========================
public static final String TAG = "WinBoLLActivity";
protected TextView mTagView;
private static final String VERSION_TAG_TEXT = "MIMO SDK V%s"; // 版本标签文本格式
private static final float VERSION_TAG_TEXT_SIZE = 10f; // 版本标签字体大小sp
// ======================== 成员变量 =========================
protected volatile AESThemeBean.ThemeType mThemeType; // 当前主题类型
protected TextView mTagView; // 版本标签显示控件
// ======================== 接口实现 & 抽象方法 =========================
@Override
public abstract Activity getActivity();
@Override
public abstract String getTag();
// ======================== 生命周期方法 =========================
@Override
protected void onCreate(Bundle savedInstanceState) {
LogUtils.d(TAG, String.format("【%s-onCreate】窗口基类初始化开始", getTag()));
// 初始化主题
mThemeType = getThemeType();
setThemeStyle();
super.onCreate(savedInstanceState);
changeFullScreen(this);
LogUtils.d(TAG, String.format("【%s-onCreate】窗口基类初始化完成当前主题%s", getTag(), mThemeType));
}
@Override
protected void onStart() {
super.onStart();
LogUtils.d(TAG, String.format("【%s-onStart】添加版本标签到页面", getTag()));
// 添加版本标签
addVersionNameToContentView();
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// 注册到Activity管理器
WinBoLLActivityManager.getInstance().add(this);
LogUtils.d(TAG, String.format("【%s-onPostCreate】已注册到Activity管理器", getTag()));
}
@Override
protected void onDestroy() {
super.onDestroy();
// 从Activity管理器移除
WinBoLLActivityManager.getInstance().registeRemove(this);
LogUtils.d(TAG, String.format("【%s-onDestroy】已从Activity管理器移除", getTag()));
}
// ======================== 主题相关方法 =========================
/**
* 获取当前主题类型
* @return 主题类型枚举
*/
AESThemeBean.ThemeType getThemeType() {
int themeId = AESThemeUtil.getThemeTypeID(getApplicationContext());
AESThemeBean.ThemeType themeType = AESThemeBean.getThemeStyleType(themeId);
LogUtils.d(TAG, String.format("【%s-getThemeType】获取主题类型ID%d类型%s", getTag(), themeId, themeType));
return themeType;
}
/**
* 设置主题样式
*/
void setThemeStyle() {
int themeId = AESThemeUtil.getThemeTypeID(getApplicationContext());
setTheme(themeId);
LogUtils.d(TAG, String.format("【%s-setThemeStyle】应用主题样式ID%d", getTag(), themeId));
}
// ======================== UI 配置方法 =========================
/**
* 添加版本标签到页面底部
*/
protected void addVersionNameToContentView() {
if (!isTagViewVisible()) {
LogUtils.d(TAG, String.format("【%s-addVersionNameToContentView】版本标签不可见跳过添加", getTag()));
return;
}
if (mTagView == null) {
mTagView = new TextView(this);
// 配置版本标签样式
mTagView.setTextColor(Color.GRAY);
mTagView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
mTagView.setText("MIMO SDK V" + BuildConfig.VERSION_NAME);
mTagView.setTextSize(TypedValue.COMPLEX_UNIT_SP, VERSION_TAG_TEXT_SIZE);
mTagView.setText(String.format(VERSION_TAG_TEXT, BuildConfig.VERSION_NAME));
// 配置布局参数
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
// 添加到根布局
FrameLayout frameLayout = findViewById(android.R.id.content);
frameLayout.addView(mTagView, params);
if (frameLayout != null) {
frameLayout.addView(mTagView, params);
LogUtils.d(TAG, String.format("【%s-addVersionNameToContentView】版本标签添加完成版本%s", getTag(), BuildConfig.VERSION_NAME));
} else {
LogUtils.w(TAG, String.format("【%s-addVersionNameToContentView】根布局为空无法添加版本标签", getTag()));
}
}
}
protected boolean isTagViewVisible() {
return true;
}
/**
* 配置工具栏,显示返回按钮
*/
public void setupToolbar() {
Toolbar mToolbar = findViewById(R.id.toolbar);
if (mToolbar != null) {
setSupportActionBar(mToolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
LogUtils.d(TAG, String.format("【%s-setupToolbar】工具栏配置完成已显示返回按钮", getTag()));
} else {
LogUtils.w(TAG, String.format("【%s-setupToolbar】ActionBar为空无法显示返回按钮", getTag()));
}
} else {
LogUtils.w(TAG, String.format("【%s-setupToolbar】未找到工具栏控件IDtoolbar", getTag()));
}
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
//GlobalApplication.getWinBoLLActivityManager().add(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
//GlobalApplication.getWinBoLLActivityManager().registeRemove(this);
/**
* 版本标签是否可见
* @return 默认为true子类可重写修改
*/
protected boolean isTagViewVisible() {
return true;
}
// ======================== 菜单 & 返回键处理 =========================
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
//GlobalApplication.getWinBoLLActivityManager().startWinBoLLActivity(getApplicationContext(), MainActivity.class);
LogUtils.d(TAG, String.format("【%s-onOptionsItemSelected】点击返回菜单", getTag()));
return true;
}
// 在switch语句中处理每个ID并在处理完后返回true未处理的情况返回false。
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
super.onBackPressed();
//GlobalApplication.getWinBoLLActivityManager().startWinBoLLActivity(getApplicationContext(), MainActivity.class);
}
public void changeFullScreen(Activity activity) {
Window window = activity.getWindow();
if (window == null){
@Override
public void onBackPressed() {
super.onBackPressed();
LogUtils.d(TAG, String.format("【%s-onBackPressed】触发返回键", getTag()));
}
// ======================== 工具方法 =========================
/**
* 切换至全屏模式,隐藏状态栏与导航栏
* @param activity 目标Activity
*/
public void changeFullScreen(Activity activity) {
if (activity == null) {
LogUtils.w(TAG, String.format("【%s-changeFullScreen】目标Activity为空无法切换全屏", getTag()));
return;
}
Window window = activity.getWindow();
if (window == null) {
LogUtils.w(TAG, String.format("【%s-changeFullScreen】窗口为空无法切换全屏", getTag()));
return;
}
View decorView = window.getDecorView();
if (decorView == null){
if (decorView == null) {
LogUtils.w(TAG, String.format("【%s-changeFullScreen】DecorView为空无法切换全屏", getTag()));
return;
}
// 配置全屏标志位
int flag = decorView.getSystemUiVisibility();
flag |= View.SYSTEM_UI_FLAG_FULLSCREEN;
flag |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
@@ -120,6 +207,9 @@ public abstract class WinBoLLActivity extends AppCompatActivity implements IWinB
flag |= View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
flag |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
decorView.setSystemUiVisibility(flag);
// 配置窗口标志位
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
LogUtils.d(TAG, String.format("【%s-changeFullScreen】已切换至全屏模式", getTag()));
}
}

View File

@@ -1,60 +1,106 @@
package cc.winboll.studio.powerbell.adapters;
/**
* @Author ZhanGSKen<zhangsken@qq.com>
* @Date 2025/03/22 14:38:55
* @Describe 电池报告数据适配器
*/
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.powerbell.R;
import cc.winboll.studio.powerbell.adapters.BatteryAdapter;
import cc.winboll.studio.powerbell.beans.BatteryData;
import cc.winboll.studio.powerbell.models.BatteryData;
import java.util.ArrayList;
import java.util.List;
/**
* 电池报告数据适配器用于RecyclerView展示电池电量、充放电时间数据
* 适配 API30基于 Java7 开发
* @Author ZhanGSKen<zhangsken@qq.com>
* @Date 2025/03/22 14:38:55
* @Describe 电池报告数据适配器
*/
public class BatteryAdapter extends RecyclerView.Adapter<BatteryAdapter.ViewHolder> {
// ======================== 静态常量 =========================
public static final String TAG = "BatteryAdapter";
private List<BatteryData> dataList = new ArrayList<>();
private static final String FORMAT_BATTERY_LEVEL = "%d%%"; // 电量显示格式
private static final String PREFIX_DISCHARGE_TIME = "使用时间: "; // 放电时间前缀
private static final String PREFIX_CHARGE_TIME = "充电时间: "; // 充电时间前缀
public void updateData(List<BatteryData> newData) {
dataList = newData;
notifyDataSetChanged();
// ======================== 成员变量 =========================
private List<BatteryData> dataList = new ArrayList<>(); // 电池数据列表
// ======================== 构造方法 =========================
public BatteryAdapter() {
LogUtils.d(TAG, "【BatteryAdapter】适配器初始化初始数据列表为空");
}
// ======================== 数据操作方法 =========================
/**
* 更新适配器数据并刷新列表
* @param newData 新的电池数据列表
*/
public void updateData(List<BatteryData> newData) {
LogUtils.d(TAG, "【updateData】开始更新数据新数据列表是否为空" + (newData == null));
// 判空处理,避免空指针
if (newData != null) {
dataList = newData;
LogUtils.d(TAG, "【updateData】数据更新完成当前数据量" + dataList.size());
} else {
dataList.clear();
LogUtils.w(TAG, "【updateData】新数据列表为空已清空本地数据");
}
notifyDataSetChanged();
LogUtils.d(TAG, "【updateData】已通知列表刷新");
}
// ======================== RecyclerView 重写方法 =========================
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LogUtils.d(TAG, "【onCreateViewHolder】创建ViewHolder父容器" + parent.getContext().getClass().getSimpleName());
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_battery_report, parent, false);
return new ViewHolder(view);
.inflate(R.layout.item_battery_report, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
LogUtils.d(TAG, "【onCreateViewHolder】ViewHolder创建完成");
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
LogUtils.d(TAG, "【onBindViewHolder】绑定ViewHolder位置" + position);
// 判空与越界校验
if (dataList == null || dataList.isEmpty() || position >= dataList.size()) {
LogUtils.w(TAG, "【onBindViewHolder】数据异常无法绑定视图位置" + position);
return;
}
BatteryData item = dataList.get(position);
holder.tvLevel.setText(String.format("%d%%", item.getCurrentLevel()));
holder.tvDischargeTime.setText("使用时间: " + item.getDischargeTime());
holder.tvChargeTime.setText("充电时间: " + item.getChargeTime());
// 绑定数据到视图
holder.tvLevel.setText(String.format(FORMAT_BATTERY_LEVEL, item.getCurrentLevel()));
holder.tvDischargeTime.setText(PREFIX_DISCHARGE_TIME + item.getDischargeTime());
holder.tvChargeTime.setText(PREFIX_CHARGE_TIME + item.getChargeTime());
LogUtils.d(TAG, "【onBindViewHolder】视图绑定完成位置" + position + ",电量:" + item.getCurrentLevel() + "%");
}
@Override
public int getItemCount() {
return dataList.size();
int count = dataList.size();
LogUtils.d(TAG, "【getItemCount】获取条目数量" + count);
return count;
}
// ======================== ViewHolder 内部类 =========================
static class ViewHolder extends RecyclerView.ViewHolder {
TextView tvLevel;
TextView tvDischargeTime;
TextView tvChargeTime;
TextView tvLevel; // 电量显示
TextView tvDischargeTime; // 放电时间显示
TextView tvChargeTime; // 充电时间显示
ViewHolder(View itemView) {
super(itemView);
// 初始化视图控件
tvLevel = itemView.findViewById(R.id.tvLevel);
tvDischargeTime = itemView.findViewById(R.id.tvDischargeTime);
tvChargeTime = itemView.findViewById(R.id.tvChargeTime);
LogUtils.d(TAG, "【ViewHolder】控件初始化完成");
}
}
}

View File

@@ -1,130 +0,0 @@
package cc.winboll.studio.powerbell.beans;
/**
* @Author ZhanGSKen<zhangsken@qq.com>
* @Date 2024/04/29 17:24:53
* @Describe 应用运行参数类
*/
import android.util.JsonReader;
import android.util.JsonWriter;
import cc.winboll.studio.libappbase.BaseBean;
import java.io.IOException;
import java.io.Serializable;
public class AppConfigBean extends BaseBean implements Serializable {
transient public static final String TAG = "AppConfigBean";
boolean isEnableUsegeReminder = false;
int usegeReminderValue = 45;
boolean isEnableChargeReminder = false;
int chargeReminderValue = 100;
// 铃声提醒间隔时间。.
int reminderIntervalTime = 5000;
// 电池是否正在充电。
boolean isCharging = false;
// 电池当前电量。.
int currentValue = -1;
public AppConfigBean() {
setChargeReminderValue(100);
setIsEnableChargeReminder(false);
setUsegeReminderValue(10);
setIsEnableUsegeReminder(false);
setReminderIntervalTime(5000);
}
public void setReminderIntervalTime(int reminderIntervalTime) {
this.reminderIntervalTime = reminderIntervalTime;
}
public int getReminderIntervalTime() {
return reminderIntervalTime;
}
public void setIsCharging(boolean isCharging) {
this.isCharging = isCharging;
}
public boolean isCharging() {
return isCharging;
}
public void setCurrentValue(int currentValue) {
this.currentValue = currentValue;
}
public int getCurrentValue() {
return currentValue;
}
public void setIsEnableUsegeReminder(boolean isEnableUsegeReminder) {
this.isEnableUsegeReminder = isEnableUsegeReminder;
}
public boolean isEnableUsegeReminder() {
return isEnableUsegeReminder;
}
public void setUsegeReminderValue(int usegeReminderValue) {
this.usegeReminderValue = usegeReminderValue;
}
public int getUsegeReminderValue() {
return usegeReminderValue;
}
public void setIsEnableChargeReminder(boolean isEnableChargeReminder) {
this.isEnableChargeReminder = isEnableChargeReminder;
}
public boolean isEnableChargeReminder() {
return isEnableChargeReminder;
}
public void setChargeReminderValue(int chargeReminderValue) {
this.chargeReminderValue = chargeReminderValue;
}
public int getChargeReminderValue() {
return chargeReminderValue;
}
@Override
public String getName() {
return AppConfigBean.class.getName();
}
@Override
public void writeThisToJsonWriter(JsonWriter jsonWriter) throws IOException {
super.writeThisToJsonWriter(jsonWriter);
AppConfigBean bean = this;
jsonWriter.name("isEnableUsegeReminder").value(bean.isEnableUsegeReminder());
jsonWriter.name("usegeReminderValue").value(bean.getUsegeReminderValue());
jsonWriter.name("isEnableChargeReminder").value(bean.isEnableChargeReminder());
jsonWriter.name("chargeReminderValue").value(bean.getChargeReminderValue());
}
@Override
public BaseBean readBeanFromJsonReader(JsonReader jsonReader) throws IOException {
AppConfigBean bean = new AppConfigBean();
jsonReader.beginObject();
while (jsonReader.hasNext()) {
String name = jsonReader.nextName();
if (name.equals("isEnableUsegeReminder")) {
bean.setIsEnableUsegeReminder(jsonReader.nextBoolean());
} else if (name.equals("usegeReminderValue")) {
bean.setUsegeReminderValue(jsonReader.nextInt());
} else if (name.equals("isEnableChargeReminder")) {
bean.setIsEnableChargeReminder(jsonReader.nextBoolean());
} else if (name.equals("chargeReminderValue")) {
bean.setChargeReminderValue(jsonReader.nextInt());
} else {
jsonReader.skipValue();
}
}
// 结束 JSON 对象
jsonReader.endObject();
return bean;
}
}

View File

@@ -1,99 +0,0 @@
package cc.winboll.studio.powerbell.beans;
/**
* @Author ZhanGSKen<zhangsken@qq.com>
* @Date 2024/07/18 11:52:28
* @Describe 应用背景图片数据类
*/
import android.util.JsonReader;
import android.util.JsonWriter;
import cc.winboll.studio.libappbase.BaseBean;
import java.io.IOException;
public class BackgroundPictureBean extends BaseBean {
public static final String TAG = "BackgroundPictureBean";
int backgroundWidth = 100;
int backgroundHeight = 100;
boolean isUseBackgroundFile = false;
// 图片拾取像素颜色
int pixelColor = 0;
public BackgroundPictureBean() {
}
public BackgroundPictureBean(String recivedFileName, boolean isUseBackgroundFile) {
this.isUseBackgroundFile = isUseBackgroundFile;
}
public void setPixelColor(int pixelColor) {
this.pixelColor = pixelColor;
}
public int getPixelColor() {
return pixelColor;
}
public void setBackgroundWidth(int backgroundWidth) {
this.backgroundWidth = backgroundWidth;
}
public int getBackgroundWidth() {
return backgroundWidth;
}
public void setBackgroundHeight(int backgroundHeight) {
this.backgroundHeight = backgroundHeight;
}
public int getBackgroundHeight() {
return backgroundHeight;
}
public void setIsUseBackgroundFile(boolean isUseBackgroundFile) {
this.isUseBackgroundFile = isUseBackgroundFile;
}
public boolean isUseBackgroundFile() {
return isUseBackgroundFile;
}
@Override
public String getName() {
return BackgroundPictureBean.class.getName();
}
@Override
public void writeThisToJsonWriter(JsonWriter jsonWriter) throws IOException {
super.writeThisToJsonWriter(jsonWriter);
BackgroundPictureBean bean = this;
jsonWriter.name("backgroundWidth").value(bean.getBackgroundWidth());
jsonWriter.name("backgroundHeight").value(bean.getBackgroundHeight());
jsonWriter.name("isUseBackgroundFile").value(bean.isUseBackgroundFile());
jsonWriter.name("pixelColor").value(bean.getPixelColor());
}
@Override
public BaseBean readBeanFromJsonReader(JsonReader jsonReader) throws IOException {
BackgroundPictureBean bean = new BackgroundPictureBean();
jsonReader.beginObject();
while (jsonReader.hasNext()) {
String name = jsonReader.nextName();
if (name.equals("backgroundWidth")) {
bean.setBackgroundWidth(jsonReader.nextInt());
} else if (name.equals("backgroundHeight")) {
bean.setBackgroundHeight(jsonReader.nextInt());
} else if (name.equals("isUseBackgroundFile")) {
bean.setIsUseBackgroundFile(jsonReader.nextBoolean());
} else if (name.equals("pixelColor")) {
bean.setPixelColor(jsonReader.nextInt());
} else {
jsonReader.skipValue();
}
}
// 结束 JSON 对象
jsonReader.endObject();
return bean;
}
}

View File

@@ -1,26 +0,0 @@
package cc.winboll.studio.powerbell.beans;
/**
* @Author ZhanGSKen<zhangsken@qq.com>
* @Date 2025/03/22 14:30:51
* @Describe 电池报告数据模型
*/
public class BatteryData {
public static final String TAG = "BatteryData";
private int currentLevel;
private String dischargeTime;
private String chargeTime;
public BatteryData(int currentLevel, String dischargeTime, String chargeTime) {
this.currentLevel = currentLevel;
this.dischargeTime = dischargeTime;
this.chargeTime = chargeTime;
}
public int getCurrentLevel() { return currentLevel; }
public String getDischargeTime() { return dischargeTime; }
public String getChargeTime() { return chargeTime; }
}

View File

@@ -1,75 +0,0 @@
package cc.winboll.studio.powerbell.beans;
import android.util.JsonReader;
import android.util.JsonWriter;
import cc.winboll.studio.libappbase.BaseBean;
import java.io.IOException;
import java.io.Serializable;
public class BatteryInfoBean extends BaseBean implements Serializable {
public static final String TAG = "BatteryInfoBean";
// 记录电量的时间戳
long timeStamp;
// 电量值
int battetyValue;
public BatteryInfoBean() {
this.timeStamp = 0;
this.battetyValue = 0;
}
public BatteryInfoBean(long timeStamp, int battetyValue) {
this.timeStamp = timeStamp;
this.battetyValue = battetyValue;
}
public void setTimeStamp(long timeStamp) {
this.timeStamp = timeStamp;
}
public long getTimeStamp() {
return timeStamp;
}
public void setBattetyValue(int battetyValue) {
this.battetyValue = battetyValue;
}
public int getBattetyValue() {
return battetyValue;
}
@Override
public String getName() {
return BatteryInfoBean.class.getName();
}
@Override
public void writeThisToJsonWriter(JsonWriter jsonWriter) throws IOException {
super.writeThisToJsonWriter(jsonWriter);
BatteryInfoBean bean = this;
jsonWriter.name("timeStamp").value(bean.getTimeStamp());
jsonWriter.name("battetyValue").value(bean.getBattetyValue());
}
@Override
public BaseBean readBeanFromJsonReader(JsonReader jsonReader) throws IOException {
BatteryInfoBean bean = new BatteryInfoBean();
jsonReader.beginObject();
while (jsonReader.hasNext()) {
String name = jsonReader.nextName();
if (name.equals("timeStamp")) {
bean.setTimeStamp(jsonReader.nextLong());
} else if (name.equals("battetyValue")) {
bean.setBattetyValue(jsonReader.nextInt());
} else {
jsonReader.skipValue();
}
}
// 结束 JSON 对象
jsonReader.endObject();
return bean;
}
}

View File

@@ -1,63 +0,0 @@
package cc.winboll.studio.powerbell.beans;
/**
* @Author ZhanGSKen<zhangsken@qq.com>
* @Date 2024/07/18 07:06:07
* @Describe 服务控制参数
*/
import android.util.JsonReader;
import android.util.JsonWriter;
import cc.winboll.studio.libappbase.BaseBean;
import java.io.IOException;
public class ControlCenterServiceBean extends BaseBean {
public static final String TAG = "ControlCenterServiceBean";
boolean isEnableService = false;
public ControlCenterServiceBean() {
this.isEnableService = false;
}
public ControlCenterServiceBean(boolean isEnableService) {
this.isEnableService = isEnableService;
}
public void setIsEnableService(boolean isEnableService) {
this.isEnableService = isEnableService;
}
public boolean isEnableService() {
return isEnableService;
}
@Override
public String getName() {
return ControlCenterServiceBean.class.getName();
}
@Override
public void writeThisToJsonWriter(JsonWriter jsonWriter) throws IOException {
super.writeThisToJsonWriter(jsonWriter);
ControlCenterServiceBean bean = this;
jsonWriter.name("isEnableService").value(bean.isEnableService());
}
@Override
public BaseBean readBeanFromJsonReader(JsonReader jsonReader) throws IOException {
ControlCenterServiceBean bean = new ControlCenterServiceBean();
jsonReader.beginObject();
while (jsonReader.hasNext()) {
String name = jsonReader.nextName();
if (name.equals("isEnableService")) {
bean.setIsEnableService(jsonReader.nextBoolean());
} else {
jsonReader.skipValue();
}
}
// 结束 JSON 对象
jsonReader.endObject();
return bean;
}
}

View File

@@ -1,40 +0,0 @@
package cc.winboll.studio.powerbell.beans;
// 应用消息结构
//
public class NotificationMessage {
String Title;
String Content;
String RemindMSG;
public NotificationMessage(String title, String content) {
Title = title;
Content = content;
}
public void setRemindMSG(String remindMSG) {
RemindMSG = remindMSG;
}
public String getRemindMSG() {
return RemindMSG;
}
public void setTitle(String title) {
Title = title;
}
public String getTitle() {
return Title;
}
public void setContent(String content) {
Content = content;
}
public String getContent() {
return Content;
}
}

View File

@@ -1,140 +1,153 @@
package cc.winboll.studio.powerbell.dialogs;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.powerbell.App;
import cc.winboll.studio.powerbell.MainActivity;
import cc.winboll.studio.powerbell.R;
import cc.winboll.studio.powerbell.activities.BackgroundPictureActivity;
import cc.winboll.studio.powerbell.utils.BackgroundPictureUtils;
import cc.winboll.studio.powerbell.utils.FileUtils;
import cc.winboll.studio.powerbell.utils.UriUtil;
import java.io.File;
import java.io.IOException;
import cc.winboll.studio.powerbell.activities.BackgroundSettingsActivity;
import cc.winboll.studio.powerbell.utils.BackgroundSourceUtils;
import cc.winboll.studio.powerbell.utils.UriUtils;
import cc.winboll.studio.powerbell.views.BackgroundView;
/**
* 背景图片的接收分享文件后的预览对话框
* 适配 API30基于 Java7 开发支持分享图片的Uri解析、预览与确认选择
* @Author ZhanGSKen<zhangsken@qq.com>
* @Date 2024/04/25 16:27:53
* @Describe 背景图片的接收分享文件后的预览对话框
*/
public class BackgroundPicturePreviewDialog extends Dialog {
// ======================== 静态常量 =========================
public static final String TAG = "BackgroundPicturePreviewDialog";
private static final String TOAST_MSG_EMPTY_FILE = "接收到的文件为空。"; // 空文件提示文本
Context mContext;
BackgroundPictureUtils mBackgroundPictureUtils;
Button dialogbackgroundpicturepreviewButton1;
Button dialogbackgroundpicturepreviewButton2;
String mszPreReceivedFileName;
// ======================== 成员变量 =========================
private Context mContext; // 上下文对象
private IOnRecivedPictureListener mIOnRecivedPictureListener; // 图片接收监听
private Uri mUriRecivedPicture; // 接收的图片Uri
// 控件对象
private BackgroundView mBackgroundView; // 背景预览视图
private Button dialogbackgroundpicturepreviewButton1; // 取消按钮
private Button dialogbackgroundpicturepreviewButton2; // 确认按钮
public BackgroundPicturePreviewDialog(Context context) {
// ======================== 接口定义 =========================
/**
* 图片接收监听接口用于通知确认选择的图片Uri
*/
public interface IOnRecivedPictureListener {
void onAcceptRecivedPicture(Uri uriRecivedPicture);
}
// ======================== 构造方法 =========================
public BackgroundPicturePreviewDialog(Context context, IOnRecivedPictureListener iOnRecivedPictureListener) {
super(context);
setContentView(R.layout.dialog_backgroundpicturepreview);
initEnv();
LogUtils.d(TAG, "【BackgroundPicturePreviewDialog】对话框初始化开始");
// 初始化成员变量
mContext = context;
mBackgroundPictureUtils = ((BackgroundPictureActivity)context).mBackgroundPictureUtils;
ImageView imageView = findViewById(R.id.dialogbackgroundpicturepreviewImageView1);
copyAndViewRecivePicture(imageView);
mIOnRecivedPictureListener = iOnRecivedPictureListener;
// 设置布局与控件
setContentView(R.layout.dialog_backgroundpicturepreview);
initViews();
bindButtonClickEvents();
// 预览接收的图片
previewRecivedPicture();
LogUtils.d(TAG, "【BackgroundPicturePreviewDialog】对话框初始化完成");
}
// ======================== 视图初始化方法 =========================
/**
* 初始化对话框内所有控件
*/
private void initViews() {
mBackgroundView = findViewById(R.id.backgroundview);
dialogbackgroundpicturepreviewButton1 = findViewById(R.id.dialogbackgroundpicturepreviewButton1);
dialogbackgroundpicturepreviewButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 不使用分享到的图片
// 跳转到主窗口
Intent i = new Intent(mContext, MainActivity.class);
mContext.startActivity(i);
}
});
dialogbackgroundpicturepreviewButton2 = findViewById(R.id.dialogbackgroundpicturepreviewButton2);
dialogbackgroundpicturepreviewButton2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// 使用分享到的图片
//
//LogUtils.d(TAG, "mszReceivedFileName : " + mszReceivedFileName);
((IOnRecivedPictureListener)mContext).onAcceptRecivedPicture(mszPreReceivedFileName);
// 关闭对话框
dismiss();
}
});
}
void initEnv() {
LogUtils.d(TAG, "initEnv()");
mszPreReceivedFileName = "PreReceived.data";
LogUtils.d(TAG, "【initViews】对话框控件初始化完成");
}
void copyAndViewRecivePicture(ImageView imageView) {
//AppConfigUtils appConfigUtils = AppConfigUtils.getInstance((GlobalApplication)mContext.getApplicationContext());
BackgroundPictureActivity activity = ((BackgroundPictureActivity)mContext);
// ======================== 事件绑定方法 =========================
/**
* 绑定按钮点击事件
*/
private void bindButtonClickEvents() {
// 取消按钮:跳转到主页面并关闭对话框
dialogbackgroundpicturepreviewButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LogUtils.d(TAG, "【onClick】点击取消按钮跳转到主页面");
Intent intent = new Intent(mContext, MainActivity.class);
mContext.startActivity(intent);
dismiss();
LogUtils.d(TAG, "【onClick】对话框已关闭");
}
});
//取出文件uri
Uri uri = activity.getIntent().getData();
if (uri == null) {
uri = activity.getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
}
//获取文件真实地址
String szSrcImage = UriUtil.getFilePathFromUri(mContext, uri);
if (TextUtils.isEmpty(szSrcImage)) {
Toast.makeText(mContext, "接收到的文件为空。", Toast.LENGTH_SHORT).show();
// 确认按钮:通知监听并关闭对话框
dialogbackgroundpicturepreviewButton2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "【onClick】点击确认按钮通知接收图片");
if (mIOnRecivedPictureListener != null && mUriRecivedPicture != null) {
mIOnRecivedPictureListener.onAcceptRecivedPicture(mUriRecivedPicture);
LogUtils.d(TAG, "【onClick】已通知监听图片Uri" + mUriRecivedPicture);
} else {
LogUtils.w(TAG, "【onClick】监听为空或图片Uri无效无法通知");
}
dismiss();
LogUtils.d(TAG, "【onClick】对话框已关闭");
}
});
LogUtils.d(TAG, "【bindButtonClickEvents】按钮点击事件绑定完成");
}
// ======================== 业务逻辑方法 =========================
/**
* 预览接收的分享图片
*/
private void previewRecivedPicture() {
LogUtils.d(TAG, "【previewRecivedPicture】开始预览接收的图片");
// 校验上下文类型
if (!(mContext instanceof BackgroundSettingsActivity)) {
LogUtils.e(TAG, "【previewRecivedPicture】上下文不是BackgroundSettingsActivity无法获取图片Uri");
Toast.makeText(mContext, TOAST_MSG_EMPTY_FILE, Toast.LENGTH_SHORT).show();
dismiss();
return;
}
File fSrcImage = new File(szSrcImage);
//mszPreReceivedFileName = DateUtils.getDateNowString() + "-" + fSrcImage.getName();
File mfPreReceivedPhoto = new File(activity.mBackgroundPictureUtils.getBackgroundDir(), mszPreReceivedFileName);
// 复制源图片到剪裁文件
try {
FileUtils.copyFileUsingFileChannels(fSrcImage, mfPreReceivedPhoto);
LogUtils.d(TAG, "copyFileUsingFileChannels");
Drawable drawable = Drawable.createFromPath(mfPreReceivedPhoto.getPath());
imageView.setBackground(drawable);
//LogUtils.d(TAG, "mszPreReceivedFileName : " + mszPreReceivedFileName);
} catch (IOException e) {
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
}
}
//
// 创建图片背景图片目录
//
boolean createBackgroundFolder2(String szBackgroundFolder) {
// 文件路径参数为空值或无效值时返回false.
if (szBackgroundFolder == null | szBackgroundFolder.equals("")) {
return false;
}
LogUtils.d(TAG, "Background Folder Is : " + szBackgroundFolder);
File f = new File(szBackgroundFolder);
if (f.exists()) {
if (f.isDirectory()) {
return true;
} else {
// 工作路径不是一个目录
LogUtils.d(TAG, "createImageWorkFolder() error : szImageCacheFolder isDirectory return false. -->" + szBackgroundFolder);
return false;
}
BackgroundSettingsActivity activity = (BackgroundSettingsActivity) mContext;
// 从Intent中获取图片Uri优先getData其次EXTRA_STREAM
mUriRecivedPicture = activity.getIntent().getData();
if (mUriRecivedPicture == null) {
mUriRecivedPicture = activity.getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
LogUtils.d(TAG, "【previewRecivedPicture】从EXTRA_STREAM获取Uri" + mUriRecivedPicture);
} else {
return f.mkdirs();
LogUtils.d(TAG, "【previewRecivedPicture】从getData获取Uri" + mUriRecivedPicture);
}
}
public interface IOnRecivedPictureListener {
void onAcceptRecivedPicture(String szBackgroundFileName);
}
// 解析Uri为文件路径
String szSrcImage = UriUtils.getFilePathFromUri(mContext, mUriRecivedPicture);
//App.notifyMessage(TAG, "szSrcImage : " + szSrcImage);
if (TextUtils.isEmpty(szSrcImage)) {
LogUtils.w(TAG, "【previewRecivedPicture】解析的文件路径为空");
Toast.makeText(mContext, TOAST_MSG_EMPTY_FILE, Toast.LENGTH_SHORT).show();
dismiss();
return;
}
// 加载图片到预览视图
int nCurrentPixelColor = BackgroundSourceUtils.getInstance(mContext).getCurrentBackgroundBean().getPixelColor();
mBackgroundView.loadImage(nCurrentPixelColor, szSrcImage, true);
LogUtils.d(TAG, "【previewRecivedPicture】图片预览完成文件路径" + szSrcImage);
}
}

View File

@@ -0,0 +1,733 @@
package cc.winboll.studio.powerbell.dialogs;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.ToastUtils;
import cc.winboll.studio.powerbell.R;
import com.a4455jkjh.colorpicker.ColorPickerDialog;
import com.a4455jkjh.colorpicker.view.OnColorChangedListener;
/**
* 调色板对话框支持颜色拾取、RGB输入、透明度/亮度调节,兼容 API29-30+ 小米机型)
* 适配 API30基于 Java7 开发,返回 0xAARRGGBB 格式颜色(含透明度)
* @Author ZhanGSKen<zhangsken@qq.com>
* @Date 2025/12/16 11:47
* @Describe 调色板对话框支持颜色拾取、RGB输入、透明度/亮度调节,兼容 API29-30+ 小米机型)
*/
public class ColorPaletteDialog extends Dialog implements View.OnClickListener, SeekBar.OnSeekBarChangeListener {
// ====================== 静态常量(首屏可见,统一管理) ======================
public static final String TAG = "ColorPaletteDialog";
private static final int MAX_RGB_VALUE = 255; // RGB分量最大值0-255
private static final int DEFAULT_BRIGHTNESS = 100; // 默认亮度百分比100%,无调节)
private static final int BRIGHTNESS_STEP = 5; // 亮度调节步长每次±5%,精准流畅)
private static final int MIN_BRIGHTNESS = 10; // 亮度最小值10%,避免全黑看不见)
private static final int MAX_BRIGHTNESS = 200; // 亮度最大值200%,避免过曝失真)
private static final int MAX_ALPHA_PERCENT = 100; // 透明度最大值100%=不透明)
private static final int MIN_ALPHA_PERCENT = 0; // 透明度最小值0%=完全透明)
private static final String FORMAT_COLOR_HEX = "#%08X"; // 颜色值格式化AARRGGBB
private static final String FORMAT_PERCENT = "%d%%"; // 百分比格式化X%
// ====================== 回调接口(紧跟常量,逻辑关联) ======================
public interface OnColorSelectedListener {
void onColorSelected(int color); // 返回0xAARRGGBB格式颜色含透明度
}
// ====================== 成员变量(按优先级排序:核心数据→控件引用) ======================
// 核心数据:原始基准值(用户输入/选择颜色时更新)+ 实时调节值(亮度/透明度变化时更新)
private OnColorSelectedListener mListener; // 颜色选择回调(非空校验)
private int mInitialColor; // 初始颜色(传入的默认颜色)
private int mCurrentColor; // 当前最终颜色(含亮度+透明度调节)
private int mCurrentBrightnessPercent; // 当前亮度百分比10%-200%
// 透明度百分比0-100%,用户直观操作)+ 原始/实时值0-255颜色计算用
private int mOriginalAlphaPercent; // 原始透明度百分比(基准值,用户输入/选色时更新)
private int mCurrentAlphaPercent; // 实时透明度百分比(调节进度条时更新)
private int mOriginalAlpha; // 原始透明度0-255基准值
private int mCurrentAlpha; // 实时透明度0-255计算用
// RGB原始基准值+实时调节值
private int mOriginalR; // 原始R分量基准值用户输入/选色时更新)
private int mOriginalG; // 原始G分量基准值用户输入/选色时更新)
private int mOriginalB; // 原始B分量基准值用户输入/选色时更新)
private int mCurrentR; // 实时R分量亮度调节后同步输入框显示
private int mCurrentG; // 实时G分量亮度调节后同步输入框显示
private int mCurrentB; // 实时B分量亮度调节后同步输入框显示
// 并发控制标记:是否是应用程序自身在更新颜色(避免循环回调/重复触发)
private static volatile boolean isAppSelfUpdatingColor = false;
// 控件引用
private ImageView ivColorPicker; // 颜色预览拾取框
private ImageView ivColorScaler; // 颜色渐变拾取框
private EditText etR; // R分量输入框显示实时调节值
private EditText etG; // G分量输入框显示实时调节值
private EditText etB; // B分量输入框显示实时调节值
private EditText etColorValue; // 颜色值输入框(#AARRGGBB显示最终值
private SeekBar sbAlpha; // 透明度调节进度条0-100%
private TextView tvAlphaValue; // 透明度数值显示X%
private TextView tvBrightnessMinus;// 亮度减少按钮(-
private TextView tvBrightnessValue;// 亮度数值显示X%,直观易懂)
private TextView tvBrightnessPlus; // 亮度增加按钮(+
private TextView tvConfirm; // 确认按钮
private TextView tvCancel; // 取消按钮
// ====================== 构造方法(初始化核心数据,严格校验) ======================
public ColorPaletteDialog(Context context, int initialColor, OnColorSelectedListener listener) {
super(context, R.style.CustomDialogStyle);
this.mInitialColor = initialColor;
this.mListener = listener;
// 1. 强制回调非空,避免后续空指针(容错)
if (mListener == null) {
throw new IllegalArgumentException("OnColorSelectedListener can not be null!");
}
// 2. 解析初始颜色:原始基准值 = 实时值(初始无调节)
this.mOriginalAlpha = Color.alpha(initialColor);
this.mOriginalAlphaPercent = alpha2Percent(mOriginalAlpha);
this.mCurrentAlpha = mOriginalAlpha;
this.mCurrentAlphaPercent = mOriginalAlphaPercent;
this.mOriginalR = Color.red(initialColor);
this.mOriginalG = Color.green(initialColor);
this.mOriginalB = Color.blue(initialColor);
this.mCurrentR = mOriginalR;
this.mCurrentG = mOriginalG;
this.mCurrentB = mOriginalB;
// 3. 初始化当前状态默认亮度100%,当前颜色=初始颜色)
this.mCurrentBrightnessPercent = DEFAULT_BRIGHTNESS;
this.mCurrentColor = initialColor;
LogUtils.d(TAG, String.format("init dialog success | 初始颜色:%s | 原始RGB%d,%d,%d | 原始透明度:%s | 初始亮度:%s",
String.format(FORMAT_COLOR_HEX, initialColor),
mOriginalR, mOriginalG, mOriginalB,
String.format(FORMAT_PERCENT, mOriginalAlphaPercent),
String.format(FORMAT_PERCENT, mCurrentBrightnessPercent)));
}
// ====================== 生命周期方法(按执行顺序排列,逻辑清晰) ======================
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); // 隐藏标题栏
View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_color_palette, null);
setContentView(view);
// 初始化流程:控件绑定→数据赋值→监听设置→尺寸适配(小米机型优先适配)
initViewBind(view);
initData();
initListener();
adjustDialogSize();
LogUtils.d(TAG, "dialog create complete | 适配小米API29-30机型");
}
@Override
public void dismiss() {
super.dismiss();
// 释放资源,避免内存泄漏(回调引用置空)
mListener = null;
LogUtils.d(TAG, "dialog dismiss | 释放资源完成");
}
// ====================== 初始化核心方法(职责单一,便于维护) ======================
/**
* 控件绑定
*/
private void initViewBind(View view) {
ivColorPicker = view.findViewById(R.id.iv_color_picker);
ivColorScaler = view.findViewById(R.id.iv_color_scaler);
etR = view.findViewById(R.id.et_r);
etG = view.findViewById(R.id.et_g);
etB = view.findViewById(R.id.et_b);
etColorValue = view.findViewById(R.id.et_color_value);
sbAlpha = view.findViewById(R.id.sb_alpha);
tvAlphaValue = view.findViewById(R.id.tv_alpha_value);
tvBrightnessMinus = view.findViewById(R.id.tv_brightness_minus);
tvBrightnessValue = view.findViewById(R.id.tv_brightness_value);
tvBrightnessPlus = view.findViewById(R.id.tv_brightness_plus);
tvConfirm = view.findViewById(R.id.tv_confirm);
tvCancel = view.findViewById(R.id.tv_cancel);
// 控件非空校验(小米低版本容错,绑定失败直接关闭对话框)
if (ivColorPicker == null || ivColorScaler == null || etR == null || etG == null || etB == null || etColorValue == null
|| sbAlpha == null || tvAlphaValue == null
|| tvBrightnessMinus == null || tvBrightnessValue == null || tvBrightnessPlus == null
|| tvConfirm == null || tvCancel == null) {
LogUtils.e(TAG, "view bind failed | 请检查布局ID是否正确");
dismiss();
return;
}
LogUtils.d(TAG, "view bind complete | 所有控件绑定成功");
}
/**
* 数据初始化(无监听状态下赋值,避免循环回调)
*/
private void initData() {
// 1. 颜色预览(显示当前最终颜色,初始=原始颜色)
ivColorPicker.setBackgroundColor(mCurrentColor);
// 2. RGB输入框显示「实时分量」初始=原始值)
etR.setText(String.valueOf(mCurrentR));
etG.setText(String.valueOf(mCurrentG));
etB.setText(String.valueOf(mCurrentB));
// 3. 颜色值输入框(显示当前最终颜色,格式#AARRGGBB
etColorValue.setText(String.format(FORMAT_COLOR_HEX, mCurrentColor));
// 4. 透明度控件(进度条+文本,初始=原始透明度)
sbAlpha.setProgress(mCurrentAlphaPercent);
tvAlphaValue.setText(String.format(FORMAT_PERCENT, mCurrentAlphaPercent));
// 5. 亮度控件显示默认100%,初始化按钮状态)
tvBrightnessValue.setText(String.format(FORMAT_PERCENT, mCurrentBrightnessPercent));
updateBrightnessBtnStatus(); // 禁用边界值按钮
LogUtils.d(TAG, String.format("init data complete | 原始透明度:%s",
String.format(FORMAT_PERCENT, mOriginalAlphaPercent)));
}
/**
* 监听初始化
*/
private void initListener() {
// 点击监听(按钮+颜色拾取框)
ivColorPicker.setOnClickListener(this);
ivColorScaler.setOnClickListener(this);
tvConfirm.setOnClickListener(this);
tvCancel.setOnClickListener(this);
tvBrightnessMinus.setOnClickListener(this);
tvBrightnessPlus.setOnClickListener(this);
// 透明度进度条监听
sbAlpha.setOnSeekBarChangeListener(this);
// 输入框监听RGB+颜色值,避免循环同步)
initTextWatcherListener();
LogUtils.d(TAG, "all listener init complete | 监听绑定成功");
}
/**
* 对话框尺寸适配(小米全面屏+软键盘优化,避免输入框被遮挡)
*/
private void adjustDialogSize() {
Window window = getWindow();
if (window != null) {
WindowManager.LayoutParams lp = window.getAttributes();
// 宽度占屏幕80%,高度自适应(适配不同屏幕尺寸)
lp.width = (int) (getContext().getResources().getDisplayMetrics().widthPixels * 0.8);
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
// 软键盘适配:小米虚拟导航栏兼容
window.setAttributes(lp);
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN
| WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
LogUtils.d(TAG, "dialog size adjust complete | 适配全面屏+软键盘");
}
}
// ====================== 监听子方法(细分类型,逻辑清晰) ======================
/**
* 输入框文本监听RGB+颜色值传入触发ID避免循环同步
*/
private void initTextWatcherListener() {
// RGB输入框监听复用方法减少冗余
setEditTextWatcher(etR, R.id.et_r);
setEditTextWatcher(etG, R.id.et_g);
setEditTextWatcher(etB, R.id.et_b);
// 颜色值输入框监听(支持#RRGGBB/#AARRGGBB格式
etColorValue.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
// 关键:判断非应用自身更新,才执行解析(避免循环回调)
if (!isAppSelfUpdatingColor) {
parseColorFromStr(s.toString().trim(), R.id.et_color_value);
}
}
});
}
// ====================== 透明度进度条监听实现 ======================
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// 仅处理用户手动拖动进度条(避免应用自身更新时触发)
if (fromUser && !isAppSelfUpdatingColor) {
updateAlphaBySeekBar(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
/**
* 拖动透明度进度条更新颜色
*/
private synchronized void updateAlphaBySeekBar(int alphaPercent) {
if (!isAppSelfUpdatingColor) {
isAppSelfUpdatingColor = true; // 标记为应用自身更新
try {
// 更新实时透明度(百分比+0-255值
mCurrentAlphaPercent = alphaPercent;
mCurrentAlpha = percent2Alpha(alphaPercent);
// 重新计算最终颜色(基于当前亮度+新透明度)
calculateBrightnessAndUpdate();
// 同步所有控件
updateAllViews();
LogUtils.d(TAG, String.format("update alpha by seekbar | 透明度:%s",
String.format(FORMAT_PERCENT, mCurrentAlphaPercent)));
} finally {
isAppSelfUpdatingColor = false; // 释放标记
}
}
}
// ====================== 颜色核心逻辑 ======================
/**
* 核心计算基于原始RGB+当前亮度+当前透明度计算实时RGB+最终颜色
*/
private void calculateBrightnessAndUpdate() {
// 亮度百分比转调节系数10%→0.1100%→1.0200%→2.0
float brightnessFactor = mCurrentBrightnessPercent / 100.0f;
// RGB三个分量同时调节基于原始基准值避免叠加失真限制0-255
mCurrentR = Math.min(Math.max(Math.round(mOriginalR * brightnessFactor), 0), MAX_RGB_VALUE);
mCurrentG = Math.min(Math.max(Math.round(mOriginalG * brightnessFactor), 0), MAX_RGB_VALUE);
mCurrentB = Math.min(Math.max(Math.round(mOriginalB * brightnessFactor), 0), MAX_RGB_VALUE);
// 拼接「实时透明度」+「实时RGB」得到最终颜色0xAARRGGBB
mCurrentColor = Color.argb(mCurrentAlpha, mCurrentR, mCurrentG, mCurrentB);
}
/**
* 亮度减少每次减5%最低10%
*/
private void decreaseBrightness() {
changeBrightness(false);
}
/**
* 亮度增加每次加5%最高200%
*/
private void increaseBrightness() {
changeBrightness(true);
}
/**
* 亮度调节核心方法(统一逻辑,加并发控制)
*/
private synchronized void changeBrightness(boolean isIncrease) {
if (!isAppSelfUpdatingColor) {
isAppSelfUpdatingColor = true;
try {
if (isIncrease) {
if (mCurrentBrightnessPercent >= MAX_BRIGHTNESS) return;
mCurrentBrightnessPercent += BRIGHTNESS_STEP;
} else {
if (mCurrentBrightnessPercent <= MIN_BRIGHTNESS) return;
mCurrentBrightnessPercent -= BRIGHTNESS_STEP;
}
// 计算亮度调节后的实时RGB+最终颜色
calculateBrightnessAndUpdate();
// 同步所有控件
updateAllViews();
LogUtils.d(TAG, String.format("%s brightness | 亮度:%s | 实时RGB%d,%d,%d",
isIncrease ? "increase" : "decrease",
String.format(FORMAT_PERCENT, mCurrentBrightnessPercent),
mCurrentR, mCurrentG, mCurrentB));
} finally {
isAppSelfUpdatingColor = false;
}
}
}
/**
* 解析颜色字符串(支持#RRGGBB/#AARRGGBB更新原始基准值+实时值)
*/
private void parseColorFromStr(String colorStr, int triggerViewId) {
if (!isAppSelfUpdatingColor) {
isAppSelfUpdatingColor = true;
try {
if (TextUtils.isEmpty(colorStr)) return;
// 补全#前缀(兼容用户输入习惯)
if (!colorStr.startsWith("#")) {
colorStr = "#" + colorStr;
}
// 格式校验仅支持6位RRGGBB/8位AARRGGBB
if (colorStr.length() != 7 && colorStr.length() != 9) {
LogUtils.e(TAG, String.format("parse color failed | 格式错误(需#RRGGBB/#AARRGGBB输入%s", colorStr));
return;
}
// 解析颜色
int parsedColor = Color.parseColor(colorStr);
// 更新原始基准值与实时值
mOriginalAlpha = Color.alpha(parsedColor);
mOriginalAlphaPercent = alpha2Percent(mOriginalAlpha);
mOriginalR = Color.red(parsedColor);
mOriginalG = Color.green(parsedColor);
mOriginalB = Color.blue(parsedColor);
mCurrentAlpha = mOriginalAlpha;
mCurrentAlphaPercent = mOriginalAlphaPercent;
mCurrentR = mOriginalR;
mCurrentG = mOriginalG;
mCurrentB = mOriginalB;
mCurrentBrightnessPercent = DEFAULT_BRIGHTNESS;
mCurrentColor = parsedColor;
// 同步所有控件
updateAllViews();
LogUtils.d(TAG, String.format("parse color success | 解析颜色:%s | 透明度:%s | 重置亮度:%s",
String.format(FORMAT_COLOR_HEX, parsedColor),
String.format(FORMAT_PERCENT, mCurrentAlphaPercent),
String.format(FORMAT_PERCENT, DEFAULT_BRIGHTNESS)));
} catch (IllegalArgumentException e) {
LogUtils.e(TAG, String.format("parse color failed | 非法颜色格式,输入:%s", colorStr), e);
} finally {
isAppSelfUpdatingColor = false;
}
}
}
/**
* 通过RGB输入框更新颜色用户输入后更新原始基准值+实时值重置亮度为100%
*/
private synchronized void updateColorByRGB(int triggerViewId) {
if (!isAppSelfUpdatingColor) {
isAppSelfUpdatingColor = true;
try {
// 解析用户输入的RGB值限制0-255非法输入设为0
int inputR = parseInputValue(etR.getText().toString());
int inputG = parseInputValue(etG.getText().toString());
int inputB = parseInputValue(etB.getText().toString());
// 更新原始基准值与实时值
mOriginalR = inputR;
mOriginalG = inputG;
mOriginalB = inputB;
mCurrentR = inputR;
mCurrentG = inputG;
mCurrentB = inputB;
mCurrentBrightnessPercent = DEFAULT_BRIGHTNESS;
mCurrentColor = Color.argb(mCurrentAlpha, mCurrentR, mCurrentG, mCurrentB);
// 同步所有控件
updateAllViews();
LogUtils.d(TAG, String.format("update color by RGB | 新原始RGB%d,%d,%d | 透明度:%s | 重置亮度:%s",
mOriginalR, mOriginalG, mOriginalB,
String.format(FORMAT_PERCENT, mCurrentAlphaPercent),
String.format(FORMAT_PERCENT, DEFAULT_BRIGHTNESS)));
} catch (Exception e) {
LogUtils.e(TAG, "update color by RGB failed", e);
} finally {
isAppSelfUpdatingColor = false;
}
}
}
/**
* 核心同步:更新所有控件显示
*/
private void updateAllViews() {
// 1. 同步颜色预览
ivColorPicker.setBackgroundColor(mCurrentColor);
// 2. 同步RGB输入框
etR.setText(String.valueOf(mCurrentR));
etG.setText(String.valueOf(mCurrentG));
etB.setText(String.valueOf(mCurrentB));
// 3. 同步颜色值输入框
etColorValue.setText(String.format(FORMAT_COLOR_HEX, mCurrentColor));
// 4. 同步透明度控件
sbAlpha.setProgress(mCurrentAlphaPercent);
tvAlphaValue.setText(String.format(FORMAT_PERCENT, mCurrentAlphaPercent));
// 5. 同步亮度控件
tvBrightnessValue.setText(String.format(FORMAT_PERCENT, mCurrentBrightnessPercent));
updateBrightnessBtnStatus();
LogUtils.d(TAG, String.format("sync all views complete | 最终颜色:%s | 实时RGB%d,%d,%d | 透明度:%s | 亮度:%s",
String.format(FORMAT_COLOR_HEX, mCurrentColor),
mCurrentR, mCurrentG, mCurrentB,
String.format(FORMAT_PERCENT, mCurrentAlphaPercent),
String.format(FORMAT_PERCENT, mCurrentBrightnessPercent)));
}
/**
* 更新亮度按钮状态(边界值禁用,提升交互体验)
*/
private void updateBrightnessBtnStatus() {
boolean canMinus = mCurrentBrightnessPercent > MIN_BRIGHTNESS;
boolean canPlus = mCurrentBrightnessPercent < MAX_BRIGHTNESS;
tvBrightnessMinus.setEnabled(canMinus);
tvBrightnessPlus.setEnabled(canPlus);
tvBrightnessMinus.setTextColor(canMinus ? Color.BLACK : Color.parseColor("#CCCCCC"));
tvBrightnessPlus.setTextColor(canPlus ? Color.BLACK : Color.parseColor("#CCCCCC"));
}
// ====================== 工具方法 ======================
/**
* 透明度0-255 → 0-100%
*/
private int alpha2Percent(int alpha) {
return Math.round((float) alpha / MAX_RGB_VALUE * MAX_ALPHA_PERCENT);
}
/**
* 透明度0-100% → 0-255
*/
private int percent2Alpha(int percent) {
return Math.round((float) percent / MAX_ALPHA_PERCENT * MAX_RGB_VALUE);
}
/**
* 解析输入值限制0-255非法输入返回0
*/
private int parseInputValue(String input) {
if (TextUtils.isEmpty(input)) return 0;
try {
int value = Integer.parseInt(input);
return Math.min(Math.max(value, 0), MAX_RGB_VALUE);
} catch (NumberFormatException e) {
LogUtils.e(TAG, String.format("parse input failed | 非法数字,输入:%s", input), e);
return 0;
}
}
/**
* RGB输入框监听复用
*/
private void setEditTextWatcher(EditText editText, final int viewId) {
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
if (!isAppSelfUpdatingColor) {
updateColorByRGB(viewId);
}
}
});
}
/**
* dp转px适配小米不同分辨率
*/
private int dp2px(float dp) {
return (int) (dp * getContext().getResources().getDisplayMetrics().density + 0.5f);
}
/**
* 显示系统颜色选择器兼容API29-30无高版本依赖小米机型适配
*/
private void showSystemColorPicker() {
LogUtils.d(TAG, "show system color picker | 兼容小米API29-30支持横向滚动");
final android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(getContext());
builder.setTitle("选择基础颜色");
// 50种常用颜色按彩虹光谱顺序排列
final int[] systemColors = {
0xFFCC0000, 0xFFFF0000, 0xFFFF6666, 0xFFFF1493, 0xFF8B0000, 0xFFFF4500,
0xFFCC6600, 0xFFFF8800, 0xFFFFAA33, 0xFFFFBB00, 0xFFF5A623,
0xFFCCCC00, 0xFFFFFF00, 0xFFFFEE99, 0xFFFFFACD, 0xFFFFD700,
0xFF006600, 0xFF00FF00, 0xFF99FF99, 0xFF66CC66, 0xFF98FB98, 0xFF00FF99, 0xFF003300,
0xFF006666, 0xFF00FFFF, 0xFF99FFFF, 0xFF00CCCC, 0xFF40E0D0,
0xFF0000CC, 0xFF00008B, 0xFF0000FF, 0xFF6666FF, 0xFF87CEEB, 0xFF0066FF, 0xFF0099FF, 0xFF4B0082,
0xFF660099, 0xFF8800FF, 0xFFAA99FF, 0xFF9370DB, 0xFFCBC3E3, 0xFF8A2BE2,
0xFFFF00FF, 0xFFFF99CC, 0xFFFFCCDD, 0xFFFFB6C1, 0xFFFFA5A5,
0xFF8B4513, 0xFFA0522D, 0xFFD2B48C, 0xFFCD853F,
0xFF333333, 0xFF666666, 0xFF888888, 0xFFAAAAAA, 0xFFCCCCCC, 0xFFE6E6E6,
0xFF000000, 0xFFFFFFFF, 0xFFFFFAFA
};
// 1. 第一级:水平滚动容器
HorizontalScrollView horizontalScrollView = new HorizontalScrollView(getContext());
horizontalScrollView.setHorizontalScrollBarEnabled(true);
horizontalScrollView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
horizontalScrollView.setPadding(dp2px(5), dp2px(5), dp2px(5), dp2px(5));
// 2. 第二级:颜色排列容器(横向)
LinearLayout colorLayout = new LinearLayout(getContext());
colorLayout.setOrientation(LinearLayout.HORIZONTAL);
colorLayout.setGravity(Gravity.CENTER_VERTICAL);
colorLayout.setPadding(dp2px(10), dp2px(10), dp2px(10), dp2px(10));
// 3. 循环添加颜色按钮(内置圆形效果)
for (int i = 0; i < systemColors.length; i++) {
final int color = systemColors[i];
ImageView colorBtn = new ImageView(getContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(dp2px(40), dp2px(40));
if (i != systemColors.length - 1) {
lp.setMargins(0, 0, dp2px(10), 0); // 按钮间距
}
colorBtn.setLayoutParams(lp);
// 内置圆形背景(白色边框+圆形形状)
GradientDrawable circleBg = new GradientDrawable();
circleBg.setShape(GradientDrawable.OVAL);
circleBg.setColor(color);
circleBg.setStroke(dp2px(2), Color.WHITE);
colorBtn.setBackground(circleBg);
colorBtn.setClickable(true);
colorBtn.setFocusable(true);
// 点击事件
colorBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isAppSelfUpdatingColor) {
isAppSelfUpdatingColor = true;
try {
mOriginalAlpha = Color.alpha(color);
mOriginalAlphaPercent = alpha2Percent(mOriginalAlpha);
mOriginalR = Color.red(color);
mOriginalG = Color.green(color);
mOriginalB = Color.blue(color);
mCurrentAlpha = mOriginalAlpha;
mCurrentAlphaPercent = mOriginalAlphaPercent;
mCurrentR = mOriginalR;
mCurrentG = mOriginalG;
mCurrentB = mOriginalB;
mCurrentBrightnessPercent = DEFAULT_BRIGHTNESS;
mCurrentColor = color;
updateAllViews();
builder.create().dismiss();
LogUtils.d(TAG, String.format("select system color | 选择颜色:%s | 透明度:%s",
String.format(FORMAT_COLOR_HEX, color),
String.format(FORMAT_PERCENT, mCurrentAlphaPercent)));
} finally {
isAppSelfUpdatingColor = false;
}
}
}
});
colorLayout.addView(colorBtn);
}
// 层级嵌套
horizontalScrollView.addView(colorLayout);
builder.setView(horizontalScrollView).setNegativeButton("关闭", null).show();
}
// ====================== 点击事件实现 ======================
@Override
public void onClick(View v) {
int id = v.getId();
// 所有点击事件均加并发判断
if (!isAppSelfUpdatingColor) {
if (id == R.id.iv_color_picker) {
showSystemColorPicker();
} else if (id == R.id.iv_color_scaler) {
openColorScalerDialog(mCurrentColor);
} else if (id == R.id.tv_confirm) {
mListener.onColorSelected(mCurrentColor);
LogUtils.d(TAG, String.format("confirm color | 回调颜色:%s",
String.format(FORMAT_COLOR_HEX, mCurrentColor)));
dismiss();
} else if (id == R.id.tv_cancel) {
dismiss();
LogUtils.d(TAG, "cancel color | 取消选择,关闭对话框");
} else if (id == R.id.tv_brightness_minus) {
decreaseBrightness();
} else if (id == R.id.tv_brightness_plus) {
increaseBrightness();
}
}
}
/**
* 打开颜色渐变选择器
*/
void openColorScalerDialog(int nColor) {
LogUtils.d(TAG, String.format("openColorScalerDialog | 初始颜色:%s",
String.format(FORMAT_COLOR_HEX, nColor)));
final ColorScalerDialog dlg = new ColorScalerDialog(getContext(), nColor);
dlg.setOnColorChangedListener(new OnColorChangedListener() {
@Override
public void beforeColorChanged() {}
@Override
public void onColorChanged(int color) {
dlg.currentColorScalerDialogColor = color;
}
@Override
public void afterColorChanged() {}
});
dlg.show();
}
// ====================== 内部类 ======================
class ColorScalerDialog extends ColorPickerDialog {
public int currentColorScalerDialogColor = 0;
public ColorScalerDialog(Context context, int p) {
super(context, p);
this.currentColorScalerDialogColor = p;
}
@Override
public void dismiss() {
super.dismiss();
int color = currentColorScalerDialogColor;
ToastUtils.show(String.format("选择颜色:%s", String.format(FORMAT_COLOR_HEX, color)));
if (!isAppSelfUpdatingColor) {
isAppSelfUpdatingColor = true;
try {
mOriginalAlpha = Color.alpha(color);
mOriginalAlphaPercent = alpha2Percent(mOriginalAlpha);
mOriginalR = Color.red(color);
mOriginalG = Color.green(color);
mOriginalB = Color.blue(color);
mCurrentAlpha = mOriginalAlpha;
mCurrentAlphaPercent = mOriginalAlphaPercent;
mCurrentR = mOriginalR;
mCurrentG = mOriginalG;
mCurrentB = mOriginalB;
mCurrentBrightnessPercent = DEFAULT_BRIGHTNESS;
mCurrentColor = color;
updateAllViews();
LogUtils.d(TAG, String.format("select scaler color | 选择颜色:%s | 透明度:%s",
String.format(FORMAT_COLOR_HEX, color),
String.format(FORMAT_PERCENT, mCurrentAlphaPercent)));
} finally {
isAppSelfUpdatingColor = false;
}
}
}
}
}

View File

@@ -1,10 +1,10 @@
package cc.winboll.studio.powerbell.dialogs;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
@@ -15,7 +15,8 @@ import androidx.appcompat.app.AlertDialog;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.ToastUtils;
import cc.winboll.studio.powerbell.R;
import cc.winboll.studio.powerbell.utils.PictureUtils;
import cc.winboll.studio.powerbell.utils.BackgroundSourceUtils;
import cc.winboll.studio.powerbell.utils.ImageDownloader;
import cc.winboll.studio.powerbell.views.BackgroundView;
import java.io.File;
import java.io.FileInputStream;
@@ -24,56 +25,72 @@ import java.io.IOException;
/**
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2025/11/19 20:11
* @Describe 网络后台使用提示对话框
* @Describe 网络背景使用提示对话框
* 继承 AndroidX AlertDialog绑定自定义布局 dialog_networkbackground.xml
* 适配 API30基于 Java7 开发,支持网络图片下载、预览与回调
*/
public class NetworkBackgroundDialog extends AlertDialog {
// ====================== 静态常量(首屏可见,统一管理) ======================
public static final String TAG = "NetworkBackgroundDialog";
// 消息标识:图片加载成功
private static final int MSG_IMAGE_LOAD_SUCCESS = 1001;
// 消息标识:图片加载失败
private static final int MSG_IMAGE_LOAD_FAILED = 1002;
private static final int MSG_IMAGE_LOAD_SUCCESS = 1001; // 图片加载成功消息标识
private static final int MSG_IMAGE_LOAD_FAILED = 1002; // 图片加载失败消息标识
// 控件引用
private TextView tvTitle;
private TextView tvContent;
private Button btnCancel;
private Button btnConfirm;
private Button btnPreview;
private EditText etURL;
BackgroundView bvBackgroundPreview;
Context mContext;
// 主线程 Handler用于接收子线程消息并更新 UI
private Handler mUiHandler;
String previewFilePath;
// 按钮点击回调接口Java7 接口实现)
// ====================== 回调接口(紧跟常量,逻辑关联) ======================
/**
* 按钮点击回调接口Java7 接口实现)
*/
public interface OnDialogClickListener {
void onConfirm(); // 确认按钮点击
void onCancel(); // 取消按钮点击
void onConfirm(String szConfirmFilePath); // 确认按钮点击,返回图片路径
void onCancel(); // 取消按钮点击
}
private OnDialogClickListener listener;
// ====================== 成员变量(按优先级排序:核心数据→控件引用) ======================
// 核心数据
private OnDialogClickListener listener; // 按钮点击回调
private Context mContext; // 上下文对象
private Handler mUiHandler; // 主线程 Handler用于接收子线程消息更新 UI
private String mPreviewFilePath; // 预览图片文件路径
private String mPreviewFileUrl; // 预览图片网络 URL
private String mDownloadSavedPath; // 下载图片保存路径
// 控件引用
private TextView tvTitle; // 对话框标题
private TextView tvContent; // 对话框内容
private Button btnCancel; // 取消按钮
private Button btnConfirm; // 确认按钮
private Button btnPreview; // 预览按钮
private EditText etURL; // URL 输入框
private BackgroundView mBackgroundView; // 背景预览视图
// Java7 显式构造(必须传入 Context
// ====================== 构造方法Java7 显式构造,按参数重载排序) ======================
/**
* 基础构造(仅传入 Context
* @param context 上下文
*/
public NetworkBackgroundDialog(@NonNull Context context) {
super(context);
initHandler(); // 初始化 Handler
initView(); // 初始化布局和控件
setDismissListener(); // 设置对话框消失监听
}
// 带回调的构造(便于外部处理点击事件)
public NetworkBackgroundDialog(@NonNull Context context, OnDialogClickListener listener) {
super(context);
this.listener = listener;
initHandler(); // 初始化 Handler
LogUtils.d(TAG, "NetworkBackgroundDialog: 基础构造初始化");
initHandler();
initView();
setDismissListener(); // 设置对话框消失监听
setDismissListener();
}
/**
* 初始化主线程 Handler用于更新 UI
* 带回调的构造(便于外部处理点击事件)
* @param context 上下文
* @param listener 按钮点击回调
*/
public NetworkBackgroundDialog(@NonNull Context context, OnDialogClickListener listener) {
super(context);
this.listener = listener;
LogUtils.d(TAG, "NetworkBackgroundDialog: 带回调构造初始化");
initHandler();
initView();
setDismissListener();
}
// ====================== 生命周期相关方法对话框消失监听、Handler 初始化) ======================
/**
* 初始化主线程 Handler用于接收子线程消息并更新 UI
*/
private void initHandler() {
mUiHandler = new Handler() {
@@ -82,22 +99,30 @@ public class NetworkBackgroundDialog extends AlertDialog {
super.handleMessage(msg);
// 对话框已消失时,不再处理 UI 消息
if (!isShowing()) {
LogUtils.d(TAG, "handleMessage: 对话框已消失,忽略消息");
return;
}
switch (msg.what) {
case MSG_IMAGE_LOAD_SUCCESS:
// 图片加载成功,获取文件路径并设置背景
String filePath = (String) msg.obj;
setBackgroundFromPath(filePath);
mDownloadSavedPath = (String) msg.obj;
LogUtils.d(TAG, String.format("handleMessage: 图片加载成功,保存路径:%s", mDownloadSavedPath));
int nCurrentPixelColor = BackgroundSourceUtils.getInstance(mContext).getCurrentBackgroundBean().getPixelColor();
mBackgroundView.loadImage(nCurrentPixelColor, mDownloadSavedPath, true);
break;
case MSG_IMAGE_LOAD_FAILED:
// 图片加载失败,设置默认背景
bvBackgroundPreview.setBackgroundResource(R.drawable.ic_launcher);
LogUtils.e(TAG, "handleMessage: 图片加载失败");
mBackgroundView.setBackgroundResource(R.drawable.ic_launcher);
ToastUtils.show("图片预览失败,请检查链接");
break;
default:
break;
}
}
};
LogUtils.d(TAG, "initHandler: 主线程 Handler 初始化完成");
}
/**
@@ -110,20 +135,22 @@ public class NetworkBackgroundDialog extends AlertDialog {
// 对话框消失时,移除所有未处理的消息和回调
if (mUiHandler != null) {
mUiHandler.removeCallbacksAndMessages(null);
LogUtils.d(TAG, "onDismiss: Handler 消息已清理");
}
LogUtils.d(TAG, "对话框已消失Handler 消息已清理");
LogUtils.d(TAG, "onDismiss: 对话框已消失");
}
});
LogUtils.d(TAG, "setDismissListener: 对话框消失监听已设置");
}
// ====================== 初始化方法(布局、控件、点击事件) ======================
/**
* 初始化布局和控件
*/
private void initView() {
mContext = this.getContext();
// 加载自定义布局
View dialogView = LayoutInflater.from(getContext())
.inflate(R.layout.dialog_networkbackground, null);
View dialogView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_networkbackground, null);
// 设置对话框内容视图
setView(dialogView);
@@ -134,10 +161,22 @@ public class NetworkBackgroundDialog extends AlertDialog {
btnConfirm = (Button) dialogView.findViewById(R.id.btn_confirm);
btnPreview = (Button) dialogView.findViewById(R.id.btn_preview);
etURL = (EditText) dialogView.findViewById(R.id.et_url);
bvBackgroundPreview = (BackgroundView) dialogView.findViewById(R.id.bv_background_preview);
mBackgroundView = (BackgroundView) dialogView.findViewById(R.id.bv_background_preview);
// 控件非空校验
if (tvTitle == null || tvContent == null || btnCancel == null || btnConfirm == null || btnPreview == null
|| etURL == null || mBackgroundView == null) {
LogUtils.e(TAG, "initView: 控件绑定失败请检查布局ID是否正确");
dismiss();
return;
}
// 加载初始图片
mBackgroundView.setBackgroundResource(R.drawable.blank100x100);
// 设置按钮点击事件
setButtonClickListeners();
LogUtils.d(TAG, "initView: 布局和控件初始化完成");
}
/**
@@ -148,10 +187,14 @@ public class NetworkBackgroundDialog extends AlertDialog {
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d("NetworkBackgroundDialog", "取消按钮点击");
LogUtils.d(TAG, "onClick: 取消按钮点击");
BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(mContext);
utils.setCurrentSourceToPreview();
dismiss(); // 关闭对话框
if (listener != null) {
listener.onCancel();
LogUtils.d(TAG, "onClick: 取消回调已执行");
}
}
});
@@ -160,13 +203,16 @@ public class NetworkBackgroundDialog extends AlertDialog {
btnConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d("NetworkBackgroundDialog", "确认按钮点击");
// 确定预览背景资源
bvBackgroundPreview.saveToBackgroundSources(previewFilePath);
LogUtils.d(TAG, "onClick: 确认按钮点击");
dismiss(); // 关闭对话框
if (TextUtils.isEmpty(mDownloadSavedPath)) {
ToastUtils.show("未下载图片。");
LogUtils.w(TAG, "onClick: 确认失败,未下载图片");
return;
}
if (listener != null) {
listener.onConfirm();
listener.onConfirm(mDownloadSavedPath);
LogUtils.d(TAG, String.format("onClick: 确认回调已执行,图片路径:%s", mDownloadSavedPath));
}
}
});
@@ -175,117 +221,120 @@ public class NetworkBackgroundDialog extends AlertDialog {
btnPreview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d("NetworkBackgroundDialog", "确认预览点击");
LogUtils.d(TAG, "onClick: 预览按钮点击");
downloadImageToAlbumAndPreview();
/*String url = etURL.getText().toString().trim();
if (url.isEmpty()) {
ToastUtils.show("请输入图片链接");
return;
}
ImageDownloader.getInstance(mContext).downloadImage(url, mDownloadCallback);*/
}
});
LogUtils.d(TAG, "setButtonClickListeners: 按钮点击监听已设置");
}
// ====================== 业务逻辑方法(图片下载、预览) ======================
/**
* 根据文件路径设置 BackgroundView 背景(主线程调用)
* @param filePath 图片文件路径
* 下载网络图片并预览
*/
private void setBackgroundFromPath(String filePath) {
FileInputStream fis = null;
try {
File imageFile = new File(filePath);
if (!imageFile.exists()) {
LogUtils.e(TAG, "图片文件不存在:" + filePath);
ToastUtils.show("Test");
//bvBackgroundPreview.setBackgroundResource(R.drawable.ic_launcher);
return;
}
// 预览背景
previewFilePath = filePath;
bvBackgroundPreview.previewBackgroundImage(previewFilePath);
LogUtils.d(TAG, "图片预览成功:" + filePath);
} catch (Exception e) {
e.printStackTrace();
bvBackgroundPreview.setBackgroundResource(R.drawable.ic_launcher);
LogUtils.e(TAG, "图片预览失败:" + e.getMessage());
} finally {
// Java7 手动关闭流,避免资源泄漏
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
void downloadImageToAlbumAndPreview() {
mPreviewFileUrl = etURL.getText().toString().trim();
if (TextUtils.isEmpty(mPreviewFileUrl)) {
ToastUtils.show("请输入图片URL");
LogUtils.w(TAG, "downloadImageToAlbumAndPreview: 图片URL为空");
return;
}
}
/**
* 对外提供方法:修改对话框标题(灵活适配不同场景)
*/
public void setTitle(String title) {
if (tvTitle != null) {
tvTitle.setText(title);
}
}
/**
* 对外提供方法:修改对话框内容(灵活适配不同场景)
*/
public void setContent(String content) {
if (tvContent != null) {
tvContent.setText(content);
}
}
/**
* 对外提供方法:设置按钮点击回调(替代带参构造)
*/
public void setOnDialogClickListener(OnDialogClickListener listener) {
this.listener = listener;
}
/*ImageDownloader.DownloadCallback mDownloadCallback = new ImageDownloader.DownloadCallback() {
@Override
public void onSuccess(String filePath) {
ToastUtils.show("图片下载成功:" + filePath);
LogUtils.d(TAG, filePath);
// 发送消息到主线程,携带图片路径
Message successMsg = mUiHandler.obtainMessage(MSG_IMAGE_LOAD_SUCCESS, filePath);
mUiHandler.sendMessage(successMsg);
}
@Override
public void onFailure(String errorMsg) {
ToastUtils.show("下载失败:" + errorMsg);
LogUtils.e(TAG, errorMsg);
// 发送图片加载失败消息
mUiHandler.sendEmptyMessage(MSG_IMAGE_LOAD_FAILED);
}
};*/
void downloadImageToAlbumAndPreview() {
//String imgUrl = "https://example.com/test.jpg";
String imgUrl = etURL.getText().toString();
PictureUtils.downloadImageToAlbum(mContext, imgUrl, new PictureUtils.DownloadCallback(){
LogUtils.d(TAG, String.format("downloadImageToAlbumAndPreview: 开始下载图片URL%s", mPreviewFileUrl));
ImageDownloader.getInstance(mContext).downloadImage(mPreviewFileUrl, new ImageDownloader.DownloadCallback() {
@Override
public void onSuccess(String savePath) {
ToastUtils.show("下载成功:" + savePath);
LogUtils.d(TAG, String.format("onSuccess: 图片下载成功,保存路径:%s", savePath));
// 发送消息到主线程,携带图片路径
Message successMsg = mUiHandler.obtainMessage(MSG_IMAGE_LOAD_SUCCESS, savePath);
mUiHandler.sendMessage(successMsg);
}
@Override
public void onFailure(Exception e) {
ToastUtils.show("下载失败:" + e.getMessage());
public void onFailure(String errorMsg) {
LogUtils.e(TAG, String.format("onFailure: 图片下载失败,错误信息:%s", errorMsg));
ToastUtils.show("下载失败:" + errorMsg);
// 发送图片加载失败消息
Message failMsg = mUiHandler.obtainMessage(MSG_IMAGE_LOAD_FAILED);
mUiHandler.sendMessage(failMsg);
}
});
}
}
/**
* 根据文件路径设置 BackgroundView 背景(主线程调用)
* @param previewFilePath 图片文件路径
*/
private void previewBackground(String previewFilePath) {
if (TextUtils.isEmpty(previewFilePath)) {
LogUtils.w(TAG, "previewBackground: 预览文件路径为空");
return;
}
FileInputStream fis = null;
try {
File imageFile = new File(previewFilePath);
if (!imageFile.exists()) {
ToastUtils.show("图片文件不存在:" + previewFilePath);
LogUtils.e(TAG, String.format("previewBackground: 图片文件不存在,路径:%s", previewFilePath));
mBackgroundView.setBackgroundResource(R.drawable.ic_launcher);
return;
}
// 预览背景
mPreviewFilePath = previewFilePath;
BackgroundSourceUtils utils = BackgroundSourceUtils.getInstance(mContext);
utils.saveFileToPreviewBean(new File(mPreviewFilePath), mPreviewFileUrl);
mBackgroundView.loadByBackgroundBean(utils.getPreviewBackgroundBean());
LogUtils.d(TAG, String.format("previewBackground: 图片预览成功,路径:%s", previewFilePath));
} catch (Exception e) {
LogUtils.e(TAG, String.format("previewBackground: 图片预览失败,错误信息:%s", e.getMessage()), e);
mBackgroundView.setBackgroundResource(R.drawable.ic_launcher);
} finally {
// Java7 手动关闭流,避免资源泄漏
if (fis != null) {
try {
fis.close();
LogUtils.d(TAG, "previewBackground: 文件输入流已关闭");
} catch (IOException e) {
LogUtils.e(TAG, String.format("previewBackground: 关闭文件输入流失败,错误信息:%s", e.getMessage()), e);
}
}
}
}
// ====================== 对外提供方法(灵活适配不同场景) ======================
/**
* 对外提供方法:修改对话框标题
* @param title 标题文本
*/
public void setTitle(String title) {
if (tvTitle != null && !TextUtils.isEmpty(title)) {
tvTitle.setText(title);
LogUtils.d(TAG, String.format("setTitle: 对话框标题已修改为:%s", title));
}
}
/**
* 对外提供方法:修改对话框内容
* @param content 内容文本
*/
public void setContent(String content) {
if (tvContent != null && !TextUtils.isEmpty(content)) {
tvContent.setText(content);
LogUtils.d(TAG, String.format("setContent: 对话框内容已修改为:%s", content));
}
}
/**
* 对外提供方法:设置按钮点击回调(替代带参构造)
* @param listener 按钮点击回调
*/
public void setOnDialogClickListener(OnDialogClickListener listener) {
this.listener = listener;
LogUtils.d(TAG, "setOnDialogClickListener: 按钮点击回调已设置");
}
}

View File

@@ -1,59 +0,0 @@
package cc.winboll.studio.powerbell.dialogs;
/**
* @Author ZhanGSKen<zhangsken@qq.com>
* @Date 2024/06/10 19:32:55
* @Describe 用户确定与否选择框
*/
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
public class YesNoAlertDialog {
public static final String TAG = "YesNoAlertDialog";
public static void show(Context context, String szTitle, String szMessage, final OnDialogResultListener listener) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle(szTitle);
// set dialog message
alertDialogBuilder
.setMessage(szMessage)
.setCancelable(true)
.setOnCancelListener(new DialogInterface.OnCancelListener(){
@Override
public void onCancel(DialogInterface dialog) {
listener.onNo();
}
})
.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
listener.onYes();
}
})
.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
public interface OnDialogResultListener {
abstract void onYes();
abstract void onNo();
}
}

View File

@@ -1,359 +0,0 @@
package cc.winboll.studio.powerbell.fragments;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.powerbell.App;
import cc.winboll.studio.powerbell.R;
import cc.winboll.studio.powerbell.services.ControlCenterService;
import cc.winboll.studio.powerbell.utils.AppConfigUtils;
import cc.winboll.studio.powerbell.utils.ServiceUtils;
import cc.winboll.studio.powerbell.views.BackgroundView;
import cc.winboll.studio.powerbell.views.BatteryDrawable;
import cc.winboll.studio.powerbell.views.VerticalSeekBar;
public class MainViewFragment extends Fragment {
public static final String TAG = "MainViewFragment";
public static final int MSG_RELOAD_APPCONFIG = 0;
public static final int MSG_CURRENTVALUEBATTERY = 1;
static MainViewFragment _mMainViewFragment;
AppConfigUtils mAppConfigUtils;
View mView;
Drawable mDrawableFrame;
LinearLayout mllLeftSeekBar;
LinearLayout mllRightSeekBar;
CheckBox mcbIsEnableChargeReminder;
CheckBox mcbIsEnableUsegeReminder;
Switch mswIsEnableService;
TextView mtvTips;
// 背景布局
//LinearLayout mLinearLayoutloadBackground;
// 现在电量图示
BatteryDrawable mCurrentValueBatteryDrawable;
// 现在充电提醒电量图示
BatteryDrawable mChargeReminderValueBatteryDrawable;
// 现在耗电提醒电量图示
BatteryDrawable mUsegeReminderValueBatteryDrawable;
ImageView mCurrentValueBatteryImageView;
ImageView mChargeReminderValueBatteryImageView;
ImageView mUsegeReminderValueBatteryImageView;
VerticalSeekBar mChargeReminderSeekBar;
ChargeReminderSeekBarChangeListener mChargeReminderSeekBarChangeListener;
TextView mtvChargeReminderValue;
VerticalSeekBar mUsegeReminderSeekBar;
UsegeReminderSeekBarChangeListener mUsegeReminderSeekBarChangeListener;
TextView mtvUsegeReminderValue;
CheckBox mcbUsegeReminderValue;
TextView mtvCurrentValue;
BackgroundView bvPreviewBackground;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_mainview, container, false);
_mMainViewFragment = MainViewFragment.this;
mAppConfigUtils = App.getAppConfigUtils(getActivity());
// 获取指定ID的View实例
bvPreviewBackground = mView.findViewById(R.id.fragmentmainviewBackgroundView1);
/*final View mainImageView = mView.findViewById(R.id.fragmentmainviewImageView1);
// 注册OnGlobalLayoutListener
mainImageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// 获取宽度和高度
int width = mainImageView.getMeasuredWidth();
int height = mainImageView.getMeasuredHeight();
BackgroundPictureUtils utils = BackgroundPictureUtils.getInstance(getActivity());
BackgroundPictureBean bean = utils.loadBackgroundPictureBean();
bean.setBackgroundWidth(width);
bean.setBackgroundHeight(height);
utils.saveData();
// 移除监听器以避免内存泄漏
mainImageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});*/
mDrawableFrame = getActivity().getDrawable(R.drawable.bg_frame);
mllLeftSeekBar = (LinearLayout) mView.findViewById(R.id.fragmentmainviewLinearLayout1);
mllRightSeekBar = (LinearLayout) mView.findViewById(R.id.fragmentmainviewLinearLayout2);
// 初始化充电电量提醒设置控件
mtvChargeReminderValue = (TextView) mView.findViewById(R.id.fragmentandroidviewTextView2);
mChargeReminderSeekBar = (VerticalSeekBar) mView.findViewById(R.id.fragmentandroidviewVerticalSeekBar1);
mcbIsEnableChargeReminder = mView.findViewById(R.id.fragmentmainviewCheckBox1);
// 初始化耗电电量提醒设置控件
mtvUsegeReminderValue = (TextView) mView.findViewById(R.id.fragmentandroidviewTextView3);
mUsegeReminderSeekBar = (VerticalSeekBar) mView.findViewById(R.id.fragmentandroidviewVerticalSeekBar2);
mcbIsEnableUsegeReminder = mView.findViewById(R.id.fragmentmainviewCheckBox2);
// 初始化现在电量显示控件
mtvCurrentValue = (TextView) mView.findViewById(R.id.fragmentandroidviewTextView4);
// 初始化服务总开关
mswIsEnableService = (Switch) mView.findViewById(R.id.fragmentandroidviewSwitch1);
mtvTips = mView.findViewById(R.id.fragmentandroidviewTextView1);
// 设置视图显示数据
setViewData();
// 设置视图控件响应
setViewListener();
// 注册一个广播接收
//mMainActivityReceiver = new MainActivityReceiver(this);
//mMainActivityReceiver.registerAction();
// 启动的时候检查一下服务
if (mAppConfigUtils.getIsEnableService()
&& ServiceUtils.isServiceAlive(getActivity(), ControlCenterService.class.getName()) == false) {
// 如果配置了服务启动,服务没有启动
// 就启动服务
Intent intent = new Intent(getActivity(), ControlCenterService.class);
getActivity().startForegroundService(intent);
}
return mView;
}
void setViewData() {
int nChargeReminderValue = mAppConfigUtils.getChargeReminderValue();
int nUsegeReminderValue = mAppConfigUtils.getUsegeReminderValue();
int nCurrentValue = mAppConfigUtils.getCurrentValue();
mllLeftSeekBar.setBackground(mDrawableFrame);
mllRightSeekBar.setBackground(mDrawableFrame);
// 初始化电量图
mCurrentValueBatteryDrawable = new BatteryDrawable(getActivity().getColor(R.color.colorCurrent));
mCurrentValueBatteryDrawable.setValue(mAppConfigUtils.getCurrentValue());
mCurrentValueBatteryImageView = mView.findViewById(R.id.fragmentandroidviewImageView1);
mCurrentValueBatteryImageView.setImageDrawable(mCurrentValueBatteryDrawable);
// 初始化充电电量提醒图
mChargeReminderValueBatteryDrawable = new BatteryDrawable(getActivity().getColor(R.color.colorCharge));
mChargeReminderValueBatteryDrawable.setValue(nChargeReminderValue);
mChargeReminderValueBatteryImageView = mView.findViewById(R.id.fragmentandroidviewImageView3);
mChargeReminderValueBatteryImageView.setImageDrawable(mChargeReminderValueBatteryDrawable);
// 初始化耗电电量提醒图
mUsegeReminderValueBatteryDrawable = new BatteryDrawable(getActivity().getColor(R.color.colorUsege));
mUsegeReminderValueBatteryDrawable.setValue(nUsegeReminderValue);
mUsegeReminderValueBatteryImageView = mView.findViewById(R.id.fragmentandroidviewImageView2);
mUsegeReminderValueBatteryImageView.setImageDrawable(mUsegeReminderValueBatteryDrawable);
// 初始化充电电量提醒设置控件
mtvChargeReminderValue.setTextColor(getActivity().getColor(R.color.colorCharge));
//LogUtils.d(TAG, "Color.YELLOW is " + Integer.toString(mApplication.getColor(R.color.colorCharge)));
mtvChargeReminderValue.setText(Integer.toString(nChargeReminderValue) + "%");
mChargeReminderSeekBar.setProgress(nChargeReminderValue);
mcbIsEnableChargeReminder.setChecked(mAppConfigUtils.getIsEnableChargeReminder());
// 初始化耗电电量提醒设置控件
mtvUsegeReminderValue.setTextColor(getActivity().getColor(R.color.colorUsege));
mtvUsegeReminderValue.setText(Integer.toString(nUsegeReminderValue) + "%");
mUsegeReminderSeekBar.setProgress(nUsegeReminderValue);
mcbIsEnableUsegeReminder.setChecked(mAppConfigUtils.getIsEnableUsegeReminder());
// 初始化现在电量显示控件
mtvCurrentValue.setTextColor(getActivity().getColor(R.color.colorCurrent));
mtvCurrentValue.setText(Integer.toString(nCurrentValue) + "%");
// 初始化服务总开关
mswIsEnableService.setChecked(mAppConfigUtils.getIsEnableService());
if (mAppConfigUtils.getIsEnableService()) {
//LogUtils.d(TAG, "mApplication.getIsEnableService() " + Boolean.toString(mAppConfigUtils.getIsEnableService()));
ControlCenterService.startControlCenterService(getActivity());
} else {
//LogUtils.d(TAG, "mApplication.getIsEnableService() " + Boolean.toString(mAppConfigUtils.getIsEnableService()));
ControlCenterService.stopControlCenterService(getActivity());
}
mswIsEnableService.setText(getString(R.string.txt_aboveswitch));
mtvTips.setText(getString(R.string.txt_aboveswitchtips));
}
void setViewListener() {
// 初始化充电电量提醒设置控件
mChargeReminderSeekBarChangeListener = new ChargeReminderSeekBarChangeListener();
mChargeReminderSeekBar.setOnSeekBarChangeListener(mChargeReminderSeekBarChangeListener);
mcbIsEnableChargeReminder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "setIsEnableChargeReminder");
mAppConfigUtils.setIsEnableChargeReminder(mcbIsEnableChargeReminder.isChecked());
//ControlCenterService.updateIsEnableChargeReminder(mcbIsEnableChargeReminder.isChecked());
}
});
// 初始化耗电电量提醒设置控件
mUsegeReminderSeekBarChangeListener = new UsegeReminderSeekBarChangeListener();
mUsegeReminderSeekBar.setOnSeekBarChangeListener(mUsegeReminderSeekBarChangeListener);
mcbIsEnableUsegeReminder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "setIsEnableUsegeReminder");
mAppConfigUtils.setIsEnableUsegeReminder(mcbIsEnableUsegeReminder.isChecked());
//ControlCenterService.updateIsEnableUsegeReminder(mcbIsEnableUsegeReminder.isChecked());
}
});
// 初始化服务总开关
mswIsEnableService.setOnClickListener(new CompoundButton.OnClickListener() {
@Override
public void onClick(View view) {
mAppConfigUtils.setIsEnableService(getActivity(), mswIsEnableService.isChecked());
}
});
}
void setCurrentValueBattery(int value) {
//LogUtils.d(TAG, "setCurrentValueBattery");
mtvCurrentValue.setText(Integer.toString(value) + "%");
mCurrentValueBatteryDrawable.setValue(value);
mCurrentValueBatteryDrawable.invalidateSelf();
}
class ChargeReminderSeekBarChangeListener implements SeekBar.OnSeekBarChangeListener {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//LogUtils.d(TAG, "call onProgressChanged");
int nChargeReminderValue = progress;
mtvChargeReminderValue.setText(Integer.toString(nChargeReminderValue) + "%");
mChargeReminderValueBatteryDrawable.setValue(nChargeReminderValue);
mChargeReminderValueBatteryDrawable.invalidateSelf();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
//LogUtils.d(TAG, "call onStartTrackingTouch");
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//LogUtils.d(TAG, "call onStopTrackingTouch");
//取得当前进度条的刻度
int nChargeReminderValue = ((VerticalSeekBar)seekBar)._mnProgress;
mAppConfigUtils.setChargeReminderValue(nChargeReminderValue);
mtvChargeReminderValue.setText(Integer.toString(nChargeReminderValue) + "%");
//ControlCenterService.updateChargeReminderValue(nChargeReminderValue);
}
}
class UsegeReminderSeekBarChangeListener implements SeekBar.OnSeekBarChangeListener {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//LogUtils.d(TAG, "call onProgressChanged");
int nUsegeReminderValue = progress;
mtvUsegeReminderValue.setText(Integer.toString(nUsegeReminderValue) + "%");
mUsegeReminderValueBatteryDrawable.setValue(nUsegeReminderValue);
mUsegeReminderValueBatteryDrawable.invalidateSelf();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
//LogUtils.d(TAG, "call onStartTrackingTouch");
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//LogUtils.d(TAG, "call onStopTrackingTouch");
//取得当前进度条的刻度
int nUsegeReminderValue = ((VerticalSeekBar)seekBar)._mnProgress;
LogUtils.d(TAG, "nUsegeReminderValue is " + Integer.toString(nUsegeReminderValue));
//LogUtils.d(TAG, "mPowerReminder is " + mApplication);
mAppConfigUtils.setUsegeReminderValue(nUsegeReminderValue);
//LogUtils.d(TAG, "opopopopopopopop");
mtvUsegeReminderValue.setText(Integer.toString(nUsegeReminderValue) + "%");
//ControlCenterService.updateUsegeReminderValue(nUsegeReminderValue);
}
}
public void reloadBackground() {
bvPreviewBackground.reloadBackgroundImage();
// BackgroundPictureBean bean = BackgroundPictureUtils.getInstance(getActivity()).getBackgroundPictureBean();
// ImageView imageView = mView.findViewById(R.id.fragmentmainviewImageView1);
// String szBackgroundFilePath = BackgroundPictureUtils.getInstance(getActivity()).getBackgroundDir() + BackgroundPictureActivity.getBackgroundFileName();
// File fBackgroundFilePath = new File(szBackgroundFilePath);
// LogUtils.d(TAG, "szBackgroundFilePath : " + szBackgroundFilePath);
// LogUtils.d(TAG, String.format("fBackgroundFilePath.exists() %s", fBackgroundFilePath.exists()));
// if (bean.isUseBackgroundFile() && fBackgroundFilePath.exists()) {
// Drawable drawableBackground = Drawable.createFromPath(szBackgroundFilePath);
// //drawableBackground.setAlpha(120);
// imageView.setImageDrawable(drawableBackground);
// } else {
// Drawable drawableBackground = getActivity().getDrawable(R.drawable.blank10x10);
// //drawableBackground.setAlpha(120);
// imageView.setImageDrawable(drawableBackground);
// }
}
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_RELOAD_APPCONFIG : {
setViewData();
break;
}
case MSG_CURRENTVALUEBATTERY : {
setCurrentValueBattery(msg.arg1);
break;
}
}
super.handleMessage(msg);
}
};
public static void relaodAppConfigs() {
if (_mMainViewFragment != null) {
Handler handler = _mMainViewFragment.mHandler;
handler.sendMessage(handler.obtainMessage(MSG_RELOAD_APPCONFIG));
}
}
public static void sendMsgCurrentValueBattery(int value) {
if (_mMainViewFragment != null) {
Handler handler = _mMainViewFragment.mHandler;
Message msg = handler.obtainMessage(MSG_CURRENTVALUEBATTERY);
msg.arg1 = value;
handler.sendMessage(msg);
}
}
}

View File

@@ -2,35 +2,119 @@ package cc.winboll.studio.powerbell.handlers;
import android.os.Handler;
import android.os.Message;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.powerbell.models.NotificationMessage;
import cc.winboll.studio.powerbell.services.ControlCenterService;
import java.lang.ref.WeakReference;
/**
* 服务通信Handler
* 功能:处理电量提醒消息,构建并发送标准化通知
* 特性:弱引用防泄漏、参数严格校验、通知格式统一
* 适配Java7 | API30 | 小米手机
*/
public class ControlCenterServiceHandler extends Handler {
public static final String TAG = ControlCenterServiceHandler.class.getSimpleName();
// ================================== 静态常量区(置顶归类,消除魔法值)=================================
public static final String TAG = "ControlCenterServiceHandler";
public static final int MSG_REMIND_TEXT = 1001; // 电量提醒消息标识
public static final int MSG_REMIND_TEXT = 0;
// 提醒类型常量
private static final String REMIND_TYPE_CHARGE = "+";
private static final String REMIND_TYPE_USAGE = "-";
WeakReference<ControlCenterService> serviceWeakReference;
// 电量范围常量
private static final int BATTERY_LEVEL_MIN = 0;
private static final int BATTERY_LEVEL_MAX = 100;
// 通知文案常量(抽离魔法值,便于统一修改)
private static final String CHARGE_REMIND_TITLE = "充电提醒";
private static final String USAGE_REMIND_TITLE = "耗电提醒";
private static final String CHARGE_REMIND_CONTENT_FORMAT = "(+)电量已达额定值。当前电量%d%%%s。";
private static final String USAGE_REMIND_CONTENT_FORMAT = "(-)电量低于指定值。当前电量%d%%%s。";
private static final String CHARGE_STATE_CHARGING = "充电中";
private static final String CHARGE_STATE_NOT_CHARGING = "未充电";
// ================================== 成员变量区弱引用防泄漏final保证不可变=================================
private final WeakReference<ControlCenterService> mwrControlCenterService;
// ================================== 构造方法(强制传入服务,初始化弱引用)=================================
public ControlCenterServiceHandler(ControlCenterService service) {
serviceWeakReference = new WeakReference<ControlCenterService>(service);
LogUtils.d(TAG, "构造方法执行 | service=" + (service != null ? service.getClass().getSimpleName() : "null"));
this.mwrControlCenterService = new WeakReference<>(service);
}
// ================================== 核心消息处理重写handleMessage解析多参数消息=================================
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
// 解析消息参数obj=提醒类型(+/-)arg1=当前电量arg2=充电状态(1=充电/0=未充电)
String remindType = (msg.obj != null) ? (String) msg.obj : "";
int currentBattery = msg.arg1;
boolean isCharging = msg.arg2 == 1;
LogUtils.d(TAG, "handleMessage: 接收消息 | what=" + msg.what + " | type=" + remindType + " | battery=" + currentBattery + " | isCharging=" + isCharging);
// 弱引用获取服务,避免内存泄漏
ControlCenterService service = mwrControlCenterService.get();
if (service == null) {
LogUtils.e(TAG, "handleMessage: 服务实例已被GC回收终止消息处理");
return;
}
// 按消息类型分发处理
switch (msg.what) {
case MSG_REMIND_TEXT: // 处理下载完成消息更新UI
{
// 显示提醒消息
//
//LogUtils.d(TAG, "显示提醒消息");
ControlCenterService controlCenterService = serviceWeakReference.get();
if (controlCenterService != null) {
//LogUtils.d(TAG, ((NotificationMessage)msg.obj).getTitle());
//LogUtils.d(TAG, ((NotificationMessage)msg.obj).getContent());
controlCenterService.appenRemindMSG((String)msg.obj);
}
break;
}
case MSG_REMIND_TEXT:
handleRemindMessage(service, remindType, currentBattery, isCharging);
break;
default:
LogUtils.w(TAG, "handleMessage: 未知消息类型,忽略处理 | what=" + msg.what);
break;
}
}
// ================================== 业务辅助方法(构建通知并发送,全链路参数校验)=================================
/**
* 处理电量提醒消息,构建带电量+充电状态的通知并发送
* @param service 控制中心服务实例(已校验非空)
* @param remindType 提醒类型(+充电/-耗电)
* @param currentBattery 当前电量0-100
* @param isCharging 充电状态
*/
private void handleRemindMessage(ControlCenterService service, String remindType, int currentBattery, boolean isCharging) {
LogUtils.d(TAG, "handleRemindMessage: 开始处理提醒消息 | type=" + remindType + " | battery=" + currentBattery + " | isCharging=" + isCharging);
// 1. 前置校验:通知工具类+参数有效性
if (service.getNotificationManager() == null) {
LogUtils.e(TAG, "handleRemindMessage: 通知管理工具类未初始化,无法发送提醒");
return;
}
if (!REMIND_TYPE_CHARGE.equals(remindType) && !REMIND_TYPE_USAGE.equals(remindType)) {
LogUtils.w(TAG, "handleRemindMessage: 提醒类型无效,忽略 | type=" + remindType + " | 允许值:" + REMIND_TYPE_CHARGE + "/" + REMIND_TYPE_USAGE);
return;
}
if (currentBattery < BATTERY_LEVEL_MIN || currentBattery > BATTERY_LEVEL_MAX) {
LogUtils.w(TAG, "handleRemindMessage: 电量值超出范围,忽略 | battery=" + currentBattery + " | 允许范围:" + BATTERY_LEVEL_MIN + "-" + BATTERY_LEVEL_MAX);
return;
}
// 2. 构建通知模型,使用统一格式
NotificationMessage remindMsg = new NotificationMessage();
String chargeStateDesc = isCharging ? CHARGE_STATE_CHARGING : CHARGE_STATE_NOT_CHARGING;
if (REMIND_TYPE_CHARGE.equals(remindType)) {
remindMsg.setTitle(CHARGE_REMIND_TITLE);
remindMsg.setContent(String.format(CHARGE_REMIND_CONTENT_FORMAT, currentBattery, chargeStateDesc));
remindMsg.setRemindMSG("charge_remind");
} else {
remindMsg.setTitle(USAGE_REMIND_TITLE);
remindMsg.setContent(String.format(USAGE_REMIND_CONTENT_FORMAT, currentBattery, chargeStateDesc));
remindMsg.setRemindMSG("usage_remind");
}
LogUtils.d(TAG, "handleRemindMessage: 通知模型构建完成 | title=" + remindMsg.getTitle() + " | content=" + remindMsg.getContent());
// 3. 调用通知工具类发送提醒
LogUtils.d(TAG, "handleRemindMessage: 调用通知工具类发送提醒 | remindMSG=" + remindMsg.getRemindMSG());
service.getNotificationManager().showRemindNotification(service, remindMsg);
LogUtils.d(TAG, "handleRemindMessage: 提醒通知发送流程执行完毕");
}
}

View File

@@ -0,0 +1,274 @@
package cc.winboll.studio.powerbell.models;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.JsonReader;
import android.util.JsonWriter;
import cc.winboll.studio.libappbase.BaseBean;
import cc.winboll.studio.libappbase.LogUtils;
import java.io.IOException;
import java.io.Serializable;
/**
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
* @Date 2024/04/29 17:24:53
* @Describe 应用运行参数类
* 适配 API30支持 Serializable 持久化、Parcelable Intent 传递、JSON 序列化/反序列化
* 包含耗电提醒、充电提醒、电量检测、铃声提醒、相框尺寸等核心配置
*/
public class AppConfigBean extends BaseBean implements Serializable, Parcelable {
// ====================== 静态常量区(首屏可见,统一管理) ======================
// 序列化版本号Serializable 必备,避免反序列化失败)
private static final long serialVersionUID = 1L;
// 日志标签(全局统一)
transient public static final String TAG = "AppConfigBean";
// 字段校验常量(统一阈值,避免硬编码)
private static final int MIN_INTERVAL = 500; // 最小检测间隔ms
private static final int MIN_REMIND_INTERVAL = 1000;// 最小提醒间隔ms
private static final int BATTERY_MIN = 0; // 电量最小值
private static final int BATTERY_MAX = 100; // 电量最大值
private static final int INVALID_BATTERY = -1; // 无效电量标识
private static final int DEFAULT_FRAME_WIDTH = 500; // 默认相框宽度px
private static final int DEFAULT_FRAME_HEIGHT = 500;// 默认相框高度px
// ====================== 成员变量区(按功能分类:提醒配置→电量状态→检测配置→相框配置) ======================
// 耗电提醒配置
boolean isEnableUsageReminder = false; // 耗电提醒开关
int usageReminderValue = 45; // 耗电提醒阈值0-100
// 充电提醒配置
boolean isEnableChargeReminder = false;// 充电提醒开关
int chargeReminderValue = 100; // 充电提醒阈值0-100
// 铃声提醒配置
int reminderIntervalTime = 5000; // 铃声提醒间隔ms
// 电量状态
boolean isCharging = false; // 是否充电
// 电量检测配置
int batteryDetectInterval = 2000; // 电量检测间隔ms适配 RemindThread
// 相框配置
int defaultFrameWidth = DEFAULT_FRAME_WIDTH; // 默认相框宽度px
int defaultFrameHeight = DEFAULT_FRAME_HEIGHT;// 默认相框高度px
// ====================== 构造方法(初始化默认配置,强化默认值校验) ======================
public AppConfigBean() {
setChargeReminderValue(100);
setEnableChargeReminder(false);
setUsageReminderValue(10);
setEnableUsageReminder(false);
setReminderIntervalTime(5000);
setBatteryDetectInterval(1000);
setDefaultFrameWidth(DEFAULT_FRAME_WIDTH);
setDefaultFrameHeight(DEFAULT_FRAME_HEIGHT);
LogUtils.d(TAG, "AppConfigBean() 构造器执行 | 默认配置初始化完成");
}
// ====================== 核心业务方法Setter/Getter按字段功能分类补充调试日志 ======================
// --------------- 充电状态相关 ---------------
public void setIsCharging(boolean isCharging) {
this.isCharging = isCharging;
LogUtils.d(TAG, String.format("setIsCharging() 执行 | 充电状态=%b", isCharging));
}
public boolean isCharging() {
return isCharging;
}
// --------------- 耗电提醒配置相关 ---------------
public void setEnableUsageReminder(boolean isEnableUsageReminder) {
this.isEnableUsageReminder = isEnableUsageReminder;
LogUtils.d(TAG, String.format("setEnableUsageReminder() 执行 | 耗电提醒开关=%b", isEnableUsageReminder));
}
public boolean isEnableUsageReminder() {
return isEnableUsageReminder;
}
public void setUsageReminderValue(int usageReminderValue) {
this.usageReminderValue = Math.min(Math.max(usageReminderValue, BATTERY_MIN), BATTERY_MAX);
LogUtils.d(TAG, String.format("setUsageReminderValue() 执行 | 最终阈值=%d | 输入值=%d", this.usageReminderValue, usageReminderValue));
}
public int getUsageReminderValue() {
return usageReminderValue;
}
// --------------- 充电提醒配置相关 ---------------
public void setEnableChargeReminder(boolean isEnableChargeReminder) {
this.isEnableChargeReminder = isEnableChargeReminder;
LogUtils.d(TAG, String.format("setEnableChargeReminder() 执行 | 充电提醒开关=%b", isEnableChargeReminder));
}
public boolean isEnableChargeReminder() {
return isEnableChargeReminder;
}
public void setChargeReminderValue(int chargeReminderValue) {
this.chargeReminderValue = Math.min(Math.max(chargeReminderValue, BATTERY_MIN), BATTERY_MAX);
LogUtils.d(TAG, String.format("setChargeReminderValue() 执行 | 最终阈值=%d | 输入值=%d", this.chargeReminderValue, chargeReminderValue));
}
public int getChargeReminderValue() {
return chargeReminderValue;
}
// --------------- 铃声提醒配置相关 ---------------
public void setReminderIntervalTime(int reminderIntervalTime) {
this.reminderIntervalTime = Math.max(reminderIntervalTime, MIN_REMIND_INTERVAL);
LogUtils.d(TAG, String.format("setReminderIntervalTime() 执行 | 最终间隔=%dms | 输入值=%dms", this.reminderIntervalTime, reminderIntervalTime));
}
public int getReminderIntervalTime() {
return reminderIntervalTime;
}
// --------------- 电量检测配置相关 ---------------
public void setBatteryDetectInterval(int batteryDetectInterval) {
this.batteryDetectInterval = Math.max(batteryDetectInterval, MIN_INTERVAL);
LogUtils.d(TAG, String.format("setBatteryDetectInterval() 执行 | 最终间隔=%dms | 输入值=%dms", this.batteryDetectInterval, batteryDetectInterval));
}
public int getBatteryDetectInterval() {
return batteryDetectInterval;
}
// --------------- 相框配置相关 ---------------
public void setDefaultFrameWidth(int defaultFrameWidth) {
this.defaultFrameWidth = defaultFrameWidth;
LogUtils.d(TAG, String.format("setDefaultFrameWidth() 执行 | 最终宽度=%dpx | 输入值=%dpx", this.defaultFrameWidth, defaultFrameWidth));
}
public int getDefaultFrameWidth() {
return defaultFrameWidth;
}
public void setDefaultFrameHeight(int defaultFrameHeight) {
this.defaultFrameHeight = defaultFrameHeight;
LogUtils.d(TAG, String.format("setDefaultFrameHeight() 执行 | 最终高度=%dpx | 输入值=%dpx", this.defaultFrameHeight, defaultFrameHeight));
}
public int getDefaultFrameHeight() {
return defaultFrameHeight;
}
// ====================== 父类重写方法JSON 序列化/反序列化,兼容旧配置) ======================
@Override
public String getName() {
return AppConfigBean.class.getName();
}
@Override
public void writeThisToJsonWriter(JsonWriter jsonWriter) throws IOException {
super.writeThisToJsonWriter(jsonWriter);
LogUtils.d(TAG, "writeThisToJsonWriter() 执行 | 开始JSON序列化");
// 原有字段序列化
jsonWriter.name("isEnableUsageReminder").value(isEnableUsageReminder());
jsonWriter.name("usageReminderValue").value(getUsageReminderValue());
jsonWriter.name("isEnableChargeReminder").value(isEnableChargeReminder());
jsonWriter.name("chargeReminderValue").value(getChargeReminderValue());
jsonWriter.name("reminderIntervalTime").value(getReminderIntervalTime());
jsonWriter.name("isCharging").value(isCharging());
// 新增字段序列化(检测配置)
jsonWriter.name("batteryDetectInterval").value(getBatteryDetectInterval());
// 新增字段序列化(相框配置)
jsonWriter.name("defaultFrameWidth").value(getDefaultFrameWidth());
jsonWriter.name("defaultFrameHeight").value(getDefaultFrameHeight());
LogUtils.d(TAG, "writeThisToJsonWriter() 完成 | JSON序列化成功");
}
@Override
public BaseBean readBeanFromJsonReader(JsonReader jsonReader) throws IOException {
LogUtils.d(TAG, "readBeanFromJsonReader() 执行 | 开始JSON反序列化");
AppConfigBean bean = new AppConfigBean();
jsonReader.beginObject();
while (jsonReader.hasNext()) {
String name = jsonReader.nextName();
// 兼容拼写错误字段isEnableUsegeReminder → isEnableUsageReminder
if (name.equals("isEnableUsageReminder") || name.equals("isEnableUsegeReminder")) {
bean.setEnableUsageReminder(jsonReader.nextBoolean());
LogUtils.d(TAG, String.format("readBeanFromJsonReader() 读取字段 | %s=%b", name, bean.isEnableUsageReminder()));
} else if (name.equals("usageReminderValue") || name.equals("usegeReminderValue")) {
bean.setUsageReminderValue(jsonReader.nextInt());
LogUtils.d(TAG, String.format("readBeanFromJsonReader() 读取字段 | %s=%d", name, bean.getUsageReminderValue()));
} else if (name.equals("isEnableChargeReminder")) {
bean.setEnableChargeReminder(jsonReader.nextBoolean());
LogUtils.d(TAG, String.format("readBeanFromJsonReader() 读取字段 | %s=%b", name, bean.isEnableChargeReminder()));
} else if (name.equals("chargeReminderValue")) {
bean.setChargeReminderValue(jsonReader.nextInt());
LogUtils.d(TAG, String.format("readBeanFromJsonReader() 读取字段 | %s=%d", name, bean.getChargeReminderValue()));
} else if (name.equals("reminderIntervalTime")) {
bean.setReminderIntervalTime(jsonReader.nextInt());
LogUtils.d(TAG, String.format("readBeanFromJsonReader() 读取字段 | %s=%d", name, bean.getReminderIntervalTime()));
} else if (name.equals("isCharging")) {
bean.setIsCharging(jsonReader.nextBoolean());
LogUtils.d(TAG, String.format("readBeanFromJsonReader() 读取字段 | %s=%b", name, bean.isCharging()));
} else if (name.equals("batteryDetectInterval")) {
bean.setBatteryDetectInterval(jsonReader.nextInt());
LogUtils.d(TAG, String.format("readBeanFromJsonReader() 读取字段 | %s=%d", name, bean.getBatteryDetectInterval()));
} else if (name.equals("defaultFrameWidth")) {
bean.setDefaultFrameWidth(jsonReader.nextInt());
LogUtils.d(TAG, String.format("readBeanFromJsonReader() 读取字段 | %s=%d", name, bean.getDefaultFrameWidth()));
} else if (name.equals("defaultFrameHeight")) {
bean.setDefaultFrameHeight(jsonReader.nextInt());
LogUtils.d(TAG, String.format("readBeanFromJsonReader() 读取字段 | %s=%d", name, bean.getDefaultFrameHeight()));
} else {
jsonReader.skipValue();
LogUtils.w(TAG, String.format("readBeanFromJsonReader() 跳过未知字段 | %s", name));
}
}
jsonReader.endObject();
LogUtils.d(TAG, "readBeanFromJsonReader() 完成 | JSON反序列化成功");
return bean;
}
// ====================== Parcelable 接口实现API30 Intent 传递必备) ======================
@Override
public int describeContents() {
return 0; // 无特殊内容描述固定返回0
}
@Override
public void writeToParcel(Parcel dest, int flags) {
LogUtils.d(TAG, "writeToParcel() 执行 | 开始Parcel序列化");
// 按成员变量顺序写入boolean 转 byte 存储
dest.writeByte((byte) (isEnableUsageReminder ? 1 : 0));
dest.writeInt(usageReminderValue);
dest.writeByte((byte) (isEnableChargeReminder ? 1 : 0));
dest.writeInt(chargeReminderValue);
dest.writeInt(reminderIntervalTime);
dest.writeByte((byte) (isCharging ? 1 : 0));
dest.writeInt(batteryDetectInterval);
dest.writeInt(defaultFrameWidth);
dest.writeInt(defaultFrameHeight);
LogUtils.d(TAG, "writeToParcel() 完成 | Parcel序列化成功");
}
// 反序列化 Creator必须 public static final 修饰Java7 适配)
public static final Parcelable.Creator<AppConfigBean> CREATOR = new Parcelable.Creator<AppConfigBean>() {
@Override
public AppConfigBean createFromParcel(Parcel source) {
LogUtils.d(TAG, "createFromParcel() 执行 | 开始Parcel反序列化");
AppConfigBean bean = new AppConfigBean();
// 按 writeToParcel 顺序读取
bean.isEnableUsageReminder = source.readByte() != 0;
bean.usageReminderValue = source.readInt();
bean.isEnableChargeReminder = source.readByte() != 0;
bean.chargeReminderValue = source.readInt();
bean.reminderIntervalTime = source.readInt();
bean.isCharging = source.readByte() != 0;
bean.batteryDetectInterval = source.readInt();
bean.defaultFrameWidth = source.readInt();
bean.defaultFrameHeight = source.readInt();
LogUtils.d(TAG, "createFromParcel() 完成 | Parcel反序列化成功");
return bean;
}
@Override
public AppConfigBean[] newArray(int size) {
return new AppConfigBean[size];
}
};
}

View File

@@ -0,0 +1,296 @@
package cc.winboll.studio.powerbell.models;
import android.util.JsonReader;
import android.util.JsonWriter;
import cc.winboll.studio.libappbase.BaseBean;
import cc.winboll.studio.libappbase.LogUtils;
import java.io.IOException;
import java.io.Serializable;
/**
* @Author ZhanGSKen<zhangsken@qq.com>
* @Date 2024/07/18 11:52:28
* @Describe 应用背景图片数据类
* 适配 API30支持 Serializable 持久化、JSON 序列化/反序列化
* 存储正式/预览背景配置,包含原图、压缩图、裁剪比例、像素颜色等核心字段
*/
public class BackgroundBean extends BaseBean implements Serializable {
// ====================== 静态常量(首屏可见,统一管理) ======================
// 日志标签(全局统一,替换 Log 为 LogUtils
public static final String TAG = "BackgroundBean";
// 兼容旧字段常量(统一管理,避免硬编码)
private static final String OLD_FIELD_USE_SCALED_COMPRESS = "isUseScaledCompress";
// 字段默认值常量(统一管理,避免魔法值)
private static final int DEFAULT_DIMENSION = 100; // 默认宽高
private static final int MIN_DIMENSION = 1; // 最小宽高
// ====================== 成员变量(按功能分类:原图配置→压缩图配置→控制字段→裁剪配置→像素颜色) ======================
// 原图配置
private String backgroundFileName = ""; // 背景图片文件名
private String backgroundFilePath = ""; // 背景图片完整路径
private String backgroundFileInfo = ""; // 图片信息Uri、网络地址等
// 压缩图配置
private String backgroundScaledCompressFileName = ""; // 压缩后背景图片文件名
private String backgroundScaledCompressFilePath = ""; // 压缩后背景图片完整路径
// 控制字段
private boolean isUseBackgroundFile = false; // 是否启用背景图片
private boolean isUseBackgroundScaledCompressFile = false; // 是否启用压缩背景图重命名原isUseScaledCompress
// 裁剪配置
private int backgroundWidth = DEFAULT_DIMENSION; // 背景图宽度
private int backgroundHeight = DEFAULT_DIMENSION; // 背景图高度
// 像素颜色
private int pixelColor = 0xFFFFFFFF; // 拾取的像素颜色(纯色背景用)
// ====================== 构造方法无参构造JSON反序列化必备 ======================
/**
* 无参构造器必须JSON反序列化时需默认构造器
*/
public BackgroundBean() {
LogUtils.d(TAG, "BackgroundBean: 无参构造初始化完成");
}
// ====================== Getter/Setter 方法(按功能分类,补充调试日志,强化校验) ======================
// --------------- 原图配置相关 ---------------
public String getBackgroundFileName() {
return backgroundFileName;
}
public void setBackgroundFileName(String backgroundFileName) {
this.backgroundFileName = backgroundFileName == null ? "" : backgroundFileName;
LogUtils.d(TAG, String.format("setBackgroundFileName: 背景文件名设置为 %s", this.backgroundFileName));
}
public String getBackgroundFilePath() {
return backgroundFilePath;
}
public void setBackgroundFilePath(String backgroundFilePath) {
this.backgroundFilePath = backgroundFilePath == null ? "" : backgroundFilePath;
LogUtils.d(TAG, String.format("setBackgroundFilePath: 背景文件路径设置为 %s", this.backgroundFilePath));
}
public String getBackgroundFileInfo() {
return backgroundFileInfo;
}
public void setBackgroundFileInfo(String backgroundFileInfo) {
this.backgroundFileInfo = backgroundFileInfo == null ? "" : backgroundFileInfo;
LogUtils.d(TAG, String.format("setBackgroundFileInfo: 背景文件信息设置为 %s", this.backgroundFileInfo));
}
// --------------- 控制字段相关 ---------------
public boolean isUseBackgroundFile() {
return isUseBackgroundFile;
}
public void setIsUseBackgroundFile(boolean isUseBackgroundFile) {
this.isUseBackgroundFile = isUseBackgroundFile;
LogUtils.d(TAG, String.format("setIsUseBackgroundFile: 是否启用背景图设置为 %b", isUseBackgroundFile));
}
// --------------- 压缩图配置相关 ---------------
public String getBackgroundScaledCompressFileName() {
return backgroundScaledCompressFileName;
}
public void setBackgroundScaledCompressFileName(String backgroundScaledCompressFileName) {
this.backgroundScaledCompressFileName = backgroundScaledCompressFileName == null ? "" : backgroundScaledCompressFileName;
LogUtils.d(TAG, String.format("setBackgroundScaledCompressFileName: 压缩背景文件名设置为 %s", this.backgroundScaledCompressFileName));
}
public String getBackgroundScaledCompressFilePath() {
return backgroundScaledCompressFilePath;
}
public void setBackgroundScaledCompressFilePath(String backgroundScaledCompressFilePath) {
this.backgroundScaledCompressFilePath = backgroundScaledCompressFilePath == null ? "" : backgroundScaledCompressFilePath;
LogUtils.d(TAG, String.format("setBackgroundScaledCompressFilePath: 压缩背景文件路径设置为 %s", this.backgroundScaledCompressFilePath));
}
/**
* 重命名原isUseScaledCompress → 新isUseBackgroundScaledCompressFileGetter/Setter同步修改
* 语义:明确表示“是否启用背景压缩图文件”,避免与其他压缩逻辑混淆
*/
public boolean isUseBackgroundScaledCompressFile() {
return isUseBackgroundScaledCompressFile;
}
public void setIsUseBackgroundScaledCompressFile(boolean isUseBackgroundScaledCompressFile) {
this.isUseBackgroundScaledCompressFile = isUseBackgroundScaledCompressFile;
LogUtils.d(TAG, String.format("setIsUseBackgroundScaledCompressFile: 是否启用压缩背景图设置为 %b", isUseBackgroundScaledCompressFile));
}
// --------------- 裁剪配置相关 ---------------
public int getBackgroundWidth() {
return backgroundWidth;
}
public void setBackgroundWidth(int backgroundWidth) {
this.backgroundWidth = backgroundWidth < MIN_DIMENSION ? DEFAULT_DIMENSION : backgroundWidth;
LogUtils.d(TAG, String.format("setBackgroundWidth: 背景宽度设置为 %d输入值%d", this.backgroundWidth, backgroundWidth));
}
public int getBackgroundHeight() {
return backgroundHeight;
}
public void setBackgroundHeight(int backgroundHeight) {
this.backgroundHeight = backgroundHeight < MIN_DIMENSION ? DEFAULT_DIMENSION : backgroundHeight;
LogUtils.d(TAG, String.format("setBackgroundHeight: 背景高度设置为 %d输入值%d", this.backgroundHeight, backgroundHeight));
}
// --------------- 像素颜色相关 ---------------
public int getPixelColor() {
return pixelColor;
}
public void setPixelColor(int pixelColor) {
this.pixelColor = pixelColor;
LogUtils.d(TAG, String.format("setPixelColor: 像素颜色设置为 0x%08X", pixelColor));
}
// ====================== 序列化/反序列化方法(适配重命名字段,兼容旧版本,补充调试日志) ======================
@Override
public String getName() {
String className = BackgroundBean.class.getName();
LogUtils.d(TAG, String.format("getName: 类名标识为 %s", className));
return className;
}
/**
* 序列化同步重命名字段原isUseScaledCompress → 新isUseBackgroundScaledCompressFile
* 确保新字段能正常持久化同时兼容旧版本JSON保留旧字段写入避免旧版本读取异常
*/
@Override
public void writeThisToJsonWriter(JsonWriter jsonWriter) throws IOException {
super.writeThisToJsonWriter(jsonWriter);
BackgroundBean bean = this;
// 原图配置序列化
jsonWriter.name("backgroundFileName").value(bean.getBackgroundFileName());
jsonWriter.name("backgroundFilePath").value(bean.getBackgroundFilePath());
jsonWriter.name("backgroundFileInfo").value(bean.getBackgroundFileInfo());
// 控制字段序列化
jsonWriter.name("isUseBackgroundFile").value(bean.isUseBackgroundFile());
// 压缩图配置序列化
jsonWriter.name("backgroundScaledCompressFileName").value(bean.getBackgroundScaledCompressFileName());
jsonWriter.name("backgroundScaledCompressFilePath").value(bean.getBackgroundScaledCompressFilePath());
// 关键:新字段序列化(核心)
jsonWriter.name("isUseBackgroundScaledCompressFile").value(bean.isUseBackgroundScaledCompressFile());
// 兼容旧版本保留旧字段名写入避免旧版本Bean读取时缺失字段
jsonWriter.name(OLD_FIELD_USE_SCALED_COMPRESS).value(bean.isUseBackgroundScaledCompressFile());
// 裁剪配置与像素颜色序列化
jsonWriter.name("backgroundWidth").value(bean.getBackgroundWidth());
jsonWriter.name("backgroundHeight").value(bean.getBackgroundHeight());
jsonWriter.name("pixelColor").value(bean.getPixelColor());
LogUtils.d(TAG, "writeThisToJsonWriter: JSON 序列化完成,已兼容旧字段");
}
/**
* 反序列化同步处理重命名字段兼容旧版本JSON新旧字段都能读取
* 逻辑:优先读取新字段,若新字段不存在则读取旧字段(确保升级后旧配置仍有效)
*/
@Override
public BaseBean readBeanFromJsonReader(JsonReader jsonReader) throws IOException {
BackgroundBean bean = new BackgroundBean();
jsonReader.beginObject();
// 临时变量:存储旧字段值(用于兼容)
boolean tempUseScaledCompress = false;
while (jsonReader.hasNext()) {
String name = jsonReader.nextName();
switch (name) {
case "backgroundFileName":
bean.setBackgroundFileName(jsonReader.nextString());
break;
case "backgroundFilePath":
bean.setBackgroundFilePath(jsonReader.nextString());
break;
case "backgroundFileInfo":
bean.setBackgroundFileInfo(jsonReader.nextString());
break;
case "isUseBackgroundFile":
bean.setIsUseBackgroundFile(jsonReader.nextBoolean());
break;
case "backgroundScaledCompressFileName":
bean.setBackgroundScaledCompressFileName(jsonReader.nextString());
break;
case "backgroundScaledCompressFilePath":
bean.setBackgroundScaledCompressFilePath(jsonReader.nextString());
break;
case "isUseBackgroundScaledCompressFile":
// 关键:读取新字段(优先)
bean.setIsUseBackgroundScaledCompressFile(jsonReader.nextBoolean());
LogUtils.d(TAG, "readBeanFromJsonReader: 读取新字段 isUseBackgroundScaledCompressFile 完成");
break;
case OLD_FIELD_USE_SCALED_COMPRESS:
// 兼容旧版本:读取旧字段(若新字段未读取,则用旧字段值)
tempUseScaledCompress = jsonReader.nextBoolean();
LogUtils.d(TAG, "readBeanFromJsonReader: 读取旧字段 isUseScaledCompress 完成");
break;
case "backgroundWidth":
bean.setBackgroundWidth(jsonReader.nextInt());
break;
case "backgroundHeight":
bean.setBackgroundHeight(jsonReader.nextInt());
break;
case "pixelColor":
bean.setPixelColor(jsonReader.nextInt());
break;
default:
jsonReader.skipValue();
LogUtils.w(TAG, String.format("readBeanFromJsonReader: 跳过未知字段 %s", name));
break;
}
}
jsonReader.endObject();
// 兼容逻辑若新字段未被赋值旧版本JSON无此字段则用旧字段值填充
if (!bean.isUseBackgroundScaledCompressFile()) {
bean.setIsUseBackgroundScaledCompressFile(tempUseScaledCompress);
LogUtils.d(TAG, "readBeanFromJsonReader: 旧字段值已填充到新字段");
}
LogUtils.d(TAG, "readBeanFromJsonReader: JSON 反序列化完成");
return bean;
}
// ====================== 辅助方法(重置配置、配置校验,补充调试日志) ======================
/**
* 重置背景配置(适配“取消背景”功能,同步重置重命名字段)
*/
public void resetBackgroundConfig() {
this.backgroundFileName = "";
this.backgroundFilePath = "";
this.backgroundScaledCompressFileName = "";
this.backgroundScaledCompressFilePath = "";
this.backgroundFileInfo = "";
this.isUseBackgroundFile = false;
this.isUseBackgroundScaledCompressFile = false;
this.backgroundWidth = DEFAULT_DIMENSION;
this.backgroundHeight = DEFAULT_DIMENSION;
LogUtils.d(TAG, "resetBackgroundConfig: 背景配置已重置为默认值");
}
/**
* 检查背景配置是否有效适配BackgroundSettingsActivity的预览/保存校验)
* 同步使用重命名字段判断压缩图是否启用
* @return true-配置有效可显示背景图false-配置无效
*/
public boolean isBackgroundConfigValid() {
// 启用背景图时,需确保:原图路径/文件名 或 压缩图路径/文件名 非空
if (!isUseBackgroundFile) {
LogUtils.d(TAG, "isBackgroundConfigValid: 未启用背景图,配置无效");
return false;
}
// 原图校验:路径非空 或 文件名非空
boolean isOriginalValid = !backgroundFilePath.isEmpty() || !backgroundFileName.isEmpty();
// 压缩图校验:启用压缩图时,路径/文件名需非空
boolean isCompressValid = true;
if (isUseBackgroundScaledCompressFile()) {
isCompressValid = !backgroundScaledCompressFilePath.isEmpty() || !backgroundScaledCompressFileName.isEmpty();
}
// 逻辑:启用压缩图则需压缩图有效;不启用压缩图则需原图有效
boolean isValid = isUseBackgroundScaledCompressFile() ? isCompressValid : isOriginalValid;
LogUtils.d(TAG, String.format("isBackgroundConfigValid: 背景配置有效性为 %b启用压缩图%b原图有效%b压缩图有效%b",
isValid, isUseBackgroundScaledCompressFile(), isOriginalValid, isCompressValid));
return isValid;
}
}

View File

@@ -0,0 +1,82 @@
package cc.winboll.studio.powerbell.models;
import cc.winboll.studio.libappbase.LogUtils;
/**
* @Author ZhanGSKen<zhangsken@qq.com>
* @Date 2025/03/22 14:30:51
* @Describe 电池报告数据模型
* 适配 API30存储当前电量、放电时间、充电时间核心数据
* 支持参数校验与调试日志输出
*/
public class BatteryData {
// ====================== 静态常量(首屏可见,统一管理) ======================
public static final String TAG = "BatteryData";
// 字段校验常量(避免硬编码,统一管理)
private static final int BATTERY_MIN = 0;
private static final int BATTERY_MAX = 100;
private static final String EMPTY_TIME = "00:00:00";
// ====================== 成员变量(按功能分类:电量→时间) ======================
private int currentLevel; // 当前电池电量0-100
private String dischargeTime; // 放电时间
private String chargeTime; // 充电时间
// ====================== 构造方法(按参数重载排序,补充校验与日志) ======================
/**
* 无参构造器(适配 JSON 反序列化、反射实例化场景)
*/
public BatteryData() {
this.currentLevel = BATTERY_MIN;
this.dischargeTime = EMPTY_TIME;
this.chargeTime = EMPTY_TIME;
LogUtils.d(TAG, "BatteryData: 无参构造初始化完成,默认值已设置");
}
/**
* 带参构造器(核心构造,初始化所有字段)
* @param currentLevel 当前电量0-100
* @param dischargeTime 放电时间
* @param chargeTime 充电时间
*/
public BatteryData(int currentLevel, String dischargeTime, String chargeTime) {
// 电量范围校验0-100异常值置为0
this.currentLevel = currentLevel >= BATTERY_MIN && currentLevel <= BATTERY_MAX
? currentLevel : BATTERY_MIN;
// 时间字段防 null空值置为默认空时间
this.dischargeTime = dischargeTime == null ? EMPTY_TIME : dischargeTime;
this.chargeTime = chargeTime == null ? EMPTY_TIME : chargeTime;
// 调试日志:输出入参与最终赋值结果
LogUtils.d(TAG, String.format("BatteryData: 带参构造初始化完成 | 当前电量:%d输入%d| 放电时间:%s输入%s| 充电时间:%s输入%s",
this.currentLevel, currentLevel,
this.dischargeTime, dischargeTime,
this.chargeTime, chargeTime));
}
// ====================== Getter 方法(按成员变量顺序排列,补充日志可选) ======================
/**
* 获取当前电池电量
* @return 当前电量0-100
*/
public int getCurrentLevel() {
return currentLevel;
}
/**
* 获取放电时间
* @return 放电时间
*/
public String getDischargeTime() {
return dischargeTime;
}
/**
* 获取充电时间
* @return 充电时间
*/
public String getChargeTime() {
return chargeTime;
}
}

View File

@@ -0,0 +1,130 @@
package cc.winboll.studio.powerbell.models;
import android.util.JsonReader;
import android.util.JsonWriter;
import cc.winboll.studio.libappbase.BaseBean;
import cc.winboll.studio.libappbase.LogUtils;
import java.io.IOException;
import java.io.Serializable;
/**
* @Author ZhanGSKen<zhangsken@qq.com>
* @Describe 电池信息数据模型
* 适配 API30存储电量时间戳与电量值支持 JSON 序列化/反序列化
* 修复字段拼写错误,补充数据校验与调试日志
*/
public class BatteryInfoBean extends BaseBean implements Serializable {
// ====================== 静态常量(首屏可见,统一管理) ======================
public static final String TAG = "BatteryInfoBean";
// 字段校验常量(避免硬编码,统一管理)
private static final int BATTERY_MIN = 0;
private static final int BATTERY_MAX = 100;
private static final long DEFAULT_TIMESTAMP = 0L;
private static final int DEFAULT_BATTERY_VALUE = 0;
// ====================== 成员变量修复拼写错误battetyValue → batteryValue ======================
private long timeStamp; // 记录电量的时间戳
private int batteryValue; // 电量值0-100
// ====================== 构造方法(按参数重载排序,补充校验与日志) ======================
/**
* 无参构造器JSON 反序列化、反射实例化必备)
*/
public BatteryInfoBean() {
this.timeStamp = DEFAULT_TIMESTAMP;
this.batteryValue = DEFAULT_BATTERY_VALUE;
LogUtils.d(TAG, "BatteryInfoBean: 无参构造初始化完成,默认时间戳:" + timeStamp + ",默认电量:" + batteryValue);
}
/**
* 带参构造器(核心构造,初始化所有字段)
* @param timeStamp 电量记录时间戳
* @param batteryValue 电量值0-100
*/
public BatteryInfoBean(long timeStamp, int batteryValue) {
this.timeStamp = timeStamp;
// 电量范围校验0-100异常值置为默认值
this.batteryValue = batteryValue >= BATTERY_MIN && batteryValue <= BATTERY_MAX
? batteryValue : DEFAULT_BATTERY_VALUE;
LogUtils.d(TAG, String.format("BatteryInfoBean: 带参构造初始化完成 | 时间戳:%d | 电量:%d输入%d",
this.timeStamp, this.batteryValue, batteryValue));
}
// ====================== Setter/Getter 方法(按成员变量顺序排列,修复拼写错误,补充日志) ======================
/**
* 设置电量记录时间戳
* @param timeStamp 时间戳
*/
public void setTimeStamp(long timeStamp) {
this.timeStamp = timeStamp;
LogUtils.d(TAG, "setTimeStamp: 时间戳设置为 " + timeStamp);
}
public long getTimeStamp() {
return timeStamp;
}
/**
* 设置电量值修复拼写错误battetyValue → batteryValue
* @param batteryValue 电量值0-100
*/
public void setBatteryValue(int batteryValue) {
this.batteryValue = batteryValue >= BATTERY_MIN && batteryValue <= BATTERY_MAX
? batteryValue : DEFAULT_BATTERY_VALUE;
LogUtils.d(TAG, String.format("setBatteryValue: 电量设置为 %d输入%d",
this.batteryValue, batteryValue));
}
public int getBatteryValue() {
return batteryValue;
}
// ====================== JSON 序列化/反序列化方法(修复字段拼写错误,补充调试日志) ======================
@Override
public String getName() {
String className = BatteryInfoBean.class.getName();
LogUtils.d(TAG, "getName: 类名标识为 " + className);
return className;
}
@Override
public void writeThisToJsonWriter(JsonWriter jsonWriter) throws IOException {
super.writeThisToJsonWriter(jsonWriter);
BatteryInfoBean bean = this;
jsonWriter.name("timeStamp").value(bean.getTimeStamp());
// 修复 JSON 字段名拼写错误battetyValue → batteryValue
jsonWriter.name("batteryValue").value(bean.getBatteryValue());
LogUtils.d(TAG, "writeThisToJsonWriter: JSON 序列化完成 | 时间戳:" + bean.getTimeStamp() + ",电量:" + bean.getBatteryValue());
}
@Override
public BaseBean readBeanFromJsonReader(JsonReader jsonReader) throws IOException {
BatteryInfoBean bean = new BatteryInfoBean();
jsonReader.beginObject();
while (jsonReader.hasNext()) {
String name = jsonReader.nextName();
switch (name) {
case "timeStamp":
bean.setTimeStamp(jsonReader.nextLong());
break;
case "batteryValue":
bean.setBatteryValue(jsonReader.nextInt());
break;
// 兼容旧字段名battetyValue避免旧配置解析失败
case "battetyValue":
int oldBatteryValue = jsonReader.nextInt();
bean.setBatteryValue(oldBatteryValue);
LogUtils.w(TAG, "readBeanFromJsonReader: 读取旧字段 battetyValue已兼容为 batteryValue" + oldBatteryValue);
break;
default:
jsonReader.skipValue();
LogUtils.w(TAG, "readBeanFromJsonReader: 跳过未知字段 " + name);
break;
}
}
jsonReader.endObject();
LogUtils.d(TAG, "readBeanFromJsonReader: JSON 反序列化完成 | 时间戳:" + bean.getTimeStamp() + ",电量:" + bean.getBatteryValue());
return bean;
}
}

View File

@@ -0,0 +1,12 @@
package cc.winboll.studio.powerbell.models;
/**
* 电池绘制样式枚举 (单选选项)
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
*/
public enum BatteryStyle {
ENERGY_STYLE, // 能量样式
ZEBRA_STYLE, // 条纹样式
POINT_STYLE // 点阵样式
}

View File

@@ -0,0 +1,131 @@
package cc.winboll.studio.powerbell.models;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.JsonReader;
import android.util.JsonWriter;
import java.io.IOException;
import java.io.Serializable;
import cc.winboll.studio.libappbase.BaseBean;
import cc.winboll.studio.libappbase.LogUtils;
/**
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2025/12/17 15:55
* @Describe 服务控制参数模型
* 适配 API30管理服务启用状态支持 Serializable 持久化、Parcelable 组件传递、JSON 序列化解析
*/
public class ControlCenterServiceBean extends BaseBean implements Parcelable, Serializable {
// ====================== 静态常量(置顶统一管理,避免魔法值) ======================
//private static final long serialVersionUID = 1L; // Serializable 必备,保障反序列化兼容
private static final String TAG = "ControlCenterServiceBean";
private static final String JSON_FIELD_IS_ENABLE_SERVICE = "isEnableService"; // JSON 字段常量,避免硬编码
// ====================== 核心成员变量(私有封装,规范命名) ======================
private boolean isEnableService = false; // 服务启用状态true=启用false=禁用
// ====================== Parcelable 静态创建器(必须 public static final适配 API30 组件传递) ======================
public static final Parcelable.Creator<ControlCenterServiceBean> CREATOR = new Parcelable.Creator<ControlCenterServiceBean>() {
@Override
public ControlCenterServiceBean createFromParcel(Parcel source) {
boolean isEnable = source.readByte() != 0;
ControlCenterServiceBean bean = new ControlCenterServiceBean(isEnable);
LogUtils.d(TAG, String.format("createFromParcel: 反序列化完成isEnableService=%b", isEnable));
return bean;
}
@Override
public ControlCenterServiceBean[] newArray(int size) {
LogUtils.d(TAG, String.format("newArray: 创建数组,长度=%d", size));
return new ControlCenterServiceBean[size];
}
};
// ====================== 构造方法(无参+有参,满足不同初始化场景) ======================
/**
* 无参构造JSON解析、反射创建必备
*/
public ControlCenterServiceBean() {
this.isEnableService = false;
LogUtils.d(TAG, "无参构造初始化服务状态为禁用false");
}
/**
* 有参构造(指定服务启用状态)
* @param isEnableService 服务启用状态
*/
public ControlCenterServiceBean(boolean isEnableService) {
this.isEnableService = isEnableService;
LogUtils.d(TAG, String.format("有参构造初始化服务状态isEnableService=%b", isEnableService));
}
// ====================== Getter/Setter 方法(封装成员变量,控制访问) ======================
public boolean isEnableService() {
LogUtils.d(TAG, String.format("isEnableService: 当前状态=%b", isEnableService));
return isEnableService;
}
public void setIsEnableService(boolean isEnableService) {
LogUtils.d(TAG, String.format("setIsEnableService: 旧状态=%b新状态=%b", this.isEnableService, isEnableService));
this.isEnableService = isEnableService;
}
// ====================== 父类 BaseBean 方法重写核心业务逻辑JSON 序列化/反序列化) ======================
@Override
public String getName() {
String className = ControlCenterServiceBean.class.getName();
LogUtils.d(TAG, String.format("getName: 返回类名=%s", className));
return className;
}
/**
* 序列化对象到 JSON适配数据持久化/网络传输)
*/
@Override
public void writeThisToJsonWriter(JsonWriter jsonWriter) throws IOException {
super.writeThisToJsonWriter(jsonWriter);
jsonWriter.name(JSON_FIELD_IS_ENABLE_SERVICE).value(this.isEnableService);
LogUtils.d(TAG, String.format("writeThisToJsonWriter: 序列化完成,%s=%b", JSON_FIELD_IS_ENABLE_SERVICE, this.isEnableService));
}
/**
* 从 JSON 反序列化创建对象(适配数据恢复)
*/
@Override
public BaseBean readBeanFromJsonReader(JsonReader jsonReader) throws IOException {
ControlCenterServiceBean bean = new ControlCenterServiceBean();
jsonReader.beginObject();
while (jsonReader.hasNext()) {
String fieldName = jsonReader.nextName();
if (JSON_FIELD_IS_ENABLE_SERVICE.equals(fieldName)) {
boolean isEnable = jsonReader.nextBoolean();
bean.setIsEnableService(isEnable);
LogUtils.d(TAG, String.format("readBeanFromJsonReader: 读取字段,%s=%b", fieldName, isEnable));
} else {
jsonReader.skipValue();
LogUtils.w(TAG, String.format("readBeanFromJsonReader: 跳过未知字段=%s", fieldName));
}
}
jsonReader.endObject();
LogUtils.d(TAG, "readBeanFromJsonReader: 反序列化完成");
return bean;
}
// ====================== Parcelable 接口方法实现(适配 Intent 组件间传递Java7 适配) ======================
@Override
public int describeContents() {
LogUtils.d(TAG, "describeContents: 返回内容描述符=0");
return 0; // 无特殊内容如文件描述符返回0即可API30 标准实现)
}
/**
* 序列化对象到 ParcelIntent 传递必备Java7 适配:用 byte 存储 boolean
*/
@Override
public void writeToParcel(Parcel dest, int flags) {
byte flag = (byte) (this.isEnableService ? 1 : 0);
dest.writeByte(flag);
LogUtils.d(TAG, String.format("writeToParcel: 序列化完成isEnableService=%b存储为byte=%d", this.isEnableService, flag));
}
}

View File

@@ -0,0 +1,75 @@
package cc.winboll.studio.powerbell.models;
import cc.winboll.studio.libappbase.LogUtils;
/**
* 通知数据模型
* 适配 API30统一存储通知标题、内容、标识信息支持各组件数据传递
* @Author ZhanGSKen<zhangsken@qq.com>
* @Describe 通知数据模型:统一存储通知标题、内容等信息,适配各组件数据传递
*/
public class NotificationMessage {
// ====================== 静态常量(统一管理) ======================
private static final String TAG = "NotificationMessage";
private static final String EMPTY_STRING = "";
// ====================== 核心成员变量(按业务逻辑排序) ======================
private String title; // 通知标题
private String content; // 通知内容
private String remindMSG; // 通知标识(区分服务运行/充电/耗电)
// ====================== 构造方法(无参+全参,满足不同初始化场景) ======================
/**
* 无参构造器反射实例化、JSON反序列化必备
*/
public NotificationMessage() {
this.title = EMPTY_STRING;
this.content = EMPTY_STRING;
this.remindMSG = EMPTY_STRING;
LogUtils.d(TAG, "无参构造:初始化通知数据模型,默认值为空字符串");
}
/**
* 全参构造器(直接传参创建实例,简化调用)
* @param title 通知标题
* @param content 通知内容
* @param remindMSG 通知标识
*/
public NotificationMessage(String title, String content, String remindMSG) {
this.title = title == null ? EMPTY_STRING : title;
this.content = content == null ? EMPTY_STRING : content;
this.remindMSG = remindMSG == null ? EMPTY_STRING : remindMSG;
LogUtils.d(TAG, String.format("全参构造:初始化完成 | 标题:%s | 内容:%s | 标识:%s",
this.title, this.content, this.remindMSG));
}
// ====================== Setter 方法(补充空值防护与调试日志) ======================
public void setTitle(String title) {
this.title = title == null ? EMPTY_STRING : title;
LogUtils.d(TAG, String.format("setTitle通知标题设置为「%s」", this.title));
}
public void setContent(String content) {
this.content = content == null ? EMPTY_STRING : content;
LogUtils.d(TAG, String.format("setContent通知内容设置为「%s」", this.content));
}
public void setRemindMSG(String remindMSG) {
this.remindMSG = remindMSG == null ? EMPTY_STRING : remindMSG;
LogUtils.d(TAG, String.format("setRemindMSG通知标识设置为「%s」", this.remindMSG));
}
// ====================== Getter 方法(按成员变量顺序排列) ======================
public String getTitle() {
return title;
}
public String getContent() {
return content;
}
public String getRemindMSG() {
return remindMSG;
}
}

View File

@@ -0,0 +1,47 @@
package cc.winboll.studio.powerbell.models;
import cc.winboll.studio.libappbase.LogUtils;
import java.io.Serializable;
/**
* TTS 语音播放文本内容实体类
* 适配Java7 语法规范 | Android API30 系统版本
* 特性:实现序列化接口,支持跨页面/进程传递,属性默认值初始化
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
* @Date 2025/12/29 19:13
*/
public class TTSSpeakTextBean implements Serializable {
// ====================================== 常量区 - 置顶排序 ======================================
/** 日志TAG 瞬态修饰,不参与序列化,减少序列化体积 */
transient public static final String TAG = "TTSSpeakTextBean";
// ====================================== 成员属性区 - 业务属性排序 ======================================
/** 延迟播放时长 单位毫秒默认值0无延迟播放 */
public int mnDelay = 0;
/** TTS语音播放文本内容默认值空字符串防止空指针 */
public String mszSpeakContent = "";
// ====================================== 构造方法区 - 无参+有参 完整实现 ======================================
/**
* 无参构造方法
* Java7序列化规范必备 + 兼容反射实例化场景
*/
public TTSSpeakTextBean() {
LogUtils.d(TAG, "【无参构造】TTSSpeakTextBean 实例化,使用默认值 | 延迟:" + mnDelay + " | 文本:" + mszSpeakContent);
}
/**
* 有参构造方法【主构造】
* @param nDelay 延迟播放时长(ms)
* @param szSpeakContent 语音播放文本内容
*/
public TTSSpeakTextBean(int nDelay, String szSpeakContent) {
LogUtils.d(TAG, "【有参构造】TTSSpeakTextBean 实例化,入参 | 延迟:" + nDelay + " | 文本:" + szSpeakContent);
this.mnDelay = nDelay;
this.mszSpeakContent = szSpeakContent;
LogUtils.d(TAG, "【有参构造】赋值完成 | 最终延迟:" + this.mnDelay + " | 最终文本:" + this.mszSpeakContent);
}
}

View File

@@ -0,0 +1,156 @@
package cc.winboll.studio.powerbell.models;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.JsonReader;
import android.util.JsonWriter;
import java.io.IOException;
import java.io.Serializable;
import cc.winboll.studio.libappbase.BaseBean;
import cc.winboll.studio.libappbase.LogUtils;
/**
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
* @Date 2025/12/29 20:59
* @Describe 贴心服务配置实体类 (适配API30 / Java7)
*/
public class ThoughtfulServiceBean extends BaseBean implements Parcelable, Serializable {
// ====================== 常量区 - 置顶统一管理 ======================
public static final String TAG = ThoughtfulServiceBean.class.getSimpleName();
private static final long serialVersionUID = 1L; // Serializable 序列化兼容必备
// JSON序列化字段常量 杜绝硬编码
public static final String JSON_FIELD_IS_ENABLE_CHARGE_TTS = "isEnableChargeTts";
public static final String JSON_FIELD_IS_ENABLE_USE_POWER_TTS = "isEnableUsePowerTts";
// ====================== 核心成员变量 - 私有封装 ======================
private boolean isEnableChargeTts = false; // 是否启用 充电TTS贴心语音服务
private boolean isEnableUsePowerTts = false; // 是否启用 用电TTS贴心语音服务
// ====================== Parcelable 静态创建器 (API30标准写法 必须public static final) ======================
public static final Creator<ThoughtfulServiceBean> CREATOR = new Creator<ThoughtfulServiceBean>() {
@Override
public ThoughtfulServiceBean createFromParcel(Parcel source) {
return new ThoughtfulServiceBean(source);
}
@Override
public ThoughtfulServiceBean[] newArray(int size) {
LogUtils.d(TAG, "newArray: 初始化数组size = " + size);
return new ThoughtfulServiceBean[size];
}
};
// ====================== 构造方法区 (无参+有参+Parcel构造 全覆盖) ======================
/**
* 无参构造 - JSON解析/反射实例化 必备
*/
public ThoughtfulServiceBean() {
LogUtils.d(TAG, "ThoughtfulServiceBean: 无参构造初始化默认禁用所有TTS服务");
}
/**
* 全参构造 - 手动配置所有服务状态
* @param isEnableChargeTts 充电TTS服务开关
* @param isEnableUsePowerTts 用电TTS服务开关
*/
public ThoughtfulServiceBean(boolean isEnableChargeTts, boolean isEnableUsePowerTts) {
this.isEnableChargeTts = isEnableChargeTts;
this.isEnableUsePowerTts = isEnableUsePowerTts;
LogUtils.d(TAG, "ThoughtfulServiceBean: 全参构造 | isEnableChargeTts=" + isEnableChargeTts + " | isEnableUsePowerTts=" + isEnableUsePowerTts);
}
/**
* Parcel反序列化构造 - Parcelable必备 私有私有化
*/
private ThoughtfulServiceBean(Parcel in) {
this.isEnableChargeTts = in.readByte() != 0;
this.isEnableUsePowerTts = in.readByte() != 0;
LogUtils.d(TAG, "ThoughtfulServiceBean: Parcel构造解析完成 | isEnableChargeTts=" + isEnableChargeTts + " | isEnableUsePowerTts=" + isEnableUsePowerTts);
}
// ====================== Getter/Setter 方法区 (封装成员变量 统一访问) ======================
public boolean isEnableChargeTts() {
return isEnableChargeTts;
}
public void setIsEnableChargeTts(boolean isEnableChargeTts) {
LogUtils.d(TAG, "setIsEnableChargeTts: 旧值=" + this.isEnableChargeTts + " 新值=" + isEnableChargeTts);
this.isEnableChargeTts = isEnableChargeTts;
}
public boolean isEnableUsePowerTts() {
return isEnableUsePowerTts;
}
public void setIsEnableUsePowerTts(boolean isEnableUsePowerTts) {
LogUtils.d(TAG, "setIsEnableUsePowerTts: 旧值=" + this.isEnableUsePowerTts + " 新值=" + isEnableUsePowerTts);
this.isEnableUsePowerTts = isEnableUsePowerTts;
}
// ====================== 重写父类 BaseBean 核心方法 (JSON序列化/反序列化 业务核心) ======================
@Override
public String getName() {
String className = ThoughtfulServiceBean.class.getName();
LogUtils.d(TAG, "getName: 返回当前实体类名 = " + className);
return className;
}
/**
* JSON序列化 - 写入所有字段 适配持久化/网络传输
*/
@Override
public void writeThisToJsonWriter(JsonWriter jsonWriter) throws IOException {
super.writeThisToJsonWriter(jsonWriter);
jsonWriter.name(JSON_FIELD_IS_ENABLE_CHARGE_TTS).value(this.isEnableChargeTts);
jsonWriter.name(JSON_FIELD_IS_ENABLE_USE_POWER_TTS).value(this.isEnableUsePowerTts);
LogUtils.d(TAG, "writeThisToJsonWriter: JSON序列化完成所有TTS服务状态已写入");
}
/**
* JSON反序列化 - 读取字段生成实体 适配数据恢复
*/
@Override
public BaseBean readBeanFromJsonReader(JsonReader jsonReader) throws IOException {
ThoughtfulServiceBean bean = new ThoughtfulServiceBean();
jsonReader.beginObject();
while (jsonReader.hasNext()) {
String fieldName = jsonReader.nextName();
switch (fieldName) {
case JSON_FIELD_IS_ENABLE_CHARGE_TTS:
bean.setIsEnableChargeTts(jsonReader.nextBoolean());
break;
case JSON_FIELD_IS_ENABLE_USE_POWER_TTS:
bean.setIsEnableUsePowerTts(jsonReader.nextBoolean());
break;
default:
jsonReader.skipValue();
LogUtils.w(TAG, "readBeanFromJsonReader: 跳过未知JSON字段 = " + fieldName);
break;
}
}
jsonReader.endObject();
LogUtils.d(TAG, "readBeanFromJsonReader: JSON反序列化完成生成实体对象");
return bean;
}
// ====================== 实现 Parcelable 接口方法 (组件间Intent传递必备 API30/Java7完美适配) ======================
@Override
public int describeContents() {
return 0; // 无文件描述符等特殊内容固定返回0即可
}
/**
* Parcel序列化 - boolean用byte存储(Java7/API30标准写法 避免兼容性问题)
*/
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeByte((byte) (isEnableChargeTts ? 1 : 0));
dest.writeByte((byte) (isEnableUsePowerTts ? 1 : 0));
LogUtils.d(TAG, "writeToParcel: Parcel序列化完成所有TTS服务状态已写入");
}
}

View File

@@ -5,83 +5,271 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.powerbell.beans.AppConfigBean;
import cc.winboll.studio.powerbell.App;
import cc.winboll.studio.powerbell.models.AppConfigBean;
import cc.winboll.studio.powerbell.models.NotificationMessage;
import cc.winboll.studio.powerbell.services.ControlCenterService;
import cc.winboll.studio.powerbell.utils.AppConfigUtils;
import cc.winboll.studio.powerbell.utils.BatteryUtils;
import cc.winboll.studio.powerbell.utils.NotificationManagerUtils;
import java.lang.ref.WeakReference;
import cc.winboll.studio.powerbell.services.ThoughtfulService;
/**
* 控制中心广播接收器
* 功能:监听电池状态变化、前台通知更新、配置变更指令
* 适配Java7 | API30 | 内存泄漏防护 | 多线程状态同步
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
* @Date 2025/12/19 20:23
* @Describe 统一处理系统与应用内广播,同步电池状态与配置,保障多线程数据一致性
*/
public class ControlCenterServiceReceiver extends BroadcastReceiver {
public static final String TAG = ControlCenterServiceReceiver.class.getSimpleName();
// ====================== 静态常量区(置顶归类,消除魔法值) ======================
public static final String TAG = "ControlCenterServiceReceiver";
public static final String ACTION_UPDATE_SERVICENOTIFICATION = ControlCenterServiceReceiver.class.getName() + ".ACTION_UPDATE_NOTIFICATION";
public static final String ACTION_START_REMINDTHREAD = ControlCenterServiceReceiver.class.getName() + ".ACTION_UPDATE_REMINDTHREAD";
// 广播Action常量带包名前缀防冲突
public static final String ACTION_UPDATE_FOREGROUND_NOTIFICATION = "cc.winboll.studio.powerbell.action.ACTION_UPDATE_FOREGROUND_NOTIFICATION";
public static final String ACTION_APPCONFIG_CHANGED = "cc.winboll.studio.powerbell.action.ACTION_APPCONFIG_CHANGED";
public static final String EXTRA_APP_CONFIG_BEAN = "extra_app_config_bean";
WeakReference<ControlCenterService> mwrService;
// 存储电量指示值,
// 用于校验电量消息时的电量变化
static volatile int _mnTheQuantityOfElectricityOld = -1;
static volatile boolean _mIsCharging = false;
// 广播优先级与电量范围常量
private static final int BROADCAST_PRIORITY = IntentFilter.SYSTEM_HIGH_PRIORITY - 10;
private static final int BATTERY_LEVEL_MIN = 0;
private static final int BATTERY_LEVEL_MAX = 100;
private static final int INVALID_BATTERY = -1; // 无效电量标识
// ====================== 静态状态标记volatile保证多线程可见性 ======================
private static volatile int sLastBatteryLevel = INVALID_BATTERY; // 上次电量(多线程可见)
private static volatile boolean sIsCharging = false; // 上次充电状态(多线程可见)
// ====================== 成员变量区(弱引用防泄漏,按功能分层) ======================
private WeakReference<ControlCenterService> mwrControlCenterService;
private boolean isRegistered = false; // 标记广播注册状态,避免冗余操作
// ====================== 构造方法(初始化弱引用,避免服务强引用泄漏) ======================
public ControlCenterServiceReceiver(ControlCenterService service) {
mwrService = new WeakReference<ControlCenterService>(service);
LogUtils.d(TAG, String.format("ControlCenterServiceReceiver() 构造 | 服务实例:%s",
service != null ? service.getClass().getSimpleName() : "null"));
this.mwrControlCenterService = new WeakReference<ControlCenterService>(service);
}
// ====================== 广播核心接收逻辑入口方法分Action分发处理 ======================
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_UPDATE_SERVICENOTIFICATION)) {
mwrService.get().updateServiceNotification();
} else if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
boolean isCharging = BatteryUtils.isCharging(intent);
int nTheQuantityOfElectricity = BatteryUtils.getTheQuantityOfElectricity(intent);
if (mwrService.get().getRemindThread() != null) {
// 先设置提醒进程电池状态标志
if (_mIsCharging != isCharging) {
mwrService.get().getRemindThread().setIsCharging(isCharging);
}
if (_mnTheQuantityOfElectricityOld != nTheQuantityOfElectricity) {
mwrService.get().getRemindThread().setQuantityOfElectricity(nTheQuantityOfElectricity);
}
String action = intent != null ? intent.getAction() : "null";
LogUtils.d(TAG, String.format("onReceive() 执行 | 接收广播 Action%s", action));
// 基础参数校验
if (context == null || intent == null || action == null) {
LogUtils.e(TAG, "onReceive() 终止 | 参数无效context=" + context + " | intent=" + intent + "");
return;
}
// 弱引用获取服务,双重校验服务有效性
ControlCenterService service = mwrControlCenterService != null ? mwrControlCenterService.get() : null;
if (service == null || service.isDestroyed()) {
LogUtils.e(TAG, "onReceive() 终止 | 服务已销毁或为空,执行注销");
unregisterAction(context);
return;
}
// 分Action处理业务逻辑
switch (action) {
case Intent.ACTION_BATTERY_CHANGED:
handleBatteryStateChanged(service, intent);
break;
case ACTION_UPDATE_FOREGROUND_NOTIFICATION:
handleUpdateForegroundNotification(service);
break;
case ACTION_APPCONFIG_CHANGED:
LogUtils.d(TAG, "onReceive() 分发 | 处理配置更新广播");
handleNotifyAppConfigUpdate(service);
break;
default:
LogUtils.w(TAG, String.format("onReceive() 警告 | 未知Action=%s", action));
}
LogUtils.d(TAG, "onReceive() 完成 | 广播处理结束");
}
// ====================== 业务处理方法(按功能拆分,强化容错与日志) ======================
/**
* 处理电池状态变化广播
* @param service 控制中心服务实例
* @param intent 电池状态广播意图
*/
private void handleBatteryStateChanged(ControlCenterService service, Intent intent) {
LogUtils.d(TAG, "handleBatteryStateChanged() 执行 | 解析电池状态");
try {
// 1. 解析并校验当前电池状态
boolean currentCharging = BatteryUtils.isCharging(intent);
int currentBatteryLevel = BatteryUtils.getCurrentBatteryLevel(intent);
currentBatteryLevel = Math.min(Math.max(currentBatteryLevel, BATTERY_LEVEL_MIN), BATTERY_LEVEL_MAX);
LogUtils.d(TAG, String.format("handleBatteryStateChanged() 解析 | 充电=%b | 电量=%d%%", currentCharging, currentBatteryLevel));
// 2. 状态无变化则跳过,减少无效运算
if (currentCharging == sIsCharging && currentBatteryLevel == sLastBatteryLevel) {
LogUtils.d(TAG, "handleBatteryStateChanged() 跳过 | 电池状态无变化");
return;
}
// 新电池状态标志某一个有变化就更新显示信息
if (_mIsCharging != isCharging || _mnTheQuantityOfElectricityOld != nTheQuantityOfElectricity) {
mwrService.get().updateServiceNotification();
AppConfigUtils appConfigUtils = AppConfigUtils.getInstance(context);
appConfigUtils.loadAppConfigBean();
AppConfigBean appConfigBean = appConfigUtils.mAppConfigBean;
appConfigBean.setCurrentValue(nTheQuantityOfElectricity);
appConfigBean.setIsCharging(isCharging);
mwrService.get().startRemindThread(appConfigBean);
// 保存电池报告
// 示例数据更新逻辑
// List<BatteryData> newData = new ArrayList<>(adapter.getDataList());
// newData.add(0, new BatteryData(percentage, "00:00:00", "00:00:00"));
// adapter.updateData(newData);
// 保存好新的电池状态标志
_mIsCharging = isCharging;
_mnTheQuantityOfElectricityOld = nTheQuantityOfElectricity;
}
} else if (intent.getAction().equals(ACTION_START_REMINDTHREAD)) {
LogUtils.d(TAG, "ACTION_START_REMINDTHREAD");
//AppConfigUtils appConfigUtils = AppConfigUtils.getInstance(context);
//appConfigUtils.loadAppConfigBean();
AppConfigBean appConfigBean = (AppConfigBean)intent.getSerializableExtra("appConfigBean");
mwrService.get().startRemindThread(appConfigBean);
// 在插拔充电线时,执行贴心服务
if(currentCharging != sIsCharging && sLastBatteryLevel != INVALID_BATTERY) {
//App.notifyMessage(TAG, String.format("sLastBatteryLevel %d", sLastBatteryLevel));
if(currentCharging) {
ThoughtfulService.startServiceWithType(service, ThoughtfulService.ServiceType.CHARGE_STATE);
} else {
ThoughtfulService.startServiceWithType(service, ThoughtfulService.ServiceType.DISCHARGE_STATE);
}
}
// 3. 更新静态缓存状态,保证多线程可见
sIsCharging = currentCharging;
sLastBatteryLevel = currentBatteryLevel;
// 4. 同步缓存状态到配置
handleNotifyAppConfigUpdate(service);
LogUtils.d(TAG, String.format("handleBatteryStateChanged() 完成 | 缓存电量=%d%% | 缓存充电状态=%b",
sLastBatteryLevel, sIsCharging));
} catch (Exception e) {
LogUtils.e(TAG, "handleBatteryStateChanged() 失败", e);
}
}
// 注册 Receiver
//
/**
* 处理配置变更通知,同步缓存状态到配置
* @param service 控制中心服务实例
*/
private void handleNotifyAppConfigUpdate(ControlCenterService service) {
LogUtils.d(TAG, "handleNotifyAppConfigUpdate() 执行 | 同步缓存状态到配置");
try {
// 加载最新配置
AppConfigBean latestConfig = AppConfigUtils.getInstance(service).loadAppConfig();
if (latestConfig == null) {
LogUtils.e(TAG, "handleNotifyAppConfigUpdate() 终止 | 最新配置为空");
return;
}
LogUtils.d(TAG, String.format("handleNotifyAppConfigUpdate() 加载 | 充电阈值=%d | 耗电阈值=%d",
latestConfig.getChargeReminderValue(), latestConfig.getUsageReminderValue()));
// 同步缓存的电池状态到配置
App.sQuantityOfElectricity = sLastBatteryLevel;
latestConfig.setIsCharging(sIsCharging);
service.notifyAppConfigUpdate(latestConfig);
LogUtils.d(TAG, String.format("handleNotifyAppConfigUpdate() 完成 | 缓存电量=%d%% | 充电状态=%b",
sLastBatteryLevel, sIsCharging));
} catch (Exception e) {
LogUtils.e(TAG, "handleNotifyAppConfigUpdate() 失败", e);
}
}
/**
* 处理前台服务通知更新
* @param service 控制中心服务实例
*/
private void handleUpdateForegroundNotification(ControlCenterService service) {
LogUtils.d(TAG, "handleUpdateForegroundNotification() 执行 | 更新前台通知");
try {
NotificationManagerUtils notifyUtils = service.getNotificationManager();
NotificationMessage notifyMsg = service.getForegroundNotifyMsg();
// 非空校验,避免空指针
if (notifyUtils == null || notifyMsg == null) {
LogUtils.e(TAG, String.format("handleUpdateForegroundNotification() 终止 | 通知工具类或消息为空notifyUtils=%s | notifyMsg=%s",
notifyUtils, notifyMsg));
return;
}
notifyUtils.updateForegroundServiceNotify(notifyMsg);
LogUtils.d(TAG, String.format("handleUpdateForegroundNotification() 完成 | 标题=%s", notifyMsg.getTitle()));
} catch (Exception e) {
LogUtils.e(TAG, "handleUpdateForegroundNotification() 失败", e);
}
}
// ====================== 广播注册/注销(强化容错,避免重复操作) ======================
/**
* 注册广播接收器
* @param context 上下文
*/
public void registerAction(Context context) {
IntentFilter filter=new IntentFilter();
filter.addAction(ACTION_UPDATE_SERVICENOTIFICATION);
filter.addAction(ACTION_START_REMINDTHREAD);
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
context.registerReceiver(this, filter);
LogUtils.d(TAG, "registerAction() 执行 | 注册广播接收器");
if (context == null || isRegistered) {
LogUtils.e(TAG, "registerAction() 失败 | 上下文为空或已注册");
return;
}
try {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
filter.addAction(ACTION_UPDATE_FOREGROUND_NOTIFICATION);
filter.addAction(ACTION_APPCONFIG_CHANGED);
filter.setPriority(BROADCAST_PRIORITY);
context.registerReceiver(this, filter);
isRegistered = true;
LogUtils.d(TAG, String.format("registerAction() 完成 | 优先级=%d", BROADCAST_PRIORITY));
} catch (Exception e) {
LogUtils.e(TAG, "registerAction() 失败", e);
}
}
/**
* 注销广播接收器
* @param context 上下文
*/
public void unregisterAction(Context context) {
LogUtils.d(TAG, "unregisterAction() 执行 | 注销广播接收器");
if (context == null || !isRegistered) {
LogUtils.e(TAG, "unregisterAction() 失败 | 上下文为空或未注册");
return;
}
try {
context.unregisterReceiver(this);
isRegistered = false;
LogUtils.d(TAG, "unregisterAction() 完成 | 广播注销成功");
} catch (IllegalArgumentException e) {
LogUtils.w(TAG, "unregisterAction() 警告 | 广播未注册,跳过注销");
} catch (Exception e) {
LogUtils.e(TAG, "unregisterAction() 失败", e);
}
}
// ====================== 资源释放与Getter方法按需开放防泄漏 ======================
/**
* 主动释放资源,避免内存泄漏
*/
public void release() {
LogUtils.d(TAG, "release() 执行 | 释放广播接收器资源");
// 清空弱引用帮助GC回收
if (mwrControlCenterService != null) {
mwrControlCenterService.clear();
mwrControlCenterService = null;
LogUtils.d(TAG, "release() 步骤 | 弱引用已清空");
}
// 重置静态状态缓存
sLastBatteryLevel = -1;
sIsCharging = false;
LogUtils.d(TAG, "release() 完成 | 静态状态缓存已重置");
}
/**
* 获取上次记录的电池电量
* @return 电量值0-100未初始化返回-1
*/
public static int getLastBatteryLevel() {
return sLastBatteryLevel;
}
/**
* 获取上次记录的充电状态
* @return true=充电中false=未充电
*/
public static boolean isLastCharging() {
return sIsCharging;
}
}

View File

@@ -4,63 +4,177 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.powerbell.App;
import cc.winboll.studio.powerbell.fragments.MainViewFragment;
import cc.winboll.studio.powerbell.MainActivity;
import cc.winboll.studio.powerbell.utils.AppConfigUtils;
import cc.winboll.studio.powerbell.utils.BatteryUtils;
import cc.winboll.studio.powerbell.utils.NotificationHelper;
/**
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2025/12/19 20:13
* @Describe 全局应用广播接收器
* 功能:监听系统电池状态变化,同步状态到配置工具类,通知页面更新
* 适配Java7 | API30 | 内存泄漏防护
*/
public class GlobalApplicationReceiver extends BroadcastReceiver {
// ====================== 静态常量区(置顶归类,消除魔法值) ======================
public static final String TAG = "GlobalApplicationReceiver";
private static final int BATTERY_LEVEL_MIN = 0;
private static final int BATTERY_LEVEL_MAX = 100;
AppConfigUtils mAppConfigUtils;
App mGlobalApplication;
// 存储电量指示值,
// 用于校验电量消息时的电量变化
static volatile int _mnTheQuantityOfElectricityOld = -1;
static volatile boolean _mIsCharging = false;
// 保存当前实例,
// 便利封装 registerAction() 函数
GlobalApplicationReceiver mReceiver;
// ====================== 静态状态标记volatile保证多线程可见性 ======================
private static volatile int sLastBatteryLevel = -1; // 历史电量0-100
private static volatile boolean sLastIsCharging = false; // 历史充电状态
// ====================== 成员变量区按功能分层移除冗余的mCurrentReceiver ======================
private App mGlobalApplication;
private AppConfigUtils mAppConfigUtils;
// ====================== 构造方法(强化参数校验,初始化核心依赖) ======================
public GlobalApplicationReceiver(App globalApplication) {
mReceiver = this;
mGlobalApplication = globalApplication;
mAppConfigUtils = App.getAppConfigUtils(mGlobalApplication);
LogUtils.d(TAG, String.format("构造接收器 | App实例%s", globalApplication));
if (globalApplication == null) {
LogUtils.e(TAG, "构造失败App实例为空");
throw new IllegalArgumentException("App cannot be null");
}
this.mGlobalApplication = globalApplication;
this.mAppConfigUtils = App.getAppConfigUtils(mGlobalApplication);
LogUtils.d(TAG, String.format("构造完成 | AppConfigUtils%s", mAppConfigUtils));
}
// ====================== 广播核心接收逻辑(入口方法,过滤电池状态广播) ======================
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
// 先设置好新电池状态标志
boolean isCharging = BatteryUtils.isCharging(intent);
if (_mIsCharging != isCharging) {
mAppConfigUtils.setIsCharging(isCharging);
String action = intent != null ? intent.getAction() : "null";
LogUtils.d(TAG, String.format("onReceive: 接收广播 | 上下文:%s | Action%s", context, action));
// 基础参数校验
if (context == null || intent == null || action == null) {
LogUtils.e(TAG, "onReceive: 参数无效,终止处理");
return;
}
// 仅处理电池状态变化广播
if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
handleBatteryStateChanged(context, intent);
}
LogUtils.d(TAG, "onReceive: 广播处理完成");
}
// ====================== 业务逻辑方法(处理电池状态变化,同步配置+通知页面) ======================
/**
* 处理电池状态变化广播
* @param context 上下文
* @param intent 电池状态广播意图
*/
private void handleBatteryStateChanged(Context context, Intent intent) {
LogUtils.d(TAG, "handleBatteryStateChanged: 解析电池状态");
try {
// 1. 解析当前电池状态(复用工具类,二次校验电量范围)
boolean currentIsCharging = BatteryUtils.isCharging(intent);
int currentBatteryLevel = BatteryUtils.getCurrentBatteryLevel(intent);
currentBatteryLevel = Math.min(Math.max(currentBatteryLevel, BATTERY_LEVEL_MIN), BATTERY_LEVEL_MAX);
LogUtils.d(TAG, String.format("handleBatteryStateChanged: 当前状态 | 充电=%b | 电量=%d%%", currentIsCharging, currentBatteryLevel));
// 2. 状态无变化则跳过,减少无效运算
if (currentIsCharging == sLastIsCharging && currentBatteryLevel == sLastBatteryLevel) {
LogUtils.d(TAG, "handleBatteryStateChanged: 状态无变化,跳过处理");
return;
}
int nTheQuantityOfElectricity = BatteryUtils.getTheQuantityOfElectricity(intent);
if (_mnTheQuantityOfElectricityOld != nTheQuantityOfElectricity) {
mAppConfigUtils.setCurrentValue(nTheQuantityOfElectricity);
// 3. 同步最新状态到配置工具类
if (mAppConfigUtils != null) {
if (currentIsCharging != sLastIsCharging) {
mAppConfigUtils.setCharging(currentIsCharging);
LogUtils.d(TAG, String.format("handleBatteryStateChanged: 同步充电状态 | %b", currentIsCharging));
}
if (currentBatteryLevel != sLastBatteryLevel) {
mAppConfigUtils.setCurrentBatteryValue(currentBatteryLevel);
LogUtils.d(TAG, String.format("handleBatteryStateChanged: 同步电量 | %d%%", currentBatteryLevel));
}
} else {
LogUtils.e(TAG, "handleBatteryStateChanged: AppConfigUtils为空同步失败");
}
// 新电池状态标志某一个有变化就更新显示信息
if (_mIsCharging != isCharging || _mnTheQuantityOfElectricityOld != nTheQuantityOfElectricity) {
// 电池状态改变先取消旧的提醒消息
//NotificationHelper.cancelRemindNotification(context);
App.getAppCacheUtils(context).addChangingTime(nTheQuantityOfElectricity);
MainViewFragment.sendMsgCurrentValueBattery(nTheQuantityOfElectricity);
// 保存好新的电池状态标志
_mIsCharging = isCharging;
_mnTheQuantityOfElectricityOld = nTheQuantityOfElectricity;
// 4. 执行状态变化后的业务逻辑
// 记录电量变化时间
if (App.getAppCacheUtils(context) != null) {
App.getAppCacheUtils(context).addChangingTime(currentBatteryLevel);
LogUtils.d(TAG, "handleBatteryStateChanged: 记录电量变化时间");
}
// 通知MainActivity更新电量
MainActivity.sendCurrentBatteryValueMessage(currentBatteryLevel);
LogUtils.d(TAG, String.format("handleBatteryStateChanged: 发送电量更新消息到MainActivity | %d%%", currentBatteryLevel));
// 5. 更新历史状态缓存
sLastIsCharging = currentIsCharging;
sLastBatteryLevel = currentBatteryLevel;
LogUtils.d(TAG, "handleBatteryStateChanged: 更新历史状态完成");
} catch (Exception e) {
LogUtils.e(TAG, "handleBatteryStateChanged: 处理失败", e);
}
}
// 注册 Receiver
//
// ====================== 广播注册/注销(强化容错,避免重复操作) ======================
/**
* 注册广播接收器
*/
public void registerAction() {
IntentFilter filter=new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
mGlobalApplication.registerReceiver(mReceiver, filter);
LogUtils.d(TAG, "registerAction: 注册广播");
if (mGlobalApplication == null) {
LogUtils.e(TAG, "注册失败App实例为空");
return;
}
try {
// 先注销再注册,避免重复注册异常
unregisterAction();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
mGlobalApplication.registerReceiver(this, filter);
LogUtils.d(TAG, "registerAction: 广播注册成功");
} catch (Exception e) {
LogUtils.e(TAG, "registerAction: 注册失败", e);
}
}
/**
* 注销广播接收器
*/
public void unregisterAction() {
LogUtils.d(TAG, "unregisterAction: 注销广播");
if (mGlobalApplication == null) {
LogUtils.e(TAG, "注销失败App实例为空");
return;
}
try {
mGlobalApplication.unregisterReceiver(this);
LogUtils.d(TAG, "unregisterAction: 广播注销成功");
} catch (IllegalArgumentException e) {
LogUtils.w(TAG, "unregisterAction: 广播未注册,跳过注销");
} catch (Exception e) {
LogUtils.e(TAG, "unregisterAction: 注销失败", e);
}
}
// ====================== 资源释放方法(主动释放,彻底避免内存泄漏) ======================
/**
* 释放接收器资源供App销毁时调用
*/
public void release() {
LogUtils.d(TAG, "release: 释放接收器资源");
// 注销广播
unregisterAction();
// 置空引用帮助GC回收
mGlobalApplication = null;
mAppConfigUtils = null;
// 重置静态状态缓存
sLastBatteryLevel = -1;
sLastIsCharging = false;
LogUtils.d(TAG, "release: 资源释放完成");
}
}

View File

@@ -1,10 +1,5 @@
package cc.winboll.studio.powerbell.receivers;
/**
* @Author ZhanGSKen<zhangsken@qq.com>
* @Date 2024/06/06 15:01:39
* @Describe 应用广播消息接收类
*/
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -14,30 +9,84 @@ import cc.winboll.studio.powerbell.App;
import cc.winboll.studio.powerbell.services.ControlCenterService;
import cc.winboll.studio.powerbell.utils.ServiceUtils;
/**
* @Author ZhanGSKen<zhangsken@qq.com>
* @Date 2024/06/06 15:01:39
* @Describe 应用核心广播接收器
* 功能:监听开机完成广播,实现服务开机自启
* 适配Java7 | API30 | 服务启动兼容性处理
*/
public class MainReceiver extends BroadcastReceiver {
// ====================== 静态常量区(置顶归类,消除魔法值) ======================
public static final String TAG = "MainReceiver";
// 系统广播Action常量
private static final String ACTION_BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED";
// API版本常量适配前台服务启动要求
private static final int API_LEVEL_26 = 26;
static final String ACTION_BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED";
// 存储电量指示值,
// 用于校验电量消息时的电量变化
static volatile int _mnTheQuantityOfElectricityOld = -1;
// ====================== 静态状态标记volatile保证多线程可见性 ======================
// 历史电量值,用于校验电量变化(暂未使用,保留扩展能力)
private static volatile int sLastBatteryLevel = -1;
// ====================== 广播核心接收逻辑入口方法分Action处理 ======================
@Override
public void onReceive(Context context, Intent intent) {
String szAction = intent.getAction();
if (szAction.equals(ACTION_BOOT_COMPLETED)) {
boolean isEnableService = App.getAppConfigUtils(context).getIsEnableService();
if (isEnableService) {
if (ServiceUtils.isServiceAlive(context.getApplicationContext(), ControlCenterService.class.getName()) == false) {
LogUtils.d(TAG, "wakeupAndBindMain() Wakeup... ControlCenterService");
if (Build.VERSION.SDK_INT >= 26) {
context.startForegroundService(new Intent(context, ControlCenterService.class));
} else {
context.startService(new Intent(context, ControlCenterService.class));
}
}
// 基础参数校验
if (context == null || intent == null) {
LogUtils.e(TAG, "onReceive: 上下文或意图为空,终止处理");
return;
}
String action = intent.getAction();
LogUtils.d(TAG, String.format("onReceive: 接收广播 | Action%s", action));
// 仅处理开机完成广播
if (ACTION_BOOT_COMPLETED.equals(action)) {
handleBootCompleted(context);
} else {
LogUtils.w(TAG, String.format("onReceive: 忽略未知Action%s", action));
}
}
// ====================== 业务处理方法(处理开机完成广播,实现服务自启) ======================
/**
* 处理开机完成广播,自动启动控制中心服务
* @param context 上下文
*/
private void handleBootCompleted(Context context) {
LogUtils.d(TAG, "handleBootCompleted: 开始处理开机完成广播");
try {
// 1. 校验服务启用状态
boolean isServiceEnabled = App.getAppConfigUtils(context).isServiceEnabled();
LogUtils.d(TAG, String.format("handleBootCompleted: 服务启用状态:%b", isServiceEnabled));
if (!isServiceEnabled) {
LogUtils.d(TAG, "handleBootCompleted: 服务未启用,跳过自启");
return;
}
// 2. 校验服务是否已运行
String serviceClassName = ControlCenterService.class.getName();
boolean isServiceAlive = ServiceUtils.isServiceAlive(context.getApplicationContext(), serviceClassName);
LogUtils.d(TAG, String.format("handleBootCompleted: 服务运行状态:%b", isServiceAlive));
if (isServiceAlive) {
LogUtils.d(TAG, "handleBootCompleted: 服务已运行,无需重复启动");
return;
}
// 3. 按API版本启动服务适配前台服务要求
Intent serviceIntent = new Intent(context, ControlCenterService.class);
if (Build.VERSION.SDK_INT >= API_LEVEL_26) {
context.startForegroundService(serviceIntent);
LogUtils.d(TAG, "handleBootCompleted: 启动前台服务API >= 26");
} else {
context.startService(serviceIntent);
LogUtils.d(TAG, "handleBootCompleted: 启动普通服务API < 26");
}
LogUtils.d(TAG, "handleBootCompleted: 服务自启处理完成");
} catch (Exception e) {
LogUtils.e(TAG, "handleBootCompleted: 服务自启失败", e);
}
}
}

View File

@@ -5,101 +5,185 @@ import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.powerbell.App;
import cc.winboll.studio.powerbell.services.ControlCenterService;
import cc.winboll.studio.powerbell.utils.AppConfigUtils;
import cc.winboll.studio.powerbell.utils.ServiceUtils;
/**
* 电池提醒核心服务进程守护类
* 功能:监听主服务 {@link ControlCenterService} 存活状态,异常断开时自动重启并绑定
* 适配Java7 | API30 | 前台服务启动规则 | 服务绑定稳定性保障
* @Author ZhanGSKen<zhangsken@qq.com>
* @Describe 守护服务保障ControlCenterService持续运行
*/
public class AssistantService extends Service {
private final static String TAG = "AssistantService";
// ====================== 静态常量区(置顶归类,消除魔法值) ======================
private static final String TAG = "AssistantService";
// 服务返回策略常量
private static final int SERVICE_RETURN_STICKY = START_STICKY;
// 服务绑定标记常量
private static final int BIND_FLAG = Context.BIND_IMPORTANT;
// API版本常量适配前台服务启动要求
private static final int API_LEVEL_26 = Build.VERSION_CODES.O;
//MyBinder mMyBinder;
MyServiceConnection mMyServiceConnection;
volatile boolean mIsThreadAlive;
AppConfigUtils mAppConfigUtils;
// ====================== 成员变量区按功能分层volatile保证多线程可见性 ======================
private AppConfigUtils mAppConfigUtils;
private MyServiceConnection mMyServiceConnection;
private volatile boolean mIsThreadAlive;
@Override
public IBinder onBind(Intent intent) {
//return mMyBinder;
return null;
}
@Override
public void onCreate() {
//LogUtils.d(TAG, "onCreate");
super.onCreate();
mAppConfigUtils = App.getAppConfigUtils(this);
//mMyBinder = new MyBinder();
if (mMyServiceConnection == null) {
mMyServiceConnection = new MyServiceConnection();
}
// 设置运行参数
mIsThreadAlive = false;
run();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//LogUtils.d(TAG, "call onStartCommand(...)");
run();
return START_STICKY;
}
/*class MyBinder extends IMyAidlInterface.Stub {
@Override
public String getServiceName() {
return AssistantService.class.getSimpleName();
}
}*/
@Override
public void onDestroy() {
//LogUtils.d(TAG, "onDestroy");
mIsThreadAlive = false;
super.onDestroy();
}
// 运行服务内容
//
void run() {
//LogUtils.d(TAG, "run");
if (mAppConfigUtils.getIsEnableService()) {
if (mIsThreadAlive == false) {
// 设置运行状态
mIsThreadAlive = true;
// 唤醒和绑定主进程
wakeupAndBindMain();
}
}
}
// 唤醒和绑定主进程
//
void wakeupAndBindMain() {
if (ServiceUtils.isServiceAlive(getApplicationContext(), ControlCenterService.class.getName()) == false) {
//LogUtils.d(TAG, "wakeupAndBindMain() Wakeup... ControlCenterService");
startForegroundService(new Intent(AssistantService.this, ControlCenterService.class));
}
//LogUtils.d(TAG, "wakeupAndBindMain() Bind... ControlCenterService");
bindService(new Intent(AssistantService.this, ControlCenterService.class), mMyServiceConnection, Context.BIND_IMPORTANT);
}
// 主进程与守护进程连接时需要用到此类
//
class MyServiceConnection implements ServiceConnection {
// ====================== 内部类(服务连接状态监听,前置定义便于引用) ======================
/**
* 服务连接状态监听器
* 主服务连接成功时记录状态,断开时自动重连
*/
private class MyServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//LogUtils.d(TAG, "call onServiceConnected(...)");
String className = name != null ? name.getClassName() : "null";
LogUtils.d(TAG, String.format("onServiceConnected: 主服务连接成功 | 组件名=%s | Binder=%s", className, service));
}
@Override
public void onServiceDisconnected(ComponentName name) {
//LogUtils.d(TAG, "call onServiceDisconnected(...)");
if (mAppConfigUtils.getIsEnableService()) {
String className = name != null ? name.getClassName() : "null";
LogUtils.d(TAG, String.format("onServiceDisconnected: 主服务连接断开 | 组件名=%s", className));
// 主服务断开且配置启用时,重新唤醒绑定
if (mAppConfigUtils != null && mAppConfigUtils.isServiceEnabled()) {
LogUtils.d(TAG, "onServiceDisconnected: 配置启用,尝试重新唤醒并绑定主服务");
wakeupAndBindMain();
}
}
}
// ====================== 服务生命周期方法按执行顺序排列onCreate→onStartCommand→onBind→onDestroy ======================
@Override
public void onCreate() {
super.onCreate();
LogUtils.d(TAG, String.format("onCreate: 守护服务启动 | 进程ID=%d", android.os.Process.myPid()));
// 初始化配置工具类,添加空指针防护
mAppConfigUtils = App.getAppConfigUtils(this);
if (mAppConfigUtils == null) {
LogUtils.e(TAG, "onCreate: AppConfigUtils初始化失败守护服务无法工作");
stopSelf();
return;
}
// 初始化服务连接对象
if (mMyServiceConnection == null) {
mMyServiceConnection = new MyServiceConnection();
LogUtils.d(TAG, "onCreate: ServiceConnection初始化完成");
}
// 初始化运行状态,执行核心守护逻辑
mIsThreadAlive = false;
run();
LogUtils.d(TAG, String.format("onCreate: 守护服务初始化完成 | 服务启用状态=%b", mAppConfigUtils.isServiceEnabled()));
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
LogUtils.d(TAG, String.format("onStartCommand: 守护服务触发重启 | flags=%d | startId=%d", flags, startId));
// 配置工具类为空时,直接返回非粘性策略
if (mAppConfigUtils == null) {
LogUtils.e(TAG, "onStartCommand: AppConfigUtils未初始化终止服务");
stopSelf();
return START_NOT_STICKY;
}
run();
int returnFlag = mAppConfigUtils.isServiceEnabled() ? SERVICE_RETURN_STICKY : super.onStartCommand(intent, flags, startId);
LogUtils.d(TAG, String.format("onStartCommand: 处理完成 | 返回策略=%s", returnFlag == SERVICE_RETURN_STICKY ? "START_STICKY" : "DEFAULT"));
return returnFlag;
}
@Override
public IBinder onBind(Intent intent) {
LogUtils.d(TAG, String.format("onBind: 服务绑定请求 | intent=%s", intent));
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
LogUtils.d(TAG, "onDestroy: 守护服务销毁流程启动");
// 重置运行状态,终止守护逻辑
mIsThreadAlive = false;
// 解绑主服务,添加异常捕获防止重复解绑崩溃
unbindMainService();
// 置空工具类引用帮助GC回收
mAppConfigUtils = null;
LogUtils.d(TAG, "onDestroy: 守护服务销毁完成");
}
// ====================== 核心业务逻辑(守护主服务存活) ======================
/**
* 执行守护逻辑:检查主服务状态,按需唤醒并绑定
* 前置条件mAppConfigUtils 必须初始化完成
*/
private void run() {
boolean isServiceEnabled = mAppConfigUtils.isServiceEnabled();
LogUtils.d(TAG, String.format("run: 执行守护逻辑 | 配置启用=%b | 线程存活=%b", isServiceEnabled, mIsThreadAlive));
if (isServiceEnabled) {
if (!mIsThreadAlive) {
mIsThreadAlive = true;
wakeupAndBindMain();
}
} else {
LogUtils.d(TAG, "run: 服务未启用,跳过守护逻辑");
// 服务未启用时,重置线程状态
mIsThreadAlive = false;
}
}
/**
* 唤醒主服务并建立绑定,确保主服务持续运行
* 适配 API26+ 前台服务启动规则,避免系统限制导致启动失败
*/
private void wakeupAndBindMain() {
// 检查主服务存活状态
String mainServiceName = ControlCenterService.class.getName();
boolean isMainServiceAlive = ServiceUtils.isServiceAlive(getApplicationContext(), mainServiceName);
LogUtils.d(TAG, String.format("wakeupAndBindMain: 主服务存活状态=%b", isMainServiceAlive));
// 主服务未存活时按需启动区分API版本
if (!isMainServiceAlive) {
Intent mainServiceIntent = new Intent(AssistantService.this, ControlCenterService.class);
if (Build.VERSION.SDK_INT >= API_LEVEL_26) {
startForegroundService(mainServiceIntent);
LogUtils.d(TAG, "wakeupAndBindMain: API26+ 以前台服务方式启动主服务");
} else {
startService(mainServiceIntent);
LogUtils.d(TAG, "wakeupAndBindMain: 以普通服务方式启动主服务");
}
}
// 绑定主服务,监听连接状态,添加结果日志
Intent bindIntent = new Intent(AssistantService.this, ControlCenterService.class);
boolean bindResult = bindService(bindIntent, mMyServiceConnection, BIND_FLAG);
LogUtils.d(TAG, String.format("wakeupAndBindMain: 绑定主服务结果=%b | 绑定标记=BIND_IMPORTANT", bindResult));
}
// ====================== 辅助工具方法(拆分独立逻辑,提高可维护性) ======================
/**
* 解绑主服务,包含异常捕获与状态日志
*/
private void unbindMainService() {
if (mMyServiceConnection != null) {
try {
unbindService(mMyServiceConnection);
LogUtils.d(TAG, "unbindMainService: 已成功解绑ControlCenterService");
} catch (IllegalArgumentException e) {
LogUtils.w(TAG, String.format("unbindMainService: 解绑服务失败,服务未绑定 | %s", e.getMessage()));
}
mMyServiceConnection = null;
}
}
}

View File

@@ -1,314 +1,506 @@
package cc.winboll.studio.powerbell.services;
/*
* PowerBy : ZhanGSKen(ZhangShaojian2018@163.com)
* 参考:
* 进程保活-双进程守护的正确姿势
* https://blog.csdn.net/sinat_35159441/article/details/75267380
* Android Service之onStartCommand方法研究
* https://blog.csdn.net/cyp331203/article/details/38920491
*/
import android.app.Notification;
import android.app.ActivityManager;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.net.Uri;
import android.os.Build;
import android.os.IBinder;
import android.os.Looper;
import android.widget.RemoteViews;
import android.os.PowerManager;
import android.provider.Settings;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.ToastUtils;
import cc.winboll.studio.powerbell.App;
import cc.winboll.studio.powerbell.MainActivity;
import cc.winboll.studio.powerbell.R;
import cc.winboll.studio.powerbell.beans.AppConfigBean;
import cc.winboll.studio.powerbell.beans.NotificationMessage;
import cc.winboll.studio.powerbell.handlers.ControlCenterServiceHandler;
import cc.winboll.studio.powerbell.models.AppConfigBean;
import cc.winboll.studio.powerbell.models.ControlCenterServiceBean;
import cc.winboll.studio.powerbell.models.NotificationMessage;
import cc.winboll.studio.powerbell.receivers.ControlCenterServiceReceiver;
import cc.winboll.studio.powerbell.services.AssistantService;
import cc.winboll.studio.powerbell.threads.RemindThread;
import cc.winboll.studio.powerbell.utils.AppCacheUtils;
import cc.winboll.studio.powerbell.utils.AppConfigUtils;
import cc.winboll.studio.powerbell.utils.NotificationHelper;
import cc.winboll.studio.powerbell.utils.ServiceUtils;
import cc.winboll.studio.powerbell.utils.StringUtils;
import cc.winboll.studio.powerbell.utils.NotificationManagerUtils;
import java.util.List;
/**
* 电池提醒核心服务
* 功能:管理前台服务生命周期、控制提醒线程启停、处理配置更新
* 适配Java7 | API30 | 前台服务超时防护 | 电池优化忽略引导
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
* @Describe 核心服务:实现电池监测、提醒控制与前台服务保活
*/
public class ControlCenterService extends Service {
// ====================== 静态常量区(置顶归类,消除魔法值) ======================
public static final String TAG = "ControlCenterService";
// 线程与服务常量
private static final long THREAD_STOP_TIMEOUT = 1000L;
private static final int SERVICE_RETURN_STICKY = START_STICKY;
private static final int RUNNING_SERVICE_LIST_LIMIT = 100;
// 默认配置常量
private static final int DEFAULT_CHARGE_REMINDER_VALUE = 80;
private static final int DEFAULT_USAGE_REMINDER_VALUE = 20;
private static final int DEFAULT_BATTERY_DETECT_INTERVAL = 1000;
// API版本常量
private static final int API_LEVEL_26 = Build.VERSION_CODES.O;
private static final int API_LEVEL_30 = Build.VERSION_CODES.R;
private static final int API_LEVEL_23 = Build.VERSION_CODES.M;
public static final int MSG_UPDATE_STATUS = 0;
// ====================== 静态状态标记volatile保证多线程可见性 ======================
private static volatile boolean isServiceRunning = false;
private static volatile boolean mIsDestroyed = true;
static ControlCenterService _mControlCenterService;
volatile boolean isServiceRunning;
AppConfigUtils mAppConfigUtils;
AppCacheUtils mAppCacheUtils;
// 前台服务通知工具
NotificationHelper mNotificationHelper;
Notification notification;
RemindThread mRemindThread;
ControlCenterServiceHandler mControlCenterServiceHandler;
MyServiceConnection mMyServiceConnection;
ControlCenterServiceReceiver mControlCenterServiceReceiver;
ControlCenterServiceReceiver mControlCenterServiceReceiverLocalBroadcast;
@Override
public IBinder onBind(Intent intent) {
return null;
}
public RemindThread getRemindThread() {
return mRemindThread;
}
// ====================== 成员变量区(按功能分层:配置→核心组件→通知相关) ======================
// 服务控制配置
private ControlCenterServiceBean mServiceControlBean;
private AppConfigBean mCurrentConfigBean;
// 业务核心组件
private ControlCenterServiceHandler mServiceHandler;
private ControlCenterServiceReceiver mControlCenterServiceReceiver;
// 通知相关
private NotificationManagerUtils mNotificationManager;
private NotificationMessage mForegroundNotifyMsg;
// ====================== 服务生命周期方法按执行顺序onCreate→onStartCommand→onBind→onDestroy ======================
@Override
public void onCreate() {
super.onCreate();
_mControlCenterService = ControlCenterService.this;
isServiceRunning = false;
mAppConfigUtils = App.getAppConfigUtils(this);
mAppCacheUtils = App.getAppCacheUtils(this);
mNotificationHelper = new NotificationHelper(ControlCenterService.this);
if (mMyServiceConnection == null) {
mMyServiceConnection = new MyServiceConnection();
}
mControlCenterServiceHandler = new ControlCenterServiceHandler(this);
// 运行服务内容
run();
LogUtils.d(TAG, String.format("onCreate() 执行 | 线程=%s | 进程ID=%d", Thread.currentThread().getName(), android.os.Process.myPid()));
runCoreServiceLogic();
boolean serviceEnabled = mServiceControlBean != null && mServiceControlBean.isEnableService();
LogUtils.d(TAG, String.format("onCreate() 完成 | 前台状态=%b | 服务启用=%b", isServiceRunning, serviceEnabled));
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 运行服务内容
run();
return (mAppConfigUtils.getIsEnableService()) ? START_STICKY : super.onStartCommand(intent, flags, startId);
String action = intent != null ? intent.getAction() : "null";
LogUtils.d(TAG, String.format("onStartCommand() 执行 | startId=%d | action=%s", startId, action));
loadLatestServiceControlConfig();
runCoreServiceLogic();
int returnFlag = (mServiceControlBean != null && mServiceControlBean.isEnableService())
? SERVICE_RETURN_STICKY
: super.onStartCommand(intent, flags, startId);
LogUtils.d(TAG, String.format("onStartCommand() 完成 | 返回策略=%s", returnFlag == SERVICE_RETURN_STICKY ? "START_STICKY" : "DEFAULT"));
return returnFlag;
}
// 运行服务内容
//
void run() {
if (mAppConfigUtils.getIsEnableService() && isServiceRunning == false) {
LogUtils.d(TAG, "run");
isServiceRunning = true;
// 唤醒守护进程
wakeupAndBindAssistant();
// 显示前台通知栏
// 在Service中
NotificationHelper helper = new NotificationHelper(this);
Intent intent = new Intent(this, MainActivity.class);
notification = helper.showForegroundNotification(intent, getString(R.string.app_name), "Service Running, Click to open app");
startForeground(NotificationHelper.FOREGROUND_NOTIFICATION_ID, notification);
// NotificationMessage notificationMessage=createNotificationMessage();
// //Toast.makeText(getApplication(), "", Toast.LENGTH_SHORT).show();
// mNotificationUtils.createForegroundNotification(this, notificationMessage);
// mNotificationUtils.createRemindNotification(this, notificationMessage);
if (mControlCenterServiceReceiver == null) {
// 注册广播接收器
mControlCenterServiceReceiver = new ControlCenterServiceReceiver(this);
mControlCenterServiceReceiver.registerAction(this);
}
new Handler(Looper.getMainLooper()).postDelayed(new Runnable(){
@Override
public void run() {
startRemindThread(mAppConfigUtils.mAppConfigBean);
ToastUtils.show("Service Is Start.");
LogUtils.i(TAG, "Service Is Start.");
}
}, 2000);
}
}
String getValuesString() {
String szReturn = "Usege: ";
szReturn += mAppConfigUtils.getIsEnableUsegeReminder() ? Integer.toString(mAppConfigUtils.getUsegeReminderValue()) : "?";
szReturn += "% Charge: ";
szReturn += mAppConfigUtils.getIsEnableChargeReminder() ? Integer.toString(mAppConfigUtils.getChargeReminderValue()) : "?";
szReturn += "%\nCurrent: " + Integer.toString(mAppConfigUtils.getCurrentValue()) + "%";
return szReturn;
}
NotificationMessage createNotificationMessage() {
String szTitle = ((App)getApplication()).getString(R.string.app_name);
String szContent = getValuesString() + " {?} " + StringUtils.formatPCMListString(mAppCacheUtils.getArrayListBatteryInfo());
return new NotificationMessage(szTitle, szContent);
}
// 更新前台通知
//
public void updateServiceNotification() {
//mNotificationUtils.updateForegroundNotification(ControlCenterService.this, createNotificationMessage());
}
// 更新前台通知
//
public void updateServiceNotification(NotificationMessage notificationMessage) {
//mNotificationUtils.updateForegroundNotification(ControlCenterService.this, notificationMessage);
}
// 更新前台通知
//
public void updateRemindNotification(NotificationMessage notificationMessage) {
//mNotificationUtils.updateRemindNotification(ControlCenterService.this, notificationMessage);
}
// 唤醒和绑定守护进程
//
void wakeupAndBindAssistant() {
if (ServiceUtils.isServiceAlive(getApplicationContext(), AssistantService.class.getName()) == false) {
startService(new Intent(ControlCenterService.this, AssistantService.class));
//LogUtils.d(TAG, "call wakeupAndBindAssistant() : Binding... AssistantService");
bindService(new Intent(ControlCenterService.this, AssistantService.class), mMyServiceConnection, Context.BIND_IMPORTANT);
}
}
// 开启提醒铃声线程
//
public void startRemindThread(AppConfigBean appConfigBean) {
//LogUtils.d(TAG, "startRemindThread");
if (mRemindThread == null) {
mRemindThread = new RemindThread(this, mControlCenterServiceHandler);
} else {
if (mRemindThread.isExist() == true) {
mRemindThread = new RemindThread(this, mControlCenterServiceHandler);
} else {
// 提醒进程正在进行中就更新状态后退出
mRemindThread.setChargeReminderValue(appConfigBean.getChargeReminderValue());
mRemindThread.setUsegeReminderValue(appConfigBean.getUsegeReminderValue());
mRemindThread.setIsEnableChargeReminder(appConfigBean.isEnableChargeReminder());
mRemindThread.setIsEnableUsegeReminder(appConfigBean.isEnableUsegeReminder());
mRemindThread.setSleepTime(appConfigBean.getReminderIntervalTime());
mRemindThread.setIsCharging(appConfigBean.isCharging());
mRemindThread.setQuantityOfElectricity(appConfigBean.getCurrentValue());
//LogUtils.d(TAG, "mRemindThread update.");
return;
}
}
mRemindThread.setChargeReminderValue(appConfigBean.getChargeReminderValue());
mRemindThread.setUsegeReminderValue(appConfigBean.getUsegeReminderValue());
mRemindThread.setSleepTime(appConfigBean.getReminderIntervalTime());
mRemindThread.setIsCharging(appConfigBean.isCharging());
mRemindThread.setQuantityOfElectricity(appConfigBean.getCurrentValue());
mRemindThread.setIsEnableChargeReminder(appConfigBean.isEnableChargeReminder());
mRemindThread.setIsEnableUsegeReminder(appConfigBean.isEnableUsegeReminder());
mRemindThread.start();
//LogUtils.d(TAG, "mRemindThread.start()");
}
public void stopRemindThread() {
if (mRemindThread != null) {
mRemindThread.setIsExist(true);
mRemindThread = null;
}
@Override
public IBinder onBind(Intent intent) {
LogUtils.d(TAG, String.format("onBind() 执行 | intent=%s", intent));
return null;
}
@Override
public void onDestroy() {
//LogUtils.d(TAG, "onDestroy");
mAppConfigUtils.loadAppConfigBean();
if (mAppConfigUtils.getIsEnableService() == false) {
// 设置运行状态
isServiceRunning = false;
// 停止守护进程
Intent intent = new Intent(this, AssistantService.class);
stopService(intent);
// 停止Receiver
if (mControlCenterServiceReceiver != null) {
unregisterReceiver(mControlCenterServiceReceiver);
mControlCenterServiceReceiver = null;
LogUtils.d(TAG, "onDestroy() 执行:服务销毁流程启动");
super.onDestroy();
// 资源释放顺序:前台服务 → 线程 → 广播接收器 → Handler → 通知 → 引用(避免内存泄漏)
stopForegroundService();
RemindThread.stopRemindThread();
releaseBroadcastReceiver();
destroyHandler();
releaseNotificationResource();
clearAllReferences();
// 状态重置
mCurrentConfigBean = null;
mForegroundNotifyMsg = null;
mServiceHandler = null;
isServiceRunning = false;
mIsDestroyed = true;
LogUtils.d(TAG, "onDestroy() 完成:服务销毁完成");
}
// ====================== 核心业务逻辑(独立抽取,统一调用) ======================
/**
* 服务核心运行逻辑在onCreate/onStartCommand复用
* 避免重复初始化,保证前台服务优先启动
*/
private synchronized void runCoreServiceLogic() {
LogUtils.d(TAG, "runCoreServiceLogic() 执行");
loadLatestServiceControlConfig();
boolean serviceEnabled = mServiceControlBean != null && mServiceControlBean.isEnableService();
LogUtils.d(TAG, String.format("runCoreServiceLogic() | 服务启用=%b | 已运行=%b | 已销毁=%b", serviceEnabled, isServiceRunning, mIsDestroyed));
if (serviceEnabled && !isServiceRunning) {
isServiceRunning = true;
mIsDestroyed = false;
if (initForegroundNotificationImmediately()) {
loadDefaultConfig();
initServiceBusinessLogic();
LogUtils.d(TAG, "runCoreServiceLogic() | 核心组件初始化成功");
} else {
LogUtils.e(TAG, "runCoreServiceLogic() | 前台通知初始化失败,终止业务");
stopForegroundService();
isServiceRunning = false;
}
// 停止前台通知栏
} else {
LogUtils.d(TAG, "runCoreServiceLogic() | 无需执行核心逻辑");
}
}
// ====================== 前台通知管理优先执行防止API26+前台服务5秒超时 ======================
/**
* 立即初始化前台通知防止API26+前台服务超时异常
* @return true=成功 false=失败
*/
private boolean initForegroundNotificationImmediately() {
LogUtils.d(TAG, "initForegroundNotificationImmediately() 执行");
try {
if (mNotificationManager == null) {
mNotificationManager = new NotificationManagerUtils(this);
LogUtils.d(TAG, "initForegroundNotificationImmediately() | 通知工具类初始化完成");
}
if (mForegroundNotifyMsg == null) {
mForegroundNotifyMsg = new NotificationMessage();
mForegroundNotifyMsg.setTitle("电池监测服务");
mForegroundNotifyMsg.setContent("后台运行中");
mForegroundNotifyMsg.setRemindMSG("service_running");
LogUtils.d(TAG, "initForegroundNotificationImmediately() | 通知消息构建完成");
}
mNotificationManager.startForegroundServiceNotify(this, mForegroundNotifyMsg);
ToastUtils.show("电池监测服务已启动");
LogUtils.d(TAG, String.format("initForegroundNotificationImmediately() | 前台通知发送成功 | ID=%d", NotificationManagerUtils.NOTIFY_ID_FOREGROUND_SERVICE));
return true;
} catch (Exception e) {
LogUtils.e(TAG, "initForegroundNotificationImmediately() | 通知初始化异常", e);
return false;
}
}
/**
* 停止前台服务并取消通知
*/
private void stopForegroundService() {
LogUtils.d(TAG, "stopForegroundService() 执行");
try {
stopForeground(true);
// 停止消息提醒进程
stopRemindThread();
super.onDestroy();
//LogUtils.d(TAG, "onDestroy done");
LogUtils.d(TAG, "stopForegroundService() | 前台服务已停止,通知已取消");
} catch (Exception e) {
LogUtils.e(TAG, "stopForegroundService() | 停止异常", e);
}
}
// 主进程与守护进程连接时需要用到此类
//
private class MyServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//LogUtils.d(TAG, "call onServiceConnected(...)");
}
@Override
public void onServiceDisconnected(ComponentName name) {
//LogUtils.d(TAG, "call onServiceConnected(...)");
if (mAppConfigUtils.getIsEnableService()) {
// 唤醒守护进程
wakeupAndBindAssistant();
}
// ====================== 配置管理(本地持久化+内存同步) ======================
/**
* 加载本地最新服务控制配置
*/
private void loadLatestServiceControlConfig() {
LogUtils.d(TAG, "loadLatestServiceControlConfig() 执行");
ControlCenterServiceBean latestBean = ControlCenterServiceBean.loadBean(this, ControlCenterServiceBean.class);
if (latestBean != null) {
mServiceControlBean = latestBean;
LogUtils.d(TAG, String.format("loadLatestServiceControlConfig() | 配置读取成功 | 启用=%b", mServiceControlBean.isEnableService()));
} else {
LogUtils.w(TAG, "loadLatestServiceControlConfig() | 本地无配置,沿用内存配置");
}
}
public void appenRemindMSG(String szRemindMSG) {
String msg = "";
for (int i = 0; i < 20; i++) {
msg += szRemindMSG;
/**
* 加载默认业务配置(首次启动兜底)
*/
private void loadDefaultConfig() {
LogUtils.d(TAG, "loadDefaultConfig() 执行");
if (mCurrentConfigBean == null) {
mCurrentConfigBean = new AppConfigBean();
mCurrentConfigBean.setEnableChargeReminder(true);
mCurrentConfigBean.setChargeReminderValue(DEFAULT_CHARGE_REMINDER_VALUE);
mCurrentConfigBean.setEnableUsageReminder(true);
mCurrentConfigBean.setUsageReminderValue(DEFAULT_USAGE_REMINDER_VALUE);
mCurrentConfigBean.setBatteryDetectInterval(DEFAULT_BATTERY_DETECT_INTERVAL);
LogUtils.d(TAG, String.format("loadDefaultConfig() | 默认配置加载完成 | 充电阈值=%d | 耗电阈值=%d | 检测间隔=%dms",
DEFAULT_CHARGE_REMINDER_VALUE, DEFAULT_USAGE_REMINDER_VALUE, DEFAULT_BATTERY_DETECT_INTERVAL));
} else {
LogUtils.d(TAG, "loadDefaultConfig() | 内存已有配置,无需加载");
}
NotificationHelper helper = new NotificationHelper(ControlCenterService.this);
Intent intent = new Intent(ControlCenterService.this, MainActivity.class);
helper.showTemporaryNotification(intent, getString(R.string.app_name), msg);
// NotificationMessage notificationMessage = createNotificationMessage();
// notificationMessage.setRemindMSG(szRemindMSG);
// //LogUtils.d(TAG, "notificationMessage : " + notificationMessage.getRemindMSG());
// updateRemindNotification(notificationMessage);
}
// 设置颜色背景
public static RemoteViews setLinearLayoutColor(RemoteViews remoteViews, int viewId, int color) {
remoteViews.setInt(viewId, "setBackgroundColor", color);
return remoteViews;
// ====================== 业务组件初始化与销毁Handler/广播/线程等) ======================
/**
* 初始化Handler等核心业务组件
*/
private void initServiceBusinessLogic() {
LogUtils.d(TAG, "initServiceBusinessLogic() 执行");
// 初始化Handler
if (mServiceHandler == null) {
mServiceHandler = new ControlCenterServiceHandler(this);
LogUtils.d(TAG, "initServiceBusinessLogic() | Handler初始化完成");
} else {
LogUtils.d(TAG, "initServiceBusinessLogic() | Handler已存在");
}
// 初始化广播接收器
if (mControlCenterServiceReceiver == null) {
mControlCenterServiceReceiver = new ControlCenterServiceReceiver(this);
mControlCenterServiceReceiver.registerAction(this);
LogUtils.d(TAG, "initServiceBusinessLogic() | 广播接收器初始化并注册完成");
} else {
LogUtils.d(TAG, "initServiceBusinessLogic() | 广播接收器已存在");
}
}
// 设置Drawable背景
public static RemoteViews setLinearLayoutDrawable(RemoteViews remoteViews, int viewId, int drawableRes) {
remoteViews.setInt(viewId, "setBackgroundResource", drawableRes);
return remoteViews;
/**
* 释放广播接收器资源
*/
private void releaseBroadcastReceiver() {
LogUtils.d(TAG, "releaseBroadcastReceiver() 执行");
if (mControlCenterServiceReceiver != null) {
mControlCenterServiceReceiver.release();
mControlCenterServiceReceiver = null;
LogUtils.d(TAG, "releaseBroadcastReceiver() | 广播接收器已释放");
} else {
LogUtils.w(TAG, "releaseBroadcastReceiver() | 广播接收器实例为空");
}
}
//
// 启动服务
//
/**
* 销毁Handler移除所有消息和回调防止内存泄漏
*/
private void destroyHandler() {
LogUtils.d(TAG, "destroyHandler() 执行");
if (mServiceHandler != null) {
mServiceHandler.removeCallbacksAndMessages(null);
mServiceHandler = null;
LogUtils.d(TAG, "destroyHandler() | Handler已销毁");
} else {
LogUtils.w(TAG, "destroyHandler() | Handler实例为空");
}
}
/**
* 释放通知工具类资源
*/
private void releaseNotificationResource() {
LogUtils.d(TAG, "releaseNotificationResource() 执行");
if (mNotificationManager != null) {
mNotificationManager.release();
mNotificationManager = null;
LogUtils.d(TAG, "releaseNotificationResource() | 通知资源已释放");
} else {
LogUtils.w(TAG, "releaseNotificationResource() | 通知工具类实例为空");
}
}
/**
* 置空所有引用,防止内存泄漏
*/
private void clearAllReferences() {
LogUtils.d(TAG, "clearAllReferences() 执行");
mForegroundNotifyMsg = null;
mServiceControlBean = null;
LogUtils.d(TAG, "clearAllReferences() | 引用清理完成");
}
// ====================== 外部调用接口(静态方法,提供服务启停/配置更新入口) ======================
/**
* 外部启动服务的统一入口
* @param context 上下文
*/
public static void startControlCenterService(Context context) {
LogUtils.d(TAG, String.format("startControlCenterService() 执行 | context=%s", context));
if (context == null) {
LogUtils.e(TAG, "startControlCenterService() | Context为空启动失败");
return;
}
// 保存启用配置
ControlCenterServiceBean controlBean = new ControlCenterServiceBean(true);
ControlCenterServiceBean.saveBean(context, controlBean);
LogUtils.d(TAG, "startControlCenterService() | 服务启用配置已保存");
// 启动服务区分API版本
Intent intent = new Intent(context, ControlCenterService.class);
context.startForegroundService(intent);
if (Build.VERSION.SDK_INT >= API_LEVEL_26) {
context.startForegroundService(intent);
LogUtils.d(TAG, "startControlCenterService() | 以前台服务方式启动API26+");
} else {
context.startService(intent);
LogUtils.d(TAG, "startControlCenterService() | 以普通服务方式启动API26-");
}
}
//
// 停止服务
//
/**
* 外部停止服务的统一入口
* @param context 上下文
*/
public static void stopControlCenterService(Context context) {
LogUtils.d(TAG, String.format("stopControlCenterService() 执行 | context=%s", context));
if (context == null) {
LogUtils.e(TAG, "stopControlCenterService() | Context为空停止失败");
return;
}
// 保存停用配置
ControlCenterServiceBean controlBean = new ControlCenterServiceBean(false);
ControlCenterServiceBean.saveBean(context, controlBean);
LogUtils.d(TAG, "stopControlCenterService() | 服务停用配置已保存");
// 停止服务
Intent intent = new Intent(context, ControlCenterService.class);
context.stopService(intent);
LogUtils.d(TAG, "stopControlCenterService() | 停止指令已发送");
}
public static void updateStatus(Context context, AppConfigBean appConfigBean) {
//LogUtils.d(TAG, "updateStatus");
// 创建一个Intent实例定义广播的内容
Intent intent = new Intent(ControlCenterServiceReceiver.ACTION_START_REMINDTHREAD);
// 设置可选的Action数据如额外信息
intent.putExtra("appConfigBean", appConfigBean);
// 发送广播
/**
* 外部更新配置并触发线程重启
* @param context 上下文
*/
public static void sendAppConfigStatusUpdateMessage(Context context) {
LogUtils.d(TAG, String.format("sendAppConfigStatusUpdateMessage() 执行 | context=%s", context));
if (context == null) {
LogUtils.e(TAG, "sendAppConfigStatusUpdateMessage() | 参数为空,更新失败");
return;
}
Intent intent = new Intent(ControlCenterServiceReceiver.ACTION_APPCONFIG_CHANGED);
intent.setPackage(context.getPackageName());
context.sendBroadcast(intent);
LogUtils.d(TAG, String.format("sendAppConfigStatusUpdateMessage() | 配置更新广播发送 | action=%s", ControlCenterServiceReceiver.ACTION_APPCONFIG_CHANGED));
}
/**
* 检查并引导用户开启忽略电池优化API23+
* @param context 上下文
*/
public static void checkIgnoreBatteryOptimization(Context context) {
LogUtils.d(TAG, String.format("checkIgnoreBatteryOptimization() 执行 | context=%s", context));
if (context == null || Build.VERSION.SDK_INT < API_LEVEL_23) {
LogUtils.w(TAG, "checkIgnoreBatteryOptimization() | 无需检查Context为空或API<23");
return;
}
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (powerManager == null) {
LogUtils.e(TAG, "checkIgnoreBatteryOptimization() | PowerManager获取失败");
return;
}
String packageName = context.getPackageName();
boolean isIgnored = powerManager.isIgnoringBatteryOptimizations(packageName);
LogUtils.d(TAG, String.format("checkIgnoreBatteryOptimization() | 已忽略电池优化=%b", isIgnored));
if (!isIgnored) {
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
LogUtils.d(TAG, String.format("checkIgnoreBatteryOptimization() | 已跳转至系统设置页 | package=%s", packageName));
}
}
/**
* 检查服务是否运行适配API30+
* @param context 上下文
* @param serviceClass 服务类
* @return true=运行中 false=未运行
*/
private static boolean isServiceRunning(Context context, Class<?> serviceClass) {
LogUtils.d(TAG, String.format("isServiceRunning() 执行 | context=%s | service=%s", context, serviceClass != null ? serviceClass.getName() : "null"));
if (context == null || serviceClass == null) {
LogUtils.e(TAG, "isServiceRunning() | 参数为空");
return false;
}
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (am == null) {
LogUtils.e(TAG, "isServiceRunning() | ActivityManager获取失败");
return false;
}
boolean isRunning = false;
String packageName = context.getPackageName();
String serviceClassName = serviceClass.getName();
if (Build.VERSION.SDK_INT >= API_LEVEL_30) {
// API30+ 禁止获取其他应用服务,通过进程状态判断
List<ActivityManager.RunningAppProcessInfo> processes = am.getRunningAppProcesses();
if (processes != null) {
for (ActivityManager.RunningAppProcessInfo process : processes) {
if (packageName.equals(process.processName) &&
(process.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE ||
process.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND)) {
isRunning = true;
break;
}
}
}
LogUtils.d(TAG, String.format("isServiceRunning() | API30+ 判断结果=%b", isRunning));
} else {
// API30- 通过服务列表判断
List<ActivityManager.RunningServiceInfo> services = am.getRunningServices(RUNNING_SERVICE_LIST_LIMIT);
if (services != null) {
for (ActivityManager.RunningServiceInfo info : services) {
if (serviceClassName.equals(info.service.getClassName())) {
isRunning = true;
break;
}
}
}
LogUtils.d(TAG, String.format("isServiceRunning() | API30- 判断结果=%b", isRunning));
}
// 兜底判断:配置启用状态
if (!isRunning) {
isRunning = isServiceStarted(context, serviceClass);
LogUtils.d(TAG, String.format("isServiceRunning() | 兜底判断结果=%b", isRunning));
}
return isRunning;
}
/**
* 兜底判断服务是否已启动(通过配置文件)
*/
private static boolean isServiceStarted(Context context, Class<?> serviceClass) {
LogUtils.d(TAG, "isServiceStarted() 执行");
try {
ControlCenterServiceBean controlBean = ControlCenterServiceBean.loadBean(context, ControlCenterServiceBean.class);
return controlBean != null && controlBean.isEnableService();
} catch (Exception e) {
LogUtils.e(TAG, "isServiceStarted() | 兜底判断异常", e);
return false;
}
}
// ====================== 业务方法(配置更新/电池状态回调) ======================
/**
* 接收外部配置更新,同步到提醒线程
* @param latestConfig 最新配置
*/
public void notifyAppConfigUpdate(AppConfigBean latestConfig) {
int chargeThreshold = latestConfig != null ? latestConfig.getChargeReminderValue() : -1;
int usageThreshold = latestConfig != null ? latestConfig.getUsageReminderValue() : -1;
LogUtils.d(TAG, String.format("notifyAppConfigUpdate() 执行 | 充电阈值=%d | 耗电阈值=%d", chargeThreshold, usageThreshold));
if (latestConfig != null && mServiceHandler != null) {
mCurrentConfigBean = latestConfig;
RemindThread.startRemindThreadWithAppConfig(this, mServiceHandler, latestConfig);
LogUtils.d(TAG, "notifyAppConfigUpdate() | 配置已同步到提醒线程");
} else {
LogUtils.e(TAG, String.format("notifyAppConfigUpdate() | 参数为空,同步失败 | latestConfig=%s | mServiceHandler=%s", latestConfig, mServiceHandler));
}
}
// ====================== Getter 方法按需开放避免冗余Setter ======================
public ControlCenterServiceBean getServiceControlBean() {
return mServiceControlBean;
}
public NotificationManagerUtils getNotificationManager() {
return mNotificationManager;
}
public NotificationMessage getForegroundNotifyMsg() {
return mForegroundNotifyMsg;
}
public AppConfigBean getCurrentConfigBean() {
return mCurrentConfigBean;
}
public boolean isDestroyed() {
return mIsDestroyed;
}
}

View File

@@ -0,0 +1,83 @@
package cc.winboll.studio.powerbell.services;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.powerbell.models.TTSSpeakTextBean;
import cc.winboll.studio.powerbell.utils.TextToSpeechUtils;
import java.util.ArrayList;
/**
* TTS 语音播放后台服务组件
* 适配Java7 语法规范 | Android API30 系统版本
* 功能后台承载TTS语音播放解耦页面生命周期避免页面销毁中断播放
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
* @Date 2025/12/29 19:12
*/
public class TTSPlayService extends Service {
// ====================================== 常量区 - 静态全局常量 置顶排序 ======================================
public static final String TAG = "TTSPlayService";
public static final String EXTRA_SPEAKDATA = "EXTRA_SPEAKDATA";
// ====================================== 对外公开静态快捷调用方法【新增核心】======================================
/**
* 公开静态方法一键启动TTS播放服务播放指定文本内容
* @param context 上下文对象
* @param speakText 需要播放的语音文本内容
*/
public static void startPlayTTS(Context context, String speakText) {
LogUtils.d(TAG, "【startPlayTTS】静态快捷调用方法 | 入参Context=" + context + " | 播放文本=" + speakText);
if (context != null && speakText != null && !speakText.isEmpty()) {
// 初始化播放数据集合
ArrayList<TTSSpeakTextBean> ttsBeanList = new ArrayList<>();
// 添加播放文本延迟时间为0无延迟立即播放
ttsBeanList.add(new TTSSpeakTextBean(0, speakText));
LogUtils.d(TAG, "【startPlayTTS】封装播放数据完成创建启动服务意图");
// 创建意图并封装序列化参数
Intent intent = new Intent(context, TTSPlayService.class);
intent.putExtra(EXTRA_SPEAKDATA, ttsBeanList);
// 启动当前服务
context.startService(intent);
LogUtils.d(TAG, "【startPlayTTS】已调用startServiceTTS播放服务启动成功");
} else {
LogUtils.d(TAG, "【startPlayTTS】上下文为空 或 播放文本为空/空字符串,跳过启动服务");
}
}
// ====================================== 生命周期方法 - 绑定服务 (无绑定逻辑) ======================================
@Override
public IBinder onBind(Intent intent) {
LogUtils.d(TAG, "【onBind】服务绑定方法调用入参Intent" + intent);
return null;
}
// ====================================== 生命周期方法 - 启动服务【核心方法】 ======================================
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
LogUtils.d(TAG, "【onStartCommand】服务启动方法调用 | 入参Intent" + intent + " | flags" + flags + " | startId" + startId);
// 解析播放数据并执行播放
if (intent != null) {
LogUtils.d(TAG, "【onStartCommand】Intent不为空开始解析序列化播放数据");
ArrayList<TTSSpeakTextBean> listTTSSpeakTextBean = (ArrayList<TTSSpeakTextBean>) intent.getSerializableExtra(EXTRA_SPEAKDATA);
if (listTTSSpeakTextBean != null && listTTSSpeakTextBean.size() > 0) {
LogUtils.d(TAG, "【onStartCommand】解析播放数据成功队列长度" + listTTSSpeakTextBean.size() + "调用TTS播放工具类");
TextToSpeechUtils.getInstance(this).speekTTSList(listTTSSpeakTextBean);
} else {
LogUtils.d(TAG, "【onStartCommand】播放数据为空/长度0跳过语音播放逻辑");
}
} else {
LogUtils.d(TAG, "【onStartCommand】Intent为空无播放数据可解析");
}
// 返回默认值,保持原服务启动策略不变
int result = super.onStartCommand(intent, flags, startId);
LogUtils.d(TAG, "【onStartCommand】方法执行完成返回值" + result);
return result;
}
}

View File

@@ -0,0 +1,164 @@
package cc.winboll.studio.powerbell.services;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.powerbell.models.AppConfigBean;
import cc.winboll.studio.powerbell.models.ThoughtfulServiceBean;
import cc.winboll.studio.powerbell.utils.AppConfigUtils;
/**
* 智能电池服务(充电/放电状态处理)
* 适配Java7 语法规范 | Android API30 系统版本
* 功能:接收充电/放电状态指令,根据不同状态执行对应业务任务
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
* @Date 2025/12/29 19:29
*/
public class ThoughtfulService extends Service {
// ====================================== 常量区 - 置顶排序 ======================================
public static final String TAG = "ThoughtfulService";
/** Intent传递 服务类型 的Key值 */
public static final String EXTRA_SERVICE_TYPE = "EXTRA_SERVICE_TYPE";
// ====================================== 枚举类 - 服务类型 充电/放电状态 ======================================
/**
* 服务执行类型枚举
* CHARGE_STATE : 充电状态服务
* DISCHARGE_STATE : 放电(耗电)状态服务
*/
public enum ServiceType {
CHARGE_STATE, //充电状态服务
DISCHARGE_STATE //放电状态服务
}
// ====================================== 对外公开静态启动函数【新增核心】入参Context + 枚举 ======================================
/**
* 公开静态方法:传入上下文+服务类型枚举,一键构建意图并启动当前服务
* @param context 上下文对象
* @param serviceType 服务类型枚举【充电/放电】
*/
public static void startServiceWithType(Context context, ServiceType serviceType) {
LogUtils.d(TAG, "【startServiceWithType】静态启动方法调用 | Context=" + context + " | ServiceType=" + (serviceType == null ? "null" : serviceType.name()));
ThoughtfulServiceBean thoughtfulServiceBean = ThoughtfulServiceBean.loadBean(context, ThoughtfulServiceBean.class);
if (thoughtfulServiceBean == null) {
thoughtfulServiceBean = new ThoughtfulServiceBean();
}
// 对应TTS服务提醒没有启用就退出
if((serviceType == ServiceType.CHARGE_STATE && !thoughtfulServiceBean.isEnableChargeTts())
||(serviceType == ServiceType.DISCHARGE_STATE && !thoughtfulServiceBean.isEnableUsePowerTts())){
return;
}
// 判空健壮性校验
if (context != null && serviceType != null) {
// 构建意图 + 封装枚举参数
Intent intent = new Intent(context, ThoughtfulService.class);
intent.putExtra(EXTRA_SERVICE_TYPE, serviceType);
// 启动服务
context.startService(intent);
LogUtils.d(TAG, "【startServiceWithType】服务启动成功执行[" + serviceType.name() + "]任务");
} else {
LogUtils.d(TAG, "【startServiceWithType】上下文为空 或 服务类型枚举为空,跳过启动服务");
}
}
// ====================================== 生命周期方法 - 绑定服务 (原逻辑保留) ======================================
@Override
public IBinder onBind(Intent intent) {
LogUtils.d(TAG, "【onBind】服务绑定方法调用入参Intent" + intent);
return null;
}
// ====================================== 生命周期方法 - 启动服务【核心逻辑】接收枚举+分支执行任务 ======================================
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
LogUtils.d(TAG, "【onStartCommand】服务启动方法调用 | intent=" + intent + " | flags=" + flags + " | startId=" + startId);
// 判断意图非空,解析服务类型参数
if (intent != null) {
LogUtils.d(TAG, "【onStartCommand】Intent不为空开始解析服务类型枚举参数");
// 获取传递的服务类型枚举
ServiceType serviceType = (ServiceType) intent.getSerializableExtra(EXTRA_SERVICE_TYPE);
// 根据服务类型,执行对应任务
if (serviceType != null) {
LogUtils.d(TAG, "【onStartCommand】解析到服务类型" + serviceType.name());
switch (serviceType) {
case CHARGE_STATE:
// 执行【充电状态】对应的业务任务
executeChargeStateTask();
break;
case DISCHARGE_STATE:
// 执行【放电状态】对应的业务任务
executeDischargeStateTask();
break;
default:
LogUtils.d(TAG, "【onStartCommand】未知的服务类型不执行任何任务");
break;
}
} else {
LogUtils.d(TAG, "【onStartCommand】未解析到有效服务类型参数参数为空");
}
} else {
LogUtils.d(TAG, "【onStartCommand】启动服务的Intent为空直接返回");
}
// 返回默认策略,与原生逻辑一致
int result = super.onStartCommand(intent, flags, startId);
LogUtils.d(TAG, "【onStartCommand】服务执行完成返回值" + result);
return result;
}
// ====================================== 私有业务方法 充电/放电 分任务执行 ======================================
/**
* 执行【充电状态】的业务任务
* 可在此方法内编写 充电时的逻辑(语音提醒/电量监控/弹窗等)
*/
private void executeChargeStateTask() {
LogUtils.d(TAG, "【executeChargeStateTask】执行【充电状态】业务任务 >>> ");
//ToastUtils.show("【executeChargeStateTask】执行【充电状态】业务任务 >>> ");
// TODO 此处添加充电状态需要执行的业务逻辑代码
// 加载最新配置
AppConfigBean latestConfig = AppConfigUtils.getInstance(this).loadAppConfig();
if (latestConfig == null) {
LogUtils.e(TAG, "handleNotifyAppConfigUpdate() 终止 | 最新配置为空");
return;
}
if (latestConfig.isEnableChargeReminder()) {
int nChargeReminderValue = latestConfig.getChargeReminderValue();
String szRemind = String.format("限量充电提醒已启用,限量值为百分之%d。", nChargeReminderValue);
szRemind = szRemind + szRemind + szRemind;
TTSPlayService.startPlayTTS(this, szRemind);
}
}
/**
* 执行【放电(耗电)状态】的业务任务
* 可在此方法内编写 放电时的逻辑(语音提醒/电量监控/弹窗等)
*/
private void executeDischargeStateTask() {
LogUtils.d(TAG, "【executeDischargeStateTask】执行【放电状态】业务任务 >>> ");
//ToastUtils.show("【executeDischargeStateTask】执行【放电状态】业务任务 >>> ");
// TODO 此处添加放电状态需要执行的业务逻辑代码
// 加载最新配置
AppConfigBean latestConfig = AppConfigUtils.getInstance(this).loadAppConfig();
if (latestConfig == null) {
LogUtils.e(TAG, "handleNotifyAppConfigUpdate() 终止 | 最新配置为空");
return;
}
if (latestConfig.isEnableUsageReminder()) {
int nUsageReminderValue = latestConfig.getUsageReminderValue();
String szRemind = String.format("电量不足提醒已启用,低电值为百分之%d。", nUsageReminderValue);
//szRemind = szRemind + szRemind + szRemind;
TTSPlayService.startPlayTTS(this, szRemind);
}
}
}

View File

@@ -3,37 +3,331 @@ package cc.winboll.studio.powerbell.threads;
import android.content.Context;
import android.os.Message;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.powerbell.App;
import cc.winboll.studio.powerbell.handlers.ControlCenterServiceHandler;
import cc.winboll.studio.powerbell.models.AppConfigBean;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
/**
* 电量通知提醒线程(多实例列表管理)
* 功能:管理充电/耗电提醒逻辑触发条件时向Handler发送提醒消息
* 适配Java7 | API30 | 内存泄漏防护 | 多线程状态同步
* 对外接口:{@link #startRemindThreadWithAppConfig(Context, ControlCenterServiceHandler, AppConfigBean)}、
* {@link #startRemindThreadWithBatteryInfo(Context, ControlCenterServiceHandler, boolean, int)}、{@link #stopRemindThread()}
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
* @Describe 电量通知提醒线程
*/
public class RemindThread extends Thread {
public static final String TAG = RemindThread.class.getSimpleName();
// ====================== 静态常量区(置顶归类,消除魔法值) ======================
public static final String TAG = "RemindThread";
Context mContext;
// 控制线程是否退出的标志
volatile boolean isExist = false;
// 消息提醒开关
static volatile boolean isReminding = false;
// 充电提醒开关
static volatile boolean isEnableUsegeReminder = false;
// 耗电提醒开关
static volatile boolean isEnableChargeReminder = false;
// 电量比较停顿时间
static volatile int sleepTime = 1000;
// 充电提醒电量
static volatile int chargeReminderValue = -1;
// 耗电提醒电量
static volatile int usegeReminderValue = -1;
// 当前电量
static volatile int quantityOfElectricity = -1;
// 是否正在充电
static volatile boolean isCharging = false;
// 服务Handler, 用于线程发送消息使用
WeakReference<ControlCenterServiceHandler> mwrControlCenterServiceHandler;
// 时间常量 (ms)
private static final int MIN_SLEEP_TIME = 2000;
private static final long THREAD_JOIN_TIMEOUT = 1000L;
// 状态常量
private static final int BATTERY_LEVEL_MIN = 0;
private static final int BATTERY_LEVEL_MAX = 100;
// 提醒类型常量
private static final String REMIND_TYPE_CHARGE = "+";
private static final String REMIND_TYPE_USAGE = "-";
// ====================== 静态成员(多实例列表管理) ======================
private static volatile ArrayList<RemindThread> sRemindThreadList;
// ====================== 成员变量区按功能分层volatile保证多线程可见性 ======================
// 并发安全锁(保护线程状态变更)
private final Object mRemindLock = new Object();
// 弱引用依赖防内存泄漏ApplicationContext 避免 Activity 引用)
private Context mContext;
private WeakReference<ControlCenterServiceHandler> mwrControlCenterServiceHandler;
// 线程状态标记volatile 确保多线程可见)
private volatile boolean isReminding;
public volatile boolean isExist;
// 业务配置参数volatile 确保配置变更实时生效)
private volatile boolean isEnableChargeReminder;
private volatile boolean isEnableUsageReminder;
private volatile long sleepTime;
private volatile int chargeReminderValue;
private volatile int usageReminderValue;
private volatile boolean isCharging;
// ====================== 私有构造器(禁止外部实例化) ======================
private RemindThread(Context context, ControlCenterServiceHandler handler) {
LogUtils.d(TAG, String.format("RemindThread() 构造器调用 | context=%s | handler=%s", context, handler));
this.mContext = context.getApplicationContext();
this.mwrControlCenterServiceHandler = new WeakReference<>(handler);
resetThreadStateInternal();
LogUtils.d(TAG, String.format("RemindThread() 构造完成 | threadId=%d | 初始状态重置成功", getId()));
}
// ====================== 对外公开静态接口(多实例列表管理) ======================
/**
* 启动提醒线程,同步最新配置
* 逻辑:停止所有旧线程 → 创建新线程 → 加入列表管理
* @param context 上下文(非空)
* @param handler 服务处理器(非空)
* @param config 应用配置Bean非空
* @return true: 启动成功false: 入参非法
*/
public static boolean startRemindThreadWithAppConfig(Context context, ControlCenterServiceHandler handler, AppConfigBean config) {
LogUtils.d(TAG, String.format("startRemindThreadWithAppConfig() 调用 | context=%s | handler=%s | config=%s", context, handler, config));
// 入参严格校验
if (context == null || handler == null || config == null) {
LogUtils.e(TAG, String.format("启动失败:入参为空 | context=%s | handler=%s | config=%s", context, handler, config));
return false;
}
// 初始化线程列表(双重校验锁)
if (sRemindThreadList == null) {
synchronized (RemindThread.class) {
if (sRemindThreadList == null) {
sRemindThreadList = new ArrayList<RemindThread>();
LogUtils.d(TAG, "线程列表初始化完成");
}
}
}
// 停止所有旧线程
stopAllOldThreadsInternal();
// 创建并启动新线程
RemindThread newRemindThread = new RemindThread(context, handler);
newRemindThread.setAppConfigBean(config);
newRemindThread.isExist = false;
newRemindThread.start();
sRemindThreadList.add(newRemindThread);
LogUtils.d(TAG, String.format("新线程启动成功 | threadId=%d | 列表大小=%d", newRemindThread.getId(), sRemindThreadList.size()));
return true;
}
/**
* 安全停止所有线程,清空列表
*/
public static void stopRemindThread() {
int listSize = sRemindThreadList != null ? sRemindThreadList.size() : 0;
LogUtils.d(TAG, String.format("stopRemindThread() 调用 | 列表存在=%b | 列表大小=%d", sRemindThreadList != null, listSize));
if (sRemindThreadList == null || sRemindThreadList.isEmpty()) {
LogUtils.w(TAG, "停止失败:线程列表为空");
return;
}
// 标记所有线程退出
for (RemindThread remindThread : sRemindThreadList) {
remindThread.isExist = true;
LogUtils.d(TAG, String.format("标记线程退出 | threadId=%d", remindThread.getId()));
}
// 清空列表
sRemindThreadList.clear();
LogUtils.d(TAG, "所有线程已标记退出,列表已清空");
}
// ====================== 私有静态辅助方法(多实例管理) ======================
/**
* 停止所有旧线程并清空列表
*/
private static void stopAllOldThreadsInternal() {
if (sRemindThreadList == null || sRemindThreadList.isEmpty()) {
return;
}
// 标记所有旧线程退出
for (RemindThread remindThread : sRemindThreadList) {
remindThread.isExist = true;
LogUtils.d(TAG, String.format("标记旧线程退出 | threadId=%d", remindThread.getId()));
}
// 清空旧线程列表
sRemindThreadList.clear();
LogUtils.d(TAG, "旧线程已全部标记退出,列表已清空");
}
// ====================== 线程核心运行逻辑 ======================
@Override
public void run() {
LogUtils.d(TAG, String.format("run() 执行 | threadId=%d | 状态=%s", getId(), getState()));
// 初始化提醒状态(加锁保护,避免多线程竞争)
synchronized (mRemindLock) {
if (isReminding) {
LogUtils.w(TAG, String.format("线程已在提醒状态,退出运行 | threadId=%d", getId()));
return;
}
isReminding = true;
}
// 核心电量检测循环
LogUtils.d(TAG, String.format("进入电量检测循环 | 休眠时间=%dms | threadId=%d", sleepTime, getId()));
while (!isExist) {
try {
// 快速退出判断
if (isExist) break;
// 电量有效性校验非0-100视为无效退出电量提醒线程
if (App.sQuantityOfElectricity < BATTERY_LEVEL_MIN || App.sQuantityOfElectricity > BATTERY_LEVEL_MAX) {
LogUtils.w(TAG, String.format("电量无效,退出电量提醒线程 | 当前电量=%d | threadId=%d", App.sQuantityOfElectricity, getId()));
break;
}
// 充电/耗电提醒触发逻辑
boolean chargeRemindTrigger = isCharging && isEnableChargeReminder && App.sQuantityOfElectricity >= chargeReminderValue;
boolean usageRemindTrigger = !isCharging && isEnableUsageReminder && App.sQuantityOfElectricity <= usageReminderValue;
if (chargeRemindTrigger) {
LogUtils.d(TAG, String.format("触发充电提醒 | 当前电量=%d ≥ 阈值=%d | threadId=%d", App.sQuantityOfElectricity, chargeReminderValue, getId()));
sendNotificationMessageInternal(REMIND_TYPE_CHARGE, App.sQuantityOfElectricity, isCharging);
} else if (usageRemindTrigger) {
LogUtils.d(TAG, String.format("触发耗电提醒 | 当前电量=%d ≤ 阈值=%d | threadId=%d", App.sQuantityOfElectricity, usageReminderValue, getId()));
sendNotificationMessageInternal(REMIND_TYPE_USAGE, App.sQuantityOfElectricity, isCharging);
} else {
LogUtils.d(TAG, String.format("未有合适类型提醒,退出提醒线程 | threadId=%d", getId()));
break;
}
// 安全休眠,保留中断标记
safeSleepInternal(sleepTime);
} catch (Exception e) {
LogUtils.e(TAG, String.format("循环运行异常,退出电量提醒线程 | 当前电量=%d | threadId=%d", App.sQuantityOfElectricity, getId()), e);
break;
}
}
// 循环退出,清理状态
cleanThreadStateInternal();
LogUtils.d(TAG, String.format("run() 结束 | threadId=%d", getId()));
}
// ====================== 内部业务辅助方法 ======================
/**
* 发送提醒消息到Handler弱引用避免内存泄漏
* @param type 提醒类型:+充电/-耗电
* @param battery 当前电量
* @param isCharging 充电状态
*/
private void sendNotificationMessageInternal(String type, int battery, boolean isCharging) {
LogUtils.d(TAG, String.format("sendNotificationMessageInternal() 调用 | 类型=%s | 电量=%d | isCharging=%b | threadId=%d", type, battery, isCharging, getId()));
// 前置状态校验
if (isExist || !isReminding) {
LogUtils.d(TAG, String.format("消息发送跳过:线程已退出或提醒关闭 | threadId=%d", getId()));
return;
}
// 获取弱引用的Handler校验有效性
ControlCenterServiceHandler handler = mwrControlCenterServiceHandler.get();
if (handler == null) {
LogUtils.w(TAG, String.format("消息发送失败Handler已被回收 | threadId=%d", getId()));
return;
}
// 构建并发送消息
Message message = Message.obtain(handler, ControlCenterServiceHandler.MSG_REMIND_TEXT);
message.obj = type;
message.arg1 = battery;
message.arg2 = isCharging ? 1 : 0;
try {
handler.sendMessage(message);
LogUtils.d(TAG, String.format("提醒消息发送成功 | 类型=%s | 电量=%d | threadId=%d", type, battery, getId()));
} catch (Exception e) {
LogUtils.e(TAG, String.format("消息发送异常 | threadId=%d", getId()), e);
// 异常时回收Message避免内存泄漏
if (message != null) {
message.recycle();
}
}
}
/**
* 安全休眠,响应线程中断
* @param millis 休眠时长(ms)
*/
private void safeSleepInternal(long millis) {
LogUtils.d(TAG, String.format("safeSleepInternal() 调用 | 休眠时长=%dms | threadId=%d", millis, getId()));
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LogUtils.w(TAG, String.format("休眠被中断,线程准备退出 | threadId=%d", getId()));
}
}
/**
* 重置线程初始状态(构造器专用)
*/
private void resetThreadStateInternal() {
LogUtils.d(TAG, String.format("resetThreadStateInternal() 调用 | threadId=%d", getId()));
// 状态标记初始化
isExist = false;
isReminding = false;
// 配置参数初始化
isEnableChargeReminder = false;
isEnableUsageReminder = false;
sleepTime = MIN_SLEEP_TIME;
chargeReminderValue = -1;
usageReminderValue = -1;
isCharging = false;
LogUtils.d(TAG, String.format("线程初始状态重置完成 | threadId=%d", getId()));
}
/**
* 清理线程运行状态(循环退出时调用)
*/
private void cleanThreadStateInternal() {
LogUtils.d(TAG, String.format("cleanThreadStateInternal() 调用 | threadId=%d", getId()));
isReminding = false;
isExist = true;
// 中断当前线程(如果存活)
if (isAlive()) {
interrupt();
LogUtils.d(TAG, String.format("线程已中断 | threadId=%d", getId()));
}
LogUtils.d(TAG, String.format("线程运行状态清理完成 | threadId=%d", getId()));
}
/**
* 同步应用配置,校验参数有效性
* @param config 应用配置Bean
*/
public void setAppConfigBean(AppConfigBean config) {
LogUtils.d(TAG, String.format("setAppConfigBean() 调用 | config=%s | threadId=%d", config, getId()));
if (config == null) {
LogUtils.e(TAG, String.format("配置同步失败配置Bean为空 | threadId=%d", getId()));
return;
}
// 配置参数同步 + 范围校验(确保参数合法)
isEnableChargeReminder = config.isEnableChargeReminder();
isEnableUsageReminder = config.isEnableUsageReminder();
chargeReminderValue = Math.min(Math.max(config.getChargeReminderValue(), BATTERY_LEVEL_MIN), BATTERY_LEVEL_MAX);
usageReminderValue = Math.min(Math.max(config.getUsageReminderValue(), BATTERY_LEVEL_MIN), BATTERY_LEVEL_MAX);
sleepTime = Math.max(config.getBatteryDetectInterval(), MIN_SLEEP_TIME);
// sQuantityOfElectricity = (config.getCurrentBatteryValue() >= BATTERY_LEVEL_MIN && config.getCurrentBatteryValue() <= BATTERY_LEVEL_MAX)
// ? config.getCurrentBatteryValue() : INVALID_BATTERY_VALUE;
isCharging = config.isCharging();
LogUtils.d(TAG, String.format("配置同步完成 | 休眠时间=%dms | 充电提醒=%b | 耗电提醒=%b | 当前电量=%d | 充电阈值=%d | 耗电阈值=%d | threadId=%d",
sleepTime, isEnableChargeReminder, isEnableUsageReminder, App.sQuantityOfElectricity, chargeReminderValue, usageReminderValue, getId()));
}
/**
* 判断线程是否处于运行状态
* @return true: 运行中false: 已停止
*/
private boolean isRunning() {
boolean running = !isExist && isAlive();
LogUtils.d(TAG, String.format("isRunning() 调用 | 运行中=%b | 退出标记=%b | 存活=%b | threadId=%d", running, isExist, isAlive(), getId()));
return running;
}
// ====================== Getter/Setter按需开放 ======================
public void setIsExist(boolean isExist) {
LogUtils.d(TAG, String.format("setIsExist() 调用 | isExist=%b | threadId=%d", isExist, getId()));
this.isExist = isExist;
}
@@ -41,157 +335,20 @@ public class RemindThread extends Thread {
return isExist;
}
public static void setIsReminding(boolean isReminding) {
RemindThread.isReminding = isReminding;
}
public static boolean isReminding() {
return isReminding;
}
public static void setIsEnableUsegeReminder(boolean isEnableUsegeReminder) {
RemindThread.isEnableUsegeReminder = isEnableUsegeReminder;
}
public static boolean isEnableUsegeReminder() {
return isEnableUsegeReminder;
}
public static void setIsEnableChargeReminder(boolean isEnableChargeReminder) {
RemindThread.isEnableChargeReminder = isEnableChargeReminder;
}
public static boolean isEnableChargeReminder() {
return isEnableChargeReminder;
}
public static void setSleepTime(int sleepTime) {
RemindThread.sleepTime = sleepTime;
}
public static int getSleepTime() {
return sleepTime;
}
public static void setChargeReminderValue(int chargeReminderValue) {
RemindThread.chargeReminderValue = chargeReminderValue;
}
public static int getChargeReminderValue() {
return chargeReminderValue;
}
public static void setUsegeReminderValue(int usegeReminderValue) {
RemindThread.usegeReminderValue = usegeReminderValue;
}
public static int getUsegeReminderValue() {
return usegeReminderValue;
}
public static void setQuantityOfElectricity(int quantityOfElectricity) {
RemindThread.quantityOfElectricity = quantityOfElectricity;
}
public static int getQuantityOfElectricity() {
return quantityOfElectricity;
}
public static void setIsCharging(boolean isCharging) {
RemindThread.isCharging = isCharging;
}
public static boolean isCharging() {
return isCharging;
}
// 发送消息给用户
//
void sendNotificationMessage(String sz) {
//LogUtils.d(TAG, "sz is " + sz);
Message message = Message.obtain();
message.what = ControlCenterServiceHandler.MSG_REMIND_TEXT;
//message.obj = new NotificationMessage(mContext.getString(R.string.app_name), sz);
message.obj = sz;
ControlCenterServiceHandler handler = mwrControlCenterServiceHandler.get();
if (isReminding && handler != null) {
handler.sendMessage(message);
}
}
public RemindThread(Context context, ControlCenterServiceHandler handler) {
mContext = context;
mwrControlCenterServiceHandler = new WeakReference<ControlCenterServiceHandler>(handler);
}
// ====================== 调试辅助方法 ======================
@Override
public void run() {
//LogUtils.d(TAG, "call run()");
if (isReminding == false) {
isReminding = true;
// 等待些许时间,等所有数据初始化完成再执行下面的程序
// 解决窗口移除后自动重启后会发送一个错误消息的问题
try {
Thread.sleep(500);
} catch (InterruptedException e) {}
// 发送提醒线程开始的参数设置
//sendMessageToUser(Integer.toString(_mnTheQuantityOfElectricity) + ">>>" + Integer.toString(_mnTargetNumber));
//ToastUtils.show("Service Is Start.");
//LogUtils.i(TAG, "Service Is Start.");
while (!isExist()) {
/*
LogUtils.d(TAG, "isCharging is " + Boolean.toString(isCharging));
LogUtils.d(TAG, "usegeReminderValue is " + Integer.toString(usegeReminderValue));
LogUtils.d(TAG, "quantityOfElectricity is " + Integer.toString(quantityOfElectricity));
LogUtils.d(TAG, "chargeReminderValue is " + Integer.toString(chargeReminderValue));
LogUtils.d(TAG, "isEnableChargeReminder is " + Boolean.toString(isEnableChargeReminder));
LogUtils.d(TAG, "isEnableUsegeReminder is " + Boolean.toString(isEnableUsegeReminder));
*/
try {
if (isCharging) {
if ((quantityOfElectricity >= chargeReminderValue)
&& (isEnableChargeReminder)) {
// 正在充电时电量大于指定电量发送提醒
sendNotificationMessage("+");
// 应用需要继续提醒,设置退出标志为否
setIsExist(false);
//sendNotificationMessage("I am ready! +");
} else {
// 设置退出标志,如果后续不需要继续提醒就退出当前进程,用于应用节能。
setIsExist(true);
isReminding = false;
return;
}
} else {
if ((quantityOfElectricity <= usegeReminderValue)
&& (isEnableUsegeReminder)) {
// 正在放电时电量小于指定电量发送提醒
sendNotificationMessage("-");
// 应用需要继续提醒,设置退出标志为否
setIsExist(false);
//sendNotificationMessage("I am ready! -");
} else {
// 设置退出标志,如果后续不需要继续提醒就退出当前进程,用于应用节能。
setIsExist(true);
isReminding = false;
return;
}
}
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
LogUtils.d(TAG, e, Thread.currentThread().getStackTrace());
}
}
//ToastUtils.show("Service Is Stop.");
//LogUtils.i(TAG, "Service Is Stop.");
isReminding = false;
}
public String toString() {
return "RemindThread{" +
"threadId=" + getId() +
", threadName='" + getName() + '\'' +
", isRunning=" + isRunning() +
", isReminding=" + isReminding +
", chargeThreshold=" + chargeReminderValue +
", usageThreshold=" + usageReminderValue +
", currentBattery=" + App.sQuantityOfElectricity +
", isCharging=" + isCharging +
", sleepTime=" + sleepTime + "ms" +
'}';
}
}

View File

@@ -1,48 +0,0 @@
package cc.winboll.studio.powerbell.unittest;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import cc.winboll.studio.libappbase.GlobalApplication;
import cc.winboll.studio.libappbase.ToastUtils;
import cc.winboll.studio.powerbell.R;
import android.widget.Button;
import cc.winboll.studio.powerbell.MainActivity;
import android.content.Intent;
/**
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2025/11/19 18:16
* @Describe BackgroundViewTestFragment
*/
public class BackgroundViewTestFragment extends Fragment {
public static final String TAG = "BackgroundViewTestFragment";
View mainView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//super.onCreateView(inflater, container, savedInstanceState);
// 非调试状态就结束本线程
if (!GlobalApplication.isDebugging()) {
Thread.currentThread().destroy();
}
mainView = inflater.inflate(R.layout.fragment_test_backgroundview, container, false);
((Button)mainView.findViewById(R.id.btn_main_activity)).setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
getActivity().startActivity(new Intent(getActivity(), MainActivity.class));
}
});
ToastUtils.show(String.format("%s onCreate", TAG));
return mainView;
}
}

View File

@@ -0,0 +1,285 @@
package cc.winboll.studio.powerbell.unittest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.ToastUtils;
import cc.winboll.studio.powerbell.MainActivity;
import cc.winboll.studio.powerbell.R;
import cc.winboll.studio.powerbell.models.BackgroundBean;
import cc.winboll.studio.powerbell.utils.BackgroundSourceUtils;
import cc.winboll.studio.powerbell.utils.FileUtils;
import cc.winboll.studio.powerbell.utils.ImageCropUtils;
import cc.winboll.studio.powerbell.utils.ImageUtils;
import cc.winboll.studio.powerbell.views.MemoryCachedBackgroundView;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
/**
* 单元测试页面2内存缓存背景视图专用
* 功能测试MemoryCachedBackgroundView加载、图片裁剪、双重刷新预览等功能
* 适配Java7 | API30 | 私有目录文件操作 | 无Uri冲突 | 内存缓存视图
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
* @Describe 单元测试页2验证带内存缓存的背景视图相关逻辑
*/
public class MainUnitTest2Activity extends AppCompatActivity {
// ====================== 静态常量区(置顶归类,消除魔法值) ======================
public static final String TAG = "MainUnitTest2Activity";
public static final int REQUEST_CROP_IMAGE = 0;
private static final String ASSETS_TEST_IMAGE_PATH = "unittest/unittest-miku.png";
private static final long FILE_MIN_SIZE = 100L;
private static final long DOUBLE_REFRESH_DELAY = 200L;
// ====================== 成员变量区按功能分层移除所有Uri相关 ======================
private MemoryCachedBackgroundView mMemoryCachedBackgroundView;
private LinearLayout mllBackgroundView;
private String mAppPrivateDirPath;
private File mPrivateTestImageFile;
private File mPrivateCropImageFile;
private BackgroundBean mPreviewBackgroundBean;
// ====================== 生命周期方法按执行顺序onCreate→onActivityResult ======================
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LogUtils.d(TAG, "=== 页面 onCreate 启动 ===");
initBaseParams();
initViewAndEvent();
copyAssetsTestImageToPrivateDir();
initBackgroundBean();
doubleRefreshPreview();
ToastUtils.show("单元测试页面2启动完成");
LogUtils.d(TAG, "=== 页面 onCreate 初始化结束 ===");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
LogUtils.d(TAG, String.format("=== onActivityResult 回调 | requestCode=%d | resultCode=%d ===", requestCode, resultCode));
if (requestCode == REQUEST_CROP_IMAGE) {
handleCropResult(resultCode);
}
}
// ====================== 初始化相关方法基础参数→视图→背景Bean ======================
/**
* 初始化基础参数:私有目录、测试文件
*/
private void initBaseParams() {
LogUtils.d(TAG, "initBaseParams初始化基础参数");
// 初始化私有目录无需权限无UID冲突
mAppPrivateDirPath = getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/PowerBellTest/";
File privateDir = new File(mAppPrivateDirPath);
if (!privateDir.exists()) {
boolean isDirCreated = privateDir.mkdirs();
LogUtils.d(TAG, String.format("initBaseParams创建私有目录 | 路径=%s | 结果=%b", mAppPrivateDirPath, isDirCreated));
}
// 初始化测试文件与裁剪文件无Uri
File refFile = new File(ASSETS_TEST_IMAGE_PATH);
String uniqueTestName = FileUtils.createUniqueFileName(refFile) + ".png";
String uniqueCropName = uniqueTestName.replace(".png", "_crop.png");
mPrivateTestImageFile = new File(mAppPrivateDirPath, uniqueTestName);
mPrivateCropImageFile = new File(mAppPrivateDirPath, uniqueCropName);
LogUtils.d(TAG, String.format("initBaseParams测试图路径=%s", mPrivateTestImageFile.getAbsolutePath()));
LogUtils.d(TAG, String.format("initBaseParams裁剪图路径=%s", mPrivateCropImageFile.getAbsolutePath()));
}
/**
* 初始化布局与控件事件(含单例视图创建)
*/
private void initViewAndEvent() {
LogUtils.d(TAG, "initViewAndEvent初始化布局与控件事件");
setContentView(R.layout.activity_mainunittest2);
mllBackgroundView = (LinearLayout) findViewById(R.id.ll_backgroundview);
// 创建MemoryCachedBackgroundView单例并添加到布局
int nCurrentPixelColor = BackgroundSourceUtils.getInstance(this).getCurrentBackgroundBean().getPixelColor();
mMemoryCachedBackgroundView = MemoryCachedBackgroundView.getLastInstance(this);
mllBackgroundView.addView(mMemoryCachedBackgroundView);
LogUtils.d(TAG, "initViewAndEvent内存缓存背景视图实例创建并添加完成");
// 跳转主页面按钮
Button btnMain = (Button) findViewById(R.id.btn_main_activity);
btnMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "initViewAndEvent点击按钮→跳转主页面");
startActivity(new Intent(MainUnitTest2Activity.this, MainActivity.class));
}
});
// 裁剪按钮直接用File路径启动无Uri
Button btnCrop = (Button) findViewById(R.id.btn_test_cropimage);
btnCrop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "initViewAndEvent点击按钮→启动裁剪File路径版");
ToastUtils.show("准备启动图片裁剪");
if (isFileValid(mPrivateTestImageFile)) {
startCropTestByFile();
} else {
ToastUtils.show("测试图片未准备好,重新拷贝");
copyAssetsTestImageToPrivateDir();
}
}
});
}
/**
* 初始化背景Bean
*/
private void initBackgroundBean() {
LogUtils.d(TAG, "initBackgroundBean初始化背景Bean");
mPreviewBackgroundBean = new BackgroundBean();
mPreviewBackgroundBean.setPixelColor(ImageUtils.getColorAccent(this));
mPreviewBackgroundBean.setBackgroundFileName(mPrivateTestImageFile.getName());
mPreviewBackgroundBean.setBackgroundFilePath(mPrivateTestImageFile.getAbsolutePath());
mPreviewBackgroundBean.setBackgroundScaledCompressFileName(mPrivateCropImageFile.getName());
mPreviewBackgroundBean.setBackgroundScaledCompressFilePath(mPrivateCropImageFile.getAbsolutePath());
mPreviewBackgroundBean.setIsUseBackgroundFile(true);
LogUtils.d(TAG, "initBackgroundBean背景Bean初始化完成");
}
// ====================== 核心业务方法(文件拷贝→裁剪→结果处理→预览刷新) ======================
/**
* 从assets拷贝图片到私有目录
*/
private void copyAssetsTestImageToPrivateDir() {
LogUtils.d(TAG, "copyAssetsTestImageToPrivateDir开始拷贝assets图片到私有目录");
if (isFileValid(mPrivateTestImageFile)) {
LogUtils.d(TAG, "copyAssetsTestImageToPrivateDir图片已存在无需拷贝");
return;
}
InputStream inputStream = null;
try {
inputStream = getAssets().open(ASSETS_TEST_IMAGE_PATH);
FileUtils.copyStreamToFile(inputStream, mPrivateTestImageFile);
LogUtils.d(TAG, String.format("copyAssetsTestImageToPrivateDir图片拷贝成功 | 大小=%d字节", mPrivateTestImageFile.length()));
} catch (IOException e) {
LogUtils.e(TAG, String.format("copyAssetsTestImageToPrivateDir图片拷贝失败 | %s", e.getMessage()), e);
ToastUtils.show("图片准备失败");
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
LogUtils.e(TAG, String.format("copyAssetsTestImageToPrivateDir关闭流失败 | %s", e.getMessage()));
}
}
}
}
/**
* 直接用File启动裁剪关键调用ImageCropUtils的File重载方法
*/
private void startCropTestByFile() {
LogUtils.d(TAG, String.format("startCropTestByFile启动裁剪 | 原图=%s", mPrivateTestImageFile.getAbsolutePath()));
// 确保输出目录存在
File cropParent = mPrivateCropImageFile.getParentFile();
if (!cropParent.exists()) {
boolean isDirCreated = cropParent.mkdirs();
LogUtils.d(TAG, String.format("startCropTestByFile创建裁剪目录 | 路径=%s | 结果=%b", cropParent.getAbsolutePath(), isDirCreated));
}
// 调用ImageCropUtils的File参数方法核心绕开Uri
ImageCropUtils.startImageCrop(
this,
mPrivateTestImageFile,
mPrivateCropImageFile,
0,
0,
true,
REQUEST_CROP_IMAGE
);
LogUtils.d(TAG, String.format("startCropTestByFile裁剪请求已发送 | 输出路径=%s", mPrivateCropImageFile.getAbsolutePath()));
ToastUtils.show("已启动图片裁剪");
}
/**
* 处理裁剪结果直接校验输出File
* @param resultCode 裁剪结果码
*/
private void handleCropResult(int resultCode) {
LogUtils.d(TAG, String.format("handleCropResult裁剪回调处理 | resultCode=%d", resultCode));
if (resultCode == RESULT_OK) {
if (isFileValid(mPrivateCropImageFile)) {
int nCurrentPixelColor = BackgroundSourceUtils.getInstance(this).getCurrentBackgroundBean().getPixelColor();
mMemoryCachedBackgroundView.loadImage(nCurrentPixelColor, mPrivateCropImageFile.getAbsolutePath(), true);
LogUtils.d(TAG, String.format("handleCropResult裁剪成功 | 加载裁剪图=%s", mPrivateCropImageFile.getAbsolutePath()));
ToastUtils.show("裁剪成功");
mPreviewBackgroundBean.setIsUseBackgroundScaledCompressFile(true);
doubleRefreshPreview();
} else {
LogUtils.e(TAG, "handleCropResult裁剪成功但输出文件无效");
ToastUtils.show("裁剪失败:输出文件无效");
}
} else if (resultCode == RESULT_CANCELED) {
LogUtils.d(TAG, "handleCropResult裁剪取消");
ToastUtils.show("裁剪已取消");
} else {
LogUtils.e(TAG, String.format("handleCropResult裁剪失败 | resultCode异常=%d", resultCode));
ToastUtils.show("裁剪失败");
}
}
/**
* 双重刷新预览,确保背景加载最新数据
*/
private void doubleRefreshPreview() {
LogUtils.d(TAG, "doubleRefreshPreview执行双重刷新预览");
// 第一重刷新
try {
mMemoryCachedBackgroundView.loadByBackgroundBean(mPreviewBackgroundBean, true);
//mMemoryCachedBackgroundView.setBackgroundColor(mPreviewBackgroundBean.getPixelColor());
LogUtils.d(TAG, "doubleRefreshPreview【双重刷新】第一重完成");
} catch (Exception e) {
LogUtils.e(TAG, String.format("doubleRefreshPreview【双重刷新】第一重异常 | %s", e.getMessage()));
return;
}
// 第二重刷新(延迟执行)
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
if (mMemoryCachedBackgroundView != null && !isFinishing()) {
try {
mMemoryCachedBackgroundView.loadByBackgroundBean(mPreviewBackgroundBean, true);
//mMemoryCachedBackgroundView.setBackgroundColor(mPreviewBackgroundBean.getPixelColor());
LogUtils.d(TAG, "doubleRefreshPreview【双重刷新】第二重完成");
} catch (Exception e) {
LogUtils.e(TAG, String.format("doubleRefreshPreview【双重刷新】第二重异常 | %s", e.getMessage()));
}
}
}
}, DOUBLE_REFRESH_DELAY);
}
// ====================== 工具辅助方法(文件校验) ======================
/**
* 校验文件是否有效(存在且大小达标)
* @param file 待校验文件
* @return true=有效 false=无效
*/
private boolean isFileValid(File file) {
boolean isValid = file != null && file.exists() && file.length() > FILE_MIN_SIZE;
LogUtils.d(TAG, String.format("isFileValid文件校验 | 路径=%s | 结果=%b", file != null ? file.getAbsolutePath() : "null", isValid));
return isValid;
}
}

View File

@@ -1,39 +1,275 @@
package cc.winboll.studio.powerbell.unittest;
import android.content.Intent;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import cc.winboll.studio.libappbase.GlobalApplication;
import cc.winboll.studio.powerbell.R;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.nfc.tech.TagTechnology;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.ToastUtils;
import cc.winboll.studio.powerbell.MainActivity;
import cc.winboll.studio.powerbell.R;
import cc.winboll.studio.powerbell.models.BackgroundBean;
import cc.winboll.studio.powerbell.utils.FileUtils;
import cc.winboll.studio.powerbell.utils.ImageCropUtils;
import cc.winboll.studio.powerbell.utils.ImageUtils;
import cc.winboll.studio.powerbell.views.BackgroundView;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
/**
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2025/11/19 18:04
* @Describe 单元测试启动主页窗口
* 单元测试页面
* 功能:测试背景图加载、图片裁剪、双重刷新预览等功能
* 适配Java7 | API30 | 私有目录文件操作 | 无Uri冲突
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
* @Describe 单元测试页:验证图片处理与背景预览相关逻辑
*/
public class MainUnitTestActivity extends AppCompatActivity {
// ====================== 静态常量区(置顶归类,消除魔法值) ======================
public static final String TAG = "MainUnitTestActivity";
public static final int REQUEST_CROP_IMAGE = 0;
private static final String ASSETS_TEST_IMAGE_PATH = "unittest/unittest-miku.png";
private static final long FILE_MIN_SIZE = 100L;
private static final long DOUBLE_REFRESH_DELAY = 200L;
// ====================== 成员变量区按功能分层移除所有Uri相关 ======================
private BackgroundView mBackgroundView;
private String mAppPrivateDirPath;
private File mPrivateTestImageFile; // 仅用File不用Uri
private File mPrivateCropImageFile;
private BackgroundBean mPreviewBackgroundBean;
// ====================== 生命周期方法按执行顺序onCreate→onActivityResult ======================
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 非调试状态就退出
if (!GlobalApplication.isDebugging()) {
finish();
}
setContentView(R.layout.activity_mainunittest);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.activitymainunittestFrameLayout1, new BackgroundViewTestFragment(), BackgroundViewTestFragment.TAG);
fragmentTransaction.commit();
ToastUtils.show(String.format("%s onCreate", TAG));
LogUtils.d(TAG, "=== 页面 onCreate 启动 ===");
initBaseParams();
initViewAndEvent();
copyAssetsTestImageToPrivateDir();
initBackgroundBean();
doubleRefreshPreview();
ToastUtils.show("单元测试页面启动完成");
LogUtils.d(TAG, "=== 页面 onCreate 初始化结束 ===");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
LogUtils.d(TAG, String.format("=== onActivityResult 回调 | requestCode=%d | resultCode=%d ===", requestCode, resultCode));
if (requestCode == REQUEST_CROP_IMAGE) {
handleCropResult(resultCode);
}
}
// ====================== 初始化相关方法基础参数→视图→背景Bean ======================
/**
* 初始化基础参数:私有目录、测试文件
*/
private void initBaseParams() {
LogUtils.d(TAG, "initBaseParams初始化基础参数");
// 初始化私有目录无需权限无UID冲突
mAppPrivateDirPath = getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/PowerBellTest/";
File privateDir = new File(mAppPrivateDirPath);
if (!privateDir.exists()) {
boolean isDirCreated = privateDir.mkdirs();
LogUtils.d(TAG, String.format("initBaseParams创建私有目录 | 路径=%s | 结果=%b", mAppPrivateDirPath, isDirCreated));
}
// 初始化测试文件与裁剪文件无Uri
File refFile = new File(ASSETS_TEST_IMAGE_PATH);
String uniqueTestName = FileUtils.createUniqueFileName(refFile) + ".png";
String uniqueCropName = uniqueTestName.replace(".png", "_crop.png");
mPrivateTestImageFile = new File(mAppPrivateDirPath, uniqueTestName);
mPrivateCropImageFile = new File(mAppPrivateDirPath, uniqueCropName);
LogUtils.d(TAG, String.format("initBaseParams测试图路径=%s", mPrivateTestImageFile.getAbsolutePath()));
LogUtils.d(TAG, String.format("initBaseParams裁剪图路径=%s", mPrivateCropImageFile.getAbsolutePath()));
}
/**
* 初始化布局与控件事件
*/
private void initViewAndEvent() {
LogUtils.d(TAG, "initViewAndEvent初始化布局与控件事件");
setContentView(R.layout.activity_mainunittest);
mBackgroundView = (BackgroundView) findViewById(R.id.backgroundview);
// 跳转主页面按钮
Button btnMain = (Button) findViewById(R.id.btn_main_activity);
btnMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "initViewAndEvent点击按钮→跳转主页面");
startActivity(new Intent(MainUnitTestActivity.this, MainActivity.class));
}
});
// 裁剪按钮直接用File路径启动无Uri
Button btnCrop = (Button) findViewById(R.id.btn_test_cropimage);
btnCrop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.d(TAG, "initViewAndEvent点击按钮→启动裁剪File路径版");
ToastUtils.show("准备启动图片裁剪");
if (isFileValid(mPrivateTestImageFile)) {
startCropTestByFile();
} else {
ToastUtils.show("测试图片未准备好,重新拷贝");
copyAssetsTestImageToPrivateDir();
}
}
});
}
/**
* 初始化背景Bean
*/
private void initBackgroundBean() {
LogUtils.d(TAG, "initBackgroundBean初始化背景Bean");
mPreviewBackgroundBean = new BackgroundBean();
mPreviewBackgroundBean.setPixelColor(ImageUtils.getColorAccent(this));
mPreviewBackgroundBean.setBackgroundFileName(mPrivateTestImageFile.getName());
mPreviewBackgroundBean.setBackgroundFilePath(mPrivateTestImageFile.getAbsolutePath());
mPreviewBackgroundBean.setBackgroundScaledCompressFileName(mPrivateCropImageFile.getName());
mPreviewBackgroundBean.setBackgroundScaledCompressFilePath(mPrivateCropImageFile.getAbsolutePath());
mPreviewBackgroundBean.setIsUseBackgroundFile(true);
LogUtils.d(TAG, "initBackgroundBean背景Bean初始化完成");
}
// ====================== 核心业务方法(文件拷贝→裁剪→结果处理→预览刷新) ======================
/**
* 从assets拷贝图片到私有目录
*/
private void copyAssetsTestImageToPrivateDir() {
LogUtils.d(TAG, "copyAssetsTestImageToPrivateDir开始拷贝assets图片到私有目录");
if (isFileValid(mPrivateTestImageFile)) {
LogUtils.d(TAG, "copyAssetsTestImageToPrivateDir图片已存在无需拷贝");
return;
}
InputStream inputStream = null;
try {
inputStream = getAssets().open(ASSETS_TEST_IMAGE_PATH);
FileUtils.copyStreamToFile(inputStream, mPrivateTestImageFile);
LogUtils.d(TAG, String.format("copyAssetsTestImageToPrivateDir图片拷贝成功 | 大小=%d字节", mPrivateTestImageFile.length()));
} catch (IOException e) {
LogUtils.e(TAG, String.format("copyAssetsTestImageToPrivateDir图片拷贝失败 | %s", e.getMessage()), e);
ToastUtils.show("图片准备失败");
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
LogUtils.e(TAG, String.format("copyAssetsTestImageToPrivateDir关闭流失败 | %s", e.getMessage()));
}
}
}
}
/**
* 直接用File启动裁剪关键调用ImageCropUtils的File重载方法
*/
private void startCropTestByFile() {
LogUtils.d(TAG, String.format("startCropTestByFile启动裁剪 | 原图=%s", mPrivateTestImageFile.getAbsolutePath()));
// 确保输出目录存在
File cropParent = mPrivateCropImageFile.getParentFile();
if (!cropParent.exists()) {
boolean isDirCreated = cropParent.mkdirs();
LogUtils.d(TAG, String.format("startCropTestByFile创建裁剪目录 | 路径=%s | 结果=%b", cropParent.getAbsolutePath(), isDirCreated));
}
// 调用ImageCropUtils的File参数方法核心绕开Uri
ImageCropUtils.startImageCrop(
this,
mPrivateTestImageFile, // 原图File
mPrivateCropImageFile, // 输出File
0,
0,
true,
REQUEST_CROP_IMAGE
);
LogUtils.d(TAG, String.format("startCropTestByFile裁剪请求已发送 | 输出路径=%s", mPrivateCropImageFile.getAbsolutePath()));
ToastUtils.show("已启动图片裁剪");
}
/**
* 处理裁剪结果直接校验输出File
* @param resultCode 裁剪结果码
*/
private void handleCropResult(int resultCode) {
// LogUtils.d(TAG, String.format("handleCropResult裁剪回调处理 | resultCode=%d", resultCode));
// if (resultCode == RESULT_OK) {
// if (isFileValid(mPrivateCropImageFile)) {
// mBackgroundView.loadImage(mPrivateCropImageFile.getAbsolutePath());
// LogUtils.d(TAG, String.format("handleCropResult裁剪成功 | 加载裁剪图=%s", mPrivateCropImageFile.getAbsolutePath()));
// ToastUtils.show("裁剪成功");
// mPreviewBackgroundBean.setIsUseBackgroundScaledCompressFile(true);
// doubleRefreshPreview();
// } else {
// LogUtils.e(TAG, "handleCropResult裁剪成功但输出文件无效");
// ToastUtils.show("裁剪失败:输出文件无效");
// }
// } else if (resultCode == RESULT_CANCELED) {
// LogUtils.d(TAG, "handleCropResult裁剪取消");
// ToastUtils.show("裁剪已取消");
// } else {
// LogUtils.e(TAG, String.format("handleCropResult裁剪失败 | resultCode异常=%d", resultCode));
// ToastUtils.show("裁剪失败");
// }
}
/**
* 双重刷新预览,确保背景加载最新数据
*/
private void doubleRefreshPreview() {
LogUtils.d(TAG, "doubleRefreshPreview执行双重刷新预览");
// 第一重刷新
try {
mBackgroundView.loadByBackgroundBean(mPreviewBackgroundBean, true);
mBackgroundView.setBackgroundColor(mPreviewBackgroundBean.getPixelColor());
LogUtils.d(TAG, "doubleRefreshPreview【双重刷新】第一重完成");
} catch (Exception e) {
LogUtils.e(TAG, String.format("doubleRefreshPreview【双重刷新】第一重异常 | %s", e.getMessage()));
return;
}
// 第二重刷新(延迟执行)
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
if (mBackgroundView != null && !isFinishing()) {
try {
mBackgroundView.loadByBackgroundBean(mPreviewBackgroundBean, true);
mBackgroundView.setBackgroundColor(mPreviewBackgroundBean.getPixelColor());
LogUtils.d(TAG, "doubleRefreshPreview【双重刷新】第二重完成");
} catch (Exception e) {
LogUtils.e(TAG, String.format("doubleRefreshPreview【双重刷新】第二重异常 | %s", e.getMessage()));
}
}
}
}, DOUBLE_REFRESH_DELAY);
}
// ====================== 工具辅助方法(文件校验) ======================
/**
* 校验文件是否有效(存在且大小达标)
* @param file 待校验文件
* @return true=有效 false=无效
*/
private boolean isFileValid(File file) {
boolean isValid = file != null && file.exists() && file.length() > FILE_MIN_SIZE;
LogUtils.d(TAG, String.format("isFileValid文件校验 | 路径=%s | 结果=%b", file != null ? file.getAbsolutePath() : "null", isValid));
return isValid;
}
}

View File

@@ -1,10 +1,5 @@
package cc.winboll.studio.powerbell.utils;
/**
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2025/11/26 15:54
* @Describe 应用图标切换工具类(启用组件时创建对应快捷方式)
*/
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
@@ -13,75 +8,143 @@ import android.os.Build;
import android.widget.Toast;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.powerbell.App;
import cc.winboll.studio.powerbell.MainActivity;
import cc.winboll.studio.powerbell.R;
/**
* 应用图标切换工具类(启用组件时创建对应快捷方式)
* 适配Java7 | API30 | 高低版本快捷方式创建兼容
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
* @Describe 应用启动器组件切换与桌面快捷方式创建工具,支持多组件管理与版本兼容
*/
public class APPPlusUtils {
// ======================== 静态常量区(魔法值与标签管理)========================
public static final String TAG = "APPPlusUtils";
private static final int SHORTCUT_ICON_DEFAULT = R.drawable.ic_launcher; // 默认快捷方式图标
private static final String ACTION_INSTALL_SHORTCUT = "com.android.launcher.action.INSTALL_SHORTCUT"; // 旧版快捷方式广播Action
// 快捷方式配置(名称+图标,需与实际资源匹配)
// private static final String PLUS_SHORTCUT_NAME = "位置服务-Laojun";
// private static final int PLUS_SHORTCUT_ICON = R.mipmap.ic_launcher; // Laojun 图标资源
// ======================== 公共业务方法区(对外核心接口)========================
/**
* 添加Plus组件与图标
* 切换应用启动器组件(禁用其他组件,启用目标组件)
* @param context 上下文
* @param componentName 目标组件完整类名
* @return 切换是否成功
*/
public static boolean switchAppLauncherToComponent(Context context, String componentName) {
LogUtils.d(TAG, String.format("switchAppLauncherToComponent调用 | 传入组件名=%s", componentName));
// 参数校验
if (context == null) {
LogUtils.d(TAG, "切换失败:上下文为空");
Toast.makeText(context, context.getString(R.string.app_name) + "图标切换失败", Toast.LENGTH_SHORT).show();
LogUtils.e(TAG, "switchAppLauncherToComponent失败:上下文为空");
return false;
}
if (componentName == null || componentName.isEmpty()) {
LogUtils.e(TAG, "switchAppLauncherToComponent失败组件名为空");
return false;
}
PackageManager pm = context.getPackageManager();
ComponentName plusComponentSwitchTo = new ComponentName(context, componentName);
ComponentName plusComponentEN1 = new ComponentName(context, App.COMPONENT_EN1);
ComponentName plusComponentCN1 = new ComponentName(context, App.COMPONENT_CN1);
ComponentName plusComponentCN2 = new ComponentName(context, App.COMPONENT_CN2);
ComponentName targetComponent = new ComponentName(context, componentName);
ComponentName en1Component = new ComponentName(context, App.COMPONENT_EN1);
ComponentName cn1Component = new ComponentName(context, App.COMPONENT_CN1);
ComponentName cn2Component = new ComponentName(context, App.COMPONENT_CN2);
try {
disableComponent(pm, plusComponentEN1);
disableComponent(pm, plusComponentCN1);
disableComponent(pm, plusComponentCN2);
enableComponent(pm, plusComponentSwitchTo);
// 禁用所有其他启动器组件
disableComponent(pm, en1Component);
disableComponent(pm, cn1Component);
disableComponent(pm, cn2Component);
// 启用目标组件
enableComponent(pm, targetComponent);
LogUtils.d(TAG, String.format("switchAppLauncherToComponent成功 | 目标组件=%s", componentName));
Toast.makeText(context, context.getString(R.string.app_name) + "图标切换成功", Toast.LENGTH_SHORT).show();
return true;
} catch (Exception e) {
LogUtils.e(TAG, "图标切换失败:" + e.getMessage());
Toast.makeText(context, context.getString(R.string.app_name) + "图标切换失败" + e.getMessage(), Toast.LENGTH_SHORT).show();
LogUtils.e(TAG, String.format("switchAppLauncherToComponent失败 | 异常信息=%s", e.getMessage()), e);
Toast.makeText(context, context.getString(R.string.app_name) + "图标切换失败" + e.getMessage(), Toast.LENGTH_SHORT).show();
return false;
}
}
// ======================== 私有辅助方法区(组件状态控制)========================
/**
* 启用组件(带状态检查,避免重复操作)
* @param pm 包管理器
* @param component 目标组件
*/
private static void enableComponent(PackageManager pm, ComponentName component) {
int currentState = pm.getComponentEnabledSetting(component);
String componentName = component.getClassName();
if (currentState != PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
pm.setComponentEnabledSetting(
component,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP | PackageManager.SYNCHRONOUS
);
LogUtils.d(TAG, String.format("enableComponent成功 | 组件=%s", componentName));
} else {
LogUtils.d(TAG, String.format("enableComponent无需操作 | 组件已启用=%s", componentName));
}
}
/**
* 禁用组件(带状态检查,避免重复操作)
* @param pm 包管理器
* @param component 目标组件
*/
private static void disableComponent(PackageManager pm, ComponentName component) {
int currentState = pm.getComponentEnabledSetting(component);
String componentName = component.getClassName();
if (currentState != PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
pm.setComponentEnabledSetting(
component,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP | PackageManager.SYNCHRONOUS
);
LogUtils.d(TAG, String.format("disableComponent成功 | 组件=%s", componentName));
} else {
LogUtils.d(TAG, String.format("disableComponent无需操作 | 组件已禁用=%s", componentName));
}
}
// ======================== 私有辅助方法区(快捷方式创建)========================
/**
* 创建指定组件的桌面快捷方式(自动去重,兼容 Android 8.0+
* @param component 目标组件(如 LAOJUN_ACTIVITY
* @param context 上下文
* @param component 目标组件
* @param name 快捷方式名称
* @param iconRes 快捷方式图标资源ID
* @return 是否创建成功
*/
private static boolean createComponentShortcut(Context context, ComponentName component, String name, int iconRes) {
if (context == null || component == null || name == null || iconRes == 0) {
LogUtils.d(TAG, "快捷方式创建失败:参数为空");
// 参数校验
String componentName = component != null ? component.getClassName() : "null";
LogUtils.d(TAG, String.format("createComponentShortcut调用 | 组件=%s | 名称=%s", componentName, name));
if (context == null || component == null || name == null || name.isEmpty()) {
LogUtils.e(TAG, "createComponentShortcut失败上下文、组件或名称为空");
return false;
}
// 图标资源默认值补全
int finalIconRes = iconRes != 0 ? iconRes : SHORTCUT_ICON_DEFAULT;
// Android 8.0+API 26+):使用 ShortcutManager系统推荐
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
try {
PackageManager pm = context.getPackageManager();
android.content.pm.ShortcutManager shortcutManager = context.getSystemService(android.content.pm.ShortcutManager.class);
if (shortcutManager == null || !shortcutManager.isRequestPinShortcutSupported()) {
LogUtils.d(TAG, "系统不支持创建快捷方式");
LogUtils.w(TAG, "createComponentShortcut系统不支持创建快捷方式");
return false;
}
// 检查是否已存在该组件的快捷方式(去重)
for (android.content.pm.ShortcutInfo info : shortcutManager.getPinnedShortcuts()) {
if (component.getClassName().equals(info.getIntent().getComponent().getClassName())) {
LogUtils.d(TAG, "快捷方式已存在" + component.getClassName());
LogUtils.d(TAG, String.format("createComponentShortcut快捷方式已存在=%s", componentName));
return true;
}
}
@@ -96,16 +159,17 @@ public class APPPlusUtils {
android.content.pm.ShortcutInfo shortcutInfo = new android.content.pm.ShortcutInfo.Builder(context, component.getClassName())
.setShortLabel(name)
.setLongLabel(name)
.setIcon(android.graphics.drawable.Icon.createWithResource(context, iconRes))
.setIcon(android.graphics.drawable.Icon.createWithResource(context, finalIconRes))
.setIntent(launchIntent)
.build();
// 请求创建快捷方式(需用户确认)
shortcutManager.requestPinShortcut(shortcutInfo, null);
LogUtils.d(TAG, "createComponentShortcutAndroid O+ 快捷方式创建请求已发送");
return true;
} catch (Exception e) {
LogUtils.d(TAG, "Android O+ 快捷方式创建失败:" + e.getMessage());
LogUtils.e(TAG, String.format("createComponentShortcut失败 | Android O+ 异常=%s", e.getMessage()), e);
return false;
}
} else {
@@ -118,47 +182,22 @@ public class APPPlusUtils {
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
// 构建创建快捷方式的广播意图
Intent installIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
Intent installIntent = new Intent(ACTION_INSTALL_SHORTCUT);
installIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
installIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
installIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(context, iconRes));
Intent.ShortcutIconResource.fromContext(context, finalIconRes));
installIntent.putExtra("duplicate", false); // 禁止重复创建
context.sendBroadcast(installIntent);
LogUtils.d(TAG, "createComponentShortcutAndroid O- 快捷方式创建广播已发送");
return true;
} catch (Exception e) {
LogUtils.d(TAG, "Android O- 快捷方式创建失败:" + e.getMessage());
LogUtils.e(TAG, String.format("createComponentShortcut失败 | Android O- 异常=%s", e.getMessage()), e);
return false;
}
}
}
/**
* 启用组件(带状态检查,避免重复操作)
*/
private static void enableComponent(PackageManager pm, ComponentName component) {
if (pm.getComponentEnabledSetting(component) != PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
pm.setComponentEnabledSetting(
component,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP | PackageManager.SYNCHRONOUS
);
}
}
/**
* 禁用组件(带状态检查,避免重复操作)
*/
private static void disableComponent(PackageManager pm, ComponentName component) {
if (pm.getComponentEnabledSetting(component) != PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
pm.setComponentEnabledSetting(
component,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP | PackageManager.SYNCHRONOUS
);
}
}
}

View File

@@ -2,84 +2,142 @@ package cc.winboll.studio.powerbell.utils;
import android.content.Context;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.powerbell.beans.BatteryInfoBean;
import cc.winboll.studio.powerbell.models.BatteryInfoBean;
import java.util.ArrayList;
/**
* 应用缓存工具类适配Android API 30基于Java 7编写
* 负责电池信息的缓存、持久化与管理
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
* @Describe 电池信息缓存工具:实现电量变化记录、持久化存储与缓存限制
*/
public class AppCacheUtils {
// ===================== 静态常量区(置顶归类,消除魔法值) =====================
public static final String TAG = "AppCacheUtils";
private static final int MAX_BATTERY_RECORD_COUNT = 180; // 电池记录最大条数限制
// 保存唯一配置实例
static AppCacheUtils _mAppCacheUtils;
// 配置实例引用的上下文环境
Context mContext;
// 配置实例的数据的存储文件路径
//volatile String mAppCacheDataFilePath = null;
ArrayList<BatteryInfoBean> mlBatteryInfo;
// ===================== 静态成员区(单例相关) =====================
private static AppCacheUtils sInstance;
// 私有实例构造方法
//
AppCacheUtils(Context context) {
mContext = context;
//mAppCacheDataFilePath = context.getExternalFilesDir(TAG) + File.separator + "mlBatteryInfo.dat";
mlBatteryInfo = new ArrayList<BatteryInfoBean>();
loadAppCacheData();
}
// ===================== 成员变量区(按功能分层) =====================
private Context mContext; // ApplicationContext避免内存泄漏
private ArrayList<BatteryInfoBean> mBatteryInfoList; // 电池信息缓存列表
// 返回唯一实例
//
// ===================== 单例方法区(线程安全) =====================
/**
* 获取单例实例
* @param context 上下文内部会转换为ApplicationContext
* @return 唯一AppCacheUtils实例
*/
public static synchronized AppCacheUtils getInstance(Context context) {
if (_mAppCacheUtils == null) {
_mAppCacheUtils = new AppCacheUtils(context);
String contextType = context != null ? context.getClass().getSimpleName() : "null";
LogUtils.d(TAG, String.format("getInstance调用 | 传入Context类型=%s", contextType));
if (sInstance == null) {
if (context == null) {
LogUtils.e(TAG, "getInstance失败传入Context为null");
throw new IllegalArgumentException("Context cannot be null");
}
sInstance = new AppCacheUtils(context.getApplicationContext());
LogUtils.d(TAG, "getInstance单例实例初始化完成");
}
return _mAppCacheUtils;
return sInstance;
}
// 添加电量改变时间
//
public void addChangingTime(int nBattetyValue) {
if (mlBatteryInfo.size() == 0) {
addChangingTimeToList(nBattetyValue);
//LogUtils.d(TAG, "nBattetyValue is "+Integer.toString(nBattetyValue));
// ===================== 私有构造方法区(禁止外部实例化) =====================
/**
* 私有构造方法,初始化缓存列表并加载持久化数据
* @param context ApplicationContext
*/
private AppCacheUtils(Context context) {
LogUtils.d(TAG, "AppCacheUtils构造方法调用");
mContext = context;
mBatteryInfoList = new ArrayList<BatteryInfoBean>();
loadAppCacheData();
LogUtils.d(TAG, String.format("AppCacheUtils构造完成 | 初始电池信息数量=%d", mBatteryInfoList.size()));
}
// ===================== 公共业务方法区(对外暴露接口) =====================
/**
* 添加电池电量变化记录(仅当电量变化时添加)
* @param batteryValue 电池电量值
*/
public void addChangingTime(int batteryValue) {
LogUtils.d(TAG, String.format("addChangingTime调用 | 传入电量值=%d", batteryValue));
if (mBatteryInfoList.isEmpty()) {
addChangingTimeToList(batteryValue);
LogUtils.d(TAG, "addChangingTime缓存列表为空直接添加记录");
return;
}
if (mlBatteryInfo.get(mlBatteryInfo.size() - 1).getBattetyValue() != nBattetyValue) {
addChangingTimeToList(nBattetyValue);
//LogUtils.d(TAG, "nBattetyValue is "+Integer.toString(nBattetyValue));
// 对比最后一条记录的电量值,避免重复添加
int lastBatteryValue = mBatteryInfoList.get(mBatteryInfoList.size() - 1).getBatteryValue();
if (lastBatteryValue != batteryValue) {
addChangingTimeToList(batteryValue);
LogUtils.d(TAG, String.format("addChangingTime电量变化添加新记录 | 原电量=%d | 新电量=%d", lastBatteryValue, batteryValue));
} else {
LogUtils.d(TAG, "addChangingTime电量未变化跳过添加");
}
}
void addChangingTimeToList(int nBattetyValue) {
if (mlBatteryInfo.size() > 180) {
mlBatteryInfo.remove(0);
}
BatteryInfoBean batteryInfo = new BatteryInfoBean(System.currentTimeMillis(), nBattetyValue);
LogUtils.d(TAG, "getBattetyValue is " + Integer.toString(batteryInfo.getBattetyValue()));
LogUtils.d(TAG, "getTimeStamp is " + Long.toString(batteryInfo.getTimeStamp()));
mlBatteryInfo.add(batteryInfo);
saveAppCacheData();
}
/**
* 获取电池信息缓存列表
* @return 完整的电池信息列表
*/
public ArrayList<BatteryInfoBean> getArrayListBatteryInfo() {
LogUtils.d(TAG, String.format("getArrayListBatteryInfo调用 | 当前缓存数量=%d", mBatteryInfoList.size()));
loadAppCacheData();
return mlBatteryInfo;
}
// 读取文件存储的数据
//
void saveAppCacheData() {
BatteryInfoBean.saveBeanList(mContext, mlBatteryInfo, BatteryInfoBean.class);
}
// 保存数据到文件
//
void loadAppCacheData() {
mlBatteryInfo.clear();
BatteryInfoBean.loadBeanList(mContext, mlBatteryInfo, BatteryInfoBean.class);
return mBatteryInfoList;
}
/**
* 清除所有电池历史记录
*/
public void clearBatteryHistory() {
mlBatteryInfo.clear();
LogUtils.d(TAG, String.format("clearBatteryHistory调用 | 清除前缓存数量=%d", mBatteryInfoList.size()));
mBatteryInfoList.clear();
saveAppCacheData();
LogUtils.d(TAG, "clearBatteryHistory完成 | 缓存已清空");
}
// ===================== 私有辅助方法区(内部业务逻辑) =====================
/**
* 内部方法:添加电量记录到列表并持久化
* @param batteryValue 电池电量值
*/
private void addChangingTimeToList(int batteryValue) {
LogUtils.d(TAG, String.format("addChangingTimeToList调用 | 传入电量值=%d", batteryValue));
// 限制列表最大长度,避免内存溢出
if (mBatteryInfoList.size() >= MAX_BATTERY_RECORD_COUNT) {
mBatteryInfoList.remove(0);
LogUtils.d(TAG, String.format("addChangingTimeToList列表超过%d条移除最旧记录", MAX_BATTERY_RECORD_COUNT));
}
BatteryInfoBean batteryInfo = new BatteryInfoBean(System.currentTimeMillis(), batteryValue);
mBatteryInfoList.add(batteryInfo);
LogUtils.d(TAG, String.format("addChangingTimeToList添加新记录 | 电量=%d | 时间戳=%d", batteryInfo.getBatteryValue(), batteryInfo.getTimeStamp()));
saveAppCacheData();
}
/**
* 从文件加载缓存数据
*/
private void loadAppCacheData() {
LogUtils.d(TAG, "loadAppCacheData调用 | 开始加载持久化数据");
mBatteryInfoList.clear();
BatteryInfoBean.loadBeanList(mContext, mBatteryInfoList, BatteryInfoBean.class);
LogUtils.d(TAG, String.format("loadAppCacheData完成 | 加载数据数量=%d", mBatteryInfoList.size()));
}
/**
* 保存缓存数据到文件
*/
private void saveAppCacheData() {
LogUtils.d(TAG, String.format("saveAppCacheData调用 | 保存数据数量=%d", mBatteryInfoList.size()));
BatteryInfoBean.saveBeanList(mContext, mBatteryInfoList, BatteryInfoBean.class);
LogUtils.d(TAG, "saveAppCacheData完成 | 数据已持久化");
}
}

View File

@@ -1,203 +1,358 @@
package cc.winboll.studio.powerbell.utils;
import android.app.Activity;
import android.content.Context;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.powerbell.App;
import cc.winboll.studio.powerbell.MainActivity;
import cc.winboll.studio.powerbell.beans.AppConfigBean;
import cc.winboll.studio.powerbell.beans.ControlCenterServiceBean;
import cc.winboll.studio.powerbell.dialogs.YesNoAlertDialog;
import cc.winboll.studio.powerbell.fragments.MainViewFragment;
import cc.winboll.studio.powerbell.services.ControlCenterService;
import java.io.File;
import cc.winboll.studio.powerbell.models.AppConfigBean;
import cc.winboll.studio.powerbell.models.ControlCenterServiceBean;
import cc.winboll.studio.powerbell.threads.RemindThread;
// 应用配置工具类
//
/**
* 应用配置工具类:管理应用核心配置(服务开关、电池提醒阈值、背景设置等)
* 适配Java7 | API30 | 小米手机,单例模式,线程安全,配置持久化
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
* @Describe 应用配置全量管理工具,支持配置持久化、自动校准、线程安全访问
*/
public class AppConfigUtils {
// ======================== 静态常量区(魔法值统一管理)========================
public static final String TAG = "AppConfigUtils";
public static final String BACKGROUND_DIR = "Background"; // 背景图片存储目录
private static final int MIN_REMINDER_VALUE = 0; // 提醒阈值最小值
private static final int MAX_REMINDER_VALUE = 100; // 提醒阈值最大值
private static final int MIN_INTERVAL_TIME = 1000; // 最小提醒间隔ms
private static final int MIN_DETECT_INTERVAL = 500; // 最小电量检测间隔ms
public static final String BACKGROUND_DIR = "Background";
// ======================== 静态成员区(单例实例)========================
private static volatile AppConfigUtils sInstance; // 单例实例volatile保障双重校验锁有效性
// 保存唯一配置实例
static AppConfigUtils _mAppConfigUtils;
// 应用环境上下文
Context mContext;
// ======================== 成员变量区按依赖优先级排序final/volatile保障线程安全========================
private final Context mContext; // 应用上下文ApplicationContext避免内存泄漏
private final App mApplication; // 应用Application实例final保障不可变
public volatile AppConfigBean mAppConfigBean; // 应用配置Bean持久化核心volatile保障线程安全
private volatile boolean mIsServiceEnabled = false; // 服务开关缓存状态减少Bean读取次数
// 是否启动铃声提醒服务
volatile boolean mIsEnableService = false;
public volatile AppConfigBean mAppConfigBean;
// 电池充电提醒值。
// Battery charge reminder value.
volatile int mnChargeReminderValue = -1;
volatile boolean mIsEnableChargeReminder = false;
// 电池耗电量提醒值。
// Battery power usege reminder value.
volatile int mnUsegeReminderValue = -1;
volatile boolean mIsEnableUsegeReminder = false;
volatile boolean mIsUseBackgroundFile = false;
volatile String mszBackgroundFileName = "";
// 保存应用实例
App mApplication;
AppConfigUtils(Context context) {
mContext = context;
String szExternalFilesDir = mContext.getExternalFilesDir(TAG) + File.separator;
//mlistAppConfigBean = new ArrayList<AppConfigBean>();
mAppConfigBean = new AppConfigBean();
loadAppConfigBean();
}
// 返回唯一实例
//
// ======================== 单例相关方法区(双重校验锁+构造方法)========================
/**
* 双重校验锁单例获取方法,线程安全
* @param context 上下文不可为null
* @return 单例实例
*/
public static AppConfigUtils getInstance(Context context) {
if (_mAppConfigUtils == null) {
_mAppConfigUtils = new AppConfigUtils(context);
String contextType = context != null ? context.getClass().getSimpleName() : "null";
LogUtils.d(TAG, String.format("getInstance() 调用 | 传入Context类型=%s", contextType));
if (context == null) {
LogUtils.e(TAG, "getInstance() 失败Context不能为空");
throw new IllegalArgumentException("Context cannot be null");
}
return _mAppConfigUtils;
}
public void setIsEnableService(Activity activity, final boolean isEnableService) {
YesNoAlertDialog.show(activity, "应用设置信息", "是否保存应用配置?", new YesNoAlertDialog.OnDialogResultListener(){
@Override
public void onYes() {
mIsEnableService = isEnableService;
ControlCenterServiceBean bean = new ControlCenterServiceBean(isEnableService);
ControlCenterServiceBean.saveBean(mContext, bean);
if (mIsEnableService) {
LogUtils.d(TAG, "startControlCenterService");
ControlCenterService.startControlCenterService(mContext);
} else {
LogUtils.d(TAG, "stopControlCenterService");
ControlCenterService.stopControlCenterService(mContext);
}
}
@Override
public void onNo() {
MainViewFragment.relaodAppConfigs();
if (sInstance == null) {
synchronized (AppConfigUtils.class) {
if (sInstance == null) {
sInstance = new AppConfigUtils(context);
LogUtils.d(TAG, "getInstance():单例实例创建成功");
}
});
}
public boolean getIsEnableService() {
ControlCenterServiceBean bean = ControlCenterServiceBean.loadBean(mContext, ControlCenterServiceBean.class);
if (bean == null) {
ControlCenterServiceBean.saveBean(mContext, new ControlCenterServiceBean(false));
return false;
}
}
return bean.isEnableService();
LogUtils.d(TAG, "getInstance():单例实例获取成功");
return sInstance;
}
public void setIsEnableChargeReminder(boolean isEnableChargeReminder) {
mAppConfigBean.setIsEnableChargeReminder(isEnableChargeReminder);
saveConfigData(MainActivity._mMainActivity);
/**
* 私有构造方法,禁止外部实例化
* @param context 上下文内部转换为ApplicationContext
*/
private AppConfigUtils(Context context) {
LogUtils.d(TAG, "AppConfigUtils() 构造方法调用");
this.mContext = context.getApplicationContext();
this.mApplication = (App) context.getApplicationContext();
mAppConfigBean = new AppConfigBean();
loadAppConfig(); // 加载持久化配置
LogUtils.d(TAG, "AppConfigUtils() 构造完成,配置初始化成功");
}
public boolean getIsEnableChargeReminder() {
return mAppConfigBean.isEnableChargeReminder();
}
// ======================== 核心配置持久化方法区(加载+保存)========================
/**
* 加载应用配置(初始化/重载通用入口)
* @return 加载后的应用配置Bean
*/
public AppConfigBean loadAppConfig() {
LogUtils.d(TAG, "loadAppConfig() 调用 | 开始加载应用配置");
AppConfigBean savedAppBean = (AppConfigBean) AppConfigBean.loadBean(mContext, AppConfigBean.class);
public void setIsEnableUsegeReminder(boolean isEnableUsegeReminder) {
mAppConfigBean.setIsEnableUsegeReminder(isEnableUsegeReminder);
saveConfigData(MainActivity._mMainActivity);
}
public boolean getIsEnableUsegeReminder() {
return mAppConfigBean.isEnableUsegeReminder();
}
public void setChargeReminderValue(int value) {
mAppConfigBean.setChargeReminderValue(value);
saveConfigData(MainActivity._mMainActivity);
}
public int getChargeReminderValue() {
return mAppConfigBean.getChargeReminderValue();
}
public void setUsegeReminderValue(int value) {
mAppConfigBean.setUsegeReminderValue(value);
saveConfigData(MainActivity._mMainActivity);
}
public int getUsegeReminderValue() {
return mAppConfigBean.getUsegeReminderValue();
}
public void setIsCharging(boolean isCharging) {
mAppConfigBean.setIsCharging(isCharging);
}
public boolean isCharging() {
return mAppConfigBean.isCharging();
}
public void setCurrentValue(int nCurrentValue) {
mAppConfigBean.setCurrentValue(nCurrentValue);
}
public int getCurrentValue() {
return mAppConfigBean.getCurrentValue();
}
public void setReminderIntervalTime(int reminderIntervalTime) {
mAppConfigBean.setReminderIntervalTime(reminderIntervalTime);
}
public int getReminderIntervalTime() {
return mAppConfigBean.getReminderIntervalTime();
}
//
// 加载电池提醒配置数据
//
public void loadAppConfigBean() {
AppConfigBean bean = AppConfigBean.loadBean(mContext, AppConfigBean.class);
if (bean == null) {
bean = new AppConfigBean();
if (savedAppBean != null) {
mAppConfigBean = savedAppBean;
LogUtils.d(TAG, String.format("loadAppConfig() 成功 | 充电阈值=%d%% | 耗电阈值=%d%%",
mAppConfigBean.getChargeReminderValue(), mAppConfigBean.getUsageReminderValue()));
} else {
mAppConfigBean = new AppConfigBean();
AppConfigBean.saveBean(mContext, mAppConfigBean);
LogUtils.d(TAG, "loadAppConfig():无已保存配置,使用默认值并持久化");
}
mAppConfigBean.setIsEnableUsegeReminder(bean.isEnableUsegeReminder());
mAppConfigBean.setUsegeReminderValue(bean.getUsegeReminderValue());
mAppConfigBean.setIsEnableChargeReminder(bean.isEnableChargeReminder());
mAppConfigBean.setChargeReminderValue(bean.getChargeReminderValue());
return mAppConfigBean;
}
public void saveConfigData(final MainActivity activity) {
if (MainActivity._mMainActivity == null) {
/**
* 保存应用配置(内部核心方法,直接持久化)
*/
public void saveAppConfig() {
AppConfigBean.saveBean(mContext, mAppConfigBean);
LogUtils.d(TAG, "saveAppConfig():应用配置保存成功");
}
// ======================== 充电提醒配置方法区(开关+阈值)========================
/**
* 设置充电提醒开关状态
* @param isEnabled 目标状态true=开启false=关闭)
*/
public void setChargeReminderEnabled(final boolean isEnabled) {
LogUtils.d(TAG, String.format("setChargeReminderEnabled() 调用 | 传入状态=%b", isEnabled));
if (isEnabled == mAppConfigBean.isEnableChargeReminder()) {
LogUtils.d(TAG, "setChargeReminderEnabled():充电提醒状态无变化,无需操作");
return;
}
YesNoAlertDialog.show(activity, "应用设置信息", "是否保存应用配置?", new YesNoAlertDialog.OnDialogResultListener(){
@Override
public void onYes() {
saveConfigData();
}
@Override
public void onNo() {
AppConfigUtils.getInstance(activity).loadAppConfigBean();
MainViewFragment.relaodAppConfigs();
}
});
mAppConfigBean.setEnableChargeReminder(isEnabled);
saveAppConfig();
LogUtils.d(TAG, String.format("setChargeReminderEnabled() 成功 | 充电提醒状态=%s", isEnabled ? "开启" : "关闭"));
}
//
// 保存应用配置数据
//
void saveConfigData() {
// 更新配置先取消一下旧的的提醒消息
//NotificationHelper.cancelRemindNotification(mContext);
AppConfigBean.saveBean(mContext, mAppConfigBean);
// 通知活动窗口和服务配置已更新
ControlCenterService.updateStatus(mContext, mAppConfigBean);
MainViewFragment.relaodAppConfigs();
/**
* 获取充电提醒开关状态
* @return 充电提醒状态true=开启false=关闭)
*/
public boolean isChargeReminderEnabled() {
boolean isEnabled = mAppConfigBean.isEnableChargeReminder();
LogUtils.d(TAG, String.format("isChargeReminderEnabled():获取充电提醒状态=%s", isEnabled ? "开启" : "关闭"));
return isEnabled;
}
/**
* 设置充电提醒阈值自动校准0-100
* @param value 目标阈值
*/
public void setChargeReminderValue(final int value) {
LogUtils.d(TAG, String.format("setChargeReminderValue() 调用 | 传入阈值=%d", value));
final int calibratedValue = Math.min(Math.max(value, MIN_REMINDER_VALUE), MAX_REMINDER_VALUE);
if (calibratedValue == mAppConfigBean.getChargeReminderValue()) {
LogUtils.d(TAG, "setChargeReminderValue():充电提醒阈值无变化,无需操作");
return;
}
mAppConfigBean.setChargeReminderValue(calibratedValue);
saveAppConfig();
LogUtils.d(TAG, String.format("setChargeReminderValue() 成功 | 充电提醒阈值=%d%%", calibratedValue));
}
/**
* 获取充电提醒阈值
* @return 充电提醒阈值0-100
*/
public int getChargeReminderValue() {
int value = mAppConfigBean.getChargeReminderValue();
LogUtils.d(TAG, String.format("getChargeReminderValue():获取充电提醒阈值=%d%%", value));
return value;
}
// ======================== 耗电提醒配置方法区(开关+阈值)========================
/**
* 设置耗电提醒开关状态
* @param isEnabled 目标状态true=开启false=关闭)
*/
public void setUsageReminderEnabled(final boolean isEnabled) {
LogUtils.d(TAG, String.format("setUsageReminderEnabled() 调用 | 传入状态=%b", isEnabled));
if (isEnabled == mAppConfigBean.isEnableUsageReminder()) {
LogUtils.d(TAG, "setUsageReminderEnabled():耗电提醒状态无变化,无需操作");
return;
}
mAppConfigBean.setEnableUsageReminder(isEnabled);
saveAppConfig();
LogUtils.d(TAG, String.format("setUsageReminderEnabled() 成功 | 耗电提醒状态=%s", isEnabled ? "开启" : "关闭"));
}
/**
* 获取耗电提醒开关状态
* @return 耗电提醒状态true=开启false=关闭)
*/
public boolean isUsageReminderEnabled() {
boolean isEnabled = mAppConfigBean.isEnableUsageReminder();
LogUtils.d(TAG, String.format("isUsageReminderEnabled():获取耗电提醒状态=%s", isEnabled ? "开启" : "关闭"));
return isEnabled;
}
/**
* 设置耗电提醒阈值自动校准0-100
* @param value 目标阈值
*/
public void setUsageReminderValue(final int value) {
LogUtils.d(TAG, String.format("setUsageReminderValue() 调用 | 传入阈值=%d", value));
final int calibratedValue = Math.min(Math.max(value, MIN_REMINDER_VALUE), MAX_REMINDER_VALUE);
if (calibratedValue == mAppConfigBean.getUsageReminderValue()) {
LogUtils.d(TAG, "setUsageReminderValue():耗电提醒阈值无变化,无需操作");
return;
}
mAppConfigBean.setUsageReminderValue(calibratedValue);
saveAppConfig();
LogUtils.d(TAG, String.format("setUsageReminderValue() 成功 | 耗电提醒阈值=%d%%", calibratedValue));
}
/**
* 获取耗电提醒阈值
* @return 耗电提醒阈值0-100
*/
public int getUsageReminderValue() {
int value = mAppConfigBean.getUsageReminderValue();
LogUtils.d(TAG, String.format("getUsageReminderValue():获取耗电提醒阈值=%d%%", value));
return value;
}
// ======================== 实时电池状态配置方法区(内存缓存,不持久化)========================
/**
* 设置当前充电状态(仅内存缓存)
* @param isCharging 充电状态true=充电中false=未充电)
*/
public void setCharging(boolean isCharging) {
LogUtils.d(TAG, String.format("setCharging() 调用 | 传入状态=%b", isCharging));
if (isCharging == mAppConfigBean.isCharging()) {
LogUtils.d(TAG, "setCharging():充电状态无变化,无需操作");
return;
}
mAppConfigBean.setIsCharging(isCharging);
LogUtils.d(TAG, String.format("setCharging() 成功 | 充电状态=%s", isCharging ? "充电中" : "未充电"));
}
/**
* 获取当前充电状态
* @return 充电状态true=充电中false=未充电)
*/
public boolean isCharging() {
boolean isCharging = mAppConfigBean.isCharging();
LogUtils.d(TAG, String.format("isCharging():获取充电状态=%s", isCharging ? "充电中" : "未充电"));
return isCharging;
}
/**
* 设置当前电池电量仅内存缓存自动校准0-100
* @param value 当前电量
*/
public void setCurrentBatteryValue(int value) {
LogUtils.d(TAG, String.format("setCurrentBatteryValue() 调用 | 传入电量=%d", value));
int calibratedValue = Math.min(Math.max(value, MIN_REMINDER_VALUE), MAX_REMINDER_VALUE);
if (calibratedValue == App.sQuantityOfElectricity) {
LogUtils.d(TAG, "setCurrentBatteryValue():电池电量无变化,无需操作");
return;
}
App.sQuantityOfElectricity = calibratedValue;
LogUtils.d(TAG, String.format("setCurrentBatteryValue() 成功 | 电池电量=%d%%", calibratedValue));
}
/**
* 获取当前电池电量
* @return 当前电池电量0-100
*/
public int getCurrentBatteryValue() {
int value = App.sQuantityOfElectricity;
LogUtils.d(TAG, String.format("getCurrentBatteryValue():获取电池电量=%d%%", value));
return value;
}
// ======================== 间隔配置方法区(持久化)========================
/**
* 设置提醒间隔时间自动校准最小1000ms
* @param interval 目标间隔单位ms
*/
public void setReminderIntervalTime(final int interval) {
LogUtils.d(TAG, String.format("setReminderIntervalTime() 调用 | 传入间隔=%dms", interval));
final int calibratedInterval = Math.max(interval, MIN_INTERVAL_TIME);
if (calibratedInterval == mAppConfigBean.getReminderIntervalTime()) {
LogUtils.d(TAG, "setReminderIntervalTime():提醒间隔无变化,无需操作");
return;
}
mAppConfigBean.setReminderIntervalTime(calibratedInterval);
saveAppConfig();
LogUtils.d(TAG, String.format("setReminderIntervalTime() 成功 | 提醒间隔=%dms", calibratedInterval));
}
/**
* 获取提醒间隔时间
* @return 提醒间隔单位ms
*/
public int getReminderIntervalTime() {
int interval = mAppConfigBean.getReminderIntervalTime();
LogUtils.d(TAG, String.format("getReminderIntervalTime():获取提醒间隔=%dms", interval));
return interval;
}
/**
* 设置电量检测间隔自动校准最小500ms
* @param interval 目标间隔单位ms
*/
public void setBatteryDetectInterval(final int interval) {
LogUtils.d(TAG, String.format("setBatteryDetectInterval() 调用 | 传入间隔=%dms", interval));
final int calibratedInterval = Math.max(interval, MIN_DETECT_INTERVAL);
if (calibratedInterval == mAppConfigBean.getBatteryDetectInterval()) {
LogUtils.d(TAG, "setBatteryDetectInterval():检测间隔无变化,无需操作");
return;
}
mAppConfigBean.setBatteryDetectInterval(calibratedInterval);
saveAppConfig();
LogUtils.d(TAG, String.format("setBatteryDetectInterval() 成功 | 电量检测间隔=%dms", calibratedInterval));
}
/**
* 获取电量检测间隔
* @return 电量检测间隔单位ms
*/
public int getBatteryDetectInterval() {
int interval = mAppConfigBean.getBatteryDetectInterval();
LogUtils.d(TAG, String.format("getBatteryDetectInterval():获取电量检测间隔=%dms", interval));
return interval;
}
// ======================== 服务开关配置方法区独立Bean========================
/**
* 获取服务开关状态
* @return 服务开关状态true=开启false=关闭)
*/
public boolean isServiceEnabled() {
LogUtils.d(TAG, "isServiceEnabled() 调用 | 开始获取服务开关状态");
ControlCenterServiceBean savedServiceBean = (ControlCenterServiceBean) ControlCenterServiceBean.loadBean(mContext, ControlCenterServiceBean.class);
if (savedServiceBean != null) {
boolean isEnabled = savedServiceBean.isEnableService();
LogUtils.d(TAG, String.format("isServiceEnabled():服务开关状态=%b", isEnabled));
return isEnabled;
} else {
ControlCenterServiceBean.saveBean(mContext, new ControlCenterServiceBean(false));
LogUtils.d(TAG, "isServiceEnabled():无已保存服务配置,默认关闭并持久化");
return false;
}
}
/**
* 设置服务开关状态
* @param isServiceEnabled 目标状态true=开启false=关闭)
*/
public void setIsServiceEnabled(boolean isServiceEnabled) {
LogUtils.d(TAG, String.format("setIsServiceEnabled() 调用 | 传入状态=%b", isServiceEnabled));
ControlCenterServiceBean.saveBean(mContext, new ControlCenterServiceBean(isServiceEnabled));
LogUtils.d(TAG, String.format("setIsServiceEnabled() 成功 | 服务开关状态=%b", isServiceEnabled));
}
}

View File

@@ -0,0 +1,144 @@
package cc.winboll.studio.powerbell.utils;
import android.content.Context;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import cc.winboll.studio.libappbase.LogUtils;
/**
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2025/12/11 09:14
* @Describe Assets 目录拷贝工具类
* 支持将 assets/images/ 下所有文件、子目录拷贝到指定路径
* 适配Java7 | API30 | 递归拷贝 | 覆盖写入
*/
public class AssetsCopyUtils {
// ======================== 静态常量区 ========================
public static final String TAG = "AssetsCopyUtils";
private static final int BUFFER_SIZE = 1024 * 8; // 8KB 缓冲区,平衡性能与内存占用
// ======================== 公共快捷方法区(对外入口) ========================
/**
* 拷贝 assets/images/ 目录到指定目标目录
* @param context 上下文
* @param targetDirPath 目标目录完整路径(如 /sdcard/PowerBell/assets_images
* @return 拷贝是否成功
*/
public static boolean copyAssetsImagesToDir(Context context, String targetDirPath) {
LogUtils.d(TAG, "copyAssetsImagesToDir() 调用,目标路径:" + targetDirPath);
// 拷贝 assets/images 根目录
boolean result = copyAssetsDirToDir(context, "images", targetDirPath);
LogUtils.d(TAG, "copyAssetsImagesToDir() 执行完成,结果:" + result);
return result;
}
// ======================== 公共核心方法区(递归拷贝目录) ========================
/**
* 递归拷贝 assets 下指定目录到目标目录
* @param context 上下文
* @param assetsDir assets 下的源目录(如 "images"、"images/subdir"
* @param targetDirPath 目标目录完整路径
* @return 拷贝是否成功
*/
public static boolean copyAssetsDirToDir(Context context, String assetsDir, String targetDirPath) {
LogUtils.d(TAG, "copyAssetsDirToDir() 调用,源目录:" + assetsDir + ",目标路径:" + targetDirPath);
if (context == null) {
LogUtils.e(TAG, "copyAssetsDirToDir() 拷贝失败:上下文为空");
return false;
}
File targetDir = new File(targetDirPath);
// 创建目标目录(含多级父目录)
if (!targetDir.exists() && !targetDir.mkdirs()) {
LogUtils.e(TAG, "copyAssetsDirToDir() 创建目标目录失败:" + targetDirPath);
return false;
}
try {
// 获取 assets 目录下的文件/子目录列表
String[] fileList = context.getAssets().list(assetsDir);
if (fileList == null || fileList.length == 0) {
LogUtils.d(TAG, "copyAssetsDirToDir() assets 目录为空:" + assetsDir);
return true;
}
for (String fileName : fileList) {
String assetsFilePath = assetsDir + File.separator + fileName;
String targetFilePath = targetDirPath + File.separator + fileName;
// 判断当前项是文件还是子目录
String[] subFileList = context.getAssets().list(assetsFilePath);
if (subFileList != null && subFileList.length > 0) {
// 是子目录,递归拷贝
if (!copyAssetsDirToDir(context, assetsFilePath, targetFilePath)) {
LogUtils.e(TAG, "copyAssetsDirToDir() 递归拷贝子目录失败:" + assetsFilePath);
return false;
}
} else {
// 是文件,直接拷贝
if (!copyAssetsFileToDir(context, assetsFilePath, targetFilePath)) {
LogUtils.e(TAG, "copyAssetsDirToDir() 拷贝文件失败:" + assetsFilePath);
return false;
}
}
}
LogUtils.d(TAG, "copyAssetsDirToDir() assets 目录拷贝完成:" + assetsDir + " -> " + targetDirPath);
return true;
} catch (IOException e) {
LogUtils.e(TAG, "copyAssetsDirToDir() 拷贝 assets 目录异常:" + e.getMessage(), e);
return false;
}
}
// ======================== 私有辅助方法区(单个文件拷贝) ========================
/**
* 拷贝 assets 下单个文件到指定路径
* @param context 上下文
* @param assetsFilePath assets 下的文件路径(如 "images/cloud.png"
* @param targetFilePath 目标文件完整路径
* @return 拷贝是否成功
*/
public static boolean copyAssetsFileToDir(Context context, String assetsFilePath, String targetFilePath) {
LogUtils.d(TAG, "copyAssetsFileToDir() 调用,源文件:" + assetsFilePath + ",目标文件:" + targetFilePath);
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = context.getAssets().open(assetsFilePath);
File targetFile = new File(targetFilePath);
// 覆盖已存在的文件
if (targetFile.exists() && !targetFile.delete()) {
LogUtils.w(TAG, "copyAssetsFileToDir() 覆盖目标文件失败,跳过:" + targetFilePath);
return true;
}
outputStream = new FileOutputStream(targetFile);
byte[] buffer = new byte[BUFFER_SIZE];
int length;
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
LogUtils.d(TAG, "copyAssetsFileToDir() 文件拷贝成功:" + assetsFilePath + " -> " + targetFilePath);
return true;
} catch (IOException e) {
LogUtils.e(TAG, "copyAssetsFileToDir() 拷贝文件失败:" + assetsFilePath + ",异常:" + e.getMessage(), e);
return false;
} finally {
// 关闭流
try {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
LogUtils.e(TAG, "copyAssetsFileToDir() 关闭流异常:" + e.getMessage(), e);
}
}
}
}

View File

@@ -1,64 +0,0 @@
package cc.winboll.studio.powerbell.utils;
/**
* @Author ZhanGSKen<zhangsken@qq.com>
* @Date 2024/07/18 12:07:20
* @Describe 背景图片工具集
*/
import android.content.Context;
import cc.winboll.studio.powerbell.beans.BackgroundPictureBean;
import java.io.File;
public class BackgroundPictureUtils {
public static final String TAG = "BackgroundPictureUtils";
static BackgroundPictureUtils _mBackgroundPictureUtils;
Context mContext;
BackgroundPictureBean mBackgroundPictureBean;
// 背景图片目录
String mszBackgroundDir;
BackgroundPictureUtils(Context context) {
mContext = context;
String szExternalFilesDir = mContext.getExternalFilesDir(TAG) + File.separator;
setBackgroundDir(szExternalFilesDir + "Background" + File.separator);
loadBackgroundPictureBean();
}
public static BackgroundPictureUtils getInstance(Context context) {
if (_mBackgroundPictureUtils == null) {
_mBackgroundPictureUtils = new BackgroundPictureUtils(context);
}
return _mBackgroundPictureUtils;
}
//
// 加载应用背景图片配置数据
//
public BackgroundPictureBean loadBackgroundPictureBean() {
mBackgroundPictureBean = BackgroundPictureBean.loadBean(mContext, BackgroundPictureBean.class);
if (mBackgroundPictureBean == null) {
mBackgroundPictureBean = new BackgroundPictureBean();
BackgroundPictureBean.saveBean(mContext, mBackgroundPictureBean);
}
return mBackgroundPictureBean;
}
void setBackgroundDir(String mszBackgroundDir) {
this.mszBackgroundDir = mszBackgroundDir;
}
public String getBackgroundDir() {
return mszBackgroundDir;
}
public BackgroundPictureBean getBackgroundPictureBean() {
return mBackgroundPictureBean;
}
public void saveData() {
BackgroundPictureBean.saveBean(mContext, mBackgroundPictureBean);
}
}

View File

@@ -0,0 +1,805 @@
package cc.winboll.studio.powerbell.utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;
import androidx.core.content.FileProvider;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.ToastUtils;
import cc.winboll.studio.powerbell.BuildConfig;
import cc.winboll.studio.powerbell.models.BackgroundBean;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
/**
* @Author ZhanGSKen<zhangsken@qq.com>
* @Date 2024/07/18 12:07:20
* @Describe 背景图片工具集精简版复用FileUtils聚焦业务逻辑
*/
public class BackgroundSourceUtils {
// ====================== 常量定义(按功能分类置顶)======================
public static final String TAG = "BackgroundSourceUtils";
// FileProvider 授权常量
public static final String FILE_PROVIDER_AUTHORITY = BuildConfig.APPLICATION_ID + ".fileprovider";
// 目录名称常量
private static final String CROP_CACHE_DIR_NAME = "cache";
private static final String SOURCE_DIR_NAME = "BackgroundSource";
private static final String COMPRESS_DIR_NAME = "BackgroundCompress";
private static final String MODEL_DIR_NAME = "ModelDir";
// 文件名称常量
private static final String CURRENT_BEAN_FILE_NAME = "currentBackgroundBean.json";
private static final String PREVIEW_BEAN_FILE_NAME = "previewBackgroundBean.json";
private static final String BLANK_ASSET_PATH = "images/blank100x100.png";
// 图片操作基础目录
private static final String PICTURE_BASE_DIR = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + "PowerBell";
// 压缩常量
private static final int BITMAP_COMPRESS_QUALITY = 80;
private static final Bitmap.CompressFormat COMPRESS_FORMAT = Bitmap.CompressFormat.JPEG;
// ====================== 成员变量(按依赖优先级+功能分类)======================
// 单例实例
private static volatile BackgroundSourceUtils sInstance;
// 上下文(应用级,避免内存泄漏)
private Context mContext;
// 配置文件对象
private File currentBackgroundBeanFile;
private File previewBackgroundBeanFile;
// Bean实例
private BackgroundBean currentBackgroundBean;
private BackgroundBean previewBackgroundBean;
// 目录对象
private File fPictureBaseDir;
private File fCropCacheDir;
private File fBackgroundSourceDir;
private File fBackgroundCompressDir;
private File fUtilsDir;
private File fModelDir;
// 裁剪文件对象
private File mCropSourceFile;
private File mCropResultFile;
// ====================== 单例方法(双重校验锁)======================
private BackgroundSourceUtils(Context context) {
if (sInstance != null) {
throw new RuntimeException("BackgroundSourceUtils 是单例类,禁止重复创建!");
}
this.mContext = context.getApplicationContext();
LogUtils.d(TAG, "【单例初始化】开始初始化必要资源");
initNecessaryDirs();
initAllFiles();
loadSettings();
LogUtils.d(TAG, "【单例初始化】资源初始化完成");
}
public static BackgroundSourceUtils getInstance(Context context) {
if (sInstance == null) {
synchronized (BackgroundSourceUtils.class) {
if (sInstance == null) {
sInstance = new BackgroundSourceUtils(context);
}
}
}
return sInstance;
}
// ====================== 生命周期方法(初始化→加载→保存)======================
/**
* 统一初始化所有必要目录
*/
private void initNecessaryDirs() {
LogUtils.d(TAG, "【目录初始化】开始创建所有必要目录");
initPictureDirs();
initJsonDirs();
LogUtils.d(TAG, "【目录初始化】所有必要目录创建完成");
}
/**
* 初始化图片操作目录
*/
private void initPictureDirs() {
fPictureBaseDir = new File(PICTURE_BASE_DIR);
fBackgroundSourceDir = new File(fPictureBaseDir, SOURCE_DIR_NAME);
fCropCacheDir = new File(fPictureBaseDir, CROP_CACHE_DIR_NAME);
fBackgroundCompressDir = new File(fPictureBaseDir, COMPRESS_DIR_NAME);
createDirWithPermission(fPictureBaseDir, "图片基础目录");
createDirWithPermission(fBackgroundSourceDir, "图片存储目录");
createDirWithPermission(fCropCacheDir, "裁剪缓存目录");
createDirWithPermission(fBackgroundCompressDir, "压缩图存储目录");
validatePictureDirs();
}
/**
* 初始化JSON配置目录
*/
private void initJsonDirs() {
fUtilsDir = mContext.getExternalFilesDir(TAG);
if (fUtilsDir == null) {
LogUtils.e(TAG, "应用外置存储不可用,切换到应用内部缓存目录");
fUtilsDir = mContext.getDataDir();
}
fModelDir = new File(fUtilsDir, MODEL_DIR_NAME);
createDirWithPermission(fModelDir, "JSON配置目录");
currentBackgroundBeanFile = new File(fModelDir, CURRENT_BEAN_FILE_NAME);
previewBackgroundBeanFile = new File(fModelDir, PREVIEW_BEAN_FILE_NAME);
LogUtils.d(TAG, "【配置文件初始化】当前Bean文件" + currentBackgroundBeanFile.getAbsolutePath());
LogUtils.d(TAG, "【配置文件初始化】预览Bean文件" + previewBackgroundBeanFile.getAbsolutePath());
}
/**
* 初始化所有文件
*/
private void initAllFiles() {
clearCropTempFiles();
LogUtils.d(TAG, "【文件初始化】裁剪临时文件已清理");
}
/**
* 加载背景配置
*/
public void loadSettings() {
LogUtils.d(TAG, "【配置加载】开始加载背景配置");
// 加载当前Bean
currentBackgroundBean = BackgroundBean.loadBeanFromFile(currentBackgroundBeanFile.getAbsolutePath(), BackgroundBean.class);
if (currentBackgroundBean == null) {
currentBackgroundBean = new BackgroundBean();
currentBackgroundBean.setPixelColor(ImageUtils.getColorAccent(mContext));
BackgroundBean.saveBeanToFile(currentBackgroundBeanFile.getAbsolutePath(), currentBackgroundBean);
LogUtils.d(TAG, "【配置加载】正式背景Bean不存在已创建新实例");
}
// 加载预览Bean
previewBackgroundBean = BackgroundBean.loadBeanFromFile(previewBackgroundBeanFile.getAbsolutePath(), BackgroundBean.class);
if (previewBackgroundBean == null) {
previewBackgroundBean = new BackgroundBean();
previewBackgroundBean.setPixelColor(ImageUtils.getColorAccent(mContext));
BackgroundBean.saveBeanToFile(previewBackgroundBeanFile.getAbsolutePath(), previewBackgroundBean);
LogUtils.d(TAG, "【配置加载】预览背景Bean不存在已创建新实例");
}
LogUtils.d(TAG, "【配置加载】背景配置加载完成");
}
/**
* 保存配置
*/
public void saveSettings() {
LogUtils.d(TAG, "【配置保存】开始保存背景配置");
if (currentBackgroundBean == null || previewBackgroundBean == null) {
LogUtils.e(TAG, "【配置保存】失败current/preview Bean存在空值");
return;
}
BackgroundBean.saveBeanToFile(currentBackgroundBeanFile.getAbsolutePath(), currentBackgroundBean);
BackgroundBean.saveBeanToFile(previewBackgroundBeanFile.getAbsolutePath(), previewBackgroundBean);
LogUtils.d(TAG, "【配置保存】两份背景配置保存成功");
}
// ====================== 工具方法目录操作→文件操作→Uri转换→图片处理======================
/**
* 创建目录并校验
*/
private void createDirWithPermission(File dir, String dirDesc) {
if (dir == null) {
LogUtils.e(TAG, dirDesc + "创建失败目录对象为null");
return;
}
if (!dir.exists()) {
LogUtils.d(TAG, dirDesc + "不存在,开始创建:" + dir.getAbsolutePath());
dir.mkdirs();
}
if (!dir.exists()) {
LogUtils.e(TAG, dirDesc + "创建失败mkdirs返回false");
}
}
/**
* 校验图片目录是否就绪
*/
private void validatePictureDirs() {
boolean allReady = fPictureBaseDir.exists() && fBackgroundSourceDir.exists()
&& fCropCacheDir.exists() && fBackgroundCompressDir.exists();
if (allReady) {
LogUtils.d(TAG, "【目录校验】所有图片目录均已就绪");
} else {
LogUtils.e(TAG, "【目录校验】部分图片目录未就绪,可能影响后续功能");
}
}
/**
* 清理单个旧文件
*/
private void clearOldFile(File file, String fileDesc) {
if (file == null) {
return;
}
if (file.exists()) {
boolean isDeleted = file.delete();
LogUtils.d(TAG, fileDesc + (isDeleted ? "已删除" : "删除失败") + "" + file.getAbsolutePath());
}
}
/**
* 生成新的裁剪文件名称
*/
String genNewCropFileName() {
String fileName = UUID.randomUUID().toString() + System.currentTimeMillis();
LogUtils.d(TAG, "【文件命名】生成新裁剪文件名:" + fileName);
return fileName;
}
/**
* 将File转为ContentUri
*/
public Uri getFileProviderUri(File file) {
LogUtils.d(TAG, "【Uri转换】开始生成FileProvider Uri文件路径" + (file != null ? file.getAbsolutePath() : "null"));
if (file == null || !file.exists()) {
LogUtils.e(TAG, "【Uri转换】失败文件为空或不存在");
return null;
}
try {
Uri contentUri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
contentUri = FileProvider.getUriForFile(mContext, FILE_PROVIDER_AUTHORITY, file);
LogUtils.d(TAG, "【Uri转换】7.0+ 生成ContentUri" + contentUri.toString());
} else {
contentUri = Uri.fromFile(file);
LogUtils.d(TAG, "【Uri转换】7.0以下 生成FileUri" + contentUri.toString());
}
return contentUri;
} catch (IllegalArgumentException e) {
LogUtils.e(TAG, "【Uri转换】失败" + e.getMessage(), e);
return null;
}
}
/**
* 检查背景是否为空并创建空白背景Bean
*/
public boolean checkEmptyBackgroundAndCreateBlankBackgroundBean(BackgroundBean checkBackgroundBean) {
LogUtils.d(TAG, "【空白背景检查】开始检查背景Bean");
if (checkBackgroundBean == null) {
LogUtils.e(TAG, "【空白背景检查】失败检查Bean为空");
return false;
}
File fCheckBackgroundFile = new File(checkBackgroundBean.getBackgroundFilePath());
if (fCheckBackgroundFile.exists()) {
LogUtils.d(TAG, "【空白背景检查】背景Bean文件存在无需创建空白背景");
return false;
}
LogUtils.d(TAG, "【空白背景检查】背景Bean文件不存在开始创建空白背景");
return createBlankBackgroundBean(checkBackgroundBean.getPixelColor());
}
/**
* 获取目录类型描述
*/
public String getDirTypeDesc(File dir) {
if (dir == null) {
return "未知目录null";
}
String dirPath = dir.getAbsolutePath();
String publicPicturePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath();
String externalFilesPath = mContext.getExternalFilesDir(null) != null ? mContext.getExternalFilesDir(null).getAbsolutePath() : "";
String cachePath = mContext.getCacheDir().getAbsolutePath();
if (!TextUtils.isEmpty(publicPicturePath)) {
if (dirPath.contains(publicPicturePath + File.separator + "PowerBell" + File.separator + COMPRESS_DIR_NAME)) {
return "系统公共图片目录(/Pictures/PowerBell/BackgroundCompress压缩图统一存储目录";
} else if (dirPath.contains(publicPicturePath + File.separator + "PowerBell")) {
return "系统公共图片目录(/Pictures/PowerBell图片存储/裁剪目录)";
}
} else if (!TextUtils.isEmpty(externalFilesPath) && dirPath.contains(externalFilesPath)) {
return "应用私有外部目录getExternalFilesDir()JSON配置目录";
} else if (dirPath.contains(cachePath)) {
return "应用内部缓存目录getCacheDir(),兜底目录)";
} else {
return "外部存储目录(非应用私有,权限受限)";
}
return "未知目录";
}
/**
* 获取图片旋转角度
*/
public int getImageRotateAngle(String imagePath) {
LogUtils.d(TAG, "【图片旋转角度】开始获取图片旋转角度,路径:" + imagePath);
if (TextUtils.isEmpty(imagePath)) {
LogUtils.e(TAG, "【图片旋转角度】失败:图片路径为空");
return 0;
}
File imageFile = new File(imagePath);
if (!imageFile.exists() || !imageFile.isFile() || imageFile.length() <= 0) {
LogUtils.e(TAG, "【图片旋转角度】失败:图片文件无效:" + imagePath);
return 0;
}
InputStream inputStream = null;
try {
inputStream = new FileInputStream(imageFile);
ExifInterface exifInterface = new ExifInterface(inputStream);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
LogUtils.d(TAG, "【图片旋转角度】90度");
return 90;
case ExifInterface.ORIENTATION_ROTATE_180:
LogUtils.d(TAG, "【图片旋转角度】180度");
return 180;
case ExifInterface.ORIENTATION_ROTATE_270:
LogUtils.d(TAG, "【图片旋转角度】270度");
return 270;
default:
LogUtils.d(TAG, "【图片旋转角度】0度正常");
return 0;
}
} catch (IOException e) {
LogUtils.w(TAG, "【图片旋转角度】读取EXIF异常" + e.getMessage());
return 0;
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
LogUtils.e(TAG, "【图片旋转角度】流关闭失败:" + e.getMessage());
}
}
}
}
// ====================== 核心业务方法(按功能分类)======================
/**
* 创建空白背景Bean
*/
public boolean createBlankBackgroundBean(int nBackgroundPixelColor) {
LogUtils.d(TAG, "【空白背景创建】开始创建空白背景,像素颜色:" + String.format("#%08X", nBackgroundPixelColor));
String newCropFileName = genNewCropFileName();
String fileSuffix = "png";
mCropSourceFile = new File(fCropCacheDir, newCropFileName + "." + fileSuffix);
mCropResultFile = new File(fCropCacheDir, "SelectCompress_" + newCropFileName + "." + fileSuffix);
// 复制空白图片资源
AssetsCopyUtils.copyAssetsFileToDir(mContext, BLANK_ASSET_PATH, mCropSourceFile.getAbsolutePath());
LogUtils.d(TAG, "【空白背景创建】空白图片已复制到:" + mCropSourceFile.getAbsolutePath());
// 创建结果文件
try {
mCropResultFile.createNewFile();
LogUtils.d(TAG, "【空白背景创建】结果文件已创建:" + mCropResultFile.getAbsolutePath());
} catch (IOException e) {
LogUtils.e(TAG, "【空白背景创建】结果文件创建失败:" + e.getMessage());
return false;
}
// 更新预览Bean
loadSettings();
previewBackgroundBean.setPixelColor(nBackgroundPixelColor);
previewBackgroundBean.setIsUseBackgroundFile(true);
previewBackgroundBean.setIsUseBackgroundScaledCompressFile(false);
previewBackgroundBean.setBackgroundFileName(mCropSourceFile.getName());
previewBackgroundBean.setBackgroundFilePath(mCropSourceFile.getAbsolutePath());
previewBackgroundBean.setBackgroundScaledCompressFileName(mCropResultFile.getName());
previewBackgroundBean.setBackgroundScaledCompressFilePath(mCropResultFile.getAbsolutePath());
saveSettings();
LogUtils.d(TAG, "【空白背景创建】空白背景创建成功并更新配置");
return true;
}
/**
* 创建并更新预览剪裁环境
*/
public boolean createAndUpdatePreviewEnvironmentForCropping(BackgroundBean oldPreviewBackgroundBean) {
LogUtils.d(TAG, "【预览剪裁环境】开始初始化预览剪裁环境");
if (oldPreviewBackgroundBean == null) {
LogUtils.e(TAG, "【预览剪裁环境】失败旧预览Bean为空");
return false;
}
InputStream is = null;
FileOutputStream fos = null;
try {
clearCropTempFiles();
// 检查并创建空白背景
if (checkEmptyBackgroundAndCreateBlankBackgroundBean(oldPreviewBackgroundBean)) {
LogUtils.d(TAG, "【预览剪裁环境】空白背景创建成功,直接返回");
return true;
}
// 获取Uri和文件后缀
Uri uri = UriUtils.getUriForFile(mContext, oldPreviewBackgroundBean.getBackgroundFilePath());
LogUtils.d(TAG, "【预览剪裁环境】原Uri" + uri);
String fileSuffix = UriUtils.getSuffixFromUri(mContext, uri);
LogUtils.d(TAG, "【预览剪裁环境】文件后缀:" + fileSuffix);
// 初始化裁剪文件
String newCropFileName = genNewCropFileName();
mCropSourceFile = new File(fCropCacheDir, newCropFileName + "." + fileSuffix);
mCropResultFile = new File(fCropCacheDir, "SelectCompress_" + newCropFileName + ".png");
LogUtils.d(TAG, "【预览剪裁环境】裁剪数据源:" + mCropSourceFile.getAbsolutePath());
LogUtils.d(TAG, "【预览剪裁环境】裁剪结果文件:" + mCropResultFile.getAbsolutePath());
// 复制压缩文件
if (FileUtils.isFileExists(oldPreviewBackgroundBean.getBackgroundScaledCompressFilePath())) {
FileUtils.copyFile(new File(oldPreviewBackgroundBean.getBackgroundScaledCompressFilePath()), mCropResultFile);
LogUtils.d(TAG, "【预览剪裁环境】已复制旧压缩文件");
} else {
mCropResultFile.createNewFile();
LogUtils.d(TAG, "【预览剪裁环境】旧压缩文件不存在,已创建新文件");
}
// 复制源文件
if (FileUtils.isFileExists(oldPreviewBackgroundBean.getBackgroundFilePath())) {
FileUtils.copyFile(new File(oldPreviewBackgroundBean.getBackgroundFilePath()), mCropSourceFile);
LogUtils.d(TAG, "【预览剪裁环境】已复制旧源文件");
} else {
mCropSourceFile.createNewFile();
is = mContext.getContentResolver().openInputStream(uri);
if (is == null) {
LogUtils.e(TAG, "【预览剪裁环境】ContentResolver打开Uri失败" + uri.toString());
return false;
}
fos = new FileOutputStream(mCropSourceFile);
byte[] buffer = new byte[1024 * 8];
int readLen;
while ((readLen = is.read(buffer)) != -1) {
fos.write(buffer, 0, readLen);
}
fos.flush();
try {
fos.getFD().sync();
} catch (IOException e) {
LogUtils.w(TAG, "【预览剪裁环境】文件同步到磁盘失败flush兜底" + e.getMessage());
fos.flush();
}
LogUtils.d(TAG, "【预览剪裁环境】已从Uri读取并写入源文件");
}
// 更新预览Bean
loadSettings();
previewBackgroundBean.setBackgroundFileName(mCropSourceFile.getName());
previewBackgroundBean.setBackgroundFilePath(mCropSourceFile.getAbsolutePath());
previewBackgroundBean.setBackgroundScaledCompressFileName(mCropResultFile.getName());
previewBackgroundBean.setBackgroundScaledCompressFilePath(mCropResultFile.getAbsolutePath());
saveSettings();
LogUtils.d(TAG, "【预览剪裁环境】预览剪裁环境初始化成功");
return true;
} catch (Exception e) {
LogUtils.e(TAG, "【预览剪裁环境】初始化异常:" + e.getMessage(), e);
clearCropTempFiles();
return false;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
LogUtils.e(TAG, "【预览剪裁环境】输入流关闭失败:" + e.getMessage());
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
LogUtils.e(TAG, "【预览剪裁环境】输出流关闭失败:" + e.getMessage());
}
}
}
}
/**
* 保存裁剪结果图到预览Bean
*/
public BackgroundBean saveFileToPreviewBean(File sourceFile, String fileInfo) {
LogUtils.d(TAG, "【裁剪结果保存】开始保存裁剪结果到预览Bean源文件路径" + (sourceFile != null ? sourceFile.getAbsolutePath() : "null"));
if (sourceFile == null || !sourceFile.exists() || sourceFile.length() <= 0) {
LogUtils.e(TAG, "【裁剪结果保存】失败:源文件无效");
return previewBackgroundBean;
}
// 检查是否为原图目录
String originalImageDir = mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath();
if (sourceFile.getAbsolutePath().contains(originalImageDir)) {
LogUtils.w(TAG, "【裁剪结果保存】禁止复制原图,跳过保存");
return previewBackgroundBean;
}
// 确保目录存在
if (!fBackgroundSourceDir.exists() && !fBackgroundSourceDir.mkdirs()) {
LogUtils.e(TAG, "【裁剪结果保存】失败BackgroundSource目录创建失败");
return previewBackgroundBean;
}
// 生成唯一文件名并复制
String uniqueFileName = "bg_" + System.currentTimeMillis() + "_" + sourceFile.getName();
File targetFile = new File(fBackgroundSourceDir, uniqueFileName);
if (FileUtils.copyFile(sourceFile, targetFile)) {
LogUtils.d(TAG, "【裁剪结果保存】裁剪结果图保存成功:" + targetFile.getAbsolutePath());
// 更新预览Bean
previewBackgroundBean.setBackgroundFileName(uniqueFileName);
previewBackgroundBean.setBackgroundFilePath(targetFile.getAbsolutePath());
previewBackgroundBean.setBackgroundFileInfo(fileInfo);
previewBackgroundBean.setIsUseBackgroundFile(true);
saveSettings();
} else {
LogUtils.e(TAG, "【裁剪结果保存】失败:裁剪结果图复制失败");
}
return previewBackgroundBean;
}
/**
* 提交预览背景到正式背景
*/
public void commitPreviewSourceToCurrent() {
LogUtils.d(TAG, "【背景提交】开始深拷贝预览Bean到正式Bean");
// 深拷贝Bean属性
currentBackgroundBean = new BackgroundBean();
currentBackgroundBean.setPixelColor(ImageUtils.getColorAccent(mContext));
copyBackgroundBeanProperties(previewBackgroundBean, currentBackgroundBean);
// 复制文件
String previewFileName = previewBackgroundBean.getBackgroundFileName();
String previewCropFileName = previewBackgroundBean.getBackgroundScaledCompressFileName();
File previewFile = new File(previewBackgroundBean.getBackgroundFilePath());
File previewCropFile = new File(previewBackgroundBean.getBackgroundScaledCompressFilePath());
File currentFile = new File(fBackgroundSourceDir, previewFileName);
File currentCropFile = new File(fBackgroundCompressDir, previewCropFileName);
FileUtils.copyFile(previewFile, currentFile);
FileUtils.copyFile(previewCropFile, currentCropFile);
// 更新文件路径
currentBackgroundBean.setBackgroundFilePath(currentFile.getAbsolutePath());
currentBackgroundBean.setBackgroundScaledCompressFilePath(currentCropFile.getAbsolutePath());
saveSettings();
LogUtils.d(TAG, "【背景提交】预览背景提交到正式背景成功,两份实例完全独立");
ToastUtils.show("背景图片应用成功");
}
/**
* 将正式背景同步到预览背景
*/
public void setCurrentSourceToPreview() {
LogUtils.d(TAG, "【背景同步】开始深拷贝正式Bean到预览Bean");
// 深拷贝Bean属性
previewBackgroundBean = new BackgroundBean();
previewBackgroundBean.setPixelColor(ImageUtils.getColorAccent(mContext));
copyBackgroundBeanProperties(currentBackgroundBean, previewBackgroundBean);
saveSettings();
LogUtils.d(TAG, "【背景同步】正式背景同步到预览背景成功");
}
/**
* 清理裁剪临时文件
*/
void clearCropTempFiles() {
LogUtils.d(TAG, "【裁剪文件清理】开始清理裁剪临时文件");
File[] files = fCropCacheDir.listFiles();
if (files == null) {
LogUtils.d(TAG, "【裁剪文件清理】裁剪缓存目录为空,无需清理");
return;
}
for (File file : files) {
clearOldFile(file, "旧裁剪缓存文件");
}
mCropSourceFile = null;
mCropResultFile = null;
LogUtils.d(TAG, "【裁剪文件清理】裁剪临时文件清理完成");
}
/**
* 复制文件
*/
public boolean copyFile(File source, File target) {
LogUtils.d(TAG, "【文件复制】开始复制文件,源文件:" + (source != null ? source.getAbsolutePath() : "null") + " 目标:" + (target != null ? target.getAbsolutePath() : "null"));
if (source == null || TextUtils.isEmpty(source.getPath()) || (source.exists() && source.length() <= 0)) {
if (target == null) {
LogUtils.e(TAG, "【文件复制】失败目标对象为null");
return false;
}
File targetDir = target.isFile() ? target.getParentFile() : target;
createDirWithPermission(targetDir, "空源文件场景-目录创建");
LogUtils.d(TAG, "【文件复制】空源文件场景,目录创建完成");
return true;
}
boolean isSuccess = FileUtils.copyFile(source, target);
LogUtils.d(TAG, "【文件复制】" + (isSuccess ? "成功" : "失败"));
return isSuccess;
}
/**
* 迁移旧压缩图路径到新目录
*/
private void migrateCompressPathToNewDir(BackgroundBean bean, boolean isCurrentBean) {
LogUtils.d(TAG, "【路径迁移】开始迁移" + (isCurrentBean ? "正式" : "预览") + "Bean压缩路径");
if (bean == null) {
LogUtils.e(TAG, "【路径迁移】失败Bean为空");
return;
}
String oldCompressPath = bean.getBackgroundScaledCompressFilePath();
String beanType = isCurrentBean ? "正式Bean" : "预览Bean";
if (TextUtils.isEmpty(oldCompressPath) || oldCompressPath.contains(fBackgroundCompressDir.getAbsolutePath())) {
LogUtils.d(TAG, "【路径迁移】" + beanType + "无需迁移:旧路径为空或已在目标目录");
return;
}
File oldCompressFile = new File(oldCompressPath);
if (!oldCompressFile.exists() || !oldCompressFile.isFile() || oldCompressFile.length() <= 0) {
LogUtils.w(TAG, "【路径迁移】" + beanType + "旧压缩文件无效,无需迁移:" + oldCompressPath);
String compressFileName = bean.getBackgroundScaledCompressFileName();
if (!TextUtils.isEmpty(compressFileName)) {
File newCompressFile = new File(fBackgroundCompressDir, compressFileName);
bean.setBackgroundScaledCompressFilePath(newCompressFile.getAbsolutePath());
saveSettings();
LogUtils.d(TAG, "【路径迁移】" + beanType + "压缩路径已重置到目标目录");
}
return;
}
String compressFileName = bean.getBackgroundScaledCompressFileName();
if (TextUtils.isEmpty(compressFileName)) {
compressFileName = "ScaledCompress_" + System.currentTimeMillis() + ".jpg";
}
File newCompressFile = new File(fBackgroundCompressDir, compressFileName);
boolean copySuccess = FileUtils.copyFile(oldCompressFile, newCompressFile);
if (copySuccess) {
bean.setBackgroundScaledCompressFilePath(newCompressFile.getAbsolutePath());
saveSettings();
clearOldFile(oldCompressFile, beanType + "旧压缩文件(迁移后清理)");
LogUtils.d(TAG, "【路径迁移】" + beanType + "压缩路径迁移成功:" + oldCompressPath + "" + newCompressFile.getAbsolutePath());
} else {
LogUtils.e(TAG, "【路径迁移】" + beanType + "压缩文件复制失败,迁移终止");
}
}
/**
* 压缩图片并保存(默认路径)
*/
public void compressQualityToRecivedPicture(Bitmap bitmap) {
LogUtils.d(TAG, "【图片压缩】使用默认路径压缩图片");
String defaultCompressPath = getPreviewBackgroundScaledCompressFilePath();
compressQualityToRecivedPicture(bitmap, defaultCompressPath);
}
/**
* 压缩图片并保存(指定路径)
*/
public void compressQualityToRecivedPicture(Bitmap bitmap, String targetCompressPath) {
LogUtils.d(TAG, "【图片压缩】指定路径压缩图片,目标路径:" + targetCompressPath);
if (bitmap == null || bitmap.isRecycled()) {
ToastUtils.show("压缩失败:图片为空");
LogUtils.e(TAG, "【图片压缩】失败Bitmap为空或已回收");
return;
}
if (TextUtils.isEmpty(targetCompressPath)) {
ToastUtils.show("压缩失败:目标路径为空");
LogUtils.e(TAG, "【图片压缩】失败:目标路径为空");
return;
}
OutputStream outStream = null;
FileOutputStream fos = null;
try {
LogUtils.d(TAG, "【图片压缩】Bitmap原始大小" + bitmap.getByteCount() / 1024 + "KB");
File targetCompressFile = new File(targetCompressPath);
if (targetCompressFile.exists()) {
targetCompressFile.delete();
LogUtils.d(TAG, "【图片压缩】已删除旧压缩文件");
}
targetCompressFile.createNewFile();
fos = new FileOutputStream(targetCompressFile);
outStream = new BufferedOutputStream(fos);
boolean compressSuccess = bitmap.compress(COMPRESS_FORMAT, BITMAP_COMPRESS_QUALITY, outStream);
outStream.flush();
try {
fos.getFD().sync();
LogUtils.d(TAG, "【图片压缩】图片已强制同步到磁盘");
} catch (IOException e) {
LogUtils.w(TAG, "【图片压缩】sync失败flush兜底" + e.getMessage());
outStream.flush();
}
LogUtils.d(TAG, "【图片压缩】" + (compressSuccess ? "成功" : "失败") + ",大小:" + targetCompressFile.length() / 1024 + "KB");
ToastUtils.show(compressSuccess ? "图片压缩成功" : "图片压缩失败");
} catch (IOException e) {
LogUtils.e(TAG, "【图片压缩】IO异常" + e.getMessage(), e);
ToastUtils.show("图片压缩失败");
} finally {
if (outStream != null) {
try {
outStream.close();
} catch (IOException e) {
LogUtils.e(TAG, "【图片压缩】BufferedOutputStream关闭失败" + e.getMessage());
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
LogUtils.e(TAG, "【图片压缩】FileOutputStream关闭失败" + e.getMessage());
}
}
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
LogUtils.d(TAG, "【图片压缩】Bitmap已回收");
}
}
}
// ====================== 辅助方法(属性拷贝)======================
/**
* 拷贝BackgroundBean属性深拷贝
*/
private void copyBackgroundBeanProperties(BackgroundBean source, BackgroundBean target) {
target.setBackgroundFileName(source.getBackgroundFileName());
target.setBackgroundFilePath(source.getBackgroundFilePath());
target.setBackgroundFileInfo(source.getBackgroundFileInfo());
target.setIsUseBackgroundFile(source.isUseBackgroundFile());
target.setBackgroundScaledCompressFileName(source.getBackgroundScaledCompressFileName());
target.setBackgroundScaledCompressFilePath(source.getBackgroundScaledCompressFilePath());
target.setIsUseBackgroundScaledCompressFile(source.isUseBackgroundScaledCompressFile());
target.setBackgroundWidth(source.getBackgroundWidth());
target.setBackgroundHeight(source.getBackgroundHeight());
target.setPixelColor(source.getPixelColor());
}
// ====================== 对外提供的getter方法 ======================
public BackgroundBean getCurrentBackgroundBean() {
return currentBackgroundBean;
}
public BackgroundBean getPreviewBackgroundBean() {
return previewBackgroundBean;
}
public String getPreviewBackgroundScaledCompressFilePath() {
String compressFileName = previewBackgroundBean.getBackgroundScaledCompressFileName();
if (TextUtils.isEmpty(compressFileName)) {
LogUtils.e(TAG, "【路径获取】预览压缩背景文件名为空");
return "";
}
File file = new File(fBackgroundCompressDir, compressFileName);
return file.getAbsolutePath();
}
public String getCurrentBackgroundScaledCompressFilePath() {
String compressFileName = currentBackgroundBean.getBackgroundScaledCompressFileName();
if (TextUtils.isEmpty(compressFileName)) {
LogUtils.e(TAG, "【路径获取】正式压缩背景文件名为空");
return "";
}
File file = new File(fBackgroundCompressDir, compressFileName);
return file.getAbsolutePath();
}
public String getBackgroundSourceDirPath() {
return fBackgroundSourceDir.getAbsolutePath();
}
public String getBackgroundCompressDirPath() {
return fBackgroundCompressDir.getAbsolutePath();
}
public String getCropCacheDir() {
return fCropCacheDir.getAbsolutePath();
}
public String getFileProviderAuthority() {
return FILE_PROVIDER_AUTHORITY;
}
}

View File

@@ -1,28 +1,82 @@
package cc.winboll.studio.powerbell.utils;
/**
* @Author ZhanGSKen<zhangsken@qq.com>
* @Date 2024/07/18 04:32:46
* @Describe 电池工具类
*/
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import cc.winboll.studio.libappbase.LogUtils;
/**
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2024/07/18 04:32:46
* @Describe 电池状态工具类
* 功能解析电池广播Intent获取充电状态、当前电量
* 适配Java7 | API30 | 小米手机
*/
public class BatteryUtils {
// ================================== 静态常量区(置顶归类,消除魔法值)=================================
public static final String TAG = "BatteryUtils";
// 电池电量计算常量
private static final int BATTERY_SCALE_DEFAULT = 100; // 电量刻度默认值
private static final int BATTERY_LEVEL_MIN = 0; // 电量百分比最小值
private static final int BATTERY_LEVEL_MAX = 100; // 电量百分比最大值
private static final int EXTRA_STATUS_DEFAULT = -1; // 电池状态默认值
// ================================== 工具方法(静态方法,无状态设计)=================================
/**
* 判断当前是否处于充电状态
* @param intent 电池状态广播Intent非空
* @return true=充电中/已充满false=未充电
*/
public static boolean isCharging(Intent intent) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
LogUtils.d(TAG, "【isCharging】调用开始");
// 入参非空校验
if (intent == null) {
LogUtils.e(TAG, "【isCharging】入参异常intent为空返回false");
return false;
}
// 解析电池状态
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, EXTRA_STATUS_DEFAULT);
LogUtils.d(TAG, "【isCharging】解析电池状态status=" + status);
// 判断充电状态(充电中/已充满均视为充电状态)
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING
|| status == BatteryManager.BATTERY_STATUS_FULL;
LogUtils.d(TAG, "【isCharging】调用结束 | 充电状态=" + isCharging);
return isCharging;
}
public static int getTheQuantityOfElectricity(Intent intent) {
int intLevel = intent.getIntExtra("level", 0);
int intScale = intent.getIntExtra("scale", 100);
return intLevel * 100 / intScale;
/**
* 获取当前电池电量百分比0-100
* @param intent 电池状态广播Intent非空
* @return 电量百分比异常返回0
*/
public static int getCurrentBatteryLevel(Intent intent) {
LogUtils.d(TAG, "【getCurrentBatteryLevel】调用开始");
// 入参非空校验
if (intent == null) {
LogUtils.e(TAG, "【getCurrentBatteryLevel】入参异常intent为空返回0");
return BATTERY_LEVEL_MIN;
}
// 解析电量原始值与刻度值
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, BATTERY_LEVEL_MIN);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, BATTERY_SCALE_DEFAULT);
LogUtils.d(TAG, "【getCurrentBatteryLevel】解析原始数据 | level=" + level + " | scale=" + scale);
// 计算并校验电量百分比避免除以0或数值越界
int batteryLevel;
if (scale <= 0) {
LogUtils.w(TAG, "【getCurrentBatteryLevel】刻度值无效scale=" + scale + "直接使用level值");
batteryLevel = level;
} else {
batteryLevel = level * BATTERY_SCALE_DEFAULT / scale;
}
// 确保电量值在0-100范围内
batteryLevel = Math.max(BATTERY_LEVEL_MIN, Math.min(batteryLevel, BATTERY_LEVEL_MAX));
LogUtils.d(TAG, "【getCurrentBatteryLevel】调用结束 | 电量百分比=" + batteryLevel + "%");
return batteryLevel;
}
}

View File

@@ -0,0 +1,491 @@
package cc.winboll.studio.powerbell.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.text.TextUtils;
import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.powerbell.App;
import java.io.File;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
* @Date 2025/12/11 01:57
* @Describe 单例 Bitmap 缓存工具类Java 7 兼容)- 极致强制缓存版(无图片压缩)
* 功能:内存缓存 Bitmap支持路径关联缓存、全局获取、缓存清空、SP 持久化最后缓存路径、构造时预加载
* 特点1. 单例模式 2. 硬引用唯一缓存(极致强制保持,任何情况不自动回收) 3. 路径-Bitmap 映射 4. 线程安全
* 5. SP 持久化最后缓存路径 6. 构造时预加载 7. 引用计数防误回收 8. 无图片压缩,保留原始品质
* 核心策略无论内存如何紧张强制保持已缓存的Bitmap保留图片原始品质永不自动清理
*/
public class BitmapCacheUtils {
// ================================== 静态常量区(置顶归类,消除魔法值)=================================
public static final String TAG = "BitmapCacheUtils";
// SP 相关常量
private static final String SP_NAME = "BitmapCacheSP";
private static final String SP_KEY_LAST_CACHE_PATH = "last_cache_image_path";
// Bitmap 解码常量
private static final int BITMAP_SAMPLE_SIZE_ORIGINAL = 1; // 无压缩采样率
private static final Bitmap.Config BITMAP_CONFIG_DEFAULT = Bitmap.Config.ARGB_8888; // 全彩品质配置
// ================================== 成员变量按功能分类volatile 保证多线程可见性)=================================
// 单例实例
private static volatile BitmapCacheUtils sInstance;
// 路径-Bitmap 硬引用缓存(极致强制保持,永不自动回收)
private final Map<String, Bitmap> mHardCacheMap;
// 路径-引用计数 映射(仅统计,不影响缓存生命周期)
private final Map<String, Integer> mRefCountMap;
// SP 实例(用于持久化最后缓存路径)
private final SharedPreferences mSp;
// ================================== 单例方法(双重校验锁,线程安全)=================================
/**
* 私有构造器(单例模式)
*/
private BitmapCacheUtils() {
LogUtils.d(TAG, "【BitmapCacheUtils】单例构造开始");
//App.notifyMessage(TAG, "【BitmapCacheUtils】单例构造开始");
// 使用 ConcurrentHashMap 保证线程安全,避免手动同步
mHardCacheMap = new ConcurrentHashMap<>();
mRefCountMap = new ConcurrentHashMap<>();
// 初始化 SP使用 App 全局上下文,避免内存泄漏)
mSp = App.getInstance().getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
// 构造时自动预加载 SP 中保存的最后一次缓存路径的图片
preloadLastCachedBitmap();
// 注册内存状态监听(仅记录日志,不清理缓存)
registerMemoryStatusListener();
LogUtils.d(TAG, "【BitmapCacheUtils】单例构造完成极致强制缓存策略已启用");
}
/**
* 获取单例实例(双重校验锁,线程安全)
*/
public static BitmapCacheUtils getInstance() {
if (sInstance == null) {
synchronized (BitmapCacheUtils.class) {
if (sInstance == null) {
sInstance = new BitmapCacheUtils();
}
}
}
return sInstance;
}
// ================================== 对外监控接口App 类调用专用)=================================
/**
* 获取当前缓存的 Bitmap 数量
* @return 缓存的 Bitmap 数量
*/
public int getCacheCount() {
int count = mHardCacheMap.size();
LogUtils.d(TAG, "【getCacheCount】当前缓存 Bitmap 数量 - " + count);
return count;
}
/**
* 获取当前缓存的所有图片路径集合
* @return 路径集合
*/
public Set<String> getCachedPaths() {
Set<String> paths = mHardCacheMap.keySet();
LogUtils.d(TAG, "【getCachedPaths】当前缓存路径数量 - " + paths.size());
return paths;
}
/**
* 估算当前缓存的总内存占用(单位:字节)
* @return 总内存占用
*/
public long getTotalCacheSize() {
long totalSize = 0;
for (Bitmap bitmap : mHardCacheMap.values()) {
if (isBitmapValid(bitmap)) {
if (Build.VERSION.SDK_INT >= 12) {
totalSize += bitmap.getByteCount();
} else {
totalSize += bitmap.getRowBytes() * bitmap.getHeight();
}
}
}
LogUtils.d(TAG, "【getTotalCacheSize】当前缓存总内存占用 - " + totalSize + " 字节");
return totalSize;
}
// ================================== 对外核心接口:缓存操作(无压缩)=================================
/**
* 直接缓存已解码的 Bitmap适配 BackgroundView 改进需求)
* @param imagePath 图片绝对路径
* @param bitmap 已解码的有效 Bitmap
* @return 缓存后的 Bitmap / null参数无效
*/
public Bitmap cacheBitmap(String imagePath, Bitmap bitmap) {
LogUtils.d(TAG, "【cacheBitmap】调用开始直接缓存已解码 Bitmap| 路径=" + imagePath);
// 入参非空校验
if (TextUtils.isEmpty(imagePath) || !isBitmapValid(bitmap)) {
LogUtils.e(TAG, "【cacheBitmap】入参异常路径为空或 Bitmap 无效");
return null;
}
// 极致强制:直接存入硬引用缓存,覆盖旧值(若存在)
mHardCacheMap.put(imagePath, bitmap);
// 初始化引用计数为1若不存在
mRefCountMap.putIfAbsent(imagePath, 1);
// 持久化当前路径到 SP
saveLastCachePathToSp(imagePath);
LogUtils.d(TAG, "【cacheBitmap】调用成功直接缓存已解码 Bitmap| 路径=" + imagePath);
return bitmap;
}
/**
* 根据图片路径缓存 Bitmap 到内存,并持久化路径到 SP
* @param imagePath 图片绝对路径
* @return 缓存成功的 Bitmap / null路径无效/文件不存在/解码失败)
*/
public Bitmap cacheBitmap(String imagePath) {
LogUtils.d(TAG, "【cacheBitmap】调用开始路径缓存| 路径=" + imagePath);
// 入参非空校验
if (TextUtils.isEmpty(imagePath)) {
LogUtils.e(TAG, "【cacheBitmap】入参异常图片路径为空");
return null;
}
// 文件有效性校验
File imageFile = new File(imagePath);
if (!imageFile.exists() || !imageFile.isFile() || imageFile.length() <= 0) {
LogUtils.e(TAG, "【cacheBitmap】文件无效不存在/非文件/空文件 | 路径=" + imagePath);
return null;
}
// 已缓存则直接返回,避免重复加载
Bitmap hardCacheBitmap = mHardCacheMap.get(imagePath);
if (isBitmapValid(hardCacheBitmap)) {
LogUtils.d(TAG, "【cacheBitmap】硬引用缓存命中引用计数+1 | 路径=" + imagePath);
// 引用计数+1
increaseRefCount(imagePath);
// 持久化当前路径到 SP
saveLastCachePathToSp(imagePath);
LogUtils.d(TAG, "【cacheBitmap】调用成功缓存命中| 路径=" + imagePath);
return hardCacheBitmap;
}
// 无压缩解码 Bitmap保留原始品质
Bitmap bitmap = decodeOriginalBitmap(imagePath);
if (bitmap != null) {
// 极致强制:存入硬引用缓存,永不自动回收
mHardCacheMap.put(imagePath, bitmap);
// 初始化引用计数为1
mRefCountMap.put(imagePath, 1);
// 持久化当前路径到 SP
saveLastCachePathToSp(imagePath);
LogUtils.d(TAG, "【cacheBitmap】调用成功新缓存| 路径=" + imagePath);
} else {
LogUtils.e(TAG, "【cacheBitmap】调用失败图片解码失败 | 路径=" + imagePath);
}
return bitmap;
}
/**
* 根据路径获取缓存的 Bitmap
* @param imagePath 图片绝对路径
* @return 缓存的有效 Bitmap / null未缓存/已回收)
*/
public Bitmap getCachedBitmap(String imagePath) {
LogUtils.d(TAG, "【getCachedBitmap】调用开始 | 路径=" + imagePath);
// 入参非空校验
if (TextUtils.isEmpty(imagePath)) {
LogUtils.e(TAG, "【getCachedBitmap】入参异常图片路径为空");
return null;
}
// 仅从硬引用缓存获取,无任何 fallback
Bitmap hardCacheBitmap = mHardCacheMap.get(imagePath);
if (isBitmapValid(hardCacheBitmap)) {
LogUtils.d(TAG, "【getCachedBitmap】调用成功缓存命中| 路径=" + imagePath);
return hardCacheBitmap;
}
// 缓存未命中或 Bitmap 已失效(极致强制策略下,理论上不会出现已回收情况)
LogUtils.w(TAG, "【getCachedBitmap】调用失败缓存未命中或 Bitmap 已失效 | 路径=" + imagePath);
return null;
}
// ================================== 对外接口:引用计数管理(仅统计,不影响缓存)=================================
/**
* 增加指定路径 Bitmap 的引用计数
* @param imagePath 图片绝对路径
*/
public void increaseRefCount(String imagePath) {
LogUtils.d(TAG, "【increaseRefCount】调用开始 | 路径=" + imagePath);
if (TextUtils.isEmpty(imagePath)) {
LogUtils.e(TAG, "【increaseRefCount】入参异常图片路径为空");
return;
}
synchronized (mRefCountMap) {
Integer count = mRefCountMap.get(imagePath);
if (count == null) {
mRefCountMap.put(imagePath, 1);
} else {
mRefCountMap.put(imagePath, count + 1);
}
int newCount = mRefCountMap.get(imagePath);
LogUtils.d(TAG, "【increaseRefCount】调用成功 | 路径=" + imagePath + " | 引用计数=" + newCount);
}
}
/**
* 减少指定路径 Bitmap 的引用计数计数为0时仅标记不回收极致强制缓存策略
* @param imagePath 图片绝对路径
*/
public void decreaseRefCount(String imagePath) {
LogUtils.d(TAG, "【decreaseRefCount】调用开始 | 路径=" + imagePath);
if (TextUtils.isEmpty(imagePath)) {
LogUtils.e(TAG, "【decreaseRefCount】入参异常图片路径为空");
return;
}
synchronized (mRefCountMap) {
Integer count = mRefCountMap.get(imagePath);
if (count == null || count <= 0) {
LogUtils.w(TAG, "【decreaseRefCount】引用计数无效路径=" + imagePath);
return;
}
int newCount = count - 1;
if (newCount <= 0) {
// 极致强制缓存策略引用计数为0时仅移除计数绝对不回收 Bitmap
mRefCountMap.remove(imagePath);
LogUtils.d(TAG, "【decreaseRefCount】调用成功 | 路径=" + imagePath + " | 引用计数为0极致强制保持 Bitmap");
} else {
mRefCountMap.put(imagePath, newCount);
LogUtils.d(TAG, "【decreaseRefCount】调用成功 | 路径=" + imagePath + " | 引用计数=" + newCount);
}
}
}
// ================================== 对外接口:缓存清理(仅手动调用,永不自动执行)=================================
/**
* 清空所有 Bitmap 缓存(仅手动调用时执行,任何情况不自动执行)
*/
public void clearAllCache() {
LogUtils.w(TAG, "【clearAllCache】调用开始极致强制缓存策略下需谨慎使用");
// 清空硬引用缓存并回收 Bitmap
for (Bitmap bitmap : mHardCacheMap.values()) {
if (isBitmapValid(bitmap)) {
bitmap.recycle();
}
}
mHardCacheMap.clear();
// 清空引用计数
mRefCountMap.clear();
// 清空 SP 中保存的最后缓存路径
clearLastCachePathInSp();
LogUtils.d(TAG, "【clearAllCache】调用成功所有 Bitmap 缓存已清空");
}
/**
* 移除指定路径的 Bitmap 缓存(仅手动调用时执行,任何情况不自动执行)
* @param imagePath 图片绝对路径
*/
public void removeCachedBitmap(String imagePath) {
LogUtils.d(TAG, "【removeCachedBitmap】调用开始 | 路径=" + imagePath);
if (TextUtils.isEmpty(imagePath)) {
LogUtils.e(TAG, "【removeCachedBitmap】入参异常图片路径为空");
return;
}
synchronized (mRefCountMap) {
// 手动移除时才回收 Bitmap
Bitmap hardBitmap = mHardCacheMap.remove(imagePath);
if (isBitmapValid(hardBitmap)) {
hardBitmap.recycle();
LogUtils.d(TAG, "【removeCachedBitmap】手动回收硬引用缓存 | 路径=" + imagePath);
}
mRefCountMap.remove(imagePath);
// 若移除的是最后缓存的路径,清空 SP
String lastPath = getLastCachePathFromSp();
if (imagePath.equals(lastPath)) {
clearLastCachePathInSp();
LogUtils.d(TAG, "【removeCachedBitmap】移除最后缓存路径已清空 SP");
}
}
LogUtils.d(TAG, "【removeCachedBitmap】调用成功 | 路径=" + imagePath);
}
// ================================== 内部工具方法(无压缩解码 + Bitmap 有效性判断)=================================
/**
* 无压缩解码 Bitmap保留原始品质
* @param imagePath 图片绝对路径
* @return 解码后的 Bitmap / null文件无效/解码失败)
*/
private Bitmap decodeOriginalBitmap(String imagePath) {
LogUtils.d(TAG, "【decodeOriginalBitmap】调用开始 | 路径=" + imagePath);
// 前置校验:确保文件有效
File imageFile = new File(imagePath);
if (!imageFile.exists() || !imageFile.isFile() || imageFile.length() <= 0) {
LogUtils.e(TAG, "【decodeOriginalBitmap】文件无效跳过解码 | 路径=" + imagePath);
return null;
}
BitmapFactory.Options options = new BitmapFactory.Options();
// 仅获取尺寸用于日志记录,不参与解码逻辑
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imagePath, options);
// 校验尺寸是否有效
if (options.outWidth <= 0 || options.outHeight <= 0) {
LogUtils.e(TAG, "【decodeOriginalBitmap】图片尺寸无效 | 路径=" + imagePath);
return null;
}
LogUtils.d(TAG, "【decodeOriginalBitmap】图片原始尺寸 | 宽=" + options.outWidth + " | 高=" + options.outHeight);
// 无压缩解码配置
options.inJustDecodeBounds = false;
options.inSampleSize = BITMAP_SAMPLE_SIZE_ORIGINAL; // 不缩放采样率为1
options.inPreferredConfig = BITMAP_CONFIG_DEFAULT; // 保留全彩品质
options.inPurgeable = false; // 关闭可清除标志,极致强制保持内存
options.inInputShareable = false;
options.inDither = true; // 开启抖动,保证色彩还原
options.inScaled = false; // 关闭自动缩放,保留原始尺寸
try {
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
LogUtils.d(TAG, "【decodeOriginalBitmap】解码" + (bitmap != null ? "成功" : "失败") + " | 路径=" + imagePath);
return bitmap;
} catch (OutOfMemoryError e) {
LogUtils.e(TAG, "【decodeOriginalBitmap】OOM 异常(无压缩,图片尺寸过大)| 路径=" + imagePath);
// 极致强制缓存策略OOM 时仅放弃当前解码,绝对不清理已缓存的 Bitmap
return null;
} catch (Exception e) {
LogUtils.e(TAG, "【decodeOriginalBitmap】解码异常 | 路径=" + imagePath, e);
return null;
}
}
/**
* 判断 Bitmap 是否有效(非空且未被回收)
*/
private boolean isBitmapValid(Bitmap bitmap) {
boolean isValid = bitmap != null && !bitmap.isRecycled();
if (!isValid) {
LogUtils.w(TAG, "【isBitmapValid】Bitmap 无效:空或已回收");
}
return isValid;
}
// ================================== 内部工具方法SP 持久化相关 ==================================
/**
* 从 SP 中获取最后一次缓存的图片路径
* @return 最后缓存的路径 / null未保存
*/
private String getLastCachePathFromSp() {
String path = mSp.getString(SP_KEY_LAST_CACHE_PATH, null);
LogUtils.d(TAG, "【getLastCachePathFromSp】获取最后缓存路径 | 路径=" + path);
return path;
}
/**
* 将当前缓存路径持久化到 SP
* @param imagePath 图片绝对路径
*/
private void saveLastCachePathToSp(String imagePath) {
LogUtils.d(TAG, "【saveLastCachePathToSp】调用开始 | 路径=" + imagePath);
if (TextUtils.isEmpty(imagePath)) {
LogUtils.e(TAG, "【saveLastCachePathToSp】入参异常图片路径为空");
return;
}
mSp.edit().putString(SP_KEY_LAST_CACHE_PATH, imagePath).commit(); // Java 7 兼容,使用 commit 而非 apply
LogUtils.d(TAG, "【saveLastCachePathToSp】调用成功 | 路径=" + imagePath);
}
/**
* 清空 SP 中保存的最后缓存路径
*/
private void clearLastCachePathInSp() {
mSp.edit().remove(SP_KEY_LAST_CACHE_PATH).commit();
LogUtils.d(TAG, "【clearLastCachePathInSp】调用成功SP 中最后缓存路径已清空");
}
// ================================== 内部工具方法:预加载相关 ==================================
/**
* 构造时预加载 SP 中保存的最后一次缓存路径的图片
*/
private void preloadLastCachedBitmap() {
LogUtils.d(TAG, "【preloadLastCachedBitmap】调用开始");
String lastPath = getLastCachePathFromSp();
if (TextUtils.isEmpty(lastPath)) {
LogUtils.d(TAG, "【preloadLastCachedBitmap】SP 中无保存的缓存路径,跳过预加载");
return;
}
// 调用 cacheBitmap 预加载(内部已做文件校验和缓存判断)
Bitmap bitmap = cacheBitmap(lastPath);
if (bitmap != null) {
LogUtils.d(TAG, "【preloadLastCachedBitmap】预加载成功 | 路径=" + lastPath);
} else {
LogUtils.w(TAG, "【preloadLastCachedBitmap】预加载失败清空无效路径 | 路径=" + lastPath);
// 预加载失败,清空 SP 中无效路径
clearLastCachePathInSp();
}
}
// ================================== 内部工具方法:内存状态监听(仅记录日志)=================================
/**
* 注册内存状态监听(仅记录日志,不清理缓存,极致强制缓存策略)
*/
private void registerMemoryStatusListener() {
LogUtils.d(TAG, "【registerMemoryStatusListener】调用开始");
if (Build.VERSION.SDK_INT >= 14) {
App.getInstance().registerComponentCallbacks(new MemoryStatusCallback());
LogUtils.d(TAG, "【registerMemoryStatusListener】内存状态监听已注册仅记录日志不清理缓存");
} else {
LogUtils.w(TAG, "【registerMemoryStatusListener】API 版本低于14不支持内存状态监听");
}
}
/**
* 记录当前缓存状态(用于内存紧张时的调试)
*/
private void logCurrentCacheStatus() {
LogUtils.d(TAG, "【logCurrentCacheStatus】缓存数量 - " + getCacheCount() + ",总内存占用 - " + getTotalCacheSize() + " 字节");
LogUtils.d(TAG, "【logCurrentCacheStatus】缓存路径 - " + getCachedPaths().toString());
}
// ================================== 内部类:内存状态回调(仅记录日志)=================================
/**
* 内存状态回调(仅记录日志,不清理缓存,极致强制缓存策略)
*/
private class MemoryStatusCallback implements android.content.ComponentCallbacks2 {
@Override
public void onTrimMemory(int level) {
// 极致强制缓存策略:内存紧张时仅记录日志,不清理任何缓存
LogUtils.w(TAG, "【onTrimMemory】内存紧张级别 - " + level + ",极致强制保持所有 Bitmap 缓存(无压缩)");
// 记录当前缓存状态
logCurrentCacheStatus();
}
@Override
public void onLowMemory() {
// 极致强制缓存策略:低内存时仅记录日志,不清理任何缓存
LogUtils.w(TAG, "【onLowMemory】系统低内存极致强制保持所有 Bitmap 缓存(无压缩)");
// 记录当前缓存状态
logCurrentCacheStatus();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// 配置变化时无需处理
}
}
}

Some files were not shown because too many files have changed in this diff Show More