20251225_153508_537

This commit is contained in:
2025-12-25 15:35:12 +08:00
parent 59f5eae136
commit 4fcce7edb3
7 changed files with 179 additions and 145 deletions

View File

@@ -1,8 +1,8 @@
#Created by .winboll/winboll_app_build.gradle #Created by .winboll/winboll_app_build.gradle
#Wed Dec 24 21:23:33 HKT 2025 #Thu Dec 25 07:32:14 GMT 2025
stageCount=30 stageCount=30
libraryProject= libraryProject=
baseVersion=15.14 baseVersion=15.14
publishVersion=15.14.29 publishVersion=15.14.29
buildCount=0 buildCount=36
baseBetaVersion=15.14.30 baseBetaVersion=15.14.30

View File

@@ -5,9 +5,11 @@ import cc.winboll.studio.libaes.utils.WinBoLLActivityManager;
import cc.winboll.studio.libappbase.GlobalApplication; import cc.winboll.studio.libappbase.GlobalApplication;
import cc.winboll.studio.libappbase.LogUtils; import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.libappbase.ToastUtils; 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.receivers.GlobalApplicationReceiver;
import cc.winboll.studio.powerbell.utils.AppCacheUtils; import cc.winboll.studio.powerbell.utils.AppCacheUtils;
import cc.winboll.studio.powerbell.utils.AppConfigUtils; 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.BitmapCacheUtils;
import cc.winboll.studio.powerbell.utils.NotificationManagerUtils; import cc.winboll.studio.powerbell.utils.NotificationManagerUtils;
import cc.winboll.studio.powerbell.views.MemoryCachedBackgroundView; import cc.winboll.studio.powerbell.views.MemoryCachedBackgroundView;
@@ -40,6 +42,7 @@ public class App extends GlobalApplication {
private static AppCacheUtils sAppCacheUtils; private static AppCacheUtils sAppCacheUtils;
// 全局Bitmap缓存工具极致强制保持一旦初始化永不销毁 // 全局Bitmap缓存工具极致强制保持一旦初始化永不销毁
public static BackgroundSourceUtils sBackgroundSourceUtils;
public static BitmapCacheUtils sBitmapCacheUtils; public static BitmapCacheUtils sBitmapCacheUtils;
// 全局视图控件缓存工具(极致强制保持:一旦初始化,永不销毁) // 全局视图控件缓存工具(极致强制保持:一旦初始化,永不销毁)
@@ -50,7 +53,8 @@ public class App extends GlobalApplication {
private GlobalApplicationReceiver mGlobalReceiver; private GlobalApplicationReceiver mGlobalReceiver;
// 通知管理工具 // 通知管理工具
private NotificationManagerUtils mNotificationManager; private static NotificationManagerUtils sNotificationManagerUtils;
private static App sApp;
// ===================== 公共静态方法区(工具类实例获取) ===================== // ===================== 公共静态方法区(工具类实例获取) =====================
/** /**
@@ -116,6 +120,7 @@ public class App extends GlobalApplication {
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
sApp = this;
LogUtils.d(TAG, "onCreate() 应用启动,开始初始化"); LogUtils.d(TAG, "onCreate() 应用启动,开始初始化");
// 初始化调试模式 // 初始化调试模式
@@ -124,6 +129,7 @@ public class App extends GlobalApplication {
// 初始化基础工具 // 初始化基础工具
initBaseTools(); initBaseTools();
//App.notifyMessage(TAG, "onCreate() 应用启动中。。。");
// 初始化工具类实例(核心:极致强制缓存,永不销毁) // 初始化工具类实例(核心:极致强制缓存,永不销毁)
initUtils(); initUtils();
// 初始化广播接收器 // 初始化广播接收器
@@ -179,6 +185,8 @@ public class App extends GlobalApplication {
WinBoLLActivityManager.init(this); WinBoLLActivityManager.init(this);
ToastUtils.init(this); ToastUtils.init(this);
LogUtils.d(TAG, "initBaseTools() 基础工具初始化完成"); LogUtils.d(TAG, "initBaseTools() 基础工具初始化完成");
sNotificationManagerUtils = new NotificationManagerUtils(this);
LogUtils.d(TAG, "initBaseTools() 工具类初始化完成,极致强制缓存策略已生效");
} }
/** /**
@@ -186,19 +194,23 @@ public class App extends GlobalApplication {
*/ */
private void initUtils() { private void initUtils() {
LogUtils.d(TAG, "initUtils() 开始初始化工具类,启用极致强制缓存策略"); LogUtils.d(TAG, "initUtils() 开始初始化工具类,启用极致强制缓存策略");
//App.notifyMessage(TAG, "initUtils() 开始初始化工具类,启用极致强制缓存策略");
sAppConfigUtils = getAppConfigUtils(this); sAppConfigUtils = getAppConfigUtils(this);
sAppCacheUtils = getAppCacheUtils(this); sAppCacheUtils = getAppCacheUtils(this);
sBackgroundSourceUtils = BackgroundSourceUtils.getInstance(this);
sBackgroundSourceUtils.loadSettings();
// 极致强制初始化Bitmap缓存工具必初始化永不销毁 // 极致强制初始化Bitmap缓存工具必初始化永不销毁
sBitmapCacheUtils = BitmapCacheUtils.getInstance(); sBitmapCacheUtils = BitmapCacheUtils.getInstance();
LogUtils.d(TAG, "initUtils() Bitmap缓存工具已初始化极致强制保持永不销毁"); LogUtils.d(TAG, "initUtils() Bitmap缓存工具已初始化极致强制保持永不销毁");
// 极致强制初始化视图控件缓存工具(必初始化,永不销毁) // 极致强制初始化视图控件缓存工具(必初始化,永不销毁)
sMemoryCachedBackgroundView = MemoryCachedBackgroundView.getLastInstance(this); sMemoryCachedBackgroundView = MemoryCachedBackgroundView.getLastInstance(this);
if(sMemoryCachedBackgroundView == null) {
App.notifyMessage(TAG, "sMemoryCachedBackgroundView == null");
sMemoryCachedBackgroundView = MemoryCachedBackgroundView.getInstance(this, sBackgroundSourceUtils.getCurrentBackgroundBean(), true);
}
LogUtils.d(TAG, "initUtils() 视图控件缓存工具已初始化(极致强制保持,永不销毁)"); LogUtils.d(TAG, "initUtils() 视图控件缓存工具已初始化(极致强制保持,永不销毁)");
mNotificationManager = new NotificationManagerUtils(this);
LogUtils.d(TAG, "initUtils() 工具类初始化完成,极致强制缓存策略已生效");
} }
/** /**
@@ -231,9 +243,9 @@ public class App extends GlobalApplication {
*/ */
private void releaseNotificationManager() { private void releaseNotificationManager() {
LogUtils.d(TAG, "releaseNotificationManager() 开始释放通知工具"); LogUtils.d(TAG, "releaseNotificationManager() 开始释放通知工具");
if (mNotificationManager != null) { if (sNotificationManagerUtils != null) {
mNotificationManager.release(); sNotificationManagerUtils.release();
mNotificationManager = null; sNotificationManagerUtils = null;
LogUtils.d(TAG, "releaseNotificationManager() 通知工具资源已释放"); LogUtils.d(TAG, "releaseNotificationManager() 通知工具资源已释放");
} else { } else {
LogUtils.d(TAG, "releaseNotificationManager() 通知工具未初始化,无需释放"); LogUtils.d(TAG, "releaseNotificationManager() 通知工具未初始化,无需释放");
@@ -267,5 +279,12 @@ public class App extends GlobalApplication {
} }
LogUtils.d(TAG, "logDetailedCacheStatus() 详细缓存状态记录完成,所有缓存均极致强制保持"); LogUtils.d(TAG, "logDetailedCacheStatus() 详细缓存状态记录完成,所有缓存均极致强制保持");
} }
public static void notifyMessage(String title, String content) {
if (isDebugging() && sApp != null && sNotificationManagerUtils != null) {
NotificationMessage message = new NotificationMessage(title, content, "");
sNotificationManagerUtils.showMessageNotification(sApp, message);
}
}
} }

View File

@@ -106,7 +106,7 @@ public class MainUnitTest2Activity extends AppCompatActivity {
// 创建MemoryCachedBackgroundView单例并添加到布局 // 创建MemoryCachedBackgroundView单例并添加到布局
int nCurrentPixelColor = BackgroundSourceUtils.getInstance(this).getCurrentBackgroundBean().getPixelColor(); int nCurrentPixelColor = BackgroundSourceUtils.getInstance(this).getCurrentBackgroundBean().getPixelColor();
mMemoryCachedBackgroundView = MemoryCachedBackgroundView.getInstance(this, nCurrentPixelColor, "", false); mMemoryCachedBackgroundView = MemoryCachedBackgroundView.getLastInstance(this);
mllBackgroundView.addView(mMemoryCachedBackgroundView); mllBackgroundView.addView(mMemoryCachedBackgroundView);
LogUtils.d(TAG, "initViewAndEvent内存缓存背景视图实例创建并添加完成"); LogUtils.d(TAG, "initViewAndEvent内存缓存背景视图实例创建并添加完成");

View File

@@ -51,6 +51,7 @@ public class BitmapCacheUtils {
*/ */
private BitmapCacheUtils() { private BitmapCacheUtils() {
LogUtils.d(TAG, "【BitmapCacheUtils】单例构造开始"); LogUtils.d(TAG, "【BitmapCacheUtils】单例构造开始");
//App.notifyMessage(TAG, "【BitmapCacheUtils】单例构造开始");
// 使用 ConcurrentHashMap 保证线程安全,避免手动同步 // 使用 ConcurrentHashMap 保证线程安全,避免手动同步
mHardCacheMap = new ConcurrentHashMap<>(); mHardCacheMap = new ConcurrentHashMap<>();
mRefCountMap = new ConcurrentHashMap<>(); mRefCountMap = new ConcurrentHashMap<>();

View File

@@ -22,7 +22,7 @@ import cc.winboll.studio.powerbell.models.NotificationMessage;
/** /**
* 通知工具类:统一管理前台服务/电池提醒/应用配置信息通知 * 通知工具类:统一管理前台服务/电池提醒/应用配置信息通知
* 适配API19-30 | Java7 | 小米手机 * 适配API19-30 | Java7 | 小米手机
* 特性:前台服务无铃声、提醒通知系统默认铃声、配置通知低优先级无打扰、API分级适配、内存泄漏防护 * 特性:前台服务无铃声、提醒通知系统默认铃声、配置通知系统默认铃声无振动、API分级适配、内存泄漏防护
*/ */
public class NotificationManagerUtils { public class NotificationManagerUtils {
// ================================== 静态常量(置顶统一管理,杜绝魔法值)================================= // ================================== 静态常量(置顶统一管理,杜绝魔法值)=================================
@@ -48,6 +48,7 @@ public class NotificationManagerUtils {
private static final int PENDING_INTENT_REQUEST_CODE_FOREGROUND = 0; private static final int PENDING_INTENT_REQUEST_CODE_FOREGROUND = 0;
private static final int PENDING_INTENT_REQUEST_CODE_REMIND = 1; private static final int PENDING_INTENT_REQUEST_CODE_REMIND = 1;
private static final int PENDING_INTENT_REQUEST_CODE_CONFIG = 2; // 新增:配置通知请求码 private static final int PENDING_INTENT_REQUEST_CODE_CONFIG = 2; // 新增:配置通知请求码
private static int snMessageNotificationID = 10000;
// ================================== 成员变量(私有封装,按依赖优先级排序)================================= // ================================== 成员变量(私有封装,按依赖优先级排序)=================================
// 核心上下文(应用级,避免内存泄漏) // 核心上下文(应用级,避免内存泄漏)
@@ -59,35 +60,35 @@ public class NotificationManagerUtils {
// ================================== 构造方法(初始化核心资源,前置校验)================================= // ================================== 构造方法(初始化核心资源,前置校验)=================================
public NotificationManagerUtils(Context context) { public NotificationManagerUtils(Context context) {
LogUtils.d(TAG, "NotificationManagerUtils: 构造方法执行 | context=" + context); LogUtils.d(TAG, "NotificationManagerUtils() 构造 | context=" + context);
// 前置校验Context非空 // 前置校验Context非空
if (context == null) { if (context == null) {
LogUtils.e(TAG, "NotificationManagerUtils: 构造失败context is null"); LogUtils.e(TAG, "NotificationManagerUtils() 构造失败context is null");
return; return;
} }
// 初始化核心资源 // 初始化核心资源
this.mContext = context.getApplicationContext(); this.mContext = context.getApplicationContext();
this.mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); this.mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
LogUtils.d(TAG, "NotificationManagerUtils: 核心资源初始化完成 | mContext=" + mContext + " | mNotificationManager=" + mNotificationManager); LogUtils.d(TAG, "NotificationManagerUtils() 核心资源初始化完成 | mContext=" + mContext + " | mNotificationManager=" + mNotificationManager);
// 初始化通知渠道API26+ 必需) // 初始化通知渠道API26+ 必需)
initNotificationChannels(); initNotificationChannels();
LogUtils.d(TAG, "NotificationManagerUtils: 构造完成"); LogUtils.d(TAG, "NotificationManagerUtils() 构造完成");
} }
// ================================== 核心初始化方法通知渠道API分级适配================================= // ================================== 核心初始化方法通知渠道API分级适配=================================
/** /**
* 初始化通知渠道:前台服务渠道(无铃声+无振动)、提醒渠道(系统默认铃声+无振动)、配置信息渠道(低优先级无打扰 * 初始化通知渠道:前台服务渠道(无铃声+无振动)、提醒渠道(系统默认铃声+无振动)、配置信息渠道(系统默认铃声+无振动
*/ */
private void initNotificationChannels() { private void initNotificationChannels() {
LogUtils.d(TAG, "initNotificationChannels: 执行通知渠道初始化"); LogUtils.d(TAG, "initNotificationChannels() 执行通知渠道初始化");
// API<26 无渠道机制,直接返回 // API<26 无渠道机制,直接返回
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
LogUtils.d(TAG, "initNotificationChannels: API<26无需创建渠道"); LogUtils.d(TAG, "initNotificationChannels() API<26无需创建渠道");
return; return;
} }
// 通知服务为空,避免空指针 // 通知服务为空,避免空指针
if (mNotificationManager == null) { if (mNotificationManager == null) {
LogUtils.e(TAG, "initNotificationChannels: 失败NotificationManager is null"); LogUtils.e(TAG, "initNotificationChannels() 失败NotificationManager is null");
return; return;
} }
@@ -103,7 +104,7 @@ public class NotificationManagerUtils {
foregroundChannel.setSound(null, null); // 强制无铃声 foregroundChannel.setSound(null, null); // 强制无铃声
foregroundChannel.setShowBadge(false); foregroundChannel.setShowBadge(false);
foregroundChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET); foregroundChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
LogUtils.d(TAG, "initNotificationChannels: 前台服务渠道配置完成"); LogUtils.d(TAG, "initNotificationChannels() 前台服务渠道配置完成");
// 2. 电池提醒渠道(中优先级,系统默认铃声,无振动) // 2. 电池提醒渠道(中优先级,系统默认铃声,无振动)
NotificationChannel remindChannel = new NotificationChannel( NotificationChannel remindChannel = new NotificationChannel(
@@ -117,27 +118,27 @@ public class NotificationManagerUtils {
remindChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), Notification.AUDIO_ATTRIBUTES_DEFAULT); remindChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), Notification.AUDIO_ATTRIBUTES_DEFAULT);
remindChannel.setShowBadge(false); remindChannel.setShowBadge(false);
remindChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); remindChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
LogUtils.d(TAG, "initNotificationChannels: 电池提醒渠道配置完成"); LogUtils.d(TAG, "initNotificationChannels() 电池提醒渠道配置完成");
// 3. 应用配置信息渠道(新增:最低优先级,无铃声无振动,仅提示不打扰 // 3. 应用配置信息渠道(方案1修复默认优先级系统默认铃声无振动)
NotificationChannel configChannel = new NotificationChannel( NotificationChannel configChannel = new NotificationChannel(
CHANNEL_ID_CONFIG, CHANNEL_ID_CONFIG,
"应用配置信息", "应用配置信息",
NotificationManager.IMPORTANCE_MIN NotificationManager.IMPORTANCE_DEFAULT
); );
configChannel.setDescription("应用配置更新、参数变更等提示,无声音、无振动"); configChannel.setDescription("应用配置更新、参数变更等提示,系统默认铃声、无振动");
configChannel.enableLights(false); configChannel.enableLights(true);
configChannel.enableVibration(false); configChannel.enableVibration(false);
configChannel.setSound(null, null); configChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION), Notification.AUDIO_ATTRIBUTES_DEFAULT);
configChannel.setShowBadge(false); configChannel.setShowBadge(false);
configChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); configChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
LogUtils.d(TAG, "initNotificationChannels: 应用配置信息渠道配置完成"); LogUtils.d(TAG, "initNotificationChannels() 应用配置信息渠道配置完成");
// 注册渠道到系统 // 注册渠道到系统
mNotificationManager.createNotificationChannel(foregroundChannel); mNotificationManager.createNotificationChannel(foregroundChannel);
mNotificationManager.createNotificationChannel(remindChannel); mNotificationManager.createNotificationChannel(remindChannel);
mNotificationManager.createNotificationChannel(configChannel); // 注册新增渠道 mNotificationManager.createNotificationChannel(configChannel); // 注册新增渠道
LogUtils.d(TAG, "initNotificationChannels: 成功:创建前台服务+电池提醒+应用配置信息渠道"); LogUtils.d(TAG, "initNotificationChannels() 成功:创建前台服务+电池提醒+应用配置信息渠道");
} }
// ================================== 对外核心方法(前台服务通知:启动/更新/取消)================================= // ================================== 对外核心方法(前台服务通知:启动/更新/取消)=================================
@@ -145,26 +146,26 @@ public class NotificationManagerUtils {
* 启动前台服务通知API30适配无铃声 * 启动前台服务通知API30适配无铃声
*/ */
public void startForegroundServiceNotify(Service service, NotificationMessage message) { public void startForegroundServiceNotify(Service service, NotificationMessage message) {
LogUtils.d(TAG, "startForegroundServiceNotify: 执行 | notifyId=" + NOTIFY_ID_FOREGROUND_SERVICE + " | service=" + service + " | message=" + message); LogUtils.d(TAG, "startForegroundServiceNotify() 执行 | notifyId=" + NOTIFY_ID_FOREGROUND_SERVICE + " | service=" + service + " | message=" + message);
// 前置校验:参数非空 // 前置校验:参数非空
if (service == null || message == null || mNotificationManager == null) { if (service == null || message == null || mNotificationManager == null) {
LogUtils.e(TAG, "startForegroundServiceNotify: 失败param is null | service=" + service + " | message=" + message + " | mNotificationManager=" + mNotificationManager); LogUtils.e(TAG, "startForegroundServiceNotify() 失败param is null | service=" + service + " | message=" + message + " | mNotificationManager=" + mNotificationManager);
return; return;
} }
// 构建前台通知 // 构建前台通知
mForegroundServiceNotify = buildForegroundNotification(message); mForegroundServiceNotify = buildForegroundNotification(message);
if (mForegroundServiceNotify == null) { if (mForegroundServiceNotify == null) {
LogUtils.e(TAG, "startForegroundServiceNotify: 失败:构建通知为空"); LogUtils.e(TAG, "startForegroundServiceNotify() 失败:构建通知为空");
return; return;
} }
// 启动前台服务API30无FOREGROUND_SERVICE_TYPE限制全版本通用 // 启动前台服务API30无FOREGROUND_SERVICE_TYPE限制全版本通用
try { try {
service.startForeground(NOTIFY_ID_FOREGROUND_SERVICE, mForegroundServiceNotify); service.startForeground(NOTIFY_ID_FOREGROUND_SERVICE, mForegroundServiceNotify);
LogUtils.d(TAG, "startForegroundServiceNotify: 成功"); LogUtils.d(TAG, "startForegroundServiceNotify() 成功");
} catch (Exception e) { } catch (Exception e) {
LogUtils.e(TAG, "startForegroundServiceNotify: 异常", e); LogUtils.e(TAG, "startForegroundServiceNotify() 异常", e);
} }
} }
@@ -172,23 +173,23 @@ public class NotificationManagerUtils {
* 更新前台服务通知内容复用通知ID保持无铃声 * 更新前台服务通知内容复用通知ID保持无铃声
*/ */
public void updateForegroundServiceNotify(NotificationMessage message) { public void updateForegroundServiceNotify(NotificationMessage message) {
LogUtils.d(TAG, "updateForegroundServiceNotify: 执行 | notifyId=" + NOTIFY_ID_FOREGROUND_SERVICE + " | message=" + message); LogUtils.d(TAG, "updateForegroundServiceNotify() 执行 | notifyId=" + NOTIFY_ID_FOREGROUND_SERVICE + " | message=" + message);
if (message == null || mNotificationManager == null) { if (message == null || mNotificationManager == null) {
LogUtils.e(TAG, "updateForegroundServiceNotify: 失败param is null | message=" + message + " | mNotificationManager=" + mNotificationManager); LogUtils.e(TAG, "updateForegroundServiceNotify() 失败param is null | message=" + message + " | mNotificationManager=" + mNotificationManager);
return; return;
} }
mForegroundServiceNotify = buildForegroundNotification(message); mForegroundServiceNotify = buildForegroundNotification(message);
if (mForegroundServiceNotify == null) { if (mForegroundServiceNotify == null) {
LogUtils.e(TAG, "updateForegroundServiceNotify: 失败:构建通知为空"); LogUtils.e(TAG, "updateForegroundServiceNotify() 失败:构建通知为空");
return; return;
} }
try { try {
mNotificationManager.notify(NOTIFY_ID_FOREGROUND_SERVICE, mForegroundServiceNotify); mNotificationManager.notify(NOTIFY_ID_FOREGROUND_SERVICE, mForegroundServiceNotify);
LogUtils.d(TAG, "updateForegroundServiceNotify: 成功"); LogUtils.d(TAG, "updateForegroundServiceNotify() 成功");
} catch (Exception e) { } catch (Exception e) {
LogUtils.e(TAG, "updateForegroundServiceNotify: 异常", e); LogUtils.e(TAG, "updateForegroundServiceNotify() 异常", e);
} }
} }
@@ -196,10 +197,10 @@ public class NotificationManagerUtils {
* 取消前台服务通知Service销毁时调用 * 取消前台服务通知Service销毁时调用
*/ */
public void cancelForegroundServiceNotify() { public void cancelForegroundServiceNotify() {
LogUtils.d(TAG, "cancelForegroundServiceNotify: 执行 | notifyId=" + NOTIFY_ID_FOREGROUND_SERVICE); LogUtils.d(TAG, "cancelForegroundServiceNotify() 执行 | notifyId=" + NOTIFY_ID_FOREGROUND_SERVICE);
cancelNotification(NOTIFY_ID_FOREGROUND_SERVICE); cancelNotification(NOTIFY_ID_FOREGROUND_SERVICE);
mForegroundServiceNotify = null; // 置空释放 mForegroundServiceNotify = null; // 置空释放
LogUtils.d(TAG, "cancelForegroundServiceNotify: 成功"); LogUtils.d(TAG, "cancelForegroundServiceNotify() 成功");
} }
// ================================== 对外核心方法(电池提醒通知:发送)================================= // ================================== 对外核心方法(电池提醒通知:发送)=================================
@@ -207,48 +208,70 @@ public class NotificationManagerUtils {
* 发送电池提醒通知(系统默认铃声,无振动) * 发送电池提醒通知(系统默认铃声,无振动)
*/ */
public void showRemindNotification(Context context, NotificationMessage message) { public void showRemindNotification(Context context, NotificationMessage message) {
LogUtils.d(TAG, "showRemindNotification: 执行 | notifyId=" + NOTIFY_ID_REMIND + " | context=" + context + " | message=" + message); LogUtils.d(TAG, "showRemindNotification() 执行 | notifyId=" + NOTIFY_ID_REMIND + " | context=" + context + " | message=" + message);
if (context == null || message == null || mNotificationManager == null) { if (context == null || message == null || mNotificationManager == null) {
LogUtils.e(TAG, "showRemindNotification: 失败param is null | context=" + context + " | message=" + message + " | mNotificationManager=" + mNotificationManager); LogUtils.e(TAG, "showRemindNotification() 失败param is null | context=" + context + " | message=" + message + " | mNotificationManager=" + mNotificationManager);
return; return;
} }
Notification remindNotify = buildRemindNotification(context, message); Notification remindNotify = buildRemindNotification(context, message);
if (remindNotify == null) { if (remindNotify == null) {
LogUtils.e(TAG, "showRemindNotification: 失败:构建通知为空"); LogUtils.e(TAG, "showRemindNotification() 失败:构建通知为空");
return; return;
} }
try { try {
mNotificationManager.notify(NOTIFY_ID_REMIND, remindNotify); mNotificationManager.notify(NOTIFY_ID_REMIND, remindNotify);
LogUtils.d(TAG, "showRemindNotification: 成功"); LogUtils.d(TAG, "showRemindNotification() 成功");
} catch (Exception e) { } catch (Exception e) {
LogUtils.e(TAG, "showRemindNotification: 异常", e); LogUtils.e(TAG, "showRemindNotification() 异常", e);
} }
} }
// ================================== 对外核心方法(应用配置信息通知:发送)================================= public synchronized void showMessageNotification(Context context, NotificationMessage message) {
/** snMessageNotificationID++;
* 发送应用配置信息通知(新增:低优先级无铃声,仅提示不打扰) LogUtils.d(TAG, "showMessageNotification() 执行 | notifyId=" + snMessageNotificationID + " | context=" + context + " | message=" + message);
*/
public void showConfigNotification(Context context, NotificationMessage message) {
LogUtils.d(TAG, "showConfigNotification: 执行 | notifyId=" + NOTIFY_ID_CONFIG + " | context=" + context + " | message=" + message);
if (context == null || message == null || mNotificationManager == null) { if (context == null || message == null || mNotificationManager == null) {
LogUtils.e(TAG, "showConfigNotification: 失败param is null | context=" + context + " | message=" + message + " | mNotificationManager=" + mNotificationManager); LogUtils.e(TAG, "showMessageNotification() 失败param is null | context=" + context + " | message=" + message + " | mNotificationManager=" + mNotificationManager);
return; return;
} }
Notification configNotify = buildConfigNotification(context, message); Notification configNotify = buildConfigNotification(context, message);
if (configNotify == null) { if (configNotify == null) {
LogUtils.e(TAG, "showConfigNotification: 失败:构建通知为空"); LogUtils.e(TAG, "showMessageNotification() 失败:构建通知为空");
return;
}
try {
mNotificationManager.notify(snMessageNotificationID, configNotify);
LogUtils.d(TAG, "showMessageNotification() 成功");
} catch (Exception e) {
LogUtils.e(TAG, "showMessageNotification() 异常", e);
}
}
// ================================== 对外核心方法(应用配置信息通知:发送)=================================
/**
* 发送应用配置信息通知方案1修复系统默认铃声无振动
*/
public void showConfigNotification(Context context, NotificationMessage message) {
LogUtils.d(TAG, "showConfigNotification() 执行 | notifyId=" + NOTIFY_ID_CONFIG + " | context=" + context + " | message=" + message);
if (context == null || message == null || mNotificationManager == null) {
LogUtils.e(TAG, "showConfigNotification() 失败param is null | context=" + context + " | message=" + message + " | mNotificationManager=" + mNotificationManager);
return;
}
Notification configNotify = buildConfigNotification(context, message);
if (configNotify == null) {
LogUtils.e(TAG, "showConfigNotification() 失败:构建通知为空");
return; return;
} }
try { try {
mNotificationManager.notify(NOTIFY_ID_CONFIG, configNotify); mNotificationManager.notify(NOTIFY_ID_CONFIG, configNotify);
LogUtils.d(TAG, "showConfigNotification: 成功"); LogUtils.d(TAG, "showConfigNotification() 成功");
} catch (Exception e) { } catch (Exception e) {
LogUtils.e(TAG, "showConfigNotification: 异常", e); LogUtils.e(TAG, "showConfigNotification() 异常", e);
} }
} }
@@ -257,16 +280,16 @@ public class NotificationManagerUtils {
* 取消指定ID的通知 * 取消指定ID的通知
*/ */
public void cancelNotification(int notifyId) { public void cancelNotification(int notifyId) {
LogUtils.d(TAG, "cancelNotification: 执行 | notifyId=" + notifyId); LogUtils.d(TAG, "cancelNotification() 执行 | notifyId=" + notifyId);
if (mNotificationManager == null) { if (mNotificationManager == null) {
LogUtils.e(TAG, "cancelNotification: 失败NotificationManager is null"); LogUtils.e(TAG, "cancelNotification() 失败NotificationManager is null");
return; return;
} }
try { try {
mNotificationManager.cancel(notifyId); mNotificationManager.cancel(notifyId);
LogUtils.d(TAG, "cancelNotification: 成功 | notifyId=" + notifyId); LogUtils.d(TAG, "cancelNotification() 成功 | notifyId=" + notifyId);
} catch (Exception e) { } catch (Exception e) {
LogUtils.e(TAG, "cancelNotification: 异常 | notifyId=" + notifyId, e); LogUtils.e(TAG, "cancelNotification() 异常 | notifyId=" + notifyId, e);
} }
} }
@@ -274,16 +297,16 @@ public class NotificationManagerUtils {
* 取消所有通知(兜底场景使用) * 取消所有通知(兜底场景使用)
*/ */
public void cancelAllNotifications() { public void cancelAllNotifications() {
LogUtils.d(TAG, "cancelAllNotifications: 执行"); LogUtils.d(TAG, "cancelAllNotifications() 执行");
if (mNotificationManager == null) { if (mNotificationManager == null) {
LogUtils.e(TAG, "cancelAllNotifications: 失败NotificationManager is null"); LogUtils.e(TAG, "cancelAllNotifications() 失败NotificationManager is null");
return; return;
} }
try { try {
mNotificationManager.cancelAll(); mNotificationManager.cancelAll();
LogUtils.d(TAG, "cancelAllNotifications: 成功"); LogUtils.d(TAG, "cancelAllNotifications() 成功");
} catch (Exception e) { } catch (Exception e) {
LogUtils.e(TAG, "cancelAllNotifications: 异常", e); LogUtils.e(TAG, "cancelAllNotifications() 异常", e);
} }
} }
@@ -292,30 +315,30 @@ public class NotificationManagerUtils {
* 构建前台服务通知(全版本无铃声+无振动) * 构建前台服务通知(全版本无铃声+无振动)
*/ */
private Notification buildForegroundNotification(NotificationMessage message) { private Notification buildForegroundNotification(NotificationMessage message) {
LogUtils.d(TAG, "buildForegroundNotification: 执行 | message=" + message); LogUtils.d(TAG, "buildForegroundNotification() 执行 | message=" + message);
if (message == null || mContext == null) { if (message == null || mContext == null) {
LogUtils.e(TAG, "buildForegroundNotification: 失败param is null | message=" + message + " | mContext=" + mContext); LogUtils.e(TAG, "buildForegroundNotification() 失败param is null | message=" + message + " | mContext=" + mContext);
return null; return null;
} }
// 内容兜底 // 内容兜底
String title = message.getTitle() != null && !message.getTitle().isEmpty() ? message.getTitle() : FOREGROUND_NOTIFY_TITLE_DEFAULT; String title = message.getTitle() != null && !message.getTitle().isEmpty() ? message.getTitle() : FOREGROUND_NOTIFY_TITLE_DEFAULT;
String content = message.getContent() != null && !message.getContent().isEmpty() ? message.getContent() : FOREGROUND_NOTIFY_CONTENT_DEFAULT; String content = message.getContent() != null && !message.getContent().isEmpty() ? message.getContent() : FOREGROUND_NOTIFY_CONTENT_DEFAULT;
LogUtils.d(TAG, "buildForegroundNotification: 内容兜底完成 | title=" + title + " | content=" + content); LogUtils.d(TAG, "buildForegroundNotification() 内容兜底完成 | title=" + title + " | content=" + content);
Notification.Builder builder; Notification.Builder builder;
// API分级构建 // API分级构建
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// API26+:绑定前台渠道(渠道已配置无铃声) // API26+:绑定前台渠道(渠道已配置无铃声)
builder = new Notification.Builder(mContext, CHANNEL_ID_FOREGROUND); builder = new Notification.Builder(mContext, CHANNEL_ID_FOREGROUND);
LogUtils.d(TAG, "buildForegroundNotification: 使用API26+渠道构建"); LogUtils.d(TAG, "buildForegroundNotification() 使用API26+渠道构建");
} else { } else {
// API<26直接构建手动禁用铃声振动 // API<26直接构建手动禁用铃声振动
builder = new Notification.Builder(mContext); builder = new Notification.Builder(mContext);
builder.setSound(null); builder.setSound(null);
builder.setVibrate(new long[]{0}); builder.setVibrate(new long[]{0});
builder.setDefaults(0); builder.setDefaults(0);
LogUtils.d(TAG, "buildForegroundNotification: 使用API<26手动配置"); LogUtils.d(TAG, "buildForegroundNotification() 使用API<26手动配置");
} }
// 通用配置 // 通用配置
@@ -332,11 +355,11 @@ public class NotificationManagerUtils {
builder.setLargeIcon(getAppIcon(mContext)) builder.setLargeIcon(getAppIcon(mContext))
.setColor(mContext.getResources().getColor(R.color.colorPrimary)) .setColor(mContext.getResources().getColor(R.color.colorPrimary))
.setPriority(Notification.PRIORITY_LOW); .setPriority(Notification.PRIORITY_LOW);
LogUtils.d(TAG, "buildForegroundNotification: 补充API21+配置"); LogUtils.d(TAG, "buildForegroundNotification() 补充API21+配置");
} }
Notification notification = builder.build(); Notification notification = builder.build();
LogUtils.d(TAG, "buildForegroundNotification: 成功构建前台通知"); LogUtils.d(TAG, "buildForegroundNotification() 成功构建前台通知");
return notification; return notification;
} }
@@ -345,30 +368,30 @@ public class NotificationManagerUtils {
* 构建电池提醒通知(全版本系统默认铃声+无振动) * 构建电池提醒通知(全版本系统默认铃声+无振动)
*/ */
private Notification buildRemindNotification(Context context, NotificationMessage message) { private Notification buildRemindNotification(Context context, NotificationMessage message) {
LogUtils.d(TAG, "buildRemindNotification: 执行 | context=" + context + " | message=" + message); LogUtils.d(TAG, "buildRemindNotification() 执行 | context=" + context + " | message=" + message);
if (context == null || message == null) { if (context == null || message == null) {
LogUtils.e(TAG, "buildRemindNotification: 失败param is null | context=" + context + " | message=" + message); LogUtils.e(TAG, "buildRemindNotification() 失败param is null | context=" + context + " | message=" + message);
return null; return null;
} }
// 内容兜底 // 内容兜底
String title = message.getTitle() != null && !message.getTitle().isEmpty() ? message.getTitle() : REMIND_NOTIFY_TITLE_DEFAULT; String title = message.getTitle() != null && !message.getTitle().isEmpty() ? message.getTitle() : REMIND_NOTIFY_TITLE_DEFAULT;
String content = message.getContent() != null && !message.getContent().isEmpty() ? message.getContent() : REMIND_NOTIFY_CONTENT_DEFAULT; String content = message.getContent() != null && !message.getContent().isEmpty() ? message.getContent() : REMIND_NOTIFY_CONTENT_DEFAULT;
LogUtils.d(TAG, "buildRemindNotification: 内容兜底完成 | title=" + title + " | content=" + content); LogUtils.d(TAG, "buildRemindNotification() 内容兜底完成 | title=" + title + " | content=" + content);
Notification.Builder builder; Notification.Builder builder;
// API分级构建 // API分级构建
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// API26+:绑定提醒渠道(渠道已配置默认铃声) // API26+:绑定提醒渠道(渠道已配置默认铃声)
builder = new Notification.Builder(context, CHANNEL_ID_REMIND); builder = new Notification.Builder(context, CHANNEL_ID_REMIND);
LogUtils.d(TAG, "buildRemindNotification: 使用API26+渠道构建"); LogUtils.d(TAG, "buildRemindNotification() 使用API26+渠道构建");
} else { } else {
// API<26手动配置默认铃声关闭振动 // API<26手动配置默认铃声关闭振动
builder = new Notification.Builder(context); builder = new Notification.Builder(context);
builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI) // 显式默认铃声 builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI) // 显式默认铃声
.setVibrate(new long[]{0}) .setVibrate(new long[]{0})
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND); .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND);
LogUtils.d(TAG, "buildRemindNotification: 使用API<26手动配置"); LogUtils.d(TAG, "buildRemindNotification() 使用API<26手动配置");
} }
// 通用配置 // 通用配置
@@ -386,43 +409,43 @@ public class NotificationManagerUtils {
builder.setLargeIcon(getAppIcon(context)) builder.setLargeIcon(getAppIcon(context))
.setColor(context.getResources().getColor(R.color.colorPrimary)) .setColor(context.getResources().getColor(R.color.colorPrimary))
.setPriority(Notification.PRIORITY_DEFAULT); .setPriority(Notification.PRIORITY_DEFAULT);
LogUtils.d(TAG, "buildRemindNotification: 补充API21+配置"); LogUtils.d(TAG, "buildRemindNotification() 补充API21+配置");
} }
Notification notification = builder.build(); Notification notification = builder.build();
LogUtils.d(TAG, "buildRemindNotification: 成功构建提醒通知"); LogUtils.d(TAG, "buildRemindNotification() 成功构建提醒通知");
return notification; return notification;
} }
// ================================== 内部辅助方法(通知构建:应用配置信息通知)================================= // ================================== 内部辅助方法(通知构建:应用配置信息通知)=================================
/** /**
* 构建应用配置信息通知(新增:全版本无铃声+无振动,低优先级 * 构建应用配置信息通知(方案1修复全版本系统默认铃声+无振动)
*/ */
private Notification buildConfigNotification(Context context, NotificationMessage message) { private Notification buildConfigNotification(Context context, NotificationMessage message) {
LogUtils.d(TAG, "buildConfigNotification: 执行 | context=" + context + " | message=" + message); LogUtils.d(TAG, "buildConfigNotification() 执行 | context=" + context + " | message=" + message);
if (context == null || message == null) { if (context == null || message == null) {
LogUtils.e(TAG, "buildConfigNotification: 失败param is null | context=" + context + " | message=" + message); LogUtils.e(TAG, "buildConfigNotification() 失败param is null | context=" + context + " | message=" + message);
return null; return null;
} }
// 内容兜底 // 内容兜底
String title = message.getTitle() != null && !message.getTitle().isEmpty() ? message.getTitle() : CONFIG_NOTIFY_TITLE_DEFAULT; String title = message.getTitle() != null && !message.getTitle().isEmpty() ? message.getTitle() : CONFIG_NOTIFY_TITLE_DEFAULT;
String content = message.getContent() != null && !message.getContent().isEmpty() ? message.getContent() : CONFIG_NOTIFY_CONTENT_DEFAULT; String content = message.getContent() != null && !message.getContent().isEmpty() ? message.getContent() : CONFIG_NOTIFY_CONTENT_DEFAULT;
LogUtils.d(TAG, "buildConfigNotification: 内容兜底完成 | title=" + title + " | content=" + content); LogUtils.d(TAG, "buildConfigNotification() 内容兜底完成 | title=" + title + " | content=" + content);
Notification.Builder builder; Notification.Builder builder;
// API分级构建 // API分级构建
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// API26+:绑定配置渠道(渠道已配置铃声) // API26+:绑定配置渠道(渠道已配置默认铃声)
builder = new Notification.Builder(context, CHANNEL_ID_CONFIG); builder = new Notification.Builder(context, CHANNEL_ID_CONFIG);
LogUtils.d(TAG, "buildConfigNotification: 使用API26+渠道构建"); LogUtils.d(TAG, "buildConfigNotification() 使用API26+渠道构建");
} else { } else {
// API<26直接构建,手动禁用铃声振动 // API<26手动配置默认铃声关闭振动方案1修复保留铃声配置删除冗余DEFAULT_SOUND
builder = new Notification.Builder(context); builder = new Notification.Builder(context);
builder.setSound(null); builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
builder.setVibrate(new long[]{0}); builder.setVibrate(new long[]{0});
builder.setDefaults(0); builder.setDefaults(Notification.DEFAULT_LIGHTS);
LogUtils.d(TAG, "buildConfigNotification: 使用API<26手动配置"); LogUtils.d(TAG, "buildConfigNotification() 使用API<26手动配置");
} }
// 通用配置 // 通用配置
@@ -438,12 +461,12 @@ public class NotificationManagerUtils {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setLargeIcon(getAppIcon(context)) builder.setLargeIcon(getAppIcon(context))
.setColor(context.getResources().getColor(R.color.colorPrimary)) .setColor(context.getResources().getColor(R.color.colorPrimary))
.setPriority(Notification.PRIORITY_MIN); // 最低优先级 .setPriority(Notification.PRIORITY_DEFAULT);
LogUtils.d(TAG, "buildConfigNotification: 补充API21+配置"); LogUtils.d(TAG, "buildConfigNotification() 补充API21+配置");
} }
Notification notification = builder.build(); Notification notification = builder.build();
LogUtils.d(TAG, "buildConfigNotification: 成功构建配置信息通知"); LogUtils.d(TAG, "buildConfigNotification() 成功构建配置信息通知");
return notification; return notification;
} }
@@ -452,20 +475,20 @@ public class NotificationManagerUtils {
* 创建跳转MainActivity的PendingIntentAPI23+ 添加IMMUTABLE标记避免安全异常 * 创建跳转MainActivity的PendingIntentAPI23+ 添加IMMUTABLE标记避免安全异常
*/ */
private PendingIntent createJumpPendingIntent(Context context, int requestCode) { private PendingIntent createJumpPendingIntent(Context context, int requestCode) {
LogUtils.d(TAG, "createJumpPendingIntent: 执行 | requestCode=" + requestCode + " | context=" + context); LogUtils.d(TAG, "createJumpPendingIntent() 执行 | requestCode=" + requestCode + " | context=" + context);
Intent intent = new Intent(context, MainActivity.class); Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
LogUtils.d(TAG, "createJumpPendingIntent: 跳转Intent配置完成"); LogUtils.d(TAG, "createJumpPendingIntent() 跳转Intent配置完成");
// API23+ 必需添加IMMUTABLE适配API30安全规范 // API23+ 必需添加IMMUTABLE适配API30安全规范
int flags = PendingIntent.FLAG_UPDATE_CURRENT; int flags = PendingIntent.FLAG_UPDATE_CURRENT;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
flags |= PendingIntent.FLAG_IMMUTABLE; flags |= PendingIntent.FLAG_IMMUTABLE;
LogUtils.d(TAG, "createJumpPendingIntent: 添加FLAG_IMMUTABLE标记API23+"); LogUtils.d(TAG, "createJumpPendingIntent() 添加FLAG_IMMUTABLE标记API23+");
} }
PendingIntent pendingIntent = PendingIntent.getActivity(context, requestCode, intent, flags); PendingIntent pendingIntent = PendingIntent.getActivity(context, requestCode, intent, flags);
LogUtils.d(TAG, "createJumpPendingIntent: 成功 | requestCode=" + requestCode); LogUtils.d(TAG, "createJumpPendingIntent() 成功 | requestCode=" + requestCode);
return pendingIntent; return pendingIntent;
} }
@@ -474,14 +497,14 @@ public class NotificationManagerUtils {
* 获取APP图标失败返回默认图标 * 获取APP图标失败返回默认图标
*/ */
private Bitmap getAppIcon(Context context) { private Bitmap getAppIcon(Context context) {
LogUtils.d(TAG, "getAppIcon: 执行 | context=" + context); LogUtils.d(TAG, "getAppIcon() 执行 | context=" + context);
try { try {
PackageInfo pkgInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); PackageInfo pkgInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
Bitmap appIcon = BitmapFactory.decodeResource(context.getResources(), pkgInfo.applicationInfo.icon); Bitmap appIcon = BitmapFactory.decodeResource(context.getResources(), pkgInfo.applicationInfo.icon);
LogUtils.d(TAG, "getAppIcon: 成功:获取应用图标"); LogUtils.d(TAG, "getAppIcon() 成功:获取应用图标");
return appIcon; return appIcon;
} catch (PackageManager.NameNotFoundException e) { } catch (PackageManager.NameNotFoundException e) {
LogUtils.e(TAG, "getAppIcon: 异常:获取应用图标失败,使用默认图标", e); LogUtils.e(TAG, "getAppIcon() 异常:获取应用图标失败,使用默认图标", e);
return BitmapFactory.decodeResource(context.getResources(), NOTIFICATION_DEFAULT_ICON); return BitmapFactory.decodeResource(context.getResources(), NOTIFICATION_DEFAULT_ICON);
} }
} }
@@ -491,11 +514,11 @@ public class NotificationManagerUtils {
* 释放资源,销毁时调用 * 释放资源,销毁时调用
*/ */
public void release() { public void release() {
LogUtils.d(TAG, "release: 执行资源释放"); LogUtils.d(TAG, "release() 执行资源释放");
cancelForegroundServiceNotify(); cancelForegroundServiceNotify();
mNotificationManager = null; mNotificationManager = null;
mContext = null; mContext = null;
LogUtils.d(TAG, "release: 成功:所有资源已释放"); LogUtils.d(TAG, "release() 成功:所有资源已释放");
} }
// ================================== 对外 getter 方法(仅前台通知实例,只读)================================= // ================================== 对外 getter 方法(仅前台通知实例,只读)=================================

View File

@@ -17,9 +17,11 @@ import android.widget.TextView;
import cc.winboll.studio.libappbase.LogUtils; import cc.winboll.studio.libappbase.LogUtils;
import cc.winboll.studio.powerbell.App; import cc.winboll.studio.powerbell.App;
import cc.winboll.studio.powerbell.R; import cc.winboll.studio.powerbell.R;
import cc.winboll.studio.powerbell.models.BackgroundBean;
import cc.winboll.studio.powerbell.models.ControlCenterServiceBean; import cc.winboll.studio.powerbell.models.ControlCenterServiceBean;
import cc.winboll.studio.powerbell.services.ControlCenterService; import cc.winboll.studio.powerbell.services.ControlCenterService;
import cc.winboll.studio.powerbell.utils.AppConfigUtils; import cc.winboll.studio.powerbell.utils.AppConfigUtils;
import cc.winboll.studio.powerbell.utils.BackgroundSourceUtils;
/** /**
* 主页面核心视图封装类:统一管理视图绑定、数据更新、事件监听,解耦 Activity 逻辑 * 主页面核心视图封装类:统一管理视图绑定、数据更新、事件监听,解耦 Activity 逻辑
@@ -151,13 +153,20 @@ public class MainContentView {
// 基础布局绑定 // 基础布局绑定
mainLayout = (RelativeLayout) rootView.findViewById(R.id.activitymainRelativeLayout1); mainLayout = (RelativeLayout) rootView.findViewById(R.id.activitymainRelativeLayout1);
mllBackgroundView = (LinearLayout) rootView.findViewById(R.id.ll_backgroundview); mllBackgroundView = (LinearLayout) rootView.findViewById(R.id.ll_backgroundview);
backgroundView = App.sMemoryCachedBackgroundView.getLastInstance(mContext);
backgroundView = App.sMemoryCachedBackgroundView.getLastInstance(mContext);
if (backgroundView == null) {
App.sBackgroundSourceUtils.loadSettings();
BackgroundBean backgroundBean = App.sBackgroundSourceUtils.getCurrentBackgroundBean();
backgroundView = App.sMemoryCachedBackgroundView.getInstance(mContext, backgroundBean, true);
}
if (backgroundView.getParent() != null) { if (backgroundView.getParent() != null) {
((ViewGroup) backgroundView.getParent()).removeView(backgroundView); ((ViewGroup) backgroundView.getParent()).removeView(backgroundView);
LogUtils.d(TAG, "【bindViews】移除背景视图旧父容器"); LogUtils.d(TAG, "【bindViews】移除背景视图旧父容器");
} }
mllBackgroundView.addView(backgroundView); mllBackgroundView.addView(backgroundView);
// 容器布局绑定
// 容器布局绑定
llLeftSeekBar = (LinearLayout) rootView.findViewById(R.id.fragmentmainviewLinearLayout1); llLeftSeekBar = (LinearLayout) rootView.findViewById(R.id.fragmentmainviewLinearLayout1);
llRightSeekBar = (LinearLayout) rootView.findViewById(R.id.fragmentmainviewLinearLayout2); llRightSeekBar = (LinearLayout) rootView.findViewById(R.id.fragmentmainviewLinearLayout2);
// 开关控件绑定 // 开关控件绑定

View File

@@ -58,36 +58,27 @@ public class MemoryCachedBackgroundView extends BackgroundView {
* @param isReload 是否强制重新加载图片(路径匹配时仍刷新) * @param isReload 是否强制重新加载图片(路径匹配时仍刷新)
* @return 缓存/新创建的MemoryCachedBackgroundView实例 * @return 缓存/新创建的MemoryCachedBackgroundView实例
*/ */
public static MemoryCachedBackgroundView getInstance(Context context, int bgColor, String imagePath, boolean isReload) { public static MemoryCachedBackgroundView getInstance(Context context, BackgroundBean bean, boolean isReload) {
LogUtils.d(TAG, String.format("getInstance 调用 | 图片路径=%s | 是否重载=%b", imagePath, isReload)); LogUtils.d(TAG, String.format("getInstance 调用 | BackgroundBean=%s | 是否重载=%b", bean.toString(), isReload));
// 空路径校验 //App.notifyMessage(TAG, String.format("getInstance 调用 | BackgroundBean=%s | 是否重载=%b", bean.toString(), isReload));
if (TextUtils.isEmpty(imagePath)) {
LogUtils.e(TAG, "getInstance: 图片路径为空,创建空实例");
return new MemoryCachedBackgroundView(context);
}
// 1. 路径匹配缓存 → 判断是否强制重载
if (imagePath.equals(sCachedImagePath) && sCachedView != null) {
LogUtils.d(TAG, "getInstance: 路径已缓存,当前缓存实例有效");
if (isReload) {
LogUtils.d(TAG, String.format("getInstance: 强制重载图片 | 路径=%s", imagePath));
sCachedView.loadImage(bgColor, imagePath, isReload);
} else {
LogUtils.d(TAG, String.format("getInstance: 使用缓存实例,无需重载 | 路径=%s", imagePath));
}
return sCachedView;
}
// 2. 路径不匹配/无缓存 → 新建实例并更新静态缓存(核心:保留旧实例,仅更新引用)
LogUtils.d(TAG, String.format("getInstance: 路径未缓存,新建实例(保留旧实例) | 路径=%s", imagePath));
String oldPath = sCachedImagePath;
sCachedView = new MemoryCachedBackgroundView(context); sCachedView = new MemoryCachedBackgroundView(context);
sCachedImagePath = imagePath; sCachedView.loadByBackgroundBean(bean, isReload);
sCachedView.loadImage(bgColor, imagePath, isReload); saveLastLoadImagePath(context, getBackgroundBeanImagePath(bean));
LogUtils.d(TAG, String.format("getInstance: 已更新当前缓存实例,旧实例路径=%s强制保持", oldPath)); LogUtils.d(TAG, String.format("getInstance: 已更新当前缓存实例,旧实例路径=%s强制保持", getBackgroundBeanImagePath(bean)));
App.notifyMessage(TAG, String.format("getInstance: 已更新当前缓存实例,旧实例路径=%s强制保持", getBackgroundBeanImagePath(bean)));
return sCachedView; return sCachedView;
} }
static String getBackgroundBeanImagePath(BackgroundBean bean) {
if (bean.isUseBackgroundFile()) {
if (bean.isUseBackgroundScaledCompressFile()) {
return bean.getBackgroundScaledCompressFilePath();
}
return bean.getBackgroundFilePath();
}
return "";
}
// ====================================== 新增功能:获取最后加载的实例(强制缓存版) ====================================== // ====================================== 新增功能:获取最后加载的实例(强制缓存版) ======================================
/** /**
* 获取最后一次loadImage的路径对应的实例强制保持所有实例 * 获取最后一次loadImage的路径对应的实例强制保持所有实例
@@ -97,28 +88,19 @@ public class MemoryCachedBackgroundView extends BackgroundView {
*/ */
public static MemoryCachedBackgroundView getLastInstance(Context context) { public static MemoryCachedBackgroundView getLastInstance(Context context) {
LogUtils.d(TAG, "getLastInstance 调用"); LogUtils.d(TAG, "getLastInstance 调用");
//App.notifyMessage(TAG, "getLastInstance 调用");
// 1. 从SP获取最后加载的路径强制保持不自动删除 // 1. 从SP获取最后加载的路径强制保持不自动删除
String lastPath = getLastLoadImagePath(context); sCachedImagePath = getLastLoadImagePath(context);
if (TextUtils.isEmpty(lastPath)) { String lastPath = getBackgroundBeanImagePath(App.sBackgroundSourceUtils.getCurrentBackgroundBean());
LogUtils.e(TAG, "getLastInstance: 无最后加载路径,创建空实例"); //App.notifyMessage(TAG, String.format("sCachedImagePath : %s", sCachedImagePath));
return new MemoryCachedBackgroundView(context); //App.notifyMessage(TAG, String.format("lastPath : %s", lastPath));
}
// 2. 路径匹配当前缓存 → 直接返回
if (lastPath.equals(sCachedImagePath) && sCachedView != null) { if (lastPath.equals(sCachedImagePath) && sCachedView != null) {
LogUtils.d(TAG, String.format("getLastInstance: 使用最后路径缓存实例 | 路径=%s", lastPath)); LogUtils.d(TAG, String.format("getLastInstance: 使用最后路径缓存实例 | 路径=%s", lastPath));
App.notifyMessage(TAG, String.format("getLastInstance: 使用最后路径缓存实例 | 路径=%s", lastPath));
return sCachedView; return sCachedView;
} }
App.notifyMessage(TAG, "getLastInstance 返回 null");
// 3. 路径不匹配 → 新建实例并更新缓存(保留旧实例) return null;
LogUtils.d(TAG, String.format("getLastInstance: 最后路径未缓存,新建实例并加载(保留旧实例) | 路径=%s", lastPath));
String oldPath = sCachedImagePath;
sCachedView = new MemoryCachedBackgroundView(context);
sCachedImagePath = lastPath;
int nCurrentPixelColor = BackgroundSourceUtils.getInstance(context).getCurrentBackgroundBean().getPixelColor();
sCachedView.loadImage(nCurrentPixelColor, sCachedImagePath, false);
LogUtils.d(TAG, String.format("getLastInstance: 已更新最后路径实例,旧实例路径=%s强制保持", oldPath));
return sCachedView;
} }
// ====================================== 工具方法SP持久化最后加载路径强制保持版 ====================================== // ====================================== 工具方法SP持久化最后加载路径强制保持版 ======================================