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