Home | History | Annotate | Download | only in uicc
      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 
     17 package com.android.internal.telephony.uicc;
     18 
     19 import java.io.File;
     20 import java.io.FileNotFoundException;
     21 import java.io.FileReader;
     22 import java.io.IOException;
     23 import java.util.HashMap;
     24 
     25 import org.xmlpull.v1.XmlPullParser;
     26 import org.xmlpull.v1.XmlPullParserException;
     27 
     28 import android.os.Environment;
     29 import android.telephony.Rlog;
     30 import android.util.Xml;
     31 
     32 import com.android.internal.util.XmlUtils;
     33 
     34 public 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     static final String OEM_SPN_OVERRIDE_PATH = "telephony/spn-conf.xml";
     41 
     42     SpnOverride () {
     43         mCarrierSpnMap = new HashMap<String, String>();
     44         loadSpnOverrides();
     45     }
     46 
     47     boolean containsCarrier(String carrier) {
     48         return mCarrierSpnMap.containsKey(carrier);
     49     }
     50 
     51     String getSpn(String carrier) {
     52         return mCarrierSpnMap.get(carrier);
     53     }
     54 
     55     private void loadSpnOverrides() {
     56         FileReader spnReader;
     57 
     58         File spnFile = new File(Environment.getRootDirectory(),
     59                 PARTNER_SPN_OVERRIDE_PATH);
     60         File oemSpnFile = new File(Environment.getOemDirectory(),
     61                 OEM_SPN_OVERRIDE_PATH);
     62 
     63         if (oemSpnFile.exists()) {
     64             // OEM image exist SPN xml, get the timestamp from OEM & System image for comparison.
     65             long oemSpnTime = oemSpnFile.lastModified();
     66             long sysSpnTime = spnFile.lastModified();
     67             Rlog.d(LOG_TAG, "SPN Timestamp: oemTime = " + oemSpnTime + " sysTime = " + sysSpnTime);
     68 
     69             // To get the newer version of SPN from OEM image
     70             if (oemSpnTime > sysSpnTime) {
     71                 Rlog.d(LOG_TAG, "SPN in OEM image is newer than System image");
     72                 spnFile = oemSpnFile;
     73             }
     74         } else {
     75             // No SPN in OEM image, so load it from system image.
     76             Rlog.d(LOG_TAG, "No SPN in OEM image = " + oemSpnFile.getPath() +
     77                 " Load SPN from system image");
     78         }
     79 
     80         try {
     81             spnReader = new FileReader(spnFile);
     82         } catch (FileNotFoundException e) {
     83             Rlog.w(LOG_TAG, "Can not open " + spnFile.getAbsolutePath());
     84             return;
     85         }
     86 
     87         try {
     88             XmlPullParser parser = Xml.newPullParser();
     89             parser.setInput(spnReader);
     90 
     91             XmlUtils.beginDocument(parser, "spnOverrides");
     92 
     93             while (true) {
     94                 XmlUtils.nextElement(parser);
     95 
     96                 String name = parser.getName();
     97                 if (!"spnOverride".equals(name)) {
     98                     break;
     99                 }
    100 
    101                 String numeric = parser.getAttributeValue(null, "numeric");
    102                 String data    = parser.getAttributeValue(null, "spn");
    103 
    104                 mCarrierSpnMap.put(numeric, data);
    105             }
    106             spnReader.close();
    107         } catch (XmlPullParserException e) {
    108             Rlog.w(LOG_TAG, "Exception in spn-conf parser " + e);
    109         } catch (IOException e) {
    110             Rlog.w(LOG_TAG, "Exception in spn-conf parser " + e);
    111         }
    112     }
    113 
    114 }
    115