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     @SystemApi
401     @NonNull
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     @SystemApi
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         if (getCellBandwidths().length > 1) return true;
1416 
1417         synchronized (mNetworkRegistrationInfos) {
1418             for (NetworkRegistrationInfo nri : mNetworkRegistrationInfos) {
1419                 if (nri.isUsingCarrierAggregation()) return true;
1420             }
1421         }
1422         return false;
1423     }
1424 
1425     /**
1426      * Get the 5G NR frequency range the device is currently registered.
1427      *
1428      * @return the frequency range of 5G NR.
1429      * @hide
1430      */
getNrFrequencyRange()1431     public @FrequencyRange int getNrFrequencyRange() {
1432         return mNrFrequencyRange;
1433     }
1434 
1435     /**
1436      * Get the NR 5G state of the mobile data network.
1437      * @return the NR 5G state.
1438      * @hide
1439      */
getNrState()1440     public @NRState int getNrState() {
1441         final NetworkRegistrationInfo regInfo = getNetworkRegistrationInfo(
1442                 NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
1443         if (regInfo == null) return NetworkRegistrationInfo.NR_STATE_NONE;
1444         return regInfo.getNrState();
1445     }
1446 
1447     /**
1448      * @param nrFrequencyRange the frequency range of 5G NR.
1449      * @hide
1450      */
setNrFrequencyRange(@requencyRange int nrFrequencyRange)1451     public void setNrFrequencyRange(@FrequencyRange int nrFrequencyRange) {
1452         mNrFrequencyRange = nrFrequencyRange;
1453     }
1454 
1455     /** @hide */
getLteEarfcnRsrpBoost()1456     public int getLteEarfcnRsrpBoost() {
1457         return mLteEarfcnRsrpBoost;
1458     }
1459 
1460     /** @hide */
setLteEarfcnRsrpBoost(int LteEarfcnRsrpBoost)1461     public void setLteEarfcnRsrpBoost(int LteEarfcnRsrpBoost) {
1462         mLteEarfcnRsrpBoost = LteEarfcnRsrpBoost;
1463     }
1464 
1465     /** @hide */
1466     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
setCssIndicator(int css)1467     public void setCssIndicator(int css) {
1468         this.mCssIndicator = (css != 0);
1469     }
1470 
1471     /** @hide */
1472     @TestApi
setCdmaSystemAndNetworkId(int systemId, int networkId)1473     public void setCdmaSystemAndNetworkId(int systemId, int networkId) {
1474         this.mSystemId = systemId;
1475         this.mNetworkId = networkId;
1476     }
1477 
1478     /** @hide */
1479     @UnsupportedAppUsage
getRilVoiceRadioTechnology()1480     public int getRilVoiceRadioTechnology() {
1481         NetworkRegistrationInfo wwanRegInfo = getNetworkRegistrationInfo(
1482                 NetworkRegistrationInfo.DOMAIN_CS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
1483         if (wwanRegInfo != null) {
1484             return networkTypeToRilRadioTechnology(wwanRegInfo.getAccessNetworkTechnology());
1485         }
1486         return RIL_RADIO_TECHNOLOGY_UNKNOWN;
1487     }
1488     /** @hide */
1489     @UnsupportedAppUsage
getRilDataRadioTechnology()1490     public int getRilDataRadioTechnology() {
1491         return networkTypeToRilRadioTechnology(getDataNetworkType());
1492     }
1493 
1494     /**
1495      * @hide
1496      * @Deprecated to be removed Q3 2013 use {@link #getRilDataRadioTechnology} or
1497      * {@link #getRilVoiceRadioTechnology}
1498      */
1499     @UnsupportedAppUsage
getRadioTechnology()1500     public int getRadioTechnology() {
1501         Rlog.e(LOG_TAG, "ServiceState.getRadioTechnology() DEPRECATED will be removed *******");
1502         return getRilDataRadioTechnology();
1503     }
1504 
1505     /**
1506      * Transform RIL radio technology {@link RilRadioTechnology} value to Network
1507      * type {@link NetworkType}.
1508      *
1509      * @param rat The RIL radio technology {@link RilRadioTechnology}.
1510      * @return The network type {@link NetworkType}.
1511      *
1512      * @hide
1513      */
rilRadioTechnologyToNetworkType(@ilRadioTechnology int rat)1514     public static int rilRadioTechnologyToNetworkType(@RilRadioTechnology int rat) {
1515         switch(rat) {
1516             case RIL_RADIO_TECHNOLOGY_GPRS:
1517                 return TelephonyManager.NETWORK_TYPE_GPRS;
1518             case RIL_RADIO_TECHNOLOGY_EDGE:
1519                 return TelephonyManager.NETWORK_TYPE_EDGE;
1520             case RIL_RADIO_TECHNOLOGY_UMTS:
1521                 return TelephonyManager.NETWORK_TYPE_UMTS;
1522             case RIL_RADIO_TECHNOLOGY_HSDPA:
1523                 return TelephonyManager.NETWORK_TYPE_HSDPA;
1524             case RIL_RADIO_TECHNOLOGY_HSUPA:
1525                 return TelephonyManager.NETWORK_TYPE_HSUPA;
1526             case RIL_RADIO_TECHNOLOGY_HSPA:
1527                 return TelephonyManager.NETWORK_TYPE_HSPA;
1528             case RIL_RADIO_TECHNOLOGY_IS95A:
1529             case RIL_RADIO_TECHNOLOGY_IS95B:
1530                 return TelephonyManager.NETWORK_TYPE_CDMA;
1531             case RIL_RADIO_TECHNOLOGY_1xRTT:
1532                 return TelephonyManager.NETWORK_TYPE_1xRTT;
1533             case RIL_RADIO_TECHNOLOGY_EVDO_0:
1534                 return TelephonyManager.NETWORK_TYPE_EVDO_0;
1535             case RIL_RADIO_TECHNOLOGY_EVDO_A:
1536                 return TelephonyManager.NETWORK_TYPE_EVDO_A;
1537             case RIL_RADIO_TECHNOLOGY_EVDO_B:
1538                 return TelephonyManager.NETWORK_TYPE_EVDO_B;
1539             case RIL_RADIO_TECHNOLOGY_EHRPD:
1540                 return TelephonyManager.NETWORK_TYPE_EHRPD;
1541             case RIL_RADIO_TECHNOLOGY_LTE:
1542                 return TelephonyManager.NETWORK_TYPE_LTE;
1543             case RIL_RADIO_TECHNOLOGY_HSPAP:
1544                 return TelephonyManager.NETWORK_TYPE_HSPAP;
1545             case RIL_RADIO_TECHNOLOGY_GSM:
1546                 return TelephonyManager.NETWORK_TYPE_GSM;
1547             case RIL_RADIO_TECHNOLOGY_TD_SCDMA:
1548                 return TelephonyManager.NETWORK_TYPE_TD_SCDMA;
1549             case RIL_RADIO_TECHNOLOGY_IWLAN:
1550                 return TelephonyManager.NETWORK_TYPE_IWLAN;
1551             case RIL_RADIO_TECHNOLOGY_LTE_CA:
1552                 return TelephonyManager.NETWORK_TYPE_LTE_CA;
1553             case RIL_RADIO_TECHNOLOGY_NR:
1554                 return TelephonyManager.NETWORK_TYPE_NR;
1555             default:
1556                 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1557         }
1558     }
1559 
1560     /** @hide */
rilRadioTechnologyToAccessNetworkType(@ilRadioTechnology int rt)1561     public static int rilRadioTechnologyToAccessNetworkType(@RilRadioTechnology int rt) {
1562         switch(rt) {
1563             case RIL_RADIO_TECHNOLOGY_GPRS:
1564             case RIL_RADIO_TECHNOLOGY_EDGE:
1565             case RIL_RADIO_TECHNOLOGY_GSM:
1566                 return AccessNetworkType.GERAN;
1567             case RIL_RADIO_TECHNOLOGY_UMTS:
1568             case RIL_RADIO_TECHNOLOGY_HSDPA:
1569             case RIL_RADIO_TECHNOLOGY_HSPAP:
1570             case RIL_RADIO_TECHNOLOGY_HSUPA:
1571             case RIL_RADIO_TECHNOLOGY_HSPA:
1572             case RIL_RADIO_TECHNOLOGY_TD_SCDMA:
1573                 return AccessNetworkType.UTRAN;
1574             case RIL_RADIO_TECHNOLOGY_IS95A:
1575             case RIL_RADIO_TECHNOLOGY_IS95B:
1576             case RIL_RADIO_TECHNOLOGY_1xRTT:
1577             case RIL_RADIO_TECHNOLOGY_EVDO_0:
1578             case RIL_RADIO_TECHNOLOGY_EVDO_A:
1579             case RIL_RADIO_TECHNOLOGY_EVDO_B:
1580             case RIL_RADIO_TECHNOLOGY_EHRPD:
1581                 return AccessNetworkType.CDMA2000;
1582             case RIL_RADIO_TECHNOLOGY_LTE:
1583             case RIL_RADIO_TECHNOLOGY_LTE_CA:
1584                 return AccessNetworkType.EUTRAN;
1585             case RIL_RADIO_TECHNOLOGY_NR:
1586                 return AccessNetworkType.NGRAN;
1587             case RIL_RADIO_TECHNOLOGY_IWLAN:
1588                 return AccessNetworkType.IWLAN;
1589             case RIL_RADIO_TECHNOLOGY_UNKNOWN:
1590             default:
1591                 return AccessNetworkType.UNKNOWN;
1592         }
1593     }
1594 
1595     /**
1596      * Transform network type {@link NetworkType} value to RIL radio technology
1597      * {@link RilRadioTechnology}.
1598      *
1599      * @param networkType The network type {@link NetworkType}.
1600      * @return The RIL radio technology {@link RilRadioTechnology}.
1601      *
1602      * @hide
1603      */
networkTypeToRilRadioTechnology(int networkType)1604     public static int networkTypeToRilRadioTechnology(int networkType) {
1605         switch(networkType) {
1606             case TelephonyManager.NETWORK_TYPE_GPRS:
1607                 return RIL_RADIO_TECHNOLOGY_GPRS;
1608             case TelephonyManager.NETWORK_TYPE_EDGE:
1609                 return RIL_RADIO_TECHNOLOGY_EDGE;
1610             case TelephonyManager.NETWORK_TYPE_UMTS:
1611                 return RIL_RADIO_TECHNOLOGY_UMTS;
1612             case TelephonyManager.NETWORK_TYPE_HSDPA:
1613                 return RIL_RADIO_TECHNOLOGY_HSDPA;
1614             case TelephonyManager.NETWORK_TYPE_HSUPA:
1615                 return RIL_RADIO_TECHNOLOGY_HSUPA;
1616             case TelephonyManager.NETWORK_TYPE_HSPA:
1617                 return RIL_RADIO_TECHNOLOGY_HSPA;
1618             case TelephonyManager.NETWORK_TYPE_CDMA:
1619                 return RIL_RADIO_TECHNOLOGY_IS95A;
1620             case TelephonyManager.NETWORK_TYPE_1xRTT:
1621                 return RIL_RADIO_TECHNOLOGY_1xRTT;
1622             case TelephonyManager.NETWORK_TYPE_EVDO_0:
1623                 return RIL_RADIO_TECHNOLOGY_EVDO_0;
1624             case TelephonyManager.NETWORK_TYPE_EVDO_A:
1625                 return RIL_RADIO_TECHNOLOGY_EVDO_A;
1626             case TelephonyManager.NETWORK_TYPE_EVDO_B:
1627                 return RIL_RADIO_TECHNOLOGY_EVDO_B;
1628             case TelephonyManager.NETWORK_TYPE_EHRPD:
1629                 return RIL_RADIO_TECHNOLOGY_EHRPD;
1630             case TelephonyManager.NETWORK_TYPE_LTE:
1631                 return RIL_RADIO_TECHNOLOGY_LTE;
1632             case TelephonyManager.NETWORK_TYPE_HSPAP:
1633                 return RIL_RADIO_TECHNOLOGY_HSPAP;
1634             case TelephonyManager.NETWORK_TYPE_GSM:
1635                 return RIL_RADIO_TECHNOLOGY_GSM;
1636             case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
1637                 return RIL_RADIO_TECHNOLOGY_TD_SCDMA;
1638             case TelephonyManager.NETWORK_TYPE_IWLAN:
1639                 return RIL_RADIO_TECHNOLOGY_IWLAN;
1640             case TelephonyManager.NETWORK_TYPE_LTE_CA:
1641                 return RIL_RADIO_TECHNOLOGY_LTE_CA;
1642             case TelephonyManager.NETWORK_TYPE_NR:
1643                 return RIL_RADIO_TECHNOLOGY_NR;
1644             default:
1645                 return RIL_RADIO_TECHNOLOGY_UNKNOWN;
1646         }
1647     }
1648 
1649     /**
1650      * Get current data network type.
1651      *
1652      * Note that for IWLAN AP-assisted mode device, which is reporting both camped access networks
1653      * (cellular RAT and IWLAN)at the same time, this API is simulating the old legacy mode device
1654      * behavior,
1655      *
1656      * @return Current data network type
1657      * @hide
1658      */
1659     @SystemApi
1660     @TestApi
getDataNetworkType()1661     public @NetworkType int getDataNetworkType() {
1662         final NetworkRegistrationInfo iwlanRegInfo = getNetworkRegistrationInfo(
1663                 NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WLAN);
1664         final NetworkRegistrationInfo wwanRegInfo = getNetworkRegistrationInfo(
1665                 NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
1666 
1667         // For legacy mode device, or AP-assisted mode device but IWLAN is out of service, use
1668         // the RAT from cellular.
1669         if (iwlanRegInfo == null || !iwlanRegInfo.isInService()) {
1670             return (wwanRegInfo != null) ? wwanRegInfo.getAccessNetworkTechnology()
1671                     : TelephonyManager.NETWORK_TYPE_UNKNOWN;
1672         }
1673 
1674         // At this point, it must be an AP-assisted mode device and IWLAN is in service. We should
1675         // use the RAT from IWLAN service is cellular is out of service, or when both are in service
1676         // and any APN type of data is preferred on IWLAN.
1677         if (!wwanRegInfo.isInService() || mIsIwlanPreferred) {
1678             return iwlanRegInfo.getAccessNetworkTechnology();
1679         }
1680 
1681         // If both cellular and IWLAN are in service, but no APN is preferred on IWLAN, still use
1682         // the RAT from cellular.
1683         return wwanRegInfo.getAccessNetworkTechnology();
1684     }
1685 
1686     /** @hide */
1687     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
getVoiceNetworkType()1688     public @NetworkType int getVoiceNetworkType() {
1689         final NetworkRegistrationInfo regState = getNetworkRegistrationInfo(
1690                 NetworkRegistrationInfo.DOMAIN_CS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
1691         if (regState != null) {
1692             return regState.getAccessNetworkTechnology();
1693         }
1694         return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1695     }
1696 
1697     /** @hide */
1698     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
getCssIndicator()1699     public int getCssIndicator() {
1700         return this.mCssIndicator ? 1 : 0;
1701     }
1702 
1703     /**
1704      * Get the CDMA NID (Network Identification Number), a number uniquely identifying a network
1705      * within a wireless system. (Defined in 3GPP2 C.S0023 3.4.8)
1706      * @return The CDMA NID or {@link #UNKNOWN_ID} if not available.
1707      */
getCdmaNetworkId()1708     public int getCdmaNetworkId() {
1709         return this.mNetworkId;
1710     }
1711 
1712     /**
1713      * Get the CDMA SID (System Identification Number), a number uniquely identifying a wireless
1714      * system. (Defined in 3GPP2 C.S0023 3.4.8)
1715      * @return The CDMA SID or {@link #UNKNOWN_ID} if not available.
1716      */
getCdmaSystemId()1717     public int getCdmaSystemId() {
1718         return this.mSystemId;
1719     }
1720 
1721     /** @hide */
1722     @UnsupportedAppUsage
isGsm(int radioTechnology)1723     public static boolean isGsm(int radioTechnology) {
1724         return radioTechnology == RIL_RADIO_TECHNOLOGY_GPRS
1725                 || radioTechnology == RIL_RADIO_TECHNOLOGY_EDGE
1726                 || radioTechnology == RIL_RADIO_TECHNOLOGY_UMTS
1727                 || radioTechnology == RIL_RADIO_TECHNOLOGY_HSDPA
1728                 || radioTechnology == RIL_RADIO_TECHNOLOGY_HSUPA
1729                 || radioTechnology == RIL_RADIO_TECHNOLOGY_HSPA
1730                 || radioTechnology == RIL_RADIO_TECHNOLOGY_LTE
1731                 || radioTechnology == RIL_RADIO_TECHNOLOGY_HSPAP
1732                 || radioTechnology == RIL_RADIO_TECHNOLOGY_GSM
1733                 || radioTechnology == RIL_RADIO_TECHNOLOGY_TD_SCDMA
1734                 || radioTechnology == RIL_RADIO_TECHNOLOGY_IWLAN
1735                 || radioTechnology == RIL_RADIO_TECHNOLOGY_LTE_CA
1736                 || radioTechnology == RIL_RADIO_TECHNOLOGY_NR;
1737 
1738     }
1739 
1740     /** @hide */
1741     @UnsupportedAppUsage
isCdma(int radioTechnology)1742     public static boolean isCdma(int radioTechnology) {
1743         return radioTechnology == RIL_RADIO_TECHNOLOGY_IS95A
1744                 || radioTechnology == RIL_RADIO_TECHNOLOGY_IS95B
1745                 || radioTechnology == RIL_RADIO_TECHNOLOGY_1xRTT
1746                 || radioTechnology == RIL_RADIO_TECHNOLOGY_EVDO_0
1747                 || radioTechnology == RIL_RADIO_TECHNOLOGY_EVDO_A
1748                 || radioTechnology == RIL_RADIO_TECHNOLOGY_EVDO_B
1749                 || radioTechnology == RIL_RADIO_TECHNOLOGY_EHRPD;
1750     }
1751 
1752     /** @hide */
isPsOnlyTech(int radioTechnology)1753     public static boolean isPsOnlyTech(int radioTechnology) {
1754         return radioTechnology == RIL_RADIO_TECHNOLOGY_LTE
1755                 || radioTechnology == RIL_RADIO_TECHNOLOGY_LTE_CA
1756                 || radioTechnology == RIL_RADIO_TECHNOLOGY_NR;
1757     }
1758 
1759     /** @hide */
1760     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
bearerBitmapHasCdma(int networkTypeBitmask)1761     public static boolean bearerBitmapHasCdma(int networkTypeBitmask) {
1762         return (RIL_RADIO_CDMA_TECHNOLOGY_BITMASK
1763                 & convertNetworkTypeBitmaskToBearerBitmask(networkTypeBitmask)) != 0;
1764     }
1765 
1766     /** @hide */
1767     @UnsupportedAppUsage
bitmaskHasTech(int bearerBitmask, int radioTech)1768     public static boolean bitmaskHasTech(int bearerBitmask, int radioTech) {
1769         if (bearerBitmask == 0) {
1770             return true;
1771         } else if (radioTech >= 1) {
1772             return ((bearerBitmask & (1 << (radioTech - 1))) != 0);
1773         }
1774         return false;
1775     }
1776 
1777     /** @hide */
getBitmaskForTech(int radioTech)1778     public static int getBitmaskForTech(int radioTech) {
1779         if (radioTech >= 1) {
1780             return (1 << (radioTech - 1));
1781         }
1782         return 0;
1783     }
1784 
1785     /** @hide */
getBitmaskFromString(String bearerList)1786     public static int getBitmaskFromString(String bearerList) {
1787         String[] bearers = bearerList.split("\\|");
1788         int bearerBitmask = 0;
1789         for (String bearer : bearers) {
1790             int bearerInt = 0;
1791             try {
1792                 bearerInt = Integer.parseInt(bearer.trim());
1793             } catch (NumberFormatException nfe) {
1794                 return 0;
1795             }
1796 
1797             if (bearerInt == 0) {
1798                 return 0;
1799             }
1800 
1801             bearerBitmask |= getBitmaskForTech(bearerInt);
1802         }
1803         return bearerBitmask;
1804     }
1805 
1806     /**
1807      * Convert network type bitmask to bearer bitmask.
1808      *
1809      * @param networkTypeBitmask The network type bitmask value
1810      * @return The bearer bitmask value.
1811      *
1812      * @hide
1813      */
convertNetworkTypeBitmaskToBearerBitmask(int networkTypeBitmask)1814     public static int convertNetworkTypeBitmaskToBearerBitmask(int networkTypeBitmask) {
1815         if (networkTypeBitmask == 0) {
1816             return 0;
1817         }
1818         int bearerBitmask = 0;
1819         for (int bearerInt = 0; bearerInt < NEXT_RIL_RADIO_TECHNOLOGY; bearerInt++) {
1820             if (bitmaskHasTech(networkTypeBitmask, rilRadioTechnologyToNetworkType(bearerInt))) {
1821                 bearerBitmask |= getBitmaskForTech(bearerInt);
1822             }
1823         }
1824         return bearerBitmask;
1825     }
1826 
1827     /**
1828      * Convert bearer bitmask to network type bitmask.
1829      *
1830      * @param bearerBitmask The bearer bitmask value.
1831      * @return The network type bitmask value.
1832      *
1833      * @hide
1834      */
convertBearerBitmaskToNetworkTypeBitmask(int bearerBitmask)1835     public static int convertBearerBitmaskToNetworkTypeBitmask(int bearerBitmask) {
1836         if (bearerBitmask == 0) {
1837             return 0;
1838         }
1839         int networkTypeBitmask = 0;
1840         for (int bearerInt = 0; bearerInt < NEXT_RIL_RADIO_TECHNOLOGY; bearerInt++) {
1841             if (bitmaskHasTech(bearerBitmask, bearerInt)) {
1842                 networkTypeBitmask |= getBitmaskForTech(rilRadioTechnologyToNetworkType(bearerInt));
1843             }
1844         }
1845         return networkTypeBitmask;
1846     }
1847 
1848     /**
1849      * Returns a merged ServiceState consisting of the base SS with voice settings from the
1850      * voice SS. The voice SS is only used if it is IN_SERVICE (otherwise the base SS is returned).
1851      * @hide
1852      * */
1853     @UnsupportedAppUsage
mergeServiceStates(ServiceState baseSs, ServiceState voiceSs)1854     public static ServiceState mergeServiceStates(ServiceState baseSs, ServiceState voiceSs) {
1855         if (voiceSs.mVoiceRegState != STATE_IN_SERVICE) {
1856             return baseSs;
1857         }
1858 
1859         ServiceState newSs = new ServiceState(baseSs);
1860 
1861         // voice overrides
1862         newSs.mVoiceRegState = voiceSs.mVoiceRegState;
1863         newSs.mIsEmergencyOnly = false; // only get here if voice is IN_SERVICE
1864 
1865         return newSs;
1866     }
1867 
1868     /**
1869      * Get all of the available network registration info.
1870      *
1871      * @return List of {@link NetworkRegistrationInfo}
1872      */
1873     @NonNull
getNetworkRegistrationInfoList()1874     public List<NetworkRegistrationInfo> getNetworkRegistrationInfoList() {
1875         synchronized (mNetworkRegistrationInfos) {
1876             List<NetworkRegistrationInfo> newList = new ArrayList<>();
1877             for (NetworkRegistrationInfo nri : mNetworkRegistrationInfos) {
1878                 newList.add(new NetworkRegistrationInfo(nri));
1879             }
1880             return newList;
1881         }
1882     }
1883 
1884     /**
1885      * Get the network registration info list for the transport type.
1886      *
1887      * @param transportType The transport type
1888      * @return List of {@link NetworkRegistrationInfo}
1889      * @hide
1890      */
1891     @NonNull
1892     @SystemApi
getNetworkRegistrationInfoListForTransportType( @ransportType int transportType)1893     public List<NetworkRegistrationInfo> getNetworkRegistrationInfoListForTransportType(
1894             @TransportType int transportType) {
1895         List<NetworkRegistrationInfo> list = new ArrayList<>();
1896 
1897         synchronized (mNetworkRegistrationInfos) {
1898             for (NetworkRegistrationInfo networkRegistrationInfo : mNetworkRegistrationInfos) {
1899                 if (networkRegistrationInfo.getTransportType() == transportType) {
1900                     list.add(new NetworkRegistrationInfo(networkRegistrationInfo));
1901                 }
1902             }
1903         }
1904 
1905         return list;
1906     }
1907 
1908     /**
1909      * Get the network registration info list for the network domain.
1910      *
1911      * @param domain The network {@link NetworkRegistrationInfo.Domain domain}
1912      * @return List of {@link NetworkRegistrationInfo}
1913      * @hide
1914      */
1915     @NonNull
1916     @SystemApi
getNetworkRegistrationInfoListForDomain( @omain int domain)1917     public List<NetworkRegistrationInfo> getNetworkRegistrationInfoListForDomain(
1918             @Domain int domain) {
1919         List<NetworkRegistrationInfo> list = new ArrayList<>();
1920 
1921         synchronized (mNetworkRegistrationInfos) {
1922             for (NetworkRegistrationInfo networkRegistrationInfo : mNetworkRegistrationInfos) {
1923                 if ((networkRegistrationInfo.getDomain() & domain) != 0) {
1924                     list.add(new NetworkRegistrationInfo(networkRegistrationInfo));
1925                 }
1926             }
1927         }
1928 
1929         return list;
1930     }
1931 
1932     /**
1933      * Get the network registration state for the transport type and network domain.
1934      * If multiple domains are in the input bitmask, only the first one from
1935      * networkRegistrationInfo.getDomain() will be returned.
1936      *
1937      * @param domain The network {@link NetworkRegistrationInfo.Domain domain}
1938      * @param transportType The transport type
1939      * @return The matching {@link NetworkRegistrationInfo}
1940      * @hide
1941      */
1942     @Nullable
1943     @SystemApi
getNetworkRegistrationInfo(@omain int domain, @TransportType int transportType)1944     public NetworkRegistrationInfo getNetworkRegistrationInfo(@Domain int domain,
1945                                                               @TransportType int transportType) {
1946         synchronized (mNetworkRegistrationInfos) {
1947             for (NetworkRegistrationInfo networkRegistrationInfo : mNetworkRegistrationInfos) {
1948                 if (networkRegistrationInfo.getTransportType() == transportType
1949                         && (networkRegistrationInfo.getDomain() & domain) != 0) {
1950                     return new NetworkRegistrationInfo(networkRegistrationInfo);
1951                 }
1952             }
1953         }
1954 
1955         return null;
1956     }
1957 
1958     /**
1959      * @hide
1960      */
1961     @TestApi
addNetworkRegistrationInfo(NetworkRegistrationInfo nri)1962     public void addNetworkRegistrationInfo(NetworkRegistrationInfo nri) {
1963         if (nri == null) return;
1964 
1965         synchronized (mNetworkRegistrationInfos) {
1966             for (int i = 0; i < mNetworkRegistrationInfos.size(); i++) {
1967                 NetworkRegistrationInfo curRegState = mNetworkRegistrationInfos.get(i);
1968                 if (curRegState.getTransportType() == nri.getTransportType()
1969                         && curRegState.getDomain() == nri.getDomain()) {
1970                     mNetworkRegistrationInfos.remove(i);
1971                     break;
1972                 }
1973             }
1974 
1975             mNetworkRegistrationInfos.add(new NetworkRegistrationInfo(nri));
1976         }
1977     }
1978 
1979     /**
1980      * @hide
1981      */
getBetterNRFrequencyRange(int range1, int range2)1982     public static final int getBetterNRFrequencyRange(int range1, int range2) {
1983         return FREQUENCY_RANGE_ORDER.indexOf(range1) > FREQUENCY_RANGE_ORDER.indexOf(range2)
1984                 ? range1
1985                 : range2;
1986     }
1987 
1988     /**
1989      * Returns a copy of self with location-identifying information removed.
1990      * Always clears the NetworkRegistrationInfo's CellIdentity fields, but if removeCoarseLocation
1991      * is true, clears other info as well.
1992      *
1993      * @param removeCoarseLocation Whether to also remove coarse location information.
1994      *                             if false, it only clears fine location information such as
1995      *                             NetworkRegistrationInfo's CellIdentity fields; If true, it will
1996      *                             also remove other location information such as operator's MCC
1997      *                             and MNC.
1998      * @return the copied ServiceState with location info sanitized.
1999      * @hide
2000      */
2001     @NonNull
createLocationInfoSanitizedCopy(boolean removeCoarseLocation)2002     public ServiceState createLocationInfoSanitizedCopy(boolean removeCoarseLocation) {
2003         ServiceState state = new ServiceState(this);
2004         synchronized (state.mNetworkRegistrationInfos) {
2005             List<NetworkRegistrationInfo> networkRegistrationInfos =
2006                     state.mNetworkRegistrationInfos.stream()
2007                             .map(NetworkRegistrationInfo::sanitizeLocationInfo)
2008                             .collect(Collectors.toList());
2009             state.mNetworkRegistrationInfos.clear();
2010             state.mNetworkRegistrationInfos.addAll(networkRegistrationInfos);
2011         }
2012         if (!removeCoarseLocation) return state;
2013 
2014         state.mOperatorAlphaLong = null;
2015         state.mOperatorAlphaShort = null;
2016         state.mOperatorNumeric = null;
2017 
2018         return state;
2019     }
2020 
2021     /**
2022      * @hide
2023      */
setOperatorAlphaLongRaw(String operatorAlphaLong)2024     public void setOperatorAlphaLongRaw(String operatorAlphaLong) {
2025         mOperatorAlphaLongRaw = operatorAlphaLong;
2026     }
2027 
2028     /**
2029      * The current registered raw data network operator name in long alphanumeric format.
2030      *
2031      * @return long raw name of operator, null if unregistered or unknown
2032      * @hide
2033      */
2034     @Nullable
2035     @SystemApi
getOperatorAlphaLongRaw()2036     public String getOperatorAlphaLongRaw() {
2037         return mOperatorAlphaLongRaw;
2038     }
2039 
2040     /**
2041      * @hide
2042      */
setOperatorAlphaShortRaw(String operatorAlphaShort)2043     public void setOperatorAlphaShortRaw(String operatorAlphaShort) {
2044         mOperatorAlphaShortRaw = operatorAlphaShort;
2045     }
2046 
2047     /**
2048      * The current registered raw data network operator name in short alphanumeric format.
2049      *
2050      * @return short raw name of operator, null if unregistered or unknown
2051      * @hide
2052      */
2053     @Nullable
2054     @SystemApi
getOperatorAlphaShortRaw()2055     public String getOperatorAlphaShortRaw() {
2056         return mOperatorAlphaShortRaw;
2057     }
2058 
2059     /**
2060      * Set to {@code true} if any data network is preferred on IWLAN.
2061      *
2062      * @param isIwlanPreferred {@code true} if IWLAN is preferred.
2063      * @hide
2064      */
setIwlanPreferred(boolean isIwlanPreferred)2065     public void setIwlanPreferred(boolean isIwlanPreferred) {
2066         mIsIwlanPreferred = isIwlanPreferred;
2067     }
2068 
2069     /**
2070      * @return {@code true} if any data network is preferred on IWLAN.
2071      *
2072      * Note only when this value is true, {@link #getDataNetworkType()} will return
2073      * {@link TelephonyManager#NETWORK_TYPE_IWLAN} when AP-assisted mode device camps on both
2074      * cellular and IWLAN. This value does not affect legacy mode devices as the data network
2075      * type is directly reported by the modem.
2076      *
2077      * @hide
2078      */
isIwlanPreferred()2079     public boolean isIwlanPreferred() {
2080         return mIsIwlanPreferred;
2081     }
2082 
2083     /**
2084      * This indicates whether the device is searching for service.
2085      *
2086      * This API reports the modem searching status for
2087      * {@link AccessNetworkConstants#TRANSPORT_TYPE_WWAN} (cellular) service in either
2088      * {@link NetworkRegistrationInfo#DOMAIN_CS} or {@link NetworkRegistrationInfo#DOMAIN_PS}.
2089      * This API will not report searching status for
2090      * {@link AccessNetworkConstants#TRANSPORT_TYPE_WLAN}.
2091      *
2092      * @return {@code true} whenever the modem is searching for service.
2093      */
isSearching()2094     public boolean isSearching() {
2095         NetworkRegistrationInfo psRegState = getNetworkRegistrationInfo(
2096                 NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
2097 
2098         if (psRegState != null && psRegState.getRegistrationState()
2099                 == NetworkRegistrationInfo.REGISTRATION_STATE_NOT_REGISTERED_SEARCHING) {
2100             return true;
2101         }
2102 
2103         NetworkRegistrationInfo csRegState = getNetworkRegistrationInfo(
2104                 NetworkRegistrationInfo.DOMAIN_CS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN);
2105 
2106         if (csRegState != null && csRegState.getRegistrationState()
2107                 == NetworkRegistrationInfo.REGISTRATION_STATE_NOT_REGISTERED_SEARCHING) {
2108             return true;
2109         }
2110         return false;
2111     }
2112 }
2113