blob: 7952bf4e47a7d7692f011f7462f4ddb9d0dcc226 [file] [log] [blame]
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001/*
Jonathan Basseri6465afd2015-02-25 13:05:57 -08002 * Copyright (c) 2015, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jonathan Basseri6465afd2015-02-25 13:05:57 -080017package com.android.phone;
18
Jonathan Basseri65273c82017-07-25 15:08:42 -070019import static android.service.carrier.CarrierService.ICarrierServiceWrapper.KEY_CONFIG_BUNDLE;
20import static android.service.carrier.CarrierService.ICarrierServiceWrapper.RESULT_ERROR;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080021
Jonathan Basseri11f89572015-05-05 11:57:39 -070022import android.annotation.NonNull;
Torbjorn Eklund723527a2019-02-13 11:16:25 +010023import android.annotation.Nullable;
Hunter Knepshielde601f432020-02-19 10:16:04 -080024import android.app.AppOpsManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080025import android.content.BroadcastReceiver;
26import android.content.ComponentName;
27import android.content.Context;
28import android.content.Intent;
29import android.content.IntentFilter;
30import android.content.ServiceConnection;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070031import android.content.SharedPreferences;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070032import android.content.pm.PackageInfo;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070033import android.content.pm.PackageManager;
Junda Liu43d723a2015-05-12 17:23:45 -070034import android.os.Binder;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070035import android.os.Build;
Jonathan Basseri65273c82017-07-25 15:08:42 -070036import android.os.Bundle;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080037import android.os.Handler;
Rambo Wanga27fbe52022-04-12 19:09:07 +000038import android.os.HandlerExecutor;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080039import android.os.IBinder;
Rambo Wang7e3bc122021-03-23 09:24:38 -070040import android.os.Looper;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080041import android.os.Message;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070042import android.os.PersistableBundle;
Hunter Knepshielde601f432020-02-19 10:16:04 -080043import android.os.Process;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080044import android.os.RemoteException;
Jonathan Basseri65273c82017-07-25 15:08:42 -070045import android.os.ResultReceiver;
Meng Wang97a6a462020-01-23 16:22:16 -080046import android.os.UserHandle;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070047import android.preference.PreferenceManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080048import android.service.carrier.CarrierIdentifier;
Zach Johnson36d7aab2015-05-22 15:43:00 -070049import android.service.carrier.CarrierService;
50import android.service.carrier.ICarrierService;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080051import android.telephony.CarrierConfigManager;
52import android.telephony.SubscriptionManager;
Peter Wangc035ce42020-01-08 21:00:22 -080053import android.telephony.TelephonyFrameworkInitializer;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080054import android.telephony.TelephonyManager;
rambowangd63ba342022-10-02 11:21:08 -050055import android.telephony.TelephonyRegistryManager;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -080056import android.text.TextUtils;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080057import android.util.ArraySet;
chen xudb04c292019-03-26 17:01:15 -070058import android.util.LocalLog;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080059import android.util.Log;
60
Rambo Wang7e3bc122021-03-23 09:24:38 -070061import com.android.internal.annotations.VisibleForTesting;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080062import com.android.internal.telephony.ICarrierConfigLoader;
63import com.android.internal.telephony.IccCardConstants;
64import com.android.internal.telephony.Phone;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080065import com.android.internal.telephony.PhoneFactory;
Nathan Harold48ac0972019-03-13 22:33:01 -070066import com.android.internal.telephony.SubscriptionInfoUpdater;
Jeff Davidsona8e4e242018-03-15 17:16:18 -070067import com.android.internal.telephony.TelephonyPermissions;
Meng Wanga320b942019-11-25 11:21:26 -080068import com.android.internal.telephony.util.ArrayUtils;
shuoq26a3a4c2016-12-16 11:06:48 -080069import com.android.internal.util.IndentingPrintWriter;
arunvoddu664c36a2021-10-11 20:09:28 +000070import com.android.telephony.Rlog;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080071
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070072import java.io.File;
Junda Liu43d723a2015-05-12 17:23:45 -070073import java.io.FileDescriptor;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070074import java.io.FileInputStream;
75import java.io.FileNotFoundException;
76import java.io.FileOutputStream;
Jonathan Basseri1f743c92015-05-15 00:19:46 -070077import java.io.FilenameFilter;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070078import java.io.IOException;
Junda Liu43d723a2015-05-12 17:23:45 -070079import java.io.PrintWriter;
shuoq26a3a4c2016-12-16 11:06:48 -080080import java.util.ArrayList;
81import java.util.Arrays;
82import java.util.Collections;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080083import java.util.List;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080084import java.util.Set;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080085
86/**
87 * CarrierConfigLoader binds to privileged carrier apps to fetch carrier config overlays.
Jonathan Basseri6465afd2015-02-25 13:05:57 -080088 */
Jonathan Basseri6465afd2015-02-25 13:05:57 -080089public class CarrierConfigLoader extends ICarrierConfigLoader.Stub {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070090 private static final String LOG_TAG = "CarrierConfigLoader";
Meng Wang33ad2bc2017-03-16 20:21:20 -070091
92 // Package name for platform carrier config app, bundled with system image.
Rambo Wangfe0d7c12022-04-15 03:00:32 +000093 @NonNull private final String mPlatformCarrierConfigPackage;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080094
95 /** The singleton instance. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +000096 @Nullable private static CarrierConfigLoader sInstance;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080097 // The context for phone app, passed from PhoneGlobals.
Rambo Wangfe0d7c12022-04-15 03:00:32 +000098 @NonNull private Context mContext;
99
100 // All the states below (array indexed by phoneId) are non-null. But the member of the array
101 // is nullable, when e.g. the config for the phone is not loaded yet
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800102 // Carrier configs from default app, indexed by phoneID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000103 @NonNull private PersistableBundle[] mConfigFromDefaultApp;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800104 // Carrier configs from privileged carrier config app, indexed by phoneID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000105 @NonNull private PersistableBundle[] mConfigFromCarrierApp;
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100106 // Persistent Carrier configs that are provided via the override test API, indexed by phone ID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000107 @NonNull private PersistableBundle[] mPersistentOverrideConfigs;
Hall Liu506724b2018-10-22 18:16:14 -0700108 // Carrier configs that are provided via the override test API, indexed by phone ID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000109 @NonNull private PersistableBundle[] mOverrideConfigs;
Jayachandran C645ec612020-01-10 10:30:25 -0800110 // Carrier configs to override code default when there is no SIM inserted
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000111 @NonNull private PersistableBundle mNoSimConfig;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800112 // Service connection for binding to config app.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000113 @NonNull private CarrierServiceConnection[] mServiceConnection;
Jayachandran C645ec612020-01-10 10:30:25 -0800114 // Service connection for binding to carrier config app for no SIM config.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000115 @NonNull private CarrierServiceConnection[] mServiceConnectionForNoSimConfig;
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700116 // Whether we are bound to a service for each phone
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000117 @NonNull private boolean[] mServiceBound;
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700118 // Whether we are bound to a service for no SIM config
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000119 @NonNull private boolean[] mServiceBoundForNoSimConfig;
Sarah Chin807d5c62020-03-30 17:21:09 -0700120 // Whether we have sent config change broadcast for each phone id.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000121 @NonNull private boolean[] mHasSentConfigChange;
Sarah Chin807d5c62020-03-30 17:21:09 -0700122 // Whether the broadcast was sent from EVENT_SYSTEM_UNLOCKED, to track rebroadcasts
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000123 @NonNull private boolean[] mFromSystemUnlocked;
Rambo Wanga27fbe52022-04-12 19:09:07 +0000124 // CarrierService change monitoring
125 @NonNull private CarrierServiceChangeCallback[] mCarrierServiceChangeCallbacks;
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000126
Nathan Harold48ac0972019-03-13 22:33:01 -0700127 // SubscriptionInfoUpdater
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000128 @NonNull private final SubscriptionInfoUpdater mSubscriptionInfoUpdater;
Rambo Wanga27fbe52022-04-12 19:09:07 +0000129 // Broadcast receiver for system events (BootCompleted, MultiSimConfigChanged etc.)
130 @NonNull
131 private final BroadcastReceiver mSystemBroadcastReceiver = new ConfigLoaderBroadcastReceiver();
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000132 @NonNull private final LocalLog mCarrierConfigLoadingLog = new LocalLog(100);
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000133 // Number of phone instances (active modem count)
134 private int mNumPhones;
chen xudb04c292019-03-26 17:01:15 -0700135
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800136
137 // Message codes; see mHandler below.
138 // Request from SubscriptionInfoUpdater when SIM becomes absent or error.
139 private static final int EVENT_CLEAR_CONFIG = 0;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800140 // Has connected to default app.
141 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
142 // Has connected to carrier app.
143 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700144 // Config has been loaded from default app (or cache).
145 private static final int EVENT_FETCH_DEFAULT_DONE = 5;
146 // Config has been loaded from carrier app (or cache).
147 private static final int EVENT_FETCH_CARRIER_DONE = 6;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700148 // Attempt to fetch from default app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700149 private static final int EVENT_DO_FETCH_DEFAULT = 7;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700150 // Attempt to fetch from carrier app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700151 private static final int EVENT_DO_FETCH_CARRIER = 8;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700152 // A package has been installed, uninstalled, or updated.
153 private static final int EVENT_PACKAGE_CHANGED = 9;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700154 // Bind timed out for the default app.
155 private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
156 // Bind timed out for a carrier app.
157 private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700158 // Check if the system fingerprint has changed.
159 private static final int EVENT_CHECK_SYSTEM_UPDATE = 12;
Nanxi Chen3d670502016-03-17 16:32:09 -0700160 // Rerun carrier config binding after system is unlocked.
161 private static final int EVENT_SYSTEM_UNLOCKED = 13;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700162 // Fetching config timed out from the default app.
163 private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14;
164 // Fetching config timed out from a carrier app.
165 private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15;
Nathan Harold48ac0972019-03-13 22:33:01 -0700166 // SubscriptionInfoUpdater has finished updating the sub for the carrier config.
167 private static final int EVENT_SUBSCRIPTION_INFO_UPDATED = 16;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700168 // Multi-SIM config changed.
169 private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 17;
Jayachandran C645ec612020-01-10 10:30:25 -0800170 // Attempt to fetch from default app or read from XML for no SIM case.
171 private static final int EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG = 18;
172 // No SIM config has been loaded from default app (or cache).
173 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE = 19;
174 // Has connected to default app for no SIM config.
175 private static final int EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG = 20;
176 // Bind timed out for the default app when trying to fetch no SIM config.
177 private static final int EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 21;
178 // Fetching config timed out from the default app for no SIM config.
179 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 22;
Rambo Wangab13e3e2021-04-08 15:01:06 -0700180 // NOTE: any new EVENT_* values must be added to method eventToString().
Jonathan Basseri930701e2015-06-11 20:56:43 -0700181
Junda Liu6cb45002016-03-22 17:18:20 -0700182 private static final int BIND_TIMEOUT_MILLIS = 30000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800183
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800184 // Keys used for saving and restoring config bundle from file.
185 private static final String KEY_VERSION = "__carrier_config_package_version__";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800186
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100187 private static final String OVERRIDE_PACKAGE_ADDITION = "-override";
188
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700189 // SharedPreferences key for last known build fingerprint.
190 private static final String KEY_FINGERPRINT = "build_fingerprint";
191
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800192 // Argument for #dump that indicates we should also call the default and specified carrier
193 // service's #dump method. In multi-SIM devices, it's possible that carrier A is on SIM 1 and
194 // carrier B is on SIM 2, in which case we should not dump carrier B's service when carrier A
195 // requested the dump.
196 private static final String DUMP_ARG_REQUESTING_PACKAGE = "--requesting-package";
197
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800198 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700199 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700200 // For each phoneId, the event sequence should be:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700201 // fetch default, connected to default, fetch default (async), fetch default done,
202 // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700203 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700204 // If there is a saved config file for either the default app or the carrier app, we skip
205 // binding to the app and go straight from fetch to loaded.
206 //
207 // At any time, at most one connection is active. If events are not in this order, previous
208 // connection will be unbound, so only latest event takes effect.
209 //
210 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
211 // 1. loading from carrier app (even if read from a file)
212 // 2. loading from default app if there is no carrier app (even if read from a file)
213 // 3. clearing config (e.g. due to sim removal)
214 // 4. encountering bind or IPC error
Jonathan Basseri65273c82017-07-25 15:08:42 -0700215 private class ConfigHandler extends Handler {
Rambo Wang7e3bc122021-03-23 09:24:38 -0700216 ConfigHandler(@NonNull Looper looper) {
217 super(looper);
218 }
219
Jonathan Basseri65273c82017-07-25 15:08:42 -0700220 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000221 public void handleMessage(@NonNull Message msg) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700222 final int phoneId = msg.arg1;
Rambo Wangab13e3e2021-04-08 15:01:06 -0700223 logdWithLocalLog("mHandler: " + eventToString(msg.what) + " phoneId: " + phoneId);
Malcolm Chen5ea18532019-10-24 19:55:44 -0700224 if (!SubscriptionManager.isValidPhoneId(phoneId)
225 && msg.what != EVENT_MULTI_SIM_CONFIG_CHANGED) {
226 return;
227 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800228 switch (msg.what) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800229 case EVENT_CLEAR_CONFIG: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700230 clearConfigForPhone(phoneId, true);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800231 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700232 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700233
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800234 case EVENT_SYSTEM_UNLOCKED: {
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000235 for (int i = 0; i < mNumPhones; ++i) {
Sarah Chin807d5c62020-03-30 17:21:09 -0700236 // When the user unlocks the device, send the broadcast again (with a
237 // rebroadcast extra) if we have sent it before unlock. This will avoid
238 // trying to load the carrier config when the SIM is still loading when the
239 // unlock happens.
Junda Liu03aad762017-07-21 13:32:17 -0700240 if (mHasSentConfigChange[i]) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800241 logdWithLocalLog("System unlocked");
Sarah Chin807d5c62020-03-30 17:21:09 -0700242 mFromSystemUnlocked[i] = true;
Junda Liu03aad762017-07-21 13:32:17 -0700243 updateConfigForPhoneId(i);
244 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700245 }
246 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700247 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700248
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800249 case EVENT_PACKAGE_CHANGED: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700250 final String carrierPackageName = (String) msg.obj;
Rambo Wanga27fbe52022-04-12 19:09:07 +0000251 // Always clear up the cache and re-load config from scratch since the carrier
252 // service change is reliable and specific to the phoneId now.
253 clearCachedConfigForPackage(carrierPackageName);
254 logdWithLocalLog("Package changed: " + carrierPackageName
255 + ", phone=" + phoneId);
256 updateConfigForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700257 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700258 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700259
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800260 case EVENT_DO_FETCH_DEFAULT: {
Rambo Wang9bc7d362021-03-09 09:10:58 -0800261 // Clear in-memory cache for carrier app config, so when carrier app gets
262 // uninstalled, no stale config is left.
263 if (mConfigFromCarrierApp[phoneId] != null
264 && getCarrierPackageForPhoneId(phoneId) == null) {
265 mConfigFromCarrierApp[phoneId] = null;
266 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100267 // Restore persistent override values.
268 PersistableBundle config = restoreConfigFromXml(
269 mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId);
270 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800271 logd("Loaded persistent override config from XML. package="
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100272 + mPlatformCarrierConfigPackage
273 + " phoneId=" + phoneId);
274 mPersistentOverrideConfigs[phoneId] = config;
275 }
276
277 config = restoreConfigFromXml(mPlatformCarrierConfigPackage, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700278 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800279 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700280 "Loaded config from XML. package="
281 + mPlatformCarrierConfigPackage
282 + " phoneId="
283 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700284 mConfigFromDefaultApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700285 Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700286 newMsg.getData().putBoolean("loaded_from_xml", true);
287 mHandler.sendMessage(newMsg);
288 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700289 // No cached config, so fetch it from the default app.
290 if (bindToConfigPackage(
291 mPlatformCarrierConfigPackage,
292 phoneId,
293 EVENT_CONNECTED_TO_DEFAULT)) {
294 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800295 obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1 /*arg2*/,
296 getMessageToken(phoneId)),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700297 BIND_TIMEOUT_MILLIS);
298 } else {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800299 // Put a stub bundle in place so that the rest of the logic continues
300 // smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000301 mConfigFromDefaultApp[phoneId] = new PersistableBundle();
Jonathan Basseri65273c82017-07-25 15:08:42 -0700302 // Send broadcast if bind fails.
Nathan Harold48ac0972019-03-13 22:33:01 -0700303 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700304 // TODO: We *must* call unbindService even if bindService returns false.
305 // (And possibly if SecurityException was thrown.)
chen xudb04c292019-03-26 17:01:15 -0700306 loge("binding to default app: "
307 + mPlatformCarrierConfigPackage + " fails");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700308 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800309 }
310 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700311 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800312
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800313 case EVENT_CONNECTED_TO_DEFAULT: {
Rambo Wang610fdf62021-03-08 21:37:11 -0800314 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT, getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700315 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800316 // If new service connection has been created, unbind.
317 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700318 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800319 break;
320 }
chen xu02581692018-11-11 19:03:44 -0800321 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700322 // ResultReceiver callback will execute in this Handler's thread.
323 final ResultReceiver resultReceiver =
324 new ResultReceiver(this) {
325 @Override
326 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700327 unbindIfBound(mContext, conn, phoneId);
Rambo Wanga9d27a52021-04-09 16:17:33 -0700328 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT,
329 getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700330 // If new service connection has been created, this is stale.
331 if (mServiceConnection[phoneId] != conn) {
332 loge("Received response for stale request.");
333 return;
334 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700335 if (resultCode == RESULT_ERROR || resultData == null) {
336 // On error, abort config fetching.
337 loge("Failed to get carrier config");
Nathan Harold48ac0972019-03-13 22:33:01 -0700338 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700339 return;
340 }
341 PersistableBundle config =
342 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100343 saveConfigToXml(mPlatformCarrierConfigPackage, "", phoneId,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800344 carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700345 mConfigFromDefaultApp[phoneId] = config;
346 sendMessage(
347 obtainMessage(
348 EVENT_FETCH_DEFAULT_DONE, phoneId, -1));
349 }
350 };
351 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800352 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700353 ICarrierService carrierService =
354 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800355 carrierService.getCarrierConfig(phoneId, carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800356 logdWithLocalLog("Fetch config for default app: "
chen xudb04c292019-03-26 17:01:15 -0700357 + mPlatformCarrierConfigPackage
358 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700359 } catch (RemoteException e) {
chen xudb04c292019-03-26 17:01:15 -0700360 loge("Failed to get carrier config from default app: " +
361 mPlatformCarrierConfigPackage + " err: " + e.toString());
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700362 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700363 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800364 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700365 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800366 obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1 /*arg2*/,
367 getMessageToken(phoneId)),
Jonathan Basseri65273c82017-07-25 15:08:42 -0700368 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800369 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700370 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800371
Jonathan Basseri930701e2015-06-11 20:56:43 -0700372 case EVENT_BIND_DEFAULT_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800373 case EVENT_FETCH_DEFAULT_TIMEOUT: {
374 loge("Bind/fetch time out from " + mPlatformCarrierConfigPackage);
Rambo Wang610fdf62021-03-08 21:37:11 -0800375 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT, getMessageToken(phoneId));
chen xuf60b3162019-05-01 21:31:05 -0700376 // If we attempted to bind to the app, but the service connection is null due to
377 // the race condition that clear config event happens before bind/fetch complete
378 // then config was cleared while we were waiting and we should not continue.
379 if (mServiceConnection[phoneId] != null) {
380 // If a ResponseReceiver callback is in the queue when this happens, we will
381 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700382 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700383 broadcastConfigChangedIntent(phoneId);
384 }
Nathan Harold1122e3d2021-01-22 15:45:24 -0800385 // Put a stub bundle in place so that the rest of the logic continues smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000386 mConfigFromDefaultApp[phoneId] = new PersistableBundle();
Nathan Harold48ac0972019-03-13 22:33:01 -0700387 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700388 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700389 }
Jonathan Basseri930701e2015-06-11 20:56:43 -0700390
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800391 case EVENT_FETCH_DEFAULT_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700392 // If we attempted to bind to the app, but the service connection is null, then
393 // config was cleared while we were waiting and we should not continue.
394 if (!msg.getData().getBoolean("loaded_from_xml", false)
395 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800396 break;
397 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700398 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700399 if (carrierPackageName != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800400 logd("Found carrier config app: " + carrierPackageName);
Jordan Liu38a614c2019-03-20 15:01:17 -0700401 sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId, -1));
Jonathan Basserib919e932015-05-27 16:53:08 +0000402 } else {
Nathan Harold48ac0972019-03-13 22:33:01 -0700403 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700404 }
405 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700406 }
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700407
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800408 case EVENT_DO_FETCH_CARRIER: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700409 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700410 final PersistableBundle config =
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100411 restoreConfigFromXml(carrierPackageName, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700412 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800413 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700414 "Loaded config from XML. package="
415 + carrierPackageName
416 + " phoneId="
417 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700418 mConfigFromCarrierApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700419 Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700420 newMsg.getData().putBoolean("loaded_from_xml", true);
421 sendMessage(newMsg);
422 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700423 // No cached config, so fetch it from a carrier app.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800424 if (carrierPackageName != null && bindToConfigPackage(carrierPackageName,
425 phoneId, EVENT_CONNECTED_TO_CARRIER)) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700426 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800427 obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1 /*arg2*/,
428 getMessageToken(phoneId)),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700429 BIND_TIMEOUT_MILLIS);
430 } else {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800431 // Put a stub bundle in place so that the rest of the logic continues
432 // smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000433 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
Jonathan Basseri65273c82017-07-25 15:08:42 -0700434 // Send broadcast if bind fails.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700435 broadcastConfigChangedIntent(phoneId);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800436 loge("Bind to carrier app: " + carrierPackageName + " fails");
Nathan Harold48ac0972019-03-13 22:33:01 -0700437 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700438 }
439 }
440 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700441 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700442
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800443 case EVENT_CONNECTED_TO_CARRIER: {
Rambo Wang610fdf62021-03-08 21:37:11 -0800444 removeMessages(EVENT_BIND_CARRIER_TIMEOUT, getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700445 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800446 // If new service connection has been created, unbind.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700447 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700448 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800449 break;
450 }
chen xu02581692018-11-11 19:03:44 -0800451 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700452 // ResultReceiver callback will execute in this Handler's thread.
453 final ResultReceiver resultReceiver =
454 new ResultReceiver(this) {
455 @Override
456 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700457 unbindIfBound(mContext, conn, phoneId);
Rambo Wanga9d27a52021-04-09 16:17:33 -0700458 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT,
459 getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700460 // If new service connection has been created, this is stale.
461 if (mServiceConnection[phoneId] != conn) {
462 loge("Received response for stale request.");
463 return;
464 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700465 if (resultCode == RESULT_ERROR || resultData == null) {
466 // On error, abort config fetching.
chen xudb04c292019-03-26 17:01:15 -0700467 loge("Failed to get carrier config from carrier app: "
468 + getCarrierPackageForPhoneId(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700469 broadcastConfigChangedIntent(phoneId);
Nathan Harold48ac0972019-03-13 22:33:01 -0700470 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700471 return;
472 }
473 PersistableBundle config =
474 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100475 saveConfigToXml(getCarrierPackageForPhoneId(phoneId), "",
476 phoneId, carrierId, config);
Amit Mahajan304e92b2021-04-08 14:03:30 -0700477 if (config != null) {
478 mConfigFromCarrierApp[phoneId] = config;
479 } else {
480 logdWithLocalLog("Config from carrier app is null "
481 + "for phoneId " + phoneId);
482 // Put a stub bundle in place so that the rest of the logic
483 // continues smoothly.
484 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
485 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700486 sendMessage(
487 obtainMessage(
488 EVENT_FETCH_CARRIER_DONE, phoneId, -1));
489 }
490 };
491 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800492 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700493 ICarrierService carrierService =
494 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800495 carrierService.getCarrierConfig(phoneId, carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800496 logdWithLocalLog("Fetch config for carrier app: "
chen xudb04c292019-03-26 17:01:15 -0700497 + getCarrierPackageForPhoneId(phoneId)
498 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700499 } catch (RemoteException e) {
500 loge("Failed to get carrier config: " + e.toString());
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700501 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700502 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800503 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700504 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800505 obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1 /*arg2*/,
506 getMessageToken(phoneId)),
Jonathan Basseri65273c82017-07-25 15:08:42 -0700507 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800508 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700509 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800510
Jonathan Basseri930701e2015-06-11 20:56:43 -0700511 case EVENT_BIND_CARRIER_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800512 case EVENT_FETCH_CARRIER_TIMEOUT: {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800513 loge("Bind/fetch from carrier app timeout, package="
514 + getCarrierPackageForPhoneId(phoneId));
Rambo Wang610fdf62021-03-08 21:37:11 -0800515 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT, getMessageToken(phoneId));
chen xuf60b3162019-05-01 21:31:05 -0700516 // If we attempted to bind to the app, but the service connection is null due to
517 // the race condition that clear config event happens before bind/fetch complete
518 // then config was cleared while we were waiting and we should not continue.
519 if (mServiceConnection[phoneId] != null) {
520 // If a ResponseReceiver callback is in the queue when this happens, we will
521 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700522 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700523 broadcastConfigChangedIntent(phoneId);
524 }
Nathan Harold1122e3d2021-01-22 15:45:24 -0800525 // Put a stub bundle in place so that the rest of the logic continues smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000526 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
Nathan Harold48ac0972019-03-13 22:33:01 -0700527 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700528 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700529 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800530 case EVENT_FETCH_CARRIER_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700531 // If we attempted to bind to the app, but the service connection is null, then
532 // config was cleared while we were waiting and we should not continue.
533 if (!msg.getData().getBoolean("loaded_from_xml", false)
534 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800535 break;
536 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700537 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800538 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700539 }
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700540
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800541 case EVENT_CHECK_SYSTEM_UPDATE: {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700542 SharedPreferences sharedPrefs =
543 PreferenceManager.getDefaultSharedPreferences(mContext);
544 final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
545 if (!Build.FINGERPRINT.equals(lastFingerprint)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800546 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700547 "Build fingerprint changed. old: "
548 + lastFingerprint
549 + " new: "
550 + Build.FINGERPRINT);
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700551 clearCachedConfigForPackage(null);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700552 sharedPrefs
553 .edit()
554 .putString(KEY_FINGERPRINT, Build.FINGERPRINT)
555 .apply();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700556 }
557 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700558 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700559
560 case EVENT_SUBSCRIPTION_INFO_UPDATED:
561 broadcastConfigChangedIntent(phoneId);
562 break;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700563 case EVENT_MULTI_SIM_CONFIG_CHANGED:
564 onMultiSimConfigChanged();
565 break;
Jayachandran C645ec612020-01-10 10:30:25 -0800566
567 case EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG: {
568 PersistableBundle config =
569 restoreNoSimConfigFromXml(mPlatformCarrierConfigPackage);
570
571 if (config != null) {
572 logd("Loaded no SIM config from XML. package="
573 + mPlatformCarrierConfigPackage);
574 mNoSimConfig = config;
575 sendMessage(
576 obtainMessage(
577 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE,
578 phoneId, -1));
579 } else {
580 // No cached config, so fetch it from the default app.
581 if (bindToConfigPackage(
582 mPlatformCarrierConfigPackage,
583 phoneId,
584 EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG)) {
585 sendMessageDelayed(
586 obtainMessage(
587 EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT,
588 phoneId, -1), BIND_TIMEOUT_MILLIS);
589 } else {
590 broadcastConfigChangedIntent(phoneId, false);
591 // TODO: We *must* call unbindService even if bindService returns false.
592 // (And possibly if SecurityException was thrown.)
593 loge("binding to default app to fetch no SIM config: "
594 + mPlatformCarrierConfigPackage + " fails");
595 }
596 }
597 break;
598 }
599
600 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE: {
601 broadcastConfigChangedIntent(phoneId, false);
602 break;
603 }
604
605 case EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
606 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT: {
607 loge("Bind/fetch time out for no SIM config from "
608 + mPlatformCarrierConfigPackage);
609 removeMessages(EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
610 // If we attempted to bind to the app, but the service connection is null due to
611 // the race condition that clear config event happens before bind/fetch complete
612 // then config was cleared while we were waiting and we should not continue.
613 if (mServiceConnectionForNoSimConfig[phoneId] != null) {
614 // If a ResponseReceiver callback is in the queue when this happens, we will
615 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700616 unbindIfBoundForNoSimConfig(mContext,
617 mServiceConnectionForNoSimConfig[phoneId], phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800618 }
619 broadcastConfigChangedIntent(phoneId, false);
620 break;
621 }
622
623 case EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG: {
624 removeMessages(EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
625 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
626 // If new service connection has been created, unbind.
627 if (mServiceConnectionForNoSimConfig[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700628 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800629 break;
630 }
631
632 // ResultReceiver callback will execute in this Handler's thread.
633 final ResultReceiver resultReceiver =
634 new ResultReceiver(this) {
635 @Override
636 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700637 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800638 // If new service connection has been created, this is stale.
639 if (mServiceConnectionForNoSimConfig[phoneId] != conn) {
640 loge("Received response for stale request.");
641 return;
642 }
643 removeMessages(EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
644 if (resultCode == RESULT_ERROR || resultData == null) {
645 // On error, abort config fetching.
646 loge("Failed to get no SIM carrier config");
647 return;
648 }
649 PersistableBundle config =
650 resultData.getParcelable(KEY_CONFIG_BUNDLE);
651 saveNoSimConfigToXml(mPlatformCarrierConfigPackage, config);
652 mNoSimConfig = config;
653 sendMessage(
654 obtainMessage(
655 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE,
656 phoneId, -1));
657 }
658 };
659 // Now fetch the config asynchronously from the ICarrierService.
660 try {
661 ICarrierService carrierService =
662 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800663 carrierService.getCarrierConfig(phoneId, null, resultReceiver);
Jayachandran C645ec612020-01-10 10:30:25 -0800664 logdWithLocalLog("Fetch no sim config from default app: "
665 + mPlatformCarrierConfigPackage);
666 } catch (RemoteException e) {
667 loge("Failed to get no sim carrier config from default app: " +
668 mPlatformCarrierConfigPackage + " err: " + e.toString());
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700669 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800670 break; // So we don't set a timeout.
671 }
672 sendMessageDelayed(
673 obtainMessage(
674 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT,
675 phoneId, -1), BIND_TIMEOUT_MILLIS);
676 break;
677 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800678 }
679 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700680 }
681
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000682 @NonNull private final Handler mHandler;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800683
684 /**
685 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
686 * receiver for relevant events.
687 */
Rambo Wang7e3bc122021-03-23 09:24:38 -0700688 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000689 /* package */ CarrierConfigLoader(@NonNull Context context,
690 @NonNull SubscriptionInfoUpdater subscriptionInfoUpdater, @NonNull Looper looper) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800691 mContext = context;
Meng Wang33ad2bc2017-03-16 20:21:20 -0700692 mPlatformCarrierConfigPackage =
693 mContext.getString(R.string.platform_carrier_config_package);
Rambo Wang7e3bc122021-03-23 09:24:38 -0700694 mHandler = new ConfigHandler(looper);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800695
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000696 IntentFilter systemEventsFilter = new IntentFilter();
697 systemEventsFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
698 systemEventsFilter.addAction(TelephonyManager.ACTION_MULTI_SIM_CONFIG_CHANGED);
Rambo Wanga27fbe52022-04-12 19:09:07 +0000699 context.registerReceiver(mSystemBroadcastReceiver, systemEventsFilter);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800700
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000701 mNumPhones = TelephonyManager.from(context).getActiveModemCount();
702 mConfigFromDefaultApp = new PersistableBundle[mNumPhones];
703 mConfigFromCarrierApp = new PersistableBundle[mNumPhones];
704 mPersistentOverrideConfigs = new PersistableBundle[mNumPhones];
705 mOverrideConfigs = new PersistableBundle[mNumPhones];
Rambo Wang42026112021-04-07 20:57:28 +0000706 mNoSimConfig = new PersistableBundle();
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000707 mServiceConnection = new CarrierServiceConnection[mNumPhones];
708 mServiceBound = new boolean[mNumPhones];
709 mHasSentConfigChange = new boolean[mNumPhones];
710 mFromSystemUnlocked = new boolean[mNumPhones];
711 mServiceConnectionForNoSimConfig = new CarrierServiceConnection[mNumPhones];
712 mServiceBoundForNoSimConfig = new boolean[mNumPhones];
Rambo Wanga27fbe52022-04-12 19:09:07 +0000713 mCarrierServiceChangeCallbacks = new CarrierServiceChangeCallback[mNumPhones];
714 for (int phoneId = 0; phoneId < mNumPhones; phoneId++) {
715 mCarrierServiceChangeCallbacks[phoneId] = new CarrierServiceChangeCallback(phoneId);
716 TelephonyManager.from(context).registerCarrierPrivilegesCallback(phoneId,
717 new HandlerExecutor(mHandler), mCarrierServiceChangeCallbacks[phoneId]);
718 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800719 logd("CarrierConfigLoader has started");
Rambo Wang7e3bc122021-03-23 09:24:38 -0700720 mSubscriptionInfoUpdater = subscriptionInfoUpdater;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700721 mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800722 }
723
724 /**
725 * Initialize the singleton CarrierConfigLoader instance.
726 *
727 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
728 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000729 @NonNull
730 /* package */ static CarrierConfigLoader init(@NonNull Context context) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800731 synchronized (CarrierConfigLoader.class) {
732 if (sInstance == null) {
Rambo Wang7e3bc122021-03-23 09:24:38 -0700733 sInstance = new CarrierConfigLoader(context,
734 PhoneFactory.getSubscriptionInfoUpdater(), Looper.myLooper());
Brad Ebingerfa6575f2021-05-04 00:29:50 +0000735 // Make this service available through ServiceManager.
736 TelephonyFrameworkInitializer.getTelephonyServiceManager()
737 .getCarrierConfigServiceRegisterer().register(sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800738 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700739 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800740 }
741 return sInstance;
742 }
743 }
744
Rambo Wang7e3bc122021-03-23 09:24:38 -0700745 @VisibleForTesting
746 /* package */ void clearConfigForPhone(int phoneId, boolean fetchNoSimConfig) {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700747 /* Ignore clear configuration request if device is being shutdown. */
748 Phone phone = PhoneFactory.getPhone(phoneId);
749 if (phone != null) {
750 if (phone.isShuttingDown()) {
751 return;
752 }
753 }
754
755 mConfigFromDefaultApp[phoneId] = null;
756 mConfigFromCarrierApp[phoneId] = null;
757 mServiceConnection[phoneId] = null;
758 mHasSentConfigChange[phoneId] = false;
759
Jayachandran C645ec612020-01-10 10:30:25 -0800760 if (fetchNoSimConfig) {
761 // To fetch no SIM config
762 mHandler.sendMessage(
763 mHandler.obtainMessage(
764 EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG, phoneId, -1));
765 }
Malcolm Chen5ea18532019-10-24 19:55:44 -0700766 }
767
Nathan Harold48ac0972019-03-13 22:33:01 -0700768 private void notifySubscriptionInfoUpdater(int phoneId) {
769 String configPackagename;
770 PersistableBundle configToSend;
771 int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
772 // Prefer the carrier privileged carrier app, but if there is not one, use the platform
773 // default carrier app.
774 if (mConfigFromCarrierApp[phoneId] != null) {
775 configPackagename = getCarrierPackageForPhoneId(phoneId);
776 configToSend = mConfigFromCarrierApp[phoneId];
777 } else {
778 configPackagename = mPlatformCarrierConfigPackage;
779 configToSend = mConfigFromDefaultApp[phoneId];
780 }
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700781
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800782 if (configToSend == null) {
Rambo Wang42026112021-04-07 20:57:28 +0000783 configToSend = new PersistableBundle();
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800784 }
785
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700786 // mOverrideConfigs is for testing. And it will override current configs.
787 PersistableBundle config = mOverrideConfigs[phoneId];
788 if (config != null) {
Torbjorn Eklund1050cb02018-11-16 14:05:38 +0100789 configToSend = new PersistableBundle(configToSend);
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700790 configToSend.putAll(config);
791 }
792
Nathan Harold48ac0972019-03-13 22:33:01 -0700793 mSubscriptionInfoUpdater.updateSubscriptionByCarrierConfigAndNotifyComplete(
794 phoneId, configPackagename, configToSend,
795 mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1));
796 }
797
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800798 private void broadcastConfigChangedIntent(int phoneId) {
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800799 broadcastConfigChangedIntent(phoneId, true);
800 }
801
802 private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) {
rambowangd63ba342022-10-02 11:21:08 -0500803 int subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
804 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
805 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
806
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800807 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800808 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
chen xuf9db49f2019-03-31 23:04:42 -0700809 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800810 if (addSubIdExtra) {
rambowangd63ba342022-10-02 11:21:08 -0500811 int simApplicationState = getSimApplicationStateForPhone(phoneId);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800812 // Include subId/carrier id extra only if SIM records are loaded
813 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
814 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
rambowangd63ba342022-10-02 11:21:08 -0500815 subId = SubscriptionManager.getSubscriptionId(phoneId);
816 carrierId = getCarrierIdForPhoneId(phoneId);
817 specificCarrierId = getSpecificCarrierIdForPhoneId(phoneId);
818 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID, specificCarrierId);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800819 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
rambowangd63ba342022-10-02 11:21:08 -0500820 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, carrierId);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800821 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800822 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800823 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Sarah Chin807d5c62020-03-30 17:21:09 -0700824 intent.putExtra(CarrierConfigManager.EXTRA_REBROADCAST_ON_UNLOCK,
825 mFromSystemUnlocked[phoneId]);
rambowangd63ba342022-10-02 11:21:08 -0500826
827 TelephonyRegistryManager trm = mContext.getSystemService(TelephonyRegistryManager.class);
828 // Unlike broadcast, we wouldn't notify registrants on carrier config change when device is
829 // unlocked. Only real carrier config change will send the notification to registrants.
830 if (trm != null && !mFromSystemUnlocked[phoneId]) {
831 trm.notifyCarrierConfigChanged(phoneId, subId, carrierId, specificCarrierId);
832 }
833
Meng Wang97a6a462020-01-23 16:22:16 -0800834 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
rambowangd63ba342022-10-02 11:21:08 -0500835
Jack Yu00ece8c2022-11-19 22:29:12 -0800836 if (SubscriptionManager.isValidSubscriptionId(subId)) {
837 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId + ", subId=" + subId);
Jack Yue9e95be2020-03-22 10:56:06 -0700838 } else {
839 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
840 }
Junda Liu03aad762017-07-21 13:32:17 -0700841 mHasSentConfigChange[phoneId] = true;
Sarah Chin807d5c62020-03-30 17:21:09 -0700842 mFromSystemUnlocked[phoneId] = false;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800843 }
844
rambowangd63ba342022-10-02 11:21:08 -0500845 private int getSimApplicationStateForPhone(int phoneId) {
846 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
847 int subId = SubscriptionManager.getSubscriptionId(phoneId);
848 if (SubscriptionManager.isValidSubscriptionId(subId)) {
849 TelephonyManager telMgr = TelephonyManager.from(mContext)
850 .createForSubscriptionId(subId);
851 simApplicationState = telMgr.getSimApplicationState();
852 }
853 return simApplicationState;
854 }
855
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800856 /** Binds to the default or carrier config app. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000857 private boolean bindToConfigPackage(@NonNull String pkgName, int phoneId, int eventId) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800858 logdWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700859 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700860 carrierService.setPackage(pkgName);
Jayachandran C645ec612020-01-10 10:30:25 -0800861 CarrierServiceConnection serviceConnection = new CarrierServiceConnection(
862 phoneId, pkgName, eventId);
863 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
864 mServiceConnectionForNoSimConfig[phoneId] = serviceConnection;
865 } else {
866 mServiceConnection[phoneId] = serviceConnection;
867 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800868 try {
Jayachandran C645ec612020-01-10 10:30:25 -0800869 if (mContext.bindService(carrierService, serviceConnection,
Jordan Liu178430d2020-02-10 14:32:15 -0800870 Context.BIND_AUTO_CREATE)) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700871 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
872 mServiceBoundForNoSimConfig[phoneId] = true;
873 } else {
874 mServiceBound[phoneId] = true;
875 }
Jordan Liu178430d2020-02-10 14:32:15 -0800876 return true;
877 } else {
878 return false;
879 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800880 } catch (SecurityException ex) {
881 return false;
882 }
883 }
884
Rambo Wang7e3bc122021-03-23 09:24:38 -0700885 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000886 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -0700887 /* package */ CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800888 String mcc = "";
889 String mnc = "";
890 String imsi = "";
891 String gid1 = "";
892 String gid2 = "";
893 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
894 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800895 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800896 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700897 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
898 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800899 mcc = simOperator.substring(0, 3);
900 mnc = simOperator.substring(3);
901 }
902 Phone phone = PhoneFactory.getPhone(phoneId);
903 if (phone != null) {
904 imsi = phone.getSubscriberId();
905 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700906 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800907 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800908 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800909 }
chen xua31f22b2019-03-06 15:28:50 -0800910 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800911 }
912
Rambo Wangf7c214a2022-03-17 12:36:22 -0700913 /** Returns the package name of a privileged carrier app, or null if there is none. */
Nathan Harold1122e3d2021-01-22 15:45:24 -0800914 @Nullable
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700915 private String getCarrierPackageForPhoneId(int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -0700916 final long token = Binder.clearCallingIdentity();
917 try {
Rambo Wangf7c214a2022-03-17 12:36:22 -0700918 return TelephonyManager.from(mContext)
919 .getCarrierServicePackageNameForLogicalSlot(phoneId);
Nazanin1adf4562021-03-29 15:35:30 -0700920 } finally {
921 Binder.restoreCallingIdentity(token);
922 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700923 }
924
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000925 @Nullable
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700926 private String getIccIdForPhoneId(int phoneId) {
927 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
928 return null;
929 }
930 Phone phone = PhoneFactory.getPhone(phoneId);
931 if (phone == null) {
932 return null;
933 }
934 return phone.getIccSerialNumber();
935 }
936
chen xu02581692018-11-11 19:03:44 -0800937 /**
chen xua31f22b2019-03-06 15:28:50 -0800938 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800939 */
chen xua31f22b2019-03-06 15:28:50 -0800940 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700941 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800942 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700943 }
944 Phone phone = PhoneFactory.getPhone(phoneId);
945 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800946 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700947 }
chen xua31f22b2019-03-06 15:28:50 -0800948 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800949 }
950
951 /**
952 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
953 */
954 private int getCarrierIdForPhoneId(int phoneId) {
955 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
956 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700957 }
chen xu02581692018-11-11 19:03:44 -0800958 Phone phone = PhoneFactory.getPhone(phoneId);
959 if (phone == null) {
960 return TelephonyManager.UNKNOWN_CARRIER_ID;
961 }
962 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700963 }
964
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700965 /**
966 * Writes a bundle to an XML file.
967 *
chen xu02581692018-11-11 19:03:44 -0800968 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -0800969 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -0800970 * should have a single copy of XML file named after carrier id. However, it's still possible
971 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
972 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
973 * carrier while iccid remains the same.
974 *
975 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
976 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700977 *
978 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700979 *
Jayachandran C645ec612020-01-10 10:30:25 -0800980 * @param packageName the name of the package from which we fetched this bundle.
981 * @param extraString An extra string to be used in the XML file name.
982 * @param phoneId the phone ID.
983 * @param carrierId contains all carrier-identifying information.
984 * @param config the bundle to be written. Null will be treated as an empty bundle.
985 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700986 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000987 private void saveConfigToXml(@Nullable String packageName, @NonNull String extraString,
988 int phoneId, @Nullable CarrierIdentifier carrierId, @NonNull PersistableBundle config,
989 boolean isNoSimConfig) {
Jayachandran C645ec612020-01-10 10:30:25 -0800990 if (packageName == null) {
991 loge("Cannot save config with null packageName");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800992 return;
993 }
994
Jayachandran C645ec612020-01-10 10:30:25 -0800995 String fileName;
996 if (isNoSimConfig) {
997 fileName = getFilenameForNoSimConfig(packageName);
998 } else {
Jack Yuf5badd92022-12-08 00:50:53 -0800999 if (TelephonyManager.getSimStateForSlotIndex(phoneId)
Jayachandran C645ec612020-01-10 10:30:25 -08001000 != TelephonyManager.SIM_STATE_LOADED) {
1001 loge("Skip save config because SIM records are not loaded.");
1002 return;
1003 }
1004
1005 final String iccid = getIccIdForPhoneId(phoneId);
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001006 final int cid = carrierId != null ? carrierId.getSpecificCarrierId()
1007 : TelephonyManager.UNKNOWN_CARRIER_ID;
Jayachandran C645ec612020-01-10 10:30:25 -08001008 if (iccid == null) {
1009 loge("Cannot save config with null iccid.");
1010 return;
1011 }
1012 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001013 }
Jayachandran C645ec612020-01-10 10:30:25 -08001014
Junda Liu02596502016-11-15 13:46:43 -08001015 // b/32668103 Only save to file if config isn't empty.
1016 // In case of failure, not caching an empty bundle will
1017 // try loading config again on next power on or sim loaded.
1018 // Downside is for genuinely empty bundle, will bind and load
1019 // on every power on.
1020 if (config == null || config.isEmpty()) {
1021 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001022 }
1023
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001024 final String version = getPackageVersion(packageName);
1025 if (version == null) {
1026 loge("Failed to get package version for: " + packageName);
1027 return;
1028 }
1029
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001030 logdWithLocalLog(
1031 "Save config to xml, packagename: " + packageName + " phoneId: " + phoneId);
chen xudb04c292019-03-26 17:01:15 -07001032
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001033 FileOutputStream outFile = null;
1034 try {
Jayachandran C645ec612020-01-10 10:30:25 -08001035 outFile = new FileOutputStream(new File(mContext.getFilesDir(), fileName));
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001036 config.putString(KEY_VERSION, version);
1037 config.writeToStream(outFile);
1038 outFile.flush();
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001039 outFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001040 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001041 loge(e.toString());
1042 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001043 }
1044
Rambo Wang7e3bc122021-03-23 09:24:38 -07001045 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001046 /* package */ void saveConfigToXml(@Nullable String packageName, @NonNull String extraString,
1047 int phoneId, @NonNull CarrierIdentifier carrierId, @NonNull PersistableBundle config) {
Jayachandran C645ec612020-01-10 10:30:25 -08001048 saveConfigToXml(packageName, extraString, phoneId, carrierId, config, false);
1049 }
1050
Rambo Wang7e3bc122021-03-23 09:24:38 -07001051 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001052 /* package */ void saveNoSimConfigToXml(@Nullable String packageName,
1053 @NonNull PersistableBundle config) {
Jayachandran C645ec612020-01-10 10:30:25 -08001054 saveConfigToXml(packageName, "", -1, null, config, true);
1055 }
1056
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001057 /**
1058 * Reads a bundle from an XML file.
1059 *
1060 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +08001061 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001062 *
1063 * In case of errors, or if the saved config is from a different package version than the
1064 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001065 *
Jayachandran C645ec612020-01-10 10:30:25 -08001066 * @param packageName the name of the package from which we fetched this bundle.
1067 * @param extraString An extra string to be used in the XML file name.
1068 * @param phoneId the phone ID.
1069 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001070 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001071 * version does not match, or reading config fails.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001072 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001073 @Nullable
1074 private PersistableBundle restoreConfigFromXml(@Nullable String packageName,
1075 @NonNull String extraString, int phoneId, boolean isNoSimConfig) {
Jayachandran C645ec612020-01-10 10:30:25 -08001076 if (packageName == null) {
1077 loge("Cannot restore config with null packageName");
1078 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001079 final String version = getPackageVersion(packageName);
1080 if (version == null) {
1081 loge("Failed to get package version for: " + packageName);
1082 return null;
1083 }
Yuchen Dongc15afed2018-04-26 17:05:22 +08001084
Jayachandran C645ec612020-01-10 10:30:25 -08001085 String fileName;
arunvoddu664c36a2021-10-11 20:09:28 +00001086 String iccid = null;
Jayachandran C645ec612020-01-10 10:30:25 -08001087 if (isNoSimConfig) {
1088 fileName = getFilenameForNoSimConfig(packageName);
1089 } else {
Jack Yuf5badd92022-12-08 00:50:53 -08001090 if (TelephonyManager.getSimStateForSlotIndex(phoneId)
Jayachandran C645ec612020-01-10 10:30:25 -08001091 != TelephonyManager.SIM_STATE_LOADED) {
1092 loge("Skip restore config because SIM records are not loaded.");
1093 return null;
1094 }
1095
arunvoddu664c36a2021-10-11 20:09:28 +00001096 iccid = getIccIdForPhoneId(phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001097 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1098 if (iccid == null) {
1099 loge("Cannot restore config with null iccid.");
1100 return null;
1101 }
1102 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001103 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001104
1105 PersistableBundle restoredBundle = null;
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001106 File file = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001107 FileInputStream inFile = null;
1108 try {
Jayachandran C645ec612020-01-10 10:30:25 -08001109 file = new File(mContext.getFilesDir(),fileName);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001110 inFile = new FileInputStream(file);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001111
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001112 restoredBundle = PersistableBundle.readFromStream(inFile);
1113 String savedVersion = restoredBundle.getString(KEY_VERSION);
1114 restoredBundle.remove(KEY_VERSION);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001115
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001116 if (!version.equals(savedVersion)) {
1117 loge("Saved version mismatch: " + version + " vs " + savedVersion);
1118 restoredBundle = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001119 }
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001120
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001121 inFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001122 } catch (FileNotFoundException e) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001123 // Missing file is normal occurrence that might occur with a new sim or when restoring
1124 // an override file during boot and should not be treated as an error.
arunvoddu664c36a2021-10-11 20:09:28 +00001125 if (file != null) {
1126 if (isNoSimConfig) {
1127 logd("File not found: " + file.getPath());
1128 } else {
1129 String filePath = file.getPath();
1130 filePath = getFilePathForLogging(filePath, iccid);
1131 logd("File not found : " + filePath);
1132 }
1133 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001134 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001135 loge(e.toString());
1136 }
1137
1138 return restoredBundle;
1139 }
1140
arunvoddu664c36a2021-10-11 20:09:28 +00001141 /**
1142 * This method will mask most part of iccid in the filepath for logging on userbuild
1143 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001144 @NonNull
1145 private String getFilePathForLogging(@Nullable String filePath, @Nullable String iccid) {
arunvoddu664c36a2021-10-11 20:09:28 +00001146 // If loggable then return with actual file path
1147 if (Rlog.isLoggable(LOG_TAG, Log.VERBOSE)) {
1148 return filePath;
1149 }
1150 String path = filePath;
1151 int length = (iccid != null) ? iccid.length() : 0;
1152 if (length > 5 && filePath != null) {
1153 path = filePath.replace(iccid.substring(5), "***************");
1154 }
1155 return path;
1156 }
1157
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001158 @Nullable
1159 private PersistableBundle restoreConfigFromXml(@Nullable String packageName,
1160 @NonNull String extraString, int phoneId) {
Jayachandran C645ec612020-01-10 10:30:25 -08001161 return restoreConfigFromXml(packageName, extraString, phoneId, false);
1162 }
1163
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001164 @Nullable
1165 private PersistableBundle restoreNoSimConfigFromXml(@Nullable String packageName) {
Jayachandran C645ec612020-01-10 10:30:25 -08001166 return restoreConfigFromXml(packageName, "", -1, true);
1167 }
1168
Junda Liu8a8a53a2015-05-15 16:19:57 -07001169 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001170 * Clears cached carrier config.
1171 * This deletes all saved XML files associated with the given package name. If packageName is
1172 * null, then it deletes all saved XML files.
1173 *
1174 * @param packageName the name of a carrier package, or null if all cached config should be
1175 * cleared.
1176 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -07001177 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001178 private boolean clearCachedConfigForPackage(@Nullable final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001179 File dir = mContext.getFilesDir();
1180 File[] packageFiles = dir.listFiles(new FilenameFilter() {
1181 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001182 if (packageName != null) {
1183 return filename.startsWith("carrierconfig-" + packageName + "-");
1184 } else {
1185 return filename.startsWith("carrierconfig-");
1186 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001187 }
1188 });
Junda Liu8a8a53a2015-05-15 16:19:57 -07001189 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001190 for (File f : packageFiles) {
arunvoddub7c6e8e2022-09-05 15:48:06 +00001191 logd("Deleting " + getFilePathForLogging(f.getName()));
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001192 f.delete();
1193 }
Junda Liu8a8a53a2015-05-15 16:19:57 -07001194 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001195 }
1196
arunvoddub7c6e8e2022-09-05 15:48:06 +00001197 private String getFilePathForLogging(String filePath) {
1198 if (!TextUtils.isEmpty(filePath)) {
1199 String[] fileTokens = filePath.split("-");
1200 if (fileTokens != null && fileTokens.length > 2) {
1201 String iccid = fileTokens[fileTokens.length -2];
1202 return getFilePathForLogging(filePath, iccid);
1203 }
1204 return filePath;
1205 }
1206 return filePath;
1207 }
1208
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001209 /** Builds a canonical file name for a config file. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001210 @NonNull
Jayachandran C645ec612020-01-10 10:30:25 -08001211 private static String getFilenameForConfig(
1212 @NonNull String packageName, @NonNull String extraString,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001213 @NonNull String iccid, int cid) {
chen xu02581692018-11-11 19:03:44 -08001214 // the same carrier should have a single copy of XML file named after carrier id.
1215 // However, it's still possible that platform doesn't recognize the current sim carrier,
1216 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
1217 // cases SIM OTA resolves to different carrier while iccid remains the same.
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001218 return "carrierconfig-" + packageName + extraString + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001219 }
1220
Jayachandran C645ec612020-01-10 10:30:25 -08001221 /** Builds a canonical file name for no SIM config file. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001222 @NonNull
Jayachandran C645ec612020-01-10 10:30:25 -08001223 private String getFilenameForNoSimConfig(@NonNull String packageName) {
1224 return "carrierconfig-" + packageName + "-" + "nosim" + ".xml";
1225 }
1226
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001227 /** Return the current version code of a package, or null if the name is not found. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001228 @Nullable
1229 private String getPackageVersion(@NonNull String packageName) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001230 try {
1231 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -08001232 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001233 } catch (PackageManager.NameNotFoundException e) {
1234 return null;
1235 }
1236 }
1237
Jonathan Basseri65273c82017-07-25 15:08:42 -07001238 /**
1239 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001240 *
1241 * This reads config bundles for the given phoneId. That means getting the latest bundle from
1242 * the default app and a privileged carrier app, if present. This will not bind to an app if we
1243 * have a saved config file to use instead.
1244 */
1245 private void updateConfigForPhoneId(int phoneId) {
Jonathan Basseri65273c82017-07-25 15:08:42 -07001246 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001247 }
1248
Malcolm Chen5ea18532019-10-24 19:55:44 -07001249 private void onMultiSimConfigChanged() {
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001250 int oldNumPhones = mNumPhones;
1251 mNumPhones = TelephonyManager.from(mContext).getActiveModemCount();
1252 if (mNumPhones == oldNumPhones) {
1253 return;
1254 }
1255 logdWithLocalLog("mNumPhones change from " + oldNumPhones + " to " + mNumPhones);
1256
Rambo Wanga27fbe52022-04-12 19:09:07 +00001257 // If DS -> SS switch, release the resources BEFORE truncating the arrays to avoid leaking
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001258 for (int phoneId = mNumPhones; phoneId < oldNumPhones; phoneId++) {
1259 if (mServiceConnection[phoneId] != null) {
1260 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
1261 }
1262 if (mServiceConnectionForNoSimConfig[phoneId] != null) {
1263 unbindIfBoundForNoSimConfig(mContext, mServiceConnectionForNoSimConfig[phoneId],
1264 phoneId);
1265 }
1266 }
1267
Rambo Wanga27fbe52022-04-12 19:09:07 +00001268 // The phone to slot mapping may change, unregister here and re-register callbacks later
1269 for (int phoneId = 0; phoneId < oldNumPhones; phoneId++) {
1270 if (mCarrierServiceChangeCallbacks[phoneId] != null) {
1271 TelephonyManager.from(mContext).unregisterCarrierPrivilegesCallback(
1272 mCarrierServiceChangeCallbacks[phoneId]);
1273 }
1274 }
1275
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001276 // Copy the original arrays, truncate or padding with zeros (if necessary) to new length
1277 mConfigFromDefaultApp = Arrays.copyOf(mConfigFromDefaultApp, mNumPhones);
1278 mConfigFromCarrierApp = Arrays.copyOf(mConfigFromCarrierApp, mNumPhones);
1279 mPersistentOverrideConfigs = Arrays.copyOf(mPersistentOverrideConfigs, mNumPhones);
1280 mOverrideConfigs = Arrays.copyOf(mOverrideConfigs, mNumPhones);
1281 mServiceConnection = Arrays.copyOf(mServiceConnection, mNumPhones);
1282 mServiceConnectionForNoSimConfig =
1283 Arrays.copyOf(mServiceConnectionForNoSimConfig, mNumPhones);
1284 mServiceBound = Arrays.copyOf(mServiceBound, mNumPhones);
1285 mServiceBoundForNoSimConfig = Arrays.copyOf(mServiceBoundForNoSimConfig, mNumPhones);
1286 mHasSentConfigChange = Arrays.copyOf(mHasSentConfigChange, mNumPhones);
1287 mFromSystemUnlocked = Arrays.copyOf(mFromSystemUnlocked, mNumPhones);
Rambo Wanga27fbe52022-04-12 19:09:07 +00001288 mCarrierServiceChangeCallbacks = Arrays.copyOf(mCarrierServiceChangeCallbacks, mNumPhones);
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001289
Rambo Wanga27fbe52022-04-12 19:09:07 +00001290 // Load the config for all the phones and re-register callback AFTER padding the arrays.
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001291 for (int phoneId = 0; phoneId < mNumPhones; phoneId++) {
1292 updateConfigForPhoneId(phoneId);
Rambo Wanga27fbe52022-04-12 19:09:07 +00001293 mCarrierServiceChangeCallbacks[phoneId] = new CarrierServiceChangeCallback(phoneId);
1294 TelephonyManager.from(mContext).registerCarrierPrivilegesCallback(phoneId,
1295 new HandlerExecutor(mHandler), mCarrierServiceChangeCallbacks[phoneId]);
Malcolm Chen5ea18532019-10-24 19:55:44 -07001296 }
1297 }
1298
Hall Liu506724b2018-10-22 18:16:14 -07001299 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001300 @NonNull
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001301 public PersistableBundle getConfigForSubId(int subscriptionId, @NonNull String callingPackage) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001302 return getConfigForSubIdWithFeature(subscriptionId, callingPackage, null);
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08001303 }
1304
1305 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001306 @NonNull
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001307 public PersistableBundle getConfigForSubIdWithFeature(int subscriptionId,
1308 @NonNull String callingPackage, @Nullable String callingFeatureId) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001309 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subscriptionId,
1310 callingPackage, callingFeatureId, "getCarrierConfig")) {
Rambo Wang42026112021-04-07 20:57:28 +00001311 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -07001312 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -07001313
Rambo Wangd2b004b2021-03-30 10:10:23 -07001314 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001315 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001316 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001317 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001318 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001319 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001320 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001321 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001322 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001323 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001324 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001325 config = mPersistentOverrideConfigs[phoneId];
1326 if (config != null) {
1327 retConfig.putAll(config);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001328 }
Hall Liu506724b2018-10-22 18:16:14 -07001329 config = mOverrideConfigs[phoneId];
1330 if (config != null) {
1331 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -07001332 }
Nathan Harold1122e3d2021-01-22 15:45:24 -08001333 // Ignore the theoretical case of the default app not being present since that won't
1334 // work in CarrierConfigLoader today.
1335 final boolean allConfigsApplied =
1336 (mConfigFromCarrierApp[phoneId] != null
1337 || getCarrierPackageForPhoneId(phoneId) == null)
1338 && mConfigFromDefaultApp[phoneId] != null;
1339 retConfig.putBoolean(
1340 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, allConfigsApplied);
Jayachandran C645ec612020-01-10 10:30:25 -08001341 } else {
1342 if (mNoSimConfig != null) {
1343 retConfig.putAll(mNoSimConfig);
1344 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001345 }
1346 return retConfig;
1347 }
1348
1349 @Override
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001350 public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides,
1351 boolean persistent) {
Hall Liu506724b2018-10-22 18:16:14 -07001352 mContext.enforceCallingOrSelfPermission(
1353 android.Manifest.permission.MODIFY_PHONE_STATE, null);
1354 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1355 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001356 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
Rambo Wangd2b004b2021-03-30 10:10:23 -07001357 throw new IllegalArgumentException(
1358 "Invalid phoneId " + phoneId + " for subId " + subscriptionId);
Hall Liu506724b2018-10-22 18:16:14 -07001359 }
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001360 // Post to run on handler thread on which all states should be confined.
1361 mHandler.post(() -> {
1362 overrideConfig(mOverrideConfigs, phoneId, overrides);
Hall Liu506724b2018-10-22 18:16:14 -07001363
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001364 if (persistent) {
1365 overrideConfig(mPersistentOverrideConfigs, phoneId, overrides);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001366
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001367 if (overrides != null) {
1368 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
1369 saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION,
1370 phoneId,
1371 carrierId, mPersistentOverrideConfigs[phoneId]);
1372 } else {
1373 final String iccid = getIccIdForPhoneId(phoneId);
1374 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1375 String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage,
1376 OVERRIDE_PACKAGE_ADDITION, iccid, cid);
1377 File fileToDelete = new File(mContext.getFilesDir(), fileName);
1378 fileToDelete.delete();
1379 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001380 }
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001381 notifySubscriptionInfoUpdater(phoneId);
1382 });
Hall Liu506724b2018-10-22 18:16:14 -07001383 }
1384
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001385 private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId,
1386 @Nullable PersistableBundle overrides) {
1387 if (overrides == null) {
Rambo Wang42026112021-04-07 20:57:28 +00001388 currentOverrides[phoneId] = new PersistableBundle();
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001389 } else if (currentOverrides[phoneId] == null) {
1390 currentOverrides[phoneId] = overrides;
1391 } else {
1392 currentOverrides[phoneId].putAll(overrides);
1393 }
1394 }
1395
Hall Liu506724b2018-10-22 18:16:14 -07001396 @Override
Rambo Wangd2b004b2021-03-30 10:10:23 -07001397 public void notifyConfigChangedForSubId(int subscriptionId) {
Jordan Liud7d57fe2019-04-16 12:29:43 -07001398 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001399 // system privileged app with MODIFY_PHONE_STATE permission.
Rambo Wangd2b004b2021-03-30 10:10:23 -07001400 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext,
1401 subscriptionId, "Require carrier privileges or MODIFY_PHONE_STATE permission.");
1402
1403 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1404 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1405 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
1406 throw new IllegalArgumentException(
1407 "Invalid phoneId " + phoneId + " for subId " + subscriptionId);
1408 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001409
1410 // This method should block until deleting has completed, so that an error which prevents us
1411 // from clearing the cache is passed back to the carrier app. With the files successfully
1412 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001413 String callingPackageName = mContext.getPackageManager().getNameForUid(
1414 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001415 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001416 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001417 }
1418
1419 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001420 public void updateConfigForPhoneId(int phoneId, @NonNull String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -07001421 mContext.enforceCallingOrSelfPermission(
1422 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001423 logdWithLocalLog("Update config for phoneId: " + phoneId + " simState: " + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001424 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001425 throw new IllegalArgumentException("Invalid phoneId: " + phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001426 }
1427 // requires Java 7 for switch on string.
1428 switch (simState) {
1429 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1430 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001431 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001432 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Malcolm Chendb66de12019-12-09 18:57:07 -08001433 case IccCardConstants.INTENT_VALUE_ICC_NOT_READY:
Junda Liu9f2d2712015-05-15 14:22:45 -07001434 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001435 break;
1436 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1437 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001438 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001439 break;
1440 }
1441 }
1442
Junda Liu43d723a2015-05-12 17:23:45 -07001443 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001444 @NonNull
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001445 public String getDefaultCarrierServicePackageName() {
Shuo Qianefdb7962019-12-19 15:54:27 -08001446 mContext.enforceCallingOrSelfPermission(
1447 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1448 "getDefaultCarrierServicePackageName");
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001449 return mPlatformCarrierConfigPackage;
1450 }
1451
Rambo Wang7e3bc122021-03-23 09:24:38 -07001452 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001453 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -07001454 /* package */ Handler getHandler() {
1455 return mHandler;
1456 }
1457
1458 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001459 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001460 /* package */ PersistableBundle getConfigFromDefaultApp(int phoneId) {
1461 return mConfigFromDefaultApp[phoneId];
1462 }
1463
1464 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001465 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001466 /* package */ PersistableBundle getConfigFromCarrierApp(int phoneId) {
1467 return mConfigFromCarrierApp[phoneId];
1468 }
1469
1470 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001471 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -07001472 /* package */ PersistableBundle getNoSimConfig() {
1473 return mNoSimConfig;
1474 }
1475
1476 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001477 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001478 /* package */ PersistableBundle getOverrideConfig(int phoneId) {
1479 return mOverrideConfigs[phoneId];
1480 }
1481
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001482 // TODO(b/185129900): always call unbindService after bind, no matter if it succeeded
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001483 private void unbindIfBound(@NonNull Context context, @NonNull CarrierServiceConnection conn,
Rambo Wangb0f3e2f2021-10-28 12:40:12 -07001484 int phoneId) {
1485 if (mServiceBound[phoneId]) {
1486 mServiceBound[phoneId] = false;
1487 context.unbindService(conn);
1488 }
1489 }
1490
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001491 private void unbindIfBoundForNoSimConfig(@NonNull Context context,
1492 @NonNull CarrierServiceConnection conn, int phoneId) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -07001493 if (mServiceBoundForNoSimConfig[phoneId]) {
1494 mServiceBoundForNoSimConfig[phoneId] = false;
Jayachandran C645ec612020-01-10 10:30:25 -08001495 context.unbindService(conn);
1496 }
1497 }
1498
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001499 /**
Rambo Wang610fdf62021-03-08 21:37:11 -08001500 * Returns a boxed Integer object for phoneId, services as message token to distinguish messages
1501 * with same code when calling {@link Handler#removeMessages(int, Object)}.
1502 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001503 @NonNull
Rambo Wang610fdf62021-03-08 21:37:11 -08001504 private Integer getMessageToken(int phoneId) {
1505 if (phoneId < -128 || phoneId > 127) {
1506 throw new IllegalArgumentException("phoneId should be in range [-128, 127], inclusive");
1507 }
1508 // Integer#valueOf guarantees the integers within [-128, 127] are cached and thus memory
1509 // comparison (==) returns true for the same integer.
1510 return Integer.valueOf(phoneId);
1511 }
1512
1513 /**
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001514 * If {@code args} contains {@link #DUMP_ARG_REQUESTING_PACKAGE} and a following package name,
1515 * we'll also call {@link IBinder#dump} on the default carrier service (if bound) and the
1516 * specified carrier service (if bound). Typically, this is done for connectivity bug reports
1517 * where we don't call {@code dumpsys activity service all-non-platform} because that contains
1518 * too much info, but we still want to let carrier apps include their diagnostics.
1519 */
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001520 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001521 public void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter pw, @NonNull String[] args) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001522 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
Junda Liu43d723a2015-05-12 17:23:45 -07001523 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1524 != PackageManager.PERMISSION_GRANTED) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001525 indentPW.println("Permission Denial: can't dump carrierconfig from from pid="
Junda Liu43d723a2015-05-12 17:23:45 -07001526 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1527 return;
1528 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001529 String requestingPackage = null;
1530 int requestingPackageIndex = ArrayUtils.indexOf(args, DUMP_ARG_REQUESTING_PACKAGE);
1531 if (requestingPackageIndex >= 0 && requestingPackageIndex < args.length - 1
1532 && !TextUtils.isEmpty(args[requestingPackageIndex + 1])) {
1533 requestingPackage = args[requestingPackageIndex + 1];
1534 // Throws a SecurityException if the caller is impersonating another app in an effort to
1535 // dump extra info (which may contain PII the caller doesn't have a right to).
1536 enforceCallerIsSystemOrRequestingPackage(requestingPackage);
1537 }
1538
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001539 indentPW.println("CarrierConfigLoader: " + this);
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001540 for (int i = 0; i < mNumPhones; i++) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001541 indentPW.println("Phone Id = " + i);
shuoq26a3a4c2016-12-16 11:06:48 -08001542 // display default values in CarrierConfigManager
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001543 printConfig(CarrierConfigManager.getDefaultConfig(), indentPW,
shuoq26a3a4c2016-12-16 11:06:48 -08001544 "Default Values from CarrierConfigManager");
shuoq26a3a4c2016-12-16 11:06:48 -08001545 // display ConfigFromDefaultApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001546 printConfig(mConfigFromDefaultApp[i], indentPW, "mConfigFromDefaultApp");
shuoq26a3a4c2016-12-16 11:06:48 -08001547 // display ConfigFromCarrierApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001548 printConfig(mConfigFromCarrierApp[i], indentPW, "mConfigFromCarrierApp");
1549 printConfig(mPersistentOverrideConfigs[i], indentPW, "mPersistentOverrideConfigs");
1550 printConfig(mOverrideConfigs[i], indentPW, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001551 }
chen xudb04c292019-03-26 17:01:15 -07001552
Jayachandran C645ec612020-01-10 10:30:25 -08001553 printConfig(mNoSimConfig, indentPW, "mNoSimConfig");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001554 indentPW.println("CarrierConfigLoadingLog=");
1555 mCarrierConfigLoadingLog.dump(fd, indentPW, args);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001556
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001557 if (requestingPackage != null) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001558 logd("Including default and requesting package " + requestingPackage
1559 + " carrier services in dump");
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001560 indentPW.println("");
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001561 indentPW.println("Connected services");
1562 dumpCarrierServiceIfBound(fd, indentPW, "Default config package",
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001563 mPlatformCarrierConfigPackage, false /* considerCarrierPrivileges */);
1564 dumpCarrierServiceIfBound(fd, indentPW, "Requesting package", requestingPackage,
1565 true /* considerCarrierPrivileges */);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001566 }
shuoq26a3a4c2016-12-16 11:06:48 -08001567 }
1568
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001569 private void printConfig(@NonNull PersistableBundle configApp,
1570 @NonNull IndentingPrintWriter indentPW, @NonNull String name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001571 indentPW.increaseIndent();
shuoq26a3a4c2016-12-16 11:06:48 -08001572 if (configApp == null) {
shuoq26a3a4c2016-12-16 11:06:48 -08001573 indentPW.println(name + " : null ");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001574 indentPW.decreaseIndent();
1575 indentPW.println("");
shuoq26a3a4c2016-12-16 11:06:48 -08001576 return;
1577 }
shuoq26a3a4c2016-12-16 11:06:48 -08001578 indentPW.println(name + " : ");
1579 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1580 Collections.sort(sortedKeys);
1581 indentPW.increaseIndent();
1582 indentPW.increaseIndent();
1583 for (String key : sortedKeys) {
1584 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1585 indentPW.println(key + " = " +
1586 Arrays.toString((Object[]) configApp.get(key)));
1587 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1588 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1589 } else {
1590 indentPW.println(key + " = " + configApp.get(key));
1591 }
Junda Liu43d723a2015-05-12 17:23:45 -07001592 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001593 indentPW.decreaseIndent();
1594 indentPW.decreaseIndent();
1595 indentPW.decreaseIndent();
1596 indentPW.println("");
Junda Liu43d723a2015-05-12 17:23:45 -07001597 }
1598
Hunter Knepshielde601f432020-02-19 10:16:04 -08001599 /**
1600 * Passes without problem when one of these conditions is true:
1601 * - The caller is a privileged UID (e.g. for dumpstate.cpp generating a bug report, where the
1602 * system knows the true caller plumbed in through the {@link android.os.BugreportManager} API).
1603 * - The caller's UID matches the supplied package.
1604 *
1605 * @throws SecurityException if none of the above conditions are met.
1606 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001607 private void enforceCallerIsSystemOrRequestingPackage(@NonNull String requestingPackage)
Hunter Knepshielde601f432020-02-19 10:16:04 -08001608 throws SecurityException {
1609 final int callingUid = Binder.getCallingUid();
1610 if (callingUid == Process.ROOT_UID || callingUid == Process.SYSTEM_UID
1611 || callingUid == Process.SHELL_UID || callingUid == Process.PHONE_UID) {
1612 // Bug reports (dumpstate.cpp) run as SHELL, and let some other privileged UIDs through
1613 // as well.
1614 return;
1615 }
1616 // An app is trying to dump extra detail, block it if they aren't who they claim to be.
1617 AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class);
1618 if (appOps == null) {
1619 throw new SecurityException("No AppOps");
1620 }
1621 // Will throw a SecurityException if the UID and package don't match.
1622 appOps.checkPackage(callingUid, requestingPackage);
1623 }
1624
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001625 /**
1626 * Searches for one or more appropriate {@link CarrierService} instances to dump based on the
1627 * current connections.
1628 *
1629 * @param targetPkgName the target package name to dump carrier services for
1630 * @param considerCarrierPrivileges if true, allow a carrier service to be dumped if it shares
1631 * carrier privileges with {@code targetPkgName};
1632 * otherwise, only dump a carrier service if it is {@code
1633 * targetPkgName}
1634 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001635 private void dumpCarrierServiceIfBound(@NonNull FileDescriptor fd,
1636 @NonNull IndentingPrintWriter indentPW, @NonNull String prefix,
1637 @NonNull String targetPkgName, boolean considerCarrierPrivileges) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001638 // Null package is possible if it's early in the boot process, there was a recent crash, we
1639 // loaded the config from XML most recently, or a SIM slot is empty. Carrier apps with
1640 // long-lived bindings should typically get dumped here regardless. Even if an app is being
1641 // used for multiple phoneIds, we assume that it's smart enough to handle that on its own,
1642 // and that in most cases we'd just be dumping duplicate information and bloating a report.
1643 indentPW.increaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001644 indentPW.println(prefix + " : " + targetPkgName);
1645 Set<String> dumpedPkgNames = new ArraySet<>(mServiceConnection.length);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001646 for (CarrierServiceConnection connection : mServiceConnection) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001647 if (connection == null || !SubscriptionManager.isValidPhoneId(connection.phoneId)
1648 || TextUtils.isEmpty(connection.pkgName)) {
1649 continue;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001650 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001651 final String servicePkgName = connection.pkgName;
1652 // Note: we intentionally ignore system components here because we should NOT match the
1653 // shell caller that's typically used for bug reports via non-BugreportManager triggers.
1654 final boolean exactPackageMatch = TextUtils.equals(targetPkgName, servicePkgName);
1655 final boolean carrierPrivilegesMatch =
1656 considerCarrierPrivileges && hasCarrierPrivileges(targetPkgName,
1657 connection.phoneId);
1658 if (!exactPackageMatch && !carrierPrivilegesMatch) continue;
1659 // Make sure this service is actually alive before trying to dump it. We don't pay
1660 // attention to mServiceBound[connection.phoneId] because typically carrier apps will
1661 // request long-lived bindings, and even if we unbind the app, it may still be alive due
1662 // to CarrierServiceBindHelper. Pull it out as a reference so even if it gets set to
1663 // null within the ServiceConnection during unbinding we can avoid an NPE.
1664 final IBinder service = connection.service;
1665 if (service == null || !service.isBinderAlive() || !service.pingBinder()) continue;
1666 // We've got a live service. Last check is just to make sure we don't dump a package
1667 // multiple times.
1668 if (!dumpedPkgNames.add(servicePkgName)) continue;
1669 if (!exactPackageMatch) {
1670 logd(targetPkgName + " has carrier privileges on phoneId " + connection.phoneId
1671 + ", service provided by " + servicePkgName);
1672 indentPW.increaseIndent();
1673 indentPW.println("Proxy : " + servicePkgName);
1674 indentPW.decreaseIndent();
1675 }
1676 // Flush before we let the app output anything to ensure correct ordering of output.
1677 // Internally, Binder#dump calls flush on its printer after finishing so we don't
1678 // need to do anything after.
1679 indentPW.flush();
1680 try {
1681 logd("Dumping " + servicePkgName);
1682 // We don't need to give the carrier service any args.
1683 connection.service.dump(fd, null /* args */);
1684 logd("Done with " + servicePkgName);
1685 } catch (RemoteException e) {
1686 logd("RemoteException from " + servicePkgName, e);
1687 indentPW.increaseIndent();
1688 indentPW.println("RemoteException");
1689 indentPW.increaseIndent();
1690 e.printStackTrace(indentPW);
1691 indentPW.decreaseIndent();
1692 indentPW.decreaseIndent();
1693 // We won't retry this package again because now it's in dumpedPkgNames.
1694 }
1695 indentPW.println("");
Jordan Liu46ed5db2020-01-28 08:55:20 -08001696 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001697 if (dumpedPkgNames.isEmpty()) {
1698 indentPW.increaseIndent();
1699 indentPW.println("Not bound");
1700 indentPW.decreaseIndent();
1701 indentPW.println("");
1702 }
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001703 indentPW.decreaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001704 }
1705
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001706 private boolean hasCarrierPrivileges(@NonNull String pkgName, int phoneId) {
Jack Yu00ece8c2022-11-19 22:29:12 -08001707 int subId = SubscriptionManager.getSubscriptionId(phoneId);
1708 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001709 return false;
1710 }
Jack Yu00ece8c2022-11-19 22:29:12 -08001711 return TelephonyManager.from(mContext).createForSubscriptionId(subId)
1712 .checkCarrierPrivilegesForPackage(pkgName)
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001713 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001714 }
1715
Zach Johnson36d7aab2015-05-22 15:43:00 -07001716 private class CarrierServiceConnection implements ServiceConnection {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001717 final int phoneId;
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001718 @NonNull final String pkgName;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001719 final int eventId;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001720 IBinder service;
1721
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001722 CarrierServiceConnection(int phoneId, @NonNull String pkgName, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001723 this.phoneId = phoneId;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001724 this.pkgName = pkgName;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001725 this.eventId = eventId;
1726 }
1727
1728 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001729 public void onServiceConnected(@NonNull ComponentName name, @NonNull IBinder service) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001730 logd("Connected to config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001731 this.service = service;
1732 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1733 }
1734
1735 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001736 public void onServiceDisconnected(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001737 logd("Disconnected from config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001738 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001739 }
1740
1741 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001742 public void onBindingDied(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001743 logd("Binding died from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001744 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001745 }
1746
1747 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001748 public void onNullBinding(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001749 logd("Null binding from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001750 this.service = null;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001751 }
1752 }
1753
1754 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1755 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001756 public void onReceive(@NonNull Context context, @NonNull Intent intent) {
Rambo Wanga27fbe52022-04-12 19:09:07 +00001757 switch (intent.getAction()) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001758 case Intent.ACTION_BOOT_COMPLETED:
1759 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1760 break;
1761
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001762 case TelephonyManager.ACTION_MULTI_SIM_CONFIG_CHANGED:
1763 mHandler.sendEmptyMessage(EVENT_MULTI_SIM_CONFIG_CHANGED);
1764 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001765 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001766 }
1767 }
1768
Rambo Wanga27fbe52022-04-12 19:09:07 +00001769 private class CarrierServiceChangeCallback implements
1770 TelephonyManager.CarrierPrivilegesCallback {
1771 final int mPhoneId;
1772 // CarrierPrivilegesCallback will be triggered upon registration. Filter the first callback
1773 // here since we really care of the *change* of carrier service instead of the content
1774 private boolean mHasSentServiceChangeCallback;
1775
1776 CarrierServiceChangeCallback(int phoneId) {
1777 this.mPhoneId = phoneId;
1778 this.mHasSentServiceChangeCallback = false;
1779 }
1780
1781 @Override
1782 public void onCarrierPrivilegesChanged(
1783 @androidx.annotation.NonNull Set<String> privilegedPackageNames,
1784 @androidx.annotation.NonNull Set<Integer> privilegedUids) {
1785 // Ignored, not interested here
1786 }
1787
1788 @Override
1789 public void onCarrierServiceChanged(
1790 @androidx.annotation.Nullable String carrierServicePackageName,
1791 int carrierServiceUid) {
1792 // Ignore the first callback which is triggered upon registration
1793 if (!mHasSentServiceChangeCallback) {
1794 mHasSentServiceChangeCallback = true;
1795 return;
1796 }
1797 mHandler.sendMessage(
1798 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, mPhoneId, -1,
1799 carrierServicePackageName));
1800 }
1801 }
1802
Rambo Wangab13e3e2021-04-08 15:01:06 -07001803 // Get readable string for the message code supported in this class.
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001804 @NonNull
Rambo Wangab13e3e2021-04-08 15:01:06 -07001805 private static String eventToString(int code) {
1806 switch (code) {
1807 case EVENT_CLEAR_CONFIG:
1808 return "EVENT_CLEAR_CONFIG";
1809 case EVENT_CONNECTED_TO_DEFAULT:
1810 return "EVENT_CONNECTED_TO_DEFAULT";
1811 case EVENT_CONNECTED_TO_CARRIER:
1812 return "EVENT_CONNECTED_TO_CARRIER";
1813 case EVENT_FETCH_DEFAULT_DONE:
1814 return "EVENT_FETCH_DEFAULT_DONE";
1815 case EVENT_FETCH_CARRIER_DONE:
1816 return "EVENT_FETCH_CARRIER_DONE";
1817 case EVENT_DO_FETCH_DEFAULT:
1818 return "EVENT_DO_FETCH_DEFAULT";
1819 case EVENT_DO_FETCH_CARRIER:
1820 return "EVENT_DO_FETCH_CARRIER";
1821 case EVENT_PACKAGE_CHANGED:
1822 return "EVENT_PACKAGE_CHANGED";
1823 case EVENT_BIND_DEFAULT_TIMEOUT:
1824 return "EVENT_BIND_DEFAULT_TIMEOUT";
1825 case EVENT_BIND_CARRIER_TIMEOUT:
1826 return "EVENT_BIND_CARRIER_TIMEOUT";
1827 case EVENT_CHECK_SYSTEM_UPDATE:
1828 return "EVENT_CHECK_SYSTEM_UPDATE";
1829 case EVENT_SYSTEM_UNLOCKED:
1830 return "EVENT_SYSTEM_UNLOCKED";
1831 case EVENT_FETCH_DEFAULT_TIMEOUT:
1832 return "EVENT_FETCH_DEFAULT_TIMEOUT";
1833 case EVENT_FETCH_CARRIER_TIMEOUT:
1834 return "EVENT_FETCH_CARRIER_TIMEOUT";
1835 case EVENT_SUBSCRIPTION_INFO_UPDATED:
1836 return "EVENT_SUBSCRIPTION_INFO_UPDATED";
1837 case EVENT_MULTI_SIM_CONFIG_CHANGED:
1838 return "EVENT_MULTI_SIM_CONFIG_CHANGED";
1839 case EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG:
1840 return "EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG";
1841 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE:
1842 return "EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE";
1843 case EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG:
1844 return "EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG";
1845 case EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
1846 return "EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT";
1847 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
1848 return "EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT";
1849 default:
1850 return "UNKNOWN(" + code + ")";
1851 }
1852 }
1853
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001854 private void logd(@NonNull String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001855 Log.d(LOG_TAG, msg);
1856 }
1857
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001858 private void logd(@NonNull String msg, Throwable tr) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001859 Log.d(LOG_TAG, msg, tr);
1860 }
1861
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001862 private void logdWithLocalLog(@NonNull String msg) {
chen xudb04c292019-03-26 17:01:15 -07001863 Log.d(LOG_TAG, msg);
1864 mCarrierConfigLoadingLog.log(msg);
1865 }
1866
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001867 private void loge(@NonNull String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001868 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07001869 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001870 }
1871}