源码整理

This commit is contained in:
2025-12-26 19:47:06 +08:00
parent 4e98c8d699
commit 7278e9f22f
2 changed files with 45 additions and 50 deletions

View File

@@ -39,8 +39,8 @@ import java.util.Map;
public class BatteryReportActivity extends WinBoLLActivity implements IWinBoLLActivity { public class BatteryReportActivity extends WinBoLLActivity implements IWinBoLLActivity {
// ======================== 静态常量(按功能分类) ========================= // ======================== 静态常量(按功能分类) =========================
public static final String TAG = "BatteryReportActivity"; public static final String TAG = "BatteryReportActivity";
private static final long ONE_DAY_MS = 24 * 3600 * 1000; // 24小时毫秒数 private static final long ONE_DAY_MS = 24 * 3600 * 1000; // 24小时毫秒数
private static final long ONE_MINUTE_MS = 60 * 1000; // 1分钟毫秒数 private static final long ONE_MINUTE_MS = 60 * 1000; // 1分钟毫秒数
// ======================== 成员变量(按依赖优先级+功能分类) ========================= // ======================== 成员变量(按依赖优先级+功能分类) =========================
// UI组件 // UI组件
@@ -50,18 +50,18 @@ public class BatteryReportActivity extends WinBoLLActivity implements IWinBoLLAc
// 数据与适配器 // 数据与适配器
private BatteryReportAdapter adapter; private BatteryReportAdapter adapter;
private List<AppBatteryModel> dataList = new ArrayList<AppBatteryModel>(); private List<AppBatteryModel> dataList = new ArrayList<>();
private List<AppBatteryModel> filteredList = new ArrayList<AppBatteryModel>(); private List<AppBatteryModel> filteredList = new ArrayList<>();
// 电池相关 // 电池相关
private BroadcastReceiver batteryReceiver; private BroadcastReceiver batteryReceiver;
private int batteryCapacity = 5400; // 电池容量mAh private int batteryCapacity = 5400; // 电池容量mAh
private float lastBatteryPercent = 100.0f; private float lastBatteryPercent = 100.0f; // 上次电池百分比
private long lastCheckTime = System.currentTimeMillis(); private long lastCheckTime = System.currentTimeMillis(); // 上次检查时间戳
// 缓存相关 // 缓存相关
private Map<String, Long> appRunTimeCache = new HashMap<String, Long>(); private Map<String, Long> appRunTimeCache = new HashMap<>();
private Map<String, String> packageToAppNameCache = new HashMap<String, String>(); private Map<String, String> packageToAppNameCache = new HashMap<>();
private PackageManager mPackageManager; private PackageManager mPackageManager;
// ======================== 接口实现方法 ========================= // ======================== 接口实现方法 =========================
@@ -110,9 +110,8 @@ public class BatteryReportActivity extends WinBoLLActivity implements IWinBoLLAc
rvBatteryReport.setAdapter(adapter); rvBatteryReport.setAdapter(adapter);
LogUtils.d(TAG, "【onCreate】适配器初始化完成过滤后数据量" + filteredList.size()); LogUtils.d(TAG, "【onCreate】适配器初始化完成过滤后数据量" + filteredList.size());
// 绑定搜索监听 // 绑定搜索监听 + 注册电池广播
bindSearchListener(); bindSearchListener();
// 注册电池广播
registerBatteryReceiver(); registerBatteryReceiver();
LogUtils.d(TAG, "【onCreate】BatteryReportActivity 初始化完成"); LogUtils.d(TAG, "【onCreate】BatteryReportActivity 初始化完成");
@@ -185,11 +184,14 @@ public class BatteryReportActivity extends WinBoLLActivity implements IWinBoLLAc
float dropPercent = lastBatteryPercent - currentPercent; float dropPercent = lastBatteryPercent - currentPercent;
long duration = System.currentTimeMillis() - lastCheckTime; long duration = System.currentTimeMillis() - lastCheckTime;
LogUtils.d(TAG, "【电池广播】电池消耗:" + dropPercent + "%,时长:" + formatRunTime(duration)); LogUtils.d(TAG, "【电池广播】电池消耗:" + dropPercent + "%,时长:" + formatRunTime(duration));
// 更新运行时长并计算耗电
appRunTimeCache = getAppRunTime(); appRunTimeCache = getAppRunTime();
updateAppRunTimeToModel(); updateAppRunTimeToModel();
calculateSingleConsumptionAndAccumulate(dropPercent, appRunTimeCache); calculateSingleConsumptionAndAccumulate(dropPercent, appRunTimeCache);
} }
// 刷新记录
lastBatteryPercent = currentPercent; lastBatteryPercent = currentPercent;
lastCheckTime = System.currentTimeMillis(); lastCheckTime = System.currentTimeMillis();
} }
@@ -234,15 +236,12 @@ public class BatteryReportActivity extends WinBoLLActivity implements IWinBoLLAc
private void loadAllAppPackage() { private void loadAllAppPackage() {
List<ApplicationInfo> appList = mPackageManager.getInstalledApplications(PackageManager.GET_META_DATA); List<ApplicationInfo> appList = mPackageManager.getInstalledApplications(PackageManager.GET_META_DATA);
dataList.clear(); dataList.clear();
LogUtils.d(TAG, "【loadAllAppPackage】开始加载应用包名列表共找到" + appList.size() + "个应用"); LogUtils.d(TAG, "【loadAllAppPackage】开始加载应用包名列表共找到" + appList.size() + "个应用");
for (ApplicationInfo appInfo : appList) { for (ApplicationInfo appInfo : appList) {
String packageName = appInfo.packageName; String packageName = appInfo.packageName;
// 初始化:单次耗电=0累计耗电=0运行时长=0
dataList.add(new AppBatteryModel(packageName, 0.0f, 0.0f, 0)); dataList.add(new AppBatteryModel(packageName, 0.0f, 0.0f, 0));
} }
LogUtils.d(TAG, "【loadAllAppPackage】应用包名列表加载完成共添加" + dataList.size() + "个包名"); LogUtils.d(TAG, "【loadAllAppPackage】应用包名列表加载完成共添加" + dataList.size() + "个包名");
} }
@@ -258,7 +257,6 @@ public class BatteryReportActivity extends WinBoLLActivity implements IWinBoLLAc
String appName = getAppNameByPackage(packageName); String appName = getAppNameByPackage(packageName);
packageToAppNameCache.put(packageName, appName); packageToAppNameCache.put(packageName, appName);
} }
LogUtils.d(TAG, "【preCacheAllAppNames】预缓存完成共缓存" + packageToAppNameCache.size() + "个应用名称"); LogUtils.d(TAG, "【preCacheAllAppNames】预缓存完成共缓存" + packageToAppNameCache.size() + "个应用名称");
} }
@@ -302,7 +300,7 @@ public class BatteryReportActivity extends WinBoLLActivity implements IWinBoLLAc
* @return 应用包名-运行时长ms映射 * @return 应用包名-运行时长ms映射
*/ */
private Map<String, Long> getAppRunTime() { private Map<String, Long> getAppRunTime() {
Map<String, Long> runTimeMap = new HashMap<String, Long>(); Map<String, Long> runTimeMap = new HashMap<>();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
try { try {
android.app.usage.UsageStatsManager manager = android.app.usage.UsageStatsManager manager =
@@ -347,7 +345,6 @@ public class BatteryReportActivity extends WinBoLLActivity implements IWinBoLLAc
String packageName = model.getPackageName(); String packageName = model.getPackageName();
Long app24hRunTime = appRunTimeCache.getOrDefault(packageName, 0L); Long app24hRunTime = appRunTimeCache.getOrDefault(packageName, 0L);
// 计算占比与累计耗电
float ratio = (total24hRunTime > 0) ? (float) app24hRunTime / total24hRunTime : 0; float ratio = (total24hRunTime > 0) ? (float) app24hRunTime / total24hRunTime : 0;
float initialTotalConsumption = batteryCapacity * ratio; float initialTotalConsumption = batteryCapacity * ratio;
model.setTotalConsumption(initialTotalConsumption); model.setTotalConsumption(initialTotalConsumption);
@@ -357,9 +354,9 @@ public class BatteryReportActivity extends WinBoLLActivity implements IWinBoLLAc
} }
/** /**
* 计算单次耗电赋值给consumption+ 累加至累计耗电totalConsumption = totalConsumption + consumption * 计算单次耗电赋值给consumption+ 累加至累计耗电
* @param dropPercent 电池下降百分比 * @param dropPercent 电池下降百分比
* @param runTimeMap 应用运行时长映射 * @param runTimeMap 应用运行时长映射
*/ */
private void calculateSingleConsumptionAndAccumulate(float dropPercent, Map<String, Long> runTimeMap) { private void calculateSingleConsumptionAndAccumulate(float dropPercent, Map<String, Long> runTimeMap) {
LogUtils.d(TAG, "【calculateSingleConsumptionAndAccumulate】开始计算电池下降百分比" + dropPercent); LogUtils.d(TAG, "【calculateSingleConsumptionAndAccumulate】开始计算电池下降百分比" + dropPercent);
@@ -370,28 +367,25 @@ public class BatteryReportActivity extends WinBoLLActivity implements IWinBoLLAc
} }
LogUtils.d(TAG, "【calculateSingleConsumptionAndAccumulate】本次电池下降总运行时长" + formatRunTime(totalSingleRunTime)); LogUtils.d(TAG, "【calculateSingleConsumptionAndAccumulate】本次电池下降总运行时长" + formatRunTime(totalSingleRunTime));
// 2. 遍历计算每个应用的单次耗电”并“累加至累计” // 2. 遍历计算每个应用的单次耗电并累加
for (AppBatteryModel model : dataList) { for (AppBatteryModel model : dataList) {
String packageName = model.getPackageName(); String packageName = model.getPackageName();
Long appSingleRunTime = runTimeMap.getOrDefault(packageName, 0L); Long appSingleRunTime = runTimeMap.getOrDefault(packageName, 0L);
// 步骤1计算本次单次耗电
float ratio = (totalSingleRunTime > 0) ? (float) appSingleRunTime / totalSingleRunTime : 0; float ratio = (totalSingleRunTime > 0) ? (float) appSingleRunTime / totalSingleRunTime : 0;
float singleConsumption = batteryCapacity * dropPercent / 100 * ratio; float singleConsumption = batteryCapacity * dropPercent / 100 * ratio;
model.setConsumption(singleConsumption); model.setConsumption(singleConsumption);
// 步骤2累加单次耗电到累计耗电 // 累加至累计耗电
float newTotalConsumption = model.getTotalConsumption() + singleConsumption; float newTotalConsumption = model.getTotalConsumption() + singleConsumption;
model.setTotalConsumption(newTotalConsumption); model.setTotalConsumption(newTotalConsumption);
// 同步运行时长
model.setRunTime(appSingleRunTime); model.setRunTime(appSingleRunTime);
LogUtils.v(TAG, String.format("【calculateSingleConsumptionAndAccumulate】应用包%s单次耗电%.1f mAh累计耗电%.1f mAh", LogUtils.v(TAG, String.format("【calculateSingleConsumptionAndAccumulate】应用包%s单次耗电%.1f mAh累计耗电%.1f mAh",
packageName, singleConsumption, newTotalConsumption)); packageName, singleConsumption, newTotalConsumption));
} }
// 3. 按累计耗电排序(从高到低) // 3. 按累计耗电降序排序
Collections.sort(dataList, new Comparator<AppBatteryModel>() { Collections.sort(dataList, new Comparator<AppBatteryModel>() {
@Override @Override
public int compare(AppBatteryModel m1, AppBatteryModel m2) { public int compare(AppBatteryModel m1, AppBatteryModel m2) {
@@ -399,7 +393,7 @@ public class BatteryReportActivity extends WinBoLLActivity implements IWinBoLLAc
} }
}); });
// 4. 重新应用过滤并刷新列表 // 4. 重新过滤并刷新列表
filterAppsByPackageAndName(etSearch.getText().toString().trim()); filterAppsByPackageAndName(etSearch.getText().toString().trim());
LogUtils.d(TAG, "【calculateSingleConsumptionAndAccumulate】单次耗电计算与累加完成列表已刷新"); LogUtils.d(TAG, "【calculateSingleConsumptionAndAccumulate】单次耗电计算与累加完成列表已刷新");
} }
@@ -421,9 +415,7 @@ public class BatteryReportActivity extends WinBoLLActivity implements IWinBoLLAc
String appName = packageToAppNameCache.get(packageName); String appName = packageToAppNameCache.get(packageName);
String appNameLower = appName.toLowerCase(); String appNameLower = appName.toLowerCase();
boolean isMatched = packageNameLower.contains(lowerKeyword) boolean isMatched = packageNameLower.contains(lowerKeyword) || appNameLower.contains(lowerKeyword);
|| appNameLower.contains(lowerKeyword);
if (isMatched) { if (isMatched) {
filteredList.add(model); filteredList.add(model);
} }

