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