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