blob: 47fd96e8e53fdad942616b175e7ced7eeb54967d [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;
joonhunshinf624b2a2024-04-18 04:42:12 +000050import android.os.SystemProperties;
Meng Wang97a6a462020-01-23 16:22:16 -080051import android.os.UserHandle;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070052import android.preference.PreferenceManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080053import android.service.carrier.CarrierIdentifier;
Zach Johnson36d7aab2015-05-22 15:43:00 -070054import android.service.carrier.CarrierService;
55import android.service.carrier.ICarrierService;
Rambo Wang0c386012024-02-22 22:38:17 +000056import android.telephony.AnomalyReporter;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080057import android.telephony.CarrierConfigManager;
58import android.telephony.SubscriptionManager;
Peter Wangc035ce42020-01-08 21:00:22 -080059import android.telephony.TelephonyFrameworkInitializer;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080060import android.telephony.TelephonyManager;
rambowangd63ba342022-10-02 11:21:08 -050061import android.telephony.TelephonyRegistryManager;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -080062import android.text.TextUtils;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080063import android.util.ArraySet;
chen xudb04c292019-03-26 17:01:15 -070064import android.util.LocalLog;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080065import android.util.Log;
66
Rambo Wang7e3bc122021-03-23 09:24:38 -070067import com.android.internal.annotations.VisibleForTesting;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080068import com.android.internal.telephony.ICarrierConfigLoader;
69import com.android.internal.telephony.IccCardConstants;
70import com.android.internal.telephony.Phone;
Jack Yu771c6ce2023-02-02 17:51:42 -080071import com.android.internal.telephony.PhoneConfigurationManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080072import com.android.internal.telephony.PhoneFactory;
Jeff Davidsona8e4e242018-03-15 17:16:18 -070073import com.android.internal.telephony.TelephonyPermissions;
joonhunshin21a86812023-12-10 08:21:25 +000074import com.android.internal.telephony.flags.FeatureFlags;
Jack Yue37dd262022-12-16 11:53:37 -080075import com.android.internal.telephony.subscription.SubscriptionManagerService;
Meng Wanga320b942019-11-25 11:21:26 -080076import com.android.internal.telephony.util.ArrayUtils;
Jack Yu9be55d32023-08-10 13:47:46 -070077import com.android.internal.telephony.util.TelephonyUtils;
shuoq26a3a4c2016-12-16 11:06:48 -080078import com.android.internal.util.IndentingPrintWriter;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080079
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070080import java.io.File;
Junda Liu43d723a2015-05-12 17:23:45 -070081import java.io.FileDescriptor;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070082import java.io.FileInputStream;
83import java.io.FileNotFoundException;
84import java.io.FileOutputStream;
Jonathan Basseri1f743c92015-05-15 00:19:46 -070085import java.io.FilenameFilter;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070086import java.io.IOException;
Junda Liu43d723a2015-05-12 17:23:45 -070087import java.io.PrintWriter;
Jack Yu9be55d32023-08-10 13:47:46 -070088import java.nio.file.Files;
89import java.nio.file.Paths;
90import java.nio.file.attribute.BasicFileAttributes;
91import java.text.SimpleDateFormat;
shuoq26a3a4c2016-12-16 11:06:48 -080092import java.util.ArrayList;
93import java.util.Arrays;
94import java.util.Collections;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080095import java.util.List;
Jack Yu9be55d32023-08-10 13:47:46 -070096import java.util.Locale;
rambowang14757c22022-10-03 11:54:56 -050097import java.util.Objects;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080098import java.util.Set;
Rambo Wang0c386012024-02-22 22:38:17 +000099import java.util.UUID;
Jack Yu9be55d32023-08-10 13:47:46 -0700100import java.util.stream.Collectors;
101import java.util.stream.Stream;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800102
103/**
104 * CarrierConfigLoader binds to privileged carrier apps to fetch carrier config overlays.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800105 */
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800106public class CarrierConfigLoader extends ICarrierConfigLoader.Stub {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700107 private static final String LOG_TAG = "CarrierConfigLoader";
Meng Wang33ad2bc2017-03-16 20:21:20 -0700108
Jack Yu9be55d32023-08-10 13:47:46 -0700109 private static final SimpleDateFormat TIME_FORMAT =
110 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
111
Meng Wang33ad2bc2017-03-16 20:21:20 -0700112 // Package name for platform carrier config app, bundled with system image.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000113 @NonNull private final String mPlatformCarrierConfigPackage;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800114
115 /** The singleton instance. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000116 @Nullable private static CarrierConfigLoader sInstance;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800117 // The context for phone app, passed from PhoneGlobals.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000118 @NonNull private Context mContext;
119
120 // All the states below (array indexed by phoneId) are non-null. But the member of the array
121 // is nullable, when e.g. the config for the phone is not loaded yet
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800122 // Carrier configs from default app, indexed by phoneID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000123 @NonNull private PersistableBundle[] mConfigFromDefaultApp;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800124 // Carrier configs from privileged carrier config app, indexed by phoneID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000125 @NonNull private PersistableBundle[] mConfigFromCarrierApp;
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100126 // Persistent Carrier configs that are provided via the override test API, indexed by phone ID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000127 @NonNull private PersistableBundle[] mPersistentOverrideConfigs;
Hall Liu506724b2018-10-22 18:16:14 -0700128 // Carrier configs that are provided via the override test API, indexed by phone ID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000129 @NonNull private PersistableBundle[] mOverrideConfigs;
Jayachandran C645ec612020-01-10 10:30:25 -0800130 // Carrier configs to override code default when there is no SIM inserted
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000131 @NonNull private PersistableBundle mNoSimConfig;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800132 // Service connection for binding to config app.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000133 @NonNull private CarrierServiceConnection[] mServiceConnection;
Jayachandran C645ec612020-01-10 10:30:25 -0800134 // Service connection for binding to carrier config app for no SIM config.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000135 @NonNull private CarrierServiceConnection[] mServiceConnectionForNoSimConfig;
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700136 // Whether we are bound to a service for each phone
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000137 @NonNull private boolean[] mServiceBound;
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700138 // Whether we are bound to a service for no SIM config
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000139 @NonNull private boolean[] mServiceBoundForNoSimConfig;
Sarah Chin807d5c62020-03-30 17:21:09 -0700140 // Whether we have sent config change broadcast for each phone id.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000141 @NonNull private boolean[] mHasSentConfigChange;
Sarah Chin807d5c62020-03-30 17:21:09 -0700142 // Whether the broadcast was sent from EVENT_SYSTEM_UNLOCKED, to track rebroadcasts
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000143 @NonNull private boolean[] mFromSystemUnlocked;
Rambo Wanga27fbe52022-04-12 19:09:07 +0000144 // CarrierService change monitoring
145 @NonNull private CarrierServiceChangeCallback[] mCarrierServiceChangeCallbacks;
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000146
Jack Yu771c6ce2023-02-02 17:51:42 -0800147 // Broadcast receiver for system events
Rambo Wanga27fbe52022-04-12 19:09:07 +0000148 @NonNull
149 private final BroadcastReceiver mSystemBroadcastReceiver = new ConfigLoaderBroadcastReceiver();
Jack Yu9be55d32023-08-10 13:47:46 -0700150 @NonNull private final LocalLog mCarrierConfigLoadingLog = new LocalLog(256);
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000151 // Number of phone instances (active modem count)
152 private int mNumPhones;
chen xudb04c292019-03-26 17:01:15 -0700153
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800154
155 // Message codes; see mHandler below.
Jack Yu3beaf9d2023-04-14 09:17:27 -0700156 // Request from UiccController when SIM becomes absent or error.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800157 private static final int EVENT_CLEAR_CONFIG = 0;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800158 // Has connected to default app.
159 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
160 // Has connected to carrier app.
161 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700162 // Config has been loaded from default app (or cache).
163 private static final int EVENT_FETCH_DEFAULT_DONE = 5;
164 // Config has been loaded from carrier app (or cache).
165 private static final int EVENT_FETCH_CARRIER_DONE = 6;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700166 // Attempt to fetch from default app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700167 private static final int EVENT_DO_FETCH_DEFAULT = 7;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700168 // Attempt to fetch from carrier app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700169 private static final int EVENT_DO_FETCH_CARRIER = 8;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700170 // A package has been installed, uninstalled, or updated.
171 private static final int EVENT_PACKAGE_CHANGED = 9;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700172 // Bind timed out for the default app.
173 private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
174 // Bind timed out for a carrier app.
175 private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700176 // Check if the system fingerprint has changed.
177 private static final int EVENT_CHECK_SYSTEM_UPDATE = 12;
Nanxi Chen3d670502016-03-17 16:32:09 -0700178 // Rerun carrier config binding after system is unlocked.
179 private static final int EVENT_SYSTEM_UNLOCKED = 13;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700180 // Fetching config timed out from the default app.
181 private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14;
182 // Fetching config timed out from a carrier app.
183 private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15;
Jack Yu3beaf9d2023-04-14 09:17:27 -0700184 // SubscriptionManagerService has finished updating the sub for the carrier config.
Nathan Harold48ac0972019-03-13 22:33:01 -0700185 private static final int EVENT_SUBSCRIPTION_INFO_UPDATED = 16;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700186 // Multi-SIM config changed.
187 private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 17;
Jayachandran C645ec612020-01-10 10:30:25 -0800188 // Attempt to fetch from default app or read from XML for no SIM case.
189 private static final int EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG = 18;
190 // No SIM config has been loaded from default app (or cache).
191 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE = 19;
192 // Has connected to default app for no SIM config.
193 private static final int EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG = 20;
194 // Bind timed out for the default app when trying to fetch no SIM config.
195 private static final int EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 21;
196 // Fetching config timed out from the default app for no SIM config.
197 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 22;
Rambo Wangab13e3e2021-04-08 15:01:06 -0700198 // NOTE: any new EVENT_* values must be added to method eventToString().
Jonathan Basseri930701e2015-06-11 20:56:43 -0700199
Junda Liu6cb45002016-03-22 17:18:20 -0700200 private static final int BIND_TIMEOUT_MILLIS = 30000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800201
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800202 // Keys used for saving and restoring config bundle from file.
203 private static final String KEY_VERSION = "__carrier_config_package_version__";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800204
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100205 private static final String OVERRIDE_PACKAGE_ADDITION = "-override";
206
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700207 // SharedPreferences key for last known build fingerprint.
208 private static final String KEY_FINGERPRINT = "build_fingerprint";
209
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800210 // Argument for #dump that indicates we should also call the default and specified carrier
211 // service's #dump method. In multi-SIM devices, it's possible that carrier A is on SIM 1 and
212 // carrier B is on SIM 2, in which case we should not dump carrier B's service when carrier A
213 // requested the dump.
214 private static final String DUMP_ARG_REQUESTING_PACKAGE = "--requesting-package";
215
rambowang14757c22022-10-03 11:54:56 -0500216 // Configs that should always be included when clients calls getConfig[ForSubId] with specified
217 // keys (even configs are not explicitly specified). Those configs have special purpose for the
218 // carrier config APIs to work correctly.
219 private static final String[] CONFIG_SUBSET_METADATA_KEYS = new String[] {
220 CarrierConfigManager.KEY_CARRIER_CONFIG_VERSION_STRING,
221 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL
222 };
223
Rambo Wang0c386012024-02-22 22:38:17 +0000224 // UUID to report anomaly when config changed reported with subId that map to invalid phone
225 private static final String UUID_NOTIFY_CONFIG_CHANGED_WITH_INVALID_PHONE =
226 "d81cef11-c2f1-4d76-955d-7f50e8590c48";
227
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800228 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700229 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700230 // For each phoneId, the event sequence should be:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700231 // fetch default, connected to default, fetch default (async), fetch default done,
232 // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700233 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700234 // If there is a saved config file for either the default app or the carrier app, we skip
235 // binding to the app and go straight from fetch to loaded.
236 //
237 // At any time, at most one connection is active. If events are not in this order, previous
238 // connection will be unbound, so only latest event takes effect.
239 //
240 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
241 // 1. loading from carrier app (even if read from a file)
242 // 2. loading from default app if there is no carrier app (even if read from a file)
243 // 3. clearing config (e.g. due to sim removal)
244 // 4. encountering bind or IPC error
Jonathan Basseri65273c82017-07-25 15:08:42 -0700245 private class ConfigHandler extends Handler {
Rambo Wang7e3bc122021-03-23 09:24:38 -0700246 ConfigHandler(@NonNull Looper looper) {
247 super(looper);
248 }
249
Jonathan Basseri65273c82017-07-25 15:08:42 -0700250 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000251 public void handleMessage(@NonNull Message msg) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700252 final int phoneId = msg.arg1;
Jack Yu9be55d32023-08-10 13:47:46 -0700253 logd(eventToString(msg.what) + " phoneId: " + phoneId);
Malcolm Chen5ea18532019-10-24 19:55:44 -0700254 if (!SubscriptionManager.isValidPhoneId(phoneId)
255 && msg.what != EVENT_MULTI_SIM_CONFIG_CHANGED) {
256 return;
257 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800258 switch (msg.what) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800259 case EVENT_CLEAR_CONFIG: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700260 clearConfigForPhone(phoneId, true);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800261 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700262 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700263
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800264 case EVENT_SYSTEM_UNLOCKED: {
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000265 for (int i = 0; i < mNumPhones; ++i) {
Sarah Chin807d5c62020-03-30 17:21:09 -0700266 // When the user unlocks the device, send the broadcast again (with a
267 // rebroadcast extra) if we have sent it before unlock. This will avoid
268 // trying to load the carrier config when the SIM is still loading when the
269 // unlock happens.
Junda Liu03aad762017-07-21 13:32:17 -0700270 if (mHasSentConfigChange[i]) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800271 logdWithLocalLog("System unlocked");
Sarah Chin807d5c62020-03-30 17:21:09 -0700272 mFromSystemUnlocked[i] = true;
Junda Liu03aad762017-07-21 13:32:17 -0700273 updateConfigForPhoneId(i);
274 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700275 }
276 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700277 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700278
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800279 case EVENT_PACKAGE_CHANGED: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700280 final String carrierPackageName = (String) msg.obj;
Rambo Wanga27fbe52022-04-12 19:09:07 +0000281 // Always clear up the cache and re-load config from scratch since the carrier
282 // service change is reliable and specific to the phoneId now.
283 clearCachedConfigForPackage(carrierPackageName);
284 logdWithLocalLog("Package changed: " + carrierPackageName
285 + ", phone=" + phoneId);
286 updateConfigForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700287 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700288 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700289
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800290 case EVENT_DO_FETCH_DEFAULT: {
Rambo Wang9bc7d362021-03-09 09:10:58 -0800291 // Clear in-memory cache for carrier app config, so when carrier app gets
292 // uninstalled, no stale config is left.
293 if (mConfigFromCarrierApp[phoneId] != null
294 && getCarrierPackageForPhoneId(phoneId) == null) {
295 mConfigFromCarrierApp[phoneId] = null;
296 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100297 // Restore persistent override values.
298 PersistableBundle config = restoreConfigFromXml(
299 mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId);
300 if (config != null) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100301 mPersistentOverrideConfigs[phoneId] = config;
302 }
303
304 config = restoreConfigFromXml(mPlatformCarrierConfigPackage, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700305 if (config != null) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700306 mConfigFromDefaultApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700307 Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700308 newMsg.getData().putBoolean("loaded_from_xml", true);
309 mHandler.sendMessage(newMsg);
310 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700311 // No cached config, so fetch it from the default app.
312 if (bindToConfigPackage(
313 mPlatformCarrierConfigPackage,
314 phoneId,
315 EVENT_CONNECTED_TO_DEFAULT)) {
316 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800317 obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1 /*arg2*/,
318 getMessageToken(phoneId)),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700319 BIND_TIMEOUT_MILLIS);
320 } else {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800321 // Put a stub bundle in place so that the rest of the logic continues
322 // smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000323 mConfigFromDefaultApp[phoneId] = new PersistableBundle();
Jonathan Basseri65273c82017-07-25 15:08:42 -0700324 // Send broadcast if bind fails.
Jack Yue37dd262022-12-16 11:53:37 -0800325 updateSubscriptionDatabase(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700326 // TODO: We *must* call unbindService even if bindService returns false.
327 // (And possibly if SecurityException was thrown.)
chen xudb04c292019-03-26 17:01:15 -0700328 loge("binding to default app: "
329 + mPlatformCarrierConfigPackage + " fails");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700330 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800331 }
332 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700333 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800334
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800335 case EVENT_CONNECTED_TO_DEFAULT: {
Rambo Wang610fdf62021-03-08 21:37:11 -0800336 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT, getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700337 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800338 // If new service connection has been created, unbind.
339 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700340 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800341 break;
342 }
chen xu02581692018-11-11 19:03:44 -0800343 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700344 // ResultReceiver callback will execute in this Handler's thread.
345 final ResultReceiver resultReceiver =
346 new ResultReceiver(this) {
347 @Override
348 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700349 unbindIfBound(mContext, conn, phoneId);
Rambo Wanga9d27a52021-04-09 16:17:33 -0700350 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT,
351 getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700352 // If new service connection has been created, this is stale.
353 if (mServiceConnection[phoneId] != conn) {
354 loge("Received response for stale request.");
355 return;
356 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700357 if (resultCode == RESULT_ERROR || resultData == null) {
358 // On error, abort config fetching.
359 loge("Failed to get carrier config");
Jack Yue37dd262022-12-16 11:53:37 -0800360 updateSubscriptionDatabase(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700361 return;
362 }
363 PersistableBundle config =
364 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100365 saveConfigToXml(mPlatformCarrierConfigPackage, "", phoneId,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800366 carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700367 mConfigFromDefaultApp[phoneId] = config;
368 sendMessage(
369 obtainMessage(
370 EVENT_FETCH_DEFAULT_DONE, phoneId, -1));
371 }
372 };
373 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800374 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700375 ICarrierService carrierService =
376 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800377 carrierService.getCarrierConfig(phoneId, carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800378 logdWithLocalLog("Fetch config for default app: "
chen xudb04c292019-03-26 17:01:15 -0700379 + mPlatformCarrierConfigPackage
Jack Yu9be55d32023-08-10 13:47:46 -0700380 + ", carrierId=" + carrierId.getSpecificCarrierId());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700381 } catch (RemoteException e) {
chen xudb04c292019-03-26 17:01:15 -0700382 loge("Failed to get carrier config from default app: " +
Jack Yu9be55d32023-08-10 13:47:46 -0700383 mPlatformCarrierConfigPackage + " err: " + e);
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700384 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700385 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800386 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700387 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800388 obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1 /*arg2*/,
389 getMessageToken(phoneId)),
Jonathan Basseri65273c82017-07-25 15:08:42 -0700390 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800391 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700392 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800393
Jonathan Basseri930701e2015-06-11 20:56:43 -0700394 case EVENT_BIND_DEFAULT_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800395 case EVENT_FETCH_DEFAULT_TIMEOUT: {
396 loge("Bind/fetch time out from " + mPlatformCarrierConfigPackage);
Rambo Wang610fdf62021-03-08 21:37:11 -0800397 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT, getMessageToken(phoneId));
chen xuf60b3162019-05-01 21:31:05 -0700398 // If we attempted to bind to the app, but the service connection is null due to
399 // the race condition that clear config event happens before bind/fetch complete
400 // then config was cleared while we were waiting and we should not continue.
401 if (mServiceConnection[phoneId] != null) {
402 // If a ResponseReceiver callback is in the queue when this happens, we will
403 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700404 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700405 broadcastConfigChangedIntent(phoneId);
406 }
Nathan Harold1122e3d2021-01-22 15:45:24 -0800407 // Put a stub bundle in place so that the rest of the logic continues smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000408 mConfigFromDefaultApp[phoneId] = new PersistableBundle();
Jack Yue37dd262022-12-16 11:53:37 -0800409 updateSubscriptionDatabase(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700410 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700411 }
Jonathan Basseri930701e2015-06-11 20:56:43 -0700412
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800413 case EVENT_FETCH_DEFAULT_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700414 // If we attempted to bind to the app, but the service connection is null, then
415 // config was cleared while we were waiting and we should not continue.
416 if (!msg.getData().getBoolean("loaded_from_xml", false)
417 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800418 break;
419 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700420 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700421 if (carrierPackageName != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800422 logd("Found carrier config app: " + carrierPackageName);
Jordan Liu38a614c2019-03-20 15:01:17 -0700423 sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId, -1));
Jonathan Basserib919e932015-05-27 16:53:08 +0000424 } else {
Jack Yue37dd262022-12-16 11:53:37 -0800425 updateSubscriptionDatabase(phoneId);
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700426 }
427 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700428 }
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700429
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800430 case EVENT_DO_FETCH_CARRIER: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700431 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700432 final PersistableBundle config =
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100433 restoreConfigFromXml(carrierPackageName, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700434 if (config != null) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700435 mConfigFromCarrierApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700436 Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700437 newMsg.getData().putBoolean("loaded_from_xml", true);
438 sendMessage(newMsg);
439 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700440 // No cached config, so fetch it from a carrier app.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800441 if (carrierPackageName != null && bindToConfigPackage(carrierPackageName,
442 phoneId, EVENT_CONNECTED_TO_CARRIER)) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700443 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800444 obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1 /*arg2*/,
445 getMessageToken(phoneId)),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700446 BIND_TIMEOUT_MILLIS);
447 } else {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800448 // Put a stub bundle in place so that the rest of the logic continues
449 // smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000450 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
Jonathan Basseri65273c82017-07-25 15:08:42 -0700451 // Send broadcast if bind fails.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700452 broadcastConfigChangedIntent(phoneId);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800453 loge("Bind to carrier app: " + carrierPackageName + " fails");
Jack Yue37dd262022-12-16 11:53:37 -0800454 updateSubscriptionDatabase(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700455 }
456 }
457 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700458 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700459
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800460 case EVENT_CONNECTED_TO_CARRIER: {
Rambo Wang610fdf62021-03-08 21:37:11 -0800461 removeMessages(EVENT_BIND_CARRIER_TIMEOUT, getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700462 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800463 // If new service connection has been created, unbind.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700464 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700465 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800466 break;
467 }
chen xu02581692018-11-11 19:03:44 -0800468 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700469 // ResultReceiver callback will execute in this Handler's thread.
470 final ResultReceiver resultReceiver =
471 new ResultReceiver(this) {
472 @Override
473 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700474 unbindIfBound(mContext, conn, phoneId);
Rambo Wanga9d27a52021-04-09 16:17:33 -0700475 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT,
476 getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700477 // If new service connection has been created, this is stale.
478 if (mServiceConnection[phoneId] != conn) {
479 loge("Received response for stale request.");
480 return;
481 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700482 if (resultCode == RESULT_ERROR || resultData == null) {
483 // On error, abort config fetching.
chen xudb04c292019-03-26 17:01:15 -0700484 loge("Failed to get carrier config from carrier app: "
485 + getCarrierPackageForPhoneId(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700486 broadcastConfigChangedIntent(phoneId);
Jack Yue37dd262022-12-16 11:53:37 -0800487 updateSubscriptionDatabase(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700488 return;
489 }
490 PersistableBundle config =
491 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100492 saveConfigToXml(getCarrierPackageForPhoneId(phoneId), "",
493 phoneId, carrierId, config);
Amit Mahajan304e92b2021-04-08 14:03:30 -0700494 if (config != null) {
495 mConfigFromCarrierApp[phoneId] = config;
496 } else {
497 logdWithLocalLog("Config from carrier app is null "
498 + "for phoneId " + phoneId);
499 // Put a stub bundle in place so that the rest of the logic
500 // continues smoothly.
501 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
502 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700503 sendMessage(
504 obtainMessage(
505 EVENT_FETCH_CARRIER_DONE, phoneId, -1));
506 }
507 };
508 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800509 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700510 ICarrierService carrierService =
511 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800512 carrierService.getCarrierConfig(phoneId, carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800513 logdWithLocalLog("Fetch config for carrier app: "
chen xudb04c292019-03-26 17:01:15 -0700514 + getCarrierPackageForPhoneId(phoneId)
Jack Yu9be55d32023-08-10 13:47:46 -0700515 + ", carrierId=" + carrierId.getSpecificCarrierId());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700516 } catch (RemoteException e) {
Jack Yu9be55d32023-08-10 13:47:46 -0700517 loge("Failed to get carrier config: " + e);
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700518 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700519 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800520 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700521 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800522 obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1 /*arg2*/,
523 getMessageToken(phoneId)),
Jonathan Basseri65273c82017-07-25 15:08:42 -0700524 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800525 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700526 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800527
Jonathan Basseri930701e2015-06-11 20:56:43 -0700528 case EVENT_BIND_CARRIER_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800529 case EVENT_FETCH_CARRIER_TIMEOUT: {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800530 loge("Bind/fetch from carrier app timeout, package="
531 + getCarrierPackageForPhoneId(phoneId));
Rambo Wang610fdf62021-03-08 21:37:11 -0800532 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT, getMessageToken(phoneId));
chen xuf60b3162019-05-01 21:31:05 -0700533 // If we attempted to bind to the app, but the service connection is null due to
534 // the race condition that clear config event happens before bind/fetch complete
535 // then config was cleared while we were waiting and we should not continue.
536 if (mServiceConnection[phoneId] != null) {
537 // If a ResponseReceiver callback is in the queue when this happens, we will
538 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700539 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700540 broadcastConfigChangedIntent(phoneId);
541 }
Nathan Harold1122e3d2021-01-22 15:45:24 -0800542 // Put a stub bundle in place so that the rest of the logic continues smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000543 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
Jack Yue37dd262022-12-16 11:53:37 -0800544 updateSubscriptionDatabase(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700545 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700546 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800547 case EVENT_FETCH_CARRIER_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700548 // If we attempted to bind to the app, but the service connection is null, then
549 // config was cleared while we were waiting and we should not continue.
550 if (!msg.getData().getBoolean("loaded_from_xml", false)
551 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800552 break;
553 }
Jack Yue37dd262022-12-16 11:53:37 -0800554 updateSubscriptionDatabase(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800555 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700556 }
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700557
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800558 case EVENT_CHECK_SYSTEM_UPDATE: {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700559 SharedPreferences sharedPrefs =
560 PreferenceManager.getDefaultSharedPreferences(mContext);
561 final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
562 if (!Build.FINGERPRINT.equals(lastFingerprint)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800563 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700564 "Build fingerprint changed. old: "
565 + lastFingerprint
566 + " new: "
567 + Build.FINGERPRINT);
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700568 clearCachedConfigForPackage(null);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700569 sharedPrefs
570 .edit()
571 .putString(KEY_FINGERPRINT, Build.FINGERPRINT)
572 .apply();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700573 }
574 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700575 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700576
577 case EVENT_SUBSCRIPTION_INFO_UPDATED:
578 broadcastConfigChangedIntent(phoneId);
579 break;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700580 case EVENT_MULTI_SIM_CONFIG_CHANGED:
581 onMultiSimConfigChanged();
582 break;
Jayachandran C645ec612020-01-10 10:30:25 -0800583
584 case EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG: {
585 PersistableBundle config =
586 restoreNoSimConfigFromXml(mPlatformCarrierConfigPackage);
587
588 if (config != null) {
Jayachandran C645ec612020-01-10 10:30:25 -0800589 mNoSimConfig = config;
590 sendMessage(
591 obtainMessage(
592 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE,
593 phoneId, -1));
594 } else {
595 // No cached config, so fetch it from the default app.
596 if (bindToConfigPackage(
597 mPlatformCarrierConfigPackage,
598 phoneId,
599 EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG)) {
600 sendMessageDelayed(
601 obtainMessage(
602 EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT,
603 phoneId, -1), BIND_TIMEOUT_MILLIS);
604 } else {
605 broadcastConfigChangedIntent(phoneId, false);
606 // TODO: We *must* call unbindService even if bindService returns false.
607 // (And possibly if SecurityException was thrown.)
608 loge("binding to default app to fetch no SIM config: "
609 + mPlatformCarrierConfigPackage + " fails");
610 }
611 }
612 break;
613 }
614
615 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE: {
616 broadcastConfigChangedIntent(phoneId, false);
617 break;
618 }
619
620 case EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
621 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT: {
622 loge("Bind/fetch time out for no SIM config from "
623 + mPlatformCarrierConfigPackage);
624 removeMessages(EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
625 // If we attempted to bind to the app, but the service connection is null due to
626 // the race condition that clear config event happens before bind/fetch complete
627 // then config was cleared while we were waiting and we should not continue.
628 if (mServiceConnectionForNoSimConfig[phoneId] != null) {
629 // If a ResponseReceiver callback is in the queue when this happens, we will
630 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700631 unbindIfBoundForNoSimConfig(mContext,
632 mServiceConnectionForNoSimConfig[phoneId], phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800633 }
634 broadcastConfigChangedIntent(phoneId, false);
635 break;
636 }
637
638 case EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG: {
639 removeMessages(EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
640 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
641 // If new service connection has been created, unbind.
642 if (mServiceConnectionForNoSimConfig[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700643 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800644 break;
645 }
646
647 // ResultReceiver callback will execute in this Handler's thread.
648 final ResultReceiver resultReceiver =
649 new ResultReceiver(this) {
650 @Override
651 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700652 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800653 // If new service connection has been created, this is stale.
654 if (mServiceConnectionForNoSimConfig[phoneId] != conn) {
655 loge("Received response for stale request.");
656 return;
657 }
658 removeMessages(EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
659 if (resultCode == RESULT_ERROR || resultData == null) {
660 // On error, abort config fetching.
661 loge("Failed to get no SIM carrier config");
662 return;
663 }
664 PersistableBundle config =
665 resultData.getParcelable(KEY_CONFIG_BUNDLE);
666 saveNoSimConfigToXml(mPlatformCarrierConfigPackage, config);
667 mNoSimConfig = config;
668 sendMessage(
669 obtainMessage(
670 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE,
671 phoneId, -1));
672 }
673 };
674 // Now fetch the config asynchronously from the ICarrierService.
675 try {
676 ICarrierService carrierService =
677 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800678 carrierService.getCarrierConfig(phoneId, null, resultReceiver);
Jayachandran C645ec612020-01-10 10:30:25 -0800679 logdWithLocalLog("Fetch no sim config from default app: "
680 + mPlatformCarrierConfigPackage);
681 } catch (RemoteException e) {
682 loge("Failed to get no sim carrier config from default app: " +
Jack Yu9be55d32023-08-10 13:47:46 -0700683 mPlatformCarrierConfigPackage + " err: " + e);
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700684 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800685 break; // So we don't set a timeout.
686 }
687 sendMessageDelayed(
688 obtainMessage(
689 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT,
690 phoneId, -1), BIND_TIMEOUT_MILLIS);
691 break;
692 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800693 }
694 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700695 }
696
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000697 @NonNull private final Handler mHandler;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800698
joonhunshin21a86812023-12-10 08:21:25 +0000699 @NonNull private final FeatureFlags mFeatureFlags;
700
701 @NonNull private final PackageManager mPackageManager;
joonhunshinf624b2a2024-04-18 04:42:12 +0000702 private final int mVendorApiLevel;
joonhunshin21a86812023-12-10 08:21:25 +0000703
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800704 /**
705 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
706 * receiver for relevant events.
707 */
Rambo Wang7e3bc122021-03-23 09:24:38 -0700708 @VisibleForTesting
joonhunshin21a86812023-12-10 08:21:25 +0000709 /* package */ CarrierConfigLoader(@NonNull Context context, @NonNull Looper looper,
710 @NonNull FeatureFlags featureFlags) {
mattgilbride5b6b7182023-04-03 18:56:44 +0000711 super(PermissionEnforcer.fromContext(context));
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800712 mContext = context;
Meng Wang33ad2bc2017-03-16 20:21:20 -0700713 mPlatformCarrierConfigPackage =
714 mContext.getString(R.string.platform_carrier_config_package);
Rambo Wang7e3bc122021-03-23 09:24:38 -0700715 mHandler = new ConfigHandler(looper);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800716
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000717 IntentFilter systemEventsFilter = new IntentFilter();
718 systemEventsFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
Rambo Wanga27fbe52022-04-12 19:09:07 +0000719 context.registerReceiver(mSystemBroadcastReceiver, systemEventsFilter);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800720
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000721 mNumPhones = TelephonyManager.from(context).getActiveModemCount();
722 mConfigFromDefaultApp = new PersistableBundle[mNumPhones];
723 mConfigFromCarrierApp = new PersistableBundle[mNumPhones];
724 mPersistentOverrideConfigs = new PersistableBundle[mNumPhones];
725 mOverrideConfigs = new PersistableBundle[mNumPhones];
Rambo Wang42026112021-04-07 20:57:28 +0000726 mNoSimConfig = new PersistableBundle();
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000727 mServiceConnection = new CarrierServiceConnection[mNumPhones];
728 mServiceBound = new boolean[mNumPhones];
729 mHasSentConfigChange = new boolean[mNumPhones];
730 mFromSystemUnlocked = new boolean[mNumPhones];
731 mServiceConnectionForNoSimConfig = new CarrierServiceConnection[mNumPhones];
732 mServiceBoundForNoSimConfig = new boolean[mNumPhones];
Rambo Wanga27fbe52022-04-12 19:09:07 +0000733 mCarrierServiceChangeCallbacks = new CarrierServiceChangeCallback[mNumPhones];
734 for (int phoneId = 0; phoneId < mNumPhones; phoneId++) {
735 mCarrierServiceChangeCallbacks[phoneId] = new CarrierServiceChangeCallback(phoneId);
736 TelephonyManager.from(context).registerCarrierPrivilegesCallback(phoneId,
737 new HandlerExecutor(mHandler), mCarrierServiceChangeCallbacks[phoneId]);
738 }
joonhunshin21a86812023-12-10 08:21:25 +0000739 mFeatureFlags = featureFlags;
740 mPackageManager = context.getPackageManager();
joonhunshinf624b2a2024-04-18 04:42:12 +0000741 mVendorApiLevel = SystemProperties.getInt(
742 "ro.vendor.api_level", Build.VERSION.DEVICE_INITIAL_SDK_INT);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800743 logd("CarrierConfigLoader has started");
Jack Yu771c6ce2023-02-02 17:51:42 -0800744
745 PhoneConfigurationManager.registerForMultiSimConfigChange(
746 mHandler, EVENT_MULTI_SIM_CONFIG_CHANGED, null);
747
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700748 mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800749 }
750
751 /**
752 * Initialize the singleton CarrierConfigLoader instance.
753 *
754 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
755 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000756 @NonNull
joonhunshin21a86812023-12-10 08:21:25 +0000757 /* package */ static CarrierConfigLoader init(@NonNull Context context,
758 @NonNull FeatureFlags featureFlags) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800759 synchronized (CarrierConfigLoader.class) {
760 if (sInstance == null) {
joonhunshin21a86812023-12-10 08:21:25 +0000761 sInstance = new CarrierConfigLoader(context, Looper.myLooper(), featureFlags);
Brad Ebingerfa6575f2021-05-04 00:29:50 +0000762 // Make this service available through ServiceManager.
763 TelephonyFrameworkInitializer.getTelephonyServiceManager()
764 .getCarrierConfigServiceRegisterer().register(sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800765 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700766 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800767 }
768 return sInstance;
769 }
770 }
771
Rambo Wang7e3bc122021-03-23 09:24:38 -0700772 @VisibleForTesting
773 /* package */ void clearConfigForPhone(int phoneId, boolean fetchNoSimConfig) {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700774 /* Ignore clear configuration request if device is being shutdown. */
775 Phone phone = PhoneFactory.getPhone(phoneId);
776 if (phone != null) {
777 if (phone.isShuttingDown()) {
778 return;
779 }
780 }
781
Jack Yu771c6ce2023-02-02 17:51:42 -0800782 if (mConfigFromDefaultApp.length <= phoneId) {
783 Log.wtf(LOG_TAG, "Invalid phone id " + phoneId);
784 return;
785 }
786
Malcolm Chen5ea18532019-10-24 19:55:44 -0700787 mConfigFromDefaultApp[phoneId] = null;
788 mConfigFromCarrierApp[phoneId] = null;
789 mServiceConnection[phoneId] = null;
790 mHasSentConfigChange[phoneId] = false;
791
Jayachandran C645ec612020-01-10 10:30:25 -0800792 if (fetchNoSimConfig) {
793 // To fetch no SIM config
794 mHandler.sendMessage(
795 mHandler.obtainMessage(
796 EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG, phoneId, -1));
797 }
Malcolm Chen5ea18532019-10-24 19:55:44 -0700798 }
799
Jack Yue37dd262022-12-16 11:53:37 -0800800 private void updateSubscriptionDatabase(int phoneId) {
Jack Yu90813e62023-01-17 17:46:27 -0800801 logd("updateSubscriptionDatabase: phoneId=" + phoneId);
Jack Yue37dd262022-12-16 11:53:37 -0800802 String configPackageName;
Nathan Harold48ac0972019-03-13 22:33:01 -0700803 PersistableBundle configToSend;
804 int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
805 // Prefer the carrier privileged carrier app, but if there is not one, use the platform
806 // default carrier app.
807 if (mConfigFromCarrierApp[phoneId] != null) {
Jack Yue37dd262022-12-16 11:53:37 -0800808 configPackageName = getCarrierPackageForPhoneId(phoneId);
Nathan Harold48ac0972019-03-13 22:33:01 -0700809 configToSend = mConfigFromCarrierApp[phoneId];
810 } else {
Jack Yue37dd262022-12-16 11:53:37 -0800811 configPackageName = mPlatformCarrierConfigPackage;
Nathan Harold48ac0972019-03-13 22:33:01 -0700812 configToSend = mConfigFromDefaultApp[phoneId];
813 }
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700814
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800815 if (configToSend == null) {
Rambo Wang42026112021-04-07 20:57:28 +0000816 configToSend = new PersistableBundle();
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800817 }
818
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700819 // mOverrideConfigs is for testing. And it will override current configs.
820 PersistableBundle config = mOverrideConfigs[phoneId];
821 if (config != null) {
Torbjorn Eklund1050cb02018-11-16 14:05:38 +0100822 configToSend = new PersistableBundle(configToSend);
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700823 configToSend.putAll(config);
824 }
825
Jack Yu3beaf9d2023-04-14 09:17:27 -0700826 SubscriptionManagerService.getInstance().updateSubscriptionByCarrierConfig(
827 phoneId, configPackageName, configToSend,
828 () -> mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1)
829 .sendToTarget());
Nathan Harold48ac0972019-03-13 22:33:01 -0700830 }
831
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800832 private void broadcastConfigChangedIntent(int phoneId) {
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800833 broadcastConfigChangedIntent(phoneId, true);
834 }
835
836 private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) {
rambowangd63ba342022-10-02 11:21:08 -0500837 int subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
838 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
839 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
840
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800841 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800842 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
chen xuf9db49f2019-03-31 23:04:42 -0700843 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800844 if (addSubIdExtra) {
rambowangd63ba342022-10-02 11:21:08 -0500845 int simApplicationState = getSimApplicationStateForPhone(phoneId);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800846 // Include subId/carrier id extra only if SIM records are loaded
847 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
848 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
rambowangd63ba342022-10-02 11:21:08 -0500849 subId = SubscriptionManager.getSubscriptionId(phoneId);
850 carrierId = getCarrierIdForPhoneId(phoneId);
851 specificCarrierId = getSpecificCarrierIdForPhoneId(phoneId);
852 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID, specificCarrierId);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800853 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
rambowangd63ba342022-10-02 11:21:08 -0500854 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, carrierId);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800855 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800856 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800857 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Sarah Chin807d5c62020-03-30 17:21:09 -0700858 intent.putExtra(CarrierConfigManager.EXTRA_REBROADCAST_ON_UNLOCK,
859 mFromSystemUnlocked[phoneId]);
rambowangd63ba342022-10-02 11:21:08 -0500860
861 TelephonyRegistryManager trm = mContext.getSystemService(TelephonyRegistryManager.class);
862 // Unlike broadcast, we wouldn't notify registrants on carrier config change when device is
863 // unlocked. Only real carrier config change will send the notification to registrants.
864 if (trm != null && !mFromSystemUnlocked[phoneId]) {
865 trm.notifyCarrierConfigChanged(phoneId, subId, carrierId, specificCarrierId);
866 }
867
Meng Wang97a6a462020-01-23 16:22:16 -0800868 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
rambowangd63ba342022-10-02 11:21:08 -0500869
Jack Yu00ece8c2022-11-19 22:29:12 -0800870 if (SubscriptionManager.isValidSubscriptionId(subId)) {
871 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId + ", subId=" + subId);
Jack Yue9e95be2020-03-22 10:56:06 -0700872 } else {
873 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
874 }
Junda Liu03aad762017-07-21 13:32:17 -0700875 mHasSentConfigChange[phoneId] = true;
Sarah Chin807d5c62020-03-30 17:21:09 -0700876 mFromSystemUnlocked[phoneId] = false;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800877 }
878
rambowangd63ba342022-10-02 11:21:08 -0500879 private int getSimApplicationStateForPhone(int phoneId) {
880 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
881 int subId = SubscriptionManager.getSubscriptionId(phoneId);
882 if (SubscriptionManager.isValidSubscriptionId(subId)) {
883 TelephonyManager telMgr = TelephonyManager.from(mContext)
884 .createForSubscriptionId(subId);
885 simApplicationState = telMgr.getSimApplicationState();
886 }
887 return simApplicationState;
888 }
889
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800890 /** Binds to the default or carrier config app. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000891 private boolean bindToConfigPackage(@NonNull String pkgName, int phoneId, int eventId) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800892 logdWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700893 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700894 carrierService.setPackage(pkgName);
Jayachandran C645ec612020-01-10 10:30:25 -0800895 CarrierServiceConnection serviceConnection = new CarrierServiceConnection(
896 phoneId, pkgName, eventId);
897 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
898 mServiceConnectionForNoSimConfig[phoneId] = serviceConnection;
899 } else {
900 mServiceConnection[phoneId] = serviceConnection;
901 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800902 try {
Jayachandran C645ec612020-01-10 10:30:25 -0800903 if (mContext.bindService(carrierService, serviceConnection,
Jordan Liu178430d2020-02-10 14:32:15 -0800904 Context.BIND_AUTO_CREATE)) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700905 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
906 mServiceBoundForNoSimConfig[phoneId] = true;
907 } else {
908 mServiceBound[phoneId] = true;
909 }
Jordan Liu178430d2020-02-10 14:32:15 -0800910 return true;
911 } else {
912 return false;
913 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800914 } catch (SecurityException ex) {
915 return false;
916 }
917 }
918
Rambo Wang7e3bc122021-03-23 09:24:38 -0700919 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000920 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -0700921 /* package */ CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800922 String mcc = "";
923 String mnc = "";
924 String imsi = "";
925 String gid1 = "";
926 String gid2 = "";
927 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
928 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800929 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800930 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700931 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
932 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800933 mcc = simOperator.substring(0, 3);
934 mnc = simOperator.substring(3);
935 }
936 Phone phone = PhoneFactory.getPhone(phoneId);
937 if (phone != null) {
938 imsi = phone.getSubscriberId();
939 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700940 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800941 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800942 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800943 }
chen xua31f22b2019-03-06 15:28:50 -0800944 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800945 }
946
Rambo Wangf7c214a2022-03-17 12:36:22 -0700947 /** Returns the package name of a privileged carrier app, or null if there is none. */
Nathan Harold1122e3d2021-01-22 15:45:24 -0800948 @Nullable
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700949 private String getCarrierPackageForPhoneId(int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -0700950 final long token = Binder.clearCallingIdentity();
951 try {
Rambo Wangf7c214a2022-03-17 12:36:22 -0700952 return TelephonyManager.from(mContext)
953 .getCarrierServicePackageNameForLogicalSlot(phoneId);
Nazanin1adf4562021-03-29 15:35:30 -0700954 } finally {
955 Binder.restoreCallingIdentity(token);
956 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700957 }
958
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000959 @Nullable
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700960 private String getIccIdForPhoneId(int phoneId) {
961 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
962 return null;
963 }
964 Phone phone = PhoneFactory.getPhone(phoneId);
965 if (phone == null) {
966 return null;
967 }
968 return phone.getIccSerialNumber();
969 }
970
chen xu02581692018-11-11 19:03:44 -0800971 /**
chen xua31f22b2019-03-06 15:28:50 -0800972 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800973 */
chen xua31f22b2019-03-06 15:28:50 -0800974 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700975 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800976 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700977 }
978 Phone phone = PhoneFactory.getPhone(phoneId);
979 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800980 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700981 }
chen xua31f22b2019-03-06 15:28:50 -0800982 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800983 }
984
985 /**
986 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
987 */
988 private int getCarrierIdForPhoneId(int phoneId) {
989 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
990 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700991 }
chen xu02581692018-11-11 19:03:44 -0800992 Phone phone = PhoneFactory.getPhone(phoneId);
993 if (phone == null) {
994 return TelephonyManager.UNKNOWN_CARRIER_ID;
995 }
996 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700997 }
998
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700999 /**
1000 * Writes a bundle to an XML file.
1001 *
chen xu02581692018-11-11 19:03:44 -08001002 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -08001003 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -08001004 * should have a single copy of XML file named after carrier id. However, it's still possible
1005 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
1006 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
1007 * carrier while iccid remains the same.
1008 *
1009 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
1010 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001011 *
1012 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001013 *
Jayachandran C645ec612020-01-10 10:30:25 -08001014 * @param packageName the name of the package from which we fetched this bundle.
1015 * @param extraString An extra string to be used in the XML file name.
1016 * @param phoneId the phone ID.
1017 * @param carrierId contains all carrier-identifying information.
1018 * @param config the bundle to be written. Null will be treated as an empty bundle.
1019 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001020 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001021 private void saveConfigToXml(@Nullable String packageName, @NonNull String extraString,
1022 int phoneId, @Nullable CarrierIdentifier carrierId, @NonNull PersistableBundle config,
1023 boolean isNoSimConfig) {
Jayachandran C645ec612020-01-10 10:30:25 -08001024 if (packageName == null) {
Jack Yu9be55d32023-08-10 13:47:46 -07001025 loge("Cannot save config with null packageName. phoneId=" + phoneId);
Yuchen Dongc15afed2018-04-26 17:05:22 +08001026 return;
1027 }
1028
Jayachandran C645ec612020-01-10 10:30:25 -08001029 String fileName;
1030 if (isNoSimConfig) {
1031 fileName = getFilenameForNoSimConfig(packageName);
1032 } else {
Jack Yuf5badd92022-12-08 00:50:53 -08001033 if (TelephonyManager.getSimStateForSlotIndex(phoneId)
Jayachandran C645ec612020-01-10 10:30:25 -08001034 != TelephonyManager.SIM_STATE_LOADED) {
Jack Yu9be55d32023-08-10 13:47:46 -07001035 loge("Skip saving config because SIM records are not loaded. phoneId=" + phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001036 return;
1037 }
1038
1039 final String iccid = getIccIdForPhoneId(phoneId);
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001040 final int cid = carrierId != null ? carrierId.getSpecificCarrierId()
1041 : TelephonyManager.UNKNOWN_CARRIER_ID;
Jayachandran C645ec612020-01-10 10:30:25 -08001042 if (iccid == null) {
Jack Yu9be55d32023-08-10 13:47:46 -07001043 loge("Cannot save config with null iccid. phoneId=" + phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001044 return;
1045 }
1046 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001047 }
Jayachandran C645ec612020-01-10 10:30:25 -08001048
Junda Liu02596502016-11-15 13:46:43 -08001049 // b/32668103 Only save to file if config isn't empty.
1050 // In case of failure, not caching an empty bundle will
1051 // try loading config again on next power on or sim loaded.
1052 // Downside is for genuinely empty bundle, will bind and load
1053 // on every power on.
1054 if (config == null || config.isEmpty()) {
1055 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001056 }
1057
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001058 final String version = getPackageVersion(packageName);
1059 if (version == null) {
Jack Yu9be55d32023-08-10 13:47:46 -07001060 loge("Failed to get package version for: " + packageName + ", phoneId=" + phoneId);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001061 return;
1062 }
1063
Jack Yu9be55d32023-08-10 13:47:46 -07001064 logdWithLocalLog("Save carrier config to cache. phoneId=" + phoneId
1065 + ", xml=" + getFilePathForLogging(fileName) + ", version=" + version);
chen xudb04c292019-03-26 17:01:15 -07001066
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001067 FileOutputStream outFile = null;
1068 try {
Jayachandran C645ec612020-01-10 10:30:25 -08001069 outFile = new FileOutputStream(new File(mContext.getFilesDir(), fileName));
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001070 config.putString(KEY_VERSION, version);
1071 config.writeToStream(outFile);
1072 outFile.flush();
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001073 outFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001074 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001075 loge(e.toString());
1076 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001077 }
1078
Rambo Wang7e3bc122021-03-23 09:24:38 -07001079 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001080 /* package */ void saveConfigToXml(@Nullable String packageName, @NonNull String extraString,
1081 int phoneId, @NonNull CarrierIdentifier carrierId, @NonNull PersistableBundle config) {
Jayachandran C645ec612020-01-10 10:30:25 -08001082 saveConfigToXml(packageName, extraString, phoneId, carrierId, config, false);
1083 }
1084
Rambo Wang7e3bc122021-03-23 09:24:38 -07001085 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001086 /* package */ void saveNoSimConfigToXml(@Nullable String packageName,
1087 @NonNull PersistableBundle config) {
Jayachandran C645ec612020-01-10 10:30:25 -08001088 saveConfigToXml(packageName, "", -1, null, config, true);
1089 }
1090
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001091 /**
1092 * Reads a bundle from an XML file.
1093 *
1094 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +08001095 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001096 *
1097 * In case of errors, or if the saved config is from a different package version than the
1098 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001099 *
Jayachandran C645ec612020-01-10 10:30:25 -08001100 * @param packageName the name of the package from which we fetched this bundle.
1101 * @param extraString An extra string to be used in the XML file name.
1102 * @param phoneId the phone ID.
1103 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001104 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001105 * version does not match, or reading config fails.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001106 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001107 @Nullable
1108 private PersistableBundle restoreConfigFromXml(@Nullable String packageName,
1109 @NonNull String extraString, int phoneId, boolean isNoSimConfig) {
Jayachandran C645ec612020-01-10 10:30:25 -08001110 if (packageName == null) {
1111 loge("Cannot restore config with null packageName");
1112 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001113 final String version = getPackageVersion(packageName);
1114 if (version == null) {
1115 loge("Failed to get package version for: " + packageName);
1116 return null;
1117 }
Yuchen Dongc15afed2018-04-26 17:05:22 +08001118
Jayachandran C645ec612020-01-10 10:30:25 -08001119 String fileName;
arunvoddu664c36a2021-10-11 20:09:28 +00001120 String iccid = null;
Jayachandran C645ec612020-01-10 10:30:25 -08001121 if (isNoSimConfig) {
1122 fileName = getFilenameForNoSimConfig(packageName);
1123 } else {
Jack Yuf5badd92022-12-08 00:50:53 -08001124 if (TelephonyManager.getSimStateForSlotIndex(phoneId)
Jayachandran C645ec612020-01-10 10:30:25 -08001125 != TelephonyManager.SIM_STATE_LOADED) {
Jack Yu9be55d32023-08-10 13:47:46 -07001126 loge("Skip restore config because SIM records are not loaded. phoneId=" + phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001127 return null;
1128 }
1129
arunvoddu664c36a2021-10-11 20:09:28 +00001130 iccid = getIccIdForPhoneId(phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001131 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1132 if (iccid == null) {
Jack Yu9be55d32023-08-10 13:47:46 -07001133 loge("Cannot restore config with null iccid. phoneId=" + phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001134 return null;
1135 }
1136 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001137 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001138
1139 PersistableBundle restoredBundle = null;
Mateus Azisa13d2502023-05-25 08:44:38 -07001140 File file = new File(mContext.getFilesDir(), fileName);
Jack Yu9be55d32023-08-10 13:47:46 -07001141 String filePath = file.getPath();
1142 String savedVersion = null;
Mateus Azisa13d2502023-05-25 08:44:38 -07001143 try (FileInputStream inFile = new FileInputStream(file)) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001144
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001145 restoredBundle = PersistableBundle.readFromStream(inFile);
Jack Yu9be55d32023-08-10 13:47:46 -07001146 savedVersion = restoredBundle.getString(KEY_VERSION);
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001147 restoredBundle.remove(KEY_VERSION);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001148
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001149 if (!version.equals(savedVersion)) {
Jack Yu9be55d32023-08-10 13:47:46 -07001150 loge("Saved version mismatch: " + version + " vs " + savedVersion
1151 + ", phoneId=" + phoneId);
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001152 restoredBundle = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001153 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001154 } catch (FileNotFoundException e) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001155 // Missing file is normal occurrence that might occur with a new sim or when restoring
1156 // an override file during boot and should not be treated as an error.
Mateus Azisa13d2502023-05-25 08:44:38 -07001157 if (isNoSimConfig) {
Jack Yu9be55d32023-08-10 13:47:46 -07001158 logd("File not found: " + file.getPath() + ", phoneId=" + phoneId);
Mateus Azisa13d2502023-05-25 08:44:38 -07001159 } else {
Jack Yu9be55d32023-08-10 13:47:46 -07001160 logd("File not found : " + getFilePathForLogging(filePath, iccid)
1161 + ", phoneId=" + phoneId);
arunvoddu664c36a2021-10-11 20:09:28 +00001162 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001163 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001164 loge(e.toString());
1165 }
1166
Jack Yu9be55d32023-08-10 13:47:46 -07001167 if (restoredBundle != null) {
1168 logdWithLocalLog("Restored carrier config from cache. phoneId=" + phoneId + ", xml="
1169 + getFilePathForLogging(fileName) + ", version=" + savedVersion
1170 + ", modified time=" + getFileTime(filePath));
1171 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001172 return restoredBundle;
1173 }
1174
arunvoddu664c36a2021-10-11 20:09:28 +00001175 /**
1176 * This method will mask most part of iccid in the filepath for logging on userbuild
1177 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001178 @NonNull
1179 private String getFilePathForLogging(@Nullable String filePath, @Nullable String iccid) {
arunvoddu664c36a2021-10-11 20:09:28 +00001180 // If loggable then return with actual file path
Jack Yu9be55d32023-08-10 13:47:46 -07001181 if (TelephonyUtils.IS_DEBUGGABLE) {
arunvoddu664c36a2021-10-11 20:09:28 +00001182 return filePath;
1183 }
1184 String path = filePath;
1185 int length = (iccid != null) ? iccid.length() : 0;
1186 if (length > 5 && filePath != null) {
1187 path = filePath.replace(iccid.substring(5), "***************");
1188 }
1189 return path;
1190 }
1191
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001192 @Nullable
1193 private PersistableBundle restoreConfigFromXml(@Nullable String packageName,
1194 @NonNull String extraString, int phoneId) {
Jayachandran C645ec612020-01-10 10:30:25 -08001195 return restoreConfigFromXml(packageName, extraString, phoneId, false);
1196 }
1197
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001198 @Nullable
1199 private PersistableBundle restoreNoSimConfigFromXml(@Nullable String packageName) {
Jayachandran C645ec612020-01-10 10:30:25 -08001200 return restoreConfigFromXml(packageName, "", -1, true);
1201 }
1202
Junda Liu8a8a53a2015-05-15 16:19:57 -07001203 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001204 * Clears cached carrier config.
1205 * This deletes all saved XML files associated with the given package name. If packageName is
1206 * null, then it deletes all saved XML files.
1207 *
1208 * @param packageName the name of a carrier package, or null if all cached config should be
1209 * cleared.
1210 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -07001211 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001212 private boolean clearCachedConfigForPackage(@Nullable final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001213 File dir = mContext.getFilesDir();
1214 File[] packageFiles = dir.listFiles(new FilenameFilter() {
1215 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001216 if (packageName != null) {
1217 return filename.startsWith("carrierconfig-" + packageName + "-");
1218 } else {
1219 return filename.startsWith("carrierconfig-");
1220 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001221 }
1222 });
Junda Liu8a8a53a2015-05-15 16:19:57 -07001223 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001224 for (File f : packageFiles) {
Jack Yu9be55d32023-08-10 13:47:46 -07001225 logdWithLocalLog("Deleting " + getFilePathForLogging(f.getName()));
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001226 f.delete();
1227 }
Junda Liu8a8a53a2015-05-15 16:19:57 -07001228 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001229 }
1230
arunvoddub7c6e8e2022-09-05 15:48:06 +00001231 private String getFilePathForLogging(String filePath) {
1232 if (!TextUtils.isEmpty(filePath)) {
1233 String[] fileTokens = filePath.split("-");
1234 if (fileTokens != null && fileTokens.length > 2) {
1235 String iccid = fileTokens[fileTokens.length -2];
1236 return getFilePathForLogging(filePath, iccid);
1237 }
1238 return filePath;
1239 }
1240 return filePath;
1241 }
1242
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001243 /** Builds a canonical file name for a config file. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001244 @NonNull
Jayachandran C645ec612020-01-10 10:30:25 -08001245 private static String getFilenameForConfig(
1246 @NonNull String packageName, @NonNull String extraString,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001247 @NonNull String iccid, int cid) {
chen xu02581692018-11-11 19:03:44 -08001248 // the same carrier should have a single copy of XML file named after carrier id.
1249 // However, it's still possible that platform doesn't recognize the current sim carrier,
1250 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
1251 // cases SIM OTA resolves to different carrier while iccid remains the same.
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001252 return "carrierconfig-" + packageName + extraString + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001253 }
1254
Jayachandran C645ec612020-01-10 10:30:25 -08001255 /** Builds a canonical file name for no SIM config file. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001256 @NonNull
Jayachandran C645ec612020-01-10 10:30:25 -08001257 private String getFilenameForNoSimConfig(@NonNull String packageName) {
1258 return "carrierconfig-" + packageName + "-" + "nosim" + ".xml";
1259 }
1260
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001261 /** Return the current version code of a package, or null if the name is not found. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001262 @Nullable
1263 private String getPackageVersion(@NonNull String packageName) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001264 try {
1265 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -08001266 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001267 } catch (PackageManager.NameNotFoundException e) {
1268 return null;
1269 }
1270 }
1271
Jonathan Basseri65273c82017-07-25 15:08:42 -07001272 /**
1273 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001274 *
1275 * This reads config bundles for the given phoneId. That means getting the latest bundle from
1276 * the default app and a privileged carrier app, if present. This will not bind to an app if we
1277 * have a saved config file to use instead.
1278 */
1279 private void updateConfigForPhoneId(int phoneId) {
Jonathan Basseri65273c82017-07-25 15:08:42 -07001280 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001281 }
1282
Malcolm Chen5ea18532019-10-24 19:55:44 -07001283 private void onMultiSimConfigChanged() {
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001284 int oldNumPhones = mNumPhones;
1285 mNumPhones = TelephonyManager.from(mContext).getActiveModemCount();
1286 if (mNumPhones == oldNumPhones) {
1287 return;
1288 }
1289 logdWithLocalLog("mNumPhones change from " + oldNumPhones + " to " + mNumPhones);
1290
Rambo Wanga27fbe52022-04-12 19:09:07 +00001291 // If DS -> SS switch, release the resources BEFORE truncating the arrays to avoid leaking
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001292 for (int phoneId = mNumPhones; phoneId < oldNumPhones; phoneId++) {
1293 if (mServiceConnection[phoneId] != null) {
1294 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
1295 }
1296 if (mServiceConnectionForNoSimConfig[phoneId] != null) {
1297 unbindIfBoundForNoSimConfig(mContext, mServiceConnectionForNoSimConfig[phoneId],
1298 phoneId);
1299 }
1300 }
1301
Rambo Wanga27fbe52022-04-12 19:09:07 +00001302 // The phone to slot mapping may change, unregister here and re-register callbacks later
1303 for (int phoneId = 0; phoneId < oldNumPhones; phoneId++) {
1304 if (mCarrierServiceChangeCallbacks[phoneId] != null) {
1305 TelephonyManager.from(mContext).unregisterCarrierPrivilegesCallback(
1306 mCarrierServiceChangeCallbacks[phoneId]);
1307 }
1308 }
1309
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001310 // Copy the original arrays, truncate or padding with zeros (if necessary) to new length
1311 mConfigFromDefaultApp = Arrays.copyOf(mConfigFromDefaultApp, mNumPhones);
1312 mConfigFromCarrierApp = Arrays.copyOf(mConfigFromCarrierApp, mNumPhones);
1313 mPersistentOverrideConfigs = Arrays.copyOf(mPersistentOverrideConfigs, mNumPhones);
1314 mOverrideConfigs = Arrays.copyOf(mOverrideConfigs, mNumPhones);
1315 mServiceConnection = Arrays.copyOf(mServiceConnection, mNumPhones);
1316 mServiceConnectionForNoSimConfig =
1317 Arrays.copyOf(mServiceConnectionForNoSimConfig, mNumPhones);
1318 mServiceBound = Arrays.copyOf(mServiceBound, mNumPhones);
1319 mServiceBoundForNoSimConfig = Arrays.copyOf(mServiceBoundForNoSimConfig, mNumPhones);
1320 mHasSentConfigChange = Arrays.copyOf(mHasSentConfigChange, mNumPhones);
1321 mFromSystemUnlocked = Arrays.copyOf(mFromSystemUnlocked, mNumPhones);
Rambo Wanga27fbe52022-04-12 19:09:07 +00001322 mCarrierServiceChangeCallbacks = Arrays.copyOf(mCarrierServiceChangeCallbacks, mNumPhones);
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001323
Rambo Wanga27fbe52022-04-12 19:09:07 +00001324 // Load the config for all the phones and re-register callback AFTER padding the arrays.
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001325 for (int phoneId = 0; phoneId < mNumPhones; phoneId++) {
1326 updateConfigForPhoneId(phoneId);
Rambo Wanga27fbe52022-04-12 19:09:07 +00001327 mCarrierServiceChangeCallbacks[phoneId] = new CarrierServiceChangeCallback(phoneId);
1328 TelephonyManager.from(mContext).registerCarrierPrivilegesCallback(phoneId,
1329 new HandlerExecutor(mHandler), mCarrierServiceChangeCallbacks[phoneId]);
Malcolm Chen5ea18532019-10-24 19:55:44 -07001330 }
1331 }
1332
Hall Liu506724b2018-10-22 18:16:14 -07001333 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001334 @NonNull
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001335 public PersistableBundle getConfigForSubId(int subscriptionId, @NonNull String callingPackage) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001336 return getConfigForSubIdWithFeature(subscriptionId, callingPackage, null);
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08001337 }
1338
1339 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001340 @NonNull
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001341 public PersistableBundle getConfigForSubIdWithFeature(int subscriptionId,
1342 @NonNull String callingPackage, @Nullable String callingFeatureId) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001343 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subscriptionId,
1344 callingPackage, callingFeatureId, "getCarrierConfig")) {
Rambo Wang42026112021-04-07 20:57:28 +00001345 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -07001346 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -07001347
joonhunshin21a86812023-12-10 08:21:25 +00001348 enforceTelephonyFeatureWithException(callingPackage, "getConfigForSubIdWithFeature");
1349
Rambo Wangd2b004b2021-03-30 10:10:23 -07001350 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001351 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001352 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001353 PersistableBundle config = mConfigFromDefaultApp[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 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001357 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001358 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001359 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001360 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001361 config = mPersistentOverrideConfigs[phoneId];
1362 if (config != null) {
1363 retConfig.putAll(config);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001364 }
Hall Liu506724b2018-10-22 18:16:14 -07001365 config = mOverrideConfigs[phoneId];
1366 if (config != null) {
1367 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -07001368 }
Nathan Harold1122e3d2021-01-22 15:45:24 -08001369 // Ignore the theoretical case of the default app not being present since that won't
1370 // work in CarrierConfigLoader today.
1371 final boolean allConfigsApplied =
1372 (mConfigFromCarrierApp[phoneId] != null
1373 || getCarrierPackageForPhoneId(phoneId) == null)
1374 && mConfigFromDefaultApp[phoneId] != null;
1375 retConfig.putBoolean(
1376 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, allConfigsApplied);
Jayachandran C645ec612020-01-10 10:30:25 -08001377 } else {
1378 if (mNoSimConfig != null) {
1379 retConfig.putAll(mNoSimConfig);
1380 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001381 }
1382 return retConfig;
1383 }
1384
1385 @Override
rambowang14757c22022-10-03 11:54:56 -05001386 @NonNull
1387 public PersistableBundle getConfigSubsetForSubIdWithFeature(int subscriptionId,
1388 @NonNull String callingPackage, @Nullable String callingFeatureId,
1389 @NonNull String[] keys) {
1390 Objects.requireNonNull(callingPackage, "Calling package must be non-null");
1391 Objects.requireNonNull(keys, "Config keys must be non-null");
1392 enforceCallerIsSystemOrRequestingPackage(callingPackage);
1393
joonhunshin21a86812023-12-10 08:21:25 +00001394 enforceTelephonyFeatureWithException(callingPackage,
1395 "getConfigSubsetForSubIdWithFeature");
1396
rambowang14757c22022-10-03 11:54:56 -05001397 // Permission check is performed inside and an empty bundle will return on failure.
1398 // No SecurityException thrown here since most clients expect to retrieve the overridden
1399 // value if present or use default one if not
1400 PersistableBundle allConfigs = getConfigForSubIdWithFeature(subscriptionId, callingPackage,
1401 callingFeatureId);
1402 if (allConfigs.isEmpty()) {
1403 return allConfigs;
1404 }
1405 for (String key : keys) {
1406 Objects.requireNonNull(key, "Config key must be non-null");
rambowang14757c22022-10-03 11:54:56 -05001407 }
1408
1409 PersistableBundle configSubset = new PersistableBundle(
1410 keys.length + CONFIG_SUBSET_METADATA_KEYS.length);
1411 for (String carrierConfigKey : keys) {
1412 Object value = allConfigs.get(carrierConfigKey);
rambowangb49e90f2022-12-13 18:29:06 -06001413 if (value == null) {
1414 // Filter out keys without values.
1415 // In history, many AOSP or OEMs/carriers private configs didn't provide default
1416 // values. We have to continue supporting them for now. See b/261776046 for details.
1417 continue;
1418 }
rambowang14757c22022-10-03 11:54:56 -05001419 // Config value itself could be PersistableBundle which requires different API to put
1420 if (value instanceof PersistableBundle) {
1421 configSubset.putPersistableBundle(carrierConfigKey, (PersistableBundle) value);
1422 } else {
1423 configSubset.putObject(carrierConfigKey, value);
1424 }
1425 }
1426
1427 // Configs in CONFIG_SUBSET_ALWAYS_INCLUDED_KEYS should always be included
1428 for (String generalKey : CONFIG_SUBSET_METADATA_KEYS) {
1429 configSubset.putObject(generalKey, allConfigs.get(generalKey));
1430 }
1431
1432 return configSubset;
1433 }
1434
mattgilbride5b6b7182023-04-03 18:56:44 +00001435 @android.annotation.EnforcePermission(android.Manifest.permission.MODIFY_PHONE_STATE)
rambowang14757c22022-10-03 11:54:56 -05001436 @Override
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001437 public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides,
1438 boolean persistent) {
mattgilbride5b6b7182023-04-03 18:56:44 +00001439 overrideConfig_enforcePermission();
Hall Liu506724b2018-10-22 18:16:14 -07001440 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1441 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001442 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
Rambo Wangd2b004b2021-03-30 10:10:23 -07001443 throw new IllegalArgumentException(
1444 "Invalid phoneId " + phoneId + " for subId " + subscriptionId);
Hall Liu506724b2018-10-22 18:16:14 -07001445 }
joonhunshin21a86812023-12-10 08:21:25 +00001446
1447 enforceTelephonyFeatureWithException(getCurrentPackageName(), "overrideConfig");
1448
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001449 // Post to run on handler thread on which all states should be confined.
1450 mHandler.post(() -> {
1451 overrideConfig(mOverrideConfigs, phoneId, overrides);
Hall Liu506724b2018-10-22 18:16:14 -07001452
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001453 if (persistent) {
1454 overrideConfig(mPersistentOverrideConfigs, phoneId, overrides);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001455
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001456 if (overrides != null) {
1457 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
1458 saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION,
1459 phoneId,
1460 carrierId, mPersistentOverrideConfigs[phoneId]);
1461 } else {
1462 final String iccid = getIccIdForPhoneId(phoneId);
1463 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1464 String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage,
1465 OVERRIDE_PACKAGE_ADDITION, iccid, cid);
1466 File fileToDelete = new File(mContext.getFilesDir(), fileName);
1467 fileToDelete.delete();
1468 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001469 }
Jack Yu9be55d32023-08-10 13:47:46 -07001470 logdWithLocalLog("overrideConfig: subId=" + subscriptionId + ", persistent="
1471 + persistent + ", overrides=" + overrides);
Jack Yue37dd262022-12-16 11:53:37 -08001472 updateSubscriptionDatabase(phoneId);
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001473 });
Hall Liu506724b2018-10-22 18:16:14 -07001474 }
1475
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001476 private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId,
1477 @Nullable PersistableBundle overrides) {
1478 if (overrides == null) {
Rambo Wang42026112021-04-07 20:57:28 +00001479 currentOverrides[phoneId] = new PersistableBundle();
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001480 } else if (currentOverrides[phoneId] == null) {
1481 currentOverrides[phoneId] = overrides;
1482 } else {
1483 currentOverrides[phoneId].putAll(overrides);
1484 }
1485 }
1486
Hall Liu506724b2018-10-22 18:16:14 -07001487 @Override
Rambo Wangd2b004b2021-03-30 10:10:23 -07001488 public void notifyConfigChangedForSubId(int subscriptionId) {
Jordan Liud7d57fe2019-04-16 12:29:43 -07001489 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001490 // system privileged app with MODIFY_PHONE_STATE permission.
Rambo Wangd2b004b2021-03-30 10:10:23 -07001491 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext,
1492 subscriptionId, "Require carrier privileges or MODIFY_PHONE_STATE permission.");
1493
1494 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1495 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Rambo Wang0c386012024-02-22 22:38:17 +00001496 final String msg =
1497 "Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId;
1498 if (mFeatureFlags.addAnomalyWhenNotifyConfigChangedWithInvalidPhone()) {
1499 AnomalyReporter.reportAnomaly(
1500 UUID.fromString(UUID_NOTIFY_CONFIG_CHANGED_WITH_INVALID_PHONE), msg);
1501 }
1502 logd(msg);
1503 throw new IllegalArgumentException(msg);
Rambo Wangd2b004b2021-03-30 10:10:23 -07001504 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001505
joonhunshin21a86812023-12-10 08:21:25 +00001506 enforceTelephonyFeatureWithException(getCurrentPackageName(),
1507 "notifyConfigChangedForSubId");
1508
Jack Yu9be55d32023-08-10 13:47:46 -07001509 logdWithLocalLog("Notified carrier config changed. phoneId=" + phoneId
1510 + ", subId=" + subscriptionId);
1511
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001512 // This method should block until deleting has completed, so that an error which prevents us
1513 // from clearing the cache is passed back to the carrier app. With the files successfully
1514 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001515 String callingPackageName = mContext.getPackageManager().getNameForUid(
1516 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001517 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001518 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001519 }
1520
mattgilbride5b6b7182023-04-03 18:56:44 +00001521 @android.annotation.EnforcePermission(android.Manifest.permission.MODIFY_PHONE_STATE)
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001522 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001523 public void updateConfigForPhoneId(int phoneId, @NonNull String simState) {
mattgilbride5b6b7182023-04-03 18:56:44 +00001524 updateConfigForPhoneId_enforcePermission();
Jack Yu9be55d32023-08-10 13:47:46 -07001525 logdWithLocalLog("Update config for phoneId=" + phoneId + " simState=" + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001526 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001527 throw new IllegalArgumentException("Invalid phoneId: " + phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001528 }
joonhunshin21a86812023-12-10 08:21:25 +00001529
1530 enforceTelephonyFeatureWithException(getCurrentPackageName(), "updateConfigForPhoneId");
1531
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001532 // requires Java 7 for switch on string.
1533 switch (simState) {
1534 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1535 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001536 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001537 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Malcolm Chendb66de12019-12-09 18:57:07 -08001538 case IccCardConstants.INTENT_VALUE_ICC_NOT_READY:
Junda Liu9f2d2712015-05-15 14:22:45 -07001539 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001540 break;
1541 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1542 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001543 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001544 break;
1545 }
1546 }
1547
mattgilbride5b6b7182023-04-03 18:56:44 +00001548 @android.annotation.EnforcePermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE)
Junda Liu43d723a2015-05-12 17:23:45 -07001549 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001550 @NonNull
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001551 public String getDefaultCarrierServicePackageName() {
mattgilbride5b6b7182023-04-03 18:56:44 +00001552 getDefaultCarrierServicePackageName_enforcePermission();
joonhunshin21a86812023-12-10 08:21:25 +00001553
1554 enforceTelephonyFeatureWithException(getCurrentPackageName(),
1555 "getDefaultCarrierServicePackageName");
1556
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001557 return mPlatformCarrierConfigPackage;
1558 }
1559
Rambo Wang7e3bc122021-03-23 09:24:38 -07001560 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001561 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -07001562 /* package */ Handler getHandler() {
1563 return mHandler;
1564 }
1565
1566 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001567 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001568 /* package */ PersistableBundle getConfigFromDefaultApp(int phoneId) {
1569 return mConfigFromDefaultApp[phoneId];
1570 }
1571
1572 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001573 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001574 /* package */ PersistableBundle getConfigFromCarrierApp(int phoneId) {
1575 return mConfigFromCarrierApp[phoneId];
1576 }
1577
1578 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001579 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -07001580 /* package */ PersistableBundle getNoSimConfig() {
1581 return mNoSimConfig;
1582 }
1583
1584 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001585 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001586 /* package */ PersistableBundle getOverrideConfig(int phoneId) {
1587 return mOverrideConfigs[phoneId];
1588 }
1589
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001590 // TODO(b/185129900): always call unbindService after bind, no matter if it succeeded
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001591 private void unbindIfBound(@NonNull Context context, @NonNull CarrierServiceConnection conn,
Rambo Wangb0f3e2f2021-10-28 12:40:12 -07001592 int phoneId) {
1593 if (mServiceBound[phoneId]) {
1594 mServiceBound[phoneId] = false;
1595 context.unbindService(conn);
1596 }
1597 }
1598
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001599 private void unbindIfBoundForNoSimConfig(@NonNull Context context,
1600 @NonNull CarrierServiceConnection conn, int phoneId) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -07001601 if (mServiceBoundForNoSimConfig[phoneId]) {
1602 mServiceBoundForNoSimConfig[phoneId] = false;
Jayachandran C645ec612020-01-10 10:30:25 -08001603 context.unbindService(conn);
1604 }
1605 }
1606
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001607 /**
Rambo Wang610fdf62021-03-08 21:37:11 -08001608 * Returns a boxed Integer object for phoneId, services as message token to distinguish messages
1609 * with same code when calling {@link Handler#removeMessages(int, Object)}.
1610 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001611 @NonNull
Rambo Wang610fdf62021-03-08 21:37:11 -08001612 private Integer getMessageToken(int phoneId) {
1613 if (phoneId < -128 || phoneId > 127) {
1614 throw new IllegalArgumentException("phoneId should be in range [-128, 127], inclusive");
1615 }
1616 // Integer#valueOf guarantees the integers within [-128, 127] are cached and thus memory
1617 // comparison (==) returns true for the same integer.
1618 return Integer.valueOf(phoneId);
1619 }
1620
1621 /**
Jack Yu9be55d32023-08-10 13:47:46 -07001622 * Get the file time in readable format.
1623 *
1624 * @param filePath The full file path.
1625 *
1626 * @return The time in string format.
1627 */
1628 @Nullable
1629 private String getFileTime(@NonNull String filePath) {
1630 String formattedModifiedTime = null;
1631 try {
1632 // Convert the modified time to a readable format
1633 formattedModifiedTime = TIME_FORMAT.format(Files.readAttributes(Paths.get(filePath),
1634 BasicFileAttributes.class).lastModifiedTime().toMillis());
1635 } catch (Exception e) {
1636 e.printStackTrace();
1637 }
1638
1639 return formattedModifiedTime;
1640 }
1641
1642 /**
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001643 * If {@code args} contains {@link #DUMP_ARG_REQUESTING_PACKAGE} and a following package name,
1644 * we'll also call {@link IBinder#dump} on the default carrier service (if bound) and the
1645 * specified carrier service (if bound). Typically, this is done for connectivity bug reports
1646 * where we don't call {@code dumpsys activity service all-non-platform} because that contains
1647 * too much info, but we still want to let carrier apps include their diagnostics.
1648 */
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001649 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001650 public void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter pw, @NonNull String[] args) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001651 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
Junda Liu43d723a2015-05-12 17:23:45 -07001652 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1653 != PackageManager.PERMISSION_GRANTED) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001654 indentPW.println("Permission Denial: can't dump carrierconfig from from pid="
Junda Liu43d723a2015-05-12 17:23:45 -07001655 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1656 return;
1657 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001658 String requestingPackage = null;
1659 int requestingPackageIndex = ArrayUtils.indexOf(args, DUMP_ARG_REQUESTING_PACKAGE);
1660 if (requestingPackageIndex >= 0 && requestingPackageIndex < args.length - 1
1661 && !TextUtils.isEmpty(args[requestingPackageIndex + 1])) {
1662 requestingPackage = args[requestingPackageIndex + 1];
1663 // Throws a SecurityException if the caller is impersonating another app in an effort to
1664 // dump extra info (which may contain PII the caller doesn't have a right to).
1665 enforceCallerIsSystemOrRequestingPackage(requestingPackage);
1666 }
1667
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001668 indentPW.println("CarrierConfigLoader: " + this);
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001669 for (int i = 0; i < mNumPhones; i++) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001670 indentPW.println("Phone Id = " + i);
shuoq26a3a4c2016-12-16 11:06:48 -08001671 // display default values in CarrierConfigManager
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001672 printConfig(CarrierConfigManager.getDefaultConfig(), indentPW,
shuoq26a3a4c2016-12-16 11:06:48 -08001673 "Default Values from CarrierConfigManager");
shuoq26a3a4c2016-12-16 11:06:48 -08001674 // display ConfigFromDefaultApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001675 printConfig(mConfigFromDefaultApp[i], indentPW, "mConfigFromDefaultApp");
shuoq26a3a4c2016-12-16 11:06:48 -08001676 // display ConfigFromCarrierApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001677 printConfig(mConfigFromCarrierApp[i], indentPW, "mConfigFromCarrierApp");
1678 printConfig(mPersistentOverrideConfigs[i], indentPW, "mPersistentOverrideConfigs");
1679 printConfig(mOverrideConfigs[i], indentPW, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001680 }
chen xudb04c292019-03-26 17:01:15 -07001681
Jayachandran C645ec612020-01-10 10:30:25 -08001682 printConfig(mNoSimConfig, indentPW, "mNoSimConfig");
Jack Yu9be55d32023-08-10 13:47:46 -07001683 indentPW.println("mNumPhones=" + mNumPhones);
1684 indentPW.println("mPlatformCarrierConfigPackage=" + mPlatformCarrierConfigPackage);
1685 indentPW.println("mServiceConnection=[" + Stream.of(mServiceConnection)
1686 .map(c -> c != null ? c.pkgName : null)
1687 .collect(Collectors.joining(", ")) + "]");
1688 indentPW.println("mServiceBoundForNoSimConfig="
1689 + Arrays.toString(mServiceBoundForNoSimConfig));
1690 indentPW.println("mHasSentConfigChange=" + Arrays.toString(mHasSentConfigChange));
1691 indentPW.println("mFromSystemUnlocked=" + Arrays.toString(mFromSystemUnlocked));
1692 indentPW.println();
1693 indentPW.println("CarrierConfigLoader local log=");
1694 indentPW.increaseIndent();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001695 mCarrierConfigLoadingLog.dump(fd, indentPW, args);
Jack Yu9be55d32023-08-10 13:47:46 -07001696 indentPW.decreaseIndent();
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001697
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001698 if (requestingPackage != null) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001699 logd("Including default and requesting package " + requestingPackage
1700 + " carrier services in dump");
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001701 indentPW.println("");
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001702 indentPW.println("Connected services");
1703 dumpCarrierServiceIfBound(fd, indentPW, "Default config package",
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001704 mPlatformCarrierConfigPackage, false /* considerCarrierPrivileges */);
1705 dumpCarrierServiceIfBound(fd, indentPW, "Requesting package", requestingPackage,
1706 true /* considerCarrierPrivileges */);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001707 }
Jack Yu9be55d32023-08-10 13:47:46 -07001708
1709 indentPW.println();
1710 indentPW.println("Cached config files:");
1711 indentPW.increaseIndent();
1712 for (File f : mContext.getFilesDir().listFiles((FilenameFilter) (d, filename)
1713 -> filename.startsWith("carrierconfig-"))) {
1714 indentPW.println(getFilePathForLogging(f.getName()) + ", modified time="
1715 + getFileTime(f.getAbsolutePath()));
1716 }
1717 indentPW.decreaseIndent();
shuoq26a3a4c2016-12-16 11:06:48 -08001718 }
1719
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001720 private void printConfig(@NonNull PersistableBundle configApp,
1721 @NonNull IndentingPrintWriter indentPW, @NonNull String name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001722 indentPW.increaseIndent();
shuoq26a3a4c2016-12-16 11:06:48 -08001723 if (configApp == null) {
shuoq26a3a4c2016-12-16 11:06:48 -08001724 indentPW.println(name + " : null ");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001725 indentPW.decreaseIndent();
1726 indentPW.println("");
shuoq26a3a4c2016-12-16 11:06:48 -08001727 return;
1728 }
shuoq26a3a4c2016-12-16 11:06:48 -08001729 indentPW.println(name + " : ");
1730 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1731 Collections.sort(sortedKeys);
1732 indentPW.increaseIndent();
1733 indentPW.increaseIndent();
1734 for (String key : sortedKeys) {
1735 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1736 indentPW.println(key + " = " +
1737 Arrays.toString((Object[]) configApp.get(key)));
1738 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1739 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1740 } else {
1741 indentPW.println(key + " = " + configApp.get(key));
1742 }
Junda Liu43d723a2015-05-12 17:23:45 -07001743 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001744 indentPW.decreaseIndent();
1745 indentPW.decreaseIndent();
1746 indentPW.decreaseIndent();
1747 indentPW.println("");
Junda Liu43d723a2015-05-12 17:23:45 -07001748 }
1749
Hunter Knepshielde601f432020-02-19 10:16:04 -08001750 /**
1751 * Passes without problem when one of these conditions is true:
1752 * - The caller is a privileged UID (e.g. for dumpstate.cpp generating a bug report, where the
1753 * system knows the true caller plumbed in through the {@link android.os.BugreportManager} API).
1754 * - The caller's UID matches the supplied package.
1755 *
1756 * @throws SecurityException if none of the above conditions are met.
1757 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001758 private void enforceCallerIsSystemOrRequestingPackage(@NonNull String requestingPackage)
Hunter Knepshielde601f432020-02-19 10:16:04 -08001759 throws SecurityException {
1760 final int callingUid = Binder.getCallingUid();
1761 if (callingUid == Process.ROOT_UID || callingUid == Process.SYSTEM_UID
1762 || callingUid == Process.SHELL_UID || callingUid == Process.PHONE_UID) {
1763 // Bug reports (dumpstate.cpp) run as SHELL, and let some other privileged UIDs through
1764 // as well.
1765 return;
1766 }
1767 // An app is trying to dump extra detail, block it if they aren't who they claim to be.
1768 AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class);
1769 if (appOps == null) {
1770 throw new SecurityException("No AppOps");
1771 }
1772 // Will throw a SecurityException if the UID and package don't match.
1773 appOps.checkPackage(callingUid, requestingPackage);
1774 }
1775
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001776 /**
1777 * Searches for one or more appropriate {@link CarrierService} instances to dump based on the
1778 * current connections.
1779 *
1780 * @param targetPkgName the target package name to dump carrier services for
1781 * @param considerCarrierPrivileges if true, allow a carrier service to be dumped if it shares
1782 * carrier privileges with {@code targetPkgName};
1783 * otherwise, only dump a carrier service if it is {@code
1784 * targetPkgName}
1785 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001786 private void dumpCarrierServiceIfBound(@NonNull FileDescriptor fd,
1787 @NonNull IndentingPrintWriter indentPW, @NonNull String prefix,
1788 @NonNull String targetPkgName, boolean considerCarrierPrivileges) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001789 // Null package is possible if it's early in the boot process, there was a recent crash, we
1790 // loaded the config from XML most recently, or a SIM slot is empty. Carrier apps with
1791 // long-lived bindings should typically get dumped here regardless. Even if an app is being
1792 // used for multiple phoneIds, we assume that it's smart enough to handle that on its own,
1793 // and that in most cases we'd just be dumping duplicate information and bloating a report.
1794 indentPW.increaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001795 indentPW.println(prefix + " : " + targetPkgName);
1796 Set<String> dumpedPkgNames = new ArraySet<>(mServiceConnection.length);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001797 for (CarrierServiceConnection connection : mServiceConnection) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001798 if (connection == null || !SubscriptionManager.isValidPhoneId(connection.phoneId)
1799 || TextUtils.isEmpty(connection.pkgName)) {
1800 continue;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001801 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001802 final String servicePkgName = connection.pkgName;
1803 // Note: we intentionally ignore system components here because we should NOT match the
1804 // shell caller that's typically used for bug reports via non-BugreportManager triggers.
1805 final boolean exactPackageMatch = TextUtils.equals(targetPkgName, servicePkgName);
1806 final boolean carrierPrivilegesMatch =
1807 considerCarrierPrivileges && hasCarrierPrivileges(targetPkgName,
1808 connection.phoneId);
1809 if (!exactPackageMatch && !carrierPrivilegesMatch) continue;
1810 // Make sure this service is actually alive before trying to dump it. We don't pay
1811 // attention to mServiceBound[connection.phoneId] because typically carrier apps will
1812 // request long-lived bindings, and even if we unbind the app, it may still be alive due
1813 // to CarrierServiceBindHelper. Pull it out as a reference so even if it gets set to
1814 // null within the ServiceConnection during unbinding we can avoid an NPE.
1815 final IBinder service = connection.service;
1816 if (service == null || !service.isBinderAlive() || !service.pingBinder()) continue;
1817 // We've got a live service. Last check is just to make sure we don't dump a package
1818 // multiple times.
1819 if (!dumpedPkgNames.add(servicePkgName)) continue;
1820 if (!exactPackageMatch) {
1821 logd(targetPkgName + " has carrier privileges on phoneId " + connection.phoneId
1822 + ", service provided by " + servicePkgName);
1823 indentPW.increaseIndent();
1824 indentPW.println("Proxy : " + servicePkgName);
1825 indentPW.decreaseIndent();
1826 }
1827 // Flush before we let the app output anything to ensure correct ordering of output.
1828 // Internally, Binder#dump calls flush on its printer after finishing so we don't
1829 // need to do anything after.
1830 indentPW.flush();
1831 try {
1832 logd("Dumping " + servicePkgName);
1833 // We don't need to give the carrier service any args.
1834 connection.service.dump(fd, null /* args */);
1835 logd("Done with " + servicePkgName);
1836 } catch (RemoteException e) {
1837 logd("RemoteException from " + servicePkgName, e);
1838 indentPW.increaseIndent();
1839 indentPW.println("RemoteException");
1840 indentPW.increaseIndent();
1841 e.printStackTrace(indentPW);
1842 indentPW.decreaseIndent();
1843 indentPW.decreaseIndent();
1844 // We won't retry this package again because now it's in dumpedPkgNames.
1845 }
1846 indentPW.println("");
Jordan Liu46ed5db2020-01-28 08:55:20 -08001847 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001848 if (dumpedPkgNames.isEmpty()) {
1849 indentPW.increaseIndent();
1850 indentPW.println("Not bound");
1851 indentPW.decreaseIndent();
1852 indentPW.println("");
1853 }
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001854 indentPW.decreaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001855 }
1856
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001857 private boolean hasCarrierPrivileges(@NonNull String pkgName, int phoneId) {
Jack Yu00ece8c2022-11-19 22:29:12 -08001858 int subId = SubscriptionManager.getSubscriptionId(phoneId);
1859 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001860 return false;
1861 }
Jack Yu00ece8c2022-11-19 22:29:12 -08001862 return TelephonyManager.from(mContext).createForSubscriptionId(subId)
1863 .checkCarrierPrivilegesForPackage(pkgName)
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001864 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001865 }
1866
joonhunshin21a86812023-12-10 08:21:25 +00001867 /**
1868 * Get the current calling package name.
1869 * @return the current calling package name
1870 */
1871 @Nullable
1872 private String getCurrentPackageName() {
1873 if (mPackageManager == null) return null;
1874 String[] callingUids = mPackageManager.getPackagesForUid(Binder.getCallingUid());
1875 return (callingUids == null) ? null : callingUids[0];
1876 }
1877
1878 /**
1879 * Make sure the device has required telephony feature
1880 *
1881 * @throws UnsupportedOperationException if the device does not have required telephony feature
1882 */
1883 private void enforceTelephonyFeatureWithException(@Nullable String callingPackage,
1884 @NonNull String methodName) {
1885 if (callingPackage == null || mPackageManager == null) {
1886 return;
1887 }
1888
1889 if (!mFeatureFlags.enforceTelephonyFeatureMappingForPublicApis()
1890 || !CompatChanges.isChangeEnabled(ENABLE_FEATURE_MAPPING, callingPackage,
joonhunshinf624b2a2024-04-18 04:42:12 +00001891 Binder.getCallingUserHandle())
1892 || mVendorApiLevel < Build.VERSION_CODES.VANILLA_ICE_CREAM) {
1893 // Skip to check associated telephony feature,
1894 // if compatibility change is not enabled for the current process or
1895 // the SDK version of vendor partition is less than Android V.
joonhunshin21a86812023-12-10 08:21:25 +00001896 return;
1897 }
1898
1899 if (!mPackageManager.hasSystemFeature(FEATURE_TELEPHONY_SUBSCRIPTION)) {
1900 throw new UnsupportedOperationException(
1901 methodName + " is unsupported without " + FEATURE_TELEPHONY_SUBSCRIPTION);
1902 }
1903 }
1904
Zach Johnson36d7aab2015-05-22 15:43:00 -07001905 private class CarrierServiceConnection implements ServiceConnection {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001906 final int phoneId;
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001907 @NonNull final String pkgName;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001908 final int eventId;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001909 IBinder service;
1910
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001911 CarrierServiceConnection(int phoneId, @NonNull String pkgName, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001912 this.phoneId = phoneId;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001913 this.pkgName = pkgName;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001914 this.eventId = eventId;
1915 }
1916
1917 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001918 public void onServiceConnected(@NonNull ComponentName name, @NonNull IBinder service) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001919 logd("Connected to config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001920 this.service = service;
1921 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1922 }
1923
1924 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001925 public void onServiceDisconnected(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001926 logd("Disconnected from config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001927 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001928 }
1929
1930 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001931 public void onBindingDied(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001932 logd("Binding died from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001933 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001934 }
1935
1936 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001937 public void onNullBinding(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001938 logd("Null binding from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001939 this.service = null;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001940 }
1941 }
1942
1943 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1944 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001945 public void onReceive(@NonNull Context context, @NonNull Intent intent) {
Rambo Wanga27fbe52022-04-12 19:09:07 +00001946 switch (intent.getAction()) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001947 case Intent.ACTION_BOOT_COMPLETED:
1948 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1949 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001950 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001951 }
1952 }
1953
Rambo Wanga27fbe52022-04-12 19:09:07 +00001954 private class CarrierServiceChangeCallback implements
1955 TelephonyManager.CarrierPrivilegesCallback {
1956 final int mPhoneId;
1957 // CarrierPrivilegesCallback will be triggered upon registration. Filter the first callback
1958 // here since we really care of the *change* of carrier service instead of the content
1959 private boolean mHasSentServiceChangeCallback;
1960
1961 CarrierServiceChangeCallback(int phoneId) {
1962 this.mPhoneId = phoneId;
1963 this.mHasSentServiceChangeCallback = false;
1964 }
1965
1966 @Override
1967 public void onCarrierPrivilegesChanged(
1968 @androidx.annotation.NonNull Set<String> privilegedPackageNames,
1969 @androidx.annotation.NonNull Set<Integer> privilegedUids) {
1970 // Ignored, not interested here
1971 }
1972
1973 @Override
1974 public void onCarrierServiceChanged(
1975 @androidx.annotation.Nullable String carrierServicePackageName,
1976 int carrierServiceUid) {
1977 // Ignore the first callback which is triggered upon registration
1978 if (!mHasSentServiceChangeCallback) {
1979 mHasSentServiceChangeCallback = true;
1980 return;
1981 }
1982 mHandler.sendMessage(
1983 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, mPhoneId, -1,
1984 carrierServicePackageName));
1985 }
1986 }
1987
Rambo Wangab13e3e2021-04-08 15:01:06 -07001988 // Get readable string for the message code supported in this class.
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001989 @NonNull
Rambo Wangab13e3e2021-04-08 15:01:06 -07001990 private static String eventToString(int code) {
1991 switch (code) {
1992 case EVENT_CLEAR_CONFIG:
1993 return "EVENT_CLEAR_CONFIG";
1994 case EVENT_CONNECTED_TO_DEFAULT:
1995 return "EVENT_CONNECTED_TO_DEFAULT";
1996 case EVENT_CONNECTED_TO_CARRIER:
1997 return "EVENT_CONNECTED_TO_CARRIER";
1998 case EVENT_FETCH_DEFAULT_DONE:
1999 return "EVENT_FETCH_DEFAULT_DONE";
2000 case EVENT_FETCH_CARRIER_DONE:
2001 return "EVENT_FETCH_CARRIER_DONE";
2002 case EVENT_DO_FETCH_DEFAULT:
2003 return "EVENT_DO_FETCH_DEFAULT";
2004 case EVENT_DO_FETCH_CARRIER:
2005 return "EVENT_DO_FETCH_CARRIER";
2006 case EVENT_PACKAGE_CHANGED:
2007 return "EVENT_PACKAGE_CHANGED";
2008 case EVENT_BIND_DEFAULT_TIMEOUT:
2009 return "EVENT_BIND_DEFAULT_TIMEOUT";
2010 case EVENT_BIND_CARRIER_TIMEOUT:
2011 return "EVENT_BIND_CARRIER_TIMEOUT";
2012 case EVENT_CHECK_SYSTEM_UPDATE:
2013 return "EVENT_CHECK_SYSTEM_UPDATE";
2014 case EVENT_SYSTEM_UNLOCKED:
2015 return "EVENT_SYSTEM_UNLOCKED";
2016 case EVENT_FETCH_DEFAULT_TIMEOUT:
2017 return "EVENT_FETCH_DEFAULT_TIMEOUT";
2018 case EVENT_FETCH_CARRIER_TIMEOUT:
2019 return "EVENT_FETCH_CARRIER_TIMEOUT";
2020 case EVENT_SUBSCRIPTION_INFO_UPDATED:
2021 return "EVENT_SUBSCRIPTION_INFO_UPDATED";
2022 case EVENT_MULTI_SIM_CONFIG_CHANGED:
2023 return "EVENT_MULTI_SIM_CONFIG_CHANGED";
2024 case EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG:
2025 return "EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG";
2026 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE:
2027 return "EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE";
2028 case EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG:
2029 return "EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG";
2030 case EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
2031 return "EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT";
2032 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
2033 return "EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT";
2034 default:
2035 return "UNKNOWN(" + code + ")";
2036 }
2037 }
2038
Rambo Wangfe0d7c12022-04-15 03:00:32 +00002039 private void logd(@NonNull String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07002040 Log.d(LOG_TAG, msg);
2041 }
2042
Rambo Wangfe0d7c12022-04-15 03:00:32 +00002043 private void logd(@NonNull String msg, Throwable tr) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08002044 Log.d(LOG_TAG, msg, tr);
2045 }
2046
Rambo Wangfe0d7c12022-04-15 03:00:32 +00002047 private void logdWithLocalLog(@NonNull String msg) {
chen xudb04c292019-03-26 17:01:15 -07002048 Log.d(LOG_TAG, msg);
2049 mCarrierConfigLoadingLog.log(msg);
2050 }
2051
Rambo Wangfe0d7c12022-04-15 03:00:32 +00002052 private void loge(@NonNull String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07002053 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07002054 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08002055 }
2056}