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