blob: 8409ff8d925bb4276fa63637db8d53b6626fd81e [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;
Rambo Wang1cf71512022-04-12 19:09:07 +000038import android.os.HandlerExecutor;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080039import android.os.IBinder;
Rambo Wang7e3bc122021-03-23 09:24:38 -070040import android.os.Looper;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080041import android.os.Message;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070042import android.os.PersistableBundle;
Hunter Knepshielde601f432020-02-19 10:16:04 -080043import android.os.Process;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080044import android.os.RemoteException;
Jonathan Basseri65273c82017-07-25 15:08:42 -070045import android.os.ResultReceiver;
Meng Wang97a6a462020-01-23 16:22:16 -080046import android.os.UserHandle;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070047import android.preference.PreferenceManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080048import android.service.carrier.CarrierIdentifier;
Zach Johnson36d7aab2015-05-22 15:43:00 -070049import android.service.carrier.CarrierService;
50import android.service.carrier.ICarrierService;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080051import android.telephony.CarrierConfigManager;
52import android.telephony.SubscriptionManager;
Peter Wangc035ce42020-01-08 21:00:22 -080053import android.telephony.TelephonyFrameworkInitializer;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080054import android.telephony.TelephonyManager;
rambowangc2ab1202022-10-02 11:21:08 -050055import android.telephony.TelephonyRegistryManager;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -080056import android.text.TextUtils;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080057import android.util.ArraySet;
chen xudb04c292019-03-26 17:01:15 -070058import android.util.LocalLog;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080059import android.util.Log;
60
Rambo Wang7e3bc122021-03-23 09:24:38 -070061import com.android.internal.annotations.VisibleForTesting;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080062import com.android.internal.telephony.ICarrierConfigLoader;
63import com.android.internal.telephony.IccCardConstants;
64import com.android.internal.telephony.Phone;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080065import com.android.internal.telephony.PhoneFactory;
Nathan Harold48ac0972019-03-13 22:33:01 -070066import com.android.internal.telephony.SubscriptionInfoUpdater;
Jeff Davidsona8e4e242018-03-15 17:16:18 -070067import com.android.internal.telephony.TelephonyPermissions;
Meng Wanga320b942019-11-25 11:21:26 -080068import com.android.internal.telephony.util.ArrayUtils;
shuoq26a3a4c2016-12-16 11:06:48 -080069import com.android.internal.util.IndentingPrintWriter;
arunvoddu664c36a2021-10-11 20:09:28 +000070import com.android.telephony.Rlog;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080071
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070072import java.io.File;
Junda Liu43d723a2015-05-12 17:23:45 -070073import java.io.FileDescriptor;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070074import java.io.FileInputStream;
75import java.io.FileNotFoundException;
76import java.io.FileOutputStream;
Jonathan Basseri1f743c92015-05-15 00:19:46 -070077import java.io.FilenameFilter;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070078import java.io.IOException;
Junda Liu43d723a2015-05-12 17:23:45 -070079import java.io.PrintWriter;
shuoq26a3a4c2016-12-16 11:06:48 -080080import java.util.ArrayList;
81import java.util.Arrays;
82import java.util.Collections;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080083import java.util.List;
rambowang63f4fc42022-10-03 11:54:56 -050084import java.util.Objects;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080085import java.util.Set;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080086
87/**
88 * CarrierConfigLoader binds to privileged carrier apps to fetch carrier config overlays.
Jonathan Basseri6465afd2015-02-25 13:05:57 -080089 */
Jonathan Basseri6465afd2015-02-25 13:05:57 -080090public class CarrierConfigLoader extends ICarrierConfigLoader.Stub {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070091 private static final String LOG_TAG = "CarrierConfigLoader";
Meng Wang33ad2bc2017-03-16 20:21:20 -070092
93 // Package name for platform carrier config app, bundled with system image.
Rambo Wang1b89db32022-04-15 03:00:32 +000094 @NonNull private final String mPlatformCarrierConfigPackage;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080095
96 /** The singleton instance. */
Rambo Wang1b89db32022-04-15 03:00:32 +000097 @Nullable private static CarrierConfigLoader sInstance;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080098 // The context for phone app, passed from PhoneGlobals.
Rambo Wang1b89db32022-04-15 03:00:32 +000099 @NonNull private Context mContext;
100
101 // All the states below (array indexed by phoneId) are non-null. But the member of the array
102 // is nullable, when e.g. the config for the phone is not loaded yet
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800103 // Carrier configs from default app, indexed by phoneID.
Rambo Wang1b89db32022-04-15 03:00:32 +0000104 @NonNull private PersistableBundle[] mConfigFromDefaultApp;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800105 // Carrier configs from privileged carrier config app, indexed by phoneID.
Rambo Wang1b89db32022-04-15 03:00:32 +0000106 @NonNull private PersistableBundle[] mConfigFromCarrierApp;
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100107 // Persistent Carrier configs that are provided via the override test API, indexed by phone ID.
Rambo Wang1b89db32022-04-15 03:00:32 +0000108 @NonNull private PersistableBundle[] mPersistentOverrideConfigs;
Hall Liu506724b2018-10-22 18:16:14 -0700109 // Carrier configs that are provided via the override test API, indexed by phone ID.
Rambo Wang1b89db32022-04-15 03:00:32 +0000110 @NonNull private PersistableBundle[] mOverrideConfigs;
Jayachandran C645ec612020-01-10 10:30:25 -0800111 // Carrier configs to override code default when there is no SIM inserted
Rambo Wang1b89db32022-04-15 03:00:32 +0000112 @NonNull private PersistableBundle mNoSimConfig;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800113 // Service connection for binding to config app.
Rambo Wang1b89db32022-04-15 03:00:32 +0000114 @NonNull private CarrierServiceConnection[] mServiceConnection;
Jayachandran C645ec612020-01-10 10:30:25 -0800115 // Service connection for binding to carrier config app for no SIM config.
Rambo Wang1b89db32022-04-15 03:00:32 +0000116 @NonNull private CarrierServiceConnection[] mServiceConnectionForNoSimConfig;
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700117 // Whether we are bound to a service for each phone
Rambo Wang1b89db32022-04-15 03:00:32 +0000118 @NonNull private boolean[] mServiceBound;
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700119 // Whether we are bound to a service for no SIM config
Rambo Wang1b89db32022-04-15 03:00:32 +0000120 @NonNull private boolean[] mServiceBoundForNoSimConfig;
Sarah Chin807d5c62020-03-30 17:21:09 -0700121 // Whether we have sent config change broadcast for each phone id.
Rambo Wang1b89db32022-04-15 03:00:32 +0000122 @NonNull private boolean[] mHasSentConfigChange;
Sarah Chin807d5c62020-03-30 17:21:09 -0700123 // Whether the broadcast was sent from EVENT_SYSTEM_UNLOCKED, to track rebroadcasts
Rambo Wang1b89db32022-04-15 03:00:32 +0000124 @NonNull private boolean[] mFromSystemUnlocked;
Rambo Wang1cf71512022-04-12 19:09:07 +0000125 // CarrierService change monitoring
126 @NonNull private CarrierServiceChangeCallback[] mCarrierServiceChangeCallbacks;
Rambo Wang1b89db32022-04-15 03:00:32 +0000127
Nathan Harold48ac0972019-03-13 22:33:01 -0700128 // SubscriptionInfoUpdater
Rambo Wang1b89db32022-04-15 03:00:32 +0000129 @NonNull private final SubscriptionInfoUpdater mSubscriptionInfoUpdater;
Rambo Wang1cf71512022-04-12 19:09:07 +0000130 // Broadcast receiver for system events (BootCompleted, MultiSimConfigChanged etc.)
131 @NonNull
132 private final BroadcastReceiver mSystemBroadcastReceiver = new ConfigLoaderBroadcastReceiver();
Rambo Wang1b89db32022-04-15 03:00:32 +0000133 @NonNull private final LocalLog mCarrierConfigLoadingLog = new LocalLog(100);
Rambo Wang6f8328d2022-04-15 01:26:23 +0000134 // Number of phone instances (active modem count)
135 private int mNumPhones;
chen xudb04c292019-03-26 17:01:15 -0700136
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800137
138 // Message codes; see mHandler below.
139 // Request from SubscriptionInfoUpdater when SIM becomes absent or error.
140 private static final int EVENT_CLEAR_CONFIG = 0;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800141 // Has connected to default app.
142 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
143 // Has connected to carrier app.
144 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700145 // Config has been loaded from default app (or cache).
146 private static final int EVENT_FETCH_DEFAULT_DONE = 5;
147 // Config has been loaded from carrier app (or cache).
148 private static final int EVENT_FETCH_CARRIER_DONE = 6;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700149 // Attempt to fetch from default app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700150 private static final int EVENT_DO_FETCH_DEFAULT = 7;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700151 // Attempt to fetch from carrier app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700152 private static final int EVENT_DO_FETCH_CARRIER = 8;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700153 // A package has been installed, uninstalled, or updated.
154 private static final int EVENT_PACKAGE_CHANGED = 9;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700155 // Bind timed out for the default app.
156 private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
157 // Bind timed out for a carrier app.
158 private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700159 // Check if the system fingerprint has changed.
160 private static final int EVENT_CHECK_SYSTEM_UPDATE = 12;
Nanxi Chen3d670502016-03-17 16:32:09 -0700161 // Rerun carrier config binding after system is unlocked.
162 private static final int EVENT_SYSTEM_UNLOCKED = 13;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700163 // Fetching config timed out from the default app.
164 private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14;
165 // Fetching config timed out from a carrier app.
166 private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15;
Nathan Harold48ac0972019-03-13 22:33:01 -0700167 // SubscriptionInfoUpdater has finished updating the sub for the carrier config.
168 private static final int EVENT_SUBSCRIPTION_INFO_UPDATED = 16;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700169 // Multi-SIM config changed.
170 private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 17;
Jayachandran C645ec612020-01-10 10:30:25 -0800171 // Attempt to fetch from default app or read from XML for no SIM case.
172 private static final int EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG = 18;
173 // No SIM config has been loaded from default app (or cache).
174 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE = 19;
175 // Has connected to default app for no SIM config.
176 private static final int EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG = 20;
177 // Bind timed out for the default app when trying to fetch no SIM config.
178 private static final int EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 21;
179 // Fetching config timed out from the default app for no SIM config.
180 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 22;
Rambo Wangab13e3e2021-04-08 15:01:06 -0700181 // NOTE: any new EVENT_* values must be added to method eventToString().
Jonathan Basseri930701e2015-06-11 20:56:43 -0700182
Junda Liu6cb45002016-03-22 17:18:20 -0700183 private static final int BIND_TIMEOUT_MILLIS = 30000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800184
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800185 // Keys used for saving and restoring config bundle from file.
186 private static final String KEY_VERSION = "__carrier_config_package_version__";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800187
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100188 private static final String OVERRIDE_PACKAGE_ADDITION = "-override";
189
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700190 // SharedPreferences key for last known build fingerprint.
191 private static final String KEY_FINGERPRINT = "build_fingerprint";
192
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800193 // Argument for #dump that indicates we should also call the default and specified carrier
194 // service's #dump method. In multi-SIM devices, it's possible that carrier A is on SIM 1 and
195 // carrier B is on SIM 2, in which case we should not dump carrier B's service when carrier A
196 // requested the dump.
197 private static final String DUMP_ARG_REQUESTING_PACKAGE = "--requesting-package";
198
rambowang63f4fc42022-10-03 11:54:56 -0500199 // Configs that should always be included when clients calls getConfig[ForSubId] with specified
200 // keys (even configs are not explicitly specified). Those configs have special purpose for the
201 // carrier config APIs to work correctly.
202 private static final String[] CONFIG_SUBSET_METADATA_KEYS = new String[] {
203 CarrierConfigManager.KEY_CARRIER_CONFIG_VERSION_STRING,
204 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL
205 };
206
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800207 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700208 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700209 // For each phoneId, the event sequence should be:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700210 // fetch default, connected to default, fetch default (async), fetch default done,
211 // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700212 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700213 // If there is a saved config file for either the default app or the carrier app, we skip
214 // binding to the app and go straight from fetch to loaded.
215 //
216 // At any time, at most one connection is active. If events are not in this order, previous
217 // connection will be unbound, so only latest event takes effect.
218 //
219 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
220 // 1. loading from carrier app (even if read from a file)
221 // 2. loading from default app if there is no carrier app (even if read from a file)
222 // 3. clearing config (e.g. due to sim removal)
223 // 4. encountering bind or IPC error
Jonathan Basseri65273c82017-07-25 15:08:42 -0700224 private class ConfigHandler extends Handler {
Rambo Wang7e3bc122021-03-23 09:24:38 -0700225 ConfigHandler(@NonNull Looper looper) {
226 super(looper);
227 }
228
Jonathan Basseri65273c82017-07-25 15:08:42 -0700229 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +0000230 public void handleMessage(@NonNull Message msg) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700231 final int phoneId = msg.arg1;
Rambo Wangab13e3e2021-04-08 15:01:06 -0700232 logdWithLocalLog("mHandler: " + eventToString(msg.what) + " phoneId: " + phoneId);
Malcolm Chen5ea18532019-10-24 19:55:44 -0700233 if (!SubscriptionManager.isValidPhoneId(phoneId)
234 && msg.what != EVENT_MULTI_SIM_CONFIG_CHANGED) {
235 return;
236 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800237 switch (msg.what) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800238 case EVENT_CLEAR_CONFIG: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700239 clearConfigForPhone(phoneId, true);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800240 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700241 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700242
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800243 case EVENT_SYSTEM_UNLOCKED: {
Rambo Wang6f8328d2022-04-15 01:26:23 +0000244 for (int i = 0; i < mNumPhones; ++i) {
Sarah Chin807d5c62020-03-30 17:21:09 -0700245 // When the user unlocks the device, send the broadcast again (with a
246 // rebroadcast extra) if we have sent it before unlock. This will avoid
247 // trying to load the carrier config when the SIM is still loading when the
248 // unlock happens.
Junda Liu03aad762017-07-21 13:32:17 -0700249 if (mHasSentConfigChange[i]) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800250 logdWithLocalLog("System unlocked");
Sarah Chin807d5c62020-03-30 17:21:09 -0700251 mFromSystemUnlocked[i] = true;
Junda Liu03aad762017-07-21 13:32:17 -0700252 updateConfigForPhoneId(i);
253 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700254 }
255 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700256 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700257
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800258 case EVENT_PACKAGE_CHANGED: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700259 final String carrierPackageName = (String) msg.obj;
Rambo Wang1cf71512022-04-12 19:09:07 +0000260 // Always clear up the cache and re-load config from scratch since the carrier
261 // service change is reliable and specific to the phoneId now.
262 clearCachedConfigForPackage(carrierPackageName);
263 logdWithLocalLog("Package changed: " + carrierPackageName
264 + ", phone=" + phoneId);
265 updateConfigForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700266 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700267 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700268
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800269 case EVENT_DO_FETCH_DEFAULT: {
Rambo Wang9bc7d362021-03-09 09:10:58 -0800270 // Clear in-memory cache for carrier app config, so when carrier app gets
271 // uninstalled, no stale config is left.
272 if (mConfigFromCarrierApp[phoneId] != null
273 && getCarrierPackageForPhoneId(phoneId) == null) {
274 mConfigFromCarrierApp[phoneId] = null;
275 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100276 // Restore persistent override values.
277 PersistableBundle config = restoreConfigFromXml(
278 mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId);
279 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800280 logd("Loaded persistent override config from XML. package="
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100281 + mPlatformCarrierConfigPackage
282 + " phoneId=" + phoneId);
283 mPersistentOverrideConfigs[phoneId] = config;
284 }
285
286 config = restoreConfigFromXml(mPlatformCarrierConfigPackage, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700287 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800288 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700289 "Loaded config from XML. package="
290 + mPlatformCarrierConfigPackage
291 + " phoneId="
292 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700293 mConfigFromDefaultApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700294 Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700295 newMsg.getData().putBoolean("loaded_from_xml", true);
296 mHandler.sendMessage(newMsg);
297 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700298 // No cached config, so fetch it from the default app.
299 if (bindToConfigPackage(
300 mPlatformCarrierConfigPackage,
301 phoneId,
302 EVENT_CONNECTED_TO_DEFAULT)) {
303 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800304 obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1 /*arg2*/,
305 getMessageToken(phoneId)),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700306 BIND_TIMEOUT_MILLIS);
307 } else {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800308 // Put a stub bundle in place so that the rest of the logic continues
309 // smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000310 mConfigFromDefaultApp[phoneId] = new PersistableBundle();
Jonathan Basseri65273c82017-07-25 15:08:42 -0700311 // Send broadcast if bind fails.
Nathan Harold48ac0972019-03-13 22:33:01 -0700312 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700313 // TODO: We *must* call unbindService even if bindService returns false.
314 // (And possibly if SecurityException was thrown.)
chen xudb04c292019-03-26 17:01:15 -0700315 loge("binding to default app: "
316 + mPlatformCarrierConfigPackage + " fails");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700317 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800318 }
319 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700320 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800321
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800322 case EVENT_CONNECTED_TO_DEFAULT: {
Rambo Wang610fdf62021-03-08 21:37:11 -0800323 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT, getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700324 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800325 // If new service connection has been created, unbind.
326 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700327 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800328 break;
329 }
chen xu02581692018-11-11 19:03:44 -0800330 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700331 // ResultReceiver callback will execute in this Handler's thread.
332 final ResultReceiver resultReceiver =
333 new ResultReceiver(this) {
334 @Override
335 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700336 unbindIfBound(mContext, conn, phoneId);
Rambo Wanga9d27a52021-04-09 16:17:33 -0700337 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT,
338 getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700339 // If new service connection has been created, this is stale.
340 if (mServiceConnection[phoneId] != conn) {
341 loge("Received response for stale request.");
342 return;
343 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700344 if (resultCode == RESULT_ERROR || resultData == null) {
345 // On error, abort config fetching.
346 loge("Failed to get carrier config");
Nathan Harold48ac0972019-03-13 22:33:01 -0700347 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700348 return;
349 }
350 PersistableBundle config =
351 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100352 saveConfigToXml(mPlatformCarrierConfigPackage, "", phoneId,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800353 carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700354 mConfigFromDefaultApp[phoneId] = config;
355 sendMessage(
356 obtainMessage(
357 EVENT_FETCH_DEFAULT_DONE, phoneId, -1));
358 }
359 };
360 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800361 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700362 ICarrierService carrierService =
363 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800364 carrierService.getCarrierConfig(phoneId, carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800365 logdWithLocalLog("Fetch config for default app: "
chen xudb04c292019-03-26 17:01:15 -0700366 + mPlatformCarrierConfigPackage
367 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700368 } catch (RemoteException e) {
chen xudb04c292019-03-26 17:01:15 -0700369 loge("Failed to get carrier config from default app: " +
370 mPlatformCarrierConfigPackage + " err: " + e.toString());
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700371 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700372 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800373 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700374 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800375 obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1 /*arg2*/,
376 getMessageToken(phoneId)),
Jonathan Basseri65273c82017-07-25 15:08:42 -0700377 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800378 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700379 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800380
Jonathan Basseri930701e2015-06-11 20:56:43 -0700381 case EVENT_BIND_DEFAULT_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800382 case EVENT_FETCH_DEFAULT_TIMEOUT: {
383 loge("Bind/fetch time out from " + mPlatformCarrierConfigPackage);
Rambo Wang610fdf62021-03-08 21:37:11 -0800384 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT, getMessageToken(phoneId));
chen xuf60b3162019-05-01 21:31:05 -0700385 // If we attempted to bind to the app, but the service connection is null due to
386 // the race condition that clear config event happens before bind/fetch complete
387 // then config was cleared while we were waiting and we should not continue.
388 if (mServiceConnection[phoneId] != null) {
389 // If a ResponseReceiver callback is in the queue when this happens, we will
390 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700391 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700392 broadcastConfigChangedIntent(phoneId);
393 }
Nathan Harold1122e3d2021-01-22 15:45:24 -0800394 // Put a stub bundle in place so that the rest of the logic continues smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000395 mConfigFromDefaultApp[phoneId] = new PersistableBundle();
Nathan Harold48ac0972019-03-13 22:33:01 -0700396 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700397 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700398 }
Jonathan Basseri930701e2015-06-11 20:56:43 -0700399
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800400 case EVENT_FETCH_DEFAULT_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700401 // If we attempted to bind to the app, but the service connection is null, then
402 // config was cleared while we were waiting and we should not continue.
403 if (!msg.getData().getBoolean("loaded_from_xml", false)
404 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800405 break;
406 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700407 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700408 if (carrierPackageName != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800409 logd("Found carrier config app: " + carrierPackageName);
Jordan Liu38a614c2019-03-20 15:01:17 -0700410 sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId, -1));
Jonathan Basserib919e932015-05-27 16:53:08 +0000411 } else {
Nathan Harold48ac0972019-03-13 22:33:01 -0700412 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700413 }
414 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700415 }
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700416
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800417 case EVENT_DO_FETCH_CARRIER: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700418 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700419 final PersistableBundle config =
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100420 restoreConfigFromXml(carrierPackageName, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700421 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800422 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700423 "Loaded config from XML. package="
424 + carrierPackageName
425 + " phoneId="
426 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700427 mConfigFromCarrierApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700428 Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700429 newMsg.getData().putBoolean("loaded_from_xml", true);
430 sendMessage(newMsg);
431 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700432 // No cached config, so fetch it from a carrier app.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800433 if (carrierPackageName != null && bindToConfigPackage(carrierPackageName,
434 phoneId, EVENT_CONNECTED_TO_CARRIER)) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700435 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800436 obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1 /*arg2*/,
437 getMessageToken(phoneId)),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700438 BIND_TIMEOUT_MILLIS);
439 } else {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800440 // Put a stub bundle in place so that the rest of the logic continues
441 // smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000442 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
Jonathan Basseri65273c82017-07-25 15:08:42 -0700443 // Send broadcast if bind fails.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700444 broadcastConfigChangedIntent(phoneId);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800445 loge("Bind to carrier app: " + carrierPackageName + " fails");
Nathan Harold48ac0972019-03-13 22:33:01 -0700446 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700447 }
448 }
449 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700450 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700451
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800452 case EVENT_CONNECTED_TO_CARRIER: {
Rambo Wang610fdf62021-03-08 21:37:11 -0800453 removeMessages(EVENT_BIND_CARRIER_TIMEOUT, getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700454 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800455 // If new service connection has been created, unbind.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700456 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700457 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800458 break;
459 }
chen xu02581692018-11-11 19:03:44 -0800460 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700461 // ResultReceiver callback will execute in this Handler's thread.
462 final ResultReceiver resultReceiver =
463 new ResultReceiver(this) {
464 @Override
465 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700466 unbindIfBound(mContext, conn, phoneId);
Rambo Wanga9d27a52021-04-09 16:17:33 -0700467 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT,
468 getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700469 // If new service connection has been created, this is stale.
470 if (mServiceConnection[phoneId] != conn) {
471 loge("Received response for stale request.");
472 return;
473 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700474 if (resultCode == RESULT_ERROR || resultData == null) {
475 // On error, abort config fetching.
chen xudb04c292019-03-26 17:01:15 -0700476 loge("Failed to get carrier config from carrier app: "
477 + getCarrierPackageForPhoneId(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700478 broadcastConfigChangedIntent(phoneId);
Nathan Harold48ac0972019-03-13 22:33:01 -0700479 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700480 return;
481 }
482 PersistableBundle config =
483 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100484 saveConfigToXml(getCarrierPackageForPhoneId(phoneId), "",
485 phoneId, carrierId, config);
Amit Mahajan304e92b2021-04-08 14:03:30 -0700486 if (config != null) {
487 mConfigFromCarrierApp[phoneId] = config;
488 } else {
489 logdWithLocalLog("Config from carrier app is null "
490 + "for phoneId " + phoneId);
491 // Put a stub bundle in place so that the rest of the logic
492 // continues smoothly.
493 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
494 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700495 sendMessage(
496 obtainMessage(
497 EVENT_FETCH_CARRIER_DONE, phoneId, -1));
498 }
499 };
500 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800501 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700502 ICarrierService carrierService =
503 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800504 carrierService.getCarrierConfig(phoneId, carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800505 logdWithLocalLog("Fetch config for carrier app: "
chen xudb04c292019-03-26 17:01:15 -0700506 + getCarrierPackageForPhoneId(phoneId)
507 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700508 } catch (RemoteException e) {
509 loge("Failed to get carrier config: " + e.toString());
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700510 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700511 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800512 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700513 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800514 obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1 /*arg2*/,
515 getMessageToken(phoneId)),
Jonathan Basseri65273c82017-07-25 15:08:42 -0700516 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800517 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700518 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800519
Jonathan Basseri930701e2015-06-11 20:56:43 -0700520 case EVENT_BIND_CARRIER_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800521 case EVENT_FETCH_CARRIER_TIMEOUT: {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800522 loge("Bind/fetch from carrier app timeout, package="
523 + getCarrierPackageForPhoneId(phoneId));
Rambo Wang610fdf62021-03-08 21:37:11 -0800524 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT, getMessageToken(phoneId));
chen xuf60b3162019-05-01 21:31:05 -0700525 // If we attempted to bind to the app, but the service connection is null due to
526 // the race condition that clear config event happens before bind/fetch complete
527 // then config was cleared while we were waiting and we should not continue.
528 if (mServiceConnection[phoneId] != null) {
529 // If a ResponseReceiver callback is in the queue when this happens, we will
530 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700531 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700532 broadcastConfigChangedIntent(phoneId);
533 }
Nathan Harold1122e3d2021-01-22 15:45:24 -0800534 // Put a stub bundle in place so that the rest of the logic continues smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000535 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
Nathan Harold48ac0972019-03-13 22:33:01 -0700536 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700537 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700538 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800539 case EVENT_FETCH_CARRIER_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700540 // If we attempted to bind to the app, but the service connection is null, then
541 // config was cleared while we were waiting and we should not continue.
542 if (!msg.getData().getBoolean("loaded_from_xml", false)
543 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800544 break;
545 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700546 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800547 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700548 }
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700549
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800550 case EVENT_CHECK_SYSTEM_UPDATE: {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700551 SharedPreferences sharedPrefs =
552 PreferenceManager.getDefaultSharedPreferences(mContext);
553 final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
554 if (!Build.FINGERPRINT.equals(lastFingerprint)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800555 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700556 "Build fingerprint changed. old: "
557 + lastFingerprint
558 + " new: "
559 + Build.FINGERPRINT);
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700560 clearCachedConfigForPackage(null);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700561 sharedPrefs
562 .edit()
563 .putString(KEY_FINGERPRINT, Build.FINGERPRINT)
564 .apply();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700565 }
566 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700567 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700568
569 case EVENT_SUBSCRIPTION_INFO_UPDATED:
570 broadcastConfigChangedIntent(phoneId);
571 break;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700572 case EVENT_MULTI_SIM_CONFIG_CHANGED:
573 onMultiSimConfigChanged();
574 break;
Jayachandran C645ec612020-01-10 10:30:25 -0800575
576 case EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG: {
577 PersistableBundle config =
578 restoreNoSimConfigFromXml(mPlatformCarrierConfigPackage);
579
580 if (config != null) {
581 logd("Loaded no SIM config from XML. package="
582 + mPlatformCarrierConfigPackage);
583 mNoSimConfig = config;
584 sendMessage(
585 obtainMessage(
586 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE,
587 phoneId, -1));
588 } else {
589 // No cached config, so fetch it from the default app.
590 if (bindToConfigPackage(
591 mPlatformCarrierConfigPackage,
592 phoneId,
593 EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG)) {
594 sendMessageDelayed(
595 obtainMessage(
596 EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT,
597 phoneId, -1), BIND_TIMEOUT_MILLIS);
598 } else {
599 broadcastConfigChangedIntent(phoneId, false);
600 // TODO: We *must* call unbindService even if bindService returns false.
601 // (And possibly if SecurityException was thrown.)
602 loge("binding to default app to fetch no SIM config: "
603 + mPlatformCarrierConfigPackage + " fails");
604 }
605 }
606 break;
607 }
608
609 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE: {
610 broadcastConfigChangedIntent(phoneId, false);
611 break;
612 }
613
614 case EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
615 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT: {
616 loge("Bind/fetch time out for no SIM config from "
617 + mPlatformCarrierConfigPackage);
618 removeMessages(EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
619 // If we attempted to bind to the app, but the service connection is null due to
620 // the race condition that clear config event happens before bind/fetch complete
621 // then config was cleared while we were waiting and we should not continue.
622 if (mServiceConnectionForNoSimConfig[phoneId] != null) {
623 // If a ResponseReceiver callback is in the queue when this happens, we will
624 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700625 unbindIfBoundForNoSimConfig(mContext,
626 mServiceConnectionForNoSimConfig[phoneId], phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800627 }
628 broadcastConfigChangedIntent(phoneId, false);
629 break;
630 }
631
632 case EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG: {
633 removeMessages(EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
634 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
635 // If new service connection has been created, unbind.
636 if (mServiceConnectionForNoSimConfig[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700637 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800638 break;
639 }
640
641 // ResultReceiver callback will execute in this Handler's thread.
642 final ResultReceiver resultReceiver =
643 new ResultReceiver(this) {
644 @Override
645 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700646 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800647 // If new service connection has been created, this is stale.
648 if (mServiceConnectionForNoSimConfig[phoneId] != conn) {
649 loge("Received response for stale request.");
650 return;
651 }
652 removeMessages(EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
653 if (resultCode == RESULT_ERROR || resultData == null) {
654 // On error, abort config fetching.
655 loge("Failed to get no SIM carrier config");
656 return;
657 }
658 PersistableBundle config =
659 resultData.getParcelable(KEY_CONFIG_BUNDLE);
660 saveNoSimConfigToXml(mPlatformCarrierConfigPackage, config);
661 mNoSimConfig = config;
662 sendMessage(
663 obtainMessage(
664 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE,
665 phoneId, -1));
666 }
667 };
668 // Now fetch the config asynchronously from the ICarrierService.
669 try {
670 ICarrierService carrierService =
671 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800672 carrierService.getCarrierConfig(phoneId, null, resultReceiver);
Jayachandran C645ec612020-01-10 10:30:25 -0800673 logdWithLocalLog("Fetch no sim config from default app: "
674 + mPlatformCarrierConfigPackage);
675 } catch (RemoteException e) {
676 loge("Failed to get no sim carrier config from default app: " +
677 mPlatformCarrierConfigPackage + " err: " + e.toString());
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700678 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800679 break; // So we don't set a timeout.
680 }
681 sendMessageDelayed(
682 obtainMessage(
683 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT,
684 phoneId, -1), BIND_TIMEOUT_MILLIS);
685 break;
686 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800687 }
688 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700689 }
690
Rambo Wang1b89db32022-04-15 03:00:32 +0000691 @NonNull private final Handler mHandler;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800692
693 /**
694 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
695 * receiver for relevant events.
696 */
Rambo Wang7e3bc122021-03-23 09:24:38 -0700697 @VisibleForTesting
Rambo Wang1b89db32022-04-15 03:00:32 +0000698 /* package */ CarrierConfigLoader(@NonNull Context context,
699 @NonNull SubscriptionInfoUpdater subscriptionInfoUpdater, @NonNull Looper looper) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800700 mContext = context;
Meng Wang33ad2bc2017-03-16 20:21:20 -0700701 mPlatformCarrierConfigPackage =
702 mContext.getString(R.string.platform_carrier_config_package);
Rambo Wang7e3bc122021-03-23 09:24:38 -0700703 mHandler = new ConfigHandler(looper);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800704
Rambo Wang6f8328d2022-04-15 01:26:23 +0000705 IntentFilter systemEventsFilter = new IntentFilter();
706 systemEventsFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
707 systemEventsFilter.addAction(TelephonyManager.ACTION_MULTI_SIM_CONFIG_CHANGED);
Rambo Wang1cf71512022-04-12 19:09:07 +0000708 context.registerReceiver(mSystemBroadcastReceiver, systemEventsFilter);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800709
Rambo Wang6f8328d2022-04-15 01:26:23 +0000710 mNumPhones = TelephonyManager.from(context).getActiveModemCount();
711 mConfigFromDefaultApp = new PersistableBundle[mNumPhones];
712 mConfigFromCarrierApp = new PersistableBundle[mNumPhones];
713 mPersistentOverrideConfigs = new PersistableBundle[mNumPhones];
714 mOverrideConfigs = new PersistableBundle[mNumPhones];
Rambo Wang42026112021-04-07 20:57:28 +0000715 mNoSimConfig = new PersistableBundle();
Rambo Wang6f8328d2022-04-15 01:26:23 +0000716 mServiceConnection = new CarrierServiceConnection[mNumPhones];
717 mServiceBound = new boolean[mNumPhones];
718 mHasSentConfigChange = new boolean[mNumPhones];
719 mFromSystemUnlocked = new boolean[mNumPhones];
720 mServiceConnectionForNoSimConfig = new CarrierServiceConnection[mNumPhones];
721 mServiceBoundForNoSimConfig = new boolean[mNumPhones];
Rambo Wang1cf71512022-04-12 19:09:07 +0000722 mCarrierServiceChangeCallbacks = new CarrierServiceChangeCallback[mNumPhones];
723 for (int phoneId = 0; phoneId < mNumPhones; phoneId++) {
724 mCarrierServiceChangeCallbacks[phoneId] = new CarrierServiceChangeCallback(phoneId);
725 TelephonyManager.from(context).registerCarrierPrivilegesCallback(phoneId,
726 new HandlerExecutor(mHandler), mCarrierServiceChangeCallbacks[phoneId]);
727 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800728 logd("CarrierConfigLoader has started");
Rambo Wang7e3bc122021-03-23 09:24:38 -0700729 mSubscriptionInfoUpdater = subscriptionInfoUpdater;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700730 mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800731 }
732
733 /**
734 * Initialize the singleton CarrierConfigLoader instance.
735 *
736 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
737 */
Rambo Wang1b89db32022-04-15 03:00:32 +0000738 @NonNull
739 /* package */ static CarrierConfigLoader init(@NonNull Context context) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800740 synchronized (CarrierConfigLoader.class) {
741 if (sInstance == null) {
Rambo Wang7e3bc122021-03-23 09:24:38 -0700742 sInstance = new CarrierConfigLoader(context,
743 PhoneFactory.getSubscriptionInfoUpdater(), Looper.myLooper());
Brad Ebingerfa6575f2021-05-04 00:29:50 +0000744 // Make this service available through ServiceManager.
745 TelephonyFrameworkInitializer.getTelephonyServiceManager()
746 .getCarrierConfigServiceRegisterer().register(sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800747 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700748 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800749 }
750 return sInstance;
751 }
752 }
753
Rambo Wang7e3bc122021-03-23 09:24:38 -0700754 @VisibleForTesting
755 /* package */ void clearConfigForPhone(int phoneId, boolean fetchNoSimConfig) {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700756 /* Ignore clear configuration request if device is being shutdown. */
757 Phone phone = PhoneFactory.getPhone(phoneId);
758 if (phone != null) {
759 if (phone.isShuttingDown()) {
760 return;
761 }
762 }
763
764 mConfigFromDefaultApp[phoneId] = null;
765 mConfigFromCarrierApp[phoneId] = null;
766 mServiceConnection[phoneId] = null;
767 mHasSentConfigChange[phoneId] = false;
768
Jayachandran C645ec612020-01-10 10:30:25 -0800769 if (fetchNoSimConfig) {
770 // To fetch no SIM config
771 mHandler.sendMessage(
772 mHandler.obtainMessage(
773 EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG, phoneId, -1));
774 }
Malcolm Chen5ea18532019-10-24 19:55:44 -0700775 }
776
Nathan Harold48ac0972019-03-13 22:33:01 -0700777 private void notifySubscriptionInfoUpdater(int phoneId) {
778 String configPackagename;
779 PersistableBundle configToSend;
780 int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
781 // Prefer the carrier privileged carrier app, but if there is not one, use the platform
782 // default carrier app.
783 if (mConfigFromCarrierApp[phoneId] != null) {
784 configPackagename = getCarrierPackageForPhoneId(phoneId);
785 configToSend = mConfigFromCarrierApp[phoneId];
786 } else {
787 configPackagename = mPlatformCarrierConfigPackage;
788 configToSend = mConfigFromDefaultApp[phoneId];
789 }
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700790
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800791 if (configToSend == null) {
Rambo Wang42026112021-04-07 20:57:28 +0000792 configToSend = new PersistableBundle();
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800793 }
794
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700795 // mOverrideConfigs is for testing. And it will override current configs.
796 PersistableBundle config = mOverrideConfigs[phoneId];
797 if (config != null) {
Torbjorn Eklund1050cb02018-11-16 14:05:38 +0100798 configToSend = new PersistableBundle(configToSend);
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700799 configToSend.putAll(config);
800 }
801
Nathan Harold48ac0972019-03-13 22:33:01 -0700802 mSubscriptionInfoUpdater.updateSubscriptionByCarrierConfigAndNotifyComplete(
803 phoneId, configPackagename, configToSend,
804 mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1));
805 }
806
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800807 private void broadcastConfigChangedIntent(int phoneId) {
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800808 broadcastConfigChangedIntent(phoneId, true);
809 }
810
811 private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) {
rambowangc2ab1202022-10-02 11:21:08 -0500812 int subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
813 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
814 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
815
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800816 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800817 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
chen xuf9db49f2019-03-31 23:04:42 -0700818 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800819 if (addSubIdExtra) {
rambowangc2ab1202022-10-02 11:21:08 -0500820 int simApplicationState = getSimApplicationStateForPhone(phoneId);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800821 // Include subId/carrier id extra only if SIM records are loaded
822 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
823 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
rambowangc2ab1202022-10-02 11:21:08 -0500824 subId = SubscriptionManager.getSubscriptionId(phoneId);
825 carrierId = getCarrierIdForPhoneId(phoneId);
826 specificCarrierId = getSpecificCarrierIdForPhoneId(phoneId);
827 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID, specificCarrierId);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800828 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
rambowangc2ab1202022-10-02 11:21:08 -0500829 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, carrierId);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800830 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800831 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800832 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Sarah Chin807d5c62020-03-30 17:21:09 -0700833 intent.putExtra(CarrierConfigManager.EXTRA_REBROADCAST_ON_UNLOCK,
834 mFromSystemUnlocked[phoneId]);
rambowangc2ab1202022-10-02 11:21:08 -0500835
836 TelephonyRegistryManager trm = mContext.getSystemService(TelephonyRegistryManager.class);
837 // Unlike broadcast, we wouldn't notify registrants on carrier config change when device is
838 // unlocked. Only real carrier config change will send the notification to registrants.
839 if (trm != null && !mFromSystemUnlocked[phoneId]) {
840 trm.notifyCarrierConfigChanged(phoneId, subId, carrierId, specificCarrierId);
841 }
842
Meng Wang97a6a462020-01-23 16:22:16 -0800843 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
rambowangc2ab1202022-10-02 11:21:08 -0500844
Jack Yude40a3f2022-11-19 22:29:12 -0800845 if (SubscriptionManager.isValidSubscriptionId(subId)) {
846 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId + ", subId=" + subId);
Jack Yue9e95be2020-03-22 10:56:06 -0700847 } else {
848 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
849 }
Junda Liu03aad762017-07-21 13:32:17 -0700850 mHasSentConfigChange[phoneId] = true;
Sarah Chin807d5c62020-03-30 17:21:09 -0700851 mFromSystemUnlocked[phoneId] = false;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800852 }
853
rambowangc2ab1202022-10-02 11:21:08 -0500854 private int getSimApplicationStateForPhone(int phoneId) {
855 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
856 int subId = SubscriptionManager.getSubscriptionId(phoneId);
857 if (SubscriptionManager.isValidSubscriptionId(subId)) {
858 TelephonyManager telMgr = TelephonyManager.from(mContext)
859 .createForSubscriptionId(subId);
860 simApplicationState = telMgr.getSimApplicationState();
861 }
862 return simApplicationState;
863 }
864
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800865 /** Binds to the default or carrier config app. */
Rambo Wang1b89db32022-04-15 03:00:32 +0000866 private boolean bindToConfigPackage(@NonNull String pkgName, int phoneId, int eventId) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800867 logdWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700868 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700869 carrierService.setPackage(pkgName);
Jayachandran C645ec612020-01-10 10:30:25 -0800870 CarrierServiceConnection serviceConnection = new CarrierServiceConnection(
871 phoneId, pkgName, eventId);
872 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
873 mServiceConnectionForNoSimConfig[phoneId] = serviceConnection;
874 } else {
875 mServiceConnection[phoneId] = serviceConnection;
876 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800877 try {
Jayachandran C645ec612020-01-10 10:30:25 -0800878 if (mContext.bindService(carrierService, serviceConnection,
Jordan Liu178430d2020-02-10 14:32:15 -0800879 Context.BIND_AUTO_CREATE)) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700880 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
881 mServiceBoundForNoSimConfig[phoneId] = true;
882 } else {
883 mServiceBound[phoneId] = true;
884 }
Jordan Liu178430d2020-02-10 14:32:15 -0800885 return true;
886 } else {
887 return false;
888 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800889 } catch (SecurityException ex) {
890 return false;
891 }
892 }
893
Rambo Wang7e3bc122021-03-23 09:24:38 -0700894 @VisibleForTesting
Rambo Wang1b89db32022-04-15 03:00:32 +0000895 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -0700896 /* package */ CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800897 String mcc = "";
898 String mnc = "";
899 String imsi = "";
900 String gid1 = "";
901 String gid2 = "";
902 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
903 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800904 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800905 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700906 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
907 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800908 mcc = simOperator.substring(0, 3);
909 mnc = simOperator.substring(3);
910 }
911 Phone phone = PhoneFactory.getPhone(phoneId);
912 if (phone != null) {
913 imsi = phone.getSubscriberId();
914 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700915 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800916 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800917 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800918 }
chen xua31f22b2019-03-06 15:28:50 -0800919 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800920 }
921
Rambo Wang76e83b52022-03-17 12:36:22 -0700922 /** Returns the package name of a privileged carrier app, or null if there is none. */
Nathan Harold1122e3d2021-01-22 15:45:24 -0800923 @Nullable
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700924 private String getCarrierPackageForPhoneId(int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -0700925 final long token = Binder.clearCallingIdentity();
926 try {
Rambo Wang76e83b52022-03-17 12:36:22 -0700927 return TelephonyManager.from(mContext)
928 .getCarrierServicePackageNameForLogicalSlot(phoneId);
Nazanin1adf4562021-03-29 15:35:30 -0700929 } finally {
930 Binder.restoreCallingIdentity(token);
931 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700932 }
933
Rambo Wang1b89db32022-04-15 03:00:32 +0000934 @Nullable
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700935 private String getIccIdForPhoneId(int phoneId) {
936 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
937 return null;
938 }
939 Phone phone = PhoneFactory.getPhone(phoneId);
940 if (phone == null) {
941 return null;
942 }
943 return phone.getIccSerialNumber();
944 }
945
chen xu02581692018-11-11 19:03:44 -0800946 /**
chen xua31f22b2019-03-06 15:28:50 -0800947 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800948 */
chen xua31f22b2019-03-06 15:28:50 -0800949 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700950 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800951 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700952 }
953 Phone phone = PhoneFactory.getPhone(phoneId);
954 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800955 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700956 }
chen xua31f22b2019-03-06 15:28:50 -0800957 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800958 }
959
960 /**
961 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
962 */
963 private int getCarrierIdForPhoneId(int phoneId) {
964 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
965 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700966 }
chen xu02581692018-11-11 19:03:44 -0800967 Phone phone = PhoneFactory.getPhone(phoneId);
968 if (phone == null) {
969 return TelephonyManager.UNKNOWN_CARRIER_ID;
970 }
971 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700972 }
973
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700974 /**
975 * Writes a bundle to an XML file.
976 *
chen xu02581692018-11-11 19:03:44 -0800977 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -0800978 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -0800979 * should have a single copy of XML file named after carrier id. However, it's still possible
980 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
981 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
982 * carrier while iccid remains the same.
983 *
984 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
985 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700986 *
987 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700988 *
Jayachandran C645ec612020-01-10 10:30:25 -0800989 * @param packageName the name of the package from which we fetched this bundle.
990 * @param extraString An extra string to be used in the XML file name.
991 * @param phoneId the phone ID.
992 * @param carrierId contains all carrier-identifying information.
993 * @param config the bundle to be written. Null will be treated as an empty bundle.
994 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700995 */
Rambo Wang1b89db32022-04-15 03:00:32 +0000996 private void saveConfigToXml(@Nullable String packageName, @NonNull String extraString,
997 int phoneId, @Nullable CarrierIdentifier carrierId, @NonNull PersistableBundle config,
998 boolean isNoSimConfig) {
Jayachandran C645ec612020-01-10 10:30:25 -0800999 if (packageName == null) {
1000 loge("Cannot save config with null packageName");
Yuchen Dongc15afed2018-04-26 17:05:22 +08001001 return;
1002 }
1003
Jayachandran C645ec612020-01-10 10:30:25 -08001004 String fileName;
1005 if (isNoSimConfig) {
1006 fileName = getFilenameForNoSimConfig(packageName);
1007 } else {
1008 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
1009 != TelephonyManager.SIM_STATE_LOADED) {
1010 loge("Skip save config because SIM records are not loaded.");
1011 return;
1012 }
1013
1014 final String iccid = getIccIdForPhoneId(phoneId);
Rambo Wang1b89db32022-04-15 03:00:32 +00001015 final int cid = carrierId != null ? carrierId.getSpecificCarrierId()
1016 : TelephonyManager.UNKNOWN_CARRIER_ID;
Jayachandran C645ec612020-01-10 10:30:25 -08001017 if (iccid == null) {
1018 loge("Cannot save config with null iccid.");
1019 return;
1020 }
1021 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001022 }
Jayachandran C645ec612020-01-10 10:30:25 -08001023
Junda Liu02596502016-11-15 13:46:43 -08001024 // b/32668103 Only save to file if config isn't empty.
1025 // In case of failure, not caching an empty bundle will
1026 // try loading config again on next power on or sim loaded.
1027 // Downside is for genuinely empty bundle, will bind and load
1028 // on every power on.
1029 if (config == null || config.isEmpty()) {
1030 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001031 }
1032
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001033 final String version = getPackageVersion(packageName);
1034 if (version == null) {
1035 loge("Failed to get package version for: " + packageName);
1036 return;
1037 }
1038
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001039 logdWithLocalLog(
1040 "Save config to xml, packagename: " + packageName + " phoneId: " + phoneId);
chen xudb04c292019-03-26 17:01:15 -07001041
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001042 FileOutputStream outFile = null;
1043 try {
Jayachandran C645ec612020-01-10 10:30:25 -08001044 outFile = new FileOutputStream(new File(mContext.getFilesDir(), fileName));
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001045 config.putString(KEY_VERSION, version);
1046 config.writeToStream(outFile);
1047 outFile.flush();
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001048 outFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001049 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001050 loge(e.toString());
1051 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001052 }
1053
Rambo Wang7e3bc122021-03-23 09:24:38 -07001054 @VisibleForTesting
Rambo Wang1b89db32022-04-15 03:00:32 +00001055 /* package */ void saveConfigToXml(@Nullable String packageName, @NonNull String extraString,
1056 int phoneId, @NonNull CarrierIdentifier carrierId, @NonNull PersistableBundle config) {
Jayachandran C645ec612020-01-10 10:30:25 -08001057 saveConfigToXml(packageName, extraString, phoneId, carrierId, config, false);
1058 }
1059
Rambo Wang7e3bc122021-03-23 09:24:38 -07001060 @VisibleForTesting
Rambo Wang1b89db32022-04-15 03:00:32 +00001061 /* package */ void saveNoSimConfigToXml(@Nullable String packageName,
1062 @NonNull PersistableBundle config) {
Jayachandran C645ec612020-01-10 10:30:25 -08001063 saveConfigToXml(packageName, "", -1, null, config, true);
1064 }
1065
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001066 /**
1067 * Reads a bundle from an XML file.
1068 *
1069 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +08001070 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001071 *
1072 * In case of errors, or if the saved config is from a different package version than the
1073 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001074 *
Jayachandran C645ec612020-01-10 10:30:25 -08001075 * @param packageName the name of the package from which we fetched this bundle.
1076 * @param extraString An extra string to be used in the XML file name.
1077 * @param phoneId the phone ID.
1078 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001079 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001080 * version does not match, or reading config fails.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001081 */
Rambo Wang1b89db32022-04-15 03:00:32 +00001082 @Nullable
1083 private PersistableBundle restoreConfigFromXml(@Nullable String packageName,
1084 @NonNull String extraString, int phoneId, boolean isNoSimConfig) {
Jayachandran C645ec612020-01-10 10:30:25 -08001085 if (packageName == null) {
1086 loge("Cannot restore config with null packageName");
1087 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001088 final String version = getPackageVersion(packageName);
1089 if (version == null) {
1090 loge("Failed to get package version for: " + packageName);
1091 return null;
1092 }
Yuchen Dongc15afed2018-04-26 17:05:22 +08001093
Jayachandran C645ec612020-01-10 10:30:25 -08001094 String fileName;
arunvoddu664c36a2021-10-11 20:09:28 +00001095 String iccid = null;
Jayachandran C645ec612020-01-10 10:30:25 -08001096 if (isNoSimConfig) {
1097 fileName = getFilenameForNoSimConfig(packageName);
1098 } else {
1099 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
1100 != TelephonyManager.SIM_STATE_LOADED) {
1101 loge("Skip restore config because SIM records are not loaded.");
1102 return null;
1103 }
1104
arunvoddu664c36a2021-10-11 20:09:28 +00001105 iccid = getIccIdForPhoneId(phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001106 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1107 if (iccid == null) {
1108 loge("Cannot restore config with null iccid.");
1109 return null;
1110 }
1111 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001112 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001113
1114 PersistableBundle restoredBundle = null;
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001115 File file = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001116 FileInputStream inFile = null;
1117 try {
Jayachandran C645ec612020-01-10 10:30:25 -08001118 file = new File(mContext.getFilesDir(),fileName);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001119 inFile = new FileInputStream(file);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001120
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001121 restoredBundle = PersistableBundle.readFromStream(inFile);
1122 String savedVersion = restoredBundle.getString(KEY_VERSION);
1123 restoredBundle.remove(KEY_VERSION);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001124
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001125 if (!version.equals(savedVersion)) {
1126 loge("Saved version mismatch: " + version + " vs " + savedVersion);
1127 restoredBundle = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001128 }
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001129
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001130 inFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001131 } catch (FileNotFoundException e) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001132 // Missing file is normal occurrence that might occur with a new sim or when restoring
1133 // an override file during boot and should not be treated as an error.
arunvoddu664c36a2021-10-11 20:09:28 +00001134 if (file != null) {
1135 if (isNoSimConfig) {
1136 logd("File not found: " + file.getPath());
1137 } else {
1138 String filePath = file.getPath();
1139 filePath = getFilePathForLogging(filePath, iccid);
1140 logd("File not found : " + filePath);
1141 }
1142 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001143 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001144 loge(e.toString());
1145 }
1146
1147 return restoredBundle;
1148 }
1149
arunvoddu664c36a2021-10-11 20:09:28 +00001150 /**
1151 * This method will mask most part of iccid in the filepath for logging on userbuild
1152 */
Rambo Wang1b89db32022-04-15 03:00:32 +00001153 @NonNull
1154 private String getFilePathForLogging(@Nullable String filePath, @Nullable String iccid) {
arunvoddu664c36a2021-10-11 20:09:28 +00001155 // If loggable then return with actual file path
1156 if (Rlog.isLoggable(LOG_TAG, Log.VERBOSE)) {
1157 return filePath;
1158 }
1159 String path = filePath;
1160 int length = (iccid != null) ? iccid.length() : 0;
1161 if (length > 5 && filePath != null) {
1162 path = filePath.replace(iccid.substring(5), "***************");
1163 }
1164 return path;
1165 }
1166
Rambo Wang1b89db32022-04-15 03:00:32 +00001167 @Nullable
1168 private PersistableBundle restoreConfigFromXml(@Nullable String packageName,
1169 @NonNull String extraString, int phoneId) {
Jayachandran C645ec612020-01-10 10:30:25 -08001170 return restoreConfigFromXml(packageName, extraString, phoneId, false);
1171 }
1172
Rambo Wang1b89db32022-04-15 03:00:32 +00001173 @Nullable
1174 private PersistableBundle restoreNoSimConfigFromXml(@Nullable String packageName) {
Jayachandran C645ec612020-01-10 10:30:25 -08001175 return restoreConfigFromXml(packageName, "", -1, true);
1176 }
1177
Junda Liu8a8a53a2015-05-15 16:19:57 -07001178 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001179 * Clears cached carrier config.
1180 * This deletes all saved XML files associated with the given package name. If packageName is
1181 * null, then it deletes all saved XML files.
1182 *
1183 * @param packageName the name of a carrier package, or null if all cached config should be
1184 * cleared.
1185 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -07001186 */
Rambo Wang1b89db32022-04-15 03:00:32 +00001187 private boolean clearCachedConfigForPackage(@Nullable final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001188 File dir = mContext.getFilesDir();
1189 File[] packageFiles = dir.listFiles(new FilenameFilter() {
1190 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001191 if (packageName != null) {
1192 return filename.startsWith("carrierconfig-" + packageName + "-");
1193 } else {
1194 return filename.startsWith("carrierconfig-");
1195 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001196 }
1197 });
Junda Liu8a8a53a2015-05-15 16:19:57 -07001198 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001199 for (File f : packageFiles) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001200 logd("Deleting " + f.getName());
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001201 f.delete();
1202 }
Junda Liu8a8a53a2015-05-15 16:19:57 -07001203 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001204 }
1205
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001206 /** Builds a canonical file name for a config file. */
Rambo Wang1b89db32022-04-15 03:00:32 +00001207 @NonNull
Jayachandran C645ec612020-01-10 10:30:25 -08001208 private static String getFilenameForConfig(
1209 @NonNull String packageName, @NonNull String extraString,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001210 @NonNull String iccid, int cid) {
chen xu02581692018-11-11 19:03:44 -08001211 // the same carrier should have a single copy of XML file named after carrier id.
1212 // However, it's still possible that platform doesn't recognize the current sim carrier,
1213 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
1214 // cases SIM OTA resolves to different carrier while iccid remains the same.
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001215 return "carrierconfig-" + packageName + extraString + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001216 }
1217
Jayachandran C645ec612020-01-10 10:30:25 -08001218 /** Builds a canonical file name for no SIM config file. */
Rambo Wang1b89db32022-04-15 03:00:32 +00001219 @NonNull
Jayachandran C645ec612020-01-10 10:30:25 -08001220 private String getFilenameForNoSimConfig(@NonNull String packageName) {
1221 return "carrierconfig-" + packageName + "-" + "nosim" + ".xml";
1222 }
1223
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001224 /** Return the current version code of a package, or null if the name is not found. */
Rambo Wang1b89db32022-04-15 03:00:32 +00001225 @Nullable
1226 private String getPackageVersion(@NonNull String packageName) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001227 try {
1228 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -08001229 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001230 } catch (PackageManager.NameNotFoundException e) {
1231 return null;
1232 }
1233 }
1234
Jonathan Basseri65273c82017-07-25 15:08:42 -07001235 /**
1236 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001237 *
1238 * This reads config bundles for the given phoneId. That means getting the latest bundle from
1239 * the default app and a privileged carrier app, if present. This will not bind to an app if we
1240 * have a saved config file to use instead.
1241 */
1242 private void updateConfigForPhoneId(int phoneId) {
Jonathan Basseri65273c82017-07-25 15:08:42 -07001243 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001244 }
1245
Malcolm Chen5ea18532019-10-24 19:55:44 -07001246 private void onMultiSimConfigChanged() {
Rambo Wang6f8328d2022-04-15 01:26:23 +00001247 int oldNumPhones = mNumPhones;
1248 mNumPhones = TelephonyManager.from(mContext).getActiveModemCount();
1249 if (mNumPhones == oldNumPhones) {
1250 return;
1251 }
1252 logdWithLocalLog("mNumPhones change from " + oldNumPhones + " to " + mNumPhones);
1253
Rambo Wang1cf71512022-04-12 19:09:07 +00001254 // If DS -> SS switch, release the resources BEFORE truncating the arrays to avoid leaking
Rambo Wang6f8328d2022-04-15 01:26:23 +00001255 for (int phoneId = mNumPhones; phoneId < oldNumPhones; phoneId++) {
1256 if (mServiceConnection[phoneId] != null) {
1257 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
1258 }
1259 if (mServiceConnectionForNoSimConfig[phoneId] != null) {
1260 unbindIfBoundForNoSimConfig(mContext, mServiceConnectionForNoSimConfig[phoneId],
1261 phoneId);
1262 }
1263 }
1264
Rambo Wang1cf71512022-04-12 19:09:07 +00001265 // The phone to slot mapping may change, unregister here and re-register callbacks later
1266 for (int phoneId = 0; phoneId < oldNumPhones; phoneId++) {
1267 if (mCarrierServiceChangeCallbacks[phoneId] != null) {
1268 TelephonyManager.from(mContext).unregisterCarrierPrivilegesCallback(
1269 mCarrierServiceChangeCallbacks[phoneId]);
1270 }
1271 }
1272
Rambo Wang6f8328d2022-04-15 01:26:23 +00001273 // Copy the original arrays, truncate or padding with zeros (if necessary) to new length
1274 mConfigFromDefaultApp = Arrays.copyOf(mConfigFromDefaultApp, mNumPhones);
1275 mConfigFromCarrierApp = Arrays.copyOf(mConfigFromCarrierApp, mNumPhones);
1276 mPersistentOverrideConfigs = Arrays.copyOf(mPersistentOverrideConfigs, mNumPhones);
1277 mOverrideConfigs = Arrays.copyOf(mOverrideConfigs, mNumPhones);
1278 mServiceConnection = Arrays.copyOf(mServiceConnection, mNumPhones);
1279 mServiceConnectionForNoSimConfig =
1280 Arrays.copyOf(mServiceConnectionForNoSimConfig, mNumPhones);
1281 mServiceBound = Arrays.copyOf(mServiceBound, mNumPhones);
1282 mServiceBoundForNoSimConfig = Arrays.copyOf(mServiceBoundForNoSimConfig, mNumPhones);
1283 mHasSentConfigChange = Arrays.copyOf(mHasSentConfigChange, mNumPhones);
1284 mFromSystemUnlocked = Arrays.copyOf(mFromSystemUnlocked, mNumPhones);
Rambo Wang1cf71512022-04-12 19:09:07 +00001285 mCarrierServiceChangeCallbacks = Arrays.copyOf(mCarrierServiceChangeCallbacks, mNumPhones);
Rambo Wang6f8328d2022-04-15 01:26:23 +00001286
Rambo Wang1cf71512022-04-12 19:09:07 +00001287 // Load the config for all the phones and re-register callback AFTER padding the arrays.
Rambo Wang6f8328d2022-04-15 01:26:23 +00001288 for (int phoneId = 0; phoneId < mNumPhones; phoneId++) {
1289 updateConfigForPhoneId(phoneId);
Rambo Wang1cf71512022-04-12 19:09:07 +00001290 mCarrierServiceChangeCallbacks[phoneId] = new CarrierServiceChangeCallback(phoneId);
1291 TelephonyManager.from(mContext).registerCarrierPrivilegesCallback(phoneId,
1292 new HandlerExecutor(mHandler), mCarrierServiceChangeCallbacks[phoneId]);
Malcolm Chen5ea18532019-10-24 19:55:44 -07001293 }
1294 }
1295
Hall Liu506724b2018-10-22 18:16:14 -07001296 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001297 @NonNull
Rambo Wang1b89db32022-04-15 03:00:32 +00001298 public PersistableBundle getConfigForSubId(int subscriptionId, @NonNull String callingPackage) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001299 return getConfigForSubIdWithFeature(subscriptionId, callingPackage, null);
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08001300 }
1301
1302 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001303 @NonNull
Rambo Wang1b89db32022-04-15 03:00:32 +00001304 public PersistableBundle getConfigForSubIdWithFeature(int subscriptionId,
1305 @NonNull String callingPackage, @Nullable String callingFeatureId) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001306 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subscriptionId,
1307 callingPackage, callingFeatureId, "getCarrierConfig")) {
Rambo Wang42026112021-04-07 20:57:28 +00001308 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -07001309 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -07001310
Rambo Wangd2b004b2021-03-30 10:10:23 -07001311 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001312 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001313 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001314 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001315 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001316 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001317 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001318 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001319 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001320 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001321 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001322 config = mPersistentOverrideConfigs[phoneId];
1323 if (config != null) {
1324 retConfig.putAll(config);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001325 }
Hall Liu506724b2018-10-22 18:16:14 -07001326 config = mOverrideConfigs[phoneId];
1327 if (config != null) {
1328 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -07001329 }
Nathan Harold1122e3d2021-01-22 15:45:24 -08001330 // Ignore the theoretical case of the default app not being present since that won't
1331 // work in CarrierConfigLoader today.
1332 final boolean allConfigsApplied =
1333 (mConfigFromCarrierApp[phoneId] != null
1334 || getCarrierPackageForPhoneId(phoneId) == null)
1335 && mConfigFromDefaultApp[phoneId] != null;
1336 retConfig.putBoolean(
1337 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, allConfigsApplied);
Jayachandran C645ec612020-01-10 10:30:25 -08001338 } else {
1339 if (mNoSimConfig != null) {
1340 retConfig.putAll(mNoSimConfig);
1341 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001342 }
1343 return retConfig;
1344 }
1345
1346 @Override
rambowang63f4fc42022-10-03 11:54:56 -05001347 @NonNull
1348 public PersistableBundle getConfigSubsetForSubIdWithFeature(int subscriptionId,
1349 @NonNull String callingPackage, @Nullable String callingFeatureId,
1350 @NonNull String[] keys) {
1351 Objects.requireNonNull(callingPackage, "Calling package must be non-null");
1352 Objects.requireNonNull(keys, "Config keys must be non-null");
1353 enforceCallerIsSystemOrRequestingPackage(callingPackage);
1354
1355 // Permission check is performed inside and an empty bundle will return on failure.
1356 // No SecurityException thrown here since most clients expect to retrieve the overridden
1357 // value if present or use default one if not
1358 PersistableBundle allConfigs = getConfigForSubIdWithFeature(subscriptionId, callingPackage,
1359 callingFeatureId);
1360 if (allConfigs.isEmpty()) {
1361 return allConfigs;
1362 }
1363 for (String key : keys) {
1364 Objects.requireNonNull(key, "Config key must be non-null");
1365 // TODO(b/261776046): validate provided key which may has no default value.
1366 // For now, return empty bundle if any required key is not supported
1367 if (!allConfigs.containsKey(key)) {
1368 return new PersistableBundle();
1369 }
1370 }
1371
1372 PersistableBundle configSubset = new PersistableBundle(
1373 keys.length + CONFIG_SUBSET_METADATA_KEYS.length);
1374 for (String carrierConfigKey : keys) {
1375 Object value = allConfigs.get(carrierConfigKey);
1376 // Config value itself could be PersistableBundle which requires different API to put
1377 if (value instanceof PersistableBundle) {
1378 configSubset.putPersistableBundle(carrierConfigKey, (PersistableBundle) value);
1379 } else {
1380 configSubset.putObject(carrierConfigKey, value);
1381 }
1382 }
1383
1384 // Configs in CONFIG_SUBSET_ALWAYS_INCLUDED_KEYS should always be included
1385 for (String generalKey : CONFIG_SUBSET_METADATA_KEYS) {
1386 configSubset.putObject(generalKey, allConfigs.get(generalKey));
1387 }
1388
1389 return configSubset;
1390 }
1391
1392 @Override
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001393 public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides,
1394 boolean persistent) {
Hall Liu506724b2018-10-22 18:16:14 -07001395 mContext.enforceCallingOrSelfPermission(
1396 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Brad Ebingerf6281ad2019-04-25 11:02:04 -07001397 //TODO: Also check for SHELL UID to restrict this method to testing only (b/131326259)
Hall Liu506724b2018-10-22 18:16:14 -07001398 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1399 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001400 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
Rambo Wangd2b004b2021-03-30 10:10:23 -07001401 throw new IllegalArgumentException(
1402 "Invalid phoneId " + phoneId + " for subId " + subscriptionId);
Hall Liu506724b2018-10-22 18:16:14 -07001403 }
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001404 // Post to run on handler thread on which all states should be confined.
1405 mHandler.post(() -> {
1406 overrideConfig(mOverrideConfigs, phoneId, overrides);
Hall Liu506724b2018-10-22 18:16:14 -07001407
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001408 if (persistent) {
1409 overrideConfig(mPersistentOverrideConfigs, phoneId, overrides);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001410
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001411 if (overrides != null) {
1412 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
1413 saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION,
1414 phoneId,
1415 carrierId, mPersistentOverrideConfigs[phoneId]);
1416 } else {
1417 final String iccid = getIccIdForPhoneId(phoneId);
1418 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1419 String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage,
1420 OVERRIDE_PACKAGE_ADDITION, iccid, cid);
1421 File fileToDelete = new File(mContext.getFilesDir(), fileName);
1422 fileToDelete.delete();
1423 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001424 }
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001425 notifySubscriptionInfoUpdater(phoneId);
1426 });
Hall Liu506724b2018-10-22 18:16:14 -07001427 }
1428
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001429 private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId,
1430 @Nullable PersistableBundle overrides) {
1431 if (overrides == null) {
Rambo Wang42026112021-04-07 20:57:28 +00001432 currentOverrides[phoneId] = new PersistableBundle();
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001433 } else if (currentOverrides[phoneId] == null) {
1434 currentOverrides[phoneId] = overrides;
1435 } else {
1436 currentOverrides[phoneId].putAll(overrides);
1437 }
1438 }
1439
Hall Liu506724b2018-10-22 18:16:14 -07001440 @Override
Rambo Wangd2b004b2021-03-30 10:10:23 -07001441 public void notifyConfigChangedForSubId(int subscriptionId) {
Jordan Liud7d57fe2019-04-16 12:29:43 -07001442 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001443 // system privileged app with MODIFY_PHONE_STATE permission.
Rambo Wangd2b004b2021-03-30 10:10:23 -07001444 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext,
1445 subscriptionId, "Require carrier privileges or MODIFY_PHONE_STATE permission.");
1446
1447 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1448 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1449 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
1450 throw new IllegalArgumentException(
1451 "Invalid phoneId " + phoneId + " for subId " + subscriptionId);
1452 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001453
1454 // This method should block until deleting has completed, so that an error which prevents us
1455 // from clearing the cache is passed back to the carrier app. With the files successfully
1456 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001457 String callingPackageName = mContext.getPackageManager().getNameForUid(
1458 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001459 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001460 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001461 }
1462
1463 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +00001464 public void updateConfigForPhoneId(int phoneId, @NonNull String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -07001465 mContext.enforceCallingOrSelfPermission(
1466 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001467 logdWithLocalLog("Update config for phoneId: " + phoneId + " simState: " + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001468 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001469 throw new IllegalArgumentException("Invalid phoneId: " + phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001470 }
1471 // requires Java 7 for switch on string.
1472 switch (simState) {
1473 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1474 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001475 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001476 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Malcolm Chendb66de12019-12-09 18:57:07 -08001477 case IccCardConstants.INTENT_VALUE_ICC_NOT_READY:
Junda Liu9f2d2712015-05-15 14:22:45 -07001478 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001479 break;
1480 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1481 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001482 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001483 break;
1484 }
1485 }
1486
Junda Liu43d723a2015-05-12 17:23:45 -07001487 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +00001488 @NonNull
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001489 public String getDefaultCarrierServicePackageName() {
Shuo Qianefdb7962019-12-19 15:54:27 -08001490 mContext.enforceCallingOrSelfPermission(
1491 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1492 "getDefaultCarrierServicePackageName");
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001493 return mPlatformCarrierConfigPackage;
1494 }
1495
Rambo Wang7e3bc122021-03-23 09:24:38 -07001496 @VisibleForTesting
Rambo Wang1b89db32022-04-15 03:00:32 +00001497 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -07001498 /* package */ Handler getHandler() {
1499 return mHandler;
1500 }
1501
1502 @VisibleForTesting
Rambo Wang1b89db32022-04-15 03:00:32 +00001503 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001504 /* package */ PersistableBundle getConfigFromDefaultApp(int phoneId) {
1505 return mConfigFromDefaultApp[phoneId];
1506 }
1507
1508 @VisibleForTesting
Rambo Wang1b89db32022-04-15 03:00:32 +00001509 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001510 /* package */ PersistableBundle getConfigFromCarrierApp(int phoneId) {
1511 return mConfigFromCarrierApp[phoneId];
1512 }
1513
1514 @VisibleForTesting
Rambo Wang1b89db32022-04-15 03:00:32 +00001515 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -07001516 /* package */ PersistableBundle getNoSimConfig() {
1517 return mNoSimConfig;
1518 }
1519
1520 @VisibleForTesting
Rambo Wang1b89db32022-04-15 03:00:32 +00001521 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001522 /* package */ PersistableBundle getOverrideConfig(int phoneId) {
1523 return mOverrideConfigs[phoneId];
1524 }
1525
Rambo Wang6f8328d2022-04-15 01:26:23 +00001526 // TODO(b/185129900): always call unbindService after bind, no matter if it succeeded
Rambo Wang1b89db32022-04-15 03:00:32 +00001527 private void unbindIfBound(@NonNull Context context, @NonNull CarrierServiceConnection conn,
Rambo Wangb0f3e2f2021-10-28 12:40:12 -07001528 int phoneId) {
1529 if (mServiceBound[phoneId]) {
1530 mServiceBound[phoneId] = false;
1531 context.unbindService(conn);
1532 }
1533 }
1534
Rambo Wang1b89db32022-04-15 03:00:32 +00001535 private void unbindIfBoundForNoSimConfig(@NonNull Context context,
1536 @NonNull CarrierServiceConnection conn, int phoneId) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -07001537 if (mServiceBoundForNoSimConfig[phoneId]) {
1538 mServiceBoundForNoSimConfig[phoneId] = false;
Jayachandran C645ec612020-01-10 10:30:25 -08001539 context.unbindService(conn);
1540 }
1541 }
1542
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001543 /**
Rambo Wang610fdf62021-03-08 21:37:11 -08001544 * Returns a boxed Integer object for phoneId, services as message token to distinguish messages
1545 * with same code when calling {@link Handler#removeMessages(int, Object)}.
1546 */
Rambo Wang1b89db32022-04-15 03:00:32 +00001547 @NonNull
Rambo Wang610fdf62021-03-08 21:37:11 -08001548 private Integer getMessageToken(int phoneId) {
1549 if (phoneId < -128 || phoneId > 127) {
1550 throw new IllegalArgumentException("phoneId should be in range [-128, 127], inclusive");
1551 }
1552 // Integer#valueOf guarantees the integers within [-128, 127] are cached and thus memory
1553 // comparison (==) returns true for the same integer.
1554 return Integer.valueOf(phoneId);
1555 }
1556
1557 /**
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001558 * If {@code args} contains {@link #DUMP_ARG_REQUESTING_PACKAGE} and a following package name,
1559 * we'll also call {@link IBinder#dump} on the default carrier service (if bound) and the
1560 * specified carrier service (if bound). Typically, this is done for connectivity bug reports
1561 * where we don't call {@code dumpsys activity service all-non-platform} because that contains
1562 * too much info, but we still want to let carrier apps include their diagnostics.
1563 */
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001564 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +00001565 public void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter pw, @NonNull String[] args) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001566 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
Junda Liu43d723a2015-05-12 17:23:45 -07001567 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1568 != PackageManager.PERMISSION_GRANTED) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001569 indentPW.println("Permission Denial: can't dump carrierconfig from from pid="
Junda Liu43d723a2015-05-12 17:23:45 -07001570 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1571 return;
1572 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001573 String requestingPackage = null;
1574 int requestingPackageIndex = ArrayUtils.indexOf(args, DUMP_ARG_REQUESTING_PACKAGE);
1575 if (requestingPackageIndex >= 0 && requestingPackageIndex < args.length - 1
1576 && !TextUtils.isEmpty(args[requestingPackageIndex + 1])) {
1577 requestingPackage = args[requestingPackageIndex + 1];
1578 // Throws a SecurityException if the caller is impersonating another app in an effort to
1579 // dump extra info (which may contain PII the caller doesn't have a right to).
1580 enforceCallerIsSystemOrRequestingPackage(requestingPackage);
1581 }
1582
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001583 indentPW.println("CarrierConfigLoader: " + this);
Rambo Wang6f8328d2022-04-15 01:26:23 +00001584 for (int i = 0; i < mNumPhones; i++) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001585 indentPW.println("Phone Id = " + i);
shuoq26a3a4c2016-12-16 11:06:48 -08001586 // display default values in CarrierConfigManager
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001587 printConfig(CarrierConfigManager.getDefaultConfig(), indentPW,
shuoq26a3a4c2016-12-16 11:06:48 -08001588 "Default Values from CarrierConfigManager");
shuoq26a3a4c2016-12-16 11:06:48 -08001589 // display ConfigFromDefaultApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001590 printConfig(mConfigFromDefaultApp[i], indentPW, "mConfigFromDefaultApp");
shuoq26a3a4c2016-12-16 11:06:48 -08001591 // display ConfigFromCarrierApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001592 printConfig(mConfigFromCarrierApp[i], indentPW, "mConfigFromCarrierApp");
1593 printConfig(mPersistentOverrideConfigs[i], indentPW, "mPersistentOverrideConfigs");
1594 printConfig(mOverrideConfigs[i], indentPW, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001595 }
chen xudb04c292019-03-26 17:01:15 -07001596
Jayachandran C645ec612020-01-10 10:30:25 -08001597 printConfig(mNoSimConfig, indentPW, "mNoSimConfig");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001598 indentPW.println("CarrierConfigLoadingLog=");
1599 mCarrierConfigLoadingLog.dump(fd, indentPW, args);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001600
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001601 if (requestingPackage != null) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001602 logd("Including default and requesting package " + requestingPackage
1603 + " carrier services in dump");
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001604 indentPW.println("");
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001605 indentPW.println("Connected services");
1606 dumpCarrierServiceIfBound(fd, indentPW, "Default config package",
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001607 mPlatformCarrierConfigPackage, false /* considerCarrierPrivileges */);
1608 dumpCarrierServiceIfBound(fd, indentPW, "Requesting package", requestingPackage,
1609 true /* considerCarrierPrivileges */);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001610 }
shuoq26a3a4c2016-12-16 11:06:48 -08001611 }
1612
Rambo Wang1b89db32022-04-15 03:00:32 +00001613 private void printConfig(@NonNull PersistableBundle configApp,
1614 @NonNull IndentingPrintWriter indentPW, @NonNull String name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001615 indentPW.increaseIndent();
shuoq26a3a4c2016-12-16 11:06:48 -08001616 if (configApp == null) {
shuoq26a3a4c2016-12-16 11:06:48 -08001617 indentPW.println(name + " : null ");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001618 indentPW.decreaseIndent();
1619 indentPW.println("");
shuoq26a3a4c2016-12-16 11:06:48 -08001620 return;
1621 }
shuoq26a3a4c2016-12-16 11:06:48 -08001622 indentPW.println(name + " : ");
1623 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1624 Collections.sort(sortedKeys);
1625 indentPW.increaseIndent();
1626 indentPW.increaseIndent();
1627 for (String key : sortedKeys) {
1628 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1629 indentPW.println(key + " = " +
1630 Arrays.toString((Object[]) configApp.get(key)));
1631 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1632 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1633 } else {
1634 indentPW.println(key + " = " + configApp.get(key));
1635 }
Junda Liu43d723a2015-05-12 17:23:45 -07001636 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001637 indentPW.decreaseIndent();
1638 indentPW.decreaseIndent();
1639 indentPW.decreaseIndent();
1640 indentPW.println("");
Junda Liu43d723a2015-05-12 17:23:45 -07001641 }
1642
Hunter Knepshielde601f432020-02-19 10:16:04 -08001643 /**
1644 * Passes without problem when one of these conditions is true:
1645 * - The caller is a privileged UID (e.g. for dumpstate.cpp generating a bug report, where the
1646 * system knows the true caller plumbed in through the {@link android.os.BugreportManager} API).
1647 * - The caller's UID matches the supplied package.
1648 *
1649 * @throws SecurityException if none of the above conditions are met.
1650 */
Rambo Wang1b89db32022-04-15 03:00:32 +00001651 private void enforceCallerIsSystemOrRequestingPackage(@NonNull String requestingPackage)
Hunter Knepshielde601f432020-02-19 10:16:04 -08001652 throws SecurityException {
1653 final int callingUid = Binder.getCallingUid();
1654 if (callingUid == Process.ROOT_UID || callingUid == Process.SYSTEM_UID
1655 || callingUid == Process.SHELL_UID || callingUid == Process.PHONE_UID) {
1656 // Bug reports (dumpstate.cpp) run as SHELL, and let some other privileged UIDs through
1657 // as well.
1658 return;
1659 }
1660 // An app is trying to dump extra detail, block it if they aren't who they claim to be.
1661 AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class);
1662 if (appOps == null) {
1663 throw new SecurityException("No AppOps");
1664 }
1665 // Will throw a SecurityException if the UID and package don't match.
1666 appOps.checkPackage(callingUid, requestingPackage);
1667 }
1668
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001669 /**
1670 * Searches for one or more appropriate {@link CarrierService} instances to dump based on the
1671 * current connections.
1672 *
1673 * @param targetPkgName the target package name to dump carrier services for
1674 * @param considerCarrierPrivileges if true, allow a carrier service to be dumped if it shares
1675 * carrier privileges with {@code targetPkgName};
1676 * otherwise, only dump a carrier service if it is {@code
1677 * targetPkgName}
1678 */
Rambo Wang1b89db32022-04-15 03:00:32 +00001679 private void dumpCarrierServiceIfBound(@NonNull FileDescriptor fd,
1680 @NonNull IndentingPrintWriter indentPW, @NonNull String prefix,
1681 @NonNull String targetPkgName, boolean considerCarrierPrivileges) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001682 // Null package is possible if it's early in the boot process, there was a recent crash, we
1683 // loaded the config from XML most recently, or a SIM slot is empty. Carrier apps with
1684 // long-lived bindings should typically get dumped here regardless. Even if an app is being
1685 // used for multiple phoneIds, we assume that it's smart enough to handle that on its own,
1686 // and that in most cases we'd just be dumping duplicate information and bloating a report.
1687 indentPW.increaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001688 indentPW.println(prefix + " : " + targetPkgName);
1689 Set<String> dumpedPkgNames = new ArraySet<>(mServiceConnection.length);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001690 for (CarrierServiceConnection connection : mServiceConnection) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001691 if (connection == null || !SubscriptionManager.isValidPhoneId(connection.phoneId)
1692 || TextUtils.isEmpty(connection.pkgName)) {
1693 continue;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001694 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001695 final String servicePkgName = connection.pkgName;
1696 // Note: we intentionally ignore system components here because we should NOT match the
1697 // shell caller that's typically used for bug reports via non-BugreportManager triggers.
1698 final boolean exactPackageMatch = TextUtils.equals(targetPkgName, servicePkgName);
1699 final boolean carrierPrivilegesMatch =
1700 considerCarrierPrivileges && hasCarrierPrivileges(targetPkgName,
1701 connection.phoneId);
1702 if (!exactPackageMatch && !carrierPrivilegesMatch) continue;
1703 // Make sure this service is actually alive before trying to dump it. We don't pay
1704 // attention to mServiceBound[connection.phoneId] because typically carrier apps will
1705 // request long-lived bindings, and even if we unbind the app, it may still be alive due
1706 // to CarrierServiceBindHelper. Pull it out as a reference so even if it gets set to
1707 // null within the ServiceConnection during unbinding we can avoid an NPE.
1708 final IBinder service = connection.service;
1709 if (service == null || !service.isBinderAlive() || !service.pingBinder()) continue;
1710 // We've got a live service. Last check is just to make sure we don't dump a package
1711 // multiple times.
1712 if (!dumpedPkgNames.add(servicePkgName)) continue;
1713 if (!exactPackageMatch) {
1714 logd(targetPkgName + " has carrier privileges on phoneId " + connection.phoneId
1715 + ", service provided by " + servicePkgName);
1716 indentPW.increaseIndent();
1717 indentPW.println("Proxy : " + servicePkgName);
1718 indentPW.decreaseIndent();
1719 }
1720 // Flush before we let the app output anything to ensure correct ordering of output.
1721 // Internally, Binder#dump calls flush on its printer after finishing so we don't
1722 // need to do anything after.
1723 indentPW.flush();
1724 try {
1725 logd("Dumping " + servicePkgName);
1726 // We don't need to give the carrier service any args.
1727 connection.service.dump(fd, null /* args */);
1728 logd("Done with " + servicePkgName);
1729 } catch (RemoteException e) {
1730 logd("RemoteException from " + servicePkgName, e);
1731 indentPW.increaseIndent();
1732 indentPW.println("RemoteException");
1733 indentPW.increaseIndent();
1734 e.printStackTrace(indentPW);
1735 indentPW.decreaseIndent();
1736 indentPW.decreaseIndent();
1737 // We won't retry this package again because now it's in dumpedPkgNames.
1738 }
1739 indentPW.println("");
Jordan Liu46ed5db2020-01-28 08:55:20 -08001740 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001741 if (dumpedPkgNames.isEmpty()) {
1742 indentPW.increaseIndent();
1743 indentPW.println("Not bound");
1744 indentPW.decreaseIndent();
1745 indentPW.println("");
1746 }
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001747 indentPW.decreaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001748 }
1749
Rambo Wang1b89db32022-04-15 03:00:32 +00001750 private boolean hasCarrierPrivileges(@NonNull String pkgName, int phoneId) {
Jack Yude40a3f2022-11-19 22:29:12 -08001751 int subId = SubscriptionManager.getSubscriptionId(phoneId);
1752 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001753 return false;
1754 }
Jack Yude40a3f2022-11-19 22:29:12 -08001755 return TelephonyManager.from(mContext).createForSubscriptionId(subId)
1756 .checkCarrierPrivilegesForPackage(pkgName)
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001757 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001758 }
1759
Zach Johnson36d7aab2015-05-22 15:43:00 -07001760 private class CarrierServiceConnection implements ServiceConnection {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001761 final int phoneId;
Rambo Wang1b89db32022-04-15 03:00:32 +00001762 @NonNull final String pkgName;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001763 final int eventId;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001764 IBinder service;
1765
Rambo Wang1b89db32022-04-15 03:00:32 +00001766 CarrierServiceConnection(int phoneId, @NonNull String pkgName, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001767 this.phoneId = phoneId;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001768 this.pkgName = pkgName;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001769 this.eventId = eventId;
1770 }
1771
1772 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +00001773 public void onServiceConnected(@NonNull ComponentName name, @NonNull IBinder service) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001774 logd("Connected to config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001775 this.service = service;
1776 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1777 }
1778
1779 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +00001780 public void onServiceDisconnected(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001781 logd("Disconnected from config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001782 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001783 }
1784
1785 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +00001786 public void onBindingDied(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001787 logd("Binding died from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001788 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001789 }
1790
1791 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +00001792 public void onNullBinding(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001793 logd("Null binding from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001794 this.service = null;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001795 }
1796 }
1797
1798 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1799 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +00001800 public void onReceive(@NonNull Context context, @NonNull Intent intent) {
Rambo Wang1cf71512022-04-12 19:09:07 +00001801 switch (intent.getAction()) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001802 case Intent.ACTION_BOOT_COMPLETED:
1803 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1804 break;
1805
Rambo Wang6f8328d2022-04-15 01:26:23 +00001806 case TelephonyManager.ACTION_MULTI_SIM_CONFIG_CHANGED:
1807 mHandler.sendEmptyMessage(EVENT_MULTI_SIM_CONFIG_CHANGED);
1808 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001809 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001810 }
1811 }
1812
Rambo Wang1cf71512022-04-12 19:09:07 +00001813 private class CarrierServiceChangeCallback implements
1814 TelephonyManager.CarrierPrivilegesCallback {
1815 final int mPhoneId;
1816 // CarrierPrivilegesCallback will be triggered upon registration. Filter the first callback
1817 // here since we really care of the *change* of carrier service instead of the content
1818 private boolean mHasSentServiceChangeCallback;
1819
1820 CarrierServiceChangeCallback(int phoneId) {
1821 this.mPhoneId = phoneId;
1822 this.mHasSentServiceChangeCallback = false;
1823 }
1824
1825 @Override
1826 public void onCarrierPrivilegesChanged(
1827 @androidx.annotation.NonNull Set<String> privilegedPackageNames,
1828 @androidx.annotation.NonNull Set<Integer> privilegedUids) {
1829 // Ignored, not interested here
1830 }
1831
1832 @Override
1833 public void onCarrierServiceChanged(
1834 @androidx.annotation.Nullable String carrierServicePackageName,
1835 int carrierServiceUid) {
1836 // Ignore the first callback which is triggered upon registration
1837 if (!mHasSentServiceChangeCallback) {
1838 mHasSentServiceChangeCallback = true;
1839 return;
1840 }
1841 mHandler.sendMessage(
1842 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, mPhoneId, -1,
1843 carrierServicePackageName));
1844 }
1845 }
1846
Rambo Wangab13e3e2021-04-08 15:01:06 -07001847 // Get readable string for the message code supported in this class.
Rambo Wang1b89db32022-04-15 03:00:32 +00001848 @NonNull
Rambo Wangab13e3e2021-04-08 15:01:06 -07001849 private static String eventToString(int code) {
1850 switch (code) {
1851 case EVENT_CLEAR_CONFIG:
1852 return "EVENT_CLEAR_CONFIG";
1853 case EVENT_CONNECTED_TO_DEFAULT:
1854 return "EVENT_CONNECTED_TO_DEFAULT";
1855 case EVENT_CONNECTED_TO_CARRIER:
1856 return "EVENT_CONNECTED_TO_CARRIER";
1857 case EVENT_FETCH_DEFAULT_DONE:
1858 return "EVENT_FETCH_DEFAULT_DONE";
1859 case EVENT_FETCH_CARRIER_DONE:
1860 return "EVENT_FETCH_CARRIER_DONE";
1861 case EVENT_DO_FETCH_DEFAULT:
1862 return "EVENT_DO_FETCH_DEFAULT";
1863 case EVENT_DO_FETCH_CARRIER:
1864 return "EVENT_DO_FETCH_CARRIER";
1865 case EVENT_PACKAGE_CHANGED:
1866 return "EVENT_PACKAGE_CHANGED";
1867 case EVENT_BIND_DEFAULT_TIMEOUT:
1868 return "EVENT_BIND_DEFAULT_TIMEOUT";
1869 case EVENT_BIND_CARRIER_TIMEOUT:
1870 return "EVENT_BIND_CARRIER_TIMEOUT";
1871 case EVENT_CHECK_SYSTEM_UPDATE:
1872 return "EVENT_CHECK_SYSTEM_UPDATE";
1873 case EVENT_SYSTEM_UNLOCKED:
1874 return "EVENT_SYSTEM_UNLOCKED";
1875 case EVENT_FETCH_DEFAULT_TIMEOUT:
1876 return "EVENT_FETCH_DEFAULT_TIMEOUT";
1877 case EVENT_FETCH_CARRIER_TIMEOUT:
1878 return "EVENT_FETCH_CARRIER_TIMEOUT";
1879 case EVENT_SUBSCRIPTION_INFO_UPDATED:
1880 return "EVENT_SUBSCRIPTION_INFO_UPDATED";
1881 case EVENT_MULTI_SIM_CONFIG_CHANGED:
1882 return "EVENT_MULTI_SIM_CONFIG_CHANGED";
1883 case EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG:
1884 return "EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG";
1885 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE:
1886 return "EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE";
1887 case EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG:
1888 return "EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG";
1889 case EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
1890 return "EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT";
1891 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
1892 return "EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT";
1893 default:
1894 return "UNKNOWN(" + code + ")";
1895 }
1896 }
1897
Rambo Wang1b89db32022-04-15 03:00:32 +00001898 private void logd(@NonNull String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001899 Log.d(LOG_TAG, msg);
1900 }
1901
Rambo Wang1b89db32022-04-15 03:00:32 +00001902 private void logd(@NonNull String msg, Throwable tr) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001903 Log.d(LOG_TAG, msg, tr);
1904 }
1905
Rambo Wang1b89db32022-04-15 03:00:32 +00001906 private void logdWithLocalLog(@NonNull String msg) {
chen xudb04c292019-03-26 17:01:15 -07001907 Log.d(LOG_TAG, msg);
1908 mCarrierConfigLoadingLog.log(msg);
1909 }
1910
Rambo Wang1b89db32022-04-15 03:00:32 +00001911 private void loge(@NonNull String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001912 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07001913 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001914 }
1915}