View File

@@ -22,19 +22,20 @@ import java.util.ArrayList;
/** /**
* 电池记录清理页面,支持滑动清理记录、切换记录显示格式 * 电池记录清理页面,支持滑动清理记录、切换记录显示格式
* 适配 API30基于 Java7 开发 * 适配 API30基于 Java7 开发
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com> * @Author 豆包&ZhanGSKen<zhangsken@qq.com>
*/ */
public class ClearRecordActivity extends WinBoLLActivity implements IWinBoLLActivity { public class ClearRecordActivity extends WinBoLLActivity implements IWinBoLLActivity {
// ======================== 静态常量 ========================= // ======================== 静态常量(按功能分类) =========================
public static final String TAG = "ClearRecordActivity"; public static final String TAG = "ClearRecordActivity";
private static final String TOAST_MSG_CLEAR_SUCCESS = "The APP battery record is cleaned."; private static final String TOAST_MSG_CLEAR_SUCCESS = "The APP battery record is cleaned.";
// ======================== 成员变量 ========================= // ======================== 成员变量(按依赖优先级+功能分类) =========================
// UI组件 // UI组件
private Toolbar mToolbar; private Toolbar mToolbar;
private TextView mtvRecordText; private TextView mtvRecordText;
private TextView tvAOHPCTCSeekBarMSG; private TextView tvAOHPCTCSeekBarMSG;
private AOHPCTCSeekBar aOHPCTCSeekBar; private AOHPCTCSeekBar aOHPCTCSeekBar;
// 应用与配置 // 应用与配置
private App mApplication; private App mApplication;
private boolean mIsShowRecordWithEnter = false; // 记录是否带换行显示 private boolean mIsShowRecordWithEnter = false; // 记录是否带换行显示
@@ -50,7 +51,7 @@ public class ClearRecordActivity extends WinBoLLActivity implements IWinBoLLActi
return TAG; return TAG;
} }
// ======================== 生命周期方法 ========================= // ======================== 生命周期方法(按执行顺序排列) =========================
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@@ -59,11 +60,11 @@ public class ClearRecordActivity extends WinBoLLActivity implements IWinBoLLActi
// 初始化应用实例 // 初始化应用实例
mApplication = (App) getApplication(); mApplication = (App) getApplication();
// 初始化UI组件 LogUtils.d(TAG, "【onCreate】应用实例初始化完成");
// 初始化核心逻辑
initView(); initView();
// 初始化滑动清理控件
initSeekBar(); initSeekBar();
// 初始化记录显示文本
initRecordText(); initRecordText();
LogUtils.d(TAG, "【onCreate】ClearRecordActivity 初始化完成"); LogUtils.d(TAG, "【onCreate】ClearRecordActivity 初始化完成");
@@ -75,7 +76,7 @@ public class ClearRecordActivity extends WinBoLLActivity implements IWinBoLLActi
*/ */
private void initView() { private void initView() {
// 初始化Toolbar // 初始化Toolbar
mToolbar = findViewById(R.id.toolbar); mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar); setSupportActionBar(mToolbar);
mToolbar.setSubtitle(getTag()); mToolbar.setSubtitle(getTag());
mToolbar.setTitleTextAppearance(this, R.style.Toolbar_TitleText); mToolbar.setTitleTextAppearance(this, R.style.Toolbar_TitleText);
@@ -83,29 +84,30 @@ public class ClearRecordActivity extends WinBoLLActivity implements IWinBoLLActi
mToolbar.setNavigationOnClickListener(new View.OnClickListener() { mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
LogUtils.d(TAG, "【导航栏】点击返回"); LogUtils.d(TAG, "【导航栏】点击返回按钮,关闭当前页面");
finish(); finish();
} }
}); });
// 初始化显示文本组件 // 初始化显示文本组件
tvAOHPCTCSeekBarMSG = findViewById(R.id.activityclearrecordTextView1); tvAOHPCTCSeekBarMSG = (TextView) findViewById(R.id.activityclearrecordTextView1);
mtvRecordText = findViewById(R.id.activityclearrecordTextView2); mtvRecordText = (TextView) findViewById(R.id.activityclearrecordTextView2);
tvAOHPCTCSeekBarMSG.setText(R.string.msg_AOHPCTCSeekBar_ClearRecord); tvAOHPCTCSeekBarMSG.setText(R.string.msg_AOHPCTCSeekBar_ClearRecord);
LogUtils.d(TAG, "【initView】UI组件初始化完成"); LogUtils.d(TAG, "【initView】UI组件初始化完成");
} }
/** /**
* 初始化滑动清理控件 * 初始化滑动清理控件,设置回调监听
*/ */
private void initSeekBar() { private void initSeekBar() {
aOHPCTCSeekBar = findViewById(R.id.activityclearrecordAOHPCTCSeekBar1); aOHPCTCSeekBar = (AOHPCTCSeekBar) findViewById(R.id.activityclearrecordAOHPCTCSeekBar1);
aOHPCTCSeekBar.setThumb(getDrawable(R.drawable.cursor_pointer)); aOHPCTCSeekBar.setThumb(getDrawable(R.drawable.cursor_pointer));
aOHPCTCSeekBar.setThumbOffset(0); aOHPCTCSeekBar.setThumbOffset(0);
aOHPCTCSeekBar.setOnOHPCListener(new AOHPCTCSeekBar.OnOHPCListener() { aOHPCTCSeekBar.setOnOHPCListener(new AOHPCTCSeekBar.OnOHPCListener() {
@Override @Override
public void onOHPCommit() { public void onOHPCommit() {
LogUtils.d(TAG, "【onOHPCommit】滑动清理触发"); LogUtils.d(TAG, "【onOHPCommit】滑动清理触发,开始执行记录清理逻辑");
// 清理电池历史记录 // 清理电池历史记录
mApplication.clearBatteryHistory(); mApplication.clearBatteryHistory();
// 发送广播更新前台通知 // 发送广播更新前台通知
@@ -114,10 +116,11 @@ public class ClearRecordActivity extends WinBoLLActivity implements IWinBoLLActi
initRecordText(); initRecordText();
// 提示清理成功 // 提示清理成功
ToastUtils.show(TOAST_MSG_CLEAR_SUCCESS); ToastUtils.show(TOAST_MSG_CLEAR_SUCCESS);
LogUtils.d(TAG, "【onOHPCommit】电池记录清理完成已发送更新广播"); LogUtils.d(TAG, "【onOHPCommit】电池记录清理完成已发送前台通知更新广播");
} }
}); });
LogUtils.d(TAG, "【initSeekBar】滑动清理控件初始化完成");
LogUtils.d(TAG, "【initSeekBar】滑动清理控件初始化完成回调监听已绑定");
} }
// ======================== 业务逻辑方法 ========================= // ======================== 业务逻辑方法 =========================
@@ -131,20 +134,20 @@ public class ClearRecordActivity extends WinBoLLActivity implements IWinBoLLActi
// 判空处理:避免空列表导致异常 // 判空处理:避免空列表导致异常
if (listBatteryInfo == null || listBatteryInfo.isEmpty()) { if (listBatteryInfo == null || listBatteryInfo.isEmpty()) {
szRecordText = getString(R.string.msg_no_battery_record); szRecordText = getString(R.string.msg_no_battery_record);
LogUtils.d(TAG, "【initRecordText】无电池记录数据"); LogUtils.d(TAG, "【initRecordText】无电池记录数据,显示空记录提示文本");
} else { } else {
// 根据配置切换显示格式 // 根据配置切换显示格式
if (mIsShowRecordWithEnter) { if (mIsShowRecordWithEnter) {
szRecordText = StringUtils.formatPCMListStringWithEnter(listBatteryInfo); szRecordText = StringUtils.formatPCMListStringWithEnter(listBatteryInfo);
LogUtils.d(TAG, "【initRecordText】使用带换行格式显示记录数量" + listBatteryInfo.size()); LogUtils.d(TAG, String.format("【initRecordText】使用带换行格式显示记录记录数量:%d", listBatteryInfo.size()));
} else { } else {
szRecordText = StringUtils.formatPCMListString(listBatteryInfo); szRecordText = StringUtils.formatPCMListString(listBatteryInfo);
LogUtils.d(TAG, "【initRecordText】使用无换行格式显示记录数量" + listBatteryInfo.size()); LogUtils.d(TAG, String.format("【initRecordText】使用无换行格式显示记录记录数量:%d", listBatteryInfo.size()));
} }
} }
mtvRecordText.setText(szRecordText); mtvRecordText.setText(szRecordText);
LogUtils.d(TAG, "【initRecordText】记录显示文本初始化完成"); LogUtils.d(TAG, "【initRecordText】记录显示文本刷新完成");
} }
// ======================== 事件回调方法 ========================= // ======================== 事件回调方法 =========================
@@ -155,7 +158,7 @@ public class ClearRecordActivity extends WinBoLLActivity implements IWinBoLLActi
public void onShowRecordWithEnter(View view) { public void onShowRecordWithEnter(View view) {
Switch swShowRecordWithEnter = (Switch) view; Switch swShowRecordWithEnter = (Switch) view;
mIsShowRecordWithEnter = swShowRecordWithEnter.isChecked(); mIsShowRecordWithEnter = swShowRecordWithEnter.isChecked();
LogUtils.d(TAG, "【onShowRecordWithEnter】记录显示格式切换带换行" + mIsShowRecordWithEnter); LogUtils.d(TAG, String.format("【onShowRecordWithEnter】记录显示格式切换带换行显示:%b", mIsShowRecordWithEnter));
// 刷新记录显示 // 刷新记录显示
initRecordText(); initRecordText();
} }