Files
WinBoLL/powerbell/src/main/java/cc/winboll/studio/powerbell/App.java
2025-12-29 09:45:01 +08:00

309 lines
11 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cc.winboll.studio.powerbell;
import android.content.Context;
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 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;
/**
* 应用全局入口类适配Android API 30基于Java 7编写
* 核心策略:极致强制缓存 - 无论内存紧张程度永不自动清理任何缓存Bitmap/视图控件/路径记录)
*
* @Author 豆包&ZhanGSKen<zhangsken@qq.com>
* @Version 1.0.0
* @Date 2025-12-29
*/
public class App extends GlobalApplication {
// ==================== 常量定义 ====================
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_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;
// ==================== 公共静态方法 (工具/单例) ====================
/**
* 获取应用单例
*/
public static App getInstance() {
LogUtils.d(TAG, "getInstance() called | Result: " + sApp);
return sApp;
}
/**
* 获取配置工具实例
*/
public static AppConfigUtils getAppConfigUtils(Context context) {
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: Initialized new instance");
}
return sAppConfigUtils;
}
/**
* 获取缓存工具实例
*/
public static AppCacheUtils getAppCacheUtils(Context context) {
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: Initialized new instance");
}
return sAppCacheUtils;
}
// ==================== 公共成员方法 (业务) ====================
/**
* 清除电池历史数据
*/
public void clearBatteryHistory() {
LogUtils.d(TAG, "clearBatteryHistory() called");
if (sAppCacheUtils != null) {
sAppCacheUtils.clearBatteryHistory();
LogUtils.d(TAG, "clearBatteryHistory: Success");
} else {
LogUtils.w(TAG, "clearBatteryHistory: Failed, sAppCacheUtils is null");
}
}
/**
* 手动清理所有缓存(仅主动调用生效)
*/
public static void manualClearAllCache() {
LogUtils.w(CACHE_PROTECT_TAG, "manualClearAllCache() called - Manual trigger only");
if (sBitmapCacheUtils != null) {
sBitmapCacheUtils.clearAllCache();
LogUtils.d(CACHE_PROTECT_TAG, "manualClearAllCache: Bitmap cache cleared");
}
// 仅置空引用,不销毁实例(符合极致缓存策略)
if (sMemoryCachedBackgroundView != null) {
LogUtils.d(CACHE_PROTECT_TAG, "manualClearAllCache: View cache reference cleared");
sMemoryCachedBackgroundView = null;
}
LogUtils.w(CACHE_PROTECT_TAG, "manualClearAllCache: Manual cleanup finished");
}
/**
* 获取视图缓存实例
*/
public MemoryCachedBackgroundView getMemoryCachedBackgroundView() {
LogUtils.d(TAG, "getMemoryCachedBackgroundView() called | Current: " + sMemoryCachedBackgroundView);
return sMemoryCachedBackgroundView;
}
/**
* 发送通知消息
*/
public static void notifyMessage(String title, String content) {
LogUtils.d(TAG, "notifyMessage() called with: title = [" + title + "], content = [" + content + "]");
boolean canSend = isDebugging() && sApp != null && sNotificationManagerUtils != null;
if (canSend) {
NotificationMessage message = new NotificationMessage(title, content, "");
sNotificationManagerUtils.showMessageNotification(sApp, message);
LogUtils.d(TAG, "notifyMessage: Sent successfully");
} else {
LogUtils.d(TAG, "notifyMessage: Send failed, conditions not met");
}
}
// ==================== 生命周期方法 ====================
@Override
public void onCreate() {
super.onCreate();
LogUtils.d(TAG, "onCreate() called | Initializing application...");
sApp = this;
setIsDebugging(BuildConfig.DEBUG);
LogUtils.d(TAG, "onCreate: Debug mode = " + BuildConfig.DEBUG);
// 初始化核心组件
initBaseTools();
initUtils();
initReceiver();
LogUtils.d(TAG, "onCreate: Application initialization completed. Force-cache strategy active.");
}
@Override
public void onTerminate() {
super.onTerminate();
LogUtils.d(TAG, "onTerminate() called | Releasing non-cache resources");
// 仅释放非缓存资源
ToastUtils.release();
releaseNotificationManager();
releaseReceiver();
// 核心策略:不清理缓存
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() called with level: " + level + " | Ignoring, caches protected");
logDetailedCacheStatus();
}
@Override
public void onLowMemory() {
super.onLowMemory();
LogUtils.w(CACHE_PROTECT_TAG, "onLowMemory() called | Force-cache active, no cleanup performed");
logDetailedCacheStatus();
}
// ==================== 私有初始化方法 ====================
/**
* 初始化基础工具类
*/
private void initBaseTools() {
LogUtils.d(TAG, "initBaseTools: Starting...");
WinBoLLActivityManager.init(this);
ToastUtils.init(this);
sNotificationManagerUtils = new NotificationManagerUtils(this);
LogUtils.d(TAG, "initBaseTools: Completed");
}
/**
* 初始化核心工具与缓存(极致强制驻留)
*/
private void initUtils() {
LogUtils.d(TAG, "initUtils: Starting with force-cache strategy");
// 1. 配置与基础缓存
sAppConfigUtils = getAppConfigUtils(this);
sAppCacheUtils = getAppCacheUtils(this);
// 2. 资源与Bitmap缓存
sBackgroundSourceUtils = BackgroundSourceUtils.getInstance(this);
sBackgroundSourceUtils.loadSettings();
sBitmapCacheUtils = BitmapCacheUtils.getInstance();
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: View cache - New instance created");
}
LogUtils.d(TAG, "initUtils: View cache initialized (Permanent)");
}
/**
* 注册全局广播接收器
*/
private void initReceiver() {
LogUtils.d(TAG, "initReceiver: Starting...");
mGlobalReceiver = new GlobalApplicationReceiver(this);
mGlobalReceiver.registerAction();
LogUtils.d(TAG, "initReceiver: Completed");
}
// ==================== 私有释放方法 ====================
/**
* 释放广播接收器
*/
private void releaseReceiver() {
LogUtils.d(TAG, "releaseReceiver: Starting...");
if (mGlobalReceiver != null) {
mGlobalReceiver.unregisterAction();
mGlobalReceiver = null;
LogUtils.d(TAG, "releaseReceiver: Completed");
}
}
/**
* 释放通知管理器
*/
private void releaseNotificationManager() {
LogUtils.d(TAG, "releaseNotificationManager: Starting...");
if (sNotificationManagerUtils != null) {
sNotificationManagerUtils.release();
sNotificationManagerUtils = null;
LogUtils.d(TAG, "releaseNotificationManager: Completed");
}
}
// ==================== 私有辅助方法 ====================
/**
* 记录当前缓存详细状态(用于调试监控)
*/
private void logDetailedCacheStatus() {
LogUtils.d(TAG, "logDetailedCacheStatus: Reporting cache state");
if (sBitmapCacheUtils != null) {
LogUtils.d(CACHE_PROTECT_TAG, "Cache Status: BitmapCache [Valid]");
try {
LogUtils.d(CACHE_PROTECT_TAG, "Cache Detail: Bitmap Count = " + sBitmapCacheUtils.getCacheCount());
} catch (Exception e) {
LogUtils.d(CACHE_PROTECT_TAG, "Cache Detail: Failed to get bitmap count - " + e.getMessage());
}
}
if (sMemoryCachedBackgroundView != null) {
LogUtils.d(CACHE_PROTECT_TAG, "Cache Status: ViewCache [Valid]");
LogUtils.d(CACHE_PROTECT_TAG, "Cache Detail: View Instance Count = " + MemoryCachedBackgroundView.getInstanceCount());
}
}
}