Home | History | Annotate | Download | only in fuelgauge
      1 /*
      2  * Copyright (C) 2017 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.settings.fuelgauge;
     17 
     18 import android.content.Context;
     19 import android.content.pm.PackageManager;
     20 import android.os.BatteryStats;
     21 import android.os.SystemClock;
     22 import android.support.annotation.IntDef;
     23 import android.support.annotation.Nullable;
     24 import android.support.annotation.VisibleForTesting;
     25 import android.text.format.DateUtils;
     26 import android.util.Log;
     27 import android.util.SparseLongArray;
     28 
     29 import com.android.internal.os.BatterySipper;
     30 import com.android.internal.os.BatteryStatsHelper;
     31 import com.android.settings.overlay.FeatureFactory;
     32 
     33 import java.lang.annotation.Retention;
     34 import java.lang.annotation.RetentionPolicy;
     35 import java.util.Collections;
     36 import java.util.Comparator;
     37 import java.util.List;
     38 
     39 /**
     40  * Utils for battery operation
     41  */
     42 public class BatteryUtils {
     43     @Retention(RetentionPolicy.SOURCE)
     44     @IntDef({StatusType.FOREGROUND,
     45             StatusType.BACKGROUND,
     46             StatusType.ALL
     47     })
     48     public @interface StatusType {
     49         int FOREGROUND = 0;
     50         int BACKGROUND = 1;
     51         int ALL = 2;
     52     }
     53 
     54     private static final String TAG = "BatteryUtils";
     55 
     56     private static final int MIN_POWER_THRESHOLD_MILLI_AMP = 5;
     57     private static final int SECONDS_IN_HOUR = 60 * 60;
     58     private static BatteryUtils sInstance;
     59 
     60     private PackageManager mPackageManager;
     61     @VisibleForTesting
     62     PowerUsageFeatureProvider mPowerUsageFeatureProvider;
     63 
     64     public static BatteryUtils getInstance(Context context) {
     65         if (sInstance == null || sInstance.isDataCorrupted()) {
     66             sInstance = new BatteryUtils(context);
     67         }
     68         return sInstance;
     69     }
     70 
     71     @VisibleForTesting
     72     BatteryUtils(Context context) {
     73         mPackageManager = context.getPackageManager();
     74         mPowerUsageFeatureProvider = FeatureFactory.getFactory(
     75                 context).getPowerUsageFeatureProvider(context);
     76     }
     77 
     78     public long getProcessTimeMs(@StatusType int type, @Nullable BatteryStats.Uid uid,
     79             int which) {
     80         if (uid == null) {
     81             return 0;
     82         }
     83 
     84         switch (type) {
     85             case StatusType.FOREGROUND:
     86                 return getProcessForegroundTimeMs(uid, which);
     87             case StatusType.BACKGROUND:
     88                 return getProcessBackgroundTimeMs(uid, which);
     89             case StatusType.ALL:
     90                 return getProcessForegroundTimeMs(uid, which)
     91                         + getProcessBackgroundTimeMs(uid, which);
     92         }
     93         return 0;
     94     }
     95 
     96     private long getProcessBackgroundTimeMs(BatteryStats.Uid uid, int which) {
     97         final long rawRealTimeUs = convertMsToUs(SystemClock.elapsedRealtime());
     98         final long timeUs = uid.getProcessStateTime(
     99                 BatteryStats.Uid.PROCESS_STATE_BACKGROUND, rawRealTimeUs, which);
    100 
    101         Log.v(TAG, "package: " + mPackageManager.getNameForUid(uid.getUid()));
    102         Log.v(TAG, "background time(us): " + timeUs);
    103         return convertUsToMs(timeUs);
    104     }
    105 
    106     private long getProcessForegroundTimeMs(BatteryStats.Uid uid, int which) {
    107         final long rawRealTimeUs = convertMsToUs(SystemClock.elapsedRealtime());
    108         final int foregroundTypes[] = {BatteryStats.Uid.PROCESS_STATE_TOP};
    109         Log.v(TAG, "package: " + mPackageManager.getNameForUid(uid.getUid()));
    110 
    111         long timeUs = 0;
    112         for (int type : foregroundTypes) {
    113             final long localTime = uid.getProcessStateTime(type, rawRealTimeUs, which);
    114             Log.v(TAG, "type: " + type + " time(us): " + localTime);
    115             timeUs += localTime;
    116         }
    117         Log.v(TAG, "foreground time(us): " + timeUs);
    118 
    119         return convertUsToMs(timeUs);
    120     }
    121 
    122     /**
    123      * Remove the {@link BatterySipper} that we should hide and smear the screen usage based on
    124      * foreground activity time.
    125      *
    126      * @param sippers sipper list that need to check and remove
    127      * @return the total power of the hidden items of {@link BatterySipper}
    128      * for proportional smearing
    129      */
    130     public double removeHiddenBatterySippers(List<BatterySipper> sippers) {
    131         double proportionalSmearPowerMah = 0;
    132         BatterySipper screenSipper = null;
    133         for (int i = sippers.size() - 1; i >= 0; i--) {
    134             final BatterySipper sipper = sippers.get(i);
    135             if (shouldHideSipper(sipper)) {
    136                 sippers.remove(i);
    137                 if (sipper.drainType != BatterySipper.DrainType.OVERCOUNTED
    138                         && sipper.drainType != BatterySipper.DrainType.SCREEN
    139                         && sipper.drainType != BatterySipper.DrainType.UNACCOUNTED
    140                         && sipper.drainType != BatterySipper.DrainType.BLUETOOTH
    141                         && sipper.drainType != BatterySipper.DrainType.WIFI) {
    142                     // Don't add it if it is overcounted, unaccounted, wifi, bluetooth, or screen
    143                     proportionalSmearPowerMah += sipper.totalPowerMah;
    144                 }
    145             }
    146 
    147             if (sipper.drainType == BatterySipper.DrainType.SCREEN) {
    148                 screenSipper = sipper;
    149             }
    150         }
    151 
    152         smearScreenBatterySipper(sippers, screenSipper);
    153 
    154         return proportionalSmearPowerMah;
    155     }
    156 
    157     /**
    158      * Smear the screen on power usage among {@code sippers}, based on ratio of foreground activity
    159      * time.
    160      */
    161     @VisibleForTesting
    162     void smearScreenBatterySipper(List<BatterySipper> sippers, BatterySipper screenSipper) {
    163         final long rawRealtimeMs = SystemClock.elapsedRealtime();
    164         long totalActivityTimeMs = 0;
    165         final SparseLongArray activityTimeArray = new SparseLongArray();
    166         for (int i = 0, size = sippers.size(); i < size; i++) {
    167             final BatteryStats.Uid uid = sippers.get(i).uidObj;
    168             if (uid != null) {
    169                 final long timeMs = getForegroundActivityTotalTimeMs(uid, rawRealtimeMs);
    170                 activityTimeArray.put(uid.getUid(), timeMs);
    171                 totalActivityTimeMs += timeMs;
    172             }
    173         }
    174 
    175         if (totalActivityTimeMs >= 10 * DateUtils.MINUTE_IN_MILLIS) {
    176             final double screenPowerMah = screenSipper.totalPowerMah;
    177             for (int i = 0, size = sippers.size(); i < size; i++) {
    178                 final BatterySipper sipper = sippers.get(i);
    179                 sipper.totalPowerMah += screenPowerMah * activityTimeArray.get(sipper.getUid(), 0)
    180                         / totalActivityTimeMs;
    181             }
    182         }
    183     }
    184 
    185     /**
    186      * Check whether we should hide the battery sipper.
    187      */
    188     public boolean shouldHideSipper(BatterySipper sipper) {
    189         final BatterySipper.DrainType drainType = sipper.drainType;
    190 
    191         return drainType == BatterySipper.DrainType.IDLE
    192                 || drainType == BatterySipper.DrainType.CELL
    193                 || drainType == BatterySipper.DrainType.SCREEN
    194                 || drainType == BatterySipper.DrainType.UNACCOUNTED
    195                 || drainType == BatterySipper.DrainType.OVERCOUNTED
    196                 || drainType == BatterySipper.DrainType.BLUETOOTH
    197                 || drainType == BatterySipper.DrainType.WIFI
    198                 || (sipper.totalPowerMah * SECONDS_IN_HOUR) < MIN_POWER_THRESHOLD_MILLI_AMP
    199                 || mPowerUsageFeatureProvider.isTypeService(sipper)
    200                 || mPowerUsageFeatureProvider.isTypeSystem(sipper);
    201     }
    202 
    203     /**
    204      * Calculate the power usage percentage for an app
    205      *
    206      * @param powerUsageMah   power used by the app
    207      * @param totalPowerMah   total power used in the system
    208      * @param hiddenPowerMah  power used by no-actionable app that we want to hide, i.e. Screen,
    209      *                        Android OS.
    210      * @param dischargeAmount The discharge amount calculated by {@link BatteryStats}
    211      * @return A percentage value scaled by {@paramref dischargeAmount}
    212      * @see BatteryStats#getDischargeAmount(int)
    213      */
    214     public double calculateBatteryPercent(double powerUsageMah, double totalPowerMah,
    215             double hiddenPowerMah, int dischargeAmount) {
    216         if (totalPowerMah == 0) {
    217             return 0;
    218         }
    219 
    220         return (powerUsageMah / (totalPowerMah - hiddenPowerMah)) * dischargeAmount;
    221     }
    222 
    223     /**
    224      * Sort the {@code usageList} based on {@link BatterySipper#totalPowerMah}
    225      * @param usageList
    226      */
    227     public void sortUsageList(List<BatterySipper> usageList) {
    228         Collections.sort(usageList, new Comparator<BatterySipper>() {
    229             @Override
    230             public int compare(BatterySipper a, BatterySipper b) {
    231                 return Double.compare(b.totalPowerMah, a.totalPowerMah);
    232             }
    233         });
    234     }
    235 
    236     /**
    237      * Calculate the time since last full charge, including the device off time
    238      *
    239      * @param batteryStatsHelper utility class that contains the data
    240      * @param currentTimeMs      current wall time
    241      * @return time in millis
    242      */
    243     public long calculateLastFullChargeTime(BatteryStatsHelper batteryStatsHelper,
    244             long currentTimeMs) {
    245         return currentTimeMs - batteryStatsHelper.getStats().getStartClockTime();
    246 
    247     }
    248 
    249     private long convertUsToMs(long timeUs) {
    250         return timeUs / 1000;
    251     }
    252 
    253     private long convertMsToUs(long timeMs) {
    254         return timeMs * 1000;
    255     }
    256 
    257     private boolean isDataCorrupted() {
    258         return mPackageManager == null;
    259     }
    260 
    261     @VisibleForTesting
    262     long getForegroundActivityTotalTimeMs(BatteryStats.Uid uid, long rawRealtimeMs) {
    263         final BatteryStats.Timer timer = uid.getForegroundActivityTimer();
    264         if (timer != null) {
    265             return timer.getTotalTimeLocked(rawRealtimeMs, BatteryStats.STATS_SINCE_CHARGED);
    266         }
    267 
    268         return 0;
    269     }
    270 
    271 }
    272 
    273