1 /*
2 * Copyright (C) 2014 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.compat.annotation.UnsupportedAppUsage;
20 import android.os.Build;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 import android.telephony.TelephonyManager.PrefNetworkMode;
24 
25 import com.android.internal.telephony.RILConstants;
26 
27 
28 /**
29  * Object to indicate the phone radio type and access technology.
30  *
31  * @hide
32  */
33 public class RadioAccessFamily implements Parcelable {
34 
35     /**
36      * TODO: get rid of RAF definition in RadioAccessFamily and
37      * use {@link TelephonyManager.NetworkTypeBitMask}
38      * TODO: public definition {@link TelephonyManager.NetworkTypeBitMask} is long.
39      * TODO: Convert from int to long everywhere including HAL definitions.
40      */
41     // 2G
42     public static final int RAF_UNKNOWN = (int) TelephonyManager.NETWORK_TYPE_BITMASK_UNKNOWN;
43     public static final int RAF_GSM = (int) TelephonyManager.NETWORK_TYPE_BITMASK_GSM;
44     public static final int RAF_GPRS = (int) TelephonyManager.NETWORK_TYPE_BITMASK_GPRS;
45     public static final int RAF_EDGE = (int) TelephonyManager.NETWORK_TYPE_BITMASK_EDGE;
46     public static final int RAF_IS95A = (int) TelephonyManager.NETWORK_TYPE_BITMASK_CDMA;
47     public static final int RAF_IS95B = (int) TelephonyManager.NETWORK_TYPE_BITMASK_CDMA;
48     public static final int RAF_1xRTT = (int) TelephonyManager.NETWORK_TYPE_BITMASK_1xRTT;
49     // 3G
50     public static final int RAF_EVDO_0 = (int) TelephonyManager.NETWORK_TYPE_BITMASK_EVDO_0;
51     public static final int RAF_EVDO_A = (int) TelephonyManager.NETWORK_TYPE_BITMASK_EVDO_A;
52     public static final int RAF_EVDO_B = (int) TelephonyManager.NETWORK_TYPE_BITMASK_EVDO_B;
53     public static final int RAF_EHRPD = (int) TelephonyManager.NETWORK_TYPE_BITMASK_EHRPD;
54     public static final int RAF_HSUPA = (int) TelephonyManager.NETWORK_TYPE_BITMASK_HSUPA;
55     public static final int RAF_HSDPA = (int) TelephonyManager.NETWORK_TYPE_BITMASK_HSDPA;
56     public static final int RAF_HSPA = (int) TelephonyManager.NETWORK_TYPE_BITMASK_HSPA;
57     public static final int RAF_HSPAP = (int) TelephonyManager.NETWORK_TYPE_BITMASK_HSPAP;
58     public static final int RAF_UMTS = (int) TelephonyManager.NETWORK_TYPE_BITMASK_UMTS;
59     public static final int RAF_TD_SCDMA = (int) TelephonyManager.NETWORK_TYPE_BITMASK_TD_SCDMA;
60     // 4G
61     public static final int RAF_LTE = (int) TelephonyManager.NETWORK_TYPE_BITMASK_LTE;
62     public static final int RAF_LTE_CA = (int) TelephonyManager.NETWORK_TYPE_BITMASK_LTE_CA;
63 
64     // 5G
65     public static final int RAF_NR = (int) TelephonyManager.NETWORK_TYPE_BITMASK_NR;
66 
67     // Grouping of RAFs
68     // 2G
69     private static final int GSM = RAF_GSM | RAF_GPRS | RAF_EDGE;
70     private static final int CDMA = RAF_IS95A | RAF_IS95B | RAF_1xRTT;
71     // 3G
72     private static final int EVDO = RAF_EVDO_0 | RAF_EVDO_A | RAF_EVDO_B | RAF_EHRPD;
73     private static final int HS = RAF_HSUPA | RAF_HSDPA | RAF_HSPA | RAF_HSPAP;
74     private static final int WCDMA = HS | RAF_UMTS;
75     // 4G
76     private static final int LTE = RAF_LTE | RAF_LTE_CA;
77 
78     // 5G
79     private static final int NR = RAF_NR;
80 
81     /* Phone ID of phone */
82     private int mPhoneId;
83 
84     /* Radio Access Family */
85     private int mRadioAccessFamily;
86 
87     /**
88      * Constructor.
89      *
90      * @param phoneId the phone ID
91      * @param radioAccessFamily the phone radio access family bitmask based on
92      * {@link TelephonyManager.NetworkTypeBitMask}. It's a bit mask value to represent the support
93      *                          type.
94      */
95     @UnsupportedAppUsage
RadioAccessFamily(int phoneId, int radioAccessFamily)96     public RadioAccessFamily(int phoneId, int radioAccessFamily) {
97         mPhoneId = phoneId;
98         mRadioAccessFamily = radioAccessFamily;
99     }
100 
101     /**
102      * Get phone ID.
103      *
104      * @return phone ID
105      */
106     @UnsupportedAppUsage
getPhoneId()107     public int getPhoneId() {
108         return mPhoneId;
109     }
110 
111     /**
112      * get radio access family.
113      *
114      * @return radio access family
115      */
116     @UnsupportedAppUsage
getRadioAccessFamily()117     public @TelephonyManager.NetworkTypeBitMask int getRadioAccessFamily() {
118         return mRadioAccessFamily;
119     }
120 
121     @Override
toString()122     public String toString() {
123         String ret = "{ mPhoneId = " + mPhoneId
124                 + ", mRadioAccessFamily = " + mRadioAccessFamily
125                 + "}";
126         return ret;
127     }
128 
129     /**
130      * Implement the Parcelable interface.
131      *
132      * @return describe content
133      */
134     @Override
describeContents()135     public int describeContents() {
136         return 0;
137     }
138 
139     /**
140      * Implement the Parcelable interface.
141      *
142      * @param outParcel The Parcel in which the object should be written.
143      * @param flags Additional flags about how the object should be written.
144      */
145     @Override
writeToParcel(Parcel outParcel, int flags)146     public void writeToParcel(Parcel outParcel, int flags) {
147         outParcel.writeInt(mPhoneId);
148         outParcel.writeInt(mRadioAccessFamily);
149     }
150 
151     /**
152      * Implement the Parcelable interface.
153      */
154     public static final @android.annotation.NonNull Creator<android.telephony.RadioAccessFamily> CREATOR =
155             new Creator<android.telephony.RadioAccessFamily>() {
156 
157         @Override
158         public android.telephony.RadioAccessFamily createFromParcel(Parcel in) {
159             int phoneId = in.readInt();
160             int radioAccessFamily = in.readInt();
161 
162             return new android.telephony.RadioAccessFamily(phoneId, radioAccessFamily);
163         }
164 
165         @Override
166         public android.telephony.RadioAccessFamily[] newArray(int size) {
167             return new android.telephony.RadioAccessFamily[size];
168         }
169     };
170 
171     @UnsupportedAppUsage
172     @TelephonyManager.NetworkTypeBitMask
getRafFromNetworkType(@refNetworkMode int type)173     public static int getRafFromNetworkType(@PrefNetworkMode int type) {
174         switch (type) {
175             case RILConstants.NETWORK_MODE_WCDMA_PREF:
176                 return GSM | WCDMA;
177             case RILConstants.NETWORK_MODE_GSM_ONLY:
178                 return GSM;
179             case RILConstants.NETWORK_MODE_WCDMA_ONLY:
180                 return WCDMA;
181             case RILConstants.NETWORK_MODE_GSM_UMTS:
182                 return GSM | WCDMA;
183             case RILConstants.NETWORK_MODE_CDMA:
184                 return CDMA | EVDO;
185             case RILConstants.NETWORK_MODE_LTE_CDMA_EVDO:
186                 return LTE | CDMA | EVDO;
187             case RILConstants.NETWORK_MODE_LTE_GSM_WCDMA:
188                 return LTE | GSM | WCDMA;
189             case RILConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA:
190                 return LTE | CDMA | EVDO | GSM | WCDMA;
191             case RILConstants.NETWORK_MODE_LTE_ONLY:
192                 return LTE;
193             case RILConstants.NETWORK_MODE_LTE_WCDMA:
194                 return LTE | WCDMA;
195             case RILConstants.NETWORK_MODE_CDMA_NO_EVDO:
196                 return CDMA;
197             case RILConstants.NETWORK_MODE_EVDO_NO_CDMA:
198                 return EVDO;
199             case RILConstants.NETWORK_MODE_GLOBAL:
200                 return GSM | WCDMA | CDMA | EVDO;
201             case RILConstants.NETWORK_MODE_TDSCDMA_ONLY:
202                 return RAF_TD_SCDMA;
203             case RILConstants.NETWORK_MODE_TDSCDMA_WCDMA:
204                 return RAF_TD_SCDMA | WCDMA;
205             case RILConstants.NETWORK_MODE_LTE_TDSCDMA:
206                 return LTE | RAF_TD_SCDMA;
207             case RILConstants.NETWORK_MODE_TDSCDMA_GSM:
208                 return RAF_TD_SCDMA | GSM;
209             case RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM:
210                 return LTE | RAF_TD_SCDMA | GSM;
211             case RILConstants.NETWORK_MODE_TDSCDMA_GSM_WCDMA:
212                 return RAF_TD_SCDMA | GSM | WCDMA;
213             case RILConstants.NETWORK_MODE_LTE_TDSCDMA_WCDMA:
214                 return LTE | RAF_TD_SCDMA | WCDMA;
215             case RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA:
216                 return LTE | RAF_TD_SCDMA | GSM | WCDMA;
217             case RILConstants.NETWORK_MODE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
218                 return RAF_TD_SCDMA | CDMA | EVDO | GSM | WCDMA;
219             case RILConstants.NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
220                 return LTE | RAF_TD_SCDMA | CDMA | EVDO | GSM | WCDMA;
221             case (RILConstants.NETWORK_MODE_NR_ONLY):
222                 return NR;
223             case (RILConstants.NETWORK_MODE_NR_LTE):
224                 return NR | LTE;
225             case (RILConstants.NETWORK_MODE_NR_LTE_CDMA_EVDO):
226                 return NR | LTE | CDMA | EVDO;
227             case (RILConstants.NETWORK_MODE_NR_LTE_GSM_WCDMA):
228                 return NR | LTE | GSM | WCDMA;
229             case (RILConstants.NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA):
230                 return NR | LTE | CDMA | EVDO | GSM | WCDMA;
231             case (RILConstants.NETWORK_MODE_NR_LTE_WCDMA):
232                 return NR | LTE | WCDMA;
233             case (RILConstants.NETWORK_MODE_NR_LTE_TDSCDMA):
234                 return NR | LTE | RAF_TD_SCDMA;
235             case (RILConstants.NETWORK_MODE_NR_LTE_TDSCDMA_GSM):
236                 return NR | LTE | RAF_TD_SCDMA | GSM;
237             case (RILConstants.NETWORK_MODE_NR_LTE_TDSCDMA_WCDMA):
238                 return NR | LTE | RAF_TD_SCDMA | WCDMA;
239             case (RILConstants.NETWORK_MODE_NR_LTE_TDSCDMA_GSM_WCDMA):
240                 return NR | LTE | RAF_TD_SCDMA | GSM | WCDMA;
241             case (RILConstants.NETWORK_MODE_NR_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA):
242                 return NR | LTE | RAF_TD_SCDMA | CDMA | EVDO | GSM | WCDMA;
243             default:
244                 return RAF_UNKNOWN;
245         }
246     }
247 
248     /**
249      * if the raf includes ANY bit set for a group
250      * adjust it to contain ALL the bits for that group
251      */
getAdjustedRaf(int raf)252     private static int getAdjustedRaf(int raf) {
253         raf = ((GSM & raf) > 0) ? (GSM | raf) : raf;
254         raf = ((WCDMA & raf) > 0) ? (WCDMA | raf) : raf;
255         raf = ((CDMA & raf) > 0) ? (CDMA | raf) : raf;
256         raf = ((EVDO & raf) > 0) ? (EVDO | raf) : raf;
257         raf = ((LTE & raf) > 0) ? (LTE | raf) : raf;
258         raf = ((NR & raf) > 0) ? (NR | raf) : raf;
259 
260         return raf;
261     }
262 
263     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
264     @PrefNetworkMode
getNetworkTypeFromRaf(int raf)265     public static int getNetworkTypeFromRaf(int raf) {
266         raf = getAdjustedRaf(raf);
267 
268         switch (raf) {
269             case (GSM | WCDMA):
270                 return RILConstants.NETWORK_MODE_WCDMA_PREF;
271             case GSM:
272                 return RILConstants.NETWORK_MODE_GSM_ONLY;
273             case WCDMA:
274                 return RILConstants.NETWORK_MODE_WCDMA_ONLY;
275             case (CDMA | EVDO):
276                 return RILConstants.NETWORK_MODE_CDMA;
277             case (LTE | CDMA | EVDO):
278                 return RILConstants.NETWORK_MODE_LTE_CDMA_EVDO;
279             case (LTE | GSM | WCDMA):
280                 return RILConstants.NETWORK_MODE_LTE_GSM_WCDMA;
281             case (LTE | CDMA | EVDO | GSM | WCDMA):
282                 return RILConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA;
283             case LTE:
284                 return RILConstants.NETWORK_MODE_LTE_ONLY;
285             case (LTE | WCDMA):
286                 return RILConstants.NETWORK_MODE_LTE_WCDMA;
287             case CDMA:
288                 return RILConstants.NETWORK_MODE_CDMA_NO_EVDO;
289             case EVDO:
290                 return RILConstants.NETWORK_MODE_EVDO_NO_CDMA;
291             case (GSM | WCDMA | CDMA | EVDO):
292                 return RILConstants.NETWORK_MODE_GLOBAL;
293             case RAF_TD_SCDMA:
294                 return RILConstants.NETWORK_MODE_TDSCDMA_ONLY;
295             case (RAF_TD_SCDMA | WCDMA):
296                 return RILConstants.NETWORK_MODE_TDSCDMA_WCDMA;
297             case (LTE | RAF_TD_SCDMA):
298                 return RILConstants.NETWORK_MODE_LTE_TDSCDMA;
299             case (RAF_TD_SCDMA | GSM):
300                 return RILConstants.NETWORK_MODE_TDSCDMA_GSM;
301             case (LTE | RAF_TD_SCDMA | GSM):
302                 return RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM;
303             case (RAF_TD_SCDMA | GSM | WCDMA):
304                 return RILConstants.NETWORK_MODE_TDSCDMA_GSM_WCDMA;
305             case (LTE | RAF_TD_SCDMA | WCDMA):
306                 return RILConstants.NETWORK_MODE_LTE_TDSCDMA_WCDMA;
307             case (LTE | RAF_TD_SCDMA | GSM | WCDMA):
308                 return RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA;
309             case (RAF_TD_SCDMA | CDMA | EVDO | GSM | WCDMA):
310                 return RILConstants.NETWORK_MODE_TDSCDMA_CDMA_EVDO_GSM_WCDMA;
311             case (LTE | RAF_TD_SCDMA | CDMA | EVDO | GSM | WCDMA):
312                 return RILConstants.NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA;
313             case (NR):
314                 return RILConstants.NETWORK_MODE_NR_ONLY;
315             case (NR | LTE):
316                 return RILConstants.NETWORK_MODE_NR_LTE;
317             case (NR | LTE | CDMA | EVDO):
318                 return RILConstants.NETWORK_MODE_NR_LTE_CDMA_EVDO;
319             case (NR | LTE | GSM | WCDMA):
320                 return RILConstants.NETWORK_MODE_NR_LTE_GSM_WCDMA;
321             case (NR | LTE | CDMA | EVDO | GSM | WCDMA):
322                 return RILConstants.NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA;
323             case (NR | LTE | WCDMA):
324                 return RILConstants.NETWORK_MODE_NR_LTE_WCDMA;
325             case (NR | LTE | RAF_TD_SCDMA):
326                 return RILConstants.NETWORK_MODE_NR_LTE_TDSCDMA;
327             case (NR | LTE | RAF_TD_SCDMA | GSM):
328                 return RILConstants.NETWORK_MODE_NR_LTE_TDSCDMA_GSM;
329             case (NR | LTE | RAF_TD_SCDMA | WCDMA):
330                 return RILConstants.NETWORK_MODE_NR_LTE_TDSCDMA_WCDMA;
331             case (NR | LTE | RAF_TD_SCDMA | GSM | WCDMA):
332                 return RILConstants.NETWORK_MODE_NR_LTE_TDSCDMA_GSM_WCDMA;
333             case (NR | LTE | RAF_TD_SCDMA | CDMA | EVDO | GSM | WCDMA):
334                 return RILConstants.NETWORK_MODE_NR_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA;
335             default:
336                 return RILConstants.PREFERRED_NETWORK_MODE;
337         }
338     }
339 
singleRafTypeFromString(String rafString)340     public static int singleRafTypeFromString(String rafString) {
341         switch (rafString) {
342             case "GPRS":    return RAF_GPRS;
343             case "EDGE":    return RAF_EDGE;
344             case "UMTS":    return RAF_UMTS;
345             case "IS95A":   return RAF_IS95A;
346             case "IS95B":   return RAF_IS95B;
347             case "1XRTT":   return RAF_1xRTT;
348             case "EVDO_0":  return RAF_EVDO_0;
349             case "EVDO_A":  return RAF_EVDO_A;
350             case "HSDPA":   return RAF_HSDPA;
351             case "HSUPA":   return RAF_HSUPA;
352             case "HSPA":    return RAF_HSPA;
353             case "EVDO_B":  return RAF_EVDO_B;
354             case "EHRPD":   return RAF_EHRPD;
355             case "LTE":     return RAF_LTE;
356             case "HSPAP":   return RAF_HSPAP;
357             case "GSM":     return RAF_GSM;
358             case "TD_SCDMA":return RAF_TD_SCDMA;
359             case "HS":      return HS;
360             case "CDMA":    return CDMA;
361             case "EVDO":    return EVDO;
362             case "WCDMA":   return WCDMA;
363             case "LTE_CA":  return RAF_LTE_CA;
364             case "NR":      return RAF_NR;
365             default:        return RAF_UNKNOWN;
366         }
367     }
368 
rafTypeFromString(String rafList)369     public static int rafTypeFromString(String rafList) {
370         rafList = rafList.toUpperCase();
371         String[] rafs = rafList.split("\\|");
372         int result = 0;
373         for(String raf : rafs) {
374             int rafType = singleRafTypeFromString(raf.trim());
375             if (rafType == RAF_UNKNOWN) return rafType;
376             result |= rafType;
377         }
378         return result;
379     }
380 
381     /**
382      * Compare two sets of network types to see which is more capable.
383      *
384      * This algorithm first tries to see see if a set has a strict superset of RAT support for
385      * each generation, from newest to oldest; if that results in a tie, then it returns the set
386      * that supports the most RAT types.
387      */
compare(long networkTypeBitmaskL, long networkTypeBitmaskR)388     public static int compare(long networkTypeBitmaskL, long networkTypeBitmaskR) {
389         final long[] prioritizedNetworkClassBitmasks = new long[] {
390             TelephonyManager.NETWORK_CLASS_BITMASK_5G,
391             TelephonyManager.NETWORK_CLASS_BITMASK_4G,
392             TelephonyManager.NETWORK_CLASS_BITMASK_3G,
393             TelephonyManager.NETWORK_CLASS_BITMASK_2G,
394         };
395 
396         long lhsUnique = networkTypeBitmaskL & ~networkTypeBitmaskR;
397         long rhsUnique = networkTypeBitmaskR & ~networkTypeBitmaskL;
398 
399         // See if one has a strict super-set of capabilities, generation by generation.
400         for (long classBitmask : prioritizedNetworkClassBitmasks) {
401             int result = 0;
402             if ((lhsUnique & classBitmask) != 0) ++result;
403             if ((rhsUnique & classBitmask) != 0) --result;
404             if (result != 0) return result;
405         }
406 
407         // Without a clear winner, return the one that supports the most types.
408         return Long.bitCount(networkTypeBitmaskL) - Long.bitCount(networkTypeBitmaskR);
409     }
410 }
411