blob: 307170a48f66058324bfd3eb4d97f5068764fc9c [file] [log] [blame]
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001/*
Jonathan Basseri6465afd2015-02-25 13:05:57 -08002 * Copyright (c) 2015, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jonathan Basseri6465afd2015-02-25 13:05:57 -080017package com.android.phone;
18
Jonathan Basseri65273c82017-07-25 15:08:42 -070019import static android.service.carrier.CarrierService.ICarrierServiceWrapper.KEY_CONFIG_BUNDLE;
20import static android.service.carrier.CarrierService.ICarrierServiceWrapper.RESULT_ERROR;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080021
Jonathan Basseri11f89572015-05-05 11:57:39 -070022import android.annotation.NonNull;
Torbjorn Eklund723527a2019-02-13 11:16:25 +010023import android.annotation.Nullable;
Hunter Knepshielde601f432020-02-19 10:16:04 -080024import android.app.AppOpsManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080025import android.content.BroadcastReceiver;
26import android.content.ComponentName;
27import android.content.Context;
28import android.content.Intent;
29import android.content.IntentFilter;
30import android.content.ServiceConnection;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070031import android.content.SharedPreferences;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070032import android.content.pm.PackageInfo;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070033import android.content.pm.PackageManager;
Junda Liu43d723a2015-05-12 17:23:45 -070034import android.os.Binder;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070035import android.os.Build;
Jonathan Basseri65273c82017-07-25 15:08:42 -070036import android.os.Bundle;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080037import android.os.Handler;
Rambo Wanga27fbe52022-04-12 19:09:07 +000038import android.os.HandlerExecutor;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080039import android.os.IBinder;
Rambo Wang7e3bc122021-03-23 09:24:38 -070040import android.os.Looper;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080041import android.os.Message;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070042import android.os.PersistableBundle;
Hunter Knepshielde601f432020-02-19 10:16:04 -080043import android.os.Process;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080044import android.os.RemoteException;
Jonathan Basseri65273c82017-07-25 15:08:42 -070045import android.os.ResultReceiver;
Meng Wang97a6a462020-01-23 16:22:16 -080046import android.os.UserHandle;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070047import android.preference.PreferenceManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080048import android.service.carrier.CarrierIdentifier;
Zach Johnson36d7aab2015-05-22 15:43:00 -070049import android.service.carrier.CarrierService;
50import android.service.carrier.ICarrierService;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080051import android.telephony.CarrierConfigManager;
52import android.telephony.SubscriptionManager;
Peter Wangc035ce42020-01-08 21:00:22 -080053import android.telephony.TelephonyFrameworkInitializer;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080054import android.telephony.TelephonyManager;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -080055import android.text.TextUtils;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080056import android.util.ArraySet;
chen xudb04c292019-03-26 17:01:15 -070057import android.util.LocalLog;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080058import android.util.Log;
59
Rambo Wang7e3bc122021-03-23 09:24:38 -070060import com.android.internal.annotations.VisibleForTesting;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080061import com.android.internal.telephony.ICarrierConfigLoader;
62import com.android.internal.telephony.IccCardConstants;
63import com.android.internal.telephony.Phone;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080064import com.android.internal.telephony.PhoneFactory;
Nathan Harold48ac0972019-03-13 22:33:01 -070065import com.android.internal.telephony.SubscriptionInfoUpdater;
Jeff Davidsona8e4e242018-03-15 17:16:18 -070066import com.android.internal.telephony.TelephonyPermissions;
Meng Wanga320b942019-11-25 11:21:26 -080067import com.android.internal.telephony.util.ArrayUtils;
shuoq26a3a4c2016-12-16 11:06:48 -080068import com.android.internal.util.IndentingPrintWriter;
arunvoddu664c36a2021-10-11 20:09:28 +000069import com.android.telephony.Rlog;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080070
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070071import java.io.File;
Junda Liu43d723a2015-05-12 17:23:45 -070072import java.io.FileDescriptor;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070073import java.io.FileInputStream;
74import java.io.FileNotFoundException;
75import java.io.FileOutputStream;
Jonathan Basseri1f743c92015-05-15 00:19:46 -070076import java.io.FilenameFilter;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070077import java.io.IOException;
Junda Liu43d723a2015-05-12 17:23:45 -070078import java.io.PrintWriter;
shuoq26a3a4c2016-12-16 11:06:48 -080079import java.util.ArrayList;
80import java.util.Arrays;
81import java.util.Collections;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080082import java.util.List;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080083import java.util.Set;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080084
85/**
86 * CarrierConfigLoader binds to privileged carrier apps to fetch carrier config overlays.
Jonathan Basseri6465afd2015-02-25 13:05:57 -080087 */
Jonathan Basseri6465afd2015-02-25 13:05:57 -080088public class CarrierConfigLoader extends ICarrierConfigLoader.Stub {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070089 private static final String LOG_TAG = "CarrierConfigLoader";
Meng Wang33ad2bc2017-03-16 20:21:20 -070090
91 // Package name for platform carrier config app, bundled with system image.
Rambo Wangfe0d7c12022-04-15 03:00:32 +000092 @NonNull private final String mPlatformCarrierConfigPackage;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080093
94 /** The singleton instance. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +000095 @Nullable private static CarrierConfigLoader sInstance;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080096 // The context for phone app, passed from PhoneGlobals.
Rambo Wangfe0d7c12022-04-15 03:00:32 +000097 @NonNull private Context mContext;
98
99 // All the states below (array indexed by phoneId) are non-null. But the member of the array
100 // is nullable, when e.g. the config for the phone is not loaded yet
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800101 // Carrier configs from default app, indexed by phoneID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000102 @NonNull private PersistableBundle[] mConfigFromDefaultApp;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800103 // Carrier configs from privileged carrier config app, indexed by phoneID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000104 @NonNull private PersistableBundle[] mConfigFromCarrierApp;
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100105 // Persistent Carrier configs that are provided via the override test API, indexed by phone ID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000106 @NonNull private PersistableBundle[] mPersistentOverrideConfigs;
Hall Liu506724b2018-10-22 18:16:14 -0700107 // Carrier configs that are provided via the override test API, indexed by phone ID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000108 @NonNull private PersistableBundle[] mOverrideConfigs;
Jayachandran C645ec612020-01-10 10:30:25 -0800109 // Carrier configs to override code default when there is no SIM inserted
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000110 @NonNull private PersistableBundle mNoSimConfig;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800111 // Service connection for binding to config app.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000112 @NonNull private CarrierServiceConnection[] mServiceConnection;
Jayachandran C645ec612020-01-10 10:30:25 -0800113 // Service connection for binding to carrier config app for no SIM config.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000114 @NonNull private CarrierServiceConnection[] mServiceConnectionForNoSimConfig;
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700115 // Whether we are bound to a service for each phone
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000116 @NonNull private boolean[] mServiceBound;
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700117 // Whether we are bound to a service for no SIM config
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000118 @NonNull private boolean[] mServiceBoundForNoSimConfig;
Sarah Chin807d5c62020-03-30 17:21:09 -0700119 // Whether we have sent config change broadcast for each phone id.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000120 @NonNull private boolean[] mHasSentConfigChange;
Sarah Chin807d5c62020-03-30 17:21:09 -0700121 // Whether the broadcast was sent from EVENT_SYSTEM_UNLOCKED, to track rebroadcasts
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000122 @NonNull private boolean[] mFromSystemUnlocked;
Rambo Wanga27fbe52022-04-12 19:09:07 +0000123 // CarrierService change monitoring
124 @NonNull private CarrierServiceChangeCallback[] mCarrierServiceChangeCallbacks;
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000125
Nathan Harold48ac0972019-03-13 22:33:01 -0700126 // SubscriptionInfoUpdater
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000127 @NonNull private final SubscriptionInfoUpdater mSubscriptionInfoUpdater;
Rambo Wanga27fbe52022-04-12 19:09:07 +0000128 // Broadcast receiver for system events (BootCompleted, MultiSimConfigChanged etc.)
129 @NonNull
130 private final BroadcastReceiver mSystemBroadcastReceiver = new ConfigLoaderBroadcastReceiver();
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000131 @NonNull private final LocalLog mCarrierConfigLoadingLog = new LocalLog(100);
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000132 // Number of phone instances (active modem count)
133 private int mNumPhones;
chen xudb04c292019-03-26 17:01:15 -0700134
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800135
136 // Message codes; see mHandler below.
137 // Request from SubscriptionInfoUpdater when SIM becomes absent or error.
138 private static final int EVENT_CLEAR_CONFIG = 0;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800139 // Has connected to default app.
140 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
141 // Has connected to carrier app.
142 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700143 // Config has been loaded from default app (or cache).
144 private static final int EVENT_FETCH_DEFAULT_DONE = 5;
145 // Config has been loaded from carrier app (or cache).
146 private static final int EVENT_FETCH_CARRIER_DONE = 6;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700147 // Attempt to fetch from default app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700148 private static final int EVENT_DO_FETCH_DEFAULT = 7;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700149 // Attempt to fetch from carrier app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700150 private static final int EVENT_DO_FETCH_CARRIER = 8;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700151 // A package has been installed, uninstalled, or updated.
152 private static final int EVENT_PACKAGE_CHANGED = 9;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700153 // Bind timed out for the default app.
154 private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
155 // Bind timed out for a carrier app.
156 private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700157 // Check if the system fingerprint has changed.
158 private static final int EVENT_CHECK_SYSTEM_UPDATE = 12;
Nanxi Chen3d670502016-03-17 16:32:09 -0700159 // Rerun carrier config binding after system is unlocked.
160 private static final int EVENT_SYSTEM_UNLOCKED = 13;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700161 // Fetching config timed out from the default app.
162 private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14;
163 // Fetching config timed out from a carrier app.
164 private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15;
Nathan Harold48ac0972019-03-13 22:33:01 -0700165 // SubscriptionInfoUpdater has finished updating the sub for the carrier config.
166 private static final int EVENT_SUBSCRIPTION_INFO_UPDATED = 16;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700167 // Multi-SIM config changed.
168 private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 17;
Jayachandran C645ec612020-01-10 10:30:25 -0800169 // Attempt to fetch from default app or read from XML for no SIM case.
170 private static final int EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG = 18;
171 // No SIM config has been loaded from default app (or cache).
172 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE = 19;
173 // Has connected to default app for no SIM config.
174 private static final int EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG = 20;
175 // Bind timed out for the default app when trying to fetch no SIM config.
176 private static final int EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 21;
177 // Fetching config timed out from the default app for no SIM config.
178 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 22;
Rambo Wangab13e3e2021-04-08 15:01:06 -0700179 // NOTE: any new EVENT_* values must be added to method eventToString().
Jonathan Basseri930701e2015-06-11 20:56:43 -0700180
Junda Liu6cb45002016-03-22 17:18:20 -0700181 private static final int BIND_TIMEOUT_MILLIS = 30000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800182
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800183 // Keys used for saving and restoring config bundle from file.
184 private static final String KEY_VERSION = "__carrier_config_package_version__";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800185
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100186 private static final String OVERRIDE_PACKAGE_ADDITION = "-override";
187
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700188 // SharedPreferences key for last known build fingerprint.
189 private static final String KEY_FINGERPRINT = "build_fingerprint";
190
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800191 // Argument for #dump that indicates we should also call the default and specified carrier
192 // service's #dump method. In multi-SIM devices, it's possible that carrier A is on SIM 1 and
193 // carrier B is on SIM 2, in which case we should not dump carrier B's service when carrier A
194 // requested the dump.
195 private static final String DUMP_ARG_REQUESTING_PACKAGE = "--requesting-package";
196
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800197 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700198 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700199 // For each phoneId, the event sequence should be:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700200 // fetch default, connected to default, fetch default (async), fetch default done,
201 // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700202 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700203 // If there is a saved config file for either the default app or the carrier app, we skip
204 // binding to the app and go straight from fetch to loaded.
205 //
206 // At any time, at most one connection is active. If events are not in this order, previous
207 // connection will be unbound, so only latest event takes effect.
208 //
209 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
210 // 1. loading from carrier app (even if read from a file)
211 // 2. loading from default app if there is no carrier app (even if read from a file)
212 // 3. clearing config (e.g. due to sim removal)
213 // 4. encountering bind or IPC error
Jonathan Basseri65273c82017-07-25 15:08:42 -0700214 private class ConfigHandler extends Handler {
Rambo Wang7e3bc122021-03-23 09:24:38 -0700215 ConfigHandler(@NonNull Looper looper) {
216 super(looper);
217 }
218
Jonathan Basseri65273c82017-07-25 15:08:42 -0700219 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000220 public void handleMessage(@NonNull Message msg) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700221 final int phoneId = msg.arg1;
Rambo Wangab13e3e2021-04-08 15:01:06 -0700222 logdWithLocalLog("mHandler: " + eventToString(msg.what) + " phoneId: " + phoneId);
Malcolm Chen5ea18532019-10-24 19:55:44 -0700223 if (!SubscriptionManager.isValidPhoneId(phoneId)
224 && msg.what != EVENT_MULTI_SIM_CONFIG_CHANGED) {
225 return;
226 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800227 switch (msg.what) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800228 case EVENT_CLEAR_CONFIG: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700229 clearConfigForPhone(phoneId, true);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800230 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700231 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700232
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800233 case EVENT_SYSTEM_UNLOCKED: {
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000234 for (int i = 0; i < mNumPhones; ++i) {
Sarah Chin807d5c62020-03-30 17:21:09 -0700235 // When the user unlocks the device, send the broadcast again (with a
236 // rebroadcast extra) if we have sent it before unlock. This will avoid
237 // trying to load the carrier config when the SIM is still loading when the
238 // unlock happens.
Junda Liu03aad762017-07-21 13:32:17 -0700239 if (mHasSentConfigChange[i]) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800240 logdWithLocalLog("System unlocked");
Sarah Chin807d5c62020-03-30 17:21:09 -0700241 mFromSystemUnlocked[i] = true;
Junda Liu03aad762017-07-21 13:32:17 -0700242 updateConfigForPhoneId(i);
243 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700244 }
245 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700246 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700247
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800248 case EVENT_PACKAGE_CHANGED: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700249 final String carrierPackageName = (String) msg.obj;
Rambo Wanga27fbe52022-04-12 19:09:07 +0000250 // Always clear up the cache and re-load config from scratch since the carrier
251 // service change is reliable and specific to the phoneId now.
252 clearCachedConfigForPackage(carrierPackageName);
253 logdWithLocalLog("Package changed: " + carrierPackageName
254 + ", phone=" + phoneId);
255 updateConfigForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700256 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700257 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700258
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800259 case EVENT_DO_FETCH_DEFAULT: {
Rambo Wang9bc7d362021-03-09 09:10:58 -0800260 // Clear in-memory cache for carrier app config, so when carrier app gets
261 // uninstalled, no stale config is left.
262 if (mConfigFromCarrierApp[phoneId] != null
263 && getCarrierPackageForPhoneId(phoneId) == null) {
264 mConfigFromCarrierApp[phoneId] = null;
265 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100266 // Restore persistent override values.
267 PersistableBundle config = restoreConfigFromXml(
268 mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId);
269 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800270 logd("Loaded persistent override config from XML. package="
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100271 + mPlatformCarrierConfigPackage
272 + " phoneId=" + phoneId);
273 mPersistentOverrideConfigs[phoneId] = config;
274 }
275
276 config = restoreConfigFromXml(mPlatformCarrierConfigPackage, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700277 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800278 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700279 "Loaded config from XML. package="
280 + mPlatformCarrierConfigPackage
281 + " phoneId="
282 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700283 mConfigFromDefaultApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700284 Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700285 newMsg.getData().putBoolean("loaded_from_xml", true);
286 mHandler.sendMessage(newMsg);
287 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700288 // No cached config, so fetch it from the default app.
289 if (bindToConfigPackage(
290 mPlatformCarrierConfigPackage,
291 phoneId,
292 EVENT_CONNECTED_TO_DEFAULT)) {
293 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800294 obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1 /*arg2*/,
295 getMessageToken(phoneId)),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700296 BIND_TIMEOUT_MILLIS);
297 } else {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800298 // Put a stub bundle in place so that the rest of the logic continues
299 // smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000300 mConfigFromDefaultApp[phoneId] = new PersistableBundle();
Jonathan Basseri65273c82017-07-25 15:08:42 -0700301 // Send broadcast if bind fails.
Nathan Harold48ac0972019-03-13 22:33:01 -0700302 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700303 // TODO: We *must* call unbindService even if bindService returns false.
304 // (And possibly if SecurityException was thrown.)
chen xudb04c292019-03-26 17:01:15 -0700305 loge("binding to default app: "
306 + mPlatformCarrierConfigPackage + " fails");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700307 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800308 }
309 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700310 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800311
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800312 case EVENT_CONNECTED_TO_DEFAULT: {
Rambo Wang610fdf62021-03-08 21:37:11 -0800313 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT, getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700314 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800315 // If new service connection has been created, unbind.
316 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700317 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800318 break;
319 }
chen xu02581692018-11-11 19:03:44 -0800320 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700321 // ResultReceiver callback will execute in this Handler's thread.
322 final ResultReceiver resultReceiver =
323 new ResultReceiver(this) {
324 @Override
325 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700326 unbindIfBound(mContext, conn, phoneId);
Rambo Wanga9d27a52021-04-09 16:17:33 -0700327 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT,
328 getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700329 // If new service connection has been created, this is stale.
330 if (mServiceConnection[phoneId] != conn) {
331 loge("Received response for stale request.");
332 return;
333 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700334 if (resultCode == RESULT_ERROR || resultData == null) {
335 // On error, abort config fetching.
336 loge("Failed to get carrier config");
Nathan Harold48ac0972019-03-13 22:33:01 -0700337 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700338 return;
339 }
340 PersistableBundle config =
341 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100342 saveConfigToXml(mPlatformCarrierConfigPackage, "", phoneId,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800343 carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700344 mConfigFromDefaultApp[phoneId] = config;
345 sendMessage(
346 obtainMessage(
347 EVENT_FETCH_DEFAULT_DONE, phoneId, -1));
348 }
349 };
350 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800351 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700352 ICarrierService carrierService =
353 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800354 carrierService.getCarrierConfig(phoneId, carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800355 logdWithLocalLog("Fetch config for default app: "
chen xudb04c292019-03-26 17:01:15 -0700356 + mPlatformCarrierConfigPackage
357 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700358 } catch (RemoteException e) {
chen xudb04c292019-03-26 17:01:15 -0700359 loge("Failed to get carrier config from default app: " +
360 mPlatformCarrierConfigPackage + " err: " + e.toString());
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700361 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700362 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800363 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700364 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800365 obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1 /*arg2*/,
366 getMessageToken(phoneId)),
Jonathan Basseri65273c82017-07-25 15:08:42 -0700367 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800368 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700369 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800370
Jonathan Basseri930701e2015-06-11 20:56:43 -0700371 case EVENT_BIND_DEFAULT_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800372 case EVENT_FETCH_DEFAULT_TIMEOUT: {
373 loge("Bind/fetch time out from " + mPlatformCarrierConfigPackage);
Rambo Wang610fdf62021-03-08 21:37:11 -0800374 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT, getMessageToken(phoneId));
chen xuf60b3162019-05-01 21:31:05 -0700375 // If we attempted to bind to the app, but the service connection is null due to
376 // the race condition that clear config event happens before bind/fetch complete
377 // then config was cleared while we were waiting and we should not continue.
378 if (mServiceConnection[phoneId] != null) {
379 // If a ResponseReceiver callback is in the queue when this happens, we will
380 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700381 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700382 broadcastConfigChangedIntent(phoneId);
383 }
Nathan Harold1122e3d2021-01-22 15:45:24 -0800384 // Put a stub bundle in place so that the rest of the logic continues smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000385 mConfigFromDefaultApp[phoneId] = new PersistableBundle();
Nathan Harold48ac0972019-03-13 22:33:01 -0700386 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700387 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700388 }
Jonathan Basseri930701e2015-06-11 20:56:43 -0700389
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800390 case EVENT_FETCH_DEFAULT_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700391 // If we attempted to bind to the app, but the service connection is null, then
392 // config was cleared while we were waiting and we should not continue.
393 if (!msg.getData().getBoolean("loaded_from_xml", false)
394 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800395 break;
396 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700397 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700398 if (carrierPackageName != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800399 logd("Found carrier config app: " + carrierPackageName);
Jordan Liu38a614c2019-03-20 15:01:17 -0700400 sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId, -1));
Jonathan Basserib919e932015-05-27 16:53:08 +0000401 } else {
Nathan Harold48ac0972019-03-13 22:33:01 -0700402 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700403 }
404 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700405 }
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700406
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800407 case EVENT_DO_FETCH_CARRIER: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700408 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700409 final PersistableBundle config =
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100410 restoreConfigFromXml(carrierPackageName, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700411 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800412 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700413 "Loaded config from XML. package="
414 + carrierPackageName
415 + " phoneId="
416 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700417 mConfigFromCarrierApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700418 Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700419 newMsg.getData().putBoolean("loaded_from_xml", true);
420 sendMessage(newMsg);
421 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700422 // No cached config, so fetch it from a carrier app.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800423 if (carrierPackageName != null && bindToConfigPackage(carrierPackageName,
424 phoneId, EVENT_CONNECTED_TO_CARRIER)) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700425 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800426 obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1 /*arg2*/,
427 getMessageToken(phoneId)),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700428 BIND_TIMEOUT_MILLIS);
429 } else {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800430 // Put a stub bundle in place so that the rest of the logic continues
431 // smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000432 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
Jonathan Basseri65273c82017-07-25 15:08:42 -0700433 // Send broadcast if bind fails.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700434 broadcastConfigChangedIntent(phoneId);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800435 loge("Bind to carrier app: " + carrierPackageName + " fails");
Nathan Harold48ac0972019-03-13 22:33:01 -0700436 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700437 }
438 }
439 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700440 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700441
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800442 case EVENT_CONNECTED_TO_CARRIER: {
Rambo Wang610fdf62021-03-08 21:37:11 -0800443 removeMessages(EVENT_BIND_CARRIER_TIMEOUT, getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700444 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800445 // If new service connection has been created, unbind.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700446 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700447 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800448 break;
449 }
chen xu02581692018-11-11 19:03:44 -0800450 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700451 // ResultReceiver callback will execute in this Handler's thread.
452 final ResultReceiver resultReceiver =
453 new ResultReceiver(this) {
454 @Override
455 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700456 unbindIfBound(mContext, conn, phoneId);
Rambo Wanga9d27a52021-04-09 16:17:33 -0700457 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT,
458 getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700459 // If new service connection has been created, this is stale.
460 if (mServiceConnection[phoneId] != conn) {
461 loge("Received response for stale request.");
462 return;
463 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700464 if (resultCode == RESULT_ERROR || resultData == null) {
465 // On error, abort config fetching.
chen xudb04c292019-03-26 17:01:15 -0700466 loge("Failed to get carrier config from carrier app: "
467 + getCarrierPackageForPhoneId(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700468 broadcastConfigChangedIntent(phoneId);
Nathan Harold48ac0972019-03-13 22:33:01 -0700469 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700470 return;
471 }
472 PersistableBundle config =
473 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100474 saveConfigToXml(getCarrierPackageForPhoneId(phoneId), "",
475 phoneId, carrierId, config);
Amit Mahajan304e92b2021-04-08 14:03:30 -0700476 if (config != null) {
477 mConfigFromCarrierApp[phoneId] = config;
478 } else {
479 logdWithLocalLog("Config from carrier app is null "
480 + "for phoneId " + phoneId);
481 // Put a stub bundle in place so that the rest of the logic
482 // continues smoothly.
483 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
484 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700485 sendMessage(
486 obtainMessage(
487 EVENT_FETCH_CARRIER_DONE, phoneId, -1));
488 }
489 };
490 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800491 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700492 ICarrierService carrierService =
493 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800494 carrierService.getCarrierConfig(phoneId, carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800495 logdWithLocalLog("Fetch config for carrier app: "
chen xudb04c292019-03-26 17:01:15 -0700496 + getCarrierPackageForPhoneId(phoneId)
497 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700498 } catch (RemoteException e) {
499 loge("Failed to get carrier config: " + e.toString());
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700500 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700501 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800502 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700503 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800504 obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1 /*arg2*/,
505 getMessageToken(phoneId)),
Jonathan Basseri65273c82017-07-25 15:08:42 -0700506 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800507 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700508 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800509
Jonathan Basseri930701e2015-06-11 20:56:43 -0700510 case EVENT_BIND_CARRIER_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800511 case EVENT_FETCH_CARRIER_TIMEOUT: {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800512 loge("Bind/fetch from carrier app timeout, package="
513 + getCarrierPackageForPhoneId(phoneId));
Rambo Wang610fdf62021-03-08 21:37:11 -0800514 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT, getMessageToken(phoneId));
chen xuf60b3162019-05-01 21:31:05 -0700515 // If we attempted to bind to the app, but the service connection is null due to
516 // the race condition that clear config event happens before bind/fetch complete
517 // then config was cleared while we were waiting and we should not continue.
518 if (mServiceConnection[phoneId] != null) {
519 // If a ResponseReceiver callback is in the queue when this happens, we will
520 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700521 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700522 broadcastConfigChangedIntent(phoneId);
523 }
Nathan Harold1122e3d2021-01-22 15:45:24 -0800524 // Put a stub bundle in place so that the rest of the logic continues smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000525 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
Nathan Harold48ac0972019-03-13 22:33:01 -0700526 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700527 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700528 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800529 case EVENT_FETCH_CARRIER_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700530 // If we attempted to bind to the app, but the service connection is null, then
531 // config was cleared while we were waiting and we should not continue.
532 if (!msg.getData().getBoolean("loaded_from_xml", false)
533 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800534 break;
535 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700536 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800537 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700538 }
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700539
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800540 case EVENT_CHECK_SYSTEM_UPDATE: {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700541 SharedPreferences sharedPrefs =
542 PreferenceManager.getDefaultSharedPreferences(mContext);
543 final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
544 if (!Build.FINGERPRINT.equals(lastFingerprint)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800545 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700546 "Build fingerprint changed. old: "
547 + lastFingerprint
548 + " new: "
549 + Build.FINGERPRINT);
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700550 clearCachedConfigForPackage(null);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700551 sharedPrefs
552 .edit()
553 .putString(KEY_FINGERPRINT, Build.FINGERPRINT)
554 .apply();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700555 }
556 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700557 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700558
559 case EVENT_SUBSCRIPTION_INFO_UPDATED:
560 broadcastConfigChangedIntent(phoneId);
561 break;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700562 case EVENT_MULTI_SIM_CONFIG_CHANGED:
563 onMultiSimConfigChanged();
564 break;
Jayachandran C645ec612020-01-10 10:30:25 -0800565
566 case EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG: {
567 PersistableBundle config =
568 restoreNoSimConfigFromXml(mPlatformCarrierConfigPackage);
569
570 if (config != null) {
571 logd("Loaded no SIM config from XML. package="
572 + mPlatformCarrierConfigPackage);
573 mNoSimConfig = config;
574 sendMessage(
575 obtainMessage(
576 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE,
577 phoneId, -1));
578 } else {
579 // No cached config, so fetch it from the default app.
580 if (bindToConfigPackage(
581 mPlatformCarrierConfigPackage,
582 phoneId,
583 EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG)) {
584 sendMessageDelayed(
585 obtainMessage(
586 EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT,
587 phoneId, -1), BIND_TIMEOUT_MILLIS);
588 } else {
589 broadcastConfigChangedIntent(phoneId, false);
590 // TODO: We *must* call unbindService even if bindService returns false.
591 // (And possibly if SecurityException was thrown.)
592 loge("binding to default app to fetch no SIM config: "
593 + mPlatformCarrierConfigPackage + " fails");
594 }
595 }
596 break;
597 }
598
599 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE: {
600 broadcastConfigChangedIntent(phoneId, false);
601 break;
602 }
603
604 case EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
605 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT: {
606 loge("Bind/fetch time out for no SIM config from "
607 + mPlatformCarrierConfigPackage);
608 removeMessages(EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
609 // If we attempted to bind to the app, but the service connection is null due to
610 // the race condition that clear config event happens before bind/fetch complete
611 // then config was cleared while we were waiting and we should not continue.
612 if (mServiceConnectionForNoSimConfig[phoneId] != null) {
613 // If a ResponseReceiver callback is in the queue when this happens, we will
614 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700615 unbindIfBoundForNoSimConfig(mContext,
616 mServiceConnectionForNoSimConfig[phoneId], phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800617 }
618 broadcastConfigChangedIntent(phoneId, false);
619 break;
620 }
621
622 case EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG: {
623 removeMessages(EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
624 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
625 // If new service connection has been created, unbind.
626 if (mServiceConnectionForNoSimConfig[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700627 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800628 break;
629 }
630
631 // ResultReceiver callback will execute in this Handler's thread.
632 final ResultReceiver resultReceiver =
633 new ResultReceiver(this) {
634 @Override
635 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700636 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800637 // If new service connection has been created, this is stale.
638 if (mServiceConnectionForNoSimConfig[phoneId] != conn) {
639 loge("Received response for stale request.");
640 return;
641 }
642 removeMessages(EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
643 if (resultCode == RESULT_ERROR || resultData == null) {
644 // On error, abort config fetching.
645 loge("Failed to get no SIM carrier config");
646 return;
647 }
648 PersistableBundle config =
649 resultData.getParcelable(KEY_CONFIG_BUNDLE);
650 saveNoSimConfigToXml(mPlatformCarrierConfigPackage, config);
651 mNoSimConfig = config;
652 sendMessage(
653 obtainMessage(
654 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE,
655 phoneId, -1));
656 }
657 };
658 // Now fetch the config asynchronously from the ICarrierService.
659 try {
660 ICarrierService carrierService =
661 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800662 carrierService.getCarrierConfig(phoneId, null, resultReceiver);
Jayachandran C645ec612020-01-10 10:30:25 -0800663 logdWithLocalLog("Fetch no sim config from default app: "
664 + mPlatformCarrierConfigPackage);
665 } catch (RemoteException e) {
666 loge("Failed to get no sim carrier config from default app: " +
667 mPlatformCarrierConfigPackage + " err: " + e.toString());
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700668 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800669 break; // So we don't set a timeout.
670 }
671 sendMessageDelayed(
672 obtainMessage(
673 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT,
674 phoneId, -1), BIND_TIMEOUT_MILLIS);
675 break;
676 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800677 }
678 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700679 }
680
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000681 @NonNull private final Handler mHandler;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800682
683 /**
684 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
685 * receiver for relevant events.
686 */
Rambo Wang7e3bc122021-03-23 09:24:38 -0700687 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000688 /* package */ CarrierConfigLoader(@NonNull Context context,
689 @NonNull SubscriptionInfoUpdater subscriptionInfoUpdater, @NonNull Looper looper) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800690 mContext = context;
Meng Wang33ad2bc2017-03-16 20:21:20 -0700691 mPlatformCarrierConfigPackage =
692 mContext.getString(R.string.platform_carrier_config_package);
Rambo Wang7e3bc122021-03-23 09:24:38 -0700693 mHandler = new ConfigHandler(looper);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800694
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000695 IntentFilter systemEventsFilter = new IntentFilter();
696 systemEventsFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
697 systemEventsFilter.addAction(TelephonyManager.ACTION_MULTI_SIM_CONFIG_CHANGED);
Rambo Wanga27fbe52022-04-12 19:09:07 +0000698 context.registerReceiver(mSystemBroadcastReceiver, systemEventsFilter);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800699
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000700 mNumPhones = TelephonyManager.from(context).getActiveModemCount();
701 mConfigFromDefaultApp = new PersistableBundle[mNumPhones];
702 mConfigFromCarrierApp = new PersistableBundle[mNumPhones];
703 mPersistentOverrideConfigs = new PersistableBundle[mNumPhones];
704 mOverrideConfigs = new PersistableBundle[mNumPhones];
Rambo Wang42026112021-04-07 20:57:28 +0000705 mNoSimConfig = new PersistableBundle();
Rambo Wang8f16f3e2022-04-15 01:26:23 +0000706 mServiceConnection = new CarrierServiceConnection[mNumPhones];
707 mServiceBound = new boolean[mNumPhones];
708 mHasSentConfigChange = new boolean[mNumPhones];
709 mFromSystemUnlocked = new boolean[mNumPhones];
710 mServiceConnectionForNoSimConfig = new CarrierServiceConnection[mNumPhones];
711 mServiceBoundForNoSimConfig = new boolean[mNumPhones];
Rambo Wanga27fbe52022-04-12 19:09:07 +0000712 mCarrierServiceChangeCallbacks = new CarrierServiceChangeCallback[mNumPhones];
713 for (int phoneId = 0; phoneId < mNumPhones; phoneId++) {
714 mCarrierServiceChangeCallbacks[phoneId] = new CarrierServiceChangeCallback(phoneId);
715 TelephonyManager.from(context).registerCarrierPrivilegesCallback(phoneId,
716 new HandlerExecutor(mHandler), mCarrierServiceChangeCallbacks[phoneId]);
717 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800718 logd("CarrierConfigLoader has started");
Rambo Wang7e3bc122021-03-23 09:24:38 -0700719 mSubscriptionInfoUpdater = subscriptionInfoUpdater;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700720 mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800721 }
722
723 /**
724 * Initialize the singleton CarrierConfigLoader instance.
725 *
726 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
727 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000728 @NonNull
729 /* package */ static CarrierConfigLoader init(@NonNull Context context) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800730 synchronized (CarrierConfigLoader.class) {
731 if (sInstance == null) {
Rambo Wang7e3bc122021-03-23 09:24:38 -0700732 sInstance = new CarrierConfigLoader(context,
733 PhoneFactory.getSubscriptionInfoUpdater(), Looper.myLooper());
Brad Ebingerfa6575f2021-05-04 00:29:50 +0000734 // Make this service available through ServiceManager.
735 TelephonyFrameworkInitializer.getTelephonyServiceManager()
736 .getCarrierConfigServiceRegisterer().register(sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800737 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700738 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800739 }
740 return sInstance;
741 }
742 }
743
Rambo Wang7e3bc122021-03-23 09:24:38 -0700744 @VisibleForTesting
745 /* package */ void clearConfigForPhone(int phoneId, boolean fetchNoSimConfig) {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700746 /* Ignore clear configuration request if device is being shutdown. */
747 Phone phone = PhoneFactory.getPhone(phoneId);
748 if (phone != null) {
749 if (phone.isShuttingDown()) {
750 return;
751 }
752 }
753
754 mConfigFromDefaultApp[phoneId] = null;
755 mConfigFromCarrierApp[phoneId] = null;
756 mServiceConnection[phoneId] = null;
757 mHasSentConfigChange[phoneId] = false;
758
Jayachandran C645ec612020-01-10 10:30:25 -0800759 if (fetchNoSimConfig) {
760 // To fetch no SIM config
761 mHandler.sendMessage(
762 mHandler.obtainMessage(
763 EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG, phoneId, -1));
764 }
Malcolm Chen5ea18532019-10-24 19:55:44 -0700765 }
766
Nathan Harold48ac0972019-03-13 22:33:01 -0700767 private void notifySubscriptionInfoUpdater(int phoneId) {
768 String configPackagename;
769 PersistableBundle configToSend;
770 int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
771 // Prefer the carrier privileged carrier app, but if there is not one, use the platform
772 // default carrier app.
773 if (mConfigFromCarrierApp[phoneId] != null) {
774 configPackagename = getCarrierPackageForPhoneId(phoneId);
775 configToSend = mConfigFromCarrierApp[phoneId];
776 } else {
777 configPackagename = mPlatformCarrierConfigPackage;
778 configToSend = mConfigFromDefaultApp[phoneId];
779 }
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700780
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800781 if (configToSend == null) {
Rambo Wang42026112021-04-07 20:57:28 +0000782 configToSend = new PersistableBundle();
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800783 }
784
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700785 // mOverrideConfigs is for testing. And it will override current configs.
786 PersistableBundle config = mOverrideConfigs[phoneId];
787 if (config != null) {
Torbjorn Eklund1050cb02018-11-16 14:05:38 +0100788 configToSend = new PersistableBundle(configToSend);
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700789 configToSend.putAll(config);
790 }
791
Nathan Harold48ac0972019-03-13 22:33:01 -0700792 mSubscriptionInfoUpdater.updateSubscriptionByCarrierConfigAndNotifyComplete(
793 phoneId, configPackagename, configToSend,
794 mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1));
795 }
796
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800797 private void broadcastConfigChangedIntent(int phoneId) {
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800798 broadcastConfigChangedIntent(phoneId, true);
799 }
800
801 private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800802 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800803 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
chen xuf9db49f2019-03-31 23:04:42 -0700804 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800805 if (addSubIdExtra) {
806 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
807 int[] subIds = SubscriptionManager.getSubId(phoneId);
808 if (!ArrayUtils.isEmpty(subIds)) {
809 TelephonyManager telMgr = TelephonyManager.from(mContext)
810 .createForSubscriptionId(subIds[0]);
811 simApplicationState = telMgr.getSimApplicationState();
812 }
Malcolm Chenf7635ec2020-04-15 15:45:00 -0700813 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId
814 + " simApplicationState " + simApplicationState);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800815 // Include subId/carrier id extra only if SIM records are loaded
816 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
817 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
818 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
819 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID,
820 getSpecificCarrierIdForPhoneId(phoneId));
821 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, getCarrierIdForPhoneId(phoneId));
822 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800823 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800824 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Sarah Chin807d5c62020-03-30 17:21:09 -0700825 intent.putExtra(CarrierConfigManager.EXTRA_REBROADCAST_ON_UNLOCK,
826 mFromSystemUnlocked[phoneId]);
Meng Wang97a6a462020-01-23 16:22:16 -0800827 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
Jack Yue9e95be2020-03-22 10:56:06 -0700828 int[] subIds = SubscriptionManager.getSubId(phoneId);
829 if (subIds != null && subIds.length > 0) {
830 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId + ", subId=" + subIds[0]);
831 } else {
832 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
833 }
Junda Liu03aad762017-07-21 13:32:17 -0700834 mHasSentConfigChange[phoneId] = true;
Sarah Chin807d5c62020-03-30 17:21:09 -0700835 mFromSystemUnlocked[phoneId] = false;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800836 }
837
838 /** Binds to the default or carrier config app. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000839 private boolean bindToConfigPackage(@NonNull String pkgName, int phoneId, int eventId) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800840 logdWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700841 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700842 carrierService.setPackage(pkgName);
Jayachandran C645ec612020-01-10 10:30:25 -0800843 CarrierServiceConnection serviceConnection = new CarrierServiceConnection(
844 phoneId, pkgName, eventId);
845 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
846 mServiceConnectionForNoSimConfig[phoneId] = serviceConnection;
847 } else {
848 mServiceConnection[phoneId] = serviceConnection;
849 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800850 try {
Jayachandran C645ec612020-01-10 10:30:25 -0800851 if (mContext.bindService(carrierService, serviceConnection,
Jordan Liu178430d2020-02-10 14:32:15 -0800852 Context.BIND_AUTO_CREATE)) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700853 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
854 mServiceBoundForNoSimConfig[phoneId] = true;
855 } else {
856 mServiceBound[phoneId] = true;
857 }
Jordan Liu178430d2020-02-10 14:32:15 -0800858 return true;
859 } else {
860 return false;
861 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800862 } catch (SecurityException ex) {
863 return false;
864 }
865 }
866
Rambo Wang7e3bc122021-03-23 09:24:38 -0700867 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000868 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -0700869 /* package */ CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800870 String mcc = "";
871 String mnc = "";
872 String imsi = "";
873 String gid1 = "";
874 String gid2 = "";
875 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
876 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800877 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800878 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700879 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
880 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800881 mcc = simOperator.substring(0, 3);
882 mnc = simOperator.substring(3);
883 }
884 Phone phone = PhoneFactory.getPhone(phoneId);
885 if (phone != null) {
886 imsi = phone.getSubscriberId();
887 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700888 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800889 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800890 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800891 }
chen xua31f22b2019-03-06 15:28:50 -0800892 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800893 }
894
Rambo Wangf7c214a2022-03-17 12:36:22 -0700895 /** Returns the package name of a privileged carrier app, or null if there is none. */
Nathan Harold1122e3d2021-01-22 15:45:24 -0800896 @Nullable
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700897 private String getCarrierPackageForPhoneId(int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -0700898 final long token = Binder.clearCallingIdentity();
899 try {
Rambo Wangf7c214a2022-03-17 12:36:22 -0700900 return TelephonyManager.from(mContext)
901 .getCarrierServicePackageNameForLogicalSlot(phoneId);
Nazanin1adf4562021-03-29 15:35:30 -0700902 } finally {
903 Binder.restoreCallingIdentity(token);
904 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700905 }
906
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000907 @Nullable
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700908 private String getIccIdForPhoneId(int phoneId) {
909 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
910 return null;
911 }
912 Phone phone = PhoneFactory.getPhone(phoneId);
913 if (phone == null) {
914 return null;
915 }
916 return phone.getIccSerialNumber();
917 }
918
chen xu02581692018-11-11 19:03:44 -0800919 /**
chen xua31f22b2019-03-06 15:28:50 -0800920 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800921 */
chen xua31f22b2019-03-06 15:28:50 -0800922 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700923 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800924 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700925 }
926 Phone phone = PhoneFactory.getPhone(phoneId);
927 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800928 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700929 }
chen xua31f22b2019-03-06 15:28:50 -0800930 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800931 }
932
933 /**
934 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
935 */
936 private int getCarrierIdForPhoneId(int phoneId) {
937 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
938 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700939 }
chen xu02581692018-11-11 19:03:44 -0800940 Phone phone = PhoneFactory.getPhone(phoneId);
941 if (phone == null) {
942 return TelephonyManager.UNKNOWN_CARRIER_ID;
943 }
944 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700945 }
946
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700947 /**
948 * Writes a bundle to an XML file.
949 *
chen xu02581692018-11-11 19:03:44 -0800950 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -0800951 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -0800952 * should have a single copy of XML file named after carrier id. However, it's still possible
953 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
954 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
955 * carrier while iccid remains the same.
956 *
957 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
958 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700959 *
960 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700961 *
Jayachandran C645ec612020-01-10 10:30:25 -0800962 * @param packageName the name of the package from which we fetched this bundle.
963 * @param extraString An extra string to be used in the XML file name.
964 * @param phoneId the phone ID.
965 * @param carrierId contains all carrier-identifying information.
966 * @param config the bundle to be written. Null will be treated as an empty bundle.
967 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700968 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000969 private void saveConfigToXml(@Nullable String packageName, @NonNull String extraString,
970 int phoneId, @Nullable CarrierIdentifier carrierId, @NonNull PersistableBundle config,
971 boolean isNoSimConfig) {
Jayachandran C645ec612020-01-10 10:30:25 -0800972 if (packageName == null) {
973 loge("Cannot save config with null packageName");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800974 return;
975 }
976
Jayachandran C645ec612020-01-10 10:30:25 -0800977 String fileName;
978 if (isNoSimConfig) {
979 fileName = getFilenameForNoSimConfig(packageName);
980 } else {
981 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
982 != TelephonyManager.SIM_STATE_LOADED) {
983 loge("Skip save config because SIM records are not loaded.");
984 return;
985 }
986
987 final String iccid = getIccIdForPhoneId(phoneId);
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000988 final int cid = carrierId != null ? carrierId.getSpecificCarrierId()
989 : TelephonyManager.UNKNOWN_CARRIER_ID;
Jayachandran C645ec612020-01-10 10:30:25 -0800990 if (iccid == null) {
991 loge("Cannot save config with null iccid.");
992 return;
993 }
994 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700995 }
Jayachandran C645ec612020-01-10 10:30:25 -0800996
Junda Liu02596502016-11-15 13:46:43 -0800997 // b/32668103 Only save to file if config isn't empty.
998 // In case of failure, not caching an empty bundle will
999 // try loading config again on next power on or sim loaded.
1000 // Downside is for genuinely empty bundle, will bind and load
1001 // on every power on.
1002 if (config == null || config.isEmpty()) {
1003 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001004 }
1005
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001006 final String version = getPackageVersion(packageName);
1007 if (version == null) {
1008 loge("Failed to get package version for: " + packageName);
1009 return;
1010 }
1011
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001012 logdWithLocalLog(
1013 "Save config to xml, packagename: " + packageName + " phoneId: " + phoneId);
chen xudb04c292019-03-26 17:01:15 -07001014
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001015 FileOutputStream outFile = null;
1016 try {
Jayachandran C645ec612020-01-10 10:30:25 -08001017 outFile = new FileOutputStream(new File(mContext.getFilesDir(), fileName));
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001018 config.putString(KEY_VERSION, version);
1019 config.writeToStream(outFile);
1020 outFile.flush();
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001021 outFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001022 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001023 loge(e.toString());
1024 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001025 }
1026
Rambo Wang7e3bc122021-03-23 09:24:38 -07001027 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001028 /* package */ void saveConfigToXml(@Nullable String packageName, @NonNull String extraString,
1029 int phoneId, @NonNull CarrierIdentifier carrierId, @NonNull PersistableBundle config) {
Jayachandran C645ec612020-01-10 10:30:25 -08001030 saveConfigToXml(packageName, extraString, phoneId, carrierId, config, false);
1031 }
1032
Rambo Wang7e3bc122021-03-23 09:24:38 -07001033 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001034 /* package */ void saveNoSimConfigToXml(@Nullable String packageName,
1035 @NonNull PersistableBundle config) {
Jayachandran C645ec612020-01-10 10:30:25 -08001036 saveConfigToXml(packageName, "", -1, null, config, true);
1037 }
1038
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001039 /**
1040 * Reads a bundle from an XML file.
1041 *
1042 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +08001043 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001044 *
1045 * In case of errors, or if the saved config is from a different package version than the
1046 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001047 *
Jayachandran C645ec612020-01-10 10:30:25 -08001048 * @param packageName the name of the package from which we fetched this bundle.
1049 * @param extraString An extra string to be used in the XML file name.
1050 * @param phoneId the phone ID.
1051 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001052 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001053 * version does not match, or reading config fails.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001054 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001055 @Nullable
1056 private PersistableBundle restoreConfigFromXml(@Nullable String packageName,
1057 @NonNull String extraString, int phoneId, boolean isNoSimConfig) {
Jayachandran C645ec612020-01-10 10:30:25 -08001058 if (packageName == null) {
1059 loge("Cannot restore config with null packageName");
1060 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001061 final String version = getPackageVersion(packageName);
1062 if (version == null) {
1063 loge("Failed to get package version for: " + packageName);
1064 return null;
1065 }
Yuchen Dongc15afed2018-04-26 17:05:22 +08001066
Jayachandran C645ec612020-01-10 10:30:25 -08001067 String fileName;
arunvoddu664c36a2021-10-11 20:09:28 +00001068 String iccid = null;
Jayachandran C645ec612020-01-10 10:30:25 -08001069 if (isNoSimConfig) {
1070 fileName = getFilenameForNoSimConfig(packageName);
1071 } else {
1072 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
1073 != TelephonyManager.SIM_STATE_LOADED) {
1074 loge("Skip restore config because SIM records are not loaded.");
1075 return null;
1076 }
1077
arunvoddu664c36a2021-10-11 20:09:28 +00001078 iccid = getIccIdForPhoneId(phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001079 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1080 if (iccid == null) {
1081 loge("Cannot restore config with null iccid.");
1082 return null;
1083 }
1084 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001085 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001086
1087 PersistableBundle restoredBundle = null;
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001088 File file = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001089 FileInputStream inFile = null;
1090 try {
Jayachandran C645ec612020-01-10 10:30:25 -08001091 file = new File(mContext.getFilesDir(),fileName);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001092 inFile = new FileInputStream(file);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001093
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001094 restoredBundle = PersistableBundle.readFromStream(inFile);
1095 String savedVersion = restoredBundle.getString(KEY_VERSION);
1096 restoredBundle.remove(KEY_VERSION);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001097
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001098 if (!version.equals(savedVersion)) {
1099 loge("Saved version mismatch: " + version + " vs " + savedVersion);
1100 restoredBundle = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001101 }
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001102
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001103 inFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001104 } catch (FileNotFoundException e) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001105 // Missing file is normal occurrence that might occur with a new sim or when restoring
1106 // an override file during boot and should not be treated as an error.
arunvoddu664c36a2021-10-11 20:09:28 +00001107 if (file != null) {
1108 if (isNoSimConfig) {
1109 logd("File not found: " + file.getPath());
1110 } else {
1111 String filePath = file.getPath();
1112 filePath = getFilePathForLogging(filePath, iccid);
1113 logd("File not found : " + filePath);
1114 }
1115 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001116 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001117 loge(e.toString());
1118 }
1119
1120 return restoredBundle;
1121 }
1122
arunvoddu664c36a2021-10-11 20:09:28 +00001123 /**
1124 * This method will mask most part of iccid in the filepath for logging on userbuild
1125 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001126 @NonNull
1127 private String getFilePathForLogging(@Nullable String filePath, @Nullable String iccid) {
arunvoddu664c36a2021-10-11 20:09:28 +00001128 // If loggable then return with actual file path
1129 if (Rlog.isLoggable(LOG_TAG, Log.VERBOSE)) {
1130 return filePath;
1131 }
1132 String path = filePath;
1133 int length = (iccid != null) ? iccid.length() : 0;
1134 if (length > 5 && filePath != null) {
1135 path = filePath.replace(iccid.substring(5), "***************");
1136 }
1137 return path;
1138 }
1139
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001140 @Nullable
1141 private PersistableBundle restoreConfigFromXml(@Nullable String packageName,
1142 @NonNull String extraString, int phoneId) {
Jayachandran C645ec612020-01-10 10:30:25 -08001143 return restoreConfigFromXml(packageName, extraString, phoneId, false);
1144 }
1145
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001146 @Nullable
1147 private PersistableBundle restoreNoSimConfigFromXml(@Nullable String packageName) {
Jayachandran C645ec612020-01-10 10:30:25 -08001148 return restoreConfigFromXml(packageName, "", -1, true);
1149 }
1150
Junda Liu8a8a53a2015-05-15 16:19:57 -07001151 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001152 * Clears cached carrier config.
1153 * This deletes all saved XML files associated with the given package name. If packageName is
1154 * null, then it deletes all saved XML files.
1155 *
1156 * @param packageName the name of a carrier package, or null if all cached config should be
1157 * cleared.
1158 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -07001159 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001160 private boolean clearCachedConfigForPackage(@Nullable final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001161 File dir = mContext.getFilesDir();
1162 File[] packageFiles = dir.listFiles(new FilenameFilter() {
1163 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001164 if (packageName != null) {
1165 return filename.startsWith("carrierconfig-" + packageName + "-");
1166 } else {
1167 return filename.startsWith("carrierconfig-");
1168 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001169 }
1170 });
Junda Liu8a8a53a2015-05-15 16:19:57 -07001171 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001172 for (File f : packageFiles) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001173 logd("Deleting " + f.getName());
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001174 f.delete();
1175 }
Junda Liu8a8a53a2015-05-15 16:19:57 -07001176 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001177 }
1178
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001179 /** Builds a canonical file name for a config file. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001180 @NonNull
Jayachandran C645ec612020-01-10 10:30:25 -08001181 private static String getFilenameForConfig(
1182 @NonNull String packageName, @NonNull String extraString,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001183 @NonNull String iccid, int cid) {
chen xu02581692018-11-11 19:03:44 -08001184 // the same carrier should have a single copy of XML file named after carrier id.
1185 // However, it's still possible that platform doesn't recognize the current sim carrier,
1186 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
1187 // cases SIM OTA resolves to different carrier while iccid remains the same.
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001188 return "carrierconfig-" + packageName + extraString + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001189 }
1190
Jayachandran C645ec612020-01-10 10:30:25 -08001191 /** Builds a canonical file name for no SIM config file. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001192 @NonNull
Jayachandran C645ec612020-01-10 10:30:25 -08001193 private String getFilenameForNoSimConfig(@NonNull String packageName) {
1194 return "carrierconfig-" + packageName + "-" + "nosim" + ".xml";
1195 }
1196
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001197 /** Return the current version code of a package, or null if the name is not found. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001198 @Nullable
1199 private String getPackageVersion(@NonNull String packageName) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001200 try {
1201 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -08001202 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001203 } catch (PackageManager.NameNotFoundException e) {
1204 return null;
1205 }
1206 }
1207
Jonathan Basseri65273c82017-07-25 15:08:42 -07001208 /**
1209 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001210 *
1211 * This reads config bundles for the given phoneId. That means getting the latest bundle from
1212 * the default app and a privileged carrier app, if present. This will not bind to an app if we
1213 * have a saved config file to use instead.
1214 */
1215 private void updateConfigForPhoneId(int phoneId) {
Jonathan Basseri65273c82017-07-25 15:08:42 -07001216 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001217 }
1218
Malcolm Chen5ea18532019-10-24 19:55:44 -07001219 private void onMultiSimConfigChanged() {
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001220 int oldNumPhones = mNumPhones;
1221 mNumPhones = TelephonyManager.from(mContext).getActiveModemCount();
1222 if (mNumPhones == oldNumPhones) {
1223 return;
1224 }
1225 logdWithLocalLog("mNumPhones change from " + oldNumPhones + " to " + mNumPhones);
1226
Rambo Wanga27fbe52022-04-12 19:09:07 +00001227 // If DS -> SS switch, release the resources BEFORE truncating the arrays to avoid leaking
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001228 for (int phoneId = mNumPhones; phoneId < oldNumPhones; phoneId++) {
1229 if (mServiceConnection[phoneId] != null) {
1230 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
1231 }
1232 if (mServiceConnectionForNoSimConfig[phoneId] != null) {
1233 unbindIfBoundForNoSimConfig(mContext, mServiceConnectionForNoSimConfig[phoneId],
1234 phoneId);
1235 }
1236 }
1237
Rambo Wanga27fbe52022-04-12 19:09:07 +00001238 // The phone to slot mapping may change, unregister here and re-register callbacks later
1239 for (int phoneId = 0; phoneId < oldNumPhones; phoneId++) {
1240 if (mCarrierServiceChangeCallbacks[phoneId] != null) {
1241 TelephonyManager.from(mContext).unregisterCarrierPrivilegesCallback(
1242 mCarrierServiceChangeCallbacks[phoneId]);
1243 }
1244 }
1245
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001246 // Copy the original arrays, truncate or padding with zeros (if necessary) to new length
1247 mConfigFromDefaultApp = Arrays.copyOf(mConfigFromDefaultApp, mNumPhones);
1248 mConfigFromCarrierApp = Arrays.copyOf(mConfigFromCarrierApp, mNumPhones);
1249 mPersistentOverrideConfigs = Arrays.copyOf(mPersistentOverrideConfigs, mNumPhones);
1250 mOverrideConfigs = Arrays.copyOf(mOverrideConfigs, mNumPhones);
1251 mServiceConnection = Arrays.copyOf(mServiceConnection, mNumPhones);
1252 mServiceConnectionForNoSimConfig =
1253 Arrays.copyOf(mServiceConnectionForNoSimConfig, mNumPhones);
1254 mServiceBound = Arrays.copyOf(mServiceBound, mNumPhones);
1255 mServiceBoundForNoSimConfig = Arrays.copyOf(mServiceBoundForNoSimConfig, mNumPhones);
1256 mHasSentConfigChange = Arrays.copyOf(mHasSentConfigChange, mNumPhones);
1257 mFromSystemUnlocked = Arrays.copyOf(mFromSystemUnlocked, mNumPhones);
Rambo Wanga27fbe52022-04-12 19:09:07 +00001258 mCarrierServiceChangeCallbacks = Arrays.copyOf(mCarrierServiceChangeCallbacks, mNumPhones);
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001259
Rambo Wanga27fbe52022-04-12 19:09:07 +00001260 // Load the config for all the phones and re-register callback AFTER padding the arrays.
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001261 for (int phoneId = 0; phoneId < mNumPhones; phoneId++) {
1262 updateConfigForPhoneId(phoneId);
Rambo Wanga27fbe52022-04-12 19:09:07 +00001263 mCarrierServiceChangeCallbacks[phoneId] = new CarrierServiceChangeCallback(phoneId);
1264 TelephonyManager.from(mContext).registerCarrierPrivilegesCallback(phoneId,
1265 new HandlerExecutor(mHandler), mCarrierServiceChangeCallbacks[phoneId]);
Malcolm Chen5ea18532019-10-24 19:55:44 -07001266 }
1267 }
1268
Hall Liu506724b2018-10-22 18:16:14 -07001269 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001270 @NonNull
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001271 public PersistableBundle getConfigForSubId(int subscriptionId, @NonNull String callingPackage) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001272 return getConfigForSubIdWithFeature(subscriptionId, callingPackage, null);
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08001273 }
1274
1275 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001276 @NonNull
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001277 public PersistableBundle getConfigForSubIdWithFeature(int subscriptionId,
1278 @NonNull String callingPackage, @Nullable String callingFeatureId) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001279 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subscriptionId,
1280 callingPackage, callingFeatureId, "getCarrierConfig")) {
Rambo Wang42026112021-04-07 20:57:28 +00001281 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -07001282 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -07001283
Rambo Wangd2b004b2021-03-30 10:10:23 -07001284 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001285 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001286 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001287 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001288 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001289 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001290 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001291 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001292 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001293 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001294 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001295 config = mPersistentOverrideConfigs[phoneId];
1296 if (config != null) {
1297 retConfig.putAll(config);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001298 }
Hall Liu506724b2018-10-22 18:16:14 -07001299 config = mOverrideConfigs[phoneId];
1300 if (config != null) {
1301 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -07001302 }
Nathan Harold1122e3d2021-01-22 15:45:24 -08001303 // Ignore the theoretical case of the default app not being present since that won't
1304 // work in CarrierConfigLoader today.
1305 final boolean allConfigsApplied =
1306 (mConfigFromCarrierApp[phoneId] != null
1307 || getCarrierPackageForPhoneId(phoneId) == null)
1308 && mConfigFromDefaultApp[phoneId] != null;
1309 retConfig.putBoolean(
1310 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, allConfigsApplied);
Jayachandran C645ec612020-01-10 10:30:25 -08001311 } else {
1312 if (mNoSimConfig != null) {
1313 retConfig.putAll(mNoSimConfig);
1314 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001315 }
1316 return retConfig;
1317 }
1318
1319 @Override
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001320 public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides,
1321 boolean persistent) {
Hall Liu506724b2018-10-22 18:16:14 -07001322 mContext.enforceCallingOrSelfPermission(
1323 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Brad Ebingerf6281ad2019-04-25 11:02:04 -07001324 //TODO: Also check for SHELL UID to restrict this method to testing only (b/131326259)
Hall Liu506724b2018-10-22 18:16:14 -07001325 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1326 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001327 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
Rambo Wangd2b004b2021-03-30 10:10:23 -07001328 throw new IllegalArgumentException(
1329 "Invalid phoneId " + phoneId + " for subId " + subscriptionId);
Hall Liu506724b2018-10-22 18:16:14 -07001330 }
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001331 // Post to run on handler thread on which all states should be confined.
1332 mHandler.post(() -> {
1333 overrideConfig(mOverrideConfigs, phoneId, overrides);
Hall Liu506724b2018-10-22 18:16:14 -07001334
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001335 if (persistent) {
1336 overrideConfig(mPersistentOverrideConfigs, phoneId, overrides);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001337
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001338 if (overrides != null) {
1339 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
1340 saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION,
1341 phoneId,
1342 carrierId, mPersistentOverrideConfigs[phoneId]);
1343 } else {
1344 final String iccid = getIccIdForPhoneId(phoneId);
1345 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1346 String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage,
1347 OVERRIDE_PACKAGE_ADDITION, iccid, cid);
1348 File fileToDelete = new File(mContext.getFilesDir(), fileName);
1349 fileToDelete.delete();
1350 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001351 }
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001352 notifySubscriptionInfoUpdater(phoneId);
1353 });
Hall Liu506724b2018-10-22 18:16:14 -07001354 }
1355
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001356 private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId,
1357 @Nullable PersistableBundle overrides) {
1358 if (overrides == null) {
Rambo Wang42026112021-04-07 20:57:28 +00001359 currentOverrides[phoneId] = new PersistableBundle();
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001360 } else if (currentOverrides[phoneId] == null) {
1361 currentOverrides[phoneId] = overrides;
1362 } else {
1363 currentOverrides[phoneId].putAll(overrides);
1364 }
1365 }
1366
Hall Liu506724b2018-10-22 18:16:14 -07001367 @Override
Rambo Wangd2b004b2021-03-30 10:10:23 -07001368 public void notifyConfigChangedForSubId(int subscriptionId) {
Jordan Liud7d57fe2019-04-16 12:29:43 -07001369 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001370 // system privileged app with MODIFY_PHONE_STATE permission.
Rambo Wangd2b004b2021-03-30 10:10:23 -07001371 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext,
1372 subscriptionId, "Require carrier privileges or MODIFY_PHONE_STATE permission.");
1373
1374 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1375 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1376 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
1377 throw new IllegalArgumentException(
1378 "Invalid phoneId " + phoneId + " for subId " + subscriptionId);
1379 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001380
1381 // This method should block until deleting has completed, so that an error which prevents us
1382 // from clearing the cache is passed back to the carrier app. With the files successfully
1383 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001384 String callingPackageName = mContext.getPackageManager().getNameForUid(
1385 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001386 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001387 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001388 }
1389
1390 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001391 public void updateConfigForPhoneId(int phoneId, @NonNull String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -07001392 mContext.enforceCallingOrSelfPermission(
1393 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001394 logdWithLocalLog("Update config for phoneId: " + phoneId + " simState: " + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001395 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001396 throw new IllegalArgumentException("Invalid phoneId: " + phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001397 }
1398 // requires Java 7 for switch on string.
1399 switch (simState) {
1400 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1401 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001402 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001403 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Malcolm Chendb66de12019-12-09 18:57:07 -08001404 case IccCardConstants.INTENT_VALUE_ICC_NOT_READY:
Junda Liu9f2d2712015-05-15 14:22:45 -07001405 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001406 break;
1407 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1408 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001409 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001410 break;
1411 }
1412 }
1413
Junda Liu43d723a2015-05-12 17:23:45 -07001414 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001415 @NonNull
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001416 public String getDefaultCarrierServicePackageName() {
Shuo Qianefdb7962019-12-19 15:54:27 -08001417 mContext.enforceCallingOrSelfPermission(
1418 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1419 "getDefaultCarrierServicePackageName");
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001420 return mPlatformCarrierConfigPackage;
1421 }
1422
Rambo Wang7e3bc122021-03-23 09:24:38 -07001423 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001424 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -07001425 /* package */ Handler getHandler() {
1426 return mHandler;
1427 }
1428
1429 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001430 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001431 /* package */ PersistableBundle getConfigFromDefaultApp(int phoneId) {
1432 return mConfigFromDefaultApp[phoneId];
1433 }
1434
1435 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001436 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001437 /* package */ PersistableBundle getConfigFromCarrierApp(int phoneId) {
1438 return mConfigFromCarrierApp[phoneId];
1439 }
1440
1441 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001442 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -07001443 /* package */ PersistableBundle getNoSimConfig() {
1444 return mNoSimConfig;
1445 }
1446
1447 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001448 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001449 /* package */ PersistableBundle getOverrideConfig(int phoneId) {
1450 return mOverrideConfigs[phoneId];
1451 }
1452
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001453 // TODO(b/185129900): always call unbindService after bind, no matter if it succeeded
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001454 private void unbindIfBound(@NonNull Context context, @NonNull CarrierServiceConnection conn,
Rambo Wangb0f3e2f2021-10-28 12:40:12 -07001455 int phoneId) {
1456 if (mServiceBound[phoneId]) {
1457 mServiceBound[phoneId] = false;
1458 context.unbindService(conn);
1459 }
1460 }
1461
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001462 private void unbindIfBoundForNoSimConfig(@NonNull Context context,
1463 @NonNull CarrierServiceConnection conn, int phoneId) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -07001464 if (mServiceBoundForNoSimConfig[phoneId]) {
1465 mServiceBoundForNoSimConfig[phoneId] = false;
Jayachandran C645ec612020-01-10 10:30:25 -08001466 context.unbindService(conn);
1467 }
1468 }
1469
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001470 /**
Rambo Wang610fdf62021-03-08 21:37:11 -08001471 * Returns a boxed Integer object for phoneId, services as message token to distinguish messages
1472 * with same code when calling {@link Handler#removeMessages(int, Object)}.
1473 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001474 @NonNull
Rambo Wang610fdf62021-03-08 21:37:11 -08001475 private Integer getMessageToken(int phoneId) {
1476 if (phoneId < -128 || phoneId > 127) {
1477 throw new IllegalArgumentException("phoneId should be in range [-128, 127], inclusive");
1478 }
1479 // Integer#valueOf guarantees the integers within [-128, 127] are cached and thus memory
1480 // comparison (==) returns true for the same integer.
1481 return Integer.valueOf(phoneId);
1482 }
1483
1484 /**
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001485 * If {@code args} contains {@link #DUMP_ARG_REQUESTING_PACKAGE} and a following package name,
1486 * we'll also call {@link IBinder#dump} on the default carrier service (if bound) and the
1487 * specified carrier service (if bound). Typically, this is done for connectivity bug reports
1488 * where we don't call {@code dumpsys activity service all-non-platform} because that contains
1489 * too much info, but we still want to let carrier apps include their diagnostics.
1490 */
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001491 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001492 public void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter pw, @NonNull String[] args) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001493 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
Junda Liu43d723a2015-05-12 17:23:45 -07001494 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1495 != PackageManager.PERMISSION_GRANTED) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001496 indentPW.println("Permission Denial: can't dump carrierconfig from from pid="
Junda Liu43d723a2015-05-12 17:23:45 -07001497 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1498 return;
1499 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001500 String requestingPackage = null;
1501 int requestingPackageIndex = ArrayUtils.indexOf(args, DUMP_ARG_REQUESTING_PACKAGE);
1502 if (requestingPackageIndex >= 0 && requestingPackageIndex < args.length - 1
1503 && !TextUtils.isEmpty(args[requestingPackageIndex + 1])) {
1504 requestingPackage = args[requestingPackageIndex + 1];
1505 // Throws a SecurityException if the caller is impersonating another app in an effort to
1506 // dump extra info (which may contain PII the caller doesn't have a right to).
1507 enforceCallerIsSystemOrRequestingPackage(requestingPackage);
1508 }
1509
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001510 indentPW.println("CarrierConfigLoader: " + this);
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001511 for (int i = 0; i < mNumPhones; i++) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001512 indentPW.println("Phone Id = " + i);
shuoq26a3a4c2016-12-16 11:06:48 -08001513 // display default values in CarrierConfigManager
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001514 printConfig(CarrierConfigManager.getDefaultConfig(), indentPW,
shuoq26a3a4c2016-12-16 11:06:48 -08001515 "Default Values from CarrierConfigManager");
shuoq26a3a4c2016-12-16 11:06:48 -08001516 // display ConfigFromDefaultApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001517 printConfig(mConfigFromDefaultApp[i], indentPW, "mConfigFromDefaultApp");
shuoq26a3a4c2016-12-16 11:06:48 -08001518 // display ConfigFromCarrierApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001519 printConfig(mConfigFromCarrierApp[i], indentPW, "mConfigFromCarrierApp");
1520 printConfig(mPersistentOverrideConfigs[i], indentPW, "mPersistentOverrideConfigs");
1521 printConfig(mOverrideConfigs[i], indentPW, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001522 }
chen xudb04c292019-03-26 17:01:15 -07001523
Jayachandran C645ec612020-01-10 10:30:25 -08001524 printConfig(mNoSimConfig, indentPW, "mNoSimConfig");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001525 indentPW.println("CarrierConfigLoadingLog=");
1526 mCarrierConfigLoadingLog.dump(fd, indentPW, args);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001527
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001528 if (requestingPackage != null) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001529 logd("Including default and requesting package " + requestingPackage
1530 + " carrier services in dump");
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001531 indentPW.println("");
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001532 indentPW.println("Connected services");
1533 dumpCarrierServiceIfBound(fd, indentPW, "Default config package",
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001534 mPlatformCarrierConfigPackage, false /* considerCarrierPrivileges */);
1535 dumpCarrierServiceIfBound(fd, indentPW, "Requesting package", requestingPackage,
1536 true /* considerCarrierPrivileges */);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001537 }
shuoq26a3a4c2016-12-16 11:06:48 -08001538 }
1539
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001540 private void printConfig(@NonNull PersistableBundle configApp,
1541 @NonNull IndentingPrintWriter indentPW, @NonNull String name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001542 indentPW.increaseIndent();
shuoq26a3a4c2016-12-16 11:06:48 -08001543 if (configApp == null) {
shuoq26a3a4c2016-12-16 11:06:48 -08001544 indentPW.println(name + " : null ");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001545 indentPW.decreaseIndent();
1546 indentPW.println("");
shuoq26a3a4c2016-12-16 11:06:48 -08001547 return;
1548 }
shuoq26a3a4c2016-12-16 11:06:48 -08001549 indentPW.println(name + " : ");
1550 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1551 Collections.sort(sortedKeys);
1552 indentPW.increaseIndent();
1553 indentPW.increaseIndent();
1554 for (String key : sortedKeys) {
1555 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1556 indentPW.println(key + " = " +
1557 Arrays.toString((Object[]) configApp.get(key)));
1558 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1559 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1560 } else {
1561 indentPW.println(key + " = " + configApp.get(key));
1562 }
Junda Liu43d723a2015-05-12 17:23:45 -07001563 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001564 indentPW.decreaseIndent();
1565 indentPW.decreaseIndent();
1566 indentPW.decreaseIndent();
1567 indentPW.println("");
Junda Liu43d723a2015-05-12 17:23:45 -07001568 }
1569
Hunter Knepshielde601f432020-02-19 10:16:04 -08001570 /**
1571 * Passes without problem when one of these conditions is true:
1572 * - The caller is a privileged UID (e.g. for dumpstate.cpp generating a bug report, where the
1573 * system knows the true caller plumbed in through the {@link android.os.BugreportManager} API).
1574 * - The caller's UID matches the supplied package.
1575 *
1576 * @throws SecurityException if none of the above conditions are met.
1577 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001578 private void enforceCallerIsSystemOrRequestingPackage(@NonNull String requestingPackage)
Hunter Knepshielde601f432020-02-19 10:16:04 -08001579 throws SecurityException {
1580 final int callingUid = Binder.getCallingUid();
1581 if (callingUid == Process.ROOT_UID || callingUid == Process.SYSTEM_UID
1582 || callingUid == Process.SHELL_UID || callingUid == Process.PHONE_UID) {
1583 // Bug reports (dumpstate.cpp) run as SHELL, and let some other privileged UIDs through
1584 // as well.
1585 return;
1586 }
1587 // An app is trying to dump extra detail, block it if they aren't who they claim to be.
1588 AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class);
1589 if (appOps == null) {
1590 throw new SecurityException("No AppOps");
1591 }
1592 // Will throw a SecurityException if the UID and package don't match.
1593 appOps.checkPackage(callingUid, requestingPackage);
1594 }
1595
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001596 /**
1597 * Searches for one or more appropriate {@link CarrierService} instances to dump based on the
1598 * current connections.
1599 *
1600 * @param targetPkgName the target package name to dump carrier services for
1601 * @param considerCarrierPrivileges if true, allow a carrier service to be dumped if it shares
1602 * carrier privileges with {@code targetPkgName};
1603 * otherwise, only dump a carrier service if it is {@code
1604 * targetPkgName}
1605 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001606 private void dumpCarrierServiceIfBound(@NonNull FileDescriptor fd,
1607 @NonNull IndentingPrintWriter indentPW, @NonNull String prefix,
1608 @NonNull String targetPkgName, boolean considerCarrierPrivileges) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001609 // Null package is possible if it's early in the boot process, there was a recent crash, we
1610 // loaded the config from XML most recently, or a SIM slot is empty. Carrier apps with
1611 // long-lived bindings should typically get dumped here regardless. Even if an app is being
1612 // used for multiple phoneIds, we assume that it's smart enough to handle that on its own,
1613 // and that in most cases we'd just be dumping duplicate information and bloating a report.
1614 indentPW.increaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001615 indentPW.println(prefix + " : " + targetPkgName);
1616 Set<String> dumpedPkgNames = new ArraySet<>(mServiceConnection.length);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001617 for (CarrierServiceConnection connection : mServiceConnection) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001618 if (connection == null || !SubscriptionManager.isValidPhoneId(connection.phoneId)
1619 || TextUtils.isEmpty(connection.pkgName)) {
1620 continue;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001621 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001622 final String servicePkgName = connection.pkgName;
1623 // Note: we intentionally ignore system components here because we should NOT match the
1624 // shell caller that's typically used for bug reports via non-BugreportManager triggers.
1625 final boolean exactPackageMatch = TextUtils.equals(targetPkgName, servicePkgName);
1626 final boolean carrierPrivilegesMatch =
1627 considerCarrierPrivileges && hasCarrierPrivileges(targetPkgName,
1628 connection.phoneId);
1629 if (!exactPackageMatch && !carrierPrivilegesMatch) continue;
1630 // Make sure this service is actually alive before trying to dump it. We don't pay
1631 // attention to mServiceBound[connection.phoneId] because typically carrier apps will
1632 // request long-lived bindings, and even if we unbind the app, it may still be alive due
1633 // to CarrierServiceBindHelper. Pull it out as a reference so even if it gets set to
1634 // null within the ServiceConnection during unbinding we can avoid an NPE.
1635 final IBinder service = connection.service;
1636 if (service == null || !service.isBinderAlive() || !service.pingBinder()) continue;
1637 // We've got a live service. Last check is just to make sure we don't dump a package
1638 // multiple times.
1639 if (!dumpedPkgNames.add(servicePkgName)) continue;
1640 if (!exactPackageMatch) {
1641 logd(targetPkgName + " has carrier privileges on phoneId " + connection.phoneId
1642 + ", service provided by " + servicePkgName);
1643 indentPW.increaseIndent();
1644 indentPW.println("Proxy : " + servicePkgName);
1645 indentPW.decreaseIndent();
1646 }
1647 // Flush before we let the app output anything to ensure correct ordering of output.
1648 // Internally, Binder#dump calls flush on its printer after finishing so we don't
1649 // need to do anything after.
1650 indentPW.flush();
1651 try {
1652 logd("Dumping " + servicePkgName);
1653 // We don't need to give the carrier service any args.
1654 connection.service.dump(fd, null /* args */);
1655 logd("Done with " + servicePkgName);
1656 } catch (RemoteException e) {
1657 logd("RemoteException from " + servicePkgName, e);
1658 indentPW.increaseIndent();
1659 indentPW.println("RemoteException");
1660 indentPW.increaseIndent();
1661 e.printStackTrace(indentPW);
1662 indentPW.decreaseIndent();
1663 indentPW.decreaseIndent();
1664 // We won't retry this package again because now it's in dumpedPkgNames.
1665 }
1666 indentPW.println("");
Jordan Liu46ed5db2020-01-28 08:55:20 -08001667 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001668 if (dumpedPkgNames.isEmpty()) {
1669 indentPW.increaseIndent();
1670 indentPW.println("Not bound");
1671 indentPW.decreaseIndent();
1672 indentPW.println("");
1673 }
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001674 indentPW.decreaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001675 }
1676
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001677 private boolean hasCarrierPrivileges(@NonNull String pkgName, int phoneId) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001678 int[] subIds = SubscriptionManager.getSubId(phoneId);
1679 if (ArrayUtils.isEmpty(subIds)) {
1680 return false;
1681 }
1682 return TelephonyManager.from(mContext).createForSubscriptionId(
1683 subIds[0]).checkCarrierPrivilegesForPackage(pkgName)
1684 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001685 }
1686
Zach Johnson36d7aab2015-05-22 15:43:00 -07001687 private class CarrierServiceConnection implements ServiceConnection {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001688 final int phoneId;
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001689 @NonNull final String pkgName;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001690 final int eventId;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001691 IBinder service;
1692
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001693 CarrierServiceConnection(int phoneId, @NonNull String pkgName, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001694 this.phoneId = phoneId;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001695 this.pkgName = pkgName;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001696 this.eventId = eventId;
1697 }
1698
1699 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001700 public void onServiceConnected(@NonNull ComponentName name, @NonNull IBinder service) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001701 logd("Connected to config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001702 this.service = service;
1703 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1704 }
1705
1706 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001707 public void onServiceDisconnected(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001708 logd("Disconnected from config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001709 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001710 }
1711
1712 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001713 public void onBindingDied(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001714 logd("Binding died from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001715 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001716 }
1717
1718 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001719 public void onNullBinding(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001720 logd("Null binding from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001721 this.service = null;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001722 }
1723 }
1724
1725 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1726 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001727 public void onReceive(@NonNull Context context, @NonNull Intent intent) {
Rambo Wanga27fbe52022-04-12 19:09:07 +00001728 switch (intent.getAction()) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001729 case Intent.ACTION_BOOT_COMPLETED:
1730 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1731 break;
1732
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001733 case TelephonyManager.ACTION_MULTI_SIM_CONFIG_CHANGED:
1734 mHandler.sendEmptyMessage(EVENT_MULTI_SIM_CONFIG_CHANGED);
1735 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001736 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001737 }
1738 }
1739
Rambo Wanga27fbe52022-04-12 19:09:07 +00001740 private class CarrierServiceChangeCallback implements
1741 TelephonyManager.CarrierPrivilegesCallback {
1742 final int mPhoneId;
1743 // CarrierPrivilegesCallback will be triggered upon registration. Filter the first callback
1744 // here since we really care of the *change* of carrier service instead of the content
1745 private boolean mHasSentServiceChangeCallback;
1746
1747 CarrierServiceChangeCallback(int phoneId) {
1748 this.mPhoneId = phoneId;
1749 this.mHasSentServiceChangeCallback = false;
1750 }
1751
1752 @Override
1753 public void onCarrierPrivilegesChanged(
1754 @androidx.annotation.NonNull Set<String> privilegedPackageNames,
1755 @androidx.annotation.NonNull Set<Integer> privilegedUids) {
1756 // Ignored, not interested here
1757 }
1758
1759 @Override
1760 public void onCarrierServiceChanged(
1761 @androidx.annotation.Nullable String carrierServicePackageName,
1762 int carrierServiceUid) {
1763 // Ignore the first callback which is triggered upon registration
1764 if (!mHasSentServiceChangeCallback) {
1765 mHasSentServiceChangeCallback = true;
1766 return;
1767 }
1768 mHandler.sendMessage(
1769 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, mPhoneId, -1,
1770 carrierServicePackageName));
1771 }
1772 }
1773
Rambo Wangab13e3e2021-04-08 15:01:06 -07001774 // Get readable string for the message code supported in this class.
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001775 @NonNull
Rambo Wangab13e3e2021-04-08 15:01:06 -07001776 private static String eventToString(int code) {
1777 switch (code) {
1778 case EVENT_CLEAR_CONFIG:
1779 return "EVENT_CLEAR_CONFIG";
1780 case EVENT_CONNECTED_TO_DEFAULT:
1781 return "EVENT_CONNECTED_TO_DEFAULT";
1782 case EVENT_CONNECTED_TO_CARRIER:
1783 return "EVENT_CONNECTED_TO_CARRIER";
1784 case EVENT_FETCH_DEFAULT_DONE:
1785 return "EVENT_FETCH_DEFAULT_DONE";
1786 case EVENT_FETCH_CARRIER_DONE:
1787 return "EVENT_FETCH_CARRIER_DONE";
1788 case EVENT_DO_FETCH_DEFAULT:
1789 return "EVENT_DO_FETCH_DEFAULT";
1790 case EVENT_DO_FETCH_CARRIER:
1791 return "EVENT_DO_FETCH_CARRIER";
1792 case EVENT_PACKAGE_CHANGED:
1793 return "EVENT_PACKAGE_CHANGED";
1794 case EVENT_BIND_DEFAULT_TIMEOUT:
1795 return "EVENT_BIND_DEFAULT_TIMEOUT";
1796 case EVENT_BIND_CARRIER_TIMEOUT:
1797 return "EVENT_BIND_CARRIER_TIMEOUT";
1798 case EVENT_CHECK_SYSTEM_UPDATE:
1799 return "EVENT_CHECK_SYSTEM_UPDATE";
1800 case EVENT_SYSTEM_UNLOCKED:
1801 return "EVENT_SYSTEM_UNLOCKED";
1802 case EVENT_FETCH_DEFAULT_TIMEOUT:
1803 return "EVENT_FETCH_DEFAULT_TIMEOUT";
1804 case EVENT_FETCH_CARRIER_TIMEOUT:
1805 return "EVENT_FETCH_CARRIER_TIMEOUT";
1806 case EVENT_SUBSCRIPTION_INFO_UPDATED:
1807 return "EVENT_SUBSCRIPTION_INFO_UPDATED";
1808 case EVENT_MULTI_SIM_CONFIG_CHANGED:
1809 return "EVENT_MULTI_SIM_CONFIG_CHANGED";
1810 case EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG:
1811 return "EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG";
1812 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE:
1813 return "EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE";
1814 case EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG:
1815 return "EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG";
1816 case EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
1817 return "EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT";
1818 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
1819 return "EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT";
1820 default:
1821 return "UNKNOWN(" + code + ")";
1822 }
1823 }
1824
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001825 private void logd(@NonNull String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001826 Log.d(LOG_TAG, msg);
1827 }
1828
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001829 private void logd(@NonNull String msg, Throwable tr) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001830 Log.d(LOG_TAG, msg, tr);
1831 }
1832
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001833 private void logdWithLocalLog(@NonNull String msg) {
chen xudb04c292019-03-26 17:01:15 -07001834 Log.d(LOG_TAG, msg);
1835 mCarrierConfigLoadingLog.log(msg);
1836 }
1837
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001838 private void loge(@NonNull String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001839 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07001840 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001841 }
1842}