• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
isPowerComponentSupported(@atteryConsumer.PowerComponent int powerComponent)40     public boolean isPowerComponentSupported(@BatteryConsumer.PowerComponent int powerComponent) {
41         return powerComponent == BatteryConsumer.POWER_COMPONENT_CAMERA;
42     }
43 
44     @Override
calculate(BatteryUsageStats.Builder builder, BatteryStats batteryStats, long rawRealtimeUs, long rawUptimeUs, BatteryUsageStatsQuery query)45     public void calculate(BatteryUsageStats.Builder builder, BatteryStats batteryStats,
46             long rawRealtimeUs, long rawUptimeUs, BatteryUsageStatsQuery query) {
47         super.calculate(builder, batteryStats, rawRealtimeUs, rawUptimeUs, query);
48 
49         final long durationMs = batteryStats.getCameraOnTime(rawRealtimeUs,
50                 BatteryStats.STATS_SINCE_CHARGED) / 1000;
51         final double powerMah = mPowerEstimator.calculatePower(durationMs);
52         builder.getAggregateBatteryConsumerBuilder(
53                 BatteryUsageStats.AGGREGATE_BATTERY_CONSUMER_SCOPE_DEVICE)
54                 .setUsageDurationMillis(BatteryConsumer.POWER_COMPONENT_CAMERA, durationMs)
55                 .setConsumedPower(BatteryConsumer.POWER_COMPONENT_CAMERA, powerMah);
56         builder.getAggregateBatteryConsumerBuilder(
57                 BatteryUsageStats.AGGREGATE_BATTERY_CONSUMER_SCOPE_ALL_APPS)
58                 .setUsageDurationMillis(BatteryConsumer.POWER_COMPONENT_CAMERA, durationMs)
59                 .setConsumedPower(BatteryConsumer.POWER_COMPONENT_CAMERA, powerMah);
60     }
61 
62     @Override
calculateApp(UidBatteryConsumer.Builder app, BatteryStats.Uid u, long rawRealtimeUs, long rawUptimeUs, BatteryUsageStatsQuery query)63     protected void calculateApp(UidBatteryConsumer.Builder app, BatteryStats.Uid u,
64             long rawRealtimeUs, long rawUptimeUs, BatteryUsageStatsQuery query) {
65         final long durationMs =
66                 mPowerEstimator.calculateDuration(u.getCameraTurnedOnTimer(), rawRealtimeUs,
67                         BatteryStats.STATS_SINCE_CHARGED);
68         final double powerMah = mPowerEstimator.calculatePower(durationMs);
69         app.setUsageDurationMillis(BatteryConsumer.POWER_COMPONENT_CAMERA, durationMs)
70                 .setConsumedPower(BatteryConsumer.POWER_COMPONENT_CAMERA, powerMah);
71     }
72 }
73