1 /* 2 * Copyright (C) 2015 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.BatteryConsumer; 19 import android.os.BatteryStats; 20 import android.os.BatteryUsageStats; 21 import android.os.BatteryUsageStatsQuery; 22 import android.os.UidBatteryConsumer; 23 24 /** 25 * Power calculator for the flashlight. 26 */ 27 public class FlashlightPowerCalculator extends PowerCalculator { 28 // Calculate flashlight power usage. Right now, this is based on the average power draw 29 // of the flash unit when kept on over a short period of time. 30 private final UsageBasedPowerEstimator mPowerEstimator; 31 FlashlightPowerCalculator(PowerProfile profile)32 public FlashlightPowerCalculator(PowerProfile profile) { 33 mPowerEstimator = new UsageBasedPowerEstimator( 34 profile.getAveragePower(PowerProfile.POWER_FLASHLIGHT)); 35 } 36 37 @Override isPowerComponentSupported(@atteryConsumer.PowerComponent int powerComponent)38 public boolean isPowerComponentSupported(@BatteryConsumer.PowerComponent int powerComponent) { 39 return powerComponent == BatteryConsumer.POWER_COMPONENT_FLASHLIGHT; 40 } 41 42 @Override calculate(BatteryUsageStats.Builder builder, BatteryStats batteryStats, long rawRealtimeUs, long rawUptimeUs, BatteryUsageStatsQuery query)43 public void calculate(BatteryUsageStats.Builder builder, BatteryStats batteryStats, 44 long rawRealtimeUs, long rawUptimeUs, BatteryUsageStatsQuery query) { 45 super.calculate(builder, batteryStats, rawRealtimeUs, rawUptimeUs, query); 46 47 final long durationMs = batteryStats.getFlashlightOnTime(rawRealtimeUs, 48 BatteryStats.STATS_SINCE_CHARGED) / 1000; 49 final double powerMah = mPowerEstimator.calculatePower(durationMs); 50 builder.getAggregateBatteryConsumerBuilder( 51 BatteryUsageStats.AGGREGATE_BATTERY_CONSUMER_SCOPE_DEVICE) 52 .setUsageDurationMillis(BatteryConsumer.POWER_COMPONENT_FLASHLIGHT, durationMs) 53 .setConsumedPower(BatteryConsumer.POWER_COMPONENT_FLASHLIGHT, powerMah); 54 builder.getAggregateBatteryConsumerBuilder( 55 BatteryUsageStats.AGGREGATE_BATTERY_CONSUMER_SCOPE_ALL_APPS) 56 .setUsageDurationMillis(BatteryConsumer.POWER_COMPONENT_FLASHLIGHT, durationMs) 57 .setConsumedPower(BatteryConsumer.POWER_COMPONENT_FLASHLIGHT, powerMah); 58 } 59 60 @Override calculateApp(UidBatteryConsumer.Builder app, BatteryStats.Uid u, long rawRealtimeUs, long rawUptimeUs, BatteryUsageStatsQuery query)61 protected void calculateApp(UidBatteryConsumer.Builder app, BatteryStats.Uid u, 62 long rawRealtimeUs, long rawUptimeUs, BatteryUsageStatsQuery query) { 63 final long durationMs = mPowerEstimator.calculateDuration(u.getFlashlightTurnedOnTimer(), 64 rawRealtimeUs, BatteryStats.STATS_SINCE_CHARGED); 65 final double powerMah = mPowerEstimator.calculatePower(durationMs); 66 app.setUsageDurationMillis(BatteryConsumer.POWER_COMPONENT_FLASHLIGHT, durationMs) 67 .setConsumedPower(BatteryConsumer.POWER_COMPONENT_FLASHLIGHT, powerMah); 68 } 69 } 70