CellSignalStrengthCdma.java revision efbb8f337658f417a3af5f8e656eb5d23372596e
1/*
2 * Copyright (C) 2012 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;
21import android.telephony.Rlog;
22
23/**
24 * Signal strength related information.
25 */
26public final class CellSignalStrengthCdma extends CellSignalStrength implements Parcelable {
27
28    private static final String LOG_TAG = "CellSignalStrengthCdma";
29    private static final boolean DBG = false;
30
31    private int mCdmaDbm;   // This value is the RSSI value
32    private int mCdmaEcio;  // This value is the Ec/Io
33    private int mEvdoDbm;   // This value is the EVDO RSSI value
34    private int mEvdoEcio;  // This value is the EVDO Ec/Io
35    private int mEvdoSnr;   // Valid values are 0-8.  8 is the highest signal to noise ratio
36
37    /**
38     * Empty constructor
39     *
40     * @hide
41     */
42    public CellSignalStrengthCdma() {
43        setDefaultValues();
44    }
45
46    /**
47     * Constructor
48     *
49     * @hide
50     */
51    public CellSignalStrengthCdma(int cdmaDbm, int cdmaEcio, int evdoDbm, int evdoEcio,
52            int evdoSnr) {
53        initialize(cdmaDbm, cdmaEcio, evdoDbm, evdoEcio, evdoSnr);
54    }
55
56    /**
57     * Copy constructors
58     *
59     * @param s Source SignalStrength
60     *
61     * @hide
62     */
63    public CellSignalStrengthCdma(CellSignalStrengthCdma s) {
64        copyFrom(s);
65    }
66
67    /**
68     * Initialize all the values
69     *
70     * @param cdmaDbm
71     * @param cdmaEcio
72     * @param evdoDbm
73     * @param evdoEcio
74     * @param evdoSnr
75     *
76     * @hide
77     */
78    public void initialize(int cdmaDbm, int cdmaEcio, int evdoDbm, int evdoEcio, int evdoSnr) {
79        mCdmaDbm = cdmaDbm;
80        mCdmaEcio = cdmaEcio;
81        mEvdoDbm = evdoDbm;
82        mEvdoEcio = evdoEcio;
83        mEvdoSnr = evdoSnr;
84    }
85
86    /**
87     * @hide
88     */
89    protected void copyFrom(CellSignalStrengthCdma s) {
90        mCdmaDbm = s.mCdmaDbm;
91        mCdmaEcio = s.mCdmaEcio;
92        mEvdoDbm = s.mEvdoDbm;
93        mEvdoEcio = s.mEvdoEcio;
94        mEvdoSnr = s.mEvdoSnr;
95    }
96
97    /**
98     * @hide
99     */
100    @Override
101    public CellSignalStrengthCdma copy() {
102        return new CellSignalStrengthCdma(this);
103    }
104
105    /** @hide */
106    @Override
107    public void setDefaultValues() {
108        mCdmaDbm = Integer.MAX_VALUE;
109        mCdmaEcio = Integer.MAX_VALUE;
110        mEvdoDbm = Integer.MAX_VALUE;
111        mEvdoEcio = Integer.MAX_VALUE;
112        mEvdoSnr = Integer.MAX_VALUE;
113    }
114
115    /**
116     * Get signal level as an int from 0..4
117     */
118    @Override
119    public int getLevel() {
120        int level;
121
122        int cdmaLevel = getCdmaLevel();
123        int evdoLevel = getEvdoLevel();
124        if (evdoLevel == SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
125            /* We don't know evdo, use cdma */
126            level = getCdmaLevel();
127        } else if (cdmaLevel == SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
128            /* We don't know cdma, use evdo */
129            level = getEvdoLevel();
130        } else {
131            /* We know both, use the lowest level */
132            level = cdmaLevel < evdoLevel ? cdmaLevel : evdoLevel;
133        }
134        if (DBG) log("getLevel=" + level);
135        return level;
136    }
137
138    /**
139     * Get the signal level as an asu value between 0..97, 99 is unknown
140     */
141    @Override
142    public int getAsuLevel() {
143        final int cdmaDbm = getCdmaDbm();
144        final int cdmaEcio = getCdmaEcio();
145        int cdmaAsuLevel;
146        int ecioAsuLevel;
147
148        if (cdmaDbm == Integer.MAX_VALUE) cdmaAsuLevel = 99;
149        else if (cdmaDbm >= -75) cdmaAsuLevel = 16;
150        else if (cdmaDbm >= -82) cdmaAsuLevel = 8;
151        else if (cdmaDbm >= -90) cdmaAsuLevel = 4;
152        else if (cdmaDbm >= -95) cdmaAsuLevel = 2;
153        else if (cdmaDbm >= -100) cdmaAsuLevel = 1;
154        else cdmaAsuLevel = 99;
155
156        // Ec/Io are in dB*10
157        if (cdmaEcio == Integer.MAX_VALUE) ecioAsuLevel = 99;
158        else if (cdmaEcio >= -90) ecioAsuLevel = 16;
159        else if (cdmaEcio >= -100) ecioAsuLevel = 8;
160        else if (cdmaEcio >= -115) ecioAsuLevel = 4;
161        else if (cdmaEcio >= -130) ecioAsuLevel = 2;
162        else if (cdmaEcio >= -150) ecioAsuLevel = 1;
163        else ecioAsuLevel = 99;
164
165        int level = (cdmaAsuLevel < ecioAsuLevel) ? cdmaAsuLevel : ecioAsuLevel;
166        if (DBG) log("getAsuLevel=" + level);
167        return level;
168    }
169
170    /**
171     * Get cdma as level 0..4
172     */
173    public int getCdmaLevel() {
174        final int cdmaDbm = getCdmaDbm();
175        final int cdmaEcio = getCdmaEcio();
176        int levelDbm;
177        int levelEcio;
178
179        if (cdmaDbm == Integer.MAX_VALUE) levelDbm = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
180        else if (cdmaDbm >= -75) levelDbm = SIGNAL_STRENGTH_GREAT;
181        else if (cdmaDbm >= -85) levelDbm = SIGNAL_STRENGTH_GOOD;
182        else if (cdmaDbm >= -95) levelDbm = SIGNAL_STRENGTH_MODERATE;
183        else if (cdmaDbm >= -100) levelDbm = SIGNAL_STRENGTH_POOR;
184        else levelDbm = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
185
186        // Ec/Io are in dB*10
187        if (cdmaEcio == Integer.MAX_VALUE) levelEcio = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
188        else if (cdmaEcio >= -90) levelEcio = SIGNAL_STRENGTH_GREAT;
189        else if (cdmaEcio >= -110) levelEcio = SIGNAL_STRENGTH_GOOD;
190        else if (cdmaEcio >= -130) levelEcio = SIGNAL_STRENGTH_MODERATE;
191        else if (cdmaEcio >= -150) levelEcio = SIGNAL_STRENGTH_POOR;
192        else levelEcio = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
193
194        int level = (levelDbm < levelEcio) ? levelDbm : levelEcio;
195        if (DBG) log("getCdmaLevel=" + level);
196        return level;
197    }
198
199    /**
200     * Get Evdo as level 0..4
201     */
202    public int getEvdoLevel() {
203        int evdoDbm = getEvdoDbm();
204        int evdoSnr = getEvdoSnr();
205        int levelEvdoDbm;
206        int levelEvdoSnr;
207
208        if (evdoDbm == Integer.MAX_VALUE) levelEvdoDbm = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
209        else if (evdoDbm >= -65) levelEvdoDbm = SIGNAL_STRENGTH_GREAT;
210        else if (evdoDbm >= -75) levelEvdoDbm = SIGNAL_STRENGTH_GOOD;
211        else if (evdoDbm >= -90) levelEvdoDbm = SIGNAL_STRENGTH_MODERATE;
212        else if (evdoDbm >= -105) levelEvdoDbm = SIGNAL_STRENGTH_POOR;
213        else levelEvdoDbm = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
214
215        if (evdoSnr == Integer.MAX_VALUE) levelEvdoSnr = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
216        else if (evdoSnr >= 7) levelEvdoSnr = SIGNAL_STRENGTH_GREAT;
217        else if (evdoSnr >= 5) levelEvdoSnr = SIGNAL_STRENGTH_GOOD;
218        else if (evdoSnr >= 3) levelEvdoSnr = SIGNAL_STRENGTH_MODERATE;
219        else if (evdoSnr >= 1) levelEvdoSnr = SIGNAL_STRENGTH_POOR;
220        else levelEvdoSnr = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
221
222        int level = (levelEvdoDbm < levelEvdoSnr) ? levelEvdoDbm : levelEvdoSnr;
223        if (DBG) log("getEvdoLevel=" + level);
224        return level;
225    }
226
227    /**
228     * Get the signal strength as dBm
229     */
230    @Override
231    public int getDbm() {
232        int cdmaDbm = getCdmaDbm();
233        int evdoDbm = getEvdoDbm();
234
235        // Use the lower value to be conservative
236        return (cdmaDbm < evdoDbm) ? cdmaDbm : evdoDbm;
237    }
238
239    /**
240     * Get the CDMA RSSI value in dBm
241     */
242    public int getCdmaDbm() {
243        return mCdmaDbm;
244    }
245    /** @hide */
246    public void setCdmaDbm(int cdmaDbm) {
247        mCdmaDbm = cdmaDbm;
248    }
249
250    /**
251     * Get the CDMA Ec/Io value in dB*10
252     */
253    public int getCdmaEcio() {
254        return mCdmaEcio;
255    }
256    /** @hide */
257    public void setCdmaEcio(int cdmaEcio) {
258        mCdmaEcio = cdmaEcio;
259    }
260
261    /**
262     * Get the EVDO RSSI value in dBm
263     */
264    public int getEvdoDbm() {
265        return mEvdoDbm;
266    }
267    /** @hide */
268    public void setEvdoDbm(int evdoDbm) {
269        mEvdoDbm = evdoDbm;
270    }
271
272    /**
273     * Get the EVDO Ec/Io value in dB*10
274     */
275    public int getEvdoEcio() {
276        return mEvdoEcio;
277    }
278    /** @hide */
279    public void setEvdoEcio(int evdoEcio) {
280        mEvdoEcio = evdoEcio;
281    }
282
283    /**
284     * Get the signal to noise ratio. Valid values are 0-8. 8 is the highest.
285     */
286    public int getEvdoSnr() {
287        return mEvdoSnr;
288    }
289    /** @hide */
290    public void setEvdoSnr(int evdoSnr) {
291        mEvdoSnr = evdoSnr;
292    }
293
294    @Override
295    public int hashCode() {
296        int primeNum = 31;
297        return ((mCdmaDbm * primeNum) + (mCdmaEcio * primeNum)
298                + (mEvdoDbm * primeNum) + (mEvdoEcio * primeNum) + (mEvdoSnr * primeNum));
299    }
300
301    @Override
302    public boolean equals (Object o) {
303        CellSignalStrengthCdma s;
304
305        try {
306            s = (CellSignalStrengthCdma) o;
307        } catch (ClassCastException ex) {
308            return false;
309        }
310
311        if (o == null) {
312            return false;
313        }
314
315        return mCdmaDbm == s.mCdmaDbm
316                && mCdmaEcio == s.mCdmaEcio
317                && mEvdoDbm == s.mEvdoDbm
318                && mEvdoEcio == s.mEvdoEcio
319                && mEvdoSnr == s.mEvdoSnr;
320    }
321
322    /**
323     * @return string representation.
324     */
325    @Override
326    public String toString() {
327        return "CellSignalStrengthCdma:"
328                + " cdmaDbm=" + mCdmaDbm
329                + " cdmaEcio=" + mCdmaEcio
330                + " evdoDbm=" + mEvdoDbm
331                + " evdoEcio=" + mEvdoEcio
332                + " evdoSnr=" + mEvdoSnr;
333    }
334
335    /** Implement the Parcelable interface */
336    @Override
337    public void writeToParcel(Parcel dest, int flags) {
338        if (DBG) log("writeToParcel(Parcel, int): " + toString());
339        // Need to multiply CdmaDbm, CdmaEcio, EvdoDbm and EvdoEcio by -1
340        // to ensure consistency when reading values written here
341        // unless the value is invalid
342        dest.writeInt(mCdmaDbm * (mCdmaDbm != Integer.MAX_VALUE ? -1 : 1));
343        dest.writeInt(mCdmaEcio * (mCdmaEcio != Integer.MAX_VALUE ? -1 : 1));
344        dest.writeInt(mEvdoDbm * (mEvdoDbm != Integer.MAX_VALUE ? -1 : 1));
345        dest.writeInt(mEvdoEcio * (mEvdoEcio != Integer.MAX_VALUE ? -1 : 1));
346        dest.writeInt(mEvdoSnr);
347    }
348
349    /**
350     * Construct a SignalStrength object from the given parcel
351     * where the TYPE_CDMA token is already been processed.
352     */
353    private CellSignalStrengthCdma(Parcel in) {
354        // CdmaDbm, CdmaEcio, EvdoDbm and EvdoEcio are written into
355        // the parcel as positive values.
356        // Need to convert into negative values unless the value is invalid
357        mCdmaDbm = in.readInt();
358        if (mCdmaDbm != Integer.MAX_VALUE) mCdmaDbm *= -1;
359        mCdmaEcio = in.readInt();
360        if (mCdmaEcio != Integer.MAX_VALUE) mCdmaEcio *= -1;
361        mEvdoDbm = in.readInt();
362        if (mEvdoDbm != Integer.MAX_VALUE) mEvdoDbm *= -1;
363        mEvdoEcio = in.readInt();
364        if (mEvdoEcio != Integer.MAX_VALUE) mEvdoEcio *= -1;
365        mEvdoSnr = in.readInt();
366        if (DBG) log("CellSignalStrengthCdma(Parcel): " + toString());
367    }
368
369    /** Implement the Parcelable interface */
370    @Override
371    public int describeContents() {
372        return 0;
373    }
374
375    /** Implement the Parcelable interface */
376    @SuppressWarnings("hiding")
377    public static final Parcelable.Creator<CellSignalStrengthCdma> CREATOR =
378            new Parcelable.Creator<CellSignalStrengthCdma>() {
379        @Override
380        public CellSignalStrengthCdma createFromParcel(Parcel in) {
381            return new CellSignalStrengthCdma(in);
382        }
383
384        @Override
385        public CellSignalStrengthCdma[] newArray(int size) {
386            return new CellSignalStrengthCdma[size];
387        }
388    };
389
390    /**
391     * log
392     */
393    private static void log(String s) {
394        Rlog.w(LOG_TAG, s);
395    }
396}
397