ServiceState.java revision 6714030083b1d8ec5b2df6dfef08034d0d30c2fe
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.telephony.Rlog;
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    static final boolean DBG = true;
40
41    /**
42     * Normal operation condition, the phone is registered
43     * with an operator either in home network or in roaming.
44     */
45    public static final int STATE_IN_SERVICE = 0;
46
47    /**
48     * Phone is not registered with any operator, the phone
49     * can be currently searching a new operator to register to, or not
50     * searching to registration at all, or registration is denied, or radio
51     * signal is not available.
52     */
53    public static final int STATE_OUT_OF_SERVICE = 1;
54
55    /**
56     * The phone is registered and locked.  Only emergency numbers are allowed. {@more}
57     */
58    public static final int STATE_EMERGENCY_ONLY = 2;
59
60    /**
61     * Radio of telephony is explicitly powered off.
62     */
63    public static final int STATE_POWER_OFF = 3;
64
65    /**
66     * RIL level registration state values from ril.h
67     * ((const char **)response)[0] is registration state 0-6,
68     *              0 - Not registered, MT is not currently searching
69     *                  a new operator to register
70     *              1 - Registered, home network
71     *              2 - Not registered, but MT is currently searching
72     *                  a new operator to register
73     *              3 - Registration denied
74     *              4 - Unknown
75     *              5 - Registered, roaming
76     *             10 - Same as 0, but indicates that emergency calls
77     *                  are enabled.
78     *             12 - Same as 2, but indicates that emergency calls
79     *                  are enabled.
80     *             13 - Same as 3, but indicates that emergency calls
81     *                  are enabled.
82     *             14 - Same as 4, but indicates that emergency calls
83     *                  are enabled.
84     * @hide
85     */
86    public static final int RIL_REG_STATE_NOT_REG = 0;
87    /** @hide */
88    public static final int RIL_REG_STATE_HOME = 1;
89    /** @hide */
90    public static final int RIL_REG_STATE_SEARCHING = 2;
91    /** @hide */
92    public static final int RIL_REG_STATE_DENIED = 3;
93    /** @hide */
94    public static final int RIL_REG_STATE_UNKNOWN = 4;
95    /** @hide */
96    public static final int RIL_REG_STATE_ROAMING = 5;
97    /** @hide */
98    public static final int RIL_REG_STATE_NOT_REG_EMERGENCY_CALL_ENABLED = 10;
99    /** @hide */
100    public static final int RIL_REG_STATE_SEARCHING_EMERGENCY_CALL_ENABLED = 12;
101    /** @hide */
102    public static final int RIL_REG_STATE_DENIED_EMERGENCY_CALL_ENABLED = 13;
103    /** @hide */
104    public static final int RIL_REG_STATE_UNKNOWN_EMERGENCY_CALL_ENABLED = 14;
105
106    /**
107     * Available radio technologies for GSM, UMTS and CDMA.
108     * Duplicates the constants from hardware/radio/include/ril.h
109     * This should only be used by agents working with the ril.  Others
110     * should use the equivalent TelephonyManager.NETWORK_TYPE_*
111     */
112    /** @hide */
113    public static final int RIL_RADIO_TECHNOLOGY_UNKNOWN = 0;
114    /** @hide */
115    public static final int RIL_RADIO_TECHNOLOGY_GPRS = 1;
116    /** @hide */
117    public static final int RIL_RADIO_TECHNOLOGY_EDGE = 2;
118    /** @hide */
119    public static final int RIL_RADIO_TECHNOLOGY_UMTS = 3;
120    /** @hide */
121    public static final int RIL_RADIO_TECHNOLOGY_IS95A = 4;
122    /** @hide */
123    public static final int RIL_RADIO_TECHNOLOGY_IS95B = 5;
124    /** @hide */
125    public static final int RIL_RADIO_TECHNOLOGY_1xRTT = 6;
126    /** @hide */
127    public static final int RIL_RADIO_TECHNOLOGY_EVDO_0 = 7;
128    /** @hide */
129    public static final int RIL_RADIO_TECHNOLOGY_EVDO_A = 8;
130    /** @hide */
131    public static final int RIL_RADIO_TECHNOLOGY_HSDPA = 9;
132    /** @hide */
133    public static final int RIL_RADIO_TECHNOLOGY_HSUPA = 10;
134    /** @hide */
135    public static final int RIL_RADIO_TECHNOLOGY_HSPA = 11;
136    /** @hide */
137    public static final int RIL_RADIO_TECHNOLOGY_EVDO_B = 12;
138    /** @hide */
139    public static final int RIL_RADIO_TECHNOLOGY_EHRPD = 13;
140    /** @hide */
141    public static final int RIL_RADIO_TECHNOLOGY_LTE = 14;
142    /** @hide */
143    public static final int RIL_RADIO_TECHNOLOGY_HSPAP = 15;
144    /**
145     * GSM radio technology only supports voice. It does not support data.
146     * @hide
147     */
148    public static final int RIL_RADIO_TECHNOLOGY_GSM = 16;
149    /** @hide */
150    public static final int RIL_RADIO_TECHNOLOGY_TD_SCDMA = 17;
151    /**
152     * IWLAN
153     * @hide
154     */
155    public static final int RIL_RADIO_TECHNOLOGY_IWLAN = 18;
156    /**
157     * Available registration states for GSM, UMTS and CDMA.
158     */
159    /** @hide */
160    public static final int REGISTRATION_STATE_NOT_REGISTERED_AND_NOT_SEARCHING = 0;
161    /** @hide */
162    public static final int REGISTRATION_STATE_HOME_NETWORK = 1;
163    /** @hide */
164    public static final int REGISTRATION_STATE_NOT_REGISTERED_AND_SEARCHING = 2;
165    /** @hide */
166    public static final int REGISTRATION_STATE_REGISTRATION_DENIED = 3;
167    /** @hide */
168    public static final int REGISTRATION_STATE_UNKNOWN = 4;
169    /** @hide */
170    public static final int REGISTRATION_STATE_ROAMING = 5;
171
172    private int mVoiceRegState = STATE_OUT_OF_SERVICE;
173    private int mDataRegState = STATE_OUT_OF_SERVICE;
174
175    /**
176     * Roaming type
177     * HOME : in home network
178     * @hide
179     */
180    public static final int ROAMING_TYPE_NOT_ROAMING = 0;
181    /**
182     * Roaming type
183     * UNKNOWN : in a roaming network, but we can not tell if it's domestic or international
184     * @hide
185     */
186    public static final int ROAMING_TYPE_UNKNOWN = 1;
187    /**
188     * Roaming type
189     * DOMESTIC : in domestic roaming network
190     * @hide
191     */
192    public static final int ROAMING_TYPE_DOMESTIC = 2;
193    /**
194     * Roaming type
195     * INTERNATIONAL : in international roaming network
196     * @hide
197     */
198    public static final int ROAMING_TYPE_INTERNATIONAL = 3;
199
200    private int mVoiceRoamingType;
201    private int mDataRoamingType;
202    private String mVoiceOperatorAlphaLong;
203    private String mVoiceOperatorAlphaShort;
204    private String mVoiceOperatorNumeric;
205    private String mDataOperatorAlphaLong;
206    private String mDataOperatorAlphaShort;
207    private String mDataOperatorNumeric;
208    private boolean mIsManualNetworkSelection;
209
210    private boolean mIsEmergencyOnly;
211
212    private int mRilVoiceRadioTechnology;
213    private int mRilDataRadioTechnology;
214
215    private boolean mCssIndicator;
216    private int mNetworkId;
217    private int mSystemId;
218    private int mCdmaRoamingIndicator;
219    private int mCdmaDefaultRoamingIndicator;
220    private int mCdmaEriIconIndex;
221    private int mCdmaEriIconMode;
222
223    private boolean mIsDataRoamingFromRegistration;
224
225    /**
226     * get String description of roaming type
227     * @hide
228     */
229    public static final String getRoamingLogString(int roamingType) {
230        switch (roamingType) {
231            case ROAMING_TYPE_NOT_ROAMING:
232                return "home";
233
234            case ROAMING_TYPE_UNKNOWN:
235                return "roaming";
236
237            case ROAMING_TYPE_DOMESTIC:
238                return "Domestic Roaming";
239
240            case ROAMING_TYPE_INTERNATIONAL:
241                return "International Roaming";
242
243            default:
244                return "UNKNOWN";
245        }
246    }
247
248    /**
249     * Create a new ServiceState from a intent notifier Bundle
250     *
251     * This method is used by PhoneStateIntentReceiver and maybe by
252     * external applications.
253     *
254     * @param m Bundle from intent notifier
255     * @return newly created ServiceState
256     * @hide
257     */
258    public static ServiceState newFromBundle(Bundle m) {
259        ServiceState ret;
260        ret = new ServiceState();
261        ret.setFromNotifierBundle(m);
262        return ret;
263    }
264
265    /**
266     * Empty constructor
267     */
268    public ServiceState() {
269    }
270
271    /**
272     * Copy constructors
273     *
274     * @param s Source service state
275     */
276    public ServiceState(ServiceState s) {
277        copyFrom(s);
278    }
279
280    protected void copyFrom(ServiceState s) {
281        mVoiceRegState = s.mVoiceRegState;
282        mDataRegState = s.mDataRegState;
283        mVoiceRoamingType = s.mVoiceRoamingType;
284        mDataRoamingType = s.mDataRoamingType;
285        mVoiceOperatorAlphaLong = s.mVoiceOperatorAlphaLong;
286        mVoiceOperatorAlphaShort = s.mVoiceOperatorAlphaShort;
287        mVoiceOperatorNumeric = s.mVoiceOperatorNumeric;
288        mDataOperatorAlphaLong = s.mDataOperatorAlphaLong;
289        mDataOperatorAlphaShort = s.mDataOperatorAlphaShort;
290        mDataOperatorNumeric = s.mDataOperatorNumeric;
291        mIsManualNetworkSelection = s.mIsManualNetworkSelection;
292        mRilVoiceRadioTechnology = s.mRilVoiceRadioTechnology;
293        mRilDataRadioTechnology = s.mRilDataRadioTechnology;
294        mCssIndicator = s.mCssIndicator;
295        mNetworkId = s.mNetworkId;
296        mSystemId = s.mSystemId;
297        mCdmaRoamingIndicator = s.mCdmaRoamingIndicator;
298        mCdmaDefaultRoamingIndicator = s.mCdmaDefaultRoamingIndicator;
299        mCdmaEriIconIndex = s.mCdmaEriIconIndex;
300        mCdmaEriIconMode = s.mCdmaEriIconMode;
301        mIsEmergencyOnly = s.mIsEmergencyOnly;
302        mIsDataRoamingFromRegistration = s.mIsDataRoamingFromRegistration;
303    }
304
305    /**
306     * Construct a ServiceState object from the given parcel.
307     */
308    public ServiceState(Parcel in) {
309        mVoiceRegState = in.readInt();
310        mDataRegState = in.readInt();
311        mVoiceRoamingType = in.readInt();
312        mDataRoamingType = in.readInt();
313        mVoiceOperatorAlphaLong = in.readString();
314        mVoiceOperatorAlphaShort = in.readString();
315        mVoiceOperatorNumeric = in.readString();
316        mDataOperatorAlphaLong = in.readString();
317        mDataOperatorAlphaShort = in.readString();
318        mDataOperatorNumeric = in.readString();
319        mIsManualNetworkSelection = in.readInt() != 0;
320        mRilVoiceRadioTechnology = in.readInt();
321        mRilDataRadioTechnology = in.readInt();
322        mCssIndicator = (in.readInt() != 0);
323        mNetworkId = in.readInt();
324        mSystemId = in.readInt();
325        mCdmaRoamingIndicator = in.readInt();
326        mCdmaDefaultRoamingIndicator = in.readInt();
327        mCdmaEriIconIndex = in.readInt();
328        mCdmaEriIconMode = in.readInt();
329        mIsEmergencyOnly = in.readInt() != 0;
330        mIsDataRoamingFromRegistration = in.readInt() != 0;
331    }
332
333    public void writeToParcel(Parcel out, int flags) {
334        out.writeInt(mVoiceRegState);
335        out.writeInt(mDataRegState);
336        out.writeInt(mVoiceRoamingType);
337        out.writeInt(mDataRoamingType);
338        out.writeString(mVoiceOperatorAlphaLong);
339        out.writeString(mVoiceOperatorAlphaShort);
340        out.writeString(mVoiceOperatorNumeric);
341        out.writeString(mDataOperatorAlphaLong);
342        out.writeString(mDataOperatorAlphaShort);
343        out.writeString(mDataOperatorNumeric);
344        out.writeInt(mIsManualNetworkSelection ? 1 : 0);
345        out.writeInt(mRilVoiceRadioTechnology);
346        out.writeInt(mRilDataRadioTechnology);
347        out.writeInt(mCssIndicator ? 1 : 0);
348        out.writeInt(mNetworkId);
349        out.writeInt(mSystemId);
350        out.writeInt(mCdmaRoamingIndicator);
351        out.writeInt(mCdmaDefaultRoamingIndicator);
352        out.writeInt(mCdmaEriIconIndex);
353        out.writeInt(mCdmaEriIconMode);
354        out.writeInt(mIsEmergencyOnly ? 1 : 0);
355        out.writeInt(mIsDataRoamingFromRegistration ? 1 : 0);
356    }
357
358    public int describeContents() {
359        return 0;
360    }
361
362    public static final Parcelable.Creator<ServiceState> CREATOR =
363            new Parcelable.Creator<ServiceState>() {
364        public ServiceState createFromParcel(Parcel in) {
365            return new ServiceState(in);
366        }
367
368        public ServiceState[] newArray(int size) {
369            return new ServiceState[size];
370        }
371    };
372
373    /**
374     * Get current voice service state
375     */
376    public int getState() {
377        return getVoiceRegState();
378    }
379
380    /**
381     * Get current voice service state
382     *
383     * @see #STATE_IN_SERVICE
384     * @see #STATE_OUT_OF_SERVICE
385     * @see #STATE_EMERGENCY_ONLY
386     * @see #STATE_POWER_OFF
387     *
388     * @hide
389     */
390    public int getVoiceRegState() {
391        return mVoiceRegState;
392    }
393
394    /**
395     * Get current data service state
396     *
397     * @see #STATE_IN_SERVICE
398     * @see #STATE_OUT_OF_SERVICE
399     * @see #STATE_EMERGENCY_ONLY
400     * @see #STATE_POWER_OFF
401     *
402     * @hide
403     */
404    public int getDataRegState() {
405        return mDataRegState;
406    }
407
408    /**
409     * Get current roaming indicator of phone
410     * (note: not just decoding from TS 27.007 7.2)
411     *
412     * @return true if TS 27.007 7.2 roaming is true
413     *              and ONS is different from SPN
414     */
415    public boolean getRoaming() {
416        return getVoiceRoaming() || getDataRoaming();
417    }
418
419    /**
420     * Get current voice network roaming status
421     * @return roaming status
422     * @hide
423     */
424    public boolean getVoiceRoaming() {
425        return mVoiceRoamingType != ROAMING_TYPE_NOT_ROAMING;
426    }
427
428    /**
429     * Get current voice network roaming type
430     * @return roaming type
431     * @hide
432     */
433    public int getVoiceRoamingType() {
434        return mVoiceRoamingType;
435    }
436
437    /**
438     * Get current data network roaming type
439     * @return roaming type
440     * @hide
441     */
442    public boolean getDataRoaming() {
443        return mDataRoamingType != ROAMING_TYPE_NOT_ROAMING;
444    }
445
446    /**
447     * Set whether data network registration state is roaming
448     *
449     * This should only be set to the roaming value received
450     * once the data registration phase has completed.
451     * @hide
452     */
453    public void setDataRoamingFromRegistration(boolean dataRoaming) {
454        mIsDataRoamingFromRegistration = dataRoaming;
455    }
456
457    /**
458     * Get whether data network registration state is roaming
459     * @return true if registration indicates roaming, false otherwise
460     * @hide
461     */
462    public boolean getDataRoamingFromRegistration() {
463        return mIsDataRoamingFromRegistration;
464    }
465
466    /**
467     * Get current data network roaming type
468     * @return roaming type
469     * @hide
470     */
471    public int getDataRoamingType() {
472        return mDataRoamingType;
473    }
474
475    /**
476     * @hide
477     */
478    public boolean isEmergencyOnly() {
479        return mIsEmergencyOnly;
480    }
481
482    /**
483     * @hide
484     */
485    public int getCdmaRoamingIndicator(){
486        return this.mCdmaRoamingIndicator;
487    }
488
489    /**
490     * @hide
491     */
492    public int getCdmaDefaultRoamingIndicator(){
493        return this.mCdmaDefaultRoamingIndicator;
494    }
495
496    /**
497     * @hide
498     */
499    public int getCdmaEriIconIndex() {
500        return this.mCdmaEriIconIndex;
501    }
502
503    /**
504     * @hide
505     */
506    public int getCdmaEriIconMode() {
507        return this.mCdmaEriIconMode;
508    }
509
510    /**
511     * Get current registered operator name in long alphanumeric format.
512     *
513     * In GSM/UMTS, long format can be up to 16 characters long.
514     * In CDMA, returns the ERI text, if set. Otherwise, returns the ONS.
515     *
516     * @return long name of operator, null if unregistered or unknown
517     */
518    public String getOperatorAlphaLong() {
519        return mVoiceOperatorAlphaLong;
520    }
521
522    /**
523     * Get current registered voice network operator name in long alphanumeric format.
524     * @return long name of operator
525     * @hide
526     */
527    public String getVoiceOperatorAlphaLong() {
528        return mVoiceOperatorAlphaLong;
529    }
530
531    /**
532     * Get current registered data network operator name in long alphanumeric format.
533     * @return long name of voice operator
534     * @hide
535     */
536    public String getDataOperatorAlphaLong() {
537        return mDataOperatorAlphaLong;
538    }
539
540    /**
541     * Get current registered operator name in short alphanumeric format.
542     *
543     * In GSM/UMTS, short format can be up to 8 characters long.
544     *
545     * @return short name of operator, null if unregistered or unknown
546     */
547    public String getOperatorAlphaShort() {
548        return mVoiceOperatorAlphaShort;
549    }
550
551    /**
552     * Get current registered voice network operator name in short alphanumeric format.
553     * @return short name of operator, null if unregistered or unknown
554     * @hide
555     */
556    public String getVoiceOperatorAlphaShort() {
557        return mVoiceOperatorAlphaShort;
558    }
559
560    /**
561     * Get current registered data network operator name in short alphanumeric format.
562     * @return short name of operator, null if unregistered or unknown
563     * @hide
564     */
565    public String getDataOperatorAlphaShort() {
566        return mDataOperatorAlphaShort;
567    }
568
569    /**
570     * Get current registered operator numeric id.
571     *
572     * In GSM/UMTS, numeric format is 3 digit country code plus 2 or 3 digit
573     * network code.
574     *
575     * @return numeric format of operator, null if unregistered or unknown
576     */
577    /*
578     * The country code can be decoded using
579     * {@link com.android.internal.telephony.MccTable#countryCodeForMcc(int)}.
580     */
581    public String getOperatorNumeric() {
582        return mVoiceOperatorNumeric;
583    }
584
585    /**
586     * Get current registered voice network operator numeric id.
587     * @return numeric format of operator, null if unregistered or unknown
588     * @hide
589     */
590    public String getVoiceOperatorNumeric() {
591        return mVoiceOperatorNumeric;
592    }
593
594    /**
595     * Get current registered data network operator numeric id.
596     * @return numeric format of operator, null if unregistered or unknown
597     * @hide
598     */
599    public String getDataOperatorNumeric() {
600        return mDataOperatorNumeric;
601    }
602
603    /**
604     * Get current network selection mode.
605     *
606     * @return true if manual mode, false if automatic mode
607     */
608    public boolean getIsManualSelection() {
609        return mIsManualNetworkSelection;
610    }
611
612    @Override
613    public int hashCode() {
614        return ((mVoiceRegState * 31)
615                + (mDataRegState * 37)
616                + mVoiceRoamingType
617                + mDataRoamingType
618                + (mIsManualNetworkSelection ? 1 : 0)
619                + ((null == mVoiceOperatorAlphaLong) ? 0 : mVoiceOperatorAlphaLong.hashCode())
620                + ((null == mVoiceOperatorAlphaShort) ? 0 : mVoiceOperatorAlphaShort.hashCode())
621                + ((null == mVoiceOperatorNumeric) ? 0 : mVoiceOperatorNumeric.hashCode())
622                + ((null == mDataOperatorAlphaLong) ? 0 : mDataOperatorAlphaLong.hashCode())
623                + ((null == mDataOperatorAlphaShort) ? 0 : mDataOperatorAlphaShort.hashCode())
624                + ((null == mDataOperatorNumeric) ? 0 : mDataOperatorNumeric.hashCode())
625                + mCdmaRoamingIndicator
626                + mCdmaDefaultRoamingIndicator
627                + (mIsEmergencyOnly ? 1 : 0)
628                + (mIsDataRoamingFromRegistration ? 1 : 0));
629    }
630
631    @Override
632    public boolean equals (Object o) {
633        ServiceState s;
634
635        try {
636            s = (ServiceState) o;
637        } catch (ClassCastException ex) {
638            return false;
639        }
640
641        if (o == null) {
642            return false;
643        }
644
645        return (mVoiceRegState == s.mVoiceRegState
646                && mDataRegState == s.mDataRegState
647                && mIsManualNetworkSelection == s.mIsManualNetworkSelection
648                && mVoiceRoamingType == s.mVoiceRoamingType
649                && mDataRoamingType == s.mDataRoamingType
650                && equalsHandlesNulls(mVoiceOperatorAlphaLong, s.mVoiceOperatorAlphaLong)
651                && equalsHandlesNulls(mVoiceOperatorAlphaShort, s.mVoiceOperatorAlphaShort)
652                && equalsHandlesNulls(mVoiceOperatorNumeric, s.mVoiceOperatorNumeric)
653                && equalsHandlesNulls(mDataOperatorAlphaLong, s.mDataOperatorAlphaLong)
654                && equalsHandlesNulls(mDataOperatorAlphaShort, s.mDataOperatorAlphaShort)
655                && equalsHandlesNulls(mDataOperatorNumeric, s.mDataOperatorNumeric)
656                && equalsHandlesNulls(mRilVoiceRadioTechnology, s.mRilVoiceRadioTechnology)
657                && equalsHandlesNulls(mRilDataRadioTechnology, s.mRilDataRadioTechnology)
658                && equalsHandlesNulls(mCssIndicator, s.mCssIndicator)
659                && equalsHandlesNulls(mNetworkId, s.mNetworkId)
660                && equalsHandlesNulls(mSystemId, s.mSystemId)
661                && equalsHandlesNulls(mCdmaRoamingIndicator, s.mCdmaRoamingIndicator)
662                && equalsHandlesNulls(mCdmaDefaultRoamingIndicator,
663                        s.mCdmaDefaultRoamingIndicator)
664                && mIsEmergencyOnly == s.mIsEmergencyOnly
665                && mIsDataRoamingFromRegistration == s.mIsDataRoamingFromRegistration);
666    }
667
668    /**
669     * Convert radio technology to String
670     *
671     * @param radioTechnology
672     * @return String representation of the RAT
673     *
674     * @hide
675     */
676    public static String rilRadioTechnologyToString(int rt) {
677        String rtString;
678
679        switch(rt) {
680            case RIL_RADIO_TECHNOLOGY_UNKNOWN:
681                rtString = "Unknown";
682                break;
683            case RIL_RADIO_TECHNOLOGY_GPRS:
684                rtString = "GPRS";
685                break;
686            case RIL_RADIO_TECHNOLOGY_EDGE:
687                rtString = "EDGE";
688                break;
689            case RIL_RADIO_TECHNOLOGY_UMTS:
690                rtString = "UMTS";
691                break;
692            case RIL_RADIO_TECHNOLOGY_IS95A:
693                rtString = "CDMA-IS95A";
694                break;
695            case RIL_RADIO_TECHNOLOGY_IS95B:
696                rtString = "CDMA-IS95B";
697                break;
698            case RIL_RADIO_TECHNOLOGY_1xRTT:
699                rtString = "1xRTT";
700                break;
701            case RIL_RADIO_TECHNOLOGY_EVDO_0:
702                rtString = "EvDo-rev.0";
703                break;
704            case RIL_RADIO_TECHNOLOGY_EVDO_A:
705                rtString = "EvDo-rev.A";
706                break;
707            case RIL_RADIO_TECHNOLOGY_HSDPA:
708                rtString = "HSDPA";
709                break;
710            case RIL_RADIO_TECHNOLOGY_HSUPA:
711                rtString = "HSUPA";
712                break;
713            case RIL_RADIO_TECHNOLOGY_HSPA:
714                rtString = "HSPA";
715                break;
716            case RIL_RADIO_TECHNOLOGY_EVDO_B:
717                rtString = "EvDo-rev.B";
718                break;
719            case RIL_RADIO_TECHNOLOGY_EHRPD:
720                rtString = "eHRPD";
721                break;
722            case RIL_RADIO_TECHNOLOGY_LTE:
723                rtString = "LTE";
724                break;
725            case RIL_RADIO_TECHNOLOGY_HSPAP:
726                rtString = "HSPAP";
727                break;
728            case RIL_RADIO_TECHNOLOGY_GSM:
729                rtString = "GSM";
730                break;
731            case RIL_RADIO_TECHNOLOGY_IWLAN:
732                rtString = "IWLAN";
733                break;
734            case RIL_RADIO_TECHNOLOGY_TD_SCDMA:
735                rtString = "TD-SCDMA";
736                break;
737            default:
738                rtString = "Unexpected";
739                Rlog.w(LOG_TAG, "Unexpected radioTechnology=" + rt);
740                break;
741        }
742        return rtString;
743    }
744
745    @Override
746    public String toString() {
747        String radioTechnology = rilRadioTechnologyToString(mRilVoiceRadioTechnology);
748        String dataRadioTechnology = rilRadioTechnologyToString(mRilDataRadioTechnology);
749
750        return (mVoiceRegState + " " + mDataRegState
751                + " "
752                + "voice " + getRoamingLogString(mVoiceRoamingType)
753                + " "
754                + "data " + getRoamingLogString(mDataRoamingType)
755                + " " + mVoiceOperatorAlphaLong
756                + " " + mVoiceOperatorAlphaShort
757                + " " + mVoiceOperatorNumeric
758                + " " + mDataOperatorAlphaLong
759                + " " + mDataOperatorAlphaShort
760                + " " + mDataOperatorNumeric
761                + " " + (mIsManualNetworkSelection ? "(manual)" : "")
762                + " " + radioTechnology
763                + " " + dataRadioTechnology
764                + " " + (mCssIndicator ? "CSS supported" : "CSS not supported")
765                + " " + mNetworkId
766                + " " + mSystemId
767                + " RoamInd=" + mCdmaRoamingIndicator
768                + " DefRoamInd=" + mCdmaDefaultRoamingIndicator
769                + " EmergOnly=" + mIsEmergencyOnly
770                + " IsDataRoamingFromRegistration=" + mIsDataRoamingFromRegistration);
771    }
772
773    private void setNullState(int state) {
774        if (DBG) Rlog.d(LOG_TAG, "[ServiceState] setNullState=" + state);
775        mVoiceRegState = state;
776        mDataRegState = state;
777        mVoiceRoamingType = ROAMING_TYPE_NOT_ROAMING;
778        mDataRoamingType = ROAMING_TYPE_NOT_ROAMING;
779        mVoiceOperatorAlphaLong = null;
780        mVoiceOperatorAlphaShort = null;
781        mVoiceOperatorNumeric = null;
782        mDataOperatorAlphaLong = null;
783        mDataOperatorAlphaShort = null;
784        mDataOperatorNumeric = null;
785        mIsManualNetworkSelection = false;
786        mRilVoiceRadioTechnology = 0;
787        mRilDataRadioTechnology = 0;
788        mCssIndicator = false;
789        mNetworkId = -1;
790        mSystemId = -1;
791        mCdmaRoamingIndicator = -1;
792        mCdmaDefaultRoamingIndicator = -1;
793        mCdmaEriIconIndex = -1;
794        mCdmaEriIconMode = -1;
795        mIsEmergencyOnly = false;
796        mIsDataRoamingFromRegistration = false;
797    }
798
799    public void setStateOutOfService() {
800        setNullState(STATE_OUT_OF_SERVICE);
801    }
802
803    public void setStateOff() {
804        setNullState(STATE_POWER_OFF);
805    }
806
807    public void setState(int state) {
808        setVoiceRegState(state);
809        if (DBG) Rlog.e(LOG_TAG, "[ServiceState] setState deprecated use setVoiceRegState()");
810    }
811
812    /** @hide */
813    public void setVoiceRegState(int state) {
814        mVoiceRegState = state;
815        if (DBG) Rlog.d(LOG_TAG, "[ServiceState] setVoiceRegState=" + mVoiceRegState);
816    }
817
818    /** @hide */
819    public void setDataRegState(int state) {
820        mDataRegState = state;
821        if (DBG) Rlog.d(LOG_TAG, "[ServiceState] setDataRegState=" + mDataRegState);
822    }
823
824    public void setRoaming(boolean roaming) {
825        mVoiceRoamingType = (roaming ? ROAMING_TYPE_UNKNOWN : ROAMING_TYPE_NOT_ROAMING);
826        mDataRoamingType = mVoiceRoamingType;
827    }
828
829    /** @hide */
830    public void setVoiceRoaming(boolean roaming) {
831        mVoiceRoamingType = (roaming ? ROAMING_TYPE_UNKNOWN : ROAMING_TYPE_NOT_ROAMING);
832    }
833
834    /** @hide */
835    public void setVoiceRoamingType(int type) {
836        mVoiceRoamingType = type;
837    }
838
839    /** @hide */
840    public void setDataRoaming(boolean dataRoaming) {
841        mDataRoamingType = (dataRoaming ? ROAMING_TYPE_UNKNOWN : ROAMING_TYPE_NOT_ROAMING);
842    }
843
844    /** @hide */
845    public void setDataRoamingType(int type) {
846        mDataRoamingType = type;
847    }
848
849    /**
850     * @hide
851     */
852    public void setEmergencyOnly(boolean emergencyOnly) {
853        mIsEmergencyOnly = emergencyOnly;
854    }
855
856    /**
857     * @hide
858     */
859    public void setCdmaRoamingIndicator(int roaming) {
860        this.mCdmaRoamingIndicator = roaming;
861    }
862
863    /**
864     * @hide
865     */
866    public void setCdmaDefaultRoamingIndicator (int roaming) {
867        this.mCdmaDefaultRoamingIndicator = roaming;
868    }
869
870    /**
871     * @hide
872     */
873    public void setCdmaEriIconIndex(int index) {
874        this.mCdmaEriIconIndex = index;
875    }
876
877    /**
878     * @hide
879     */
880    public void setCdmaEriIconMode(int mode) {
881        this.mCdmaEriIconMode = mode;
882    }
883
884    public void setOperatorName(String longName, String shortName, String numeric) {
885        mVoiceOperatorAlphaLong = longName;
886        mVoiceOperatorAlphaShort = shortName;
887        mVoiceOperatorNumeric = numeric;
888        mDataOperatorAlphaLong = longName;
889        mDataOperatorAlphaShort = shortName;
890        mDataOperatorNumeric = numeric;
891    }
892
893    /** @hide */
894    public void setVoiceOperatorName(String longName, String shortName, String numeric) {
895        mVoiceOperatorAlphaLong = longName;
896        mVoiceOperatorAlphaShort = shortName;
897        mVoiceOperatorNumeric = numeric;
898    }
899
900    /** @hide */
901    public void setDataOperatorName(String longName, String shortName, String numeric) {
902        mDataOperatorAlphaLong = longName;
903        mDataOperatorAlphaShort = shortName;
904        mDataOperatorNumeric = numeric;
905    }
906
907    /**
908     * In CDMA, mOperatorAlphaLong can be set from the ERI text.
909     * This is done from the GsmCdmaPhone and not from the ServiceStateTracker.
910     *
911     * @hide
912     */
913    public void setOperatorAlphaLong(String longName) {
914        mVoiceOperatorAlphaLong = longName;
915        mDataOperatorAlphaLong = longName;
916    }
917
918    /** @hide */
919    public void setVoiceOperatorAlphaLong(String longName) {
920        mVoiceOperatorAlphaLong = longName;
921    }
922
923    /** @hide */
924    public void setDataOperatorAlphaLong(String longName) {
925        mDataOperatorAlphaLong = longName;
926    }
927
928    public void setIsManualSelection(boolean isManual) {
929        mIsManualNetworkSelection = isManual;
930    }
931
932    /**
933     * Test whether two objects hold the same data values or both are null.
934     *
935     * @param a first obj
936     * @param b second obj
937     * @return true if two objects equal or both are null
938     */
939    private static boolean equalsHandlesNulls (Object a, Object b) {
940        return (a == null) ? (b == null) : a.equals (b);
941    }
942
943    /**
944     * Set ServiceState based on intent notifier map.
945     *
946     * @param m intent notifier map
947     * @hide
948     */
949    private void setFromNotifierBundle(Bundle m) {
950        mVoiceRegState = m.getInt("voiceRegState");
951        mDataRegState = m.getInt("dataRegState");
952        mVoiceRoamingType = m.getInt("voiceRoamingType");
953        mDataRoamingType = m.getInt("dataRoamingType");
954        mVoiceOperatorAlphaLong = m.getString("operator-alpha-long");
955        mVoiceOperatorAlphaShort = m.getString("operator-alpha-short");
956        mVoiceOperatorNumeric = m.getString("operator-numeric");
957        mDataOperatorAlphaLong = m.getString("data-operator-alpha-long");
958        mDataOperatorAlphaShort = m.getString("data-operator-alpha-short");
959        mDataOperatorNumeric = m.getString("data-operator-numeric");
960        mIsManualNetworkSelection = m.getBoolean("manual");
961        mRilVoiceRadioTechnology = m.getInt("radioTechnology");
962        mRilDataRadioTechnology = m.getInt("dataRadioTechnology");
963        mCssIndicator = m.getBoolean("cssIndicator");
964        mNetworkId = m.getInt("networkId");
965        mSystemId = m.getInt("systemId");
966        mCdmaRoamingIndicator = m.getInt("cdmaRoamingIndicator");
967        mCdmaDefaultRoamingIndicator = m.getInt("cdmaDefaultRoamingIndicator");
968        mIsEmergencyOnly = m.getBoolean("emergencyOnly");
969        mIsDataRoamingFromRegistration = m.getBoolean("isDataRoamingFromRegistration");
970    }
971
972    /**
973     * Set intent notifier Bundle based on service state.
974     *
975     * @param m intent notifier Bundle
976     * @hide
977     */
978    public void fillInNotifierBundle(Bundle m) {
979        m.putInt("voiceRegState", mVoiceRegState);
980        m.putInt("dataRegState", mDataRegState);
981        m.putInt("voiceRoamingType", mVoiceRoamingType);
982        m.putInt("dataRoamingType", mDataRoamingType);
983        m.putString("operator-alpha-long", mVoiceOperatorAlphaLong);
984        m.putString("operator-alpha-short", mVoiceOperatorAlphaShort);
985        m.putString("operator-numeric", mVoiceOperatorNumeric);
986        m.putString("data-operator-alpha-long", mDataOperatorAlphaLong);
987        m.putString("data-operator-alpha-short", mDataOperatorAlphaShort);
988        m.putString("data-operator-numeric", mDataOperatorNumeric);
989        m.putBoolean("manual", Boolean.valueOf(mIsManualNetworkSelection));
990        m.putInt("radioTechnology", mRilVoiceRadioTechnology);
991        m.putInt("dataRadioTechnology", mRilDataRadioTechnology);
992        m.putBoolean("cssIndicator", mCssIndicator);
993        m.putInt("networkId", mNetworkId);
994        m.putInt("systemId", mSystemId);
995        m.putInt("cdmaRoamingIndicator", mCdmaRoamingIndicator);
996        m.putInt("cdmaDefaultRoamingIndicator", mCdmaDefaultRoamingIndicator);
997        m.putBoolean("emergencyOnly", Boolean.valueOf(mIsEmergencyOnly));
998        m.putBoolean("isDataRoamingFromRegistration", Boolean.valueOf(mIsDataRoamingFromRegistration));
999    }
1000
1001    /** @hide */
1002    public void setRilVoiceRadioTechnology(int rt) {
1003        this.mRilVoiceRadioTechnology = rt;
1004    }
1005
1006    /** @hide */
1007    public void setRilDataRadioTechnology(int rt) {
1008        this.mRilDataRadioTechnology = rt;
1009        if (DBG) Rlog.d(LOG_TAG, "[ServiceState] setDataRadioTechnology=" + mRilDataRadioTechnology);
1010    }
1011
1012    /** @hide */
1013    public void setCssIndicator(int css) {
1014        this.mCssIndicator = (css != 0);
1015    }
1016
1017    /** @hide */
1018    public void setSystemAndNetworkId(int systemId, int networkId) {
1019        this.mSystemId = systemId;
1020        this.mNetworkId = networkId;
1021    }
1022
1023    /** @hide */
1024    public int getRilVoiceRadioTechnology() {
1025        return this.mRilVoiceRadioTechnology;
1026    }
1027    /** @hide */
1028    public int getRilDataRadioTechnology() {
1029        return this.mRilDataRadioTechnology;
1030    }
1031    /**
1032     * @hide
1033     * @Deprecated to be removed Q3 2013 use {@link #getRilDataRadioTechnology} or
1034     * {@link #getRilVoiceRadioTechnology}
1035     */
1036    public int getRadioTechnology() {
1037        Rlog.e(LOG_TAG, "ServiceState.getRadioTechnology() DEPRECATED will be removed *******");
1038        return getRilDataRadioTechnology();
1039    }
1040
1041    private int rilRadioTechnologyToNetworkType(int rt) {
1042        switch(rt) {
1043        case ServiceState.RIL_RADIO_TECHNOLOGY_GPRS:
1044            return TelephonyManager.NETWORK_TYPE_GPRS;
1045        case ServiceState.RIL_RADIO_TECHNOLOGY_EDGE:
1046            return TelephonyManager.NETWORK_TYPE_EDGE;
1047        case ServiceState.RIL_RADIO_TECHNOLOGY_UMTS:
1048            return TelephonyManager.NETWORK_TYPE_UMTS;
1049        case ServiceState.RIL_RADIO_TECHNOLOGY_HSDPA:
1050            return TelephonyManager.NETWORK_TYPE_HSDPA;
1051        case ServiceState.RIL_RADIO_TECHNOLOGY_HSUPA:
1052            return TelephonyManager.NETWORK_TYPE_HSUPA;
1053        case ServiceState.RIL_RADIO_TECHNOLOGY_HSPA:
1054            return TelephonyManager.NETWORK_TYPE_HSPA;
1055        case ServiceState.RIL_RADIO_TECHNOLOGY_IS95A:
1056        case ServiceState.RIL_RADIO_TECHNOLOGY_IS95B:
1057            return TelephonyManager.NETWORK_TYPE_CDMA;
1058        case ServiceState.RIL_RADIO_TECHNOLOGY_1xRTT:
1059            return TelephonyManager.NETWORK_TYPE_1xRTT;
1060        case ServiceState.RIL_RADIO_TECHNOLOGY_EVDO_0:
1061            return TelephonyManager.NETWORK_TYPE_EVDO_0;
1062        case ServiceState.RIL_RADIO_TECHNOLOGY_EVDO_A:
1063            return TelephonyManager.NETWORK_TYPE_EVDO_A;
1064        case ServiceState.RIL_RADIO_TECHNOLOGY_EVDO_B:
1065            return TelephonyManager.NETWORK_TYPE_EVDO_B;
1066        case ServiceState.RIL_RADIO_TECHNOLOGY_EHRPD:
1067            return TelephonyManager.NETWORK_TYPE_EHRPD;
1068        case ServiceState.RIL_RADIO_TECHNOLOGY_LTE:
1069            return TelephonyManager.NETWORK_TYPE_LTE;
1070        case ServiceState.RIL_RADIO_TECHNOLOGY_HSPAP:
1071            return TelephonyManager.NETWORK_TYPE_HSPAP;
1072        case ServiceState.RIL_RADIO_TECHNOLOGY_GSM:
1073            return TelephonyManager.NETWORK_TYPE_GSM;
1074        case ServiceState.RIL_RADIO_TECHNOLOGY_TD_SCDMA:
1075            return TelephonyManager.NETWORK_TYPE_TD_SCDMA;
1076        case ServiceState.RIL_RADIO_TECHNOLOGY_IWLAN:
1077            return TelephonyManager.NETWORK_TYPE_IWLAN;
1078        default:
1079            return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1080        }
1081    }
1082
1083    /**
1084     * @Deprecated to be removed Q3 2013 use {@link #getVoiceNetworkType}
1085     * @hide
1086     */
1087    public int getNetworkType() {
1088        Rlog.e(LOG_TAG, "ServiceState.getNetworkType() DEPRECATED will be removed *******");
1089        return rilRadioTechnologyToNetworkType(mRilVoiceRadioTechnology);
1090    }
1091
1092    /** @hide */
1093    public int getDataNetworkType() {
1094        return rilRadioTechnologyToNetworkType(mRilDataRadioTechnology);
1095    }
1096
1097    /** @hide */
1098    public int getVoiceNetworkType() {
1099        return rilRadioTechnologyToNetworkType(mRilVoiceRadioTechnology);
1100    }
1101
1102    /** @hide */
1103    public int getCssIndicator() {
1104        return this.mCssIndicator ? 1 : 0;
1105    }
1106
1107    /** @hide */
1108    public int getNetworkId() {
1109        return this.mNetworkId;
1110    }
1111
1112    /** @hide */
1113    public int getSystemId() {
1114        return this.mSystemId;
1115    }
1116
1117    /** @hide */
1118    public static boolean isGsm(int radioTechnology) {
1119        return radioTechnology == RIL_RADIO_TECHNOLOGY_GPRS
1120                || radioTechnology == RIL_RADIO_TECHNOLOGY_EDGE
1121                || radioTechnology == RIL_RADIO_TECHNOLOGY_UMTS
1122                || radioTechnology == RIL_RADIO_TECHNOLOGY_HSDPA
1123                || radioTechnology == RIL_RADIO_TECHNOLOGY_HSUPA
1124                || radioTechnology == RIL_RADIO_TECHNOLOGY_HSPA
1125                || radioTechnology == RIL_RADIO_TECHNOLOGY_LTE
1126                || radioTechnology == RIL_RADIO_TECHNOLOGY_HSPAP
1127                || radioTechnology == RIL_RADIO_TECHNOLOGY_GSM
1128                || radioTechnology == RIL_RADIO_TECHNOLOGY_TD_SCDMA
1129                || radioTechnology == RIL_RADIO_TECHNOLOGY_IWLAN;
1130    }
1131
1132    /** @hide */
1133    public static boolean isCdma(int radioTechnology) {
1134        return radioTechnology == RIL_RADIO_TECHNOLOGY_IS95A
1135                || radioTechnology == RIL_RADIO_TECHNOLOGY_IS95B
1136                || radioTechnology == RIL_RADIO_TECHNOLOGY_1xRTT
1137                || radioTechnology == RIL_RADIO_TECHNOLOGY_EVDO_0
1138                || radioTechnology == RIL_RADIO_TECHNOLOGY_EVDO_A
1139                || radioTechnology == RIL_RADIO_TECHNOLOGY_EVDO_B
1140                || radioTechnology == RIL_RADIO_TECHNOLOGY_EHRPD;
1141    }
1142
1143    /** @hide */
1144    public static boolean hasCdma(int radioTechnologyBitmask) {
1145        int cdmaBitmask = (RIL_RADIO_TECHNOLOGY_IS95A
1146                | RIL_RADIO_TECHNOLOGY_IS95B
1147                | RIL_RADIO_TECHNOLOGY_1xRTT
1148                | RIL_RADIO_TECHNOLOGY_EVDO_0
1149                | RIL_RADIO_TECHNOLOGY_EVDO_A
1150                | RIL_RADIO_TECHNOLOGY_EVDO_B
1151                | RIL_RADIO_TECHNOLOGY_EHRPD);
1152
1153        return ((radioTechnologyBitmask & cdmaBitmask) != 0);
1154    }
1155
1156    /** @hide */
1157    public static boolean bitmaskHasTech(int bearerBitmask, int radioTech) {
1158        if (bearerBitmask == 0) {
1159            return true;
1160        } else if (radioTech >= 1) {
1161            return ((bearerBitmask & (1 << (radioTech - 1))) != 0);
1162        }
1163        return false;
1164    }
1165
1166    /** @hide */
1167    public static int getBitmaskForTech(int radioTech) {
1168        if (radioTech >= 1) {
1169            return (1 << (radioTech - 1));
1170        }
1171        return 0;
1172    }
1173
1174    /** @hide */
1175    public static int getBitmaskFromString(String bearerList) {
1176        String[] bearers = bearerList.split("\\|");
1177        int bearerBitmask = 0;
1178        for (String bearer : bearers) {
1179            int bearerInt = 0;
1180            try {
1181                bearerInt = Integer.parseInt(bearer.trim());
1182            } catch (NumberFormatException nfe) {
1183                return 0;
1184            }
1185
1186            if (bearerInt == 0) {
1187                return 0;
1188            }
1189
1190            bearerBitmask |= getBitmaskForTech(bearerInt);
1191        }
1192        return bearerBitmask;
1193    }
1194
1195    /**
1196     * Returns a merged ServiceState consisting of the base SS with voice settings from the
1197     * voice SS. The voice SS is only used if it is IN_SERVICE (otherwise the base SS is returned).
1198     * @hide
1199     * */
1200    public static ServiceState mergeServiceStates(ServiceState baseSs, ServiceState voiceSs) {
1201        if (voiceSs.mVoiceRegState != STATE_IN_SERVICE) {
1202            return baseSs;
1203        }
1204
1205        ServiceState newSs = new ServiceState(baseSs);
1206
1207        // voice overrides
1208        newSs.mVoiceRegState = voiceSs.mVoiceRegState;
1209        newSs.mIsEmergencyOnly = false; // only get here if voice is IN_SERVICE
1210
1211        return newSs;
1212    }
1213}
1214