• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 
17 package com.android.internal.os;
18 
19 import android.os.BatteryStats;
20 
21 /**
22  * Implements a simple linear power model based on the assumption that the power consumer
23  * consumes a fixed current when it is used and no current when it is unused.
24  *
25  * <code>power = usageDuration * averagePower</code>
26  */
27 public class UsageBasedPowerEstimator {
28     private static final double MILLIS_IN_HOUR = 1000.0 * 60 * 60;
29     private final double mAveragePowerMahPerMs;
30 
UsageBasedPowerEstimator(double averagePowerMilliAmp)31     public UsageBasedPowerEstimator(double averagePowerMilliAmp) {
32         mAveragePowerMahPerMs = averagePowerMilliAmp / MILLIS_IN_HOUR;
33     }
34 
isSupported()35     public boolean isSupported() {
36         return mAveragePowerMahPerMs != 0;
37     }
38 
39     /**
40      * Given a {@link BatteryStats.Timer}, returns the accumulated duration.
41      */
calculateDuration(BatteryStats.Timer timer, long rawRealtimeUs, int statsType)42     public long calculateDuration(BatteryStats.Timer timer, long rawRealtimeUs, int statsType) {
43         return timer == null ? 0 : timer.getTotalTimeLocked(rawRealtimeUs, statsType) / 1000;
44     }
45 
46     /**
47      * Given a duration in milliseconds, return the estimated power consumption.
48      */
calculatePower(long durationMs)49     public double calculatePower(long durationMs) {
50         return mAveragePowerMahPerMs * durationMs;
51     }
52 }
53