SignalStrength.java revision cc6ff2bc6011294367d9a8c644f811449e952eb7
1/*
2 * Copyright (C) 2009 Qualcomm Innovation Center, Inc.  All Rights Reserved.
3 * Copyright (C) 2009 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package android.telephony;
19
20import android.os.Bundle;
21import android.os.Parcel;
22import android.os.Parcelable;
23import android.util.Log;
24
25/**
26 * Contains phone signal strength related information.
27 */
28public class SignalStrength implements Parcelable {
29
30    static final String LOG_TAG = "PHONE";
31
32    private int mGsmSignalStrength; // Valid values are (0-31, 99) as defined in TS 27.007 8.5
33    private int mGsmBitErrorRate;   // bit error rate (0-7, 99) as defined in TS 27.007 8.5
34    private int mCdmaDbm;   // This value is the RSSI value
35    private int mCdmaEcio;  // This value is the Ec/Io
36    private int mEvdoDbm;   // This value is the EVDO RSSI value
37    private int mEvdoEcio;  // This value is the EVDO Ec/Io
38    private int mEvdoSnr;   // Valid values are 0-8.  8 is the highest signal to noise ratio
39
40    private boolean isGsm; // This value is set by the ServiceStateTracker onSignalStrengthResult
41
42    /**
43     * Create a new SignalStrength from a intent notifier Bundle
44     *
45     * This method is used by PhoneStateIntentReceiver and maybe by
46     * external applications.
47     *
48     * @param m Bundle from intent notifier
49     * @return newly created SignalStrength
50     *
51     * @hide
52     */
53    public static SignalStrength newFromBundle(Bundle m) {
54        SignalStrength ret;
55        ret = new SignalStrength();
56        ret.setFromNotifierBundle(m);
57        return ret;
58    }
59
60    /**
61     * Empty constructor
62     *
63     * @hide
64     */
65    public SignalStrength() {
66        mGsmSignalStrength = 99;
67        mGsmBitErrorRate = -1;
68        mCdmaDbm = -1;
69        mCdmaEcio = -1;
70        mEvdoDbm = -1;
71        mEvdoEcio = -1;
72        mEvdoSnr = -1;
73        isGsm = true;
74    }
75
76    /**
77     * Constructor
78     *
79     * @hide
80     */
81    public SignalStrength(int gsmSignalStrength, int gsmBitErrorRate,
82            int cdmaDbm, int cdmaEcio,
83            int evdoDbm, int evdoEcio, int evdoSnr, boolean gsm) {
84        mGsmSignalStrength = gsmSignalStrength;
85        mGsmBitErrorRate = gsmBitErrorRate;
86        mCdmaDbm = cdmaDbm;
87        mCdmaEcio = cdmaEcio;
88        mEvdoDbm = evdoDbm;
89        mEvdoEcio = evdoEcio;
90        mEvdoSnr = evdoSnr;
91        isGsm = gsm;
92    }
93
94    /**
95     * Copy constructors
96     *
97     * @param s Source SignalStrength
98     *
99     * @hide
100     */
101    public SignalStrength(SignalStrength s) {
102        copyFrom(s);
103    }
104
105    /**
106     * @hide
107     */
108    protected void copyFrom(SignalStrength s) {
109        mGsmSignalStrength = s.mGsmSignalStrength;
110        mGsmBitErrorRate = s.mGsmBitErrorRate;
111        mCdmaDbm = s.mCdmaDbm;
112        mCdmaEcio = s.mCdmaEcio;
113        mEvdoDbm = s.mEvdoDbm;
114        mEvdoEcio = s.mEvdoEcio;
115        mEvdoSnr = s.mEvdoSnr;
116        isGsm = s.isGsm;
117    }
118
119    /**
120     * Construct a SignalStrength object from the given parcel.
121     *
122     * @hide
123     */
124    public SignalStrength(Parcel in) {
125        mGsmSignalStrength = in.readInt();
126        mGsmBitErrorRate = in.readInt();
127        mCdmaDbm = in.readInt();
128        mCdmaEcio = in.readInt();
129        mEvdoDbm = in.readInt();
130        mEvdoEcio = in.readInt();
131        mEvdoSnr = in.readInt();
132        isGsm = (in.readInt() != 0);
133    }
134
135    /**
136     * {@link Parcelable#writeToParcel}
137     */
138    public void writeToParcel(Parcel out, int flags) {
139        out.writeInt(mGsmSignalStrength);
140        out.writeInt(mGsmBitErrorRate);
141        out.writeInt(mCdmaDbm);
142        out.writeInt(mCdmaEcio);
143        out.writeInt(mEvdoDbm);
144        out.writeInt(mEvdoEcio);
145        out.writeInt(mEvdoSnr);
146        out.writeInt(isGsm ? 1 : 0);
147    }
148
149    /**
150     * {@link Parcelable#describeContents}
151     */
152    public int describeContents() {
153        return 0;
154    }
155
156    /**
157     * {@link Parcelable.Creator}
158     *
159     * @hide
160     */
161    public static final Parcelable.Creator<SignalStrength> CREATOR = new Parcelable.Creator() {
162        public SignalStrength createFromParcel(Parcel in) {
163            return new SignalStrength(in);
164        }
165
166        public SignalStrength[] newArray(int size) {
167            return new SignalStrength[size];
168        }
169    };
170
171    /**
172     * Get the GSM Signal Strength, valid values are (0-31, 99) as defined in TS 27.007 8.5
173     */
174    public int getGsmSignalStrength() {
175        return this.mGsmSignalStrength;
176    }
177
178    /**
179     * Get the GSM bit error rate (0-7, 99) as defined in TS 27.007 8.5
180     */
181    public int getGsmBitErrorRate() {
182        return this.mGsmBitErrorRate;
183    }
184
185    /**
186     * Get the CDMA RSSI value in dBm
187     */
188    public int getCdmaDbm() {
189        return this.mCdmaDbm;
190    }
191
192    /**
193     * Get the CDMA Ec/Io value in dB*10
194     */
195    public int getCdmaEcio() {
196        return this.mCdmaEcio;
197    }
198
199    /**
200     * Get the EVDO RSSI value in dBm
201     */
202    public int getEvdoDbm() {
203        return this.mEvdoDbm;
204    }
205
206    /**
207     * Get the EVDO Ec/Io value in dB*10
208     */
209    public int getEvdoEcio() {
210        return this.mEvdoEcio;
211    }
212
213    /**
214     * Get the signal to noise ratio. Valid values are 0-8. 8 is the highest.
215     */
216    public int getEvdoSnr() {
217        return this.mEvdoSnr;
218    }
219
220    /**
221     * @return true if this is for GSM
222     */
223    public boolean isGsm() {
224        return this.isGsm;
225    }
226
227    /**
228     * @return hash code
229     */
230    @Override
231    public int hashCode() {
232        return ((mGsmSignalStrength * 0x1234)
233                + mGsmBitErrorRate
234                + mCdmaDbm + mCdmaEcio
235                + mEvdoDbm + mEvdoEcio + mEvdoSnr
236                + (isGsm ? 1 : 0));
237    }
238
239    /**
240     * @return true if the signal strengths are the same
241     */
242    @Override
243    public boolean equals (Object o) {
244        SignalStrength s;
245
246        try {
247            s = (SignalStrength) o;
248        } catch (ClassCastException ex) {
249            return false;
250        }
251
252        if (o == null) {
253            return false;
254        }
255
256        return (mGsmSignalStrength == s.mGsmSignalStrength
257                && mGsmBitErrorRate == s.mGsmBitErrorRate
258                && mCdmaDbm == s.mCdmaDbm
259                && mCdmaEcio == s.mCdmaEcio
260                && mEvdoDbm == s.mEvdoDbm
261                && mEvdoEcio == s.mEvdoEcio
262                && mEvdoSnr == s.mEvdoSnr
263                && isGsm == s.isGsm);
264    }
265
266    /**
267     * @return string representation.
268     */
269    @Override
270    public String toString() {
271        return ("SignalStrength:"
272                + " " + mGsmSignalStrength
273                + " " + mGsmBitErrorRate
274                + " " + mCdmaDbm
275                + " " + mCdmaEcio
276                + " " + mEvdoDbm
277                + " " + mEvdoEcio
278                + " " + mEvdoSnr
279                + " " + (isGsm ? "gsm" : "cdma"));
280    }
281
282    /**
283     * Test whether two objects hold the same data values or both are null
284     *
285     * @param a first obj
286     * @param b second obj
287     * @return true if two objects equal or both are null
288     * @hide
289     */
290    private static boolean equalsHandlesNulls (Object a, Object b) {
291        return (a == null) ? (b == null) : a.equals (b);
292    }
293
294    /**
295     * Set SignalStrength based on intent notifier map
296     *
297     * @param m intent notifier map
298     * @hide
299     */
300    private void setFromNotifierBundle(Bundle m) {
301        mGsmSignalStrength = m.getInt("GsmSignalStrength");
302        mGsmBitErrorRate = m.getInt("GsmBitErrorRate");
303        mCdmaDbm = m.getInt("CdmaDbm");
304        mCdmaEcio = m.getInt("CdmaEcio");
305        mEvdoDbm = m.getInt("EvdoDbm");
306        mEvdoEcio = m.getInt("EvdoEcio");
307        mEvdoSnr = m.getInt("EvdoSnr");
308        isGsm = m.getBoolean("isGsm");
309    }
310
311    /**
312     * Set intent notifier Bundle based on SignalStrength
313     *
314     * @param m intent notifier Bundle
315     * @hide
316     */
317    public void fillInNotifierBundle(Bundle m) {
318        m.putInt("GsmSignalStrength", mGsmSignalStrength);
319        m.putInt("GsmBitErrorRate", mGsmBitErrorRate);
320        m.putInt("CdmaDbm", mCdmaDbm);
321        m.putInt("CdmaEcio", mCdmaEcio);
322        m.putInt("EvdoDbm", mEvdoDbm);
323        m.putInt("EvdoEcio", mEvdoEcio);
324        m.putInt("EvdoSnr", mEvdoSnr);
325        m.putBoolean("isGsm", Boolean.valueOf(isGsm));
326    }
327}
328