blob: fe9560b26a00f5df8812114fc53a51eb3ef68a7a [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();
Jack Yu37d219a2019-10-17 15:19:48 -0700110 private final LocalLog mCarrierConfigLoadingLog = new LocalLog(100);
chen xudb04c292019-03-26 17:01:15 -0700111
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;
Malcolm Chen5acb9cf2019-10-24 19:55:44 -0700144 // Multi-SIM config changed.
145 private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 17;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700146
Junda Liu6cb45002016-03-22 17:18:20 -0700147 private static final int BIND_TIMEOUT_MILLIS = 30000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800148
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700149 // Tags used for saving and restoring XML documents.
150 private static final String TAG_DOCUMENT = "carrier_config";
151 private static final String TAG_VERSION = "package_version";
152 private static final String TAG_BUNDLE = "bundle_data";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800153
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700154 // SharedPreferences key for last known build fingerprint.
155 private static final String KEY_FINGERPRINT = "build_fingerprint";
156
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800157 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700158 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700159 // For each phoneId, the event sequence should be:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700160 // fetch default, connected to default, fetch default (async), fetch default done,
161 // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700162 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700163 // If there is a saved config file for either the default app or the carrier app, we skip
164 // binding to the app and go straight from fetch to loaded.
165 //
166 // At any time, at most one connection is active. If events are not in this order, previous
167 // connection will be unbound, so only latest event takes effect.
168 //
169 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
170 // 1. loading from carrier app (even if read from a file)
171 // 2. loading from default app if there is no carrier app (even if read from a file)
172 // 3. clearing config (e.g. due to sim removal)
173 // 4. encountering bind or IPC error
Jonathan Basseri65273c82017-07-25 15:08:42 -0700174 private class ConfigHandler extends Handler {
175 @Override
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800176 public void handleMessage(Message msg) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700177 final int phoneId = msg.arg1;
chen xuc4810972019-04-17 23:44:43 -0700178 logWithLocalLog("mHandler: " + msg.what + " phoneId: " + phoneId);
Malcolm Chen5acb9cf2019-10-24 19:55:44 -0700179 if (!SubscriptionManager.isValidPhoneId(phoneId)
180 && msg.what != EVENT_MULTI_SIM_CONFIG_CHANGED) {
181 return;
182 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800183 switch (msg.what) {
184 case EVENT_CLEAR_CONFIG:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700185 {
Malcolm Chen5acb9cf2019-10-24 19:55:44 -0700186 clearConfigForPhone(phoneId, true);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800187 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700188 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700189
Nanxi Chen3d670502016-03-17 16:32:09 -0700190 case EVENT_SYSTEM_UNLOCKED:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700191 {
Malcolm Chen5acb9cf2019-10-24 19:55:44 -0700192 for (int i = 0; i < TelephonyManager.from(mContext).getActiveModemCount();
193 ++i) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700194 // When user unlock device, we should only try to send broadcast again if we
195 // have sent it before unlock. This will avoid we try to load carrier config
196 // when SIM is still loading when unlock happens.
Junda Liu03aad762017-07-21 13:32:17 -0700197 if (mHasSentConfigChange[i]) {
Jack Yu37d219a2019-10-17 15:19:48 -0700198 logWithLocalLog("System unlocked");
Junda Liu03aad762017-07-21 13:32:17 -0700199 updateConfigForPhoneId(i);
200 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700201 }
202 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700203 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700204
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700205 case EVENT_PACKAGE_CHANGED:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700206 {
207 final String carrierPackageName = (String) msg.obj;
208 // Only update if there are cached config removed to avoid updating config for
209 // unrelated packages.
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700210 if (clearCachedConfigForPackage(carrierPackageName)) {
Malcolm Chen5acb9cf2019-10-24 19:55:44 -0700211 int numPhones = TelephonyManager.from(mContext).getActiveModemCount();
Junda Liu8a8a53a2015-05-15 16:19:57 -0700212 for (int i = 0; i < numPhones; ++i) {
Jack Yu37d219a2019-10-17 15:19:48 -0700213 logWithLocalLog("Package changed: " + carrierPackageName
214 + ", phone=" + i);
Junda Liu8a8a53a2015-05-15 16:19:57 -0700215 updateConfigForPhoneId(i);
216 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700217 }
218 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700219 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700220
Jonathan Basseri65273c82017-07-25 15:08:42 -0700221 case EVENT_DO_FETCH_DEFAULT:
222 {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700223 final PersistableBundle config =
Yuchen Dongc15afed2018-04-26 17:05:22 +0800224 restoreConfigFromXml(mPlatformCarrierConfigPackage, phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700225 if (config != null) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700226 log(
227 "Loaded config from XML. package="
228 + mPlatformCarrierConfigPackage
229 + " phoneId="
230 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700231 mConfigFromDefaultApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700232 Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700233 newMsg.getData().putBoolean("loaded_from_xml", true);
234 mHandler.sendMessage(newMsg);
235 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700236 // No cached config, so fetch it from the default app.
237 if (bindToConfigPackage(
238 mPlatformCarrierConfigPackage,
239 phoneId,
240 EVENT_CONNECTED_TO_DEFAULT)) {
241 sendMessageDelayed(
242 obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700243 BIND_TIMEOUT_MILLIS);
244 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700245 // Send broadcast if bind fails.
Nathan Harold48ac0972019-03-13 22:33:01 -0700246 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700247 // TODO: We *must* call unbindService even if bindService returns false.
248 // (And possibly if SecurityException was thrown.)
chen xudb04c292019-03-26 17:01:15 -0700249 loge("binding to default app: "
250 + mPlatformCarrierConfigPackage + " fails");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700251 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800252 }
253 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700254 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800255
256 case EVENT_CONNECTED_TO_DEFAULT:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700257 {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700258 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700259 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800260 // If new service connection has been created, unbind.
261 if (mServiceConnection[phoneId] != conn || conn.service == null) {
262 mContext.unbindService(conn);
263 break;
264 }
chen xu02581692018-11-11 19:03:44 -0800265 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700266 // ResultReceiver callback will execute in this Handler's thread.
267 final ResultReceiver resultReceiver =
268 new ResultReceiver(this) {
269 @Override
270 public void onReceiveResult(int resultCode, Bundle resultData) {
271 mContext.unbindService(conn);
272 // If new service connection has been created, this is stale.
273 if (mServiceConnection[phoneId] != conn) {
274 loge("Received response for stale request.");
275 return;
276 }
277 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
278 if (resultCode == RESULT_ERROR || resultData == null) {
279 // On error, abort config fetching.
280 loge("Failed to get carrier config");
Nathan Harold48ac0972019-03-13 22:33:01 -0700281 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700282 return;
283 }
284 PersistableBundle config =
285 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Chen Xu49b1de32019-07-29 16:34:52 -0700286 saveConfigToXml(mPlatformCarrierConfigPackage, phoneId,
287 carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700288 mConfigFromDefaultApp[phoneId] = config;
289 sendMessage(
290 obtainMessage(
291 EVENT_FETCH_DEFAULT_DONE, phoneId, -1));
292 }
293 };
294 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800295 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700296 ICarrierService carrierService =
297 ICarrierService.Stub.asInterface(conn.service);
298 carrierService.getCarrierConfig(carrierId, resultReceiver);
chen xudb04c292019-03-26 17:01:15 -0700299 logWithLocalLog("fetch config for default app: "
300 + mPlatformCarrierConfigPackage
301 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700302 } catch (RemoteException e) {
chen xudb04c292019-03-26 17:01:15 -0700303 loge("Failed to get carrier config from default app: " +
304 mPlatformCarrierConfigPackage + " err: " + e.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700305 mContext.unbindService(conn);
306 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800307 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700308 sendMessageDelayed(
309 obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1),
310 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800311 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700312 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800313
Jonathan Basseri930701e2015-06-11 20:56:43 -0700314 case EVENT_BIND_DEFAULT_TIMEOUT:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700315 case EVENT_FETCH_DEFAULT_TIMEOUT:
316 {
chen xudb04c292019-03-26 17:01:15 -0700317 loge("bind/fetch time out from " + mPlatformCarrierConfigPackage);
chen xuf60b3162019-05-01 21:31:05 -0700318 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
319 // If we attempted to bind to the app, but the service connection is null due to
320 // the race condition that clear config event happens before bind/fetch complete
321 // then config was cleared while we were waiting and we should not continue.
322 if (mServiceConnection[phoneId] != null) {
323 // If a ResponseReceiver callback is in the queue when this happens, we will
324 // unbind twice and throw an exception.
325 mContext.unbindService(mServiceConnection[phoneId]);
326 broadcastConfigChangedIntent(phoneId);
327 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700328 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700329 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700330 }
Jonathan Basseri930701e2015-06-11 20:56:43 -0700331
Jonathan Basseri65273c82017-07-25 15:08:42 -0700332 case EVENT_FETCH_DEFAULT_DONE:
333 {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700334 // If we attempted to bind to the app, but the service connection is null, then
335 // config was cleared while we were waiting and we should not continue.
336 if (!msg.getData().getBoolean("loaded_from_xml", false)
337 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800338 break;
339 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700340 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700341 if (carrierPackageName != null) {
342 log("Found carrier config app: " + carrierPackageName);
Jordan Liu38a614c2019-03-20 15:01:17 -0700343 sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId, -1));
Jonathan Basserib919e932015-05-27 16:53:08 +0000344 } else {
Nathan Harold48ac0972019-03-13 22:33:01 -0700345 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700346 }
347 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700348 }
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700349
Jonathan Basseri65273c82017-07-25 15:08:42 -0700350 case EVENT_DO_FETCH_CARRIER:
351 {
352 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700353 final PersistableBundle config =
Yuchen Dongc15afed2018-04-26 17:05:22 +0800354 restoreConfigFromXml(carrierPackageName, phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700355 if (config != null) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700356 log(
357 "Loaded config from XML. package="
358 + carrierPackageName
359 + " phoneId="
360 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700361 mConfigFromCarrierApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700362 Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700363 newMsg.getData().putBoolean("loaded_from_xml", true);
364 sendMessage(newMsg);
365 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700366 // No cached config, so fetch it from a carrier app.
Jonathan Basseri40473a52015-08-14 14:36:29 -0700367 if (carrierPackageName != null
Jonathan Basseri65273c82017-07-25 15:08:42 -0700368 && bindToConfigPackage(
369 carrierPackageName,
370 phoneId,
371 EVENT_CONNECTED_TO_CARRIER)) {
372 sendMessageDelayed(
373 obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700374 BIND_TIMEOUT_MILLIS);
375 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700376 // Send broadcast if bind fails.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700377 broadcastConfigChangedIntent(phoneId);
chen xudb04c292019-03-26 17:01:15 -0700378 loge("bind to carrier app: " + carrierPackageName + " fails");
Nathan Harold48ac0972019-03-13 22:33:01 -0700379 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700380 }
381 }
382 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700383 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700384
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800385 case EVENT_CONNECTED_TO_CARRIER:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700386 {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700387 removeMessages(EVENT_BIND_CARRIER_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700388 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800389 // If new service connection has been created, unbind.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700390 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800391 mContext.unbindService(conn);
392 break;
393 }
chen xu02581692018-11-11 19:03:44 -0800394 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700395 // ResultReceiver callback will execute in this Handler's thread.
396 final ResultReceiver resultReceiver =
397 new ResultReceiver(this) {
398 @Override
399 public void onReceiveResult(int resultCode, Bundle resultData) {
400 mContext.unbindService(conn);
401 // If new service connection has been created, this is stale.
402 if (mServiceConnection[phoneId] != conn) {
403 loge("Received response for stale request.");
404 return;
405 }
406 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
407 if (resultCode == RESULT_ERROR || resultData == null) {
408 // On error, abort config fetching.
chen xudb04c292019-03-26 17:01:15 -0700409 loge("Failed to get carrier config from carrier app: "
410 + getCarrierPackageForPhoneId(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700411 broadcastConfigChangedIntent(phoneId);
Nathan Harold48ac0972019-03-13 22:33:01 -0700412 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700413 return;
414 }
415 PersistableBundle config =
416 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Chen Xu49b1de32019-07-29 16:34:52 -0700417 saveConfigToXml(getCarrierPackageForPhoneId(phoneId), phoneId,
418 carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700419 mConfigFromCarrierApp[phoneId] = config;
420 sendMessage(
421 obtainMessage(
422 EVENT_FETCH_CARRIER_DONE, phoneId, -1));
423 }
424 };
425 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800426 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700427 ICarrierService carrierService =
428 ICarrierService.Stub.asInterface(conn.service);
429 carrierService.getCarrierConfig(carrierId, resultReceiver);
chen xudb04c292019-03-26 17:01:15 -0700430 logWithLocalLog("fetch config for carrier app: "
431 + getCarrierPackageForPhoneId(phoneId)
432 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700433 } catch (RemoteException e) {
434 loge("Failed to get carrier config: " + e.toString());
435 mContext.unbindService(conn);
436 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800437 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700438 sendMessageDelayed(
439 obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1),
440 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800441 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700442 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800443
Jonathan Basseri930701e2015-06-11 20:56:43 -0700444 case EVENT_BIND_CARRIER_TIMEOUT:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700445 case EVENT_FETCH_CARRIER_TIMEOUT:
446 {
chen xudb04c292019-03-26 17:01:15 -0700447 loge("bind/fetch from carrier app timeout");
chen xuf60b3162019-05-01 21:31:05 -0700448 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
449 // If we attempted to bind to the app, but the service connection is null due to
450 // the race condition that clear config event happens before bind/fetch complete
451 // then config was cleared while we were waiting and we should not continue.
452 if (mServiceConnection[phoneId] != null) {
453 // If a ResponseReceiver callback is in the queue when this happens, we will
454 // unbind twice and throw an exception.
455 mContext.unbindService(mServiceConnection[phoneId]);
456 broadcastConfigChangedIntent(phoneId);
457 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700458 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700459 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700460 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700461 case EVENT_FETCH_CARRIER_DONE:
462 {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700463 // If we attempted to bind to the app, but the service connection is null, then
464 // config was cleared while we were waiting and we should not continue.
465 if (!msg.getData().getBoolean("loaded_from_xml", false)
466 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800467 break;
468 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700469 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800470 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700471 }
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700472
473 case EVENT_CHECK_SYSTEM_UPDATE:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700474 {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700475 SharedPreferences sharedPrefs =
476 PreferenceManager.getDefaultSharedPreferences(mContext);
477 final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
478 if (!Build.FINGERPRINT.equals(lastFingerprint)) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700479 log(
480 "Build fingerprint changed. old: "
481 + lastFingerprint
482 + " new: "
483 + Build.FINGERPRINT);
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700484 clearCachedConfigForPackage(null);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700485 sharedPrefs
486 .edit()
487 .putString(KEY_FINGERPRINT, Build.FINGERPRINT)
488 .apply();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700489 }
490 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700491 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700492
493 case EVENT_SUBSCRIPTION_INFO_UPDATED:
494 broadcastConfigChangedIntent(phoneId);
495 break;
Malcolm Chen5acb9cf2019-10-24 19:55:44 -0700496 case EVENT_MULTI_SIM_CONFIG_CHANGED:
497 onMultiSimConfigChanged();
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 Chenb39578a2019-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
Malcolm Chen5acb9cf2019-10-24 19:55:44 -0700558 private void clearConfigForPhone(int phoneId, boolean sendBroadcast) {
559 /* Ignore clear configuration request if device is being shutdown. */
560 Phone phone = PhoneFactory.getPhone(phoneId);
561 if (phone != null) {
562 if (phone.isShuttingDown()) {
563 return;
564 }
565 }
566
567 mConfigFromDefaultApp[phoneId] = null;
568 mConfigFromCarrierApp[phoneId] = null;
569 mServiceConnection[phoneId] = null;
570 mHasSentConfigChange[phoneId] = false;
571
572 if (sendBroadcast) broadcastConfigChangedIntent(phoneId, false);
573 }
574
Nathan Harold48ac0972019-03-13 22:33:01 -0700575 private void notifySubscriptionInfoUpdater(int phoneId) {
576 String configPackagename;
577 PersistableBundle configToSend;
578 int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
579 // Prefer the carrier privileged carrier app, but if there is not one, use the platform
580 // default carrier app.
581 if (mConfigFromCarrierApp[phoneId] != null) {
582 configPackagename = getCarrierPackageForPhoneId(phoneId);
583 configToSend = mConfigFromCarrierApp[phoneId];
584 } else {
585 configPackagename = mPlatformCarrierConfigPackage;
586 configToSend = mConfigFromDefaultApp[phoneId];
587 }
Nazanin Bakhshic93a1702019-09-18 17:54:01 -0700588
589 // mOverrideConfigs is for testing. And it will override current configs.
590 PersistableBundle config = mOverrideConfigs[phoneId];
591 if (config != null) {
592 configToSend.putAll(config);
593 }
594
Nathan Harold48ac0972019-03-13 22:33:01 -0700595 mSubscriptionInfoUpdater.updateSubscriptionByCarrierConfigAndNotifyComplete(
596 phoneId, configPackagename, configToSend,
597 mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1));
598 }
599
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800600 private void broadcastConfigChangedIntent(int phoneId) {
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800601 broadcastConfigChangedIntent(phoneId, true);
602 }
603
604 private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800605 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800606 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
chen xuf9db49f2019-03-31 23:04:42 -0700607 Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND |
608 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800609 if (addSubIdExtra) {
610 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
611 int[] subIds = SubscriptionManager.getSubId(phoneId);
612 if (!ArrayUtils.isEmpty(subIds)) {
613 TelephonyManager telMgr = TelephonyManager.from(mContext)
614 .createForSubscriptionId(subIds[0]);
615 simApplicationState = telMgr.getSimApplicationState();
616 }
617 // Include subId/carrier id extra only if SIM records are loaded
618 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
619 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
620 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
621 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID,
622 getSpecificCarrierIdForPhoneId(phoneId));
623 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, getCarrierIdForPhoneId(phoneId));
624 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800625 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800626 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Jack Yuf1661322019-04-06 00:37:23 -0700627 log("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
Jordan Liu5ca24762019-07-10 12:51:09 -0700628 mContext.sendBroadcast(intent);
Junda Liu03aad762017-07-21 13:32:17 -0700629 mHasSentConfigChange[phoneId] = true;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800630 }
631
632 /** Binds to the default or carrier config app. */
633 private boolean bindToConfigPackage(String pkgName, int phoneId, int eventId) {
chen xudb04c292019-03-26 17:01:15 -0700634 logWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700635 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700636 carrierService.setPackage(pkgName);
637 mServiceConnection[phoneId] = new CarrierServiceConnection(phoneId, eventId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800638 try {
Zach Johnson36d7aab2015-05-22 15:43:00 -0700639 return mContext.bindService(carrierService, mServiceConnection[phoneId],
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800640 Context.BIND_AUTO_CREATE);
641 } catch (SecurityException ex) {
642 return false;
643 }
644 }
645
chen xu02581692018-11-11 19:03:44 -0800646 private CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800647 String mcc = "";
648 String mnc = "";
649 String imsi = "";
650 String gid1 = "";
651 String gid2 = "";
652 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
653 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800654 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800655 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700656 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
657 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800658 mcc = simOperator.substring(0, 3);
659 mnc = simOperator.substring(3);
660 }
661 Phone phone = PhoneFactory.getPhone(phoneId);
662 if (phone != null) {
663 imsi = phone.getSubscriberId();
664 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700665 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800666 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800667 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800668 }
chen xua31f22b2019-03-06 15:28:50 -0800669 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800670 }
671
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700672 /** Returns the package name of a priveleged carrier app, or null if there is none. */
673 private String getCarrierPackageForPhoneId(int phoneId) {
674 List<String> carrierPackageNames = TelephonyManager.from(mContext)
675 .getCarrierPackageNamesForIntentAndPhone(
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700676 new Intent(CarrierService.CARRIER_SERVICE_INTERFACE), phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700677 if (carrierPackageNames != null && carrierPackageNames.size() > 0) {
678 return carrierPackageNames.get(0);
679 } else {
680 return null;
681 }
682 }
683
684 private String getIccIdForPhoneId(int phoneId) {
685 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
686 return null;
687 }
688 Phone phone = PhoneFactory.getPhone(phoneId);
689 if (phone == null) {
690 return null;
691 }
692 return phone.getIccSerialNumber();
693 }
694
chen xu02581692018-11-11 19:03:44 -0800695 /**
chen xua31f22b2019-03-06 15:28:50 -0800696 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800697 */
chen xua31f22b2019-03-06 15:28:50 -0800698 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700699 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800700 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700701 }
702 Phone phone = PhoneFactory.getPhone(phoneId);
703 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800704 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700705 }
chen xua31f22b2019-03-06 15:28:50 -0800706 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800707 }
708
709 /**
710 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
711 */
712 private int getCarrierIdForPhoneId(int phoneId) {
713 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
714 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700715 }
chen xu02581692018-11-11 19:03:44 -0800716 Phone phone = PhoneFactory.getPhone(phoneId);
717 if (phone == null) {
718 return TelephonyManager.UNKNOWN_CARRIER_ID;
719 }
720 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700721 }
722
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700723 /**
724 * Writes a bundle to an XML file.
725 *
chen xu02581692018-11-11 19:03:44 -0800726 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -0800727 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -0800728 * should have a single copy of XML file named after carrier id. However, it's still possible
729 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
730 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
731 * carrier while iccid remains the same.
732 *
733 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
734 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700735 *
736 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700737 *
738 * @param packageName the name of the package from which we fetched this bundle.
Yuchen Dongc15afed2018-04-26 17:05:22 +0800739 * @param phoneId the phone ID.
Chen Xu49b1de32019-07-29 16:34:52 -0700740 * @param carrierId contains all carrier-identifying information.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700741 * @param config the bundle to be written. Null will be treated as an empty bundle.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700742 */
Chen Xu49b1de32019-07-29 16:34:52 -0700743 private void saveConfigToXml(String packageName, int phoneId, CarrierIdentifier carrierId,
744 PersistableBundle config) {
Yuchen Dongc15afed2018-04-26 17:05:22 +0800745 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
746 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700747 loge("Skip save config because SIM records are not loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800748 return;
749 }
750
751 final String iccid = getIccIdForPhoneId(phoneId);
Chen Xu49b1de32019-07-29 16:34:52 -0700752 final int cid = carrierId.getSpecificCarrierId();
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700753 if (packageName == null || iccid == null) {
754 loge("Cannot save config with null packageName or iccid.");
755 return;
756 }
Junda Liu02596502016-11-15 13:46:43 -0800757 // b/32668103 Only save to file if config isn't empty.
758 // In case of failure, not caching an empty bundle will
759 // try loading config again on next power on or sim loaded.
760 // Downside is for genuinely empty bundle, will bind and load
761 // on every power on.
762 if (config == null || config.isEmpty()) {
763 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700764 }
765
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700766 final String version = getPackageVersion(packageName);
767 if (version == null) {
768 loge("Failed to get package version for: " + packageName);
769 return;
770 }
771
chen xudb04c292019-03-26 17:01:15 -0700772 logWithLocalLog("save config to xml, packagename: " + packageName + " phoneId: " + phoneId);
773
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700774 FileOutputStream outFile = null;
775 try {
776 outFile = new FileOutputStream(
chen xu02581692018-11-11 19:03:44 -0800777 new File(mContext.getFilesDir(),
778 getFilenameForConfig(packageName, iccid, cid)));
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700779 FastXmlSerializer out = new FastXmlSerializer();
780 out.setOutput(outFile, "utf-8");
781 out.startDocument("utf-8", true);
782 out.startTag(null, TAG_DOCUMENT);
783 out.startTag(null, TAG_VERSION);
784 out.text(version);
785 out.endTag(null, TAG_VERSION);
786 out.startTag(null, TAG_BUNDLE);
787 config.saveToXml(out);
788 out.endTag(null, TAG_BUNDLE);
789 out.endTag(null, TAG_DOCUMENT);
790 out.endDocument();
791 out.flush();
792 outFile.close();
793 }
794 catch (IOException e) {
795 loge(e.toString());
796 }
797 catch (XmlPullParserException e) {
798 loge(e.toString());
799 }
800 }
801
802 /**
803 * Reads a bundle from an XML file.
804 *
805 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +0800806 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700807 *
808 * In case of errors, or if the saved config is from a different package version than the
809 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700810 *
811 * @param packageName the name of the package from which we fetched this bundle.
Yuchen Dongc15afed2018-04-26 17:05:22 +0800812 * @param phoneId the phone ID.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700813 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
814 * version does not match, or reading config fails.
815 */
Yuchen Dongc15afed2018-04-26 17:05:22 +0800816 private PersistableBundle restoreConfigFromXml(String packageName, int phoneId) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700817 final String version = getPackageVersion(packageName);
818 if (version == null) {
819 loge("Failed to get package version for: " + packageName);
820 return null;
821 }
Yuchen Dongc15afed2018-04-26 17:05:22 +0800822 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
823 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700824 loge("Skip restoring config because SIM records are not yet loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800825 return null;
826 }
827
828 final String iccid = getIccIdForPhoneId(phoneId);
chen xua31f22b2019-03-06 15:28:50 -0800829 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700830 if (packageName == null || iccid == null) {
831 loge("Cannot restore config with null packageName or iccid.");
832 return null;
833 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700834
835 PersistableBundle restoredBundle = null;
836 FileInputStream inFile = null;
837 try {
838 inFile = new FileInputStream(
chen xu02581692018-11-11 19:03:44 -0800839 new File(mContext.getFilesDir(),
840 getFilenameForConfig(packageName, iccid, cid)));
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700841 XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
842 parser.setInput(inFile, "utf-8");
843
844 int event;
845 while (((event = parser.next()) != XmlPullParser.END_DOCUMENT)) {
846
847 if (event == XmlPullParser.START_TAG && TAG_VERSION.equals(parser.getName())) {
848 String savedVersion = parser.nextText();
849 if (!version.equals(savedVersion)) {
chen xudb04c292019-03-26 17:01:15 -0700850 loge("Saved version mismatch: " + version + " vs " + savedVersion);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700851 break;
852 }
853 }
854
855 if (event == XmlPullParser.START_TAG && TAG_BUNDLE.equals(parser.getName())) {
856 restoredBundle = PersistableBundle.restoreFromXml(parser);
857 }
858 }
859 inFile.close();
860 }
861 catch (FileNotFoundException e) {
862 loge(e.toString());
863 }
864 catch (XmlPullParserException e) {
865 loge(e.toString());
866 }
867 catch (IOException e) {
868 loge(e.toString());
869 }
870
871 return restoredBundle;
872 }
873
Junda Liu8a8a53a2015-05-15 16:19:57 -0700874 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700875 * Clears cached carrier config.
876 * This deletes all saved XML files associated with the given package name. If packageName is
877 * null, then it deletes all saved XML files.
878 *
879 * @param packageName the name of a carrier package, or null if all cached config should be
880 * cleared.
881 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -0700882 */
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700883 private boolean clearCachedConfigForPackage(final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700884 File dir = mContext.getFilesDir();
885 File[] packageFiles = dir.listFiles(new FilenameFilter() {
886 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700887 if (packageName != null) {
888 return filename.startsWith("carrierconfig-" + packageName + "-");
889 } else {
890 return filename.startsWith("carrierconfig-");
891 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700892 }
893 });
Junda Liu8a8a53a2015-05-15 16:19:57 -0700894 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700895 for (File f : packageFiles) {
896 log("deleting " + f.getName());
897 f.delete();
898 }
Junda Liu8a8a53a2015-05-15 16:19:57 -0700899 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700900 }
901
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700902 /** Builds a canonical file name for a config file. */
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700903 private String getFilenameForConfig(@NonNull String packageName, @NonNull String iccid,
chen xu02581692018-11-11 19:03:44 -0800904 int cid) {
905 // the same carrier should have a single copy of XML file named after carrier id.
906 // However, it's still possible that platform doesn't recognize the current sim carrier,
907 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
908 // cases SIM OTA resolves to different carrier while iccid remains the same.
909 return "carrierconfig-" + packageName + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700910 }
911
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700912 /** Return the current version code of a package, or null if the name is not found. */
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700913 private String getPackageVersion(String packageName) {
914 try {
915 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -0800916 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700917 } catch (PackageManager.NameNotFoundException e) {
918 return null;
919 }
920 }
921
Jonathan Basseri65273c82017-07-25 15:08:42 -0700922 /**
923 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700924 *
925 * This reads config bundles for the given phoneId. That means getting the latest bundle from
926 * the default app and a privileged carrier app, if present. This will not bind to an app if we
927 * have a saved config file to use instead.
928 */
929 private void updateConfigForPhoneId(int phoneId) {
Junda Liu8a8a53a2015-05-15 16:19:57 -0700930 // Clear in-memory cache for carrier app config, so when carrier app gets uninstalled, no
931 // stale config is left.
932 if (mConfigFromCarrierApp[phoneId] != null &&
933 getCarrierPackageForPhoneId(phoneId) == null) {
934 mConfigFromCarrierApp[phoneId] = null;
935 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700936 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700937 }
938
Malcolm Chen5acb9cf2019-10-24 19:55:44 -0700939 private void onMultiSimConfigChanged() {
940 for (int i = TelephonyManager.from(mContext).getActiveModemCount();
941 i < mConfigFromDefaultApp.length; i++) {
942 clearConfigForPhone(i, false);
943 }
944 }
945
Hall Liu506724b2018-10-22 18:16:14 -0700946 @Override
947 public @NonNull PersistableBundle getConfigForSubId(int subId, String callingPackage) {
Malcolm Chen6d3d6b32018-03-01 13:58:03 -0800948 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
949 mContext, subId, callingPackage, "getCarrierConfig")) {
950 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -0700951 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -0700952
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800953 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700954 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800955 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700956 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800957 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700958 retConfig.putAll(config);
yinxu0e3c3dc2019-08-16 14:20:08 -0700959 if (getCarrierPackageForPhoneId(phoneId) == null) {
960 retConfig.putBoolean(
961 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
962 }
yinxuc5e27622017-11-29 15:08:50 -0800963 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800964 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800965 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700966 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -0800967 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
968 }
Hall Liu506724b2018-10-22 18:16:14 -0700969 config = mOverrideConfigs[phoneId];
970 if (config != null) {
971 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -0700972 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800973 }
974 return retConfig;
975 }
976
977 @Override
Hall Liu506724b2018-10-22 18:16:14 -0700978 public void overrideConfig(int subscriptionId, PersistableBundle overrides) {
979 mContext.enforceCallingOrSelfPermission(
980 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Brad Ebingerf6281ad2019-04-25 11:02:04 -0700981 //TODO: Also check for SHELL UID to restrict this method to testing only (b/131326259)
Hall Liu506724b2018-10-22 18:16:14 -0700982 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
983 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
984 log("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
985 return;
986 }
987
988 if (overrides == null) {
989 mOverrideConfigs[phoneId] = new PersistableBundle();
Brad Ebinger725edf62019-05-15 18:24:13 -0700990 } else if (mOverrideConfigs[phoneId] == null) {
Hall Liu506724b2018-10-22 18:16:14 -0700991 mOverrideConfigs[phoneId] = overrides;
992 } else {
993 mOverrideConfigs[phoneId].putAll(overrides);
994 }
Brad Ebingerc9d1fa12019-04-17 15:21:18 -0700995
996 notifySubscriptionInfoUpdater(phoneId);
Hall Liu506724b2018-10-22 18:16:14 -0700997 }
998
999 @Override
Jonathan Basseri86030352015-06-08 12:27:56 -07001000 public void notifyConfigChangedForSubId(int subId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001001 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001002 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001003 log("Ignore invalid phoneId: " + phoneId + " for subId: " + subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001004 return;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001005 }
Jordan Liud7d57fe2019-04-16 12:29:43 -07001006
1007 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001008 // system privileged app with MODIFY_PHONE_STATE permission.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001009 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext, subId,
1010 "Require carrier privileges or MODIFY_PHONE_STATE permission.");
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001011
1012 // This method should block until deleting has completed, so that an error which prevents us
1013 // from clearing the cache is passed back to the carrier app. With the files successfully
1014 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001015 String callingPackageName = mContext.getPackageManager().getNameForUid(
1016 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001017 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001018 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001019 }
1020
1021 @Override
1022 public void updateConfigForPhoneId(int phoneId, String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -07001023 mContext.enforceCallingOrSelfPermission(
1024 android.Manifest.permission.MODIFY_PHONE_STATE, null);
chen xuc4810972019-04-17 23:44:43 -07001025 logWithLocalLog("update config for phoneId: " + phoneId + " simState: " + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001026 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1027 return;
1028 }
1029 // requires Java 7 for switch on string.
1030 switch (simState) {
1031 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1032 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001033 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001034 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Junda Liu9f2d2712015-05-15 14:22:45 -07001035 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001036 break;
1037 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1038 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001039 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001040 break;
1041 }
1042 }
1043
Junda Liu43d723a2015-05-12 17:23:45 -07001044 @Override
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001045 public String getDefaultCarrierServicePackageName() {
1046 return mPlatformCarrierConfigPackage;
1047 }
1048
1049 @Override
Junda Liu43d723a2015-05-12 17:23:45 -07001050 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1051 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1052 != PackageManager.PERMISSION_GRANTED) {
1053 pw.println("Permission Denial: can't dump carrierconfig from from pid="
1054 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1055 return;
1056 }
1057 pw.println("CarrierConfigLoader: " + this);
1058 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
shuoq26a3a4c2016-12-16 11:06:48 -08001059 pw.println("Phone Id = " + i);
1060 // display default values in CarrierConfigManager
1061 printConfig(CarrierConfigManager.getDefaultConfig(), pw,
1062 "Default Values from CarrierConfigManager");
1063 pw.println("");
1064 // display ConfigFromDefaultApp
1065 printConfig(mConfigFromDefaultApp[i], pw, "mConfigFromDefaultApp");
1066 pw.println("");
1067 // display ConfigFromCarrierApp
1068 printConfig(mConfigFromCarrierApp[i], pw, "mConfigFromCarrierApp");
Hall Liu506724b2018-10-22 18:16:14 -07001069 pw.println("");
1070 printConfig(mOverrideConfigs[i], pw, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001071 }
chen xudb04c292019-03-26 17:01:15 -07001072
1073 pw.println("CarrierConfigLoadingLog=");
1074 mCarrierConfigLoadingLog.dump(fd, pw, args);
shuoq26a3a4c2016-12-16 11:06:48 -08001075 }
1076
1077 private void printConfig(PersistableBundle configApp, PrintWriter pw, String name) {
1078 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
1079 if (configApp == null) {
1080 indentPW.increaseIndent();
1081 indentPW.println(name + " : null ");
1082 return;
1083 }
1084 indentPW.increaseIndent();
1085 indentPW.println(name + " : ");
1086 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1087 Collections.sort(sortedKeys);
1088 indentPW.increaseIndent();
1089 indentPW.increaseIndent();
1090 for (String key : sortedKeys) {
1091 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1092 indentPW.println(key + " = " +
1093 Arrays.toString((Object[]) configApp.get(key)));
1094 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1095 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1096 } else {
1097 indentPW.println(key + " = " + configApp.get(key));
1098 }
Junda Liu43d723a2015-05-12 17:23:45 -07001099 }
1100 }
1101
Zach Johnson36d7aab2015-05-22 15:43:00 -07001102 private class CarrierServiceConnection implements ServiceConnection {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001103 int phoneId;
1104 int eventId;
1105 IBinder service;
1106
Zach Johnson36d7aab2015-05-22 15:43:00 -07001107 public CarrierServiceConnection(int phoneId, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001108 this.phoneId = phoneId;
1109 this.eventId = eventId;
1110 }
1111
1112 @Override
1113 public void onServiceConnected(ComponentName name, IBinder service) {
1114 log("Connected to config app: " + name.flattenToString());
1115 this.service = service;
1116 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1117 }
1118
1119 @Override
1120 public void onServiceDisconnected(ComponentName name) {
1121 this.service = null;
1122 }
1123 }
1124
1125 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1126 @Override
1127 public void onReceive(Context context, Intent intent) {
1128 String action = intent.getAction();
Junda Liu8a8a53a2015-05-15 16:19:57 -07001129 boolean replace = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1130 // If replace is true, only care ACTION_PACKAGE_REPLACED.
1131 if (replace && !Intent.ACTION_PACKAGE_REPLACED.equals(action))
1132 return;
1133
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001134 switch (action) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001135 case Intent.ACTION_BOOT_COMPLETED:
1136 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1137 break;
1138
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001139 case Intent.ACTION_PACKAGE_ADDED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001140 case Intent.ACTION_PACKAGE_REMOVED:
Junda Liu8a8a53a2015-05-15 16:19:57 -07001141 case Intent.ACTION_PACKAGE_REPLACED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001142 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1143 String packageName = mContext.getPackageManager().getNameForUid(uid);
Junda Liu8a8a53a2015-05-15 16:19:57 -07001144 if (packageName != null) {
1145 // We don't have a phoneId for arg1.
1146 mHandler.sendMessage(
1147 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, packageName));
1148 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001149 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001150 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001151 }
1152 }
1153
chen xudb04c292019-03-26 17:01:15 -07001154 private void log(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001155 Log.d(LOG_TAG, msg);
1156 }
1157
chen xudb04c292019-03-26 17:01:15 -07001158 private void logWithLocalLog(String msg) {
1159 Log.d(LOG_TAG, msg);
1160 mCarrierConfigLoadingLog.log(msg);
1161 }
1162
1163 private void loge(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001164 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07001165 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001166 }
1167}