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