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