Home | History | Annotate | Download | only in os
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.internal.os;
     17 
     18 import android.os.BatteryStats.Uid;
     19 
     20 /**
     21  * Contains power usage of an application, system service, or hardware type.
     22  */
     23 public class BatterySipper implements Comparable<BatterySipper> {
     24     public int userId;
     25     public Uid uidObj;
     26     public double totalPowerMah;
     27     public DrainType drainType;
     28 
     29     /**
     30      * Generic usage time in milliseconds.
     31      */
     32     public long usageTimeMs;
     33 
     34     /**
     35      * Generic power usage in mAh.
     36      */
     37     public double usagePowerMah;
     38 
     39     // Subsystem usage times.
     40     public long cpuTimeMs;
     41     public long gpsTimeMs;
     42     public long wifiRunningTimeMs;
     43     public long cpuFgTimeMs;
     44     public long wakeLockTimeMs;
     45     public long cameraTimeMs;
     46     public long flashlightTimeMs;
     47 
     48     public long mobileRxPackets;
     49     public long mobileTxPackets;
     50     public long mobileActive;
     51     public int mobileActiveCount;
     52     public double mobilemspp;         // milliseconds per packet
     53     public long wifiRxPackets;
     54     public long wifiTxPackets;
     55     public long mobileRxBytes;
     56     public long mobileTxBytes;
     57     public long wifiRxBytes;
     58     public long wifiTxBytes;
     59     public double percent;
     60     public double noCoveragePercent;
     61     public String[] mPackages;
     62     public String packageWithHighestDrain;
     63 
     64     // Measured in mAh (milli-ampere per hour).
     65     // These are included when summed.
     66     public double wifiPowerMah;
     67     public double cpuPowerMah;
     68     public double wakeLockPowerMah;
     69     public double mobileRadioPowerMah;
     70     public double gpsPowerMah;
     71     public double sensorPowerMah;
     72     public double cameraPowerMah;
     73     public double flashlightPowerMah;
     74 
     75     public enum DrainType {
     76         IDLE,
     77         CELL,
     78         PHONE,
     79         WIFI,
     80         BLUETOOTH,
     81         FLASHLIGHT,
     82         SCREEN,
     83         APP,
     84         USER,
     85         UNACCOUNTED,
     86         OVERCOUNTED,
     87         CAMERA
     88     }
     89 
     90     public BatterySipper(DrainType drainType, Uid uid, double value) {
     91         this.totalPowerMah = value;
     92         this.drainType = drainType;
     93         uidObj = uid;
     94     }
     95 
     96     public void computeMobilemspp() {
     97         long packets = mobileRxPackets+mobileTxPackets;
     98         mobilemspp = packets > 0 ? (mobileActive / (double)packets) : 0;
     99     }
    100 
    101     @Override
    102     public int compareTo(BatterySipper other) {
    103         // Over-counted always goes to the bottom.
    104         if (drainType != other.drainType) {
    105             if (drainType == DrainType.OVERCOUNTED) {
    106                 // This is "larger"
    107                 return 1;
    108             } else if (other.drainType == DrainType.OVERCOUNTED) {
    109                 return -1;
    110             }
    111         }
    112         // Return the flipped value because we want the items in descending order
    113         return Double.compare(other.totalPowerMah, totalPowerMah);
    114     }
    115 
    116     /**
    117      * Gets a list of packages associated with the current user
    118      */
    119     public String[] getPackages() {
    120         return mPackages;
    121     }
    122 
    123     public int getUid() {
    124         // Bail out if the current sipper is not an App sipper.
    125         if (uidObj == null) {
    126             return 0;
    127         }
    128         return uidObj.getUid();
    129     }
    130 
    131     /**
    132      * Add stats from other to this BatterySipper.
    133      */
    134     public void add(BatterySipper other) {
    135         totalPowerMah += other.totalPowerMah;
    136         usageTimeMs += other.usageTimeMs;
    137         usagePowerMah += other.usagePowerMah;
    138         cpuTimeMs += other.cpuTimeMs;
    139         gpsTimeMs += other.gpsTimeMs;
    140         wifiRunningTimeMs += other.wifiRunningTimeMs;
    141         cpuFgTimeMs += other.cpuFgTimeMs;
    142         wakeLockTimeMs += other.wakeLockTimeMs;
    143         cameraTimeMs += other.cameraTimeMs;
    144         flashlightTimeMs += other.flashlightTimeMs;
    145         mobileRxPackets += other.mobileRxPackets;
    146         mobileTxPackets += other.mobileTxPackets;
    147         mobileActive += other.mobileActive;
    148         mobileActiveCount += other.mobileActiveCount;
    149         wifiRxPackets += other.wifiRxPackets;
    150         wifiTxPackets += other.wifiTxPackets;
    151         mobileRxBytes += other.mobileRxBytes;
    152         mobileTxBytes += other.mobileTxBytes;
    153         wifiRxBytes += other.wifiRxBytes;
    154         wifiTxBytes += other.wifiTxBytes;
    155         wifiPowerMah += other.wifiPowerMah;
    156         gpsPowerMah += other.gpsPowerMah;
    157         cpuPowerMah += other.cpuPowerMah;
    158         sensorPowerMah += other.sensorPowerMah;
    159         mobileRadioPowerMah += other.mobileRadioPowerMah;
    160         wakeLockPowerMah += other.wakeLockPowerMah;
    161         cameraPowerMah += other.cameraPowerMah;
    162         flashlightPowerMah += other.flashlightPowerMah;
    163     }
    164 
    165     /**
    166      * Sum all the powers and store the value into `value`.
    167      * @return the sum of all the power in this BatterySipper.
    168      */
    169     public double sumPower() {
    170         return totalPowerMah = usagePowerMah + wifiPowerMah + gpsPowerMah + cpuPowerMah +
    171                 sensorPowerMah + mobileRadioPowerMah + wakeLockPowerMah + cameraPowerMah +
    172                 flashlightPowerMah;
    173     }
    174 }
    175