summaryrefslogtreecommitdiffstats
path: root/src/com/android/messaging/sms/ApnsXmlProcessor.java
blob: 976896c6a8a6aff5a13168c4cc3ccae076b2a30b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.messaging.sms;

import android.content.ContentValues;
import android.provider.Telephony;

import com.android.messaging.util.Assert;
import com.android.messaging.util.LogUtil;
import com.android.messaging.util.PhoneUtils;
import com.google.common.collect.Maps;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.Map;

/*
 * XML processor for the following files:
 * 1. res/xml/apns.xml
 * 2. res/xml/mms_config.xml (or related overlay files)
 */
class ApnsXmlProcessor {
    public interface ApnHandler {
        public void process(ContentValues apnValues);
    }

    public interface MmsConfigHandler {
        public void process(String mccMnc, String key, String value, String type);
    }

    private static final String TAG = LogUtil.BUGLE_TAG;

    private static final Map<String, String> APN_ATTRIBUTE_MAP = Maps.newHashMap();
    static {
        APN_ATTRIBUTE_MAP.put("mcc", Telephony.Carriers.MCC);
        APN_ATTRIBUTE_MAP.put("mnc", Telephony.Carriers.MNC);
        APN_ATTRIBUTE_MAP.put("carrier", Telephony.Carriers.NAME);
        APN_ATTRIBUTE_MAP.put("apn", Telephony.Carriers.APN);
        APN_ATTRIBUTE_MAP.put("mmsc", Telephony.Carriers.MMSC);
        APN_ATTRIBUTE_MAP.put("mmsproxy", Telephony.Carriers.MMSPROXY);
        APN_ATTRIBUTE_MAP.put("mmsport", Telephony.Carriers.MMSPORT);
        APN_ATTRIBUTE_MAP.put("type", Telephony.Carriers.TYPE);
        APN_ATTRIBUTE_MAP.put("user", Telephony.Carriers.USER);
        APN_ATTRIBUTE_MAP.put("password", Telephony.Carriers.PASSWORD);
        APN_ATTRIBUTE_MAP.put("authtype", Telephony.Carriers.AUTH_TYPE);
        APN_ATTRIBUTE_MAP.put("mvno_match_data", Telephony.Carriers.MVNO_MATCH_DATA);
        APN_ATTRIBUTE_MAP.put("mvno_type", Telephony.Carriers.MVNO_TYPE);
        APN_ATTRIBUTE_MAP.put("protocol", Telephony.Carriers.PROTOCOL);
        APN_ATTRIBUTE_MAP.put("bearer", Telephony.Carriers.BEARER);
        APN_ATTRIBUTE_MAP.put("server", Telephony.Carriers.SERVER);
        APN_ATTRIBUTE_MAP.put("roaming_protocol", Telephony.Carriers.ROAMING_PROTOCOL);
        APN_ATTRIBUTE_MAP.put("proxy", Telephony.Carriers.PROXY);
        APN_ATTRIBUTE_MAP.put("port", Telephony.Carriers.PORT);
        APN_ATTRIBUTE_MAP.put("carrier_enabled", Telephony.Carriers.CARRIER_ENABLED);
    }

    private static final String TAG_APNS = "apns";
    private static final String TAG_APN = "apn";
    private static final String TAG_MMS_CONFIG = "mms_config";

    // Handler to process one apn
    private ApnHandler mApnHandler;
    // Handler to process one mms_config key/value pair
    private MmsConfigHandler mMmsConfigHandler;

    private final StringBuilder mLogStringBuilder = new StringBuilder();

    private final XmlPullParser mInputParser;

    private ApnsXmlProcessor(XmlPullParser parser) {
        mInputParser = parser;
        mApnHandler = null;
        mMmsConfigHandler = null;
    }

    public static ApnsXmlProcessor get(XmlPullParser parser) {
        Assert.notNull(parser);
        return new ApnsXmlProcessor(parser);
    }

    public ApnsXmlProcessor setApnHandler(ApnHandler handler) {
        mApnHandler = handler;
        return this;
    }

    public ApnsXmlProcessor setMmsConfigHandler(MmsConfigHandler handler) {
        mMmsConfigHandler = handler;
        return this;
    }

