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 */
16
17package android.telephony;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22import java.util.Arrays;
23
24/**
25 * Reports modem activity information
26 * @hide
27 */
28public class ModemActivityInfo implements Parcelable {
29    /**
30     * Tx power index
31     * index 0 = tx_power < 0dBm
32     * index 1 = 0dBm < tx_power < 5dBm
33     * index 2 = 5dBm < tx_power < 15dBm
34     * index 3 = 15dBm < tx_power < 20dBm
35     * index 4 = tx_power > 20dBm
36     */
37    public static final int TX_POWER_LEVELS = 5;
38
39    private final long mTimestamp;
40    private final int mSleepTimeMs;
41    private final int mIdleTimeMs;
42    private final int [] mTxTimeMs = new int[TX_POWER_LEVELS];
43    private final int mRxTimeMs;
44    private final int mEnergyUsed;
45
46    public ModemActivityInfo(long timestamp, int sleepTimeMs, int idleTimeMs,
47                        int[] txTimeMs, int rxTimeMs, int energyUsed) {
48        mTimestamp = timestamp;
49        mSleepTimeMs = sleepTimeMs;
50        mIdleTimeMs = idleTimeMs;
51        if (txTimeMs != null) {
52            System.arraycopy(txTimeMs, 0, mTxTimeMs, 0, Math.min(txTimeMs.length, TX_POWER_LEVELS));
53        }
54        mRxTimeMs = rxTimeMs;
55        mEnergyUsed = energyUsed;
56    }
57
58    @Override
59    public String toString() {
60        return "ModemActivityInfo{"
61            + " mTimestamp=" + mTimestamp
62            + " mSleepTimeMs=" + mSleepTimeMs
63            + " mIdleTimeMs=" + mIdleTimeMs
64            + " mTxTimeMs[]=" + Arrays.toString(mTxTimeMs)
65            + " mRxTimeMs=" + mRxTimeMs
66            + " mEnergyUsed=" + mEnergyUsed
67            + "}";
68    }
69
70    public int describeContents() {
71        return 0;
72    }
73
74    public static final Parcelable.Creator<ModemActivityInfo> CREATOR =
75            new Parcelable.Creator<ModemActivityInfo>() {
76        public ModemActivityInfo createFromParcel(Parcel in) {
77            long timestamp = in.readLong();
78            int sleepTimeMs = in.readInt();
79            int idleTimeMs = in.readInt();
80            int[] txTimeMs = new int[TX_POWER_LEVELS];
81            for (int i = 0; i < TX_POWER_LEVELS; i++) {
82                txTimeMs[i] = in.readInt();
83            }
84            int rxTimeMs = in.readInt();
85            int energyUsed = in.readInt();
86            return new ModemActivityInfo(timestamp, sleepTimeMs, idleTimeMs,
87                                txTimeMs, rxTimeMs, energyUsed);
88        }
89
90        public ModemActivityInfo[] newArray(int size) {
91            return new ModemActivityInfo[size];
92        }
93    };
94
95    public void writeToParcel(Parcel dest, int flags) {
96        dest.writeLong(mTimestamp);
97        dest.writeInt(mSleepTimeMs);
98        dest.writeInt(mIdleTimeMs);
99        for (int i = 0; i < TX_POWER_LEVELS; i++) {
100            dest.writeInt(mTxTimeMs[i]);
101        }
102        dest.writeInt(mRxTimeMs);
103        dest.writeInt(mEnergyUsed);
104    }
105
106    /**
107     * @return timestamp of record creation
108     */
109    public long getTimestamp() {
110        return mTimestamp;
111    }
112
113    /**
114     * @return tx time in ms. It's an array of tx times
115     * with each index...
116     */
117    public int [] getTxTimeMillis() {
118        return mTxTimeMs;
119    }
120
121    /**
122     * @return sleep time in ms.
123     */
124    public int getSleepTimeMillis() {
125        return mSleepTimeMs;
126    }
127
128    /**
129     * @return idle time in ms.
130     */
131    public int getIdleTimeMillis() {
132        return mIdleTimeMs;
133    }
134
135    /**
136     * @return rx time in ms.
137     */
138    public int getRxTimeMillis() {
139        return mRxTimeMs;
140    }
141
142    /**
143     * product of current(mA), voltage(V) and time(ms)
144     * @return energy used
145     */
146    public int getEnergyUsed () {
147        return mEnergyUsed;
148    }
149
150    /**
151     * @return if the record is valid
152     */
153    public boolean isValid() {
154        for (int txVal : getTxTimeMillis()) {
155            if(txVal < 0) {
156                return false;
157            }
158        }
159
160        return ((getIdleTimeMillis() >= 0) && (getSleepTimeMillis() >= 0)
161                && (getRxTimeMillis() >= 0) && (getEnergyUsed() >= 0) && !isEmpty());
162    }
163
164    private boolean isEmpty() {
165        for (int txVal : getTxTimeMillis()) {
166            if(txVal != 0) {
167                return false;
168            }
169        }
170
171        return ((getIdleTimeMillis() == 0) && (getSleepTimeMillis() == 0)
172                && (getRxTimeMillis() == 0) && (getEnergyUsed() == 0));
173    }
174}
175