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.os.BatteryStats;
19 import android.os.Parcel;
20 import android.os.Parcelable;
21 
22 import android.telephony.ModemActivityInfo;
23 import android.telephony.SignalStrength;
24 
25 import java.util.Arrays;
26 
27 /**
28  * API for Cellular power stats
29  *
30  * @hide
31  */
32 public final class CellularBatteryStats implements Parcelable {
33 
34   private long mLoggingDurationMs;
35   private long mKernelActiveTimeMs;
36   private long mNumPacketsTx;
37   private long mNumBytesTx;
38   private long mNumPacketsRx;
39   private long mNumBytesRx;
40   private long mSleepTimeMs;
41   private long mIdleTimeMs;
42   private long mRxTimeMs;
43   private long mEnergyConsumedMaMs;
44   private long[] mTimeInRatMs;
45   private long[] mTimeInRxSignalStrengthLevelMs;
46   private long[] mTxTimeMs;
47 
48   public static final Parcelable.Creator<CellularBatteryStats> CREATOR = new
49       Parcelable.Creator<CellularBatteryStats>() {
50         public CellularBatteryStats createFromParcel(Parcel in) {
51           return new CellularBatteryStats(in);
52         }
53 
54         public CellularBatteryStats[] newArray(int size) {
55           return new CellularBatteryStats[size];
56         }
57       };
58 
CellularBatteryStats()59   public CellularBatteryStats() {
60     initialize();
61   }
62 
writeToParcel(Parcel out, int flags)63   public void writeToParcel(Parcel out, int flags) {
64     out.writeLong(mLoggingDurationMs);
65     out.writeLong(mKernelActiveTimeMs);
66     out.writeLong(mNumPacketsTx);
67     out.writeLong(mNumBytesTx);
68     out.writeLong(mNumPacketsRx);
69     out.writeLong(mNumBytesRx);
70     out.writeLong(mSleepTimeMs);
71     out.writeLong(mIdleTimeMs);
72     out.writeLong(mRxTimeMs);
73     out.writeLong(mEnergyConsumedMaMs);
74     out.writeLongArray(mTimeInRatMs);
75     out.writeLongArray(mTimeInRxSignalStrengthLevelMs);
76     out.writeLongArray(mTxTimeMs);
77   }
78 
readFromParcel(Parcel in)79   public void readFromParcel(Parcel in) {
80     mLoggingDurationMs = in.readLong();
81     mKernelActiveTimeMs = in.readLong();
82     mNumPacketsTx = in.readLong();
83     mNumBytesTx = in.readLong();
84     mNumPacketsRx = in.readLong();
85     mNumBytesRx = in.readLong();
86     mSleepTimeMs = in.readLong();
87     mIdleTimeMs = in.readLong();
88     mRxTimeMs = in.readLong();
89     mEnergyConsumedMaMs = in.readLong();
90     in.readLongArray(mTimeInRatMs);
91     in.readLongArray(mTimeInRxSignalStrengthLevelMs);
92     in.readLongArray(mTxTimeMs);
93   }
94 
getLoggingDurationMs()95   public long getLoggingDurationMs() {
96     return mLoggingDurationMs;
97   }
98 
getKernelActiveTimeMs()99   public long getKernelActiveTimeMs() {
100     return mKernelActiveTimeMs;
101   }
102 
getNumPacketsTx()103   public long getNumPacketsTx() {
104     return mNumPacketsTx;
105   }
106 
getNumBytesTx()107   public long getNumBytesTx() {
108     return mNumBytesTx;
109   }
110 
getNumPacketsRx()111   public long getNumPacketsRx() {
112     return mNumPacketsRx;
113   }
114 
getNumBytesRx()115   public long getNumBytesRx() {
116     return mNumBytesRx;
117   }
118 
getSleepTimeMs()119   public long getSleepTimeMs() {
120     return mSleepTimeMs;
121   }
122 
getIdleTimeMs()123   public long getIdleTimeMs() {
124     return mIdleTimeMs;
125   }
126 
getRxTimeMs()127   public long getRxTimeMs() {
128     return mRxTimeMs;
129   }
130 
getEnergyConsumedMaMs()131   public long getEnergyConsumedMaMs() {
132     return mEnergyConsumedMaMs;
133   }
134 
getTimeInRatMs()135   public long[] getTimeInRatMs() {
136     return mTimeInRatMs;
137   }
138 
getTimeInRxSignalStrengthLevelMs()139   public long[] getTimeInRxSignalStrengthLevelMs() {
140     return mTimeInRxSignalStrengthLevelMs;
141   }
142 
getTxTimeMs()143   public long[] getTxTimeMs() {
144     return mTxTimeMs;
145   }
146 
setLoggingDurationMs(long t)147   public void setLoggingDurationMs(long t) {
148     mLoggingDurationMs = t;
149     return;
150   }
151 
setKernelActiveTimeMs(long t)152   public void setKernelActiveTimeMs(long t) {
153     mKernelActiveTimeMs = t;
154     return;
155   }
156 
setNumPacketsTx(long n)157   public void setNumPacketsTx(long n) {
158     mNumPacketsTx = n;
159     return;
160   }
161 
setNumBytesTx(long b)162   public void setNumBytesTx(long b) {
163     mNumBytesTx = b;
164     return;
165   }
166 
setNumPacketsRx(long n)167   public void setNumPacketsRx(long n) {
168     mNumPacketsRx = n;
169     return;
170   }
171 
setNumBytesRx(long b)172   public void setNumBytesRx(long b) {
173     mNumBytesRx = b;
174     return;
175   }
176 
setSleepTimeMs(long t)177   public void setSleepTimeMs(long t) {
178     mSleepTimeMs = t;
179     return;
180   }
181 
setIdleTimeMs(long t)182   public void setIdleTimeMs(long t) {
183     mIdleTimeMs = t;
184     return;
185   }
186 
setRxTimeMs(long t)187   public void setRxTimeMs(long t) {
188     mRxTimeMs = t;
189     return;
190   }
191 
setEnergyConsumedMaMs(long e)192   public void setEnergyConsumedMaMs(long e) {
193     mEnergyConsumedMaMs = e;
194     return;
195   }
196 
setTimeInRatMs(long[] t)197   public void setTimeInRatMs(long[] t) {
198     mTimeInRatMs = Arrays.copyOfRange(t, 0,
199         Math.min(t.length, BatteryStats.NUM_DATA_CONNECTION_TYPES));
200     return;
201   }
202 
setTimeInRxSignalStrengthLevelMs(long[] t)203   public void setTimeInRxSignalStrengthLevelMs(long[] t) {
204     mTimeInRxSignalStrengthLevelMs = Arrays.copyOfRange(t, 0,
205         Math.min(t.length, SignalStrength.NUM_SIGNAL_STRENGTH_BINS));
206     return;
207   }
208 
setTxTimeMs(long[] t)209   public void setTxTimeMs(long[] t) {
210     mTxTimeMs = Arrays.copyOfRange(t, 0, Math.min(t.length, ModemActivityInfo.TX_POWER_LEVELS));
211     return;
212   }
213 
describeContents()214   public int describeContents() {
215     return 0;
216   }
217 
CellularBatteryStats(Parcel in)218   private CellularBatteryStats(Parcel in) {
219     initialize();
220     readFromParcel(in);
221   }
222 
initialize()223   private void initialize() {
224     mLoggingDurationMs = 0;
225     mKernelActiveTimeMs = 0;
226     mNumPacketsTx = 0;
227     mNumBytesTx = 0;
228     mNumPacketsRx = 0;
229     mNumBytesRx = 0;
230     mSleepTimeMs = 0;
231     mIdleTimeMs = 0;
232     mRxTimeMs = 0;
233     mEnergyConsumedMaMs = 0;
234     mTimeInRatMs = new long[BatteryStats.NUM_DATA_CONNECTION_TYPES];
235     Arrays.fill(mTimeInRatMs, 0);
236     mTimeInRxSignalStrengthLevelMs = new long[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
237     Arrays.fill(mTimeInRxSignalStrengthLevelMs, 0);
238     mTxTimeMs = new long[ModemActivityInfo.TX_POWER_LEVELS];
239     Arrays.fill(mTxTimeMs, 0);
240     return;
241   }
242 }