blob: 9edd0aa605dcbfddda9707ced4ba0de7c3f378bb [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
Jonathan Basseri65273c82017-07-25 15:08:42 -070019import static android.service.carrier.CarrierService.ICarrierServiceWrapper.KEY_CONFIG_BUNDLE;
20import static android.service.carrier.CarrierService.ICarrierServiceWrapper.RESULT_ERROR;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080021
Jonathan Basseri11f89572015-05-05 11:57:39 -070022import android.annotation.NonNull;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080023import android.content.BroadcastReceiver;
24import android.content.ComponentName;
25import android.content.Context;
26import android.content.Intent;
27import android.content.IntentFilter;
28import android.content.ServiceConnection;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070029import android.content.SharedPreferences;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070030import android.content.pm.PackageInfo;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070031import android.content.pm.PackageManager;
Junda Liu43d723a2015-05-12 17:23:45 -070032import android.os.Binder;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070033import android.os.Build;
Jonathan Basseri65273c82017-07-25 15:08:42 -070034import android.os.Bundle;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080035import android.os.Handler;
36import android.os.IBinder;
37import android.os.Message;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070038import android.os.PersistableBundle;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080039import android.os.RemoteException;
Jonathan Basseri65273c82017-07-25 15:08:42 -070040import android.os.ResultReceiver;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080041import android.os.ServiceManager;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070042import android.preference.PreferenceManager;
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;
chen xudb04c292019-03-26 17:01:15 -070049import android.util.LocalLog;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080050import android.util.Log;
51
52import com.android.internal.telephony.ICarrierConfigLoader;
53import com.android.internal.telephony.IccCardConstants;
54import com.android.internal.telephony.Phone;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080055import com.android.internal.telephony.PhoneFactory;
Nathan Harold48ac0972019-03-13 22:33:01 -070056import com.android.internal.telephony.SubscriptionInfoUpdater;
Jeff Davidsona8e4e242018-03-15 17:16:18 -070057import com.android.internal.telephony.TelephonyPermissions;
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +080058import com.android.internal.util.ArrayUtils;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070059import com.android.internal.util.FastXmlSerializer;
shuoq26a3a4c2016-12-16 11:06:48 -080060import com.android.internal.util.IndentingPrintWriter;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080061
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070062import org.xmlpull.v1.XmlPullParser;
63import org.xmlpull.v1.XmlPullParserException;
64import org.xmlpull.v1.XmlPullParserFactory;
65
66import java.io.File;
Junda Liu43d723a2015-05-12 17:23:45 -070067import java.io.FileDescriptor;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070068import java.io.FileInputStream;
69import java.io.FileNotFoundException;
70import java.io.FileOutputStream;
Jonathan Basseri1f743c92015-05-15 00:19:46 -070071import java.io.FilenameFilter;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070072import java.io.IOException;
Junda Liu43d723a2015-05-12 17:23:45 -070073import java.io.PrintWriter;
shuoq26a3a4c2016-12-16 11:06:48 -080074import java.util.ArrayList;
75import java.util.Arrays;
76import java.util.Collections;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080077import java.util.List;
78
79/**
80 * CarrierConfigLoader binds to privileged carrier apps to fetch carrier config overlays.
Jonathan Basseri6465afd2015-02-25 13:05:57 -080081 */
82
83public class CarrierConfigLoader extends ICarrierConfigLoader.Stub {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070084 private static final String LOG_TAG = "CarrierConfigLoader";
Meng Wang33ad2bc2017-03-16 20:21:20 -070085
86 // Package name for platform carrier config app, bundled with system image.
87 private final String mPlatformCarrierConfigPackage;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080088
89 /** The singleton instance. */
90 private static CarrierConfigLoader sInstance;
91 // The context for phone app, passed from PhoneGlobals.
92 private Context mContext;
93 // Carrier configs from default app, indexed by phoneID.
Jonathan Basseric31f1f32015-05-12 10:13:03 -070094 private PersistableBundle[] mConfigFromDefaultApp;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080095 // Carrier configs from privileged carrier config app, indexed by phoneID.
Jonathan Basseric31f1f32015-05-12 10:13:03 -070096 private PersistableBundle[] mConfigFromCarrierApp;
Hall Liu506724b2018-10-22 18:16:14 -070097 // Carrier configs that are provided via the override test API, indexed by phone ID.
98 private PersistableBundle[] mOverrideConfigs;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080099 // Service connection for binding to config app.
Zach Johnson36d7aab2015-05-22 15:43:00 -0700100 private CarrierServiceConnection[] mServiceConnection;
Junda Liu03aad762017-07-21 13:32:17 -0700101 // Whether we have sent config change bcast for each phone id.
102 private boolean[] mHasSentConfigChange;
Nathan Harold48ac0972019-03-13 22:33:01 -0700103 // SubscriptionInfoUpdater
104 private final SubscriptionInfoUpdater mSubscriptionInfoUpdater;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800105
Nanxi Chen3d670502016-03-17 16:32:09 -0700106 // Broadcast receiver for Boot intents, register intent filter in construtor.
107 private final BroadcastReceiver mBootReceiver = new ConfigLoaderBroadcastReceiver();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800108 // Broadcast receiver for SIM and pkg intents, register intent filter in constructor.
Nanxi Chen3d670502016-03-17 16:32:09 -0700109 private final BroadcastReceiver mPackageReceiver = new ConfigLoaderBroadcastReceiver();
chen xudb04c292019-03-26 17:01:15 -0700110 private final LocalLog mCarrierConfigLoadingLog = new LocalLog(50);
111
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800112
113 // Message codes; see mHandler below.
114 // Request from SubscriptionInfoUpdater when SIM becomes absent or error.
115 private static final int EVENT_CLEAR_CONFIG = 0;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800116 // Has connected to default app.
117 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
118 // Has connected to carrier app.
119 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700120 // Config has been loaded from default app (or cache).
121 private static final int EVENT_FETCH_DEFAULT_DONE = 5;
122 // Config has been loaded from carrier app (or cache).
123 private static final int EVENT_FETCH_CARRIER_DONE = 6;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700124 // Attempt to fetch from default app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700125 private static final int EVENT_DO_FETCH_DEFAULT = 7;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700126 // Attempt to fetch from carrier app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700127 private static final int EVENT_DO_FETCH_CARRIER = 8;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700128 // A package has been installed, uninstalled, or updated.
129 private static final int EVENT_PACKAGE_CHANGED = 9;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700130 // Bind timed out for the default app.
131 private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
132 // Bind timed out for a carrier app.
133 private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700134 // Check if the system fingerprint has changed.
135 private static final int EVENT_CHECK_SYSTEM_UPDATE = 12;
Nanxi Chen3d670502016-03-17 16:32:09 -0700136 // Rerun carrier config binding after system is unlocked.
137 private static final int EVENT_SYSTEM_UNLOCKED = 13;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700138 // Fetching config timed out from the default app.
139 private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14;
140 // Fetching config timed out from a carrier app.
141 private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15;
Nathan Harold48ac0972019-03-13 22:33:01 -0700142 // SubscriptionInfoUpdater has finished updating the sub for the carrier config.
143 private static final int EVENT_SUBSCRIPTION_INFO_UPDATED = 16;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700144
Junda Liu6cb45002016-03-22 17:18:20 -0700145 private static final int BIND_TIMEOUT_MILLIS = 30000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800146
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700147 // Tags used for saving and restoring XML documents.
148 private static final String TAG_DOCUMENT = "carrier_config";
149 private static final String TAG_VERSION = "package_version";
150 private static final String TAG_BUNDLE = "bundle_data";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800151
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700152 // SharedPreferences key for last known build fingerprint.
153 private static final String KEY_FINGERPRINT = "build_fingerprint";
154
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800155 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700156 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700157 // For each phoneId, the event sequence should be:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700158 // fetch default, connected to default, fetch default (async), fetch default done,
159 // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700160 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700161 // If there is a saved config file for either the default app or the carrier app, we skip
162 // binding to the app and go straight from fetch to loaded.
163 //
164 // At any time, at most one connection is active. If events are not in this order, previous
165 // connection will be unbound, so only latest event takes effect.
166 //
167 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
168 // 1. loading from carrier app (even if read from a file)
169 // 2. loading from default app if there is no carrier app (even if read from a file)
170 // 3. clearing config (e.g. due to sim removal)
171 // 4. encountering bind or IPC error
Jonathan Basseri65273c82017-07-25 15:08:42 -0700172 private class ConfigHandler extends Handler {
173 @Override
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800174 public void handleMessage(Message msg) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700175 final int phoneId = msg.arg1;
chen xuc4810972019-04-17 23:44:43 -0700176 logWithLocalLog("mHandler: " + msg.what + " phoneId: " + phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800177 switch (msg.what) {
178 case EVENT_CLEAR_CONFIG:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700179 {
Sooraj Sasindran52119cd2017-10-23 16:24:28 -0700180 /* Ignore clear configuration request if device is being shutdown. */
181 Phone phone = PhoneFactory.getPhone(phoneId);
182 if (phone != null) {
183 if (phone.isShuttingDown()) {
184 break;
185 }
186 }
187
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800188 mConfigFromDefaultApp[phoneId] = null;
189 mConfigFromCarrierApp[phoneId] = null;
190 mServiceConnection[phoneId] = null;
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800191 broadcastConfigChangedIntent(phoneId, false);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800192 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700193 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700194
Nanxi Chen3d670502016-03-17 16:32:09 -0700195 case EVENT_SYSTEM_UNLOCKED:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700196 {
Malcolm Chen978dda52019-10-08 18:15:25 -0700197 for (int i = 0; i < TelephonyManager.from(mContext)
198 .getSupportedModemCount(); ++i) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700199 // When user unlock device, we should only try to send broadcast again if we
200 // have sent it before unlock. This will avoid we try to load carrier config
201 // when SIM is still loading when unlock happens.
Junda Liu03aad762017-07-21 13:32:17 -0700202 if (mHasSentConfigChange[i]) {
203 updateConfigForPhoneId(i);
204 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700205 }
206 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700207 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700208
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700209 case EVENT_PACKAGE_CHANGED:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700210 {
211 final String carrierPackageName = (String) msg.obj;
212 // Only update if there are cached config removed to avoid updating config for
213 // unrelated packages.
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700214 if (clearCachedConfigForPackage(carrierPackageName)) {
Malcolm Chen978dda52019-10-08 18:15:25 -0700215 int numPhones = TelephonyManager.from(mContext)
216 .getSupportedModemCount();
Junda Liu8a8a53a2015-05-15 16:19:57 -0700217 for (int i = 0; i < numPhones; ++i) {
218 updateConfigForPhoneId(i);
219 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700220 }
221 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700222 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700223
Jonathan Basseri65273c82017-07-25 15:08:42 -0700224 case EVENT_DO_FETCH_DEFAULT:
225 {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700226 final PersistableBundle config =
Yuchen Dongc15afed2018-04-26 17:05:22 +0800227 restoreConfigFromXml(mPlatformCarrierConfigPackage, phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700228 if (config != null) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700229 log(
230 "Loaded config from XML. package="
231 + mPlatformCarrierConfigPackage
232 + " phoneId="
233 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700234 mConfigFromDefaultApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700235 Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700236 newMsg.getData().putBoolean("loaded_from_xml", true);
237 mHandler.sendMessage(newMsg);
238 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700239 // No cached config, so fetch it from the default app.
240 if (bindToConfigPackage(
241 mPlatformCarrierConfigPackage,
242 phoneId,
243 EVENT_CONNECTED_TO_DEFAULT)) {
244 sendMessageDelayed(
245 obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700246 BIND_TIMEOUT_MILLIS);
247 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700248 // Send broadcast if bind fails.
Nathan Harold48ac0972019-03-13 22:33:01 -0700249 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700250 // TODO: We *must* call unbindService even if bindService returns false.
251 // (And possibly if SecurityException was thrown.)
chen xudb04c292019-03-26 17:01:15 -0700252 loge("binding to default app: "
253 + mPlatformCarrierConfigPackage + " fails");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700254 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800255 }
256 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700257 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800258
259 case EVENT_CONNECTED_TO_DEFAULT:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700260 {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700261 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700262 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800263 // If new service connection has been created, unbind.
264 if (mServiceConnection[phoneId] != conn || conn.service == null) {
265 mContext.unbindService(conn);
266 break;
267 }
chen xu02581692018-11-11 19:03:44 -0800268 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700269 // ResultReceiver callback will execute in this Handler's thread.
270 final ResultReceiver resultReceiver =
271 new ResultReceiver(this) {
272 @Override
273 public void onReceiveResult(int resultCode, Bundle resultData) {
274 mContext.unbindService(conn);
275 // If new service connection has been created, this is stale.
276 if (mServiceConnection[phoneId] != conn) {
277 loge("Received response for stale request.");
278 return;
279 }
280 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
281 if (resultCode == RESULT_ERROR || resultData == null) {
282 // On error, abort config fetching.
283 loge("Failed to get carrier config");
Nathan Harold48ac0972019-03-13 22:33:01 -0700284 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700285 return;
286 }
287 PersistableBundle config =
288 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Chen Xucd4a6372019-07-29 16:34:52 -0700289 saveConfigToXml(mPlatformCarrierConfigPackage, phoneId,
290 carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700291 mConfigFromDefaultApp[phoneId] = config;
292 sendMessage(
293 obtainMessage(
294 EVENT_FETCH_DEFAULT_DONE, phoneId, -1));
295 }
296 };
297 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800298 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700299 ICarrierService carrierService =
300 ICarrierService.Stub.asInterface(conn.service);
301 carrierService.getCarrierConfig(carrierId, resultReceiver);
chen xudb04c292019-03-26 17:01:15 -0700302 logWithLocalLog("fetch config for default app: "
303 + mPlatformCarrierConfigPackage
304 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700305 } catch (RemoteException e) {
chen xudb04c292019-03-26 17:01:15 -0700306 loge("Failed to get carrier config from default app: " +
307 mPlatformCarrierConfigPackage + " err: " + e.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700308 mContext.unbindService(conn);
309 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800310 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700311 sendMessageDelayed(
312 obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1),
313 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800314 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700315 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800316
Jonathan Basseri930701e2015-06-11 20:56:43 -0700317 case EVENT_BIND_DEFAULT_TIMEOUT:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700318 case EVENT_FETCH_DEFAULT_TIMEOUT:
319 {
chen xudb04c292019-03-26 17:01:15 -0700320 loge("bind/fetch time out from " + mPlatformCarrierConfigPackage);
chen xuf60b3162019-05-01 21:31:05 -0700321 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
322 // If we attempted to bind to the app, but the service connection is null due to
323 // the race condition that clear config event happens before bind/fetch complete
324 // then config was cleared while we were waiting and we should not continue.
325 if (mServiceConnection[phoneId] != null) {
326 // If a ResponseReceiver callback is in the queue when this happens, we will
327 // unbind twice and throw an exception.
328 mContext.unbindService(mServiceConnection[phoneId]);
329 broadcastConfigChangedIntent(phoneId);
330 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700331 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700332 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700333 }
Jonathan Basseri930701e2015-06-11 20:56:43 -0700334
Jonathan Basseri65273c82017-07-25 15:08:42 -0700335 case EVENT_FETCH_DEFAULT_DONE:
336 {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700337 // If we attempted to bind to the app, but the service connection is null, then
338 // config was cleared while we were waiting and we should not continue.
339 if (!msg.getData().getBoolean("loaded_from_xml", false)
340 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800341 break;
342 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700343 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700344 if (carrierPackageName != null) {
345 log("Found carrier config app: " + carrierPackageName);
Jordan Liu38a614c2019-03-20 15:01:17 -0700346 sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId, -1));
Jonathan Basserib919e932015-05-27 16:53:08 +0000347 } else {
Nathan Harold48ac0972019-03-13 22:33:01 -0700348 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700349 }
350 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700351 }
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700352
Jonathan Basseri65273c82017-07-25 15:08:42 -0700353 case EVENT_DO_FETCH_CARRIER:
354 {
355 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700356 final PersistableBundle config =
Yuchen Dongc15afed2018-04-26 17:05:22 +0800357 restoreConfigFromXml(carrierPackageName, phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700358 if (config != null) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700359 log(
360 "Loaded config from XML. package="
361 + carrierPackageName
362 + " phoneId="
363 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700364 mConfigFromCarrierApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700365 Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700366 newMsg.getData().putBoolean("loaded_from_xml", true);
367 sendMessage(newMsg);
368 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700369 // No cached config, so fetch it from a carrier app.
Jonathan Basseri40473a52015-08-14 14:36:29 -0700370 if (carrierPackageName != null
Jonathan Basseri65273c82017-07-25 15:08:42 -0700371 && bindToConfigPackage(
372 carrierPackageName,
373 phoneId,
374 EVENT_CONNECTED_TO_CARRIER)) {
375 sendMessageDelayed(
376 obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700377 BIND_TIMEOUT_MILLIS);
378 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700379 // Send broadcast if bind fails.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700380 broadcastConfigChangedIntent(phoneId);
chen xudb04c292019-03-26 17:01:15 -0700381 loge("bind to carrier app: " + carrierPackageName + " fails");
Nathan Harold48ac0972019-03-13 22:33:01 -0700382 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700383 }
384 }
385 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700386 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700387
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800388 case EVENT_CONNECTED_TO_CARRIER:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700389 {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700390 removeMessages(EVENT_BIND_CARRIER_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700391 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800392 // If new service connection has been created, unbind.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700393 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800394 mContext.unbindService(conn);
395 break;
396 }
chen xu02581692018-11-11 19:03:44 -0800397 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700398 // ResultReceiver callback will execute in this Handler's thread.
399 final ResultReceiver resultReceiver =
400 new ResultReceiver(this) {
401 @Override
402 public void onReceiveResult(int resultCode, Bundle resultData) {
403 mContext.unbindService(conn);
404 // If new service connection has been created, this is stale.
405 if (mServiceConnection[phoneId] != conn) {
406 loge("Received response for stale request.");
407 return;
408 }
409 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
410 if (resultCode == RESULT_ERROR || resultData == null) {
411 // On error, abort config fetching.
chen xudb04c292019-03-26 17:01:15 -0700412 loge("Failed to get carrier config from carrier app: "
413 + getCarrierPackageForPhoneId(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700414 broadcastConfigChangedIntent(phoneId);
Nathan Harold48ac0972019-03-13 22:33:01 -0700415 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700416 return;
417 }
418 PersistableBundle config =
419 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Chen Xucd4a6372019-07-29 16:34:52 -0700420 saveConfigToXml(getCarrierPackageForPhoneId(phoneId), phoneId,
421 carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700422 mConfigFromCarrierApp[phoneId] = config;
423 sendMessage(
424 obtainMessage(
425 EVENT_FETCH_CARRIER_DONE, phoneId, -1));
426 }
427 };
428 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800429 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700430 ICarrierService carrierService =
431 ICarrierService.Stub.asInterface(conn.service);
432 carrierService.getCarrierConfig(carrierId, resultReceiver);
chen xudb04c292019-03-26 17:01:15 -0700433 logWithLocalLog("fetch config for carrier app: "
434 + getCarrierPackageForPhoneId(phoneId)
435 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700436 } catch (RemoteException e) {
437 loge("Failed to get carrier config: " + e.toString());
438 mContext.unbindService(conn);
439 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800440 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700441 sendMessageDelayed(
442 obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1),
443 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800444 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700445 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800446
Jonathan Basseri930701e2015-06-11 20:56:43 -0700447 case EVENT_BIND_CARRIER_TIMEOUT:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700448 case EVENT_FETCH_CARRIER_TIMEOUT:
449 {
chen xudb04c292019-03-26 17:01:15 -0700450 loge("bind/fetch from carrier app timeout");
chen xuf60b3162019-05-01 21:31:05 -0700451 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
452 // If we attempted to bind to the app, but the service connection is null due to
453 // the race condition that clear config event happens before bind/fetch complete
454 // then config was cleared while we were waiting and we should not continue.
455 if (mServiceConnection[phoneId] != null) {
456 // If a ResponseReceiver callback is in the queue when this happens, we will
457 // unbind twice and throw an exception.
458 mContext.unbindService(mServiceConnection[phoneId]);
459 broadcastConfigChangedIntent(phoneId);
460 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700461 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700462 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700463 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700464 case EVENT_FETCH_CARRIER_DONE:
465 {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700466 // If we attempted to bind to the app, but the service connection is null, then
467 // config was cleared while we were waiting and we should not continue.
468 if (!msg.getData().getBoolean("loaded_from_xml", false)
469 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800470 break;
471 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700472 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800473 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700474 }
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700475
476 case EVENT_CHECK_SYSTEM_UPDATE:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700477 {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700478 SharedPreferences sharedPrefs =
479 PreferenceManager.getDefaultSharedPreferences(mContext);
480 final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
481 if (!Build.FINGERPRINT.equals(lastFingerprint)) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700482 log(
483 "Build fingerprint changed. old: "
484 + lastFingerprint
485 + " new: "
486 + Build.FINGERPRINT);
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700487 clearCachedConfigForPackage(null);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700488 sharedPrefs
489 .edit()
490 .putString(KEY_FINGERPRINT, Build.FINGERPRINT)
491 .apply();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700492 }
493 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700494 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700495
496 case EVENT_SUBSCRIPTION_INFO_UPDATED:
497 broadcastConfigChangedIntent(phoneId);
498 break;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800499 }
500 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700501 }
502
503 private final Handler mHandler;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800504
505 /**
506 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
507 * receiver for relevant events.
508 */
509 private CarrierConfigLoader(Context context) {
510 mContext = context;
Meng Wang33ad2bc2017-03-16 20:21:20 -0700511 mPlatformCarrierConfigPackage =
512 mContext.getString(R.string.platform_carrier_config_package);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700513 mHandler = new ConfigHandler();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800514
Nanxi Chen3d670502016-03-17 16:32:09 -0700515 IntentFilter bootFilter = new IntentFilter();
516 bootFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
517 context.registerReceiver(mBootReceiver, bootFilter);
518
Junda Liu8a8a53a2015-05-15 16:19:57 -0700519 // Register for package updates. Update app or uninstall app update will have all 3 intents,
520 // in the order or removed, added, replaced, all with extra_replace set to true.
521 IntentFilter pkgFilter = new IntentFilter();
522 pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
523 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
524 pkgFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
525 pkgFilter.addDataScheme("package");
Jordan Liu5ca24762019-07-10 12:51:09 -0700526 context.registerReceiver(mPackageReceiver, pkgFilter);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800527
Malcolm Chen978dda52019-10-08 18:15:25 -0700528 int numPhones = TelephonyManager.from(context).getSupportedModemCount();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700529 mConfigFromDefaultApp = new PersistableBundle[numPhones];
530 mConfigFromCarrierApp = new PersistableBundle[numPhones];
Hall Liu506724b2018-10-22 18:16:14 -0700531 mOverrideConfigs = new PersistableBundle[numPhones];
Zach Johnson36d7aab2015-05-22 15:43:00 -0700532 mServiceConnection = new CarrierServiceConnection[numPhones];
Junda Liu03aad762017-07-21 13:32:17 -0700533 mHasSentConfigChange = new boolean[numPhones];
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800534 // Make this service available through ServiceManager.
535 ServiceManager.addService(Context.CARRIER_CONFIG_SERVICE, this);
536 log("CarrierConfigLoader has started");
Nathan Harold48ac0972019-03-13 22:33:01 -0700537 mSubscriptionInfoUpdater = PhoneFactory.getSubscriptionInfoUpdater();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700538 mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800539 }
540
541 /**
542 * Initialize the singleton CarrierConfigLoader instance.
543 *
544 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
545 */
546 /* package */
547 static CarrierConfigLoader init(Context context) {
548 synchronized (CarrierConfigLoader.class) {
549 if (sInstance == null) {
550 sInstance = new CarrierConfigLoader(context);
551 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700552 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800553 }
554 return sInstance;
555 }
556 }
557
Nathan Harold48ac0972019-03-13 22:33:01 -0700558 private void notifySubscriptionInfoUpdater(int phoneId) {
559 String configPackagename;
560 PersistableBundle configToSend;
561 int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
562 // Prefer the carrier privileged carrier app, but if there is not one, use the platform
563 // default carrier app.
564 if (mConfigFromCarrierApp[phoneId] != null) {
565 configPackagename = getCarrierPackageForPhoneId(phoneId);
566 configToSend = mConfigFromCarrierApp[phoneId];
567 } else {
568 configPackagename = mPlatformCarrierConfigPackage;
569 configToSend = mConfigFromDefaultApp[phoneId];
570 }
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700571
572 // mOverrideConfigs is for testing. And it will override current configs.
573 PersistableBundle config = mOverrideConfigs[phoneId];
574 if (config != null) {
Torbjorn Eklund1050cb02018-11-16 14:05:38 +0100575 configToSend = new PersistableBundle(configToSend);
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700576 configToSend.putAll(config);
577 }
578
Nathan Harold48ac0972019-03-13 22:33:01 -0700579 mSubscriptionInfoUpdater.updateSubscriptionByCarrierConfigAndNotifyComplete(
580 phoneId, configPackagename, configToSend,
581 mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1));
582 }
583
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800584 private void broadcastConfigChangedIntent(int phoneId) {
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800585 broadcastConfigChangedIntent(phoneId, true);
586 }
587
588 private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800589 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800590 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
chen xuf9db49f2019-03-31 23:04:42 -0700591 Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND |
592 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800593 if (addSubIdExtra) {
594 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
595 int[] subIds = SubscriptionManager.getSubId(phoneId);
596 if (!ArrayUtils.isEmpty(subIds)) {
597 TelephonyManager telMgr = TelephonyManager.from(mContext)
598 .createForSubscriptionId(subIds[0]);
599 simApplicationState = telMgr.getSimApplicationState();
600 }
601 // Include subId/carrier id extra only if SIM records are loaded
602 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
603 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
604 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
605 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID,
606 getSpecificCarrierIdForPhoneId(phoneId));
607 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, getCarrierIdForPhoneId(phoneId));
608 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800609 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800610 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Jack Yuf1661322019-04-06 00:37:23 -0700611 log("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
Jordan Liu5ca24762019-07-10 12:51:09 -0700612 mContext.sendBroadcast(intent);
Junda Liu03aad762017-07-21 13:32:17 -0700613 mHasSentConfigChange[phoneId] = true;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800614 }
615
616 /** Binds to the default or carrier config app. */
617 private boolean bindToConfigPackage(String pkgName, int phoneId, int eventId) {
chen xudb04c292019-03-26 17:01:15 -0700618 logWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700619 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700620 carrierService.setPackage(pkgName);
621 mServiceConnection[phoneId] = new CarrierServiceConnection(phoneId, eventId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800622 try {
Zach Johnson36d7aab2015-05-22 15:43:00 -0700623 return mContext.bindService(carrierService, mServiceConnection[phoneId],
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800624 Context.BIND_AUTO_CREATE);
625 } catch (SecurityException ex) {
626 return false;
627 }
628 }
629
chen xu02581692018-11-11 19:03:44 -0800630 private CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800631 String mcc = "";
632 String mnc = "";
633 String imsi = "";
634 String gid1 = "";
635 String gid2 = "";
636 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
637 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800638 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800639 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700640 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
641 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800642 mcc = simOperator.substring(0, 3);
643 mnc = simOperator.substring(3);
644 }
645 Phone phone = PhoneFactory.getPhone(phoneId);
646 if (phone != null) {
647 imsi = phone.getSubscriberId();
648 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700649 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800650 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800651 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800652 }
chen xua31f22b2019-03-06 15:28:50 -0800653 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800654 }
655
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700656 /** Returns the package name of a priveleged carrier app, or null if there is none. */
657 private String getCarrierPackageForPhoneId(int phoneId) {
658 List<String> carrierPackageNames = TelephonyManager.from(mContext)
659 .getCarrierPackageNamesForIntentAndPhone(
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700660 new Intent(CarrierService.CARRIER_SERVICE_INTERFACE), phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700661 if (carrierPackageNames != null && carrierPackageNames.size() > 0) {
662 return carrierPackageNames.get(0);
663 } else {
664 return null;
665 }
666 }
667
668 private String getIccIdForPhoneId(int phoneId) {
669 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
670 return null;
671 }
672 Phone phone = PhoneFactory.getPhone(phoneId);
673 if (phone == null) {
674 return null;
675 }
676 return phone.getIccSerialNumber();
677 }
678
chen xu02581692018-11-11 19:03:44 -0800679 /**
chen xua31f22b2019-03-06 15:28:50 -0800680 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800681 */
chen xua31f22b2019-03-06 15:28:50 -0800682 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700683 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800684 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700685 }
686 Phone phone = PhoneFactory.getPhone(phoneId);
687 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800688 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700689 }
chen xua31f22b2019-03-06 15:28:50 -0800690 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800691 }
692
693 /**
694 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
695 */
696 private int getCarrierIdForPhoneId(int phoneId) {
697 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
698 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700699 }
chen xu02581692018-11-11 19:03:44 -0800700 Phone phone = PhoneFactory.getPhone(phoneId);
701 if (phone == null) {
702 return TelephonyManager.UNKNOWN_CARRIER_ID;
703 }
704 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700705 }
706
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700707 /**
708 * Writes a bundle to an XML file.
709 *
chen xu02581692018-11-11 19:03:44 -0800710 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -0800711 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -0800712 * should have a single copy of XML file named after carrier id. However, it's still possible
713 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
714 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
715 * carrier while iccid remains the same.
716 *
717 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
718 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700719 *
720 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700721 *
722 * @param packageName the name of the package from which we fetched this bundle.
Yuchen Dongc15afed2018-04-26 17:05:22 +0800723 * @param phoneId the phone ID.
Chen Xucd4a6372019-07-29 16:34:52 -0700724 * @param carrierId contains all carrier-identifying information.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700725 * @param config the bundle to be written. Null will be treated as an empty bundle.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700726 */
Chen Xucd4a6372019-07-29 16:34:52 -0700727 private void saveConfigToXml(String packageName, int phoneId, CarrierIdentifier carrierId,
728 PersistableBundle config) {
Yuchen Dongc15afed2018-04-26 17:05:22 +0800729 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
730 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700731 loge("Skip save config because SIM records are not loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800732 return;
733 }
734
735 final String iccid = getIccIdForPhoneId(phoneId);
Chen Xucd4a6372019-07-29 16:34:52 -0700736 final int cid = carrierId.getSpecificCarrierId();
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700737 if (packageName == null || iccid == null) {
738 loge("Cannot save config with null packageName or iccid.");
739 return;
740 }
Junda Liu02596502016-11-15 13:46:43 -0800741 // b/32668103 Only save to file if config isn't empty.
742 // In case of failure, not caching an empty bundle will
743 // try loading config again on next power on or sim loaded.
744 // Downside is for genuinely empty bundle, will bind and load
745 // on every power on.
746 if (config == null || config.isEmpty()) {
747 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700748 }
749
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700750 final String version = getPackageVersion(packageName);
751 if (version == null) {
752 loge("Failed to get package version for: " + packageName);
753 return;
754 }
755
chen xudb04c292019-03-26 17:01:15 -0700756 logWithLocalLog("save config to xml, packagename: " + packageName + " phoneId: " + phoneId);
757
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700758 FileOutputStream outFile = null;
759 try {
760 outFile = new FileOutputStream(
chen xu02581692018-11-11 19:03:44 -0800761 new File(mContext.getFilesDir(),
762 getFilenameForConfig(packageName, iccid, cid)));
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700763 FastXmlSerializer out = new FastXmlSerializer();
764 out.setOutput(outFile, "utf-8");
765 out.startDocument("utf-8", true);
766 out.startTag(null, TAG_DOCUMENT);
767 out.startTag(null, TAG_VERSION);
768 out.text(version);
769 out.endTag(null, TAG_VERSION);
770 out.startTag(null, TAG_BUNDLE);
771 config.saveToXml(out);
772 out.endTag(null, TAG_BUNDLE);
773 out.endTag(null, TAG_DOCUMENT);
774 out.endDocument();
775 out.flush();
776 outFile.close();
777 }
778 catch (IOException e) {
779 loge(e.toString());
780 }
781 catch (XmlPullParserException e) {
782 loge(e.toString());
783 }
784 }
785
786 /**
787 * Reads a bundle from an XML file.
788 *
789 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +0800790 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700791 *
792 * In case of errors, or if the saved config is from a different package version than the
793 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700794 *
795 * @param packageName the name of the package from which we fetched this bundle.
Yuchen Dongc15afed2018-04-26 17:05:22 +0800796 * @param phoneId the phone ID.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700797 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
798 * version does not match, or reading config fails.
799 */
Yuchen Dongc15afed2018-04-26 17:05:22 +0800800 private PersistableBundle restoreConfigFromXml(String packageName, int phoneId) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700801 final String version = getPackageVersion(packageName);
802 if (version == null) {
803 loge("Failed to get package version for: " + packageName);
804 return null;
805 }
Yuchen Dongc15afed2018-04-26 17:05:22 +0800806 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
807 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700808 loge("Skip restoring config because SIM records are not yet loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800809 return null;
810 }
811
812 final String iccid = getIccIdForPhoneId(phoneId);
chen xua31f22b2019-03-06 15:28:50 -0800813 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700814 if (packageName == null || iccid == null) {
815 loge("Cannot restore config with null packageName or iccid.");
816 return null;
817 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700818
819 PersistableBundle restoredBundle = null;
820 FileInputStream inFile = null;
821 try {
822 inFile = new FileInputStream(
chen xu02581692018-11-11 19:03:44 -0800823 new File(mContext.getFilesDir(),
824 getFilenameForConfig(packageName, iccid, cid)));
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700825 XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
826 parser.setInput(inFile, "utf-8");
827
828 int event;
829 while (((event = parser.next()) != XmlPullParser.END_DOCUMENT)) {
830
831 if (event == XmlPullParser.START_TAG && TAG_VERSION.equals(parser.getName())) {
832 String savedVersion = parser.nextText();
833 if (!version.equals(savedVersion)) {
chen xudb04c292019-03-26 17:01:15 -0700834 loge("Saved version mismatch: " + version + " vs " + savedVersion);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700835 break;
836 }
837 }
838
839 if (event == XmlPullParser.START_TAG && TAG_BUNDLE.equals(parser.getName())) {
840 restoredBundle = PersistableBundle.restoreFromXml(parser);
841 }
842 }
843 inFile.close();
844 }
845 catch (FileNotFoundException e) {
846 loge(e.toString());
847 }
848 catch (XmlPullParserException e) {
849 loge(e.toString());
850 }
851 catch (IOException e) {
852 loge(e.toString());
853 }
854
855 return restoredBundle;
856 }
857
Junda Liu8a8a53a2015-05-15 16:19:57 -0700858 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700859 * Clears cached carrier config.
860 * This deletes all saved XML files associated with the given package name. If packageName is
861 * null, then it deletes all saved XML files.
862 *
863 * @param packageName the name of a carrier package, or null if all cached config should be
864 * cleared.
865 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -0700866 */
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700867 private boolean clearCachedConfigForPackage(final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700868 File dir = mContext.getFilesDir();
869 File[] packageFiles = dir.listFiles(new FilenameFilter() {
870 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700871 if (packageName != null) {
872 return filename.startsWith("carrierconfig-" + packageName + "-");
873 } else {
874 return filename.startsWith("carrierconfig-");
875 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700876 }
877 });
Junda Liu8a8a53a2015-05-15 16:19:57 -0700878 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700879 for (File f : packageFiles) {
880 log("deleting " + f.getName());
881 f.delete();
882 }
Junda Liu8a8a53a2015-05-15 16:19:57 -0700883 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700884 }
885
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700886 /** Builds a canonical file name for a config file. */
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700887 private String getFilenameForConfig(@NonNull String packageName, @NonNull String iccid,
chen xu02581692018-11-11 19:03:44 -0800888 int cid) {
889 // the same carrier should have a single copy of XML file named after carrier id.
890 // However, it's still possible that platform doesn't recognize the current sim carrier,
891 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
892 // cases SIM OTA resolves to different carrier while iccid remains the same.
893 return "carrierconfig-" + packageName + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700894 }
895
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700896 /** Return the current version code of a package, or null if the name is not found. */
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700897 private String getPackageVersion(String packageName) {
898 try {
899 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -0800900 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700901 } catch (PackageManager.NameNotFoundException e) {
902 return null;
903 }
904 }
905
Jonathan Basseri65273c82017-07-25 15:08:42 -0700906 /**
907 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700908 *
909 * This reads config bundles for the given phoneId. That means getting the latest bundle from
910 * the default app and a privileged carrier app, if present. This will not bind to an app if we
911 * have a saved config file to use instead.
912 */
913 private void updateConfigForPhoneId(int phoneId) {
Junda Liu8a8a53a2015-05-15 16:19:57 -0700914 // Clear in-memory cache for carrier app config, so when carrier app gets uninstalled, no
915 // stale config is left.
916 if (mConfigFromCarrierApp[phoneId] != null &&
917 getCarrierPackageForPhoneId(phoneId) == null) {
918 mConfigFromCarrierApp[phoneId] = null;
919 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700920 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700921 }
922
Hall Liu506724b2018-10-22 18:16:14 -0700923 @Override
924 public @NonNull PersistableBundle getConfigForSubId(int subId, String callingPackage) {
Malcolm Chen6d3d6b32018-03-01 13:58:03 -0800925 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
926 mContext, subId, callingPackage, "getCarrierConfig")) {
927 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -0700928 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -0700929
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800930 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700931 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800932 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700933 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800934 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700935 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -0800936 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
937 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800938 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800939 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700940 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -0800941 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
942 }
Hall Liu506724b2018-10-22 18:16:14 -0700943 config = mOverrideConfigs[phoneId];
944 if (config != null) {
945 retConfig.putAll(config);
946 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
947 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800948 }
949 return retConfig;
950 }
951
952 @Override
Hall Liu506724b2018-10-22 18:16:14 -0700953 public void overrideConfig(int subscriptionId, PersistableBundle overrides) {
954 mContext.enforceCallingOrSelfPermission(
955 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Brad Ebingerf6281ad2019-04-25 11:02:04 -0700956 //TODO: Also check for SHELL UID to restrict this method to testing only (b/131326259)
Hall Liu506724b2018-10-22 18:16:14 -0700957 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
958 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
959 log("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
960 return;
961 }
962
963 if (overrides == null) {
964 mOverrideConfigs[phoneId] = new PersistableBundle();
Brad Ebinger725edf62019-05-15 18:24:13 -0700965 } else if (mOverrideConfigs[phoneId] == null) {
Hall Liu506724b2018-10-22 18:16:14 -0700966 mOverrideConfigs[phoneId] = overrides;
967 } else {
968 mOverrideConfigs[phoneId].putAll(overrides);
969 }
Brad Ebingerc9d1fa12019-04-17 15:21:18 -0700970
971 notifySubscriptionInfoUpdater(phoneId);
Hall Liu506724b2018-10-22 18:16:14 -0700972 }
973
974 @Override
Jonathan Basseri86030352015-06-08 12:27:56 -0700975 public void notifyConfigChangedForSubId(int subId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800976 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700977 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800978 log("Ignore invalid phoneId: " + phoneId + " for subId: " + subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700979 return;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800980 }
Jordan Liud7d57fe2019-04-16 12:29:43 -0700981
982 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -0700983 // system privileged app with MODIFY_PHONE_STATE permission.
Jordan Liud7d57fe2019-04-16 12:29:43 -0700984 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext, subId,
985 "Require carrier privileges or MODIFY_PHONE_STATE permission.");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700986
987 // This method should block until deleting has completed, so that an error which prevents us
988 // from clearing the cache is passed back to the carrier app. With the files successfully
989 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -0700990 String callingPackageName = mContext.getPackageManager().getNameForUid(
991 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700992 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700993 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800994 }
995
996 @Override
997 public void updateConfigForPhoneId(int phoneId, String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -0700998 mContext.enforceCallingOrSelfPermission(
999 android.Manifest.permission.MODIFY_PHONE_STATE, null);
chen xuc4810972019-04-17 23:44:43 -07001000 logWithLocalLog("update config for phoneId: " + phoneId + " simState: " + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001001 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1002 return;
1003 }
1004 // requires Java 7 for switch on string.
1005 switch (simState) {
1006 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1007 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001008 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001009 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Junda Liu9f2d2712015-05-15 14:22:45 -07001010 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001011 break;
1012 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1013 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001014 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001015 break;
1016 }
1017 }
1018
Junda Liu43d723a2015-05-12 17:23:45 -07001019 @Override
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001020 public String getDefaultCarrierServicePackageName() {
1021 return mPlatformCarrierConfigPackage;
1022 }
1023
1024 @Override
Junda Liu43d723a2015-05-12 17:23:45 -07001025 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1026 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1027 != PackageManager.PERMISSION_GRANTED) {
1028 pw.println("Permission Denial: can't dump carrierconfig from from pid="
1029 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1030 return;
1031 }
1032 pw.println("CarrierConfigLoader: " + this);
1033 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
shuoq26a3a4c2016-12-16 11:06:48 -08001034 pw.println("Phone Id = " + i);
1035 // display default values in CarrierConfigManager
1036 printConfig(CarrierConfigManager.getDefaultConfig(), pw,
1037 "Default Values from CarrierConfigManager");
1038 pw.println("");
1039 // display ConfigFromDefaultApp
1040 printConfig(mConfigFromDefaultApp[i], pw, "mConfigFromDefaultApp");
1041 pw.println("");
1042 // display ConfigFromCarrierApp
1043 printConfig(mConfigFromCarrierApp[i], pw, "mConfigFromCarrierApp");
Hall Liu506724b2018-10-22 18:16:14 -07001044 pw.println("");
1045 printConfig(mOverrideConfigs[i], pw, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001046 }
chen xudb04c292019-03-26 17:01:15 -07001047
1048 pw.println("CarrierConfigLoadingLog=");
1049 mCarrierConfigLoadingLog.dump(fd, pw, args);
shuoq26a3a4c2016-12-16 11:06:48 -08001050 }
1051
1052 private void printConfig(PersistableBundle configApp, PrintWriter pw, String name) {
1053 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
1054 if (configApp == null) {
1055 indentPW.increaseIndent();
1056 indentPW.println(name + " : null ");
1057 return;
1058 }
1059 indentPW.increaseIndent();
1060 indentPW.println(name + " : ");
1061 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1062 Collections.sort(sortedKeys);
1063 indentPW.increaseIndent();
1064 indentPW.increaseIndent();
1065 for (String key : sortedKeys) {
1066 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1067 indentPW.println(key + " = " +
1068 Arrays.toString((Object[]) configApp.get(key)));
1069 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1070 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1071 } else {
1072 indentPW.println(key + " = " + configApp.get(key));
1073 }
Junda Liu43d723a2015-05-12 17:23:45 -07001074 }
1075 }
1076
Zach Johnson36d7aab2015-05-22 15:43:00 -07001077 private class CarrierServiceConnection implements ServiceConnection {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001078 int phoneId;
1079 int eventId;
1080 IBinder service;
1081
Zach Johnson36d7aab2015-05-22 15:43:00 -07001082 public CarrierServiceConnection(int phoneId, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001083 this.phoneId = phoneId;
1084 this.eventId = eventId;
1085 }
1086
1087 @Override
1088 public void onServiceConnected(ComponentName name, IBinder service) {
1089 log("Connected to config app: " + name.flattenToString());
1090 this.service = service;
1091 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1092 }
1093
1094 @Override
1095 public void onServiceDisconnected(ComponentName name) {
1096 this.service = null;
1097 }
1098 }
1099
1100 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1101 @Override
1102 public void onReceive(Context context, Intent intent) {
1103 String action = intent.getAction();
Junda Liu8a8a53a2015-05-15 16:19:57 -07001104 boolean replace = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1105 // If replace is true, only care ACTION_PACKAGE_REPLACED.
1106 if (replace && !Intent.ACTION_PACKAGE_REPLACED.equals(action))
1107 return;
1108
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001109 switch (action) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001110 case Intent.ACTION_BOOT_COMPLETED:
1111 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1112 break;
1113
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001114 case Intent.ACTION_PACKAGE_ADDED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001115 case Intent.ACTION_PACKAGE_REMOVED:
Junda Liu8a8a53a2015-05-15 16:19:57 -07001116 case Intent.ACTION_PACKAGE_REPLACED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001117 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1118 String packageName = mContext.getPackageManager().getNameForUid(uid);
Junda Liu8a8a53a2015-05-15 16:19:57 -07001119 if (packageName != null) {
1120 // We don't have a phoneId for arg1.
1121 mHandler.sendMessage(
1122 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, packageName));
1123 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001124 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001125 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001126 }
1127 }
1128
chen xudb04c292019-03-26 17:01:15 -07001129 private void log(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001130 Log.d(LOG_TAG, msg);
1131 }
1132
chen xudb04c292019-03-26 17:01:15 -07001133 private void logWithLocalLog(String msg) {
1134 Log.d(LOG_TAG, msg);
1135 mCarrierConfigLoadingLog.log(msg);
1136 }
1137
1138 private void loge(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001139 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07001140 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001141 }
1142}