blob: 9b71919f386031c6156216811891f9d2552ae2c8 [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;
Rambo Wang0c386012024-02-22 22:38:17 +000055import android.telephony.AnomalyReporter;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080056import android.telephony.CarrierConfigManager;
57import android.telephony.SubscriptionManager;
Peter Wangc035ce42020-01-08 21:00:22 -080058import android.telephony.TelephonyFrameworkInitializer;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080059import android.telephony.TelephonyManager;
rambowangd63ba342022-10-02 11:21:08 -050060import android.telephony.TelephonyRegistryManager;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -080061import android.text.TextUtils;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080062import android.util.ArraySet;
chen xudb04c292019-03-26 17:01:15 -070063import android.util.LocalLog;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080064import android.util.Log;
65
Rambo Wang7e3bc122021-03-23 09:24:38 -070066import com.android.internal.annotations.VisibleForTesting;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080067import com.android.internal.telephony.ICarrierConfigLoader;
68import com.android.internal.telephony.IccCardConstants;
69import com.android.internal.telephony.Phone;
Jack Yu771c6ce2023-02-02 17:51:42 -080070import com.android.internal.telephony.PhoneConfigurationManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080071import com.android.internal.telephony.PhoneFactory;
Jeff Davidsona8e4e242018-03-15 17:16:18 -070072import com.android.internal.telephony.TelephonyPermissions;
joonhunshin21a86812023-12-10 08:21:25 +000073import com.android.internal.telephony.flags.FeatureFlags;
Jack Yue37dd262022-12-16 11:53:37 -080074import com.android.internal.telephony.subscription.SubscriptionManagerService;
Meng Wanga320b942019-11-25 11:21:26 -080075import com.android.internal.telephony.util.ArrayUtils;
Jack Yu9be55d32023-08-10 13:47:46 -070076import com.android.internal.telephony.util.TelephonyUtils;
shuoq26a3a4c2016-12-16 11:06:48 -080077import com.android.internal.util.IndentingPrintWriter;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080078
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070079import java.io.File;
Junda Liu43d723a2015-05-12 17:23:45 -070080import java.io.FileDescriptor;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070081import java.io.FileInputStream;
82import java.io.FileNotFoundException;
83import java.io.FileOutputStream;
Jonathan Basseri1f743c92015-05-15 00:19:46 -070084import java.io.FilenameFilter;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070085import java.io.IOException;
Junda Liu43d723a2015-05-12 17:23:45 -070086import java.io.PrintWriter;
Jack Yu9be55d32023-08-10 13:47:46 -070087import java.nio.file.Files;
88import java.nio.file.Paths;
89import java.nio.file.attribute.BasicFileAttributes;
90import java.text.SimpleDateFormat;
shuoq26a3a4c2016-12-16 11:06:48 -080091import java.util.ArrayList;
92import java.util.Arrays;
93import java.util.Collections;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080094import java.util.List;
Jack Yu9be55d32023-08-10 13:47:46 -070095import java.util.Locale;
rambowang14757c22022-10-03 11:54:56 -050096import java.util.Objects;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080097import java.util.Set;
Rambo Wang0c386012024-02-22 22:38:17 +000098import java.util.UUID;
Jack Yu9be55d32023-08-10 13:47:46 -070099import java.util.stream.Collectors;
100import java.util.stream.Stream;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800101
102/**
103 * CarrierConfigLoader binds to privileged carrier apps to fetch carrier config overlays.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800104 */
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800105public class CarrierConfigLoader extends ICarrierConfigLoader.Stub {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700106 private static final String LOG_TAG = "CarrierConfigLoader";
Meng Wang33ad2bc2017-03-16 20:21:20 -0700107
Jack Yu9be55d32023-08-10 13:47:46 -0700108 private static final SimpleDateFormat TIME_FORMAT =
109 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
110
Meng Wang33ad2bc2017-03-16 20:21:20 -0700111 // Package name for platform carrier config app, bundled with system image.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000112 @NonNull private final String mPlatformCarrierConfigPackage;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800113
114 /** The singleton instance. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000115 @Nullable private static CarrierConfigLoader sInstance;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800116 // The context for phone app, passed from PhoneGlobals.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000117 @NonNull private Context mContext;
118
119 // All the states below (array indexed by phoneId) are non-null. But the member of the array
120 // is nullable, when e.g. the config for the phone is not loaded yet
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800121 // Carrier configs from default app, indexed by phoneID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000122 @NonNull private PersistableBundle[] mConfigFromDefaultApp;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800123 // Carrier configs from privileged carrier config app, indexed by phoneID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000124 @NonNull private PersistableBundle[] mConfigFromCarrierApp;
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100125 // Persistent 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[] mPersistentOverrideConfigs;
Hall Liu506724b2018-10-22 18:16:14 -0700127 // Carrier configs that are provided via the override test API, indexed by phone ID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000128 @NonNull private PersistableBundle[] mOverrideConfigs;
Jayachandran C645ec612020-01-10 10:30:25 -0800129 // Carrier configs to override code default when there is no SIM inserted
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000130 @NonNull private PersistableBundle mNoSimConfig;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800131 // Service connection for binding to config app.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000132 @NonNull private CarrierServiceConnection[] mServiceConnection;
Jayachandran C645ec612020-01-10 10:30:25 -0800133 // Service connection for binding to carrier config app for no SIM config.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000134 @NonNull private CarrierServiceConnection[] mServiceConnectionForNoSimConfig;
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700135 // Whether we are bound to a service for each phone
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000136 @NonNull private boolean[] mServiceBound;
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700137 // Whether we are bound to a service for no SIM config
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000138 @NonNull private boolean[] mServiceBoundForNoSimConfig;
Sarah Chin807d5c62020-03-30 17:21:09 -0700139 // Whether we have sent config change broadcast for each phone id.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000140 @NonNull private boolean[] mHasSentConfigChange;
Sarah Chin807d5c62020-03-30 17:21:09 -0700141 // Whether the broadcast was sent from EVENT_SYSTEM_UNLOCKED, to track rebroadcasts
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000142 @NonNull private boolean[] mFromSystemUnlocked;
Rambo Wanga27fbe52022-04-12 19:09:07 +0000143 // CarrierService change monitoring
144 @NonNull private CarrierServiceChangeCallback[] mCarrierServiceChangeCallbacks;
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000145
Jack Yu771c6ce2023-02-02 17:51:42 -0800146 // Broadcast receiver for system events
Rambo Wanga27fbe52022-04-12 19:09:07 +0000147 @NonNull
148 private final BroadcastReceiver mSystemBroadcastReceiver = new ConfigLoaderBroadcastReceiver();
Jack Yu9be55d32023-08-10 13:47:46 -0700149 @NonNull private final LocalLog mCarrierConfigLoadingLog = new LocalLog(256);
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000150 // Number of phone instances (active modem count)
151 private int mNumPhones;
chen xudb04c292019-03-26 17:01:15 -0700152
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800153
154 // Message codes; see mHandler below.
Jack Yu3beaf9d2023-04-14 09:17:27 -0700155 // Request from UiccController when SIM becomes absent or error.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800156 private static final int EVENT_CLEAR_CONFIG = 0;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800157 // Has connected to default app.
158 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
159 // Has connected to carrier app.
160 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700161 // Config has been loaded from default app (or cache).
162 private static final int EVENT_FETCH_DEFAULT_DONE = 5;
163 // Config has been loaded from carrier app (or cache).
164 private static final int EVENT_FETCH_CARRIER_DONE = 6;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700165 // Attempt to fetch from default app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700166 private static final int EVENT_DO_FETCH_DEFAULT = 7;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700167 // Attempt to fetch from carrier app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700168 private static final int EVENT_DO_FETCH_CARRIER = 8;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700169 // A package has been installed, uninstalled, or updated.
170 private static final int EVENT_PACKAGE_CHANGED = 9;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700171 // Bind timed out for the default app.
172 private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
173 // Bind timed out for a carrier app.
174 private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700175 // Check if the system fingerprint has changed.
176 private static final int EVENT_CHECK_SYSTEM_UPDATE = 12;
Nanxi Chen3d670502016-03-17 16:32:09 -0700177 // Rerun carrier config binding after system is unlocked.
178 private static final int EVENT_SYSTEM_UNLOCKED = 13;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700179 // Fetching config timed out from the default app.
180 private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14;
181 // Fetching config timed out from a carrier app.
182 private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15;
Jack Yu3beaf9d2023-04-14 09:17:27 -0700183 // SubscriptionManagerService has finished updating the sub for the carrier config.
Nathan Harold48ac0972019-03-13 22:33:01 -0700184 private static final int EVENT_SUBSCRIPTION_INFO_UPDATED = 16;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700185 // Multi-SIM config changed.
186 private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 17;
Jayachandran C645ec612020-01-10 10:30:25 -0800187 // Attempt to fetch from default app or read from XML for no SIM case.
188 private static final int EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG = 18;
189 // No SIM config has been loaded from default app (or cache).
190 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE = 19;
191 // Has connected to default app for no SIM config.
192 private static final int EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG = 20;
193 // Bind timed out for the default app when trying to fetch no SIM config.
194 private static final int EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 21;
195 // Fetching config timed out from the default app for no SIM config.
196 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 22;
Rambo Wangab13e3e2021-04-08 15:01:06 -0700197 // NOTE: any new EVENT_* values must be added to method eventToString().
Jonathan Basseri930701e2015-06-11 20:56:43 -0700198
Junda Liu6cb45002016-03-22 17:18:20 -0700199 private static final int BIND_TIMEOUT_MILLIS = 30000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800200
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800201 // Keys used for saving and restoring config bundle from file.
202 private static final String KEY_VERSION = "__carrier_config_package_version__";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800203
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100204 private static final String OVERRIDE_PACKAGE_ADDITION = "-override";
205
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700206 // SharedPreferences key for last known build fingerprint.
207 private static final String KEY_FINGERPRINT = "build_fingerprint";
208
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800209 // Argument for #dump that indicates we should also call the default and specified carrier
210 // service's #dump method. In multi-SIM devices, it's possible that carrier A is on SIM 1 and
211 // carrier B is on SIM 2, in which case we should not dump carrier B's service when carrier A
212 // requested the dump.
213 private static final String DUMP_ARG_REQUESTING_PACKAGE = "--requesting-package";
214
rambowang14757c22022-10-03 11:54:56 -0500215 // Configs that should always be included when clients calls getConfig[ForSubId] with specified
216 // keys (even configs are not explicitly specified). Those configs have special purpose for the
217 // carrier config APIs to work correctly.
218 private static final String[] CONFIG_SUBSET_METADATA_KEYS = new String[] {
219 CarrierConfigManager.KEY_CARRIER_CONFIG_VERSION_STRING,
220 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL
221 };
222
Rambo Wang0c386012024-02-22 22:38:17 +0000223 // UUID to report anomaly when config changed reported with subId that map to invalid phone
224 private static final String UUID_NOTIFY_CONFIG_CHANGED_WITH_INVALID_PHONE =
225 "d81cef11-c2f1-4d76-955d-7f50e8590c48";
226
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800227 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700228 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700229 // For each phoneId, the event sequence should be:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700230 // fetch default, connected to default, fetch default (async), fetch default done,
231 // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700232 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700233 // If there is a saved config file for either the default app or the carrier app, we skip
234 // binding to the app and go straight from fetch to loaded.
235 //
236 // At any time, at most one connection is active. If events are not in this order, previous
237 // connection will be unbound, so only latest event takes effect.
238 //
239 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
240 // 1. loading from carrier app (even if read from a file)
241 // 2. loading from default app if there is no carrier app (even if read from a file)
242 // 3. clearing config (e.g. due to sim removal)
243 // 4. encountering bind or IPC error
Jonathan Basseri65273c82017-07-25 15:08:42 -0700244 private class ConfigHandler extends Handler {
Rambo Wang7e3bc122021-03-23 09:24:38 -0700245 ConfigHandler(@NonNull Looper looper) {
246 super(looper);
247 }
248
Jonathan Basseri65273c82017-07-25 15:08:42 -0700249 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000250 public void handleMessage(@NonNull Message msg) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700251 final int phoneId = msg.arg1;
Jack Yu9be55d32023-08-10 13:47:46 -0700252 logd(eventToString(msg.what) + " phoneId: " + phoneId);
Malcolm Chen5ea18532019-10-24 19:55:44 -0700253 if (!SubscriptionManager.isValidPhoneId(phoneId)
254 && msg.what != EVENT_MULTI_SIM_CONFIG_CHANGED) {
255 return;
256 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800257 switch (msg.what) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800258 case EVENT_CLEAR_CONFIG: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700259 clearConfigForPhone(phoneId, true);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800260 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700261 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700262
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800263 case EVENT_SYSTEM_UNLOCKED: {
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000264 for (int i = 0; i < mNumPhones; ++i) {
Sarah Chin807d5c62020-03-30 17:21:09 -0700265 // When the user unlocks the device, send the broadcast again (with a
266 // rebroadcast extra) if we have sent it before unlock. This will avoid
267 // trying to load the carrier config when the SIM is still loading when the
268 // unlock happens.
Junda Liu03aad762017-07-21 13:32:17 -0700269 if (mHasSentConfigChange[i]) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800270 logdWithLocalLog("System unlocked");
Sarah Chin807d5c62020-03-30 17:21:09 -0700271 mFromSystemUnlocked[i] = true;
Junda Liu03aad762017-07-21 13:32:17 -0700272 updateConfigForPhoneId(i);
273 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700274 }
275 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700276 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700277
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800278 case EVENT_PACKAGE_CHANGED: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700279 final String carrierPackageName = (String) msg.obj;
Rambo Wanga27fbe52022-04-12 19:09:07 +0000280 // Always clear up the cache and re-load config from scratch since the carrier
281 // service change is reliable and specific to the phoneId now.
282 clearCachedConfigForPackage(carrierPackageName);
283 logdWithLocalLog("Package changed: " + carrierPackageName
284 + ", phone=" + phoneId);
285 updateConfigForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700286 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700287 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700288
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800289 case EVENT_DO_FETCH_DEFAULT: {
Rambo Wang9bc7d362021-03-09 09:10:58 -0800290 // Clear in-memory cache for carrier app config, so when carrier app gets
291 // uninstalled, no stale config is left.
292 if (mConfigFromCarrierApp[phoneId] != null
293 && getCarrierPackageForPhoneId(phoneId) == null) {
294 mConfigFromCarrierApp[phoneId] = null;
295 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100296 // Restore persistent override values.
297 PersistableBundle config = restoreConfigFromXml(
298 mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId);
299 if (config != null) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100300 mPersistentOverrideConfigs[phoneId] = config;
301 }
302
303 config = restoreConfigFromXml(mPlatformCarrierConfigPackage, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700304 if (config != null) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700305 mConfigFromDefaultApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700306 Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700307 newMsg.getData().putBoolean("loaded_from_xml", true);
308 mHandler.sendMessage(newMsg);
309 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700310 // No cached config, so fetch it from the default app.
311 if (bindToConfigPackage(
312 mPlatformCarrierConfigPackage,
313 phoneId,
314 EVENT_CONNECTED_TO_DEFAULT)) {
315 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800316 obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1 /*arg2*/,
317 getMessageToken(phoneId)),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700318 BIND_TIMEOUT_MILLIS);
319 } else {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800320 // Put a stub bundle in place so that the rest of the logic continues
321 // smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000322 mConfigFromDefaultApp[phoneId] = new PersistableBundle();
Jonathan Basseri65273c82017-07-25 15:08:42 -0700323 // Send broadcast if bind fails.
Jack Yue37dd262022-12-16 11:53:37 -0800324 updateSubscriptionDatabase(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700325 // TODO: We *must* call unbindService even if bindService returns false.
326 // (And possibly if SecurityException was thrown.)
chen xudb04c292019-03-26 17:01:15 -0700327 loge("binding to default app: "
328 + mPlatformCarrierConfigPackage + " fails");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700329 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800330 }
331 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700332 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800333
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800334 case EVENT_CONNECTED_TO_DEFAULT: {
Rambo Wang610fdf62021-03-08 21:37:11 -0800335 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT, getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700336 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800337 // If new service connection has been created, unbind.
338 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700339 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800340 break;
341 }
chen xu02581692018-11-11 19:03:44 -0800342 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700343 // ResultReceiver callback will execute in this Handler's thread.
344 final ResultReceiver resultReceiver =
345 new ResultReceiver(this) {
346 @Override
347 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700348 unbindIfBound(mContext, conn, phoneId);
Rambo Wanga9d27a52021-04-09 16:17:33 -0700349 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT,
350 getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700351 // If new service connection has been created, this is stale.
352 if (mServiceConnection[phoneId] != conn) {
353 loge("Received response for stale request.");
354 return;
355 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700356 if (resultCode == RESULT_ERROR || resultData == null) {
357 // On error, abort config fetching.
358 loge("Failed to get carrier config");
Jack Yue37dd262022-12-16 11:53:37 -0800359 updateSubscriptionDatabase(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700360 return;
361 }
362 PersistableBundle config =
363 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100364 saveConfigToXml(mPlatformCarrierConfigPackage, "", phoneId,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800365 carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700366 mConfigFromDefaultApp[phoneId] = config;
367 sendMessage(
368 obtainMessage(
369 EVENT_FETCH_DEFAULT_DONE, phoneId, -1));
370 }
371 };
372 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800373 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700374 ICarrierService carrierService =
375 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800376 carrierService.getCarrierConfig(phoneId, carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800377 logdWithLocalLog("Fetch config for default app: "
chen xudb04c292019-03-26 17:01:15 -0700378 + mPlatformCarrierConfigPackage
Jack Yu9be55d32023-08-10 13:47:46 -0700379 + ", carrierId=" + carrierId.getSpecificCarrierId());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700380 } catch (RemoteException e) {
chen xudb04c292019-03-26 17:01:15 -0700381 loge("Failed to get carrier config from default app: " +
Jack Yu9be55d32023-08-10 13:47:46 -0700382 mPlatformCarrierConfigPackage + " err: " + e);
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700383 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700384 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800385 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700386 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800387 obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1 /*arg2*/,
388 getMessageToken(phoneId)),
Jonathan Basseri65273c82017-07-25 15:08:42 -0700389 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800390 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700391 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800392
Jonathan Basseri930701e2015-06-11 20:56:43 -0700393 case EVENT_BIND_DEFAULT_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800394 case EVENT_FETCH_DEFAULT_TIMEOUT: {
395 loge("Bind/fetch time out from " + mPlatformCarrierConfigPackage);
Rambo Wang610fdf62021-03-08 21:37:11 -0800396 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT, getMessageToken(phoneId));
chen xuf60b3162019-05-01 21:31:05 -0700397 // If we attempted to bind to the app, but the service connection is null due to
398 // the race condition that clear config event happens before bind/fetch complete
399 // then config was cleared while we were waiting and we should not continue.
400 if (mServiceConnection[phoneId] != null) {
401 // If a ResponseReceiver callback is in the queue when this happens, we will
402 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700403 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700404 broadcastConfigChangedIntent(phoneId);
405 }
Nathan Harold1122e3d2021-01-22 15:45:24 -0800406 // Put a stub bundle in place so that the rest of the logic continues smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000407 mConfigFromDefaultApp[phoneId] = new PersistableBundle();
Jack Yue37dd262022-12-16 11:53:37 -0800408 updateSubscriptionDatabase(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700409 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700410 }
Jonathan Basseri930701e2015-06-11 20:56:43 -0700411
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800412 case EVENT_FETCH_DEFAULT_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700413 // If we attempted to bind to the app, but the service connection is null, then
414 // config was cleared while we were waiting and we should not continue.
415 if (!msg.getData().getBoolean("loaded_from_xml", false)
416 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800417 break;
418 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700419 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700420 if (carrierPackageName != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800421 logd("Found carrier config app: " + carrierPackageName);
Jordan Liu38a614c2019-03-20 15:01:17 -0700422 sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId, -1));
Jonathan Basserib919e932015-05-27 16:53:08 +0000423 } else {
Jack Yue37dd262022-12-16 11:53:37 -0800424 updateSubscriptionDatabase(phoneId);
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700425 }
426 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700427 }
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700428
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800429 case EVENT_DO_FETCH_CARRIER: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700430 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700431 final PersistableBundle config =
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100432 restoreConfigFromXml(carrierPackageName, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700433 if (config != null) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700434 mConfigFromCarrierApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700435 Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700436 newMsg.getData().putBoolean("loaded_from_xml", true);
437 sendMessage(newMsg);
438 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700439 // No cached config, so fetch it from a carrier app.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800440 if (carrierPackageName != null && bindToConfigPackage(carrierPackageName,
441 phoneId, EVENT_CONNECTED_TO_CARRIER)) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700442 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800443 obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1 /*arg2*/,
444 getMessageToken(phoneId)),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700445 BIND_TIMEOUT_MILLIS);
446 } else {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800447 // Put a stub bundle in place so that the rest of the logic continues
448 // smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000449 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
Jonathan Basseri65273c82017-07-25 15:08:42 -0700450 // Send broadcast if bind fails.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700451 broadcastConfigChangedIntent(phoneId);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800452 loge("Bind to carrier app: " + carrierPackageName + " fails");
Jack Yue37dd262022-12-16 11:53:37 -0800453 updateSubscriptionDatabase(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700454 }
455 }
456 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700457 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700458
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800459 case EVENT_CONNECTED_TO_CARRIER: {
Rambo Wang610fdf62021-03-08 21:37:11 -0800460 removeMessages(EVENT_BIND_CARRIER_TIMEOUT, getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700461 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800462 // If new service connection has been created, unbind.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700463 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700464 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800465 break;
466 }
chen xu02581692018-11-11 19:03:44 -0800467 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700468 // ResultReceiver callback will execute in this Handler's thread.
469 final ResultReceiver resultReceiver =
470 new ResultReceiver(this) {
471 @Override
472 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700473 unbindIfBound(mContext, conn, phoneId);
Rambo Wanga9d27a52021-04-09 16:17:33 -0700474 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT,
475 getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700476 // If new service connection has been created, this is stale.
477 if (mServiceConnection[phoneId] != conn) {
478 loge("Received response for stale request.");
479 return;
480 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700481 if (resultCode == RESULT_ERROR || resultData == null) {
482 // On error, abort config fetching.
chen xudb04c292019-03-26 17:01:15 -0700483 loge("Failed to get carrier config from carrier app: "
484 + getCarrierPackageForPhoneId(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700485 broadcastConfigChangedIntent(phoneId);
Jack Yue37dd262022-12-16 11:53:37 -0800486 updateSubscriptionDatabase(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700487 return;
488 }
489 PersistableBundle config =
490 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100491 saveConfigToXml(getCarrierPackageForPhoneId(phoneId), "",
492 phoneId, carrierId, config);
Amit Mahajan304e92b2021-04-08 14:03:30 -0700493 if (config != null) {
494 mConfigFromCarrierApp[phoneId] = config;
495 } else {
496 logdWithLocalLog("Config from carrier app is null "
497 + "for phoneId " + phoneId);
498 // Put a stub bundle in place so that the rest of the logic
499 // continues smoothly.
500 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
501 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700502 sendMessage(
503 obtainMessage(
504 EVENT_FETCH_CARRIER_DONE, phoneId, -1));
505 }
506 };
507 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800508 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700509 ICarrierService carrierService =
510 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800511 carrierService.getCarrierConfig(phoneId, carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800512 logdWithLocalLog("Fetch config for carrier app: "
chen xudb04c292019-03-26 17:01:15 -0700513 + getCarrierPackageForPhoneId(phoneId)
Jack Yu9be55d32023-08-10 13:47:46 -0700514 + ", carrierId=" + carrierId.getSpecificCarrierId());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700515 } catch (RemoteException e) {
Jack Yu9be55d32023-08-10 13:47:46 -0700516 loge("Failed to get carrier config: " + e);
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700517 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700518 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800519 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700520 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800521 obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1 /*arg2*/,
522 getMessageToken(phoneId)),
Jonathan Basseri65273c82017-07-25 15:08:42 -0700523 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800524 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700525 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800526
Jonathan Basseri930701e2015-06-11 20:56:43 -0700527 case EVENT_BIND_CARRIER_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800528 case EVENT_FETCH_CARRIER_TIMEOUT: {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800529 loge("Bind/fetch from carrier app timeout, package="
530 + getCarrierPackageForPhoneId(phoneId));
Rambo Wang610fdf62021-03-08 21:37:11 -0800531 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT, getMessageToken(phoneId));
chen xuf60b3162019-05-01 21:31:05 -0700532 // If we attempted to bind to the app, but the service connection is null due to
533 // the race condition that clear config event happens before bind/fetch complete
534 // then config was cleared while we were waiting and we should not continue.
535 if (mServiceConnection[phoneId] != null) {
536 // If a ResponseReceiver callback is in the queue when this happens, we will
537 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700538 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700539 broadcastConfigChangedIntent(phoneId);
540 }
Nathan Harold1122e3d2021-01-22 15:45:24 -0800541 // Put a stub bundle in place so that the rest of the logic continues smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000542 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
Jack Yue37dd262022-12-16 11:53:37 -0800543 updateSubscriptionDatabase(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700544 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700545 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800546 case EVENT_FETCH_CARRIER_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700547 // If we attempted to bind to the app, but the service connection is null, then
548 // config was cleared while we were waiting and we should not continue.
549 if (!msg.getData().getBoolean("loaded_from_xml", false)
550 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800551 break;
552 }
Jack Yue37dd262022-12-16 11:53:37 -0800553 updateSubscriptionDatabase(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800554 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700555 }
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700556
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800557 case EVENT_CHECK_SYSTEM_UPDATE: {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700558 SharedPreferences sharedPrefs =
559 PreferenceManager.getDefaultSharedPreferences(mContext);
560 final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
561 if (!Build.FINGERPRINT.equals(lastFingerprint)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800562 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700563 "Build fingerprint changed. old: "
564 + lastFingerprint
565 + " new: "
566 + Build.FINGERPRINT);
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700567 clearCachedConfigForPackage(null);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700568 sharedPrefs
569 .edit()
570 .putString(KEY_FINGERPRINT, Build.FINGERPRINT)
571 .apply();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700572 }
573 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700574 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700575
576 case EVENT_SUBSCRIPTION_INFO_UPDATED:
577 broadcastConfigChangedIntent(phoneId);
578 break;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700579 case EVENT_MULTI_SIM_CONFIG_CHANGED:
580 onMultiSimConfigChanged();
581 break;
Jayachandran C645ec612020-01-10 10:30:25 -0800582
583 case EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG: {
584 PersistableBundle config =
585 restoreNoSimConfigFromXml(mPlatformCarrierConfigPackage);
586
587 if (config != null) {
Jayachandran C645ec612020-01-10 10:30:25 -0800588 mNoSimConfig = config;
589 sendMessage(
590 obtainMessage(
591 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE,
592 phoneId, -1));
593 } else {
594 // No cached config, so fetch it from the default app.
595 if (bindToConfigPackage(
596 mPlatformCarrierConfigPackage,
597 phoneId,
598 EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG)) {
599 sendMessageDelayed(
600 obtainMessage(
601 EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT,
602 phoneId, -1), BIND_TIMEOUT_MILLIS);
603 } else {
604 broadcastConfigChangedIntent(phoneId, false);
605 // TODO: We *must* call unbindService even if bindService returns false.
606 // (And possibly if SecurityException was thrown.)
607 loge("binding to default app to fetch no SIM config: "
608 + mPlatformCarrierConfigPackage + " fails");
609 }
610 }
611 break;
612 }
613
614 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE: {
615 broadcastConfigChangedIntent(phoneId, false);
616 break;
617 }
618
619 case EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
620 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT: {
621 loge("Bind/fetch time out for no SIM config from "
622 + mPlatformCarrierConfigPackage);
623 removeMessages(EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
624 // If we attempted to bind to the app, but the service connection is null due to
625 // the race condition that clear config event happens before bind/fetch complete
626 // then config was cleared while we were waiting and we should not continue.
627 if (mServiceConnectionForNoSimConfig[phoneId] != null) {
628 // If a ResponseReceiver callback is in the queue when this happens, we will
629 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700630 unbindIfBoundForNoSimConfig(mContext,
631 mServiceConnectionForNoSimConfig[phoneId], phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800632 }
633 broadcastConfigChangedIntent(phoneId, false);
634 break;
635 }
636
637 case EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG: {
638 removeMessages(EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
639 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
640 // If new service connection has been created, unbind.
641 if (mServiceConnectionForNoSimConfig[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700642 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800643 break;
644 }
645
646 // ResultReceiver callback will execute in this Handler's thread.
647 final ResultReceiver resultReceiver =
648 new ResultReceiver(this) {
649 @Override
650 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700651 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800652 // If new service connection has been created, this is stale.
653 if (mServiceConnectionForNoSimConfig[phoneId] != conn) {
654 loge("Received response for stale request.");
655 return;
656 }
657 removeMessages(EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
658 if (resultCode == RESULT_ERROR || resultData == null) {
659 // On error, abort config fetching.
660 loge("Failed to get no SIM carrier config");
661 return;
662 }
663 PersistableBundle config =
664 resultData.getParcelable(KEY_CONFIG_BUNDLE);
665 saveNoSimConfigToXml(mPlatformCarrierConfigPackage, config);
666 mNoSimConfig = config;
667 sendMessage(
668 obtainMessage(
669 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE,
670 phoneId, -1));
671 }
672 };
673 // Now fetch the config asynchronously from the ICarrierService.
674 try {
675 ICarrierService carrierService =
676 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800677 carrierService.getCarrierConfig(phoneId, null, resultReceiver);
Jayachandran C645ec612020-01-10 10:30:25 -0800678 logdWithLocalLog("Fetch no sim config from default app: "
679 + mPlatformCarrierConfigPackage);
680 } catch (RemoteException e) {
681 loge("Failed to get no sim carrier config from default app: " +
Jack Yu9be55d32023-08-10 13:47:46 -0700682 mPlatformCarrierConfigPackage + " err: " + e);
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700683 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800684 break; // So we don't set a timeout.
685 }
686 sendMessageDelayed(
687 obtainMessage(
688 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT,
689 phoneId, -1), BIND_TIMEOUT_MILLIS);
690 break;
691 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800692 }
693 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700694 }
695
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000696 @NonNull private final Handler mHandler;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800697
joonhunshin21a86812023-12-10 08:21:25 +0000698 @NonNull private final FeatureFlags mFeatureFlags;
699
700 @NonNull private final PackageManager mPackageManager;
701
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800702 /**
703 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
704 * receiver for relevant events.
705 */
Rambo Wang7e3bc122021-03-23 09:24:38 -0700706 @VisibleForTesting
joonhunshin21a86812023-12-10 08:21:25 +0000707 /* package */ CarrierConfigLoader(@NonNull Context context, @NonNull Looper looper,
708 @NonNull FeatureFlags featureFlags) {
mattgilbride5b6b7182023-04-03 18:56:44 +0000709 super(PermissionEnforcer.fromContext(context));
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800710 mContext = context;
Meng Wang33ad2bc2017-03-16 20:21:20 -0700711 mPlatformCarrierConfigPackage =
712 mContext.getString(R.string.platform_carrier_config_package);
Rambo Wang7e3bc122021-03-23 09:24:38 -0700713 mHandler = new ConfigHandler(looper);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800714
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000715 IntentFilter systemEventsFilter = new IntentFilter();
716 systemEventsFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
Rambo Wanga27fbe52022-04-12 19:09:07 +0000717 context.registerReceiver(mSystemBroadcastReceiver, systemEventsFilter);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800718
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000719 mNumPhones = TelephonyManager.from(context).getActiveModemCount();
720 mConfigFromDefaultApp = new PersistableBundle[mNumPhones];
721 mConfigFromCarrierApp = new PersistableBundle[mNumPhones];
722 mPersistentOverrideConfigs = new PersistableBundle[mNumPhones];
723 mOverrideConfigs = new PersistableBundle[mNumPhones];
Rambo Wang42026112021-04-07 20:57:28 +0000724 mNoSimConfig = new PersistableBundle();
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000725 mServiceConnection = new CarrierServiceConnection[mNumPhones];
726 mServiceBound = new boolean[mNumPhones];
727 mHasSentConfigChange = new boolean[mNumPhones];
728 mFromSystemUnlocked = new boolean[mNumPhones];
729 mServiceConnectionForNoSimConfig = new CarrierServiceConnection[mNumPhones];
730 mServiceBoundForNoSimConfig = new boolean[mNumPhones];
Rambo Wanga27fbe52022-04-12 19:09:07 +0000731 mCarrierServiceChangeCallbacks = new CarrierServiceChangeCallback[mNumPhones];
732 for (int phoneId = 0; phoneId < mNumPhones; phoneId++) {
733 mCarrierServiceChangeCallbacks[phoneId] = new CarrierServiceChangeCallback(phoneId);
734 TelephonyManager.from(context).registerCarrierPrivilegesCallback(phoneId,
735 new HandlerExecutor(mHandler), mCarrierServiceChangeCallbacks[phoneId]);
736 }
joonhunshin21a86812023-12-10 08:21:25 +0000737 mFeatureFlags = featureFlags;
738 mPackageManager = context.getPackageManager();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800739 logd("CarrierConfigLoader has started");
Jack Yu771c6ce2023-02-02 17:51:42 -0800740
741 PhoneConfigurationManager.registerForMultiSimConfigChange(
742 mHandler, EVENT_MULTI_SIM_CONFIG_CHANGED, null);
743
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700744 mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800745 }
746
747 /**
748 * Initialize the singleton CarrierConfigLoader instance.
749 *
750 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
751 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000752 @NonNull
joonhunshin21a86812023-12-10 08:21:25 +0000753 /* package */ static CarrierConfigLoader init(@NonNull Context context,
754 @NonNull FeatureFlags featureFlags) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800755 synchronized (CarrierConfigLoader.class) {
756 if (sInstance == null) {
joonhunshin21a86812023-12-10 08:21:25 +0000757 sInstance = new CarrierConfigLoader(context, Looper.myLooper(), featureFlags);
Brad Ebingerfa6575f2021-05-04 00:29:50 +0000758 // Make this service available through ServiceManager.
759 TelephonyFrameworkInitializer.getTelephonyServiceManager()
760 .getCarrierConfigServiceRegisterer().register(sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800761 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700762 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800763 }
764 return sInstance;
765 }
766 }
767
Rambo Wang7e3bc122021-03-23 09:24:38 -0700768 @VisibleForTesting
769 /* package */ void clearConfigForPhone(int phoneId, boolean fetchNoSimConfig) {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700770 /* Ignore clear configuration request if device is being shutdown. */
771 Phone phone = PhoneFactory.getPhone(phoneId);
772 if (phone != null) {
773 if (phone.isShuttingDown()) {
774 return;
775 }
776 }
777
Jack Yu771c6ce2023-02-02 17:51:42 -0800778 if (mConfigFromDefaultApp.length <= phoneId) {
779 Log.wtf(LOG_TAG, "Invalid phone id " + phoneId);
780 return;
781 }
782
Malcolm Chen5ea18532019-10-24 19:55:44 -0700783 mConfigFromDefaultApp[phoneId] = null;
784 mConfigFromCarrierApp[phoneId] = null;
785 mServiceConnection[phoneId] = null;
786 mHasSentConfigChange[phoneId] = false;
787
Jayachandran C645ec612020-01-10 10:30:25 -0800788 if (fetchNoSimConfig) {
789 // To fetch no SIM config
790 mHandler.sendMessage(
791 mHandler.obtainMessage(
792 EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG, phoneId, -1));
793 }
Malcolm Chen5ea18532019-10-24 19:55:44 -0700794 }
795
Jack Yue37dd262022-12-16 11:53:37 -0800796 private void updateSubscriptionDatabase(int phoneId) {
Jack Yu90813e62023-01-17 17:46:27 -0800797 logd("updateSubscriptionDatabase: phoneId=" + phoneId);
Jack Yue37dd262022-12-16 11:53:37 -0800798 String configPackageName;
Nathan Harold48ac0972019-03-13 22:33:01 -0700799 PersistableBundle configToSend;
800 int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
801 // Prefer the carrier privileged carrier app, but if there is not one, use the platform
802 // default carrier app.
803 if (mConfigFromCarrierApp[phoneId] != null) {
Jack Yue37dd262022-12-16 11:53:37 -0800804 configPackageName = getCarrierPackageForPhoneId(phoneId);
Nathan Harold48ac0972019-03-13 22:33:01 -0700805 configToSend = mConfigFromCarrierApp[phoneId];
806 } else {
Jack Yue37dd262022-12-16 11:53:37 -0800807 configPackageName = mPlatformCarrierConfigPackage;
Nathan Harold48ac0972019-03-13 22:33:01 -0700808 configToSend = mConfigFromDefaultApp[phoneId];
809 }
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700810
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800811 if (configToSend == null) {
Rambo Wang42026112021-04-07 20:57:28 +0000812 configToSend = new PersistableBundle();
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800813 }
814
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700815 // mOverrideConfigs is for testing. And it will override current configs.
816 PersistableBundle config = mOverrideConfigs[phoneId];
817 if (config != null) {
Torbjorn Eklund1050cb02018-11-16 14:05:38 +0100818 configToSend = new PersistableBundle(configToSend);
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700819 configToSend.putAll(config);
820 }
821
Jack Yu3beaf9d2023-04-14 09:17:27 -0700822 SubscriptionManagerService.getInstance().updateSubscriptionByCarrierConfig(
823 phoneId, configPackageName, configToSend,
824 () -> mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1)
825 .sendToTarget());
Nathan Harold48ac0972019-03-13 22:33:01 -0700826 }
827
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800828 private void broadcastConfigChangedIntent(int phoneId) {
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800829 broadcastConfigChangedIntent(phoneId, true);
830 }
831
832 private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) {
rambowangd63ba342022-10-02 11:21:08 -0500833 int subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
834 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
835 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
836
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800837 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800838 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
chen xuf9db49f2019-03-31 23:04:42 -0700839 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800840 if (addSubIdExtra) {
rambowangd63ba342022-10-02 11:21:08 -0500841 int simApplicationState = getSimApplicationStateForPhone(phoneId);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800842 // Include subId/carrier id extra only if SIM records are loaded
843 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
844 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
rambowangd63ba342022-10-02 11:21:08 -0500845 subId = SubscriptionManager.getSubscriptionId(phoneId);
846 carrierId = getCarrierIdForPhoneId(phoneId);
847 specificCarrierId = getSpecificCarrierIdForPhoneId(phoneId);
848 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID, specificCarrierId);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800849 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
rambowangd63ba342022-10-02 11:21:08 -0500850 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, carrierId);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800851 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800852 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800853 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Sarah Chin807d5c62020-03-30 17:21:09 -0700854 intent.putExtra(CarrierConfigManager.EXTRA_REBROADCAST_ON_UNLOCK,
855 mFromSystemUnlocked[phoneId]);
rambowangd63ba342022-10-02 11:21:08 -0500856
857 TelephonyRegistryManager trm = mContext.getSystemService(TelephonyRegistryManager.class);
858 // Unlike broadcast, we wouldn't notify registrants on carrier config change when device is
859 // unlocked. Only real carrier config change will send the notification to registrants.
860 if (trm != null && !mFromSystemUnlocked[phoneId]) {
861 trm.notifyCarrierConfigChanged(phoneId, subId, carrierId, specificCarrierId);
862 }
863
Meng Wang97a6a462020-01-23 16:22:16 -0800864 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
rambowangd63ba342022-10-02 11:21:08 -0500865
Jack Yu00ece8c2022-11-19 22:29:12 -0800866 if (SubscriptionManager.isValidSubscriptionId(subId)) {
867 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId + ", subId=" + subId);
Jack Yue9e95be2020-03-22 10:56:06 -0700868 } else {
869 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
870 }
Junda Liu03aad762017-07-21 13:32:17 -0700871 mHasSentConfigChange[phoneId] = true;
Sarah Chin807d5c62020-03-30 17:21:09 -0700872 mFromSystemUnlocked[phoneId] = false;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800873 }
874
rambowangd63ba342022-10-02 11:21:08 -0500875 private int getSimApplicationStateForPhone(int phoneId) {
876 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
877 int subId = SubscriptionManager.getSubscriptionId(phoneId);
878 if (SubscriptionManager.isValidSubscriptionId(subId)) {
879 TelephonyManager telMgr = TelephonyManager.from(mContext)
880 .createForSubscriptionId(subId);
881 simApplicationState = telMgr.getSimApplicationState();
882 }
883 return simApplicationState;
884 }
885
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800886 /** Binds to the default or carrier config app. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000887 private boolean bindToConfigPackage(@NonNull String pkgName, int phoneId, int eventId) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800888 logdWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700889 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700890 carrierService.setPackage(pkgName);
Jayachandran C645ec612020-01-10 10:30:25 -0800891 CarrierServiceConnection serviceConnection = new CarrierServiceConnection(
892 phoneId, pkgName, eventId);
893 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
894 mServiceConnectionForNoSimConfig[phoneId] = serviceConnection;
895 } else {
896 mServiceConnection[phoneId] = serviceConnection;
897 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800898 try {
Jayachandran C645ec612020-01-10 10:30:25 -0800899 if (mContext.bindService(carrierService, serviceConnection,
Jordan Liu178430d2020-02-10 14:32:15 -0800900 Context.BIND_AUTO_CREATE)) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700901 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
902 mServiceBoundForNoSimConfig[phoneId] = true;
903 } else {
904 mServiceBound[phoneId] = true;
905 }
Jordan Liu178430d2020-02-10 14:32:15 -0800906 return true;
907 } else {
908 return false;
909 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800910 } catch (SecurityException ex) {
911 return false;
912 }
913 }
914
Rambo Wang7e3bc122021-03-23 09:24:38 -0700915 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000916 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -0700917 /* package */ CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800918 String mcc = "";
919 String mnc = "";
920 String imsi = "";
921 String gid1 = "";
922 String gid2 = "";
923 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
924 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800925 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800926 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700927 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
928 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800929 mcc = simOperator.substring(0, 3);
930 mnc = simOperator.substring(3);
931 }
932 Phone phone = PhoneFactory.getPhone(phoneId);
933 if (phone != null) {
934 imsi = phone.getSubscriberId();
935 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700936 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800937 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800938 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800939 }
chen xua31f22b2019-03-06 15:28:50 -0800940 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800941 }
942
Rambo Wangf7c214a2022-03-17 12:36:22 -0700943 /** Returns the package name of a privileged carrier app, or null if there is none. */
Nathan Harold1122e3d2021-01-22 15:45:24 -0800944 @Nullable
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700945 private String getCarrierPackageForPhoneId(int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -0700946 final long token = Binder.clearCallingIdentity();
947 try {
Rambo Wangf7c214a2022-03-17 12:36:22 -0700948 return TelephonyManager.from(mContext)
949 .getCarrierServicePackageNameForLogicalSlot(phoneId);
Nazanin1adf4562021-03-29 15:35:30 -0700950 } finally {
951 Binder.restoreCallingIdentity(token);
952 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700953 }
954
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000955 @Nullable
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700956 private String getIccIdForPhoneId(int phoneId) {
957 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
958 return null;
959 }
960 Phone phone = PhoneFactory.getPhone(phoneId);
961 if (phone == null) {
962 return null;
963 }
964 return phone.getIccSerialNumber();
965 }
966
chen xu02581692018-11-11 19:03:44 -0800967 /**
chen xua31f22b2019-03-06 15:28:50 -0800968 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800969 */
chen xua31f22b2019-03-06 15:28:50 -0800970 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700971 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800972 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700973 }
974 Phone phone = PhoneFactory.getPhone(phoneId);
975 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800976 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700977 }
chen xua31f22b2019-03-06 15:28:50 -0800978 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800979 }
980
981 /**
982 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
983 */
984 private int getCarrierIdForPhoneId(int phoneId) {
985 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
986 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700987 }
chen xu02581692018-11-11 19:03:44 -0800988 Phone phone = PhoneFactory.getPhone(phoneId);
989 if (phone == null) {
990 return TelephonyManager.UNKNOWN_CARRIER_ID;
991 }
992 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700993 }
994
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700995 /**
996 * Writes a bundle to an XML file.
997 *
chen xu02581692018-11-11 19:03:44 -0800998 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -0800999 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -08001000 * should have a single copy of XML file named after carrier id. However, it's still possible
1001 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
1002 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
1003 * carrier while iccid remains the same.
1004 *
1005 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
1006 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001007 *
1008 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001009 *
Jayachandran C645ec612020-01-10 10:30:25 -08001010 * @param packageName the name of the package from which we fetched this bundle.
1011 * @param extraString An extra string to be used in the XML file name.
1012 * @param phoneId the phone ID.
1013 * @param carrierId contains all carrier-identifying information.
1014 * @param config the bundle to be written. Null will be treated as an empty bundle.
1015 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001016 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001017 private void saveConfigToXml(@Nullable String packageName, @NonNull String extraString,
1018 int phoneId, @Nullable CarrierIdentifier carrierId, @NonNull PersistableBundle config,
1019 boolean isNoSimConfig) {
Jayachandran C645ec612020-01-10 10:30:25 -08001020 if (packageName == null) {
Jack Yu9be55d32023-08-10 13:47:46 -07001021 loge("Cannot save config with null packageName. phoneId=" + phoneId);
Yuchen Dongc15afed2018-04-26 17:05:22 +08001022 return;
1023 }
1024
Jayachandran C645ec612020-01-10 10:30:25 -08001025 String fileName;
1026 if (isNoSimConfig) {
1027 fileName = getFilenameForNoSimConfig(packageName);
1028 } else {
Jack Yuf5badd92022-12-08 00:50:53 -08001029 if (TelephonyManager.getSimStateForSlotIndex(phoneId)
Jayachandran C645ec612020-01-10 10:30:25 -08001030 != TelephonyManager.SIM_STATE_LOADED) {
Jack Yu9be55d32023-08-10 13:47:46 -07001031 loge("Skip saving config because SIM records are not loaded. phoneId=" + phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001032 return;
1033 }
1034
1035 final String iccid = getIccIdForPhoneId(phoneId);
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001036 final int cid = carrierId != null ? carrierId.getSpecificCarrierId()
1037 : TelephonyManager.UNKNOWN_CARRIER_ID;
Jayachandran C645ec612020-01-10 10:30:25 -08001038 if (iccid == null) {
Jack Yu9be55d32023-08-10 13:47:46 -07001039 loge("Cannot save config with null iccid. phoneId=" + phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001040 return;
1041 }
1042 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001043 }
Jayachandran C645ec612020-01-10 10:30:25 -08001044
Junda Liu02596502016-11-15 13:46:43 -08001045 // b/32668103 Only save to file if config isn't empty.
1046 // In case of failure, not caching an empty bundle will
1047 // try loading config again on next power on or sim loaded.
1048 // Downside is for genuinely empty bundle, will bind and load
1049 // on every power on.
1050 if (config == null || config.isEmpty()) {
1051 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001052 }
1053
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001054 final String version = getPackageVersion(packageName);
1055 if (version == null) {
Jack Yu9be55d32023-08-10 13:47:46 -07001056 loge("Failed to get package version for: " + packageName + ", phoneId=" + phoneId);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001057 return;
1058 }
1059
Jack Yu9be55d32023-08-10 13:47:46 -07001060 logdWithLocalLog("Save carrier config to cache. phoneId=" + phoneId
1061 + ", xml=" + getFilePathForLogging(fileName) + ", version=" + version);
chen xudb04c292019-03-26 17:01:15 -07001062
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001063 FileOutputStream outFile = null;
1064 try {
Jayachandran C645ec612020-01-10 10:30:25 -08001065 outFile = new FileOutputStream(new File(mContext.getFilesDir(), fileName));
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001066 config.putString(KEY_VERSION, version);
1067 config.writeToStream(outFile);
1068 outFile.flush();
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001069 outFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001070 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001071 loge(e.toString());
1072 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001073 }
1074
Rambo Wang7e3bc122021-03-23 09:24:38 -07001075 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001076 /* package */ void saveConfigToXml(@Nullable String packageName, @NonNull String extraString,
1077 int phoneId, @NonNull CarrierIdentifier carrierId, @NonNull PersistableBundle config) {
Jayachandran C645ec612020-01-10 10:30:25 -08001078 saveConfigToXml(packageName, extraString, phoneId, carrierId, config, false);
1079 }
1080
Rambo Wang7e3bc122021-03-23 09:24:38 -07001081 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001082 /* package */ void saveNoSimConfigToXml(@Nullable String packageName,
1083 @NonNull PersistableBundle config) {
Jayachandran C645ec612020-01-10 10:30:25 -08001084 saveConfigToXml(packageName, "", -1, null, config, true);
1085 }
1086
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001087 /**
1088 * Reads a bundle from an XML file.
1089 *
1090 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +08001091 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001092 *
1093 * In case of errors, or if the saved config is from a different package version than the
1094 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001095 *
Jayachandran C645ec612020-01-10 10:30:25 -08001096 * @param packageName the name of the package from which we fetched this bundle.
1097 * @param extraString An extra string to be used in the XML file name.
1098 * @param phoneId the phone ID.
1099 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001100 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001101 * version does not match, or reading config fails.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001102 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001103 @Nullable
1104 private PersistableBundle restoreConfigFromXml(@Nullable String packageName,
1105 @NonNull String extraString, int phoneId, boolean isNoSimConfig) {
Jayachandran C645ec612020-01-10 10:30:25 -08001106 if (packageName == null) {
1107 loge("Cannot restore config with null packageName");
1108 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001109 final String version = getPackageVersion(packageName);
1110 if (version == null) {
1111 loge("Failed to get package version for: " + packageName);
1112 return null;
1113 }
Yuchen Dongc15afed2018-04-26 17:05:22 +08001114
Jayachandran C645ec612020-01-10 10:30:25 -08001115 String fileName;
arunvoddu664c36a2021-10-11 20:09:28 +00001116 String iccid = null;
Jayachandran C645ec612020-01-10 10:30:25 -08001117 if (isNoSimConfig) {
1118 fileName = getFilenameForNoSimConfig(packageName);
1119 } else {
Jack Yuf5badd92022-12-08 00:50:53 -08001120 if (TelephonyManager.getSimStateForSlotIndex(phoneId)
Jayachandran C645ec612020-01-10 10:30:25 -08001121 != TelephonyManager.SIM_STATE_LOADED) {
Jack Yu9be55d32023-08-10 13:47:46 -07001122 loge("Skip restore config because SIM records are not loaded. phoneId=" + phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001123 return null;
1124 }
1125
arunvoddu664c36a2021-10-11 20:09:28 +00001126 iccid = getIccIdForPhoneId(phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001127 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1128 if (iccid == null) {
Jack Yu9be55d32023-08-10 13:47:46 -07001129 loge("Cannot restore config with null iccid. phoneId=" + phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001130 return null;
1131 }
1132 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001133 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001134
1135 PersistableBundle restoredBundle = null;
Mateus Azisa13d2502023-05-25 08:44:38 -07001136 File file = new File(mContext.getFilesDir(), fileName);
Jack Yu9be55d32023-08-10 13:47:46 -07001137 String filePath = file.getPath();
1138 String savedVersion = null;
Mateus Azisa13d2502023-05-25 08:44:38 -07001139 try (FileInputStream inFile = new FileInputStream(file)) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001140
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001141 restoredBundle = PersistableBundle.readFromStream(inFile);
Jack Yu9be55d32023-08-10 13:47:46 -07001142 savedVersion = restoredBundle.getString(KEY_VERSION);
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001143 restoredBundle.remove(KEY_VERSION);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001144
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001145 if (!version.equals(savedVersion)) {
Jack Yu9be55d32023-08-10 13:47:46 -07001146 loge("Saved version mismatch: " + version + " vs " + savedVersion
1147 + ", phoneId=" + phoneId);
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001148 restoredBundle = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001149 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001150 } catch (FileNotFoundException e) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001151 // Missing file is normal occurrence that might occur with a new sim or when restoring
1152 // an override file during boot and should not be treated as an error.
Mateus Azisa13d2502023-05-25 08:44:38 -07001153 if (isNoSimConfig) {
Jack Yu9be55d32023-08-10 13:47:46 -07001154 logd("File not found: " + file.getPath() + ", phoneId=" + phoneId);
Mateus Azisa13d2502023-05-25 08:44:38 -07001155 } else {
Jack Yu9be55d32023-08-10 13:47:46 -07001156 logd("File not found : " + getFilePathForLogging(filePath, iccid)
1157 + ", phoneId=" + phoneId);
arunvoddu664c36a2021-10-11 20:09:28 +00001158 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001159 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001160 loge(e.toString());
1161 }
1162
Jack Yu9be55d32023-08-10 13:47:46 -07001163 if (restoredBundle != null) {
1164 logdWithLocalLog("Restored carrier config from cache. phoneId=" + phoneId + ", xml="
1165 + getFilePathForLogging(fileName) + ", version=" + savedVersion
1166 + ", modified time=" + getFileTime(filePath));
1167 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001168 return restoredBundle;
1169 }
1170
arunvoddu664c36a2021-10-11 20:09:28 +00001171 /**
1172 * This method will mask most part of iccid in the filepath for logging on userbuild
1173 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001174 @NonNull
1175 private String getFilePathForLogging(@Nullable String filePath, @Nullable String iccid) {
arunvoddu664c36a2021-10-11 20:09:28 +00001176 // If loggable then return with actual file path
Jack Yu9be55d32023-08-10 13:47:46 -07001177 if (TelephonyUtils.IS_DEBUGGABLE) {
arunvoddu664c36a2021-10-11 20:09:28 +00001178 return filePath;
1179 }
1180 String path = filePath;
1181 int length = (iccid != null) ? iccid.length() : 0;
1182 if (length > 5 && filePath != null) {
1183 path = filePath.replace(iccid.substring(5), "***************");
1184 }
1185 return path;
1186 }
1187
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001188 @Nullable
1189 private PersistableBundle restoreConfigFromXml(@Nullable String packageName,
1190 @NonNull String extraString, int phoneId) {
Jayachandran C645ec612020-01-10 10:30:25 -08001191 return restoreConfigFromXml(packageName, extraString, phoneId, false);
1192 }
1193
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001194 @Nullable
1195 private PersistableBundle restoreNoSimConfigFromXml(@Nullable String packageName) {
Jayachandran C645ec612020-01-10 10:30:25 -08001196 return restoreConfigFromXml(packageName, "", -1, true);
1197 }
1198
Junda Liu8a8a53a2015-05-15 16:19:57 -07001199 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001200 * Clears cached carrier config.
1201 * This deletes all saved XML files associated with the given package name. If packageName is
1202 * null, then it deletes all saved XML files.
1203 *
1204 * @param packageName the name of a carrier package, or null if all cached config should be
1205 * cleared.
1206 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -07001207 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001208 private boolean clearCachedConfigForPackage(@Nullable final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001209 File dir = mContext.getFilesDir();
1210 File[] packageFiles = dir.listFiles(new FilenameFilter() {
1211 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001212 if (packageName != null) {
1213 return filename.startsWith("carrierconfig-" + packageName + "-");
1214 } else {
1215 return filename.startsWith("carrierconfig-");
1216 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001217 }
1218 });
Junda Liu8a8a53a2015-05-15 16:19:57 -07001219 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001220 for (File f : packageFiles) {
Jack Yu9be55d32023-08-10 13:47:46 -07001221 logdWithLocalLog("Deleting " + getFilePathForLogging(f.getName()));
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001222 f.delete();
1223 }
Junda Liu8a8a53a2015-05-15 16:19:57 -07001224 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001225 }
1226
arunvoddub7c6e8e2022-09-05 15:48:06 +00001227 private String getFilePathForLogging(String filePath) {
1228 if (!TextUtils.isEmpty(filePath)) {
1229 String[] fileTokens = filePath.split("-");
1230 if (fileTokens != null && fileTokens.length > 2) {
1231 String iccid = fileTokens[fileTokens.length -2];
1232 return getFilePathForLogging(filePath, iccid);
1233 }
1234 return filePath;
1235 }
1236 return filePath;
1237 }
1238
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001239 /** Builds a canonical file name for a config file. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001240 @NonNull
Jayachandran C645ec612020-01-10 10:30:25 -08001241 private static String getFilenameForConfig(
1242 @NonNull String packageName, @NonNull String extraString,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001243 @NonNull String iccid, int cid) {
chen xu02581692018-11-11 19:03:44 -08001244 // the same carrier should have a single copy of XML file named after carrier id.
1245 // However, it's still possible that platform doesn't recognize the current sim carrier,
1246 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
1247 // cases SIM OTA resolves to different carrier while iccid remains the same.
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001248 return "carrierconfig-" + packageName + extraString + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001249 }
1250
Jayachandran C645ec612020-01-10 10:30:25 -08001251 /** Builds a canonical file name for no SIM config file. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001252 @NonNull
Jayachandran C645ec612020-01-10 10:30:25 -08001253 private String getFilenameForNoSimConfig(@NonNull String packageName) {
1254 return "carrierconfig-" + packageName + "-" + "nosim" + ".xml";
1255 }
1256
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001257 /** Return the current version code of a package, or null if the name is not found. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001258 @Nullable
1259 private String getPackageVersion(@NonNull String packageName) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001260 try {
1261 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -08001262 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001263 } catch (PackageManager.NameNotFoundException e) {
1264 return null;
1265 }
1266 }
1267
Jonathan Basseri65273c82017-07-25 15:08:42 -07001268 /**
1269 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001270 *
1271 * This reads config bundles for the given phoneId. That means getting the latest bundle from
1272 * the default app and a privileged carrier app, if present. This will not bind to an app if we
1273 * have a saved config file to use instead.
1274 */
1275 private void updateConfigForPhoneId(int phoneId) {
Jonathan Basseri65273c82017-07-25 15:08:42 -07001276 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001277 }
1278
Malcolm Chen5ea18532019-10-24 19:55:44 -07001279 private void onMultiSimConfigChanged() {
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001280 int oldNumPhones = mNumPhones;
1281 mNumPhones = TelephonyManager.from(mContext).getActiveModemCount();
1282 if (mNumPhones == oldNumPhones) {
1283 return;
1284 }
1285 logdWithLocalLog("mNumPhones change from " + oldNumPhones + " to " + mNumPhones);
1286
Rambo Wanga27fbe52022-04-12 19:09:07 +00001287 // If DS -> SS switch, release the resources BEFORE truncating the arrays to avoid leaking
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001288 for (int phoneId = mNumPhones; phoneId < oldNumPhones; phoneId++) {
1289 if (mServiceConnection[phoneId] != null) {
1290 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
1291 }
1292 if (mServiceConnectionForNoSimConfig[phoneId] != null) {
1293 unbindIfBoundForNoSimConfig(mContext, mServiceConnectionForNoSimConfig[phoneId],
1294 phoneId);
1295 }
1296 }
1297
Rambo Wanga27fbe52022-04-12 19:09:07 +00001298 // The phone to slot mapping may change, unregister here and re-register callbacks later
1299 for (int phoneId = 0; phoneId < oldNumPhones; phoneId++) {
1300 if (mCarrierServiceChangeCallbacks[phoneId] != null) {
1301 TelephonyManager.from(mContext).unregisterCarrierPrivilegesCallback(
1302 mCarrierServiceChangeCallbacks[phoneId]);
1303 }
1304 }
1305
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001306 // Copy the original arrays, truncate or padding with zeros (if necessary) to new length
1307 mConfigFromDefaultApp = Arrays.copyOf(mConfigFromDefaultApp, mNumPhones);
1308 mConfigFromCarrierApp = Arrays.copyOf(mConfigFromCarrierApp, mNumPhones);
1309 mPersistentOverrideConfigs = Arrays.copyOf(mPersistentOverrideConfigs, mNumPhones);
1310 mOverrideConfigs = Arrays.copyOf(mOverrideConfigs, mNumPhones);
1311 mServiceConnection = Arrays.copyOf(mServiceConnection, mNumPhones);
1312 mServiceConnectionForNoSimConfig =
1313 Arrays.copyOf(mServiceConnectionForNoSimConfig, mNumPhones);
1314 mServiceBound = Arrays.copyOf(mServiceBound, mNumPhones);
1315 mServiceBoundForNoSimConfig = Arrays.copyOf(mServiceBoundForNoSimConfig, mNumPhones);
1316 mHasSentConfigChange = Arrays.copyOf(mHasSentConfigChange, mNumPhones);
1317 mFromSystemUnlocked = Arrays.copyOf(mFromSystemUnlocked, mNumPhones);
Rambo Wanga27fbe52022-04-12 19:09:07 +00001318 mCarrierServiceChangeCallbacks = Arrays.copyOf(mCarrierServiceChangeCallbacks, mNumPhones);
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001319
Rambo Wanga27fbe52022-04-12 19:09:07 +00001320 // Load the config for all the phones and re-register callback AFTER padding the arrays.
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001321 for (int phoneId = 0; phoneId < mNumPhones; phoneId++) {
1322 updateConfigForPhoneId(phoneId);
Rambo Wanga27fbe52022-04-12 19:09:07 +00001323 mCarrierServiceChangeCallbacks[phoneId] = new CarrierServiceChangeCallback(phoneId);
1324 TelephonyManager.from(mContext).registerCarrierPrivilegesCallback(phoneId,
1325 new HandlerExecutor(mHandler), mCarrierServiceChangeCallbacks[phoneId]);
Malcolm Chen5ea18532019-10-24 19:55:44 -07001326 }
1327 }
1328
Hall Liu506724b2018-10-22 18:16:14 -07001329 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001330 @NonNull
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001331 public PersistableBundle getConfigForSubId(int subscriptionId, @NonNull String callingPackage) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001332 return getConfigForSubIdWithFeature(subscriptionId, callingPackage, null);
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08001333 }
1334
1335 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001336 @NonNull
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001337 public PersistableBundle getConfigForSubIdWithFeature(int subscriptionId,
1338 @NonNull String callingPackage, @Nullable String callingFeatureId) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001339 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subscriptionId,
1340 callingPackage, callingFeatureId, "getCarrierConfig")) {
Rambo Wang42026112021-04-07 20:57:28 +00001341 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -07001342 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -07001343
joonhunshin21a86812023-12-10 08:21:25 +00001344 enforceTelephonyFeatureWithException(callingPackage, "getConfigForSubIdWithFeature");
1345
Rambo Wangd2b004b2021-03-30 10:10:23 -07001346 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001347 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001348 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001349 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001350 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001351 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001352 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001353 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001354 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001355 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001356 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001357 config = mPersistentOverrideConfigs[phoneId];
1358 if (config != null) {
1359 retConfig.putAll(config);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001360 }
Hall Liu506724b2018-10-22 18:16:14 -07001361 config = mOverrideConfigs[phoneId];
1362 if (config != null) {
1363 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -07001364 }
Nathan Harold1122e3d2021-01-22 15:45:24 -08001365 // Ignore the theoretical case of the default app not being present since that won't
1366 // work in CarrierConfigLoader today.
1367 final boolean allConfigsApplied =
1368 (mConfigFromCarrierApp[phoneId] != null
1369 || getCarrierPackageForPhoneId(phoneId) == null)
1370 && mConfigFromDefaultApp[phoneId] != null;
1371 retConfig.putBoolean(
1372 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, allConfigsApplied);
Jayachandran C645ec612020-01-10 10:30:25 -08001373 } else {
1374 if (mNoSimConfig != null) {
1375 retConfig.putAll(mNoSimConfig);
1376 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001377 }
1378 return retConfig;
1379 }
1380
1381 @Override
rambowang14757c22022-10-03 11:54:56 -05001382 @NonNull
1383 public PersistableBundle getConfigSubsetForSubIdWithFeature(int subscriptionId,
1384 @NonNull String callingPackage, @Nullable String callingFeatureId,
1385 @NonNull String[] keys) {
1386 Objects.requireNonNull(callingPackage, "Calling package must be non-null");
1387 Objects.requireNonNull(keys, "Config keys must be non-null");
1388 enforceCallerIsSystemOrRequestingPackage(callingPackage);
1389
joonhunshin21a86812023-12-10 08:21:25 +00001390 enforceTelephonyFeatureWithException(callingPackage,
1391 "getConfigSubsetForSubIdWithFeature");
1392
rambowang14757c22022-10-03 11:54:56 -05001393 // Permission check is performed inside and an empty bundle will return on failure.
1394 // No SecurityException thrown here since most clients expect to retrieve the overridden
1395 // value if present or use default one if not
1396 PersistableBundle allConfigs = getConfigForSubIdWithFeature(subscriptionId, callingPackage,
1397 callingFeatureId);
1398 if (allConfigs.isEmpty()) {
1399 return allConfigs;
1400 }
1401 for (String key : keys) {
1402 Objects.requireNonNull(key, "Config key must be non-null");
rambowang14757c22022-10-03 11:54:56 -05001403 }
1404
1405 PersistableBundle configSubset = new PersistableBundle(
1406 keys.length + CONFIG_SUBSET_METADATA_KEYS.length);
1407 for (String carrierConfigKey : keys) {
1408 Object value = allConfigs.get(carrierConfigKey);
rambowangb49e90f2022-12-13 18:29:06 -06001409 if (value == null) {
1410 // Filter out keys without values.
1411 // In history, many AOSP or OEMs/carriers private configs didn't provide default
1412 // values. We have to continue supporting them for now. See b/261776046 for details.
1413 continue;
1414 }
rambowang14757c22022-10-03 11:54:56 -05001415 // Config value itself could be PersistableBundle which requires different API to put
1416 if (value instanceof PersistableBundle) {
1417 configSubset.putPersistableBundle(carrierConfigKey, (PersistableBundle) value);
1418 } else {
1419 configSubset.putObject(carrierConfigKey, value);
1420 }
1421 }
1422
1423 // Configs in CONFIG_SUBSET_ALWAYS_INCLUDED_KEYS should always be included
1424 for (String generalKey : CONFIG_SUBSET_METADATA_KEYS) {
1425 configSubset.putObject(generalKey, allConfigs.get(generalKey));
1426 }
1427
1428 return configSubset;
1429 }
1430
mattgilbride5b6b7182023-04-03 18:56:44 +00001431 @android.annotation.EnforcePermission(android.Manifest.permission.MODIFY_PHONE_STATE)
rambowang14757c22022-10-03 11:54:56 -05001432 @Override
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001433 public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides,
1434 boolean persistent) {
mattgilbride5b6b7182023-04-03 18:56:44 +00001435 overrideConfig_enforcePermission();
Hall Liu506724b2018-10-22 18:16:14 -07001436 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1437 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001438 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
Rambo Wangd2b004b2021-03-30 10:10:23 -07001439 throw new IllegalArgumentException(
1440 "Invalid phoneId " + phoneId + " for subId " + subscriptionId);
Hall Liu506724b2018-10-22 18:16:14 -07001441 }
joonhunshin21a86812023-12-10 08:21:25 +00001442
1443 enforceTelephonyFeatureWithException(getCurrentPackageName(), "overrideConfig");
1444
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001445 // Post to run on handler thread on which all states should be confined.
1446 mHandler.post(() -> {
1447 overrideConfig(mOverrideConfigs, phoneId, overrides);
Hall Liu506724b2018-10-22 18:16:14 -07001448
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001449 if (persistent) {
1450 overrideConfig(mPersistentOverrideConfigs, phoneId, overrides);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001451
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001452 if (overrides != null) {
1453 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
1454 saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION,
1455 phoneId,
1456 carrierId, mPersistentOverrideConfigs[phoneId]);
1457 } else {
1458 final String iccid = getIccIdForPhoneId(phoneId);
1459 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1460 String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage,
1461 OVERRIDE_PACKAGE_ADDITION, iccid, cid);
1462 File fileToDelete = new File(mContext.getFilesDir(), fileName);
1463 fileToDelete.delete();
1464 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001465 }
Jack Yu9be55d32023-08-10 13:47:46 -07001466 logdWithLocalLog("overrideConfig: subId=" + subscriptionId + ", persistent="
1467 + persistent + ", overrides=" + overrides);
Jack Yue37dd262022-12-16 11:53:37 -08001468 updateSubscriptionDatabase(phoneId);
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001469 });
Hall Liu506724b2018-10-22 18:16:14 -07001470 }
1471
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001472 private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId,
1473 @Nullable PersistableBundle overrides) {
1474 if (overrides == null) {
Rambo Wang42026112021-04-07 20:57:28 +00001475 currentOverrides[phoneId] = new PersistableBundle();
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001476 } else if (currentOverrides[phoneId] == null) {
1477 currentOverrides[phoneId] = overrides;
1478 } else {
1479 currentOverrides[phoneId].putAll(overrides);
1480 }
1481 }
1482
Hall Liu506724b2018-10-22 18:16:14 -07001483 @Override
Rambo Wangd2b004b2021-03-30 10:10:23 -07001484 public void notifyConfigChangedForSubId(int subscriptionId) {
Jordan Liud7d57fe2019-04-16 12:29:43 -07001485 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001486 // system privileged app with MODIFY_PHONE_STATE permission.
Rambo Wangd2b004b2021-03-30 10:10:23 -07001487 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext,
1488 subscriptionId, "Require carrier privileges or MODIFY_PHONE_STATE permission.");
1489
1490 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1491 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Rambo Wang0c386012024-02-22 22:38:17 +00001492 final String msg =
1493 "Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId;
1494 if (mFeatureFlags.addAnomalyWhenNotifyConfigChangedWithInvalidPhone()) {
1495 AnomalyReporter.reportAnomaly(
1496 UUID.fromString(UUID_NOTIFY_CONFIG_CHANGED_WITH_INVALID_PHONE), msg);
1497 }
1498 logd(msg);
1499 throw new IllegalArgumentException(msg);
Rambo Wangd2b004b2021-03-30 10:10:23 -07001500 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001501
joonhunshin21a86812023-12-10 08:21:25 +00001502 enforceTelephonyFeatureWithException(getCurrentPackageName(),
1503 "notifyConfigChangedForSubId");
1504
Jack Yu9be55d32023-08-10 13:47:46 -07001505 logdWithLocalLog("Notified carrier config changed. phoneId=" + phoneId
1506 + ", subId=" + subscriptionId);
1507
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001508 // This method should block until deleting has completed, so that an error which prevents us
1509 // from clearing the cache is passed back to the carrier app. With the files successfully
1510 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001511 String callingPackageName = mContext.getPackageManager().getNameForUid(
1512 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001513 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001514 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001515 }
1516
mattgilbride5b6b7182023-04-03 18:56:44 +00001517 @android.annotation.EnforcePermission(android.Manifest.permission.MODIFY_PHONE_STATE)
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001518 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001519 public void updateConfigForPhoneId(int phoneId, @NonNull String simState) {
mattgilbride5b6b7182023-04-03 18:56:44 +00001520 updateConfigForPhoneId_enforcePermission();
Jack Yu9be55d32023-08-10 13:47:46 -07001521 logdWithLocalLog("Update config for phoneId=" + phoneId + " simState=" + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001522 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001523 throw new IllegalArgumentException("Invalid phoneId: " + phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001524 }
joonhunshin21a86812023-12-10 08:21:25 +00001525
1526 enforceTelephonyFeatureWithException(getCurrentPackageName(), "updateConfigForPhoneId");
1527
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001528 // requires Java 7 for switch on string.
1529 switch (simState) {
1530 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1531 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001532 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001533 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Malcolm Chendb66de12019-12-09 18:57:07 -08001534 case IccCardConstants.INTENT_VALUE_ICC_NOT_READY:
Junda Liu9f2d2712015-05-15 14:22:45 -07001535 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001536 break;
1537 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1538 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001539 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001540 break;
1541 }
1542 }
1543
mattgilbride5b6b7182023-04-03 18:56:44 +00001544 @android.annotation.EnforcePermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
Junda Liu43d723a2015-05-12 17:23:45 -07001545 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001546 @NonNull
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001547 public String getDefaultCarrierServicePackageName() {
mattgilbride5b6b7182023-04-03 18:56:44 +00001548 getDefaultCarrierServicePackageName_enforcePermission();
joonhunshin21a86812023-12-10 08:21:25 +00001549
1550 enforceTelephonyFeatureWithException(getCurrentPackageName(),
1551 "getDefaultCarrierServicePackageName");
1552
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001553 return mPlatformCarrierConfigPackage;
1554 }
1555
Rambo Wang7e3bc122021-03-23 09:24:38 -07001556 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001557 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -07001558 /* package */ Handler getHandler() {
1559 return mHandler;
1560 }
1561
1562 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001563 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001564 /* package */ PersistableBundle getConfigFromDefaultApp(int phoneId) {
1565 return mConfigFromDefaultApp[phoneId];
1566 }
1567
1568 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001569 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001570 /* package */ PersistableBundle getConfigFromCarrierApp(int phoneId) {
1571 return mConfigFromCarrierApp[phoneId];
1572 }
1573
1574 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001575 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -07001576 /* package */ PersistableBundle getNoSimConfig() {
1577 return mNoSimConfig;
1578 }
1579
1580 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001581 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001582 /* package */ PersistableBundle getOverrideConfig(int phoneId) {
1583 return mOverrideConfigs[phoneId];
1584 }
1585
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001586 // TODO(b/185129900): always call unbindService after bind, no matter if it succeeded
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001587 private void unbindIfBound(@NonNull Context context, @NonNull CarrierServiceConnection conn,
Rambo Wangb0f3e2f2021-10-28 12:40:12 -07001588 int phoneId) {
1589 if (mServiceBound[phoneId]) {
1590 mServiceBound[phoneId] = false;
1591 context.unbindService(conn);
1592 }
1593 }
1594
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001595 private void unbindIfBoundForNoSimConfig(@NonNull Context context,
1596 @NonNull CarrierServiceConnection conn, int phoneId) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -07001597 if (mServiceBoundForNoSimConfig[phoneId]) {
1598 mServiceBoundForNoSimConfig[phoneId] = false;
Jayachandran C645ec612020-01-10 10:30:25 -08001599 context.unbindService(conn);
1600 }
1601 }
1602
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001603 /**
Rambo Wang610fdf62021-03-08 21:37:11 -08001604 * Returns a boxed Integer object for phoneId, services as message token to distinguish messages
1605 * with same code when calling {@link Handler#removeMessages(int, Object)}.
1606 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001607 @NonNull
Rambo Wang610fdf62021-03-08 21:37:11 -08001608 private Integer getMessageToken(int phoneId) {
1609 if (phoneId < -128 || phoneId > 127) {
1610 throw new IllegalArgumentException("phoneId should be in range [-128, 127], inclusive");
1611 }
1612 // Integer#valueOf guarantees the integers within [-128, 127] are cached and thus memory
1613 // comparison (==) returns true for the same integer.
1614 return Integer.valueOf(phoneId);
1615 }
1616
1617 /**
Jack Yu9be55d32023-08-10 13:47:46 -07001618 * Get the file time in readable format.
1619 *
1620 * @param filePath The full file path.
1621 *
1622 * @return The time in string format.
1623 */
1624 @Nullable
1625 private String getFileTime(@NonNull String filePath) {
1626 String formattedModifiedTime = null;
1627 try {
1628 // Convert the modified time to a readable format
1629 formattedModifiedTime = TIME_FORMAT.format(Files.readAttributes(Paths.get(filePath),
1630 BasicFileAttributes.class).lastModifiedTime().toMillis());
1631 } catch (Exception e) {
1632 e.printStackTrace();
1633 }
1634
1635 return formattedModifiedTime;
1636 }
1637
1638 /**
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001639 * If {@code args} contains {@link #DUMP_ARG_REQUESTING_PACKAGE} and a following package name,
1640 * we'll also call {@link IBinder#dump} on the default carrier service (if bound) and the
1641 * specified carrier service (if bound). Typically, this is done for connectivity bug reports
1642 * where we don't call {@code dumpsys activity service all-non-platform} because that contains
1643 * too much info, but we still want to let carrier apps include their diagnostics.
1644 */
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001645 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001646 public void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter pw, @NonNull String[] args) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001647 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
Junda Liu43d723a2015-05-12 17:23:45 -07001648 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1649 != PackageManager.PERMISSION_GRANTED) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001650 indentPW.println("Permission Denial: can't dump carrierconfig from from pid="
Junda Liu43d723a2015-05-12 17:23:45 -07001651 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1652 return;
1653 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001654 String requestingPackage = null;
1655 int requestingPackageIndex = ArrayUtils.indexOf(args, DUMP_ARG_REQUESTING_PACKAGE);
1656 if (requestingPackageIndex >= 0 && requestingPackageIndex < args.length - 1
1657 && !TextUtils.isEmpty(args[requestingPackageIndex + 1])) {
1658 requestingPackage = args[requestingPackageIndex + 1];
1659 // Throws a SecurityException if the caller is impersonating another app in an effort to
1660 // dump extra info (which may contain PII the caller doesn't have a right to).
1661 enforceCallerIsSystemOrRequestingPackage(requestingPackage);
1662 }
1663
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001664 indentPW.println("CarrierConfigLoader: " + this);
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001665 for (int i = 0; i < mNumPhones; i++) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001666 indentPW.println("Phone Id = " + i);
shuoq26a3a4c2016-12-16 11:06:48 -08001667 // display default values in CarrierConfigManager
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001668 printConfig(CarrierConfigManager.getDefaultConfig(), indentPW,
shuoq26a3a4c2016-12-16 11:06:48 -08001669 "Default Values from CarrierConfigManager");
shuoq26a3a4c2016-12-16 11:06:48 -08001670 // display ConfigFromDefaultApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001671 printConfig(mConfigFromDefaultApp[i], indentPW, "mConfigFromDefaultApp");
shuoq26a3a4c2016-12-16 11:06:48 -08001672 // display ConfigFromCarrierApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001673 printConfig(mConfigFromCarrierApp[i], indentPW, "mConfigFromCarrierApp");
1674 printConfig(mPersistentOverrideConfigs[i], indentPW, "mPersistentOverrideConfigs");
1675 printConfig(mOverrideConfigs[i], indentPW, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001676 }
chen xudb04c292019-03-26 17:01:15 -07001677
Jayachandran C645ec612020-01-10 10:30:25 -08001678 printConfig(mNoSimConfig, indentPW, "mNoSimConfig");
Jack Yu9be55d32023-08-10 13:47:46 -07001679 indentPW.println("mNumPhones=" + mNumPhones);
1680 indentPW.println("mPlatformCarrierConfigPackage=" + mPlatformCarrierConfigPackage);
1681 indentPW.println("mServiceConnection=[" + Stream.of(mServiceConnection)
1682 .map(c -> c != null ? c.pkgName : null)
1683 .collect(Collectors.joining(", ")) + "]");
1684 indentPW.println("mServiceBoundForNoSimConfig="
1685 + Arrays.toString(mServiceBoundForNoSimConfig));
1686 indentPW.println("mHasSentConfigChange=" + Arrays.toString(mHasSentConfigChange));
1687 indentPW.println("mFromSystemUnlocked=" + Arrays.toString(mFromSystemUnlocked));
1688 indentPW.println();
1689 indentPW.println("CarrierConfigLoader local log=");
1690 indentPW.increaseIndent();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001691 mCarrierConfigLoadingLog.dump(fd, indentPW, args);
Jack Yu9be55d32023-08-10 13:47:46 -07001692 indentPW.decreaseIndent();
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001693
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001694 if (requestingPackage != null) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001695 logd("Including default and requesting package " + requestingPackage
1696 + " carrier services in dump");
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001697 indentPW.println("");
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001698 indentPW.println("Connected services");
1699 dumpCarrierServiceIfBound(fd, indentPW, "Default config package",
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001700 mPlatformCarrierConfigPackage, false /* considerCarrierPrivileges */);
1701 dumpCarrierServiceIfBound(fd, indentPW, "Requesting package", requestingPackage,
1702 true /* considerCarrierPrivileges */);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001703 }
Jack Yu9be55d32023-08-10 13:47:46 -07001704
1705 indentPW.println();
1706 indentPW.println("Cached config files:");
1707 indentPW.increaseIndent();
1708 for (File f : mContext.getFilesDir().listFiles((FilenameFilter) (d, filename)
1709 -> filename.startsWith("carrierconfig-"))) {
1710 indentPW.println(getFilePathForLogging(f.getName()) + ", modified time="
1711 + getFileTime(f.getAbsolutePath()));
1712 }
1713 indentPW.decreaseIndent();
shuoq26a3a4c2016-12-16 11:06:48 -08001714 }
1715
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001716 private void printConfig(@NonNull PersistableBundle configApp,
1717 @NonNull IndentingPrintWriter indentPW, @NonNull String name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001718 indentPW.increaseIndent();
shuoq26a3a4c2016-12-16 11:06:48 -08001719 if (configApp == null) {
shuoq26a3a4c2016-12-16 11:06:48 -08001720 indentPW.println(name + " : null ");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001721 indentPW.decreaseIndent();
1722 indentPW.println("");
shuoq26a3a4c2016-12-16 11:06:48 -08001723 return;
1724 }
shuoq26a3a4c2016-12-16 11:06:48 -08001725 indentPW.println(name + " : ");
1726 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1727 Collections.sort(sortedKeys);
1728 indentPW.increaseIndent();
1729 indentPW.increaseIndent();
1730 for (String key : sortedKeys) {
1731 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1732 indentPW.println(key + " = " +
1733 Arrays.toString((Object[]) configApp.get(key)));
1734 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1735 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1736 } else {
1737 indentPW.println(key + " = " + configApp.get(key));
1738 }
Junda Liu43d723a2015-05-12 17:23:45 -07001739 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001740 indentPW.decreaseIndent();
1741 indentPW.decreaseIndent();
1742 indentPW.decreaseIndent();
1743 indentPW.println("");
Junda Liu43d723a2015-05-12 17:23:45 -07001744 }
1745
Hunter Knepshielde601f432020-02-19 10:16:04 -08001746 /**
1747 * Passes without problem when one of these conditions is true:
1748 * - The caller is a privileged UID (e.g. for dumpstate.cpp generating a bug report, where the
1749 * system knows the true caller plumbed in through the {@link android.os.BugreportManager} API).
1750 * - The caller's UID matches the supplied package.
1751 *
1752 * @throws SecurityException if none of the above conditions are met.
1753 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001754 private void enforceCallerIsSystemOrRequestingPackage(@NonNull String requestingPackage)
Hunter Knepshielde601f432020-02-19 10:16:04 -08001755 throws SecurityException {
1756 final int callingUid = Binder.getCallingUid();
1757 if (callingUid == Process.ROOT_UID || callingUid == Process.SYSTEM_UID
1758 || callingUid == Process.SHELL_UID || callingUid == Process.PHONE_UID) {
1759 // Bug reports (dumpstate.cpp) run as SHELL, and let some other privileged UIDs through
1760 // as well.
1761 return;
1762 }
1763 // An app is trying to dump extra detail, block it if they aren't who they claim to be.
1764 AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class);
1765 if (appOps == null) {
1766 throw new SecurityException("No AppOps");
1767 }
1768 // Will throw a SecurityException if the UID and package don't match.
1769 appOps.checkPackage(callingUid, requestingPackage);
1770 }
1771
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001772 /**
1773 * Searches for one or more appropriate {@link CarrierService} instances to dump based on the
1774 * current connections.
1775 *
1776 * @param targetPkgName the target package name to dump carrier services for
1777 * @param considerCarrierPrivileges if true, allow a carrier service to be dumped if it shares
1778 * carrier privileges with {@code targetPkgName};
1779 * otherwise, only dump a carrier service if it is {@code
1780 * targetPkgName}
1781 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001782 private void dumpCarrierServiceIfBound(@NonNull FileDescriptor fd,
1783 @NonNull IndentingPrintWriter indentPW, @NonNull String prefix,
1784 @NonNull String targetPkgName, boolean considerCarrierPrivileges) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001785 // Null package is possible if it's early in the boot process, there was a recent crash, we
1786 // loaded the config from XML most recently, or a SIM slot is empty. Carrier apps with
1787 // long-lived bindings should typically get dumped here regardless. Even if an app is being
1788 // used for multiple phoneIds, we assume that it's smart enough to handle that on its own,
1789 // and that in most cases we'd just be dumping duplicate information and bloating a report.
1790 indentPW.increaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001791 indentPW.println(prefix + " : " + targetPkgName);
1792 Set<String> dumpedPkgNames = new ArraySet<>(mServiceConnection.length);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001793 for (CarrierServiceConnection connection : mServiceConnection) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001794 if (connection == null || !SubscriptionManager.isValidPhoneId(connection.phoneId)
1795 || TextUtils.isEmpty(connection.pkgName)) {
1796 continue;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001797 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001798 final String servicePkgName = connection.pkgName;
1799 // Note: we intentionally ignore system components here because we should NOT match the
1800 // shell caller that's typically used for bug reports via non-BugreportManager triggers.
1801 final boolean exactPackageMatch = TextUtils.equals(targetPkgName, servicePkgName);
1802 final boolean carrierPrivilegesMatch =
1803 considerCarrierPrivileges && hasCarrierPrivileges(targetPkgName,
1804 connection.phoneId);
1805 if (!exactPackageMatch && !carrierPrivilegesMatch) continue;
1806 // Make sure this service is actually alive before trying to dump it. We don't pay
1807 // attention to mServiceBound[connection.phoneId] because typically carrier apps will
1808 // request long-lived bindings, and even if we unbind the app, it may still be alive due
1809 // to CarrierServiceBindHelper. Pull it out as a reference so even if it gets set to
1810 // null within the ServiceConnection during unbinding we can avoid an NPE.
1811 final IBinder service = connection.service;
1812 if (service == null || !service.isBinderAlive() || !service.pingBinder()) continue;
1813 // We've got a live service. Last check is just to make sure we don't dump a package
1814 // multiple times.
1815 if (!dumpedPkgNames.add(servicePkgName)) continue;
1816 if (!exactPackageMatch) {
1817 logd(targetPkgName + " has carrier privileges on phoneId " + connection.phoneId
1818 + ", service provided by " + servicePkgName);
1819 indentPW.increaseIndent();
1820 indentPW.println("Proxy : " + servicePkgName);
1821 indentPW.decreaseIndent();
1822 }
1823 // Flush before we let the app output anything to ensure correct ordering of output.
1824 // Internally, Binder#dump calls flush on its printer after finishing so we don't
1825 // need to do anything after.
1826 indentPW.flush();
1827 try {
1828 logd("Dumping " + servicePkgName);
1829 // We don't need to give the carrier service any args.
1830 connection.service.dump(fd, null /* args */);
1831 logd("Done with " + servicePkgName);
1832 } catch (RemoteException e) {
1833 logd("RemoteException from " + servicePkgName, e);
1834 indentPW.increaseIndent();
1835 indentPW.println("RemoteException");
1836 indentPW.increaseIndent();
1837 e.printStackTrace(indentPW);
1838 indentPW.decreaseIndent();
1839 indentPW.decreaseIndent();
1840 // We won't retry this package again because now it's in dumpedPkgNames.
1841 }
1842 indentPW.println("");
Jordan Liu46ed5db2020-01-28 08:55:20 -08001843 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001844 if (dumpedPkgNames.isEmpty()) {
1845 indentPW.increaseIndent();
1846 indentPW.println("Not bound");
1847 indentPW.decreaseIndent();
1848 indentPW.println("");
1849 }
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001850 indentPW.decreaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001851 }
1852
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001853 private boolean hasCarrierPrivileges(@NonNull String pkgName, int phoneId) {
Jack Yu00ece8c2022-11-19 22:29:12 -08001854 int subId = SubscriptionManager.getSubscriptionId(phoneId);
1855 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001856 return false;
1857 }
Jack Yu00ece8c2022-11-19 22:29:12 -08001858 return TelephonyManager.from(mContext).createForSubscriptionId(subId)
1859 .checkCarrierPrivilegesForPackage(pkgName)
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001860 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001861 }
1862
joonhunshin21a86812023-12-10 08:21:25 +00001863 /**
1864 * Get the current calling package name.
1865 * @return the current calling package name
1866 */
1867 @Nullable
1868 private String getCurrentPackageName() {
1869 if (mPackageManager == null) return null;
1870 String[] callingUids = mPackageManager.getPackagesForUid(Binder.getCallingUid());
1871 return (callingUids == null) ? null : callingUids[0];
1872 }
1873
1874 /**
1875 * Make sure the device has required telephony feature
1876 *
1877 * @throws UnsupportedOperationException if the device does not have required telephony feature
1878 */
1879 private void enforceTelephonyFeatureWithException(@Nullable String callingPackage,
1880 @NonNull String methodName) {
1881 if (callingPackage == null || mPackageManager == null) {
1882 return;
1883 }
1884
1885 if (!mFeatureFlags.enforceTelephonyFeatureMappingForPublicApis()
1886 || !CompatChanges.isChangeEnabled(ENABLE_FEATURE_MAPPING, callingPackage,
1887 Binder.getCallingUserHandle())) {
1888 return;
1889 }
1890
1891 if (!mPackageManager.hasSystemFeature(FEATURE_TELEPHONY_SUBSCRIPTION)) {
1892 throw new UnsupportedOperationException(
1893 methodName + " is unsupported without " + FEATURE_TELEPHONY_SUBSCRIPTION);
1894 }
1895 }
1896
Zach Johnson36d7aab2015-05-22 15:43:00 -07001897 private class CarrierServiceConnection implements ServiceConnection {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001898 final int phoneId;
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001899 @NonNull final String pkgName;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001900 final int eventId;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001901 IBinder service;
1902
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001903 CarrierServiceConnection(int phoneId, @NonNull String pkgName, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001904 this.phoneId = phoneId;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001905 this.pkgName = pkgName;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001906 this.eventId = eventId;
1907 }
1908
1909 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001910 public void onServiceConnected(@NonNull ComponentName name, @NonNull IBinder service) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001911 logd("Connected to config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001912 this.service = service;
1913 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1914 }
1915
1916 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001917 public void onServiceDisconnected(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001918 logd("Disconnected from config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001919 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001920 }
1921
1922 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001923 public void onBindingDied(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001924 logd("Binding died from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001925 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001926 }
1927
1928 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001929 public void onNullBinding(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001930 logd("Null binding from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001931 this.service = null;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001932 }
1933 }
1934
1935 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1936 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001937 public void onReceive(@NonNull Context context, @NonNull Intent intent) {
Rambo Wanga27fbe52022-04-12 19:09:07 +00001938 switch (intent.getAction()) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001939 case Intent.ACTION_BOOT_COMPLETED:
1940 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1941 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001942 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001943 }
1944 }
1945
Rambo Wanga27fbe52022-04-12 19:09:07 +00001946 private class CarrierServiceChangeCallback implements
1947 TelephonyManager.CarrierPrivilegesCallback {
1948 final int mPhoneId;
1949 // CarrierPrivilegesCallback will be triggered upon registration. Filter the first callback
1950 // here since we really care of the *change* of carrier service instead of the content
1951 private boolean mHasSentServiceChangeCallback;
1952
1953 CarrierServiceChangeCallback(int phoneId) {
1954 this.mPhoneId = phoneId;
1955 this.mHasSentServiceChangeCallback = false;
1956 }
1957
1958 @Override
1959 public void onCarrierPrivilegesChanged(
1960 @androidx.annotation.NonNull Set<String> privilegedPackageNames,
1961 @androidx.annotation.NonNull Set<Integer> privilegedUids) {
1962 // Ignored, not interested here
1963 }
1964
1965 @Override
1966 public void onCarrierServiceChanged(
1967 @androidx.annotation.Nullable String carrierServicePackageName,
1968 int carrierServiceUid) {
1969 // Ignore the first callback which is triggered upon registration
1970 if (!mHasSentServiceChangeCallback) {
1971 mHasSentServiceChangeCallback = true;
1972 return;
1973 }
1974 mHandler.sendMessage(
1975 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, mPhoneId, -1,
1976 carrierServicePackageName));
1977 }
1978 }
1979
Rambo Wangab13e3e2021-04-08 15:01:06 -07001980 // Get readable string for the message code supported in this class.
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001981 @NonNull
Rambo Wangab13e3e2021-04-08 15:01:06 -07001982 private static String eventToString(int code) {
1983 switch (code) {
1984 case EVENT_CLEAR_CONFIG:
1985 return "EVENT_CLEAR_CONFIG";
1986 case EVENT_CONNECTED_TO_DEFAULT:
1987 return "EVENT_CONNECTED_TO_DEFAULT";
1988 case EVENT_CONNECTED_TO_CARRIER:
1989 return "EVENT_CONNECTED_TO_CARRIER";
1990 case EVENT_FETCH_DEFAULT_DONE:
1991 return "EVENT_FETCH_DEFAULT_DONE";
1992 case EVENT_FETCH_CARRIER_DONE:
1993 return "EVENT_FETCH_CARRIER_DONE";
1994 case EVENT_DO_FETCH_DEFAULT:
1995 return "EVENT_DO_FETCH_DEFAULT";
1996 case EVENT_DO_FETCH_CARRIER:
1997 return "EVENT_DO_FETCH_CARRIER";
1998 case EVENT_PACKAGE_CHANGED:
1999 return "EVENT_PACKAGE_CHANGED";
2000 case EVENT_BIND_DEFAULT_TIMEOUT:
2001 return "EVENT_BIND_DEFAULT_TIMEOUT";
2002 case EVENT_BIND_CARRIER_TIMEOUT:
2003 return "EVENT_BIND_CARRIER_TIMEOUT";
2004 case EVENT_CHECK_SYSTEM_UPDATE:
2005 return "EVENT_CHECK_SYSTEM_UPDATE";
2006 case EVENT_SYSTEM_UNLOCKED:
2007 return "EVENT_SYSTEM_UNLOCKED";
2008 case EVENT_FETCH_DEFAULT_TIMEOUT:
2009 return "EVENT_FETCH_DEFAULT_TIMEOUT";
2010 case EVENT_FETCH_CARRIER_TIMEOUT:
2011 return "EVENT_FETCH_CARRIER_TIMEOUT";
2012 case EVENT_SUBSCRIPTION_INFO_UPDATED:
2013 return "EVENT_SUBSCRIPTION_INFO_UPDATED";
2014 case EVENT_MULTI_SIM_CONFIG_CHANGED:
2015 return "EVENT_MULTI_SIM_CONFIG_CHANGED";
2016 case EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG:
2017 return "EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG";
2018 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE:
2019 return "EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE";
2020 case EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG:
2021 return "EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG";
2022 case EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
2023 return "EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT";
2024 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
2025 return "EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT";
2026 default:
2027 return "UNKNOWN(" + code + ")";
2028 }
2029 }
2030
Rambo Wangfe0d7c12022-04-15 03:00:32 +00002031 private void logd(@NonNull String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07002032 Log.d(LOG_TAG, msg);
2033 }
2034
Rambo Wangfe0d7c12022-04-15 03:00:32 +00002035 private void logd(@NonNull String msg, Throwable tr) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08002036 Log.d(LOG_TAG, msg, tr);
2037 }
2038
Rambo Wangfe0d7c12022-04-15 03:00:32 +00002039 private void logdWithLocalLog(@NonNull String msg) {
chen xudb04c292019-03-26 17:01:15 -07002040 Log.d(LOG_TAG, msg);
2041 mCarrierConfigLoadingLog.log(msg);
2042 }
2043
Rambo Wangfe0d7c12022-04-15 03:00:32 +00002044 private void loge(@NonNull String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07002045 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07002046 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08002047 }
2048}