ApnSetting.java revision 3ae69aa0ef2e02f140a2229e6a31cfd291528295
1/*
2 * Copyright (C) 2006 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 com.android.internal.telephony.dataconnection;
18
19import com.android.internal.telephony.PhoneConstants;
20import com.android.internal.telephony.RILConstants;
21
22import java.util.Locale;
23
24/**
25 * This class represents a apn setting for create PDP link
26 */
27public class ApnSetting {
28
29    static final String V2_FORMAT_REGEX = "^\\[ApnSettingV2\\]\\s*";
30
31    public final String carrier;
32    public final String apn;
33    public final String proxy;
34    public final String port;
35    public final String mmsc;
36    public final String mmsProxy;
37    public final String mmsPort;
38    public final String user;
39    public final String password;
40    public final int authType;
41    public final String[] types;
42    public final int id;
43    public final String numeric;
44    public final String protocol;
45    public final String roamingProtocol;
46    /**
47      * Current status of APN
48      * true : enabled APN, false : disabled APN.
49      */
50    public final boolean carrierEnabled;
51    /**
52      * Radio Access Technology info
53      * To check what values can hold, refer to ServiceState.java.
54      * This should be spread to other technologies,
55      * but currently only used for LTE(14) and EHRPD(13).
56      */
57    public final int bearer;
58
59    public ApnSetting(int id, String numeric, String carrier, String apn,
60            String proxy, String port,
61            String mmsc, String mmsProxy, String mmsPort,
62            String user, String password, int authType, String[] types,
63            String protocol, String roamingProtocol, boolean carrierEnabled, int bearer) {
64        this.id = id;
65        this.numeric = numeric;
66        this.carrier = carrier;
67        this.apn = apn;
68        this.proxy = proxy;
69        this.port = port;
70        this.mmsc = mmsc;
71        this.mmsProxy = mmsProxy;
72        this.mmsPort = mmsPort;
73        this.user = user;
74        this.password = password;
75        this.authType = authType;
76        this.types = new String[types.length];
77        for (int i = 0; i < types.length; i++) {
78            this.types[i] = types[i].toLowerCase(Locale.ROOT);
79        }
80        this.protocol = protocol;
81        this.roamingProtocol = roamingProtocol;
82        this.carrierEnabled = carrierEnabled;
83        this.bearer = bearer;
84    }
85
86    /**
87     * Creates an ApnSetting object from a string.
88     *
89     * @param data the string to read.
90     *
91     * The string must be in one of two formats (newlines added for clarity,
92     * spaces are optional):
93     *
94     * v1 format:
95     *   <carrier>, <apn>, <proxy>, <port>, <mmsc>, <mmsproxy>,
96     *   <mmsport>, <user>, <password>, <authtype>, <mcc>,<mnc>,
97     *   <type>[, <type>...]
98     *
99     * v2 format:
100     *   [ApnSettingV2] <carrier>, <apn>, <proxy>, <port>, <mmsc>, <mmsproxy>,
101     *   <mmsport>, <user>, <password>, <authtype>, <mcc>, <mnc>,
102     *   <type>[| <type>...], <protocol>, <roaming_protocol>, <carrierEnabled>, <bearer>
103     *
104     * Note that the strings generated by toString() do not contain the username
105     * and password and thus cannot be read by this method.
106     */
107    public static ApnSetting fromString(String data) {
108        if (data == null) return null;
109
110        int version;
111        // matches() operates on the whole string, so append .* to the regex.
112        if (data.matches(V2_FORMAT_REGEX + ".*")) {
113            version = 2;
114            data = data.replaceFirst(V2_FORMAT_REGEX, "");
115        } else {
116            version = 1;
117        }
118
119        String[] a = data.split("\\s*,\\s*");
120        if (a.length < 14) {
121            return null;
122        }
123
124        int authType;
125        try {
126            authType = Integer.parseInt(a[12]);
127        } catch (Exception e) {
128            authType = 0;
129        }
130
131        String[] typeArray;
132        String protocol, roamingProtocol;
133        boolean carrierEnabled;
134        int bearer;
135        if (version == 1) {
136            typeArray = new String[a.length - 13];
137            System.arraycopy(a, 13, typeArray, 0, a.length - 13);
138            protocol = RILConstants.SETUP_DATA_PROTOCOL_IP;
139            roamingProtocol = RILConstants.SETUP_DATA_PROTOCOL_IP;
140            carrierEnabled = true;
141            bearer = 0;
142        } else {
143            if (a.length < 18) {
144                return null;
145            }
146            typeArray = a[13].split("\\s*\\|\\s*");
147            protocol = a[14];
148            roamingProtocol = a[15];
149            try {
150                carrierEnabled = Boolean.parseBoolean(a[16]);
151            } catch (Exception e) {
152                carrierEnabled = true;
153            }
154            bearer = Integer.parseInt(a[17]);
155        }
156
157        return new ApnSetting(-1,a[10]+a[11],a[0],a[1],a[2],a[3],a[7],a[8],
158                a[9],a[4],a[5],authType,typeArray,protocol,roamingProtocol,carrierEnabled,bearer);
159    }
160
161    @Override
162    public String toString() {
163        StringBuilder sb = new StringBuilder();
164        sb.append("[ApnSettingV2] ")
165        .append(carrier)
166        .append(", ").append(id)
167        .append(", ").append(numeric)
168        .append(", ").append(apn)
169        .append(", ").append(proxy)
170        .append(", ").append(mmsc)
171        .append(", ").append(mmsProxy)
172        .append(", ").append(mmsPort)
173        .append(", ").append(port)
174        .append(", ").append(authType).append(", ");
175        for (int i = 0; i < types.length; i++) {
176            sb.append(types[i]);
177            if (i < types.length - 1) {
178                sb.append(" | ");
179            }
180        }
181        sb.append(", ").append(protocol);
182        sb.append(", ").append(roamingProtocol);
183        sb.append(", ").append(carrierEnabled);
184        sb.append(", ").append(bearer);
185        return sb.toString();
186    }
187
188    public boolean canHandleType(String type) {
189        for (String t : types) {
190            // DEFAULT handles all, and HIPRI is handled by DEFAULT
191            if (t.equalsIgnoreCase(type) ||
192                    t.equalsIgnoreCase(PhoneConstants.APN_TYPE_ALL) ||
193                    (t.equalsIgnoreCase(PhoneConstants.APN_TYPE_DEFAULT) &&
194                    type.equalsIgnoreCase(PhoneConstants.APN_TYPE_HIPRI))) {
195                return true;
196            }
197        }
198        return false;
199    }
200
201    // TODO - if we have this function we should also have hashCode.
202    // Also should handle changes in type order and perhaps case-insensitivity
203    @Override
204    public boolean equals(Object o) {
205        if (o instanceof ApnSetting == false) return false;
206        return (toString().equals(o.toString()));
207    }
208}
209