OSDN Git Service

Merge "cherrypick from master: Change-Id: I169749dc594ca1d79a802db4c53ec330924fdd2c...
[android-x86/frameworks-base.git] / telephony / java / com / android / internal / telephony / ApnSetting.java
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
17 package com.android.internal.telephony;
18
19 /**
20  * This class represents a apn setting for create PDP link
21  */
22 public class ApnSetting {
23
24     static final String V2_FORMAT_REGEX = "^\\[ApnSettingV2\\]\\s*";
25
26     public String carrier;
27     public String apn;
28     public String proxy;
29     public String port;
30     public String mmsc;
31     public String mmsProxy;
32     public String mmsPort;
33     public String user;
34     public String password;
35     public int authType;
36     public String[] types;
37     public int id;
38     public String numeric;
39     public String protocol;
40     public String roamingProtocol;
41
42     public ApnSetting(int id, String numeric, String carrier, String apn,
43             String proxy, String port,
44             String mmsc, String mmsProxy, String mmsPort,
45             String user, String password, int authType, String[] types,
46             String protocol, String roamingProtocol) {
47         this.id = id;
48         this.numeric = numeric;
49         this.carrier = carrier;
50         this.apn = apn;
51         this.proxy = proxy;
52         this.port = port;
53         this.mmsc = mmsc;
54         this.mmsProxy = mmsProxy;
55         this.mmsPort = mmsPort;
56         this.user = user;
57         this.password = password;
58         this.authType = authType;
59         this.types = types;
60         this.protocol = protocol;
61         this.roamingProtocol = roamingProtocol;
62     }
63
64     /**
65      * Creates an ApnSetting object from a string.
66      *
67      * @param data the string to read.
68      *
69      * The string must be in one of two formats (newlines added for clarity,
70      * spaces are optional):
71      *
72      * v1 format:
73      *   <carrier>, <apn>, <proxy>, <port>, <mmsc>, <mmsproxy>,
74      *   <mmsport>, <user>, <password>, <authtype>, <mcc>,<mnc>,
75      *   <type>[, <type>...]
76      *
77      * v2 format:
78      *   [ApnSettingV2] <carrier>, <apn>, <proxy>, <port>, <mmsc>, <mmsproxy>,
79      *   <mmsport>, <user>, <password, <authtype>, <mcc>, <mnc>,
80      *   <type>[| <type>...], <protocol>, <roaming_protocol>
81      *
82      * Note that the strings generated by toString() do not contain the username
83      * and password and thus cannot be read by this method.
84      *
85      * @see ApnSettingTest
86      */
87     public static ApnSetting fromString(String data) {
88         if (data == null) return null;
89
90         int version;
91         // matches() operates on the whole string, so append .* to the regex.
92         if (data.matches(V2_FORMAT_REGEX + ".*")) {
93             version = 2;
94             data = data.replaceFirst(V2_FORMAT_REGEX, "");
95         } else {
96             version = 1;
97         }
98
99         String[] a = data.split("\\s*,\\s*");
100         if (a.length < 14) {
101             return null;
102         }
103
104         int authType;
105         try {
106             authType = Integer.parseInt(a[12]);
107         } catch (Exception e) {
108             authType = 0;
109         }
110
111         String[] typeArray;
112         String protocol, roamingProtocol;
113         if (version == 1) {
114             typeArray = new String[a.length - 13];
115             System.arraycopy(a, 13, typeArray, 0, a.length - 13);
116             protocol = RILConstants.SETUP_DATA_PROTOCOL_IP;
117             roamingProtocol = RILConstants.SETUP_DATA_PROTOCOL_IP;
118         } else {
119             if (a.length < 16) {
120                 return null;
121             }
122             typeArray = a[13].split("\\s*\\|\\s*");
123             protocol = a[14];
124             roamingProtocol = a[15];
125         }
126
127         return new ApnSetting(-1,a[10]+a[11],a[0],a[1],a[2],a[3],a[7],a[8],
128                 a[9],a[4],a[5],authType,typeArray,protocol,roamingProtocol);
129     }
130
131     public String toString() {
132         StringBuilder sb = new StringBuilder();
133         sb.append("[ApnSettingV2] ")
134         .append(carrier)
135         .append(", ").append(id)
136         .append(", ").append(numeric)
137         .append(", ").append(apn)
138         .append(", ").append(proxy)
139         .append(", ").append(mmsc)
140         .append(", ").append(mmsProxy)
141         .append(", ").append(mmsPort)
142         .append(", ").append(port)
143         .append(", ").append(authType).append(", ");
144         for (int i = 0; i < types.length; i++) {
145             sb.append(types[i]);
146             if (i < types.length - 1) {
147                 sb.append(" | ");
148             }
149         }
150         sb.append(", ").append(protocol);
151         sb.append(", ").append(roamingProtocol);
152         return sb.toString();
153     }
154
155     public boolean canHandleType(String type) {
156         for (String t : types) {
157             // DEFAULT handles all, and HIPRI is handled by DEFAULT
158             if (t.equals(type) || t.equals(Phone.APN_TYPE_ALL) ||
159                     (t.equals(Phone.APN_TYPE_DEFAULT) &&
160                     type.equals(Phone.APN_TYPE_HIPRI))) {
161                 return true;
162             }
163         }
164         return false;
165     }
166 }