Search in sources :

Example 21 with BatterySipper

use of com.android.internal.os.BatterySipper in project android_packages_apps_Settings by omnirom.

the class BatteryUtils method smearScreenBatterySipper.

/**
 * Smear the screen on power usage among {@code sippers}, based on ratio of foreground activity
 * time.
 */
@VisibleForTesting
void smearScreenBatterySipper(List<BatterySipper> sippers, BatterySipper screenSipper) {
    long totalActivityTimeMs = 0;
    final SparseLongArray activityTimeArray = new SparseLongArray();
    for (int i = 0, size = sippers.size(); i < size; i++) {
        final BatteryStats.Uid uid = sippers.get(i).uidObj;
        if (uid != null) {
            final long timeMs = getProcessTimeMs(StatusType.SCREEN_USAGE, uid, BatteryStats.STATS_SINCE_CHARGED);
            activityTimeArray.put(uid.getUid(), timeMs);
            totalActivityTimeMs += timeMs;
        }
    }
    if (totalActivityTimeMs >= 10 * DateUtils.MINUTE_IN_MILLIS) {
        final double screenPowerMah = screenSipper.totalPowerMah;
        for (int i = 0, size = sippers.size(); i < size; i++) {
            final BatterySipper sipper = sippers.get(i);
            sipper.totalPowerMah += screenPowerMah * activityTimeArray.get(sipper.getUid(), 0) / totalActivityTimeMs;
        }
    }
}
Also used : BatterySipper(com.android.internal.os.BatterySipper) SparseLongArray(android.util.SparseLongArray) BatteryStats(android.os.BatteryStats) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 22 with BatterySipper

use of com.android.internal.os.BatterySipper in project android_packages_apps_Settings by omnirom.

the class WakeupAlarmAnomalyDetector method detectAnomalies.

@Override
public List<Anomaly> detectAnomalies(BatteryStatsHelper batteryStatsHelper, String targetPackageName) {
    final List<BatterySipper> batterySippers = batteryStatsHelper.getUsageList();
    final List<Anomaly> anomalies = new ArrayList<>();
    final double totalRunningHours = mBatteryUtils.calculateRunningTimeBasedOnStatsType(batteryStatsHelper, BatteryStats.STATS_SINCE_CHARGED) / (double) DateUtils.HOUR_IN_MILLIS;
    final int targetUid = mBatteryUtils.getPackageUid(targetPackageName);
    if (totalRunningHours >= 1) {
        for (int i = 0, size = batterySippers.size(); i < size; i++) {
            final BatterySipper sipper = batterySippers.get(i);
            final BatteryStats.Uid uid = sipper.uidObj;
            if (uid == null || mBatteryUtils.shouldHideSipper(sipper) || (targetUid != BatteryUtils.UID_NULL && targetUid != uid.getUid())) {
                continue;
            }
            final int wakeupAlarmCount = (int) (getWakeupAlarmCountFromUid(uid) / totalRunningHours);
            if (wakeupAlarmCount > mWakeupAlarmThreshold) {
                final String packageName = mBatteryUtils.getPackageName(uid.getUid());
                final CharSequence displayName = Utils.getApplicationLabel(mContext, packageName);
                final int targetSdkVersion = mBatteryUtils.getTargetSdkVersion(packageName);
                Anomaly anomaly = new Anomaly.Builder().setUid(uid.getUid()).setType(Anomaly.AnomalyType.WAKEUP_ALARM).setDisplayName(displayName).setPackageName(packageName).setTargetSdkVersion(targetSdkVersion).setBackgroundRestrictionEnabled(mBatteryUtils.isBackgroundRestrictionEnabled(targetSdkVersion, uid.getUid(), packageName)).setWakeupAlarmCount(wakeupAlarmCount).build();
                if (mAnomalyUtils.getAnomalyAction(anomaly).isActionActive(anomaly)) {
                    anomalies.add(anomaly);
                }
            }
        }
    }
    return anomalies;
}
Also used : BatterySipper(com.android.internal.os.BatterySipper) ArrayList(java.util.ArrayList) Anomaly(com.android.settings.fuelgauge.anomaly.Anomaly) BatteryStats(android.os.BatteryStats)

Example 23 with BatterySipper

use of com.android.internal.os.BatterySipper in project android_packages_apps_Settings by crdroidandroid.