    /**
     * Move XML parser forward to next event type or the end of doc
     *
     * @param eventType
     * @return The final event type we meet
     * @throws XmlPullParserException
     * @throws IOException
     */
    private int advanceToNextEvent(int eventType) throws XmlPullParserException, IOException {
        for (;;) {
            int nextEvent = mInputParser.next();
            if (nextEvent == eventType
                    || nextEvent == XmlPullParser.END_DOCUMENT) {
                return nextEvent;
            }
        }
    }

    public void process() {
        try {
            // Find the first element
            if (advanceToNextEvent(XmlPullParser.START_TAG) != XmlPullParser.START_TAG) {
                throw new XmlPullParserException("ApnsXmlProcessor: expecting start tag @"
                        + xmlParserDebugContext());
            }
            // A single ContentValues object for holding the parsing result of
            // an apn element
            final ContentValues values = new ContentValues();
            String tagName = mInputParser.getName();
            // Top level tag can be "apns" (apns.xml)
            // or "mms_config" (mms_config.xml)
            if (TAG_APNS.equals(tagName)) {
                // For "apns", there could be "apn" or both "apn" and "mms_config"
                for (;;) {
                    if (advanceToNextEvent(XmlPullParser.START_TAG) != XmlPullParser.START_TAG) {
                        break;
                    }
                    tagName = mInputParser.getName();
                    if (TAG_APN.equals(tagName)) {
                        processApn(values);
                    } else if (TAG_MMS_CONFIG.equals(tagName)) {
                        processMmsConfig();
                    }
                }
            } else if (TAG_MMS_CONFIG.equals(tagName)) {
                // mms_config.xml resource
                processMmsConfig();
            }
        } catch (IOException e) {
            LogUtil.e(TAG, "ApnsXmlProcessor: I/O failure " + e, e);
        } catch (XmlPullParserException e) {
            LogUtil.e(TAG, "ApnsXmlProcessor: parsing failure " + e, e);
        }
    }

    private Integer parseInt(String text, Integer defaultValue, String logHint) {
        Integer value = defaultValue;
        try {
            value = Integer.parseInt(text);
        } catch (Exception e) {
            LogUtil.e(TAG,
                    "Invalid value " + text + "for" + logHint + " @" + xmlParserDebugContext());
        }
        return value;
    }

    private Boolean parseBoolean(String text, Boolean defaultValue, String logHint) {
        Boolean value = defaultValue;
        try {
            value = Boolean.parseBoolean(text);
        } catch (Exception e) {
            LogUtil.e(TAG,
                    "Invalid value " + text + "for" + logHint + " @" + xmlParserDebugContext());
        }
        return value;
    }

    private static String xmlParserEventString(int event) {
        switch (event) {
            case XmlPullParser.START_DOCUMENT: return "START_DOCUMENT";
            case XmlPullParser.END_DOCUMENT: return "END_DOCUMENT";
            case XmlPullParser.START_TAG: return "START_TAG";
            case XmlPullParser.END_TAG: return "END_TAG";
            case XmlPullParser.TEXT: return "TEXT";
        }
        return Integer.toString(event);
    }

    /**
     * @return The debugging information of the parser's current position
     */
    private String xmlParserDebugContext() {
        mLogStringBuilder.setLength(0);
        if (mInputParser != null) {
            try {
                final int eventType = mInputParser.getEventType();
                mLogStringBuilder.append(xmlParserEventString(eventType));
                if (eventType == XmlPullParser.START_TAG
                        || eventType == XmlPullParser.END_TAG
                        || eventType == XmlPullParser.TEXT) {
                    mLogStringBuilder.append('<').append(mInputParser.getName());
                    for (int i = 0; i < mInputParser.getAttributeCount(); i++) {
                        mLogStringBuilder.append(' ')
                            .append(mInputParser.getAttributeName(i))
                            .append('=')
                            .append(mInputParser.getAttributeValue(i));
                    }
                    mLogStringBuilder.append("/>");
                }
                return mLogStringBuilder.toString();
            } catch (XmlPullParserException e) {
                LogUtil.e(TAG, "xmlParserDebugContext: " + e, e);
            }
        }
        return "Unknown";
    }

