blob: f1706be52b9f63f1ff1c1dd00fa87f191eabf8b1 [file] [log] [blame]
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001/**
2 * Copyright (c) 2015, 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
Jonathan Basseri6465afd2015-02-25 13:05:57 -080017package com.android.phone;
18
19import static android.Manifest.permission.READ_PHONE_STATE;
20import static com.android.internal.telephony.uicc.IccCardProxy.ACTION_INTERNAL_SIM_STATE_CHANGED;
21
Jonathan Basseri11f89572015-05-05 11:57:39 -070022import android.annotation.NonNull;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080023import android.app.ActivityManagerNative;
24import android.content.BroadcastReceiver;
25import android.content.ComponentName;
26import android.content.Context;
27import android.content.Intent;
28import android.content.IntentFilter;
29import android.content.ServiceConnection;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070030import android.content.pm.PackageInfo;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070031import android.content.pm.PackageManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080032import android.database.sqlite.SQLiteDatabase;
33import android.database.sqlite.SQLiteOpenHelper;
34import android.os.AsyncResult;
Junda Liu43d723a2015-05-12 17:23:45 -070035import android.os.Binder;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080036import android.os.Handler;
37import android.os.IBinder;
38import android.os.Message;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070039import android.os.PersistableBundle;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080040import android.os.RemoteException;
41import android.os.ServiceManager;
42import android.os.UserHandle;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080043import android.service.carrier.CarrierIdentifier;
Zach Johnson36d7aab2015-05-22 15:43:00 -070044import android.service.carrier.CarrierService;
45import android.service.carrier.ICarrierService;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080046import android.telephony.CarrierConfigManager;
47import android.telephony.SubscriptionManager;
48import android.telephony.TelephonyManager;
49import android.util.Log;
50
51import com.android.internal.telephony.ICarrierConfigLoader;
52import com.android.internal.telephony.IccCardConstants;
53import com.android.internal.telephony.Phone;
54import com.android.internal.telephony.PhoneConstants;
55import com.android.internal.telephony.PhoneFactory;
56import com.android.internal.telephony.TelephonyIntents;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070057import com.android.internal.util.FastXmlSerializer;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080058
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070059import org.xmlpull.v1.XmlPullParser;
60import org.xmlpull.v1.XmlPullParserException;
61import org.xmlpull.v1.XmlPullParserFactory;
62
63import java.io.File;
Junda Liu43d723a2015-05-12 17:23:45 -070064import java.io.FileDescriptor;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070065import java.io.FileInputStream;
66import java.io.FileNotFoundException;
67import java.io.FileOutputStream;
Jonathan Basseri1f743c92015-05-15 00:19:46 -070068import java.io.FilenameFilter;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070069import java.io.IOException;
Junda Liu43d723a2015-05-12 17:23:45 -070070import java.io.PrintWriter;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080071import java.util.List;
72
73/**
74 * CarrierConfigLoader binds to privileged carrier apps to fetch carrier config overlays.
Jonathan Basseri6465afd2015-02-25 13:05:57 -080075 * TODO: handle package install/uninstall events
76 */
77
78public class CarrierConfigLoader extends ICarrierConfigLoader.Stub {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070079 private static final String LOG_TAG = "CarrierConfigLoader";
Jonathan Basseri6465afd2015-02-25 13:05:57 -080080 // Package name for default carrier config app, bundled with system image.
81 private static final String DEFAULT_CARRIER_CONFIG_PACKAGE = "com.android.carrierconfig";
82
83 /** The singleton instance. */
84 private static CarrierConfigLoader sInstance;
85 // The context for phone app, passed from PhoneGlobals.
86 private Context mContext;
87 // Carrier configs from default app, indexed by phoneID.
Jonathan Basseric31f1f32015-05-12 10:13:03 -070088 private PersistableBundle[] mConfigFromDefaultApp;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080089 // Carrier configs from privileged carrier config app, indexed by phoneID.
Jonathan Basseric31f1f32015-05-12 10:13:03 -070090 private PersistableBundle[] mConfigFromCarrierApp;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080091 // Service connection for binding to config app.
Zach Johnson36d7aab2015-05-22 15:43:00 -070092 private CarrierServiceConnection[] mServiceConnection;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080093
94 // Broadcast receiver for SIM and pkg intents, register intent filter in constructor.
95 private final BroadcastReceiver mReceiver = new ConfigLoaderBroadcastReceiver();
96
97 // Message codes; see mHandler below.
98 // Request from SubscriptionInfoUpdater when SIM becomes absent or error.
99 private static final int EVENT_CLEAR_CONFIG = 0;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800100 // Has connected to default app.
101 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
102 // Has connected to carrier app.
103 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
104 // Config has been loaded from default app.
105 private static final int EVENT_LOADED_FROM_DEFAULT = 5;
106 // Config has been loaded from carrier app.
107 private static final int EVENT_LOADED_FROM_CARRIER = 6;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700108 // Attempt to fetch from default app or read from XML.
109 private static final int EVENT_FETCH_DEFAULT = 7;
110 // Attempt to fetch from carrier app or read from XML.
111 private static final int EVENT_FETCH_CARRIER = 8;
112 // A package has been installed, uninstalled, or updated.
113 private static final int EVENT_PACKAGE_CHANGED = 9;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700114 // Bind timed out for the default app.
115 private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
116 // Bind timed out for a carrier app.
117 private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
118
119 private static final int BIND_TIMEOUT_MILLIS = 10000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800120
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700121 // Tags used for saving and restoring XML documents.
122 private static final String TAG_DOCUMENT = "carrier_config";
123 private static final String TAG_VERSION = "package_version";
124 private static final String TAG_BUNDLE = "bundle_data";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800125
126 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700127 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700128 // For each phoneId, the event sequence should be:
129 // fetch default, connected to default, loaded from default,
130 // fetch carrier, connected to carrier, loaded from carrier.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700131 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700132 // If there is a saved config file for either the default app or the carrier app, we skip
133 // binding to the app and go straight from fetch to loaded.
134 //
135 // At any time, at most one connection is active. If events are not in this order, previous
136 // connection will be unbound, so only latest event takes effect.
137 //
138 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
139 // 1. loading from carrier app (even if read from a file)
140 // 2. loading from default app if there is no carrier app (even if read from a file)
141 // 3. clearing config (e.g. due to sim removal)
142 // 4. encountering bind or IPC error
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800143 private Handler mHandler = new Handler() {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700144 @Override
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800145 public void handleMessage(Message msg) {
146 int phoneId = msg.arg1;
147 log("mHandler: " + msg.what + " phoneId: " + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700148 String iccid;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800149 CarrierIdentifier carrierId;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700150 String carrierPackageName;
Zach Johnson36d7aab2015-05-22 15:43:00 -0700151 CarrierServiceConnection conn;
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700152 PersistableBundle config;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800153 switch (msg.what) {
154 case EVENT_CLEAR_CONFIG:
Junda Liu0486bdb2015-05-22 15:01:35 -0700155 if (mConfigFromDefaultApp[phoneId] == null &&
156 mConfigFromCarrierApp[phoneId] == null)
157 break;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800158 mConfigFromDefaultApp[phoneId] = null;
159 mConfigFromCarrierApp[phoneId] = null;
160 mServiceConnection[phoneId] = null;
161 broadcastConfigChangedIntent(phoneId);
162 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700163
164 case EVENT_PACKAGE_CHANGED:
165 carrierPackageName = (String) msg.obj;
166 deleteConfigForPackage(carrierPackageName);
167 int numPhones = TelephonyManager.from(mContext).getPhoneCount();
168 for (int i = 0; i < numPhones; ++i) {
169 updateConfigForPhoneId(i);
170 }
171 break;
172
173 case EVENT_FETCH_DEFAULT:
174 iccid = getIccIdForPhoneId(phoneId);
175 config = restoreConfigFromXml(DEFAULT_CARRIER_CONFIG_PACKAGE, iccid);
176 if (config != null) {
177 log("Loaded config from XML. package=" + DEFAULT_CARRIER_CONFIG_PACKAGE
178 + " phoneId=" + phoneId);
179 mConfigFromDefaultApp[phoneId] = config;
180 Message newMsg = obtainMessage(EVENT_LOADED_FROM_DEFAULT, phoneId, -1);
181 newMsg.getData().putBoolean("loaded_from_xml", true);
182 mHandler.sendMessage(newMsg);
183 } else {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700184 if (bindToConfigPackage(DEFAULT_CARRIER_CONFIG_PACKAGE,
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700185 phoneId, EVENT_CONNECTED_TO_DEFAULT)) {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700186 sendMessageDelayed(obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1),
187 BIND_TIMEOUT_MILLIS);
188 } else {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700189 // Send bcast if bind fails
190 broadcastConfigChangedIntent(phoneId);
191 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800192 }
193 break;
194
195 case EVENT_CONNECTED_TO_DEFAULT:
Jonathan Basseri930701e2015-06-11 20:56:43 -0700196 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800197 carrierId = getCarrierIdForPhoneId(phoneId);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700198 conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800199 // If new service connection has been created, unbind.
200 if (mServiceConnection[phoneId] != conn || conn.service == null) {
201 mContext.unbindService(conn);
202 break;
203 }
204 try {
Zach Johnson36d7aab2015-05-22 15:43:00 -0700205 ICarrierService carrierService = ICarrierService.Stub
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700206 .asInterface(conn.service);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700207 config = carrierService.getCarrierConfig(carrierId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700208 iccid = getIccIdForPhoneId(phoneId);
209 saveConfigToXml(DEFAULT_CARRIER_CONFIG_PACKAGE, iccid, config);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800210 mConfigFromDefaultApp[phoneId] = config;
Junda Liu9f2d2712015-05-15 14:22:45 -0700211 sendMessage(obtainMessage(EVENT_LOADED_FROM_DEFAULT, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800212 } catch (RemoteException ex) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700213 loge("Failed to get carrier config: " + ex.toString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800214 } finally {
215 mContext.unbindService(mServiceConnection[phoneId]);
216 }
217 break;
218
Jonathan Basseri930701e2015-06-11 20:56:43 -0700219 case EVENT_BIND_DEFAULT_TIMEOUT:
220 mContext.unbindService(mServiceConnection[phoneId]);
221 broadcastConfigChangedIntent(phoneId);
222 break;
223
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800224 case EVENT_LOADED_FROM_DEFAULT:
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700225 // If we attempted to bind to the app, but the service connection is null, then
226 // config was cleared while we were waiting and we should not continue.
227 if (!msg.getData().getBoolean("loaded_from_xml", false)
228 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800229 break;
230 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700231 carrierPackageName = getCarrierPackageForPhoneId(phoneId);
232 if (carrierPackageName != null) {
233 log("Found carrier config app: " + carrierPackageName);
234 sendMessage(obtainMessage(EVENT_FETCH_CARRIER, phoneId));
Jonathan Basserib919e932015-05-27 16:53:08 +0000235 } else {
236 broadcastConfigChangedIntent(phoneId);
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700237 }
238 break;
239
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700240 case EVENT_FETCH_CARRIER:
241 carrierPackageName = getCarrierPackageForPhoneId(phoneId);
242 iccid = getIccIdForPhoneId(phoneId);
243 config = restoreConfigFromXml(carrierPackageName, iccid);
244 if (config != null) {
245 log("Loaded config from XML. package=" + carrierPackageName + " phoneId="
246 + phoneId);
247 mConfigFromCarrierApp[phoneId] = config;
248 Message newMsg = obtainMessage(EVENT_LOADED_FROM_CARRIER, phoneId, -1);
249 newMsg.getData().putBoolean("loaded_from_xml", true);
250 sendMessage(newMsg);
251 } else {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700252 if (bindToConfigPackage(carrierPackageName, phoneId,
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700253 EVENT_CONNECTED_TO_CARRIER)) {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700254 sendMessageDelayed(obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1),
255 BIND_TIMEOUT_MILLIS);
256 } else {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700257 // Send bcast if bind fails
258 broadcastConfigChangedIntent(phoneId);
259 }
260 }
261 break;
262
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800263 case EVENT_CONNECTED_TO_CARRIER:
Jonathan Basseri930701e2015-06-11 20:56:43 -0700264 removeMessages(EVENT_BIND_CARRIER_TIMEOUT);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800265 carrierId = getCarrierIdForPhoneId(phoneId);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700266 conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800267 // If new service connection has been created, unbind.
268 if (mServiceConnection[phoneId] != conn ||
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700269 conn.service == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800270 mContext.unbindService(conn);
271 break;
272 }
273 try {
Zach Johnson36d7aab2015-05-22 15:43:00 -0700274 ICarrierService carrierService = ICarrierService.Stub
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700275 .asInterface(conn.service);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700276 config = carrierService.getCarrierConfig(carrierId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700277 carrierPackageName = getCarrierPackageForPhoneId(phoneId);
278 iccid = getIccIdForPhoneId(phoneId);
279 saveConfigToXml(carrierPackageName, iccid, config);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800280 mConfigFromCarrierApp[phoneId] = config;
Junda Liu9f2d2712015-05-15 14:22:45 -0700281 sendMessage(obtainMessage(EVENT_LOADED_FROM_CARRIER, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800282 } catch (RemoteException ex) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700283 loge("Failed to get carrier config: " + ex.toString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800284 } finally {
285 mContext.unbindService(mServiceConnection[phoneId]);
286 }
287 break;
288
Jonathan Basseri930701e2015-06-11 20:56:43 -0700289 case EVENT_BIND_CARRIER_TIMEOUT:
290 mContext.unbindService(mServiceConnection[phoneId]);
291 broadcastConfigChangedIntent(phoneId);
292 break;
293
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800294 case EVENT_LOADED_FROM_CARRIER:
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700295 // If we attempted to bind to the app, but the service connection is null, then
296 // config was cleared while we were waiting and we should not continue.
297 if (!msg.getData().getBoolean("loaded_from_xml", false)
298 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800299 break;
300 }
301 broadcastConfigChangedIntent(phoneId);
302 break;
303 }
304 }
305 };
306
307 /**
308 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
309 * receiver for relevant events.
310 */
311 private CarrierConfigLoader(Context context) {
312 mContext = context;
313
314 // Register for package updates.
315 IntentFilter triggers = new IntentFilter();
316 triggers.addAction(Intent.ACTION_PACKAGE_ADDED);
317 triggers.addAction(Intent.ACTION_PACKAGE_CHANGED);
318 triggers.addAction(Intent.ACTION_PACKAGE_REMOVED);
319 mContext.registerReceiver(mReceiver, triggers);
320
321 int numPhones = TelephonyManager.from(context).getPhoneCount();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700322 mConfigFromDefaultApp = new PersistableBundle[numPhones];
323 mConfigFromCarrierApp = new PersistableBundle[numPhones];
Zach Johnson36d7aab2015-05-22 15:43:00 -0700324 mServiceConnection = new CarrierServiceConnection[numPhones];
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800325 // Make this service available through ServiceManager.
326 ServiceManager.addService(Context.CARRIER_CONFIG_SERVICE, this);
327 log("CarrierConfigLoader has started");
328 }
329
330 /**
331 * Initialize the singleton CarrierConfigLoader instance.
332 *
333 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
334 */
335 /* package */
336 static CarrierConfigLoader init(Context context) {
337 synchronized (CarrierConfigLoader.class) {
338 if (sInstance == null) {
339 sInstance = new CarrierConfigLoader(context);
340 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700341 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800342 }
343 return sInstance;
344 }
345 }
346
347 private void broadcastConfigChangedIntent(int phoneId) {
348 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
349 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
350 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
351 ActivityManagerNative.broadcastStickyIntent(intent, READ_PHONE_STATE,
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700352 UserHandle.USER_ALL);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800353 }
354
355 /** Binds to the default or carrier config app. */
356 private boolean bindToConfigPackage(String pkgName, int phoneId, int eventId) {
357 log("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsona9d689f2015-05-27 16:23:22 -0700358 Intent carrierService = new Intent(CarrierService.CONFIG_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700359 carrierService.setPackage(pkgName);
360 mServiceConnection[phoneId] = new CarrierServiceConnection(phoneId, eventId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800361 try {
Zach Johnson36d7aab2015-05-22 15:43:00 -0700362 return mContext.bindService(carrierService, mServiceConnection[phoneId],
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800363 Context.BIND_AUTO_CREATE);
364 } catch (SecurityException ex) {
365 return false;
366 }
367 }
368
369 private CarrierIdentifier getCarrierIdForPhoneId(int phoneId) {
370 String mcc = "";
371 String mnc = "";
372 String imsi = "";
373 String gid1 = "";
374 String gid2 = "";
375 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
376 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700377 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
378 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800379 mcc = simOperator.substring(0, 3);
380 mnc = simOperator.substring(3);
381 }
382 Phone phone = PhoneFactory.getPhone(phoneId);
383 if (phone != null) {
384 imsi = phone.getSubscriberId();
385 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700386 gid2 = phone.getGroupIdLevel2();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800387 }
388
389 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2);
390 }
391
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700392 /** Returns the package name of a priveleged carrier app, or null if there is none. */
393 private String getCarrierPackageForPhoneId(int phoneId) {
394 List<String> carrierPackageNames = TelephonyManager.from(mContext)
395 .getCarrierPackageNamesForIntentAndPhone(
396 new Intent(CarrierService.CONFIG_SERVICE_INTERFACE), phoneId);
397 if (carrierPackageNames != null && carrierPackageNames.size() > 0) {
398 return carrierPackageNames.get(0);
399 } else {
400 return null;
401 }
402 }
403
404 private String getIccIdForPhoneId(int phoneId) {
405 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
406 return null;
407 }
408 Phone phone = PhoneFactory.getPhone(phoneId);
409 if (phone == null) {
410 return null;
411 }
412 return phone.getIccSerialNumber();
413 }
414
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700415 /**
416 * Writes a bundle to an XML file.
417 *
418 * The bundle will be written to a file named after the package name and ICCID, so that it can
419 * be restored later with {@link @restoreConfigFromXml}. The XML output will include the bundle
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700420 * and the current version of the specified package.
421 *
422 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700423 *
424 * @param packageName the name of the package from which we fetched this bundle.
425 * @param iccid the ICCID of the subscription for which this bundle was fetched.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700426 * @param config the bundle to be written. Null will be treated as an empty bundle.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700427 */
428 private void saveConfigToXml(String packageName, String iccid, PersistableBundle config) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700429 if (packageName == null || iccid == null) {
430 loge("Cannot save config with null packageName or iccid.");
431 return;
432 }
433 if (config == null) {
434 config = new PersistableBundle();
435 }
436
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700437 final String version = getPackageVersion(packageName);
438 if (version == null) {
439 loge("Failed to get package version for: " + packageName);
440 return;
441 }
442
443 FileOutputStream outFile = null;
444 try {
445 outFile = new FileOutputStream(
446 new File(mContext.getFilesDir(), getFilenameForConfig(packageName, iccid)));
447 FastXmlSerializer out = new FastXmlSerializer();
448 out.setOutput(outFile, "utf-8");
449 out.startDocument("utf-8", true);
450 out.startTag(null, TAG_DOCUMENT);
451 out.startTag(null, TAG_VERSION);
452 out.text(version);
453 out.endTag(null, TAG_VERSION);
454 out.startTag(null, TAG_BUNDLE);
455 config.saveToXml(out);
456 out.endTag(null, TAG_BUNDLE);
457 out.endTag(null, TAG_DOCUMENT);
458 out.endDocument();
459 out.flush();
460 outFile.close();
461 }
462 catch (IOException e) {
463 loge(e.toString());
464 }
465 catch (XmlPullParserException e) {
466 loge(e.toString());
467 }
468 }
469
470 /**
471 * Reads a bundle from an XML file.
472 *
473 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700474 * config bundle for the given package and ICCID.
475 *
476 * In case of errors, or if the saved config is from a different package version than the
477 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700478 *
479 * @param packageName the name of the package from which we fetched this bundle.
480 * @param iccid the ICCID of the subscription for which this bundle was fetched.
481 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
482 * version does not match, or reading config fails.
483 */
484 private PersistableBundle restoreConfigFromXml(String packageName, String iccid) {
485 final String version = getPackageVersion(packageName);
486 if (version == null) {
487 loge("Failed to get package version for: " + packageName);
488 return null;
489 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700490 if (packageName == null || iccid == null) {
491 loge("Cannot restore config with null packageName or iccid.");
492 return null;
493 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700494
495 PersistableBundle restoredBundle = null;
496 FileInputStream inFile = null;
497 try {
498 inFile = new FileInputStream(
499 new File(mContext.getFilesDir(), getFilenameForConfig(packageName, iccid)));
500 XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
501 parser.setInput(inFile, "utf-8");
502
503 int event;
504 while (((event = parser.next()) != XmlPullParser.END_DOCUMENT)) {
505
506 if (event == XmlPullParser.START_TAG && TAG_VERSION.equals(parser.getName())) {
507 String savedVersion = parser.nextText();
508 if (!version.equals(savedVersion)) {
509 log("Saved version mismatch: " + version + " vs " + savedVersion);
510 break;
511 }
512 }
513
514 if (event == XmlPullParser.START_TAG && TAG_BUNDLE.equals(parser.getName())) {
515 restoredBundle = PersistableBundle.restoreFromXml(parser);
516 }
517 }
518 inFile.close();
519 }
520 catch (FileNotFoundException e) {
521 loge(e.toString());
522 }
523 catch (XmlPullParserException e) {
524 loge(e.toString());
525 }
526 catch (IOException e) {
527 loge(e.toString());
528 }
529
530 return restoredBundle;
531 }
532
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700533 /** Deletes all saved XML files associated with the given package name. */
534 private void deleteConfigForPackage(final String packageName) {
535 File dir = mContext.getFilesDir();
536 File[] packageFiles = dir.listFiles(new FilenameFilter() {
537 public boolean accept(File dir, String filename) {
538 return filename.startsWith("carrierconfig-" + packageName + "-");
539 }
540 });
541 for (File f : packageFiles) {
542 log("deleting " + f.getName());
543 f.delete();
544 }
545 }
546
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700547 /** Builds a canonical file name for a config file. */
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700548 private String getFilenameForConfig(@NonNull String packageName, @NonNull String iccid) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700549 return "carrierconfig-" + packageName + "-" + iccid + ".xml";
550 }
551
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700552 /** Return the current version code of a package, or null if the name is not found. */
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700553 private String getPackageVersion(String packageName) {
554 try {
555 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700556 return Integer.toString(info.versionCode);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700557 } catch (PackageManager.NameNotFoundException e) {
558 return null;
559 }
560 }
561
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700562 /** Read up to date config.
563 *
564 * This reads config bundles for the given phoneId. That means getting the latest bundle from
565 * the default app and a privileged carrier app, if present. This will not bind to an app if we
566 * have a saved config file to use instead.
567 */
568 private void updateConfigForPhoneId(int phoneId) {
569 mHandler.sendMessage(mHandler.obtainMessage(EVENT_FETCH_DEFAULT, phoneId, -1));
570 }
571
572 @Override public
573 @NonNull
574 PersistableBundle getConfigForSubId(int subId) {
Junda Liu1f2b34d2015-06-18 15:26:56 -0700575 mContext.enforceCallingOrSelfPermission(READ_PHONE_STATE, null);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800576 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700577 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800578 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700579 PersistableBundle config = mConfigFromDefaultApp[phoneId];
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700580 if (config != null)
581 retConfig.putAll(config);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800582 config = mConfigFromCarrierApp[phoneId];
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700583 if (config != null)
584 retConfig.putAll(config);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800585 }
586 return retConfig;
587 }
588
589 @Override
Jonathan Basseri86030352015-06-08 12:27:56 -0700590 public void notifyConfigChangedForSubId(int subId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800591 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700592 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800593 log("Ignore invalid phoneId: " + phoneId + " for subId: " + subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700594 return;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800595 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700596 String callingPackageName = mContext.getPackageManager().getNameForUid(
597 Binder.getCallingUid());
598 // TODO: Check that the calling packages is privileged for subId specifically.
599 int privilegeStatus = TelephonyManager.from(mContext).checkCarrierPrivilegesForPackage(
600 callingPackageName);
601 if (privilegeStatus != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
602 throw new SecurityException(
603 "Package is not privileged for subId=" + subId + ": " + callingPackageName);
604 }
605
606 // This method should block until deleting has completed, so that an error which prevents us
607 // from clearing the cache is passed back to the carrier app. With the files successfully
608 // deleted, this can return and we will eventually bind to the carrier app.
609 deleteConfigForPackage(callingPackageName);
610 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800611 }
612
613 @Override
614 public void updateConfigForPhoneId(int phoneId, String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -0700615 mContext.enforceCallingOrSelfPermission(
616 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800617 log("update config for phoneId: " + phoneId + " simState: " + simState);
618 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
619 return;
620 }
621 // requires Java 7 for switch on string.
622 switch (simState) {
623 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
624 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
625 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Junda Liu9f2d2712015-05-15 14:22:45 -0700626 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800627 break;
628 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
629 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700630 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800631 break;
632 }
633 }
634
Junda Liu43d723a2015-05-12 17:23:45 -0700635 @Override
636 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
637 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
638 != PackageManager.PERMISSION_GRANTED) {
639 pw.println("Permission Denial: can't dump carrierconfig from from pid="
640 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
641 return;
642 }
643 pw.println("CarrierConfigLoader: " + this);
644 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
645 pw.println(" Phone Id=" + i);
646 pw.println(" mConfigFromDefaultApp=" + mConfigFromDefaultApp[i]);
647 pw.println(" mConfigFromCarrierApp=" + mConfigFromCarrierApp[i]);
648 }
649 }
650
Zach Johnson36d7aab2015-05-22 15:43:00 -0700651 private class CarrierServiceConnection implements ServiceConnection {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800652 int phoneId;
653 int eventId;
654 IBinder service;
655
Zach Johnson36d7aab2015-05-22 15:43:00 -0700656 public CarrierServiceConnection(int phoneId, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800657 this.phoneId = phoneId;
658 this.eventId = eventId;
659 }
660
661 @Override
662 public void onServiceConnected(ComponentName name, IBinder service) {
663 log("Connected to config app: " + name.flattenToString());
664 this.service = service;
665 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
666 }
667
668 @Override
669 public void onServiceDisconnected(ComponentName name) {
670 this.service = null;
671 }
672 }
673
674 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
675 @Override
676 public void onReceive(Context context, Intent intent) {
677 String action = intent.getAction();
678 log("Receive action: " + action);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700679 switch (action) {
680 case Intent.ACTION_PACKAGE_ADDED:
681 case Intent.ACTION_PACKAGE_CHANGED:
682 case Intent.ACTION_PACKAGE_REMOVED:
683 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
684 String packageName = mContext.getPackageManager().getNameForUid(uid);
685 // We don't have a phoneId for arg1.
686 mHandler.sendMessage(
687 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, packageName));
688 break;
689
690 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800691 }
692 }
693
694 private static void log(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700695 Log.d(LOG_TAG, msg);
696 }
697
698 private static void loge(String msg) {
699 Log.e(LOG_TAG, msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800700 }
701}