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 */
16package com.android.internal.os;
17
18import android.hardware.Sensor;
19import android.hardware.SensorManager;
20import android.os.BatteryStats;
21import android.util.SparseArray;
22
23import java.util.List;
24
25public class SensorPowerCalculator extends PowerCalculator {
26    private final List<Sensor> mSensors;
27    private final double mGpsPowerOn;
28
29    public SensorPowerCalculator(PowerProfile profile, SensorManager sensorManager) {
30        mSensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
31        mGpsPowerOn = profile.getAveragePower(PowerProfile.POWER_GPS_ON);
32    }
33
34    @Override
35    public void calculateApp(BatterySipper app, BatteryStats.Uid u, long rawRealtimeUs,
36                             long rawUptimeUs, int statsType) {
37        // Process Sensor usage
38        final SparseArray<? extends BatteryStats.Uid.Sensor> sensorStats = u.getSensorStats();
39        final int NSE = sensorStats.size();
40        for (int ise = 0; ise < NSE; ise++) {
41            final BatteryStats.Uid.Sensor sensor = sensorStats.valueAt(ise);
42            final int sensorHandle = sensorStats.keyAt(ise);
43            final BatteryStats.Timer timer = sensor.getSensorTime();
44            final long sensorTime = timer.getTotalTimeLocked(rawRealtimeUs, statsType) / 1000;
45            switch (sensorHandle) {
46                case BatteryStats.Uid.Sensor.GPS:
47                    app.gpsTimeMs = sensorTime;
48                    app.gpsPowerMah = (app.gpsTimeMs * mGpsPowerOn) / (1000*60*60);
49                    break;
50                default:
51                    final int sensorsCount = mSensors.size();
52                    for (int i = 0; i < sensorsCount; i++) {
53                        final Sensor s = mSensors.get(i);
54                        if (s.getHandle() == sensorHandle) {
55                            app.sensorPowerMah += (sensorTime * s.getPower()) / (1000*60*60);
56                            break;
57                        }
58                    }
59                    break;
60            }
61        }
62    }
63}
64