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 
17 package android.telephony;
18 
19 import android.os.Parcel;
20 import android.os.Parcelable;
21 import android.telephony.Rlog;
22 
23 /**
24  * Signal strength related information.
25  */
26 public 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      */
CellSignalStrengthCdma()42     public CellSignalStrengthCdma() {
43         setDefaultValues();
44     }
45 
46     /**
47      * Constructor
48      *
49      * @hide
50      */
CellSignalStrengthCdma(int cdmaDbm, int cdmaEcio, int evdoDbm, int evdoEcio, int evdoSnr)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      */
CellSignalStrengthCdma(CellSignalStrengthCdma s)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      */
initialize(int cdmaDbm, int cdmaEcio, int evdoDbm, int evdoEcio, int evdoSnr)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      */
copyFrom(CellSignalStrengthCdma s)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
copy()101     public CellSignalStrengthCdma copy() {
102         return new CellSignalStrengthCdma(this);
103     }
104 
105     /** @hide */
106     @Override
setDefaultValues()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
getLevel()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 >= -75) cdmaAsuLevel = 16;
149         else if (cdmaDbm >= -82) cdmaAsuLevel = 8;
150         else if (cdmaDbm >= -90) cdmaAsuLevel = 4;
151         else if (cdmaDbm >= -95) cdmaAsuLevel = 2;
152         else if (cdmaDbm >= -100) cdmaAsuLevel = 1;
153         else cdmaAsuLevel = 99;
154 
155         // Ec/Io are in dB*10
156         if (cdmaEcio >= -90) ecioAsuLevel = 16;
157         else if (cdmaEcio >= -100) ecioAsuLevel = 8;
158         else if (cdmaEcio >= -115) ecioAsuLevel = 4;
159         else if (cdmaEcio >= -130) ecioAsuLevel = 2;
160         else if (cdmaEcio >= -150) ecioAsuLevel = 1;
161         else ecioAsuLevel = 99;
162 
163         int level = (cdmaAsuLevel < ecioAsuLevel) ? cdmaAsuLevel : ecioAsuLevel;
164         if (DBG) log("getAsuLevel=" + level);
165         return level;
166     }
167 
168     /**
169      * Get cdma as level 0..4
170      */
getCdmaLevel()171     public int getCdmaLevel() {
172         final int cdmaDbm = getCdmaDbm();
173         final int cdmaEcio = getCdmaEcio();
174         int levelDbm;
175         int levelEcio;
176 
177         if (cdmaDbm >= -75) levelDbm = SIGNAL_STRENGTH_GREAT;
178         else if (cdmaDbm >= -85) levelDbm = SIGNAL_STRENGTH_GOOD;
179         else if (cdmaDbm >= -95) levelDbm = SIGNAL_STRENGTH_MODERATE;
180         else if (cdmaDbm >= -100) levelDbm = SIGNAL_STRENGTH_POOR;
181         else levelDbm = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
182 
183         // Ec/Io are in dB*10
184         if (cdmaEcio >= -90) levelEcio = SIGNAL_STRENGTH_GREAT;
185         else if (cdmaEcio >= -110) levelEcio = SIGNAL_STRENGTH_GOOD;
186         else if (cdmaEcio >= -130) levelEcio = SIGNAL_STRENGTH_MODERATE;
187         else if (cdmaEcio >= -150) levelEcio = SIGNAL_STRENGTH_POOR;
188         else levelEcio = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
189 
190         int level = (levelDbm < levelEcio) ? levelDbm : levelEcio;
191         if (DBG) log("getCdmaLevel=" + level);
192         return level;
193     }
194 
195     /**
196      * Get Evdo as level 0..4
197      */
getEvdoLevel()198     public int getEvdoLevel() {
199         int evdoDbm = getEvdoDbm();
200         int evdoSnr = getEvdoSnr();
201         int levelEvdoDbm;
202         int levelEvdoSnr;
203 
204         if (evdoDbm >= -65) levelEvdoDbm = SIGNAL_STRENGTH_GREAT;
205         else if (evdoDbm >= -75) levelEvdoDbm = SIGNAL_STRENGTH_GOOD;
206         else if (evdoDbm >= -90) levelEvdoDbm = SIGNAL_STRENGTH_MODERATE;
207         else if (evdoDbm >= -105) levelEvdoDbm = SIGNAL_STRENGTH_POOR;
208         else levelEvdoDbm = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
209 
210         if (evdoSnr >= 7) levelEvdoSnr = SIGNAL_STRENGTH_GREAT;
211         else if (evdoSnr >= 5) levelEvdoSnr = SIGNAL_STRENGTH_GOOD;
212         else if (evdoSnr >= 3) levelEvdoSnr = SIGNAL_STRENGTH_MODERATE;
213         else if (evdoSnr >= 1) levelEvdoSnr = SIGNAL_STRENGTH_POOR;
214         else levelEvdoSnr = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
215 
216         int level = (levelEvdoDbm < levelEvdoSnr) ? levelEvdoDbm : levelEvdoSnr;
217         if (DBG) log("getEvdoLevel=" + level);
218         return level;
219     }
220 
221     /**
222      * Get the signal strength as dBm
223      */
224     @Override
getDbm()225     public int getDbm() {
226         int cdmaDbm = getCdmaDbm();
227         int evdoDbm = getEvdoDbm();
228 
229         // Use the lower value to be conservative
230         return (cdmaDbm < evdoDbm) ? cdmaDbm : evdoDbm;
231     }
232 
233     /**
234      * Get the CDMA RSSI value in dBm
235      */
getCdmaDbm()236     public int getCdmaDbm() {
237         return mCdmaDbm;
238     }
239     /** @hide */
setCdmaDbm(int cdmaDbm)240     public void setCdmaDbm(int cdmaDbm) {
241         mCdmaDbm = cdmaDbm;
242     }
243 
244     /**
245      * Get the CDMA Ec/Io value in dB*10
246      */
getCdmaEcio()247     public int getCdmaEcio() {
248         return mCdmaEcio;
249     }
250     /** @hide */
setCdmaEcio(int cdmaEcio)251     public void setCdmaEcio(int cdmaEcio) {
252         mCdmaEcio = cdmaEcio;
253     }
254 
255     /**
256      * Get the EVDO RSSI value in dBm
257      */
getEvdoDbm()258     public int getEvdoDbm() {
259         return mEvdoDbm;
260     }
261     /** @hide */
setEvdoDbm(int evdoDbm)262     public void setEvdoDbm(int evdoDbm) {
263         mEvdoDbm = evdoDbm;
264     }
265 
266     /**
267      * Get the EVDO Ec/Io value in dB*10
268      */
getEvdoEcio()269     public int getEvdoEcio() {
270         return mEvdoEcio;
271     }
272     /** @hide */
setEvdoEcio(int evdoEcio)273     public void setEvdoEcio(int evdoEcio) {
274         mEvdoEcio = evdoEcio;
275     }
276 
277     /**
278      * Get the signal to noise ratio. Valid values are 0-8. 8 is the highest.
279      */
getEvdoSnr()280     public int getEvdoSnr() {
281         return mEvdoSnr;
282     }
283     /** @hide */
setEvdoSnr(int evdoSnr)284     public void setEvdoSnr(int evdoSnr) {
285         mEvdoSnr = evdoSnr;
286     }
287 
288     @Override
hashCode()289     public int hashCode() {
290         int primeNum = 31;
291         return ((mCdmaDbm * primeNum) + (mCdmaEcio * primeNum)
292                 + (mEvdoDbm * primeNum) + (mEvdoEcio * primeNum) + (mEvdoSnr * primeNum));
293     }
294 
295     @Override
equals(Object o)296     public boolean equals (Object o) {
297         CellSignalStrengthCdma s;
298 
299         try {
300             s = (CellSignalStrengthCdma) o;
301         } catch (ClassCastException ex) {
302             return false;
303         }
304 
305         if (o == null) {
306             return false;
307         }
308 
309         return mCdmaDbm == s.mCdmaDbm
310                 && mCdmaEcio == s.mCdmaEcio
311                 && mEvdoDbm == s.mEvdoDbm
312                 && mEvdoEcio == s.mEvdoEcio
313                 && mEvdoSnr == s.mEvdoSnr;
314     }
315 
316     /**
317      * @return string representation.
318      */
319     @Override
toString()320     public String toString() {
321         return "CellSignalStrengthCdma:"
322                 + " cdmaDbm=" + mCdmaDbm
323                 + " cdmaEcio=" + mCdmaEcio
324                 + " evdoDbm=" + mEvdoDbm
325                 + " evdoEcio=" + mEvdoEcio
326                 + " evdoSnr=" + mEvdoSnr;
327     }
328 
329     /** Implement the Parcelable interface */
330     @Override
writeToParcel(Parcel dest, int flags)331     public void writeToParcel(Parcel dest, int flags) {
332         if (DBG) log("writeToParcel(Parcel, int): " + toString());
333         // Need to multiply CdmaDbm, CdmaEcio, EvdoDbm and EvdoEcio by -1
334         // to ensure consistency when reading values written here
335         dest.writeInt(mCdmaDbm * -1);
336         dest.writeInt(mCdmaEcio * -1);
337         dest.writeInt(mEvdoDbm * -1);
338         dest.writeInt(mEvdoEcio * -1);
339         dest.writeInt(mEvdoSnr);
340     }
341 
342     /**
343      * Construct a SignalStrength object from the given parcel
344      * where the TYPE_CDMA token is already been processed.
345      */
CellSignalStrengthCdma(Parcel in)346     private CellSignalStrengthCdma(Parcel in) {
347         // CdmaDbm, CdmaEcio, EvdoDbm and EvdoEcio are written into
348         // the parcel as positive values.
349         // Need to convert into negative values
350         mCdmaDbm = in.readInt() * -1;
351         mCdmaEcio = in.readInt() * -1;
352         mEvdoDbm = in.readInt() * -1;
353         mEvdoEcio = in.readInt() * -1;
354         mEvdoSnr = in.readInt();
355         if (DBG) log("CellSignalStrengthCdma(Parcel): " + toString());
356     }
357 
358     /** Implement the Parcelable interface */
359     @Override
describeContents()360     public int describeContents() {
361         return 0;
362     }
363 
364     /** Implement the Parcelable interface */
365     @SuppressWarnings("hiding")
366     public static final Parcelable.Creator<CellSignalStrengthCdma> CREATOR =
367             new Parcelable.Creator<CellSignalStrengthCdma>() {
368         @Override
369         public CellSignalStrengthCdma createFromParcel(Parcel in) {
370             return new CellSignalStrengthCdma(in);
371         }
372 
373         @Override
374         public CellSignalStrengthCdma[] newArray(int size) {
375             return new CellSignalStrengthCdma[size];
376         }
377     };
378 
379     /**
380      * log
381      */
log(String s)382     private static void log(String s) {
383         Rlog.w(LOG_TAG, s);
384     }
385 }
386