Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1383532e authored by Dmitri Plotnikov's avatar Dmitri Plotnikov Committed by Android (Google) Code Review
Browse files

Merge "Remove BatteryStatsHelper and BatterySipper"

parents 81a193fc 58fff848
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -8591,7 +8591,10 @@ public abstract class BatteryStats implements Parcelable {
        proto.end(sToken);
    }

    private static boolean checkWifiOnly(Context context) {
    /**
     * Returns true if the device does not have data-capable telephony.
     */
    public static boolean checkWifiOnly(Context context) {
        final TelephonyManager tm = context.getSystemService(TelephonyManager.class);
        if (tm == null) {
            return false;
+0 −27
Original line number Diff line number Diff line
@@ -22,10 +22,6 @@ import android.os.BatteryConsumer;
import android.os.BatteryStats;
import android.os.BatteryUsageStats;
import android.os.BatteryUsageStatsQuery;
import android.os.UserHandle;
import android.util.SparseArray;

import java.util.List;

/**
 * Estimates power consumed by the ambient display
@@ -67,29 +63,6 @@ public class AmbientDisplayPowerCalculator extends PowerCalculator {
                        powerMah, powerModel);
    }

    /**
     * Ambient display power is the additional power the screen takes while in ambient display/
     * screen doze/ always-on display (interchangeable terms) mode. Ambient display power should
     * be hidden {@link BatteryStatsHelper#shouldHideSipper(BatterySipper)}, but should not be
     * included in smearing {@link BatteryStatsHelper#removeHiddenBatterySippers(List)}.
     */
    @Override
    public void calculate(List<BatterySipper> sippers, BatteryStats batteryStats,
            long rawRealtimeUs, long rawUptimeUs, int statsType, SparseArray<UserHandle> asUsers) {
        final long measuredEnergyUC = batteryStats.getScreenDozeMeasuredBatteryConsumptionUC();
        final long durationMs = calculateDuration(batteryStats, rawRealtimeUs, statsType);
        final int powerModel = getPowerModel(measuredEnergyUC);
        final double powerMah = calculateTotalPower(powerModel, batteryStats, rawRealtimeUs,
                measuredEnergyUC);
        if (powerMah > 0) {
            BatterySipper bs = new BatterySipper(BatterySipper.DrainType.AMBIENT_DISPLAY, null, 0);
            bs.usagePowerMah = powerMah;
            bs.usageTimeMs = durationMs;
            bs.sumPower();
            sippers.add(bs);
        }
    }

    private long calculateDuration(BatteryStats batteryStats, long rawRealtimeUs, int statsType) {
        return batteryStats.getScreenDozeTime(rawRealtimeUs, statsType) / 1000;
    }
+0 −10
Original line number Diff line number Diff line
@@ -20,10 +20,6 @@ import android.os.BatteryConsumer;
import android.os.BatteryStats;
import android.os.BatteryUsageStats;
import android.os.BatteryUsageStatsQuery;
import android.os.UserHandle;
import android.util.SparseArray;

import java.util.List;

/**
 * Estimates the battery discharge amounts.
@@ -81,10 +77,4 @@ public class BatteryChargeCalculator extends PowerCalculator {
                BatteryUsageStats.AGGREGATE_BATTERY_CONSUMER_SCOPE_DEVICE)
                .setConsumedPower(dischargeMah);
    }

    @Override
    public void calculate(List<BatterySipper> sippers, BatteryStats batteryStats,
            long rawRealtimeUs, long rawUptimeUs, int statsType, SparseArray<UserHandle> asUsers) {
        // Not implemented. The computation is done by BatteryStatsHelper
    }
}
+0 −301
Original line number Diff line number Diff line
/*
 * Copyright (C) 2009 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.internal.os;

import android.compat.annotation.UnsupportedAppUsage;
import android.os.BatteryStats.Uid;
import android.os.Build;

import java.util.List;

/**
 * Contains power usage of an application, system service, or hardware type.
 *
 * @deprecated Please use BatteryStatsManager.getBatteryUsageStats instead.
 */
@Deprecated
public class BatterySipper implements Comparable<BatterySipper> {
    @UnsupportedAppUsage
    public int userId;
    @UnsupportedAppUsage
    public Uid uidObj;
    @UnsupportedAppUsage
    public DrainType drainType;

    /**
     * Smeared power from screen usage.
     * We split the screen usage power and smear them among apps, based on activity time.
     * The actual screen usage power may be measured or estimated, affecting the granularity and
     * accuracy of the smearing, but the smearing algorithm is essentially the same.
     */
    public double screenPowerMah;

    /**
     * Smeared power using proportional method.
     *
     * we smear power usage from hidden sippers to all apps proportionally.(except for screen usage)
     *
     * @see BatteryStatsHelper#shouldHideSipper(BatterySipper)
     * @see BatteryStatsHelper#removeHiddenBatterySippers(List)
     */
    public double proportionalSmearMah;

    /**
     * Total power that adding the smeared power.
     *
     * @see #sumPower()
     */
    public double totalSmearedPowerMah;

    /**
     * Total power before smearing
     */
    @UnsupportedAppUsage
    public double totalPowerMah;

    /**
     * Whether we should hide this sipper
     *
     * @see BatteryStatsHelper#shouldHideSipper(BatterySipper)
     */
    public boolean shouldHide;

    /**
     * Generic usage time in milliseconds.
     */
    @UnsupportedAppUsage
    public long usageTimeMs;

    /**
     * Generic power usage in mAh.
     */
    public double usagePowerMah;

    // Subsystem usage times.
    public long audioTimeMs;
    public long bluetoothRunningTimeMs;
    public long cameraTimeMs;
    @UnsupportedAppUsage
    public long cpuFgTimeMs;
    @UnsupportedAppUsage
    public long cpuTimeMs;
    public long flashlightTimeMs;
    @UnsupportedAppUsage
    public long gpsTimeMs;
    public long videoTimeMs;
    @UnsupportedAppUsage
    public long wakeLockTimeMs;
    @UnsupportedAppUsage
    public long wifiRunningTimeMs;

    public long mobileRxPackets;
    public long mobileTxPackets;
    public long mobileActive;
    public int mobileActiveCount;
    public double mobilemspp;         // milliseconds per packet
    public long wifiRxPackets;
    public long wifiTxPackets;
    public long mobileRxBytes;
    public long mobileTxBytes;
    public long wifiRxBytes;
    public long wifiTxBytes;
    public long btRxBytes;
    public long btTxBytes;
    public double percent;
    public double noCoveragePercent;
    @UnsupportedAppUsage
    public String[] mPackages;
    @UnsupportedAppUsage
    public String packageWithHighestDrain;

    // Measured in mAh (milli-ampere per hour).
    // These are included when summed.
    public double audioPowerMah;
    public double bluetoothPowerMah;
    public double cameraPowerMah;
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
    public double cpuPowerMah;
    public double flashlightPowerMah;
    public double gpsPowerMah;
    public double mobileRadioPowerMah;
    public double sensorPowerMah;
    public double videoPowerMah;
    public double wakeLockPowerMah;
    public double wifiPowerMah;
    public double systemServiceCpuPowerMah;
    public double[] customMeasuredPowerMah;

    // Power that is re-attributed to other sippers. For example, for System Server
    // this represents the power attributed to apps requesting system services.
    // The value should be negative or zero.
    public double powerReattributedToOtherSippersMah;

    // Do not include this sipper in results because it is included
    // in an aggregate sipper.
    public boolean isAggregated;

    //                           ****************
    // This list must be kept current with atoms.proto (frameworks/base/cmds/statsd/src/atoms.proto)
    // so the ordinal values (and therefore the order) must never change.
    //                           ****************
    @UnsupportedAppUsage(implicitMember =
            "values()[Lcom/android/internal/os/BatterySipper$DrainType;")
    public enum DrainType {
        AMBIENT_DISPLAY,
        @UnsupportedAppUsage
        APP,
        BLUETOOTH,
        CAMERA,
        CELL,
        FLASHLIGHT,
        IDLE,
        MEMORY,
        OVERCOUNTED,
        PHONE,
        SCREEN,
        UNACCOUNTED,
        USER,
        WIFI,
    }

    @UnsupportedAppUsage
    public BatterySipper(DrainType drainType, Uid uid, double value) {
        this.totalPowerMah = value;
        this.drainType = drainType;
        uidObj = uid;
    }

    public void computeMobilemspp() {
        long packets = mobileRxPackets + mobileTxPackets;
        mobilemspp = packets > 0 ? (mobileActive / (double) packets) : 0;
    }

    @Override
    public int compareTo(BatterySipper other) {
        // Over-counted always goes to the bottom.
        if (drainType != other.drainType) {
            if (drainType == DrainType.OVERCOUNTED) {
                // This is "larger"
                return 1;
            } else if (other.drainType == DrainType.OVERCOUNTED) {
                return -1;
            }
        }
        // Return the flipped value because we want the items in descending order
        return Double.compare(other.totalPowerMah, totalPowerMah);
    }

    /**
     * Gets a list of packages associated with the current user
     */
    @UnsupportedAppUsage
    public String[] getPackages() {
        return mPackages;
    }

    @UnsupportedAppUsage
    public int getUid() {
        // Bail out if the current sipper is not an App sipper.
        if (uidObj == null) {
            return 0;
        }
        return uidObj.getUid();
    }

    /**
     * Add stats from other to this BatterySipper.
     */
    @UnsupportedAppUsage
    public void add(BatterySipper other) {
        totalPowerMah += other.totalPowerMah;
        usageTimeMs += other.usageTimeMs;
        usagePowerMah += other.usagePowerMah;
        audioTimeMs += other.audioTimeMs;
        cpuTimeMs += other.cpuTimeMs;
        gpsTimeMs += other.gpsTimeMs;
        wifiRunningTimeMs += other.wifiRunningTimeMs;
        cpuFgTimeMs += other.cpuFgTimeMs;
        videoTimeMs += other.videoTimeMs;
        wakeLockTimeMs += other.wakeLockTimeMs;
        cameraTimeMs += other.cameraTimeMs;
        flashlightTimeMs += other.flashlightTimeMs;
        bluetoothRunningTimeMs += other.bluetoothRunningTimeMs;
        mobileRxPackets += other.mobileRxPackets;
        mobileTxPackets += other.mobileTxPackets;
        mobileActive += other.mobileActive;
        mobileActiveCount += other.mobileActiveCount;
        wifiRxPackets += other.wifiRxPackets;
        wifiTxPackets += other.wifiTxPackets;
        mobileRxBytes += other.mobileRxBytes;
        mobileTxBytes += other.mobileTxBytes;
        wifiRxBytes += other.wifiRxBytes;
        wifiTxBytes += other.wifiTxBytes;
        btRxBytes += other.btRxBytes;
        btTxBytes += other.btTxBytes;
        audioPowerMah += other.audioPowerMah;
        wifiPowerMah += other.wifiPowerMah;
        gpsPowerMah += other.gpsPowerMah;
        cpuPowerMah += other.cpuPowerMah;
        sensorPowerMah += other.sensorPowerMah;
        mobileRadioPowerMah += other.mobileRadioPowerMah;
        wakeLockPowerMah += other.wakeLockPowerMah;
        cameraPowerMah += other.cameraPowerMah;
        flashlightPowerMah += other.flashlightPowerMah;
        bluetoothPowerMah += other.bluetoothPowerMah;
        screenPowerMah += other.screenPowerMah;
        videoPowerMah += other.videoPowerMah;
        proportionalSmearMah += other.proportionalSmearMah;
        totalSmearedPowerMah += other.totalSmearedPowerMah;
        systemServiceCpuPowerMah += other.systemServiceCpuPowerMah;
        if (other.customMeasuredPowerMah != null) {
            if (customMeasuredPowerMah == null) {
                customMeasuredPowerMah = new double[other.customMeasuredPowerMah.length];
            }
            if (customMeasuredPowerMah.length == other.customMeasuredPowerMah.length) {
                // This should always be true.
                for (int idx = 0; idx < other.customMeasuredPowerMah.length; idx++) {
                    customMeasuredPowerMah[idx] += other.customMeasuredPowerMah[idx];
                }
            }
        }
        powerReattributedToOtherSippersMah += other.powerReattributedToOtherSippersMah;
    }

    /**
     * Sum all the powers and store the value into `value`.
     * Also sum the {@code smearedTotalPowerMah} by adding smeared powerMah.
     *
     * @return the sum of all the power in this BatterySipper.
     */
    public double sumPower() {
        totalPowerMah = usagePowerMah + wifiPowerMah + gpsPowerMah + cpuPowerMah +
                sensorPowerMah + mobileRadioPowerMah + wakeLockPowerMah + cameraPowerMah +
                flashlightPowerMah + bluetoothPowerMah + audioPowerMah + videoPowerMah
                + systemServiceCpuPowerMah;
        if (customMeasuredPowerMah != null) {
            for (int idx = 0; idx < customMeasuredPowerMah.length; idx++) {
                totalPowerMah += customMeasuredPowerMah[idx];
            }
        }

        // powerAttributedToOtherSippersMah is negative or zero
        totalPowerMah = totalPowerMah + powerReattributedToOtherSippersMah;

        totalSmearedPowerMah = totalPowerMah + screenPowerMah + proportionalSmearMah;

        return totalPowerMah;
    }
}
+0 −703

File deleted.

Preview size limit exceeded, changes collapsed.

Loading