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.util.Xml;
21 
22 import com.android.internal.telephony.util.XmlUtils;
23 import com.android.telephony.Rlog;
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="mccmnc" value="310010" />
47        <carrierTestOverride key="gid1" value="bae0000000000000"/>
48        <carrierTestOverride key="gid2" value="ffffffffffffffff"/>
49        <carrierTestOverride key="imsi" value="310010123456789"/>
50        <carrierTestOverride key="spn" value="Verizon"/>
51        <carrierTestOverride key="pnn" value="Verizon network"/>
52        <carrierTestOverride key="iccid" value="123456789012345678901"/>
53        </carrierTestOverrides>
54      */
55     static final String DATA_CARRIER_TEST_OVERRIDE_PATH =
56             "/user_de/0/com.android.phone/files/carrier_test_conf.xml";
57     static final String CARRIER_TEST_XML_HEADER = "carrierTestOverrides";
58     static final String CARRIER_TEST_XML_SUBHEADER = "carrierTestOverride";
59     static final String CARRIER_TEST_XML_ITEM_KEY = "key";
60     static final String CARRIER_TEST_XML_ITEM_VALUE = "value";
61     static final String CARRIER_TEST_XML_ITEM_KEY_STRING_ISINTESTMODE = "isInTestMode";
62     static final String CARRIER_TEST_XML_ITEM_KEY_STRING_MCCMNC = "mccmnc";
63     static final String CARRIER_TEST_XML_ITEM_KEY_STRING_GID1 = "gid1";
64     static final String CARRIER_TEST_XML_ITEM_KEY_STRING_GID2 = "gid2";
65     static final String CARRIER_TEST_XML_ITEM_KEY_STRING_IMSI = "imsi";
66     static final String CARRIER_TEST_XML_ITEM_KEY_STRING_SPN = "spn";
67     static final String CARRIER_TEST_XML_ITEM_KEY_STRING_PNN = "pnn";
68     static final String CARRIER_TEST_XML_ITEM_KEY_STRING_ICCID = "iccid";
69 
70     private HashMap<String, String> mCarrierTestParamMap;
71 
CarrierTestOverride()72     CarrierTestOverride() {
73         mCarrierTestParamMap = new HashMap<String, String>();
74         loadCarrierTestOverrides();
75     }
76 
isInTestMode()77     boolean isInTestMode() {
78         return mCarrierTestParamMap.containsKey(CARRIER_TEST_XML_ITEM_KEY_STRING_ISINTESTMODE)
79                 && mCarrierTestParamMap.get(CARRIER_TEST_XML_ITEM_KEY_STRING_ISINTESTMODE)
80                 .equals("true");
81     }
82 
getFakeSpn()83     String getFakeSpn() {
84         try {
85             String spn = mCarrierTestParamMap.get(CARRIER_TEST_XML_ITEM_KEY_STRING_SPN);
86             Rlog.d(LOG_TAG, "reading spn from CarrierTestConfig file: " + spn);
87             return spn;
88         } catch (NullPointerException e) {
89             Rlog.w(LOG_TAG, "No spn in CarrierTestConfig file ");
90             return null;
91         }
92     }
93 
getFakeIMSI()94     String getFakeIMSI() {
95         try {
96             String imsi = mCarrierTestParamMap.get(CARRIER_TEST_XML_ITEM_KEY_STRING_IMSI);
97             Rlog.d(LOG_TAG, "reading imsi from CarrierTestConfig file: " + imsi);
98             return imsi;
99         } catch (NullPointerException e) {
100             Rlog.w(LOG_TAG, "No imsi in CarrierTestConfig file ");
101             return null;
102         }
103     }
104 
getFakeGid1()105     String getFakeGid1() {
106         try {
107             String gid1 = mCarrierTestParamMap.get(CARRIER_TEST_XML_ITEM_KEY_STRING_GID1);
108             Rlog.d(LOG_TAG, "reading gid1 from CarrierTestConfig file: " + gid1);
109             return gid1;
110         } catch (NullPointerException e) {
111             Rlog.w(LOG_TAG, "No gid1 in CarrierTestConfig file ");
112             return null;
113         }
114     }
115 
getFakeGid2()116     String getFakeGid2() {
117         try {
118             String gid2 = mCarrierTestParamMap.get(CARRIER_TEST_XML_ITEM_KEY_STRING_GID2);
119             Rlog.d(LOG_TAG, "reading gid2 from CarrierTestConfig file: " + gid2);
120             return gid2;
121         } catch (NullPointerException e) {
122             Rlog.w(LOG_TAG, "No gid2 in CarrierTestConfig file ");
123             return null;
124         }
125     }
126 
getFakePnnHomeName()127     String getFakePnnHomeName() {
128         try {
129             String pnn = mCarrierTestParamMap.get(CARRIER_TEST_XML_ITEM_KEY_STRING_PNN);
130             Rlog.d(LOG_TAG, "reading pnn from CarrierTestConfig file: " + pnn);
131             return pnn;
132         } catch (NullPointerException e) {
133             Rlog.w(LOG_TAG, "No pnn in CarrierTestConfig file ");
134             return null;
135         }
136     }
137 
getFakeIccid()138     String getFakeIccid() {
139         try {
140             String iccid = mCarrierTestParamMap.get(CARRIER_TEST_XML_ITEM_KEY_STRING_ICCID);
141             Rlog.d(LOG_TAG, "reading iccid from CarrierTestConfig file: " + iccid);
142             return iccid;
143         } catch (NullPointerException e) {
144             Rlog.w(LOG_TAG, "No iccid in CarrierTestConfig file ");
145             return null;
146         }
147     }
148 
getFakeMccMnc()149     String getFakeMccMnc() {
150         try {
151             String mccmnc = mCarrierTestParamMap.get(CARRIER_TEST_XML_ITEM_KEY_STRING_MCCMNC);
152             Rlog.d(LOG_TAG, "reading mccmnc from CarrierTestConfig file: " + mccmnc);
153             return mccmnc;
154         } catch (NullPointerException e) {
155             Rlog.w(LOG_TAG, "No mccmnc in CarrierTestConfig file ");
156             return null;
157         }
158     }
159 
override(String mccmnc, String imsi, String iccid, String gid1, String gid2, String pnn, String spn)160     void override(String mccmnc, String imsi, String iccid, String gid1, String gid2, String pnn,
161             String spn) {
162         mCarrierTestParamMap.put(CARRIER_TEST_XML_ITEM_KEY_STRING_ISINTESTMODE, "true");
163         mCarrierTestParamMap.put(CARRIER_TEST_XML_ITEM_KEY_STRING_MCCMNC, mccmnc);
164         mCarrierTestParamMap.put(CARRIER_TEST_XML_ITEM_KEY_STRING_IMSI, imsi);
165         mCarrierTestParamMap.put(CARRIER_TEST_XML_ITEM_KEY_STRING_ICCID, iccid);
166         mCarrierTestParamMap.put(CARRIER_TEST_XML_ITEM_KEY_STRING_GID1, gid1);
167         mCarrierTestParamMap.put(CARRIER_TEST_XML_ITEM_KEY_STRING_GID2, gid2);
168         mCarrierTestParamMap.put(CARRIER_TEST_XML_ITEM_KEY_STRING_PNN, pnn);
169         mCarrierTestParamMap.put(CARRIER_TEST_XML_ITEM_KEY_STRING_SPN, spn);
170     }
171 
loadCarrierTestOverrides()172     private void loadCarrierTestOverrides() {
173 
174         FileReader carrierTestConfigReader;
175 
176         File carrierTestConfigFile = new File(Environment.getDataDirectory(),
177                 DATA_CARRIER_TEST_OVERRIDE_PATH);
178 
179         try {
180             carrierTestConfigReader = new FileReader(carrierTestConfigFile);
181             Rlog.d(LOG_TAG, "CarrierTestConfig file Modified Timestamp: "
182                     + carrierTestConfigFile.lastModified());
183         } catch (FileNotFoundException e) {
184             Rlog.w(LOG_TAG, "Can not open " + carrierTestConfigFile.getAbsolutePath());
185             return;
186         }
187 
188         try {
189             XmlPullParser parser = Xml.newPullParser();
190             parser.setInput(carrierTestConfigReader);
191 
192             XmlUtils.beginDocument(parser, CARRIER_TEST_XML_HEADER);
193 
194             while (true) {
195                 XmlUtils.nextElement(parser);
196 
197                 String name = parser.getName();
198                 if (!CARRIER_TEST_XML_SUBHEADER.equals(name)) {
199                     break;
200                 }
201 
202                 String key = parser.getAttributeValue(null, CARRIER_TEST_XML_ITEM_KEY);
203                 String value = parser.getAttributeValue(null, CARRIER_TEST_XML_ITEM_VALUE);
204 
205                 Rlog.d(LOG_TAG,
206                         "extracting key-values from CarrierTestConfig file: " + key + "|" + value);
207                 mCarrierTestParamMap.put(key, value);
208             }
209             carrierTestConfigReader.close();
210         } catch (XmlPullParserException e) {
211             Rlog.w(LOG_TAG, "Exception in carrier_test_conf parser " + e);
212         } catch (IOException e) {
213             Rlog.w(LOG_TAG, "Exception in carrier_test_conf parser " + e);
214         }
215     }
216 }
217