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.annotation.UnsupportedAppUsage;
20 import android.hardware.radio.V1_0.RadioTechnology;
21 import android.hardware.radio.V1_4.CellInfo.Info;
22 import android.os.Build;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 import android.telephony.TelephonyManager.PrefNetworkMode;
26 
27 import com.android.internal.telephony.RILConstants;
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
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
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
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
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     /**
266      * Returns the highest capability of the RadioAccessFamily (4G > 3G > 2G).
267      * @param raf The RadioAccessFamily that we wish to filter
268      * @return The highest radio capability
269      */
getHighestRafCapability(int raf)270     public static int getHighestRafCapability(int raf) {
271         if ((LTE & raf) > 0) {
272             return TelephonyManager.NETWORK_CLASS_4_G;
273         }
274         if ((EVDO|HS|WCDMA & raf) > 0) {
275             return TelephonyManager.NETWORK_CLASS_3_G;
276         }
277         if((GSM|CDMA & raf) > 0) {
278             return TelephonyManager.NETWORK_CLASS_2_G;
279         }
280         return TelephonyManager.NETWORK_CLASS_UNKNOWN;
281     }
282 
283     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
284     @PrefNetworkMode
getNetworkTypeFromRaf(int raf)285     public static int getNetworkTypeFromRaf(int raf) {
286         raf = getAdjustedRaf(raf);
287 
288         switch (raf) {
289             case (GSM | WCDMA):
290                 return RILConstants.NETWORK_MODE_WCDMA_PREF;
291             case GSM:
292                 return RILConstants.NETWORK_MODE_GSM_ONLY;
293             case WCDMA:
294                 return RILConstants.NETWORK_MODE_WCDMA_ONLY;
295             case (CDMA | EVDO):
296                 return RILConstants.NETWORK_MODE_CDMA;
297             case (LTE | CDMA | EVDO):
298                 return RILConstants.NETWORK_MODE_LTE_CDMA_EVDO;
299             case (LTE | GSM | WCDMA):
300                 return RILConstants.NETWORK_MODE_LTE_GSM_WCDMA;
301             case (LTE | CDMA | EVDO | GSM | WCDMA):
302                 return RILConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA;
303             case LTE:
304                 return RILConstants.NETWORK_MODE_LTE_ONLY;
305             case (LTE | WCDMA):
306                 return RILConstants.NETWORK_MODE_LTE_WCDMA;
307             case CDMA:
308                 return RILConstants.NETWORK_MODE_CDMA_NO_EVDO;
309             case EVDO:
310                 return RILConstants.NETWORK_MODE_EVDO_NO_CDMA;
311             case (GSM | WCDMA | CDMA | EVDO):
312                 return RILConstants.NETWORK_MODE_GLOBAL;
313             case RAF_TD_SCDMA:
314                 return RILConstants.NETWORK_MODE_TDSCDMA_ONLY;
315             case (RAF_TD_SCDMA | WCDMA):
316                 return RILConstants.NETWORK_MODE_TDSCDMA_WCDMA;
317             case (LTE | RAF_TD_SCDMA):
318                 return RILConstants.NETWORK_MODE_LTE_TDSCDMA;
319             case (RAF_TD_SCDMA | GSM):
320                 return RILConstants.NETWORK_MODE_TDSCDMA_GSM;
321             case (LTE | RAF_TD_SCDMA | GSM):
322                 return RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM;
323             case (RAF_TD_SCDMA | GSM | WCDMA):
324                 return RILConstants.NETWORK_MODE_TDSCDMA_GSM_WCDMA;
325             case (LTE | RAF_TD_SCDMA | WCDMA):
326                 return RILConstants.NETWORK_MODE_LTE_TDSCDMA_WCDMA;
327             case (LTE | RAF_TD_SCDMA | GSM | WCDMA):
328                 return RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA;
329             case (RAF_TD_SCDMA | CDMA | EVDO | GSM | WCDMA):
330                 return RILConstants.NETWORK_MODE_TDSCDMA_CDMA_EVDO_GSM_WCDMA;
331             case (LTE | RAF_TD_SCDMA | CDMA | EVDO | GSM | WCDMA):
332                 return RILConstants.NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA;
333             case (NR):
334                 return RILConstants.NETWORK_MODE_NR_ONLY;
335             case (NR | LTE):
336                 return RILConstants.NETWORK_MODE_NR_LTE;
337             case (NR | LTE | CDMA | EVDO):
338                 return RILConstants.NETWORK_MODE_NR_LTE_CDMA_EVDO;
339             case (NR | LTE | GSM | WCDMA):
340                 return RILConstants.NETWORK_MODE_NR_LTE_GSM_WCDMA;
341             case (NR | LTE | CDMA | EVDO | GSM | WCDMA):
342                 return RILConstants.NETWORK_MODE_NR_LTE_CDMA_EVDO_GSM_WCDMA;
343             case (NR | LTE | WCDMA):
344                 return RILConstants.NETWORK_MODE_NR_LTE_WCDMA;
345             case (NR | LTE | RAF_TD_SCDMA):
346                 return RILConstants.NETWORK_MODE_NR_LTE_TDSCDMA;
347             case (NR | LTE | RAF_TD_SCDMA | GSM):
348                 return RILConstants.NETWORK_MODE_NR_LTE_TDSCDMA_GSM;
349             case (NR | LTE | RAF_TD_SCDMA | WCDMA):
350                 return RILConstants.NETWORK_MODE_NR_LTE_TDSCDMA_WCDMA;
351             case (NR | LTE | RAF_TD_SCDMA | GSM | WCDMA):
352                 return RILConstants.NETWORK_MODE_NR_LTE_TDSCDMA_GSM_WCDMA;
353             case (NR | LTE | RAF_TD_SCDMA | CDMA | EVDO | GSM | WCDMA):
354                 return RILConstants.NETWORK_MODE_NR_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA;
355             default:
356                 return RILConstants.PREFERRED_NETWORK_MODE;
357         }
358     }
359 
singleRafTypeFromString(String rafString)360     public static int singleRafTypeFromString(String rafString) {
361         switch (rafString) {
362             case "GPRS":    return RAF_GPRS;
363             case "EDGE":    return RAF_EDGE;
364             case "UMTS":    return RAF_UMTS;
365             case "IS95A":   return RAF_IS95A;
366             case "IS95B":   return RAF_IS95B;
367             case "1XRTT":   return RAF_1xRTT;
368             case "EVDO_0":  return RAF_EVDO_0;
369             case "EVDO_A":  return RAF_EVDO_A;
370             case "HSDPA":   return RAF_HSDPA;
371             case "HSUPA":   return RAF_HSUPA;
372             case "HSPA":    return RAF_HSPA;
373             case "EVDO_B":  return RAF_EVDO_B;
374             case "EHRPD":   return RAF_EHRPD;
375             case "LTE":     return RAF_LTE;
376             case "HSPAP":   return RAF_HSPAP;
377             case "GSM":     return RAF_GSM;
378             case "TD_SCDMA":return RAF_TD_SCDMA;
379             case "HS":      return HS;
380             case "CDMA":    return CDMA;
381             case "EVDO":    return EVDO;
382             case "WCDMA":   return WCDMA;
383             case "LTE_CA":  return RAF_LTE_CA;
384             case "NR":      return RAF_NR;
385             default:        return RAF_UNKNOWN;
386         }
387     }
388 
rafTypeFromString(String rafList)389     public static int rafTypeFromString(String rafList) {
390         rafList = rafList.toUpperCase();
391         String[] rafs = rafList.split("\\|");
392         int result = 0;
393         for(String raf : rafs) {
394             int rafType = singleRafTypeFromString(raf.trim());
395             if (rafType == RAF_UNKNOWN) return rafType;
396             result |= rafType;
397         }
398         return result;
399     }
400 }
401