the class PowerUsageAdvancedTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    mShadowContext = spy(RuntimeEnvironment.application);
    mPowerUsageAdvanced = spy(new PowerUsageAdvanced());
    List<BatterySipper> batterySippers = new ArrayList<>();
    batterySippers.add(new BatterySipper(DrainType.APP, new FakeUid(FAKE_UID_1), TYPE_APP_USAGE));
    batterySippers.add(new BatterySipper(DrainType.APP, new FakeUid(FAKE_UID_2), TYPE_APP_USAGE));
    batterySippers.add(new BatterySipper(DrainType.BLUETOOTH, new FakeUid(FAKE_UID_1), TYPE_BLUETOOTH_USAGE));
    batterySippers.add(new BatterySipper(DrainType.WIFI, new FakeUid(FAKE_UID_1), TYPE_WIFI_USAGE));
    mDischargingBatteryIntent = BatteryTestUtils.getDischargingIntent();
    doReturn(mDischargingBatteryIntent).when(mShadowContext).registerReceiver(any(), any());
    when(mBatteryStatsHelper.getStats().getDischargeAmount(anyInt())).thenReturn(DISCHARGE_AMOUNT);
    when(mBatteryStatsHelper.getUsageList()).thenReturn(batterySippers);
    when(mBatteryStatsHelper.getTotalPower()).thenReturn(TOTAL_USAGE);
    when(mPowerUsageAdvanced.getContext()).thenReturn(mShadowContext);
    doReturn(STUB_STRING).when(mPowerUsageAdvanced).getString(anyInt(), any(), any());
    doReturn(STUB_STRING).when(mPowerUsageAdvanced).getString(anyInt(), any());
    doReturn(mShadowContext.getText(R.string.battery_used_for)).when(mPowerUsageAdvanced).getText(R.string.battery_used_for);
    mPowerUsageAdvanced.setPackageManager(mPackageManager);
    mPowerUsageAdvanced.setPowerUsageFeatureProvider(mPowerUsageFeatureProvider);
    mPowerUsageAdvanced.setUserManager(mUserManager);
    mPowerUsageAdvanced.setBatteryUtils(BatteryUtils.getInstance(mShadowContext));
    when(mShadowContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(mConnectivityManager);
    mPowerUsageData = new PowerUsageData(UsageType.USER);
    mMaxBatterySipper.totalPowerMah = TYPE_BLUETOOTH_USAGE;
    mMaxBatterySipper.drainType = DrainType.BLUETOOTH;
    mNormalBatterySipper.drainType = DrainType.SCREEN;
}
Also used : BatterySipper(com.android.internal.os.BatterySipper) PowerUsageData(com.android.settings.fuelgauge.PowerUsageAdvanced.PowerUsageData) ArrayList(java.util.ArrayList) Before(org.junit.Before)

Example 24 with BatterySipper

use of com.android.internal.os.BatterySipper in project android_packages_apps_Settings by crdroidandroid.

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;
}
Also used : SparseArray(android.util.SparseArray) BatterySipper(com.android.internal.os.BatterySipper) ArrayList(java.util.ArrayList)

Example 25 with BatterySipper

use of com.android.internal.os.BatterySipper in project android_packages_apps_Settings by crdroidandroid.

the class PowerUsageSummary method getFakeStats.

private static List<BatterySipper> getFakeStats() {
    ArrayList<BatterySipper> stats = new ArrayList<>();
    float use = 5;
    for (DrainType type : DrainType.values()) {
        if (type == DrainType.APP) {
            continue;
        }
        stats.add(new BatterySipper(type, null, use));
        use += 5;
    }
    for (int i = 0; i < 100; i++) {
        stats.add(new BatterySipper(DrainType.APP, new FakeUid(Process.FIRST_APPLICATION_UID + i), use));
    }
    stats.add(new BatterySipper(DrainType.APP, new FakeUid(0), use));
    // Simulate dex2oat process.
    BatterySipper sipper = new BatterySipper(DrainType.APP, new FakeUid(UserHandle.getSharedAppGid(Process.FIRST_APPLICATION_UID)), 10.0f);
    sipper.packageWithHighestDrain = "dex2oat";
    stats.add(sipper);
    sipper = new BatterySipper(DrainType.APP, new FakeUid(UserHandle.getSharedAppGid(Process.FIRST_APPLICATION_UID + 1)), 10.0f);
    sipper.packageWithHighestDrain = "dex2oat";
    stats.add(sipper);
    sipper = new BatterySipper(DrainType.APP, new FakeUid(UserHandle.getSharedAppGid(Process.LOG_UID)), 9.0f);
    stats.add(sipper);
    return stats;
}
Also used : DrainType(com.android.internal.os.BatterySipper.DrainType) BatterySipper(com.android.internal.os.BatterySipper) ArrayList(java.util.ArrayList)

Aggregations

BatterySipper (com.android.internal.os.BatterySipper)148 ArrayList (java.util.ArrayList)60 BatteryStats (android.os.BatteryStats)48 VisibleForTesting (android.support.annotation.VisibleForTesting)30 Test (org.junit.Test)29 Anomaly (com.android.settings.fuelgauge.anomaly.Anomaly)18 HashMap (java.util.HashMap)16 UserHandle (android.os.UserHandle)15 ArrayMap (android.util.ArrayMap)10 BatteryStatsHelper (com.android.internal.os.BatteryStatsHelper)10 Map (java.util.Map)10 Drawable (android.graphics.drawable.Drawable)8 SparseArray (android.util.SparseArray)8 DrainType (com.android.internal.os.BatterySipper.DrainType)8 PowerProfile (com.android.internal.os.PowerProfile)8 Bundle (android.os.Bundle)7 SparseLongArray (android.util.SparseLongArray)7 Context (android.content.Context)6 TypedArray (android.content.res.TypedArray)6 PowerUsageData (com.android.settings.fuelgauge.PowerUsageAdvanced.PowerUsageData)6