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.BatteryStats;
19 
20 /**
21  * Power calculator for the flashlight.
22  */
23 public class FlashlightPowerCalculator extends PowerCalculator {
24     private final double mFlashlightPowerOnAvg;
25 
FlashlightPowerCalculator(PowerProfile profile)26     public FlashlightPowerCalculator(PowerProfile profile) {
27         mFlashlightPowerOnAvg = profile.getAveragePower(PowerProfile.POWER_FLASHLIGHT);
28     }
29 
30     @Override
calculateApp(BatterySipper app, BatteryStats.Uid u, long rawRealtimeUs, long rawUptimeUs, int statsType)31     public void calculateApp(BatterySipper app, BatteryStats.Uid u, long rawRealtimeUs,
32                              long rawUptimeUs, int statsType) {
33 
34         // Calculate flashlight power usage.  Right now, this is based on the average power draw
35         // of the flash unit when kept on over a short period of time.
36         final BatteryStats.Timer timer = u.getFlashlightTurnedOnTimer();
37         if (timer != null) {
38             final long totalTime = timer.getTotalTimeLocked(rawRealtimeUs, statsType) / 1000;
39             app.flashlightTimeMs = totalTime;
40             app.flashlightPowerMah = (totalTime * mFlashlightPowerOnAvg) / (1000*60*60);
41         } else {
42             app.flashlightTimeMs = 0;
43             app.flashlightPowerMah = 0;
44         }
45     }
46 }
47