blob: ef71016406019bcaee12fe88f3fbf235f4a51747 [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
joonhunshin21a86812023-12-10 08:21:25 +000019import static android.content.pm.PackageManager.FEATURE_TELEPHONY_SUBSCRIPTION;
Jonathan Basseri65273c82017-07-25 15:08:42 -070020import static android.service.carrier.CarrierService.ICarrierServiceWrapper.KEY_CONFIG_BUNDLE;
21import static android.service.carrier.CarrierService.ICarrierServiceWrapper.RESULT_ERROR;
joonhunshin21a86812023-12-10 08:21:25 +000022import static android.telephony.TelephonyManager.ENABLE_FEATURE_MAPPING;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080023
Jonathan Basseri11f89572015-05-05 11:57:39 -070024import android.annotation.NonNull;
Torbjorn Eklund723527a2019-02-13 11:16:25 +010025import android.annotation.Nullable;
Hunter Knepshielde601f432020-02-19 10:16:04 -080026import android.app.AppOpsManager;
joonhunshin21a86812023-12-10 08:21:25 +000027import android.app.compat.CompatChanges;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080028import android.content.BroadcastReceiver;
29import android.content.ComponentName;
30import android.content.Context;
31import android.content.Intent;
32import android.content.IntentFilter;
33import android.content.ServiceConnection;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070034import android.content.SharedPreferences;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070035import android.content.pm.PackageInfo;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070036import android.content.pm.PackageManager;
Junda Liu43d723a2015-05-12 17:23:45 -070037import android.os.Binder;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070038import android.os.Build;
Jonathan Basseri65273c82017-07-25 15:08:42 -070039import android.os.Bundle;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080040import android.os.Handler;
Rambo Wanga27fbe52022-04-12 19:09:07 +000041import android.os.HandlerExecutor;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080042import android.os.IBinder;
Rambo Wang7e3bc122021-03-23 09:24:38 -070043import android.os.Looper;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080044import android.os.Message;
mattgilbride5b6b7182023-04-03 18:56:44 +000045import android.os.PermissionEnforcer;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070046import android.os.PersistableBundle;
Hunter Knepshielde601f432020-02-19 10:16:04 -080047import android.os.Process;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080048import android.os.RemoteException;
Jonathan Basseri65273c82017-07-25 15:08:42 -070049import android.os.ResultReceiver;
Meng Wang97a6a462020-01-23 16:22:16 -080050import android.os.UserHandle;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070051import android.preference.PreferenceManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080052import android.service.carrier.CarrierIdentifier;
Zach Johnson36d7aab2015-05-22 15:43:00 -070053import android.service.carrier.CarrierService;
54import android.service.carrier.ICarrierService;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080055import android.telephony.CarrierConfigManager;
56import android.telephony.SubscriptionManager;
Peter Wangc035ce42020-01-08 21:00:22 -080057import android.telephony.TelephonyFrameworkInitializer;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080058import android.telephony.TelephonyManager;
rambowangd63ba342022-10-02 11:21:08 -050059import android.telephony.TelephonyRegistryManager;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -080060import android.text.TextUtils;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080061import android.util.ArraySet;
chen xudb04c292019-03-26 17:01:15 -070062import android.util.LocalLog;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080063import android.util.Log;
64
Rambo Wang7e3bc122021-03-23 09:24:38 -070065import com.android.internal.annotations.VisibleForTesting;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080066import com.android.internal.telephony.ICarrierConfigLoader;
67import com.android.internal.telephony.IccCardConstants;
68import com.android.internal.telephony.Phone;
Jack Yu771c6ce2023-02-02 17:51:42 -080069import com.android.internal.telephony.PhoneConfigurationManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080070import com.android.internal.telephony.PhoneFactory;
Jeff Davidsona8e4e242018-03-15 17:16:18 -070071import com.android.internal.telephony.TelephonyPermissions;
joonhunshin21a86812023-12-10 08:21:25 +000072import com.android.internal.telephony.flags.FeatureFlags;
Jack Yue37dd262022-12-16 11:53:37 -080073import com.android.internal.telephony.subscription.SubscriptionManagerService;
Meng Wanga320b942019-11-25 11:21:26 -080074import com.android.internal.telephony.util.ArrayUtils;
Jack Yu9be55d32023-08-10 13:47:46 -070075import com.android.internal.telephony.util.TelephonyUtils;
shuoq26a3a4c2016-12-16 11:06:48 -080076import com.android.internal.util.IndentingPrintWriter;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080077
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070078import java.io.File;
Junda Liu43d723a2015-05-12 17:23:45 -070079import java.io.FileDescriptor;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070080import java.io.FileInputStream;
81import java.io.FileNotFoundException;
82import java.io.FileOutputStream;
Jonathan Basseri1f743c92015-05-15 00:19:46 -070083import java.io.FilenameFilter;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070084import java.io.IOException;
Junda Liu43d723a2015-05-12 17:23:45 -070085import java.io.PrintWriter;
Jack Yu9be55d32023-08-10 13:47:46 -070086import java.nio.file.Files;
87import java.nio.file.Paths;
88import java.nio.file.attribute.BasicFileAttributes;
89import java.text.SimpleDateFormat;
shuoq26a3a4c2016-12-16 11:06:48 -080090import java.util.ArrayList;
91import java.util.Arrays;
92import java.util.Collections;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080093import java.util.List;
Jack Yu9be55d32023-08-10 13:47:46 -070094import java.util.Locale;
rambowang14757c22022-10-03 11:54:56 -050095import java.util.Objects;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080096import java.util.Set;
Jack Yu9be55d32023-08-10 13:47:46 -070097import java.util.stream.Collectors;
98import java.util.stream.Stream;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080099
100/**
101 * CarrierConfigLoader binds to privileged carrier apps to fetch carrier config overlays.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800102 */
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800103public class CarrierConfigLoader extends ICarrierConfigLoader.Stub {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700104 private static final String LOG_TAG = "CarrierConfigLoader";
Meng Wang33ad2bc2017-03-16 20:21:20 -0700105
Jack Yu9be55d32023-08-10 13:47:46 -0700106 private static final SimpleDateFormat TIME_FORMAT =
107 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
108
Meng Wang33ad2bc2017-03-16 20:21:20 -0700109 // Package name for platform carrier config app, bundled with system image.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000110 @NonNull private final String mPlatformCarrierConfigPackage;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800111
112 /** The singleton instance. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000113 @Nullable private static CarrierConfigLoader sInstance;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800114 // The context for phone app, passed from PhoneGlobals.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000115 @NonNull private Context mContext;
116
117 // All the states below (array indexed by phoneId) are non-null. But the member of the array
118 // is nullable, when e.g. the config for the phone is not loaded yet
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800119 // Carrier configs from default app, indexed by phoneID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000120 @NonNull private PersistableBundle[] mConfigFromDefaultApp;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800121 // Carrier configs from privileged carrier config app, indexed by phoneID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000122 @NonNull private PersistableBundle[] mConfigFromCarrierApp;
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100123 // Persistent Carrier configs that are provided via the override test API, indexed by phone ID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000124 @NonNull private PersistableBundle[] mPersistentOverrideConfigs;
Hall Liu506724b2018-10-22 18:16:14 -0700125 // Carrier configs that are provided via the override test API, indexed by phone ID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000126 @NonNull private PersistableBundle[] mOverrideConfigs;
Jayachandran C645ec612020-01-10 10:30:25 -0800127 // Carrier configs to override code default when there is no SIM inserted
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000128 @NonNull private PersistableBundle mNoSimConfig;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800129 // Service connection for binding to config app.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000130 @NonNull private CarrierServiceConnection[] mServiceConnection;
Jayachandran C645ec612020-01-10 10:30:25 -0800131 // Service connection for binding to carrier config app for no SIM config.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000132 @NonNull private CarrierServiceConnection[] mServiceConnectionForNoSimConfig;
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700133 // Whether we are bound to a service for each phone
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000134 @NonNull private boolean[] mServiceBound;
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700135 // Whether we are bound to a service for no SIM config
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000136 @NonNull private boolean[] mServiceBoundForNoSimConfig;
Sarah Chin807d5c62020-03-30 17:21:09 -0700137 // Whether we have sent config change broadcast for each phone id.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000138 @NonNull private boolean[] mHasSentConfigChange;
Sarah Chin807d5c62020-03-30 17:21:09 -0700139 // Whether the broadcast was sent from EVENT_SYSTEM_UNLOCKED, to track rebroadcasts
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000140 @NonNull private boolean[] mFromSystemUnlocked;
Rambo Wanga27fbe52022-04-12 19:09:07 +0000141 // CarrierService change monitoring
142 @NonNull private CarrierServiceChangeCallback[] mCarrierServiceChangeCallbacks;
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000143
Jack Yu771c6ce2023-02-02 17:51:42 -0800144 // Broadcast receiver for system events
Rambo Wanga27fbe52022-04-12 19:09:07 +0000145 @NonNull
146 private final BroadcastReceiver mSystemBroadcastReceiver = new ConfigLoaderBroadcastReceiver();
Jack Yu9be55d32023-08-10 13:47:46 -0700147 @NonNull private final LocalLog mCarrierConfigLoadingLog = new LocalLog(256);
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000148 // Number of phone instances (active modem count)
149 private int mNumPhones;
chen xudb04c292019-03-26 17:01:15 -0700150
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800151
152 // Message codes; see mHandler below.
Jack Yu3beaf9d2023-04-14 09:17:27 -0700153 // Request from UiccController when SIM becomes absent or error.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800154 private static final int EVENT_CLEAR_CONFIG = 0;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800155 // Has connected to default app.
156 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
157 // Has connected to carrier app.
158 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700159 // Config has been loaded from default app (or cache).
160 private static final int EVENT_FETCH_DEFAULT_DONE = 5;
161 // Config has been loaded from carrier app (or cache).
162 private static final int EVENT_FETCH_CARRIER_DONE = 6;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700163 // Attempt to fetch from default app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700164 private static final int EVENT_DO_FETCH_DEFAULT = 7;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700165 // Attempt to fetch from carrier app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700166 private static final int EVENT_DO_FETCH_CARRIER = 8;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700167 // A package has been installed, uninstalled, or updated.
168 private static final int EVENT_PACKAGE_CHANGED = 9;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700169 // Bind timed out for the default app.
170 private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
171 // Bind timed out for a carrier app.
172 private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700173 // Check if the system fingerprint has changed.
174 private static final int EVENT_CHECK_SYSTEM_UPDATE = 12;
Nanxi Chen3d670502016-03-17 16:32:09 -0700175 // Rerun carrier config binding after system is unlocked.
176 private static final int EVENT_SYSTEM_UNLOCKED = 13;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700177 // Fetching config timed out from the default app.
178 private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14;
179 // Fetching config timed out from a carrier app.
180 private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15;
Jack Yu3beaf9d2023-04-14 09:17:27 -0700181 // SubscriptionManagerService has finished updating the sub for the carrier config.
Nathan Harold48ac0972019-03-13 22:33:01 -0700182 private static final int EVENT_SUBSCRIPTION_INFO_UPDATED = 16;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700183 // Multi-SIM config changed.
184 private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 17;
Jayachandran C645ec612020-01-10 10:30:25 -0800185 // Attempt to fetch from default app or read from XML for no SIM case.
186 private static final int EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG = 18;
187 // No SIM config has been loaded from default app (or cache).
188 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE = 19;
189 // Has connected to default app for no SIM config.
190 private static final int EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG = 20;
191 // Bind timed out for the default app when trying to fetch no SIM config.
192 private static final int EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 21;
193 // Fetching config timed out from the default app for no SIM config.
194 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 22;
Rambo Wangab13e3e2021-04-08 15:01:06 -0700195 // NOTE: any new EVENT_* values must be added to method eventToString().
Jonathan Basseri930701e2015-06-11 20:56:43 -0700196
Junda Liu6cb45002016-03-22 17:18:20 -0700197 private static final int BIND_TIMEOUT_MILLIS = 30000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800198
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800199 // Keys used for saving and restoring config bundle from file.
200 private static final String KEY_VERSION = "__carrier_config_package_version__";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800201
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100202 private static final String OVERRIDE_PACKAGE_ADDITION = "-override";
203
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700204 // SharedPreferences key for last known build fingerprint.
205 private static final String KEY_FINGERPRINT = "build_fingerprint";
206
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800207 // Argument for #dump that indicates we should also call the default and specified carrier
208 // service's #dump method. In multi-SIM devices, it's possible that carrier A is on SIM 1 and
209 // carrier B is on SIM 2, in which case we should not dump carrier B's service when carrier A
210 // requested the dump.
211 private static final String DUMP_ARG_REQUESTING_PACKAGE = "--requesting-package";
212
rambowang14757c22022-10-03 11:54:56 -0500213 // Configs that should always be included when clients calls getConfig[ForSubId] with specified
214 // keys (even configs are not explicitly specified). Those configs have special purpose for the
215 // carrier config APIs to work correctly.
216 private static final String[] CONFIG_SUBSET_METADATA_KEYS = new String[] {
217 CarrierConfigManager.KEY_CARRIER_CONFIG_VERSION_STRING,
218 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL
219 };
220
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800221 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700222 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700223 // For each phoneId, the event sequence should be:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700224 // fetch default, connected to default, fetch default (async), fetch default done,
225 // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700226 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700227 // If there is a saved config file for either the default app or the carrier app, we skip
228 // binding to the app and go straight from fetch to loaded.
229 //
230 // At any time, at most one connection is active. If events are not in this order, previous
231 // connection will be unbound, so only latest event takes effect.
232 //
233 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
234 // 1. loading from carrier app (even if read from a file)
235 // 2. loading from default app if there is no carrier app (even if read from a file)
236 // 3. clearing config (e.g. due to sim removal)
237 // 4. encountering bind or IPC error
Jonathan Basseri65273c82017-07-25 15:08:42 -0700238 private class ConfigHandler extends Handler {
Rambo Wang7e3bc122021-03-23 09:24:38 -0700239 ConfigHandler(@NonNull Looper looper) {
240 super(looper);
241 }
242
Jonathan Basseri65273c82017-07-25 15:08:42 -0700243 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000244 public void handleMessage(@NonNull Message msg) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700245 final int phoneId = msg.arg1;
Jack Yu9be55d32023-08-10 13:47:46 -0700246 logd(eventToString(msg.what) + " phoneId: " + phoneId);
Malcolm Chen5ea18532019-10-24 19:55:44 -0700247 if (!SubscriptionManager.isValidPhoneId(phoneId)
248 && msg.what != EVENT_MULTI_SIM_CONFIG_CHANGED) {
249 return;
250 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800251 switch (msg.what) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800252 case EVENT_CLEAR_CONFIG: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700253 clearConfigForPhone(phoneId, true);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800254 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700255 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700256
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800257 case EVENT_SYSTEM_UNLOCKED: {
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000258 for (int i = 0; i < mNumPhones; ++i) {
Sarah Chin807d5c62020-03-30 17:21:09 -0700259 // When the user unlocks the device, send the broadcast again (with a
260 // rebroadcast extra) if we have sent it before unlock. This will avoid
261 // trying to load the carrier config when the SIM is still loading when the
262 // unlock happens.
Junda Liu03aad762017-07-21 13:32:17 -0700263 if (mHasSentConfigChange[i]) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800264 logdWithLocalLog("System unlocked");
Sarah Chin807d5c62020-03-30 17:21:09 -0700265 mFromSystemUnlocked[i] = true;
Junda Liu03aad762017-07-21 13:32:17 -0700266 updateConfigForPhoneId(i);
267 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700268 }
269 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700270 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700271
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800272 case EVENT_PACKAGE_CHANGED: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700273 final String carrierPackageName = (String) msg.obj;
Rambo Wanga27fbe52022-04-12 19:09:07 +0000274 // Always clear up the cache and re-load config from scratch since the carrier
275 // service change is reliable and specific to the phoneId now.
276 clearCachedConfigForPackage(carrierPackageName);
277 logdWithLocalLog("Package changed: " + carrierPackageName
278 + ", phone=" + phoneId);
279 updateConfigForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700280 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700281 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700282
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800283 case EVENT_DO_FETCH_DEFAULT: {
Rambo Wang9bc7d362021-03-09 09:10:58 -0800284 // Clear in-memory cache for carrier app config, so when carrier app gets
285 // uninstalled, no stale config is left.
286 if (mConfigFromCarrierApp[phoneId] != null
287 && getCarrierPackageForPhoneId(phoneId) == null) {
288 mConfigFromCarrierApp[phoneId] = null;
289 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100290 // Restore persistent override values.
291 PersistableBundle config = restoreConfigFromXml(
292 mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId);
293 if (config != null) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100294 mPersistentOverrideConfigs[phoneId] = config;
295 }
296
297 config = restoreConfigFromXml(mPlatformCarrierConfigPackage, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700298 if (config != null) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700299 mConfigFromDefaultApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700300 Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700301 newMsg.getData().putBoolean("loaded_from_xml", true);
302 mHandler.sendMessage(newMsg);
303 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700304 // No cached config, so fetch it from the default app.
305 if (bindToConfigPackage(
306 mPlatformCarrierConfigPackage,
307 phoneId,
308 EVENT_CONNECTED_TO_DEFAULT)) {
309 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800310 obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1 /*arg2*/,
311 getMessageToken(phoneId)),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700312 BIND_TIMEOUT_MILLIS);
313 } else {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800314 // Put a stub bundle in place so that the rest of the logic continues
315 // smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000316 mConfigFromDefaultApp[phoneId] = new PersistableBundle();
Jonathan Basseri65273c82017-07-25 15:08:42 -0700317 // Send broadcast if bind fails.
Jack Yue37dd262022-12-16 11:53:37 -0800318 updateSubscriptionDatabase(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700319 // TODO: We *must* call unbindService even if bindService returns false.
320 // (And possibly if SecurityException was thrown.)
chen xudb04c292019-03-26 17:01:15 -0700321 loge("binding to default app: "
322 + mPlatformCarrierConfigPackage + " fails");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700323 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800324 }
325 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700326 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800327
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800328 case EVENT_CONNECTED_TO_DEFAULT: {
Rambo Wang610fdf62021-03-08 21:37:11 -0800329 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT, getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700330 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800331 // If new service connection has been created, unbind.
332 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700333 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800334 break;
335 }
chen xu02581692018-11-11 19:03:44 -0800336 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700337 // ResultReceiver callback will execute in this Handler's thread.
338 final ResultReceiver resultReceiver =
339 new ResultReceiver(this) {
340 @Override
341 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700342 unbindIfBound(mContext, conn, phoneId);
Rambo Wanga9d27a52021-04-09 16:17:33 -0700343 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT,
344 getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700345 // If new service connection has been created, this is stale.
346 if (mServiceConnection[phoneId] != conn) {
347 loge("Received response for stale request.");
348 return;
349 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700350 if (resultCode == RESULT_ERROR || resultData == null) {
351 // On error, abort config fetching.
352 loge("Failed to get carrier config");
Jack Yue37dd262022-12-16 11:53:37 -0800353 updateSubscriptionDatabase(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700354 return;
355 }
356 PersistableBundle config =
357 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100358 saveConfigToXml(mPlatformCarrierConfigPackage, "", phoneId,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800359 carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700360 mConfigFromDefaultApp[phoneId] = config;
361 sendMessage(
362 obtainMessage(
363 EVENT_FETCH_DEFAULT_DONE, phoneId, -1));
364 }
365 };
366 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800367 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700368 ICarrierService carrierService =
369 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800370 carrierService.getCarrierConfig(phoneId, carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800371 logdWithLocalLog("Fetch config for default app: "
chen xudb04c292019-03-26 17:01:15 -0700372 + mPlatformCarrierConfigPackage
Jack Yu9be55d32023-08-10 13:47:46 -0700373 + ", carrierId=" + carrierId.getSpecificCarrierId());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700374 } catch (RemoteException e) {
chen xudb04c292019-03-26 17:01:15 -0700375 loge("Failed to get carrier config from default app: " +
Jack Yu9be55d32023-08-10 13:47:46 -0700376 mPlatformCarrierConfigPackage + " err: " + e);
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700377 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700378 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800379 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700380 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800381 obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1 /*arg2*/,
382 getMessageToken(phoneId)),
Jonathan Basseri65273c82017-07-25 15:08:42 -0700383 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800384 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700385 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800386
Jonathan Basseri930701e2015-06-11 20:56:43 -0700387 case EVENT_BIND_DEFAULT_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800388 case EVENT_FETCH_DEFAULT_TIMEOUT: {
389 loge("Bind/fetch time out from " + mPlatformCarrierConfigPackage);
Rambo Wang610fdf62021-03-08 21:37:11 -0800390 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT, getMessageToken(phoneId));
chen xuf60b3162019-05-01 21:31:05 -0700391 // If we attempted to bind to the app, but the service connection is null due to
392 // the race condition that clear config event happens before bind/fetch complete
393 // then config was cleared while we were waiting and we should not continue.
394 if (mServiceConnection[phoneId] != null) {
395 // If a ResponseReceiver callback is in the queue when this happens, we will
396 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700397 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700398 broadcastConfigChangedIntent(phoneId);
399 }
Nathan Harold1122e3d2021-01-22 15:45:24 -0800400 // Put a stub bundle in place so that the rest of the logic continues smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000401 mConfigFromDefaultApp[phoneId] = new PersistableBundle();
Jack Yue37dd262022-12-16 11:53:37 -0800402 updateSubscriptionDatabase(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700403 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700404 }
Jonathan Basseri930701e2015-06-11 20:56:43 -0700405
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800406 case EVENT_FETCH_DEFAULT_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700407 // If we attempted to bind to the app, but the service connection is null, then
408 // config was cleared while we were waiting and we should not continue.
409 if (!msg.getData().getBoolean("loaded_from_xml", false)
410 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800411 break;
412 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700413 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700414 if (carrierPackageName != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800415 logd("Found carrier config app: " + carrierPackageName);
Jordan Liu38a614c2019-03-20 15:01:17 -0700416 sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId, -1));
Jonathan Basserib919e932015-05-27 16:53:08 +0000417 } else {
Jack Yue37dd262022-12-16 11:53:37 -0800418 updateSubscriptionDatabase(phoneId);
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700419 }
420 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700421 }
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700422
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800423 case EVENT_DO_FETCH_CARRIER: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700424 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700425 final PersistableBundle config =
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100426 restoreConfigFromXml(carrierPackageName, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700427 if (config != null) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700428 mConfigFromCarrierApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700429 Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700430 newMsg.getData().putBoolean("loaded_from_xml", true);
431 sendMessage(newMsg);
432 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700433 // No cached config, so fetch it from a carrier app.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800434 if (carrierPackageName != null && bindToConfigPackage(carrierPackageName,
435 phoneId, EVENT_CONNECTED_TO_CARRIER)) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700436 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800437 obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1 /*arg2*/,
438 getMessageToken(phoneId)),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700439 BIND_TIMEOUT_MILLIS);
440 } else {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800441 // Put a stub bundle in place so that the rest of the logic continues
442 // smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000443 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
Jonathan Basseri65273c82017-07-25 15:08:42 -0700444 // Send broadcast if bind fails.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700445 broadcastConfigChangedIntent(phoneId);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800446 loge("Bind to carrier app: " + carrierPackageName + " fails");
Jack Yue37dd262022-12-16 11:53:37 -0800447 updateSubscriptionDatabase(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700448 }
449 }
450 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700451 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700452
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800453 case EVENT_CONNECTED_TO_CARRIER: {
Rambo Wang610fdf62021-03-08 21:37:11 -0800454 removeMessages(EVENT_BIND_CARRIER_TIMEOUT, getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700455 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800456 // If new service connection has been created, unbind.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700457 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700458 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800459 break;
460 }
chen xu02581692018-11-11 19:03:44 -0800461 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700462 // ResultReceiver callback will execute in this Handler's thread.
463 final ResultReceiver resultReceiver =
464 new ResultReceiver(this) {
465 @Override
466 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700467 unbindIfBound(mContext, conn, phoneId);
Rambo Wanga9d27a52021-04-09 16:17:33 -0700468 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT,
469 getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700470 // If new service connection has been created, this is stale.
471 if (mServiceConnection[phoneId] != conn) {
472 loge("Received response for stale request.");
473 return;
474 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700475 if (resultCode == RESULT_ERROR || resultData == null) {
476 // On error, abort config fetching.
chen xudb04c292019-03-26 17:01:15 -0700477 loge("Failed to get carrier config from carrier app: "
478 + getCarrierPackageForPhoneId(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700479 broadcastConfigChangedIntent(phoneId);
Jack Yue37dd262022-12-16 11:53:37 -0800480 updateSubscriptionDatabase(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700481 return;
482 }
483 PersistableBundle config =
484 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100485 saveConfigToXml(getCarrierPackageForPhoneId(phoneId), "",
486 phoneId, carrierId, config);
Amit Mahajan304e92b2021-04-08 14:03:30 -0700487 if (config != null) {
488 mConfigFromCarrierApp[phoneId] = config;
489 } else {
490 logdWithLocalLog("Config from carrier app is null "
491 + "for phoneId " + phoneId);
492 // Put a stub bundle in place so that the rest of the logic
493 // continues smoothly.
494 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
495 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700496 sendMessage(
497 obtainMessage(
498 EVENT_FETCH_CARRIER_DONE, phoneId, -1));
499 }
500 };
501 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800502 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700503 ICarrierService carrierService =
504 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800505 carrierService.getCarrierConfig(phoneId, carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800506 logdWithLocalLog("Fetch config for carrier app: "
chen xudb04c292019-03-26 17:01:15 -0700507 + getCarrierPackageForPhoneId(phoneId)
Jack Yu9be55d32023-08-10 13:47:46 -0700508 + ", carrierId=" + carrierId.getSpecificCarrierId());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700509 } catch (RemoteException e) {
Jack Yu9be55d32023-08-10 13:47:46 -0700510 loge("Failed to get carrier config: " + e);
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700511 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700512 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800513 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700514 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800515 obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1 /*arg2*/,
516 getMessageToken(phoneId)),
Jonathan Basseri65273c82017-07-25 15:08:42 -0700517 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800518 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700519 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800520
Jonathan Basseri930701e2015-06-11 20:56:43 -0700521 case EVENT_BIND_CARRIER_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800522 case EVENT_FETCH_CARRIER_TIMEOUT: {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800523 loge("Bind/fetch from carrier app timeout, package="
524 + getCarrierPackageForPhoneId(phoneId));
Rambo Wang610fdf62021-03-08 21:37:11 -0800525 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT, getMessageToken(phoneId));
chen xuf60b3162019-05-01 21:31:05 -0700526 // If we attempted to bind to the app, but the service connection is null due to
527 // the race condition that clear config event happens before bind/fetch complete
528 // then config was cleared while we were waiting and we should not continue.
529 if (mServiceConnection[phoneId] != null) {
530 // If a ResponseReceiver callback is in the queue when this happens, we will
531 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700532 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700533 broadcastConfigChangedIntent(phoneId);
534 }
Nathan Harold1122e3d2021-01-22 15:45:24 -0800535 // Put a stub bundle in place so that the rest of the logic continues smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000536 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
Jack Yue37dd262022-12-16 11:53:37 -0800537 updateSubscriptionDatabase(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700538 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700539 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800540 case EVENT_FETCH_CARRIER_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700541 // If we attempted to bind to the app, but the service connection is null, then
542 // config was cleared while we were waiting and we should not continue.
543 if (!msg.getData().getBoolean("loaded_from_xml", false)
544 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800545 break;
546 }
Jack Yue37dd262022-12-16 11:53:37 -0800547 updateSubscriptionDatabase(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800548 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700549 }
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700550
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800551 case EVENT_CHECK_SYSTEM_UPDATE: {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700552 SharedPreferences sharedPrefs =
553 PreferenceManager.getDefaultSharedPreferences(mContext);
554 final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
555 if (!Build.FINGERPRINT.equals(lastFingerprint)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800556 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700557 "Build fingerprint changed. old: "
558 + lastFingerprint
559 + " new: "
560 + Build.FINGERPRINT);
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700561 clearCachedConfigForPackage(null);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700562 sharedPrefs
563 .edit()
564 .putString(KEY_FINGERPRINT, Build.FINGERPRINT)
565 .apply();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700566 }
567 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700568 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700569
570 case EVENT_SUBSCRIPTION_INFO_UPDATED:
571 broadcastConfigChangedIntent(phoneId);
572 break;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700573 case EVENT_MULTI_SIM_CONFIG_CHANGED:
574 onMultiSimConfigChanged();
575 break;
Jayachandran C645ec612020-01-10 10:30:25 -0800576
577 case EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG: {
578 PersistableBundle config =
579 restoreNoSimConfigFromXml(mPlatformCarrierConfigPackage);
580
581 if (config != null) {
Jayachandran C645ec612020-01-10 10:30:25 -0800582 mNoSimConfig = config;
583 sendMessage(
584 obtainMessage(
585 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE,
586 phoneId, -1));
587 } else {
588 // No cached config, so fetch it from the default app.
589 if (bindToConfigPackage(
590 mPlatformCarrierConfigPackage,
591 phoneId,
592 EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG)) {
593 sendMessageDelayed(
594 obtainMessage(
595 EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT,
596 phoneId, -1), BIND_TIMEOUT_MILLIS);
597 } else {
598 broadcastConfigChangedIntent(phoneId, false);
599 // TODO: We *must* call unbindService even if bindService returns false.
600 // (And possibly if SecurityException was thrown.)
601 loge("binding to default app to fetch no SIM config: "
602 + mPlatformCarrierConfigPackage + " fails");
603 }
604 }
605 break;
606 }
607
608 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE: {
609 broadcastConfigChangedIntent(phoneId, false);
610 break;
611 }
612
613 case EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
614 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT: {
615 loge("Bind/fetch time out for no SIM config from "
616 + mPlatformCarrierConfigPackage);
617 removeMessages(EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
618 // If we attempted to bind to the app, but the service connection is null due to
619 // the race condition that clear config event happens before bind/fetch complete
620 // then config was cleared while we were waiting and we should not continue.
621 if (mServiceConnectionForNoSimConfig[phoneId] != null) {
622 // If a ResponseReceiver callback is in the queue when this happens, we will
623 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700624 unbindIfBoundForNoSimConfig(mContext,
625 mServiceConnectionForNoSimConfig[phoneId], phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800626 }
627 broadcastConfigChangedIntent(phoneId, false);
628 break;
629 }
630
631 case EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG: {
632 removeMessages(EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
633 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
634 // If new service connection has been created, unbind.
635 if (mServiceConnectionForNoSimConfig[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700636 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800637 break;
638 }
639
640 // ResultReceiver callback will execute in this Handler's thread.
641 final ResultReceiver resultReceiver =
642 new ResultReceiver(this) {
643 @Override
644 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700645 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800646 // If new service connection has been created, this is stale.
647 if (mServiceConnectionForNoSimConfig[phoneId] != conn) {
648 loge("Received response for stale request.");
649 return;
650 }
651 removeMessages(EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
652 if (resultCode == RESULT_ERROR || resultData == null) {
653 // On error, abort config fetching.
654 loge("Failed to get no SIM carrier config");
655 return;
656 }
657 PersistableBundle config =
658 resultData.getParcelable(KEY_CONFIG_BUNDLE);
659 saveNoSimConfigToXml(mPlatformCarrierConfigPackage, config);
660 mNoSimConfig = config;
661 sendMessage(
662 obtainMessage(
663 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE,
664 phoneId, -1));
665 }
666 };
667 // Now fetch the config asynchronously from the ICarrierService.
668 try {
669 ICarrierService carrierService =
670 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800671 carrierService.getCarrierConfig(phoneId, null, resultReceiver);
Jayachandran C645ec612020-01-10 10:30:25 -0800672 logdWithLocalLog("Fetch no sim config from default app: "
673 + mPlatformCarrierConfigPackage);
674 } catch (RemoteException e) {
675 loge("Failed to get no sim carrier config from default app: " +
Jack Yu9be55d32023-08-10 13:47:46 -0700676 mPlatformCarrierConfigPackage + " err: " + e);
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700677 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800678 break; // So we don't set a timeout.
679 }
680 sendMessageDelayed(
681 obtainMessage(
682 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT,
683 phoneId, -1), BIND_TIMEOUT_MILLIS);
684 break;
685 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800686 }
687 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700688 }
689
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000690 @NonNull private final Handler mHandler;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800691
joonhunshin21a86812023-12-10 08:21:25 +0000692 @NonNull private final FeatureFlags mFeatureFlags;
693
694 @NonNull private final PackageManager mPackageManager;
695
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800696 /**
697 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
698 * receiver for relevant events.
699 */
Rambo Wang7e3bc122021-03-23 09:24:38 -0700700 @VisibleForTesting
joonhunshin21a86812023-12-10 08:21:25 +0000701 /* package */ CarrierConfigLoader(@NonNull Context context, @NonNull Looper looper,
702 @NonNull FeatureFlags featureFlags) {
mattgilbride5b6b7182023-04-03 18:56:44 +0000703 super(PermissionEnforcer.fromContext(context));
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800704 mContext = context;
Meng Wang33ad2bc2017-03-16 20:21:20 -0700705 mPlatformCarrierConfigPackage =
706 mContext.getString(R.string.platform_carrier_config_package);
Rambo Wang7e3bc122021-03-23 09:24:38 -0700707 mHandler = new ConfigHandler(looper);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800708
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000709 IntentFilter systemEventsFilter = new IntentFilter();
710 systemEventsFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
Rambo Wanga27fbe52022-04-12 19:09:07 +0000711 context.registerReceiver(mSystemBroadcastReceiver, systemEventsFilter);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800712
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000713 mNumPhones = TelephonyManager.from(context).getActiveModemCount();
714 mConfigFromDefaultApp = new PersistableBundle[mNumPhones];
715 mConfigFromCarrierApp = new PersistableBundle[mNumPhones];
716 mPersistentOverrideConfigs = new PersistableBundle[mNumPhones];
717 mOverrideConfigs = new PersistableBundle[mNumPhones];
Rambo Wang42026112021-04-07 20:57:28 +0000718 mNoSimConfig = new PersistableBundle();
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000719 mServiceConnection = new CarrierServiceConnection[mNumPhones];
720 mServiceBound = new boolean[mNumPhones];
721 mHasSentConfigChange = new boolean[mNumPhones];
722 mFromSystemUnlocked = new boolean[mNumPhones];
723 mServiceConnectionForNoSimConfig = new CarrierServiceConnection[mNumPhones];
724 mServiceBoundForNoSimConfig = new boolean[mNumPhones];
Rambo Wanga27fbe52022-04-12 19:09:07 +0000725 mCarrierServiceChangeCallbacks = new CarrierServiceChangeCallback[mNumPhones];
726 for (int phoneId = 0; phoneId < mNumPhones; phoneId++) {
727 mCarrierServiceChangeCallbacks[phoneId] = new CarrierServiceChangeCallback(phoneId);
728 TelephonyManager.from(context).registerCarrierPrivilegesCallback(phoneId,
729 new HandlerExecutor(mHandler), mCarrierServiceChangeCallbacks[phoneId]);
730 }
joonhunshin21a86812023-12-10 08:21:25 +0000731 mFeatureFlags = featureFlags;
732 mPackageManager = context.getPackageManager();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800733 logd("CarrierConfigLoader has started");
Jack Yu771c6ce2023-02-02 17:51:42 -0800734
735 PhoneConfigurationManager.registerForMultiSimConfigChange(
736 mHandler, EVENT_MULTI_SIM_CONFIG_CHANGED, null);
737
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700738 mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800739 }
740
741 /**
742 * Initialize the singleton CarrierConfigLoader instance.
743 *
744 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
745 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000746 @NonNull
joonhunshin21a86812023-12-10 08:21:25 +0000747 /* package */ static CarrierConfigLoader init(@NonNull Context context,
748 @NonNull FeatureFlags featureFlags) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800749 synchronized (CarrierConfigLoader.class) {
750 if (sInstance == null) {
joonhunshin21a86812023-12-10 08:21:25 +0000751 sInstance = new CarrierConfigLoader(context, Looper.myLooper(), featureFlags);
Brad Ebingerfa6575f2021-05-04 00:29:50 +0000752 // Make this service available through ServiceManager.
753 TelephonyFrameworkInitializer.getTelephonyServiceManager()
754 .getCarrierConfigServiceRegisterer().register(sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800755 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700756 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800757 }
758 return sInstance;
759 }
760 }
761
Rambo Wang7e3bc122021-03-23 09:24:38 -0700762 @VisibleForTesting
763 /* package */ void clearConfigForPhone(int phoneId, boolean fetchNoSimConfig) {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700764 /* Ignore clear configuration request if device is being shutdown. */
765 Phone phone = PhoneFactory.getPhone(phoneId);
766 if (phone != null) {
767 if (phone.isShuttingDown()) {
768 return;
769 }
770 }
771
Jack Yu771c6ce2023-02-02 17:51:42 -0800772 if (mConfigFromDefaultApp.length <= phoneId) {
773 Log.wtf(LOG_TAG, "Invalid phone id " + phoneId);
774 return;
775 }
776
Malcolm Chen5ea18532019-10-24 19:55:44 -0700777 mConfigFromDefaultApp[phoneId] = null;
778 mConfigFromCarrierApp[phoneId] = null;
779 mServiceConnection[phoneId] = null;
780 mHasSentConfigChange[phoneId] = false;
781
Jayachandran C645ec612020-01-10 10:30:25 -0800782 if (fetchNoSimConfig) {
783 // To fetch no SIM config
784 mHandler.sendMessage(
785 mHandler.obtainMessage(
786 EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG, phoneId, -1));
787 }
Malcolm Chen5ea18532019-10-24 19:55:44 -0700788 }
789
Jack Yue37dd262022-12-16 11:53:37 -0800790 private void updateSubscriptionDatabase(int phoneId) {
Jack Yu90813e62023-01-17 17:46:27 -0800791 logd("updateSubscriptionDatabase: phoneId=" + phoneId);
Jack Yue37dd262022-12-16 11:53:37 -0800792 String configPackageName;
Nathan Harold48ac0972019-03-13 22:33:01 -0700793 PersistableBundle configToSend;
794 int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
795 // Prefer the carrier privileged carrier app, but if there is not one, use the platform
796 // default carrier app.
797 if (mConfigFromCarrierApp[phoneId] != null) {
Jack Yue37dd262022-12-16 11:53:37 -0800798 configPackageName = getCarrierPackageForPhoneId(phoneId);
Nathan Harold48ac0972019-03-13 22:33:01 -0700799 configToSend = mConfigFromCarrierApp[phoneId];
800 } else {
Jack Yue37dd262022-12-16 11:53:37 -0800801 configPackageName = mPlatformCarrierConfigPackage;
Nathan Harold48ac0972019-03-13 22:33:01 -0700802 configToSend = mConfigFromDefaultApp[phoneId];
803 }
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700804
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800805 if (configToSend == null) {
Rambo Wang42026112021-04-07 20:57:28 +0000806 configToSend = new PersistableBundle();
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800807 }
808
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700809 // mOverrideConfigs is for testing. And it will override current configs.
810 PersistableBundle config = mOverrideConfigs[phoneId];
811 if (config != null) {
Torbjorn Eklund1050cb02018-11-16 14:05:38 +0100812 configToSend = new PersistableBundle(configToSend);
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700813 configToSend.putAll(config);
814 }
815
Jack Yu3beaf9d2023-04-14 09:17:27 -0700816 SubscriptionManagerService.getInstance().updateSubscriptionByCarrierConfig(
817 phoneId, configPackageName, configToSend,
818 () -> mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1)
819 .sendToTarget());
Nathan Harold48ac0972019-03-13 22:33:01 -0700820 }
821
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800822 private void broadcastConfigChangedIntent(int phoneId) {
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800823 broadcastConfigChangedIntent(phoneId, true);
824 }
825
826 private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) {
rambowangd63ba342022-10-02 11:21:08 -0500827 int subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
828 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
829 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
830
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800831 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800832 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
chen xuf9db49f2019-03-31 23:04:42 -0700833 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800834 if (addSubIdExtra) {
rambowangd63ba342022-10-02 11:21:08 -0500835 int simApplicationState = getSimApplicationStateForPhone(phoneId);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800836 // Include subId/carrier id extra only if SIM records are loaded
837 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
838 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
rambowangd63ba342022-10-02 11:21:08 -0500839 subId = SubscriptionManager.getSubscriptionId(phoneId);
840 carrierId = getCarrierIdForPhoneId(phoneId);
841 specificCarrierId = getSpecificCarrierIdForPhoneId(phoneId);
842 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID, specificCarrierId);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800843 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
rambowangd63ba342022-10-02 11:21:08 -0500844 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, carrierId);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800845 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800846 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800847 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Sarah Chin807d5c62020-03-30 17:21:09 -0700848 intent.putExtra(CarrierConfigManager.EXTRA_REBROADCAST_ON_UNLOCK,
849 mFromSystemUnlocked[phoneId]);
rambowangd63ba342022-10-02 11:21:08 -0500850
851 TelephonyRegistryManager trm = mContext.getSystemService(TelephonyRegistryManager.class);
852 // Unlike broadcast, we wouldn't notify registrants on carrier config change when device is
853 // unlocked. Only real carrier config change will send the notification to registrants.
854 if (trm != null && !mFromSystemUnlocked[phoneId]) {
855 trm.notifyCarrierConfigChanged(phoneId, subId, carrierId, specificCarrierId);
856 }
857
Meng Wang97a6a462020-01-23 16:22:16 -0800858 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
rambowangd63ba342022-10-02 11:21:08 -0500859
Jack Yu00ece8c2022-11-19 22:29:12 -0800860 if (SubscriptionManager.isValidSubscriptionId(subId)) {
861 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId + ", subId=" + subId);
Jack Yue9e95be2020-03-22 10:56:06 -0700862 } else {
863 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
864 }
Junda Liu03aad762017-07-21 13:32:17 -0700865 mHasSentConfigChange[phoneId] = true;
Sarah Chin807d5c62020-03-30 17:21:09 -0700866 mFromSystemUnlocked[phoneId] = false;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800867 }
868
rambowangd63ba342022-10-02 11:21:08 -0500869 private int getSimApplicationStateForPhone(int phoneId) {
870 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
871 int subId = SubscriptionManager.getSubscriptionId(phoneId);
872 if (SubscriptionManager.isValidSubscriptionId(subId)) {
873 TelephonyManager telMgr = TelephonyManager.from(mContext)
874 .createForSubscriptionId(subId);
875 simApplicationState = telMgr.getSimApplicationState();
876 }
877 return simApplicationState;
878 }
879
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800880 /** Binds to the default or carrier config app. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000881 private boolean bindToConfigPackage(@NonNull String pkgName, int phoneId, int eventId) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800882 logdWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700883 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700884 carrierService.setPackage(pkgName);
Jayachandran C645ec612020-01-10 10:30:25 -0800885 CarrierServiceConnection serviceConnection = new CarrierServiceConnection(
886 phoneId, pkgName, eventId);
887 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
888 mServiceConnectionForNoSimConfig[phoneId] = serviceConnection;
889 } else {
890 mServiceConnection[phoneId] = serviceConnection;
891 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800892 try {
Jayachandran C645ec612020-01-10 10:30:25 -0800893 if (mContext.bindService(carrierService, serviceConnection,
Jordan Liu178430d2020-02-10 14:32:15 -0800894 Context.BIND_AUTO_CREATE)) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700895 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
896 mServiceBoundForNoSimConfig[phoneId] = true;
897 } else {
898 mServiceBound[phoneId] = true;
899 }
Jordan Liu178430d2020-02-10 14:32:15 -0800900 return true;
901 } else {
902 return false;
903 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800904 } catch (SecurityException ex) {
905 return false;
906 }
907 }
908
Rambo Wang7e3bc122021-03-23 09:24:38 -0700909 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000910 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -0700911 /* package */ CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800912 String mcc = "";
913 String mnc = "";
914 String imsi = "";
915 String gid1 = "";
916 String gid2 = "";
917 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
918 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800919 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800920 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700921 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
922 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800923 mcc = simOperator.substring(0, 3);
924 mnc = simOperator.substring(3);
925 }
926 Phone phone = PhoneFactory.getPhone(phoneId);
927 if (phone != null) {
928 imsi = phone.getSubscriberId();
929 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700930 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800931 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800932 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800933 }
chen xua31f22b2019-03-06 15:28:50 -0800934 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800935 }
936
Rambo Wangf7c214a2022-03-17 12:36:22 -0700937 /** Returns the package name of a privileged carrier app, or null if there is none. */
Nathan Harold1122e3d2021-01-22 15:45:24 -0800938 @Nullable
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700939 private String getCarrierPackageForPhoneId(int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -0700940 final long token = Binder.clearCallingIdentity();
941 try {
Rambo Wangf7c214a2022-03-17 12:36:22 -0700942 return TelephonyManager.from(mContext)
943 .getCarrierServicePackageNameForLogicalSlot(phoneId);
Nazanin1adf4562021-03-29 15:35:30 -0700944 } finally {
945 Binder.restoreCallingIdentity(token);
946 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700947 }
948
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000949 @Nullable
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700950 private String getIccIdForPhoneId(int phoneId) {
951 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
952 return null;
953 }
954 Phone phone = PhoneFactory.getPhone(phoneId);
955 if (phone == null) {
956 return null;
957 }
958 return phone.getIccSerialNumber();
959 }
960
chen xu02581692018-11-11 19:03:44 -0800961 /**
chen xua31f22b2019-03-06 15:28:50 -0800962 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800963 */
chen xua31f22b2019-03-06 15:28:50 -0800964 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700965 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800966 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700967 }
968 Phone phone = PhoneFactory.getPhone(phoneId);
969 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800970 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700971 }
chen xua31f22b2019-03-06 15:28:50 -0800972 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800973 }
974
975 /**
976 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
977 */
978 private int getCarrierIdForPhoneId(int phoneId) {
979 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
980 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700981 }
chen xu02581692018-11-11 19:03:44 -0800982 Phone phone = PhoneFactory.getPhone(phoneId);
983 if (phone == null) {
984 return TelephonyManager.UNKNOWN_CARRIER_ID;
985 }
986 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700987 }
988
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700989 /**
990 * Writes a bundle to an XML file.
991 *
chen xu02581692018-11-11 19:03:44 -0800992 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -0800993 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -0800994 * should have a single copy of XML file named after carrier id. However, it's still possible
995 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
996 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
997 * carrier while iccid remains the same.
998 *
999 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
1000 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001001 *
1002 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001003 *
Jayachandran C645ec612020-01-10 10:30:25 -08001004 * @param packageName the name of the package from which we fetched this bundle.
1005 * @param extraString An extra string to be used in the XML file name.
1006 * @param phoneId the phone ID.
1007 * @param carrierId contains all carrier-identifying information.
1008 * @param config the bundle to be written. Null will be treated as an empty bundle.
1009 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001010 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001011 private void saveConfigToXml(@Nullable String packageName, @NonNull String extraString,
1012 int phoneId, @Nullable CarrierIdentifier carrierId, @NonNull PersistableBundle config,
1013 boolean isNoSimConfig) {
Jayachandran C645ec612020-01-10 10:30:25 -08001014 if (packageName == null) {
Jack Yu9be55d32023-08-10 13:47:46 -07001015 loge("Cannot save config with null packageName. phoneId=" + phoneId);
Yuchen Dongc15afed2018-04-26 17:05:22 +08001016 return;
1017 }
1018
Jayachandran C645ec612020-01-10 10:30:25 -08001019 String fileName;
1020 if (isNoSimConfig) {
1021 fileName = getFilenameForNoSimConfig(packageName);
1022 } else {
Jack Yuf5badd92022-12-08 00:50:53 -08001023 if (TelephonyManager.getSimStateForSlotIndex(phoneId)
Jayachandran C645ec612020-01-10 10:30:25 -08001024 != TelephonyManager.SIM_STATE_LOADED) {
Jack Yu9be55d32023-08-10 13:47:46 -07001025 loge("Skip saving config because SIM records are not loaded. phoneId=" + phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001026 return;
1027 }
1028
1029 final String iccid = getIccIdForPhoneId(phoneId);
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001030 final int cid = carrierId != null ? carrierId.getSpecificCarrierId()
1031 : TelephonyManager.UNKNOWN_CARRIER_ID;
Jayachandran C645ec612020-01-10 10:30:25 -08001032 if (iccid == null) {
Jack Yu9be55d32023-08-10 13:47:46 -07001033 loge("Cannot save config with null iccid. phoneId=" + phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001034 return;
1035 }
1036 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001037 }
Jayachandran C645ec612020-01-10 10:30:25 -08001038
Junda Liu02596502016-11-15 13:46:43 -08001039 // b/32668103 Only save to file if config isn't empty.
1040 // In case of failure, not caching an empty bundle will
1041 // try loading config again on next power on or sim loaded.
1042 // Downside is for genuinely empty bundle, will bind and load
1043 // on every power on.
1044 if (config == null || config.isEmpty()) {
1045 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001046 }
1047
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001048 final String version = getPackageVersion(packageName);
1049 if (version == null) {
Jack Yu9be55d32023-08-10 13:47:46 -07001050 loge("Failed to get package version for: " + packageName + ", phoneId=" + phoneId);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001051 return;
1052 }
1053
Jack Yu9be55d32023-08-10 13:47:46 -07001054 logdWithLocalLog("Save carrier config to cache. phoneId=" + phoneId
1055 + ", xml=" + getFilePathForLogging(fileName) + ", version=" + version);
chen xudb04c292019-03-26 17:01:15 -07001056
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001057 FileOutputStream outFile = null;
1058 try {
Jayachandran C645ec612020-01-10 10:30:25 -08001059 outFile = new FileOutputStream(new File(mContext.getFilesDir(), fileName));
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001060 config.putString(KEY_VERSION, version);
1061 config.writeToStream(outFile);
1062 outFile.flush();
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001063 outFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001064 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001065 loge(e.toString());
1066 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001067 }
1068
Rambo Wang7e3bc122021-03-23 09:24:38 -07001069 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001070 /* package */ void saveConfigToXml(@Nullable String packageName, @NonNull String extraString,
1071 int phoneId, @NonNull CarrierIdentifier carrierId, @NonNull PersistableBundle config) {
Jayachandran C645ec612020-01-10 10:30:25 -08001072 saveConfigToXml(packageName, extraString, phoneId, carrierId, config, false);
1073 }
1074
Rambo Wang7e3bc122021-03-23 09:24:38 -07001075 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001076 /* package */ void saveNoSimConfigToXml(@Nullable String packageName,
1077 @NonNull PersistableBundle config) {
Jayachandran C645ec612020-01-10 10:30:25 -08001078 saveConfigToXml(packageName, "", -1, null, config, true);
1079 }
1080
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001081 /**
1082 * Reads a bundle from an XML file.
1083 *
1084 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +08001085 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001086 *
1087 * In case of errors, or if the saved config is from a different package version than the
1088 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001089 *
Jayachandran C645ec612020-01-10 10:30:25 -08001090 * @param packageName the name of the package from which we fetched this bundle.
1091 * @param extraString An extra string to be used in the XML file name.
1092 * @param phoneId the phone ID.
1093 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001094 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001095 * version does not match, or reading config fails.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001096 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001097 @Nullable
1098 private PersistableBundle restoreConfigFromXml(@Nullable String packageName,
1099 @NonNull String extraString, int phoneId, boolean isNoSimConfig) {
Jayachandran C645ec612020-01-10 10:30:25 -08001100 if (packageName == null) {
1101 loge("Cannot restore config with null packageName");
1102 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001103 final String version = getPackageVersion(packageName);
1104 if (version == null) {
1105 loge("Failed to get package version for: " + packageName);
1106 return null;
1107 }
Yuchen Dongc15afed2018-04-26 17:05:22 +08001108
Jayachandran C645ec612020-01-10 10:30:25 -08001109 String fileName;
arunvoddu664c36a2021-10-11 20:09:28 +00001110 String iccid = null;
Jayachandran C645ec612020-01-10 10:30:25 -08001111 if (isNoSimConfig) {
1112 fileName = getFilenameForNoSimConfig(packageName);
1113 } else {
Jack Yuf5badd92022-12-08 00:50:53 -08001114 if (TelephonyManager.getSimStateForSlotIndex(phoneId)
Jayachandran C645ec612020-01-10 10:30:25 -08001115 != TelephonyManager.SIM_STATE_LOADED) {
Jack Yu9be55d32023-08-10 13:47:46 -07001116 loge("Skip restore config because SIM records are not loaded. phoneId=" + phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001117 return null;
1118 }
1119
arunvoddu664c36a2021-10-11 20:09:28 +00001120 iccid = getIccIdForPhoneId(phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001121 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1122 if (iccid == null) {
Jack Yu9be55d32023-08-10 13:47:46 -07001123 loge("Cannot restore config with null iccid. phoneId=" + phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001124 return null;
1125 }
1126 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001127 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001128
1129 PersistableBundle restoredBundle = null;
Mateus Azisa13d2502023-05-25 08:44:38 -07001130 File file = new File(mContext.getFilesDir(), fileName);
Jack Yu9be55d32023-08-10 13:47:46 -07001131 String filePath = file.getPath();
1132 String savedVersion = null;
Mateus Azisa13d2502023-05-25 08:44:38 -07001133 try (FileInputStream inFile = new FileInputStream(file)) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001134
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001135 restoredBundle = PersistableBundle.readFromStream(inFile);
Jack Yu9be55d32023-08-10 13:47:46 -07001136 savedVersion = restoredBundle.getString(KEY_VERSION);
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001137 restoredBundle.remove(KEY_VERSION);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001138
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001139 if (!version.equals(savedVersion)) {
Jack Yu9be55d32023-08-10 13:47:46 -07001140 loge("Saved version mismatch: " + version + " vs " + savedVersion
1141 + ", phoneId=" + phoneId);
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001142 restoredBundle = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001143 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001144 } catch (FileNotFoundException e) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001145 // Missing file is normal occurrence that might occur with a new sim or when restoring
1146 // an override file during boot and should not be treated as an error.
Mateus Azisa13d2502023-05-25 08:44:38 -07001147 if (isNoSimConfig) {
Jack Yu9be55d32023-08-10 13:47:46 -07001148 logd("File not found: " + file.getPath() + ", phoneId=" + phoneId);
Mateus Azisa13d2502023-05-25 08:44:38 -07001149 } else {
Jack Yu9be55d32023-08-10 13:47:46 -07001150 logd("File not found : " + getFilePathForLogging(filePath, iccid)
1151 + ", phoneId=" + phoneId);
arunvoddu664c36a2021-10-11 20:09:28 +00001152 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001153 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001154 loge(e.toString());
1155 }
1156
Jack Yu9be55d32023-08-10 13:47:46 -07001157 if (restoredBundle != null) {
1158 logdWithLocalLog("Restored carrier config from cache. phoneId=" + phoneId + ", xml="
1159 + getFilePathForLogging(fileName) + ", version=" + savedVersion
1160 + ", modified time=" + getFileTime(filePath));
1161 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001162 return restoredBundle;
1163 }
1164
arunvoddu664c36a2021-10-11 20:09:28 +00001165 /**
1166 * This method will mask most part of iccid in the filepath for logging on userbuild
1167 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001168 @NonNull
1169 private String getFilePathForLogging(@Nullable String filePath, @Nullable String iccid) {
arunvoddu664c36a2021-10-11 20:09:28 +00001170 // If loggable then return with actual file path
Jack Yu9be55d32023-08-10 13:47:46 -07001171 if (TelephonyUtils.IS_DEBUGGABLE) {
arunvoddu664c36a2021-10-11 20:09:28 +00001172 return filePath;
1173 }
1174 String path = filePath;
1175 int length = (iccid != null) ? iccid.length() : 0;
1176 if (length > 5 && filePath != null) {
1177 path = filePath.replace(iccid.substring(5), "***************");
1178 }
1179 return path;
1180 }
1181
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001182 @Nullable
1183 private PersistableBundle restoreConfigFromXml(@Nullable String packageName,
1184 @NonNull String extraString, int phoneId) {
Jayachandran C645ec612020-01-10 10:30:25 -08001185 return restoreConfigFromXml(packageName, extraString, phoneId, false);
1186 }
1187
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001188 @Nullable
1189 private PersistableBundle restoreNoSimConfigFromXml(@Nullable String packageName) {
Jayachandran C645ec612020-01-10 10:30:25 -08001190 return restoreConfigFromXml(packageName, "", -1, true);
1191 }
1192
Junda Liu8a8a53a2015-05-15 16:19:57 -07001193 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001194 * Clears cached carrier config.
1195 * This deletes all saved XML files associated with the given package name. If packageName is
1196 * null, then it deletes all saved XML files.
1197 *
1198 * @param packageName the name of a carrier package, or null if all cached config should be
1199 * cleared.
1200 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -07001201 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001202 private boolean clearCachedConfigForPackage(@Nullable final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001203 File dir = mContext.getFilesDir();
1204 File[] packageFiles = dir.listFiles(new FilenameFilter() {
1205 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001206 if (packageName != null) {
1207 return filename.startsWith("carrierconfig-" + packageName + "-");
1208 } else {
1209 return filename.startsWith("carrierconfig-");
1210 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001211 }
1212 });
Junda Liu8a8a53a2015-05-15 16:19:57 -07001213 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001214 for (File f : packageFiles) {
Jack Yu9be55d32023-08-10 13:47:46 -07001215 logdWithLocalLog("Deleting " + getFilePathForLogging(f.getName()));
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001216 f.delete();
1217 }
Junda Liu8a8a53a2015-05-15 16:19:57 -07001218 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001219 }
1220
arunvoddub7c6e8e2022-09-05 15:48:06 +00001221 private String getFilePathForLogging(String filePath) {
1222 if (!TextUtils.isEmpty(filePath)) {
1223 String[] fileTokens = filePath.split("-");
1224 if (fileTokens != null && fileTokens.length > 2) {
1225 String iccid = fileTokens[fileTokens.length -2];
1226 return getFilePathForLogging(filePath, iccid);
1227 }
1228 return filePath;
1229 }
1230 return filePath;
1231 }
1232
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001233 /** Builds a canonical file name for a config file. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001234 @NonNull
Jayachandran C645ec612020-01-10 10:30:25 -08001235 private static String getFilenameForConfig(
1236 @NonNull String packageName, @NonNull String extraString,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001237 @NonNull String iccid, int cid) {
chen xu02581692018-11-11 19:03:44 -08001238 // the same carrier should have a single copy of XML file named after carrier id.
1239 // However, it's still possible that platform doesn't recognize the current sim carrier,
1240 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
1241 // cases SIM OTA resolves to different carrier while iccid remains the same.
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001242 return "carrierconfig-" + packageName + extraString + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001243 }
1244
Jayachandran C645ec612020-01-10 10:30:25 -08001245 /** Builds a canonical file name for no SIM config file. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001246 @NonNull
Jayachandran C645ec612020-01-10 10:30:25 -08001247 private String getFilenameForNoSimConfig(@NonNull String packageName) {
1248 return "carrierconfig-" + packageName + "-" + "nosim" + ".xml";
1249 }
1250
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001251 /** Return the current version code of a package, or null if the name is not found. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001252 @Nullable
1253 private String getPackageVersion(@NonNull String packageName) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001254 try {
1255 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -08001256 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001257 } catch (PackageManager.NameNotFoundException e) {
1258 return null;
1259 }
1260 }
1261
Jonathan Basseri65273c82017-07-25 15:08:42 -07001262 /**
1263 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001264 *
1265 * This reads config bundles for the given phoneId. That means getting the latest bundle from
1266 * the default app and a privileged carrier app, if present. This will not bind to an app if we
1267 * have a saved config file to use instead.
1268 */
1269 private void updateConfigForPhoneId(int phoneId) {
Jonathan Basseri65273c82017-07-25 15:08:42 -07001270 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001271 }
1272
Malcolm Chen5ea18532019-10-24 19:55:44 -07001273 private void onMultiSimConfigChanged() {
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001274 int oldNumPhones = mNumPhones;
1275 mNumPhones = TelephonyManager.from(mContext).getActiveModemCount();
1276 if (mNumPhones == oldNumPhones) {
1277 return;
1278 }
1279 logdWithLocalLog("mNumPhones change from " + oldNumPhones + " to " + mNumPhones);
1280
Rambo Wanga27fbe52022-04-12 19:09:07 +00001281 // If DS -> SS switch, release the resources BEFORE truncating the arrays to avoid leaking
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001282 for (int phoneId = mNumPhones; phoneId < oldNumPhones; phoneId++) {
1283 if (mServiceConnection[phoneId] != null) {
1284 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
1285 }
1286 if (mServiceConnectionForNoSimConfig[phoneId] != null) {
1287 unbindIfBoundForNoSimConfig(mContext, mServiceConnectionForNoSimConfig[phoneId],
1288 phoneId);
1289 }
1290 }
1291
Rambo Wanga27fbe52022-04-12 19:09:07 +00001292 // The phone to slot mapping may change, unregister here and re-register callbacks later
1293 for (int phoneId = 0; phoneId < oldNumPhones; phoneId++) {
1294 if (mCarrierServiceChangeCallbacks[phoneId] != null) {
1295 TelephonyManager.from(mContext).unregisterCarrierPrivilegesCallback(
1296 mCarrierServiceChangeCallbacks[phoneId]);
1297 }
1298 }
1299
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001300 // Copy the original arrays, truncate or padding with zeros (if necessary) to new length
1301 mConfigFromDefaultApp = Arrays.copyOf(mConfigFromDefaultApp, mNumPhones);
1302 mConfigFromCarrierApp = Arrays.copyOf(mConfigFromCarrierApp, mNumPhones);
1303 mPersistentOverrideConfigs = Arrays.copyOf(mPersistentOverrideConfigs, mNumPhones);
1304 mOverrideConfigs = Arrays.copyOf(mOverrideConfigs, mNumPhones);
1305 mServiceConnection = Arrays.copyOf(mServiceConnection, mNumPhones);
1306 mServiceConnectionForNoSimConfig =
1307 Arrays.copyOf(mServiceConnectionForNoSimConfig, mNumPhones);
1308 mServiceBound = Arrays.copyOf(mServiceBound, mNumPhones);
1309 mServiceBoundForNoSimConfig = Arrays.copyOf(mServiceBoundForNoSimConfig, mNumPhones);
1310 mHasSentConfigChange = Arrays.copyOf(mHasSentConfigChange, mNumPhones);
1311 mFromSystemUnlocked = Arrays.copyOf(mFromSystemUnlocked, mNumPhones);
Rambo Wanga27fbe52022-04-12 19:09:07 +00001312 mCarrierServiceChangeCallbacks = Arrays.copyOf(mCarrierServiceChangeCallbacks, mNumPhones);
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001313
Rambo Wanga27fbe52022-04-12 19:09:07 +00001314 // Load the config for all the phones and re-register callback AFTER padding the arrays.
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001315 for (int phoneId = 0; phoneId < mNumPhones; phoneId++) {
1316 updateConfigForPhoneId(phoneId);
Rambo Wanga27fbe52022-04-12 19:09:07 +00001317 mCarrierServiceChangeCallbacks[phoneId] = new CarrierServiceChangeCallback(phoneId);
1318 TelephonyManager.from(mContext).registerCarrierPrivilegesCallback(phoneId,
1319 new HandlerExecutor(mHandler), mCarrierServiceChangeCallbacks[phoneId]);
Malcolm Chen5ea18532019-10-24 19:55:44 -07001320 }
1321 }
1322
Hall Liu506724b2018-10-22 18:16:14 -07001323 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001324 @NonNull
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001325 public PersistableBundle getConfigForSubId(int subscriptionId, @NonNull String callingPackage) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001326 return getConfigForSubIdWithFeature(subscriptionId, callingPackage, null);
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08001327 }
1328
1329 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001330 @NonNull
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001331 public PersistableBundle getConfigForSubIdWithFeature(int subscriptionId,
1332 @NonNull String callingPackage, @Nullable String callingFeatureId) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001333 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subscriptionId,
1334 callingPackage, callingFeatureId, "getCarrierConfig")) {
Rambo Wang42026112021-04-07 20:57:28 +00001335 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -07001336 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -07001337
joonhunshin21a86812023-12-10 08:21:25 +00001338 enforceTelephonyFeatureWithException(callingPackage, "getConfigForSubIdWithFeature");
1339
Rambo Wangd2b004b2021-03-30 10:10:23 -07001340 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001341 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001342 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001343 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001344 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001345 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001346 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001347 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001348 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001349 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001350 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001351 config = mPersistentOverrideConfigs[phoneId];
1352 if (config != null) {
1353 retConfig.putAll(config);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001354 }
Hall Liu506724b2018-10-22 18:16:14 -07001355 config = mOverrideConfigs[phoneId];
1356 if (config != null) {
1357 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -07001358 }
Nathan Harold1122e3d2021-01-22 15:45:24 -08001359 // Ignore the theoretical case of the default app not being present since that won't
1360 // work in CarrierConfigLoader today.
1361 final boolean allConfigsApplied =
1362 (mConfigFromCarrierApp[phoneId] != null
1363 || getCarrierPackageForPhoneId(phoneId) == null)
1364 && mConfigFromDefaultApp[phoneId] != null;
1365 retConfig.putBoolean(
1366 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, allConfigsApplied);
Jayachandran C645ec612020-01-10 10:30:25 -08001367 } else {
1368 if (mNoSimConfig != null) {
1369 retConfig.putAll(mNoSimConfig);
1370 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001371 }
1372 return retConfig;
1373 }
1374
1375 @Override
rambowang14757c22022-10-03 11:54:56 -05001376 @NonNull
1377 public PersistableBundle getConfigSubsetForSubIdWithFeature(int subscriptionId,
1378 @NonNull String callingPackage, @Nullable String callingFeatureId,
1379 @NonNull String[] keys) {
1380 Objects.requireNonNull(callingPackage, "Calling package must be non-null");
1381 Objects.requireNonNull(keys, "Config keys must be non-null");
1382 enforceCallerIsSystemOrRequestingPackage(callingPackage);
1383
joonhunshin21a86812023-12-10 08:21:25 +00001384 enforceTelephonyFeatureWithException(callingPackage,
1385 "getConfigSubsetForSubIdWithFeature");
1386
rambowang14757c22022-10-03 11:54:56 -05001387 // Permission check is performed inside and an empty bundle will return on failure.
1388 // No SecurityException thrown here since most clients expect to retrieve the overridden
1389 // value if present or use default one if not
1390 PersistableBundle allConfigs = getConfigForSubIdWithFeature(subscriptionId, callingPackage,
1391 callingFeatureId);
1392 if (allConfigs.isEmpty()) {
1393 return allConfigs;
1394 }
1395 for (String key : keys) {
1396 Objects.requireNonNull(key, "Config key must be non-null");
rambowang14757c22022-10-03 11:54:56 -05001397 }
1398
1399 PersistableBundle configSubset = new PersistableBundle(
1400 keys.length + CONFIG_SUBSET_METADATA_KEYS.length);
1401 for (String carrierConfigKey : keys) {
1402 Object value = allConfigs.get(carrierConfigKey);
rambowangb49e90f2022-12-13 18:29:06 -06001403 if (value == null) {
1404 // Filter out keys without values.
1405 // In history, many AOSP or OEMs/carriers private configs didn't provide default
1406 // values. We have to continue supporting them for now. See b/261776046 for details.
1407 continue;
1408 }
rambowang14757c22022-10-03 11:54:56 -05001409 // Config value itself could be PersistableBundle which requires different API to put
1410 if (value instanceof PersistableBundle) {
1411 configSubset.putPersistableBundle(carrierConfigKey, (PersistableBundle) value);
1412 } else {
1413 configSubset.putObject(carrierConfigKey, value);
1414 }
1415 }
1416
1417 // Configs in CONFIG_SUBSET_ALWAYS_INCLUDED_KEYS should always be included
1418 for (String generalKey : CONFIG_SUBSET_METADATA_KEYS) {
1419 configSubset.putObject(generalKey, allConfigs.get(generalKey));
1420 }
1421
1422 return configSubset;
1423 }
1424
mattgilbride5b6b7182023-04-03 18:56:44 +00001425 @android.annotation.EnforcePermission(android.Manifest.permission.MODIFY_PHONE_STATE)
rambowang14757c22022-10-03 11:54:56 -05001426 @Override
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001427 public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides,
1428 boolean persistent) {
mattgilbride5b6b7182023-04-03 18:56:44 +00001429 overrideConfig_enforcePermission();
Hall Liu506724b2018-10-22 18:16:14 -07001430 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1431 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001432 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
Rambo Wangd2b004b2021-03-30 10:10:23 -07001433 throw new IllegalArgumentException(
1434 "Invalid phoneId " + phoneId + " for subId " + subscriptionId);
Hall Liu506724b2018-10-22 18:16:14 -07001435 }
joonhunshin21a86812023-12-10 08:21:25 +00001436
1437 enforceTelephonyFeatureWithException(getCurrentPackageName(), "overrideConfig");
1438
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001439 // Post to run on handler thread on which all states should be confined.
1440 mHandler.post(() -> {
1441 overrideConfig(mOverrideConfigs, phoneId, overrides);
Hall Liu506724b2018-10-22 18:16:14 -07001442
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001443 if (persistent) {
1444 overrideConfig(mPersistentOverrideConfigs, phoneId, overrides);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001445
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001446 if (overrides != null) {
1447 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
1448 saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION,
1449 phoneId,
1450 carrierId, mPersistentOverrideConfigs[phoneId]);
1451 } else {
1452 final String iccid = getIccIdForPhoneId(phoneId);
1453 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1454 String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage,
1455 OVERRIDE_PACKAGE_ADDITION, iccid, cid);
1456 File fileToDelete = new File(mContext.getFilesDir(), fileName);
1457 fileToDelete.delete();
1458 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001459 }
Jack Yu9be55d32023-08-10 13:47:46 -07001460 logdWithLocalLog("overrideConfig: subId=" + subscriptionId + ", persistent="
1461 + persistent + ", overrides=" + overrides);
Jack Yue37dd262022-12-16 11:53:37 -08001462 updateSubscriptionDatabase(phoneId);
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001463 });
Hall Liu506724b2018-10-22 18:16:14 -07001464 }
1465
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001466 private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId,
1467 @Nullable PersistableBundle overrides) {
1468 if (overrides == null) {
Rambo Wang42026112021-04-07 20:57:28 +00001469 currentOverrides[phoneId] = new PersistableBundle();
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001470 } else if (currentOverrides[phoneId] == null) {
1471 currentOverrides[phoneId] = overrides;
1472 } else {
1473 currentOverrides[phoneId].putAll(overrides);
1474 }
1475 }
1476
Hall Liu506724b2018-10-22 18:16:14 -07001477 @Override
Rambo Wangd2b004b2021-03-30 10:10:23 -07001478 public void notifyConfigChangedForSubId(int subscriptionId) {
Jordan Liud7d57fe2019-04-16 12:29:43 -07001479 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001480 // system privileged app with MODIFY_PHONE_STATE permission.
Rambo Wangd2b004b2021-03-30 10:10:23 -07001481 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext,
1482 subscriptionId, "Require carrier privileges or MODIFY_PHONE_STATE permission.");
1483
1484 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1485 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1486 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
1487 throw new IllegalArgumentException(
1488 "Invalid phoneId " + phoneId + " for subId " + subscriptionId);
1489 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001490
joonhunshin21a86812023-12-10 08:21:25 +00001491 enforceTelephonyFeatureWithException(getCurrentPackageName(),
1492 "notifyConfigChangedForSubId");
1493
Jack Yu9be55d32023-08-10 13:47:46 -07001494 logdWithLocalLog("Notified carrier config changed. phoneId=" + phoneId
1495 + ", subId=" + subscriptionId);
1496
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001497 // This method should block until deleting has completed, so that an error which prevents us
1498 // from clearing the cache is passed back to the carrier app. With the files successfully
1499 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001500 String callingPackageName = mContext.getPackageManager().getNameForUid(
1501 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001502 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001503 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001504 }
1505
mattgilbride5b6b7182023-04-03 18:56:44 +00001506 @android.annotation.EnforcePermission(android.Manifest.permission.MODIFY_PHONE_STATE)
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001507 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001508 public void updateConfigForPhoneId(int phoneId, @NonNull String simState) {
mattgilbride5b6b7182023-04-03 18:56:44 +00001509 updateConfigForPhoneId_enforcePermission();
Jack Yu9be55d32023-08-10 13:47:46 -07001510 logdWithLocalLog("Update config for phoneId=" + phoneId + " simState=" + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001511 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001512 throw new IllegalArgumentException("Invalid phoneId: " + phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001513 }
joonhunshin21a86812023-12-10 08:21:25 +00001514
1515 enforceTelephonyFeatureWithException(getCurrentPackageName(), "updateConfigForPhoneId");
1516
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001517 // requires Java 7 for switch on string.
1518 switch (simState) {
1519 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1520 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001521 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001522 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Malcolm Chendb66de12019-12-09 18:57:07 -08001523 case IccCardConstants.INTENT_VALUE_ICC_NOT_READY:
Junda Liu9f2d2712015-05-15 14:22:45 -07001524 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001525 break;
1526 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1527 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001528 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001529 break;
1530 }
1531 }
1532
mattgilbride5b6b7182023-04-03 18:56:44 +00001533 @android.annotation.EnforcePermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
Junda Liu43d723a2015-05-12 17:23:45 -07001534 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001535 @NonNull
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001536 public String getDefaultCarrierServicePackageName() {
mattgilbride5b6b7182023-04-03 18:56:44 +00001537 getDefaultCarrierServicePackageName_enforcePermission();
joonhunshin21a86812023-12-10 08:21:25 +00001538
1539 enforceTelephonyFeatureWithException(getCurrentPackageName(),
1540 "getDefaultCarrierServicePackageName");
1541
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001542 return mPlatformCarrierConfigPackage;
1543 }
1544
Rambo Wang7e3bc122021-03-23 09:24:38 -07001545 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001546 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -07001547 /* package */ Handler getHandler() {
1548 return mHandler;
1549 }
1550
1551 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001552 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001553 /* package */ PersistableBundle getConfigFromDefaultApp(int phoneId) {
1554 return mConfigFromDefaultApp[phoneId];
1555 }
1556
1557 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001558 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001559 /* package */ PersistableBundle getConfigFromCarrierApp(int phoneId) {
1560 return mConfigFromCarrierApp[phoneId];
1561 }
1562
1563 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001564 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -07001565 /* package */ PersistableBundle getNoSimConfig() {
1566 return mNoSimConfig;
1567 }
1568
1569 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001570 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001571 /* package */ PersistableBundle getOverrideConfig(int phoneId) {
1572 return mOverrideConfigs[phoneId];
1573 }
1574
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001575 // TODO(b/185129900): always call unbindService after bind, no matter if it succeeded
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001576 private void unbindIfBound(@NonNull Context context, @NonNull CarrierServiceConnection conn,
Rambo Wangb0f3e2f2021-10-28 12:40:12 -07001577 int phoneId) {
1578 if (mServiceBound[phoneId]) {
1579 mServiceBound[phoneId] = false;
1580 context.unbindService(conn);
1581 }
1582 }
1583
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001584 private void unbindIfBoundForNoSimConfig(@NonNull Context context,
1585 @NonNull CarrierServiceConnection conn, int phoneId) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -07001586 if (mServiceBoundForNoSimConfig[phoneId]) {
1587 mServiceBoundForNoSimConfig[phoneId] = false;
Jayachandran C645ec612020-01-10 10:30:25 -08001588 context.unbindService(conn);
1589 }
1590 }
1591
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001592 /**
Rambo Wang610fdf62021-03-08 21:37:11 -08001593 * Returns a boxed Integer object for phoneId, services as message token to distinguish messages
1594 * with same code when calling {@link Handler#removeMessages(int, Object)}.
1595 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001596 @NonNull
Rambo Wang610fdf62021-03-08 21:37:11 -08001597 private Integer getMessageToken(int phoneId) {
1598 if (phoneId < -128 || phoneId > 127) {
1599 throw new IllegalArgumentException("phoneId should be in range [-128, 127], inclusive");
1600 }
1601 // Integer#valueOf guarantees the integers within [-128, 127] are cached and thus memory
1602 // comparison (==) returns true for the same integer.
1603 return Integer.valueOf(phoneId);
1604 }
1605
1606 /**
Jack Yu9be55d32023-08-10 13:47:46 -07001607 * Get the file time in readable format.
1608 *
1609 * @param filePath The full file path.
1610 *
1611 * @return The time in string format.
1612 */
1613 @Nullable
1614 private String getFileTime(@NonNull String filePath) {
1615 String formattedModifiedTime = null;
1616 try {
1617 // Convert the modified time to a readable format
1618 formattedModifiedTime = TIME_FORMAT.format(Files.readAttributes(Paths.get(filePath),
1619 BasicFileAttributes.class).lastModifiedTime().toMillis());
1620 } catch (Exception e) {
1621 e.printStackTrace();
1622 }
1623
1624 return formattedModifiedTime;
1625 }
1626
1627 /**
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001628 * If {@code args} contains {@link #DUMP_ARG_REQUESTING_PACKAGE} and a following package name,
1629 * we'll also call {@link IBinder#dump} on the default carrier service (if bound) and the
1630 * specified carrier service (if bound). Typically, this is done for connectivity bug reports
1631 * where we don't call {@code dumpsys activity service all-non-platform} because that contains
1632 * too much info, but we still want to let carrier apps include their diagnostics.
1633 */
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001634 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001635 public void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter pw, @NonNull String[] args) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001636 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
Junda Liu43d723a2015-05-12 17:23:45 -07001637 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1638 != PackageManager.PERMISSION_GRANTED) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001639 indentPW.println("Permission Denial: can't dump carrierconfig from from pid="
Junda Liu43d723a2015-05-12 17:23:45 -07001640 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1641 return;
1642 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001643 String requestingPackage = null;
1644 int requestingPackageIndex = ArrayUtils.indexOf(args, DUMP_ARG_REQUESTING_PACKAGE);
1645 if (requestingPackageIndex >= 0 && requestingPackageIndex < args.length - 1
1646 && !TextUtils.isEmpty(args[requestingPackageIndex + 1])) {
1647 requestingPackage = args[requestingPackageIndex + 1];
1648 // Throws a SecurityException if the caller is impersonating another app in an effort to
1649 // dump extra info (which may contain PII the caller doesn't have a right to).
1650 enforceCallerIsSystemOrRequestingPackage(requestingPackage);
1651 }
1652
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001653 indentPW.println("CarrierConfigLoader: " + this);
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001654 for (int i = 0; i < mNumPhones; i++) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001655 indentPW.println("Phone Id = " + i);
shuoq26a3a4c2016-12-16 11:06:48 -08001656 // display default values in CarrierConfigManager
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001657 printConfig(CarrierConfigManager.getDefaultConfig(), indentPW,
shuoq26a3a4c2016-12-16 11:06:48 -08001658 "Default Values from CarrierConfigManager");
shuoq26a3a4c2016-12-16 11:06:48 -08001659 // display ConfigFromDefaultApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001660 printConfig(mConfigFromDefaultApp[i], indentPW, "mConfigFromDefaultApp");
shuoq26a3a4c2016-12-16 11:06:48 -08001661 // display ConfigFromCarrierApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001662 printConfig(mConfigFromCarrierApp[i], indentPW, "mConfigFromCarrierApp");
1663 printConfig(mPersistentOverrideConfigs[i], indentPW, "mPersistentOverrideConfigs");
1664 printConfig(mOverrideConfigs[i], indentPW, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001665 }
chen xudb04c292019-03-26 17:01:15 -07001666
Jayachandran C645ec612020-01-10 10:30:25 -08001667 printConfig(mNoSimConfig, indentPW, "mNoSimConfig");
Jack Yu9be55d32023-08-10 13:47:46 -07001668 indentPW.println("mNumPhones=" + mNumPhones);
1669 indentPW.println("mPlatformCarrierConfigPackage=" + mPlatformCarrierConfigPackage);
1670 indentPW.println("mServiceConnection=[" + Stream.of(mServiceConnection)
1671 .map(c -> c != null ? c.pkgName : null)
1672 .collect(Collectors.joining(", ")) + "]");
1673 indentPW.println("mServiceBoundForNoSimConfig="
1674 + Arrays.toString(mServiceBoundForNoSimConfig));
1675 indentPW.println("mHasSentConfigChange=" + Arrays.toString(mHasSentConfigChange));
1676 indentPW.println("mFromSystemUnlocked=" + Arrays.toString(mFromSystemUnlocked));
1677 indentPW.println();
1678 indentPW.println("CarrierConfigLoader local log=");
1679 indentPW.increaseIndent();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001680 mCarrierConfigLoadingLog.dump(fd, indentPW, args);
Jack Yu9be55d32023-08-10 13:47:46 -07001681 indentPW.decreaseIndent();
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001682
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001683 if (requestingPackage != null) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001684 logd("Including default and requesting package " + requestingPackage
1685 + " carrier services in dump");
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001686 indentPW.println("");
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001687 indentPW.println("Connected services");
1688 dumpCarrierServiceIfBound(fd, indentPW, "Default config package",
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001689 mPlatformCarrierConfigPackage, false /* considerCarrierPrivileges */);
1690 dumpCarrierServiceIfBound(fd, indentPW, "Requesting package", requestingPackage,
1691 true /* considerCarrierPrivileges */);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001692 }
Jack Yu9be55d32023-08-10 13:47:46 -07001693
1694 indentPW.println();
1695 indentPW.println("Cached config files:");
1696 indentPW.increaseIndent();
1697 for (File f : mContext.getFilesDir().listFiles((FilenameFilter) (d, filename)
1698 -> filename.startsWith("carrierconfig-"))) {
1699 indentPW.println(getFilePathForLogging(f.getName()) + ", modified time="
1700 + getFileTime(f.getAbsolutePath()));
1701 }
1702 indentPW.decreaseIndent();
shuoq26a3a4c2016-12-16 11:06:48 -08001703 }
1704
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001705 private void printConfig(@NonNull PersistableBundle configApp,
1706 @NonNull IndentingPrintWriter indentPW, @NonNull String name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001707 indentPW.increaseIndent();
shuoq26a3a4c2016-12-16 11:06:48 -08001708 if (configApp == null) {
shuoq26a3a4c2016-12-16 11:06:48 -08001709 indentPW.println(name + " : null ");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001710 indentPW.decreaseIndent();
1711 indentPW.println("");
shuoq26a3a4c2016-12-16 11:06:48 -08001712 return;
1713 }
shuoq26a3a4c2016-12-16 11:06:48 -08001714 indentPW.println(name + " : ");
1715 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1716 Collections.sort(sortedKeys);
1717 indentPW.increaseIndent();
1718 indentPW.increaseIndent();
1719 for (String key : sortedKeys) {
1720 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1721 indentPW.println(key + " = " +
1722 Arrays.toString((Object[]) configApp.get(key)));
1723 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1724 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1725 } else {
1726 indentPW.println(key + " = " + configApp.get(key));
1727 }
Junda Liu43d723a2015-05-12 17:23:45 -07001728 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001729 indentPW.decreaseIndent();
1730 indentPW.decreaseIndent();
1731 indentPW.decreaseIndent();
1732 indentPW.println("");
Junda Liu43d723a2015-05-12 17:23:45 -07001733 }
1734
Hunter Knepshielde601f432020-02-19 10:16:04 -08001735 /**
1736 * Passes without problem when one of these conditions is true:
1737 * - The caller is a privileged UID (e.g. for dumpstate.cpp generating a bug report, where the
1738 * system knows the true caller plumbed in through the {@link android.os.BugreportManager} API).
1739 * - The caller's UID matches the supplied package.
1740 *
1741 * @throws SecurityException if none of the above conditions are met.
1742 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001743 private void enforceCallerIsSystemOrRequestingPackage(@NonNull String requestingPackage)
Hunter Knepshielde601f432020-02-19 10:16:04 -08001744 throws SecurityException {
1745 final int callingUid = Binder.getCallingUid();
1746 if (callingUid == Process.ROOT_UID || callingUid == Process.SYSTEM_UID
1747 || callingUid == Process.SHELL_UID || callingUid == Process.PHONE_UID) {
1748 // Bug reports (dumpstate.cpp) run as SHELL, and let some other privileged UIDs through
1749 // as well.
1750 return;
1751 }
1752 // An app is trying to dump extra detail, block it if they aren't who they claim to be.
1753 AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class);
1754 if (appOps == null) {
1755 throw new SecurityException("No AppOps");
1756 }
1757 // Will throw a SecurityException if the UID and package don't match.
1758 appOps.checkPackage(callingUid, requestingPackage);
1759 }
1760
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001761 /**
1762 * Searches for one or more appropriate {@link CarrierService} instances to dump based on the
1763 * current connections.
1764 *
1765 * @param targetPkgName the target package name to dump carrier services for
1766 * @param considerCarrierPrivileges if true, allow a carrier service to be dumped if it shares
1767 * carrier privileges with {@code targetPkgName};
1768 * otherwise, only dump a carrier service if it is {@code
1769 * targetPkgName}
1770 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001771 private void dumpCarrierServiceIfBound(@NonNull FileDescriptor fd,
1772 @NonNull IndentingPrintWriter indentPW, @NonNull String prefix,
1773 @NonNull String targetPkgName, boolean considerCarrierPrivileges) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001774 // Null package is possible if it's early in the boot process, there was a recent crash, we
1775 // loaded the config from XML most recently, or a SIM slot is empty. Carrier apps with
1776 // long-lived bindings should typically get dumped here regardless. Even if an app is being
1777 // used for multiple phoneIds, we assume that it's smart enough to handle that on its own,
1778 // and that in most cases we'd just be dumping duplicate information and bloating a report.
1779 indentPW.increaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001780 indentPW.println(prefix + " : " + targetPkgName);
1781 Set<String> dumpedPkgNames = new ArraySet<>(mServiceConnection.length);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001782 for (CarrierServiceConnection connection : mServiceConnection) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001783 if (connection == null || !SubscriptionManager.isValidPhoneId(connection.phoneId)
1784 || TextUtils.isEmpty(connection.pkgName)) {
1785 continue;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001786 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001787 final String servicePkgName = connection.pkgName;
1788 // Note: we intentionally ignore system components here because we should NOT match the
1789 // shell caller that's typically used for bug reports via non-BugreportManager triggers.
1790 final boolean exactPackageMatch = TextUtils.equals(targetPkgName, servicePkgName);
1791 final boolean carrierPrivilegesMatch =
1792 considerCarrierPrivileges && hasCarrierPrivileges(targetPkgName,
1793 connection.phoneId);
1794 if (!exactPackageMatch && !carrierPrivilegesMatch) continue;
1795 // Make sure this service is actually alive before trying to dump it. We don't pay
1796 // attention to mServiceBound[connection.phoneId] because typically carrier apps will
1797 // request long-lived bindings, and even if we unbind the app, it may still be alive due
1798 // to CarrierServiceBindHelper. Pull it out as a reference so even if it gets set to
1799 // null within the ServiceConnection during unbinding we can avoid an NPE.
1800 final IBinder service = connection.service;
1801 if (service == null || !service.isBinderAlive() || !service.pingBinder()) continue;
1802 // We've got a live service. Last check is just to make sure we don't dump a package
1803 // multiple times.
1804 if (!dumpedPkgNames.add(servicePkgName)) continue;
1805 if (!exactPackageMatch) {
1806 logd(targetPkgName + " has carrier privileges on phoneId " + connection.phoneId
1807 + ", service provided by " + servicePkgName);
1808 indentPW.increaseIndent();
1809 indentPW.println("Proxy : " + servicePkgName);
1810 indentPW.decreaseIndent();
1811 }
1812 // Flush before we let the app output anything to ensure correct ordering of output.
1813 // Internally, Binder#dump calls flush on its printer after finishing so we don't
1814 // need to do anything after.
1815 indentPW.flush();
1816 try {
1817 logd("Dumping " + servicePkgName);
1818 // We don't need to give the carrier service any args.
1819 connection.service.dump(fd, null /* args */);
1820 logd("Done with " + servicePkgName);
1821 } catch (RemoteException e) {
1822 logd("RemoteException from " + servicePkgName, e);
1823 indentPW.increaseIndent();
1824 indentPW.println("RemoteException");
1825 indentPW.increaseIndent();
1826 e.printStackTrace(indentPW);
1827 indentPW.decreaseIndent();
1828 indentPW.decreaseIndent();
1829 // We won't retry this package again because now it's in dumpedPkgNames.
1830 }
1831 indentPW.println("");
Jordan Liu46ed5db2020-01-28 08:55:20 -08001832 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001833 if (dumpedPkgNames.isEmpty()) {
1834 indentPW.increaseIndent();
1835 indentPW.println("Not bound");
1836 indentPW.decreaseIndent();
1837 indentPW.println("");
1838 }
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001839 indentPW.decreaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001840 }
1841
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001842 private boolean hasCarrierPrivileges(@NonNull String pkgName, int phoneId) {
Jack Yu00ece8c2022-11-19 22:29:12 -08001843 int subId = SubscriptionManager.getSubscriptionId(phoneId);
1844 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001845 return false;
1846 }
Jack Yu00ece8c2022-11-19 22:29:12 -08001847 return TelephonyManager.from(mContext).createForSubscriptionId(subId)
1848 .checkCarrierPrivilegesForPackage(pkgName)
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001849 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001850 }
1851
joonhunshin21a86812023-12-10 08:21:25 +00001852 /**
1853 * Get the current calling package name.
1854 * @return the current calling package name
1855 */
1856 @Nullable
1857 private String getCurrentPackageName() {
1858 if (mPackageManager == null) return null;
1859 String[] callingUids = mPackageManager.getPackagesForUid(Binder.getCallingUid());
1860 return (callingUids == null) ? null : callingUids[0];
1861 }
1862
1863 /**
1864 * Make sure the device has required telephony feature
1865 *
1866 * @throws UnsupportedOperationException if the device does not have required telephony feature
1867 */
1868 private void enforceTelephonyFeatureWithException(@Nullable String callingPackage,
1869 @NonNull String methodName) {
1870 if (callingPackage == null || mPackageManager == null) {
1871 return;
1872 }
1873
1874 if (!mFeatureFlags.enforceTelephonyFeatureMappingForPublicApis()
1875 || !CompatChanges.isChangeEnabled(ENABLE_FEATURE_MAPPING, callingPackage,
1876 Binder.getCallingUserHandle())) {
1877 return;
1878 }
1879
1880 if (!mPackageManager.hasSystemFeature(FEATURE_TELEPHONY_SUBSCRIPTION)) {
1881 throw new UnsupportedOperationException(
1882 methodName + " is unsupported without " + FEATURE_TELEPHONY_SUBSCRIPTION);
1883 }
1884 }
1885
Zach Johnson36d7aab2015-05-22 15:43:00 -07001886 private class CarrierServiceConnection implements ServiceConnection {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001887 final int phoneId;
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001888 @NonNull final String pkgName;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001889 final int eventId;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001890 IBinder service;
1891
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001892 CarrierServiceConnection(int phoneId, @NonNull String pkgName, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001893 this.phoneId = phoneId;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001894 this.pkgName = pkgName;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001895 this.eventId = eventId;
1896 }
1897
1898 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001899 public void onServiceConnected(@NonNull ComponentName name, @NonNull IBinder service) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001900 logd("Connected to config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001901 this.service = service;
1902 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1903 }
1904
1905 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001906 public void onServiceDisconnected(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001907 logd("Disconnected from config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001908 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001909 }
1910
1911 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001912 public void onBindingDied(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001913 logd("Binding died from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001914 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001915 }
1916
1917 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001918 public void onNullBinding(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001919 logd("Null binding from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001920 this.service = null;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001921 }
1922 }
1923
1924 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1925 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001926 public void onReceive(@NonNull Context context, @NonNull Intent intent) {
Rambo Wanga27fbe52022-04-12 19:09:07 +00001927 switch (intent.getAction()) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001928 case Intent.ACTION_BOOT_COMPLETED:
1929 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1930 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001931 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001932 }
1933 }
1934
Rambo Wanga27fbe52022-04-12 19:09:07 +00001935 private class CarrierServiceChangeCallback implements
1936 TelephonyManager.CarrierPrivilegesCallback {
1937 final int mPhoneId;
1938 // CarrierPrivilegesCallback will be triggered upon registration. Filter the first callback
1939 // here since we really care of the *change* of carrier service instead of the content
1940 private boolean mHasSentServiceChangeCallback;
1941
1942 CarrierServiceChangeCallback(int phoneId) {
1943 this.mPhoneId = phoneId;
1944 this.mHasSentServiceChangeCallback = false;
1945 }
1946
1947 @Override
1948 public void onCarrierPrivilegesChanged(
1949 @androidx.annotation.NonNull Set<String> privilegedPackageNames,
1950 @androidx.annotation.NonNull Set<Integer> privilegedUids) {
1951 // Ignored, not interested here
1952 }
1953
1954 @Override
1955 public void onCarrierServiceChanged(
1956 @androidx.annotation.Nullable String carrierServicePackageName,
1957 int carrierServiceUid) {
1958 // Ignore the first callback which is triggered upon registration
1959 if (!mHasSentServiceChangeCallback) {
1960 mHasSentServiceChangeCallback = true;
1961 return;
1962 }
1963 mHandler.sendMessage(
1964 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, mPhoneId, -1,
1965 carrierServicePackageName));
1966 }
1967 }
1968
Rambo Wangab13e3e2021-04-08 15:01:06 -07001969 // Get readable string for the message code supported in this class.
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001970 @NonNull
Rambo Wangab13e3e2021-04-08 15:01:06 -07001971 private static String eventToString(int code) {
1972 switch (code) {
1973 case EVENT_CLEAR_CONFIG:
1974 return "EVENT_CLEAR_CONFIG";
1975 case EVENT_CONNECTED_TO_DEFAULT:
1976 return "EVENT_CONNECTED_TO_DEFAULT";
1977 case EVENT_CONNECTED_TO_CARRIER:
1978 return "EVENT_CONNECTED_TO_CARRIER";
1979 case EVENT_FETCH_DEFAULT_DONE:
1980 return "EVENT_FETCH_DEFAULT_DONE";
1981 case EVENT_FETCH_CARRIER_DONE:
1982 return "EVENT_FETCH_CARRIER_DONE";
1983 case EVENT_DO_FETCH_DEFAULT:
1984 return "EVENT_DO_FETCH_DEFAULT";
1985 case EVENT_DO_FETCH_CARRIER:
1986 return "EVENT_DO_FETCH_CARRIER";
1987 case EVENT_PACKAGE_CHANGED:
1988 return "EVENT_PACKAGE_CHANGED";
1989 case EVENT_BIND_DEFAULT_TIMEOUT:
1990 return "EVENT_BIND_DEFAULT_TIMEOUT";
1991 case EVENT_BIND_CARRIER_TIMEOUT:
1992 return "EVENT_BIND_CARRIER_TIMEOUT";
1993 case EVENT_CHECK_SYSTEM_UPDATE:
1994 return "EVENT_CHECK_SYSTEM_UPDATE";
1995 case EVENT_SYSTEM_UNLOCKED:
1996 return "EVENT_SYSTEM_UNLOCKED";
1997 case EVENT_FETCH_DEFAULT_TIMEOUT:
1998 return "EVENT_FETCH_DEFAULT_TIMEOUT";
1999 case EVENT_FETCH_CARRIER_TIMEOUT:
2000 return "EVENT_FETCH_CARRIER_TIMEOUT";
2001 case EVENT_SUBSCRIPTION_INFO_UPDATED:
2002 return "EVENT_SUBSCRIPTION_INFO_UPDATED";
2003 case EVENT_MULTI_SIM_CONFIG_CHANGED:
2004 return "EVENT_MULTI_SIM_CONFIG_CHANGED";
2005 case EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG:
2006 return "EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG";
2007 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE:
2008 return "EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE";
2009 case EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG:
2010 return "EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG";
2011 case EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
2012 return "EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT";
2013 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
2014 return "EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT";
2015 default:
2016 return "UNKNOWN(" + code + ")";
2017 }
2018 }
2019
Rambo Wangfe0d7c12022-04-15 03:00:32 +00002020 private void logd(@NonNull String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07002021 Log.d(LOG_TAG, msg);
2022 }
2023
Rambo Wangfe0d7c12022-04-15 03:00:32 +00002024 private void logd(@NonNull String msg, Throwable tr) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08002025 Log.d(LOG_TAG, msg, tr);
2026 }
2027
Rambo Wangfe0d7c12022-04-15 03:00:32 +00002028 private void logdWithLocalLog(@NonNull String msg) {
chen xudb04c292019-03-26 17:01:15 -07002029 Log.d(LOG_TAG, msg);
2030 mCarrierConfigLoadingLog.log(msg);
2031 }
2032
Rambo Wangfe0d7c12022-04-15 03:00:32 +00002033 private void loge(@NonNull String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07002034 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07002035 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08002036 }
2037}