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