ServiceState.java revision 390de220248d05ccb9dc10a197700ad3c1595937
1/*
2 * Copyright (C) 2006 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.Bundle;
20import android.os.Parcel;
21import android.os.Parcelable;
22import android.util.Log;
23
24/**
25 * Contains phone state and service related information.
26 *
27 * The following phone information is included in returned ServiceState:
28 *
29 * <ul>
30 *   <li>Service state: IN_SERVICE, OUT_OF_SERVICE, EMERGENCY_ONLY, POWER_OFF
31 *   <li>Roaming indicator
32 *   <li>Operator name, short name and numeric id
33 *   <li>Network selection mode
34 * </ul>
35 */
36public class ServiceState implements Parcelable {
37
38    static final String LOG_TAG = "PHONE";
39
40    /**
41     * Normal operation condition, the phone is registered
42     * with an operator either in home network or in roaming.
43     */
44    public static final int STATE_IN_SERVICE = 0;
45
46    /**
47     * Phone is not registered with any operator, the phone
48     * can be currently searching a new operator to register to, or not
49     * searching to registration at all, or registration is denied, or radio
50     * signal is not available.
51     */
52    public static final int STATE_OUT_OF_SERVICE = 1;
53
54    /**
55     * The phone is registered and locked.  Only emergency numbers are allowed. {@more}
56     */
57    public static final int STATE_EMERGENCY_ONLY = 2;
58
59    /**
60     * Radio of telephony is explicitly powered off.
61     */
62    public static final int STATE_POWER_OFF = 3;
63
64
65    /**
66     * Available radio technologies for GSM, UMTS and CDMA.
67     */
68    /** @hide */
69    public static final int RADIO_TECHNOLOGY_UNKNOWN = 0;
70    /** @hide */
71    public static final int RADIO_TECHNOLOGY_GPRS = 1;
72    /** @hide */
73    public static final int RADIO_TECHNOLOGY_EDGE = 2;
74    /** @hide */
75    public static final int RADIO_TECHNOLOGY_UMTS = 3;
76    /** @hide */
77    public static final int RADIO_TECHNOLOGY_IS95A = 4;
78    /** @hide */
79    public static final int RADIO_TECHNOLOGY_IS95B = 5;
80    /** @hide */
81    public static final int RADIO_TECHNOLOGY_1xRTT = 6;
82    /** @hide */
83    public static final int RADIO_TECHNOLOGY_EVDO_0 = 7;
84    /** @hide */
85    public static final int RADIO_TECHNOLOGY_EVDO_A = 8;
86    /** @hide */
87    public static final int RADIO_TECHNOLOGY_HSDPA = 9;
88    /** @hide */
89    public static final int RADIO_TECHNOLOGY_HSUPA = 10;
90    /** @hide */
91    public static final int RADIO_TECHNOLOGY_HSPA = 11;
92    /** @hide */
93    public static final int RADIO_TECHNOLOGY_EVDO_B = 12;
94
95    /**
96     * Available registration states for GSM, UMTS and CDMA.
97     */
98    /** @hide */
99    public static final int REGISTRATION_STATE_NOT_REGISTERED_AND_NOT_SEARCHING = 0;
100    /** @hide */
101    public static final int REGISTRATION_STATE_HOME_NETWORK = 1;
102    /** @hide */
103    public static final int REGISTRATION_STATE_NOT_REGISTERED_AND_SEARCHING = 2;
104    /** @hide */
105    public static final int REGISTRATION_STATE_REGISTRATION_DENIED = 3;
106    /** @hide */
107    public static final int REGISTRATION_STATE_UNKNOWN = 4;
108    /** @hide */
109    public static final int REGISTRATION_STATE_ROAMING = 5;
110
111    private int mState = STATE_OUT_OF_SERVICE;
112    private boolean mRoaming;
113    private String mOperatorAlphaLong;
114    private String mOperatorAlphaShort;
115    private String mOperatorNumeric;
116    private boolean mIsManualNetworkSelection;
117
118    //***** CDMA
119    private int mRadioTechnology;
120    private boolean mCssIndicator;
121    private int mNetworkId;
122    private int mSystemId;
123    private int mCdmaRoamingIndicator;
124    private int mCdmaDefaultRoamingIndicator;
125    private int mCdmaEriIconIndex;
126    private int mCdmaEriIconMode;
127
128    /**
129     * Create a new ServiceState from a intent notifier Bundle
130     *
131     * This method is used by PhoneStateIntentReceiver and maybe by
132     * external applications.
133     *
134     * @param m Bundle from intent notifier
135     * @return newly created ServiceState
136     * @hide
137     */
138    public static ServiceState newFromBundle(Bundle m) {
139        ServiceState ret;
140        ret = new ServiceState();
141        ret.setFromNotifierBundle(m);
142        return ret;
143    }
144
145    /**
146     * Empty constructor
147     */
148    public ServiceState() {
149    }
150
151    /**
152     * Copy constructors
153     *
154     * @param s Source service state
155     */
156    public ServiceState(ServiceState s) {
157        copyFrom(s);
158    }
159
160    protected void copyFrom(ServiceState s) {
161        mState = s.mState;
162        mRoaming = s.mRoaming;
163        mOperatorAlphaLong = s.mOperatorAlphaLong;
164        mOperatorAlphaShort = s.mOperatorAlphaShort;
165        mOperatorNumeric = s.mOperatorNumeric;
166        mIsManualNetworkSelection = s.mIsManualNetworkSelection;
167        mRadioTechnology = s.mRadioTechnology;
168        mCssIndicator = s.mCssIndicator;
169        mNetworkId = s.mNetworkId;
170        mSystemId = s.mSystemId;
171        mCdmaRoamingIndicator = s.mCdmaRoamingIndicator;
172        mCdmaDefaultRoamingIndicator = s.mCdmaDefaultRoamingIndicator;
173        mCdmaEriIconIndex = s.mCdmaEriIconIndex;
174        mCdmaEriIconMode = s.mCdmaEriIconMode;
175    }
176
177    /**
178     * Construct a ServiceState object from the given parcel.
179     */
180    public ServiceState(Parcel in) {
181        mState = in.readInt();
182        mRoaming = in.readInt() != 0;
183        mOperatorAlphaLong = in.readString();
184        mOperatorAlphaShort = in.readString();
185        mOperatorNumeric = in.readString();
186        mIsManualNetworkSelection = in.readInt() != 0;
187        mRadioTechnology = in.readInt();
188        mCssIndicator = (in.readInt() != 0);
189        mNetworkId = in.readInt();
190        mSystemId = in.readInt();
191        mCdmaRoamingIndicator = in.readInt();
192        mCdmaDefaultRoamingIndicator = in.readInt();
193        mCdmaEriIconIndex = in.readInt();
194        mCdmaEriIconMode = in.readInt();
195    }
196
197    public void writeToParcel(Parcel out, int flags) {
198        out.writeInt(mState);
199        out.writeInt(mRoaming ? 1 : 0);
200        out.writeString(mOperatorAlphaLong);
201        out.writeString(mOperatorAlphaShort);
202        out.writeString(mOperatorNumeric);
203        out.writeInt(mIsManualNetworkSelection ? 1 : 0);
204        out.writeInt(mRadioTechnology);
205        out.writeInt(mCssIndicator ? 1 : 0);
206        out.writeInt(mNetworkId);
207        out.writeInt(mSystemId);
208        out.writeInt(mCdmaRoamingIndicator);
209        out.writeInt(mCdmaDefaultRoamingIndicator);
210        out.writeInt(mCdmaEriIconIndex);
211        out.writeInt(mCdmaEriIconMode);
212    }
213
214    public int describeContents() {
215        return 0;
216    }
217
218    public static final Parcelable.Creator<ServiceState> CREATOR =
219            new Parcelable.Creator<ServiceState>() {
220        public ServiceState createFromParcel(Parcel in) {
221            return new ServiceState(in);
222        }
223
224        public ServiceState[] newArray(int size) {
225            return new ServiceState[size];
226        }
227    };
228
229    /**
230     * Get current service state of phone
231     *
232     * @see #STATE_IN_SERVICE
233     * @see #STATE_OUT_OF_SERVICE
234     * @see #STATE_EMERGENCY_ONLY
235     * @see #STATE_POWER_OFF
236     */
237    public int getState() {
238        return mState;
239    }
240
241    /**
242     * Get current roaming indicator of phone
243     * (note: not just decoding from TS 27.007 7.2)
244     *
245     * @return true if TS 27.007 7.2 roaming is true
246     *              and ONS is different from SPN
247     *
248     */
249    public boolean getRoaming() {
250        return mRoaming;
251    }
252
253    /**
254     * @hide
255     */
256    public int getCdmaRoamingIndicator(){
257        return this.mCdmaRoamingIndicator;
258    }
259
260    /**
261     * @hide
262     */
263    public int getCdmaDefaultRoamingIndicator(){
264        return this.mCdmaDefaultRoamingIndicator;
265    }
266
267    /**
268     * @hide
269     */
270    public int getCdmaEriIconIndex() {
271        return this.mCdmaEriIconIndex;
272    }
273
274    /**
275     * @hide
276     */
277    public int getCdmaEriIconMode() {
278        return this.mCdmaEriIconMode;
279    }
280
281    /**
282     * Get current registered operator name in long alphanumeric format.
283     *
284     * In GSM/UMTS, long format can be up to 16 characters long.
285     * In CDMA, returns the ERI text, if set. Otherwise, returns the ONS.
286     *
287     * @return long name of operator, null if unregistered or unknown
288     */
289    public String getOperatorAlphaLong() {
290        return mOperatorAlphaLong;
291    }
292
293    /**
294     * Get current registered operator name in short alphanumeric format.
295     *
296     * In GSM/UMTS, short format can be up to 8 characters long.
297     *
298     * @return short name of operator, null if unregistered or unknown
299     */
300    public String getOperatorAlphaShort() {
301        return mOperatorAlphaShort;
302    }
303
304    /**
305     * Get current registered operator numeric id.
306     *
307     * In GSM/UMTS, numeric format is 3 digit country code plus 2 or 3 digit
308     * network code.
309     *
310     * The country code can be decoded using
311     * {@link com.android.internal.telephony.MccTable#countryCodeForMcc(int)}.
312     *
313     * @return numeric format of operator, null if unregistered or unknown
314     */
315    public String getOperatorNumeric() {
316        return mOperatorNumeric;
317    }
318
319    /**
320     * Get current network selection mode.
321     *
322     * @return true if manual mode, false if automatic mode
323     */
324    public boolean getIsManualSelection() {
325        return mIsManualNetworkSelection;
326    }
327
328    @Override
329    public int hashCode() {
330        return ((mState * 0x1234)
331                + (mRoaming ? 1 : 0)
332                + (mIsManualNetworkSelection ? 1 : 0)
333                + ((null == mOperatorAlphaLong) ? 0 : mOperatorAlphaLong.hashCode())
334                + ((null == mOperatorAlphaShort) ? 0 : mOperatorAlphaShort.hashCode())
335                + ((null == mOperatorNumeric) ? 0 : mOperatorNumeric.hashCode())
336                + mCdmaRoamingIndicator
337                + mCdmaDefaultRoamingIndicator);
338    }
339
340    @Override
341    public boolean equals (Object o) {
342        ServiceState s;
343
344        try {
345            s = (ServiceState) o;
346        } catch (ClassCastException ex) {
347            return false;
348        }
349
350        if (o == null) {
351            return false;
352        }
353
354        return (mState == s.mState
355                && mRoaming == s.mRoaming
356                && mIsManualNetworkSelection == s.mIsManualNetworkSelection
357                && equalsHandlesNulls(mOperatorAlphaLong, s.mOperatorAlphaLong)
358                && equalsHandlesNulls(mOperatorAlphaShort, s.mOperatorAlphaShort)
359                && equalsHandlesNulls(mOperatorNumeric, s.mOperatorNumeric)
360                && equalsHandlesNulls(mRadioTechnology, s.mRadioTechnology)
361                && equalsHandlesNulls(mCssIndicator, s.mCssIndicator)
362                && equalsHandlesNulls(mNetworkId, s.mNetworkId)
363                && equalsHandlesNulls(mSystemId, s.mSystemId)
364                && equalsHandlesNulls(mCdmaRoamingIndicator, s.mCdmaRoamingIndicator)
365                && equalsHandlesNulls(mCdmaDefaultRoamingIndicator,
366                        s.mCdmaDefaultRoamingIndicator));
367    }
368
369    @Override
370    public String toString() {
371        String radioTechnology = new String("Error in radioTechnology");
372        switch(this.mRadioTechnology) {
373        case 0:
374            radioTechnology = "Unknown";
375            break;
376        case 1:
377            radioTechnology = "GPRS";
378            break;
379        case 2:
380            radioTechnology = "EDGE";
381            break;
382        case 3:
383            radioTechnology = "UMTS";
384            break;
385        case 4:
386            radioTechnology = "IS95A";
387            break;
388        case 5:
389            radioTechnology = "IS95B";
390            break;
391        case 6:
392            radioTechnology = "1xRTT";
393            break;
394        case 7:
395            radioTechnology = "EvDo rev. 0";
396            break;
397        case 8:
398            radioTechnology = "EvDo rev. A";
399            break;
400        case 9:
401            radioTechnology = "HSDPA";
402            break;
403        case 10:
404            radioTechnology = "HSUPA";
405            break;
406        case 11:
407            radioTechnology = "HSPA";
408            break;
409        case 12:
410            radioTechnology = "EvDo rev. B";
411            break;
412        default:
413            Log.w(LOG_TAG, "mRadioTechnology variable out of range.");
414        break;
415        }
416
417        return (mState + " " + (mRoaming ? "roaming" : "home")
418                + " " + mOperatorAlphaLong
419                + " " + mOperatorAlphaShort
420                + " " + mOperatorNumeric
421                + " " + (mIsManualNetworkSelection ? "(manual)" : "")
422                + " " + radioTechnology
423                + " " + (mCssIndicator ? "CSS supported" : "CSS not supported")
424                + " " + mNetworkId
425                + " " + mSystemId
426                + "RoamInd: " + mCdmaRoamingIndicator
427                + "DefRoamInd: " + mCdmaDefaultRoamingIndicator);
428    }
429
430    public void setStateOutOfService() {
431        mState = STATE_OUT_OF_SERVICE;
432        mRoaming = false;
433        mOperatorAlphaLong = null;
434        mOperatorAlphaShort = null;
435        mOperatorNumeric = null;
436        mIsManualNetworkSelection = false;
437        mRadioTechnology = 0;
438        mCssIndicator = false;
439        mNetworkId = -1;
440        mSystemId = -1;
441        mCdmaRoamingIndicator = -1;
442        mCdmaDefaultRoamingIndicator = -1;
443        mCdmaEriIconIndex = -1;
444        mCdmaEriIconMode = -1;
445    }
446
447    // TODO - can't this be combined with the above method?
448    public void setStateOff() {
449        mState = STATE_POWER_OFF;
450        mRoaming = false;
451        mOperatorAlphaLong = null;
452        mOperatorAlphaShort = null;
453        mOperatorNumeric = null;
454        mIsManualNetworkSelection = false;
455        mRadioTechnology = 0;
456        mCssIndicator = false;
457        mNetworkId = -1;
458        mSystemId = -1;
459        mCdmaRoamingIndicator = -1;
460        mCdmaDefaultRoamingIndicator = -1;
461        mCdmaEriIconIndex = -1;
462        mCdmaEriIconMode = -1;
463    }
464
465    public void setState(int state) {
466        mState = state;
467    }
468
469    public void setRoaming(boolean roaming) {
470        mRoaming = roaming;
471    }
472
473    /**
474     * @hide
475     */
476    public void setCdmaRoamingIndicator(int roaming) {
477        this.mCdmaRoamingIndicator = roaming;
478    }
479
480    /**
481     * @hide
482     */
483    public void setCdmaDefaultRoamingIndicator (int roaming) {
484        this.mCdmaDefaultRoamingIndicator = roaming;
485    }
486
487    /**
488     * @hide
489     */
490    public void setCdmaEriIconIndex(int index) {
491        this.mCdmaEriIconIndex = index;
492    }
493
494    /**
495     * @hide
496     */
497    public void setCdmaEriIconMode(int mode) {
498        this.mCdmaEriIconMode = mode;
499    }
500
501    public void setOperatorName(String longName, String shortName, String numeric) {
502        mOperatorAlphaLong = longName;
503        mOperatorAlphaShort = shortName;
504        mOperatorNumeric = numeric;
505    }
506
507    /**
508     * In CDMA, mOperatorAlphaLong can be set from the ERI text.
509     * This is done from the CDMAPhone and not from the CdmaServiceStateTracker.
510     *
511     * @hide
512     */
513    public void setCdmaEriText(String longName) {
514        mOperatorAlphaLong = longName;
515    }
516
517    public void setIsManualSelection(boolean isManual) {
518        mIsManualNetworkSelection = isManual;
519    }
520
521    /**
522     * Test whether two objects hold the same data values or both are null.
523     *
524     * @param a first obj
525     * @param b second obj
526     * @return true if two objects equal or both are null
527     */
528    private static boolean equalsHandlesNulls (Object a, Object b) {
529        return (a == null) ? (b == null) : a.equals (b);
530    }
531
532    /**
533     * Set ServiceState based on intent notifier map.
534     *
535     * @param m intent notifier map
536     * @hide
537     */
538    private void setFromNotifierBundle(Bundle m) {
539        mState = m.getInt("state");
540        mRoaming = m.getBoolean("roaming");
541        mOperatorAlphaLong = m.getString("operator-alpha-long");
542        mOperatorAlphaShort = m.getString("operator-alpha-short");
543        mOperatorNumeric = m.getString("operator-numeric");
544        mIsManualNetworkSelection = m.getBoolean("manual");
545        mRadioTechnology = m.getInt("radioTechnology");
546        mCssIndicator = m.getBoolean("cssIndicator");
547        mNetworkId = m.getInt("networkId");
548        mSystemId = m.getInt("systemId");
549        mCdmaRoamingIndicator = m.getInt("cdmaRoamingIndicator");
550        mCdmaDefaultRoamingIndicator = m.getInt("cdmaDefaultRoamingIndicator");
551    }
552
553    /**
554     * Set intent notifier Bundle based on service state.
555     *
556     * @param m intent notifier Bundle
557     * @hide
558     */
559    public void fillInNotifierBundle(Bundle m) {
560        m.putInt("state", mState);
561        m.putBoolean("roaming", Boolean.valueOf(mRoaming));
562        m.putString("operator-alpha-long", mOperatorAlphaLong);
563        m.putString("operator-alpha-short", mOperatorAlphaShort);
564        m.putString("operator-numeric", mOperatorNumeric);
565        m.putBoolean("manual", Boolean.valueOf(mIsManualNetworkSelection));
566        m.putInt("radioTechnology", mRadioTechnology);
567        m.putBoolean("cssIndicator", mCssIndicator);
568        m.putInt("networkId", mNetworkId);
569        m.putInt("systemId", mSystemId);
570        m.putInt("cdmaRoamingIndicator", mCdmaRoamingIndicator);
571        m.putInt("cdmaDefaultRoamingIndicator", mCdmaDefaultRoamingIndicator);
572    }
573
574    //***** CDMA
575    /** @hide */
576    public void setRadioTechnology(int state) {
577        this.mRadioTechnology = state;
578    }
579
580    /** @hide */
581    public void setCssIndicator(int css) {
582        this.mCssIndicator = (css != 0);
583    }
584
585    /** @hide */
586    public void setSystemAndNetworkId(int systemId, int networkId) {
587        this.mSystemId = systemId;
588        this.mNetworkId = networkId;
589    }
590
591    /** @hide */
592    public int getRadioTechnology() {
593        return this.mRadioTechnology;
594    }
595
596    /** @hide */
597    public int getCssIndicator() {
598        return this.mCssIndicator ? 1 : 0;
599    }
600
601    /** @hide */
602    public int getNetworkId() {
603        return this.mNetworkId;
604    }
605
606    /** @hide */
607    public int getSystemId() {
608        return this.mSystemId;
609    }
610}
611