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
calculate(BatteryUsageStats.Builder builder, BatteryStats batteryStats, long rawRealtimeUs, long rawUptimeUs, BatteryUsageStatsQuery query)38     public void calculate(BatteryUsageStats.Builder builder, BatteryStats batteryStats,
39             long rawRealtimeUs, long rawUptimeUs, BatteryUsageStatsQuery query) {
40         super.calculate(builder, batteryStats, rawRealtimeUs, rawUptimeUs, query);
41 
42         final long durationMs = batteryStats.getFlashlightOnTime(rawRealtimeUs,
43                 BatteryStats.STATS_SINCE_CHARGED) / 1000;
44         final double powerMah = mPowerEstimator.calculatePower(durationMs);
45         builder.getAggregateBatteryConsumerBuilder(
46                 BatteryUsageStats.AGGREGATE_BATTERY_CONSUMER_SCOPE_DEVICE)
47                 .setUsageDurationMillis(BatteryConsumer.POWER_COMPONENT_FLASHLIGHT, durationMs)
48                 .setConsumedPower(BatteryConsumer.POWER_COMPONENT_FLASHLIGHT, powerMah);
49         builder.getAggregateBatteryConsumerBuilder(
50                 BatteryUsageStats.AGGREGATE_BATTERY_CONSUMER_SCOPE_ALL_APPS)
51                 .setUsageDurationMillis(BatteryConsumer.POWER_COMPONENT_FLASHLIGHT, durationMs)
52                 .setConsumedPower(BatteryConsumer.POWER_COMPONENT_FLASHLIGHT, powerMah);
53     }
54 
55     @Override
calculateApp(UidBatteryConsumer.Builder app, BatteryStats.Uid u, long rawRealtimeUs, long rawUptimeUs, BatteryUsageStatsQuery query)56     protected void calculateApp(UidBatteryConsumer.Builder app, BatteryStats.Uid u,
57             long rawRealtimeUs, long rawUptimeUs, BatteryUsageStatsQuery query) {
58         final long durationMs = mPowerEstimator.calculateDuration(u.getFlashlightTurnedOnTimer(),
59                 rawRealtimeUs, BatteryStats.STATS_SINCE_CHARGED);
60         final double powerMah = mPowerEstimator.calculatePower(durationMs);
61         app.setUsageDurationMillis(BatteryConsumer.POWER_COMPONENT_FLASHLIGHT, durationMs)
62                 .setConsumedPower(BatteryConsumer.POWER_COMPONENT_FLASHLIGHT, powerMah);
63     }
64 
65     @Override
calculateApp(BatterySipper app, BatteryStats.Uid u, long rawRealtimeUs, long rawUptimeUs, int statsType)66     protected void calculateApp(BatterySipper app, BatteryStats.Uid u, long rawRealtimeUs,
67             long rawUptimeUs, int statsType) {
68         final long durationMs = mPowerEstimator.calculateDuration(u.getFlashlightTurnedOnTimer(),
69                 rawRealtimeUs, statsType);
70         final double powerMah = mPowerEstimator.calculatePower(durationMs);
71         app.flashlightTimeMs = durationMs;
72         app.flashlightPowerMah = powerMah;
73     }
74 }
75