1 /* 2 * Copyright (C) 2016 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 android.os.connectivity; 17 18 import android.annotation.FlaggedApi; 19 import android.annotation.IntRange; 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.annotation.SuppressLint; 23 import android.annotation.SystemApi; 24 import android.annotation.TestApi; 25 import android.os.BatteryStats; 26 import android.os.Parcel; 27 import android.os.Parcelable; 28 import android.telephony.Annotation.NetworkType; 29 import android.telephony.CellSignalStrength; 30 import android.telephony.ModemActivityInfo; 31 32 import java.util.Arrays; 33 import java.util.Objects; 34 35 /** 36 * API for Cellular power stats 37 * 38 * @hide 39 */ 40 @android.ravenwood.annotation.RavenwoodKeepWholeClass 41 @SystemApi 42 public final class CellularBatteryStats implements Parcelable { 43 44 private final long mLoggingDurationMs; 45 private final long mKernelActiveTimeMs; 46 private final long mNumPacketsTx; 47 private final long mNumBytesTx; 48 private final long mNumPacketsRx; 49 private final long mNumBytesRx; 50 private final long mSleepTimeMs; 51 private final long mIdleTimeMs; 52 private final long mRxTimeMs; 53 private final long mEnergyConsumedMaMs; 54 private final long[] mTimeInRatMs; 55 private final long[] mTimeInRxSignalStrengthLevelMs; 56 private final long[] mTxTimeMs; 57 private final long mMonitoredRailChargeConsumedMaMs; 58 59 public static final @NonNull Parcelable.Creator<CellularBatteryStats> CREATOR = 60 new Parcelable.Creator<CellularBatteryStats>() { 61 public CellularBatteryStats createFromParcel(Parcel in) { 62 long loggingDurationMs = in.readLong(); 63 long kernelActiveTimeMs = in.readLong(); 64 long numPacketsTx = in.readLong(); 65 long numBytesTx = in.readLong(); 66 long numPacketsRx = in.readLong(); 67 long numBytesRx = in.readLong(); 68 long sleepTimeMs = in.readLong(); 69 long idleTimeMs = in.readLong(); 70 long rxTimeMs = in.readLong(); 71 long energyConsumedMaMs = in.readLong(); 72 long[] timeInRatMs = in.createLongArray(); 73 long[] timeInRxSignalStrengthLevelMs = in.createLongArray(); 74 long[] txTimeMs = in.createLongArray(); 75 long monitoredRailChargeConsumedMaMs = in.readLong(); 76 77 return new CellularBatteryStats(loggingDurationMs, kernelActiveTimeMs, 78 numPacketsTx, numBytesTx, numPacketsRx, numBytesRx, sleepTimeMs, 79 idleTimeMs, rxTimeMs, energyConsumedMaMs, timeInRatMs, 80 timeInRxSignalStrengthLevelMs, txTimeMs, 81 monitoredRailChargeConsumedMaMs); 82 } 83 84 public CellularBatteryStats[] newArray(int size) { 85 return new CellularBatteryStats[size]; 86 } 87 }; 88 89 /** 90 * This constructor should only be used in tests. 91 * @hide 92 */ 93 @FlaggedApi( 94 com.android.server.power.optimization.Flags.FLAG_STREAMLINED_CONNECTIVITY_BATTERY_STATS) 95 @TestApi CellularBatteryStats(long loggingDurationMs, long kernelActiveTimeMs, long numPacketsTx, long numBytesTx, long numPacketsRx, long numBytesRx, long sleepTimeMs, long idleTimeMs, long rxTimeMs, long energyConsumedMaMs, @NonNull long[] timeInRatMs, @NonNull long[] timeInRxSignalStrengthLevelMs, @NonNull long[] txTimeMs, long monitoredRailChargeConsumedMaMs)96 public CellularBatteryStats(long loggingDurationMs, long kernelActiveTimeMs, long numPacketsTx, 97 long numBytesTx, long numPacketsRx, long numBytesRx, long sleepTimeMs, long idleTimeMs, 98 long rxTimeMs, long energyConsumedMaMs, @NonNull long[] timeInRatMs, 99 @NonNull long[] timeInRxSignalStrengthLevelMs, @NonNull long[] txTimeMs, 100 long monitoredRailChargeConsumedMaMs) { 101 102 mLoggingDurationMs = loggingDurationMs; 103 mKernelActiveTimeMs = kernelActiveTimeMs; 104 mNumPacketsTx = numPacketsTx; 105 mNumBytesTx = numBytesTx; 106 mNumPacketsRx = numPacketsRx; 107 mNumBytesRx = numBytesRx; 108 mSleepTimeMs = sleepTimeMs; 109 mIdleTimeMs = idleTimeMs; 110 mRxTimeMs = rxTimeMs; 111 mEnergyConsumedMaMs = energyConsumedMaMs; 112 mTimeInRatMs = Arrays.copyOfRange( 113 timeInRatMs, 0, 114 Math.min(timeInRatMs.length, BatteryStats.NUM_DATA_CONNECTION_TYPES)); 115 mTimeInRxSignalStrengthLevelMs = Arrays.copyOfRange( 116 timeInRxSignalStrengthLevelMs, 0, 117 Math.min(timeInRxSignalStrengthLevelMs.length, 118 CellSignalStrength.getNumSignalStrengthLevels())); 119 mTxTimeMs = Arrays.copyOfRange( 120 txTimeMs, 0, 121 Math.min(txTimeMs.length, ModemActivityInfo.getNumTxPowerLevels())); 122 mMonitoredRailChargeConsumedMaMs = monitoredRailChargeConsumedMaMs; 123 } 124 125 @Override writeToParcel(@onNull Parcel out, int flags)126 public void writeToParcel(@NonNull Parcel out, int flags) { 127 out.writeLong(mLoggingDurationMs); 128 out.writeLong(mKernelActiveTimeMs); 129 out.writeLong(mNumPacketsTx); 130 out.writeLong(mNumBytesTx); 131 out.writeLong(mNumPacketsRx); 132 out.writeLong(mNumBytesRx); 133 out.writeLong(mSleepTimeMs); 134 out.writeLong(mIdleTimeMs); 135 out.writeLong(mRxTimeMs); 136 out.writeLong(mEnergyConsumedMaMs); 137 out.writeLongArray(mTimeInRatMs); 138 out.writeLongArray(mTimeInRxSignalStrengthLevelMs); 139 out.writeLongArray(mTxTimeMs); 140 out.writeLong(mMonitoredRailChargeConsumedMaMs); 141 } 142 143 @Override equals(@ullable Object other)144 public boolean equals(@Nullable Object other) { 145 if (!(other instanceof CellularBatteryStats)) return false; 146 if (other == this) return true; 147 CellularBatteryStats otherStats = (CellularBatteryStats) other; 148 return this.mLoggingDurationMs == otherStats.mLoggingDurationMs 149 && this.mKernelActiveTimeMs == otherStats.mKernelActiveTimeMs 150 && this.mNumPacketsTx == otherStats.mNumPacketsTx 151 && this.mNumBytesTx == otherStats.mNumBytesTx 152 && this.mNumPacketsRx == otherStats.mNumPacketsRx 153 && this.mNumBytesRx == otherStats.mNumBytesRx 154 && this.mSleepTimeMs == otherStats.mSleepTimeMs 155 && this.mIdleTimeMs == otherStats.mIdleTimeMs 156 && this.mRxTimeMs == otherStats.mRxTimeMs 157 && this.mEnergyConsumedMaMs == otherStats.mEnergyConsumedMaMs 158 && Arrays.equals(this.mTimeInRatMs, otherStats.mTimeInRatMs) 159 && Arrays.equals(this.mTimeInRxSignalStrengthLevelMs, 160 otherStats.mTimeInRxSignalStrengthLevelMs) 161 && Arrays.equals(this.mTxTimeMs, otherStats.mTxTimeMs) 162 && this.mMonitoredRailChargeConsumedMaMs 163 == otherStats.mMonitoredRailChargeConsumedMaMs; 164 } 165 166 @Override hashCode()167 public int hashCode() { 168 return Objects.hash(mLoggingDurationMs, mKernelActiveTimeMs, mNumPacketsTx, 169 mNumBytesTx, mNumPacketsRx, mNumBytesRx, mSleepTimeMs, mIdleTimeMs, 170 mRxTimeMs, mEnergyConsumedMaMs, Arrays.hashCode(mTimeInRatMs), 171 Arrays.hashCode(mTimeInRxSignalStrengthLevelMs), Arrays.hashCode(mTxTimeMs), 172 mMonitoredRailChargeConsumedMaMs); 173 } 174 175 /** 176 * Returns the duration for which these cellular stats were collected. 177 * 178 * @return Duration of stats collection in milliseconds. 179 */ getLoggingDurationMillis()180 public long getLoggingDurationMillis() { 181 return mLoggingDurationMs; 182 } 183 184 /** 185 * Returns the duration for which the kernel was active within 186 * {@link #getLoggingDurationMillis()}. 187 * 188 * @return Duration of kernel active time in milliseconds. 189 */ getKernelActiveTimeMillis()190 public long getKernelActiveTimeMillis() { 191 return mKernelActiveTimeMs; 192 } 193 194 /** 195 * Returns the number of packets transmitted over cellular within 196 * {@link #getLoggingDurationMillis()}. 197 * 198 * @return Number of packets transmitted. 199 */ getNumPacketsTx()200 public long getNumPacketsTx() { 201 return mNumPacketsTx; 202 } 203 204 /** 205 * Returns the number of packets received over cellular within 206 * {@link #getLoggingDurationMillis()}. 207 * 208 * @return Number of packets received. 209 */ getNumBytesTx()210 public long getNumBytesTx() { 211 return mNumBytesTx; 212 } 213 214 /** 215 * Returns the number of bytes transmitted over cellular within 216 * {@link #getLoggingDurationMillis()}. 217 * 218 * @return Number of bytes transmitted. 219 */ getNumPacketsRx()220 public long getNumPacketsRx() { 221 return mNumPacketsRx; 222 } 223 224 /** 225 * Returns the number of bytes received over cellular within 226 * {@link #getLoggingDurationMillis()}. 227 * 228 * @return Number of bytes received. 229 */ getNumBytesRx()230 public long getNumBytesRx() { 231 return mNumBytesRx; 232 } 233 234 /** 235 * Returns the duration for which the device was sleeping within 236 * {@link #getLoggingDurationMillis()}. 237 * 238 * @return Duration of sleep time in milliseconds. 239 */ getSleepTimeMillis()240 public long getSleepTimeMillis() { 241 return mSleepTimeMs; 242 } 243 244 /** 245 * Returns the duration for which the device was idle within 246 * {@link #getLoggingDurationMillis()}. 247 * 248 * @return Duration of idle time in milliseconds. 249 */ getIdleTimeMillis()250 public long getIdleTimeMillis() { 251 return mIdleTimeMs; 252 } 253 254 /** 255 * Returns the duration for which the device was receiving over cellular within 256 * {@link #getLoggingDurationMillis()}. 257 * 258 * @return Duration of cellular reception time in milliseconds. 259 */ getRxTimeMillis()260 public long getRxTimeMillis() { 261 return mRxTimeMs; 262 } 263 264 /** 265 * Returns an estimation of energy consumed by cellular chip within 266 * {@link #getLoggingDurationMillis()}. 267 * 268 * @return Energy consumed in milli-ampere milliseconds (mAmS). 269 */ getEnergyConsumedMaMillis()270 public long getEnergyConsumedMaMillis() { 271 return mEnergyConsumedMaMs; 272 } 273 274 /** 275 * Returns the time in microseconds that the phone has been running with 276 * the given data connection. 277 * 278 * @param networkType The network type to query. 279 * @return The amount of time the phone spends in the {@code networkType} network type. The 280 * unit is in microseconds. 281 */ 282 @SuppressLint("MethodNameUnits") getTimeInRatMicros(@etworkType int networkType)283 public long getTimeInRatMicros(@NetworkType int networkType) { 284 if (networkType >= mTimeInRatMs.length) { 285 return -1; 286 } 287 288 return mTimeInRatMs[networkType]; 289 } 290 291 /** 292 * Returns the time in microseconds that the phone has been running with 293 * the given signal strength. 294 * 295 * @param signalStrengthBin a single integer from 0 to 4 representing the general signal 296 * quality. 297 * @return Amount of time phone spends in specific cellular rx signal strength levels 298 * in microseconds. The index is signal strength bin. 299 */ 300 @SuppressLint("MethodNameUnits") getTimeInRxSignalStrengthLevelMicros( @ntRangefrom = CellSignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN, to = CellSignalStrength.SIGNAL_STRENGTH_GREAT) int signalStrengthBin)301 public long getTimeInRxSignalStrengthLevelMicros( 302 @IntRange(from = CellSignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN, 303 to = CellSignalStrength.SIGNAL_STRENGTH_GREAT) int signalStrengthBin) { 304 if (signalStrengthBin >= mTimeInRxSignalStrengthLevelMs.length) { 305 return -1; 306 } 307 return mTimeInRxSignalStrengthLevelMs[signalStrengthBin]; 308 } 309 310 /** 311 * Returns the duration for which the device was transmitting over cellular within 312 * {@link #getLoggingDurationMillis()}. 313 * 314 * @param level a single integer from 0 to 4 representing the Tx(transmit) power level. 315 * @return Duration of cellular transmission time for specific power level in milliseconds. 316 * 317 * Tx(transmit) power level. see power index @ModemActivityInfo.TxPowerLevel below 318 * <ul> 319 * <li> index 0 = tx_power < 0dBm. </li> 320 * <li> index 1 = 0dBm < tx_power < 5dBm. </li> 321 * <li> index 2 = 5dBm < tx_power < 15dBm. </li> 322 * <li> index 3 = 15dBm < tx_power < 20dBm. </li> 323 * <li> index 4 = tx_power > 20dBm. </li> 324 * </ul> 325 */ 326 @FlaggedApi( 327 com.android.server.power.optimization.Flags.FLAG_STREAMLINED_CONNECTIVITY_BATTERY_STATS) getTxTimeMillis( @ntRangefrom = ModemActivityInfo.TX_POWER_LEVEL_0, to = ModemActivityInfo.TX_POWER_LEVEL_4) int level)328 public long getTxTimeMillis( 329 @IntRange(from = ModemActivityInfo.TX_POWER_LEVEL_0, 330 to = ModemActivityInfo.TX_POWER_LEVEL_4) int level) { 331 if (level >= mTxTimeMs.length) { 332 return -1; 333 } 334 335 return mTxTimeMs[level]; 336 } 337 338 /** 339 * Returns the energy consumed by cellular chip within {@link #getLoggingDurationMillis()}. 340 * 341 * @return Energy consumed in milli-ampere milli-seconds (mAmS). 342 */ getMonitoredRailChargeConsumedMaMillis()343 public long getMonitoredRailChargeConsumedMaMillis() { 344 return mMonitoredRailChargeConsumedMaMs; 345 } 346 347 @Override describeContents()348 public int describeContents() { 349 return 0; 350 } 351 } 352