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