1 /* 2 * Copyright (C) 2018 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.telephony.metrics; 17 18 import android.os.BatteryStatsManager; 19 import android.os.connectivity.CellularBatteryStats; 20 import android.telephony.CellSignalStrength; 21 import android.telephony.ModemActivityInfo; 22 import android.telephony.TelephonyManager; 23 import android.text.format.DateUtils; 24 25 import com.android.internal.telephony.nano.TelephonyProto.ModemPowerStats; 26 27 import java.util.ArrayList; 28 import java.util.List; 29 30 /** 31 * ModemPowerMetrics holds the modem power metrics and converts them to ModemPowerStats proto buf. 32 * This proto buf is included in the Telephony proto buf. 33 */ 34 public class ModemPowerMetrics { 35 36 37 private static final int DATA_CONNECTION_EMERGENCY_SERVICE = 38 TelephonyManager.getAllNetworkTypes().length + 1; 39 private static final int DATA_CONNECTION_OTHER = DATA_CONNECTION_EMERGENCY_SERVICE + 1; 40 private static final int NUM_DATA_CONNECTION_TYPES = DATA_CONNECTION_OTHER + 1; 41 42 /* BatteryStatsManager API */ 43 private BatteryStatsManager mBatteryStatsManager; 44 ModemPowerMetrics(BatteryStatsManager batteryStatsManager)45 public ModemPowerMetrics(BatteryStatsManager batteryStatsManager) { 46 mBatteryStatsManager = batteryStatsManager; 47 } 48 49 /** 50 * Build ModemPowerStats proto 51 * @return ModemPowerStats 52 */ buildProto()53 public ModemPowerStats buildProto() { 54 ModemPowerStats m = new ModemPowerStats(); 55 CellularBatteryStats stats = getStats(); 56 if (stats != null) { 57 m.loggingDurationMs = stats.getLoggingDurationMillis(); 58 m.energyConsumedMah = stats.getEnergyConsumedMaMillis() 59 / ((double) DateUtils.HOUR_IN_MILLIS); 60 m.numPacketsTx = stats.getNumPacketsTx(); 61 m.cellularKernelActiveTimeMs = stats.getKernelActiveTimeMillis(); 62 63 long timeInVeryPoorRxSignalLevelMs = stats.getTimeInRxSignalStrengthLevelMicros( 64 CellSignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN); 65 if (timeInVeryPoorRxSignalLevelMs >= 0) { 66 m.timeInVeryPoorRxSignalLevelMs = timeInVeryPoorRxSignalLevelMs; 67 } 68 69 m.sleepTimeMs = stats.getSleepTimeMillis(); 70 m.idleTimeMs = stats.getIdleTimeMillis(); 71 m.rxTimeMs = stats.getRxTimeMillis(); 72 73 List<Long> txTimeMillis = new ArrayList<>(); 74 for (int i = 0; i < ModemActivityInfo.getNumTxPowerLevels(); i++) { 75 long t = stats.getTxTimeMillis(i); 76 if (t >= 0) { 77 txTimeMillis.add(t); 78 } 79 } 80 m.txTimeMs = txTimeMillis.stream().mapToLong(Long::longValue).toArray(); 81 82 m.numBytesTx = stats.getNumBytesTx(); 83 m.numPacketsRx = stats.getNumPacketsRx(); 84 m.numBytesRx = stats.getNumBytesRx(); 85 List<Long> timeInRatMicros = new ArrayList<>(); 86 for (int i = 0; i < NUM_DATA_CONNECTION_TYPES; i++) { 87 long tr = stats.getTimeInRatMicros(i); 88 if (tr >= 0) { 89 timeInRatMicros.add(tr); 90 } 91 } 92 m.timeInRatMs = timeInRatMicros.stream().mapToLong(Long::longValue).toArray(); 93 94 List<Long> rxSignalStrengthLevelMicros = new ArrayList<>(); 95 for (int i = 0; i < CellSignalStrength.getNumSignalStrengthLevels(); i++) { 96 long rx = stats.getTimeInRxSignalStrengthLevelMicros(i); 97 if (rx >= 0) { 98 rxSignalStrengthLevelMicros.add(rx); 99 } 100 } 101 m.timeInRxSignalStrengthLevelMs = rxSignalStrengthLevelMicros.stream().mapToLong( 102 Long::longValue).toArray(); 103 104 m.monitoredRailEnergyConsumedMah = stats.getMonitoredRailChargeConsumedMaMillis() 105 / ((double) DateUtils.HOUR_IN_MILLIS); 106 } 107 return m; 108 } 109 110 /** 111 * Get cellular stats from BatteryStatsManager 112 * @return CellularBatteryStats 113 */ getStats()114 private CellularBatteryStats getStats() { 115 if (mBatteryStatsManager == null) { 116 return null; 117 } 118 return mBatteryStatsManager.getCellularBatteryStats(); 119 } 120 } 121