SignalStrength.java revision e9b06d754af03faf27012fbed1e7559ec1ba7c79
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 *
28 * @hide
29 */
30public class SignalStrength implements Parcelable {
31
32    static final String LOG_TAG = "PHONE";
33
34    private int mGsmSignalStrength; // Valid values are (0-31, 99) as defined in TS 27.007 8.5
35    private int mGsmBitErrorRate;   // bit error rate (0-7, 99) as defined in TS 27.007 8.5
36    private int mCdmaDbm;   // This value is the RSSI value
37    private int mCdmaEcio;  // This value is the Ec/Io
38    private int mEvdoDbm;   // This value is the EVDO RSSI value
39    private int mEvdoEcio;  // This value is the EVDO Ec/Io
40    private int mEvdoSnr;   // Valid values are 0-8.  8 is the highest signal to noise ratio
41
42    private boolean isGsm; // This value is set by the ServiceStateTracker onSignalStrengthResult
43
44    /**
45     * Create a new SignalStrength from a intent notifier Bundle
46     *
47     * This method is used by PhoneStateIntentReceiver and maybe by
48     * external applications.
49     *
50     * @param m Bundle from intent notifier
51     * @return newly created SignalStrength
52     *
53     */
54    public static SignalStrength newFromBundle(Bundle m) {
55        SignalStrength ret;
56        ret = new SignalStrength();
57        ret.setFromNotifierBundle(m);
58        return ret;
59    }
60
61    /**
62     * Empty constructor
63     *
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     */
80    public SignalStrength(int gsmSignalStrength, int gsmBitErrorRate,
81            int cdmaDbm, int cdmaEcio,
82            int evdoDbm, int evdoEcio, int evdoSnr, boolean gsm) {
83        mGsmSignalStrength = gsmSignalStrength;
84        mGsmBitErrorRate = gsmBitErrorRate;
85        mCdmaDbm = cdmaDbm;
86        mCdmaEcio = cdmaEcio;
87        mEvdoDbm = evdoDbm;
88        mEvdoEcio = evdoEcio;
89        mEvdoSnr = evdoSnr;
90        isGsm = gsm;
91    }
92
93    /**
94     * Copy constructors
95     *
96     * @param s Source SignalStrength
97     */
98    public SignalStrength(SignalStrength s) {
99        copyFrom(s);
100    }
101
102    /**
103     * @hide
104     */
105    protected void copyFrom(SignalStrength s) {
106        mGsmSignalStrength = s.mGsmSignalStrength;
107        mGsmBitErrorRate = s.mGsmBitErrorRate;
108        mCdmaDbm = s.mCdmaDbm;
109        mCdmaEcio = s.mCdmaEcio;
110        mEvdoDbm = s.mEvdoDbm;
111        mEvdoEcio = s.mEvdoEcio;
112        mEvdoSnr = s.mEvdoSnr;
113        isGsm = s.isGsm;
114    }
115
116    /**
117     * Construct a SignalStrength object from the given parcel.
118     */
119    public SignalStrength(Parcel in) {
120        mGsmSignalStrength = in.readInt();
121        mGsmBitErrorRate = in.readInt();
122        mCdmaDbm = in.readInt();
123        mCdmaEcio = in.readInt();
124        mEvdoDbm = in.readInt();
125        mEvdoEcio = in.readInt();
126        mEvdoSnr = in.readInt();
127        isGsm = (in.readInt() != 0);
128    }
129
130    public void writeToParcel(Parcel out, int flags) {
131        out.writeInt(mGsmSignalStrength);
132        out.writeInt(mGsmBitErrorRate);
133        out.writeInt(mCdmaDbm);
134        out.writeInt(mCdmaEcio);
135        out.writeInt(mEvdoDbm);
136        out.writeInt(mEvdoEcio);
137        out.writeInt(mEvdoSnr);
138        out.writeInt(isGsm ? 1 : 0);
139    }
140
141    public int describeContents() {
142        return 0;
143    }
144
145    public static final Parcelable.Creator<SignalStrength> CREATOR = new Parcelable.Creator() {
146        public SignalStrength createFromParcel(Parcel in) {
147            return new SignalStrength(in);
148        }
149
150        public SignalStrength[] newArray(int size) {
151            return new SignalStrength[size];
152        }
153    };
154
155    /**
156     * Get the GSM Signal Strength, valid values are (0-31, 99) as defined in TS 27.007 8.5
157     */
158    public int getGsmSignalStrength() {
159        return this.mGsmSignalStrength;
160    }
161
162    /**
163     * Get the GSM bit error rate (0-7, 99) as defined in TS 27.007 8.5
164     */
165    public int getGsmBitErrorRate() {
166        return this.mGsmBitErrorRate;
167    }
168
169    /**
170     * Get the CDMA RSSI value in dBm
171     */
172    public int getCdmaDbm() {
173        return this.mCdmaDbm;
174    }
175
176    /**
177     * Get the CDMA Ec/Io value in dB*10
178     */
179    public int getCdmaEcio() {
180        return this.mCdmaEcio;
181    }
182
183    /**
184     * Get the EVDO RSSI value in dBm
185     */
186    public int getEvdoDbm() {
187        return this.mEvdoDbm;
188    }
189
190    /**
191     * Get the EVDO Ec/Io value in dB*10
192     */
193    public int getEvdoEcio() {
194        return this.mEvdoEcio;
195    }
196
197    /**
198     * Get the signal to noise ratio. Valid values are 0-8. 8 is the highest.
199     */
200    public int getEvdoSnr() {
201        return this.mEvdoSnr;
202    }
203
204    /**
205     * @hide
206     */
207    public boolean isGsm() {
208        return this.isGsm;
209    }
210
211    /**
212     * @hide
213     */
214    @Override
215    public int hashCode() {
216        return ((mGsmSignalStrength * 0x1234)
217                + mGsmBitErrorRate
218                + mCdmaDbm + mCdmaEcio
219                + mEvdoDbm + mEvdoEcio + mEvdoSnr
220                + (isGsm ? 1 : 0));
221    }
222
223    /**
224     * @hide
225     */
226    @Override
227    public boolean equals (Object o) {
228        SignalStrength s;
229
230        try {
231            s = (SignalStrength) o;
232        } catch (ClassCastException ex) {
233            return false;
234        }
235
236        if (o == null) {
237            return false;
238        }
239
240        return (mGsmSignalStrength == s.mGsmSignalStrength
241                && mGsmBitErrorRate == s.mGsmBitErrorRate
242                && mCdmaDbm == s.mCdmaDbm
243                && mCdmaEcio == s.mCdmaEcio
244                && mEvdoDbm == s.mEvdoDbm
245                && mEvdoEcio == s.mEvdoEcio
246                && mEvdoSnr == s.mEvdoSnr
247                && isGsm == s.isGsm);
248    }
249
250    /**
251     * @hide
252     */
253    @Override
254    public String toString() {
255        return ("SignalStrength:"
256                + " " + mGsmSignalStrength
257                + " " + mGsmBitErrorRate
258                + " " + mCdmaDbm
259                + " " + mCdmaEcio
260                + " " + mEvdoDbm
261                + " " + mEvdoEcio
262                + " " + mEvdoSnr
263                + " " + (isGsm ? "gsm" : "cdma"));
264    }
265
266    /**
267     * Test whether two objects hold the same data values or both are null
268     *
269     * @param a first obj
270     * @param b second obj
271     * @return true if two objects equal or both are null
272     * @hide
273     */
274    private static boolean equalsHandlesNulls (Object a, Object b) {
275        return (a == null) ? (b == null) : a.equals (b);
276    }
277
278    /**
279     * Set SignalStrength based on intent notifier map
280     *
281     * @param m intent notifier map
282     * @hide
283     */
284    private void setFromNotifierBundle(Bundle m) {
285        mGsmSignalStrength = m.getInt("GsmSignalStrength");
286        mGsmBitErrorRate = m.getInt("GsmBitErrorRate");
287        mCdmaDbm = m.getInt("CdmaDbm");
288        mCdmaEcio = m.getInt("CdmaEcio");
289        mEvdoDbm = m.getInt("EvdoDbm");
290        mEvdoEcio = m.getInt("EvdoEcio");
291        mEvdoSnr = m.getInt("EvdoSnr");
292        isGsm = m.getBoolean("isGsm");
293    }
294
295    /**
296     * Set intent notifier Bundle based on SignalStrength
297     *
298     * @param m intent notifier Bundle
299     * @hide
300     */
301    public void fillInNotifierBundle(Bundle m) {
302        m.putInt("GsmSignalStrength", mGsmSignalStrength);
303        m.putInt("GsmBitErrorRate", mGsmBitErrorRate);
304        m.putInt("CdmaDbm", mCdmaDbm);
305        m.putInt("CdmaEcio", mCdmaEcio);
306        m.putInt("EvdoDbm", mEvdoDbm);
307        m.putInt("EvdoEcio", mEvdoEcio);
308        m.putInt("EvdoSnr", mEvdoSnr);
309        m.putBoolean("isGsm", Boolean.valueOf(isGsm));
310    }
311}
312