SpnOverride.java revision 22d85a8e3a575a6d01d2c788587971657dfe20c6
1/*
2 * Copyright (C) 2009 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.uicc;
18
19import java.io.File;
20import java.io.FileNotFoundException;
21import java.io.FileReader;
22import java.io.IOException;
23import java.util.HashMap;
24
25import org.xmlpull.v1.XmlPullParser;
26import org.xmlpull.v1.XmlPullParserException;
27
28import android.os.Environment;
29import android.telephony.Rlog;
30import android.util.Xml;
31
32import com.android.internal.util.XmlUtils;
33
34public class SpnOverride {
35    private HashMap<String, String> mCarrierSpnMap;
36
37
38    static final String LOG_TAG = "SpnOverride";
39    static final String PARTNER_SPN_OVERRIDE_PATH ="etc/spn-conf.xml";
40
41    SpnOverride () {
42        mCarrierSpnMap = new HashMap<String, String>();
43        loadSpnOverrides();
44    }
45
46    boolean containsCarrier(String carrier) {
47        return mCarrierSpnMap.containsKey(carrier);
48    }
49
50    String getSpn(String carrier) {
51        return mCarrierSpnMap.get(carrier);
52    }
53
54    private void loadSpnOverrides() {
55        FileReader spnReader;
56
57        final File spnFile = new File(Environment.getRootDirectory(),
58                PARTNER_SPN_OVERRIDE_PATH);
59
60        try {
61            spnReader = new FileReader(spnFile);
62        } catch (FileNotFoundException e) {
63            Rlog.w(LOG_TAG, "Can not open " +
64                    Environment.getRootDirectory() + "/" + PARTNER_SPN_OVERRIDE_PATH);
65            return;
66        }
67
68        try {
69            XmlPullParser parser = Xml.newPullParser();
70            parser.setInput(spnReader);
71
72            XmlUtils.beginDocument(parser, "spnOverrides");
73
74            while (true) {
75                XmlUtils.nextElement(parser);
76
77                String name = parser.getName();
78                if (!"spnOverride".equals(name)) {
79                    break;
80                }
81
82                String numeric = parser.getAttributeValue(null, "numeric");
83                String data    = parser.getAttributeValue(null, "spn");
84
85                mCarrierSpnMap.put(numeric, data);
86            }
87        } catch (XmlPullParserException e) {
88            Rlog.w(LOG_TAG, "Exception in spn-conf parser " + e);
89        } catch (IOException e) {
90            Rlog.w(LOG_TAG, "Exception in spn-conf parser " + e);
91        }
92    }
93
94}
95