ServiceState.java revision ebe66345e7099ca6fc95e8aa4d31a5b5cbbd6224
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
93    /**
94     * Available registration states for GSM, UMTS and CDMA.
95     */
96    /** @hide */
97    public static final int REGISTRATION_STATE_NOT_REGISTERED_AND_NOT_SEARCHING = 0;
98    /** @hide */
99    public static final int REGISTRATION_STATE_HOME_NETWORK = 1;
100    /** @hide */
101    public static final int REGISTRATION_STATE_NOT_REGISTERED_AND_SEARCHING = 2;
102    /** @hide */
103    public static final int REGISTRATION_STATE_REGISTRATION_DENIED = 3;
104    /** @hide */
105    public static final int REGISTRATION_STATE_UNKNOWN = 4;
106    /** @hide */
107    public static final int REGISTRATION_STATE_ROAMING = 5;
108
109    private int mState = STATE_OUT_OF_SERVICE;
110    private boolean mRoaming;
111    private String mOperatorAlphaLong;
112    private String mOperatorAlphaShort;
113    private String mOperatorNumeric;
114    private boolean mIsManualNetworkSelection;
115
116    //***** CDMA
117    private int mRadioTechnology;
118    private boolean mCssIndicator;
119    private int mNetworkId;
120    private int mSystemId;
121    private int mCdmaRoamingIndicator;
122    private int mCdmaDefaultRoamingIndicator;
123
124    /**
125     * Create a new ServiceState from a intent notifier Bundle
126     *
127     * This method is used by PhoneStateIntentReceiver and maybe by
128     * external applications.
129     *
130     * @param m Bundle from intent notifier
131     * @return newly created ServiceState
132     * @hide
133     */
134    public static ServiceState newFromBundle(Bundle m) {
135        ServiceState ret;
136        ret = new ServiceState();
137        ret.setFromNotifierBundle(m);
138        return ret;
139    }
140
141    /**
142     * Empty constructor
143     */
144    public ServiceState() {
145    }
146
147    /**
148     * Copy constructors
149     *
150     * @param s Source service state
151     */
152    public ServiceState(ServiceState s) {
153        copyFrom(s);
154    }
155
156    protected void copyFrom(ServiceState s) {
157        mState = s.mState;
158        mRoaming = s.mRoaming;
159        mOperatorAlphaLong = s.mOperatorAlphaLong;
160        mOperatorAlphaShort = s.mOperatorAlphaShort;
161        mOperatorNumeric = s.mOperatorNumeric;
162        mIsManualNetworkSelection = s.mIsManualNetworkSelection;
163        mRadioTechnology = s.mRadioTechnology;
164        mCssIndicator = s.mCssIndicator;
165        mNetworkId = s.mNetworkId;
166        mSystemId = s.mSystemId;
167        mCdmaRoamingIndicator = s.mCdmaRoamingIndicator;
168        mCdmaDefaultRoamingIndicator = s.mCdmaDefaultRoamingIndicator;
169    }
170
171    /**
172     * Construct a ServiceState object from the given parcel.
173     */
174    public ServiceState(Parcel in) {
175        mState = in.readInt();
176        mRoaming = in.readInt() != 0;
177        mOperatorAlphaLong = in.readString();
178        mOperatorAlphaShort = in.readString();
179        mOperatorNumeric = in.readString();
180        mIsManualNetworkSelection = in.readInt() != 0;
181        mRadioTechnology = in.readInt();
182        mCssIndicator = (in.readInt() != 0);
183        mNetworkId = in.readInt();
184        mSystemId = in.readInt();
185        mCdmaRoamingIndicator = in.readInt();
186        mCdmaDefaultRoamingIndicator = in.readInt();
187    }
188
189    public void writeToParcel(Parcel out, int flags) {
190        out.writeInt(mState);
191        out.writeInt(mRoaming ? 1 : 0);
192        out.writeString(mOperatorAlphaLong);
193        out.writeString(mOperatorAlphaShort);
194        out.writeString(mOperatorNumeric);
195        out.writeInt(mIsManualNetworkSelection ? 1 : 0);
196        out.writeInt(mRadioTechnology);
197        out.writeInt(mCssIndicator ? 1 : 0);
198        out.writeInt(mNetworkId);
199        out.writeInt(mSystemId);
200        out.writeInt(mCdmaRoamingIndicator);
201        out.writeInt(mCdmaDefaultRoamingIndicator);
202    }
203
204    public int describeContents() {
205        return 0;
206    }
207
208    public static final Parcelable.Creator<ServiceState> CREATOR = new Parcelable.Creator() {
209        public ServiceState createFromParcel(Parcel in) {
210            return new ServiceState(in);
211        }
212
213        public ServiceState[] newArray(int size) {
214            return new ServiceState[size];
215        }
216    };
217
218    /**
219     * Get current servcie state of phone
220     *
221     * @see #STATE_IN_SERVICE
222     * @see #STATE_OUT_OF_SERVICE
223     * @see #STATE_EMERGENCY_ONLY
224     * @see #STATE_POWER_OFF
225     */
226    public int getState() {
227        return mState;
228    }
229
230    /**
231     * Get current roaming indicator of phone
232     * (note: not just decoding from TS 27.007 7.2)
233     *
234     * @return true if TS 27.007 7.2 roaming is true
235     *              and ONS is different from SPN
236     *
237     */
238    public boolean getRoaming() {
239        return mRoaming;
240    }
241
242    /**
243     * @hide
244     */
245    public int getCdmaRoamingIndicator(){
246        return this.mCdmaRoamingIndicator;
247    }
248
249    /**
250     * @hide
251     */
252    public int getCdmaDefaultRoamingIndicator(){
253        return this.mCdmaDefaultRoamingIndicator;
254    }
255
256    /**
257     * Get current registered operator name in long alphanumeric format
258     *
259     * In GSM/UMTS, long format can be upto 16 characters long
260     * In CDMA, returns the ERI text, if set, otherwise the ONS
261     *
262     * @return long name of operator, null if unregistered or unknown
263     */
264    public String getOperatorAlphaLong() {
265        return mOperatorAlphaLong;
266    }
267
268    /**
269     * Get current registered operator name in short lphanumeric format
270     *
271     * In GSM/UMST, short format can be upto 8 characters long
272     *
273     * @return short name of operator, null if unregistered or unknown
274     */
275    public String getOperatorAlphaShort() {
276        return mOperatorAlphaShort;
277    }
278
279    /**
280     * Get current registered operator numeric id
281     *
282     * In GSM/UMTS, numeric format is 3 digit country code plus 2 or 3 digit
283     * network code
284     *
285     * The country code can be decoded using MccTable.countryCodeForMcc()
286     *
287     * @return numeric format of operator, null if unregistered or unknown
288     */
289    public String getOperatorNumeric() {
290        return mOperatorNumeric;
291    }
292
293    /**
294     * Get current network selection mode
295     *
296     * @return true if manual mode, false if automatic mode
297     */
298    public boolean getIsManualSelection() {
299        return mIsManualNetworkSelection;
300    }
301
302    @Override
303    public int hashCode() {
304        return ((mState * 0x1234)
305                + (mRoaming ? 1 : 0)
306                + (mIsManualNetworkSelection ? 1 : 0)
307                + ((null == mOperatorAlphaLong) ? 0 : mOperatorAlphaLong.hashCode())
308                + ((null == mOperatorAlphaShort) ? 0 : mOperatorAlphaShort.hashCode())
309                + ((null == mOperatorNumeric) ? 0 : mOperatorNumeric.hashCode())
310                + mCdmaRoamingIndicator
311                + mCdmaDefaultRoamingIndicator);
312    }
313
314    @Override
315    public boolean equals (Object o) {
316        ServiceState s;
317
318        try {
319            s = (ServiceState) o;
320        } catch (ClassCastException ex) {
321            return false;
322        }
323
324        if (o == null) {
325            return false;
326        }
327
328        return (mState == s.mState
329                && mRoaming == s.mRoaming
330                && mIsManualNetworkSelection == s.mIsManualNetworkSelection
331                && equalsHandlesNulls(mOperatorAlphaLong, s.mOperatorAlphaLong)
332                && equalsHandlesNulls(mOperatorAlphaShort, s.mOperatorAlphaShort)
333                && equalsHandlesNulls(mOperatorNumeric, s.mOperatorNumeric)
334                && equalsHandlesNulls(mRadioTechnology, s.mRadioTechnology)
335                && equalsHandlesNulls(mCssIndicator, s.mCssIndicator)
336                && equalsHandlesNulls(mNetworkId, s.mNetworkId)
337                && equalsHandlesNulls(mSystemId, s.mSystemId)
338                && equalsHandlesNulls(mCdmaRoamingIndicator, s.mCdmaRoamingIndicator)
339                && equalsHandlesNulls(mCdmaDefaultRoamingIndicator,
340                        s.mCdmaDefaultRoamingIndicator));
341    }
342
343    @Override
344    public String toString() {
345        String radioTechnology = new String("Error in radioTechnology");
346
347        switch(this.mRadioTechnology) {
348        case 0:
349            radioTechnology = "Unknown";
350            break;
351        case 1:
352            radioTechnology = "GPRS";
353            break;
354        case 2:
355            radioTechnology = "EDGE";
356            break;
357        case 3:
358            radioTechnology = "UMTS";
359            break;
360        case 4:
361            radioTechnology = "IS95A";
362            break;
363        case 5:
364            radioTechnology = "IS95B";
365            break;
366        case 6:
367            radioTechnology = "1xRTT";
368            break;
369        case 7:
370            radioTechnology = "EvDo rev. 0";
371            break;
372        case 8:
373            radioTechnology = "EvDo rev. A";
374            break;
375        case 9:
376            radioTechnology = "HSDPA";
377            break;
378        case 10:
379            radioTechnology = "HSUPA";
380            break;
381        case 11:
382            radioTechnology = "HSPA";
383            break;
384        default:
385            Log.w(LOG_TAG, "mRadioTechnology variable out of range.");
386        break;
387        }
388
389        return (mState + " " + (mRoaming ? "roaming" : "home")
390                + " " + mOperatorAlphaLong
391                + " " + mOperatorAlphaShort
392                + " " + mOperatorNumeric
393                + " " + (mIsManualNetworkSelection ? "(manual)" : "")
394                + " " + radioTechnology
395                + " " + (mCssIndicator ? "CSS supported" : "CSS not supported")
396                + " " + mNetworkId
397                + " " + mSystemId
398                + "RoamInd: " + mCdmaRoamingIndicator
399                + "DefRoamInd: " + mCdmaDefaultRoamingIndicator);
400    }
401
402    public void setStateOutOfService() {
403        mState = STATE_OUT_OF_SERVICE;
404        mRoaming = false;
405        mOperatorAlphaLong = null;
406        mOperatorAlphaShort = null;
407        mOperatorNumeric = null;
408        mIsManualNetworkSelection = false;
409        mRadioTechnology = 0;
410        mCssIndicator = false;
411        mNetworkId = -1;
412        mSystemId = -1;
413        mCdmaRoamingIndicator = -1;
414        mCdmaDefaultRoamingIndicator = -1;
415    }
416
417    public void setStateOff() {
418        mState = STATE_POWER_OFF;
419        mRoaming = false;
420        mOperatorAlphaLong = null;
421        mOperatorAlphaShort = null;
422        mOperatorNumeric = null;
423        mIsManualNetworkSelection = false;
424        mRadioTechnology = 0;
425        mCssIndicator = false;
426        mNetworkId = -1;
427        mSystemId = -1;
428        mCdmaRoamingIndicator = -1;
429        mCdmaDefaultRoamingIndicator = -1;
430    }
431
432    public void setState(int state) {
433        mState = state;
434    }
435
436    public void setRoaming(boolean roaming) {
437        mRoaming = roaming;
438    }
439
440    /**
441     * @hide
442     */
443    public void setCdmaRoamingIndicator(int roaming) {
444        this.mCdmaRoamingIndicator = roaming;
445    }
446
447    /**
448     * @hide
449     */
450    public void setCdmaDefaultRoamingIndicator (int roaming) {
451        this.mCdmaDefaultRoamingIndicator = roaming;
452    }
453
454    public void setOperatorName(String longName, String shortName, String numeric) {
455        mOperatorAlphaLong = longName;
456        mOperatorAlphaShort = shortName;
457        mOperatorNumeric = numeric;
458    }
459
460    /**
461     * In CDMA mOperatorAlphaLong can be set from the ERI
462     * text, this is done from the CDMAPhone and not from the CdmaServiceStateTracker
463     *
464     * @hide
465     */
466    public void setCdmaEriText(String longName) {
467        mOperatorAlphaLong = longName;
468    }
469
470    public void setIsManualSelection(boolean isManual) {
471        mIsManualNetworkSelection = isManual;
472    }
473
474    /**
475     * Test whether two objects hold the same data values or both are null
476     *
477     * @param a first obj
478     * @param b second obj
479     * @return true if two objects equal or both are null
480     */
481    private static boolean equalsHandlesNulls (Object a, Object b) {
482        return (a == null) ? (b == null) : a.equals (b);
483    }
484
485    /**
486     * Set ServiceState based on intent notifier map
487     *
488     * @param m intent notifier map
489     * @hide
490     */
491    private void setFromNotifierBundle(Bundle m) {
492        mState = m.getInt("state");
493        mRoaming = m.getBoolean("roaming");
494        mOperatorAlphaLong = m.getString("operator-alpha-long");
495        mOperatorAlphaShort = m.getString("operator-alpha-short");
496        mOperatorNumeric = m.getString("operator-numeric");
497        mIsManualNetworkSelection = m.getBoolean("manual");
498        mRadioTechnology = m.getInt("radioTechnology");
499        mCssIndicator = m.getBoolean("cssIndicator");
500        mNetworkId = m.getInt("networkId");
501        mSystemId = m.getInt("systemId");
502        mCdmaRoamingIndicator = m.getInt("cdmaRoamingIndicator");
503        mCdmaDefaultRoamingIndicator = m.getInt("cdmaDefaultRoamingIndicator");
504    }
505
506    /**
507     * Set intent notifier Bundle based on service state
508     *
509     * @param m intent notifier Bundle
510     * @hide
511     */
512    public void fillInNotifierBundle(Bundle m) {
513        m.putInt("state", mState);
514        m.putBoolean("roaming", Boolean.valueOf(mRoaming));
515        m.putString("operator-alpha-long", mOperatorAlphaLong);
516        m.putString("operator-alpha-short", mOperatorAlphaShort);
517        m.putString("operator-numeric", mOperatorNumeric);
518        m.putBoolean("manual", Boolean.valueOf(mIsManualNetworkSelection));
519        m.putInt("radioTechnology", mRadioTechnology);
520        m.putBoolean("cssIndicator", mCssIndicator);
521        m.putInt("networkId", mNetworkId);
522        m.putInt("systemId", mSystemId);
523        m.putInt("cdmaRoamingIndicator", mCdmaRoamingIndicator);
524        m.putInt("cdmaDefaultRoamingIndicator", mCdmaDefaultRoamingIndicator);
525    }
526
527    //***** CDMA
528    /** @hide */
529    public void setRadioTechnology(int state) {
530        this.mRadioTechnology = state;
531    }
532
533    /** @hide */
534    public void setCssIndicator(int css) {
535        this.mCssIndicator = (css != 0);
536    }
537
538    /** @hide */
539    public void setSystemAndNetworkId(int systemId, int networkId) {
540        this.mSystemId = systemId;
541        this.mNetworkId = networkId;
542    }
543
544    /** @hide */
545    public int getRadioTechnology() {
546        return this.mRadioTechnology;
547    }
548
549    /** @hide */
550    public int getCssIndicator() {
551        return this.mCssIndicator ? 1 : 0;
552    }
553
554    /** @hide */
555    public int getNetworkId() {
556        return this.mNetworkId;
557    }
558
559    /** @hide */
560    public int getSystemId() {
561        return this.mSystemId;
562    }
563}
564