ServiceState.java revision 0a5174a6e947d5cbeb8846a1273a90b6de065cbf
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 explictly 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 = new Parcelable.Creator() {
219        public ServiceState createFromParcel(Parcel in) {
220            return new ServiceState(in);
221        }
222
223        public ServiceState[] newArray(int size) {
224            return new ServiceState[size];
225        }
226    };
227
228    /**
229     * Get current servcie state of phone
230     *
231     * @see #STATE_IN_SERVICE
232     * @see #STATE_OUT_OF_SERVICE
233     * @see #STATE_EMERGENCY_ONLY
234     * @see #STATE_POWER_OFF
235     */
236    public int getState() {
237        return mState;
238    }
239
240    /**
241     * Get current roaming indicator of phone
242     * (note: not just decoding from TS 27.007 7.2)
243     *
244     * @return true if TS 27.007 7.2 roaming is true
245     *              and ONS is different from SPN
246     *
247     */
248    public boolean getRoaming() {
249        return mRoaming;
250    }
251
252    /**
253     * @hide
254     */
255    public int getCdmaRoamingIndicator(){
256        return this.mCdmaRoamingIndicator;
257    }
258
259    /**
260     * @hide
261     */
262    public int getCdmaDefaultRoamingIndicator(){
263        return this.mCdmaDefaultRoamingIndicator;
264    }
265
266    /**
267     * @hide
268     */
269    public int getCdmaEriIconIndex() {
270        return this.mCdmaEriIconIndex;
271    }
272
273    /**
274     * @hide
275     */
276    public int getCdmaEriIconMode() {
277        return this.mCdmaEriIconMode;
278    }
279
280    /**
281     * Get current registered operator name in long alphanumeric format
282     *
283     * In GSM/UMTS, long format can be upto 16 characters long
284     * In CDMA, returns the ERI text, if set, otherwise the ONS
285     *
286     * @return long name of operator, null if unregistered or unknown
287     */
288    public String getOperatorAlphaLong() {
289        return mOperatorAlphaLong;
290    }
291
292    /**
293     * Get current registered operator name in short lphanumeric format
294     *
295     * In GSM/UMST, short format can be upto 8 characters long
296     *
297     * @return short name of operator, null if unregistered or unknown
298     */
299    public String getOperatorAlphaShort() {
300        return mOperatorAlphaShort;
301    }
302
303    /**
304     * Get current registered operator numeric id
305     *
306     * In GSM/UMTS, numeric format is 3 digit country code plus 2 or 3 digit
307     * network code
308     *
309     * The country code can be decoded using MccTable.countryCodeForMcc()
310     *
311     * @return numeric format of operator, null if unregistered or unknown
312     */
313    public String getOperatorNumeric() {
314        return mOperatorNumeric;
315    }
316
317    /**
318     * Get current network selection mode
319     *
320     * @return true if manual mode, false if automatic mode
321     */
322    public boolean getIsManualSelection() {
323        return mIsManualNetworkSelection;
324    }
325
326    @Override
327    public int hashCode() {
328        return ((mState * 0x1234)
329                + (mRoaming ? 1 : 0)
330                + (mIsManualNetworkSelection ? 1 : 0)
331                + ((null == mOperatorAlphaLong) ? 0 : mOperatorAlphaLong.hashCode())
332                + ((null == mOperatorAlphaShort) ? 0 : mOperatorAlphaShort.hashCode())
333                + ((null == mOperatorNumeric) ? 0 : mOperatorNumeric.hashCode())
334                + mCdmaRoamingIndicator
335                + mCdmaDefaultRoamingIndicator);
336    }
337
338    @Override
339    public boolean equals (Object o) {
340        ServiceState s;
341
342        try {
343            s = (ServiceState) o;
344        } catch (ClassCastException ex) {
345            return false;
346        }
347
348        if (o == null) {
349            return false;
350        }
351
352        return (mState == s.mState
353                && mRoaming == s.mRoaming
354                && mIsManualNetworkSelection == s.mIsManualNetworkSelection
355                && equalsHandlesNulls(mOperatorAlphaLong, s.mOperatorAlphaLong)
356                && equalsHandlesNulls(mOperatorAlphaShort, s.mOperatorAlphaShort)
357                && equalsHandlesNulls(mOperatorNumeric, s.mOperatorNumeric)
358                && equalsHandlesNulls(mRadioTechnology, s.mRadioTechnology)
359                && equalsHandlesNulls(mCssIndicator, s.mCssIndicator)
360                && equalsHandlesNulls(mNetworkId, s.mNetworkId)
361                && equalsHandlesNulls(mSystemId, s.mSystemId)
362                && equalsHandlesNulls(mCdmaRoamingIndicator, s.mCdmaRoamingIndicator)
363                && equalsHandlesNulls(mCdmaDefaultRoamingIndicator,
364                        s.mCdmaDefaultRoamingIndicator));
365    }
366
367    @Override
368    public String toString() {
369        String radioTechnology = new String("Error in radioTechnology");
370        switch(this.mRadioTechnology) {
371        case 0:
372            radioTechnology = "Unknown";
373            break;
374        case 1:
375            radioTechnology = "GPRS";
376            break;
377        case 2:
378            radioTechnology = "EDGE";
379            break;
380        case 3:
381            radioTechnology = "UMTS";
382            break;
383        case 4:
384            radioTechnology = "IS95A";
385            break;
386        case 5:
387            radioTechnology = "IS95B";
388            break;
389        case 6:
390            radioTechnology = "1xRTT";
391            break;
392        case 7:
393            radioTechnology = "EvDo rev. 0";
394            break;
395        case 8:
396            radioTechnology = "EvDo rev. A";
397            break;
398        case 9:
399            radioTechnology = "HSDPA";
400            break;
401        case 10:
402            radioTechnology = "HSUPA";
403            break;
404        case 11:
405            radioTechnology = "HSPA";
406            break;
407        case 12:
408            radioTechnology = "EvDo rev. B";
409            break;
410        default:
411            Log.w(LOG_TAG, "mRadioTechnology variable out of range.");
412        break;
413        }
414
415        return (mState + " " + (mRoaming ? "roaming" : "home")
416                + " " + mOperatorAlphaLong
417                + " " + mOperatorAlphaShort
418                + " " + mOperatorNumeric
419                + " " + (mIsManualNetworkSelection ? "(manual)" : "")
420                + " " + radioTechnology
421                + " " + (mCssIndicator ? "CSS supported" : "CSS not supported")
422                + " " + mNetworkId
423                + " " + mSystemId
424                + "RoamInd: " + mCdmaRoamingIndicator
425                + "DefRoamInd: " + mCdmaDefaultRoamingIndicator);
426    }
427
428    public void setStateOutOfService() {
429        mState = STATE_OUT_OF_SERVICE;
430        mRoaming = false;
431        mOperatorAlphaLong = null;
432        mOperatorAlphaShort = null;
433        mOperatorNumeric = null;
434        mIsManualNetworkSelection = false;
435        mRadioTechnology = 0;
436        mCssIndicator = false;
437        mNetworkId = -1;
438        mSystemId = -1;
439        mCdmaRoamingIndicator = -1;
440        mCdmaDefaultRoamingIndicator = -1;
441        mCdmaEriIconIndex = -1;
442        mCdmaEriIconMode = -1;
443    }
444
445    // TODO - can't this be combined with the above func..
446    public void setStateOff() {
447        mState = STATE_POWER_OFF;
448        mRoaming = false;
449        mOperatorAlphaLong = null;
450        mOperatorAlphaShort = null;
451        mOperatorNumeric = null;
452        mIsManualNetworkSelection = false;
453        mRadioTechnology = 0;
454        mCssIndicator = false;
455        mNetworkId = -1;
456        mSystemId = -1;
457        mCdmaRoamingIndicator = -1;
458        mCdmaDefaultRoamingIndicator = -1;
459        mCdmaEriIconIndex = -1;
460        mCdmaEriIconMode = -1;
461    }
462
463    public void setState(int state) {
464        mState = state;
465    }
466
467    public void setRoaming(boolean roaming) {
468        mRoaming = roaming;
469    }
470
471    /**
472     * @hide
473     */
474    public void setCdmaRoamingIndicator(int roaming) {
475        this.mCdmaRoamingIndicator = roaming;
476    }
477
478    /**
479     * @hide
480     */
481    public void setCdmaDefaultRoamingIndicator (int roaming) {
482        this.mCdmaDefaultRoamingIndicator = roaming;
483    }
484
485    /**
486     * @hide
487     */
488    public void setCdmaEriIconIndex(int index) {
489        this.mCdmaEriIconIndex = index;
490    }
491
492    /**
493     * @hide
494     */
495    public void setCdmaEriIconMode(int mode) {
496        this.mCdmaEriIconMode = mode;
497    }
498
499    public void setOperatorName(String longName, String shortName, String numeric) {
500        mOperatorAlphaLong = longName;
501        mOperatorAlphaShort = shortName;
502        mOperatorNumeric = numeric;
503    }
504
505    /**
506     * In CDMA mOperatorAlphaLong can be set from the ERI
507     * text, this is done from the CDMAPhone and not from the CdmaServiceStateTracker
508     *
509     * @hide
510     */
511    public void setCdmaEriText(String longName) {
512        mOperatorAlphaLong = longName;
513    }
514
515    public void setIsManualSelection(boolean isManual) {
516        mIsManualNetworkSelection = isManual;
517    }
518
519    /**
520     * Test whether two objects hold the same data values or both are null
521     *
522     * @param a first obj
523     * @param b second obj
524     * @return true if two objects equal or both are null
525     */
526    private static boolean equalsHandlesNulls (Object a, Object b) {
527        return (a == null) ? (b == null) : a.equals (b);
528    }
529
530    /**
531     * Set ServiceState based on intent notifier map
532     *
533     * @param m intent notifier map
534     * @hide
535     */
536    private void setFromNotifierBundle(Bundle m) {
537        mState = m.getInt("state");
538        mRoaming = m.getBoolean("roaming");
539        mOperatorAlphaLong = m.getString("operator-alpha-long");
540        mOperatorAlphaShort = m.getString("operator-alpha-short");
541        mOperatorNumeric = m.getString("operator-numeric");
542        mIsManualNetworkSelection = m.getBoolean("manual");
543        mRadioTechnology = m.getInt("radioTechnology");
544        mCssIndicator = m.getBoolean("cssIndicator");
545        mNetworkId = m.getInt("networkId");
546        mSystemId = m.getInt("systemId");
547        mCdmaRoamingIndicator = m.getInt("cdmaRoamingIndicator");
548        mCdmaDefaultRoamingIndicator = m.getInt("cdmaDefaultRoamingIndicator");
549    }
550
551    /**
552     * Set intent notifier Bundle based on service state
553     *
554     * @param m intent notifier Bundle
555     * @hide
556     */
557    public void fillInNotifierBundle(Bundle m) {
558        m.putInt("state", mState);
559        m.putBoolean("roaming", Boolean.valueOf(mRoaming));
560        m.putString("operator-alpha-long", mOperatorAlphaLong);
561        m.putString("operator-alpha-short", mOperatorAlphaShort);
562        m.putString("operator-numeric", mOperatorNumeric);
563        m.putBoolean("manual", Boolean.valueOf(mIsManualNetworkSelection));
564        m.putInt("radioTechnology", mRadioTechnology);
565        m.putBoolean("cssIndicator", mCssIndicator);
566        m.putInt("networkId", mNetworkId);
567        m.putInt("systemId", mSystemId);
568        m.putInt("cdmaRoamingIndicator", mCdmaRoamingIndicator);
569        m.putInt("cdmaDefaultRoamingIndicator", mCdmaDefaultRoamingIndicator);
570    }
571
572    //***** CDMA
573    /** @hide */
574    public void setRadioTechnology(int state) {
575        this.mRadioTechnology = state;
576    }
577
578    /** @hide */
579    public void setCssIndicator(int css) {
580        this.mCssIndicator = (css != 0);
581    }
582
583    /** @hide */
584    public void setSystemAndNetworkId(int systemId, int networkId) {
585        this.mSystemId = systemId;
586        this.mNetworkId = networkId;
587    }
588
589    /** @hide */
590    public int getRadioTechnology() {
591        return this.mRadioTechnology;
592    }
593
594    /** @hide */
595    public int getCssIndicator() {
596        return this.mCssIndicator ? 1 : 0;
597    }
598
599    /** @hide */
600    public int getNetworkId() {
601        return this.mNetworkId;
602    }
603
604    /** @hide */
605    public int getSystemId() {
606        return this.mSystemId;
607    }
608}
609