    /**
     * Process one apn
     *
     * @param apnValues Where we store the parsed apn
     * @throws IOException
     * @throws XmlPullParserException
     */
    private void processApn(ContentValues apnValues) throws IOException, XmlPullParserException {
        Assert.notNull(apnValues);
        apnValues.clear();
        // Collect all the attributes
        for (int i = 0; i < mInputParser.getAttributeCount(); i++) {
            final String key = APN_ATTRIBUTE_MAP.get(mInputParser.getAttributeName(i));
            if (key != null) {
                apnValues.put(key, mInputParser.getAttributeValue(i));
            }
        }
        // Set numeric to be canonicalized mcc/mnc like "310120", always 6 digits
        final String canonicalMccMnc = PhoneUtils.canonicalizeMccMnc(
                apnValues.getAsString(Telephony.Carriers.MCC),
                apnValues.getAsString(Telephony.Carriers.MNC));
        apnValues.put(Telephony.Carriers.NUMERIC, canonicalMccMnc);
        // Some of the values should not be string type, converting them to desired types
        final String authType = apnValues.getAsString(Telephony.Carriers.AUTH_TYPE);
        if (authType != null) {
            apnValues.put(Telephony.Carriers.AUTH_TYPE, parseInt(authType, -1, "apn authtype"));
        }
        final String carrierEnabled = apnValues.getAsString(Telephony.Carriers.CARRIER_ENABLED);
        if (carrierEnabled != null) {
            apnValues.put(Telephony.Carriers.CARRIER_ENABLED,
                    parseBoolean(carrierEnabled, null, "apn carrierEnabled"));
        }
        final String bearer = apnValues.getAsString(Telephony.Carriers.BEARER);
        if (bearer != null) {
            apnValues.put(Telephony.Carriers.BEARER, parseInt(bearer, 0, "apn bearer"));
        }
        // We are at the end tag
        if (mInputParser.next() != XmlPullParser.END_TAG) {
            throw new XmlPullParserException("Apn: expecting end tag @"
                    + xmlParserDebugContext());
        }
        // We are done parsing one APN, call the handler
        if (mApnHandler != null) {
            mApnHandler.process(apnValues);
        }
    }

    /**
     * Process one mms_config.
     *
     * @throws IOException
     * @throws XmlPullParserException
     */
    private void processMmsConfig()
            throws IOException, XmlPullParserException {
        // Get the mcc and mnc attributes
        final String canonicalMccMnc = PhoneUtils.canonicalizeMccMnc(
                mInputParser.getAttributeValue(null, "mcc"),
                mInputParser.getAttributeValue(null, "mnc"));
        // We are at the start tag
        for (;;) {
            int nextEvent;
            // Skipping spaces
            while ((nextEvent = mInputParser.next()) == XmlPullParser.TEXT) {
            }
            if (nextEvent == XmlPullParser.START_TAG) {
                // Parse one mms config key/value
                processMmsConfigKeyValue(canonicalMccMnc);
            } else if (nextEvent == XmlPullParser.END_TAG) {
                break;
            } else {
                throw new XmlPullParserException("MmsConfig: expecting start or end tag @"
                        + xmlParserDebugContext());
            }
        }
    }

    /**
     * Process one mms_config key/value pair
     *
     * @param mccMnc The mcc and mnc of this mms_config
     * @throws IOException
     * @throws XmlPullParserException
     */
    private void processMmsConfigKeyValue(String mccMnc)
            throws IOException, XmlPullParserException {
        final String key = mInputParser.getAttributeValue(null, "name");
        // We are at the start tag, the name of the tag is the type
        // e.g. <int name="key">value</int>
        final String type = mInputParser.getName();
        int nextEvent = mInputParser.next();
        String value = null;
        if (nextEvent == XmlPullParser.TEXT) {
            value = mInputParser.getText();
            nextEvent = mInputParser.next();
        }
        if (nextEvent != XmlPullParser.END_TAG) {
            throw new XmlPullParserException("ApnsXmlProcessor: expecting end tag @"
                    + xmlParserDebugContext());
        }
        // We are done parsing one mms_config key/value, call the handler
        if (mMmsConfigHandler != null) {
            mMmsConfigHandler.process(mccMnc, key, value, type);
        }
    }
}