blob: 52920e3cb19319217af6b7ecd3663860df9ddc40 [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;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080047import android.os.RemoteException;
Jonathan Basseri65273c82017-07-25 15:08:42 -070048import android.os.ResultReceiver;
joonhunshinf624b2a2024-04-18 04:42:12 +000049import android.os.SystemProperties;
Meng Wang97a6a462020-01-23 16:22:16 -080050import android.os.UserHandle;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070051import android.preference.PreferenceManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080052import android.service.carrier.CarrierIdentifier;
Zach Johnson36d7aab2015-05-22 15:43:00 -070053import android.service.carrier.CarrierService;
54import android.service.carrier.ICarrierService;
Rambo Wang0c386012024-02-22 22:38:17 +000055import android.telephony.AnomalyReporter;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080056import android.telephony.CarrierConfigManager;
57import android.telephony.SubscriptionManager;
Peter Wangc035ce42020-01-08 21:00:22 -080058import android.telephony.TelephonyFrameworkInitializer;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080059import android.telephony.TelephonyManager;
rambowangd63ba342022-10-02 11:21:08 -050060import android.telephony.TelephonyRegistryManager;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -080061import android.text.TextUtils;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080062import android.util.ArraySet;
chen xudb04c292019-03-26 17:01:15 -070063import android.util.LocalLog;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080064import android.util.Log;
65
Rambo Wang7e3bc122021-03-23 09:24:38 -070066import com.android.internal.annotations.VisibleForTesting;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080067import com.android.internal.telephony.ICarrierConfigLoader;
68import com.android.internal.telephony.IccCardConstants;
69import com.android.internal.telephony.Phone;
Jack Yu771c6ce2023-02-02 17:51:42 -080070import com.android.internal.telephony.PhoneConfigurationManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080071import com.android.internal.telephony.PhoneFactory;
Jeff Davidsona8e4e242018-03-15 17:16:18 -070072import com.android.internal.telephony.TelephonyPermissions;
joonhunshin21a86812023-12-10 08:21:25 +000073import com.android.internal.telephony.flags.FeatureFlags;
Jack Yue37dd262022-12-16 11:53:37 -080074import com.android.internal.telephony.subscription.SubscriptionManagerService;
Meng Wanga320b942019-11-25 11:21:26 -080075import com.android.internal.telephony.util.ArrayUtils;
Jack Yu9be55d32023-08-10 13:47:46 -070076import com.android.internal.telephony.util.TelephonyUtils;
shuoq26a3a4c2016-12-16 11:06:48 -080077import com.android.internal.util.IndentingPrintWriter;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080078
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070079import java.io.File;
Junda Liu43d723a2015-05-12 17:23:45 -070080import java.io.FileDescriptor;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070081import java.io.FileInputStream;
82import java.io.FileNotFoundException;
83import java.io.FileOutputStream;
Jonathan Basseri1f743c92015-05-15 00:19:46 -070084import java.io.FilenameFilter;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070085import java.io.IOException;
Junda Liu43d723a2015-05-12 17:23:45 -070086import java.io.PrintWriter;
Jack Yu9be55d32023-08-10 13:47:46 -070087import java.nio.file.Files;
88import java.nio.file.Paths;
89import java.nio.file.attribute.BasicFileAttributes;
90import java.text.SimpleDateFormat;
shuoq26a3a4c2016-12-16 11:06:48 -080091import java.util.ArrayList;
92import java.util.Arrays;
93import java.util.Collections;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080094import java.util.List;
Jack Yu9be55d32023-08-10 13:47:46 -070095import java.util.Locale;
rambowang14757c22022-10-03 11:54:56 -050096import java.util.Objects;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080097import java.util.Set;
Rambo Wang0c386012024-02-22 22:38:17 +000098import java.util.UUID;
Jack Yu9be55d32023-08-10 13:47:46 -070099import java.util.stream.Collectors;
100import java.util.stream.Stream;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800101
102/**
103 * CarrierConfigLoader binds to privileged carrier apps to fetch carrier config overlays.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800104 */
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800105public class CarrierConfigLoader extends ICarrierConfigLoader.Stub {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700106 private static final String LOG_TAG = "CarrierConfigLoader";
Meng Wang33ad2bc2017-03-16 20:21:20 -0700107
Jack Yu9be55d32023-08-10 13:47:46 -0700108 private static final SimpleDateFormat TIME_FORMAT =
109 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
110
Meng Wang33ad2bc2017-03-16 20:21:20 -0700111 // Package name for platform carrier config app, bundled with system image.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000112 @NonNull private final String mPlatformCarrierConfigPackage;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800113
114 /** The singleton instance. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000115 @Nullable private static CarrierConfigLoader sInstance;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800116 // The context for phone app, passed from PhoneGlobals.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000117 @NonNull private Context mContext;
118
119 // All the states below (array indexed by phoneId) are non-null. But the member of the array
120 // is nullable, when e.g. the config for the phone is not loaded yet
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800121 // Carrier configs from default app, indexed by phoneID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000122 @NonNull private PersistableBundle[] mConfigFromDefaultApp;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800123 // Carrier configs from privileged carrier config app, indexed by phoneID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000124 @NonNull private PersistableBundle[] mConfigFromCarrierApp;
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100125 // Persistent Carrier configs that are provided via the override test API, indexed by phone ID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000126 @NonNull private PersistableBundle[] mPersistentOverrideConfigs;
Hall Liu506724b2018-10-22 18:16:14 -0700127 // Carrier configs that are provided via the override test API, indexed by phone ID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000128 @NonNull private PersistableBundle[] mOverrideConfigs;
Jayachandran C645ec612020-01-10 10:30:25 -0800129 // Carrier configs to override code default when there is no SIM inserted
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000130 @NonNull private PersistableBundle mNoSimConfig;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800131 // Service connection for binding to config app.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000132 @NonNull private CarrierServiceConnection[] mServiceConnection;
Jayachandran C645ec612020-01-10 10:30:25 -0800133 // Service connection for binding to carrier config app for no SIM config.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000134 @NonNull private CarrierServiceConnection[] mServiceConnectionForNoSimConfig;
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700135 // Whether we are bound to a service for each phone
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000136 @NonNull private boolean[] mServiceBound;
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700137 // Whether we are bound to a service for no SIM config
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000138 @NonNull private boolean[] mServiceBoundForNoSimConfig;
Sarah Chin807d5c62020-03-30 17:21:09 -0700139 // Whether we have sent config change broadcast for each phone id.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000140 @NonNull private boolean[] mHasSentConfigChange;
Sarah Chin807d5c62020-03-30 17:21:09 -0700141 // Whether the broadcast was sent from EVENT_SYSTEM_UNLOCKED, to track rebroadcasts
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000142 @NonNull private boolean[] mFromSystemUnlocked;
Rambo Wanga27fbe52022-04-12 19:09:07 +0000143 // CarrierService change monitoring
144 @NonNull private CarrierServiceChangeCallback[] mCarrierServiceChangeCallbacks;
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000145
Jack Yu771c6ce2023-02-02 17:51:42 -0800146 // Broadcast receiver for system events
Rambo Wanga27fbe52022-04-12 19:09:07 +0000147 @NonNull
148 private final BroadcastReceiver mSystemBroadcastReceiver = new ConfigLoaderBroadcastReceiver();
Jack Yu9be55d32023-08-10 13:47:46 -0700149 @NonNull private final LocalLog mCarrierConfigLoadingLog = new LocalLog(256);
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000150 // Number of phone instances (active modem count)
151 private int mNumPhones;
chen xudb04c292019-03-26 17:01:15 -0700152
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800153
154 // Message codes; see mHandler below.
Jack Yu3beaf9d2023-04-14 09:17:27 -0700155 // Request from UiccController when SIM becomes absent or error.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800156 private static final int EVENT_CLEAR_CONFIG = 0;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800157 // Has connected to default app.
158 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
159 // Has connected to carrier app.
160 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700161 // Config has been loaded from default app (or cache).
162 private static final int EVENT_FETCH_DEFAULT_DONE = 5;
163 // Config has been loaded from carrier app (or cache).
164 private static final int EVENT_FETCH_CARRIER_DONE = 6;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700165 // Attempt to fetch from default app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700166 private static final int EVENT_DO_FETCH_DEFAULT = 7;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700167 // Attempt to fetch from carrier app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700168 private static final int EVENT_DO_FETCH_CARRIER = 8;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700169 // A package has been installed, uninstalled, or updated.
170 private static final int EVENT_PACKAGE_CHANGED = 9;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700171 // Bind timed out for the default app.
172 private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
173 // Bind timed out for a carrier app.
174 private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700175 // Check if the system fingerprint has changed.
176 private static final int EVENT_CHECK_SYSTEM_UPDATE = 12;
Nanxi Chen3d670502016-03-17 16:32:09 -0700177 // Rerun carrier config binding after system is unlocked.
178 private static final int EVENT_SYSTEM_UNLOCKED = 13;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700179 // Fetching config timed out from the default app.
180 private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14;
181 // Fetching config timed out from a carrier app.
182 private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15;
Jack Yu3beaf9d2023-04-14 09:17:27 -0700183 // SubscriptionManagerService has finished updating the sub for the carrier config.
Nathan Harold48ac0972019-03-13 22:33:01 -0700184 private static final int EVENT_SUBSCRIPTION_INFO_UPDATED = 16;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700185 // Multi-SIM config changed.
186 private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 17;
Jayachandran C645ec612020-01-10 10:30:25 -0800187 // Attempt to fetch from default app or read from XML for no SIM case.
188 private static final int EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG = 18;
189 // No SIM config has been loaded from default app (or cache).
190 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE = 19;
191 // Has connected to default app for no SIM config.
192 private static final int EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG = 20;
193 // Bind timed out for the default app when trying to fetch no SIM config.
194 private static final int EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 21;
195 // Fetching config timed out from the default app for no SIM config.
196 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 22;
Rambo Wangab13e3e2021-04-08 15:01:06 -0700197 // NOTE: any new EVENT_* values must be added to method eventToString().
Jonathan Basseri930701e2015-06-11 20:56:43 -0700198
Junda Liu6cb45002016-03-22 17:18:20 -0700199 private static final int BIND_TIMEOUT_MILLIS = 30000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800200
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800201 // Keys used for saving and restoring config bundle from file.
202 private static final String KEY_VERSION = "__carrier_config_package_version__";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800203
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100204 private static final String OVERRIDE_PACKAGE_ADDITION = "-override";
205
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700206 // SharedPreferences key for last known build fingerprint.
207 private static final String KEY_FINGERPRINT = "build_fingerprint";
208
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800209 // Argument for #dump that indicates we should also call the default and specified carrier
210 // service's #dump method. In multi-SIM devices, it's possible that carrier A is on SIM 1 and
211 // carrier B is on SIM 2, in which case we should not dump carrier B's service when carrier A
212 // requested the dump.
213 private static final String DUMP_ARG_REQUESTING_PACKAGE = "--requesting-package";
214
rambowang14757c22022-10-03 11:54:56 -0500215 // Configs that should always be included when clients calls getConfig[ForSubId] with specified
216 // keys (even configs are not explicitly specified). Those configs have special purpose for the
217 // carrier config APIs to work correctly.
218 private static final String[] CONFIG_SUBSET_METADATA_KEYS = new String[] {
219 CarrierConfigManager.KEY_CARRIER_CONFIG_VERSION_STRING,
220 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL
221 };
222
Rambo Wang0c386012024-02-22 22:38:17 +0000223 // UUID to report anomaly when config changed reported with subId that map to invalid phone
224 private static final String UUID_NOTIFY_CONFIG_CHANGED_WITH_INVALID_PHONE =
225 "d81cef11-c2f1-4d76-955d-7f50e8590c48";
226
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800227 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700228 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700229 // For each phoneId, the event sequence should be:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700230 // fetch default, connected to default, fetch default (async), fetch default done,
231 // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700232 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700233 // If there is a saved config file for either the default app or the carrier app, we skip
234 // binding to the app and go straight from fetch to loaded.
235 //
236 // At any time, at most one connection is active. If events are not in this order, previous
237 // connection will be unbound, so only latest event takes effect.
238 //
239 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
240 // 1. loading from carrier app (even if read from a file)
241 // 2. loading from default app if there is no carrier app (even if read from a file)
242 // 3. clearing config (e.g. due to sim removal)
243 // 4. encountering bind or IPC error
Jonathan Basseri65273c82017-07-25 15:08:42 -0700244 private class ConfigHandler extends Handler {
Rambo Wang7e3bc122021-03-23 09:24:38 -0700245 ConfigHandler(@NonNull Looper looper) {
246 super(looper);
247 }
248
Jonathan Basseri65273c82017-07-25 15:08:42 -0700249 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000250 public void handleMessage(@NonNull Message msg) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700251 final int phoneId = msg.arg1;
Jack Yu9be55d32023-08-10 13:47:46 -0700252 logd(eventToString(msg.what) + " phoneId: " + phoneId);
Malcolm Chen5ea18532019-10-24 19:55:44 -0700253 if (!SubscriptionManager.isValidPhoneId(phoneId)
254 && msg.what != EVENT_MULTI_SIM_CONFIG_CHANGED) {
255 return;
256 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800257 switch (msg.what) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800258 case EVENT_CLEAR_CONFIG: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700259 clearConfigForPhone(phoneId, true);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800260 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700261 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700262
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800263 case EVENT_SYSTEM_UNLOCKED: {
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000264 for (int i = 0; i < mNumPhones; ++i) {
Sarah Chin807d5c62020-03-30 17:21:09 -0700265 // When the user unlocks the device, send the broadcast again (with a
266 // rebroadcast extra) if we have sent it before unlock. This will avoid
267 // trying to load the carrier config when the SIM is still loading when the
268 // unlock happens.
Junda Liu03aad762017-07-21 13:32:17 -0700269 if (mHasSentConfigChange[i]) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800270 logdWithLocalLog("System unlocked");
Sarah Chin807d5c62020-03-30 17:21:09 -0700271 mFromSystemUnlocked[i] = true;
Junda Liu03aad762017-07-21 13:32:17 -0700272 updateConfigForPhoneId(i);
273 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700274 }
275 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700276 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700277
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800278 case EVENT_PACKAGE_CHANGED: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700279 final String carrierPackageName = (String) msg.obj;
Rambo Wanga27fbe52022-04-12 19:09:07 +0000280 // Always clear up the cache and re-load config from scratch since the carrier
281 // service change is reliable and specific to the phoneId now.
282 clearCachedConfigForPackage(carrierPackageName);
283 logdWithLocalLog("Package changed: " + carrierPackageName
284 + ", phone=" + phoneId);
285 updateConfigForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700286 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700287 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700288
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800289 case EVENT_DO_FETCH_DEFAULT: {
Rambo Wang9bc7d362021-03-09 09:10:58 -0800290 // Clear in-memory cache for carrier app config, so when carrier app gets
291 // uninstalled, no stale config is left.
292 if (mConfigFromCarrierApp[phoneId] != null
293 && getCarrierPackageForPhoneId(phoneId) == null) {
294 mConfigFromCarrierApp[phoneId] = null;
295 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100296 // Restore persistent override values.
297 PersistableBundle config = restoreConfigFromXml(
298 mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId);
299 if (config != null) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100300 mPersistentOverrideConfigs[phoneId] = config;
301 }
302
303 config = restoreConfigFromXml(mPlatformCarrierConfigPackage, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700304 if (config != null) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700305 mConfigFromDefaultApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700306 Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700307 newMsg.getData().putBoolean("loaded_from_xml", true);
308 mHandler.sendMessage(newMsg);
309 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700310 // No cached config, so fetch it from the default app.
311 if (bindToConfigPackage(
312 mPlatformCarrierConfigPackage,
313 phoneId,
314 EVENT_CONNECTED_TO_DEFAULT)) {
315 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800316 obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1 /*arg2*/,
317 getMessageToken(phoneId)),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700318 BIND_TIMEOUT_MILLIS);
319 } else {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800320 // Put a stub bundle in place so that the rest of the logic continues
321 // smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000322 mConfigFromDefaultApp[phoneId] = new PersistableBundle();
Jonathan Basseri65273c82017-07-25 15:08:42 -0700323 // Send broadcast if bind fails.
Jack Yue37dd262022-12-16 11:53:37 -0800324 updateSubscriptionDatabase(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700325 // TODO: We *must* call unbindService even if bindService returns false.
326 // (And possibly if SecurityException was thrown.)
chen xudb04c292019-03-26 17:01:15 -0700327 loge("binding to default app: "
328 + mPlatformCarrierConfigPackage + " fails");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700329 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800330 }
331 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700332 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800333
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800334 case EVENT_CONNECTED_TO_DEFAULT: {
Rambo Wang610fdf62021-03-08 21:37:11 -0800335 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT, getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700336 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800337 // If new service connection has been created, unbind.
338 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700339 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800340 break;
341 }
chen xu02581692018-11-11 19:03:44 -0800342 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700343 // ResultReceiver callback will execute in this Handler's thread.
344 final ResultReceiver resultReceiver =
345 new ResultReceiver(this) {
346 @Override
347 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700348 unbindIfBound(mContext, conn, phoneId);
Rambo Wanga9d27a52021-04-09 16:17:33 -0700349 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT,
350 getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700351 // If new service connection has been created, this is stale.
352 if (mServiceConnection[phoneId] != conn) {
353 loge("Received response for stale request.");
354 return;
355 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700356 if (resultCode == RESULT_ERROR || resultData == null) {
357 // On error, abort config fetching.
358 loge("Failed to get carrier config");
Jack Yue37dd262022-12-16 11:53:37 -0800359 updateSubscriptionDatabase(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700360 return;
361 }
362 PersistableBundle config =
363 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100364 saveConfigToXml(mPlatformCarrierConfigPackage, "", phoneId,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800365 carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700366 mConfigFromDefaultApp[phoneId] = config;
367 sendMessage(
368 obtainMessage(
369 EVENT_FETCH_DEFAULT_DONE, phoneId, -1));
370 }
371 };
372 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800373 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700374 ICarrierService carrierService =
375 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800376 carrierService.getCarrierConfig(phoneId, carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800377 logdWithLocalLog("Fetch config for default app: "
chen xudb04c292019-03-26 17:01:15 -0700378 + mPlatformCarrierConfigPackage
Jack Yu9be55d32023-08-10 13:47:46 -0700379 + ", carrierId=" + carrierId.getSpecificCarrierId());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700380 } catch (RemoteException e) {
chen xudb04c292019-03-26 17:01:15 -0700381 loge("Failed to get carrier config from default app: " +
Jack Yu9be55d32023-08-10 13:47:46 -0700382 mPlatformCarrierConfigPackage + " err: " + e);
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700383 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700384 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800385 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700386 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800387 obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1 /*arg2*/,
388 getMessageToken(phoneId)),
Jonathan Basseri65273c82017-07-25 15:08:42 -0700389 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800390 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700391 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800392
Jonathan Basseri930701e2015-06-11 20:56:43 -0700393 case EVENT_BIND_DEFAULT_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800394 case EVENT_FETCH_DEFAULT_TIMEOUT: {
395 loge("Bind/fetch time out from " + mPlatformCarrierConfigPackage);
Rambo Wang610fdf62021-03-08 21:37:11 -0800396 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT, getMessageToken(phoneId));
chen xuf60b3162019-05-01 21:31:05 -0700397 // If we attempted to bind to the app, but the service connection is null due to
398 // the race condition that clear config event happens before bind/fetch complete
399 // then config was cleared while we were waiting and we should not continue.
400 if (mServiceConnection[phoneId] != null) {
401 // If a ResponseReceiver callback is in the queue when this happens, we will
402 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700403 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700404 broadcastConfigChangedIntent(phoneId);
405 }
Nathan Harold1122e3d2021-01-22 15:45:24 -0800406 // Put a stub bundle in place so that the rest of the logic continues smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000407 mConfigFromDefaultApp[phoneId] = new PersistableBundle();
Jack Yue37dd262022-12-16 11:53:37 -0800408 updateSubscriptionDatabase(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700409 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700410 }
Jonathan Basseri930701e2015-06-11 20:56:43 -0700411
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800412 case EVENT_FETCH_DEFAULT_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700413 // If we attempted to bind to the app, but the service connection is null, then
414 // config was cleared while we were waiting and we should not continue.
415 if (!msg.getData().getBoolean("loaded_from_xml", false)
416 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800417 break;
418 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700419 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700420 if (carrierPackageName != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800421 logd("Found carrier config app: " + carrierPackageName);
Jordan Liu38a614c2019-03-20 15:01:17 -0700422 sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId, -1));
Jonathan Basserib919e932015-05-27 16:53:08 +0000423 } else {
Jack Yue37dd262022-12-16 11:53:37 -0800424 updateSubscriptionDatabase(phoneId);
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700425 }
426 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700427 }
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700428
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800429 case EVENT_DO_FETCH_CARRIER: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700430 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700431 final PersistableBundle config =
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100432 restoreConfigFromXml(carrierPackageName, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700433 if (config != null) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700434 mConfigFromCarrierApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700435 Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700436 newMsg.getData().putBoolean("loaded_from_xml", true);
437 sendMessage(newMsg);
438 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700439 // No cached config, so fetch it from a carrier app.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800440 if (carrierPackageName != null && bindToConfigPackage(carrierPackageName,
441 phoneId, EVENT_CONNECTED_TO_CARRIER)) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700442 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800443 obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1 /*arg2*/,
444 getMessageToken(phoneId)),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700445 BIND_TIMEOUT_MILLIS);
446 } else {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800447 // Put a stub bundle in place so that the rest of the logic continues
448 // smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000449 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
Jonathan Basseri65273c82017-07-25 15:08:42 -0700450 // Send broadcast if bind fails.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700451 broadcastConfigChangedIntent(phoneId);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800452 loge("Bind to carrier app: " + carrierPackageName + " fails");
Jack Yue37dd262022-12-16 11:53:37 -0800453 updateSubscriptionDatabase(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700454 }
455 }
456 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700457 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700458
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800459 case EVENT_CONNECTED_TO_CARRIER: {
Rambo Wang610fdf62021-03-08 21:37:11 -0800460 removeMessages(EVENT_BIND_CARRIER_TIMEOUT, getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700461 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800462 // If new service connection has been created, unbind.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700463 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700464 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800465 break;
466 }
chen xu02581692018-11-11 19:03:44 -0800467 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700468 // ResultReceiver callback will execute in this Handler's thread.
469 final ResultReceiver resultReceiver =
470 new ResultReceiver(this) {
471 @Override
472 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700473 unbindIfBound(mContext, conn, phoneId);
Rambo Wanga9d27a52021-04-09 16:17:33 -0700474 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT,
475 getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700476 // If new service connection has been created, this is stale.
477 if (mServiceConnection[phoneId] != conn) {
478 loge("Received response for stale request.");
479 return;
480 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700481 if (resultCode == RESULT_ERROR || resultData == null) {
482 // On error, abort config fetching.
chen xudb04c292019-03-26 17:01:15 -0700483 loge("Failed to get carrier config from carrier app: "
484 + getCarrierPackageForPhoneId(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700485 broadcastConfigChangedIntent(phoneId);
Jack Yue37dd262022-12-16 11:53:37 -0800486 updateSubscriptionDatabase(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700487 return;
488 }
489 PersistableBundle config =
490 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100491 saveConfigToXml(getCarrierPackageForPhoneId(phoneId), "",
492 phoneId, carrierId, config);
Amit Mahajan304e92b2021-04-08 14:03:30 -0700493 if (config != null) {
494 mConfigFromCarrierApp[phoneId] = config;
495 } else {
496 logdWithLocalLog("Config from carrier app is null "
497 + "for phoneId " + phoneId);
498 // Put a stub bundle in place so that the rest of the logic
499 // continues smoothly.
500 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
501 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700502 sendMessage(
503 obtainMessage(
504 EVENT_FETCH_CARRIER_DONE, phoneId, -1));
505 }
506 };
507 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800508 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700509 ICarrierService carrierService =
510 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800511 carrierService.getCarrierConfig(phoneId, carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800512 logdWithLocalLog("Fetch config for carrier app: "
chen xudb04c292019-03-26 17:01:15 -0700513 + getCarrierPackageForPhoneId(phoneId)
Jack Yu9be55d32023-08-10 13:47:46 -0700514 + ", carrierId=" + carrierId.getSpecificCarrierId());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700515 } catch (RemoteException e) {
Jack Yu9be55d32023-08-10 13:47:46 -0700516 loge("Failed to get carrier config: " + e);
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700517 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700518 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800519 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700520 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800521 obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1 /*arg2*/,
522 getMessageToken(phoneId)),
Jonathan Basseri65273c82017-07-25 15:08:42 -0700523 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800524 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700525 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800526
Jonathan Basseri930701e2015-06-11 20:56:43 -0700527 case EVENT_BIND_CARRIER_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800528 case EVENT_FETCH_CARRIER_TIMEOUT: {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800529 loge("Bind/fetch from carrier app timeout, package="
530 + getCarrierPackageForPhoneId(phoneId));
Rambo Wang610fdf62021-03-08 21:37:11 -0800531 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT, getMessageToken(phoneId));
chen xuf60b3162019-05-01 21:31:05 -0700532 // If we attempted to bind to the app, but the service connection is null due to
533 // the race condition that clear config event happens before bind/fetch complete
534 // then config was cleared while we were waiting and we should not continue.
535 if (mServiceConnection[phoneId] != null) {
536 // If a ResponseReceiver callback is in the queue when this happens, we will
537 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700538 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700539 broadcastConfigChangedIntent(phoneId);
540 }
Nathan Harold1122e3d2021-01-22 15:45:24 -0800541 // Put a stub bundle in place so that the rest of the logic continues smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000542 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
Jack Yue37dd262022-12-16 11:53:37 -0800543 updateSubscriptionDatabase(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700544 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700545 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800546 case EVENT_FETCH_CARRIER_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700547 // If we attempted to bind to the app, but the service connection is null, then
548 // config was cleared while we were waiting and we should not continue.
549 if (!msg.getData().getBoolean("loaded_from_xml", false)
550 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800551 break;
552 }
Jack Yue37dd262022-12-16 11:53:37 -0800553 updateSubscriptionDatabase(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800554 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700555 }
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700556
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800557 case EVENT_CHECK_SYSTEM_UPDATE: {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700558 SharedPreferences sharedPrefs =
559 PreferenceManager.getDefaultSharedPreferences(mContext);
560 final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
maxwen9594e082019-11-18 23:45:39 +0100561 if (!String.valueOf(Build.TIME).equals(lastFingerprint)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800562 logd(
maxwen9594e082019-11-18 23:45:39 +0100563 "Build time changed. old: "
Jonathan Basseri65273c82017-07-25 15:08:42 -0700564 + lastFingerprint
565 + " new: "
maxwen9594e082019-11-18 23:45:39 +0100566 + Build.TIME);
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700567 clearCachedConfigForPackage(null);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700568 sharedPrefs
569 .edit()
maxwen9594e082019-11-18 23:45:39 +0100570 .putString(KEY_FINGERPRINT, String.valueOf(Build.TIME))
Jonathan Basseri65273c82017-07-25 15:08:42 -0700571 .apply();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700572 }
573 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700574 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700575
576 case EVENT_SUBSCRIPTION_INFO_UPDATED:
577 broadcastConfigChangedIntent(phoneId);
578 break;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700579 case EVENT_MULTI_SIM_CONFIG_CHANGED:
580 onMultiSimConfigChanged();
581 break;
Jayachandran C645ec612020-01-10 10:30:25 -0800582
583 case EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG: {
584 PersistableBundle config =
585 restoreNoSimConfigFromXml(mPlatformCarrierConfigPackage);
586
587 if (config != null) {
Jayachandran C645ec612020-01-10 10:30:25 -0800588 mNoSimConfig = config;
589 sendMessage(
590 obtainMessage(
591 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE,
592 phoneId, -1));
593 } else {
594 // No cached config, so fetch it from the default app.
595 if (bindToConfigPackage(
596 mPlatformCarrierConfigPackage,
597 phoneId,
598 EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG)) {
599 sendMessageDelayed(
600 obtainMessage(
601 EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT,
602 phoneId, -1), BIND_TIMEOUT_MILLIS);
603 } else {
604 broadcastConfigChangedIntent(phoneId, false);
605 // TODO: We *must* call unbindService even if bindService returns false.
606 // (And possibly if SecurityException was thrown.)
607 loge("binding to default app to fetch no SIM config: "
608 + mPlatformCarrierConfigPackage + " fails");
609 }
610 }
611 break;
612 }
613
614 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE: {
615 broadcastConfigChangedIntent(phoneId, false);
616 break;
617 }
618
619 case EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
620 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT: {
621 loge("Bind/fetch time out for no SIM config from "
622 + mPlatformCarrierConfigPackage);
623 removeMessages(EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
624 // If we attempted to bind to the app, but the service connection is null due to
625 // the race condition that clear config event happens before bind/fetch complete
626 // then config was cleared while we were waiting and we should not continue.
627 if (mServiceConnectionForNoSimConfig[phoneId] != null) {
628 // If a ResponseReceiver callback is in the queue when this happens, we will
629 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700630 unbindIfBoundForNoSimConfig(mContext,
631 mServiceConnectionForNoSimConfig[phoneId], phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800632 }
633 broadcastConfigChangedIntent(phoneId, false);
634 break;
635 }
636
637 case EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG: {
638 removeMessages(EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
639 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
640 // If new service connection has been created, unbind.
641 if (mServiceConnectionForNoSimConfig[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700642 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800643 break;
644 }
645
646 // ResultReceiver callback will execute in this Handler's thread.
647 final ResultReceiver resultReceiver =
648 new ResultReceiver(this) {
649 @Override
650 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700651 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800652 // If new service connection has been created, this is stale.
653 if (mServiceConnectionForNoSimConfig[phoneId] != conn) {
654 loge("Received response for stale request.");
655 return;
656 }
657 removeMessages(EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
658 if (resultCode == RESULT_ERROR || resultData == null) {
659 // On error, abort config fetching.
660 loge("Failed to get no SIM carrier config");
661 return;
662 }
663 PersistableBundle config =
664 resultData.getParcelable(KEY_CONFIG_BUNDLE);
665 saveNoSimConfigToXml(mPlatformCarrierConfigPackage, config);
666 mNoSimConfig = config;
667 sendMessage(
668 obtainMessage(
669 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE,
670 phoneId, -1));
671 }
672 };
673 // Now fetch the config asynchronously from the ICarrierService.
674 try {
675 ICarrierService carrierService =
676 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800677 carrierService.getCarrierConfig(phoneId, null, resultReceiver);
Jayachandran C645ec612020-01-10 10:30:25 -0800678 logdWithLocalLog("Fetch no sim config from default app: "
679 + mPlatformCarrierConfigPackage);
680 } catch (RemoteException e) {
681 loge("Failed to get no sim carrier config from default app: " +
Jack Yu9be55d32023-08-10 13:47:46 -0700682 mPlatformCarrierConfigPackage + " err: " + e);
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700683 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800684 break; // So we don't set a timeout.
685 }
686 sendMessageDelayed(
687 obtainMessage(
688 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT,
689 phoneId, -1), BIND_TIMEOUT_MILLIS);
690 break;
691 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800692 }
693 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700694 }
695
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000696 @NonNull private final Handler mHandler;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800697
joonhunshin21a86812023-12-10 08:21:25 +0000698 @NonNull private final FeatureFlags mFeatureFlags;
699
700 @NonNull private final PackageManager mPackageManager;
joonhunshinf624b2a2024-04-18 04:42:12 +0000701 private final int mVendorApiLevel;
joonhunshin21a86812023-12-10 08:21:25 +0000702
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800703 /**
704 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
705 * receiver for relevant events.
706 */
Rambo Wang7e3bc122021-03-23 09:24:38 -0700707 @VisibleForTesting
joonhunshin21a86812023-12-10 08:21:25 +0000708 /* package */ CarrierConfigLoader(@NonNull Context context, @NonNull Looper looper,
709 @NonNull FeatureFlags featureFlags) {
mattgilbride5b6b7182023-04-03 18:56:44 +0000710 super(PermissionEnforcer.fromContext(context));
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800711 mContext = context;
Meng Wang33ad2bc2017-03-16 20:21:20 -0700712 mPlatformCarrierConfigPackage =
713 mContext.getString(R.string.platform_carrier_config_package);
Rambo Wang7e3bc122021-03-23 09:24:38 -0700714 mHandler = new ConfigHandler(looper);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800715
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000716 IntentFilter systemEventsFilter = new IntentFilter();
717 systemEventsFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
Rambo Wanga27fbe52022-04-12 19:09:07 +0000718 context.registerReceiver(mSystemBroadcastReceiver, systemEventsFilter);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800719
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000720 mNumPhones = TelephonyManager.from(context).getActiveModemCount();
721 mConfigFromDefaultApp = new PersistableBundle[mNumPhones];
722 mConfigFromCarrierApp = new PersistableBundle[mNumPhones];
723 mPersistentOverrideConfigs = new PersistableBundle[mNumPhones];
724 mOverrideConfigs = new PersistableBundle[mNumPhones];
Rambo Wang42026112021-04-07 20:57:28 +0000725 mNoSimConfig = new PersistableBundle();
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000726 mServiceConnection = new CarrierServiceConnection[mNumPhones];
727 mServiceBound = new boolean[mNumPhones];
728 mHasSentConfigChange = new boolean[mNumPhones];
729 mFromSystemUnlocked = new boolean[mNumPhones];
730 mServiceConnectionForNoSimConfig = new CarrierServiceConnection[mNumPhones];
731 mServiceBoundForNoSimConfig = new boolean[mNumPhones];
Rambo Wanga27fbe52022-04-12 19:09:07 +0000732 mCarrierServiceChangeCallbacks = new CarrierServiceChangeCallback[mNumPhones];
733 for (int phoneId = 0; phoneId < mNumPhones; phoneId++) {
734 mCarrierServiceChangeCallbacks[phoneId] = new CarrierServiceChangeCallback(phoneId);
735 TelephonyManager.from(context).registerCarrierPrivilegesCallback(phoneId,
736 new HandlerExecutor(mHandler), mCarrierServiceChangeCallbacks[phoneId]);
737 }
joonhunshin21a86812023-12-10 08:21:25 +0000738 mFeatureFlags = featureFlags;
739 mPackageManager = context.getPackageManager();
joonhunshinf624b2a2024-04-18 04:42:12 +0000740 mVendorApiLevel = SystemProperties.getInt(
741 "ro.vendor.api_level", Build.VERSION.DEVICE_INITIAL_SDK_INT);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800742 logd("CarrierConfigLoader has started");
Jack Yu771c6ce2023-02-02 17:51:42 -0800743
744 PhoneConfigurationManager.registerForMultiSimConfigChange(
745 mHandler, EVENT_MULTI_SIM_CONFIG_CHANGED, null);
746
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700747 mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800748 }
749
750 /**
751 * Initialize the singleton CarrierConfigLoader instance.
752 *
753 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
754 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000755 @NonNull
joonhunshin21a86812023-12-10 08:21:25 +0000756 /* package */ static CarrierConfigLoader init(@NonNull Context context,
757 @NonNull FeatureFlags featureFlags) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800758 synchronized (CarrierConfigLoader.class) {
759 if (sInstance == null) {
joonhunshin21a86812023-12-10 08:21:25 +0000760 sInstance = new CarrierConfigLoader(context, Looper.myLooper(), featureFlags);
Brad Ebingerfa6575f2021-05-04 00:29:50 +0000761 // Make this service available through ServiceManager.
762 TelephonyFrameworkInitializer.getTelephonyServiceManager()
763 .getCarrierConfigServiceRegisterer().register(sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800764 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700765 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800766 }
767 return sInstance;
768 }
769 }
770
Rambo Wang7e3bc122021-03-23 09:24:38 -0700771 @VisibleForTesting
772 /* package */ void clearConfigForPhone(int phoneId, boolean fetchNoSimConfig) {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700773 /* Ignore clear configuration request if device is being shutdown. */
774 Phone phone = PhoneFactory.getPhone(phoneId);
775 if (phone != null) {
776 if (phone.isShuttingDown()) {
777 return;
778 }
779 }
780
Jack Yu771c6ce2023-02-02 17:51:42 -0800781 if (mConfigFromDefaultApp.length <= phoneId) {
782 Log.wtf(LOG_TAG, "Invalid phone id " + phoneId);
783 return;
784 }
785
Malcolm Chen5ea18532019-10-24 19:55:44 -0700786 mConfigFromDefaultApp[phoneId] = null;
787 mConfigFromCarrierApp[phoneId] = null;
788 mServiceConnection[phoneId] = null;
789 mHasSentConfigChange[phoneId] = false;
790
Jayachandran C645ec612020-01-10 10:30:25 -0800791 if (fetchNoSimConfig) {
792 // To fetch no SIM config
793 mHandler.sendMessage(
794 mHandler.obtainMessage(
795 EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG, phoneId, -1));
796 }
Malcolm Chen5ea18532019-10-24 19:55:44 -0700797 }
798
Jack Yue37dd262022-12-16 11:53:37 -0800799 private void updateSubscriptionDatabase(int phoneId) {
Jack Yu90813e62023-01-17 17:46:27 -0800800 logd("updateSubscriptionDatabase: phoneId=" + phoneId);
Jack Yue37dd262022-12-16 11:53:37 -0800801 String configPackageName;
Nathan Harold48ac0972019-03-13 22:33:01 -0700802 PersistableBundle configToSend;
803 int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
804 // Prefer the carrier privileged carrier app, but if there is not one, use the platform
805 // default carrier app.
806 if (mConfigFromCarrierApp[phoneId] != null) {
Jack Yue37dd262022-12-16 11:53:37 -0800807 configPackageName = getCarrierPackageForPhoneId(phoneId);
Nathan Harold48ac0972019-03-13 22:33:01 -0700808 configToSend = mConfigFromCarrierApp[phoneId];
809 } else {
Jack Yue37dd262022-12-16 11:53:37 -0800810 configPackageName = mPlatformCarrierConfigPackage;
Nathan Harold48ac0972019-03-13 22:33:01 -0700811 configToSend = mConfigFromDefaultApp[phoneId];
812 }
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700813
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800814 if (configToSend == null) {
Rambo Wang42026112021-04-07 20:57:28 +0000815 configToSend = new PersistableBundle();
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800816 }
817
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700818 // mOverrideConfigs is for testing. And it will override current configs.
819 PersistableBundle config = mOverrideConfigs[phoneId];
820 if (config != null) {
Torbjorn Eklund1050cb02018-11-16 14:05:38 +0100821 configToSend = new PersistableBundle(configToSend);
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700822 configToSend.putAll(config);
823 }
824
Jack Yu3beaf9d2023-04-14 09:17:27 -0700825 SubscriptionManagerService.getInstance().updateSubscriptionByCarrierConfig(
826 phoneId, configPackageName, configToSend,
827 () -> mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1)
828 .sendToTarget());
Nathan Harold48ac0972019-03-13 22:33:01 -0700829 }
830
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800831 private void broadcastConfigChangedIntent(int phoneId) {
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800832 broadcastConfigChangedIntent(phoneId, true);
833 }
834
835 private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) {
rambowangd63ba342022-10-02 11:21:08 -0500836 int subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
837 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
838 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
839
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800840 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800841 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
chen xuf9db49f2019-03-31 23:04:42 -0700842 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800843 if (addSubIdExtra) {
rambowangd63ba342022-10-02 11:21:08 -0500844 int simApplicationState = getSimApplicationStateForPhone(phoneId);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800845 // Include subId/carrier id extra only if SIM records are loaded
846 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
847 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
rambowangd63ba342022-10-02 11:21:08 -0500848 subId = SubscriptionManager.getSubscriptionId(phoneId);
849 carrierId = getCarrierIdForPhoneId(phoneId);
850 specificCarrierId = getSpecificCarrierIdForPhoneId(phoneId);
851 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID, specificCarrierId);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800852 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
rambowangd63ba342022-10-02 11:21:08 -0500853 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, carrierId);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800854 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800855 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800856 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Sarah Chin807d5c62020-03-30 17:21:09 -0700857 intent.putExtra(CarrierConfigManager.EXTRA_REBROADCAST_ON_UNLOCK,
858 mFromSystemUnlocked[phoneId]);
rambowangd63ba342022-10-02 11:21:08 -0500859
860 TelephonyRegistryManager trm = mContext.getSystemService(TelephonyRegistryManager.class);
861 // Unlike broadcast, we wouldn't notify registrants on carrier config change when device is
862 // unlocked. Only real carrier config change will send the notification to registrants.
863 if (trm != null && !mFromSystemUnlocked[phoneId]) {
864 trm.notifyCarrierConfigChanged(phoneId, subId, carrierId, specificCarrierId);
865 }
866
Meng Wang97a6a462020-01-23 16:22:16 -0800867 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
rambowangd63ba342022-10-02 11:21:08 -0500868
Jack Yu00ece8c2022-11-19 22:29:12 -0800869 if (SubscriptionManager.isValidSubscriptionId(subId)) {
870 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId + ", subId=" + subId);
Jack Yue9e95be2020-03-22 10:56:06 -0700871 } else {
872 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
873 }
Junda Liu03aad762017-07-21 13:32:17 -0700874 mHasSentConfigChange[phoneId] = true;
Sarah Chin807d5c62020-03-30 17:21:09 -0700875 mFromSystemUnlocked[phoneId] = false;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800876 }
877
rambowangd63ba342022-10-02 11:21:08 -0500878 private int getSimApplicationStateForPhone(int phoneId) {
879 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
880 int subId = SubscriptionManager.getSubscriptionId(phoneId);
881 if (SubscriptionManager.isValidSubscriptionId(subId)) {
882 TelephonyManager telMgr = TelephonyManager.from(mContext)
883 .createForSubscriptionId(subId);
884 simApplicationState = telMgr.getSimApplicationState();
885 }
886 return simApplicationState;
887 }
888
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800889 /** Binds to the default or carrier config app. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000890 private boolean bindToConfigPackage(@NonNull String pkgName, int phoneId, int eventId) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800891 logdWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700892 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700893 carrierService.setPackage(pkgName);
Jayachandran C645ec612020-01-10 10:30:25 -0800894 CarrierServiceConnection serviceConnection = new CarrierServiceConnection(
895 phoneId, pkgName, eventId);
896 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
897 mServiceConnectionForNoSimConfig[phoneId] = serviceConnection;
898 } else {
899 mServiceConnection[phoneId] = serviceConnection;
900 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800901 try {
Jayachandran C645ec612020-01-10 10:30:25 -0800902 if (mContext.bindService(carrierService, serviceConnection,
Jordan Liu178430d2020-02-10 14:32:15 -0800903 Context.BIND_AUTO_CREATE)) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700904 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
905 mServiceBoundForNoSimConfig[phoneId] = true;
906 } else {
907 mServiceBound[phoneId] = true;
908 }
Jordan Liu178430d2020-02-10 14:32:15 -0800909 return true;
910 } else {
911 return false;
912 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800913 } catch (SecurityException ex) {
914 return false;
915 }
916 }
917
Rambo Wang7e3bc122021-03-23 09:24:38 -0700918 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000919 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -0700920 /* package */ CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800921 String mcc = "";
922 String mnc = "";
923 String imsi = "";
924 String gid1 = "";
925 String gid2 = "";
926 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
927 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800928 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800929 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700930 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
931 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800932 mcc = simOperator.substring(0, 3);
933 mnc = simOperator.substring(3);
934 }
935 Phone phone = PhoneFactory.getPhone(phoneId);
936 if (phone != null) {
937 imsi = phone.getSubscriberId();
938 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700939 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800940 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800941 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800942 }
chen xua31f22b2019-03-06 15:28:50 -0800943 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800944 }
945
Rambo Wangf7c214a2022-03-17 12:36:22 -0700946 /** Returns the package name of a privileged carrier app, or null if there is none. */
Nathan Harold1122e3d2021-01-22 15:45:24 -0800947 @Nullable
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700948 private String getCarrierPackageForPhoneId(int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -0700949 final long token = Binder.clearCallingIdentity();
950 try {
Rambo Wangf7c214a2022-03-17 12:36:22 -0700951 return TelephonyManager.from(mContext)
952 .getCarrierServicePackageNameForLogicalSlot(phoneId);
Nazanin1adf4562021-03-29 15:35:30 -0700953 } finally {
954 Binder.restoreCallingIdentity(token);
955 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700956 }
957
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000958 @Nullable
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700959 private String getIccIdForPhoneId(int phoneId) {
960 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
961 return null;
962 }
963 Phone phone = PhoneFactory.getPhone(phoneId);
964 if (phone == null) {
965 return null;
966 }
967 return phone.getIccSerialNumber();
968 }
969
chen xu02581692018-11-11 19:03:44 -0800970 /**
chen xua31f22b2019-03-06 15:28:50 -0800971 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800972 */
chen xua31f22b2019-03-06 15:28:50 -0800973 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700974 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800975 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700976 }
977 Phone phone = PhoneFactory.getPhone(phoneId);
978 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800979 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700980 }
chen xua31f22b2019-03-06 15:28:50 -0800981 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800982 }
983
984 /**
985 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
986 */
987 private int getCarrierIdForPhoneId(int phoneId) {
988 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
989 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700990 }
chen xu02581692018-11-11 19:03:44 -0800991 Phone phone = PhoneFactory.getPhone(phoneId);
992 if (phone == null) {
993 return TelephonyManager.UNKNOWN_CARRIER_ID;
994 }
995 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700996 }
997
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700998 /**
999 * Writes a bundle to an XML file.
1000 *
chen xu02581692018-11-11 19:03:44 -08001001 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -08001002 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -08001003 * should have a single copy of XML file named after carrier id. However, it's still possible
1004 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
1005 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
1006 * carrier while iccid remains the same.
1007 *
1008 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
1009 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001010 *
1011 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001012 *
Jayachandran C645ec612020-01-10 10:30:25 -08001013 * @param packageName the name of the package from which we fetched this bundle.
1014 * @param extraString An extra string to be used in the XML file name.
1015 * @param phoneId the phone ID.
1016 * @param carrierId contains all carrier-identifying information.
1017 * @param config the bundle to be written. Null will be treated as an empty bundle.
1018 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001019 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001020 private void saveConfigToXml(@Nullable String packageName, @NonNull String extraString,
1021 int phoneId, @Nullable CarrierIdentifier carrierId, @NonNull PersistableBundle config,
1022 boolean isNoSimConfig) {
Jayachandran C645ec612020-01-10 10:30:25 -08001023 if (packageName == null) {
Jack Yu9be55d32023-08-10 13:47:46 -07001024 loge("Cannot save config with null packageName. phoneId=" + phoneId);
Yuchen Dongc15afed2018-04-26 17:05:22 +08001025 return;
1026 }
1027
Jayachandran C645ec612020-01-10 10:30:25 -08001028 String fileName;
1029 if (isNoSimConfig) {
1030 fileName = getFilenameForNoSimConfig(packageName);
1031 } else {
Jack Yuf5badd92022-12-08 00:50:53 -08001032 if (TelephonyManager.getSimStateForSlotIndex(phoneId)
Jayachandran C645ec612020-01-10 10:30:25 -08001033 != TelephonyManager.SIM_STATE_LOADED) {
Jack Yu9be55d32023-08-10 13:47:46 -07001034 loge("Skip saving config because SIM records are not loaded. phoneId=" + phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001035 return;
1036 }
1037
1038 final String iccid = getIccIdForPhoneId(phoneId);
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001039 final int cid = carrierId != null ? carrierId.getSpecificCarrierId()
1040 : TelephonyManager.UNKNOWN_CARRIER_ID;
Jayachandran C645ec612020-01-10 10:30:25 -08001041 if (iccid == null) {
Jack Yu9be55d32023-08-10 13:47:46 -07001042 loge("Cannot save config with null iccid. phoneId=" + phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001043 return;
1044 }
1045 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001046 }
Jayachandran C645ec612020-01-10 10:30:25 -08001047
Junda Liu02596502016-11-15 13:46:43 -08001048 // b/32668103 Only save to file if config isn't empty.
1049 // In case of failure, not caching an empty bundle will
1050 // try loading config again on next power on or sim loaded.
1051 // Downside is for genuinely empty bundle, will bind and load
1052 // on every power on.
1053 if (config == null || config.isEmpty()) {
1054 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001055 }
1056
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001057 final String version = getPackageVersion(packageName);
1058 if (version == null) {
Jack Yu9be55d32023-08-10 13:47:46 -07001059 loge("Failed to get package version for: " + packageName + ", phoneId=" + phoneId);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001060 return;
1061 }
1062
Jack Yu9be55d32023-08-10 13:47:46 -07001063 logdWithLocalLog("Save carrier config to cache. phoneId=" + phoneId
1064 + ", xml=" + getFilePathForLogging(fileName) + ", version=" + version);
chen xudb04c292019-03-26 17:01:15 -07001065
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001066 FileOutputStream outFile = null;
1067 try {
Jayachandran C645ec612020-01-10 10:30:25 -08001068 outFile = new FileOutputStream(new File(mContext.getFilesDir(), fileName));
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001069 config.putString(KEY_VERSION, version);
1070 config.writeToStream(outFile);
1071 outFile.flush();
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001072 outFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001073 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001074 loge(e.toString());
1075 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001076 }
1077
Rambo Wang7e3bc122021-03-23 09:24:38 -07001078 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001079 /* package */ void saveConfigToXml(@Nullable String packageName, @NonNull String extraString,
1080 int phoneId, @NonNull CarrierIdentifier carrierId, @NonNull PersistableBundle config) {
Jayachandran C645ec612020-01-10 10:30:25 -08001081 saveConfigToXml(packageName, extraString, phoneId, carrierId, config, false);
1082 }
1083
Rambo Wang7e3bc122021-03-23 09:24:38 -07001084 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001085 /* package */ void saveNoSimConfigToXml(@Nullable String packageName,
1086 @NonNull PersistableBundle config) {
Jayachandran C645ec612020-01-10 10:30:25 -08001087 saveConfigToXml(packageName, "", -1, null, config, true);
1088 }
1089
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001090 /**
1091 * Reads a bundle from an XML file.
1092 *
1093 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +08001094 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001095 *
1096 * In case of errors, or if the saved config is from a different package version than the
1097 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001098 *
Jayachandran C645ec612020-01-10 10:30:25 -08001099 * @param packageName the name of the package from which we fetched this bundle.
1100 * @param extraString An extra string to be used in the XML file name.
1101 * @param phoneId the phone ID.
1102 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001103 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001104 * version does not match, or reading config fails.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001105 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001106 @Nullable
1107 private PersistableBundle restoreConfigFromXml(@Nullable String packageName,
1108 @NonNull String extraString, int phoneId, boolean isNoSimConfig) {
Jayachandran C645ec612020-01-10 10:30:25 -08001109 if (packageName == null) {
1110 loge("Cannot restore config with null packageName");
1111 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001112 final String version = getPackageVersion(packageName);
1113 if (version == null) {
1114 loge("Failed to get package version for: " + packageName);
1115 return null;
1116 }
Yuchen Dongc15afed2018-04-26 17:05:22 +08001117
Jayachandran C645ec612020-01-10 10:30:25 -08001118 String fileName;
arunvoddu664c36a2021-10-11 20:09:28 +00001119 String iccid = null;
Jayachandran C645ec612020-01-10 10:30:25 -08001120 if (isNoSimConfig) {
1121 fileName = getFilenameForNoSimConfig(packageName);
1122 } else {
Jack Yuf5badd92022-12-08 00:50:53 -08001123 if (TelephonyManager.getSimStateForSlotIndex(phoneId)
Jayachandran C645ec612020-01-10 10:30:25 -08001124 != TelephonyManager.SIM_STATE_LOADED) {
Jack Yu9be55d32023-08-10 13:47:46 -07001125 loge("Skip restore config because SIM records are not loaded. phoneId=" + phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001126 return null;
1127 }
1128
arunvoddu664c36a2021-10-11 20:09:28 +00001129 iccid = getIccIdForPhoneId(phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001130 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1131 if (iccid == null) {
Jack Yu9be55d32023-08-10 13:47:46 -07001132 loge("Cannot restore config with null iccid. phoneId=" + phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001133 return null;
1134 }
1135 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001136 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001137
1138 PersistableBundle restoredBundle = null;
Mateus Azisa13d2502023-05-25 08:44:38 -07001139 File file = new File(mContext.getFilesDir(), fileName);
Jack Yu9be55d32023-08-10 13:47:46 -07001140 String filePath = file.getPath();
1141 String savedVersion = null;
Mateus Azisa13d2502023-05-25 08:44:38 -07001142 try (FileInputStream inFile = new FileInputStream(file)) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001143
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001144 restoredBundle = PersistableBundle.readFromStream(inFile);
Jack Yu9be55d32023-08-10 13:47:46 -07001145 savedVersion = restoredBundle.getString(KEY_VERSION);
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001146 restoredBundle.remove(KEY_VERSION);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001147
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001148 if (!version.equals(savedVersion)) {
Jack Yu9be55d32023-08-10 13:47:46 -07001149 loge("Saved version mismatch: " + version + " vs " + savedVersion
1150 + ", phoneId=" + phoneId);
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001151 restoredBundle = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001152 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001153 } catch (FileNotFoundException e) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001154 // Missing file is normal occurrence that might occur with a new sim or when restoring
1155 // an override file during boot and should not be treated as an error.
Mateus Azisa13d2502023-05-25 08:44:38 -07001156 if (isNoSimConfig) {
Jack Yu9be55d32023-08-10 13:47:46 -07001157 logd("File not found: " + file.getPath() + ", phoneId=" + phoneId);
Mateus Azisa13d2502023-05-25 08:44:38 -07001158 } else {
Jack Yu9be55d32023-08-10 13:47:46 -07001159 logd("File not found : " + getFilePathForLogging(filePath, iccid)
1160 + ", phoneId=" + phoneId);
arunvoddu664c36a2021-10-11 20:09:28 +00001161 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001162 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001163 loge(e.toString());
1164 }
1165
Jack Yu9be55d32023-08-10 13:47:46 -07001166 if (restoredBundle != null) {
1167 logdWithLocalLog("Restored carrier config from cache. phoneId=" + phoneId + ", xml="
1168 + getFilePathForLogging(fileName) + ", version=" + savedVersion
1169 + ", modified time=" + getFileTime(filePath));
1170 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001171 return restoredBundle;
1172 }
1173
arunvoddu664c36a2021-10-11 20:09:28 +00001174 /**
1175 * This method will mask most part of iccid in the filepath for logging on userbuild
1176 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001177 @NonNull
1178 private String getFilePathForLogging(@Nullable String filePath, @Nullable String iccid) {
arunvoddu664c36a2021-10-11 20:09:28 +00001179 // If loggable then return with actual file path
Jack Yu9be55d32023-08-10 13:47:46 -07001180 if (TelephonyUtils.IS_DEBUGGABLE) {
arunvoddu664c36a2021-10-11 20:09:28 +00001181 return filePath;
1182 }
1183 String path = filePath;
1184 int length = (iccid != null) ? iccid.length() : 0;
1185 if (length > 5 && filePath != null) {
1186 path = filePath.replace(iccid.substring(5), "***************");
1187 }
1188 return path;
1189 }
1190
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001191 @Nullable
1192 private PersistableBundle restoreConfigFromXml(@Nullable String packageName,
1193 @NonNull String extraString, int phoneId) {
Jayachandran C645ec612020-01-10 10:30:25 -08001194 return restoreConfigFromXml(packageName, extraString, phoneId, false);
1195 }
1196
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001197 @Nullable
1198 private PersistableBundle restoreNoSimConfigFromXml(@Nullable String packageName) {
Jayachandran C645ec612020-01-10 10:30:25 -08001199 return restoreConfigFromXml(packageName, "", -1, true);
1200 }
1201
Junda Liu8a8a53a2015-05-15 16:19:57 -07001202 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001203 * Clears cached carrier config.
1204 * This deletes all saved XML files associated with the given package name. If packageName is
1205 * null, then it deletes all saved XML files.
1206 *
1207 * @param packageName the name of a carrier package, or null if all cached config should be
1208 * cleared.
1209 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -07001210 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001211 private boolean clearCachedConfigForPackage(@Nullable final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001212 File dir = mContext.getFilesDir();
1213 File[] packageFiles = dir.listFiles(new FilenameFilter() {
1214 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001215 if (packageName != null) {
1216 return filename.startsWith("carrierconfig-" + packageName + "-");
1217 } else {
1218 return filename.startsWith("carrierconfig-");
1219 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001220 }
1221 });
Junda Liu8a8a53a2015-05-15 16:19:57 -07001222 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001223 for (File f : packageFiles) {
Jack Yu9be55d32023-08-10 13:47:46 -07001224 logdWithLocalLog("Deleting " + getFilePathForLogging(f.getName()));
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001225 f.delete();
1226 }
Junda Liu8a8a53a2015-05-15 16:19:57 -07001227 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001228 }
1229
arunvoddub7c6e8e2022-09-05 15:48:06 +00001230 private String getFilePathForLogging(String filePath) {
1231 if (!TextUtils.isEmpty(filePath)) {
1232 String[] fileTokens = filePath.split("-");
1233 if (fileTokens != null && fileTokens.length > 2) {
1234 String iccid = fileTokens[fileTokens.length -2];
1235 return getFilePathForLogging(filePath, iccid);
1236 }
1237 return filePath;
1238 }
1239 return filePath;
1240 }
1241
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001242 /** Builds a canonical file name for a config file. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001243 @NonNull
Jayachandran C645ec612020-01-10 10:30:25 -08001244 private static String getFilenameForConfig(
1245 @NonNull String packageName, @NonNull String extraString,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001246 @NonNull String iccid, int cid) {
chen xu02581692018-11-11 19:03:44 -08001247 // the same carrier should have a single copy of XML file named after carrier id.
1248 // However, it's still possible that platform doesn't recognize the current sim carrier,
1249 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
1250 // cases SIM OTA resolves to different carrier while iccid remains the same.
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001251 return "carrierconfig-" + packageName + extraString + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001252 }
1253
Jayachandran C645ec612020-01-10 10:30:25 -08001254 /** Builds a canonical file name for no SIM config file. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001255 @NonNull
Jayachandran C645ec612020-01-10 10:30:25 -08001256 private String getFilenameForNoSimConfig(@NonNull String packageName) {
1257 return "carrierconfig-" + packageName + "-" + "nosim" + ".xml";
1258 }
1259
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001260 /** Return the current version code of a package, or null if the name is not found. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001261 @Nullable
1262 private String getPackageVersion(@NonNull String packageName) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001263 try {
1264 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -08001265 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001266 } catch (PackageManager.NameNotFoundException e) {
1267 return null;
1268 }
1269 }
1270
Jonathan Basseri65273c82017-07-25 15:08:42 -07001271 /**
1272 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001273 *
1274 * This reads config bundles for the given phoneId. That means getting the latest bundle from
1275 * the default app and a privileged carrier app, if present. This will not bind to an app if we
1276 * have a saved config file to use instead.
1277 */
1278 private void updateConfigForPhoneId(int phoneId) {
Jonathan Basseri65273c82017-07-25 15:08:42 -07001279 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001280 }
1281
Malcolm Chen5ea18532019-10-24 19:55:44 -07001282 private void onMultiSimConfigChanged() {
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001283 int oldNumPhones = mNumPhones;
1284 mNumPhones = TelephonyManager.from(mContext).getActiveModemCount();
1285 if (mNumPhones == oldNumPhones) {
1286 return;
1287 }
1288 logdWithLocalLog("mNumPhones change from " + oldNumPhones + " to " + mNumPhones);
1289
Rambo Wanga27fbe52022-04-12 19:09:07 +00001290 // If DS -> SS switch, release the resources BEFORE truncating the arrays to avoid leaking
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001291 for (int phoneId = mNumPhones; phoneId < oldNumPhones; phoneId++) {
1292 if (mServiceConnection[phoneId] != null) {
1293 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
1294 }
1295 if (mServiceConnectionForNoSimConfig[phoneId] != null) {
1296 unbindIfBoundForNoSimConfig(mContext, mServiceConnectionForNoSimConfig[phoneId],
1297 phoneId);
1298 }
1299 }
1300
Rambo Wanga27fbe52022-04-12 19:09:07 +00001301 // The phone to slot mapping may change, unregister here and re-register callbacks later
1302 for (int phoneId = 0; phoneId < oldNumPhones; phoneId++) {
1303 if (mCarrierServiceChangeCallbacks[phoneId] != null) {
1304 TelephonyManager.from(mContext).unregisterCarrierPrivilegesCallback(
1305 mCarrierServiceChangeCallbacks[phoneId]);
1306 }
1307 }
1308
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001309 // Copy the original arrays, truncate or padding with zeros (if necessary) to new length
1310 mConfigFromDefaultApp = Arrays.copyOf(mConfigFromDefaultApp, mNumPhones);
1311 mConfigFromCarrierApp = Arrays.copyOf(mConfigFromCarrierApp, mNumPhones);
1312 mPersistentOverrideConfigs = Arrays.copyOf(mPersistentOverrideConfigs, mNumPhones);
1313 mOverrideConfigs = Arrays.copyOf(mOverrideConfigs, mNumPhones);
1314 mServiceConnection = Arrays.copyOf(mServiceConnection, mNumPhones);
1315 mServiceConnectionForNoSimConfig =
1316 Arrays.copyOf(mServiceConnectionForNoSimConfig, mNumPhones);
1317 mServiceBound = Arrays.copyOf(mServiceBound, mNumPhones);
1318 mServiceBoundForNoSimConfig = Arrays.copyOf(mServiceBoundForNoSimConfig, mNumPhones);
1319 mHasSentConfigChange = Arrays.copyOf(mHasSentConfigChange, mNumPhones);
1320 mFromSystemUnlocked = Arrays.copyOf(mFromSystemUnlocked, mNumPhones);
Rambo Wanga27fbe52022-04-12 19:09:07 +00001321 mCarrierServiceChangeCallbacks = Arrays.copyOf(mCarrierServiceChangeCallbacks, mNumPhones);
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001322
Rambo Wanga27fbe52022-04-12 19:09:07 +00001323 // Load the config for all the phones and re-register callback AFTER padding the arrays.
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001324 for (int phoneId = 0; phoneId < mNumPhones; phoneId++) {
1325 updateConfigForPhoneId(phoneId);
Rambo Wanga27fbe52022-04-12 19:09:07 +00001326 mCarrierServiceChangeCallbacks[phoneId] = new CarrierServiceChangeCallback(phoneId);
1327 TelephonyManager.from(mContext).registerCarrierPrivilegesCallback(phoneId,
1328 new HandlerExecutor(mHandler), mCarrierServiceChangeCallbacks[phoneId]);
Malcolm Chen5ea18532019-10-24 19:55:44 -07001329 }
1330 }
1331
Hall Liu506724b2018-10-22 18:16:14 -07001332 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001333 @NonNull
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001334 public PersistableBundle getConfigForSubId(int subscriptionId, @NonNull String callingPackage) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001335 return getConfigForSubIdWithFeature(subscriptionId, callingPackage, null);
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08001336 }
1337
1338 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001339 @NonNull
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001340 public PersistableBundle getConfigForSubIdWithFeature(int subscriptionId,
1341 @NonNull String callingPackage, @Nullable String callingFeatureId) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001342 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subscriptionId,
1343 callingPackage, callingFeatureId, "getCarrierConfig")) {
Rambo Wang42026112021-04-07 20:57:28 +00001344 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -07001345 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -07001346
Tomasz Wasilczyk751bb1b2024-06-04 12:03:46 -07001347 if (!mContext.getResources().getBoolean(
1348 com.android.internal.R.bool.config_force_phone_globals_creation)) {
1349 enforceTelephonyFeatureWithException(callingPackage, "getConfigForSubIdWithFeature");
1350 }
joonhunshin21a86812023-12-10 08:21:25 +00001351
Rambo Wangd2b004b2021-03-30 10:10:23 -07001352 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001353 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001354 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001355 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001356 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001357 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001358 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001359 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001360 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001361 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001362 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001363 config = mPersistentOverrideConfigs[phoneId];
1364 if (config != null) {
1365 retConfig.putAll(config);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001366 }
Hall Liu506724b2018-10-22 18:16:14 -07001367 config = mOverrideConfigs[phoneId];
1368 if (config != null) {
1369 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -07001370 }
Nathan Harold1122e3d2021-01-22 15:45:24 -08001371 // Ignore the theoretical case of the default app not being present since that won't
1372 // work in CarrierConfigLoader today.
1373 final boolean allConfigsApplied =
1374 (mConfigFromCarrierApp[phoneId] != null
1375 || getCarrierPackageForPhoneId(phoneId) == null)
1376 && mConfigFromDefaultApp[phoneId] != null;
1377 retConfig.putBoolean(
1378 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, allConfigsApplied);
Jayachandran C645ec612020-01-10 10:30:25 -08001379 } else {
1380 if (mNoSimConfig != null) {
1381 retConfig.putAll(mNoSimConfig);
1382 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001383 }
1384 return retConfig;
1385 }
1386
1387 @Override
rambowang14757c22022-10-03 11:54:56 -05001388 @NonNull
1389 public PersistableBundle getConfigSubsetForSubIdWithFeature(int subscriptionId,
1390 @NonNull String callingPackage, @Nullable String callingFeatureId,
1391 @NonNull String[] keys) {
1392 Objects.requireNonNull(callingPackage, "Calling package must be non-null");
1393 Objects.requireNonNull(keys, "Config keys must be non-null");
1394 enforceCallerIsSystemOrRequestingPackage(callingPackage);
1395
joonhunshin21a86812023-12-10 08:21:25 +00001396 enforceTelephonyFeatureWithException(callingPackage,
1397 "getConfigSubsetForSubIdWithFeature");
1398
rambowang14757c22022-10-03 11:54:56 -05001399 // Permission check is performed inside and an empty bundle will return on failure.
1400 // No SecurityException thrown here since most clients expect to retrieve the overridden
1401 // value if present or use default one if not
1402 PersistableBundle allConfigs = getConfigForSubIdWithFeature(subscriptionId, callingPackage,
1403 callingFeatureId);
1404 if (allConfigs.isEmpty()) {
1405 return allConfigs;
1406 }
1407 for (String key : keys) {
1408 Objects.requireNonNull(key, "Config key must be non-null");
rambowang14757c22022-10-03 11:54:56 -05001409 }
1410
1411 PersistableBundle configSubset = new PersistableBundle(
1412 keys.length + CONFIG_SUBSET_METADATA_KEYS.length);
1413 for (String carrierConfigKey : keys) {
1414 Object value = allConfigs.get(carrierConfigKey);
rambowangb49e90f2022-12-13 18:29:06 -06001415 if (value == null) {
1416 // Filter out keys without values.
1417 // In history, many AOSP or OEMs/carriers private configs didn't provide default
1418 // values. We have to continue supporting them for now. See b/261776046 for details.
1419 continue;
1420 }
rambowang14757c22022-10-03 11:54:56 -05001421 // Config value itself could be PersistableBundle which requires different API to put
1422 if (value instanceof PersistableBundle) {
1423 configSubset.putPersistableBundle(carrierConfigKey, (PersistableBundle) value);
1424 } else {
1425 configSubset.putObject(carrierConfigKey, value);
1426 }
1427 }
1428
1429 // Configs in CONFIG_SUBSET_ALWAYS_INCLUDED_KEYS should always be included
1430 for (String generalKey : CONFIG_SUBSET_METADATA_KEYS) {
1431 configSubset.putObject(generalKey, allConfigs.get(generalKey));
1432 }
1433
1434 return configSubset;
1435 }
1436
mattgilbride5b6b7182023-04-03 18:56:44 +00001437 @android.annotation.EnforcePermission(android.Manifest.permission.MODIFY_PHONE_STATE)
rambowang14757c22022-10-03 11:54:56 -05001438 @Override
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001439 public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides,
1440 boolean persistent) {
mattgilbride5b6b7182023-04-03 18:56:44 +00001441 overrideConfig_enforcePermission();
Hall Liu506724b2018-10-22 18:16:14 -07001442 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1443 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001444 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
Rambo Wangd2b004b2021-03-30 10:10:23 -07001445 throw new IllegalArgumentException(
1446 "Invalid phoneId " + phoneId + " for subId " + subscriptionId);
Hall Liu506724b2018-10-22 18:16:14 -07001447 }
joonhunshin21a86812023-12-10 08:21:25 +00001448
1449 enforceTelephonyFeatureWithException(getCurrentPackageName(), "overrideConfig");
1450
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001451 // Post to run on handler thread on which all states should be confined.
1452 mHandler.post(() -> {
1453 overrideConfig(mOverrideConfigs, phoneId, overrides);
Hall Liu506724b2018-10-22 18:16:14 -07001454
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001455 if (persistent) {
1456 overrideConfig(mPersistentOverrideConfigs, phoneId, overrides);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001457
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001458 if (overrides != null) {
1459 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
1460 saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION,
1461 phoneId,
1462 carrierId, mPersistentOverrideConfigs[phoneId]);
1463 } else {
1464 final String iccid = getIccIdForPhoneId(phoneId);
1465 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1466 String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage,
1467 OVERRIDE_PACKAGE_ADDITION, iccid, cid);
1468 File fileToDelete = new File(mContext.getFilesDir(), fileName);
1469 fileToDelete.delete();
1470 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001471 }
Jack Yu9be55d32023-08-10 13:47:46 -07001472 logdWithLocalLog("overrideConfig: subId=" + subscriptionId + ", persistent="
1473 + persistent + ", overrides=" + overrides);
Jack Yue37dd262022-12-16 11:53:37 -08001474 updateSubscriptionDatabase(phoneId);
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001475 });
Hall Liu506724b2018-10-22 18:16:14 -07001476 }
1477
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001478 private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId,
1479 @Nullable PersistableBundle overrides) {
1480 if (overrides == null) {
Rambo Wang42026112021-04-07 20:57:28 +00001481 currentOverrides[phoneId] = new PersistableBundle();
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001482 } else if (currentOverrides[phoneId] == null) {
1483 currentOverrides[phoneId] = overrides;
1484 } else {
1485 currentOverrides[phoneId].putAll(overrides);
1486 }
1487 }
1488
Hall Liu506724b2018-10-22 18:16:14 -07001489 @Override
Rambo Wangd2b004b2021-03-30 10:10:23 -07001490 public void notifyConfigChangedForSubId(int subscriptionId) {
Jordan Liud7d57fe2019-04-16 12:29:43 -07001491 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001492 // system privileged app with MODIFY_PHONE_STATE permission.
Rambo Wangd2b004b2021-03-30 10:10:23 -07001493 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext,
1494 subscriptionId, "Require carrier privileges or MODIFY_PHONE_STATE permission.");
1495
1496 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1497 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Rambo Wang0c386012024-02-22 22:38:17 +00001498 final String msg =
1499 "Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId;
rambowangdaf5b482024-09-04 12:15:20 -05001500 AnomalyReporter.reportAnomaly(
1501 UUID.fromString(UUID_NOTIFY_CONFIG_CHANGED_WITH_INVALID_PHONE), msg);
Rambo Wang0c386012024-02-22 22:38:17 +00001502 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();
Rambo Wangb7a95a42024-03-07 04:57:29 +00001761 if (TelephonyPermissions.isRootOrShell(callingUid)
1762 || TelephonyPermissions.isSystemOrPhone(
1763 callingUid)) {
1764 // Bug reports (dumpstate.cpp) run as SHELL, and let some other privileged UIDs
1765 // through as well.
Hunter Knepshielde601f432020-02-19 10:16:04 -08001766 return;
1767 }
Rambo Wangb7a95a42024-03-07 04:57:29 +00001768
Hunter Knepshielde601f432020-02-19 10:16:04 -08001769 // An app is trying to dump extra detail, block it if they aren't who they claim to be.
1770 AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class);
1771 if (appOps == null) {
1772 throw new SecurityException("No AppOps");
1773 }
1774 // Will throw a SecurityException if the UID and package don't match.
1775 appOps.checkPackage(callingUid, requestingPackage);
1776 }
1777
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001778 /**
1779 * Searches for one or more appropriate {@link CarrierService} instances to dump based on the
1780 * current connections.
1781 *
1782 * @param targetPkgName the target package name to dump carrier services for
1783 * @param considerCarrierPrivileges if true, allow a carrier service to be dumped if it shares
1784 * carrier privileges with {@code targetPkgName};
1785 * otherwise, only dump a carrier service if it is {@code
1786 * targetPkgName}
1787 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001788 private void dumpCarrierServiceIfBound(@NonNull FileDescriptor fd,
1789 @NonNull IndentingPrintWriter indentPW, @NonNull String prefix,
1790 @NonNull String targetPkgName, boolean considerCarrierPrivileges) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001791 // Null package is possible if it's early in the boot process, there was a recent crash, we
1792 // loaded the config from XML most recently, or a SIM slot is empty. Carrier apps with
1793 // long-lived bindings should typically get dumped here regardless. Even if an app is being
1794 // used for multiple phoneIds, we assume that it's smart enough to handle that on its own,
1795 // and that in most cases we'd just be dumping duplicate information and bloating a report.
1796 indentPW.increaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001797 indentPW.println(prefix + " : " + targetPkgName);
1798 Set<String> dumpedPkgNames = new ArraySet<>(mServiceConnection.length);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001799 for (CarrierServiceConnection connection : mServiceConnection) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001800 if (connection == null || !SubscriptionManager.isValidPhoneId(connection.phoneId)
1801 || TextUtils.isEmpty(connection.pkgName)) {
1802 continue;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001803 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001804 final String servicePkgName = connection.pkgName;
1805 // Note: we intentionally ignore system components here because we should NOT match the
1806 // shell caller that's typically used for bug reports via non-BugreportManager triggers.
1807 final boolean exactPackageMatch = TextUtils.equals(targetPkgName, servicePkgName);
1808 final boolean carrierPrivilegesMatch =
1809 considerCarrierPrivileges && hasCarrierPrivileges(targetPkgName,
1810 connection.phoneId);
1811 if (!exactPackageMatch && !carrierPrivilegesMatch) continue;
1812 // Make sure this service is actually alive before trying to dump it. We don't pay
1813 // attention to mServiceBound[connection.phoneId] because typically carrier apps will
1814 // request long-lived bindings, and even if we unbind the app, it may still be alive due
1815 // to CarrierServiceBindHelper. Pull it out as a reference so even if it gets set to
1816 // null within the ServiceConnection during unbinding we can avoid an NPE.
1817 final IBinder service = connection.service;
1818 if (service == null || !service.isBinderAlive() || !service.pingBinder()) continue;
1819 // We've got a live service. Last check is just to make sure we don't dump a package
1820 // multiple times.
1821 if (!dumpedPkgNames.add(servicePkgName)) continue;
1822 if (!exactPackageMatch) {
1823 logd(targetPkgName + " has carrier privileges on phoneId " + connection.phoneId
1824 + ", service provided by " + servicePkgName);
1825 indentPW.increaseIndent();
1826 indentPW.println("Proxy : " + servicePkgName);
1827 indentPW.decreaseIndent();
1828 }
1829 // Flush before we let the app output anything to ensure correct ordering of output.
1830 // Internally, Binder#dump calls flush on its printer after finishing so we don't
1831 // need to do anything after.
1832 indentPW.flush();
1833 try {
1834 logd("Dumping " + servicePkgName);
1835 // We don't need to give the carrier service any args.
1836 connection.service.dump(fd, null /* args */);
1837 logd("Done with " + servicePkgName);
1838 } catch (RemoteException e) {
1839 logd("RemoteException from " + servicePkgName, e);
1840 indentPW.increaseIndent();
1841 indentPW.println("RemoteException");
1842 indentPW.increaseIndent();
1843 e.printStackTrace(indentPW);
1844 indentPW.decreaseIndent();
1845 indentPW.decreaseIndent();
1846 // We won't retry this package again because now it's in dumpedPkgNames.
1847 }
1848 indentPW.println("");
Jordan Liu46ed5db2020-01-28 08:55:20 -08001849 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001850 if (dumpedPkgNames.isEmpty()) {
1851 indentPW.increaseIndent();
1852 indentPW.println("Not bound");
1853 indentPW.decreaseIndent();
1854 indentPW.println("");
1855 }
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001856 indentPW.decreaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001857 }
1858
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001859 private boolean hasCarrierPrivileges(@NonNull String pkgName, int phoneId) {
Jack Yu00ece8c2022-11-19 22:29:12 -08001860 int subId = SubscriptionManager.getSubscriptionId(phoneId);
1861 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001862 return false;
1863 }
Jack Yu00ece8c2022-11-19 22:29:12 -08001864 return TelephonyManager.from(mContext).createForSubscriptionId(subId)
1865 .checkCarrierPrivilegesForPackage(pkgName)
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001866 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001867 }
1868
joonhunshin21a86812023-12-10 08:21:25 +00001869 /**
1870 * Get the current calling package name.
1871 * @return the current calling package name
1872 */
1873 @Nullable
1874 private String getCurrentPackageName() {
Jack Yube3fa442024-09-16 14:39:45 -07001875 if (mFeatureFlags.hsumPackageManager()) {
1876 PackageManager pm = mContext.createContextAsUser(Binder.getCallingUserHandle(), 0)
1877 .getPackageManager();
1878 if (pm == null) return null;
1879 String[] callingPackageNames = pm.getPackagesForUid(Binder.getCallingUid());
1880 return (callingPackageNames == null) ? null : callingPackageNames[0];
1881 }
joonhunshin21a86812023-12-10 08:21:25 +00001882 if (mPackageManager == null) return null;
Jack Yube3fa442024-09-16 14:39:45 -07001883 String[] callingPackageNames = mPackageManager.getPackagesForUid(Binder.getCallingUid());
1884 return (callingPackageNames == null) ? null : callingPackageNames[0];
joonhunshin21a86812023-12-10 08:21:25 +00001885 }
1886
1887 /**
1888 * Make sure the device has required telephony feature
1889 *
1890 * @throws UnsupportedOperationException if the device does not have required telephony feature
1891 */
1892 private void enforceTelephonyFeatureWithException(@Nullable String callingPackage,
1893 @NonNull String methodName) {
1894 if (callingPackage == null || mPackageManager == null) {
1895 return;
1896 }
1897
1898 if (!mFeatureFlags.enforceTelephonyFeatureMappingForPublicApis()
1899 || !CompatChanges.isChangeEnabled(ENABLE_FEATURE_MAPPING, callingPackage,
joonhunshinf624b2a2024-04-18 04:42:12 +00001900 Binder.getCallingUserHandle())
1901 || mVendorApiLevel < Build.VERSION_CODES.VANILLA_ICE_CREAM) {
1902 // Skip to check associated telephony feature,
1903 // if compatibility change is not enabled for the current process or
1904 // the SDK version of vendor partition is less than Android V.
joonhunshin21a86812023-12-10 08:21:25 +00001905 return;
1906 }
1907
1908 if (!mPackageManager.hasSystemFeature(FEATURE_TELEPHONY_SUBSCRIPTION)) {
1909 throw new UnsupportedOperationException(
1910 methodName + " is unsupported without " + FEATURE_TELEPHONY_SUBSCRIPTION);
1911 }
1912 }
1913
Zach Johnson36d7aab2015-05-22 15:43:00 -07001914 private class CarrierServiceConnection implements ServiceConnection {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001915 final int phoneId;
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001916 @NonNull final String pkgName;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001917 final int eventId;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001918 IBinder service;
1919
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001920 CarrierServiceConnection(int phoneId, @NonNull String pkgName, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001921 this.phoneId = phoneId;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001922 this.pkgName = pkgName;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001923 this.eventId = eventId;
1924 }
1925
1926 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001927 public void onServiceConnected(@NonNull ComponentName name, @NonNull IBinder service) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001928 logd("Connected to config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001929 this.service = service;
1930 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1931 }
1932
1933 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001934 public void onServiceDisconnected(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001935 logd("Disconnected from config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001936 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001937 }
1938
1939 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001940 public void onBindingDied(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001941 logd("Binding died from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001942 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001943 }
1944
1945 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001946 public void onNullBinding(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001947 logd("Null binding from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001948 this.service = null;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001949 }
1950 }
1951
1952 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1953 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001954 public void onReceive(@NonNull Context context, @NonNull Intent intent) {
Rambo Wanga27fbe52022-04-12 19:09:07 +00001955 switch (intent.getAction()) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001956 case Intent.ACTION_BOOT_COMPLETED:
1957 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1958 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001959 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001960 }
1961 }
1962
Rambo Wanga27fbe52022-04-12 19:09:07 +00001963 private class CarrierServiceChangeCallback implements
1964 TelephonyManager.CarrierPrivilegesCallback {
1965 final int mPhoneId;
1966 // CarrierPrivilegesCallback will be triggered upon registration. Filter the first callback
1967 // here since we really care of the *change* of carrier service instead of the content
1968 private boolean mHasSentServiceChangeCallback;
1969
1970 CarrierServiceChangeCallback(int phoneId) {
1971 this.mPhoneId = phoneId;
1972 this.mHasSentServiceChangeCallback = false;
1973 }
1974
1975 @Override
1976 public void onCarrierPrivilegesChanged(
1977 @androidx.annotation.NonNull Set<String> privilegedPackageNames,
1978 @androidx.annotation.NonNull Set<Integer> privilegedUids) {
1979 // Ignored, not interested here
1980 }
1981
1982 @Override
1983 public void onCarrierServiceChanged(
1984 @androidx.annotation.Nullable String carrierServicePackageName,
1985 int carrierServiceUid) {
1986 // Ignore the first callback which is triggered upon registration
1987 if (!mHasSentServiceChangeCallback) {
1988 mHasSentServiceChangeCallback = true;
1989 return;
1990 }
1991 mHandler.sendMessage(
1992 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, mPhoneId, -1,
1993 carrierServicePackageName));
1994 }
1995 }
1996
Rambo Wangab13e3e2021-04-08 15:01:06 -07001997 // Get readable string for the message code supported in this class.
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001998 @NonNull
Rambo Wangab13e3e2021-04-08 15:01:06 -07001999 private static String eventToString(int code) {
2000 switch (code) {
2001 case EVENT_CLEAR_CONFIG:
2002 return "EVENT_CLEAR_CONFIG";
2003 case EVENT_CONNECTED_TO_DEFAULT:
2004 return "EVENT_CONNECTED_TO_DEFAULT";
2005 case EVENT_CONNECTED_TO_CARRIER:
2006 return "EVENT_CONNECTED_TO_CARRIER";
2007 case EVENT_FETCH_DEFAULT_DONE:
2008 return "EVENT_FETCH_DEFAULT_DONE";
2009 case EVENT_FETCH_CARRIER_DONE:
2010 return "EVENT_FETCH_CARRIER_DONE";
2011 case EVENT_DO_FETCH_DEFAULT:
2012 return "EVENT_DO_FETCH_DEFAULT";
2013 case EVENT_DO_FETCH_CARRIER:
2014 return "EVENT_DO_FETCH_CARRIER";
2015 case EVENT_PACKAGE_CHANGED:
2016 return "EVENT_PACKAGE_CHANGED";
2017 case EVENT_BIND_DEFAULT_TIMEOUT:
2018 return "EVENT_BIND_DEFAULT_TIMEOUT";
2019 case EVENT_BIND_CARRIER_TIMEOUT:
2020 return "EVENT_BIND_CARRIER_TIMEOUT";
2021 case EVENT_CHECK_SYSTEM_UPDATE:
2022 return "EVENT_CHECK_SYSTEM_UPDATE";
2023 case EVENT_SYSTEM_UNLOCKED:
2024 return "EVENT_SYSTEM_UNLOCKED";
2025 case EVENT_FETCH_DEFAULT_TIMEOUT:
2026 return "EVENT_FETCH_DEFAULT_TIMEOUT";
2027 case EVENT_FETCH_CARRIER_TIMEOUT:
2028 return "EVENT_FETCH_CARRIER_TIMEOUT";
2029 case EVENT_SUBSCRIPTION_INFO_UPDATED:
2030 return "EVENT_SUBSCRIPTION_INFO_UPDATED";
2031 case EVENT_MULTI_SIM_CONFIG_CHANGED:
2032 return "EVENT_MULTI_SIM_CONFIG_CHANGED";
2033 case EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG:
2034 return "EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG";
2035 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE:
2036 return "EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE";
2037 case EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG:
2038 return "EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG";
2039 case EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
2040 return "EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT";
2041 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
2042 return "EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT";
2043 default:
2044 return "UNKNOWN(" + code + ")";
2045 }
2046 }
2047
Rambo Wangfe0d7c12022-04-15 03:00:32 +00002048 private void logd(@NonNull String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07002049 Log.d(LOG_TAG, msg);
2050 }
2051
Rambo Wangfe0d7c12022-04-15 03:00:32 +00002052 private void logd(@NonNull String msg, Throwable tr) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08002053 Log.d(LOG_TAG, msg, tr);
2054 }
2055
Rambo Wangfe0d7c12022-04-15 03:00:32 +00002056 private void logdWithLocalLog(@NonNull String msg) {
chen xudb04c292019-03-26 17:01:15 -07002057 Log.d(LOG_TAG, msg);
2058 mCarrierConfigLoadingLog.log(msg);
2059 }
2060
Rambo Wangfe0d7c12022-04-15 03:00:32 +00002061 private void loge(@NonNull String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07002062 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07002063 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08002064 }
2065}