• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 android.os.Environment;
20 import android.telephony.Rlog;
21 import android.util.Xml;
22 
23 import com.android.internal.util.XmlUtils;
24 
25 import org.xmlpull.v1.XmlPullParser;
26 import org.xmlpull.v1.XmlPullParserException;
27 
28 import java.io.File;
29 import java.io.FileNotFoundException;
30 import java.io.FileReader;
31 import java.io.IOException;
32 import java.util.HashMap;
33 
34 /**
35  * Provide a machanism to override MVNO paramteres under CarrierConfig through a config file.
36  */
37 public class CarrierTestOverride {
38     static final String LOG_TAG = "CarrierTestOverride";
39 
40     /**
41      * Config file that can be created and adb-pushed by tester/developer
42      *
43      * Sample xml:
44      * <carrierTestOverrides>
45        <carrierTestOverride key="isInTestMode" value="true"/>
46        <carrierTestOverride key="gid1" value="bae0000000000000"/>
47        <carrierTestOverride key="gid2" value="ffffffffffffffff"/>
48        <carrierTestOverride key="imsi" value="310010123456789"/>
49        <carrierTestOverride key="spn" value="Verizon"/>
50        </carrierTestOverrides>
51      */
52     static final String DATA_CARRIER_TEST_OVERRIDE_PATH =
53             "/user_de/0/com.android.phone/files/carrier_test_conf.xml";
54     static final String CARRIER_TEST_XML_HEADER = "carrierTestOverrides";
55     static final String CARRIER_TEST_XML_SUBHEADER = "carrierTestOverride";
56     static final String CARRIER_TEST_XML_ITEM_KEY = "key";
57     static final String CARRIER_TEST_XML_ITEM_VALUE = "value";
58     static final String CARRIER_TEST_XML_ITEM_KEY_STRING_ISINTESTMODE = "isInTestMode";
59     static final String CARRIER_TEST_XML_ITEM_KEY_STRING_GID1 = "gid1";
60     static final String CARRIER_TEST_XML_ITEM_KEY_STRING_GID2 = "gid2";
61     static final String CARRIER_TEST_XML_ITEM_KEY_STRING_IMSI = "imsi";
62     static final String CARRIER_TEST_XML_ITEM_KEY_STRING_SPN = "spn";
63 
64     private HashMap<String, String> mCarrierTestParamMap;
65 
CarrierTestOverride()66     CarrierTestOverride() {
67         mCarrierTestParamMap = new HashMap<String, String>();
68         loadCarrierTestOverrides();
69     }
70 
isInTestMode()71     boolean isInTestMode() {
72         return mCarrierTestParamMap.containsKey(CARRIER_TEST_XML_ITEM_KEY_STRING_ISINTESTMODE)
73                 && mCarrierTestParamMap.get(CARRIER_TEST_XML_ITEM_KEY_STRING_ISINTESTMODE)
74                 .equals("true");
75     }
76 
getFakeSpn()77     String getFakeSpn() {
78         try {
79             String spn = mCarrierTestParamMap.get(CARRIER_TEST_XML_ITEM_KEY_STRING_SPN);
80             Rlog.d(LOG_TAG, "reading spn from CarrierTestConfig file: " + spn);
81             return spn;
82         } catch (NullPointerException e) {
83             Rlog.w(LOG_TAG, "No spn in CarrierTestConfig file ");
84             return null;
85         }
86     }
87 
getFakeIMSI()88     String getFakeIMSI() {
89         try {
90             String imsi = mCarrierTestParamMap.get(CARRIER_TEST_XML_ITEM_KEY_STRING_IMSI);
91             Rlog.d(LOG_TAG, "reading imsi from CarrierTestConfig file: " + imsi);
92             return imsi;
93         } catch (NullPointerException e) {
94             Rlog.w(LOG_TAG, "No imsi in CarrierTestConfig file ");
95             return null;
96         }
97     }
98 
getFakeGid1()99     String getFakeGid1() {
100         try {
101             String gid1 = mCarrierTestParamMap.get(CARRIER_TEST_XML_ITEM_KEY_STRING_GID1);
102             Rlog.d(LOG_TAG, "reading gid1 from CarrierTestConfig file: " + gid1);
103             return gid1;
104         } catch (NullPointerException e) {
105             Rlog.w(LOG_TAG, "No gid1 in CarrierTestConfig file ");
106             return null;
107         }
108     }
109 
getFakeGid2()110     String getFakeGid2() {
111         try {
112             String gid2 = mCarrierTestParamMap.get(CARRIER_TEST_XML_ITEM_KEY_STRING_GID2);
113             Rlog.d(LOG_TAG, "reading gid2 from CarrierTestConfig file: " + gid2);
114             return gid2;
115         } catch (NullPointerException e) {
116             Rlog.w(LOG_TAG, "No gid2 in CarrierTestConfig file ");
117             return null;
118         }
119     }
120 
loadCarrierTestOverrides()121     private void loadCarrierTestOverrides() {
122 
123         FileReader carrierTestConfigReader;
124 
125         File carrierTestConfigFile = new File(Environment.getDataDirectory(),
126                 DATA_CARRIER_TEST_OVERRIDE_PATH);
127 
128         try {
129             carrierTestConfigReader = new FileReader(carrierTestConfigFile);
130             Rlog.d(LOG_TAG, "CarrierTestConfig file Modified Timestamp: "
131                     + carrierTestConfigFile.lastModified());
132         } catch (FileNotFoundException e) {
133             Rlog.w(LOG_TAG, "Can not open " + carrierTestConfigFile.getAbsolutePath());
134             return;
135         }
136 
137         try {
138             XmlPullParser parser = Xml.newPullParser();
139             parser.setInput(carrierTestConfigReader);
140 
141             XmlUtils.beginDocument(parser, CARRIER_TEST_XML_HEADER);
142 
143             while (true) {
144                 XmlUtils.nextElement(parser);
145 
146                 String name = parser.getName();
147                 if (!CARRIER_TEST_XML_SUBHEADER.equals(name)) {
148                     break;
149                 }
150 
151                 String key = parser.getAttributeValue(null, CARRIER_TEST_XML_ITEM_KEY);
152                 String value = parser.getAttributeValue(null, CARRIER_TEST_XML_ITEM_VALUE);
153 
154                 Rlog.d(LOG_TAG,
155                         "extracting key-values from CarrierTestConfig file: " + key + "|" + value);
156                 mCarrierTestParamMap.put(key, value);
157             }
158             carrierTestConfigReader.close();
159         } catch (XmlPullParserException e) {
160             Rlog.w(LOG_TAG, "Exception in carrier_test_conf parser " + e);
161         } catch (IOException e) {
162             Rlog.w(LOG_TAG, "Exception in carrier_test_conf parser " + e);
163         }
164     }
165 }
166