1 /* 2 * Copyright (C) 2018 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; 19 20 /** 21 * A {@link PowerCalculator} to calculate power consumed by audio and video hardware. 22 * 23 * Also see {@link PowerProfile#POWER_AUDIO} and {@link PowerProfile#POWER_VIDEO}. 24 */ 25 public class MediaPowerCalculator extends PowerCalculator { 26 private static final int MS_IN_HR = 1000 * 60 * 60; 27 private final double mAudioAveragePowerMa; 28 private final double mVideoAveragePowerMa; 29 MediaPowerCalculator(PowerProfile profile)30 public MediaPowerCalculator(PowerProfile profile) { 31 mAudioAveragePowerMa = profile.getAveragePower(PowerProfile.POWER_AUDIO); 32 mVideoAveragePowerMa = profile.getAveragePower(PowerProfile.POWER_VIDEO); 33 } 34 35 @Override calculateApp(BatterySipper app, BatteryStats.Uid u, long rawRealtimeUs, long rawUptimeUs, int statsType)36 public void calculateApp(BatterySipper app, BatteryStats.Uid u, long rawRealtimeUs, 37 long rawUptimeUs, int statsType) { 38 // Calculate audio power usage, an estimate based on the average power routed to different 39 // components like speaker, bluetooth, usb-c, earphone, etc. 40 final BatteryStats.Timer audioTimer = u.getAudioTurnedOnTimer(); 41 if (audioTimer == null) { 42 app.audioTimeMs = 0; 43 app.audioPowerMah = 0; 44 } else { 45 final long totalTime = audioTimer.getTotalTimeLocked(rawRealtimeUs, statsType) / 1000; 46 app.audioTimeMs = totalTime; 47 app.audioPowerMah = (totalTime * mAudioAveragePowerMa) / MS_IN_HR; 48 } 49 50 // Calculate video power usage. 51 final BatteryStats.Timer videoTimer = u.getVideoTurnedOnTimer(); 52 if (videoTimer == null) { 53 app.videoTimeMs = 0; 54 app.videoPowerMah = 0; 55 } else { 56 final long totalTime = videoTimer.getTotalTimeLocked(rawRealtimeUs, statsType) / 1000; 57 app.videoTimeMs = totalTime; 58 app.videoPowerMah = (totalTime * mVideoAveragePowerMa) / MS_IN_HR; 59 } 60 } 61 } 62