229 lines
9.4 KiB
Java
229 lines
9.4 KiB
Java
package cc.winboll.studio.powerbell;
|
||
|
||
import android.content.ComponentCallbacks2;
|
||
import android.content.Context;
|
||
import android.os.Environment;
|
||
import android.os.Handler;
|
||
import android.os.Looper;
|
||
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.BackgroundBean;
|
||
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 java.io.File;
|
||
|
||
public class App extends GlobalApplication {
|
||
public static final String TAG = "App";
|
||
|
||
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 final String TRIM_MEMORY_NOTIFY_TITLE = "应用使用时内存紧张提醒";
|
||
private static final String TRIM_MEMORY_NOTIFY_CONTENT = "由于本应用使用时,系统通知内存紧张程度级别较高,图片缓存功能暂时不启用。";
|
||
|
||
// 数据配置存储工具
|
||
static AppConfigUtils _mAppConfigUtils;
|
||
static AppCacheUtils _mAppCacheUtils;
|
||
// 全局 Bitmap 缓存工具(常驻内存)
|
||
public static BitmapCacheUtils _mBitmapCacheUtils;
|
||
|
||
GlobalApplicationReceiver mReceiver;
|
||
static String szTempDir = "";
|
||
// 通知工具类实例(用于发送内存紧张通知)
|
||
private NotificationManagerUtils mNotificationManager;
|
||
|
||
public static String getTempDirPath() {
|
||
return szTempDir;
|
||
}
|
||
|
||
@Override
|
||
public void onCreate() {
|
||
super.onCreate();
|
||
setIsDebugging(BuildConfig.DEBUG);
|
||
|
||
// 初始化活动窗口管理
|
||
WinBoLLActivityManager.init(this);
|
||
// 初始化 Toast 框架
|
||
ToastUtils.init(this);
|
||
|
||
// 临时文件夹初始化(保持原有逻辑)
|
||
File picturesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
|
||
File powerBellDir = new File(picturesDir, "PowerBell");
|
||
if (!powerBellDir.exists()) {
|
||
powerBellDir.mkdirs();
|
||
}
|
||
szTempDir = powerBellDir.getAbsolutePath();
|
||
|
||
// 设置数据配置存储工具
|
||
_mAppConfigUtils = getAppConfigUtils(this);
|
||
_mAppCacheUtils = getAppCacheUtils(this);
|
||
// 初始化全局 Bitmap 缓存工具
|
||
_mBitmapCacheUtils = BitmapCacheUtils.getInstance();
|
||
|
||
// 初始化通知工具类(使用整理后的 NotificationManagerUtils)
|
||
mNotificationManager = new NotificationManagerUtils(this);
|
||
LogUtils.d(TAG, "onCreate: 通知工具类初始化完成");
|
||
|
||
mReceiver = new GlobalApplicationReceiver(this);
|
||
mReceiver.registerAction();
|
||
|
||
// 异步预加载背景图(保持原有逻辑)
|
||
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
new Thread(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
try {
|
||
BackgroundSourceUtils bgSourceUtils = BackgroundSourceUtils.getInstance(App.this);
|
||
if (bgSourceUtils == null) {
|
||
LogUtils.e(TAG, "preloadBitmap: BackgroundSourceUtils 实例为空");
|
||
return;
|
||
}
|
||
BackgroundBean bgBean = bgSourceUtils.getCurrentBackgroundBean();
|
||
if (bgBean == null || !bgBean.isUseBackgroundFile()) {
|
||
LogUtils.d(TAG, "preloadBitmap: 无有效背景文件,跳过预加载");
|
||
return;
|
||
}
|
||
String bgPath = bgBean.isUseBackgroundScaledCompressFile()
|
||
? bgBean.getBackgroundScaledCompressFilePath()
|
||
: bgBean.getBackgroundFilePath();
|
||
if (_mBitmapCacheUtils != null) {
|
||
_mBitmapCacheUtils.cacheBitmap(bgPath);
|
||
LogUtils.d(TAG, "preloadBitmap: 应用启动时预加载成功 - " + bgPath);
|
||
} else {
|
||
LogUtils.e(TAG, "preloadBitmap: 全局 BitmapCacheUtils 未初始化");
|
||
}
|
||
} catch (Exception e) {
|
||
LogUtils.e(TAG, "preloadBitmap: 预加载失败 - " + e.getMessage());
|
||
}
|
||
}
|
||
}).start();
|
||
}
|
||
}, 1000);
|
||
}
|
||
|
||
// 保持原有方法不变
|
||
public static AppConfigUtils getAppConfigUtils(Context context) {
|
||
if (_mAppConfigUtils == null) {
|
||
_mAppConfigUtils = AppConfigUtils.getInstance(context);
|
||
}
|
||
return _mAppConfigUtils;
|
||
}
|
||
|
||
public static AppCacheUtils getAppCacheUtils(Context context) {
|
||
if (_mAppCacheUtils == null) {
|
||
_mAppCacheUtils = AppCacheUtils.getInstance(context);
|
||
}
|
||
return _mAppCacheUtils;
|
||
}
|
||
|
||
public void clearBatteryHistory() {
|
||
_mAppCacheUtils.clearBatteryHistory();
|
||
}
|
||
|
||
@Override
|
||
public void onTerminate() {
|
||
super.onTerminate();
|
||
ToastUtils.release();
|
||
// 释放通知工具类资源,避免内存泄漏
|
||
if (mNotificationManager != null) {
|
||
mNotificationManager.release();
|
||
mNotificationManager = null;
|
||
LogUtils.d(TAG, "onTerminate: 通知工具类资源已释放");
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onTrimMemory(int level) {
|
||
super.onTrimMemory(level);
|
||
LogUtils.d(TAG, "onTrimMemory: 内存等级变化 | level=" + getTrimMemoryLevelDesc(level));
|
||
|
||
// 仅在中等及以上内存紧张等级发送通知,避免频繁打扰
|
||
if (mNotificationManager == null) {
|
||
mNotificationManager = new NotificationManagerUtils(this);
|
||
}
|
||
if (level > ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
|
||
sendTrimMemoryNotification(level);
|
||
} else {
|
||
// 再次缓存 Bitmap 缓存工具
|
||
_mBitmapCacheUtils = BitmapCacheUtils.getInstance();
|
||
LogUtils.d(TAG, "Bitmap 缓存启用中。");
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 发送内存紧张通知(完全复用 NotificationManagerUtils 的 showRemindNotification 方法)
|
||
*/
|
||
private void sendTrimMemoryNotification(int level) {
|
||
LogUtils.d(TAG, "sendTrimMemoryNotification: 准备发送内存紧张通知");
|
||
// 构建通知消息体
|
||
NotificationMessage message = new NotificationMessage();
|
||
message.setTitle(TRIM_MEMORY_NOTIFY_TITLE);
|
||
message.setContent(String.format("%s [ 缓存紧张级别描述: Level %d | %s ]",TRIM_MEMORY_NOTIFY_CONTENT, level, getTrimMemoryLevelDesc(level)));
|
||
// 使用整理后的 NotificationManagerUtils 发送通知(复用提醒渠道配置)
|
||
mNotificationManager.showConfigNotification(this, message);
|
||
LogUtils.d(TAG, "sendTrimMemoryNotification: 通知已通过 NotificationManagerUtils 发送");
|
||
}
|
||
|
||
/**
|
||
* 转换内存等级为可读描述,便于日志调试
|
||
* 排序规则:按 ComponentCallbacks2 枚举数值从高到低排列(数值越高,内存越紧张)
|
||
*/
|
||
/**
|
||
* 转换内存等级为可读描述,便于日志调试
|
||
* 排序规则:按 ComponentCallbacks2 枚举实际数值(10进制)从高到低排列
|
||
* 数值来源:接口中定义的 16进制(注释10进制)数值
|
||
*/
|
||
private String getTrimMemoryLevelDesc(int level) {
|
||
switch (level) {
|
||
// 数值 80(0x50):应用内存完全紧张(补充接口中实际存在的枚举项)
|
||
case ComponentCallbacks2.TRIM_MEMORY_COMPLETE:
|
||
return "TRIM_MEMORY_COMPLETE(应用内存完全紧张)";
|
||
// 数值 60(0x3c):中等内存紧张
|
||
case ComponentCallbacks2.TRIM_MEMORY_MODERATE:
|
||
return "MODERATE(中等内存紧张)";
|
||
// 数值 40(0x28):应用进入后台
|
||
case ComponentCallbacks2.TRIM_MEMORY_BACKGROUND:
|
||
return "BACKGROUND(应用进入后台)";
|
||
// 数值 20(0x14):应用UI隐藏
|
||
case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN:
|
||
return "BACKGROUND(应用UI隐藏)";
|
||
// 数值 15(0xf):应用运行时关键级紧张
|
||
case ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL:
|
||
return "RUNNING_CRITICAL(应用运行关键级紧张)";
|
||
// 数值 10(0xa):应用运行时低内存
|
||
case ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW:
|
||
return "RUNNING_LOW(应用运行低内存)";
|
||
// 数值 5(0x5):应用运行时中等紧张
|
||
case ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE:
|
||
return "RUNNING_MODERATE(应用运行中等内存紧张)";
|
||
|
||
// 以下为注释备用项(接口中未提供,按你的原有注释保留)
|
||
// 数值 100:内存极度紧张(系统可能强制杀死应用)
|
||
// case ComponentCallbacks2.TRIM_MEMORY_URGENT:
|
||
// return "URGENT(内存极度紧张)";
|
||
// 数值 20:用户正在离开应用(如按Home键)
|
||
// case ComponentCallbacks2.TRIM_MEMORY_USER_LEAVING:
|
||
// return "USER_LEAVING(用户正在离开应用)";
|
||
|
||
// 未知等级
|
||
default:
|
||
return "UNKNOWN(" + level + ")";
|
||
}
|
||
}
|
||
}
|
||
|