源码整理

This commit is contained in:
2025-12-29 09:45:01 +08:00
parent 9124303fd3
commit 798357aedd

View File

@@ -17,13 +17,18 @@ import cc.winboll.studio.powerbell.views.MemoryCachedBackgroundView;
/** /**
* 应用全局入口类适配Android API 30基于Java 7编写 * 应用全局入口类适配Android API 30基于Java 7编写
* 核心策略:极致强制缓存 - 无论内存紧张程度永不自动清理任何缓存Bitmap/视图控件/路径记录) * 核心策略:极致强制缓存 - 无论内存紧张程度永不自动清理任何缓存Bitmap/视图控件/路径记录)
*
* @Author 豆包&ZhanGSKen<zhangsken@qq.com> * @Author 豆包&ZhanGSKen<zhangsken@qq.com>
* @Version 1.0.0 * @Version 1.0.0
* @Date 2025-12-25 * @Date 2025-12-29
*/ */
public class App extends GlobalApplication { public class App extends GlobalApplication {
// ===================== 常量定义区(按功能分类排序) =====================
public static final String TAG = "App"; // ==================== 常量定义 ====================
private static final String TAG = "App";
private static final String CACHE_PROTECT_TAG = "FORCE_CACHE_PROTECT";
private static final int INVALID_BATTERY_VALUE = -1;
// 组件跳转常量 // 组件跳转常量
public static final String COMPONENT_EN1 = "cc.winboll.studio.powerbell.MainActivityEN1"; public static final String COMPONENT_EN1 = "cc.winboll.studio.powerbell.MainActivityEN1";
@@ -35,286 +40,269 @@ public class App extends GlobalApplication {
public static final String ACTION_SWITCHTO_CN1 = "cc.winboll.studio.powerbell.App.ACTION_SWITCHTO_CN1"; 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"; public static final String ACTION_SWITCHTO_CN2 = "cc.winboll.studio.powerbell.App.ACTION_SWITCHTO_CN2";
// 缓存防护常量 // ==================== 静态属性 ====================
private static final String CACHE_PROTECT_TAG = "FORCE_CACHE_PROTECT";
private static final int INVALID_BATTERY_VALUE = -1;
// ===================== 静态属性区(按工具类优先级排序) =====================
// 应用单例
private static App sApp; private static App sApp;
// 数据配置工具 // 配置与缓存
public static AppConfigUtils sAppConfigUtils; public static AppConfigUtils sAppConfigUtils;
private static AppCacheUtils sAppCacheUtils; private static AppCacheUtils sAppCacheUtils;
// 全局Bitmap缓存工具极致强制保持一旦初始化永不销毁 // 资源与视图缓存(强制驻留
public static BackgroundSourceUtils sBackgroundSourceUtils; public static BackgroundSourceUtils sBackgroundSourceUtils;
public static BitmapCacheUtils sBitmapCacheUtils; public static BitmapCacheUtils sBitmapCacheUtils;
// 全局视图控件缓存工具(极致强制保持:一旦初始化,永不销毁)
private static MemoryCachedBackgroundView sMemoryCachedBackgroundView; private static MemoryCachedBackgroundView sMemoryCachedBackgroundView;
// 电池状态 // 状态
public static volatile int sQuantityOfElectricity = INVALID_BATTERY_VALUE; public static volatile int sQuantityOfElectricity = INVALID_BATTERY_VALUE;
// 通知管理工具 // 系统工具
private static NotificationManagerUtils sNotificationManagerUtils; private static NotificationManagerUtils sNotificationManagerUtils;
// ===================== 成员属性区(按生命周期关联度排序) ===================== // ==================== 成员属性 ====================
// 全局广播接收器
private GlobalApplicationReceiver mGlobalReceiver; private GlobalApplicationReceiver mGlobalReceiver;
// ===================== 公共静态方法区(单例/工具类实例获取) ===================== // ==================== 公共静态方法 (工具/单例) ====================
/** /**
* 获取应用单例实例 * 获取应用单例
*/ */
public static App getInstance() { public static App getInstance() {
LogUtils.d(TAG, "getInstance() 调用 | 返回实例:" + sApp); LogUtils.d(TAG, "getInstance() called | Result: " + sApp);
return sApp; return sApp;
} }
/** /**
* 获取应用配置工具实例 * 获取配置工具实例
*/ */
public static AppConfigUtils getAppConfigUtils(Context context) { public static AppConfigUtils getAppConfigUtils(Context context) {
String contextType = context != null ? context.getClass().getSimpleName() : "null"; String contextName = context != null ? context.getClass().getSimpleName() : "null";
LogUtils.d(TAG, "getAppConfigUtils() 调用 | 入参Context类型:" + contextType); LogUtils.d(TAG, "getAppConfigUtils() called with: context = [" + contextName + "]");
if (sAppConfigUtils == null) { if (sAppConfigUtils == null) {
sAppConfigUtils = AppConfigUtils.getInstance(context); sAppConfigUtils = AppConfigUtils.getInstance(context);
LogUtils.d(TAG, "getAppConfigUtils() | AppConfigUtils实例初始化完成"); LogUtils.d(TAG, "getAppConfigUtils: Initialized new instance");
} }
LogUtils.d(TAG, "getAppConfigUtils() | 返回实例:" + sAppConfigUtils);
return sAppConfigUtils; return sAppConfigUtils;
} }
/** /**
* 获取应用缓存工具实例 * 获取缓存工具实例
*/ */
public static AppCacheUtils getAppCacheUtils(Context context) { public static AppCacheUtils getAppCacheUtils(Context context) {
String contextType = context != null ? context.getClass().getSimpleName() : "null"; String contextName = context != null ? context.getClass().getSimpleName() : "null";
LogUtils.d(TAG, "getAppCacheUtils() 调用 | 入参Context类型:" + contextType); LogUtils.d(TAG, "getAppCacheUtils() called with: context = [" + contextName + "]");
if (sAppCacheUtils == null) { if (sAppCacheUtils == null) {
sAppCacheUtils = AppCacheUtils.getInstance(context); sAppCacheUtils = AppCacheUtils.getInstance(context);
LogUtils.d(TAG, "getAppCacheUtils() | AppCacheUtils实例初始化完成"); LogUtils.d(TAG, "getAppCacheUtils: Initialized new instance");
} }
LogUtils.d(TAG, "getAppCacheUtils() | 返回实例:" + sAppCacheUtils);
return sAppCacheUtils; return sAppCacheUtils;
} }
// ===================== 公共成员方法区(业务功能) ===================== // ==================== 公共成员方法 (业务) ====================
/** /**
* 清除电池历史数据 * 清除电池历史数据
*/ */
public void clearBatteryHistory() { public void clearBatteryHistory() {
LogUtils.d(TAG, "clearBatteryHistory() 调用"); LogUtils.d(TAG, "clearBatteryHistory() called");
if (sAppCacheUtils != null) { if (sAppCacheUtils != null) {
sAppCacheUtils.clearBatteryHistory(); sAppCacheUtils.clearBatteryHistory();
LogUtils.d(TAG, "clearBatteryHistory() | 电池历史数据清除成功"); LogUtils.d(TAG, "clearBatteryHistory: Success");
} else { } else {
LogUtils.w(TAG, "clearBatteryHistory() | 失败:AppCacheUtils未初始化"); LogUtils.w(TAG, "clearBatteryHistory: Failed, sAppCacheUtils is null");
} }
} }
/** /**
* 手动清理所有缓存(带严格权限控制,仅主动调用生效) * 手动清理所有缓存(仅主动调用生效)
* 极致强制缓存策略下,仅提供手动清理入口,永不自动调用
*/ */
public static void manualClearAllCache() { public static void manualClearAllCache() {
LogUtils.w(CACHE_PROTECT_TAG, "manualClearAllCache() 调用 | 极致缓存策略下谨慎使用"); LogUtils.w(CACHE_PROTECT_TAG, "manualClearAllCache() called - Manual trigger only");
// 清理Bitmap缓存
if (sBitmapCacheUtils != null) { if (sBitmapCacheUtils != null) {
sBitmapCacheUtils.clearAllCache(); sBitmapCacheUtils.clearAllCache();
LogUtils.d(CACHE_PROTECT_TAG, "manualClearAllCache() | Bitmap缓存手动清理完成"); LogUtils.d(CACHE_PROTECT_TAG, "manualClearAllCache: Bitmap cache cleared");
} }
// 清理视图控件缓存(仅清除静态引用,不销毁实例 // 仅置空引用,不销毁实例(符合极致缓存策略
if (sMemoryCachedBackgroundView != null) { if (sMemoryCachedBackgroundView != null) {
LogUtils.d(CACHE_PROTECT_TAG, "manualClearAllCache() | 视图缓存保留实例,仅清除静态引用"); LogUtils.d(CACHE_PROTECT_TAG, "manualClearAllCache: View cache reference cleared");
sMemoryCachedBackgroundView = null; sMemoryCachedBackgroundView = null;
} }
LogUtils.w(CACHE_PROTECT_TAG, "manualClearAllCache() | 手动清理完成 | 部分缓存实例仍驻留内存");
LogUtils.w(CACHE_PROTECT_TAG, "manualClearAllCache: Manual cleanup finished");
} }
/** /**
* 获取视图控件缓存实例非通用仅通过App实例调用避免全局直接访问 * 获取视图缓存实例
*/ */
public MemoryCachedBackgroundView getMemoryCachedBackgroundView() { public MemoryCachedBackgroundView getMemoryCachedBackgroundView() {
LogUtils.d(TAG, "getMemoryCachedBackgroundView() 调用 | 当前实例:" + sMemoryCachedBackgroundView); LogUtils.d(TAG, "getMemoryCachedBackgroundView() called | Current: " + sMemoryCachedBackgroundView);
return sMemoryCachedBackgroundView; return sMemoryCachedBackgroundView;
} }
/** /**
* 发送调试通知 * 发送通知消息
*/ */
public static void notifyMessage(String title, String content) { public static void notifyMessage(String title, String content) {
LogUtils.d(TAG, "notifyMessage() 调用 | 入参title" + title + " | content" + content); LogUtils.d(TAG, "notifyMessage() called with: title = [" + title + "], content = [" + content + "]");
boolean canNotify = isDebugging() && sApp != null && sNotificationManagerUtils != null; boolean canSend = isDebugging() && sApp != null && sNotificationManagerUtils != null;
if (canNotify) { if (canSend) {
NotificationMessage message = new NotificationMessage(title, content, ""); NotificationMessage message = new NotificationMessage(title, content, "");
sNotificationManagerUtils.showMessageNotification(sApp, message); sNotificationManagerUtils.showMessageNotification(sApp, message);
LogUtils.d(TAG, "notifyMessage() | 调试通知发送成功"); LogUtils.d(TAG, "notifyMessage: Sent successfully");
} else { } else {
LogUtils.d(TAG, "notifyMessage() | 发送失败:调试模式未开启/工具类未初始化"); LogUtils.d(TAG, "notifyMessage: Send failed, conditions not met");
} }
} }
// ===================== 生命周期方法区(按执行顺序排序) ===================== // ==================== 生命周期方法 ====================
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
LogUtils.d(TAG, "onCreate() | 应用启动,开始初始化流程"); LogUtils.d(TAG, "onCreate() called | Initializing application...");
sApp = this;
// 初始化调试模式 sApp = this;
setIsDebugging(BuildConfig.DEBUG); setIsDebugging(BuildConfig.DEBUG);
LogUtils.d(TAG, "onCreate() | 调试模式状态:" + BuildConfig.DEBUG); LogUtils.d(TAG, "onCreate: Debug mode = " + BuildConfig.DEBUG);
// 初始化核心组件 // 初始化核心组件
initBaseTools(); initBaseTools();
initUtils(); initUtils();
initReceiver(); initReceiver();
LogUtils.d(TAG, "onCreate() | 应用初始化完成 | 极致强制缓存策略已启用"); LogUtils.d(TAG, "onCreate: Application initialization completed. Force-cache strategy active.");
} }
@Override @Override
public void onTerminate() { public void onTerminate() {
super.onTerminate(); super.onTerminate();
LogUtils.d(TAG, "onTerminate() | 应用终止,释放非缓存资源"); LogUtils.d(TAG, "onTerminate() called | Releasing non-cache resources");
// 释放轻量级工具 // 释放非缓存资源
ToastUtils.release(); ToastUtils.release();
LogUtils.d(TAG, "onTerminate() | Toast工具资源释放完成");
releaseNotificationManager(); releaseNotificationManager();
releaseReceiver(); releaseReceiver();
// 核心策略:应用终止不清理任何缓存 // 核心策略:不清理缓存
LogUtils.w(CACHE_PROTECT_TAG, "onTerminate() | 极致缓存策略生效 | 所有缓存实例保持驻留"); LogUtils.w(CACHE_PROTECT_TAG, "onTerminate: Force-cache active, caches remain in memory");
LogUtils.d(TAG, "onTerminate() | 非缓存资源释放完成"); LogUtils.d(TAG, "onTerminate: Non-cache resources released");
} }
@Override @Override
public void onTrimMemory(int level) { public void onTrimMemory(int level) {
super.onTrimMemory(level); super.onTrimMemory(level);
// 极致缓存策略:拒绝系统触发的缓存清理 LogUtils.w(CACHE_PROTECT_TAG, "onTrimMemory() called with level: " + level + " | Ignoring, caches protected");
LogUtils.w(CACHE_PROTECT_TAG, "onTrimMemory() 调用 | 内存等级:" + level + " | 强制保持所有缓存");
logDetailedCacheStatus(); logDetailedCacheStatus();
} }
@Override @Override
public void onLowMemory() { public void onLowMemory() {
super.onLowMemory(); super.onLowMemory();
// 低内存场景:不清理缓存,仅记录状态 LogUtils.w(CACHE_PROTECT_TAG, "onLowMemory() called | Force-cache active, no cleanup performed");
LogUtils.w(CACHE_PROTECT_TAG, "onLowMemory() 调用 | 极致缓存策略:不执行任何清理操作");
logDetailedCacheStatus(); logDetailedCacheStatus();
} }
// ===================== 私有初始化方法区(按初始化顺序排序) ===================== // ==================== 私有初始化方法 ====================
/** /**
* 初始化基础工具Activity管理、Toast、通知工具 * 初始化基础工具
*/ */
private void initBaseTools() { private void initBaseTools() {
LogUtils.d(TAG, "initBaseTools() | 开始初始化基础工具集"); LogUtils.d(TAG, "initBaseTools: Starting...");
WinBoLLActivityManager.init(this); WinBoLLActivityManager.init(this);
ToastUtils.init(this); ToastUtils.init(this);
sNotificationManagerUtils = new NotificationManagerUtils(this); sNotificationManagerUtils = new NotificationManagerUtils(this);
LogUtils.d(TAG, "initBaseTools() | ActivityManager/Toast/Notification工具初始化完成"); LogUtils.d(TAG, "initBaseTools: Completed");
} }
/** /**
* 初始化核心工具(极致强制缓存:一旦初始化永不销毁 * 初始化核心工具与缓存(极致强制驻留
*/ */
private void initUtils() { private void initUtils() {
LogUtils.d(TAG, "initUtils() | 开始初始化核心工具类 | 启用极致强制缓存策略"); LogUtils.d(TAG, "initUtils: Starting with force-cache strategy");
// 初始化配置与缓存工具 // 1. 配置与基础缓存
sAppConfigUtils = getAppConfigUtils(this); sAppConfigUtils = getAppConfigUtils(this);
sAppCacheUtils = getAppCacheUtils(this); sAppCacheUtils = getAppCacheUtils(this);
// 初始化背景资源与Bitmap缓存 // 2. 资源与Bitmap缓存
sBackgroundSourceUtils = BackgroundSourceUtils.getInstance(this); sBackgroundSourceUtils = BackgroundSourceUtils.getInstance(this);
sBackgroundSourceUtils.loadSettings(); sBackgroundSourceUtils.loadSettings();
sBitmapCacheUtils = BitmapCacheUtils.getInstance(); sBitmapCacheUtils = BitmapCacheUtils.getInstance();
LogUtils.d(TAG, "initUtils() | BackgroundSource/BitmapCache工具初始化完成 | 永久驻留内存"); LogUtils.d(TAG, "initUtils: Resource & Bitmap tools initialized (Permanent)");
// 初始化视图控件缓存 // 3. 视图缓存
sMemoryCachedBackgroundView = MemoryCachedBackgroundView.getLastInstance(this); sMemoryCachedBackgroundView = MemoryCachedBackgroundView.getLastInstance(this);
if (sMemoryCachedBackgroundView == null) { if (sMemoryCachedBackgroundView == null) {
sMemoryCachedBackgroundView = MemoryCachedBackgroundView.getInstance(this, sBackgroundSourceUtils.getCurrentBackgroundBean(), true); sMemoryCachedBackgroundView = MemoryCachedBackgroundView.getInstance(this, sBackgroundSourceUtils.getCurrentBackgroundBean(), true);
LogUtils.d(TAG, "initUtils() | 视图缓存工具:新建实例完成"); LogUtils.d(TAG, "initUtils: View cache - New instance created");
} }
LogUtils.d(TAG, "initUtils() | MemoryCachedBackgroundView初始化完成 | 永久驻留内存"); LogUtils.d(TAG, "initUtils: View cache initialized (Permanent)");
} }
/** /**
* 初始化全局广播接收器 * 注册全局广播接收器
*/ */
private void initReceiver() { private void initReceiver() {
LogUtils.d(TAG, "initReceiver() | 开始初始化广播接收器"); LogUtils.d(TAG, "initReceiver: Starting...");
mGlobalReceiver = new GlobalApplicationReceiver(this); mGlobalReceiver = new GlobalApplicationReceiver(this);
mGlobalReceiver.registerAction(); mGlobalReceiver.registerAction();
LogUtils.d(TAG, "initReceiver() | 广播接收器注册完成"); LogUtils.d(TAG, "initReceiver: Completed");
} }
// ===================== 私有释放方法区(按资源重要性排序) ===================== // ==================== 私有释放方法 ====================
/** /**
* 释放广播接收器资源 * 释放广播接收器
*/ */
private void releaseReceiver() { private void releaseReceiver() {
LogUtils.d(TAG, "releaseReceiver() | 开始释放广播接收器"); LogUtils.d(TAG, "releaseReceiver: Starting...");
if (mGlobalReceiver != null) { if (mGlobalReceiver != null) {
mGlobalReceiver.unregisterAction(); mGlobalReceiver.unregisterAction();
mGlobalReceiver = null; mGlobalReceiver = null;
LogUtils.d(TAG, "releaseReceiver() | 广播接收器资源释放完成"); LogUtils.d(TAG, "releaseReceiver: Completed");
} else {
LogUtils.d(TAG, "releaseReceiver() | 无需释放:广播接收器未初始化");
} }
} }
/** /**
* 释放通知管理工具资源 * 释放通知管理
*/ */
private void releaseNotificationManager() { private void releaseNotificationManager() {
LogUtils.d(TAG, "releaseNotificationManager() | 开始释放通知工具"); LogUtils.d(TAG, "releaseNotificationManager: Starting...");
if (sNotificationManagerUtils != null) { if (sNotificationManagerUtils != null) {
sNotificationManagerUtils.release(); sNotificationManagerUtils.release();
sNotificationManagerUtils = null; sNotificationManagerUtils = null;
LogUtils.d(TAG, "releaseNotificationManager() | 通知工具资源释放完成"); LogUtils.d(TAG, "releaseNotificationManager: Completed");
} else {
LogUtils.d(TAG, "releaseNotificationManager() | 无需释放:通知工具未初始化");
} }
} }
// ===================== 私有工具方法区(辅助功能) ===================== // ==================== 私有辅助方法 ====================
/** /**
* 记录详细缓存状态(用于调试监控极致强制缓存效果 * 记录当前缓存详细状态(用于调试监控)
*/ */
private void logDetailedCacheStatus() { private void logDetailedCacheStatus() {
LogUtils.d(TAG, "logDetailedCacheStatus() | 开始记录缓存状态"); LogUtils.d(TAG, "logDetailedCacheStatus: Reporting cache state");
// 记录Bitmap缓存状态
if (sBitmapCacheUtils != null) { if (sBitmapCacheUtils != null) {
LogUtils.d(CACHE_PROTECT_TAG, "Bitmap缓存工具实例有效永久驻留"); LogUtils.d(CACHE_PROTECT_TAG, "Cache Status: BitmapCache [Valid]");
try { try {
int cacheCount = sBitmapCacheUtils.getCacheCount(); LogUtils.d(CACHE_PROTECT_TAG, "Cache Detail: Bitmap Count = " + sBitmapCacheUtils.getCacheCount());
LogUtils.d(CACHE_PROTECT_TAG, "Bitmap缓存数量" + cacheCount);
} catch (Exception e) { } catch (Exception e) {
LogUtils.d(CACHE_PROTECT_TAG, "Bitmap缓存数量获取失败 | 异常信息:" + e.getMessage()); LogUtils.d(CACHE_PROTECT_TAG, "Cache Detail: Failed to get bitmap count - " + e.getMessage());
} }
} }
// 记录视图缓存状态
if (sMemoryCachedBackgroundView != null) { if (sMemoryCachedBackgroundView != null) {
LogUtils.d(CACHE_PROTECT_TAG, "视图缓存工具:实例有效(永久驻留)"); LogUtils.d(CACHE_PROTECT_TAG, "Cache Status: ViewCache [Valid]");
int viewCount = MemoryCachedBackgroundView.getInstanceCount(); LogUtils.d(CACHE_PROTECT_TAG, "Cache Detail: View Instance Count = " + MemoryCachedBackgroundView.getInstanceCount());
LogUtils.d(CACHE_PROTECT_TAG, "视图缓存实例总数:" + viewCount);
} }
LogUtils.d(TAG, "logDetailedCacheStatus() | 缓存状态记录完成");
} }
} }