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.os.Parcel;
20 import android.os.Parcelable;
21 
22 import com.android.internal.telephony.RILConstants;
23 
24 /**
25  * Object to indicate the phone radio type and access technology.
26  *
27  * @hide
28  */
29 public class RadioAccessFamily implements Parcelable {
30 
31     // Radio Access Family
32     public static final int RAF_UNKNOWN = (1 <<  ServiceState.RIL_RADIO_TECHNOLOGY_UNKNOWN);
33     public static final int RAF_GPRS = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_GPRS);
34     public static final int RAF_EDGE = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_EDGE);
35     public static final int RAF_UMTS = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_UMTS);
36     public static final int RAF_IS95A = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_IS95A);
37     public static final int RAF_IS95B = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_IS95B);
38     public static final int RAF_1xRTT = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_1xRTT);
39     public static final int RAF_EVDO_0 = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_EVDO_0);
40     public static final int RAF_EVDO_A = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_EVDO_A);
41     public static final int RAF_HSDPA = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_HSDPA);
42     public static final int RAF_HSUPA = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_HSUPA);
43     public static final int RAF_HSPA = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_HSPA);
44     public static final int RAF_EVDO_B = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_EVDO_B);
45     public static final int RAF_EHRPD = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_EHRPD);
46     public static final int RAF_LTE = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_LTE);
47     public static final int RAF_HSPAP = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_HSPAP);
48     public static final int RAF_GSM = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_GSM);
49     public static final int RAF_TD_SCDMA = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_TD_SCDMA);
50 
51     // Grouping of RAFs
52     private static final int GSM = RAF_GSM | RAF_GPRS | RAF_EDGE;
53     private static final int HS = RAF_HSUPA | RAF_HSDPA | RAF_HSPA | RAF_HSPAP;
54     private static final int CDMA = RAF_IS95A | RAF_IS95B | RAF_1xRTT;
55     private static final int EVDO = RAF_EVDO_0 | RAF_EVDO_A | RAF_EVDO_B | RAF_EHRPD;
56     private static final int WCDMA = HS | RAF_UMTS;
57 
58     /* Phone ID of phone */
59     private int mPhoneId;
60 
61     /* Radio Access Family */
62     private int mRadioAccessFamily;
63 
64     /**
65      * Constructor.
66      *
67      * @param phoneId the phone ID
68      * @param radioAccessFamily the phone radio access family defined
69      *        in RadioAccessFamily. It's a bit mask value to represent
70      *        the support type.
71      */
RadioAccessFamily(int phoneId, int radioAccessFamily)72     public RadioAccessFamily(int phoneId, int radioAccessFamily) {
73         mPhoneId = phoneId;
74         mRadioAccessFamily = radioAccessFamily;
75     }
76 
77     /**
78      * Get phone ID.
79      *
80      * @return phone ID
81      */
getPhoneId()82     public int getPhoneId() {
83         return mPhoneId;
84     }
85 
86     /**
87      * get radio access family.
88      *
89      * @return radio access family
90      */
getRadioAccessFamily()91     public int getRadioAccessFamily() {
92         return mRadioAccessFamily;
93     }
94 
95     @Override
toString()96     public String toString() {
97         String ret = "{ mPhoneId = " + mPhoneId
98                 + ", mRadioAccessFamily = " + mRadioAccessFamily
99                 + "}";
100         return ret;
101     }
102 
103     /**
104      * Implement the Parcelable interface.
105      *
106      * @return describe content
107      */
108     @Override
describeContents()109     public int describeContents() {
110         return 0;
111     }
112 
113     /**
114      * Implement the Parcelable interface.
115      *
116      * @param outParcel The Parcel in which the object should be written.
117      * @param flags Additional flags about how the object should be written.
118      */
119     @Override
writeToParcel(Parcel outParcel, int flags)120     public void writeToParcel(Parcel outParcel, int flags) {
121         outParcel.writeInt(mPhoneId);
122         outParcel.writeInt(mRadioAccessFamily);
123     }
124 
125     /**
126      * Implement the Parcelable interface.
127      */
128     public static final Creator<RadioAccessFamily> CREATOR =
129             new Creator<RadioAccessFamily>() {
130 
131         @Override
132         public RadioAccessFamily createFromParcel(Parcel in) {
133             int phoneId = in.readInt();
134             int radioAccessFamily = in.readInt();
135 
136             return new RadioAccessFamily(phoneId, radioAccessFamily);
137         }
138 
139         @Override
140         public RadioAccessFamily[] newArray(int size) {
141             return new RadioAccessFamily[size];
142         }
143     };
144 
getRafFromNetworkType(int type)145     public static int getRafFromNetworkType(int type) {
146         int raf;
147 
148         switch (type) {
149             case RILConstants.NETWORK_MODE_WCDMA_PREF:
150                 raf = GSM | WCDMA;
151                 break;
152             case RILConstants.NETWORK_MODE_GSM_ONLY:
153                 raf = GSM;
154                 break;
155             case RILConstants.NETWORK_MODE_WCDMA_ONLY:
156                 raf = WCDMA;
157                 break;
158             case RILConstants.NETWORK_MODE_GSM_UMTS:
159                 raf = GSM | WCDMA;
160                 break;
161             case RILConstants.NETWORK_MODE_CDMA:
162                 raf = CDMA | EVDO;
163                 break;
164             case RILConstants.NETWORK_MODE_LTE_CDMA_EVDO:
165                 raf = RAF_LTE | CDMA | EVDO;
166                 break;
167             case RILConstants.NETWORK_MODE_LTE_GSM_WCDMA:
168                 raf = RAF_LTE | GSM | WCDMA;
169                 break;
170             case RILConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA:
171                 raf = RAF_LTE | CDMA | EVDO | GSM | WCDMA;
172                 break;
173             case RILConstants.NETWORK_MODE_LTE_ONLY:
174                 raf = RAF_LTE;
175                 break;
176             case RILConstants.NETWORK_MODE_LTE_WCDMA:
177                 raf = RAF_LTE | WCDMA;
178                 break;
179             case RILConstants.NETWORK_MODE_CDMA_NO_EVDO:
180                 raf = CDMA;
181                 break;
182             case RILConstants.NETWORK_MODE_EVDO_NO_CDMA:
183                 raf = EVDO;
184                 break;
185             case RILConstants.NETWORK_MODE_GLOBAL:
186                 raf = GSM | WCDMA | CDMA | EVDO;
187                 break;
188             default:
189                 raf = RAF_UNKNOWN;
190                 break;
191         }
192 
193         return raf;
194     }
195 
196     /**
197      * if the raf includes ANY bit set for a group
198      * adjust it to contain ALL the bits for that group
199      */
getAdjustedRaf(int raf)200     private static int getAdjustedRaf(int raf) {
201         raf = ((GSM & raf) > 0) ? (GSM | raf) : raf;
202         raf = ((WCDMA & raf) > 0) ? (WCDMA | raf) : raf;
203         raf = ((CDMA & raf) > 0) ? (CDMA | raf) : raf;
204         raf = ((EVDO & raf) > 0) ? (EVDO | raf) : raf;
205 
206         return raf;
207     }
208 
getNetworkTypeFromRaf(int raf)209     public static int getNetworkTypeFromRaf(int raf) {
210         int type;
211 
212         raf = getAdjustedRaf(raf);
213 
214         switch (raf) {
215             case (GSM | WCDMA):
216                 type = RILConstants.NETWORK_MODE_WCDMA_PREF;
217                 break;
218             case GSM:
219                 type = RILConstants.NETWORK_MODE_GSM_ONLY;
220                 break;
221             case WCDMA:
222                 type = RILConstants.NETWORK_MODE_WCDMA_ONLY;
223                 break;
224             case (CDMA | EVDO):
225                 type = RILConstants.NETWORK_MODE_CDMA;
226                 break;
227             case (RAF_LTE | CDMA | EVDO):
228                 type = RILConstants.NETWORK_MODE_LTE_CDMA_EVDO;
229                 break;
230             case (RAF_LTE | GSM | WCDMA):
231                 type = RILConstants.NETWORK_MODE_LTE_GSM_WCDMA;
232                 break;
233             case (RAF_LTE | CDMA | EVDO | GSM | WCDMA):
234                 type = RILConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA;
235                 break;
236             case RAF_LTE:
237                 type = RILConstants.NETWORK_MODE_LTE_ONLY;
238                 break;
239             case (RAF_LTE | WCDMA):
240                 type = RILConstants.NETWORK_MODE_LTE_WCDMA;
241                 break;
242             case CDMA:
243                 type = RILConstants.NETWORK_MODE_CDMA_NO_EVDO;
244                 break;
245             case EVDO:
246                 type = RILConstants.NETWORK_MODE_EVDO_NO_CDMA;
247                 break;
248             case (GSM | WCDMA | CDMA | EVDO):
249                 type = RILConstants.NETWORK_MODE_GLOBAL;
250                 break;
251             default:
252                 type = RILConstants.PREFERRED_NETWORK_MODE ;
253                 break;
254         }
255 
256         return type;
257     }
258 
singleRafTypeFromString(String rafString)259     public static int singleRafTypeFromString(String rafString) {
260         switch (rafString) {
261             case "GPRS":    return RAF_GPRS;
262             case "EDGE":    return RAF_EDGE;
263             case "UMTS":    return RAF_UMTS;
264             case "IS95A":   return RAF_IS95A;
265             case "IS95B":   return RAF_IS95B;
266             case "1XRTT":   return RAF_1xRTT;
267             case "EVDO_0":  return RAF_EVDO_0;
268             case "EVDO_A":  return RAF_EVDO_A;
269             case "HSDPA":   return RAF_HSDPA;
270             case "HSUPA":   return RAF_HSUPA;
271             case "HSPA":    return RAF_HSPA;
272             case "EVDO_B":  return RAF_EVDO_B;
273             case "EHRPD":   return RAF_EHRPD;
274             case "LTE":     return RAF_LTE;
275             case "HSPAP":   return RAF_HSPAP;
276             case "GSM":     return RAF_GSM;
277             case "TD_SCDMA":return RAF_TD_SCDMA;
278             case "HS":      return HS;
279             case "CDMA":    return CDMA;
280             case "EVDO":    return EVDO;
281             case "WCDMA":   return WCDMA;
282             default:        return RAF_UNKNOWN;
283         }
284     }
285 
rafTypeFromString(String rafList)286     public static int rafTypeFromString(String rafList) {
287         rafList = rafList.toUpperCase();
288         String[] rafs = rafList.split("\\|");
289         int result = 0;
290         for(String raf : rafs) {
291             int rafType = singleRafTypeFromString(raf.trim());
292             if (rafType == RAF_UNKNOWN) return rafType;
293             result |= rafType;
294         }
295         return result;
296     }
297 }
298