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 
17 package android.telephony;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.SystemApi;
23 import android.annotation.TestApi;
24 import android.compat.annotation.UnsupportedAppUsage;
25 import android.content.Intent;
26 import android.os.Build;
27 import android.os.Bundle;
28 import android.os.Parcel;
29 import android.os.Parcelable;
30 import android.telephony.AccessNetworkConstants.AccessNetworkType;
31 import android.telephony.AccessNetworkConstants.TransportType;
32 import android.telephony.Annotation.NetworkType;
33 import android.telephony.NetworkRegistrationInfo.Domain;
34 import android.telephony.NetworkRegistrationInfo.NRState;
35 import android.text.TextUtils;
36 
37 import com.android.telephony.Rlog;
38 
39 import java.lang.annotation.Retention;
40 import java.lang.annotation.RetentionPolicy;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.List;
44 import java.util.Objects;
45 import java.util.stream.Collectors;
46 
47 /**
48  * Contains phone state and service related information.
49  *
50  * The following phone information is included in returned ServiceState:
51  *
52  * <ul>
53  *   <li>Service state: IN_SERVICE, OUT_OF_SERVICE, EMERGENCY_ONLY, POWER_OFF
54  *   <li>Duplex mode: UNKNOWN, FDD, TDD
55  *   <li>Roaming indicator
56  *   <li>Operator name, short name and numeric id
57  *   <li>Network selection mode
58  * </ul>
59  *
60  * For historical reasons this class is not declared as final; however,
61  * it should be treated as though it were final.
62  */
63 public class ServiceState implements Parcelable {
64 
65     static final String LOG_TAG = "PHONE";
66     static final boolean DBG = false;
67     static final boolean VDBG = false;  // STOPSHIP if true
68 
69     /** @hide */
70     @Retention(RetentionPolicy.SOURCE)
71     @IntDef(prefix = "STATE_",
72             value = {STATE_IN_SERVICE, STATE_OUT_OF_SERVICE, STATE_EMERGENCY_ONLY,
73                     STATE_POWER_OFF})
74     public @interface RegState {}
75 
76     /**
77      * Normal operation condition, the phone is registered
78      * with an operator either in home network or in roaming.
79      */
80     public static final int STATE_IN_SERVICE = TelephonyProtoEnums.SERVICE_STATE_IN_SERVICE; // 0
81 
82     /**
83      * Phone is not registered with any operator, the phone
84      * can be currently searching a new operator to register to, or not
85      * searching to registration at all, or registration is denied, or radio
86      * signal is not available.
87      */
88     public static final int STATE_OUT_OF_SERVICE =
89             TelephonyProtoEnums.SERVICE_STATE_OUT_OF_SERVICE;  // 1
90 
91     /**
92      * The phone is registered and locked.  Only emergency numbers are allowed. {@more}
93      */
94     //TODO: This state is not used anymore. It should be deprecated in a future release.
95     public static final int STATE_EMERGENCY_ONLY =
96             TelephonyProtoEnums.SERVICE_STATE_EMERGENCY_ONLY;  // 2
97 
98     /**
99      * Radio of telephony is explicitly powered off.
100      */
101     public static final int STATE_POWER_OFF = TelephonyProtoEnums.SERVICE_STATE_POWER_OFF;  // 3
102 
103     /** @hide */
104     @Retention(RetentionPolicy.SOURCE)
105     @IntDef(prefix = "FREQUENCY_RANGE_",
106             value = {FREQUENCY_RANGE_UNKNOWN, FREQUENCY_RANGE_LOW, FREQUENCY_RANGE_MID,
107                     FREQUENCY_RANGE_HIGH, FREQUENCY_RANGE_MMWAVE})
108     public @interface FrequencyRange {}
109 
110     /**
111      * Indicates frequency range is unknown.
112      * @hide
113      */
114     public static final int FREQUENCY_RANGE_UNKNOWN = 0;
115 
116     /**
117      * Indicates the frequency range is below 1GHz.
118      * @hide
119      */
120     public static final int FREQUENCY_RANGE_LOW = 1;
121 
122     /**
123      * Indicates the frequency range is between 1GHz to 3GHz.
124      * @hide
125      */
126     public static final int FREQUENCY_RANGE_MID = 2;
127 
128     /**
129      * Indicates the frequency range is between 3GHz and 6GHz.
130      * @hide
131      */
132     public static final int FREQUENCY_RANGE_HIGH = 3;
133 
134     /**
135      * Indicates the frequency range is above 6GHz (millimeter wave frequency).
136      * @hide
137      */
138     public static final int FREQUENCY_RANGE_MMWAVE = 4;
139 
140     private static final List<Integer> FREQUENCY_RANGE_ORDER = Arrays.asList(
141             FREQUENCY_RANGE_UNKNOWN,
142             FREQUENCY_RANGE_LOW,
143             FREQUENCY_RANGE_MID,
144             FREQUENCY_RANGE_HIGH,
145             FREQUENCY_RANGE_MMWAVE);
146 
147     /** @hide */
148     @Retention(RetentionPolicy.SOURCE)
149     @IntDef(prefix = "DUPLEX_MODE_",
150             value = {DUPLEX_MODE_UNKNOWN, DUPLEX_MODE_FDD, DUPLEX_MODE_TDD})
151     public @interface DuplexMode {}
152 
153     /**
154      * Duplex mode for the phone is unknown.
155      */
156     public static final int DUPLEX_MODE_UNKNOWN = 0;
157 
158     /**
159      * Duplex mode for the phone is frequency-division duplexing.
160      */
161     public static final int DUPLEX_MODE_FDD = 1;
162 
163     /**
164      * Duplex mode for the phone is time-division duplexing.
165      */
166     public static final int DUPLEX_MODE_TDD = 2;
167 
168     /**
169      * Available radio technologies for GSM, UMTS and CDMA.
170      * Duplicates the constants from hardware/radio/include/ril.h
171      * This should only be used by agents working with the ril.  Others
172      * should use the equivalent TelephonyManager.NETWORK_TYPE_*
173      */
174     /** @hide */
175     public static final int RIL_RADIO_TECHNOLOGY_UNKNOWN = 0;
176     /** @hide */
177     public static final int RIL_RADIO_TECHNOLOGY_GPRS = 1;
178     /** @hide */
179     public static final int RIL_RADIO_TECHNOLOGY_EDGE = 2;
180     /** @hide */
181     public static final int RIL_RADIO_TECHNOLOGY_UMTS = 3;
182     /** @hide */
183     public static final int RIL_RADIO_TECHNOLOGY_IS95A = 4;
184     /** @hide */
185     public static final int RIL_RADIO_TECHNOLOGY_IS95B = 5;
186     /** @hide */
187     public static final int RIL_RADIO_TECHNOLOGY_1xRTT = 6;
188     /** @hide */
189     public static final int RIL_RADIO_TECHNOLOGY_EVDO_0 = 7;
190     /** @hide */
191     public static final int RIL_RADIO_TECHNOLOGY_EVDO_A = 8;
192     /** @hide */
193     public static final int RIL_RADIO_TECHNOLOGY_HSDPA = 9;
194     /** @hide */
195     public static final int RIL_RADIO_TECHNOLOGY_HSUPA = 10;
196     /** @hide */
197     public static final int RIL_RADIO_TECHNOLOGY_HSPA = 11;
198     /** @hide */
199     public static final int RIL_RADIO_TECHNOLOGY_EVDO_B = 12;
200     /** @hide */
201     public static final int RIL_RADIO_TECHNOLOGY_EHRPD = 13;
202     /** @hide */
203     public static final int RIL_RADIO_TECHNOLOGY_LTE = 14;
204     /** @hide */
205     public static final int RIL_RADIO_TECHNOLOGY_HSPAP = 15;
206     /**
207      * GSM radio technology only supports voice. It does not support data.
208      * @hide
209      */
210     public static final int RIL_RADIO_TECHNOLOGY_GSM = 16;
211     /** @hide */
212     public static final int RIL_RADIO_TECHNOLOGY_TD_SCDMA = 17;
213     /**
214      * IWLAN
215      * @hide
216      */
217     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
218     public static final int RIL_RADIO_TECHNOLOGY_IWLAN = 18;
219 
220     /**
221      * LTE_CA
222      * @hide
223      */
224     public static final int RIL_RADIO_TECHNOLOGY_LTE_CA = 19;
225 
226     /**
227      * NR(New Radio) 5G.
228      * @hide
229      */
230     public static final int  RIL_RADIO_TECHNOLOGY_NR = 20;
231 
232     /**
233      * RIL Radio Annotation
234      * @hide
235      */
236     @Retention(RetentionPolicy.SOURCE)
237     @IntDef(prefix = {"RIL_RADIO_TECHNOLOGY_" }, value = {
238         ServiceState.RIL_RADIO_TECHNOLOGY_UNKNOWN,
239         ServiceState.RIL_RADIO_TECHNOLOGY_GPRS,
240         ServiceState.RIL_RADIO_TECHNOLOGY_EDGE,
241         ServiceState.RIL_RADIO_TECHNOLOGY_UMTS,
242         ServiceState.RIL_RADIO_TECHNOLOGY_IS95A,
243         ServiceState.RIL_RADIO_TECHNOLOGY_IS95B,
244         ServiceState.RIL_RADIO_TECHNOLOGY_1xRTT,
245         ServiceState.RIL_RADIO_TECHNOLOGY_EVDO_0,
246         ServiceState.RIL_RADIO_TECHNOLOGY_EVDO_A,
247         ServiceState.RIL_RADIO_TECHNOLOGY_HSDPA,
248         ServiceState.RIL_RADIO_TECHNOLOGY_HSUPA,
249         ServiceState.RIL_RADIO_TECHNOLOGY_HSPA,
250         ServiceState.RIL_RADIO_TECHNOLOGY_EVDO_B,
251         ServiceState.RIL_RADIO_TECHNOLOGY_EHRPD,
252         ServiceState.RIL_RADIO_TECHNOLOGY_LTE,
253         ServiceState.RIL_RADIO_TECHNOLOGY_HSPAP,
254         ServiceState.RIL_RADIO_TECHNOLOGY_GSM,
255         ServiceState.RIL_RADIO_TECHNOLOGY_TD_SCDMA,
256         ServiceState.RIL_RADIO_TECHNOLOGY_IWLAN,
257         ServiceState.RIL_RADIO_TECHNOLOGY_LTE_CA,
258         ServiceState.RIL_RADIO_TECHNOLOGY_NR})
259     public @interface RilRadioTechnology {}
260 
261 
262     /**
263      * The number of the radio technologies.
264      */
265     private static final int NEXT_RIL_RADIO_TECHNOLOGY = 21;
266 
267     /** @hide */
268     public static final int RIL_RADIO_CDMA_TECHNOLOGY_BITMASK =
269             (1 << (RIL_RADIO_TECHNOLOGY_IS95A - 1))
270                     | (1 << (RIL_RADIO_TECHNOLOGY_IS95B - 1))
271                     | (1 << (RIL_RADIO_TECHNOLOGY_1xRTT - 1))
272                     | (1 << (RIL_RADIO_TECHNOLOGY_EVDO_0 - 1))
273                     | (1 << (RIL_RADIO_TECHNOLOGY_EVDO_A - 1))
274                     | (1 << (RIL_RADIO_TECHNOLOGY_EVDO_B - 1))
275                     | (1 << (RIL_RADIO_TECHNOLOGY_EHRPD - 1));
276 
277     private int mVoiceRegState = STATE_OUT_OF_SERVICE;
278     private int mDataRegState = STATE_OUT_OF_SERVICE;
279 
280     /** @hide */
281     @Retention(RetentionPolicy.SOURCE)
282     @IntDef(prefix = { "ROAMING_TYPE_" }, value = {
283             ROAMING_TYPE_NOT_ROAMING,
284             ROAMING_TYPE_UNKNOWN,
285             ROAMING_TYPE_DOMESTIC,
286             ROAMING_TYPE_INTERNATIONAL
287     })
288     public @interface RoamingType {}
289 
290     /**
291      * Not roaming, registered in home network.
292      * @hide
293      */
294     @SystemApi
295     public static final int ROAMING_TYPE_NOT_ROAMING = 0;
296     /**
297      * registered in a roaming network, but can not tell if it's domestic or international.
298      * @hide
299      */
300     @SystemApi
301     public static final int ROAMING_TYPE_UNKNOWN = 1;
302     /**
303      * registered in a domestic roaming network
304      * @hide
305      */
306     @SystemApi
307     public static final int ROAMING_TYPE_DOMESTIC = 2;
308     /**
309      * registered in an international roaming network
310      * @hide
311      */
312     @SystemApi
313     public static final int ROAMING_TYPE_INTERNATIONAL = 3;
314 
315     /**
316      * Unknown ID. Could be returned by {@link #getCdmaNetworkId()} or {@link #getCdmaSystemId()}
317      */
318     public static final int UNKNOWN_ID = -1;
319 
320     /**
321      * A parcelable extra used with {@link Intent#ACTION_SERVICE_STATE} representing the service
322      * state.
323      * @hide
324      */
325     private static final String EXTRA_SERVICE_STATE = "android.intent.extra.SERVICE_STATE";
326 
327 
328     private String mOperatorAlphaLong;
329     private String mOperatorAlphaShort;
330     private String mOperatorNumeric;
331     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
332     private boolean mIsManualNetworkSelection;
333 
334     private boolean mIsEmergencyOnly;
335 
336     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
337     private boolean mCssIndicator;
338     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
339     private int mNetworkId;
340     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
341     private int mSystemId;
342     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
343     private int mCdmaRoamingIndicator;
344     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
345     private int mCdmaDefaultRoamingIndicator;
346     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
347     private int mCdmaEriIconIndex;
348     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
349     private int mCdmaEriIconMode;
350 
351     @FrequencyRange
352     private int mNrFrequencyRange;
353     private int mChannelNumber;
354     private int[] mCellBandwidths = new int[0];
355 
356     /* EARFCN stands for E-UTRA Absolute Radio Frequency Channel Number,
357      * Reference: 3GPP TS 36.104 5.4.3 */
358     private int mLteEarfcnRsrpBoost = 0;
359 
360     private final List<NetworkRegistrationInfo> mNetworkRegistrationInfos = new ArrayList<>();
361 
362     private String mOperatorAlphaLongRaw;
363     private String mOperatorAlphaShortRaw;
364     private boolean mIsDataRoamingFromRegistration;
365     private boolean mIsIwlanPreferred;
366 
367     /**
368      * get String description of roaming type
369      * @hide
370      */
getRoamingLogString(int roamingType)371     public static final String getRoamingLogString(int roamingType) {
372         switch (roamingType) {
373             case ROAMING_TYPE_NOT_ROAMING:
374                 return "home";
375 
376             case ROAMING_TYPE_UNKNOWN:
377                 return "roaming";
378 
379             case ROAMING_TYPE_DOMESTIC:
380                 return "Domestic Roaming";
381 
382             case ROAMING_TYPE_INTERNATIONAL:
383                 return "International Roaming";
384 
385             default:
386                 return "UNKNOWN";
387         }
388     }
389 
390     /**
391      * Create a new ServiceState from a intent notifier Bundle
392      *
393      * This method is used to get ServiceState object from extras upon receiving
394      * {@link Intent#ACTION_SERVICE_STATE}.
395      *
396      * @param m Bundle from intent notifier
397      * @return newly created ServiceState
398      * @hide
399      */
400     @NonNull
401     @UnsupportedAppUsage
newFromBundle(@onNull Bundle m)402     public static ServiceState newFromBundle(@NonNull Bundle m) {
403         ServiceState ret;
404         ret = new ServiceState();
405         ret.setFromNotifierBundle(m);
406         return ret;
407     }
408 
409     /**
410      * Empty constructor
411      */
ServiceState()412     public ServiceState() {
413     }
414 
415     /**
416      * Copy constructors
417      *
418      * @param s Source service state
419      */
ServiceState(ServiceState s)420     public ServiceState(ServiceState s) {
421         copyFrom(s);
422     }
423 
copyFrom(ServiceState s)424     protected void copyFrom(ServiceState s) {
425         mVoiceRegState = s.mVoiceRegState;
426         mDataRegState = s.mDataRegState;
427         mOperatorAlphaLong = s.mOperatorAlphaLong;
428         mOperatorAlphaShort = s.mOperatorAlphaShort;
429         mOperatorNumeric = s.mOperatorNumeric;
430         mIsManualNetworkSelection = s.mIsManualNetworkSelection;
431         mCssIndicator = s.mCssIndicator;
432         mNetworkId = s.mNetworkId;
433         mSystemId = s.mSystemId;
434         mCdmaRoamingIndicator = s.mCdmaRoamingIndicator;
435         mCdmaDefaultRoamingIndicator = s.mCdmaDefaultRoamingIndicator;
436         mCdmaEriIconIndex = s.mCdmaEriIconIndex;
437         mCdmaEriIconMode = s.mCdmaEriIconMode;
438         mIsEmergencyOnly = s.mIsEmergencyOnly;
439         mChannelNumber = s.mChannelNumber;
440         mCellBandwidths = s.mCellBandwidths == null ? null :
441                 Arrays.copyOf(s.mCellBandwidths, s.mCellBandwidths.length);
442         mLteEarfcnRsrpBoost = s.mLteEarfcnRsrpBoost;
443         synchronized (mNetworkRegistrationInfos) {
444             mNetworkRegistrationInfos.clear();
445             mNetworkRegistrationInfos.addAll(s.getNetworkRegistrationInfoList());
446         }
447         mNrFrequencyRange = s.mNrFrequencyRange;
448         mOperatorAlphaLongRaw = s.mOperatorAlphaLongRaw;
449         mOperatorAlphaShortRaw = s.mOperatorAlphaShortRaw;
450         mIsDataRoamingFromRegistration = s.mIsDataRoamingFromRegistration;
451         mIsIwlanPreferred = s.mIsIwlanPreferred;
452     }
453 
454     /**
455      * Construct a ServiceState object from the given parcel.
456      *
457      * @deprecated The constructor takes parcel should not be public at the beginning. Use
458      * {@link #ServiceState()} instead.
459      */
460     @Deprecated
ServiceState(Parcel in)461     public ServiceState(Parcel in) {
462         mVoiceRegState = in.readInt();
463         mDataRegState = in.readInt();
464         mOperatorAlphaLong = in.readString();
465         mOperatorAlphaShort = in.readString();
466         mOperatorNumeric = in.readString();
467         mIsManualNetworkSelection = in.readInt() != 0;
468         mCssIndicator = (in.readInt() != 0);
469         mNetworkId = in.readInt();
470         mSystemId = in.readInt();
471         mCdmaRoamingIndicator = in.readInt();
472         mCdmaDefaultRoamingIndicator = in.readInt();
473         mCdmaEriIconIndex = in.readInt();
474         mCdmaEriIconMode = in.readInt();
475         mIsEmergencyOnly = in.readInt() != 0;
476         mLteEarfcnRsrpBoost = in.readInt();
477         synchronized (mNetworkRegistrationInfos) {
478             in.readList(mNetworkRegistrationInfos, NetworkRegistrationInfo.class.getClassLoader());
479         }
480         mChannelNumber = in.readInt();
481         mCellBandwidths = in.createIntArray();
482         mNrFrequencyRange = in.readInt();
483         mOperatorAlphaLongRaw = in.readString();
484         mOperatorAlphaShortRaw = in.readString();
485         mIsDataRoamingFromRegistration = in.readBoolean();
486         mIsIwlanPreferred = in.readBoolean();
487     }
488 
writeToParcel(Parcel out, int flags)489     public void writeToParcel(Parcel out, int flags) {
490         out.writeInt(mVoiceRegState);
491         out.writeInt(mDataRegState);
492         out.writeString(mOperatorAlphaLong);
493         out.writeString(mOperatorAlphaShort);
494         out.writeString(mOperatorNumeric);
495         out.writeInt(mIsManualNetworkSelection ? 1 : 0);
496         out.writeInt(mCssIndicator ? 1 : 0);
497         out.writeInt(mNetworkId);
498         out.writeInt(mSystemId);
499         out.writeInt(mCdmaRoamingIndicator);
500         out.writeInt(mCdmaDefaultRoamingIndicator);
501         out.writeInt(mCdmaEriIconIndex);
502         out.writeInt(mCdmaEriIconMode);
503         out.writeInt(mIsEmergencyOnly ? 1 : 0);
504         out.writeInt(mLteEarfcnRsrpBoost);
505         synchronized (mNetworkRegistrationInfos) {
506             out.writeList(mNetworkRegistrationInfos);
507         }
508         out.writeInt(mChannelNumber);
509         out.writeIntArray(mCellBandwidths);
510         out.writeInt(mNrFrequencyRange);
511         out.writeString(mOperatorAlphaLongRaw);
512         out.writeString(mOperatorAlphaShortRaw);
513         out.writeBoolean(mIsDataRoamingFromRegistration);
514         out.writeBoolean(mIsIwlanPreferred);
515     }
516 
describeContents()517     public int describeContents() {
518         return 0;
519     }
520 
521     public static final @android.annotation.NonNull Parcelable.Creator<ServiceState> CREATOR =
522             new Parcelable.Creator<ServiceState>() {
523         public ServiceState createFromParcel(Parcel in) {
524             return new ServiceState(in);
525         }
526 
527         public ServiceState[] newArray(int size) {
528             return new ServiceState[size];
529         }
530     };
531 
532     /**
533      * Get current voice service state
534      */
getState()535     public int getState() {
536         return getVoiceRegState();
537     }
538 
539     /**
540      * Get current voice service state
541      *
542      * @see #STATE_IN_SERVICE
543      * @see #STATE_OUT_OF_SERVICE
544      * @see #STATE_EMERGENCY_ONLY
545      * @see #STATE_POWER_OFF
546      *
547      * @hide
548      */
549     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
getVoiceRegState()550     public int getVoiceRegState() {
551         return mVoiceRegState;
552     }
553 
554     /**
555      * Get current data registration state.
556      *
557      * @see #STATE_IN_SERVICE
558      * @see #STATE_OUT_OF_SERVICE
559      * @see #STATE_EMERGENCY_ONLY
560      * @see #STATE_POWER_OFF
561      *
562      * @return current data registration state
563      *
564      * @hide
565      */
566     @UnsupportedAppUsage
getDataRegState()567     public int getDataRegState() {
568         return mDataRegState;
569     }
570 
571     /**
572      * Get current data registration state.
573      *
574      * @see #STATE_IN_SERVICE
575      * @see #STATE_OUT_OF_SERVICE
576      * @see #STATE_EMERGENCY_ONLY
577      * @see #STATE_POWER_OFF
578      *
579      * @return current data registration state
580      *
581      * @hide
582      */
getDataRegistrationState()583     public @RegState int getDataRegistrationState() {
584         return getDataRegState();
585     }
586 
587     /**
588      * Get the current duplex mode
589      *
590      * @see #DUPLEX_MODE_UNKNOWN
591      * @see #DUPLEX_MODE_FDD
592      * @see #DUPLEX_MODE_TDD
593      *
594      * @return Current {@code DuplexMode} for the phone
595      */
596     @DuplexMode
getDuplexMode()597     public int getDuplexMode() {
598         // support LTE/NR duplex mode
599         if (!isPsOnlyTech(getRilDataRadioTechnology())) {
600             return DUPLEX_MODE_UNKNOWN;
601         }
602 
603         int band = AccessNetworkUtils.getOperatingBandForEarfcn(mChannelNumber);
604         return AccessNetworkUtils.getDuplexModeForEutranBand(band);
605     }
606 
607     /**
608      * Get the channel number of the current primary serving cell, or -1 if unknown
609      *
610      * <p>This is EARFCN for LTE, UARFCN for UMTS, and ARFCN for GSM.
611      *
612      * @return Channel number of primary serving cell
613      */
getChannelNumber()614     public int getChannelNumber() {
615         return mChannelNumber;
616     }
617 
618     /**
619      * Get an array of cell bandwidths (kHz) for the current serving cells
620      *
621      * @return Current serving cell bandwidths
622      */
getCellBandwidths()623     public int[] getCellBandwidths() {
624         return mCellBandwidths == null ? new int[0] : mCellBandwidths;
625     }
626 
627     /**
628      * Get current roaming indicator of phone
629      * (note: not just decoding from TS 27.007 7.2)
630      *
631      * @return true if TS 27.007 7.2 roaming is true
632      *              and ONS is different from SPN
633      */
getRoaming()634     public boolean getRoaming() {
635         return getVoiceRoaming() || getDataRoaming();
636     }
637 
638     /**
639      * Get current voice network roaming status
640      * @return roaming status
641      * @hide
642      */
643     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
getVoiceRoaming()644     public boolean getVoiceRoaming() {
645         return getVoiceRoamingType() != ROAMING_TYPE_NOT_ROAMING;
646     }
647     /**
648      * Get current voice network roaming type
649      * @return roaming type
650      * @hide
651      */
652     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
getVoiceRoamingType()653     public @RoamingType int getVoiceRoamingType() {
654         final NetworkRegistrationInfo regState = getNetworkRegistrationInfo(
655                 NetworkRegistrationInfo.DOMAIN_CS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
656         if (regState != null) {
657             return regState.getRoamingType();
658         }
659         return ROAMING_TYPE_NOT_ROAMING;
660     }
661 
662     /**
663      * Get whether the current data network is roaming.
664      * This value may be overwritten by resource overlay or carrier configuration.
665      * @see #getDataRoamingFromRegistration() to get the value from the network registration.
666      * @return roaming type
667      * @hide
668      */
669     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
getDataRoaming()670     public boolean getDataRoaming() {
671         return getDataRoamingType() != ROAMING_TYPE_NOT_ROAMING;
672     }
673 
674     /**
675      * Set whether the data network registration state is roaming.
676      * This should only be set to the roaming value received
677      * once the data registration phase has completed.
678      * @hide
679      */
setDataRoamingFromRegistration(boolean dataRoaming)680     public void setDataRoamingFromRegistration(boolean dataRoaming) {
681         mIsDataRoamingFromRegistration = dataRoaming;
682     }
683 
684     /**
685      * Get whether data network registration state is roaming.
686      * This value is set directly from the modem and will not be overwritten
687      * by resource overlay or carrier configuration.
688      * @return true if registration indicates roaming, false otherwise
689      * @hide
690      */
getDataRoamingFromRegistration()691     public boolean getDataRoamingFromRegistration() {
692         // TODO: all callers should refactor to get roaming state directly from modem
693         // this should not be exposed as a public API
694         return mIsDataRoamingFromRegistration;
695     }
696 
697     /**
698      * Get current data network roaming type
699      * @return roaming type
700      * @hide
701      */
702     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
getDataRoamingType()703     public @RoamingType int getDataRoamingType() {
704         final NetworkRegistrationInfo regState = getNetworkRegistrationInfo(
705                 NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
706         if (regState != null) {
707             return regState.getRoamingType();
708         }
709         return ROAMING_TYPE_NOT_ROAMING;
710     }
711 
712     /**
713      * @hide
714      */
715     @UnsupportedAppUsage
isEmergencyOnly()716     public boolean isEmergencyOnly() {
717         return mIsEmergencyOnly;
718     }
719 
720     /**
721      * @hide
722      */
723     @UnsupportedAppUsage
getCdmaRoamingIndicator()724     public int getCdmaRoamingIndicator(){
725         return this.mCdmaRoamingIndicator;
726     }
727 
728     /**
729      * @hide
730      */
731     @UnsupportedAppUsage
getCdmaDefaultRoamingIndicator()732     public int getCdmaDefaultRoamingIndicator(){
733         return this.mCdmaDefaultRoamingIndicator;
734     }
735 
736     /**
737      * @hide
738      */
739     @UnsupportedAppUsage
getCdmaEriIconIndex()740     public int getCdmaEriIconIndex() {
741         return this.mCdmaEriIconIndex;
742     }
743 
744     /**
745      * @hide
746      */
747     @UnsupportedAppUsage
getCdmaEriIconMode()748     public int getCdmaEriIconMode() {
749         return this.mCdmaEriIconMode;
750     }
751 
752     /**
753      * Get current registered operator name in long alphanumeric format.
754      *
755      * In GSM/UMTS, long format can be up to 16 characters long.
756      * In CDMA, returns the ERI text, if set. Otherwise, returns the ONS.
757      *
758      * @return long name of operator, null if unregistered or unknown
759      */
getOperatorAlphaLong()760     public String getOperatorAlphaLong() {
761         return mOperatorAlphaLong;
762     }
763 
764     /**
765      * Get current registered voice network operator name in long alphanumeric format.
766      * @return long name of operator
767      * @hide
768      */
769     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.Q,
770             publicAlternatives = "Use {@link #getOperatorAlphaLong} instead.")
getVoiceOperatorAlphaLong()771     public String getVoiceOperatorAlphaLong() {
772         return mOperatorAlphaLong;
773     }
774 
775     /**
776      * Get current registered operator name in short alphanumeric format.
777      *
778      * In GSM/UMTS, short format can be up to 8 characters long.
779      *
780      * @return short name of operator, null if unregistered or unknown
781      */
getOperatorAlphaShort()782     public String getOperatorAlphaShort() {
783         return mOperatorAlphaShort;
784     }
785 
786     /**
787      * Get current registered voice network operator name in short alphanumeric format.
788      * @return short name of operator, null if unregistered or unknown
789      * @hide
790      */
791     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.Q,
792             publicAlternatives = "Use {@link #getOperatorAlphaShort} instead.")
getVoiceOperatorAlphaShort()793     public String getVoiceOperatorAlphaShort() {
794         return mOperatorAlphaShort;
795     }
796 
797     /**
798      * Get current registered data network operator name in short alphanumeric format.
799      * @return short name of operator, null if unregistered or unknown
800      * @hide
801      */
802     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.Q,
803             publicAlternatives = "Use {@link #getOperatorAlphaShort} instead.")
getDataOperatorAlphaShort()804     public String getDataOperatorAlphaShort() {
805         return mOperatorAlphaShort;
806     }
807 
808     /**
809      * Get current registered operator name in long alphanumeric format if
810      * available or short otherwise.
811      *
812      * @see #getOperatorAlphaLong
813      * @see #getOperatorAlphaShort
814      *
815      * @return name of operator, null if unregistered or unknown
816      * @hide
817      */
getOperatorAlpha()818     public String getOperatorAlpha() {
819         if (TextUtils.isEmpty(mOperatorAlphaLong)) {
820             return mOperatorAlphaShort;
821         }
822 
823         return mOperatorAlphaLong;
824     }
825 
826     /**
827      * Get current registered operator numeric id.
828      *
829      * In GSM/UMTS, numeric format is 3 digit country code plus 2 or 3 digit
830      * network code.
831      *
832      * @return numeric format of operator, null if unregistered or unknown
833      */
834     /*
835      * The country code can be decoded using
836      * {@link com.android.internal.telephony.MccTable#countryCodeForMcc(int)}.
837      */
getOperatorNumeric()838     public String getOperatorNumeric() {
839         return mOperatorNumeric;
840     }
841 
842     /**
843      * Get current registered voice network operator numeric id.
844      * @return numeric format of operator, null if unregistered or unknown
845      * @hide
846      */
847     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
getVoiceOperatorNumeric()848     public String getVoiceOperatorNumeric() {
849         return mOperatorNumeric;
850     }
851 
852     /**
853      * Get current registered data network operator numeric id.
854      * @return numeric format of operator, null if unregistered or unknown
855      * @hide
856      */
857     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.Q,
858             publicAlternatives = "Use {@link #getOperatorNumeric} instead.")
getDataOperatorNumeric()859     public String getDataOperatorNumeric() {
860         return mOperatorNumeric;
861     }
862 
863     /**
864      * Get current network selection mode.
865      *
866      * @return true if manual mode, false if automatic mode
867      */
getIsManualSelection()868     public boolean getIsManualSelection() {
869         return mIsManualNetworkSelection;
870     }
871 
872     @Override
hashCode()873     public int hashCode() {
874         synchronized (mNetworkRegistrationInfos) {
875             return Objects.hash(
876                     mVoiceRegState,
877                     mDataRegState,
878                     mChannelNumber,
879                     Arrays.hashCode(mCellBandwidths),
880                     mOperatorAlphaLong,
881                     mOperatorAlphaShort,
882                     mOperatorNumeric,
883                     mIsManualNetworkSelection,
884                     mCssIndicator,
885                     mNetworkId,
886                     mSystemId,
887                     mCdmaRoamingIndicator,
888                     mCdmaDefaultRoamingIndicator,
889                     mCdmaEriIconIndex,
890                     mCdmaEriIconMode,
891                     mIsEmergencyOnly,
892                     mLteEarfcnRsrpBoost,
893                     mNetworkRegistrationInfos,
894                     mNrFrequencyRange,
895                     mOperatorAlphaLongRaw,
896                     mOperatorAlphaShortRaw,
897                     mIsDataRoamingFromRegistration,
898                     mIsIwlanPreferred);
899         }
900     }
901 
902     @Override
equals(Object o)903     public boolean equals (Object o) {
904         if (!(o instanceof ServiceState)) return false;
905         ServiceState s = (ServiceState) o;
906 
907         synchronized (mNetworkRegistrationInfos) {
908             return mVoiceRegState == s.mVoiceRegState
909                     && mDataRegState == s.mDataRegState
910                     && mIsManualNetworkSelection == s.mIsManualNetworkSelection
911                     && mChannelNumber == s.mChannelNumber
912                     && Arrays.equals(mCellBandwidths, s.mCellBandwidths)
913                     && equalsHandlesNulls(mOperatorAlphaLong, s.mOperatorAlphaLong)
914                     && equalsHandlesNulls(mOperatorAlphaShort, s.mOperatorAlphaShort)
915                     && equalsHandlesNulls(mOperatorNumeric, s.mOperatorNumeric)
916                     && equalsHandlesNulls(mCssIndicator, s.mCssIndicator)
917                     && equalsHandlesNulls(mNetworkId, s.mNetworkId)
918                     && equalsHandlesNulls(mSystemId, s.mSystemId)
919                     && equalsHandlesNulls(mCdmaRoamingIndicator, s.mCdmaRoamingIndicator)
920                     && equalsHandlesNulls(mCdmaDefaultRoamingIndicator,
921                     s.mCdmaDefaultRoamingIndicator)
922                     && mIsEmergencyOnly == s.mIsEmergencyOnly
923                     && equalsHandlesNulls(mOperatorAlphaLongRaw, s.mOperatorAlphaLongRaw)
924                     && equalsHandlesNulls(mOperatorAlphaShortRaw, s.mOperatorAlphaShortRaw)
925                     && mNetworkRegistrationInfos.size() == s.mNetworkRegistrationInfos.size()
926                     && mNetworkRegistrationInfos.containsAll(s.mNetworkRegistrationInfos)
927                     && mNrFrequencyRange == s.mNrFrequencyRange
928                     && mIsDataRoamingFromRegistration == s.mIsDataRoamingFromRegistration
929                     && mIsIwlanPreferred == s.mIsIwlanPreferred;
930         }
931     }
932 
933     /**
934      * Convert roaming type to string
935      *
936      * @param roamingType roaming type
937      * @return The roaming type in string format
938      *
939      * @hide
940      */
roamingTypeToString(@oamingType int roamingType)941     public static String roamingTypeToString(@RoamingType int roamingType) {
942         switch (roamingType) {
943             case ROAMING_TYPE_NOT_ROAMING: return "NOT_ROAMING";
944             case ROAMING_TYPE_UNKNOWN: return "UNKNOWN";
945             case ROAMING_TYPE_DOMESTIC: return "DOMESTIC";
946             case ROAMING_TYPE_INTERNATIONAL: return "INTERNATIONAL";
947         }
948         return "Unknown roaming type " + roamingType;
949     }
950 
951     /**
952      * Convert radio technology to String
953      *
954      * @param rt radioTechnology
955      * @return String representation of the RAT
956      *
957      * @hide
958      */
959     @UnsupportedAppUsage
rilRadioTechnologyToString(int rt)960     public static String rilRadioTechnologyToString(int rt) {
961         String rtString;
962 
963         switch(rt) {
964             case RIL_RADIO_TECHNOLOGY_UNKNOWN:
965                 rtString = "Unknown";
966                 break;
967             case RIL_RADIO_TECHNOLOGY_GPRS:
968                 rtString = "GPRS";
969                 break;
970             case RIL_RADIO_TECHNOLOGY_EDGE:
971                 rtString = "EDGE";
972                 break;
973             case RIL_RADIO_TECHNOLOGY_UMTS:
974                 rtString = "UMTS";
975                 break;
976             case RIL_RADIO_TECHNOLOGY_IS95A:
977                 rtString = "CDMA-IS95A";
978                 break;
979             case RIL_RADIO_TECHNOLOGY_IS95B:
980                 rtString = "CDMA-IS95B";
981                 break;
982             case RIL_RADIO_TECHNOLOGY_1xRTT:
983                 rtString = "1xRTT";
984                 break;
985             case RIL_RADIO_TECHNOLOGY_EVDO_0:
986                 rtString = "EvDo-rev.0";
987                 break;
988             case RIL_RADIO_TECHNOLOGY_EVDO_A:
989                 rtString = "EvDo-rev.A";
990                 break;
991             case RIL_RADIO_TECHNOLOGY_HSDPA:
992                 rtString = "HSDPA";
993                 break;
994             case RIL_RADIO_TECHNOLOGY_HSUPA:
995                 rtString = "HSUPA";
996                 break;
997             case RIL_RADIO_TECHNOLOGY_HSPA:
998                 rtString = "HSPA";
999                 break;
1000             case RIL_RADIO_TECHNOLOGY_EVDO_B:
1001                 rtString = "EvDo-rev.B";
1002                 break;
1003             case RIL_RADIO_TECHNOLOGY_EHRPD:
1004                 rtString = "eHRPD";
1005                 break;
1006             case RIL_RADIO_TECHNOLOGY_LTE:
1007                 rtString = "LTE";
1008                 break;
1009             case RIL_RADIO_TECHNOLOGY_HSPAP:
1010                 rtString = "HSPAP";
1011                 break;
1012             case RIL_RADIO_TECHNOLOGY_GSM:
1013                 rtString = "GSM";
1014                 break;
1015             case RIL_RADIO_TECHNOLOGY_IWLAN:
1016                 rtString = "IWLAN";
1017                 break;
1018             case RIL_RADIO_TECHNOLOGY_TD_SCDMA:
1019                 rtString = "TD-SCDMA";
1020                 break;
1021             case RIL_RADIO_TECHNOLOGY_LTE_CA:
1022                 rtString = "LTE_CA";
1023                 break;
1024             case RIL_RADIO_TECHNOLOGY_NR:
1025                 rtString = "NR_SA";
1026                 break;
1027             default:
1028                 rtString = "Unexpected";
1029                 Rlog.w(LOG_TAG, "Unexpected radioTechnology=" + rt);
1030                 break;
1031         }
1032         return rtString;
1033     }
1034 
1035     /**
1036      * Convert frequency range into string
1037      *
1038      * @param range The cellular frequency range
1039      * @return Frequency range in string format
1040      *
1041      * @hide
1042      */
frequencyRangeToString(@requencyRange int range)1043     public static @NonNull String frequencyRangeToString(@FrequencyRange int range) {
1044         switch (range) {
1045             case FREQUENCY_RANGE_UNKNOWN: return "UNKNOWN";
1046             case FREQUENCY_RANGE_LOW: return "LOW";
1047             case FREQUENCY_RANGE_MID: return "MID";
1048             case FREQUENCY_RANGE_HIGH: return "HIGH";
1049             case FREQUENCY_RANGE_MMWAVE: return "MMWAVE";
1050             default:
1051                 return Integer.toString(range);
1052         }
1053     }
1054 
1055     /**
1056      * Convert RIL Service State to String
1057      *
1058      * @param serviceState
1059      * @return String representation of the ServiceState
1060      *
1061      * @hide
1062      */
rilServiceStateToString(int serviceState)1063     public static String rilServiceStateToString(int serviceState) {
1064         switch(serviceState) {
1065             case STATE_IN_SERVICE:
1066                 return "IN_SERVICE";
1067             case STATE_OUT_OF_SERVICE:
1068                 return "OUT_OF_SERVICE";
1069             case STATE_EMERGENCY_ONLY:
1070                 return "EMERGENCY_ONLY";
1071             case STATE_POWER_OFF:
1072                 return "POWER_OFF";
1073             default:
1074                 return "UNKNOWN";
1075         }
1076     }
1077 
1078     @Override
toString()1079     public String toString() {
1080         synchronized (mNetworkRegistrationInfos) {
1081             return new StringBuilder().append("{mVoiceRegState=").append(mVoiceRegState)
1082                     .append("(" + rilServiceStateToString(mVoiceRegState) + ")")
1083                     .append(", mDataRegState=").append(mDataRegState)
1084                     .append("(" + rilServiceStateToString(mDataRegState) + ")")
1085                     .append(", mChannelNumber=").append(mChannelNumber)
1086                     .append(", duplexMode()=").append(getDuplexMode())
1087                     .append(", mCellBandwidths=").append(Arrays.toString(mCellBandwidths))
1088                     .append(", mOperatorAlphaLong=").append(mOperatorAlphaLong)
1089                     .append(", mOperatorAlphaShort=").append(mOperatorAlphaShort)
1090                     .append(", isManualNetworkSelection=").append(mIsManualNetworkSelection)
1091                     .append(mIsManualNetworkSelection ? "(manual)" : "(automatic)")
1092                     .append(", getRilVoiceRadioTechnology=").append(getRilVoiceRadioTechnology())
1093                     .append("(" + rilRadioTechnologyToString(getRilVoiceRadioTechnology()) + ")")
1094                     .append(", getRilDataRadioTechnology=").append(getRilDataRadioTechnology())
1095                     .append("(" + rilRadioTechnologyToString(getRilDataRadioTechnology()) + ")")
1096                     .append(", mCssIndicator=").append(mCssIndicator ? "supported" : "unsupported")
1097                     .append(", mNetworkId=").append(mNetworkId)
1098                     .append(", mSystemId=").append(mSystemId)
1099                     .append(", mCdmaRoamingIndicator=").append(mCdmaRoamingIndicator)
1100                     .append(", mCdmaDefaultRoamingIndicator=").append(mCdmaDefaultRoamingIndicator)
1101                     .append(", mIsEmergencyOnly=").append(mIsEmergencyOnly)
1102                     .append(", isUsingCarrierAggregation=").append(isUsingCarrierAggregation())
1103                     .append(", mLteEarfcnRsrpBoost=").append(mLteEarfcnRsrpBoost)
1104                     .append(", mNetworkRegistrationInfos=").append(mNetworkRegistrationInfos)
1105                     .append(", mNrFrequencyRange=").append(mNrFrequencyRange)
1106                     .append(", mOperatorAlphaLongRaw=").append(mOperatorAlphaLongRaw)
1107                     .append(", mOperatorAlphaShortRaw=").append(mOperatorAlphaShortRaw)
1108                     .append(", mIsDataRoamingFromRegistration=")
1109                     .append(mIsDataRoamingFromRegistration)
1110                     .append(", mIsIwlanPreferred=").append(mIsIwlanPreferred)
1111                     .append("}").toString();
1112         }
1113     }
1114 
init()1115     private void init() {
1116         if (DBG) Rlog.d(LOG_TAG, "init");
1117         mVoiceRegState = STATE_OUT_OF_SERVICE;
1118         mDataRegState = STATE_OUT_OF_SERVICE;
1119         mChannelNumber = -1;
1120         mCellBandwidths = new int[0];
1121         mOperatorAlphaLong = null;
1122         mOperatorAlphaShort = null;
1123         mOperatorNumeric = null;
1124         mIsManualNetworkSelection = false;
1125         mCssIndicator = false;
1126         mNetworkId = -1;
1127         mSystemId = -1;
1128         mCdmaRoamingIndicator = -1;
1129         mCdmaDefaultRoamingIndicator = -1;
1130         mCdmaEriIconIndex = -1;
1131         mCdmaEriIconMode = -1;
1132         mIsEmergencyOnly = false;
1133         mLteEarfcnRsrpBoost = 0;
1134         mNrFrequencyRange = FREQUENCY_RANGE_UNKNOWN;
1135         synchronized (mNetworkRegistrationInfos) {
1136             mNetworkRegistrationInfos.clear();
1137             addNetworkRegistrationInfo(new NetworkRegistrationInfo.Builder()
1138                     .setDomain(NetworkRegistrationInfo.DOMAIN_CS)
1139                     .setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN)
1140                     .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_UNKNOWN)
1141                     .build());
1142             addNetworkRegistrationInfo(new NetworkRegistrationInfo.Builder()
1143                     .setDomain(NetworkRegistrationInfo.DOMAIN_PS)
1144                     .setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN)
1145                     .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_UNKNOWN)
1146                     .build());
1147         }
1148         mOperatorAlphaLongRaw = null;
1149         mOperatorAlphaShortRaw = null;
1150         mIsDataRoamingFromRegistration = false;
1151         mIsIwlanPreferred = false;
1152     }
1153 
setStateOutOfService()1154     public void setStateOutOfService() {
1155         init();
1156     }
1157 
setStateOff()1158     public void setStateOff() {
1159         init();
1160         mVoiceRegState = STATE_POWER_OFF;
1161         mDataRegState = STATE_POWER_OFF;
1162     }
1163 
setState(int state)1164     public void setState(int state) {
1165         setVoiceRegState(state);
1166         if (DBG) Rlog.e(LOG_TAG, "[ServiceState] setState deprecated use setVoiceRegState()");
1167     }
1168 
1169     /** @hide */
1170     @UnsupportedAppUsage
setVoiceRegState(int state)1171     public void setVoiceRegState(int state) {
1172         mVoiceRegState = state;
1173         if (DBG) Rlog.d(LOG_TAG, "[ServiceState] setVoiceRegState=" + mVoiceRegState);
1174     }
1175 
1176     /** @hide */
1177     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
setDataRegState(int state)1178     public void setDataRegState(int state) {
1179         mDataRegState = state;
1180         if (VDBG) Rlog.d(LOG_TAG, "[ServiceState] setDataRegState=" + mDataRegState);
1181     }
1182 
1183     /** @hide */
1184     @TestApi
setCellBandwidths(int[] bandwidths)1185     public void setCellBandwidths(int[] bandwidths) {
1186         mCellBandwidths = bandwidths;
1187     }
1188 
1189     /** @hide */
1190     @TestApi
setChannelNumber(int channelNumber)1191     public void setChannelNumber(int channelNumber) {
1192         mChannelNumber = channelNumber;
1193     }
1194 
setRoaming(boolean roaming)1195     public void setRoaming(boolean roaming) {
1196         setVoiceRoaming(roaming);
1197         setDataRoaming(roaming);
1198     }
1199 
1200     /** @hide */
1201     @UnsupportedAppUsage
setVoiceRoaming(boolean roaming)1202     public void setVoiceRoaming(boolean roaming) {
1203         setVoiceRoamingType(roaming ? ROAMING_TYPE_UNKNOWN : ROAMING_TYPE_NOT_ROAMING);
1204     }
1205 
1206     /** @hide */
1207     @TestApi
setVoiceRoamingType(@oamingType int type)1208     public void setVoiceRoamingType(@RoamingType int type) {
1209         NetworkRegistrationInfo regInfo = getNetworkRegistrationInfo(
1210                 NetworkRegistrationInfo.DOMAIN_CS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
1211         if (regInfo == null) {
1212             regInfo = new NetworkRegistrationInfo.Builder()
1213                     .setDomain(NetworkRegistrationInfo.DOMAIN_CS)
1214                     .setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN)
1215                     .build();
1216         }
1217         regInfo.setRoamingType(type);
1218         addNetworkRegistrationInfo(regInfo);
1219     }
1220 
1221     /** @hide */
1222     @UnsupportedAppUsage
setDataRoaming(boolean dataRoaming)1223     public void setDataRoaming(boolean dataRoaming) {
1224         setDataRoamingType(dataRoaming ? ROAMING_TYPE_UNKNOWN : ROAMING_TYPE_NOT_ROAMING);
1225     }
1226 
1227     /** @hide */
1228     @TestApi
setDataRoamingType(@oamingType int type)1229     public void setDataRoamingType(@RoamingType int type) {
1230         NetworkRegistrationInfo regInfo = getNetworkRegistrationInfo(
1231                 NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
1232         if (regInfo == null) {
1233             regInfo = new NetworkRegistrationInfo.Builder()
1234                     .setDomain(NetworkRegistrationInfo.DOMAIN_PS)
1235                     .setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN)
1236                     .build();
1237         }
1238         regInfo.setRoamingType(type);
1239         addNetworkRegistrationInfo(regInfo);
1240     }
1241 
1242     /**
1243      * @hide
1244      */
1245     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
setEmergencyOnly(boolean emergencyOnly)1246     public void setEmergencyOnly(boolean emergencyOnly) {
1247         mIsEmergencyOnly = emergencyOnly;
1248     }
1249 
1250     /**
1251      * @hide
1252      */
1253     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
setCdmaRoamingIndicator(int roaming)1254     public void setCdmaRoamingIndicator(int roaming) {
1255         this.mCdmaRoamingIndicator = roaming;
1256     }
1257 
1258     /**
1259      * @hide
1260      */
1261     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
setCdmaDefaultRoamingIndicator(int roaming)1262     public void setCdmaDefaultRoamingIndicator (int roaming) {
1263         this.mCdmaDefaultRoamingIndicator = roaming;
1264     }
1265 
1266     /**
1267      * @hide
1268      */
1269     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
setCdmaEriIconIndex(int index)1270     public void setCdmaEriIconIndex(int index) {
1271         this.mCdmaEriIconIndex = index;
1272     }
1273 
1274     /**
1275      * @hide
1276      */
1277     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
setCdmaEriIconMode(int mode)1278     public void setCdmaEriIconMode(int mode) {
1279         this.mCdmaEriIconMode = mode;
1280     }
1281 
setOperatorName(String longName, String shortName, String numeric)1282     public void setOperatorName(String longName, String shortName, String numeric) {
1283         mOperatorAlphaLong = longName;
1284         mOperatorAlphaShort = shortName;
1285         mOperatorNumeric = numeric;
1286     }
1287 
1288     /**
1289      * In CDMA, mOperatorAlphaLong can be set from the ERI text.
1290      * This is done from the GsmCdmaPhone and not from the ServiceStateTracker.
1291      *
1292      * @hide
1293      */
1294     @UnsupportedAppUsage
setOperatorAlphaLong(@ullable String longName)1295     public void setOperatorAlphaLong(@Nullable String longName) {
1296         mOperatorAlphaLong = longName;
1297     }
1298 
setIsManualSelection(boolean isManual)1299     public void setIsManualSelection(boolean isManual) {
1300         mIsManualNetworkSelection = isManual;
1301     }
1302 
1303     /**
1304      * Test whether two objects hold the same data values or both are null.
1305      *
1306      * @param a first obj
1307      * @param b second obj
1308      * @return true if two objects equal or both are null
1309      */
1310     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
equalsHandlesNulls(Object a, Object b)1311     private static boolean equalsHandlesNulls (Object a, Object b) {
1312         return (a == null) ? (b == null) : a.equals (b);
1313     }
1314 
1315     /**
1316      * Set ServiceState based on intent notifier map.
1317      *
1318      * @param m intent notifier map
1319      * @hide
1320      */
1321     @UnsupportedAppUsage
setFromNotifierBundle(Bundle m)1322     private void setFromNotifierBundle(Bundle m) {
1323         ServiceState ssFromBundle = m.getParcelable(EXTRA_SERVICE_STATE);
1324         if (ssFromBundle != null) {
1325             copyFrom(ssFromBundle);
1326         }
1327     }
1328 
1329     /**
1330      * Set intent notifier Bundle based on service state.
1331      *
1332      * Put ServiceState object and its fields into bundle which is used by TelephonyRegistry
1333      * to broadcast {@link Intent#ACTION_SERVICE_STATE}.
1334      *
1335      * @param m intent notifier Bundle
1336      * @hide
1337      *
1338      */
1339     @UnsupportedAppUsage
fillInNotifierBundle(@onNull Bundle m)1340     public void fillInNotifierBundle(@NonNull Bundle m) {
1341         m.putParcelable(EXTRA_SERVICE_STATE, this);
1342         // serviceState already consists of below entries.
1343         // for backward compatibility, we continue fill in below entries.
1344         m.putInt("voiceRegState", mVoiceRegState);
1345         m.putInt("dataRegState", mDataRegState);
1346         m.putInt("dataRoamingType", getDataRoamingType());
1347         m.putInt("voiceRoamingType", getVoiceRoamingType());
1348         m.putString("operator-alpha-long", mOperatorAlphaLong);
1349         m.putString("operator-alpha-short", mOperatorAlphaShort);
1350         m.putString("operator-numeric", mOperatorNumeric);
1351         m.putString("data-operator-alpha-long", mOperatorAlphaLong);
1352         m.putString("data-operator-alpha-short", mOperatorAlphaShort);
1353         m.putString("data-operator-numeric", mOperatorNumeric);
1354         m.putBoolean("manual", mIsManualNetworkSelection);
1355         m.putInt("radioTechnology", getRilVoiceRadioTechnology());
1356         m.putInt("dataRadioTechnology", getRadioTechnology());
1357         m.putBoolean("cssIndicator", mCssIndicator);
1358         m.putInt("networkId", mNetworkId);
1359         m.putInt("systemId", mSystemId);
1360         m.putInt("cdmaRoamingIndicator", mCdmaRoamingIndicator);
1361         m.putInt("cdmaDefaultRoamingIndicator", mCdmaDefaultRoamingIndicator);
1362         m.putBoolean("emergencyOnly", mIsEmergencyOnly);
1363         m.putBoolean("isDataRoamingFromRegistration", getDataRoamingFromRegistration());
1364         m.putBoolean("isUsingCarrierAggregation", isUsingCarrierAggregation());
1365         m.putInt("LteEarfcnRsrpBoost", mLteEarfcnRsrpBoost);
1366         m.putInt("ChannelNumber", mChannelNumber);
1367         m.putIntArray("CellBandwidths", mCellBandwidths);
1368         m.putInt("mNrFrequencyRange", mNrFrequencyRange);
1369         m.putString("operator-alpha-long-raw", mOperatorAlphaLongRaw);
1370         m.putString("operator-alpha-short-raw", mOperatorAlphaShortRaw);
1371     }
1372 
1373     /** @hide */
1374     @TestApi
setRilVoiceRadioTechnology(@ilRadioTechnology int rt)1375     public void setRilVoiceRadioTechnology(@RilRadioTechnology int rt) {
1376         Rlog.e(LOG_TAG, "ServiceState.setRilVoiceRadioTechnology() called. It's encouraged to "
1377                 + "use addNetworkRegistrationInfo() instead *******");
1378         // Sync to network registration state
1379         NetworkRegistrationInfo regInfo = getNetworkRegistrationInfo(
1380                 NetworkRegistrationInfo.DOMAIN_CS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
1381         if (regInfo == null) {
1382             regInfo = new NetworkRegistrationInfo.Builder()
1383                     .setDomain(NetworkRegistrationInfo.DOMAIN_CS)
1384                     .setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN)
1385                     .build();
1386         }
1387         regInfo.setAccessNetworkTechnology(rilRadioTechnologyToNetworkType(rt));
1388         addNetworkRegistrationInfo(regInfo);
1389     }
1390 
1391 
1392     /** @hide */
1393     @TestApi
setRilDataRadioTechnology(@ilRadioTechnology int rt)1394     public void setRilDataRadioTechnology(@RilRadioTechnology int rt) {
1395         Rlog.e(LOG_TAG, "ServiceState.setRilDataRadioTechnology() called. It's encouraged to "
1396                 + "use addNetworkRegistrationInfo() instead *******");
1397         // Sync to network registration state. Always write down the WWAN transport. For AP-assisted
1398         // mode device, use addNetworkRegistrationInfo() to set the correct transport if RAT
1399         // is IWLAN.
1400         NetworkRegistrationInfo regInfo = getNetworkRegistrationInfo(
1401                 NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
1402 
1403         if (regInfo == null) {
1404             regInfo = new NetworkRegistrationInfo.Builder()
1405                     .setDomain(NetworkRegistrationInfo.DOMAIN_PS)
1406                     .setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN)
1407                     .build();
1408         }
1409         regInfo.setAccessNetworkTechnology(rilRadioTechnologyToNetworkType(rt));
1410         addNetworkRegistrationInfo(regInfo);
1411     }
1412 
1413     /** @hide */
isUsingCarrierAggregation()1414     public boolean isUsingCarrierAggregation() {
1415         boolean isUsingCa = false;
1416         NetworkRegistrationInfo nri = getNetworkRegistrationInfo(
1417                 NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
1418         if (nri != null) {
1419             DataSpecificRegistrationInfo dsri = nri.getDataSpecificInfo();
1420             if (dsri != null) {
1421                 isUsingCa = dsri.isUsingCarrierAggregation();
1422             }
1423         }
1424         return isUsingCa || getCellBandwidths().length > 1;
1425     }
1426 
1427     /** @hide */
setIsUsingCarrierAggregation(boolean ca)1428     public void setIsUsingCarrierAggregation(boolean ca) {
1429         NetworkRegistrationInfo nri = getNetworkRegistrationInfo(
1430                 NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
1431         if (nri != null) {
1432             DataSpecificRegistrationInfo dsri = nri.getDataSpecificInfo();
1433             if (dsri != null) {
1434                 dsri.setIsUsingCarrierAggregation(ca);
1435                 addNetworkRegistrationInfo(nri);
1436             }
1437         }
1438     }
1439 
1440     /**
1441      * Get the 5G NR frequency range the device is currently registered.
1442      *
1443      * @return the frequency range of 5G NR.
1444      * @hide
1445      */
getNrFrequencyRange()1446     public @FrequencyRange int getNrFrequencyRange() {
1447         return mNrFrequencyRange;
1448     }
1449 
1450     /**
1451      * Get the NR 5G state of the mobile data network.
1452      * @return the NR 5G state.
1453      * @hide
1454      */
getNrState()1455     public @NRState int getNrState() {
1456         final NetworkRegistrationInfo regInfo = getNetworkRegistrationInfo(
1457                 NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
1458         if (regInfo == null) return NetworkRegistrationInfo.NR_STATE_NONE;
1459         return regInfo.getNrState();
1460     }
1461 
1462     /**
1463      * @param nrFrequencyRange the frequency range of 5G NR.
1464      * @hide
1465      */
setNrFrequencyRange(@requencyRange int nrFrequencyRange)1466     public void setNrFrequencyRange(@FrequencyRange int nrFrequencyRange) {
1467         mNrFrequencyRange = nrFrequencyRange;
1468     }
1469 
1470     /** @hide */
getLteEarfcnRsrpBoost()1471     public int getLteEarfcnRsrpBoost() {
1472         return mLteEarfcnRsrpBoost;
1473     }
1474 
1475     /** @hide */
setLteEarfcnRsrpBoost(int LteEarfcnRsrpBoost)1476     public void setLteEarfcnRsrpBoost(int LteEarfcnRsrpBoost) {
1477         mLteEarfcnRsrpBoost = LteEarfcnRsrpBoost;
1478     }
1479 
1480     /** @hide */
1481     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
setCssIndicator(int css)1482     public void setCssIndicator(int css) {
1483         this.mCssIndicator = (css != 0);
1484     }
1485 
1486     /** @hide */
1487     @TestApi
setCdmaSystemAndNetworkId(int systemId, int networkId)1488     public void setCdmaSystemAndNetworkId(int systemId, int networkId) {
1489         this.mSystemId = systemId;
1490         this.mNetworkId = networkId;
1491     }
1492 
1493     /** @hide */
1494     @UnsupportedAppUsage
getRilVoiceRadioTechnology()1495     public int getRilVoiceRadioTechnology() {
1496         NetworkRegistrationInfo wwanRegInfo = getNetworkRegistrationInfo(
1497                 NetworkRegistrationInfo.DOMAIN_CS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
1498         if (wwanRegInfo != null) {
1499             return networkTypeToRilRadioTechnology(wwanRegInfo.getAccessNetworkTechnology());
1500         }
1501         return RIL_RADIO_TECHNOLOGY_UNKNOWN;
1502     }
1503     /** @hide */
1504     @UnsupportedAppUsage
getRilDataRadioTechnology()1505     public int getRilDataRadioTechnology() {
1506         return networkTypeToRilRadioTechnology(getDataNetworkType());
1507     }
1508 
1509     /**
1510      * @hide
1511      * @Deprecated to be removed Q3 2013 use {@link #getRilDataRadioTechnology} or
1512      * {@link #getRilVoiceRadioTechnology}
1513      */
1514     @UnsupportedAppUsage
getRadioTechnology()1515     public int getRadioTechnology() {
1516         Rlog.e(LOG_TAG, "ServiceState.getRadioTechnology() DEPRECATED will be removed *******");
1517         return getRilDataRadioTechnology();
1518     }
1519 
1520     /**
1521      * Transform RIL radio technology {@link RilRadioTechnology} value to Network
1522      * type {@link NetworkType}.
1523      *
1524      * @param rat The RIL radio technology {@link RilRadioTechnology}.
1525      * @return The network type {@link NetworkType}.
1526      *
1527      * @hide
1528      */
rilRadioTechnologyToNetworkType(@ilRadioTechnology int rat)1529     public static int rilRadioTechnologyToNetworkType(@RilRadioTechnology int rat) {
1530         switch(rat) {
1531             case RIL_RADIO_TECHNOLOGY_GPRS:
1532                 return TelephonyManager.NETWORK_TYPE_GPRS;
1533             case RIL_RADIO_TECHNOLOGY_EDGE:
1534                 return TelephonyManager.NETWORK_TYPE_EDGE;
1535             case RIL_RADIO_TECHNOLOGY_UMTS:
1536                 return TelephonyManager.NETWORK_TYPE_UMTS;
1537             case RIL_RADIO_TECHNOLOGY_HSDPA:
1538                 return TelephonyManager.NETWORK_TYPE_HSDPA;
1539             case RIL_RADIO_TECHNOLOGY_HSUPA:
1540                 return TelephonyManager.NETWORK_TYPE_HSUPA;
1541             case RIL_RADIO_TECHNOLOGY_HSPA:
1542                 return TelephonyManager.NETWORK_TYPE_HSPA;
1543             case RIL_RADIO_TECHNOLOGY_IS95A:
1544             case RIL_RADIO_TECHNOLOGY_IS95B:
1545                 return TelephonyManager.NETWORK_TYPE_CDMA;
1546             case RIL_RADIO_TECHNOLOGY_1xRTT:
1547                 return TelephonyManager.NETWORK_TYPE_1xRTT;
1548             case RIL_RADIO_TECHNOLOGY_EVDO_0:
1549                 return TelephonyManager.NETWORK_TYPE_EVDO_0;
1550             case RIL_RADIO_TECHNOLOGY_EVDO_A:
1551                 return TelephonyManager.NETWORK_TYPE_EVDO_A;
1552             case RIL_RADIO_TECHNOLOGY_EVDO_B:
1553                 return TelephonyManager.NETWORK_TYPE_EVDO_B;
1554             case RIL_RADIO_TECHNOLOGY_EHRPD:
1555                 return TelephonyManager.NETWORK_TYPE_EHRPD;
1556             case RIL_RADIO_TECHNOLOGY_LTE:
1557                 return TelephonyManager.NETWORK_TYPE_LTE;
1558             case RIL_RADIO_TECHNOLOGY_HSPAP:
1559                 return TelephonyManager.NETWORK_TYPE_HSPAP;
1560             case RIL_RADIO_TECHNOLOGY_GSM:
1561                 return TelephonyManager.NETWORK_TYPE_GSM;
1562             case RIL_RADIO_TECHNOLOGY_TD_SCDMA:
1563                 return TelephonyManager.NETWORK_TYPE_TD_SCDMA;
1564             case RIL_RADIO_TECHNOLOGY_IWLAN:
1565                 return TelephonyManager.NETWORK_TYPE_IWLAN;
1566             case RIL_RADIO_TECHNOLOGY_LTE_CA:
1567                 return TelephonyManager.NETWORK_TYPE_LTE_CA;
1568             case RIL_RADIO_TECHNOLOGY_NR:
1569                 return TelephonyManager.NETWORK_TYPE_NR;
1570             default:
1571                 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1572         }
1573     }
1574 
1575     /** @hide */
rilRadioTechnologyToAccessNetworkType(@ilRadioTechnology int rt)1576     public static int rilRadioTechnologyToAccessNetworkType(@RilRadioTechnology int rt) {
1577         switch(rt) {
1578             case RIL_RADIO_TECHNOLOGY_GPRS:
1579             case RIL_RADIO_TECHNOLOGY_EDGE:
1580             case RIL_RADIO_TECHNOLOGY_GSM:
1581                 return AccessNetworkType.GERAN;
1582             case RIL_RADIO_TECHNOLOGY_UMTS:
1583             case RIL_RADIO_TECHNOLOGY_HSDPA:
1584             case RIL_RADIO_TECHNOLOGY_HSPAP:
1585             case RIL_RADIO_TECHNOLOGY_HSUPA:
1586             case RIL_RADIO_TECHNOLOGY_HSPA:
1587             case RIL_RADIO_TECHNOLOGY_TD_SCDMA:
1588                 return AccessNetworkType.UTRAN;
1589             case RIL_RADIO_TECHNOLOGY_IS95A:
1590             case RIL_RADIO_TECHNOLOGY_IS95B:
1591             case RIL_RADIO_TECHNOLOGY_1xRTT:
1592             case RIL_RADIO_TECHNOLOGY_EVDO_0:
1593             case RIL_RADIO_TECHNOLOGY_EVDO_A:
1594             case RIL_RADIO_TECHNOLOGY_EVDO_B:
1595             case RIL_RADIO_TECHNOLOGY_EHRPD:
1596                 return AccessNetworkType.CDMA2000;
1597             case RIL_RADIO_TECHNOLOGY_LTE:
1598             case RIL_RADIO_TECHNOLOGY_LTE_CA:
1599                 return AccessNetworkType.EUTRAN;
1600             case RIL_RADIO_TECHNOLOGY_NR:
1601                 return AccessNetworkType.NGRAN;
1602             case RIL_RADIO_TECHNOLOGY_IWLAN:
1603                 return AccessNetworkType.IWLAN;
1604             case RIL_RADIO_TECHNOLOGY_UNKNOWN:
1605             default:
1606                 return AccessNetworkType.UNKNOWN;
1607         }
1608     }
1609 
1610     /**
1611      * Transform network type {@link NetworkType} value to RIL radio technology
1612      * {@link RilRadioTechnology}.
1613      *
1614      * @param networkType The network type {@link NetworkType}.
1615      * @return The RIL radio technology {@link RilRadioTechnology}.
1616      *
1617      * @hide
1618      */
networkTypeToRilRadioTechnology(int networkType)1619     public static int networkTypeToRilRadioTechnology(int networkType) {
1620         switch(networkType) {
1621             case TelephonyManager.NETWORK_TYPE_GPRS:
1622                 return RIL_RADIO_TECHNOLOGY_GPRS;
1623             case TelephonyManager.NETWORK_TYPE_EDGE:
1624                 return RIL_RADIO_TECHNOLOGY_EDGE;
1625             case TelephonyManager.NETWORK_TYPE_UMTS:
1626                 return RIL_RADIO_TECHNOLOGY_UMTS;
1627             case TelephonyManager.NETWORK_TYPE_HSDPA:
1628                 return RIL_RADIO_TECHNOLOGY_HSDPA;
1629             case TelephonyManager.NETWORK_TYPE_HSUPA:
1630                 return RIL_RADIO_TECHNOLOGY_HSUPA;
1631             case TelephonyManager.NETWORK_TYPE_HSPA:
1632                 return RIL_RADIO_TECHNOLOGY_HSPA;
1633             case TelephonyManager.NETWORK_TYPE_CDMA:
1634                 return RIL_RADIO_TECHNOLOGY_IS95A;
1635             case TelephonyManager.NETWORK_TYPE_1xRTT:
1636                 return RIL_RADIO_TECHNOLOGY_1xRTT;
1637             case TelephonyManager.NETWORK_TYPE_EVDO_0:
1638                 return RIL_RADIO_TECHNOLOGY_EVDO_0;
1639             case TelephonyManager.NETWORK_TYPE_EVDO_A:
1640                 return RIL_RADIO_TECHNOLOGY_EVDO_A;
1641             case TelephonyManager.NETWORK_TYPE_EVDO_B:
1642                 return RIL_RADIO_TECHNOLOGY_EVDO_B;
1643             case TelephonyManager.NETWORK_TYPE_EHRPD:
1644                 return RIL_RADIO_TECHNOLOGY_EHRPD;
1645             case TelephonyManager.NETWORK_TYPE_LTE:
1646                 return RIL_RADIO_TECHNOLOGY_LTE;
1647             case TelephonyManager.NETWORK_TYPE_HSPAP:
1648                 return RIL_RADIO_TECHNOLOGY_HSPAP;
1649             case TelephonyManager.NETWORK_TYPE_GSM:
1650                 return RIL_RADIO_TECHNOLOGY_GSM;
1651             case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
1652                 return RIL_RADIO_TECHNOLOGY_TD_SCDMA;
1653             case TelephonyManager.NETWORK_TYPE_IWLAN:
1654                 return RIL_RADIO_TECHNOLOGY_IWLAN;
1655             case TelephonyManager.NETWORK_TYPE_LTE_CA:
1656                 return RIL_RADIO_TECHNOLOGY_LTE_CA;
1657             case TelephonyManager.NETWORK_TYPE_NR:
1658                 return RIL_RADIO_TECHNOLOGY_NR;
1659             default:
1660                 return RIL_RADIO_TECHNOLOGY_UNKNOWN;
1661         }
1662     }
1663 
1664     /**
1665      * Get current data network type.
1666      *
1667      * Note that for IWLAN AP-assisted mode device, which is reporting both camped access networks
1668      * (cellular RAT and IWLAN)at the same time, this API is simulating the old legacy mode device
1669      * behavior,
1670      *
1671      * @return Current data network type
1672      * @hide
1673      */
1674     @TestApi
getDataNetworkType()1675     public @NetworkType int getDataNetworkType() {
1676         final NetworkRegistrationInfo iwlanRegInfo = getNetworkRegistrationInfo(
1677                 NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WLAN);
1678         final NetworkRegistrationInfo wwanRegInfo = getNetworkRegistrationInfo(
1679                 NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
1680 
1681         // For legacy mode device, or AP-assisted mode device but IWLAN is out of service, use
1682         // the RAT from cellular.
1683         if (iwlanRegInfo == null || !iwlanRegInfo.isInService()) {
1684             return (wwanRegInfo != null) ? wwanRegInfo.getAccessNetworkTechnology()
1685                     : TelephonyManager.NETWORK_TYPE_UNKNOWN;
1686         }
1687 
1688         // At this point, it must be an AP-assisted mode device and IWLAN is in service. We should
1689         // use the RAT from IWLAN service is cellular is out of service, or when both are in service
1690         // and any APN type of data is preferred on IWLAN.
1691         if (!wwanRegInfo.isInService() || mIsIwlanPreferred) {
1692             return iwlanRegInfo.getAccessNetworkTechnology();
1693         }
1694 
1695         // If both cellular and IWLAN are in service, but no APN is preferred on IWLAN, still use
1696         // the RAT from cellular.
1697         return wwanRegInfo.getAccessNetworkTechnology();
1698     }
1699 
1700     /** @hide */
1701     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
getVoiceNetworkType()1702     public @NetworkType int getVoiceNetworkType() {
1703         final NetworkRegistrationInfo regState = getNetworkRegistrationInfo(
1704                 NetworkRegistrationInfo.DOMAIN_CS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
1705         if (regState != null) {
1706             return regState.getAccessNetworkTechnology();
1707         }
1708         return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1709     }
1710 
1711     /** @hide */
1712     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
getCssIndicator()1713     public int getCssIndicator() {
1714         return this.mCssIndicator ? 1 : 0;
1715     }
1716 
1717     /**
1718      * Get the CDMA NID (Network Identification Number), a number uniquely identifying a network
1719      * within a wireless system. (Defined in 3GPP2 C.S0023 3.4.8)
1720      * @return The CDMA NID or {@link #UNKNOWN_ID} if not available.
1721      */
getCdmaNetworkId()1722     public int getCdmaNetworkId() {
1723         return this.mNetworkId;
1724     }
1725 
1726     /**
1727      * Get the CDMA SID (System Identification Number), a number uniquely identifying a wireless
1728      * system. (Defined in 3GPP2 C.S0023 3.4.8)
1729      * @return The CDMA SID or {@link #UNKNOWN_ID} if not available.
1730      */
getCdmaSystemId()1731     public int getCdmaSystemId() {
1732         return this.mSystemId;
1733     }
1734 
1735     /** @hide */
1736     @UnsupportedAppUsage
isGsm(int radioTechnology)1737     public static boolean isGsm(int radioTechnology) {
1738         return radioTechnology == RIL_RADIO_TECHNOLOGY_GPRS
1739                 || radioTechnology == RIL_RADIO_TECHNOLOGY_EDGE
1740                 || radioTechnology == RIL_RADIO_TECHNOLOGY_UMTS
1741                 || radioTechnology == RIL_RADIO_TECHNOLOGY_HSDPA
1742                 || radioTechnology == RIL_RADIO_TECHNOLOGY_HSUPA
1743                 || radioTechnology == RIL_RADIO_TECHNOLOGY_HSPA
1744                 || radioTechnology == RIL_RADIO_TECHNOLOGY_LTE
1745                 || radioTechnology == RIL_RADIO_TECHNOLOGY_HSPAP
1746                 || radioTechnology == RIL_RADIO_TECHNOLOGY_GSM
1747                 || radioTechnology == RIL_RADIO_TECHNOLOGY_TD_SCDMA
1748                 || radioTechnology == RIL_RADIO_TECHNOLOGY_IWLAN
1749                 || radioTechnology == RIL_RADIO_TECHNOLOGY_LTE_CA
1750                 || radioTechnology == RIL_RADIO_TECHNOLOGY_NR;
1751 
1752     }
1753 
1754     /** @hide */
1755     @UnsupportedAppUsage
isCdma(int radioTechnology)1756     public static boolean isCdma(int radioTechnology) {
1757         return radioTechnology == RIL_RADIO_TECHNOLOGY_IS95A
1758                 || radioTechnology == RIL_RADIO_TECHNOLOGY_IS95B
1759                 || radioTechnology == RIL_RADIO_TECHNOLOGY_1xRTT
1760                 || radioTechnology == RIL_RADIO_TECHNOLOGY_EVDO_0
1761                 || radioTechnology == RIL_RADIO_TECHNOLOGY_EVDO_A
1762                 || radioTechnology == RIL_RADIO_TECHNOLOGY_EVDO_B
1763                 || radioTechnology == RIL_RADIO_TECHNOLOGY_EHRPD;
1764     }
1765 
1766     /** @hide */
isPsOnlyTech(int radioTechnology)1767     public static boolean isPsOnlyTech(int radioTechnology) {
1768         return radioTechnology == RIL_RADIO_TECHNOLOGY_LTE
1769                 || radioTechnology == RIL_RADIO_TECHNOLOGY_LTE_CA
1770                 || radioTechnology == RIL_RADIO_TECHNOLOGY_NR;
1771     }
1772 
1773     /** @hide */
1774     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
bearerBitmapHasCdma(int networkTypeBitmask)1775     public static boolean bearerBitmapHasCdma(int networkTypeBitmask) {
1776         return (RIL_RADIO_CDMA_TECHNOLOGY_BITMASK
1777                 & convertNetworkTypeBitmaskToBearerBitmask(networkTypeBitmask)) != 0;
1778     }
1779 
1780     /** @hide */
1781     @UnsupportedAppUsage
bitmaskHasTech(int bearerBitmask, int radioTech)1782     public static boolean bitmaskHasTech(int bearerBitmask, int radioTech) {
1783         if (bearerBitmask == 0) {
1784             return true;
1785         } else if (radioTech >= 1) {
1786             return ((bearerBitmask & (1 << (radioTech - 1))) != 0);
1787         }
1788         return false;
1789     }
1790 
1791     /** @hide */
getBitmaskForTech(int radioTech)1792     public static int getBitmaskForTech(int radioTech) {
1793         if (radioTech >= 1) {
1794             return (1 << (radioTech - 1));
1795         }
1796         return 0;
1797     }
1798 
1799     /** @hide */
getBitmaskFromString(String bearerList)1800     public static int getBitmaskFromString(String bearerList) {
1801         String[] bearers = bearerList.split("\\|");
1802         int bearerBitmask = 0;
1803         for (String bearer : bearers) {
1804             int bearerInt = 0;
1805             try {
1806                 bearerInt = Integer.parseInt(bearer.trim());
1807             } catch (NumberFormatException nfe) {
1808                 return 0;
1809             }
1810 
1811             if (bearerInt == 0) {
1812                 return 0;
1813             }
1814 
1815             bearerBitmask |= getBitmaskForTech(bearerInt);
1816         }
1817         return bearerBitmask;
1818     }
1819 
1820     /**
1821      * Convert network type bitmask to bearer bitmask.
1822      *
1823      * @param networkTypeBitmask The network type bitmask value
1824      * @return The bearer bitmask value.
1825      *
1826      * @hide
1827      */
convertNetworkTypeBitmaskToBearerBitmask(int networkTypeBitmask)1828     public static int convertNetworkTypeBitmaskToBearerBitmask(int networkTypeBitmask) {
1829         if (networkTypeBitmask == 0) {
1830             return 0;
1831         }
1832         int bearerBitmask = 0;
1833         for (int bearerInt = 0; bearerInt < NEXT_RIL_RADIO_TECHNOLOGY; bearerInt++) {
1834             if (bitmaskHasTech(networkTypeBitmask, rilRadioTechnologyToNetworkType(bearerInt))) {
1835                 bearerBitmask |= getBitmaskForTech(bearerInt);
1836             }
1837         }
1838         return bearerBitmask;
1839     }
1840 
1841     /**
1842      * Convert bearer bitmask to network type bitmask.
1843      *
1844      * @param bearerBitmask The bearer bitmask value.
1845      * @return The network type bitmask value.
1846      *
1847      * @hide
1848      */
convertBearerBitmaskToNetworkTypeBitmask(int bearerBitmask)1849     public static int convertBearerBitmaskToNetworkTypeBitmask(int bearerBitmask) {
1850         if (bearerBitmask == 0) {
1851             return 0;
1852         }
1853         int networkTypeBitmask = 0;
1854         for (int bearerInt = 0; bearerInt < NEXT_RIL_RADIO_TECHNOLOGY; bearerInt++) {
1855             if (bitmaskHasTech(bearerBitmask, bearerInt)) {
1856                 networkTypeBitmask |= getBitmaskForTech(rilRadioTechnologyToNetworkType(bearerInt));
1857             }
1858         }
1859         return networkTypeBitmask;
1860     }
1861 
1862     /**
1863      * Returns a merged ServiceState consisting of the base SS with voice settings from the
1864      * voice SS. The voice SS is only used if it is IN_SERVICE (otherwise the base SS is returned).
1865      * @hide
1866      * */
1867     @UnsupportedAppUsage
mergeServiceStates(ServiceState baseSs, ServiceState voiceSs)1868     public static ServiceState mergeServiceStates(ServiceState baseSs, ServiceState voiceSs) {
1869         if (voiceSs.mVoiceRegState != STATE_IN_SERVICE) {
1870             return baseSs;
1871         }
1872 
1873         ServiceState newSs = new ServiceState(baseSs);
1874 
1875         // voice overrides
1876         newSs.mVoiceRegState = voiceSs.mVoiceRegState;
1877         newSs.mIsEmergencyOnly = false; // only get here if voice is IN_SERVICE
1878 
1879         return newSs;
1880     }
1881 
1882     /**
1883      * Get all of the available network registration info.
1884      *
1885      * @return List of {@link NetworkRegistrationInfo}
1886      */
1887     @NonNull
getNetworkRegistrationInfoList()1888     public List<NetworkRegistrationInfo> getNetworkRegistrationInfoList() {
1889         synchronized (mNetworkRegistrationInfos) {
1890             List<NetworkRegistrationInfo> newList = new ArrayList<>();
1891             for (NetworkRegistrationInfo nri : mNetworkRegistrationInfos) {
1892                 newList.add(new NetworkRegistrationInfo(nri));
1893             }
1894             return newList;
1895         }
1896     }
1897 
1898     /**
1899      * Get the network registration info list for the transport type.
1900      *
1901      * @param transportType The transport type
1902      * @return List of {@link NetworkRegistrationInfo}
1903      * @hide
1904      */
1905     @NonNull
1906     @SystemApi
getNetworkRegistrationInfoListForTransportType( @ransportType int transportType)1907     public List<NetworkRegistrationInfo> getNetworkRegistrationInfoListForTransportType(
1908             @TransportType int transportType) {
1909         List<NetworkRegistrationInfo> list = new ArrayList<>();
1910 
1911         synchronized (mNetworkRegistrationInfos) {
1912             for (NetworkRegistrationInfo networkRegistrationInfo : mNetworkRegistrationInfos) {
1913                 if (networkRegistrationInfo.getTransportType() == transportType) {
1914                     list.add(new NetworkRegistrationInfo(networkRegistrationInfo));
1915                 }
1916             }
1917         }
1918 
1919         return list;
1920     }
1921 
1922     /**
1923      * Get the network registration info list for the network domain.
1924      *
1925      * @param domain The network {@link NetworkRegistrationInfo.Domain domain}
1926      * @return List of {@link NetworkRegistrationInfo}
1927      * @hide
1928      */
1929     @NonNull
1930     @SystemApi
getNetworkRegistrationInfoListForDomain( @omain int domain)1931     public List<NetworkRegistrationInfo> getNetworkRegistrationInfoListForDomain(
1932             @Domain int domain) {
1933         List<NetworkRegistrationInfo> list = new ArrayList<>();
1934 
1935         synchronized (mNetworkRegistrationInfos) {
1936             for (NetworkRegistrationInfo networkRegistrationInfo : mNetworkRegistrationInfos) {
1937                 if ((networkRegistrationInfo.getDomain() & domain) != 0) {
1938                     list.add(new NetworkRegistrationInfo(networkRegistrationInfo));
1939                 }
1940             }
1941         }
1942 
1943         return list;
1944     }
1945 
1946     /**
1947      * Get the network registration state for the transport type and network domain.
1948      * If multiple domains are in the input bitmask, only the first one from
1949      * networkRegistrationInfo.getDomain() will be returned.
1950      *
1951      * @param domain The network {@link NetworkRegistrationInfo.Domain domain}
1952      * @param transportType The transport type
1953      * @return The matching {@link NetworkRegistrationInfo}
1954      * @hide
1955      */
1956     @Nullable
1957     @SystemApi
getNetworkRegistrationInfo(@omain int domain, @TransportType int transportType)1958     public NetworkRegistrationInfo getNetworkRegistrationInfo(@Domain int domain,
1959                                                               @TransportType int transportType) {
1960         synchronized (mNetworkRegistrationInfos) {
1961             for (NetworkRegistrationInfo networkRegistrationInfo : mNetworkRegistrationInfos) {
1962                 if (networkRegistrationInfo.getTransportType() == transportType
1963                         && (networkRegistrationInfo.getDomain() & domain) != 0) {
1964                     return new NetworkRegistrationInfo(networkRegistrationInfo);
1965                 }
1966             }
1967         }
1968 
1969         return null;
1970     }
1971 
1972     /**
1973      * @hide
1974      */
1975     @TestApi
addNetworkRegistrationInfo(NetworkRegistrationInfo nri)1976     public void addNetworkRegistrationInfo(NetworkRegistrationInfo nri) {
1977         if (nri == null) return;
1978 
1979         synchronized (mNetworkRegistrationInfos) {
1980             for (int i = 0; i < mNetworkRegistrationInfos.size(); i++) {
1981                 NetworkRegistrationInfo curRegState = mNetworkRegistrationInfos.get(i);
1982                 if (curRegState.getTransportType() == nri.getTransportType()
1983                         && curRegState.getDomain() == nri.getDomain()) {
1984                     mNetworkRegistrationInfos.remove(i);
1985                     break;
1986                 }
1987             }
1988 
1989             mNetworkRegistrationInfos.add(new NetworkRegistrationInfo(nri));
1990         }
1991     }
1992 
1993     /**
1994      * @hide
1995      */
getBetterNRFrequencyRange(int range1, int range2)1996     public static final int getBetterNRFrequencyRange(int range1, int range2) {
1997         return FREQUENCY_RANGE_ORDER.indexOf(range1) > FREQUENCY_RANGE_ORDER.indexOf(range2)
1998                 ? range1
1999                 : range2;
2000     }
2001 
2002     /**
2003      * Returns a copy of self with location-identifying information removed.
2004      * Always clears the NetworkRegistrationInfo's CellIdentity fields, but if removeCoarseLocation
2005      * is true, clears other info as well.
2006      *
2007      * @param removeCoarseLocation Whether to also remove coarse location information.
2008      *                             if false, it only clears fine location information such as
2009      *                             NetworkRegistrationInfo's CellIdentity fields; If true, it will
2010      *                             also remove other location information such as operator's MCC
2011      *                             and MNC.
2012      * @return the copied ServiceState with location info sanitized.
2013      * @hide
2014      */
2015     @NonNull
createLocationInfoSanitizedCopy(boolean removeCoarseLocation)2016     public ServiceState createLocationInfoSanitizedCopy(boolean removeCoarseLocation) {
2017         ServiceState state = new ServiceState(this);
2018         synchronized (state.mNetworkRegistrationInfos) {
2019             List<NetworkRegistrationInfo> networkRegistrationInfos =
2020                     state.mNetworkRegistrationInfos.stream()
2021                             .map(NetworkRegistrationInfo::sanitizeLocationInfo)
2022                             .collect(Collectors.toList());
2023             state.mNetworkRegistrationInfos.clear();
2024             state.mNetworkRegistrationInfos.addAll(networkRegistrationInfos);
2025         }
2026         if (!removeCoarseLocation) return state;
2027 
2028         state.mOperatorAlphaLong = null;
2029         state.mOperatorAlphaShort = null;
2030         state.mOperatorNumeric = null;
2031 
2032         return state;
2033     }
2034 
2035     /**
2036      * @hide
2037      */
setOperatorAlphaLongRaw(String operatorAlphaLong)2038     public void setOperatorAlphaLongRaw(String operatorAlphaLong) {
2039         mOperatorAlphaLongRaw = operatorAlphaLong;
2040     }
2041 
2042     /**
2043      * The current registered raw data network operator name in long alphanumeric format.
2044      *
2045      * The long format can be up to 16 characters long.
2046      *
2047      * @return long raw name of operator, null if unregistered or unknown
2048      * @hide
2049      */
2050     @Nullable
getOperatorAlphaLongRaw()2051     public String getOperatorAlphaLongRaw() {
2052         return mOperatorAlphaLongRaw;
2053     }
2054 
2055     /**
2056      * @hide
2057      */
setOperatorAlphaShortRaw(String operatorAlphaShort)2058     public void setOperatorAlphaShortRaw(String operatorAlphaShort) {
2059         mOperatorAlphaShortRaw = operatorAlphaShort;
2060     }
2061 
2062     /**
2063      * The current registered raw data network operator name in short alphanumeric format.
2064      *
2065      * The short format can be up to 8 characters long.
2066      *
2067      * @return short raw name of operator, null if unregistered or unknown
2068      * @hide
2069      */
2070     @Nullable
getOperatorAlphaShortRaw()2071     public String getOperatorAlphaShortRaw() {
2072         return mOperatorAlphaShortRaw;
2073     }
2074 
2075     /**
2076      * Set to {@code true} if any data network is preferred on IWLAN.
2077      *
2078      * @param isIwlanPreferred {@code true} if IWLAN is preferred.
2079      * @hide
2080      */
setIwlanPreferred(boolean isIwlanPreferred)2081     public void setIwlanPreferred(boolean isIwlanPreferred) {
2082         mIsIwlanPreferred = isIwlanPreferred;
2083     }
2084 
2085     /**
2086      * @return {@code true} if any data network is preferred on IWLAN.
2087      *
2088      * Note only when this value is true, {@link #getDataNetworkType()} will return
2089      * {@link TelephonyManager#NETWORK_TYPE_IWLAN} when AP-assisted mode device camps on both
2090      * cellular and IWLAN. This value does not affect legacy mode devices as the data network
2091      * type is directly reported by the modem.
2092      *
2093      * @hide
2094      */
isIwlanPreferred()2095     public boolean isIwlanPreferred() {
2096         return mIsIwlanPreferred;
2097     }
2098 
2099     /**
2100      * This indicates whether the device is searching for service.
2101      *
2102      * This API reports the modem searching status for
2103      * {@link AccessNetworkConstants#TRANSPORT_TYPE_WWAN} (cellular) service in either
2104      * {@link NetworkRegistrationInfo#DOMAIN_CS} or {@link NetworkRegistrationInfo#DOMAIN_PS}.
2105      * This API will not report searching status for
2106      * {@link AccessNetworkConstants#TRANSPORT_TYPE_WLAN}.
2107      *
2108      * @return {@code true} whenever the modem is searching for service.
2109      */
isSearching()2110     public boolean isSearching() {
2111         NetworkRegistrationInfo psRegState = getNetworkRegistrationInfo(
2112                 NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
2113 
2114         if (psRegState != null && psRegState.getRegistrationState()
2115                 == NetworkRegistrationInfo.REGISTRATION_STATE_NOT_REGISTERED_SEARCHING) {
2116             return true;
2117         }
2118 
2119         NetworkRegistrationInfo csRegState = getNetworkRegistrationInfo(
2120                 NetworkRegistrationInfo.DOMAIN_CS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
2121 
2122         if (csRegState != null && csRegState.getRegistrationState()
2123                 == NetworkRegistrationInfo.REGISTRATION_STATE_NOT_REGISTERED_SEARCHING) {
2124             return true;
2125         }
2126         return false;
2127     }
2128 }
2129