This cl is a merge of the following 36 cls: ag/19250259 Duplicate BatteryChartPreferenceController and BatteryChartView into new files for better diff review purpose ag/19279660 Use Mockito 4.6.1 API for BatteryChartPreferenceControllerV2Test ag/19267975 Add class BatteryLevelData used to parcel the battery timestamps and levels. It behaves as an interface between UI and data. ag/19289086 Refactor BatteryChartView X-axis labels. Instead of only timestamps, also support any string[] labels. ag/19238586 Add interpolation for the history data since last full charge loaded from database. ag/19331746 Return raw history map in function getHistoryMapSinceLastFullCharge. ag/19308838 In BatteryChartViewV2, use levels.length-1 to replace mTrapezoidCount. So the chartview could show any number of slots as the given levels length-1. ag/19332266 Add class BatteryDiffData used to parcel battery usage data ag/19331467 Refactor Battery Chart View State Controll ag/19358207 Add DataProcessor to process raw history map for UI. ag/19332276 Add battery chart view model. ag/19394744 Update trapezoid validation in battery chart view. ag/19379730 Support daily and hourly battery chartview. ag/19428426 Improve X axis labels in battery chart (1) ag/19446215 Improve X axis labels in battery chart (2) ag/19394745 Add the async task to compute diff usage data and load labels and icons. ag/19447624 Support showing app usage list for two battery charts ag/19500907 Updates battery usage messages from last 24hr to last full charge. (Part1: V2 files) ag/19505324 Update the selected period message in battery chart ag/19500905 Updates battery usage messages from last 24hr to last full charge. (Part2: non-V2 files) ag/19510363 Update usage data for EBS app usage list and App usage detail from 24 hours to last full charge. ag/19523184 Update usage data for EBS app usage list and App usage detail from 24 hours to last full charge. ag/19534864 Add margin between battery daily and hourly charts ag/19491093 Always do interpolation for battery level data in daily chart. ag/19565630 Avoid NullPointerException when batteryLevelData is null. ag/19561239 Fix b/241872474 Battery usage page will crash when selecting the last hour chart bar, going to app detail page, and going back ag/19565633 Fix b/241885070: Unexpected texts moving when going back to battery usage page ag/19534850 New way to draw battery chart axis labels ag/19561240 Switch Battery Usage Chart from V1 to V2. ag/19561338 Switch Battery Usage Chart from V1 to V2. ag/19600174 Fix b/242254055 Battery usage initial screen improvements (long data loading time) ag/19600284 Fix b/242252080: Add padding space on the top of the battery chart ag/19647338 Consider usage map valid even if [all][all] is null. ag/19634227 Use new content uri everytime to avoid cache ag/19600177 Fix b/242009481: Refine the battery usage chart timestamp label rule ag/19647337 Fix b/242809981 Charge battery to 100% when battery usage page opened, the chart will refresh, but the app list isn't refreshed in that case. Test: manual Bug: 239491373 Bug: 236101166 Bug: 236101687 Fix: 236101166 Change-Id: I7de8d9dcee14627da10752534991f1ec9f616020 Merged-In: I9142c0d4e00dea3771777ba9aedeab07b635fa1a
304 lines
11 KiB
Java
304 lines
11 KiB
Java
/*
|
|
* Copyright (C) 2017 The Android Open Source Project
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package com.android.settings.applications.appinfo;
|
|
|
|
import android.content.Context;
|
|
import android.content.pm.PackageInfo;
|
|
import android.content.pm.PackageManager;
|
|
import android.os.AsyncTask;
|
|
import android.os.BatteryUsageStats;
|
|
import android.os.Bundle;
|
|
import android.os.UidBatteryConsumer;
|
|
import android.os.UserHandle;
|
|
import android.os.UserManager;
|
|
import android.util.Log;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.VisibleForTesting;
|
|
import androidx.loader.app.LoaderManager;
|
|
import androidx.loader.content.Loader;
|
|
import androidx.preference.Preference;
|
|
import androidx.preference.PreferenceScreen;
|
|
|
|
import com.android.settings.R;
|
|
import com.android.settings.Utils;
|
|
import com.android.settings.core.BasePreferenceController;
|
|
import com.android.settings.fuelgauge.AdvancedPowerUsageDetail;
|
|
import com.android.settings.fuelgauge.BatteryUtils;
|
|
import com.android.settings.fuelgauge.PowerUsageFeatureProvider;
|
|
import com.android.settings.fuelgauge.batteryusage.BatteryChartPreferenceController;
|
|
import com.android.settings.fuelgauge.batteryusage.BatteryDiffEntry;
|
|
import com.android.settings.fuelgauge.batteryusage.BatteryEntry;
|
|
import com.android.settings.fuelgauge.batteryusage.BatteryUsageStatsLoader;
|
|
import com.android.settings.overlay.FeatureFactory;
|
|
import com.android.settingslib.applications.AppUtils;
|
|
import com.android.settingslib.core.lifecycle.Lifecycle;
|
|
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
|
import com.android.settingslib.core.lifecycle.events.OnPause;
|
|
import com.android.settingslib.core.lifecycle.events.OnResume;
|
|
|
|
import java.util.List;
|
|
|
|
public class AppBatteryPreferenceController extends BasePreferenceController
|
|
implements LifecycleObserver, OnResume, OnPause {
|
|
|
|
private static final String TAG = "AppBatteryPreferenceController";
|
|
private static final String KEY_BATTERY = "battery";
|
|
|
|
@VisibleForTesting
|
|
final BatteryUsageStatsLoaderCallbacks mBatteryUsageStatsLoaderCallbacks =
|
|
new BatteryUsageStatsLoaderCallbacks();
|
|
@VisibleForTesting
|
|
BatteryUtils mBatteryUtils;
|
|
@VisibleForTesting
|
|
BatteryUsageStats mBatteryUsageStats;
|
|
@VisibleForTesting
|
|
UidBatteryConsumer mUidBatteryConsumer;
|
|
@VisibleForTesting
|
|
BatteryDiffEntry mBatteryDiffEntry;
|
|
@VisibleForTesting
|
|
boolean mIsChartGraphEnabled;
|
|
@VisibleForTesting
|
|
final AppInfoDashboardFragment mParent;
|
|
|
|
private Preference mPreference;
|
|
private String mBatteryPercent;
|
|
private final String mPackageName;
|
|
private final int mUid;
|
|
private final int mUserId;
|
|
private boolean mBatteryUsageStatsLoaded = false;
|
|
private boolean mBatteryDiffEntriesLoaded = false;
|
|
|
|
public AppBatteryPreferenceController(Context context, AppInfoDashboardFragment parent,
|
|
String packageName, int uid, Lifecycle lifecycle) {
|
|
super(context, KEY_BATTERY);
|
|
mParent = parent;
|
|
mBatteryUtils = BatteryUtils.getInstance(mContext);
|
|
mPackageName = packageName;
|
|
mUid = uid;
|
|
mUserId = mContext.getUserId();
|
|
refreshFeatureFlag(mContext);
|
|
if (lifecycle != null) {
|
|
lifecycle.addObserver(this);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public int getAvailabilityStatus() {
|
|
return mContext.getResources().getBoolean(R.bool.config_show_app_info_settings_battery)
|
|
? AVAILABLE
|
|
: CONDITIONALLY_UNAVAILABLE;
|
|
}
|
|
|
|
@Override
|
|
public void displayPreference(PreferenceScreen screen) {
|
|
super.displayPreference(screen);
|
|
mPreference = screen.findPreference(getPreferenceKey());
|
|
mPreference.setEnabled(false);
|
|
if (!AppUtils.isAppInstalled(mParent.getAppEntry())) {
|
|
mPreference.setSummary("");
|
|
return;
|
|
}
|
|
|
|
loadBatteryDiffEntries();
|
|
}
|
|
|
|
@Override
|
|
public boolean handlePreferenceTreeClick(Preference preference) {
|
|
if (!KEY_BATTERY.equals(preference.getKey())) {
|
|
return false;
|
|
}
|
|
|
|
if (mBatteryDiffEntry != null) {
|
|
Log.i(TAG, "handlePreferenceTreeClick():\n" + mBatteryDiffEntry);
|
|
AdvancedPowerUsageDetail.startBatteryDetailPage(
|
|
mParent.getActivity(),
|
|
mParent,
|
|
mBatteryDiffEntry,
|
|
Utils.formatPercentage(
|
|
mBatteryDiffEntry.getPercentOfTotal(), /* round */ true),
|
|
/*isValidToShowSummary=*/ true,
|
|
/*slotInformation=*/ null);
|
|
return true;
|
|
}
|
|
|
|
if (isBatteryStatsAvailable()) {
|
|
final UserManager userManager =
|
|
(UserManager) mContext.getSystemService(Context.USER_SERVICE);
|
|
final BatteryEntry entry = new BatteryEntry(mContext, /* handler */null, userManager,
|
|
mUidBatteryConsumer, /* isHidden */ false,
|
|
mUidBatteryConsumer.getUid(), /* packages */ null, mPackageName);
|
|
Log.i(TAG, "Battery consumer available, launch : "
|
|
+ entry.getDefaultPackageName()
|
|
+ " | uid : "
|
|
+ entry.getUid()
|
|
+ " with BatteryEntry data");
|
|
AdvancedPowerUsageDetail.startBatteryDetailPage(mParent.getActivity(), mParent, entry,
|
|
mIsChartGraphEnabled ? Utils.formatPercentage(0) : mBatteryPercent,
|
|
!mIsChartGraphEnabled);
|
|
} else {
|
|
Log.i(TAG, "Launch : " + mPackageName + " with package name");
|
|
AdvancedPowerUsageDetail.startBatteryDetailPage(mParent.getActivity(), mParent,
|
|
mPackageName);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public void onResume() {
|
|
mParent.getLoaderManager().restartLoader(
|
|
AppInfoDashboardFragment.LOADER_BATTERY_USAGE_STATS, Bundle.EMPTY,
|
|
mBatteryUsageStatsLoaderCallbacks);
|
|
}
|
|
|
|
@Override
|
|
public void onPause() {
|
|
mParent.getLoaderManager().destroyLoader(
|
|
AppInfoDashboardFragment.LOADER_BATTERY_USAGE_STATS);
|
|
}
|
|
|
|
private void loadBatteryDiffEntries() {
|
|
new AsyncTask<Void, Void, BatteryDiffEntry>() {
|
|
@Override
|
|
protected BatteryDiffEntry doInBackground(Void... unused) {
|
|
if (mPackageName == null) {
|
|
return null;
|
|
}
|
|
final BatteryDiffEntry entry =
|
|
BatteryChartPreferenceController.getAppBatteryUsageData(
|
|
mContext, mPackageName, mUserId);
|
|
Log.d(TAG, "loadBatteryDiffEntries():\n" + entry);
|
|
return entry;
|
|
}
|
|
|
|
@Override
|
|
protected void onPostExecute(BatteryDiffEntry batteryDiffEntry) {
|
|
mBatteryDiffEntry = batteryDiffEntry;
|
|
updateBatteryWithDiffEntry();
|
|
}
|
|
}.execute();
|
|
}
|
|
|
|
@VisibleForTesting
|
|
void updateBatteryWithDiffEntry() {
|
|
if (mIsChartGraphEnabled) {
|
|
if (mBatteryDiffEntry != null && mBatteryDiffEntry.mConsumePower > 0) {
|
|
mBatteryPercent = Utils.formatPercentage(
|
|
mBatteryDiffEntry.getPercentOfTotal(), /* round */ true);
|
|
mPreference.setSummary(mContext.getString(
|
|
R.string.battery_summary, mBatteryPercent));
|
|
} else {
|
|
mPreference.setSummary(
|
|
mContext.getString(R.string.no_battery_summary));
|
|
}
|
|
}
|
|
|
|
mBatteryDiffEntriesLoaded = true;
|
|
mPreference.setEnabled(mBatteryUsageStatsLoaded);
|
|
}
|
|
|
|
private void onLoadFinished() {
|
|
if (mBatteryUsageStats == null) {
|
|
return;
|
|
}
|
|
|
|
final PackageInfo packageInfo = mParent.getPackageInfo();
|
|
if (packageInfo != null) {
|
|
mUidBatteryConsumer = findTargetUidBatteryConsumer(mBatteryUsageStats,
|
|
packageInfo.applicationInfo.uid);
|
|
if (mParent.getActivity() != null) {
|
|
updateBattery();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void refreshFeatureFlag(Context context) {
|
|
if (isWorkProfile(context)) {
|
|
try {
|
|
context = context.createPackageContextAsUser(
|
|
context.getPackageName(), 0, UserHandle.OWNER);
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
Log.e(TAG, "context.createPackageContextAsUser() fail: " + e);
|
|
}
|
|
}
|
|
|
|
final PowerUsageFeatureProvider powerUsageFeatureProvider =
|
|
FeatureFactory.getFactory(context).getPowerUsageFeatureProvider(context);
|
|
mIsChartGraphEnabled = powerUsageFeatureProvider.isChartGraphEnabled(context);
|
|
}
|
|
|
|
private boolean isWorkProfile(Context context) {
|
|
final UserManager userManager = context.getSystemService(UserManager.class);
|
|
return userManager.isManagedProfile() && !userManager.isSystemUser();
|
|
}
|
|
|
|
@VisibleForTesting
|
|
void updateBattery() {
|
|
mBatteryUsageStatsLoaded = true;
|
|
mPreference.setEnabled(mBatteryDiffEntriesLoaded);
|
|
if (mIsChartGraphEnabled) {
|
|
return;
|
|
}
|
|
if (isBatteryStatsAvailable()) {
|
|
final int percentOfMax = (int) mBatteryUtils.calculateBatteryPercent(
|
|
mUidBatteryConsumer.getConsumedPower(), mBatteryUsageStats.getConsumedPower(),
|
|
mBatteryUsageStats.getDischargePercentage());
|
|
mBatteryPercent = Utils.formatPercentage(percentOfMax);
|
|
mPreference.setSummary(mContext.getString(R.string.battery_summary, mBatteryPercent));
|
|
} else {
|
|
mPreference.setSummary(mContext.getString(R.string.no_battery_summary));
|
|
}
|
|
}
|
|
|
|
@VisibleForTesting
|
|
boolean isBatteryStatsAvailable() {
|
|
return mUidBatteryConsumer != null;
|
|
}
|
|
|
|
@VisibleForTesting
|
|
UidBatteryConsumer findTargetUidBatteryConsumer(BatteryUsageStats batteryUsageStats, int uid) {
|
|
final List<UidBatteryConsumer> usageList = batteryUsageStats.getUidBatteryConsumers();
|
|
for (int i = 0, size = usageList.size(); i < size; i++) {
|
|
final UidBatteryConsumer consumer = usageList.get(i);
|
|
if (consumer.getUid() == uid) {
|
|
return consumer;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private class BatteryUsageStatsLoaderCallbacks
|
|
implements LoaderManager.LoaderCallbacks<BatteryUsageStats> {
|
|
@Override
|
|
@NonNull
|
|
public Loader<BatteryUsageStats> onCreateLoader(int id, Bundle args) {
|
|
return new BatteryUsageStatsLoader(mContext, /* includeBatteryHistory */ false);
|
|
}
|
|
|
|
@Override
|
|
public void onLoadFinished(Loader<BatteryUsageStats> loader,
|
|
BatteryUsageStats batteryUsageStats) {
|
|
mBatteryUsageStats = batteryUsageStats;
|
|
AppBatteryPreferenceController.this.onLoadFinished();
|
|
}
|
|
|
|
@Override
|
|
public void onLoaderReset(Loader<BatteryUsageStats> loader) {
|
|
}
|
|
}
|
|
}
|