blob: 9841a82db550f8c94444f38217c0dca141415d7e [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;
Meng Wang103220b2019-11-25 11:21:26 -080058import com.android.internal.telephony.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) {
Torbjorn Eklund1050cb02018-11-16 14:05:38 +0100592 configToSend = new PersistableBundle(configToSend);
Nazanin Bakhshic93a1702019-09-18 17:54:01 -0700593 configToSend.putAll(config);
594 }
595
Nathan Harold48ac0972019-03-13 22:33:01 -0700596 mSubscriptionInfoUpdater.updateSubscriptionByCarrierConfigAndNotifyComplete(
597 phoneId, configPackagename, configToSend,
598 mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1));
599 }
600
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800601 private void broadcastConfigChangedIntent(int phoneId) {
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800602 broadcastConfigChangedIntent(phoneId, true);
603 }
604
605 private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800606 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800607 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
chen xuf9db49f2019-03-31 23:04:42 -0700608 Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND |
609 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800610 if (addSubIdExtra) {
611 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
612 int[] subIds = SubscriptionManager.getSubId(phoneId);
613 if (!ArrayUtils.isEmpty(subIds)) {
614 TelephonyManager telMgr = TelephonyManager.from(mContext)
615 .createForSubscriptionId(subIds[0]);
616 simApplicationState = telMgr.getSimApplicationState();
617 }
618 // Include subId/carrier id extra only if SIM records are loaded
619 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
620 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
621 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
622 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID,
623 getSpecificCarrierIdForPhoneId(phoneId));
624 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, getCarrierIdForPhoneId(phoneId));
625 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800626 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800627 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Jack Yuf1661322019-04-06 00:37:23 -0700628 log("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
Jordan Liu5ca24762019-07-10 12:51:09 -0700629 mContext.sendBroadcast(intent);
Junda Liu03aad762017-07-21 13:32:17 -0700630 mHasSentConfigChange[phoneId] = true;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800631 }
632
633 /** Binds to the default or carrier config app. */
634 private boolean bindToConfigPackage(String pkgName, int phoneId, int eventId) {
chen xudb04c292019-03-26 17:01:15 -0700635 logWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700636 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700637 carrierService.setPackage(pkgName);
638 mServiceConnection[phoneId] = new CarrierServiceConnection(phoneId, eventId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800639 try {
Zach Johnson36d7aab2015-05-22 15:43:00 -0700640 return mContext.bindService(carrierService, mServiceConnection[phoneId],
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800641 Context.BIND_AUTO_CREATE);
642 } catch (SecurityException ex) {
643 return false;
644 }
645 }
646
chen xu02581692018-11-11 19:03:44 -0800647 private CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800648 String mcc = "";
649 String mnc = "";
650 String imsi = "";
651 String gid1 = "";
652 String gid2 = "";
653 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
654 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800655 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800656 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700657 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
658 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800659 mcc = simOperator.substring(0, 3);
660 mnc = simOperator.substring(3);
661 }
662 Phone phone = PhoneFactory.getPhone(phoneId);
663 if (phone != null) {
664 imsi = phone.getSubscriberId();
665 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700666 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800667 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800668 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800669 }
chen xua31f22b2019-03-06 15:28:50 -0800670 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800671 }
672
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700673 /** Returns the package name of a priveleged carrier app, or null if there is none. */
674 private String getCarrierPackageForPhoneId(int phoneId) {
675 List<String> carrierPackageNames = TelephonyManager.from(mContext)
676 .getCarrierPackageNamesForIntentAndPhone(
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700677 new Intent(CarrierService.CARRIER_SERVICE_INTERFACE), phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700678 if (carrierPackageNames != null && carrierPackageNames.size() > 0) {
679 return carrierPackageNames.get(0);
680 } else {
681 return null;
682 }
683 }
684
685 private String getIccIdForPhoneId(int phoneId) {
686 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
687 return null;
688 }
689 Phone phone = PhoneFactory.getPhone(phoneId);
690 if (phone == null) {
691 return null;
692 }
693 return phone.getIccSerialNumber();
694 }
695
chen xu02581692018-11-11 19:03:44 -0800696 /**
chen xua31f22b2019-03-06 15:28:50 -0800697 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800698 */
chen xua31f22b2019-03-06 15:28:50 -0800699 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700700 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800701 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700702 }
703 Phone phone = PhoneFactory.getPhone(phoneId);
704 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800705 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700706 }
chen xua31f22b2019-03-06 15:28:50 -0800707 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800708 }
709
710 /**
711 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
712 */
713 private int getCarrierIdForPhoneId(int phoneId) {
714 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
715 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700716 }
chen xu02581692018-11-11 19:03:44 -0800717 Phone phone = PhoneFactory.getPhone(phoneId);
718 if (phone == null) {
719 return TelephonyManager.UNKNOWN_CARRIER_ID;
720 }
721 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700722 }
723
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700724 /**
725 * Writes a bundle to an XML file.
726 *
chen xu02581692018-11-11 19:03:44 -0800727 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -0800728 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -0800729 * should have a single copy of XML file named after carrier id. However, it's still possible
730 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
731 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
732 * carrier while iccid remains the same.
733 *
734 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
735 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700736 *
737 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700738 *
739 * @param packageName the name of the package from which we fetched this bundle.
Yuchen Dongc15afed2018-04-26 17:05:22 +0800740 * @param phoneId the phone ID.
Chen Xu49b1de32019-07-29 16:34:52 -0700741 * @param carrierId contains all carrier-identifying information.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700742 * @param config the bundle to be written. Null will be treated as an empty bundle.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700743 */
Chen Xu49b1de32019-07-29 16:34:52 -0700744 private void saveConfigToXml(String packageName, int phoneId, CarrierIdentifier carrierId,
745 PersistableBundle config) {
Yuchen Dongc15afed2018-04-26 17:05:22 +0800746 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
747 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700748 loge("Skip save config because SIM records are not loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800749 return;
750 }
751
752 final String iccid = getIccIdForPhoneId(phoneId);
Chen Xu49b1de32019-07-29 16:34:52 -0700753 final int cid = carrierId.getSpecificCarrierId();
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700754 if (packageName == null || iccid == null) {
755 loge("Cannot save config with null packageName or iccid.");
756 return;
757 }
Junda Liu02596502016-11-15 13:46:43 -0800758 // b/32668103 Only save to file if config isn't empty.
759 // In case of failure, not caching an empty bundle will
760 // try loading config again on next power on or sim loaded.
761 // Downside is for genuinely empty bundle, will bind and load
762 // on every power on.
763 if (config == null || config.isEmpty()) {
764 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700765 }
766
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700767 final String version = getPackageVersion(packageName);
768 if (version == null) {
769 loge("Failed to get package version for: " + packageName);
770 return;
771 }
772
chen xudb04c292019-03-26 17:01:15 -0700773 logWithLocalLog("save config to xml, packagename: " + packageName + " phoneId: " + phoneId);
774
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700775 FileOutputStream outFile = null;
776 try {
777 outFile = new FileOutputStream(
chen xu02581692018-11-11 19:03:44 -0800778 new File(mContext.getFilesDir(),
779 getFilenameForConfig(packageName, iccid, cid)));
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700780 FastXmlSerializer out = new FastXmlSerializer();
781 out.setOutput(outFile, "utf-8");
782 out.startDocument("utf-8", true);
783 out.startTag(null, TAG_DOCUMENT);
784 out.startTag(null, TAG_VERSION);
785 out.text(version);
786 out.endTag(null, TAG_VERSION);
787 out.startTag(null, TAG_BUNDLE);
788 config.saveToXml(out);
789 out.endTag(null, TAG_BUNDLE);
790 out.endTag(null, TAG_DOCUMENT);
791 out.endDocument();
792 out.flush();
793 outFile.close();
794 }
795 catch (IOException e) {
796 loge(e.toString());
797 }
798 catch (XmlPullParserException e) {
799 loge(e.toString());
800 }
801 }
802
803 /**
804 * Reads a bundle from an XML file.
805 *
806 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +0800807 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700808 *
809 * In case of errors, or if the saved config is from a different package version than the
810 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700811 *
812 * @param packageName the name of the package from which we fetched this bundle.
Yuchen Dongc15afed2018-04-26 17:05:22 +0800813 * @param phoneId the phone ID.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700814 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
815 * version does not match, or reading config fails.
816 */
Yuchen Dongc15afed2018-04-26 17:05:22 +0800817 private PersistableBundle restoreConfigFromXml(String packageName, int phoneId) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700818 final String version = getPackageVersion(packageName);
819 if (version == null) {
820 loge("Failed to get package version for: " + packageName);
821 return null;
822 }
Yuchen Dongc15afed2018-04-26 17:05:22 +0800823 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
824 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700825 loge("Skip restoring config because SIM records are not yet loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800826 return null;
827 }
828
829 final String iccid = getIccIdForPhoneId(phoneId);
chen xua31f22b2019-03-06 15:28:50 -0800830 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700831 if (packageName == null || iccid == null) {
832 loge("Cannot restore config with null packageName or iccid.");
833 return null;
834 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700835
836 PersistableBundle restoredBundle = null;
837 FileInputStream inFile = null;
838 try {
839 inFile = new FileInputStream(
chen xu02581692018-11-11 19:03:44 -0800840 new File(mContext.getFilesDir(),
841 getFilenameForConfig(packageName, iccid, cid)));
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700842 XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
843 parser.setInput(inFile, "utf-8");
844
845 int event;
846 while (((event = parser.next()) != XmlPullParser.END_DOCUMENT)) {
847
848 if (event == XmlPullParser.START_TAG && TAG_VERSION.equals(parser.getName())) {
849 String savedVersion = parser.nextText();
850 if (!version.equals(savedVersion)) {
chen xudb04c292019-03-26 17:01:15 -0700851 loge("Saved version mismatch: " + version + " vs " + savedVersion);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700852 break;
853 }
854 }
855
856 if (event == XmlPullParser.START_TAG && TAG_BUNDLE.equals(parser.getName())) {
857 restoredBundle = PersistableBundle.restoreFromXml(parser);
858 }
859 }
860 inFile.close();
861 }
862 catch (FileNotFoundException e) {
863 loge(e.toString());
864 }
865 catch (XmlPullParserException e) {
866 loge(e.toString());
867 }
868 catch (IOException e) {
869 loge(e.toString());
870 }
871
872 return restoredBundle;
873 }
874
Junda Liu8a8a53a2015-05-15 16:19:57 -0700875 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700876 * Clears cached carrier config.
877 * This deletes all saved XML files associated with the given package name. If packageName is
878 * null, then it deletes all saved XML files.
879 *
880 * @param packageName the name of a carrier package, or null if all cached config should be
881 * cleared.
882 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -0700883 */
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700884 private boolean clearCachedConfigForPackage(final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700885 File dir = mContext.getFilesDir();
886 File[] packageFiles = dir.listFiles(new FilenameFilter() {
887 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700888 if (packageName != null) {
889 return filename.startsWith("carrierconfig-" + packageName + "-");
890 } else {
891 return filename.startsWith("carrierconfig-");
892 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700893 }
894 });
Junda Liu8a8a53a2015-05-15 16:19:57 -0700895 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700896 for (File f : packageFiles) {
897 log("deleting " + f.getName());
898 f.delete();
899 }
Junda Liu8a8a53a2015-05-15 16:19:57 -0700900 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700901 }
902
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700903 /** Builds a canonical file name for a config file. */
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700904 private String getFilenameForConfig(@NonNull String packageName, @NonNull String iccid,
chen xu02581692018-11-11 19:03:44 -0800905 int cid) {
906 // the same carrier should have a single copy of XML file named after carrier id.
907 // However, it's still possible that platform doesn't recognize the current sim carrier,
908 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
909 // cases SIM OTA resolves to different carrier while iccid remains the same.
910 return "carrierconfig-" + packageName + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700911 }
912
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700913 /** Return the current version code of a package, or null if the name is not found. */
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700914 private String getPackageVersion(String packageName) {
915 try {
916 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -0800917 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700918 } catch (PackageManager.NameNotFoundException e) {
919 return null;
920 }
921 }
922
Jonathan Basseri65273c82017-07-25 15:08:42 -0700923 /**
924 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700925 *
926 * This reads config bundles for the given phoneId. That means getting the latest bundle from
927 * the default app and a privileged carrier app, if present. This will not bind to an app if we
928 * have a saved config file to use instead.
929 */
930 private void updateConfigForPhoneId(int phoneId) {
Junda Liu8a8a53a2015-05-15 16:19:57 -0700931 // Clear in-memory cache for carrier app config, so when carrier app gets uninstalled, no
932 // stale config is left.
933 if (mConfigFromCarrierApp[phoneId] != null &&
934 getCarrierPackageForPhoneId(phoneId) == null) {
935 mConfigFromCarrierApp[phoneId] = null;
936 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700937 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700938 }
939
Malcolm Chen5acb9cf2019-10-24 19:55:44 -0700940 private void onMultiSimConfigChanged() {
941 for (int i = TelephonyManager.from(mContext).getActiveModemCount();
942 i < mConfigFromDefaultApp.length; i++) {
943 clearConfigForPhone(i, false);
944 }
945 }
946
Hall Liu506724b2018-10-22 18:16:14 -0700947 @Override
948 public @NonNull PersistableBundle getConfigForSubId(int subId, String callingPackage) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -0700949 return getConfigForSubIdWithFeature(subId, callingPackage, null);
950 }
951
952 @Override
953 public @NonNull PersistableBundle getConfigForSubIdWithFeature(int subId, String callingPackage,
954 String callingFeatureId) {
955 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subId, callingPackage,
956 callingFeatureId, "getCarrierConfig")) {
Malcolm Chen6d3d6b32018-03-01 13:58:03 -0800957 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -0700958 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -0700959
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800960 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700961 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800962 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700963 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800964 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700965 retConfig.putAll(config);
yinxu0e3c3dc2019-08-16 14:20:08 -0700966 if (getCarrierPackageForPhoneId(phoneId) == null) {
967 retConfig.putBoolean(
968 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
969 }
yinxuc5e27622017-11-29 15:08:50 -0800970 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800971 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800972 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700973 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -0800974 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
975 }
Hall Liu506724b2018-10-22 18:16:14 -0700976 config = mOverrideConfigs[phoneId];
977 if (config != null) {
978 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -0700979 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800980 }
981 return retConfig;
982 }
983
984 @Override
Hall Liu506724b2018-10-22 18:16:14 -0700985 public void overrideConfig(int subscriptionId, PersistableBundle overrides) {
986 mContext.enforceCallingOrSelfPermission(
987 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Brad Ebingerf6281ad2019-04-25 11:02:04 -0700988 //TODO: Also check for SHELL UID to restrict this method to testing only (b/131326259)
Hall Liu506724b2018-10-22 18:16:14 -0700989 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
990 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
991 log("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
992 return;
993 }
994
995 if (overrides == null) {
996 mOverrideConfigs[phoneId] = new PersistableBundle();
Brad Ebinger725edf62019-05-15 18:24:13 -0700997 } else if (mOverrideConfigs[phoneId] == null) {
Hall Liu506724b2018-10-22 18:16:14 -0700998 mOverrideConfigs[phoneId] = overrides;
999 } else {
1000 mOverrideConfigs[phoneId].putAll(overrides);
1001 }
Brad Ebingerc9d1fa12019-04-17 15:21:18 -07001002
1003 notifySubscriptionInfoUpdater(phoneId);
Hall Liu506724b2018-10-22 18:16:14 -07001004 }
1005
1006 @Override
Jonathan Basseri86030352015-06-08 12:27:56 -07001007 public void notifyConfigChangedForSubId(int subId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001008 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001009 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001010 log("Ignore invalid phoneId: " + phoneId + " for subId: " + subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001011 return;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001012 }
Jordan Liud7d57fe2019-04-16 12:29:43 -07001013
1014 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001015 // system privileged app with MODIFY_PHONE_STATE permission.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001016 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext, subId,
1017 "Require carrier privileges or MODIFY_PHONE_STATE permission.");
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001018
1019 // This method should block until deleting has completed, so that an error which prevents us
1020 // from clearing the cache is passed back to the carrier app. With the files successfully
1021 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001022 String callingPackageName = mContext.getPackageManager().getNameForUid(
1023 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001024 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001025 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001026 }
1027
1028 @Override
1029 public void updateConfigForPhoneId(int phoneId, String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -07001030 mContext.enforceCallingOrSelfPermission(
1031 android.Manifest.permission.MODIFY_PHONE_STATE, null);
chen xuc4810972019-04-17 23:44:43 -07001032 logWithLocalLog("update config for phoneId: " + phoneId + " simState: " + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001033 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1034 return;
1035 }
1036 // requires Java 7 for switch on string.
1037 switch (simState) {
1038 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1039 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001040 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001041 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Junda Liu9f2d2712015-05-15 14:22:45 -07001042 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001043 break;
1044 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1045 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001046 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001047 break;
1048 }
1049 }
1050
Junda Liu43d723a2015-05-12 17:23:45 -07001051 @Override
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001052 public String getDefaultCarrierServicePackageName() {
1053 return mPlatformCarrierConfigPackage;
1054 }
1055
1056 @Override
Junda Liu43d723a2015-05-12 17:23:45 -07001057 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1058 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1059 != PackageManager.PERMISSION_GRANTED) {
1060 pw.println("Permission Denial: can't dump carrierconfig from from pid="
1061 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1062 return;
1063 }
1064 pw.println("CarrierConfigLoader: " + this);
1065 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
shuoq26a3a4c2016-12-16 11:06:48 -08001066 pw.println("Phone Id = " + i);
1067 // display default values in CarrierConfigManager
1068 printConfig(CarrierConfigManager.getDefaultConfig(), pw,
1069 "Default Values from CarrierConfigManager");
1070 pw.println("");
1071 // display ConfigFromDefaultApp
1072 printConfig(mConfigFromDefaultApp[i], pw, "mConfigFromDefaultApp");
1073 pw.println("");
1074 // display ConfigFromCarrierApp
1075 printConfig(mConfigFromCarrierApp[i], pw, "mConfigFromCarrierApp");
Hall Liu506724b2018-10-22 18:16:14 -07001076 pw.println("");
1077 printConfig(mOverrideConfigs[i], pw, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001078 }
chen xudb04c292019-03-26 17:01:15 -07001079
1080 pw.println("CarrierConfigLoadingLog=");
1081 mCarrierConfigLoadingLog.dump(fd, pw, args);
shuoq26a3a4c2016-12-16 11:06:48 -08001082 }
1083
1084 private void printConfig(PersistableBundle configApp, PrintWriter pw, String name) {
1085 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
1086 if (configApp == null) {
1087 indentPW.increaseIndent();
1088 indentPW.println(name + " : null ");
1089 return;
1090 }
1091 indentPW.increaseIndent();
1092 indentPW.println(name + " : ");
1093 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1094 Collections.sort(sortedKeys);
1095 indentPW.increaseIndent();
1096 indentPW.increaseIndent();
1097 for (String key : sortedKeys) {
1098 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1099 indentPW.println(key + " = " +
1100 Arrays.toString((Object[]) configApp.get(key)));
1101 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1102 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1103 } else {
1104 indentPW.println(key + " = " + configApp.get(key));
1105 }
Junda Liu43d723a2015-05-12 17:23:45 -07001106 }
1107 }
1108
Zach Johnson36d7aab2015-05-22 15:43:00 -07001109 private class CarrierServiceConnection implements ServiceConnection {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001110 int phoneId;
1111 int eventId;
1112 IBinder service;
1113
Zach Johnson36d7aab2015-05-22 15:43:00 -07001114 public CarrierServiceConnection(int phoneId, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001115 this.phoneId = phoneId;
1116 this.eventId = eventId;
1117 }
1118
1119 @Override
1120 public void onServiceConnected(ComponentName name, IBinder service) {
1121 log("Connected to config app: " + name.flattenToString());
1122 this.service = service;
1123 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1124 }
1125
1126 @Override
1127 public void onServiceDisconnected(ComponentName name) {
1128 this.service = null;
1129 }
1130 }
1131
1132 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1133 @Override
1134 public void onReceive(Context context, Intent intent) {
1135 String action = intent.getAction();
Junda Liu8a8a53a2015-05-15 16:19:57 -07001136 boolean replace = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1137 // If replace is true, only care ACTION_PACKAGE_REPLACED.
1138 if (replace && !Intent.ACTION_PACKAGE_REPLACED.equals(action))
1139 return;
1140
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001141 switch (action) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001142 case Intent.ACTION_BOOT_COMPLETED:
1143 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1144 break;
1145
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001146 case Intent.ACTION_PACKAGE_ADDED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001147 case Intent.ACTION_PACKAGE_REMOVED:
Junda Liu8a8a53a2015-05-15 16:19:57 -07001148 case Intent.ACTION_PACKAGE_REPLACED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001149 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1150 String packageName = mContext.getPackageManager().getNameForUid(uid);
Junda Liu8a8a53a2015-05-15 16:19:57 -07001151 if (packageName != null) {
1152 // We don't have a phoneId for arg1.
1153 mHandler.sendMessage(
1154 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, packageName));
1155 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001156 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001157 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001158 }
1159 }
1160
chen xudb04c292019-03-26 17:01:15 -07001161 private void log(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001162 Log.d(LOG_TAG, msg);
1163 }
1164
chen xudb04c292019-03-26 17:01:15 -07001165 private void logWithLocalLog(String msg) {
1166 Log.d(LOG_TAG, msg);
1167 mCarrierConfigLoadingLog.log(msg);
1168 }
1169
1170 private void loge(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001171 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07001172 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001173 }
1174}