blob: a00fe98006ff882ecea4a444ae9ec2f16393199b [file] [log] [blame]
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001/*
Jonathan Basseri6465afd2015-02-25 13:05:57 -08002 * Copyright (c) 2015, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jonathan Basseri6465afd2015-02-25 13:05:57 -080017package com.android.phone;
18
Jonathan Basseri65273c82017-07-25 15:08:42 -070019import static android.service.carrier.CarrierService.ICarrierServiceWrapper.KEY_CONFIG_BUNDLE;
20import static android.service.carrier.CarrierService.ICarrierServiceWrapper.RESULT_ERROR;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080021
Jonathan Basseri11f89572015-05-05 11:57:39 -070022import android.annotation.NonNull;
Torbjorn Eklund723527a2019-02-13 11:16:25 +010023import android.annotation.Nullable;
Hunter Knepshielde601f432020-02-19 10:16:04 -080024import android.app.AppOpsManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080025import android.content.BroadcastReceiver;
26import android.content.ComponentName;
27import android.content.Context;
28import android.content.Intent;
29import android.content.IntentFilter;
30import android.content.ServiceConnection;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070031import android.content.SharedPreferences;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070032import android.content.pm.PackageInfo;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070033import android.content.pm.PackageManager;
Junda Liu43d723a2015-05-12 17:23:45 -070034import android.os.Binder;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070035import android.os.Build;
Jonathan Basseri65273c82017-07-25 15:08:42 -070036import android.os.Bundle;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080037import android.os.Handler;
Rambo Wanga27fbe52022-04-12 19:09:07 +000038import android.os.HandlerExecutor;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080039import android.os.IBinder;
Rambo Wang7e3bc122021-03-23 09:24:38 -070040import android.os.Looper;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080041import android.os.Message;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070042import android.os.PersistableBundle;
Hunter Knepshielde601f432020-02-19 10:16:04 -080043import android.os.Process;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080044import android.os.RemoteException;
Jonathan Basseri65273c82017-07-25 15:08:42 -070045import android.os.ResultReceiver;
Meng Wang97a6a462020-01-23 16:22:16 -080046import android.os.UserHandle;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070047import android.preference.PreferenceManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080048import android.service.carrier.CarrierIdentifier;
Zach Johnson36d7aab2015-05-22 15:43:00 -070049import android.service.carrier.CarrierService;
50import android.service.carrier.ICarrierService;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080051import android.telephony.CarrierConfigManager;
52import android.telephony.SubscriptionManager;
Peter Wangc035ce42020-01-08 21:00:22 -080053import android.telephony.TelephonyFrameworkInitializer;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080054import android.telephony.TelephonyManager;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -080055import android.text.TextUtils;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080056import android.util.ArraySet;
chen xudb04c292019-03-26 17:01:15 -070057import android.util.LocalLog;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080058import android.util.Log;
59
Rambo Wang7e3bc122021-03-23 09:24:38 -070060import com.android.internal.annotations.VisibleForTesting;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080061import com.android.internal.telephony.ICarrierConfigLoader;
62import com.android.internal.telephony.IccCardConstants;
63import com.android.internal.telephony.Phone;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080064import com.android.internal.telephony.PhoneFactory;
Nathan Harold48ac0972019-03-13 22:33:01 -070065import com.android.internal.telephony.SubscriptionInfoUpdater;
Jeff Davidsona8e4e242018-03-15 17:16:18 -070066import com.android.internal.telephony.TelephonyPermissions;
Meng Wanga320b942019-11-25 11:21:26 -080067import com.android.internal.telephony.util.ArrayUtils;
shuoq26a3a4c2016-12-16 11:06:48 -080068import com.android.internal.util.IndentingPrintWriter;
arunvoddu664c36a2021-10-11 20:09:28 +000069import com.android.telephony.Rlog;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080070
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070071import java.io.File;
Junda Liu43d723a2015-05-12 17:23:45 -070072import java.io.FileDescriptor;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070073import java.io.FileInputStream;
74import java.io.FileNotFoundException;
75import java.io.FileOutputStream;
Jonathan Basseri1f743c92015-05-15 00:19:46 -070076import java.io.FilenameFilter;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070077import java.io.IOException;
Junda Liu43d723a2015-05-12 17:23:45 -070078import java.io.PrintWriter;
shuoq26a3a4c2016-12-16 11:06:48 -080079import java.util.ArrayList;
80import java.util.Arrays;
81import java.util.Collections;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080082import java.util.List;
rambowang14757c22022-10-03 11:54:56 -050083import java.util.Objects;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080084import java.util.Set;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080085
86/**
87 * CarrierConfigLoader binds to privileged carrier apps to fetch carrier config overlays.
Jonathan Basseri6465afd2015-02-25 13:05:57 -080088 */
Jonathan Basseri6465afd2015-02-25 13:05:57 -080089public class CarrierConfigLoader extends ICarrierConfigLoader.Stub {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070090 private static final String LOG_TAG = "CarrierConfigLoader";
Meng Wang33ad2bc2017-03-16 20:21:20 -070091
92 // Package name for platform carrier config app, bundled with system image.
Rambo Wangfe0d7c12022-04-15 03:00:32 +000093 @NonNull private final String mPlatformCarrierConfigPackage;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080094
95 /** The singleton instance. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +000096 @Nullable private static CarrierConfigLoader sInstance;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080097 // The context for phone app, passed from PhoneGlobals.
Rambo Wangfe0d7c12022-04-15 03:00:32 +000098 @NonNull private Context mContext;
99
100 // All the states below (array indexed by phoneId) are non-null. But the member of the array
101 // is nullable, when e.g. the config for the phone is not loaded yet
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800102 // Carrier configs from default app, indexed by phoneID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000103 @NonNull private PersistableBundle[] mConfigFromDefaultApp;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800104 // Carrier configs from privileged carrier config app, indexed by phoneID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000105 @NonNull private PersistableBundle[] mConfigFromCarrierApp;
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100106 // Persistent Carrier configs that are provided via the override test API, indexed by phone ID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000107 @NonNull private PersistableBundle[] mPersistentOverrideConfigs;
Hall Liu506724b2018-10-22 18:16:14 -0700108 // Carrier configs that are provided via the override test API, indexed by phone ID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000109 @NonNull private PersistableBundle[] mOverrideConfigs;
Jayachandran C645ec612020-01-10 10:30:25 -0800110 // Carrier configs to override code default when there is no SIM inserted
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000111 @NonNull private PersistableBundle mNoSimConfig;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800112 // Service connection for binding to config app.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000113 @NonNull private CarrierServiceConnection[] mServiceConnection;
Jayachandran C645ec612020-01-10 10:30:25 -0800114 // Service connection for binding to carrier config app for no SIM config.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000115 @NonNull private CarrierServiceConnection[] mServiceConnectionForNoSimConfig;
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700116 // Whether we are bound to a service for each phone
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000117 @NonNull private boolean[] mServiceBound;
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700118 // Whether we are bound to a service for no SIM config
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000119 @NonNull private boolean[] mServiceBoundForNoSimConfig;
Sarah Chin807d5c62020-03-30 17:21:09 -0700120 // Whether we have sent config change broadcast for each phone id.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000121 @NonNull private boolean[] mHasSentConfigChange;
Sarah Chin807d5c62020-03-30 17:21:09 -0700122 // Whether the broadcast was sent from EVENT_SYSTEM_UNLOCKED, to track rebroadcasts
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000123 @NonNull private boolean[] mFromSystemUnlocked;
Rambo Wanga27fbe52022-04-12 19:09:07 +0000124 // CarrierService change monitoring
125 @NonNull private CarrierServiceChangeCallback[] mCarrierServiceChangeCallbacks;
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000126
Nathan Harold48ac0972019-03-13 22:33:01 -0700127 // SubscriptionInfoUpdater
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000128 @NonNull private final SubscriptionInfoUpdater mSubscriptionInfoUpdater;
Rambo Wanga27fbe52022-04-12 19:09:07 +0000129 // Broadcast receiver for system events (BootCompleted, MultiSimConfigChanged etc.)
130 @NonNull
131 private final BroadcastReceiver mSystemBroadcastReceiver = new ConfigLoaderBroadcastReceiver();
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000132 @NonNull private final LocalLog mCarrierConfigLoadingLog = new LocalLog(100);
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000133 // Number of phone instances (active modem count)
134 private int mNumPhones;
chen xudb04c292019-03-26 17:01:15 -0700135
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800136
137 // Message codes; see mHandler below.
138 // Request from SubscriptionInfoUpdater when SIM becomes absent or error.
139 private static final int EVENT_CLEAR_CONFIG = 0;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800140 // Has connected to default app.
141 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
142 // Has connected to carrier app.
143 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700144 // Config has been loaded from default app (or cache).
145 private static final int EVENT_FETCH_DEFAULT_DONE = 5;
146 // Config has been loaded from carrier app (or cache).
147 private static final int EVENT_FETCH_CARRIER_DONE = 6;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700148 // Attempt to fetch from default app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700149 private static final int EVENT_DO_FETCH_DEFAULT = 7;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700150 // Attempt to fetch from carrier app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700151 private static final int EVENT_DO_FETCH_CARRIER = 8;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700152 // A package has been installed, uninstalled, or updated.
153 private static final int EVENT_PACKAGE_CHANGED = 9;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700154 // Bind timed out for the default app.
155 private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
156 // Bind timed out for a carrier app.
157 private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700158 // Check if the system fingerprint has changed.
159 private static final int EVENT_CHECK_SYSTEM_UPDATE = 12;
Nanxi Chen3d670502016-03-17 16:32:09 -0700160 // Rerun carrier config binding after system is unlocked.
161 private static final int EVENT_SYSTEM_UNLOCKED = 13;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700162 // Fetching config timed out from the default app.
163 private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14;
164 // Fetching config timed out from a carrier app.
165 private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15;
Nathan Harold48ac0972019-03-13 22:33:01 -0700166 // SubscriptionInfoUpdater has finished updating the sub for the carrier config.
167 private static final int EVENT_SUBSCRIPTION_INFO_UPDATED = 16;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700168 // Multi-SIM config changed.
169 private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 17;
Jayachandran C645ec612020-01-10 10:30:25 -0800170 // Attempt to fetch from default app or read from XML for no SIM case.
171 private static final int EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG = 18;
172 // No SIM config has been loaded from default app (or cache).
173 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE = 19;
174 // Has connected to default app for no SIM config.
175 private static final int EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG = 20;
176 // Bind timed out for the default app when trying to fetch no SIM config.
177 private static final int EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 21;
178 // Fetching config timed out from the default app for no SIM config.
179 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 22;
Rambo Wangab13e3e2021-04-08 15:01:06 -0700180 // NOTE: any new EVENT_* values must be added to method eventToString().
Jonathan Basseri930701e2015-06-11 20:56:43 -0700181
Junda Liu6cb45002016-03-22 17:18:20 -0700182 private static final int BIND_TIMEOUT_MILLIS = 30000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800183
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800184 // Keys used for saving and restoring config bundle from file.
185 private static final String KEY_VERSION = "__carrier_config_package_version__";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800186
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100187 private static final String OVERRIDE_PACKAGE_ADDITION = "-override";
188
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700189 // SharedPreferences key for last known build fingerprint.
190 private static final String KEY_FINGERPRINT = "build_fingerprint";
191
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800192 // Argument for #dump that indicates we should also call the default and specified carrier
193 // service's #dump method. In multi-SIM devices, it's possible that carrier A is on SIM 1 and
194 // carrier B is on SIM 2, in which case we should not dump carrier B's service when carrier A
195 // requested the dump.
196 private static final String DUMP_ARG_REQUESTING_PACKAGE = "--requesting-package";
197
rambowang14757c22022-10-03 11:54:56 -0500198 // Configs that should always be included when clients calls getConfig[ForSubId] with specified
199 // keys (even configs are not explicitly specified). Those configs have special purpose for the
200 // carrier config APIs to work correctly.
201 private static final String[] CONFIG_SUBSET_METADATA_KEYS = new String[] {
202 CarrierConfigManager.KEY_CARRIER_CONFIG_VERSION_STRING,
203 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL
204 };
205
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800206 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700207 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700208 // For each phoneId, the event sequence should be:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700209 // fetch default, connected to default, fetch default (async), fetch default done,
210 // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700211 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700212 // If there is a saved config file for either the default app or the carrier app, we skip
213 // binding to the app and go straight from fetch to loaded.
214 //
215 // At any time, at most one connection is active. If events are not in this order, previous
216 // connection will be unbound, so only latest event takes effect.
217 //
218 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
219 // 1. loading from carrier app (even if read from a file)
220 // 2. loading from default app if there is no carrier app (even if read from a file)
221 // 3. clearing config (e.g. due to sim removal)
222 // 4. encountering bind or IPC error
Jonathan Basseri65273c82017-07-25 15:08:42 -0700223 private class ConfigHandler extends Handler {
Rambo Wang7e3bc122021-03-23 09:24:38 -0700224 ConfigHandler(@NonNull Looper looper) {
225 super(looper);
226 }
227
Jonathan Basseri65273c82017-07-25 15:08:42 -0700228 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000229 public void handleMessage(@NonNull Message msg) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700230 final int phoneId = msg.arg1;
Rambo Wangab13e3e2021-04-08 15:01:06 -0700231 logdWithLocalLog("mHandler: " + eventToString(msg.what) + " phoneId: " + phoneId);
Malcolm Chen5ea18532019-10-24 19:55:44 -0700232 if (!SubscriptionManager.isValidPhoneId(phoneId)
233 && msg.what != EVENT_MULTI_SIM_CONFIG_CHANGED) {
234 return;
235 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800236 switch (msg.what) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800237 case EVENT_CLEAR_CONFIG: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700238 clearConfigForPhone(phoneId, true);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800239 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700240 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700241
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800242 case EVENT_SYSTEM_UNLOCKED: {
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000243 for (int i = 0; i < mNumPhones; ++i) {
Sarah Chin807d5c62020-03-30 17:21:09 -0700244 // When the user unlocks the device, send the broadcast again (with a
245 // rebroadcast extra) if we have sent it before unlock. This will avoid
246 // trying to load the carrier config when the SIM is still loading when the
247 // unlock happens.
Junda Liu03aad762017-07-21 13:32:17 -0700248 if (mHasSentConfigChange[i]) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800249 logdWithLocalLog("System unlocked");
Sarah Chin807d5c62020-03-30 17:21:09 -0700250 mFromSystemUnlocked[i] = true;
Junda Liu03aad762017-07-21 13:32:17 -0700251 updateConfigForPhoneId(i);
252 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700253 }
254 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700255 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700256
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800257 case EVENT_PACKAGE_CHANGED: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700258 final String carrierPackageName = (String) msg.obj;
Rambo Wanga27fbe52022-04-12 19:09:07 +0000259 // Always clear up the cache and re-load config from scratch since the carrier
260 // service change is reliable and specific to the phoneId now.
261 clearCachedConfigForPackage(carrierPackageName);
262 logdWithLocalLog("Package changed: " + carrierPackageName
263 + ", phone=" + phoneId);
264 updateConfigForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700265 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700266 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700267
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800268 case EVENT_DO_FETCH_DEFAULT: {
Rambo Wang9bc7d362021-03-09 09:10:58 -0800269 // Clear in-memory cache for carrier app config, so when carrier app gets
270 // uninstalled, no stale config is left.
271 if (mConfigFromCarrierApp[phoneId] != null
272 && getCarrierPackageForPhoneId(phoneId) == null) {
273 mConfigFromCarrierApp[phoneId] = null;
274 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100275 // Restore persistent override values.
276 PersistableBundle config = restoreConfigFromXml(
277 mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId);
278 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800279 logd("Loaded persistent override config from XML. package="
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100280 + mPlatformCarrierConfigPackage
281 + " phoneId=" + phoneId);
282 mPersistentOverrideConfigs[phoneId] = config;
283 }
284
285 config = restoreConfigFromXml(mPlatformCarrierConfigPackage, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700286 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800287 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700288 "Loaded config from XML. package="
289 + mPlatformCarrierConfigPackage
290 + " phoneId="
291 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700292 mConfigFromDefaultApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700293 Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700294 newMsg.getData().putBoolean("loaded_from_xml", true);
295 mHandler.sendMessage(newMsg);
296 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700297 // No cached config, so fetch it from the default app.
298 if (bindToConfigPackage(
299 mPlatformCarrierConfigPackage,
300 phoneId,
301 EVENT_CONNECTED_TO_DEFAULT)) {
302 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800303 obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1 /*arg2*/,
304 getMessageToken(phoneId)),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700305 BIND_TIMEOUT_MILLIS);
306 } else {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800307 // Put a stub bundle in place so that the rest of the logic continues
308 // smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000309 mConfigFromDefaultApp[phoneId] = new PersistableBundle();
Jonathan Basseri65273c82017-07-25 15:08:42 -0700310 // Send broadcast if bind fails.
Nathan Harold48ac0972019-03-13 22:33:01 -0700311 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700312 // TODO: We *must* call unbindService even if bindService returns false.
313 // (And possibly if SecurityException was thrown.)
chen xudb04c292019-03-26 17:01:15 -0700314 loge("binding to default app: "
315 + mPlatformCarrierConfigPackage + " fails");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700316 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800317 }
318 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700319 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800320
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800321 case EVENT_CONNECTED_TO_DEFAULT: {
Rambo Wang610fdf62021-03-08 21:37:11 -0800322 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT, getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700323 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800324 // If new service connection has been created, unbind.
325 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700326 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800327 break;
328 }
chen xu02581692018-11-11 19:03:44 -0800329 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700330 // ResultReceiver callback will execute in this Handler's thread.
331 final ResultReceiver resultReceiver =
332 new ResultReceiver(this) {
333 @Override
334 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700335 unbindIfBound(mContext, conn, phoneId);
Rambo Wanga9d27a52021-04-09 16:17:33 -0700336 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT,
337 getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700338 // If new service connection has been created, this is stale.
339 if (mServiceConnection[phoneId] != conn) {
340 loge("Received response for stale request.");
341 return;
342 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700343 if (resultCode == RESULT_ERROR || resultData == null) {
344 // On error, abort config fetching.
345 loge("Failed to get carrier config");
Nathan Harold48ac0972019-03-13 22:33:01 -0700346 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700347 return;
348 }
349 PersistableBundle config =
350 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100351 saveConfigToXml(mPlatformCarrierConfigPackage, "", phoneId,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800352 carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700353 mConfigFromDefaultApp[phoneId] = config;
354 sendMessage(
355 obtainMessage(
356 EVENT_FETCH_DEFAULT_DONE, phoneId, -1));
357 }
358 };
359 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800360 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700361 ICarrierService carrierService =
362 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800363 carrierService.getCarrierConfig(phoneId, carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800364 logdWithLocalLog("Fetch config for default app: "
chen xudb04c292019-03-26 17:01:15 -0700365 + mPlatformCarrierConfigPackage
366 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700367 } catch (RemoteException e) {
chen xudb04c292019-03-26 17:01:15 -0700368 loge("Failed to get carrier config from default app: " +
369 mPlatformCarrierConfigPackage + " err: " + e.toString());
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700370 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700371 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800372 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700373 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800374 obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1 /*arg2*/,
375 getMessageToken(phoneId)),
Jonathan Basseri65273c82017-07-25 15:08:42 -0700376 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800377 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700378 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800379
Jonathan Basseri930701e2015-06-11 20:56:43 -0700380 case EVENT_BIND_DEFAULT_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800381 case EVENT_FETCH_DEFAULT_TIMEOUT: {
382 loge("Bind/fetch time out from " + mPlatformCarrierConfigPackage);
Rambo Wang610fdf62021-03-08 21:37:11 -0800383 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT, getMessageToken(phoneId));
chen xuf60b3162019-05-01 21:31:05 -0700384 // If we attempted to bind to the app, but the service connection is null due to
385 // the race condition that clear config event happens before bind/fetch complete
386 // then config was cleared while we were waiting and we should not continue.
387 if (mServiceConnection[phoneId] != null) {
388 // If a ResponseReceiver callback is in the queue when this happens, we will
389 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700390 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700391 broadcastConfigChangedIntent(phoneId);
392 }
Nathan Harold1122e3d2021-01-22 15:45:24 -0800393 // Put a stub bundle in place so that the rest of the logic continues smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000394 mConfigFromDefaultApp[phoneId] = new PersistableBundle();
Nathan Harold48ac0972019-03-13 22:33:01 -0700395 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700396 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700397 }
Jonathan Basseri930701e2015-06-11 20:56:43 -0700398
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800399 case EVENT_FETCH_DEFAULT_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700400 // If we attempted to bind to the app, but the service connection is null, then
401 // config was cleared while we were waiting and we should not continue.
402 if (!msg.getData().getBoolean("loaded_from_xml", false)
403 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800404 break;
405 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700406 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700407 if (carrierPackageName != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800408 logd("Found carrier config app: " + carrierPackageName);
Jordan Liu38a614c2019-03-20 15:01:17 -0700409 sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId, -1));
Jonathan Basserib919e932015-05-27 16:53:08 +0000410 } else {
Nathan Harold48ac0972019-03-13 22:33:01 -0700411 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700412 }
413 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700414 }
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700415
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800416 case EVENT_DO_FETCH_CARRIER: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700417 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700418 final PersistableBundle config =
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100419 restoreConfigFromXml(carrierPackageName, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700420 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800421 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700422 "Loaded config from XML. package="
423 + carrierPackageName
424 + " phoneId="
425 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700426 mConfigFromCarrierApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700427 Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700428 newMsg.getData().putBoolean("loaded_from_xml", true);
429 sendMessage(newMsg);
430 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700431 // No cached config, so fetch it from a carrier app.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800432 if (carrierPackageName != null && bindToConfigPackage(carrierPackageName,
433 phoneId, EVENT_CONNECTED_TO_CARRIER)) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700434 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800435 obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1 /*arg2*/,
436 getMessageToken(phoneId)),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700437 BIND_TIMEOUT_MILLIS);
438 } else {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800439 // Put a stub bundle in place so that the rest of the logic continues
440 // smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000441 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
Jonathan Basseri65273c82017-07-25 15:08:42 -0700442 // Send broadcast if bind fails.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700443 broadcastConfigChangedIntent(phoneId);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800444 loge("Bind to carrier app: " + carrierPackageName + " fails");
Nathan Harold48ac0972019-03-13 22:33:01 -0700445 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700446 }
447 }
448 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700449 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700450
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800451 case EVENT_CONNECTED_TO_CARRIER: {
Rambo Wang610fdf62021-03-08 21:37:11 -0800452 removeMessages(EVENT_BIND_CARRIER_TIMEOUT, getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700453 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800454 // If new service connection has been created, unbind.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700455 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700456 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800457 break;
458 }
chen xu02581692018-11-11 19:03:44 -0800459 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700460 // ResultReceiver callback will execute in this Handler's thread.
461 final ResultReceiver resultReceiver =
462 new ResultReceiver(this) {
463 @Override
464 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700465 unbindIfBound(mContext, conn, phoneId);
Rambo Wanga9d27a52021-04-09 16:17:33 -0700466 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT,
467 getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700468 // If new service connection has been created, this is stale.
469 if (mServiceConnection[phoneId] != conn) {
470 loge("Received response for stale request.");
471 return;
472 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700473 if (resultCode == RESULT_ERROR || resultData == null) {
474 // On error, abort config fetching.
chen xudb04c292019-03-26 17:01:15 -0700475 loge("Failed to get carrier config from carrier app: "
476 + getCarrierPackageForPhoneId(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700477 broadcastConfigChangedIntent(phoneId);
Nathan Harold48ac0972019-03-13 22:33:01 -0700478 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700479 return;
480 }
481 PersistableBundle config =
482 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100483 saveConfigToXml(getCarrierPackageForPhoneId(phoneId), "",
484 phoneId, carrierId, config);
Amit Mahajan304e92b2021-04-08 14:03:30 -0700485 if (config != null) {
486 mConfigFromCarrierApp[phoneId] = config;
487 } else {
488 logdWithLocalLog("Config from carrier app is null "
489 + "for phoneId " + phoneId);
490 // Put a stub bundle in place so that the rest of the logic
491 // continues smoothly.
492 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
493 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700494 sendMessage(
495 obtainMessage(
496 EVENT_FETCH_CARRIER_DONE, phoneId, -1));
497 }
498 };
499 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800500 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700501 ICarrierService carrierService =
502 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800503 carrierService.getCarrierConfig(phoneId, carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800504 logdWithLocalLog("Fetch config for carrier app: "
chen xudb04c292019-03-26 17:01:15 -0700505 + getCarrierPackageForPhoneId(phoneId)
506 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700507 } catch (RemoteException e) {
508 loge("Failed to get carrier config: " + e.toString());
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700509 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700510 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800511 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700512 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800513 obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1 /*arg2*/,
514 getMessageToken(phoneId)),
Jonathan Basseri65273c82017-07-25 15:08:42 -0700515 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800516 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700517 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800518
Jonathan Basseri930701e2015-06-11 20:56:43 -0700519 case EVENT_BIND_CARRIER_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800520 case EVENT_FETCH_CARRIER_TIMEOUT: {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800521 loge("Bind/fetch from carrier app timeout, package="
522 + getCarrierPackageForPhoneId(phoneId));
Rambo Wang610fdf62021-03-08 21:37:11 -0800523 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT, getMessageToken(phoneId));
chen xuf60b3162019-05-01 21:31:05 -0700524 // If we attempted to bind to the app, but the service connection is null due to
525 // the race condition that clear config event happens before bind/fetch complete
526 // then config was cleared while we were waiting and we should not continue.
527 if (mServiceConnection[phoneId] != null) {
528 // If a ResponseReceiver callback is in the queue when this happens, we will
529 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700530 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700531 broadcastConfigChangedIntent(phoneId);
532 }
Nathan Harold1122e3d2021-01-22 15:45:24 -0800533 // Put a stub bundle in place so that the rest of the logic continues smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000534 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
Nathan Harold48ac0972019-03-13 22:33:01 -0700535 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700536 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700537 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800538 case EVENT_FETCH_CARRIER_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700539 // If we attempted to bind to the app, but the service connection is null, then
540 // config was cleared while we were waiting and we should not continue.
541 if (!msg.getData().getBoolean("loaded_from_xml", false)
542 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800543 break;
544 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700545 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800546 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700547 }
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700548
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800549 case EVENT_CHECK_SYSTEM_UPDATE: {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700550 SharedPreferences sharedPrefs =
551 PreferenceManager.getDefaultSharedPreferences(mContext);
552 final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
553 if (!Build.FINGERPRINT.equals(lastFingerprint)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800554 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700555 "Build fingerprint changed. old: "
556 + lastFingerprint
557 + " new: "
558 + Build.FINGERPRINT);
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700559 clearCachedConfigForPackage(null);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700560 sharedPrefs
561 .edit()
562 .putString(KEY_FINGERPRINT, Build.FINGERPRINT)
563 .apply();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700564 }
565 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700566 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700567
568 case EVENT_SUBSCRIPTION_INFO_UPDATED:
569 broadcastConfigChangedIntent(phoneId);
570 break;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700571 case EVENT_MULTI_SIM_CONFIG_CHANGED:
572 onMultiSimConfigChanged();
573 break;
Jayachandran C645ec612020-01-10 10:30:25 -0800574
575 case EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG: {
576 PersistableBundle config =
577 restoreNoSimConfigFromXml(mPlatformCarrierConfigPackage);
578
579 if (config != null) {
580 logd("Loaded no SIM config from XML. package="
581 + mPlatformCarrierConfigPackage);
582 mNoSimConfig = config;
583 sendMessage(
584 obtainMessage(
585 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE,
586 phoneId, -1));
587 } else {
588 // No cached config, so fetch it from the default app.
589 if (bindToConfigPackage(
590 mPlatformCarrierConfigPackage,
591 phoneId,
592 EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG)) {
593 sendMessageDelayed(
594 obtainMessage(
595 EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT,
596 phoneId, -1), BIND_TIMEOUT_MILLIS);
597 } else {
598 broadcastConfigChangedIntent(phoneId, false);
599 // TODO: We *must* call unbindService even if bindService returns false.
600 // (And possibly if SecurityException was thrown.)
601 loge("binding to default app to fetch no SIM config: "
602 + mPlatformCarrierConfigPackage + " fails");
603 }
604 }
605 break;
606 }
607
608 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE: {
609 broadcastConfigChangedIntent(phoneId, false);
610 break;
611 }
612
613 case EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
614 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT: {
615 loge("Bind/fetch time out for no SIM config from "
616 + mPlatformCarrierConfigPackage);
617 removeMessages(EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
618 // If we attempted to bind to the app, but the service connection is null due to
619 // the race condition that clear config event happens before bind/fetch complete
620 // then config was cleared while we were waiting and we should not continue.
621 if (mServiceConnectionForNoSimConfig[phoneId] != null) {
622 // If a ResponseReceiver callback is in the queue when this happens, we will
623 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700624 unbindIfBoundForNoSimConfig(mContext,
625 mServiceConnectionForNoSimConfig[phoneId], phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800626 }
627 broadcastConfigChangedIntent(phoneId, false);
628 break;
629 }
630
631 case EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG: {
632 removeMessages(EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
633 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
634 // If new service connection has been created, unbind.
635 if (mServiceConnectionForNoSimConfig[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700636 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800637 break;
638 }
639
640 // ResultReceiver callback will execute in this Handler's thread.
641 final ResultReceiver resultReceiver =
642 new ResultReceiver(this) {
643 @Override
644 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700645 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800646 // If new service connection has been created, this is stale.
647 if (mServiceConnectionForNoSimConfig[phoneId] != conn) {
648 loge("Received response for stale request.");
649 return;
650 }
651 removeMessages(EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
652 if (resultCode == RESULT_ERROR || resultData == null) {
653 // On error, abort config fetching.
654 loge("Failed to get no SIM carrier config");
655 return;
656 }
657 PersistableBundle config =
658 resultData.getParcelable(KEY_CONFIG_BUNDLE);
659 saveNoSimConfigToXml(mPlatformCarrierConfigPackage, config);
660 mNoSimConfig = config;
661 sendMessage(
662 obtainMessage(
663 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE,
664 phoneId, -1));
665 }
666 };
667 // Now fetch the config asynchronously from the ICarrierService.
668 try {
669 ICarrierService carrierService =
670 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800671 carrierService.getCarrierConfig(phoneId, null, resultReceiver);
Jayachandran C645ec612020-01-10 10:30:25 -0800672 logdWithLocalLog("Fetch no sim config from default app: "
673 + mPlatformCarrierConfigPackage);
674 } catch (RemoteException e) {
675 loge("Failed to get no sim carrier config from default app: " +
676 mPlatformCarrierConfigPackage + " err: " + e.toString());
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700677 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800678 break; // So we don't set a timeout.
679 }
680 sendMessageDelayed(
681 obtainMessage(
682 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT,
683 phoneId, -1), BIND_TIMEOUT_MILLIS);
684 break;
685 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800686 }
687 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700688 }
689
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000690 @NonNull private final Handler mHandler;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800691
692 /**
693 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
694 * receiver for relevant events.
695 */
Rambo Wang7e3bc122021-03-23 09:24:38 -0700696 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000697 /* package */ CarrierConfigLoader(@NonNull Context context,
698 @NonNull SubscriptionInfoUpdater subscriptionInfoUpdater, @NonNull Looper looper) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800699 mContext = context;
Meng Wang33ad2bc2017-03-16 20:21:20 -0700700 mPlatformCarrierConfigPackage =
701 mContext.getString(R.string.platform_carrier_config_package);
Rambo Wang7e3bc122021-03-23 09:24:38 -0700702 mHandler = new ConfigHandler(looper);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800703
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000704 IntentFilter systemEventsFilter = new IntentFilter();
705 systemEventsFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
706 systemEventsFilter.addAction(TelephonyManager.ACTION_MULTI_SIM_CONFIG_CHANGED);
Rambo Wanga27fbe52022-04-12 19:09:07 +0000707 context.registerReceiver(mSystemBroadcastReceiver, systemEventsFilter);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800708
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000709 mNumPhones = TelephonyManager.from(context).getActiveModemCount();
710 mConfigFromDefaultApp = new PersistableBundle[mNumPhones];
711 mConfigFromCarrierApp = new PersistableBundle[mNumPhones];
712 mPersistentOverrideConfigs = new PersistableBundle[mNumPhones];
713 mOverrideConfigs = new PersistableBundle[mNumPhones];
Rambo Wang42026112021-04-07 20:57:28 +0000714 mNoSimConfig = new PersistableBundle();
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000715 mServiceConnection = new CarrierServiceConnection[mNumPhones];
716 mServiceBound = new boolean[mNumPhones];
717 mHasSentConfigChange = new boolean[mNumPhones];
718 mFromSystemUnlocked = new boolean[mNumPhones];
719 mServiceConnectionForNoSimConfig = new CarrierServiceConnection[mNumPhones];
720 mServiceBoundForNoSimConfig = new boolean[mNumPhones];
Rambo Wanga27fbe52022-04-12 19:09:07 +0000721 mCarrierServiceChangeCallbacks = new CarrierServiceChangeCallback[mNumPhones];
722 for (int phoneId = 0; phoneId < mNumPhones; phoneId++) {
723 mCarrierServiceChangeCallbacks[phoneId] = new CarrierServiceChangeCallback(phoneId);
724 TelephonyManager.from(context).registerCarrierPrivilegesCallback(phoneId,
725 new HandlerExecutor(mHandler), mCarrierServiceChangeCallbacks[phoneId]);
726 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800727 logd("CarrierConfigLoader has started");
Rambo Wang7e3bc122021-03-23 09:24:38 -0700728 mSubscriptionInfoUpdater = subscriptionInfoUpdater;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700729 mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800730 }
731
732 /**
733 * Initialize the singleton CarrierConfigLoader instance.
734 *
735 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
736 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000737 @NonNull
738 /* package */ static CarrierConfigLoader init(@NonNull Context context) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800739 synchronized (CarrierConfigLoader.class) {
740 if (sInstance == null) {
Rambo Wang7e3bc122021-03-23 09:24:38 -0700741 sInstance = new CarrierConfigLoader(context,
742 PhoneFactory.getSubscriptionInfoUpdater(), Looper.myLooper());
Brad Ebingerfa6575f2021-05-04 00:29:50 +0000743 // Make this service available through ServiceManager.
744 TelephonyFrameworkInitializer.getTelephonyServiceManager()
745 .getCarrierConfigServiceRegisterer().register(sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800746 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700747 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800748 }
749 return sInstance;
750 }
751 }
752
Rambo Wang7e3bc122021-03-23 09:24:38 -0700753 @VisibleForTesting
754 /* package */ void clearConfigForPhone(int phoneId, boolean fetchNoSimConfig) {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700755 /* Ignore clear configuration request if device is being shutdown. */
756 Phone phone = PhoneFactory.getPhone(phoneId);
757 if (phone != null) {
758 if (phone.isShuttingDown()) {
759 return;
760 }
761 }
762
763 mConfigFromDefaultApp[phoneId] = null;
764 mConfigFromCarrierApp[phoneId] = null;
765 mServiceConnection[phoneId] = null;
766 mHasSentConfigChange[phoneId] = false;
767
Jayachandran C645ec612020-01-10 10:30:25 -0800768 if (fetchNoSimConfig) {
769 // To fetch no SIM config
770 mHandler.sendMessage(
771 mHandler.obtainMessage(
772 EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG, phoneId, -1));
773 }
Malcolm Chen5ea18532019-10-24 19:55:44 -0700774 }
775
Nathan Harold48ac0972019-03-13 22:33:01 -0700776 private void notifySubscriptionInfoUpdater(int phoneId) {
777 String configPackagename;
778 PersistableBundle configToSend;
779 int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
780 // Prefer the carrier privileged carrier app, but if there is not one, use the platform
781 // default carrier app.
782 if (mConfigFromCarrierApp[phoneId] != null) {
783 configPackagename = getCarrierPackageForPhoneId(phoneId);
784 configToSend = mConfigFromCarrierApp[phoneId];
785 } else {
786 configPackagename = mPlatformCarrierConfigPackage;
787 configToSend = mConfigFromDefaultApp[phoneId];
788 }
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700789
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800790 if (configToSend == null) {
Rambo Wang42026112021-04-07 20:57:28 +0000791 configToSend = new PersistableBundle();
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800792 }
793
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700794 // mOverrideConfigs is for testing. And it will override current configs.
795 PersistableBundle config = mOverrideConfigs[phoneId];
796 if (config != null) {
Torbjorn Eklund1050cb02018-11-16 14:05:38 +0100797 configToSend = new PersistableBundle(configToSend);
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700798 configToSend.putAll(config);
799 }
800
Nathan Harold48ac0972019-03-13 22:33:01 -0700801 mSubscriptionInfoUpdater.updateSubscriptionByCarrierConfigAndNotifyComplete(
802 phoneId, configPackagename, configToSend,
803 mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1));
804 }
805
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800806 private void broadcastConfigChangedIntent(int phoneId) {
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800807 broadcastConfigChangedIntent(phoneId, true);
808 }
809
810 private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800811 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800812 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
chen xuf9db49f2019-03-31 23:04:42 -0700813 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800814 if (addSubIdExtra) {
815 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
Jack Yu00ece8c2022-11-19 22:29:12 -0800816 int subId = SubscriptionManager.getSubscriptionId(phoneId);
817 if (SubscriptionManager.isValidSubscriptionId(subId)) {
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800818 TelephonyManager telMgr = TelephonyManager.from(mContext)
Jack Yu00ece8c2022-11-19 22:29:12 -0800819 .createForSubscriptionId(subId);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800820 simApplicationState = telMgr.getSimApplicationState();
821 }
Malcolm Chenf7635ec2020-04-15 15:45:00 -0700822 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId
823 + " simApplicationState " + simApplicationState);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800824 // Include subId/carrier id extra only if SIM records are loaded
825 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
826 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
827 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
828 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID,
829 getSpecificCarrierIdForPhoneId(phoneId));
830 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, getCarrierIdForPhoneId(phoneId));
831 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800832 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800833 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Sarah Chin807d5c62020-03-30 17:21:09 -0700834 intent.putExtra(CarrierConfigManager.EXTRA_REBROADCAST_ON_UNLOCK,
835 mFromSystemUnlocked[phoneId]);
Meng Wang97a6a462020-01-23 16:22:16 -0800836 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
Jack Yu00ece8c2022-11-19 22:29:12 -0800837 int subId = SubscriptionManager.getSubscriptionId(phoneId);
838 if (SubscriptionManager.isValidSubscriptionId(subId)) {
839 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId + ", subId=" + subId);
Jack Yue9e95be2020-03-22 10:56:06 -0700840 } else {
841 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
842 }
Junda Liu03aad762017-07-21 13:32:17 -0700843 mHasSentConfigChange[phoneId] = true;
Sarah Chin807d5c62020-03-30 17:21:09 -0700844 mFromSystemUnlocked[phoneId] = false;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800845 }
846
847 /** Binds to the default or carrier config app. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000848 private boolean bindToConfigPackage(@NonNull String pkgName, int phoneId, int eventId) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800849 logdWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700850 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700851 carrierService.setPackage(pkgName);
Jayachandran C645ec612020-01-10 10:30:25 -0800852 CarrierServiceConnection serviceConnection = new CarrierServiceConnection(
853 phoneId, pkgName, eventId);
854 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
855 mServiceConnectionForNoSimConfig[phoneId] = serviceConnection;
856 } else {
857 mServiceConnection[phoneId] = serviceConnection;
858 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800859 try {
Jayachandran C645ec612020-01-10 10:30:25 -0800860 if (mContext.bindService(carrierService, serviceConnection,
Jordan Liu178430d2020-02-10 14:32:15 -0800861 Context.BIND_AUTO_CREATE)) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700862 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
863 mServiceBoundForNoSimConfig[phoneId] = true;
864 } else {
865 mServiceBound[phoneId] = true;
866 }
Jordan Liu178430d2020-02-10 14:32:15 -0800867 return true;
868 } else {
869 return false;
870 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800871 } catch (SecurityException ex) {
872 return false;
873 }
874 }
875
Rambo Wang7e3bc122021-03-23 09:24:38 -0700876 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000877 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -0700878 /* package */ CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800879 String mcc = "";
880 String mnc = "";
881 String imsi = "";
882 String gid1 = "";
883 String gid2 = "";
884 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
885 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800886 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800887 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700888 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
889 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800890 mcc = simOperator.substring(0, 3);
891 mnc = simOperator.substring(3);
892 }
893 Phone phone = PhoneFactory.getPhone(phoneId);
894 if (phone != null) {
895 imsi = phone.getSubscriberId();
896 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700897 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800898 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800899 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800900 }
chen xua31f22b2019-03-06 15:28:50 -0800901 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800902 }
903
Rambo Wangf7c214a2022-03-17 12:36:22 -0700904 /** Returns the package name of a privileged carrier app, or null if there is none. */
Nathan Harold1122e3d2021-01-22 15:45:24 -0800905 @Nullable
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700906 private String getCarrierPackageForPhoneId(int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -0700907 final long token = Binder.clearCallingIdentity();
908 try {
Rambo Wangf7c214a2022-03-17 12:36:22 -0700909 return TelephonyManager.from(mContext)
910 .getCarrierServicePackageNameForLogicalSlot(phoneId);
Nazanin1adf4562021-03-29 15:35:30 -0700911 } finally {
912 Binder.restoreCallingIdentity(token);
913 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700914 }
915
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000916 @Nullable
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700917 private String getIccIdForPhoneId(int phoneId) {
918 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
919 return null;
920 }
921 Phone phone = PhoneFactory.getPhone(phoneId);
922 if (phone == null) {
923 return null;
924 }
925 return phone.getIccSerialNumber();
926 }
927
chen xu02581692018-11-11 19:03:44 -0800928 /**
chen xua31f22b2019-03-06 15:28:50 -0800929 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800930 */
chen xua31f22b2019-03-06 15:28:50 -0800931 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700932 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800933 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700934 }
935 Phone phone = PhoneFactory.getPhone(phoneId);
936 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800937 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700938 }
chen xua31f22b2019-03-06 15:28:50 -0800939 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800940 }
941
942 /**
943 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
944 */
945 private int getCarrierIdForPhoneId(int phoneId) {
946 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
947 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700948 }
chen xu02581692018-11-11 19:03:44 -0800949 Phone phone = PhoneFactory.getPhone(phoneId);
950 if (phone == null) {
951 return TelephonyManager.UNKNOWN_CARRIER_ID;
952 }
953 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700954 }
955
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700956 /**
957 * Writes a bundle to an XML file.
958 *
chen xu02581692018-11-11 19:03:44 -0800959 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -0800960 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -0800961 * should have a single copy of XML file named after carrier id. However, it's still possible
962 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
963 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
964 * carrier while iccid remains the same.
965 *
966 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
967 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700968 *
969 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700970 *
Jayachandran C645ec612020-01-10 10:30:25 -0800971 * @param packageName the name of the package from which we fetched this bundle.
972 * @param extraString An extra string to be used in the XML file name.
973 * @param phoneId the phone ID.
974 * @param carrierId contains all carrier-identifying information.
975 * @param config the bundle to be written. Null will be treated as an empty bundle.
976 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700977 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000978 private void saveConfigToXml(@Nullable String packageName, @NonNull String extraString,
979 int phoneId, @Nullable CarrierIdentifier carrierId, @NonNull PersistableBundle config,
980 boolean isNoSimConfig) {
Jayachandran C645ec612020-01-10 10:30:25 -0800981 if (packageName == null) {
982 loge("Cannot save config with null packageName");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800983 return;
984 }
985
Jayachandran C645ec612020-01-10 10:30:25 -0800986 String fileName;
987 if (isNoSimConfig) {
988 fileName = getFilenameForNoSimConfig(packageName);
989 } else {
Jack Yuf5badd92022-12-08 00:50:53 -0800990 if (TelephonyManager.getSimStateForSlotIndex(phoneId)
Jayachandran C645ec612020-01-10 10:30:25 -0800991 != TelephonyManager.SIM_STATE_LOADED) {
992 loge("Skip save config because SIM records are not loaded.");
993 return;
994 }
995
996 final String iccid = getIccIdForPhoneId(phoneId);
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000997 final int cid = carrierId != null ? carrierId.getSpecificCarrierId()
998 : TelephonyManager.UNKNOWN_CARRIER_ID;
Jayachandran C645ec612020-01-10 10:30:25 -0800999 if (iccid == null) {
1000 loge("Cannot save config with null iccid.");
1001 return;
1002 }
1003 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001004 }
Jayachandran C645ec612020-01-10 10:30:25 -08001005
Junda Liu02596502016-11-15 13:46:43 -08001006 // b/32668103 Only save to file if config isn't empty.
1007 // In case of failure, not caching an empty bundle will
1008 // try loading config again on next power on or sim loaded.
1009 // Downside is for genuinely empty bundle, will bind and load
1010 // on every power on.
1011 if (config == null || config.isEmpty()) {
1012 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001013 }
1014
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001015 final String version = getPackageVersion(packageName);
1016 if (version == null) {
1017 loge("Failed to get package version for: " + packageName);
1018 return;
1019 }
1020
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001021 logdWithLocalLog(
1022 "Save config to xml, packagename: " + packageName + " phoneId: " + phoneId);
chen xudb04c292019-03-26 17:01:15 -07001023
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001024 FileOutputStream outFile = null;
1025 try {
Jayachandran C645ec612020-01-10 10:30:25 -08001026 outFile = new FileOutputStream(new File(mContext.getFilesDir(), fileName));
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001027 config.putString(KEY_VERSION, version);
1028 config.writeToStream(outFile);
1029 outFile.flush();
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001030 outFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001031 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001032 loge(e.toString());
1033 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001034 }
1035
Rambo Wang7e3bc122021-03-23 09:24:38 -07001036 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001037 /* package */ void saveConfigToXml(@Nullable String packageName, @NonNull String extraString,
1038 int phoneId, @NonNull CarrierIdentifier carrierId, @NonNull PersistableBundle config) {
Jayachandran C645ec612020-01-10 10:30:25 -08001039 saveConfigToXml(packageName, extraString, phoneId, carrierId, config, false);
1040 }
1041
Rambo Wang7e3bc122021-03-23 09:24:38 -07001042 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001043 /* package */ void saveNoSimConfigToXml(@Nullable String packageName,
1044 @NonNull PersistableBundle config) {
Jayachandran C645ec612020-01-10 10:30:25 -08001045 saveConfigToXml(packageName, "", -1, null, config, true);
1046 }
1047
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001048 /**
1049 * Reads a bundle from an XML file.
1050 *
1051 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +08001052 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001053 *
1054 * In case of errors, or if the saved config is from a different package version than the
1055 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001056 *
Jayachandran C645ec612020-01-10 10:30:25 -08001057 * @param packageName the name of the package from which we fetched this bundle.
1058 * @param extraString An extra string to be used in the XML file name.
1059 * @param phoneId the phone ID.
1060 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001061 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001062 * version does not match, or reading config fails.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001063 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001064 @Nullable
1065 private PersistableBundle restoreConfigFromXml(@Nullable String packageName,
1066 @NonNull String extraString, int phoneId, boolean isNoSimConfig) {
Jayachandran C645ec612020-01-10 10:30:25 -08001067 if (packageName == null) {
1068 loge("Cannot restore config with null packageName");
1069 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001070 final String version = getPackageVersion(packageName);
1071 if (version == null) {
1072 loge("Failed to get package version for: " + packageName);
1073 return null;
1074 }
Yuchen Dongc15afed2018-04-26 17:05:22 +08001075
Jayachandran C645ec612020-01-10 10:30:25 -08001076 String fileName;
arunvoddu664c36a2021-10-11 20:09:28 +00001077 String iccid = null;
Jayachandran C645ec612020-01-10 10:30:25 -08001078 if (isNoSimConfig) {
1079 fileName = getFilenameForNoSimConfig(packageName);
1080 } else {
Jack Yuf5badd92022-12-08 00:50:53 -08001081 if (TelephonyManager.getSimStateForSlotIndex(phoneId)
Jayachandran C645ec612020-01-10 10:30:25 -08001082 != TelephonyManager.SIM_STATE_LOADED) {
1083 loge("Skip restore config because SIM records are not loaded.");
1084 return null;
1085 }
1086
arunvoddu664c36a2021-10-11 20:09:28 +00001087 iccid = getIccIdForPhoneId(phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001088 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1089 if (iccid == null) {
1090 loge("Cannot restore config with null iccid.");
1091 return null;
1092 }
1093 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001094 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001095
1096 PersistableBundle restoredBundle = null;
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001097 File file = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001098 FileInputStream inFile = null;
1099 try {
Jayachandran C645ec612020-01-10 10:30:25 -08001100 file = new File(mContext.getFilesDir(),fileName);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001101 inFile = new FileInputStream(file);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001102
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001103 restoredBundle = PersistableBundle.readFromStream(inFile);
1104 String savedVersion = restoredBundle.getString(KEY_VERSION);
1105 restoredBundle.remove(KEY_VERSION);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001106
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001107 if (!version.equals(savedVersion)) {
1108 loge("Saved version mismatch: " + version + " vs " + savedVersion);
1109 restoredBundle = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001110 }
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001111
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001112 inFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001113 } catch (FileNotFoundException e) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001114 // Missing file is normal occurrence that might occur with a new sim or when restoring
1115 // an override file during boot and should not be treated as an error.
arunvoddu664c36a2021-10-11 20:09:28 +00001116 if (file != null) {
1117 if (isNoSimConfig) {
1118 logd("File not found: " + file.getPath());
1119 } else {
1120 String filePath = file.getPath();
1121 filePath = getFilePathForLogging(filePath, iccid);
1122 logd("File not found : " + filePath);
1123 }
1124 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001125 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001126 loge(e.toString());
1127 }
1128
1129 return restoredBundle;
1130 }
1131
arunvoddu664c36a2021-10-11 20:09:28 +00001132 /**
1133 * This method will mask most part of iccid in the filepath for logging on userbuild
1134 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001135 @NonNull
1136 private String getFilePathForLogging(@Nullable String filePath, @Nullable String iccid) {
arunvoddu664c36a2021-10-11 20:09:28 +00001137 // If loggable then return with actual file path
1138 if (Rlog.isLoggable(LOG_TAG, Log.VERBOSE)) {
1139 return filePath;
1140 }
1141 String path = filePath;
1142 int length = (iccid != null) ? iccid.length() : 0;
1143 if (length > 5 && filePath != null) {
1144 path = filePath.replace(iccid.substring(5), "***************");
1145 }
1146 return path;
1147 }
1148
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001149 @Nullable
1150 private PersistableBundle restoreConfigFromXml(@Nullable String packageName,
1151 @NonNull String extraString, int phoneId) {
Jayachandran C645ec612020-01-10 10:30:25 -08001152 return restoreConfigFromXml(packageName, extraString, phoneId, false);
1153 }
1154
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001155 @Nullable
1156 private PersistableBundle restoreNoSimConfigFromXml(@Nullable String packageName) {
Jayachandran C645ec612020-01-10 10:30:25 -08001157 return restoreConfigFromXml(packageName, "", -1, true);
1158 }
1159
Junda Liu8a8a53a2015-05-15 16:19:57 -07001160 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001161 * Clears cached carrier config.
1162 * This deletes all saved XML files associated with the given package name. If packageName is
1163 * null, then it deletes all saved XML files.
1164 *
1165 * @param packageName the name of a carrier package, or null if all cached config should be
1166 * cleared.
1167 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -07001168 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001169 private boolean clearCachedConfigForPackage(@Nullable final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001170 File dir = mContext.getFilesDir();
1171 File[] packageFiles = dir.listFiles(new FilenameFilter() {
1172 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001173 if (packageName != null) {
1174 return filename.startsWith("carrierconfig-" + packageName + "-");
1175 } else {
1176 return filename.startsWith("carrierconfig-");
1177 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001178 }
1179 });
Junda Liu8a8a53a2015-05-15 16:19:57 -07001180 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001181 for (File f : packageFiles) {
arunvoddub7c6e8e2022-09-05 15:48:06 +00001182 logd("Deleting " + getFilePathForLogging(f.getName()));
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001183 f.delete();
1184 }
Junda Liu8a8a53a2015-05-15 16:19:57 -07001185 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001186 }
1187
arunvoddub7c6e8e2022-09-05 15:48:06 +00001188 private String getFilePathForLogging(String filePath) {
1189 if (!TextUtils.isEmpty(filePath)) {
1190 String[] fileTokens = filePath.split("-");
1191 if (fileTokens != null && fileTokens.length > 2) {
1192 String iccid = fileTokens[fileTokens.length -2];
1193 return getFilePathForLogging(filePath, iccid);
1194 }
1195 return filePath;
1196 }
1197 return filePath;
1198 }
1199
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001200 /** Builds a canonical file name for a config file. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001201 @NonNull
Jayachandran C645ec612020-01-10 10:30:25 -08001202 private static String getFilenameForConfig(
1203 @NonNull String packageName, @NonNull String extraString,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001204 @NonNull String iccid, int cid) {
chen xu02581692018-11-11 19:03:44 -08001205 // the same carrier should have a single copy of XML file named after carrier id.
1206 // However, it's still possible that platform doesn't recognize the current sim carrier,
1207 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
1208 // cases SIM OTA resolves to different carrier while iccid remains the same.
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001209 return "carrierconfig-" + packageName + extraString + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001210 }
1211
Jayachandran C645ec612020-01-10 10:30:25 -08001212 /** Builds a canonical file name for no SIM config file. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001213 @NonNull
Jayachandran C645ec612020-01-10 10:30:25 -08001214 private String getFilenameForNoSimConfig(@NonNull String packageName) {
1215 return "carrierconfig-" + packageName + "-" + "nosim" + ".xml";
1216 }
1217
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001218 /** Return the current version code of a package, or null if the name is not found. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001219 @Nullable
1220 private String getPackageVersion(@NonNull String packageName) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001221 try {
1222 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -08001223 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001224 } catch (PackageManager.NameNotFoundException e) {
1225 return null;
1226 }
1227 }
1228
Jonathan Basseri65273c82017-07-25 15:08:42 -07001229 /**
1230 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001231 *
1232 * This reads config bundles for the given phoneId. That means getting the latest bundle from
1233 * the default app and a privileged carrier app, if present. This will not bind to an app if we
1234 * have a saved config file to use instead.
1235 */
1236 private void updateConfigForPhoneId(int phoneId) {
Jonathan Basseri65273c82017-07-25 15:08:42 -07001237 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001238 }
1239
Malcolm Chen5ea18532019-10-24 19:55:44 -07001240 private void onMultiSimConfigChanged() {
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001241 int oldNumPhones = mNumPhones;
1242 mNumPhones = TelephonyManager.from(mContext).getActiveModemCount();
1243 if (mNumPhones == oldNumPhones) {
1244 return;
1245 }
1246 logdWithLocalLog("mNumPhones change from " + oldNumPhones + " to " + mNumPhones);
1247
Rambo Wanga27fbe52022-04-12 19:09:07 +00001248 // If DS -> SS switch, release the resources BEFORE truncating the arrays to avoid leaking
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001249 for (int phoneId = mNumPhones; phoneId < oldNumPhones; phoneId++) {
1250 if (mServiceConnection[phoneId] != null) {
1251 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
1252 }
1253 if (mServiceConnectionForNoSimConfig[phoneId] != null) {
1254 unbindIfBoundForNoSimConfig(mContext, mServiceConnectionForNoSimConfig[phoneId],
1255 phoneId);
1256 }
1257 }
1258
Rambo Wanga27fbe52022-04-12 19:09:07 +00001259 // The phone to slot mapping may change, unregister here and re-register callbacks later
1260 for (int phoneId = 0; phoneId < oldNumPhones; phoneId++) {
1261 if (mCarrierServiceChangeCallbacks[phoneId] != null) {
1262 TelephonyManager.from(mContext).unregisterCarrierPrivilegesCallback(
1263 mCarrierServiceChangeCallbacks[phoneId]);
1264 }
1265 }
1266
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001267 // Copy the original arrays, truncate or padding with zeros (if necessary) to new length
1268 mConfigFromDefaultApp = Arrays.copyOf(mConfigFromDefaultApp, mNumPhones);
1269 mConfigFromCarrierApp = Arrays.copyOf(mConfigFromCarrierApp, mNumPhones);
1270 mPersistentOverrideConfigs = Arrays.copyOf(mPersistentOverrideConfigs, mNumPhones);
1271 mOverrideConfigs = Arrays.copyOf(mOverrideConfigs, mNumPhones);
1272 mServiceConnection = Arrays.copyOf(mServiceConnection, mNumPhones);
1273 mServiceConnectionForNoSimConfig =
1274 Arrays.copyOf(mServiceConnectionForNoSimConfig, mNumPhones);
1275 mServiceBound = Arrays.copyOf(mServiceBound, mNumPhones);
1276 mServiceBoundForNoSimConfig = Arrays.copyOf(mServiceBoundForNoSimConfig, mNumPhones);
1277 mHasSentConfigChange = Arrays.copyOf(mHasSentConfigChange, mNumPhones);
1278 mFromSystemUnlocked = Arrays.copyOf(mFromSystemUnlocked, mNumPhones);
Rambo Wanga27fbe52022-04-12 19:09:07 +00001279 mCarrierServiceChangeCallbacks = Arrays.copyOf(mCarrierServiceChangeCallbacks, mNumPhones);
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001280
Rambo Wanga27fbe52022-04-12 19:09:07 +00001281 // Load the config for all the phones and re-register callback AFTER padding the arrays.
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001282 for (int phoneId = 0; phoneId < mNumPhones; phoneId++) {
1283 updateConfigForPhoneId(phoneId);
Rambo Wanga27fbe52022-04-12 19:09:07 +00001284 mCarrierServiceChangeCallbacks[phoneId] = new CarrierServiceChangeCallback(phoneId);
1285 TelephonyManager.from(mContext).registerCarrierPrivilegesCallback(phoneId,
1286 new HandlerExecutor(mHandler), mCarrierServiceChangeCallbacks[phoneId]);
Malcolm Chen5ea18532019-10-24 19:55:44 -07001287 }
1288 }
1289
Hall Liu506724b2018-10-22 18:16:14 -07001290 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001291 @NonNull
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001292 public PersistableBundle getConfigForSubId(int subscriptionId, @NonNull String callingPackage) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001293 return getConfigForSubIdWithFeature(subscriptionId, callingPackage, null);
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08001294 }
1295
1296 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001297 @NonNull
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001298 public PersistableBundle getConfigForSubIdWithFeature(int subscriptionId,
1299 @NonNull String callingPackage, @Nullable String callingFeatureId) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001300 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subscriptionId,
1301 callingPackage, callingFeatureId, "getCarrierConfig")) {
Rambo Wang42026112021-04-07 20:57:28 +00001302 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -07001303 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -07001304
Rambo Wangd2b004b2021-03-30 10:10:23 -07001305 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001306 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001307 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001308 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001309 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001310 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001311 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001312 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001313 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001314 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001315 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001316 config = mPersistentOverrideConfigs[phoneId];
1317 if (config != null) {
1318 retConfig.putAll(config);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001319 }
Hall Liu506724b2018-10-22 18:16:14 -07001320 config = mOverrideConfigs[phoneId];
1321 if (config != null) {
1322 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -07001323 }
Nathan Harold1122e3d2021-01-22 15:45:24 -08001324 // Ignore the theoretical case of the default app not being present since that won't
1325 // work in CarrierConfigLoader today.
1326 final boolean allConfigsApplied =
1327 (mConfigFromCarrierApp[phoneId] != null
1328 || getCarrierPackageForPhoneId(phoneId) == null)
1329 && mConfigFromDefaultApp[phoneId] != null;
1330 retConfig.putBoolean(
1331 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, allConfigsApplied);
Jayachandran C645ec612020-01-10 10:30:25 -08001332 } else {
1333 if (mNoSimConfig != null) {
1334 retConfig.putAll(mNoSimConfig);
1335 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001336 }
1337 return retConfig;
1338 }
1339
1340 @Override
rambowang14757c22022-10-03 11:54:56 -05001341 @NonNull
1342 public PersistableBundle getConfigSubsetForSubIdWithFeature(int subscriptionId,
1343 @NonNull String callingPackage, @Nullable String callingFeatureId,
1344 @NonNull String[] keys) {
1345 Objects.requireNonNull(callingPackage, "Calling package must be non-null");
1346 Objects.requireNonNull(keys, "Config keys must be non-null");
1347 enforceCallerIsSystemOrRequestingPackage(callingPackage);
1348
1349 // Permission check is performed inside and an empty bundle will return on failure.
1350 // No SecurityException thrown here since most clients expect to retrieve the overridden
1351 // value if present or use default one if not
1352 PersistableBundle allConfigs = getConfigForSubIdWithFeature(subscriptionId, callingPackage,
1353 callingFeatureId);
1354 if (allConfigs.isEmpty()) {
1355 return allConfigs;
1356 }
1357 for (String key : keys) {
1358 Objects.requireNonNull(key, "Config key must be non-null");
rambowang14757c22022-10-03 11:54:56 -05001359 }
1360
1361 PersistableBundle configSubset = new PersistableBundle(
1362 keys.length + CONFIG_SUBSET_METADATA_KEYS.length);
1363 for (String carrierConfigKey : keys) {
1364 Object value = allConfigs.get(carrierConfigKey);
rambowangb49e90f2022-12-13 18:29:06 -06001365 if (value == null) {
1366 // Filter out keys without values.
1367 // In history, many AOSP or OEMs/carriers private configs didn't provide default
1368 // values. We have to continue supporting them for now. See b/261776046 for details.
1369 continue;
1370 }
rambowang14757c22022-10-03 11:54:56 -05001371 // Config value itself could be PersistableBundle which requires different API to put
1372 if (value instanceof PersistableBundle) {
1373 configSubset.putPersistableBundle(carrierConfigKey, (PersistableBundle) value);
1374 } else {
1375 configSubset.putObject(carrierConfigKey, value);
1376 }
1377 }
1378
1379 // Configs in CONFIG_SUBSET_ALWAYS_INCLUDED_KEYS should always be included
1380 for (String generalKey : CONFIG_SUBSET_METADATA_KEYS) {
1381 configSubset.putObject(generalKey, allConfigs.get(generalKey));
1382 }
1383
1384 return configSubset;
1385 }
1386
1387 @Override
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001388 public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides,
1389 boolean persistent) {
Hall Liu506724b2018-10-22 18:16:14 -07001390 mContext.enforceCallingOrSelfPermission(
1391 android.Manifest.permission.MODIFY_PHONE_STATE, null);
1392 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1393 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001394 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
Rambo Wangd2b004b2021-03-30 10:10:23 -07001395 throw new IllegalArgumentException(
1396 "Invalid phoneId " + phoneId + " for subId " + subscriptionId);
Hall Liu506724b2018-10-22 18:16:14 -07001397 }
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001398 // Post to run on handler thread on which all states should be confined.
1399 mHandler.post(() -> {
1400 overrideConfig(mOverrideConfigs, phoneId, overrides);
Hall Liu506724b2018-10-22 18:16:14 -07001401
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001402 if (persistent) {
1403 overrideConfig(mPersistentOverrideConfigs, phoneId, overrides);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001404
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001405 if (overrides != null) {
1406 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
1407 saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION,
1408 phoneId,
1409 carrierId, mPersistentOverrideConfigs[phoneId]);
1410 } else {
1411 final String iccid = getIccIdForPhoneId(phoneId);
1412 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1413 String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage,
1414 OVERRIDE_PACKAGE_ADDITION, iccid, cid);
1415 File fileToDelete = new File(mContext.getFilesDir(), fileName);
1416 fileToDelete.delete();
1417 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001418 }
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001419 notifySubscriptionInfoUpdater(phoneId);
1420 });
Hall Liu506724b2018-10-22 18:16:14 -07001421 }
1422
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001423 private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId,
1424 @Nullable PersistableBundle overrides) {
1425 if (overrides == null) {
Rambo Wang42026112021-04-07 20:57:28 +00001426 currentOverrides[phoneId] = new PersistableBundle();
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001427 } else if (currentOverrides[phoneId] == null) {
1428 currentOverrides[phoneId] = overrides;
1429 } else {
1430 currentOverrides[phoneId].putAll(overrides);
1431 }
1432 }
1433
Hall Liu506724b2018-10-22 18:16:14 -07001434 @Override
Rambo Wangd2b004b2021-03-30 10:10:23 -07001435 public void notifyConfigChangedForSubId(int subscriptionId) {
Jordan Liud7d57fe2019-04-16 12:29:43 -07001436 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001437 // system privileged app with MODIFY_PHONE_STATE permission.
Rambo Wangd2b004b2021-03-30 10:10:23 -07001438 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext,
1439 subscriptionId, "Require carrier privileges or MODIFY_PHONE_STATE permission.");
1440
1441 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1442 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1443 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
1444 throw new IllegalArgumentException(
1445 "Invalid phoneId " + phoneId + " for subId " + subscriptionId);
1446 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001447
1448 // This method should block until deleting has completed, so that an error which prevents us
1449 // from clearing the cache is passed back to the carrier app. With the files successfully
1450 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001451 String callingPackageName = mContext.getPackageManager().getNameForUid(
1452 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001453 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001454 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001455 }
1456
1457 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001458 public void updateConfigForPhoneId(int phoneId, @NonNull String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -07001459 mContext.enforceCallingOrSelfPermission(
1460 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001461 logdWithLocalLog("Update config for phoneId: " + phoneId + " simState: " + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001462 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001463 throw new IllegalArgumentException("Invalid phoneId: " + phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001464 }
1465 // requires Java 7 for switch on string.
1466 switch (simState) {
1467 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1468 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001469 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001470 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Malcolm Chendb66de12019-12-09 18:57:07 -08001471 case IccCardConstants.INTENT_VALUE_ICC_NOT_READY:
Junda Liu9f2d2712015-05-15 14:22:45 -07001472 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001473 break;
1474 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1475 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001476 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001477 break;
1478 }
1479 }
1480
Junda Liu43d723a2015-05-12 17:23:45 -07001481 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001482 @NonNull
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001483 public String getDefaultCarrierServicePackageName() {
Shuo Qianefdb7962019-12-19 15:54:27 -08001484 mContext.enforceCallingOrSelfPermission(
1485 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1486 "getDefaultCarrierServicePackageName");
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001487 return mPlatformCarrierConfigPackage;
1488 }
1489
Rambo Wang7e3bc122021-03-23 09:24:38 -07001490 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001491 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -07001492 /* package */ Handler getHandler() {
1493 return mHandler;
1494 }
1495
1496 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001497 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001498 /* package */ PersistableBundle getConfigFromDefaultApp(int phoneId) {
1499 return mConfigFromDefaultApp[phoneId];
1500 }
1501
1502 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001503 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001504 /* package */ PersistableBundle getConfigFromCarrierApp(int phoneId) {
1505 return mConfigFromCarrierApp[phoneId];
1506 }
1507
1508 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001509 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -07001510 /* package */ PersistableBundle getNoSimConfig() {
1511 return mNoSimConfig;
1512 }
1513
1514 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001515 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001516 /* package */ PersistableBundle getOverrideConfig(int phoneId) {
1517 return mOverrideConfigs[phoneId];
1518 }
1519
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001520 // TODO(b/185129900): always call unbindService after bind, no matter if it succeeded
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001521 private void unbindIfBound(@NonNull Context context, @NonNull CarrierServiceConnection conn,
Rambo Wangb0f3e2f2021-10-28 12:40:12 -07001522 int phoneId) {
1523 if (mServiceBound[phoneId]) {
1524 mServiceBound[phoneId] = false;
1525 context.unbindService(conn);
1526 }
1527 }
1528
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001529 private void unbindIfBoundForNoSimConfig(@NonNull Context context,
1530 @NonNull CarrierServiceConnection conn, int phoneId) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -07001531 if (mServiceBoundForNoSimConfig[phoneId]) {
1532 mServiceBoundForNoSimConfig[phoneId] = false;
Jayachandran C645ec612020-01-10 10:30:25 -08001533 context.unbindService(conn);
1534 }
1535 }
1536
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001537 /**
Rambo Wang610fdf62021-03-08 21:37:11 -08001538 * Returns a boxed Integer object for phoneId, services as message token to distinguish messages
1539 * with same code when calling {@link Handler#removeMessages(int, Object)}.
1540 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001541 @NonNull
Rambo Wang610fdf62021-03-08 21:37:11 -08001542 private Integer getMessageToken(int phoneId) {
1543 if (phoneId < -128 || phoneId > 127) {
1544 throw new IllegalArgumentException("phoneId should be in range [-128, 127], inclusive");
1545 }
1546 // Integer#valueOf guarantees the integers within [-128, 127] are cached and thus memory
1547 // comparison (==) returns true for the same integer.
1548 return Integer.valueOf(phoneId);
1549 }
1550
1551 /**
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001552 * If {@code args} contains {@link #DUMP_ARG_REQUESTING_PACKAGE} and a following package name,
1553 * we'll also call {@link IBinder#dump} on the default carrier service (if bound) and the
1554 * specified carrier service (if bound). Typically, this is done for connectivity bug reports
1555 * where we don't call {@code dumpsys activity service all-non-platform} because that contains
1556 * too much info, but we still want to let carrier apps include their diagnostics.
1557 */
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001558 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001559 public void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter pw, @NonNull String[] args) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001560 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
Junda Liu43d723a2015-05-12 17:23:45 -07001561 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1562 != PackageManager.PERMISSION_GRANTED) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001563 indentPW.println("Permission Denial: can't dump carrierconfig from from pid="
Junda Liu43d723a2015-05-12 17:23:45 -07001564 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1565 return;
1566 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001567 String requestingPackage = null;
1568 int requestingPackageIndex = ArrayUtils.indexOf(args, DUMP_ARG_REQUESTING_PACKAGE);
1569 if (requestingPackageIndex >= 0 && requestingPackageIndex < args.length - 1
1570 && !TextUtils.isEmpty(args[requestingPackageIndex + 1])) {
1571 requestingPackage = args[requestingPackageIndex + 1];
1572 // Throws a SecurityException if the caller is impersonating another app in an effort to
1573 // dump extra info (which may contain PII the caller doesn't have a right to).
1574 enforceCallerIsSystemOrRequestingPackage(requestingPackage);
1575 }
1576
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001577 indentPW.println("CarrierConfigLoader: " + this);
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001578 for (int i = 0; i < mNumPhones; i++) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001579 indentPW.println("Phone Id = " + i);
shuoq26a3a4c2016-12-16 11:06:48 -08001580 // display default values in CarrierConfigManager
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001581 printConfig(CarrierConfigManager.getDefaultConfig(), indentPW,
shuoq26a3a4c2016-12-16 11:06:48 -08001582 "Default Values from CarrierConfigManager");
shuoq26a3a4c2016-12-16 11:06:48 -08001583 // display ConfigFromDefaultApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001584 printConfig(mConfigFromDefaultApp[i], indentPW, "mConfigFromDefaultApp");
shuoq26a3a4c2016-12-16 11:06:48 -08001585 // display ConfigFromCarrierApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001586 printConfig(mConfigFromCarrierApp[i], indentPW, "mConfigFromCarrierApp");
1587 printConfig(mPersistentOverrideConfigs[i], indentPW, "mPersistentOverrideConfigs");
1588 printConfig(mOverrideConfigs[i], indentPW, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001589 }
chen xudb04c292019-03-26 17:01:15 -07001590
Jayachandran C645ec612020-01-10 10:30:25 -08001591 printConfig(mNoSimConfig, indentPW, "mNoSimConfig");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001592 indentPW.println("CarrierConfigLoadingLog=");
1593 mCarrierConfigLoadingLog.dump(fd, indentPW, args);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001594
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001595 if (requestingPackage != null) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001596 logd("Including default and requesting package " + requestingPackage
1597 + " carrier services in dump");
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001598 indentPW.println("");
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001599 indentPW.println("Connected services");
1600 dumpCarrierServiceIfBound(fd, indentPW, "Default config package",
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001601 mPlatformCarrierConfigPackage, false /* considerCarrierPrivileges */);
1602 dumpCarrierServiceIfBound(fd, indentPW, "Requesting package", requestingPackage,
1603 true /* considerCarrierPrivileges */);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001604 }
shuoq26a3a4c2016-12-16 11:06:48 -08001605 }
1606
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001607 private void printConfig(@NonNull PersistableBundle configApp,
1608 @NonNull IndentingPrintWriter indentPW, @NonNull String name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001609 indentPW.increaseIndent();
shuoq26a3a4c2016-12-16 11:06:48 -08001610 if (configApp == null) {
shuoq26a3a4c2016-12-16 11:06:48 -08001611 indentPW.println(name + " : null ");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001612 indentPW.decreaseIndent();
1613 indentPW.println("");
shuoq26a3a4c2016-12-16 11:06:48 -08001614 return;
1615 }
shuoq26a3a4c2016-12-16 11:06:48 -08001616 indentPW.println(name + " : ");
1617 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1618 Collections.sort(sortedKeys);
1619 indentPW.increaseIndent();
1620 indentPW.increaseIndent();
1621 for (String key : sortedKeys) {
1622 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1623 indentPW.println(key + " = " +
1624 Arrays.toString((Object[]) configApp.get(key)));
1625 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1626 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1627 } else {
1628 indentPW.println(key + " = " + configApp.get(key));
1629 }
Junda Liu43d723a2015-05-12 17:23:45 -07001630 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001631 indentPW.decreaseIndent();
1632 indentPW.decreaseIndent();
1633 indentPW.decreaseIndent();
1634 indentPW.println("");
Junda Liu43d723a2015-05-12 17:23:45 -07001635 }
1636
Hunter Knepshielde601f432020-02-19 10:16:04 -08001637 /**
1638 * Passes without problem when one of these conditions is true:
1639 * - The caller is a privileged UID (e.g. for dumpstate.cpp generating a bug report, where the
1640 * system knows the true caller plumbed in through the {@link android.os.BugreportManager} API).
1641 * - The caller's UID matches the supplied package.
1642 *
1643 * @throws SecurityException if none of the above conditions are met.
1644 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001645 private void enforceCallerIsSystemOrRequestingPackage(@NonNull String requestingPackage)
Hunter Knepshielde601f432020-02-19 10:16:04 -08001646 throws SecurityException {
1647 final int callingUid = Binder.getCallingUid();
1648 if (callingUid == Process.ROOT_UID || callingUid == Process.SYSTEM_UID
1649 || callingUid == Process.SHELL_UID || callingUid == Process.PHONE_UID) {
1650 // Bug reports (dumpstate.cpp) run as SHELL, and let some other privileged UIDs through
1651 // as well.
1652 return;
1653 }
1654 // An app is trying to dump extra detail, block it if they aren't who they claim to be.
1655 AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class);
1656 if (appOps == null) {
1657 throw new SecurityException("No AppOps");
1658 }
1659 // Will throw a SecurityException if the UID and package don't match.
1660 appOps.checkPackage(callingUid, requestingPackage);
1661 }
1662
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001663 /**
1664 * Searches for one or more appropriate {@link CarrierService} instances to dump based on the
1665 * current connections.
1666 *
1667 * @param targetPkgName the target package name to dump carrier services for
1668 * @param considerCarrierPrivileges if true, allow a carrier service to be dumped if it shares
1669 * carrier privileges with {@code targetPkgName};
1670 * otherwise, only dump a carrier service if it is {@code
1671 * targetPkgName}
1672 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001673 private void dumpCarrierServiceIfBound(@NonNull FileDescriptor fd,
1674 @NonNull IndentingPrintWriter indentPW, @NonNull String prefix,
1675 @NonNull String targetPkgName, boolean considerCarrierPrivileges) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001676 // Null package is possible if it's early in the boot process, there was a recent crash, we
1677 // loaded the config from XML most recently, or a SIM slot is empty. Carrier apps with
1678 // long-lived bindings should typically get dumped here regardless. Even if an app is being
1679 // used for multiple phoneIds, we assume that it's smart enough to handle that on its own,
1680 // and that in most cases we'd just be dumping duplicate information and bloating a report.
1681 indentPW.increaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001682 indentPW.println(prefix + " : " + targetPkgName);
1683 Set<String> dumpedPkgNames = new ArraySet<>(mServiceConnection.length);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001684 for (CarrierServiceConnection connection : mServiceConnection) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001685 if (connection == null || !SubscriptionManager.isValidPhoneId(connection.phoneId)
1686 || TextUtils.isEmpty(connection.pkgName)) {
1687 continue;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001688 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001689 final String servicePkgName = connection.pkgName;
1690 // Note: we intentionally ignore system components here because we should NOT match the
1691 // shell caller that's typically used for bug reports via non-BugreportManager triggers.
1692 final boolean exactPackageMatch = TextUtils.equals(targetPkgName, servicePkgName);
1693 final boolean carrierPrivilegesMatch =
1694 considerCarrierPrivileges && hasCarrierPrivileges(targetPkgName,
1695 connection.phoneId);
1696 if (!exactPackageMatch && !carrierPrivilegesMatch) continue;
1697 // Make sure this service is actually alive before trying to dump it. We don't pay
1698 // attention to mServiceBound[connection.phoneId] because typically carrier apps will
1699 // request long-lived bindings, and even if we unbind the app, it may still be alive due
1700 // to CarrierServiceBindHelper. Pull it out as a reference so even if it gets set to
1701 // null within the ServiceConnection during unbinding we can avoid an NPE.
1702 final IBinder service = connection.service;
1703 if (service == null || !service.isBinderAlive() || !service.pingBinder()) continue;
1704 // We've got a live service. Last check is just to make sure we don't dump a package
1705 // multiple times.
1706 if (!dumpedPkgNames.add(servicePkgName)) continue;
1707 if (!exactPackageMatch) {
1708 logd(targetPkgName + " has carrier privileges on phoneId " + connection.phoneId
1709 + ", service provided by " + servicePkgName);
1710 indentPW.increaseIndent();
1711 indentPW.println("Proxy : " + servicePkgName);
1712 indentPW.decreaseIndent();
1713 }
1714 // Flush before we let the app output anything to ensure correct ordering of output.
1715 // Internally, Binder#dump calls flush on its printer after finishing so we don't
1716 // need to do anything after.
1717 indentPW.flush();
1718 try {
1719 logd("Dumping " + servicePkgName);
1720 // We don't need to give the carrier service any args.
1721 connection.service.dump(fd, null /* args */);
1722 logd("Done with " + servicePkgName);
1723 } catch (RemoteException e) {
1724 logd("RemoteException from " + servicePkgName, e);
1725 indentPW.increaseIndent();
1726 indentPW.println("RemoteException");
1727 indentPW.increaseIndent();
1728 e.printStackTrace(indentPW);
1729 indentPW.decreaseIndent();
1730 indentPW.decreaseIndent();
1731 // We won't retry this package again because now it's in dumpedPkgNames.
1732 }
1733 indentPW.println("");
Jordan Liu46ed5db2020-01-28 08:55:20 -08001734 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001735 if (dumpedPkgNames.isEmpty()) {
1736 indentPW.increaseIndent();
1737 indentPW.println("Not bound");
1738 indentPW.decreaseIndent();
1739 indentPW.println("");
1740 }
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001741 indentPW.decreaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001742 }
1743
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001744 private boolean hasCarrierPrivileges(@NonNull String pkgName, int phoneId) {
Jack Yu00ece8c2022-11-19 22:29:12 -08001745 int subId = SubscriptionManager.getSubscriptionId(phoneId);
1746 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001747 return false;
1748 }
Jack Yu00ece8c2022-11-19 22:29:12 -08001749 return TelephonyManager.from(mContext).createForSubscriptionId(subId)
1750 .checkCarrierPrivilegesForPackage(pkgName)
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001751 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001752 }
1753
Zach Johnson36d7aab2015-05-22 15:43:00 -07001754 private class CarrierServiceConnection implements ServiceConnection {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001755 final int phoneId;
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001756 @NonNull final String pkgName;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001757 final int eventId;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001758 IBinder service;
1759
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001760 CarrierServiceConnection(int phoneId, @NonNull String pkgName, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001761 this.phoneId = phoneId;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001762 this.pkgName = pkgName;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001763 this.eventId = eventId;
1764 }
1765
1766 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001767 public void onServiceConnected(@NonNull ComponentName name, @NonNull IBinder service) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001768 logd("Connected to config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001769 this.service = service;
1770 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1771 }
1772
1773 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001774 public void onServiceDisconnected(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001775 logd("Disconnected from config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001776 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001777 }
1778
1779 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001780 public void onBindingDied(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001781 logd("Binding died from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001782 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001783 }
1784
1785 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001786 public void onNullBinding(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001787 logd("Null binding from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001788 this.service = null;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001789 }
1790 }
1791
1792 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1793 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001794 public void onReceive(@NonNull Context context, @NonNull Intent intent) {
Rambo Wanga27fbe52022-04-12 19:09:07 +00001795 switch (intent.getAction()) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001796 case Intent.ACTION_BOOT_COMPLETED:
1797 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1798 break;
1799
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001800 case TelephonyManager.ACTION_MULTI_SIM_CONFIG_CHANGED:
1801 mHandler.sendEmptyMessage(EVENT_MULTI_SIM_CONFIG_CHANGED);
1802 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001803 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001804 }
1805 }
1806
Rambo Wanga27fbe52022-04-12 19:09:07 +00001807 private class CarrierServiceChangeCallback implements
1808 TelephonyManager.CarrierPrivilegesCallback {
1809 final int mPhoneId;
1810 // CarrierPrivilegesCallback will be triggered upon registration. Filter the first callback
1811 // here since we really care of the *change* of carrier service instead of the content
1812 private boolean mHasSentServiceChangeCallback;
1813
1814 CarrierServiceChangeCallback(int phoneId) {
1815 this.mPhoneId = phoneId;
1816 this.mHasSentServiceChangeCallback = false;
1817 }
1818
1819 @Override
1820 public void onCarrierPrivilegesChanged(
1821 @androidx.annotation.NonNull Set<String> privilegedPackageNames,
1822 @androidx.annotation.NonNull Set<Integer> privilegedUids) {
1823 // Ignored, not interested here
1824 }
1825
1826 @Override
1827 public void onCarrierServiceChanged(
1828 @androidx.annotation.Nullable String carrierServicePackageName,
1829 int carrierServiceUid) {
1830 // Ignore the first callback which is triggered upon registration
1831 if (!mHasSentServiceChangeCallback) {
1832 mHasSentServiceChangeCallback = true;
1833 return;
1834 }
1835 mHandler.sendMessage(
1836 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, mPhoneId, -1,
1837 carrierServicePackageName));
1838 }
1839 }
1840
Rambo Wangab13e3e2021-04-08 15:01:06 -07001841 // Get readable string for the message code supported in this class.
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001842 @NonNull
Rambo Wangab13e3e2021-04-08 15:01:06 -07001843 private static String eventToString(int code) {
1844 switch (code) {
1845 case EVENT_CLEAR_CONFIG:
1846 return "EVENT_CLEAR_CONFIG";
1847 case EVENT_CONNECTED_TO_DEFAULT:
1848 return "EVENT_CONNECTED_TO_DEFAULT";
1849 case EVENT_CONNECTED_TO_CARRIER:
1850 return "EVENT_CONNECTED_TO_CARRIER";
1851 case EVENT_FETCH_DEFAULT_DONE:
1852 return "EVENT_FETCH_DEFAULT_DONE";
1853 case EVENT_FETCH_CARRIER_DONE:
1854 return "EVENT_FETCH_CARRIER_DONE";
1855 case EVENT_DO_FETCH_DEFAULT:
1856 return "EVENT_DO_FETCH_DEFAULT";
1857 case EVENT_DO_FETCH_CARRIER:
1858 return "EVENT_DO_FETCH_CARRIER";
1859 case EVENT_PACKAGE_CHANGED:
1860 return "EVENT_PACKAGE_CHANGED";
1861 case EVENT_BIND_DEFAULT_TIMEOUT:
1862 return "EVENT_BIND_DEFAULT_TIMEOUT";
1863 case EVENT_BIND_CARRIER_TIMEOUT:
1864 return "EVENT_BIND_CARRIER_TIMEOUT";
1865 case EVENT_CHECK_SYSTEM_UPDATE:
1866 return "EVENT_CHECK_SYSTEM_UPDATE";
1867 case EVENT_SYSTEM_UNLOCKED:
1868 return "EVENT_SYSTEM_UNLOCKED";
1869 case EVENT_FETCH_DEFAULT_TIMEOUT:
1870 return "EVENT_FETCH_DEFAULT_TIMEOUT";
1871 case EVENT_FETCH_CARRIER_TIMEOUT:
1872 return "EVENT_FETCH_CARRIER_TIMEOUT";
1873 case EVENT_SUBSCRIPTION_INFO_UPDATED:
1874 return "EVENT_SUBSCRIPTION_INFO_UPDATED";
1875 case EVENT_MULTI_SIM_CONFIG_CHANGED:
1876 return "EVENT_MULTI_SIM_CONFIG_CHANGED";
1877 case EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG:
1878 return "EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG";
1879 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE:
1880 return "EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE";
1881 case EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG:
1882 return "EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG";
1883 case EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
1884 return "EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT";
1885 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
1886 return "EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT";
1887 default:
1888 return "UNKNOWN(" + code + ")";
1889 }
1890 }
1891
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001892 private void logd(@NonNull String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001893 Log.d(LOG_TAG, msg);
1894 }
1895
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001896 private void logd(@NonNull String msg, Throwable tr) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001897 Log.d(LOG_TAG, msg, tr);
1898 }
1899
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001900 private void logdWithLocalLog(@NonNull String msg) {
chen xudb04c292019-03-26 17:01:15 -07001901 Log.d(LOG_TAG, msg);
1902 mCarrierConfigLoadingLog.log(msg);
1903 }
1904
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001905 private void loge(@NonNull String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001906 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07001907 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001908 }
1909}