ApnSetting.java revision cbaa45bbf2cab852b6c9c3a887e9f803d4e857ea
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    public static ApnSetting fromString(String data) {
103        if (data == null) return null;
104
105        int version;
106        // matches() operates on the whole string, so append .* to the regex.
107        if (data.matches(V2_FORMAT_REGEX + ".*")) {
108            version = 2;
109            data = data.replaceFirst(V2_FORMAT_REGEX, "");
110        } else {
111            version = 1;
112        }
113
114        String[] a = data.split("\\s*,\\s*");
115        if (a.length < 14) {
116            return null;
117        }
118
119        int authType;
120        try {
121            authType = Integer.parseInt(a[12]);
122        } catch (Exception e) {
123            authType = 0;
124        }
125
126        String[] typeArray;
127        String protocol, roamingProtocol;
128        boolean carrierEnabled;
129        int bearer;
130        if (version == 1) {
131            typeArray = new String[a.length - 13];
132            System.arraycopy(a, 13, typeArray, 0, a.length - 13);
133            protocol = RILConstants.SETUP_DATA_PROTOCOL_IP;
134            roamingProtocol = RILConstants.SETUP_DATA_PROTOCOL_IP;
135            carrierEnabled = true;
136            bearer = 0;
137        } else {
138            if (a.length < 18) {
139                return null;
140            }
141            typeArray = a[13].split("\\s*\\|\\s*");
142            protocol = a[14];
143            roamingProtocol = a[15];
144            try {
145                carrierEnabled = Boolean.parseBoolean(a[16]);
146            } catch (Exception e) {
147                carrierEnabled = true;
148            }
149            bearer = Integer.parseInt(a[17]);
150        }
151
152        return new ApnSetting(-1,a[10]+a[11],a[0],a[1],a[2],a[3],a[7],a[8],
153                a[9],a[4],a[5],authType,typeArray,protocol,roamingProtocol,carrierEnabled,bearer);
154    }
155
156    @Override
157    public String toString() {
158        StringBuilder sb = new StringBuilder();
159        sb.append("[ApnSettingV2] ")
160        .append(carrier)
161        .append(", ").append(id)
162        .append(", ").append(numeric)
163        .append(", ").append(apn)
164        .append(", ").append(proxy)
165        .append(", ").append(mmsc)
166        .append(", ").append(mmsProxy)
167        .append(", ").append(mmsPort)
168        .append(", ").append(port)
169        .append(", ").append(authType).append(", ");
170        for (int i = 0; i < types.length; i++) {
171            sb.append(types[i]);
172            if (i < types.length - 1) {
173                sb.append(" | ");
174            }
175        }
176        sb.append(", ").append(protocol);
177        sb.append(", ").append(roamingProtocol);
178        sb.append(", ").append(carrierEnabled);
179        sb.append(", ").append(bearer);
180        return sb.toString();
181    }
182
183    public boolean canHandleType(String type) {
184        for (String t : types) {
185            // DEFAULT handles all, and HIPRI is handled by DEFAULT
186            if (t.equalsIgnoreCase(type) ||
187                    t.equalsIgnoreCase(PhoneConstants.APN_TYPE_ALL) ||
188                    (t.equalsIgnoreCase(PhoneConstants.APN_TYPE_DEFAULT) &&
189                    type.equalsIgnoreCase(PhoneConstants.APN_TYPE_HIPRI))) {
190                return true;
191            }
192        }
193        return false;
194    }
195
196    // TODO - if we have this function we should also have hashCode.
197    // Also should handle changes in type order and perhaps case-insensitivity
198    @Override
199    public boolean equals(Object o) {
200        if (o instanceof ApnSetting == false) return false;
201        return (this.toString().equals(o.toString()));
202    }
203}
204