blob: d944fa0c3414c359466974b5058352d6a2dfde43 [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 Wang1cf71512022-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;
rambowang63f4fc42022-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 Wang1b89db32022-04-15 03:00:32 +000093 @NonNull private final String mPlatformCarrierConfigPackage;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080094
95 /** The singleton instance. */
Rambo Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-04-15 03:00:32 +0000123 @NonNull private boolean[] mFromSystemUnlocked;
Rambo Wang1cf71512022-04-12 19:09:07 +0000124 // CarrierService change monitoring
125 @NonNull private CarrierServiceChangeCallback[] mCarrierServiceChangeCallbacks;
Rambo Wang1b89db32022-04-15 03:00:32 +0000126
Nathan Harold48ac0972019-03-13 22:33:01 -0700127 // SubscriptionInfoUpdater
Rambo Wang1b89db32022-04-15 03:00:32 +0000128 @NonNull private final SubscriptionInfoUpdater mSubscriptionInfoUpdater;
Rambo Wang1cf71512022-04-12 19:09:07 +0000129 // Broadcast receiver for system events (BootCompleted, MultiSimConfigChanged etc.)
130 @NonNull
131 private final BroadcastReceiver mSystemBroadcastReceiver = new ConfigLoaderBroadcastReceiver();
Rambo Wang1b89db32022-04-15 03:00:32 +0000132 @NonNull private final LocalLog mCarrierConfigLoadingLog = new LocalLog(100);
Rambo Wang6f8328d2022-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
rambowang63f4fc42022-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 Wang1b89db32022-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 Wang6f8328d2022-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 Wang1cf71512022-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 Wang1b89db32022-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 Wang1b89db32022-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 Wang6f8328d2022-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 Wang1cf71512022-04-12 19:09:07 +0000707 context.registerReceiver(mSystemBroadcastReceiver, systemEventsFilter);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800708
Rambo Wang6f8328d2022-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 Wang6f8328d2022-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 Wang1cf71512022-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 Wang1b89db32022-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;
816 int[] subIds = SubscriptionManager.getSubId(phoneId);
817 if (!ArrayUtils.isEmpty(subIds)) {
818 TelephonyManager telMgr = TelephonyManager.from(mContext)
819 .createForSubscriptionId(subIds[0]);
820 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 Yue9e95be2020-03-22 10:56:06 -0700837 int[] subIds = SubscriptionManager.getSubId(phoneId);
838 if (subIds != null && subIds.length > 0) {
839 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId + ", subId=" + subIds[0]);
840 } 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 Wang1b89db32022-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 Wang1b89db32022-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 Wang76e83b52022-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 Wang76e83b52022-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 Wang1b89db32022-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 Wang1b89db32022-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 {
990 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
991 != 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 Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-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 {
1081 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
1082 != 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 Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-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) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001182 logd("Deleting " + 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
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001188 /** Builds a canonical file name for a config file. */
Rambo Wang1b89db32022-04-15 03:00:32 +00001189 @NonNull
Jayachandran C645ec612020-01-10 10:30:25 -08001190 private static String getFilenameForConfig(
1191 @NonNull String packageName, @NonNull String extraString,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001192 @NonNull String iccid, int cid) {
chen xu02581692018-11-11 19:03:44 -08001193 // the same carrier should have a single copy of XML file named after carrier id.
1194 // However, it's still possible that platform doesn't recognize the current sim carrier,
1195 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
1196 // cases SIM OTA resolves to different carrier while iccid remains the same.
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001197 return "carrierconfig-" + packageName + extraString + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001198 }
1199
Jayachandran C645ec612020-01-10 10:30:25 -08001200 /** Builds a canonical file name for no SIM config file. */
Rambo Wang1b89db32022-04-15 03:00:32 +00001201 @NonNull
Jayachandran C645ec612020-01-10 10:30:25 -08001202 private String getFilenameForNoSimConfig(@NonNull String packageName) {
1203 return "carrierconfig-" + packageName + "-" + "nosim" + ".xml";
1204 }
1205
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001206 /** Return the current version code of a package, or null if the name is not found. */
Rambo Wang1b89db32022-04-15 03:00:32 +00001207 @Nullable
1208 private String getPackageVersion(@NonNull String packageName) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001209 try {
1210 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -08001211 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001212 } catch (PackageManager.NameNotFoundException e) {
1213 return null;
1214 }
1215 }
1216
Jonathan Basseri65273c82017-07-25 15:08:42 -07001217 /**
1218 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001219 *
1220 * This reads config bundles for the given phoneId. That means getting the latest bundle from
1221 * the default app and a privileged carrier app, if present. This will not bind to an app if we
1222 * have a saved config file to use instead.
1223 */
1224 private void updateConfigForPhoneId(int phoneId) {
Jonathan Basseri65273c82017-07-25 15:08:42 -07001225 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001226 }
1227
Malcolm Chen5ea18532019-10-24 19:55:44 -07001228 private void onMultiSimConfigChanged() {
Rambo Wang6f8328d2022-04-15 01:26:23 +00001229 int oldNumPhones = mNumPhones;
1230 mNumPhones = TelephonyManager.from(mContext).getActiveModemCount();
1231 if (mNumPhones == oldNumPhones) {
1232 return;
1233 }
1234 logdWithLocalLog("mNumPhones change from " + oldNumPhones + " to " + mNumPhones);
1235
Rambo Wang1cf71512022-04-12 19:09:07 +00001236 // If DS -> SS switch, release the resources BEFORE truncating the arrays to avoid leaking
Rambo Wang6f8328d2022-04-15 01:26:23 +00001237 for (int phoneId = mNumPhones; phoneId < oldNumPhones; phoneId++) {
1238 if (mServiceConnection[phoneId] != null) {
1239 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
1240 }
1241 if (mServiceConnectionForNoSimConfig[phoneId] != null) {
1242 unbindIfBoundForNoSimConfig(mContext, mServiceConnectionForNoSimConfig[phoneId],
1243 phoneId);
1244 }
1245 }
1246
Rambo Wang1cf71512022-04-12 19:09:07 +00001247 // The phone to slot mapping may change, unregister here and re-register callbacks later
1248 for (int phoneId = 0; phoneId < oldNumPhones; phoneId++) {
1249 if (mCarrierServiceChangeCallbacks[phoneId] != null) {
1250 TelephonyManager.from(mContext).unregisterCarrierPrivilegesCallback(
1251 mCarrierServiceChangeCallbacks[phoneId]);
1252 }
1253 }
1254
Rambo Wang6f8328d2022-04-15 01:26:23 +00001255 // Copy the original arrays, truncate or padding with zeros (if necessary) to new length
1256 mConfigFromDefaultApp = Arrays.copyOf(mConfigFromDefaultApp, mNumPhones);
1257 mConfigFromCarrierApp = Arrays.copyOf(mConfigFromCarrierApp, mNumPhones);
1258 mPersistentOverrideConfigs = Arrays.copyOf(mPersistentOverrideConfigs, mNumPhones);
1259 mOverrideConfigs = Arrays.copyOf(mOverrideConfigs, mNumPhones);
1260 mServiceConnection = Arrays.copyOf(mServiceConnection, mNumPhones);
1261 mServiceConnectionForNoSimConfig =
1262 Arrays.copyOf(mServiceConnectionForNoSimConfig, mNumPhones);
1263 mServiceBound = Arrays.copyOf(mServiceBound, mNumPhones);
1264 mServiceBoundForNoSimConfig = Arrays.copyOf(mServiceBoundForNoSimConfig, mNumPhones);
1265 mHasSentConfigChange = Arrays.copyOf(mHasSentConfigChange, mNumPhones);
1266 mFromSystemUnlocked = Arrays.copyOf(mFromSystemUnlocked, mNumPhones);
Rambo Wang1cf71512022-04-12 19:09:07 +00001267 mCarrierServiceChangeCallbacks = Arrays.copyOf(mCarrierServiceChangeCallbacks, mNumPhones);
Rambo Wang6f8328d2022-04-15 01:26:23 +00001268
Rambo Wang1cf71512022-04-12 19:09:07 +00001269 // Load the config for all the phones and re-register callback AFTER padding the arrays.
Rambo Wang6f8328d2022-04-15 01:26:23 +00001270 for (int phoneId = 0; phoneId < mNumPhones; phoneId++) {
1271 updateConfigForPhoneId(phoneId);
Rambo Wang1cf71512022-04-12 19:09:07 +00001272 mCarrierServiceChangeCallbacks[phoneId] = new CarrierServiceChangeCallback(phoneId);
1273 TelephonyManager.from(mContext).registerCarrierPrivilegesCallback(phoneId,
1274 new HandlerExecutor(mHandler), mCarrierServiceChangeCallbacks[phoneId]);
Malcolm Chen5ea18532019-10-24 19:55:44 -07001275 }
1276 }
1277
Hall Liu506724b2018-10-22 18:16:14 -07001278 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001279 @NonNull
Rambo Wang1b89db32022-04-15 03:00:32 +00001280 public PersistableBundle getConfigForSubId(int subscriptionId, @NonNull String callingPackage) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001281 return getConfigForSubIdWithFeature(subscriptionId, callingPackage, null);
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08001282 }
1283
1284 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001285 @NonNull
Rambo Wang1b89db32022-04-15 03:00:32 +00001286 public PersistableBundle getConfigForSubIdWithFeature(int subscriptionId,
1287 @NonNull String callingPackage, @Nullable String callingFeatureId) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001288 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subscriptionId,
1289 callingPackage, callingFeatureId, "getCarrierConfig")) {
Rambo Wang42026112021-04-07 20:57:28 +00001290 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -07001291 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -07001292
Rambo Wangd2b004b2021-03-30 10:10:23 -07001293 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001294 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001295 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001296 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001297 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001298 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001299 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001300 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001301 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001302 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001303 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001304 config = mPersistentOverrideConfigs[phoneId];
1305 if (config != null) {
1306 retConfig.putAll(config);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001307 }
Hall Liu506724b2018-10-22 18:16:14 -07001308 config = mOverrideConfigs[phoneId];
1309 if (config != null) {
1310 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -07001311 }
Nathan Harold1122e3d2021-01-22 15:45:24 -08001312 // Ignore the theoretical case of the default app not being present since that won't
1313 // work in CarrierConfigLoader today.
1314 final boolean allConfigsApplied =
1315 (mConfigFromCarrierApp[phoneId] != null
1316 || getCarrierPackageForPhoneId(phoneId) == null)
1317 && mConfigFromDefaultApp[phoneId] != null;
1318 retConfig.putBoolean(
1319 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, allConfigsApplied);
Jayachandran C645ec612020-01-10 10:30:25 -08001320 } else {
1321 if (mNoSimConfig != null) {
1322 retConfig.putAll(mNoSimConfig);
1323 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001324 }
1325 return retConfig;
1326 }
1327
1328 @Override
rambowang63f4fc42022-10-03 11:54:56 -05001329 @NonNull
1330 public PersistableBundle getConfigSubsetForSubIdWithFeature(int subscriptionId,
1331 @NonNull String callingPackage, @Nullable String callingFeatureId,
1332 @NonNull String[] keys) {
1333 Objects.requireNonNull(callingPackage, "Calling package must be non-null");
1334 Objects.requireNonNull(keys, "Config keys must be non-null");
1335 enforceCallerIsSystemOrRequestingPackage(callingPackage);
1336
1337 // Permission check is performed inside and an empty bundle will return on failure.
1338 // No SecurityException thrown here since most clients expect to retrieve the overridden
1339 // value if present or use default one if not
1340 PersistableBundle allConfigs = getConfigForSubIdWithFeature(subscriptionId, callingPackage,
1341 callingFeatureId);
1342 if (allConfigs.isEmpty()) {
1343 return allConfigs;
1344 }
1345 for (String key : keys) {
1346 Objects.requireNonNull(key, "Config key must be non-null");
1347 // TODO(b/261776046): validate provided key which may has no default value.
1348 // For now, return empty bundle if any required key is not supported
1349 if (!allConfigs.containsKey(key)) {
1350 return new PersistableBundle();
1351 }
1352 }
1353
1354 PersistableBundle configSubset = new PersistableBundle(
1355 keys.length + CONFIG_SUBSET_METADATA_KEYS.length);
1356 for (String carrierConfigKey : keys) {
1357 Object value = allConfigs.get(carrierConfigKey);
1358 // Config value itself could be PersistableBundle which requires different API to put
1359 if (value instanceof PersistableBundle) {
1360 configSubset.putPersistableBundle(carrierConfigKey, (PersistableBundle) value);
1361 } else {
1362 configSubset.putObject(carrierConfigKey, value);
1363 }
1364 }
1365
1366 // Configs in CONFIG_SUBSET_ALWAYS_INCLUDED_KEYS should always be included
1367 for (String generalKey : CONFIG_SUBSET_METADATA_KEYS) {
1368 configSubset.putObject(generalKey, allConfigs.get(generalKey));
1369 }
1370
1371 return configSubset;
1372 }
1373
1374 @Override
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001375 public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides,
1376 boolean persistent) {
Hall Liu506724b2018-10-22 18:16:14 -07001377 mContext.enforceCallingOrSelfPermission(
1378 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Brad Ebingerf6281ad2019-04-25 11:02:04 -07001379 //TODO: Also check for SHELL UID to restrict this method to testing only (b/131326259)
Hall Liu506724b2018-10-22 18:16:14 -07001380 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1381 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001382 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
Rambo Wangd2b004b2021-03-30 10:10:23 -07001383 throw new IllegalArgumentException(
1384 "Invalid phoneId " + phoneId + " for subId " + subscriptionId);
Hall Liu506724b2018-10-22 18:16:14 -07001385 }
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001386 // Post to run on handler thread on which all states should be confined.
1387 mHandler.post(() -> {
1388 overrideConfig(mOverrideConfigs, phoneId, overrides);
Hall Liu506724b2018-10-22 18:16:14 -07001389
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001390 if (persistent) {
1391 overrideConfig(mPersistentOverrideConfigs, phoneId, overrides);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001392
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001393 if (overrides != null) {
1394 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
1395 saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION,
1396 phoneId,
1397 carrierId, mPersistentOverrideConfigs[phoneId]);
1398 } else {
1399 final String iccid = getIccIdForPhoneId(phoneId);
1400 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1401 String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage,
1402 OVERRIDE_PACKAGE_ADDITION, iccid, cid);
1403 File fileToDelete = new File(mContext.getFilesDir(), fileName);
1404 fileToDelete.delete();
1405 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001406 }
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001407 notifySubscriptionInfoUpdater(phoneId);
1408 });
Hall Liu506724b2018-10-22 18:16:14 -07001409 }
1410
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001411 private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId,
1412 @Nullable PersistableBundle overrides) {
1413 if (overrides == null) {
Rambo Wang42026112021-04-07 20:57:28 +00001414 currentOverrides[phoneId] = new PersistableBundle();
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001415 } else if (currentOverrides[phoneId] == null) {
1416 currentOverrides[phoneId] = overrides;
1417 } else {
1418 currentOverrides[phoneId].putAll(overrides);
1419 }
1420 }
1421
Hall Liu506724b2018-10-22 18:16:14 -07001422 @Override
Rambo Wangd2b004b2021-03-30 10:10:23 -07001423 public void notifyConfigChangedForSubId(int subscriptionId) {
Jordan Liud7d57fe2019-04-16 12:29:43 -07001424 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001425 // system privileged app with MODIFY_PHONE_STATE permission.
Rambo Wangd2b004b2021-03-30 10:10:23 -07001426 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext,
1427 subscriptionId, "Require carrier privileges or MODIFY_PHONE_STATE permission.");
1428
1429 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1430 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1431 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
1432 throw new IllegalArgumentException(
1433 "Invalid phoneId " + phoneId + " for subId " + subscriptionId);
1434 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001435
1436 // This method should block until deleting has completed, so that an error which prevents us
1437 // from clearing the cache is passed back to the carrier app. With the files successfully
1438 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001439 String callingPackageName = mContext.getPackageManager().getNameForUid(
1440 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001441 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001442 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001443 }
1444
1445 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +00001446 public void updateConfigForPhoneId(int phoneId, @NonNull String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -07001447 mContext.enforceCallingOrSelfPermission(
1448 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001449 logdWithLocalLog("Update config for phoneId: " + phoneId + " simState: " + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001450 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001451 throw new IllegalArgumentException("Invalid phoneId: " + phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001452 }
1453 // requires Java 7 for switch on string.
1454 switch (simState) {
1455 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1456 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001457 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001458 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Malcolm Chendb66de12019-12-09 18:57:07 -08001459 case IccCardConstants.INTENT_VALUE_ICC_NOT_READY:
Junda Liu9f2d2712015-05-15 14:22:45 -07001460 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001461 break;
1462 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1463 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001464 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001465 break;
1466 }
1467 }
1468
Junda Liu43d723a2015-05-12 17:23:45 -07001469 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +00001470 @NonNull
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001471 public String getDefaultCarrierServicePackageName() {
Shuo Qianefdb7962019-12-19 15:54:27 -08001472 mContext.enforceCallingOrSelfPermission(
1473 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1474 "getDefaultCarrierServicePackageName");
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001475 return mPlatformCarrierConfigPackage;
1476 }
1477
Rambo Wang7e3bc122021-03-23 09:24:38 -07001478 @VisibleForTesting
Rambo Wang1b89db32022-04-15 03:00:32 +00001479 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -07001480 /* package */ Handler getHandler() {
1481 return mHandler;
1482 }
1483
1484 @VisibleForTesting
Rambo Wang1b89db32022-04-15 03:00:32 +00001485 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001486 /* package */ PersistableBundle getConfigFromDefaultApp(int phoneId) {
1487 return mConfigFromDefaultApp[phoneId];
1488 }
1489
1490 @VisibleForTesting
Rambo Wang1b89db32022-04-15 03:00:32 +00001491 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001492 /* package */ PersistableBundle getConfigFromCarrierApp(int phoneId) {
1493 return mConfigFromCarrierApp[phoneId];
1494 }
1495
1496 @VisibleForTesting
Rambo Wang1b89db32022-04-15 03:00:32 +00001497 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -07001498 /* package */ PersistableBundle getNoSimConfig() {
1499 return mNoSimConfig;
1500 }
1501
1502 @VisibleForTesting
Rambo Wang1b89db32022-04-15 03:00:32 +00001503 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001504 /* package */ PersistableBundle getOverrideConfig(int phoneId) {
1505 return mOverrideConfigs[phoneId];
1506 }
1507
Rambo Wang6f8328d2022-04-15 01:26:23 +00001508 // TODO(b/185129900): always call unbindService after bind, no matter if it succeeded
Rambo Wang1b89db32022-04-15 03:00:32 +00001509 private void unbindIfBound(@NonNull Context context, @NonNull CarrierServiceConnection conn,
Rambo Wangb0f3e2f2021-10-28 12:40:12 -07001510 int phoneId) {
1511 if (mServiceBound[phoneId]) {
1512 mServiceBound[phoneId] = false;
1513 context.unbindService(conn);
1514 }
1515 }
1516
Rambo Wang1b89db32022-04-15 03:00:32 +00001517 private void unbindIfBoundForNoSimConfig(@NonNull Context context,
1518 @NonNull CarrierServiceConnection conn, int phoneId) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -07001519 if (mServiceBoundForNoSimConfig[phoneId]) {
1520 mServiceBoundForNoSimConfig[phoneId] = false;
Jayachandran C645ec612020-01-10 10:30:25 -08001521 context.unbindService(conn);
1522 }
1523 }
1524
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001525 /**
Rambo Wang610fdf62021-03-08 21:37:11 -08001526 * Returns a boxed Integer object for phoneId, services as message token to distinguish messages
1527 * with same code when calling {@link Handler#removeMessages(int, Object)}.
1528 */
Rambo Wang1b89db32022-04-15 03:00:32 +00001529 @NonNull
Rambo Wang610fdf62021-03-08 21:37:11 -08001530 private Integer getMessageToken(int phoneId) {
1531 if (phoneId < -128 || phoneId > 127) {
1532 throw new IllegalArgumentException("phoneId should be in range [-128, 127], inclusive");
1533 }
1534 // Integer#valueOf guarantees the integers within [-128, 127] are cached and thus memory
1535 // comparison (==) returns true for the same integer.
1536 return Integer.valueOf(phoneId);
1537 }
1538
1539 /**
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001540 * If {@code args} contains {@link #DUMP_ARG_REQUESTING_PACKAGE} and a following package name,
1541 * we'll also call {@link IBinder#dump} on the default carrier service (if bound) and the
1542 * specified carrier service (if bound). Typically, this is done for connectivity bug reports
1543 * where we don't call {@code dumpsys activity service all-non-platform} because that contains
1544 * too much info, but we still want to let carrier apps include their diagnostics.
1545 */
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001546 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +00001547 public void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter pw, @NonNull String[] args) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001548 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
Junda Liu43d723a2015-05-12 17:23:45 -07001549 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1550 != PackageManager.PERMISSION_GRANTED) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001551 indentPW.println("Permission Denial: can't dump carrierconfig from from pid="
Junda Liu43d723a2015-05-12 17:23:45 -07001552 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1553 return;
1554 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001555 String requestingPackage = null;
1556 int requestingPackageIndex = ArrayUtils.indexOf(args, DUMP_ARG_REQUESTING_PACKAGE);
1557 if (requestingPackageIndex >= 0 && requestingPackageIndex < args.length - 1
1558 && !TextUtils.isEmpty(args[requestingPackageIndex + 1])) {
1559 requestingPackage = args[requestingPackageIndex + 1];
1560 // Throws a SecurityException if the caller is impersonating another app in an effort to
1561 // dump extra info (which may contain PII the caller doesn't have a right to).
1562 enforceCallerIsSystemOrRequestingPackage(requestingPackage);
1563 }
1564
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001565 indentPW.println("CarrierConfigLoader: " + this);
Rambo Wang6f8328d2022-04-15 01:26:23 +00001566 for (int i = 0; i < mNumPhones; i++) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001567 indentPW.println("Phone Id = " + i);
shuoq26a3a4c2016-12-16 11:06:48 -08001568 // display default values in CarrierConfigManager
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001569 printConfig(CarrierConfigManager.getDefaultConfig(), indentPW,
shuoq26a3a4c2016-12-16 11:06:48 -08001570 "Default Values from CarrierConfigManager");
shuoq26a3a4c2016-12-16 11:06:48 -08001571 // display ConfigFromDefaultApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001572 printConfig(mConfigFromDefaultApp[i], indentPW, "mConfigFromDefaultApp");
shuoq26a3a4c2016-12-16 11:06:48 -08001573 // display ConfigFromCarrierApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001574 printConfig(mConfigFromCarrierApp[i], indentPW, "mConfigFromCarrierApp");
1575 printConfig(mPersistentOverrideConfigs[i], indentPW, "mPersistentOverrideConfigs");
1576 printConfig(mOverrideConfigs[i], indentPW, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001577 }
chen xudb04c292019-03-26 17:01:15 -07001578
Jayachandran C645ec612020-01-10 10:30:25 -08001579 printConfig(mNoSimConfig, indentPW, "mNoSimConfig");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001580 indentPW.println("CarrierConfigLoadingLog=");
1581 mCarrierConfigLoadingLog.dump(fd, indentPW, args);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001582
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001583 if (requestingPackage != null) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001584 logd("Including default and requesting package " + requestingPackage
1585 + " carrier services in dump");
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001586 indentPW.println("");
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001587 indentPW.println("Connected services");
1588 dumpCarrierServiceIfBound(fd, indentPW, "Default config package",
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001589 mPlatformCarrierConfigPackage, false /* considerCarrierPrivileges */);
1590 dumpCarrierServiceIfBound(fd, indentPW, "Requesting package", requestingPackage,
1591 true /* considerCarrierPrivileges */);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001592 }
shuoq26a3a4c2016-12-16 11:06:48 -08001593 }
1594
Rambo Wang1b89db32022-04-15 03:00:32 +00001595 private void printConfig(@NonNull PersistableBundle configApp,
1596 @NonNull IndentingPrintWriter indentPW, @NonNull String name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001597 indentPW.increaseIndent();
shuoq26a3a4c2016-12-16 11:06:48 -08001598 if (configApp == null) {
shuoq26a3a4c2016-12-16 11:06:48 -08001599 indentPW.println(name + " : null ");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001600 indentPW.decreaseIndent();
1601 indentPW.println("");
shuoq26a3a4c2016-12-16 11:06:48 -08001602 return;
1603 }
shuoq26a3a4c2016-12-16 11:06:48 -08001604 indentPW.println(name + " : ");
1605 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1606 Collections.sort(sortedKeys);
1607 indentPW.increaseIndent();
1608 indentPW.increaseIndent();
1609 for (String key : sortedKeys) {
1610 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1611 indentPW.println(key + " = " +
1612 Arrays.toString((Object[]) configApp.get(key)));
1613 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1614 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1615 } else {
1616 indentPW.println(key + " = " + configApp.get(key));
1617 }
Junda Liu43d723a2015-05-12 17:23:45 -07001618 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001619 indentPW.decreaseIndent();
1620 indentPW.decreaseIndent();
1621 indentPW.decreaseIndent();
1622 indentPW.println("");
Junda Liu43d723a2015-05-12 17:23:45 -07001623 }
1624
Hunter Knepshielde601f432020-02-19 10:16:04 -08001625 /**
1626 * Passes without problem when one of these conditions is true:
1627 * - The caller is a privileged UID (e.g. for dumpstate.cpp generating a bug report, where the
1628 * system knows the true caller plumbed in through the {@link android.os.BugreportManager} API).
1629 * - The caller's UID matches the supplied package.
1630 *
1631 * @throws SecurityException if none of the above conditions are met.
1632 */
Rambo Wang1b89db32022-04-15 03:00:32 +00001633 private void enforceCallerIsSystemOrRequestingPackage(@NonNull String requestingPackage)
Hunter Knepshielde601f432020-02-19 10:16:04 -08001634 throws SecurityException {
1635 final int callingUid = Binder.getCallingUid();
1636 if (callingUid == Process.ROOT_UID || callingUid == Process.SYSTEM_UID
1637 || callingUid == Process.SHELL_UID || callingUid == Process.PHONE_UID) {
1638 // Bug reports (dumpstate.cpp) run as SHELL, and let some other privileged UIDs through
1639 // as well.
1640 return;
1641 }
1642 // An app is trying to dump extra detail, block it if they aren't who they claim to be.
1643 AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class);
1644 if (appOps == null) {
1645 throw new SecurityException("No AppOps");
1646 }
1647 // Will throw a SecurityException if the UID and package don't match.
1648 appOps.checkPackage(callingUid, requestingPackage);
1649 }
1650
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001651 /**
1652 * Searches for one or more appropriate {@link CarrierService} instances to dump based on the
1653 * current connections.
1654 *
1655 * @param targetPkgName the target package name to dump carrier services for
1656 * @param considerCarrierPrivileges if true, allow a carrier service to be dumped if it shares
1657 * carrier privileges with {@code targetPkgName};
1658 * otherwise, only dump a carrier service if it is {@code
1659 * targetPkgName}
1660 */
Rambo Wang1b89db32022-04-15 03:00:32 +00001661 private void dumpCarrierServiceIfBound(@NonNull FileDescriptor fd,
1662 @NonNull IndentingPrintWriter indentPW, @NonNull String prefix,
1663 @NonNull String targetPkgName, boolean considerCarrierPrivileges) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001664 // Null package is possible if it's early in the boot process, there was a recent crash, we
1665 // loaded the config from XML most recently, or a SIM slot is empty. Carrier apps with
1666 // long-lived bindings should typically get dumped here regardless. Even if an app is being
1667 // used for multiple phoneIds, we assume that it's smart enough to handle that on its own,
1668 // and that in most cases we'd just be dumping duplicate information and bloating a report.
1669 indentPW.increaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001670 indentPW.println(prefix + " : " + targetPkgName);
1671 Set<String> dumpedPkgNames = new ArraySet<>(mServiceConnection.length);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001672 for (CarrierServiceConnection connection : mServiceConnection) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001673 if (connection == null || !SubscriptionManager.isValidPhoneId(connection.phoneId)
1674 || TextUtils.isEmpty(connection.pkgName)) {
1675 continue;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001676 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001677 final String servicePkgName = connection.pkgName;
1678 // Note: we intentionally ignore system components here because we should NOT match the
1679 // shell caller that's typically used for bug reports via non-BugreportManager triggers.
1680 final boolean exactPackageMatch = TextUtils.equals(targetPkgName, servicePkgName);
1681 final boolean carrierPrivilegesMatch =
1682 considerCarrierPrivileges && hasCarrierPrivileges(targetPkgName,
1683 connection.phoneId);
1684 if (!exactPackageMatch && !carrierPrivilegesMatch) continue;
1685 // Make sure this service is actually alive before trying to dump it. We don't pay
1686 // attention to mServiceBound[connection.phoneId] because typically carrier apps will
1687 // request long-lived bindings, and even if we unbind the app, it may still be alive due
1688 // to CarrierServiceBindHelper. Pull it out as a reference so even if it gets set to
1689 // null within the ServiceConnection during unbinding we can avoid an NPE.
1690 final IBinder service = connection.service;
1691 if (service == null || !service.isBinderAlive() || !service.pingBinder()) continue;
1692 // We've got a live service. Last check is just to make sure we don't dump a package
1693 // multiple times.
1694 if (!dumpedPkgNames.add(servicePkgName)) continue;
1695 if (!exactPackageMatch) {
1696 logd(targetPkgName + " has carrier privileges on phoneId " + connection.phoneId
1697 + ", service provided by " + servicePkgName);
1698 indentPW.increaseIndent();
1699 indentPW.println("Proxy : " + servicePkgName);
1700 indentPW.decreaseIndent();
1701 }
1702 // Flush before we let the app output anything to ensure correct ordering of output.
1703 // Internally, Binder#dump calls flush on its printer after finishing so we don't
1704 // need to do anything after.
1705 indentPW.flush();
1706 try {
1707 logd("Dumping " + servicePkgName);
1708 // We don't need to give the carrier service any args.
1709 connection.service.dump(fd, null /* args */);
1710 logd("Done with " + servicePkgName);
1711 } catch (RemoteException e) {
1712 logd("RemoteException from " + servicePkgName, e);
1713 indentPW.increaseIndent();
1714 indentPW.println("RemoteException");
1715 indentPW.increaseIndent();
1716 e.printStackTrace(indentPW);
1717 indentPW.decreaseIndent();
1718 indentPW.decreaseIndent();
1719 // We won't retry this package again because now it's in dumpedPkgNames.
1720 }
1721 indentPW.println("");
Jordan Liu46ed5db2020-01-28 08:55:20 -08001722 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001723 if (dumpedPkgNames.isEmpty()) {
1724 indentPW.increaseIndent();
1725 indentPW.println("Not bound");
1726 indentPW.decreaseIndent();
1727 indentPW.println("");
1728 }
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001729 indentPW.decreaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001730 }
1731
Rambo Wang1b89db32022-04-15 03:00:32 +00001732 private boolean hasCarrierPrivileges(@NonNull String pkgName, int phoneId) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001733 int[] subIds = SubscriptionManager.getSubId(phoneId);
1734 if (ArrayUtils.isEmpty(subIds)) {
1735 return false;
1736 }
1737 return TelephonyManager.from(mContext).createForSubscriptionId(
1738 subIds[0]).checkCarrierPrivilegesForPackage(pkgName)
1739 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001740 }
1741
Zach Johnson36d7aab2015-05-22 15:43:00 -07001742 private class CarrierServiceConnection implements ServiceConnection {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001743 final int phoneId;
Rambo Wang1b89db32022-04-15 03:00:32 +00001744 @NonNull final String pkgName;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001745 final int eventId;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001746 IBinder service;
1747
Rambo Wang1b89db32022-04-15 03:00:32 +00001748 CarrierServiceConnection(int phoneId, @NonNull String pkgName, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001749 this.phoneId = phoneId;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001750 this.pkgName = pkgName;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001751 this.eventId = eventId;
1752 }
1753
1754 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +00001755 public void onServiceConnected(@NonNull ComponentName name, @NonNull IBinder service) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001756 logd("Connected to config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001757 this.service = service;
1758 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1759 }
1760
1761 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +00001762 public void onServiceDisconnected(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001763 logd("Disconnected from config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001764 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001765 }
1766
1767 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +00001768 public void onBindingDied(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001769 logd("Binding died from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001770 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001771 }
1772
1773 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +00001774 public void onNullBinding(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001775 logd("Null binding from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001776 this.service = null;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001777 }
1778 }
1779
1780 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1781 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +00001782 public void onReceive(@NonNull Context context, @NonNull Intent intent) {
Rambo Wang1cf71512022-04-12 19:09:07 +00001783 switch (intent.getAction()) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001784 case Intent.ACTION_BOOT_COMPLETED:
1785 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1786 break;
1787
Rambo Wang6f8328d2022-04-15 01:26:23 +00001788 case TelephonyManager.ACTION_MULTI_SIM_CONFIG_CHANGED:
1789 mHandler.sendEmptyMessage(EVENT_MULTI_SIM_CONFIG_CHANGED);
1790 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001791 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001792 }
1793 }
1794
Rambo Wang1cf71512022-04-12 19:09:07 +00001795 private class CarrierServiceChangeCallback implements
1796 TelephonyManager.CarrierPrivilegesCallback {
1797 final int mPhoneId;
1798 // CarrierPrivilegesCallback will be triggered upon registration. Filter the first callback
1799 // here since we really care of the *change* of carrier service instead of the content
1800 private boolean mHasSentServiceChangeCallback;
1801
1802 CarrierServiceChangeCallback(int phoneId) {
1803 this.mPhoneId = phoneId;
1804 this.mHasSentServiceChangeCallback = false;
1805 }
1806
1807 @Override
1808 public void onCarrierPrivilegesChanged(
1809 @androidx.annotation.NonNull Set<String> privilegedPackageNames,
1810 @androidx.annotation.NonNull Set<Integer> privilegedUids) {
1811 // Ignored, not interested here
1812 }
1813
1814 @Override
1815 public void onCarrierServiceChanged(
1816 @androidx.annotation.Nullable String carrierServicePackageName,
1817 int carrierServiceUid) {
1818 // Ignore the first callback which is triggered upon registration
1819 if (!mHasSentServiceChangeCallback) {
1820 mHasSentServiceChangeCallback = true;
1821 return;
1822 }
1823 mHandler.sendMessage(
1824 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, mPhoneId, -1,
1825 carrierServicePackageName));
1826 }
1827 }
1828
Rambo Wangab13e3e2021-04-08 15:01:06 -07001829 // Get readable string for the message code supported in this class.
Rambo Wang1b89db32022-04-15 03:00:32 +00001830 @NonNull
Rambo Wangab13e3e2021-04-08 15:01:06 -07001831 private static String eventToString(int code) {
1832 switch (code) {
1833 case EVENT_CLEAR_CONFIG:
1834 return "EVENT_CLEAR_CONFIG";
1835 case EVENT_CONNECTED_TO_DEFAULT:
1836 return "EVENT_CONNECTED_TO_DEFAULT";
1837 case EVENT_CONNECTED_TO_CARRIER:
1838 return "EVENT_CONNECTED_TO_CARRIER";
1839 case EVENT_FETCH_DEFAULT_DONE:
1840 return "EVENT_FETCH_DEFAULT_DONE";
1841 case EVENT_FETCH_CARRIER_DONE:
1842 return "EVENT_FETCH_CARRIER_DONE";
1843 case EVENT_DO_FETCH_DEFAULT:
1844 return "EVENT_DO_FETCH_DEFAULT";
1845 case EVENT_DO_FETCH_CARRIER:
1846 return "EVENT_DO_FETCH_CARRIER";
1847 case EVENT_PACKAGE_CHANGED:
1848 return "EVENT_PACKAGE_CHANGED";
1849 case EVENT_BIND_DEFAULT_TIMEOUT:
1850 return "EVENT_BIND_DEFAULT_TIMEOUT";
1851 case EVENT_BIND_CARRIER_TIMEOUT:
1852 return "EVENT_BIND_CARRIER_TIMEOUT";
1853 case EVENT_CHECK_SYSTEM_UPDATE:
1854 return "EVENT_CHECK_SYSTEM_UPDATE";
1855 case EVENT_SYSTEM_UNLOCKED:
1856 return "EVENT_SYSTEM_UNLOCKED";
1857 case EVENT_FETCH_DEFAULT_TIMEOUT:
1858 return "EVENT_FETCH_DEFAULT_TIMEOUT";
1859 case EVENT_FETCH_CARRIER_TIMEOUT:
1860 return "EVENT_FETCH_CARRIER_TIMEOUT";
1861 case EVENT_SUBSCRIPTION_INFO_UPDATED:
1862 return "EVENT_SUBSCRIPTION_INFO_UPDATED";
1863 case EVENT_MULTI_SIM_CONFIG_CHANGED:
1864 return "EVENT_MULTI_SIM_CONFIG_CHANGED";
1865 case EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG:
1866 return "EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG";
1867 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE:
1868 return "EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE";
1869 case EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG:
1870 return "EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG";
1871 case EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
1872 return "EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT";
1873 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
1874 return "EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT";
1875 default:
1876 return "UNKNOWN(" + code + ")";
1877 }
1878 }
1879
Rambo Wang1b89db32022-04-15 03:00:32 +00001880 private void logd(@NonNull String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001881 Log.d(LOG_TAG, msg);
1882 }
1883
Rambo Wang1b89db32022-04-15 03:00:32 +00001884 private void logd(@NonNull String msg, Throwable tr) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001885 Log.d(LOG_TAG, msg, tr);
1886 }
1887
Rambo Wang1b89db32022-04-15 03:00:32 +00001888 private void logdWithLocalLog(@NonNull String msg) {
chen xudb04c292019-03-26 17:01:15 -07001889 Log.d(LOG_TAG, msg);
1890 mCarrierConfigLoadingLog.log(msg);
1891 }
1892
Rambo Wang1b89db32022-04-15 03:00:32 +00001893 private void loge(@NonNull String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001894 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07001895 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001896 }
1897}