函数简化重构。
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
#Created by .winboll/winboll_app_build.gradle
|
||||
#Fri Dec 19 12:56:06 GMT 2025
|
||||
#Fri Dec 19 19:04:54 GMT 2025
|
||||
stageCount=10
|
||||
libraryProject=
|
||||
baseVersion=15.14
|
||||
publishVersion=15.14.9
|
||||
buildCount=47
|
||||
buildCount=49
|
||||
baseBetaVersion=15.14.10
|
||||
|
||||
@@ -486,11 +486,10 @@ public class MainActivity extends WinBoLLActivity implements MainContentView.OnV
|
||||
}
|
||||
|
||||
// ======================== 消息发送方法 ========================
|
||||
private void notifyServiceAppConfigChange(AppConfigBean configBean) {
|
||||
private void notifyServiceAppConfigChange() {
|
||||
LogUtils.d(TAG, "notifyServiceAppConfigChange: 通知服务配置变更");
|
||||
// 预留配置变更通知逻辑
|
||||
ToastUtils.show("notifyServiceAppConfigChange");
|
||||
ControlCenterService.updateStatus(this, configBean);
|
||||
ControlCenterService.updateStatus(this);
|
||||
reloadAppConfig();
|
||||
}
|
||||
|
||||
// ======================== 静态工具方法 ========================
|
||||
@@ -510,32 +509,6 @@ public class MainActivity extends WinBoLLActivity implements MainContentView.OnV
|
||||
}
|
||||
}
|
||||
|
||||
// ======================== 辅助工具方法 ========================
|
||||
// private String getRealPathFromUri(Uri contentUri) {
|
||||
// LogUtils.d(TAG, "getRealPathFromUri: 解析Uri路径,Uri=" + contentUri);
|
||||
// if (contentUri == null) {
|
||||
// LogUtils.e(TAG, "getRealPathFromUri: Uri为空,解析失败");
|
||||
// return null;
|
||||
// }
|
||||
// String realPath = null;
|
||||
// String[] projection = {MediaStore.MediaColumns.DATA};
|
||||
// Cursor cursor = getContentResolver().query(contentUri, projection, null, null, null);
|
||||
// if (cursor != null) {
|
||||
// try {
|
||||
// if (cursor.moveToNext()) {
|
||||
// int columnIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DATA);
|
||||
// if (columnIndex > -1) {
|
||||
// realPath = cursor.getString(columnIndex);
|
||||
// }
|
||||
// }
|
||||
// } finally {
|
||||
// cursor.close();
|
||||
// }
|
||||
// }
|
||||
// LogUtils.d(TAG, "getRealPathFromUri: 解析完成,路径=" + realPath);
|
||||
// return realPath;
|
||||
// }
|
||||
|
||||
private APPInfo genDefaultAppInfo() {
|
||||
LogUtils.d(TAG, "genDefaultAppInfo: 生成默认应用信息");
|
||||
String branchName = "powerbell";
|
||||
|
||||
@@ -105,14 +105,7 @@ public class ControlCenterServiceReceiver extends BroadcastReceiver {
|
||||
}
|
||||
|
||||
// 3. 加载最新配置(三级兜底:本地配置→服务内存配置→默认配置)
|
||||
AppConfigBean latestConfig = loadLatestConfig(service);
|
||||
if (latestConfig == null) {
|
||||
latestConfig = service.getCurrentConfigBean();
|
||||
if (latestConfig == null) {
|
||||
latestConfig = new AppConfigBean();
|
||||
LogUtils.w(TAG, "handleBatteryStateChanged: 配置为空,使用默认配置");
|
||||
}
|
||||
}
|
||||
AppConfigBean latestConfig = AppConfigUtils.getInstance(service).loadAppConfig();
|
||||
|
||||
// 4. 同步电池状态到配置,通知服务更新线程
|
||||
latestConfig.setCurrentBatteryValue(currentBatteryLevel);
|
||||
@@ -137,15 +130,8 @@ public class ControlCenterServiceReceiver extends BroadcastReceiver {
|
||||
LogUtils.d(TAG, "handleNotifyAppConfigUpdate: 同步缓存状态到配置 | service=" + service);
|
||||
try {
|
||||
// 加载最新配置(三级兜底)
|
||||
AppConfigBean latestConfig = loadLatestConfig(service);
|
||||
if (latestConfig == null) {
|
||||
latestConfig = service.getCurrentConfigBean();
|
||||
if (latestConfig == null) {
|
||||
latestConfig = new AppConfigBean();
|
||||
LogUtils.w(TAG, "handleNotifyAppConfigUpdate: 配置为空,使用默认配置");
|
||||
}
|
||||
}
|
||||
|
||||
AppConfigBean latestConfig = AppConfigUtils.getInstance(service).loadAppConfig();
|
||||
|
||||
// 同步缓存的电池状态到配置
|
||||
latestConfig.setCurrentBatteryValue(sLastBatteryLevel);
|
||||
latestConfig.setIsCharging(sIsCharging);
|
||||
@@ -180,34 +166,6 @@ public class ControlCenterServiceReceiver extends BroadcastReceiver {
|
||||
}
|
||||
}
|
||||
|
||||
// ================================== 内部辅助方法(通用逻辑拆分,统一管理)=================================
|
||||
/**
|
||||
* 加载最新应用配置
|
||||
* @param context 上下文
|
||||
* @return 最新配置实例,失败返回null
|
||||
*/
|
||||
private AppConfigBean loadLatestConfig(Context context) {
|
||||
LogUtils.d(TAG, "loadLatestConfig: 读取本地配置 | context=" + context);
|
||||
try {
|
||||
if (context == null) {
|
||||
LogUtils.e(TAG, "loadLatestConfig: 上下文为空");
|
||||
return null;
|
||||
}
|
||||
|
||||
AppConfigUtils configUtils = AppConfigUtils.getInstance(context);
|
||||
if (configUtils == null) {
|
||||
LogUtils.e(TAG, "loadLatestConfig: 配置工具类实例为空");
|
||||
return null;
|
||||
}
|
||||
|
||||
configUtils.reloadAllConfig();
|
||||
LogUtils.d(TAG, "loadLatestConfig: 本地配置加载成功");
|
||||
return configUtils.mAppConfigBean;
|
||||
} catch (Exception e) {
|
||||
LogUtils.e(TAG, "loadLatestConfig: 加载失败", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// ================================== 广播注册/注销(强化容错,避免重复操作)=================================
|
||||
/**
|
||||
|
||||
@@ -318,16 +318,15 @@ public class ControlCenterService extends Service {
|
||||
* @param context 上下文
|
||||
* @param configBean 新配置
|
||||
*/
|
||||
public static void updateStatus(Context context, AppConfigBean configBean) {
|
||||
LogUtils.d(TAG, "updateStatus: 外部更新配置 | context=" + context + " | config=" + configBean);
|
||||
if (context == null || configBean == null) {
|
||||
public static void updateStatus(Context context) {
|
||||
LogUtils.d(TAG, "updateStatus: 外部更新配置 | context=" + context);
|
||||
if (context == null) {
|
||||
LogUtils.e(TAG, "updateStatus: 参数为空,更新失败");
|
||||
return;
|
||||
}
|
||||
|
||||
Intent intent = new Intent(context, ControlCenterService.class);
|
||||
intent.setAction(ControlCenterServiceReceiver.ACTION_APPCONFIG_CHANGED);
|
||||
intent.putExtra(ControlCenterServiceReceiver.EXTRA_APP_CONFIG_BEAN, (Serializable) configBean);
|
||||
intent.setPackage(context.getPackageName());
|
||||
context.sendBroadcast(intent);
|
||||
}
|
||||
|
||||
@@ -33,7 +33,6 @@ public class AppConfigUtils {
|
||||
|
||||
// ======================== 配置Bean属性(持久化核心,volatile保障线程安全)========================
|
||||
public volatile AppConfigBean mAppConfigBean; // 应用配置Bean
|
||||
private volatile ControlCenterServiceBean mServiceConfigBean; // 服务配置Bean
|
||||
|
||||
// ======================== 缓存状态属性(减少Bean读取次数,提升性能)========================
|
||||
private volatile boolean mIsServiceEnabled = false; // 服务开关缓存状态
|
||||
@@ -46,9 +45,8 @@ public class AppConfigUtils {
|
||||
this.mApplication = (App) context.getApplicationContext();
|
||||
// 初始化配置Bean
|
||||
mAppConfigBean = new AppConfigBean();
|
||||
mServiceConfigBean = new ControlCenterServiceBean(false);
|
||||
// 加载持久化配置
|
||||
loadAllConfig();
|
||||
loadAppConfig();
|
||||
LogUtils.d(TAG, "配置工具类初始化完成");
|
||||
}
|
||||
|
||||
@@ -74,7 +72,7 @@ public class AppConfigUtils {
|
||||
/**
|
||||
* 加载所有配置(应用配置+服务配置,统一入口,初始化/重载通用)
|
||||
*/
|
||||
private void loadAllConfig() {
|
||||
public AppConfigBean loadAppConfig() {
|
||||
LogUtils.d(TAG, "loadAllConfig: 开始加载所有配置");
|
||||
// 加载应用配置
|
||||
AppConfigBean savedAppBean = (AppConfigBean) AppConfigBean.loadBean(mContext, AppConfigBean.class);
|
||||
@@ -82,21 +80,11 @@ public class AppConfigUtils {
|
||||
mAppConfigBean = savedAppBean;
|
||||
LogUtils.d(TAG, "loadAllConfig: 应用配置加载成功");
|
||||
} else {
|
||||
mAppConfigBean = new AppConfigBean();
|
||||
AppConfigBean.saveBean(mContext, mAppConfigBean);
|
||||
LogUtils.d(TAG, "loadAllConfig: 无已保存应用配置,使用默认值并持久化");
|
||||
}
|
||||
|
||||
// 加载服务配置
|
||||
ControlCenterServiceBean savedServiceBean = (ControlCenterServiceBean) ControlCenterServiceBean.loadBean(mContext, ControlCenterServiceBean.class);
|
||||
if (savedServiceBean != null) {
|
||||
mServiceConfigBean = savedServiceBean;
|
||||
mIsServiceEnabled = savedServiceBean.isEnableService();
|
||||
LogUtils.d(TAG, "loadAllConfig: 服务配置加载成功,服务状态:" + (mIsServiceEnabled ? "开启" : "关闭"));
|
||||
} else {
|
||||
ControlCenterServiceBean.saveBean(mContext, mServiceConfigBean);
|
||||
mIsServiceEnabled = false;
|
||||
LogUtils.d(TAG, "loadAllConfig: 无已保存服务配置,使用默认值并持久化");
|
||||
}
|
||||
return mAppConfigBean;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -104,54 +92,9 @@ public class AppConfigUtils {
|
||||
*/
|
||||
private void saveAppConfig() {
|
||||
AppConfigBean.saveBean(mContext, mAppConfigBean);
|
||||
// 通知服务和Activity更新配置(适配API30服务通信规范)
|
||||
ControlCenterService.updateStatus(mContext, mAppConfigBean);
|
||||
MainActivity.reloadAppConfig();
|
||||
LogUtils.d(TAG, "saveAppConfig: 应用配置保存成功,已同步服务和Activity");
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存服务配置(内部核心方法,直接持久化)
|
||||
*/
|
||||
private void saveServiceConfig() {
|
||||
mServiceConfigBean.setIsEnableService(mIsServiceEnabled);
|
||||
ControlCenterServiceBean.saveBean(mContext, mServiceConfigBean);
|
||||
LogUtils.d(TAG, "saveServiceConfig: 服务配置保存成功,服务状态:" + (mIsServiceEnabled ? "开启" : "关闭"));
|
||||
}
|
||||
|
||||
|
||||
// ======================== 服务开关配置方法(单独归类,逻辑聚焦)========================
|
||||
/**
|
||||
* 设置服务开关状态(直接生效,无弹窗,适配API30后台服务启停规范)
|
||||
* @param isEnabled 目标状态(true=开启,false=关闭)
|
||||
*/
|
||||
public void setServiceEnabled(final boolean isEnabled) {
|
||||
if (isEnabled == mIsServiceEnabled) {
|
||||
LogUtils.d(TAG, "setServiceEnabled: 服务状态无变化,无需操作");
|
||||
return;
|
||||
}
|
||||
mIsServiceEnabled = isEnabled;
|
||||
saveServiceConfig();
|
||||
// 启停服务(适配API30后台服务限制)
|
||||
if (mIsServiceEnabled) {
|
||||
LogUtils.d(TAG, "setServiceEnabled: 启动ControlCenterService");
|
||||
ControlCenterService.startControlCenterService(mContext);
|
||||
} else {
|
||||
LogUtils.d(TAG, "setServiceEnabled: 停止ControlCenterService");
|
||||
ControlCenterService.stopControlCenterService(mContext);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取服务开关状态(直接返回缓存值,提升性能)
|
||||
* @return 服务状态(true=开启,false=关闭)
|
||||
*/
|
||||
public boolean isServiceEnabled() {
|
||||
LogUtils.d(TAG, "isServiceEnabled: 获取服务状态=" + (mIsServiceEnabled ? "开启" : "关闭"));
|
||||
return mIsServiceEnabled;
|
||||
}
|
||||
|
||||
|
||||
// ======================== 充电提醒配置方法(单独归类,逻辑聚焦)========================
|
||||
/**
|
||||
* 设置充电提醒开关状态(直接生效,无弹窗)
|
||||
@@ -354,34 +297,19 @@ public class AppConfigUtils {
|
||||
return interval;
|
||||
}
|
||||
|
||||
public boolean isServiceEnabled() {
|
||||
// 加载服务配置
|
||||
ControlCenterServiceBean savedServiceBean = (ControlCenterServiceBean) ControlCenterServiceBean.loadBean(mContext, ControlCenterServiceBean.class);
|
||||
if (savedServiceBean != null) {
|
||||
return savedServiceBean.isEnableService();
|
||||
} else {
|
||||
ControlCenterServiceBean.saveBean(mContext, new ControlCenterServiceBean(false));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// ======================== 外部配置操作入口(直接生效,无弹窗)========================
|
||||
/**
|
||||
* 保存所有配置(直接生效,无弹窗,主动保存入口)
|
||||
*/
|
||||
public void saveAllConfig() {
|
||||
saveAppConfig();
|
||||
saveServiceConfig();
|
||||
LogUtils.d(TAG, "saveAllConfig: 所有配置保存完成");
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新加载所有配置(外部调用,适配手动修改配置场景)
|
||||
*/
|
||||
public void reloadAllConfig() {
|
||||
LogUtils.d(TAG, "reloadAllConfig: 开始重新加载所有配置");
|
||||
loadAllConfig();
|
||||
MainActivity.reloadAppConfig();
|
||||
LogUtils.d(TAG, "reloadAllConfig: 配置重新加载完成");
|
||||
}
|
||||
|
||||
|
||||
// ======================== 适配辅助类(补全服务更新方法,避免编译报错)========================
|
||||
/**
|
||||
* 辅助类:补全ControlCenterService的updateStatus静态方法适配(实际项目可移至Service类)
|
||||
*/
|
||||
public static class ControlCenterServiceHelper {
|
||||
// 空实现,仅用于编译通过,实际逻辑需在ControlCenterService中实现
|
||||
}
|
||||
public void setIsServiceEnabled(boolean isServiceEnabled) {
|
||||
ControlCenterServiceBean.saveBean(mContext, new ControlCenterServiceBean(isServiceEnabled));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user