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    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     */
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     */
82    public int getPhoneId() {
83        return mPhoneId;
84    }
85
86    /**
87     * get radio access family.
88     *
89     * @return radio access family
90     */
91    public int getRadioAccessFamily() {
92        return mRadioAccessFamily;
93    }
94
95    @Override
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
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
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
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            case RILConstants.NETWORK_MODE_TDSCDMA_ONLY:
189                raf = RAF_TD_SCDMA;
190                break;
191            case RILConstants.NETWORK_MODE_TDSCDMA_WCDMA:
192                raf = RAF_TD_SCDMA | WCDMA;
193                break;
194            case RILConstants.NETWORK_MODE_LTE_TDSCDMA:
195                raf = RAF_LTE | RAF_TD_SCDMA;
196                break;
197            case RILConstants.NETWORK_MODE_TDSCDMA_GSM:
198                raf = RAF_TD_SCDMA | GSM;
199                break;
200            case RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM:
201                raf = RAF_LTE | RAF_TD_SCDMA | GSM;
202                break;
203            case RILConstants.NETWORK_MODE_TDSCDMA_GSM_WCDMA:
204                raf = RAF_TD_SCDMA | GSM | WCDMA;
205                break;
206            case RILConstants.NETWORK_MODE_LTE_TDSCDMA_WCDMA:
207                raf = RAF_LTE | RAF_TD_SCDMA | WCDMA;
208                break;
209            case RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA:
210                raf = RAF_LTE | RAF_TD_SCDMA | GSM | WCDMA;
211                break;
212            case RILConstants.NETWORK_MODE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
213                raf = RAF_TD_SCDMA | CDMA | EVDO | GSM | WCDMA;
214                break;
215            case RILConstants.NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
216                raf = RAF_LTE | RAF_TD_SCDMA | CDMA | EVDO | GSM | WCDMA;
217                break;
218            default:
219                raf = RAF_UNKNOWN;
220                break;
221        }
222
223        return raf;
224    }
225
226    /**
227     * if the raf includes ANY bit set for a group
228     * adjust it to contain ALL the bits for that group
229     */
230    private static int getAdjustedRaf(int raf) {
231        raf = ((GSM & raf) > 0) ? (GSM | raf) : raf;
232        raf = ((WCDMA & raf) > 0) ? (WCDMA | raf) : raf;
233        raf = ((CDMA & raf) > 0) ? (CDMA | raf) : raf;
234        raf = ((EVDO & raf) > 0) ? (EVDO | raf) : raf;
235
236        return raf;
237    }
238
239    public static int getNetworkTypeFromRaf(int raf) {
240        int type;
241
242        raf = getAdjustedRaf(raf);
243
244        switch (raf) {
245            case (GSM | WCDMA):
246                type = RILConstants.NETWORK_MODE_WCDMA_PREF;
247                break;
248            case GSM:
249                type = RILConstants.NETWORK_MODE_GSM_ONLY;
250                break;
251            case WCDMA:
252                type = RILConstants.NETWORK_MODE_WCDMA_ONLY;
253                break;
254            case (CDMA | EVDO):
255                type = RILConstants.NETWORK_MODE_CDMA;
256                break;
257            case (RAF_LTE | CDMA | EVDO):
258                type = RILConstants.NETWORK_MODE_LTE_CDMA_EVDO;
259                break;
260            case (RAF_LTE | GSM | WCDMA):
261                type = RILConstants.NETWORK_MODE_LTE_GSM_WCDMA;
262                break;
263            case (RAF_LTE | CDMA | EVDO | GSM | WCDMA):
264                type = RILConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA;
265                break;
266            case RAF_LTE:
267                type = RILConstants.NETWORK_MODE_LTE_ONLY;
268                break;
269            case (RAF_LTE | WCDMA):
270                type = RILConstants.NETWORK_MODE_LTE_WCDMA;
271                break;
272            case CDMA:
273                type = RILConstants.NETWORK_MODE_CDMA_NO_EVDO;
274                break;
275            case EVDO:
276                type = RILConstants.NETWORK_MODE_EVDO_NO_CDMA;
277                break;
278            case (GSM | WCDMA | CDMA | EVDO):
279                type = RILConstants.NETWORK_MODE_GLOBAL;
280                break;
281            case RAF_TD_SCDMA:
282                type = RILConstants.NETWORK_MODE_TDSCDMA_ONLY;
283                break;
284            case (RAF_TD_SCDMA | WCDMA):
285                type = RILConstants.NETWORK_MODE_TDSCDMA_WCDMA;
286                break;
287            case (RAF_LTE | RAF_TD_SCDMA):
288                type = RILConstants.NETWORK_MODE_LTE_TDSCDMA;
289                break;
290            case (RAF_TD_SCDMA | GSM):
291                type = RILConstants.NETWORK_MODE_TDSCDMA_GSM;
292                break;
293            case (RAF_LTE | RAF_TD_SCDMA | GSM):
294                type = RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM;
295                break;
296            case (RAF_TD_SCDMA | GSM | WCDMA):
297                type = RILConstants.NETWORK_MODE_TDSCDMA_GSM_WCDMA;
298                break;
299            case (RAF_LTE | RAF_TD_SCDMA | WCDMA):
300                type = RILConstants.NETWORK_MODE_LTE_TDSCDMA_WCDMA;
301                break;
302            case (RAF_LTE | RAF_TD_SCDMA | GSM | WCDMA):
303                type = RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA;
304                break;
305            case (RAF_TD_SCDMA | CDMA | EVDO | GSM | WCDMA):
306                type = RILConstants.NETWORK_MODE_TDSCDMA_CDMA_EVDO_GSM_WCDMA;
307                break;
308            case (RAF_LTE | RAF_TD_SCDMA | RAF_LTE | CDMA | EVDO | GSM | WCDMA):
309                type = RILConstants.NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA;
310                break;
311            default:
312                type = RILConstants.PREFERRED_NETWORK_MODE ;
313                break;
314        }
315
316        return type;
317    }
318
319    public static int singleRafTypeFromString(String rafString) {
320        switch (rafString) {
321            case "GPRS":    return RAF_GPRS;
322            case "EDGE":    return RAF_EDGE;
323            case "UMTS":    return RAF_UMTS;
324            case "IS95A":   return RAF_IS95A;
325            case "IS95B":   return RAF_IS95B;
326            case "1XRTT":   return RAF_1xRTT;
327            case "EVDO_0":  return RAF_EVDO_0;
328            case "EVDO_A":  return RAF_EVDO_A;
329            case "HSDPA":   return RAF_HSDPA;
330            case "HSUPA":   return RAF_HSUPA;
331            case "HSPA":    return RAF_HSPA;
332            case "EVDO_B":  return RAF_EVDO_B;
333            case "EHRPD":   return RAF_EHRPD;
334            case "LTE":     return RAF_LTE;
335            case "HSPAP":   return RAF_HSPAP;
336            case "GSM":     return RAF_GSM;
337            case "TD_SCDMA":return RAF_TD_SCDMA;
338            case "HS":      return HS;
339            case "CDMA":    return CDMA;
340            case "EVDO":    return EVDO;
341            case "WCDMA":   return WCDMA;
342            default:        return RAF_UNKNOWN;
343        }
344    }
345
346    public static int rafTypeFromString(String rafList) {
347        rafList = rafList.toUpperCase();
348        String[] rafs = rafList.split("\\|");
349        int result = 0;
350        for(String raf : rafs) {
351            int rafType = singleRafTypeFromString(raf.trim());
352            if (rafType == RAF_UNKNOWN) return rafType;
353            result |= rafType;
354        }
355        return result;
356    }
357}
358