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 camera subsystem, excluding the flashlight.
26  *
27  * Note: Power draw for the flash unit should be included in the FlashlightPowerCalculator.
28  */
29 public class CameraPowerCalculator extends PowerCalculator {
30     // Calculate camera power usage.  Right now, this is a (very) rough estimate based on the
31     // average power usage for a typical camera application.
32     private final UsageBasedPowerEstimator mPowerEstimator;
33 
CameraPowerCalculator(PowerProfile profile)34     public CameraPowerCalculator(PowerProfile profile) {
35         mPowerEstimator = new UsageBasedPowerEstimator(
36                 profile.getAveragePower(PowerProfile.POWER_CAMERA));
37     }
38 
39     @Override
calculate(BatteryUsageStats.Builder builder, BatteryStats batteryStats, long rawRealtimeUs, long rawUptimeUs, BatteryUsageStatsQuery query)40     public void calculate(BatteryUsageStats.Builder builder, BatteryStats batteryStats,
41             long rawRealtimeUs, long rawUptimeUs, BatteryUsageStatsQuery query) {
42         super.calculate(builder, batteryStats, rawRealtimeUs, rawUptimeUs, query);
43 
44         final long durationMs = batteryStats.getCameraOnTime(rawRealtimeUs,
45                 BatteryStats.STATS_SINCE_CHARGED) / 1000;
46         final double powerMah = mPowerEstimator.calculatePower(durationMs);
47         builder.getAggregateBatteryConsumerBuilder(
48                 BatteryUsageStats.AGGREGATE_BATTERY_CONSUMER_SCOPE_DEVICE)
49                 .setUsageDurationMillis(BatteryConsumer.POWER_COMPONENT_CAMERA, durationMs)
50                 .setConsumedPower(BatteryConsumer.POWER_COMPONENT_CAMERA, powerMah);
51         builder.getAggregateBatteryConsumerBuilder(
52                 BatteryUsageStats.AGGREGATE_BATTERY_CONSUMER_SCOPE_ALL_APPS)
53                 .setUsageDurationMillis(BatteryConsumer.POWER_COMPONENT_CAMERA, durationMs)
54                 .setConsumedPower(BatteryConsumer.POWER_COMPONENT_CAMERA, powerMah);
55     }
56 
57     @Override
calculateApp(UidBatteryConsumer.Builder app, BatteryStats.Uid u, long rawRealtimeUs, long rawUptimeUs, BatteryUsageStatsQuery query)58     protected void calculateApp(UidBatteryConsumer.Builder app, BatteryStats.Uid u,
59             long rawRealtimeUs, long rawUptimeUs, BatteryUsageStatsQuery query) {
60         final long durationMs =
61                 mPowerEstimator.calculateDuration(u.getCameraTurnedOnTimer(), rawRealtimeUs,
62                         BatteryStats.STATS_SINCE_CHARGED);
63         final double powerMah = mPowerEstimator.calculatePower(durationMs);
64         app.setUsageDurationMillis(BatteryConsumer.POWER_COMPONENT_CAMERA, durationMs)
65                 .setConsumedPower(BatteryConsumer.POWER_COMPONENT_CAMERA, powerMah);
66     }
67 
68     @Override
calculateApp(BatterySipper app, BatteryStats.Uid u, long rawRealtimeUs, long rawUptimeUs, int statsType)69     protected void calculateApp(BatterySipper app, BatteryStats.Uid u, long rawRealtimeUs,
70             long rawUptimeUs, int statsType) {
71         final long durationMs = mPowerEstimator.calculateDuration(u.getCameraTurnedOnTimer(),
72                 rawRealtimeUs, statsType);
73         final double powerMah = mPowerEstimator.calculatePower(durationMs);
74         app.cameraTimeMs = durationMs;
75         app.cameraPowerMah = powerMah;
76     }
77 }
78