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