1 /* 2 * Copyright (C) 2020 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.BatteryConsumer; 20 import android.os.BatteryStats; 21 import android.os.BatteryUsageStats; 22 import android.os.BatteryUsageStatsQuery; 23 24 /** 25 * Estimates power consumed by telephony. 26 */ 27 public class PhonePowerCalculator extends PowerCalculator { 28 private final UsageBasedPowerEstimator mPowerEstimator; 29 PhonePowerCalculator(PowerProfile powerProfile)30 public PhonePowerCalculator(PowerProfile powerProfile) { 31 mPowerEstimator = new UsageBasedPowerEstimator( 32 powerProfile.getAveragePower(PowerProfile.POWER_RADIO_ACTIVE)); 33 } 34 35 @Override isPowerComponentSupported(@atteryConsumer.PowerComponent int powerComponent)36 public boolean isPowerComponentSupported(@BatteryConsumer.PowerComponent int powerComponent) { 37 return powerComponent == BatteryConsumer.POWER_COMPONENT_PHONE; 38 } 39 40 @Override calculate(BatteryUsageStats.Builder builder, BatteryStats batteryStats, long rawRealtimeUs, long rawUptimeUs, BatteryUsageStatsQuery query)41 public void calculate(BatteryUsageStats.Builder builder, BatteryStats batteryStats, 42 long rawRealtimeUs, long rawUptimeUs, BatteryUsageStatsQuery query) { 43 final long energyConsumerUC = batteryStats.getPhoneEnergyConsumptionUC(); 44 final int powerModel = getPowerModel(energyConsumerUC, query); 45 46 final long phoneOnTimeMs = batteryStats.getPhoneOnTime(rawRealtimeUs, 47 BatteryStats.STATS_SINCE_CHARGED) / 1000; 48 final double phoneOnPower; 49 switch (powerModel) { 50 case BatteryConsumer.POWER_MODEL_MEASURED_ENERGY: 51 phoneOnPower = uCtoMah(energyConsumerUC); 52 break; 53 case BatteryConsumer.POWER_MODEL_POWER_PROFILE: 54 default: 55 phoneOnPower = mPowerEstimator.calculatePower(phoneOnTimeMs); 56 } 57 58 if (phoneOnPower == 0.0) return; 59 60 builder.getAggregateBatteryConsumerBuilder( 61 BatteryUsageStats.AGGREGATE_BATTERY_CONSUMER_SCOPE_DEVICE) 62 .setConsumedPower(BatteryConsumer.POWER_COMPONENT_PHONE, phoneOnPower, powerModel) 63 .setUsageDurationMillis(BatteryConsumer.POWER_COMPONENT_PHONE, phoneOnTimeMs); 64 65 } 66 } 67