blob: de413090d054a1d36b3e13b47c70fde95e946d11 [file] [log] [blame]
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001/*
Jonathan Basseri6465afd2015-02-25 13:05:57 -08002 * 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;
Torbjorn Eklund723527a2019-02-13 11:16:25 +010023import android.annotation.Nullable;
Hunter Knepshielde601f432020-02-19 10:16:04 -080024import android.app.AppOpsManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080025import android.content.BroadcastReceiver;
26import android.content.ComponentName;
27import android.content.Context;
28import android.content.Intent;
29import android.content.IntentFilter;
30import android.content.ServiceConnection;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070031import android.content.SharedPreferences;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070032import android.content.pm.PackageInfo;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070033import android.content.pm.PackageManager;
Junda Liu43d723a2015-05-12 17:23:45 -070034import android.os.Binder;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070035import android.os.Build;
Jonathan Basseri65273c82017-07-25 15:08:42 -070036import android.os.Bundle;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080037import android.os.Handler;
38import android.os.IBinder;
39import android.os.Message;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070040import android.os.PersistableBundle;
Hunter Knepshielde601f432020-02-19 10:16:04 -080041import android.os.Process;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080042import android.os.RemoteException;
Jonathan Basseri65273c82017-07-25 15:08:42 -070043import android.os.ResultReceiver;
Meng Wang97a6a462020-01-23 16:22:16 -080044import android.os.UserHandle;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070045import android.preference.PreferenceManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080046import android.service.carrier.CarrierIdentifier;
Zach Johnson36d7aab2015-05-22 15:43:00 -070047import android.service.carrier.CarrierService;
48import android.service.carrier.ICarrierService;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080049import android.telephony.CarrierConfigManager;
50import android.telephony.SubscriptionManager;
Peter Wangc035ce42020-01-08 21:00:22 -080051import android.telephony.TelephonyFrameworkInitializer;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080052import android.telephony.TelephonyManager;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -080053import android.text.TextUtils;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080054import android.util.ArraySet;
chen xudb04c292019-03-26 17:01:15 -070055import android.util.LocalLog;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080056import android.util.Log;
57
58import com.android.internal.telephony.ICarrierConfigLoader;
59import com.android.internal.telephony.IccCardConstants;
60import com.android.internal.telephony.Phone;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080061import com.android.internal.telephony.PhoneFactory;
Nathan Harold48ac0972019-03-13 22:33:01 -070062import com.android.internal.telephony.SubscriptionInfoUpdater;
Jeff Davidsona8e4e242018-03-15 17:16:18 -070063import com.android.internal.telephony.TelephonyPermissions;
Meng Wanga320b942019-11-25 11:21:26 -080064import com.android.internal.telephony.util.ArrayUtils;
shuoq26a3a4c2016-12-16 11:06:48 -080065import com.android.internal.util.IndentingPrintWriter;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080066
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070067import java.io.File;
Junda Liu43d723a2015-05-12 17:23:45 -070068import java.io.FileDescriptor;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070069import java.io.FileInputStream;
70import java.io.FileNotFoundException;
71import java.io.FileOutputStream;
Jonathan Basseri1f743c92015-05-15 00:19:46 -070072import java.io.FilenameFilter;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070073import java.io.IOException;
Junda Liu43d723a2015-05-12 17:23:45 -070074import java.io.PrintWriter;
shuoq26a3a4c2016-12-16 11:06:48 -080075import java.util.ArrayList;
76import java.util.Arrays;
77import java.util.Collections;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080078import java.util.List;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080079import java.util.Set;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080080
81/**
82 * CarrierConfigLoader binds to privileged carrier apps to fetch carrier config overlays.
Jonathan Basseri6465afd2015-02-25 13:05:57 -080083 */
Jonathan Basseri6465afd2015-02-25 13:05:57 -080084public class CarrierConfigLoader extends ICarrierConfigLoader.Stub {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070085 private static final String LOG_TAG = "CarrierConfigLoader";
Meng Wang33ad2bc2017-03-16 20:21:20 -070086
87 // Package name for platform carrier config app, bundled with system image.
88 private final String mPlatformCarrierConfigPackage;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080089
90 /** The singleton instance. */
91 private static CarrierConfigLoader sInstance;
92 // The context for phone app, passed from PhoneGlobals.
93 private Context mContext;
94 // Carrier configs from default app, indexed by phoneID.
Jonathan Basseric31f1f32015-05-12 10:13:03 -070095 private PersistableBundle[] mConfigFromDefaultApp;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080096 // Carrier configs from privileged carrier config app, indexed by phoneID.
Jonathan Basseric31f1f32015-05-12 10:13:03 -070097 private PersistableBundle[] mConfigFromCarrierApp;
Torbjorn Eklund723527a2019-02-13 11:16:25 +010098 // Persistent Carrier configs that are provided via the override test API, indexed by phone ID.
99 private PersistableBundle[] mPersistentOverrideConfigs;
Hall Liu506724b2018-10-22 18:16:14 -0700100 // Carrier configs that are provided via the override test API, indexed by phone ID.
101 private PersistableBundle[] mOverrideConfigs;
Jayachandran C645ec612020-01-10 10:30:25 -0800102 // Carrier configs to override code default when there is no SIM inserted
103 private PersistableBundle mNoSimConfig;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800104 // Service connection for binding to config app.
Zach Johnson36d7aab2015-05-22 15:43:00 -0700105 private CarrierServiceConnection[] mServiceConnection;
Jayachandran C645ec612020-01-10 10:30:25 -0800106 // Service connection for binding to carrier config app for no SIM config.
107 private CarrierServiceConnection[] mServiceConnectionForNoSimConfig;
Jordan Liu178430d2020-02-10 14:32:15 -0800108 // Whether we are bound to a service for each phone
109 private boolean[] mServiceBound;
Jayachandran C645ec612020-01-10 10:30:25 -0800110 // Whether we are bound to a service for no SIM config
111 private boolean[] mServiceBoundForNoSimConfig;
Sarah Chin807d5c62020-03-30 17:21:09 -0700112 // Whether we have sent config change broadcast for each phone id.
Junda Liu03aad762017-07-21 13:32:17 -0700113 private boolean[] mHasSentConfigChange;
Sarah Chin807d5c62020-03-30 17:21:09 -0700114 // Whether the broadcast was sent from EVENT_SYSTEM_UNLOCKED, to track rebroadcasts
115 private boolean[] mFromSystemUnlocked;
Nathan Harold48ac0972019-03-13 22:33:01 -0700116 // SubscriptionInfoUpdater
117 private final SubscriptionInfoUpdater mSubscriptionInfoUpdater;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800118
Nanxi Chen3d670502016-03-17 16:32:09 -0700119 // Broadcast receiver for Boot intents, register intent filter in construtor.
120 private final BroadcastReceiver mBootReceiver = new ConfigLoaderBroadcastReceiver();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800121 // Broadcast receiver for SIM and pkg intents, register intent filter in constructor.
Nanxi Chen3d670502016-03-17 16:32:09 -0700122 private final BroadcastReceiver mPackageReceiver = new ConfigLoaderBroadcastReceiver();
Jack Yu67663de2019-10-17 15:19:48 -0700123 private final LocalLog mCarrierConfigLoadingLog = new LocalLog(100);
chen xudb04c292019-03-26 17:01:15 -0700124
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800125
126 // Message codes; see mHandler below.
127 // Request from SubscriptionInfoUpdater when SIM becomes absent or error.
128 private static final int EVENT_CLEAR_CONFIG = 0;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800129 // Has connected to default app.
130 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
131 // Has connected to carrier app.
132 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700133 // Config has been loaded from default app (or cache).
134 private static final int EVENT_FETCH_DEFAULT_DONE = 5;
135 // Config has been loaded from carrier app (or cache).
136 private static final int EVENT_FETCH_CARRIER_DONE = 6;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700137 // Attempt to fetch from default app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700138 private static final int EVENT_DO_FETCH_DEFAULT = 7;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700139 // Attempt to fetch from carrier app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700140 private static final int EVENT_DO_FETCH_CARRIER = 8;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700141 // A package has been installed, uninstalled, or updated.
142 private static final int EVENT_PACKAGE_CHANGED = 9;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700143 // Bind timed out for the default app.
144 private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
145 // Bind timed out for a carrier app.
146 private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700147 // Check if the system fingerprint has changed.
148 private static final int EVENT_CHECK_SYSTEM_UPDATE = 12;
Nanxi Chen3d670502016-03-17 16:32:09 -0700149 // Rerun carrier config binding after system is unlocked.
150 private static final int EVENT_SYSTEM_UNLOCKED = 13;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700151 // Fetching config timed out from the default app.
152 private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14;
153 // Fetching config timed out from a carrier app.
154 private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15;
Nathan Harold48ac0972019-03-13 22:33:01 -0700155 // SubscriptionInfoUpdater has finished updating the sub for the carrier config.
156 private static final int EVENT_SUBSCRIPTION_INFO_UPDATED = 16;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700157 // Multi-SIM config changed.
158 private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 17;
Jayachandran C645ec612020-01-10 10:30:25 -0800159 // Attempt to fetch from default app or read from XML for no SIM case.
160 private static final int EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG = 18;
161 // No SIM config has been loaded from default app (or cache).
162 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE = 19;
163 // Has connected to default app for no SIM config.
164 private static final int EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG = 20;
165 // Bind timed out for the default app when trying to fetch no SIM config.
166 private static final int EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 21;
167 // Fetching config timed out from the default app for no SIM config.
168 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 22;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700169
Junda Liu6cb45002016-03-22 17:18:20 -0700170 private static final int BIND_TIMEOUT_MILLIS = 30000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800171
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800172 // Keys used for saving and restoring config bundle from file.
173 private static final String KEY_VERSION = "__carrier_config_package_version__";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800174
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100175 private static final String OVERRIDE_PACKAGE_ADDITION = "-override";
176
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700177 // SharedPreferences key for last known build fingerprint.
178 private static final String KEY_FINGERPRINT = "build_fingerprint";
179
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800180 // Argument for #dump that indicates we should also call the default and specified carrier
181 // service's #dump method. In multi-SIM devices, it's possible that carrier A is on SIM 1 and
182 // carrier B is on SIM 2, in which case we should not dump carrier B's service when carrier A
183 // requested the dump.
184 private static final String DUMP_ARG_REQUESTING_PACKAGE = "--requesting-package";
185
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800186 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700187 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700188 // For each phoneId, the event sequence should be:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700189 // fetch default, connected to default, fetch default (async), fetch default done,
190 // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700191 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700192 // If there is a saved config file for either the default app or the carrier app, we skip
193 // binding to the app and go straight from fetch to loaded.
194 //
195 // At any time, at most one connection is active. If events are not in this order, previous
196 // connection will be unbound, so only latest event takes effect.
197 //
198 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
199 // 1. loading from carrier app (even if read from a file)
200 // 2. loading from default app if there is no carrier app (even if read from a file)
201 // 3. clearing config (e.g. due to sim removal)
202 // 4. encountering bind or IPC error
Jonathan Basseri65273c82017-07-25 15:08:42 -0700203 private class ConfigHandler extends Handler {
204 @Override
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800205 public void handleMessage(Message msg) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700206 final int phoneId = msg.arg1;
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800207 logdWithLocalLog("mHandler: " + msg.what + " phoneId: " + phoneId);
Malcolm Chen5ea18532019-10-24 19:55:44 -0700208 if (!SubscriptionManager.isValidPhoneId(phoneId)
209 && msg.what != EVENT_MULTI_SIM_CONFIG_CHANGED) {
210 return;
211 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800212 switch (msg.what) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800213 case EVENT_CLEAR_CONFIG: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700214 clearConfigForPhone(phoneId, true);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800215 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700216 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700217
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800218 case EVENT_SYSTEM_UNLOCKED: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700219 for (int i = 0; i < TelephonyManager.from(mContext).getActiveModemCount();
220 ++i) {
Sarah Chin807d5c62020-03-30 17:21:09 -0700221 // When the user unlocks the device, send the broadcast again (with a
222 // rebroadcast extra) if we have sent it before unlock. This will avoid
223 // trying to load the carrier config when the SIM is still loading when the
224 // unlock happens.
Junda Liu03aad762017-07-21 13:32:17 -0700225 if (mHasSentConfigChange[i]) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800226 logdWithLocalLog("System unlocked");
Sarah Chin807d5c62020-03-30 17:21:09 -0700227 mFromSystemUnlocked[i] = true;
Junda Liu03aad762017-07-21 13:32:17 -0700228 updateConfigForPhoneId(i);
229 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700230 }
231 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700232 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700233
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800234 case EVENT_PACKAGE_CHANGED: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700235 final String carrierPackageName = (String) msg.obj;
236 // Only update if there are cached config removed to avoid updating config for
237 // unrelated packages.
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700238 if (clearCachedConfigForPackage(carrierPackageName)) {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700239 int numPhones = TelephonyManager.from(mContext).getActiveModemCount();
Junda Liu8a8a53a2015-05-15 16:19:57 -0700240 for (int i = 0; i < numPhones; ++i) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800241 logdWithLocalLog("Package changed: " + carrierPackageName
Jack Yu67663de2019-10-17 15:19:48 -0700242 + ", phone=" + i);
Junda Liu8a8a53a2015-05-15 16:19:57 -0700243 updateConfigForPhoneId(i);
244 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700245 }
246 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700247 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700248
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800249 case EVENT_DO_FETCH_DEFAULT: {
Rambo Wang9bc7d362021-03-09 09:10:58 -0800250 // Clear in-memory cache for carrier app config, so when carrier app gets
251 // uninstalled, no stale config is left.
252 if (mConfigFromCarrierApp[phoneId] != null
253 && getCarrierPackageForPhoneId(phoneId) == null) {
254 mConfigFromCarrierApp[phoneId] = null;
255 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100256 // Restore persistent override values.
257 PersistableBundle config = restoreConfigFromXml(
258 mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId);
259 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800260 logd("Loaded persistent override config from XML. package="
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100261 + mPlatformCarrierConfigPackage
262 + " phoneId=" + phoneId);
263 mPersistentOverrideConfigs[phoneId] = config;
264 }
265
266 config = restoreConfigFromXml(mPlatformCarrierConfigPackage, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700267 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800268 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700269 "Loaded config from XML. package="
270 + mPlatformCarrierConfigPackage
271 + " phoneId="
272 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700273 mConfigFromDefaultApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700274 Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700275 newMsg.getData().putBoolean("loaded_from_xml", true);
276 mHandler.sendMessage(newMsg);
277 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700278 // No cached config, so fetch it from the default app.
279 if (bindToConfigPackage(
280 mPlatformCarrierConfigPackage,
281 phoneId,
282 EVENT_CONNECTED_TO_DEFAULT)) {
283 sendMessageDelayed(
284 obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700285 BIND_TIMEOUT_MILLIS);
286 } else {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800287 // Put a stub bundle in place so that the rest of the logic continues
288 // smoothly.
289 mConfigFromDefaultApp[phoneId] = new PersistableBundle();
Jonathan Basseri65273c82017-07-25 15:08:42 -0700290 // Send broadcast if bind fails.
Nathan Harold48ac0972019-03-13 22:33:01 -0700291 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700292 // TODO: We *must* call unbindService even if bindService returns false.
293 // (And possibly if SecurityException was thrown.)
chen xudb04c292019-03-26 17:01:15 -0700294 loge("binding to default app: "
295 + mPlatformCarrierConfigPackage + " fails");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700296 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800297 }
298 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700299 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800300
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800301 case EVENT_CONNECTED_TO_DEFAULT: {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700302 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700303 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800304 // If new service connection has been created, unbind.
305 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Jordan Liu178430d2020-02-10 14:32:15 -0800306 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800307 break;
308 }
chen xu02581692018-11-11 19:03:44 -0800309 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700310 // ResultReceiver callback will execute in this Handler's thread.
311 final ResultReceiver resultReceiver =
312 new ResultReceiver(this) {
313 @Override
314 public void onReceiveResult(int resultCode, Bundle resultData) {
Jordan Liu178430d2020-02-10 14:32:15 -0800315 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700316 // If new service connection has been created, this is stale.
317 if (mServiceConnection[phoneId] != conn) {
318 loge("Received response for stale request.");
319 return;
320 }
321 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
322 if (resultCode == RESULT_ERROR || resultData == null) {
323 // On error, abort config fetching.
324 loge("Failed to get carrier config");
Nathan Harold48ac0972019-03-13 22:33:01 -0700325 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700326 return;
327 }
328 PersistableBundle config =
329 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100330 saveConfigToXml(mPlatformCarrierConfigPackage, "", phoneId,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800331 carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700332 mConfigFromDefaultApp[phoneId] = config;
333 sendMessage(
334 obtainMessage(
335 EVENT_FETCH_DEFAULT_DONE, phoneId, -1));
336 }
337 };
338 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800339 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700340 ICarrierService carrierService =
341 ICarrierService.Stub.asInterface(conn.service);
342 carrierService.getCarrierConfig(carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800343 logdWithLocalLog("Fetch config for default app: "
chen xudb04c292019-03-26 17:01:15 -0700344 + mPlatformCarrierConfigPackage
345 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700346 } catch (RemoteException e) {
chen xudb04c292019-03-26 17:01:15 -0700347 loge("Failed to get carrier config from default app: " +
348 mPlatformCarrierConfigPackage + " err: " + e.toString());
Jordan Liu178430d2020-02-10 14:32:15 -0800349 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700350 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800351 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700352 sendMessageDelayed(
353 obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1),
354 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800355 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700356 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800357
Jonathan Basseri930701e2015-06-11 20:56:43 -0700358 case EVENT_BIND_DEFAULT_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800359 case EVENT_FETCH_DEFAULT_TIMEOUT: {
360 loge("Bind/fetch time out from " + mPlatformCarrierConfigPackage);
chen xuf60b3162019-05-01 21:31:05 -0700361 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
362 // If we attempted to bind to the app, but the service connection is null due to
363 // the race condition that clear config event happens before bind/fetch complete
364 // then config was cleared while we were waiting and we should not continue.
365 if (mServiceConnection[phoneId] != null) {
366 // If a ResponseReceiver callback is in the queue when this happens, we will
367 // unbind twice and throw an exception.
Jordan Liu178430d2020-02-10 14:32:15 -0800368 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700369 broadcastConfigChangedIntent(phoneId);
370 }
Nathan Harold1122e3d2021-01-22 15:45:24 -0800371 // Put a stub bundle in place so that the rest of the logic continues smoothly.
372 mConfigFromDefaultApp[phoneId] = new PersistableBundle();
Nathan Harold48ac0972019-03-13 22:33:01 -0700373 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700374 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700375 }
Jonathan Basseri930701e2015-06-11 20:56:43 -0700376
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800377 case EVENT_FETCH_DEFAULT_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700378 // If we attempted to bind to the app, but the service connection is null, then
379 // config was cleared while we were waiting and we should not continue.
380 if (!msg.getData().getBoolean("loaded_from_xml", false)
381 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800382 break;
383 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700384 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700385 if (carrierPackageName != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800386 logd("Found carrier config app: " + carrierPackageName);
Jordan Liu38a614c2019-03-20 15:01:17 -0700387 sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId, -1));
Jonathan Basserib919e932015-05-27 16:53:08 +0000388 } else {
Nathan Harold48ac0972019-03-13 22:33:01 -0700389 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700390 }
391 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700392 }
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700393
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800394 case EVENT_DO_FETCH_CARRIER: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700395 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700396 final PersistableBundle config =
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100397 restoreConfigFromXml(carrierPackageName, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700398 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800399 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700400 "Loaded config from XML. package="
401 + carrierPackageName
402 + " phoneId="
403 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700404 mConfigFromCarrierApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700405 Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700406 newMsg.getData().putBoolean("loaded_from_xml", true);
407 sendMessage(newMsg);
408 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700409 // No cached config, so fetch it from a carrier app.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800410 if (carrierPackageName != null && bindToConfigPackage(carrierPackageName,
411 phoneId, EVENT_CONNECTED_TO_CARRIER)) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700412 sendMessageDelayed(
413 obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700414 BIND_TIMEOUT_MILLIS);
415 } else {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800416 // Put a stub bundle in place so that the rest of the logic continues
417 // smoothly.
418 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
Jonathan Basseri65273c82017-07-25 15:08:42 -0700419 // Send broadcast if bind fails.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700420 broadcastConfigChangedIntent(phoneId);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800421 loge("Bind to carrier app: " + carrierPackageName + " fails");
Nathan Harold48ac0972019-03-13 22:33:01 -0700422 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700423 }
424 }
425 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700426 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700427
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800428 case EVENT_CONNECTED_TO_CARRIER: {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700429 removeMessages(EVENT_BIND_CARRIER_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700430 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800431 // If new service connection has been created, unbind.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700432 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Jordan Liu178430d2020-02-10 14:32:15 -0800433 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800434 break;
435 }
chen xu02581692018-11-11 19:03:44 -0800436 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700437 // ResultReceiver callback will execute in this Handler's thread.
438 final ResultReceiver resultReceiver =
439 new ResultReceiver(this) {
440 @Override
441 public void onReceiveResult(int resultCode, Bundle resultData) {
Jordan Liu178430d2020-02-10 14:32:15 -0800442 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700443 // If new service connection has been created, this is stale.
444 if (mServiceConnection[phoneId] != conn) {
445 loge("Received response for stale request.");
446 return;
447 }
448 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
449 if (resultCode == RESULT_ERROR || resultData == null) {
450 // On error, abort config fetching.
chen xudb04c292019-03-26 17:01:15 -0700451 loge("Failed to get carrier config from carrier app: "
452 + getCarrierPackageForPhoneId(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700453 broadcastConfigChangedIntent(phoneId);
Nathan Harold48ac0972019-03-13 22:33:01 -0700454 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700455 return;
456 }
457 PersistableBundle config =
458 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100459 saveConfigToXml(getCarrierPackageForPhoneId(phoneId), "",
460 phoneId, carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700461 mConfigFromCarrierApp[phoneId] = config;
462 sendMessage(
463 obtainMessage(
464 EVENT_FETCH_CARRIER_DONE, phoneId, -1));
465 }
466 };
467 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800468 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700469 ICarrierService carrierService =
470 ICarrierService.Stub.asInterface(conn.service);
471 carrierService.getCarrierConfig(carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800472 logdWithLocalLog("Fetch config for carrier app: "
chen xudb04c292019-03-26 17:01:15 -0700473 + getCarrierPackageForPhoneId(phoneId)
474 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700475 } catch (RemoteException e) {
476 loge("Failed to get carrier config: " + e.toString());
Jordan Liu178430d2020-02-10 14:32:15 -0800477 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700478 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800479 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700480 sendMessageDelayed(
481 obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1),
482 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800483 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700484 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800485
Jonathan Basseri930701e2015-06-11 20:56:43 -0700486 case EVENT_BIND_CARRIER_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800487 case EVENT_FETCH_CARRIER_TIMEOUT: {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800488 loge("Bind/fetch from carrier app timeout, package="
489 + getCarrierPackageForPhoneId(phoneId));
chen xuf60b3162019-05-01 21:31:05 -0700490 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
491 // If we attempted to bind to the app, but the service connection is null due to
492 // the race condition that clear config event happens before bind/fetch complete
493 // then config was cleared while we were waiting and we should not continue.
494 if (mServiceConnection[phoneId] != null) {
495 // If a ResponseReceiver callback is in the queue when this happens, we will
496 // unbind twice and throw an exception.
Jordan Liu178430d2020-02-10 14:32:15 -0800497 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700498 broadcastConfigChangedIntent(phoneId);
499 }
Nathan Harold1122e3d2021-01-22 15:45:24 -0800500 // Put a stub bundle in place so that the rest of the logic continues smoothly.
501 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
Nathan Harold48ac0972019-03-13 22:33:01 -0700502 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700503 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700504 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800505 case EVENT_FETCH_CARRIER_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700506 // If we attempted to bind to the app, but the service connection is null, then
507 // config was cleared while we were waiting and we should not continue.
508 if (!msg.getData().getBoolean("loaded_from_xml", false)
509 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800510 break;
511 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700512 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800513 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700514 }
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700515
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800516 case EVENT_CHECK_SYSTEM_UPDATE: {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700517 SharedPreferences sharedPrefs =
518 PreferenceManager.getDefaultSharedPreferences(mContext);
519 final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
520 if (!Build.FINGERPRINT.equals(lastFingerprint)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800521 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700522 "Build fingerprint changed. old: "
523 + lastFingerprint
524 + " new: "
525 + Build.FINGERPRINT);
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700526 clearCachedConfigForPackage(null);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700527 sharedPrefs
528 .edit()
529 .putString(KEY_FINGERPRINT, Build.FINGERPRINT)
530 .apply();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700531 }
532 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700533 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700534
535 case EVENT_SUBSCRIPTION_INFO_UPDATED:
536 broadcastConfigChangedIntent(phoneId);
537 break;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700538 case EVENT_MULTI_SIM_CONFIG_CHANGED:
539 onMultiSimConfigChanged();
540 break;
Jayachandran C645ec612020-01-10 10:30:25 -0800541
542 case EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG: {
543 PersistableBundle config =
544 restoreNoSimConfigFromXml(mPlatformCarrierConfigPackage);
545
546 if (config != null) {
547 logd("Loaded no SIM config from XML. package="
548 + mPlatformCarrierConfigPackage);
549 mNoSimConfig = config;
550 sendMessage(
551 obtainMessage(
552 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE,
553 phoneId, -1));
554 } else {
555 // No cached config, so fetch it from the default app.
556 if (bindToConfigPackage(
557 mPlatformCarrierConfigPackage,
558 phoneId,
559 EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG)) {
560 sendMessageDelayed(
561 obtainMessage(
562 EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT,
563 phoneId, -1), BIND_TIMEOUT_MILLIS);
564 } else {
565 broadcastConfigChangedIntent(phoneId, false);
566 // TODO: We *must* call unbindService even if bindService returns false.
567 // (And possibly if SecurityException was thrown.)
568 loge("binding to default app to fetch no SIM config: "
569 + mPlatformCarrierConfigPackage + " fails");
570 }
571 }
572 break;
573 }
574
575 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE: {
576 broadcastConfigChangedIntent(phoneId, false);
577 break;
578 }
579
580 case EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
581 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT: {
582 loge("Bind/fetch time out for no SIM config from "
583 + mPlatformCarrierConfigPackage);
584 removeMessages(EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
585 // If we attempted to bind to the app, but the service connection is null due to
586 // the race condition that clear config event happens before bind/fetch complete
587 // then config was cleared while we were waiting and we should not continue.
588 if (mServiceConnectionForNoSimConfig[phoneId] != null) {
589 // If a ResponseReceiver callback is in the queue when this happens, we will
590 // unbind twice and throw an exception.
591 unbindIfBoundForNoSimConfig(mContext,
592 mServiceConnectionForNoSimConfig[phoneId], phoneId);
593 }
594 broadcastConfigChangedIntent(phoneId, false);
595 break;
596 }
597
598 case EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG: {
599 removeMessages(EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
600 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
601 // If new service connection has been created, unbind.
602 if (mServiceConnectionForNoSimConfig[phoneId] != conn || conn.service == null) {
603 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
604 break;
605 }
606
607 // ResultReceiver callback will execute in this Handler's thread.
608 final ResultReceiver resultReceiver =
609 new ResultReceiver(this) {
610 @Override
611 public void onReceiveResult(int resultCode, Bundle resultData) {
612 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
613 // If new service connection has been created, this is stale.
614 if (mServiceConnectionForNoSimConfig[phoneId] != conn) {
615 loge("Received response for stale request.");
616 return;
617 }
618 removeMessages(EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
619 if (resultCode == RESULT_ERROR || resultData == null) {
620 // On error, abort config fetching.
621 loge("Failed to get no SIM carrier config");
622 return;
623 }
624 PersistableBundle config =
625 resultData.getParcelable(KEY_CONFIG_BUNDLE);
626 saveNoSimConfigToXml(mPlatformCarrierConfigPackage, config);
627 mNoSimConfig = config;
628 sendMessage(
629 obtainMessage(
630 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE,
631 phoneId, -1));
632 }
633 };
634 // Now fetch the config asynchronously from the ICarrierService.
635 try {
636 ICarrierService carrierService =
637 ICarrierService.Stub.asInterface(conn.service);
638 carrierService.getCarrierConfig(null, resultReceiver);
639 logdWithLocalLog("Fetch no sim config from default app: "
640 + mPlatformCarrierConfigPackage);
641 } catch (RemoteException e) {
642 loge("Failed to get no sim carrier config from default app: " +
643 mPlatformCarrierConfigPackage + " err: " + e.toString());
644 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
645 break; // So we don't set a timeout.
646 }
647 sendMessageDelayed(
648 obtainMessage(
649 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT,
650 phoneId, -1), BIND_TIMEOUT_MILLIS);
651 break;
652 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800653 }
654 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700655 }
656
657 private final Handler mHandler;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800658
659 /**
660 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
661 * receiver for relevant events.
662 */
663 private CarrierConfigLoader(Context context) {
664 mContext = context;
Meng Wang33ad2bc2017-03-16 20:21:20 -0700665 mPlatformCarrierConfigPackage =
666 mContext.getString(R.string.platform_carrier_config_package);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700667 mHandler = new ConfigHandler();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800668
Nanxi Chen3d670502016-03-17 16:32:09 -0700669 IntentFilter bootFilter = new IntentFilter();
670 bootFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
671 context.registerReceiver(mBootReceiver, bootFilter);
672
Junda Liu8a8a53a2015-05-15 16:19:57 -0700673 // Register for package updates. Update app or uninstall app update will have all 3 intents,
674 // in the order or removed, added, replaced, all with extra_replace set to true.
675 IntentFilter pkgFilter = new IntentFilter();
676 pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
677 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
678 pkgFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
679 pkgFilter.addDataScheme("package");
Jordan Liu5ca24762019-07-10 12:51:09 -0700680 context.registerReceiver(mPackageReceiver, pkgFilter);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800681
Malcolm Chen978dda52019-10-08 18:15:25 -0700682 int numPhones = TelephonyManager.from(context).getSupportedModemCount();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700683 mConfigFromDefaultApp = new PersistableBundle[numPhones];
684 mConfigFromCarrierApp = new PersistableBundle[numPhones];
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100685 mPersistentOverrideConfigs = new PersistableBundle[numPhones];
Hall Liu506724b2018-10-22 18:16:14 -0700686 mOverrideConfigs = new PersistableBundle[numPhones];
Jayachandran C645ec612020-01-10 10:30:25 -0800687 mNoSimConfig = new PersistableBundle();
Zach Johnson36d7aab2015-05-22 15:43:00 -0700688 mServiceConnection = new CarrierServiceConnection[numPhones];
Jordan Liu178430d2020-02-10 14:32:15 -0800689 mServiceBound = new boolean[numPhones];
Junda Liu03aad762017-07-21 13:32:17 -0700690 mHasSentConfigChange = new boolean[numPhones];
Sarah Chin807d5c62020-03-30 17:21:09 -0700691 mFromSystemUnlocked = new boolean[numPhones];
Jayachandran C645ec612020-01-10 10:30:25 -0800692 mServiceConnectionForNoSimConfig = new CarrierServiceConnection[numPhones];
693 mServiceBoundForNoSimConfig = new boolean[numPhones];
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800694 // Make this service available through ServiceManager.
Peter Wangc035ce42020-01-08 21:00:22 -0800695 TelephonyFrameworkInitializer
696 .getTelephonyServiceManager().getCarrierConfigServiceRegisterer().register(this);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800697 logd("CarrierConfigLoader has started");
Nathan Harold48ac0972019-03-13 22:33:01 -0700698 mSubscriptionInfoUpdater = PhoneFactory.getSubscriptionInfoUpdater();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700699 mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800700 }
701
702 /**
703 * Initialize the singleton CarrierConfigLoader instance.
704 *
705 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
706 */
707 /* package */
708 static CarrierConfigLoader init(Context context) {
709 synchronized (CarrierConfigLoader.class) {
710 if (sInstance == null) {
711 sInstance = new CarrierConfigLoader(context);
712 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700713 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800714 }
715 return sInstance;
716 }
717 }
718
Jayachandran C645ec612020-01-10 10:30:25 -0800719 private void clearConfigForPhone(int phoneId, boolean fetchNoSimConfig) {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700720 /* Ignore clear configuration request if device is being shutdown. */
721 Phone phone = PhoneFactory.getPhone(phoneId);
722 if (phone != null) {
723 if (phone.isShuttingDown()) {
724 return;
725 }
726 }
727
728 mConfigFromDefaultApp[phoneId] = null;
729 mConfigFromCarrierApp[phoneId] = null;
730 mServiceConnection[phoneId] = null;
731 mHasSentConfigChange[phoneId] = false;
732
Jayachandran C645ec612020-01-10 10:30:25 -0800733 if (fetchNoSimConfig) {
734 // To fetch no SIM config
735 mHandler.sendMessage(
736 mHandler.obtainMessage(
737 EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG, phoneId, -1));
738 }
Malcolm Chen5ea18532019-10-24 19:55:44 -0700739 }
740
Nathan Harold48ac0972019-03-13 22:33:01 -0700741 private void notifySubscriptionInfoUpdater(int phoneId) {
742 String configPackagename;
743 PersistableBundle configToSend;
744 int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
745 // Prefer the carrier privileged carrier app, but if there is not one, use the platform
746 // default carrier app.
747 if (mConfigFromCarrierApp[phoneId] != null) {
748 configPackagename = getCarrierPackageForPhoneId(phoneId);
749 configToSend = mConfigFromCarrierApp[phoneId];
750 } else {
751 configPackagename = mPlatformCarrierConfigPackage;
752 configToSend = mConfigFromDefaultApp[phoneId];
753 }
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700754
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800755 if (configToSend == null) {
756 configToSend = new PersistableBundle();
757 }
758
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700759 // mOverrideConfigs is for testing. And it will override current configs.
760 PersistableBundle config = mOverrideConfigs[phoneId];
761 if (config != null) {
Torbjorn Eklund1050cb02018-11-16 14:05:38 +0100762 configToSend = new PersistableBundle(configToSend);
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700763 configToSend.putAll(config);
764 }
765
Nathan Harold48ac0972019-03-13 22:33:01 -0700766 mSubscriptionInfoUpdater.updateSubscriptionByCarrierConfigAndNotifyComplete(
767 phoneId, configPackagename, configToSend,
768 mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1));
769 }
770
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800771 private void broadcastConfigChangedIntent(int phoneId) {
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800772 broadcastConfigChangedIntent(phoneId, true);
773 }
774
775 private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800776 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800777 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
chen xuf9db49f2019-03-31 23:04:42 -0700778 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800779 if (addSubIdExtra) {
780 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
781 int[] subIds = SubscriptionManager.getSubId(phoneId);
782 if (!ArrayUtils.isEmpty(subIds)) {
783 TelephonyManager telMgr = TelephonyManager.from(mContext)
784 .createForSubscriptionId(subIds[0]);
785 simApplicationState = telMgr.getSimApplicationState();
786 }
Malcolm Chenf7635ec2020-04-15 15:45:00 -0700787 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId
788 + " simApplicationState " + simApplicationState);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800789 // Include subId/carrier id extra only if SIM records are loaded
790 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
791 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
792 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
793 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID,
794 getSpecificCarrierIdForPhoneId(phoneId));
795 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, getCarrierIdForPhoneId(phoneId));
796 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800797 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800798 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Sarah Chin807d5c62020-03-30 17:21:09 -0700799 intent.putExtra(CarrierConfigManager.EXTRA_REBROADCAST_ON_UNLOCK,
800 mFromSystemUnlocked[phoneId]);
Meng Wang97a6a462020-01-23 16:22:16 -0800801 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
Jack Yue9e95be2020-03-22 10:56:06 -0700802 int[] subIds = SubscriptionManager.getSubId(phoneId);
803 if (subIds != null && subIds.length > 0) {
804 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId + ", subId=" + subIds[0]);
805 } else {
806 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
807 }
Junda Liu03aad762017-07-21 13:32:17 -0700808 mHasSentConfigChange[phoneId] = true;
Sarah Chin807d5c62020-03-30 17:21:09 -0700809 mFromSystemUnlocked[phoneId] = false;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800810 }
811
812 /** Binds to the default or carrier config app. */
813 private boolean bindToConfigPackage(String pkgName, int phoneId, int eventId) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800814 logdWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700815 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700816 carrierService.setPackage(pkgName);
Jayachandran C645ec612020-01-10 10:30:25 -0800817 CarrierServiceConnection serviceConnection = new CarrierServiceConnection(
818 phoneId, pkgName, eventId);
819 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
820 mServiceConnectionForNoSimConfig[phoneId] = serviceConnection;
821 } else {
822 mServiceConnection[phoneId] = serviceConnection;
823 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800824 try {
Jayachandran C645ec612020-01-10 10:30:25 -0800825 if (mContext.bindService(carrierService, serviceConnection,
Jordan Liu178430d2020-02-10 14:32:15 -0800826 Context.BIND_AUTO_CREATE)) {
Jayachandran C645ec612020-01-10 10:30:25 -0800827 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
828 mServiceBoundForNoSimConfig[phoneId] = true;
829 } else {
830 mServiceBound[phoneId] = true;
831 }
Jordan Liu178430d2020-02-10 14:32:15 -0800832 return true;
833 } else {
834 return false;
835 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800836 } catch (SecurityException ex) {
837 return false;
838 }
839 }
840
chen xu02581692018-11-11 19:03:44 -0800841 private CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800842 String mcc = "";
843 String mnc = "";
844 String imsi = "";
845 String gid1 = "";
846 String gid2 = "";
847 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
848 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800849 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800850 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700851 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
852 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800853 mcc = simOperator.substring(0, 3);
854 mnc = simOperator.substring(3);
855 }
856 Phone phone = PhoneFactory.getPhone(phoneId);
857 if (phone != null) {
858 imsi = phone.getSubscriberId();
859 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700860 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800861 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800862 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800863 }
chen xua31f22b2019-03-06 15:28:50 -0800864 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800865 }
866
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700867 /** Returns the package name of a priveleged carrier app, or null if there is none. */
Nathan Harold1122e3d2021-01-22 15:45:24 -0800868 @Nullable
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700869 private String getCarrierPackageForPhoneId(int phoneId) {
870 List<String> carrierPackageNames = TelephonyManager.from(mContext)
871 .getCarrierPackageNamesForIntentAndPhone(
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700872 new Intent(CarrierService.CARRIER_SERVICE_INTERFACE), phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700873 if (carrierPackageNames != null && carrierPackageNames.size() > 0) {
874 return carrierPackageNames.get(0);
875 } else {
876 return null;
877 }
878 }
879
880 private String getIccIdForPhoneId(int phoneId) {
881 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
882 return null;
883 }
884 Phone phone = PhoneFactory.getPhone(phoneId);
885 if (phone == null) {
886 return null;
887 }
888 return phone.getIccSerialNumber();
889 }
890
chen xu02581692018-11-11 19:03:44 -0800891 /**
chen xua31f22b2019-03-06 15:28:50 -0800892 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800893 */
chen xua31f22b2019-03-06 15:28:50 -0800894 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700895 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800896 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700897 }
898 Phone phone = PhoneFactory.getPhone(phoneId);
899 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800900 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700901 }
chen xua31f22b2019-03-06 15:28:50 -0800902 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800903 }
904
905 /**
906 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
907 */
908 private int getCarrierIdForPhoneId(int phoneId) {
909 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
910 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700911 }
chen xu02581692018-11-11 19:03:44 -0800912 Phone phone = PhoneFactory.getPhone(phoneId);
913 if (phone == null) {
914 return TelephonyManager.UNKNOWN_CARRIER_ID;
915 }
916 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700917 }
918
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700919 /**
920 * Writes a bundle to an XML file.
921 *
chen xu02581692018-11-11 19:03:44 -0800922 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -0800923 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -0800924 * should have a single copy of XML file named after carrier id. However, it's still possible
925 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
926 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
927 * carrier while iccid remains the same.
928 *
929 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
930 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700931 *
932 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700933 *
Jayachandran C645ec612020-01-10 10:30:25 -0800934 * @param packageName the name of the package from which we fetched this bundle.
935 * @param extraString An extra string to be used in the XML file name.
936 * @param phoneId the phone ID.
937 * @param carrierId contains all carrier-identifying information.
938 * @param config the bundle to be written. Null will be treated as an empty bundle.
939 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700940 */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100941 private void saveConfigToXml(String packageName, @NonNull String extraString, int phoneId,
Jayachandran C645ec612020-01-10 10:30:25 -0800942 CarrierIdentifier carrierId, PersistableBundle config, boolean isNoSimConfig) {
943 if (packageName == null) {
944 loge("Cannot save config with null packageName");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800945 return;
946 }
947
Jayachandran C645ec612020-01-10 10:30:25 -0800948 String fileName;
949 if (isNoSimConfig) {
950 fileName = getFilenameForNoSimConfig(packageName);
951 } else {
952 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
953 != TelephonyManager.SIM_STATE_LOADED) {
954 loge("Skip save config because SIM records are not loaded.");
955 return;
956 }
957
958 final String iccid = getIccIdForPhoneId(phoneId);
959 final int cid = carrierId.getSpecificCarrierId();
960 if (iccid == null) {
961 loge("Cannot save config with null iccid.");
962 return;
963 }
964 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700965 }
Jayachandran C645ec612020-01-10 10:30:25 -0800966
Junda Liu02596502016-11-15 13:46:43 -0800967 // b/32668103 Only save to file if config isn't empty.
968 // In case of failure, not caching an empty bundle will
969 // try loading config again on next power on or sim loaded.
970 // Downside is for genuinely empty bundle, will bind and load
971 // on every power on.
972 if (config == null || config.isEmpty()) {
973 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700974 }
975
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700976 final String version = getPackageVersion(packageName);
977 if (version == null) {
978 loge("Failed to get package version for: " + packageName);
979 return;
980 }
981
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800982 logdWithLocalLog(
983 "Save config to xml, packagename: " + packageName + " phoneId: " + phoneId);
chen xudb04c292019-03-26 17:01:15 -0700984
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700985 FileOutputStream outFile = null;
986 try {
Jayachandran C645ec612020-01-10 10:30:25 -0800987 outFile = new FileOutputStream(new File(mContext.getFilesDir(), fileName));
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800988 config.putString(KEY_VERSION, version);
989 config.writeToStream(outFile);
990 outFile.flush();
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700991 outFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800992 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700993 loge(e.toString());
994 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700995 }
996
Jayachandran C645ec612020-01-10 10:30:25 -0800997 private void saveConfigToXml(String packageName, @NonNull String extraString, int phoneId,
998 CarrierIdentifier carrierId, PersistableBundle config) {
999 saveConfigToXml(packageName, extraString, phoneId, carrierId, config, false);
1000 }
1001
1002 private void saveNoSimConfigToXml(String packageName, PersistableBundle config) {
1003 saveConfigToXml(packageName, "", -1, null, config, true);
1004 }
1005
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001006 /**
1007 * Reads a bundle from an XML file.
1008 *
1009 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +08001010 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001011 *
1012 * In case of errors, or if the saved config is from a different package version than the
1013 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001014 *
Jayachandran C645ec612020-01-10 10:30:25 -08001015 * @param packageName the name of the package from which we fetched this bundle.
1016 * @param extraString An extra string to be used in the XML file name.
1017 * @param phoneId the phone ID.
1018 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001019 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001020 * version does not match, or reading config fails.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001021 */
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001022 private PersistableBundle restoreConfigFromXml(String packageName, @NonNull String extraString,
Jayachandran C645ec612020-01-10 10:30:25 -08001023 int phoneId, boolean isNoSimConfig) {
1024 if (packageName == null) {
1025 loge("Cannot restore config with null packageName");
1026 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001027 final String version = getPackageVersion(packageName);
1028 if (version == null) {
1029 loge("Failed to get package version for: " + packageName);
1030 return null;
1031 }
Yuchen Dongc15afed2018-04-26 17:05:22 +08001032
Jayachandran C645ec612020-01-10 10:30:25 -08001033 String fileName;
1034 if (isNoSimConfig) {
1035 fileName = getFilenameForNoSimConfig(packageName);
1036 } else {
1037 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
1038 != TelephonyManager.SIM_STATE_LOADED) {
1039 loge("Skip restore config because SIM records are not loaded.");
1040 return null;
1041 }
1042
1043 final String iccid = getIccIdForPhoneId(phoneId);
1044 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1045 if (iccid == null) {
1046 loge("Cannot restore config with null iccid.");
1047 return null;
1048 }
1049 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001050 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001051
1052 PersistableBundle restoredBundle = null;
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001053 File file = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001054 FileInputStream inFile = null;
1055 try {
Jayachandran C645ec612020-01-10 10:30:25 -08001056 file = new File(mContext.getFilesDir(),fileName);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001057 inFile = new FileInputStream(file);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001058
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001059 restoredBundle = PersistableBundle.readFromStream(inFile);
1060 String savedVersion = restoredBundle.getString(KEY_VERSION);
1061 restoredBundle.remove(KEY_VERSION);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001062
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001063 if (!version.equals(savedVersion)) {
1064 loge("Saved version mismatch: " + version + " vs " + savedVersion);
1065 restoredBundle = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001066 }
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001067
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001068 inFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001069 } catch (FileNotFoundException e) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001070 // Missing file is normal occurrence that might occur with a new sim or when restoring
1071 // an override file during boot and should not be treated as an error.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001072 if (file != null) logd("File not found: " + file.getPath());
1073 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001074 loge(e.toString());
1075 }
1076
1077 return restoredBundle;
1078 }
1079
Jayachandran C645ec612020-01-10 10:30:25 -08001080 private PersistableBundle restoreConfigFromXml(String packageName, @NonNull String extraString,
1081 int phoneId) {
1082 return restoreConfigFromXml(packageName, extraString, phoneId, false);
1083 }
1084
1085 private PersistableBundle restoreNoSimConfigFromXml(String packageName) {
1086 return restoreConfigFromXml(packageName, "", -1, true);
1087 }
1088
Junda Liu8a8a53a2015-05-15 16:19:57 -07001089 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001090 * Clears cached carrier config.
1091 * This deletes all saved XML files associated with the given package name. If packageName is
1092 * null, then it deletes all saved XML files.
1093 *
1094 * @param packageName the name of a carrier package, or null if all cached config should be
1095 * cleared.
1096 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -07001097 */
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001098 private boolean clearCachedConfigForPackage(final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001099 File dir = mContext.getFilesDir();
1100 File[] packageFiles = dir.listFiles(new FilenameFilter() {
1101 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001102 if (packageName != null) {
1103 return filename.startsWith("carrierconfig-" + packageName + "-");
1104 } else {
1105 return filename.startsWith("carrierconfig-");
1106 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001107 }
1108 });
Junda Liu8a8a53a2015-05-15 16:19:57 -07001109 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001110 for (File f : packageFiles) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001111 logd("Deleting " + f.getName());
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001112 f.delete();
1113 }
Junda Liu8a8a53a2015-05-15 16:19:57 -07001114 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001115 }
1116
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001117 /** Builds a canonical file name for a config file. */
Jayachandran C645ec612020-01-10 10:30:25 -08001118 private static String getFilenameForConfig(
1119 @NonNull String packageName, @NonNull String extraString,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001120 @NonNull String iccid, int cid) {
chen xu02581692018-11-11 19:03:44 -08001121 // the same carrier should have a single copy of XML file named after carrier id.
1122 // However, it's still possible that platform doesn't recognize the current sim carrier,
1123 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
1124 // cases SIM OTA resolves to different carrier while iccid remains the same.
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001125 return "carrierconfig-" + packageName + extraString + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001126 }
1127
Jayachandran C645ec612020-01-10 10:30:25 -08001128 /** Builds a canonical file name for no SIM config file. */
1129 private String getFilenameForNoSimConfig(@NonNull String packageName) {
1130 return "carrierconfig-" + packageName + "-" + "nosim" + ".xml";
1131 }
1132
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001133 /** Return the current version code of a package, or null if the name is not found. */
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001134 private String getPackageVersion(String packageName) {
1135 try {
1136 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -08001137 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001138 } catch (PackageManager.NameNotFoundException e) {
1139 return null;
1140 }
1141 }
1142
Jonathan Basseri65273c82017-07-25 15:08:42 -07001143 /**
1144 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001145 *
1146 * This reads config bundles for the given phoneId. That means getting the latest bundle from
1147 * the default app and a privileged carrier app, if present. This will not bind to an app if we
1148 * have a saved config file to use instead.
1149 */
1150 private void updateConfigForPhoneId(int phoneId) {
Jonathan Basseri65273c82017-07-25 15:08:42 -07001151 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001152 }
1153
Malcolm Chen5ea18532019-10-24 19:55:44 -07001154 private void onMultiSimConfigChanged() {
1155 for (int i = TelephonyManager.from(mContext).getActiveModemCount();
1156 i < mConfigFromDefaultApp.length; i++) {
1157 clearConfigForPhone(i, false);
1158 }
1159 }
1160
Hall Liu506724b2018-10-22 18:16:14 -07001161 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001162 @NonNull
1163 public PersistableBundle getConfigForSubId(int subId, String callingPackage) {
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08001164 return getConfigForSubIdWithFeature(subId, callingPackage, null);
1165 }
1166
1167 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001168 @NonNull
1169 public PersistableBundle getConfigForSubIdWithFeature(int subId, String callingPackage,
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08001170 String callingFeatureId) {
1171 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subId, callingPackage,
1172 callingFeatureId, "getCarrierConfig")) {
Malcolm Chen6d3d6b32018-03-01 13:58:03 -08001173 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -07001174 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -07001175
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001176 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001177 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001178 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001179 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001180 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001181 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001182 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001183 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001184 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001185 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001186 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001187 config = mPersistentOverrideConfigs[phoneId];
1188 if (config != null) {
1189 retConfig.putAll(config);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001190 }
Hall Liu506724b2018-10-22 18:16:14 -07001191 config = mOverrideConfigs[phoneId];
1192 if (config != null) {
1193 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -07001194 }
Nathan Harold1122e3d2021-01-22 15:45:24 -08001195 // Ignore the theoretical case of the default app not being present since that won't
1196 // work in CarrierConfigLoader today.
1197 final boolean allConfigsApplied =
1198 (mConfigFromCarrierApp[phoneId] != null
1199 || getCarrierPackageForPhoneId(phoneId) == null)
1200 && mConfigFromDefaultApp[phoneId] != null;
1201 retConfig.putBoolean(
1202 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, allConfigsApplied);
Jayachandran C645ec612020-01-10 10:30:25 -08001203 } else {
1204 if (mNoSimConfig != null) {
1205 retConfig.putAll(mNoSimConfig);
1206 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001207 }
1208 return retConfig;
1209 }
1210
1211 @Override
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001212 public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides,
1213 boolean persistent) {
Hall Liu506724b2018-10-22 18:16:14 -07001214 mContext.enforceCallingOrSelfPermission(
1215 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Brad Ebingerf6281ad2019-04-25 11:02:04 -07001216 //TODO: Also check for SHELL UID to restrict this method to testing only (b/131326259)
Hall Liu506724b2018-10-22 18:16:14 -07001217 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1218 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001219 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
Hall Liu506724b2018-10-22 18:16:14 -07001220 return;
1221 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001222 overrideConfig(mOverrideConfigs, phoneId, overrides);
Hall Liu506724b2018-10-22 18:16:14 -07001223
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001224 if (persistent) {
1225 overrideConfig(mPersistentOverrideConfigs, phoneId, overrides);
1226
1227 if (overrides != null) {
1228 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
1229 saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId,
1230 carrierId, mPersistentOverrideConfigs[phoneId]);
1231 } else {
1232 final String iccid = getIccIdForPhoneId(phoneId);
1233 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1234 String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage,
1235 OVERRIDE_PACKAGE_ADDITION, iccid, cid);
1236 File fileToDelete = new File(mContext.getFilesDir(), fileName);
1237 fileToDelete.delete();
1238 }
Hall Liu506724b2018-10-22 18:16:14 -07001239 }
Brad Ebingerc9d1fa12019-04-17 15:21:18 -07001240 notifySubscriptionInfoUpdater(phoneId);
Hall Liu506724b2018-10-22 18:16:14 -07001241 }
1242
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001243 private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId,
1244 @Nullable PersistableBundle overrides) {
1245 if (overrides == null) {
1246 currentOverrides[phoneId] = new PersistableBundle();
1247 } else if (currentOverrides[phoneId] == null) {
1248 currentOverrides[phoneId] = overrides;
1249 } else {
1250 currentOverrides[phoneId].putAll(overrides);
1251 }
1252 }
1253
Hall Liu506724b2018-10-22 18:16:14 -07001254 @Override
Jonathan Basseri86030352015-06-08 12:27:56 -07001255 public void notifyConfigChangedForSubId(int subId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001256 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001257 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001258 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001259 return;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001260 }
Jordan Liud7d57fe2019-04-16 12:29:43 -07001261
1262 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001263 // system privileged app with MODIFY_PHONE_STATE permission.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001264 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext, subId,
1265 "Require carrier privileges or MODIFY_PHONE_STATE permission.");
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001266
1267 // This method should block until deleting has completed, so that an error which prevents us
1268 // from clearing the cache is passed back to the carrier app. With the files successfully
1269 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001270 String callingPackageName = mContext.getPackageManager().getNameForUid(
1271 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001272 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001273 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001274 }
1275
1276 @Override
1277 public void updateConfigForPhoneId(int phoneId, String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -07001278 mContext.enforceCallingOrSelfPermission(
1279 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001280 logdWithLocalLog("Update config for phoneId: " + phoneId + " simState: " + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001281 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1282 return;
1283 }
1284 // requires Java 7 for switch on string.
1285 switch (simState) {
1286 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1287 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001288 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001289 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Malcolm Chendb66de12019-12-09 18:57:07 -08001290 case IccCardConstants.INTENT_VALUE_ICC_NOT_READY:
Junda Liu9f2d2712015-05-15 14:22:45 -07001291 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001292 break;
1293 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1294 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001295 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001296 break;
1297 }
1298 }
1299
Junda Liu43d723a2015-05-12 17:23:45 -07001300 @Override
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001301 public String getDefaultCarrierServicePackageName() {
Shuo Qianefdb7962019-12-19 15:54:27 -08001302 mContext.enforceCallingOrSelfPermission(
1303 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1304 "getDefaultCarrierServicePackageName");
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001305 return mPlatformCarrierConfigPackage;
1306 }
1307
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001308 private void unbindIfBound(Context context, CarrierServiceConnection conn,
1309 int phoneId) {
1310 if (mServiceBound[phoneId]) {
1311 mServiceBound[phoneId] = false;
1312 context.unbindService(conn);
1313 }
1314 }
1315
Jayachandran C645ec612020-01-10 10:30:25 -08001316 private void unbindIfBoundForNoSimConfig(Context context, CarrierServiceConnection conn,
1317 int phoneId) {
1318 if (mServiceBoundForNoSimConfig[phoneId]) {
1319 mServiceBoundForNoSimConfig[phoneId] = false;
1320 context.unbindService(conn);
1321 }
1322 }
1323
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001324 /**
1325 * If {@code args} contains {@link #DUMP_ARG_REQUESTING_PACKAGE} and a following package name,
1326 * we'll also call {@link IBinder#dump} on the default carrier service (if bound) and the
1327 * specified carrier service (if bound). Typically, this is done for connectivity bug reports
1328 * where we don't call {@code dumpsys activity service all-non-platform} because that contains
1329 * too much info, but we still want to let carrier apps include their diagnostics.
1330 */
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001331 @Override
Junda Liu43d723a2015-05-12 17:23:45 -07001332 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001333 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
Junda Liu43d723a2015-05-12 17:23:45 -07001334 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1335 != PackageManager.PERMISSION_GRANTED) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001336 indentPW.println("Permission Denial: can't dump carrierconfig from from pid="
Junda Liu43d723a2015-05-12 17:23:45 -07001337 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1338 return;
1339 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001340 String requestingPackage = null;
1341 int requestingPackageIndex = ArrayUtils.indexOf(args, DUMP_ARG_REQUESTING_PACKAGE);
1342 if (requestingPackageIndex >= 0 && requestingPackageIndex < args.length - 1
1343 && !TextUtils.isEmpty(args[requestingPackageIndex + 1])) {
1344 requestingPackage = args[requestingPackageIndex + 1];
1345 // Throws a SecurityException if the caller is impersonating another app in an effort to
1346 // dump extra info (which may contain PII the caller doesn't have a right to).
1347 enforceCallerIsSystemOrRequestingPackage(requestingPackage);
1348 }
1349
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001350 indentPW.println("CarrierConfigLoader: " + this);
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001351 for (int i = 0; i < TelephonyManager.from(mContext).getActiveModemCount(); i++) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001352 indentPW.println("Phone Id = " + i);
shuoq26a3a4c2016-12-16 11:06:48 -08001353 // display default values in CarrierConfigManager
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001354 printConfig(CarrierConfigManager.getDefaultConfig(), indentPW,
shuoq26a3a4c2016-12-16 11:06:48 -08001355 "Default Values from CarrierConfigManager");
shuoq26a3a4c2016-12-16 11:06:48 -08001356 // display ConfigFromDefaultApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001357 printConfig(mConfigFromDefaultApp[i], indentPW, "mConfigFromDefaultApp");
shuoq26a3a4c2016-12-16 11:06:48 -08001358 // display ConfigFromCarrierApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001359 printConfig(mConfigFromCarrierApp[i], indentPW, "mConfigFromCarrierApp");
1360 printConfig(mPersistentOverrideConfigs[i], indentPW, "mPersistentOverrideConfigs");
1361 printConfig(mOverrideConfigs[i], indentPW, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001362 }
chen xudb04c292019-03-26 17:01:15 -07001363
Jayachandran C645ec612020-01-10 10:30:25 -08001364 printConfig(mNoSimConfig, indentPW, "mNoSimConfig");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001365 indentPW.println("CarrierConfigLoadingLog=");
1366 mCarrierConfigLoadingLog.dump(fd, indentPW, args);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001367
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001368 if (requestingPackage != null) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001369 logd("Including default and requesting package " + requestingPackage
1370 + " carrier services in dump");
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001371 indentPW.println("");
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001372 indentPW.println("Connected services");
1373 dumpCarrierServiceIfBound(fd, indentPW, "Default config package",
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001374 mPlatformCarrierConfigPackage, false /* considerCarrierPrivileges */);
1375 dumpCarrierServiceIfBound(fd, indentPW, "Requesting package", requestingPackage,
1376 true /* considerCarrierPrivileges */);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001377 }
shuoq26a3a4c2016-12-16 11:06:48 -08001378 }
1379
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001380 private void printConfig(PersistableBundle configApp, IndentingPrintWriter indentPW,
1381 String name) {
1382 indentPW.increaseIndent();
shuoq26a3a4c2016-12-16 11:06:48 -08001383 if (configApp == null) {
shuoq26a3a4c2016-12-16 11:06:48 -08001384 indentPW.println(name + " : null ");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001385 indentPW.decreaseIndent();
1386 indentPW.println("");
shuoq26a3a4c2016-12-16 11:06:48 -08001387 return;
1388 }
shuoq26a3a4c2016-12-16 11:06:48 -08001389 indentPW.println(name + " : ");
1390 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1391 Collections.sort(sortedKeys);
1392 indentPW.increaseIndent();
1393 indentPW.increaseIndent();
1394 for (String key : sortedKeys) {
1395 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1396 indentPW.println(key + " = " +
1397 Arrays.toString((Object[]) configApp.get(key)));
1398 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1399 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1400 } else {
1401 indentPW.println(key + " = " + configApp.get(key));
1402 }
Junda Liu43d723a2015-05-12 17:23:45 -07001403 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001404 indentPW.decreaseIndent();
1405 indentPW.decreaseIndent();
1406 indentPW.decreaseIndent();
1407 indentPW.println("");
Junda Liu43d723a2015-05-12 17:23:45 -07001408 }
1409
Hunter Knepshielde601f432020-02-19 10:16:04 -08001410 /**
1411 * Passes without problem when one of these conditions is true:
1412 * - The caller is a privileged UID (e.g. for dumpstate.cpp generating a bug report, where the
1413 * system knows the true caller plumbed in through the {@link android.os.BugreportManager} API).
1414 * - The caller's UID matches the supplied package.
1415 *
1416 * @throws SecurityException if none of the above conditions are met.
1417 */
1418 private void enforceCallerIsSystemOrRequestingPackage(String requestingPackage)
1419 throws SecurityException {
1420 final int callingUid = Binder.getCallingUid();
1421 if (callingUid == Process.ROOT_UID || callingUid == Process.SYSTEM_UID
1422 || callingUid == Process.SHELL_UID || callingUid == Process.PHONE_UID) {
1423 // Bug reports (dumpstate.cpp) run as SHELL, and let some other privileged UIDs through
1424 // as well.
1425 return;
1426 }
1427 // An app is trying to dump extra detail, block it if they aren't who they claim to be.
1428 AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class);
1429 if (appOps == null) {
1430 throw new SecurityException("No AppOps");
1431 }
1432 // Will throw a SecurityException if the UID and package don't match.
1433 appOps.checkPackage(callingUid, requestingPackage);
1434 }
1435
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001436 /**
1437 * Searches for one or more appropriate {@link CarrierService} instances to dump based on the
1438 * current connections.
1439 *
1440 * @param targetPkgName the target package name to dump carrier services for
1441 * @param considerCarrierPrivileges if true, allow a carrier service to be dumped if it shares
1442 * carrier privileges with {@code targetPkgName};
1443 * otherwise, only dump a carrier service if it is {@code
1444 * targetPkgName}
1445 */
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001446 private void dumpCarrierServiceIfBound(FileDescriptor fd, IndentingPrintWriter indentPW,
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001447 String prefix, String targetPkgName, boolean considerCarrierPrivileges) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001448 // Null package is possible if it's early in the boot process, there was a recent crash, we
1449 // loaded the config from XML most recently, or a SIM slot is empty. Carrier apps with
1450 // long-lived bindings should typically get dumped here regardless. Even if an app is being
1451 // used for multiple phoneIds, we assume that it's smart enough to handle that on its own,
1452 // and that in most cases we'd just be dumping duplicate information and bloating a report.
1453 indentPW.increaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001454 indentPW.println(prefix + " : " + targetPkgName);
1455 Set<String> dumpedPkgNames = new ArraySet<>(mServiceConnection.length);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001456 for (CarrierServiceConnection connection : mServiceConnection) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001457 if (connection == null || !SubscriptionManager.isValidPhoneId(connection.phoneId)
1458 || TextUtils.isEmpty(connection.pkgName)) {
1459 continue;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001460 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001461 final String servicePkgName = connection.pkgName;
1462 // Note: we intentionally ignore system components here because we should NOT match the
1463 // shell caller that's typically used for bug reports via non-BugreportManager triggers.
1464 final boolean exactPackageMatch = TextUtils.equals(targetPkgName, servicePkgName);
1465 final boolean carrierPrivilegesMatch =
1466 considerCarrierPrivileges && hasCarrierPrivileges(targetPkgName,
1467 connection.phoneId);
1468 if (!exactPackageMatch && !carrierPrivilegesMatch) continue;
1469 // Make sure this service is actually alive before trying to dump it. We don't pay
1470 // attention to mServiceBound[connection.phoneId] because typically carrier apps will
1471 // request long-lived bindings, and even if we unbind the app, it may still be alive due
1472 // to CarrierServiceBindHelper. Pull it out as a reference so even if it gets set to
1473 // null within the ServiceConnection during unbinding we can avoid an NPE.
1474 final IBinder service = connection.service;
1475 if (service == null || !service.isBinderAlive() || !service.pingBinder()) continue;
1476 // We've got a live service. Last check is just to make sure we don't dump a package
1477 // multiple times.
1478 if (!dumpedPkgNames.add(servicePkgName)) continue;
1479 if (!exactPackageMatch) {
1480 logd(targetPkgName + " has carrier privileges on phoneId " + connection.phoneId
1481 + ", service provided by " + servicePkgName);
1482 indentPW.increaseIndent();
1483 indentPW.println("Proxy : " + servicePkgName);
1484 indentPW.decreaseIndent();
1485 }
1486 // Flush before we let the app output anything to ensure correct ordering of output.
1487 // Internally, Binder#dump calls flush on its printer after finishing so we don't
1488 // need to do anything after.
1489 indentPW.flush();
1490 try {
1491 logd("Dumping " + servicePkgName);
1492 // We don't need to give the carrier service any args.
1493 connection.service.dump(fd, null /* args */);
1494 logd("Done with " + servicePkgName);
1495 } catch (RemoteException e) {
1496 logd("RemoteException from " + servicePkgName, e);
1497 indentPW.increaseIndent();
1498 indentPW.println("RemoteException");
1499 indentPW.increaseIndent();
1500 e.printStackTrace(indentPW);
1501 indentPW.decreaseIndent();
1502 indentPW.decreaseIndent();
1503 // We won't retry this package again because now it's in dumpedPkgNames.
1504 }
1505 indentPW.println("");
Jordan Liu46ed5db2020-01-28 08:55:20 -08001506 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001507 if (dumpedPkgNames.isEmpty()) {
1508 indentPW.increaseIndent();
1509 indentPW.println("Not bound");
1510 indentPW.decreaseIndent();
1511 indentPW.println("");
1512 }
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001513 indentPW.decreaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001514 }
1515
1516 private boolean hasCarrierPrivileges(String pkgName, int phoneId) {
1517 int[] subIds = SubscriptionManager.getSubId(phoneId);
1518 if (ArrayUtils.isEmpty(subIds)) {
1519 return false;
1520 }
1521 return TelephonyManager.from(mContext).createForSubscriptionId(
1522 subIds[0]).checkCarrierPrivilegesForPackage(pkgName)
1523 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001524 }
1525
Zach Johnson36d7aab2015-05-22 15:43:00 -07001526 private class CarrierServiceConnection implements ServiceConnection {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001527 final int phoneId;
1528 final String pkgName;
1529 final int eventId;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001530 IBinder service;
1531
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001532 CarrierServiceConnection(int phoneId, String pkgName, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001533 this.phoneId = phoneId;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001534 this.pkgName = pkgName;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001535 this.eventId = eventId;
1536 }
1537
1538 @Override
1539 public void onServiceConnected(ComponentName name, IBinder service) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001540 logd("Connected to config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001541 this.service = service;
1542 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1543 }
1544
1545 @Override
1546 public void onServiceDisconnected(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001547 logd("Disconnected from config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001548 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001549 }
1550
1551 @Override
1552 public void onBindingDied(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001553 logd("Binding died from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001554 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001555 }
1556
1557 @Override
1558 public void onNullBinding(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001559 logd("Null binding from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001560 this.service = null;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001561 }
1562 }
1563
1564 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1565 @Override
1566 public void onReceive(Context context, Intent intent) {
1567 String action = intent.getAction();
Junda Liu8a8a53a2015-05-15 16:19:57 -07001568 boolean replace = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1569 // If replace is true, only care ACTION_PACKAGE_REPLACED.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001570 if (replace && !Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
Junda Liu8a8a53a2015-05-15 16:19:57 -07001571 return;
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001572 }
Junda Liu8a8a53a2015-05-15 16:19:57 -07001573
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001574 switch (action) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001575 case Intent.ACTION_BOOT_COMPLETED:
1576 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1577 break;
1578
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001579 case Intent.ACTION_PACKAGE_ADDED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001580 case Intent.ACTION_PACKAGE_REMOVED:
Junda Liu8a8a53a2015-05-15 16:19:57 -07001581 case Intent.ACTION_PACKAGE_REPLACED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001582 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1583 String packageName = mContext.getPackageManager().getNameForUid(uid);
Junda Liu8a8a53a2015-05-15 16:19:57 -07001584 if (packageName != null) {
1585 // We don't have a phoneId for arg1.
1586 mHandler.sendMessage(
1587 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, packageName));
1588 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001589 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001590 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001591 }
1592 }
1593
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001594 private void logd(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001595 Log.d(LOG_TAG, msg);
1596 }
1597
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001598 private void logd(String msg, Throwable tr) {
1599 Log.d(LOG_TAG, msg, tr);
1600 }
1601
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001602 private void logdWithLocalLog(String msg) {
chen xudb04c292019-03-26 17:01:15 -07001603 Log.d(LOG_TAG, msg);
1604 mCarrierConfigLoadingLog.log(msg);
1605 }
1606
1607 private void loge(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001608 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07001609 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001610 }
1611}