BluetoothPowerCalculator.java revision 8576cf941dc20b64d60b6b7f5d0879b823628393
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.os.BatteryStats;
19import android.util.Log;
20
21public class BluetoothPowerCalculator extends PowerCalculator {
22    private static final boolean DEBUG = BatteryStatsHelper.DEBUG;
23    private static final String TAG = "BluetoothPowerCalculator";
24    private final double mIdleMa;
25    private final double mRxMa;
26    private final double mTxMa;
27
28    public BluetoothPowerCalculator(PowerProfile profile) {
29        mIdleMa = profile.getAveragePower(PowerProfile.POWER_BLUETOOTH_CONTROLLER_IDLE);
30        mRxMa = profile.getAveragePower(PowerProfile.POWER_BLUETOOTH_CONTROLLER_RX);
31        mTxMa = profile.getAveragePower(PowerProfile.POWER_BLUETOOTH_CONTROLLER_TX);
32    }
33
34    @Override
35    public void calculateApp(BatterySipper app, BatteryStats.Uid u, long rawRealtimeUs,
36                             long rawUptimeUs, int statsType) {
37        // No per-app distribution yet.
38    }
39
40    @Override
41    public void calculateRemaining(BatterySipper app, BatteryStats stats, long rawRealtimeUs,
42                                   long rawUptimeUs, int statsType) {
43        final long idleTimeMs = stats.getBluetoothControllerActivity(
44                BatteryStats.CONTROLLER_IDLE_TIME, statsType);
45        final long txTimeMs = stats.getBluetoothControllerActivity(
46                BatteryStats.CONTROLLER_TX_TIME, statsType);
47        final long rxTimeMs = stats.getBluetoothControllerActivity(
48                BatteryStats.CONTROLLER_RX_TIME, statsType);
49        final long totalTimeMs = idleTimeMs + txTimeMs + rxTimeMs;
50        double powerMah = stats.getBluetoothControllerActivity(
51                BatteryStats.CONTROLLER_POWER_DRAIN, statsType) / (double)(1000*60*60);
52
53        if (powerMah == 0) {
54            // Some devices do not report the power, so calculate it.
55            powerMah = ((idleTimeMs * mIdleMa) + (rxTimeMs * mRxMa) + (txTimeMs * mTxMa))
56                    / (1000*60*60);
57        }
58
59        if (DEBUG && powerMah != 0) {
60            Log.d(TAG, "Bluetooth active: time=" + (totalTimeMs)
61                    + " power=" + BatteryStatsHelper.makemAh(powerMah));
62        }
63
64        app.usagePowerMah = powerMah;
65        app.usageTimeMs = totalTimeMs;
66    }
67}
68