Merge "Move bunch of battery methods to BatteryUtils.java" into oc-dev

This commit is contained in:
Lei Yu
2017-04-12 19:39:08 +00:00
committed by Android (Google) Code Review
5 changed files with 228 additions and 145 deletions

View File

@@ -91,6 +91,7 @@ import com.android.settings.datausage.DataUsageList;
import com.android.settings.datausage.DataUsageSummary;
import com.android.settings.fuelgauge.AdvancedPowerUsageDetail;
import com.android.settings.fuelgauge.BatteryEntry;
import com.android.settings.fuelgauge.BatteryUtils;
import com.android.settings.notification.AppNotificationSettings;
import com.android.settings.notification.NotificationBackend;
import com.android.settings.notification.NotificationBackend.AppRow;
@@ -197,6 +198,7 @@ public class InstalledAppDetails extends AppInfoBase
private AppStorageStats mLastResult;
private String mBatteryPercent;
private BatteryUtils mBatteryUtils;
private boolean handleDisableable(Button button) {
boolean disableable = false;
@@ -357,6 +359,7 @@ public class InstalledAppDetails extends AppInfoBase
removePreference(KEY_DATA);
}
mBatteryHelper = new BatteryStatsHelper(getActivity(), true);
mBatteryUtils = BatteryUtils.getInstance(getContext());
}
@Override
@@ -685,10 +688,14 @@ public class InstalledAppDetails extends AppInfoBase
private void updateBattery() {
if (mSipper != null) {
mBatteryPreference.setEnabled(true);
int dischargeAmount = mBatteryHelper.getStats().getDischargeAmount(
final int dischargeAmount = mBatteryHelper.getStats().getDischargeAmount(
BatteryStats.STATS_SINCE_CHARGED);
final int percentOfMax = (int) ((mSipper.totalPowerMah)
/ mBatteryHelper.getTotalPower() * dischargeAmount + .5f);
final List<BatterySipper> usageList = new ArrayList<>(mBatteryHelper.getUsageList());
final double hiddenAmount = mBatteryUtils.removeHiddenBatterySippers(usageList);
final int percentOfMax = (int) mBatteryUtils.calculateBatteryPercent(
mSipper.totalPowerMah, mBatteryHelper.getTotalPower(), hiddenAmount,
dischargeAmount);
mBatteryPercent = Utils.formatPercentage(percentOfMax);
mBatteryPreference.setSummary(getString(R.string.battery_summary, mBatteryPercent));
} else {

View File

@@ -22,10 +22,15 @@ import android.os.BatteryStats;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.text.format.DateUtils;
import android.util.Log;
import com.android.internal.os.BatterySipper;
import com.android.settings.overlay.FeatureFactory;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;
/**
* Utils for battery operation
@@ -43,9 +48,14 @@ public class BatteryUtils {
}
private static final String TAG = "BatteryUtils";
private static final int MIN_POWER_THRESHOLD_MILLI_AMP = 5;
private static final int SECONDS_IN_HOUR = 60 * 60;
private static BatteryUtils sInstance;
private PackageManager mPackageManager;
@VisibleForTesting
PowerUsageFeatureProvider mPowerUsageFeatureProvider;
public static BatteryUtils getInstance(Context context) {
if (sInstance == null || sInstance.isDataCorrupted()) {
@@ -56,6 +66,8 @@ public class BatteryUtils {
private BatteryUtils(Context context) {
mPackageManager = context.getPackageManager();
mPowerUsageFeatureProvider = FeatureFactory.getFactory(
context).getPowerUsageFeatureProvider(context);
}
public long getProcessTimeMs(@StatusType int type, @Nullable BatteryStats.Uid uid,
@@ -105,6 +117,67 @@ public class BatteryUtils {
return convertUsToMs(timeUs);
}
/**
* Remove the {@link BatterySipper} that we should hide.
*
* @param sippers sipper list that need to check and remove
* @return the total power of the hidden items of {@link BatterySipper}
*/
public double removeHiddenBatterySippers(List<BatterySipper> sippers) {
double totalPowerMah = 0;
for (int i = sippers.size() - 1; i >= 0; i--) {
final BatterySipper sipper = sippers.get(i);
if (shouldHideSipper(sipper)) {
sippers.remove(i);
if (sipper.drainType != BatterySipper.DrainType.OVERCOUNTED
&& sipper.drainType != BatterySipper.DrainType.UNACCOUNTED) {
// Don't add it if it is overcounted or unaccounted
totalPowerMah += sipper.totalPowerMah;
}
}
}
return totalPowerMah;
}
/**
* Check whether we should hide the battery sipper.
*/
public boolean shouldHideSipper(BatterySipper sipper) {
final BatterySipper.DrainType drainType = sipper.drainType;
return drainType == BatterySipper.DrainType.IDLE
|| drainType == BatterySipper.DrainType.CELL
|| drainType == BatterySipper.DrainType.WIFI
|| drainType == BatterySipper.DrainType.SCREEN
|| drainType == BatterySipper.DrainType.BLUETOOTH
|| drainType == BatterySipper.DrainType.UNACCOUNTED
|| drainType == BatterySipper.DrainType.OVERCOUNTED
|| (sipper.totalPowerMah * SECONDS_IN_HOUR) < MIN_POWER_THRESHOLD_MILLI_AMP
|| mPowerUsageFeatureProvider.isTypeService(sipper)
|| mPowerUsageFeatureProvider.isTypeSystem(sipper);
}
/**
* Calculate the power usage percentage for an app
*
* @param powerUsageMah power used by the app
* @param totalPowerMah total power used in the system
* @param hiddenPowerMah power used by no-actionable app that we want to hide, i.e. Screen,
* Android OS.
* @param dischargeAmount The discharge amount calculated by {@link BatteryStats}
* @return A percentage value scaled by {@paramref dischargeAmount}
* @see BatteryStats#getDischargeAmount(int)
*/
public double calculateBatteryPercent(double powerUsageMah, double totalPowerMah,
double hiddenPowerMah, int dischargeAmount) {
if (totalPowerMah == 0) {
return 0;
}
return (powerUsageMah / (totalPowerMah - hiddenPowerMah)) * dischargeAmount;
}
private long convertUsToMs(long timeUs) {
return timeUs / 1000;
}

View File

@@ -80,11 +80,8 @@ public class PowerUsageSummary extends PowerUsageBase {
private static final boolean USE_FAKE_DATA = false;
private static final String KEY_APP_LIST = "app_list";
private static final String KEY_BATTERY_HEADER = "battery_header";
private static final int MIN_POWER_THRESHOLD_MILLI_AMP = 5;
private static final int MAX_ITEMS_TO_LIST = USE_FAKE_DATA ? 30 : 10;
private static final int MIN_AVERAGE_POWER_THRESHOLD_MILLI_AMP = 10;
private static final int SECONDS_IN_HOUR = 60 * 60;
private static final String KEY_SCREEN_USAGE = "screen_usage";
private static final String KEY_TIME_SINCE_LAST_FULL_CHARGE = "last_full_charge";
@@ -432,20 +429,16 @@ public class PowerUsageSummary extends PowerUsageBase {
final List<BatterySipper> usageList = getCoalescedUsageList(
USE_FAKE_DATA ? getFakeStats() : mStatsHelper.getUsageList());
double hiddenPowerMah = mShowAllApps ? 0 : removeHiddenBatterySippers(usageList);
double hiddenPowerMah = mShowAllApps ? 0 :
mBatteryUtils.removeHiddenBatterySippers(usageList);
final int numSippers = usageList.size();
for (int i = 0; i < numSippers; i++) {
final BatterySipper sipper = usageList.get(i);
// Deduct the power of hidden items from total power, which is used to
// calculate percentOfTotal
double totalPower = USE_FAKE_DATA ?
4000 : mStatsHelper.getTotalPower() - hiddenPowerMah;
double totalPower = USE_FAKE_DATA ? 4000 : mStatsHelper.getTotalPower();
// With deduction in totalPower, percentOfTotal is higher because it adds the part
// used in screen, system, etc
final double percentOfTotal = totalPower == 0 ? 0 :
((sipper.totalPowerMah / totalPower) * dischargeAmount);
final double percentOfTotal = mBatteryUtils.calculateBatteryPercent(
sipper.totalPowerMah, totalPower, hiddenPowerMah, dischargeAmount);
if (((int) (percentOfTotal + .5)) < 1) {
continue;
@@ -594,22 +587,6 @@ public class PowerUsageSummary extends PowerUsageBase {
}
}
@VisibleForTesting
boolean shouldHideSipper(BatterySipper sipper) {
final DrainType drainType = sipper.drainType;
return drainType == DrainType.IDLE
|| drainType == DrainType.CELL
|| drainType == DrainType.WIFI
|| drainType == DrainType.SCREEN
|| drainType == DrainType.BLUETOOTH
|| drainType == DrainType.UNACCOUNTED
|| drainType == DrainType.OVERCOUNTED
|| (sipper.totalPowerMah * SECONDS_IN_HOUR) < MIN_POWER_THRESHOLD_MILLI_AMP
|| mPowerFeatureProvider.isTypeService(sipper)
|| mPowerFeatureProvider.isTypeSystem(sipper);
}
@VisibleForTesting
String extractKeyFromSipper(BatterySipper sipper) {
if (sipper.uidObj != null) {
@@ -624,24 +601,6 @@ public class PowerUsageSummary extends PowerUsageBase {
}
}
@VisibleForTesting
double removeHiddenBatterySippers(List<BatterySipper> sippers) {
double totalPowerMah = 0;
for (int i = sippers.size() - 1; i >= 0; i--) {
final BatterySipper sipper = sippers.get(i);
if (shouldHideSipper(sipper)) {
sippers.remove(i);
if (sipper.drainType != DrainType.OVERCOUNTED
&& sipper.drainType != DrainType.UNACCOUNTED) {
// Don't add it if it is overcounted or unaccounted
totalPowerMah += sipper.totalPowerMah;
}
}
}
return totalPowerMah;
}
@VisibleForTesting
void setBatteryLayoutPreference(LayoutPreference layoutPreference) {
mBatteryLayoutPref = layoutPreference;