use of com.android.internal.os.BatterySipper in project android_packages_apps_Settings by LineageOS.
the class BatteryUtilsTest method testSmearScreenBatterySipper.
@Test
public void testSmearScreenBatterySipper() {
final BatterySipper sipperNull = createTestSmearBatterySipper(TIME_FOREGROUND_ZERO, BATTERY_APP_USAGE, 0, /* uid */
true);
final BatterySipper sipperBg = createTestSmearBatterySipper(TIME_FOREGROUND_ZERO, BATTERY_APP_USAGE, 1, /* uid */
false);
final BatterySipper sipperFg = createTestSmearBatterySipper(TIME_FOREGROUND, BATTERY_APP_USAGE, 2, /* uid */
false);
final BatterySipper sipperFg2 = createTestSmearBatterySipper(TIME_FOREGROUND, BATTERY_APP_USAGE, 3, /* uid */
false);
final List<BatterySipper> sippers = new ArrayList<>();
sippers.add(sipperNull);
sippers.add(sipperBg);
sippers.add(sipperFg);
sippers.add(sipperFg2);
mBatteryUtils.smearScreenBatterySipper(sippers, mScreenBatterySipper);
assertThat(sipperNull.totalPowerMah).isWithin(PRECISION).of(BATTERY_APP_USAGE);
assertThat(sipperBg.totalPowerMah).isWithin(PRECISION).of(BATTERY_APP_USAGE);
assertThat(sipperFg.totalPowerMah).isWithin(PRECISION).of(BATTERY_APP_USAGE + BATTERY_SCREEN_USAGE / 2);
assertThat(sipperFg2.totalPowerMah).isWithin(PRECISION).of(BATTERY_APP_USAGE + BATTERY_SCREEN_USAGE / 2);
}
use of com.android.internal.os.BatterySipper in project android_packages_apps_Settings by LineageOS.
the class PowerUsageSummaryTest method testFindBatterySipperByType_findTypeApp.
@Test
public void testFindBatterySipperByType_findTypeApp() {
BatterySipper sipper = mFragment.findBatterySipperByType(mUsageList, BatterySipper.DrainType.APP);
assertThat(sipper).isSameAs(mNormalBatterySipper);
}
use of com.android.internal.os.BatterySipper in project android_packages_apps_Settings by omnirom.
the class PowerUsageSummary method getCoalescedUsageList.
/**
* We want to coalesce some UIDs. For example, dex2oat runs under a shared gid that
* exists for all users of the same app. We detect this case and merge the power use
* for dex2oat to the device OWNER's use of the app.
*
* @return A sorted list of apps using power.
*/
private List<BatterySipper> getCoalescedUsageList(final List<BatterySipper> sippers) {
final SparseArray<BatterySipper> uidList = new SparseArray<>();
final ArrayList<BatterySipper> results = new ArrayList<>();
final int numSippers = sippers.size();
for (int i = 0; i < numSippers; i++) {
BatterySipper sipper = sippers.get(i);
if (sipper.getUid() > 0) {
int realUid = sipper.getUid();
// actual app UID.
if (isSharedGid(sipper.getUid())) {
realUid = UserHandle.getUid(UserHandle.USER_SYSTEM, UserHandle.getAppIdFromSharedAppGid(sipper.getUid()));
}
// Check if this UID is a system UID (mediaserver, logd, nfc, drm, etc).
if (isSystemUid(realUid) && !"mediaserver".equals(sipper.packageWithHighestDrain)) {
// Use the system UID for all UIDs running in their own sandbox that
// are not apps. We exclude mediaserver because we already are expected to
// report that as a separate item.
realUid = Process.SYSTEM_UID;
}
if (realUid != sipper.getUid()) {
// Replace the BatterySipper with a new one with the real UID set.
BatterySipper newSipper = new BatterySipper(sipper.drainType, new FakeUid(realUid), 0.0);
newSipper.add(sipper);
newSipper.packageWithHighestDrain = sipper.packageWithHighestDrain;
newSipper.mPackages = sipper.mPackages;
sipper = newSipper;
}
int index = uidList.indexOfKey(realUid);
if (index < 0) {
// New entry.
uidList.put(realUid, sipper);
} else {
// Combine BatterySippers if we already have one with this UID.
final BatterySipper existingSipper = uidList.valueAt(index);
existingSipper.add(sipper);
if (existingSipper.packageWithHighestDrain == null && sipper.packageWithHighestDrain != null) {
existingSipper.packageWithHighestDrain = sipper.packageWithHighestDrain;
}
final int existingPackageLen = existingSipper.mPackages != null ? existingSipper.mPackages.length : 0;
final int newPackageLen = sipper.mPackages != null ? sipper.mPackages.length : 0;
if (newPackageLen > 0) {
String[] newPackages = new String[existingPackageLen + newPackageLen];
if (existingPackageLen > 0) {
System.arraycopy(existingSipper.mPackages, 0, newPackages, 0, existingPackageLen);
}
System.arraycopy(sipper.mPackages, 0, newPackages, existingPackageLen, newPackageLen);
existingSipper.mPackages = newPackages;
}
}
} else {
results.add(sipper);
}
}
final int numUidSippers = uidList.size();
for (int i = 0; i < numUidSippers; i++) {
results.add(uidList.valueAt(i));
}
// The sort order must have changed, so re-sort based on total power use.
mBatteryUtils.sortUsageList(results);
return results;
}
use of com.android.internal.os.BatterySipper in project android_packages_apps_Settings by omnirom.
the class PowerUsageAdvanced method parsePowerUsageData.
@VisibleForTesting
List<PowerUsageData> parsePowerUsageData(BatteryStatsHelper statusHelper) {
final List<BatterySipper> batterySippers = statusHelper.getUsageList();
final Map<Integer, PowerUsageData> batteryDataMap = new HashMap<>();
for (@UsageType final Integer type : mUsageTypes) {
batteryDataMap.put(type, new PowerUsageData(type));
}
// Accumulate power usage based on usage type
for (final BatterySipper sipper : batterySippers) {
sipper.mPackages = mPackageManager.getPackagesForUid(sipper.getUid());
final PowerUsageData usageData = batteryDataMap.get(extractUsageType(sipper));
usageData.totalPowerMah += sipper.totalPowerMah;
if (sipper.drainType == DrainType.APP && sipper.usageTimeMs != 0) {
sipper.usageTimeMs = mBatteryUtils.getProcessTimeMs(BatteryUtils.StatusType.FOREGROUND, sipper.uidObj, STATUS_TYPE);
}
usageData.totalUsageTimeMs += sipper.usageTimeMs;
if (shouldShowBatterySipper(sipper)) {
usageData.usageList.add(sipper);
}
}
final List<PowerUsageData> batteryDataList = new ArrayList<>(batteryDataMap.values());
final int dischargeAmount = statusHelper.getStats().getDischargeAmount(STATUS_TYPE);
final double totalPower = statusHelper.getTotalPower();
final double hiddenPower = calculateHiddenPower(batteryDataList);
for (final PowerUsageData usageData : batteryDataList) {
usageData.percentage = mBatteryUtils.calculateBatteryPercent(usageData.totalPowerMah, totalPower, hiddenPower, dischargeAmount);
updateUsageDataSummary(usageData, totalPower, dischargeAmount);
}
Collections.sort(batteryDataList);
mBatteryDataMap = batteryDataMap;
return batteryDataList;
}
use of com.android.internal.os.BatterySipper in project android_packages_apps_Settings by omnirom.
the class PowerUsageAdvanced method updateUsageDataSummary.
@VisibleForTesting
void updateUsageDataSummary(PowerUsageData usageData, double totalPower, int dischargeAmount) {
if (shouldHideSummary(usageData)) {
return;
}
if (usageData.usageList.size() <= 1) {
CharSequence timeSequence = Utils.formatElapsedTime(getContext(), usageData.totalUsageTimeMs, false);
usageData.summary = usageData.usageType == UsageType.IDLE ? timeSequence : TextUtils.expandTemplate(getText(R.string.battery_used_for), timeSequence);
} else {
BatterySipper sipper = findBatterySipperWithMaxBatteryUsage(usageData.usageList);
BatteryEntry batteryEntry = new BatteryEntry(getContext(), mHandler, mUserManager, sipper);
final double percentage = (sipper.totalPowerMah / totalPower) * dischargeAmount;
usageData.summary = getString(R.string.battery_used_by, Utils.formatPercentage(percentage, true), batteryEntry.name);
}
}
Aggregations