blob: a270e0758be94fbc75dfb367ff1c0e3e40b9f81a [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;
Jack Yu00ece8c2022-11-19 22:29:12 -0800807 int subId = SubscriptionManager.getSubscriptionId(phoneId);
808 if (SubscriptionManager.isValidSubscriptionId(subId)) {
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800809 TelephonyManager telMgr = TelephonyManager.from(mContext)
Jack Yu00ece8c2022-11-19 22:29:12 -0800810 .createForSubscriptionId(subId);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800811 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 Yu00ece8c2022-11-19 22:29:12 -0800828 int subId = SubscriptionManager.getSubscriptionId(phoneId);
829 if (SubscriptionManager.isValidSubscriptionId(subId)) {
830 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId + ", subId=" + subId);
Jack Yue9e95be2020-03-22 10:56:06 -0700831 } 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) {
arunvoddub7c6e8e2022-09-05 15:48:06 +00001173 logd("Deleting " + getFilePathForLogging(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
arunvoddub7c6e8e2022-09-05 15:48:06 +00001179 private String getFilePathForLogging(String filePath) {
1180 if (!TextUtils.isEmpty(filePath)) {
1181 String[] fileTokens = filePath.split("-");
1182 if (fileTokens != null && fileTokens.length > 2) {
1183 String iccid = fileTokens[fileTokens.length -2];
1184 return getFilePathForLogging(filePath, iccid);
1185 }
1186 return filePath;
1187 }
1188 return filePath;
1189 }
1190
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001191 /** Builds a canonical file name for a config file. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001192 @NonNull
Jayachandran C645ec612020-01-10 10:30:25 -08001193 private static String getFilenameForConfig(
1194 @NonNull String packageName, @NonNull String extraString,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001195 @NonNull String iccid, int cid) {
chen xu02581692018-11-11 19:03:44 -08001196 // the same carrier should have a single copy of XML file named after carrier id.
1197 // However, it's still possible that platform doesn't recognize the current sim carrier,
1198 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
1199 // cases SIM OTA resolves to different carrier while iccid remains the same.
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001200 return "carrierconfig-" + packageName + extraString + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001201 }
1202
Jayachandran C645ec612020-01-10 10:30:25 -08001203 /** Builds a canonical file name for no SIM config file. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001204 @NonNull
Jayachandran C645ec612020-01-10 10:30:25 -08001205 private String getFilenameForNoSimConfig(@NonNull String packageName) {
1206 return "carrierconfig-" + packageName + "-" + "nosim" + ".xml";
1207 }
1208
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001209 /** Return the current version code of a package, or null if the name is not found. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001210 @Nullable
1211 private String getPackageVersion(@NonNull String packageName) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001212 try {
1213 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -08001214 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001215 } catch (PackageManager.NameNotFoundException e) {
1216 return null;
1217 }
1218 }
1219
Jonathan Basseri65273c82017-07-25 15:08:42 -07001220 /**
1221 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001222 *
1223 * This reads config bundles for the given phoneId. That means getting the latest bundle from
1224 * the default app and a privileged carrier app, if present. This will not bind to an app if we
1225 * have a saved config file to use instead.
1226 */
1227 private void updateConfigForPhoneId(int phoneId) {
Jonathan Basseri65273c82017-07-25 15:08:42 -07001228 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001229 }
1230
Malcolm Chen5ea18532019-10-24 19:55:44 -07001231 private void onMultiSimConfigChanged() {
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001232 int oldNumPhones = mNumPhones;
1233 mNumPhones = TelephonyManager.from(mContext).getActiveModemCount();
1234 if (mNumPhones == oldNumPhones) {
1235 return;
1236 }
1237 logdWithLocalLog("mNumPhones change from " + oldNumPhones + " to " + mNumPhones);
1238
Rambo Wanga27fbe52022-04-12 19:09:07 +00001239 // If DS -> SS switch, release the resources BEFORE truncating the arrays to avoid leaking
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001240 for (int phoneId = mNumPhones; phoneId < oldNumPhones; phoneId++) {
1241 if (mServiceConnection[phoneId] != null) {
1242 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
1243 }
1244 if (mServiceConnectionForNoSimConfig[phoneId] != null) {
1245 unbindIfBoundForNoSimConfig(mContext, mServiceConnectionForNoSimConfig[phoneId],
1246 phoneId);
1247 }
1248 }
1249
Rambo Wanga27fbe52022-04-12 19:09:07 +00001250 // The phone to slot mapping may change, unregister here and re-register callbacks later
1251 for (int phoneId = 0; phoneId < oldNumPhones; phoneId++) {
1252 if (mCarrierServiceChangeCallbacks[phoneId] != null) {
1253 TelephonyManager.from(mContext).unregisterCarrierPrivilegesCallback(
1254 mCarrierServiceChangeCallbacks[phoneId]);
1255 }
1256 }
1257
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001258 // Copy the original arrays, truncate or padding with zeros (if necessary) to new length
1259 mConfigFromDefaultApp = Arrays.copyOf(mConfigFromDefaultApp, mNumPhones);
1260 mConfigFromCarrierApp = Arrays.copyOf(mConfigFromCarrierApp, mNumPhones);
1261 mPersistentOverrideConfigs = Arrays.copyOf(mPersistentOverrideConfigs, mNumPhones);
1262 mOverrideConfigs = Arrays.copyOf(mOverrideConfigs, mNumPhones);
1263 mServiceConnection = Arrays.copyOf(mServiceConnection, mNumPhones);
1264 mServiceConnectionForNoSimConfig =
1265 Arrays.copyOf(mServiceConnectionForNoSimConfig, mNumPhones);
1266 mServiceBound = Arrays.copyOf(mServiceBound, mNumPhones);
1267 mServiceBoundForNoSimConfig = Arrays.copyOf(mServiceBoundForNoSimConfig, mNumPhones);
1268 mHasSentConfigChange = Arrays.copyOf(mHasSentConfigChange, mNumPhones);
1269 mFromSystemUnlocked = Arrays.copyOf(mFromSystemUnlocked, mNumPhones);
Rambo Wanga27fbe52022-04-12 19:09:07 +00001270 mCarrierServiceChangeCallbacks = Arrays.copyOf(mCarrierServiceChangeCallbacks, mNumPhones);
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001271
Rambo Wanga27fbe52022-04-12 19:09:07 +00001272 // Load the config for all the phones and re-register callback AFTER padding the arrays.
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001273 for (int phoneId = 0; phoneId < mNumPhones; phoneId++) {
1274 updateConfigForPhoneId(phoneId);
Rambo Wanga27fbe52022-04-12 19:09:07 +00001275 mCarrierServiceChangeCallbacks[phoneId] = new CarrierServiceChangeCallback(phoneId);
1276 TelephonyManager.from(mContext).registerCarrierPrivilegesCallback(phoneId,
1277 new HandlerExecutor(mHandler), mCarrierServiceChangeCallbacks[phoneId]);
Malcolm Chen5ea18532019-10-24 19:55:44 -07001278 }
1279 }
1280
Hall Liu506724b2018-10-22 18:16:14 -07001281 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001282 @NonNull
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001283 public PersistableBundle getConfigForSubId(int subscriptionId, @NonNull String callingPackage) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001284 return getConfigForSubIdWithFeature(subscriptionId, callingPackage, null);
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08001285 }
1286
1287 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001288 @NonNull
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001289 public PersistableBundle getConfigForSubIdWithFeature(int subscriptionId,
1290 @NonNull String callingPackage, @Nullable String callingFeatureId) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001291 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subscriptionId,
1292 callingPackage, callingFeatureId, "getCarrierConfig")) {
Rambo Wang42026112021-04-07 20:57:28 +00001293 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -07001294 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -07001295
Rambo Wangd2b004b2021-03-30 10:10:23 -07001296 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001297 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001298 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001299 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001300 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001301 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001302 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001303 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001304 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001305 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001306 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001307 config = mPersistentOverrideConfigs[phoneId];
1308 if (config != null) {
1309 retConfig.putAll(config);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001310 }
Hall Liu506724b2018-10-22 18:16:14 -07001311 config = mOverrideConfigs[phoneId];
1312 if (config != null) {
1313 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -07001314 }
Nathan Harold1122e3d2021-01-22 15:45:24 -08001315 // Ignore the theoretical case of the default app not being present since that won't
1316 // work in CarrierConfigLoader today.
1317 final boolean allConfigsApplied =
1318 (mConfigFromCarrierApp[phoneId] != null
1319 || getCarrierPackageForPhoneId(phoneId) == null)
1320 && mConfigFromDefaultApp[phoneId] != null;
1321 retConfig.putBoolean(
1322 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, allConfigsApplied);
Jayachandran C645ec612020-01-10 10:30:25 -08001323 } else {
1324 if (mNoSimConfig != null) {
1325 retConfig.putAll(mNoSimConfig);
1326 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001327 }
1328 return retConfig;
1329 }
1330
1331 @Override
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001332 public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides,
1333 boolean persistent) {
Hall Liu506724b2018-10-22 18:16:14 -07001334 mContext.enforceCallingOrSelfPermission(
1335 android.Manifest.permission.MODIFY_PHONE_STATE, null);
1336 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1337 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001338 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
Rambo Wangd2b004b2021-03-30 10:10:23 -07001339 throw new IllegalArgumentException(
1340 "Invalid phoneId " + phoneId + " for subId " + subscriptionId);
Hall Liu506724b2018-10-22 18:16:14 -07001341 }
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001342 // Post to run on handler thread on which all states should be confined.
1343 mHandler.post(() -> {
1344 overrideConfig(mOverrideConfigs, phoneId, overrides);
Hall Liu506724b2018-10-22 18:16:14 -07001345
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001346 if (persistent) {
1347 overrideConfig(mPersistentOverrideConfigs, phoneId, overrides);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001348
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001349 if (overrides != null) {
1350 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
1351 saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION,
1352 phoneId,
1353 carrierId, mPersistentOverrideConfigs[phoneId]);
1354 } else {
1355 final String iccid = getIccIdForPhoneId(phoneId);
1356 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1357 String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage,
1358 OVERRIDE_PACKAGE_ADDITION, iccid, cid);
1359 File fileToDelete = new File(mContext.getFilesDir(), fileName);
1360 fileToDelete.delete();
1361 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001362 }
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001363 notifySubscriptionInfoUpdater(phoneId);
1364 });
Hall Liu506724b2018-10-22 18:16:14 -07001365 }
1366
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001367 private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId,
1368 @Nullable PersistableBundle overrides) {
1369 if (overrides == null) {
Rambo Wang42026112021-04-07 20:57:28 +00001370 currentOverrides[phoneId] = new PersistableBundle();
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001371 } else if (currentOverrides[phoneId] == null) {
1372 currentOverrides[phoneId] = overrides;
1373 } else {
1374 currentOverrides[phoneId].putAll(overrides);
1375 }
1376 }
1377
Hall Liu506724b2018-10-22 18:16:14 -07001378 @Override
Rambo Wangd2b004b2021-03-30 10:10:23 -07001379 public void notifyConfigChangedForSubId(int subscriptionId) {
Jordan Liud7d57fe2019-04-16 12:29:43 -07001380 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001381 // system privileged app with MODIFY_PHONE_STATE permission.
Rambo Wangd2b004b2021-03-30 10:10:23 -07001382 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext,
1383 subscriptionId, "Require carrier privileges or MODIFY_PHONE_STATE permission.");
1384
1385 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1386 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1387 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
1388 throw new IllegalArgumentException(
1389 "Invalid phoneId " + phoneId + " for subId " + subscriptionId);
1390 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001391
1392 // This method should block until deleting has completed, so that an error which prevents us
1393 // from clearing the cache is passed back to the carrier app. With the files successfully
1394 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001395 String callingPackageName = mContext.getPackageManager().getNameForUid(
1396 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001397 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001398 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001399 }
1400
1401 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001402 public void updateConfigForPhoneId(int phoneId, @NonNull String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -07001403 mContext.enforceCallingOrSelfPermission(
1404 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001405 logdWithLocalLog("Update config for phoneId: " + phoneId + " simState: " + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001406 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001407 throw new IllegalArgumentException("Invalid phoneId: " + phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001408 }
1409 // requires Java 7 for switch on string.
1410 switch (simState) {
1411 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1412 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001413 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001414 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Malcolm Chendb66de12019-12-09 18:57:07 -08001415 case IccCardConstants.INTENT_VALUE_ICC_NOT_READY:
Junda Liu9f2d2712015-05-15 14:22:45 -07001416 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001417 break;
1418 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1419 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001420 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001421 break;
1422 }
1423 }
1424
Junda Liu43d723a2015-05-12 17:23:45 -07001425 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001426 @NonNull
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001427 public String getDefaultCarrierServicePackageName() {
Shuo Qianefdb7962019-12-19 15:54:27 -08001428 mContext.enforceCallingOrSelfPermission(
1429 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1430 "getDefaultCarrierServicePackageName");
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001431 return mPlatformCarrierConfigPackage;
1432 }
1433
Rambo Wang7e3bc122021-03-23 09:24:38 -07001434 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001435 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -07001436 /* package */ Handler getHandler() {
1437 return mHandler;
1438 }
1439
1440 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001441 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001442 /* package */ PersistableBundle getConfigFromDefaultApp(int phoneId) {
1443 return mConfigFromDefaultApp[phoneId];
1444 }
1445
1446 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001447 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001448 /* package */ PersistableBundle getConfigFromCarrierApp(int phoneId) {
1449 return mConfigFromCarrierApp[phoneId];
1450 }
1451
1452 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001453 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -07001454 /* package */ PersistableBundle getNoSimConfig() {
1455 return mNoSimConfig;
1456 }
1457
1458 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001459 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001460 /* package */ PersistableBundle getOverrideConfig(int phoneId) {
1461 return mOverrideConfigs[phoneId];
1462 }
1463
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001464 // TODO(b/185129900): always call unbindService after bind, no matter if it succeeded
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001465 private void unbindIfBound(@NonNull Context context, @NonNull CarrierServiceConnection conn,
Rambo Wangb0f3e2f2021-10-28 12:40:12 -07001466 int phoneId) {
1467 if (mServiceBound[phoneId]) {
1468 mServiceBound[phoneId] = false;
1469 context.unbindService(conn);
1470 }
1471 }
1472
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001473 private void unbindIfBoundForNoSimConfig(@NonNull Context context,
1474 @NonNull CarrierServiceConnection conn, int phoneId) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -07001475 if (mServiceBoundForNoSimConfig[phoneId]) {
1476 mServiceBoundForNoSimConfig[phoneId] = false;
Jayachandran C645ec612020-01-10 10:30:25 -08001477 context.unbindService(conn);
1478 }
1479 }
1480
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001481 /**
Rambo Wang610fdf62021-03-08 21:37:11 -08001482 * Returns a boxed Integer object for phoneId, services as message token to distinguish messages
1483 * with same code when calling {@link Handler#removeMessages(int, Object)}.
1484 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001485 @NonNull
Rambo Wang610fdf62021-03-08 21:37:11 -08001486 private Integer getMessageToken(int phoneId) {
1487 if (phoneId < -128 || phoneId > 127) {
1488 throw new IllegalArgumentException("phoneId should be in range [-128, 127], inclusive");
1489 }
1490 // Integer#valueOf guarantees the integers within [-128, 127] are cached and thus memory
1491 // comparison (==) returns true for the same integer.
1492 return Integer.valueOf(phoneId);
1493 }
1494
1495 /**
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001496 * If {@code args} contains {@link #DUMP_ARG_REQUESTING_PACKAGE} and a following package name,
1497 * we'll also call {@link IBinder#dump} on the default carrier service (if bound) and the
1498 * specified carrier service (if bound). Typically, this is done for connectivity bug reports
1499 * where we don't call {@code dumpsys activity service all-non-platform} because that contains
1500 * too much info, but we still want to let carrier apps include their diagnostics.
1501 */
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001502 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001503 public void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter pw, @NonNull String[] args) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001504 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
Junda Liu43d723a2015-05-12 17:23:45 -07001505 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1506 != PackageManager.PERMISSION_GRANTED) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001507 indentPW.println("Permission Denial: can't dump carrierconfig from from pid="
Junda Liu43d723a2015-05-12 17:23:45 -07001508 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1509 return;
1510 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001511 String requestingPackage = null;
1512 int requestingPackageIndex = ArrayUtils.indexOf(args, DUMP_ARG_REQUESTING_PACKAGE);
1513 if (requestingPackageIndex >= 0 && requestingPackageIndex < args.length - 1
1514 && !TextUtils.isEmpty(args[requestingPackageIndex + 1])) {
1515 requestingPackage = args[requestingPackageIndex + 1];
1516 // Throws a SecurityException if the caller is impersonating another app in an effort to
1517 // dump extra info (which may contain PII the caller doesn't have a right to).
1518 enforceCallerIsSystemOrRequestingPackage(requestingPackage);
1519 }
1520
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001521 indentPW.println("CarrierConfigLoader: " + this);
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001522 for (int i = 0; i < mNumPhones; i++) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001523 indentPW.println("Phone Id = " + i);
shuoq26a3a4c2016-12-16 11:06:48 -08001524 // display default values in CarrierConfigManager
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001525 printConfig(CarrierConfigManager.getDefaultConfig(), indentPW,
shuoq26a3a4c2016-12-16 11:06:48 -08001526 "Default Values from CarrierConfigManager");
shuoq26a3a4c2016-12-16 11:06:48 -08001527 // display ConfigFromDefaultApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001528 printConfig(mConfigFromDefaultApp[i], indentPW, "mConfigFromDefaultApp");
shuoq26a3a4c2016-12-16 11:06:48 -08001529 // display ConfigFromCarrierApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001530 printConfig(mConfigFromCarrierApp[i], indentPW, "mConfigFromCarrierApp");
1531 printConfig(mPersistentOverrideConfigs[i], indentPW, "mPersistentOverrideConfigs");
1532 printConfig(mOverrideConfigs[i], indentPW, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001533 }
chen xudb04c292019-03-26 17:01:15 -07001534
Jayachandran C645ec612020-01-10 10:30:25 -08001535 printConfig(mNoSimConfig, indentPW, "mNoSimConfig");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001536 indentPW.println("CarrierConfigLoadingLog=");
1537 mCarrierConfigLoadingLog.dump(fd, indentPW, args);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001538
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001539 if (requestingPackage != null) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001540 logd("Including default and requesting package " + requestingPackage
1541 + " carrier services in dump");
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001542 indentPW.println("");
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001543 indentPW.println("Connected services");
1544 dumpCarrierServiceIfBound(fd, indentPW, "Default config package",
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001545 mPlatformCarrierConfigPackage, false /* considerCarrierPrivileges */);
1546 dumpCarrierServiceIfBound(fd, indentPW, "Requesting package", requestingPackage,
1547 true /* considerCarrierPrivileges */);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001548 }
shuoq26a3a4c2016-12-16 11:06:48 -08001549 }
1550
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001551 private void printConfig(@NonNull PersistableBundle configApp,
1552 @NonNull IndentingPrintWriter indentPW, @NonNull String name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001553 indentPW.increaseIndent();
shuoq26a3a4c2016-12-16 11:06:48 -08001554 if (configApp == null) {
shuoq26a3a4c2016-12-16 11:06:48 -08001555 indentPW.println(name + " : null ");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001556 indentPW.decreaseIndent();
1557 indentPW.println("");
shuoq26a3a4c2016-12-16 11:06:48 -08001558 return;
1559 }
shuoq26a3a4c2016-12-16 11:06:48 -08001560 indentPW.println(name + " : ");
1561 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1562 Collections.sort(sortedKeys);
1563 indentPW.increaseIndent();
1564 indentPW.increaseIndent();
1565 for (String key : sortedKeys) {
1566 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1567 indentPW.println(key + " = " +
1568 Arrays.toString((Object[]) configApp.get(key)));
1569 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1570 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1571 } else {
1572 indentPW.println(key + " = " + configApp.get(key));
1573 }
Junda Liu43d723a2015-05-12 17:23:45 -07001574 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001575 indentPW.decreaseIndent();
1576 indentPW.decreaseIndent();
1577 indentPW.decreaseIndent();
1578 indentPW.println("");
Junda Liu43d723a2015-05-12 17:23:45 -07001579 }
1580
Hunter Knepshielde601f432020-02-19 10:16:04 -08001581 /**
1582 * Passes without problem when one of these conditions is true:
1583 * - The caller is a privileged UID (e.g. for dumpstate.cpp generating a bug report, where the
1584 * system knows the true caller plumbed in through the {@link android.os.BugreportManager} API).
1585 * - The caller's UID matches the supplied package.
1586 *
1587 * @throws SecurityException if none of the above conditions are met.
1588 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001589 private void enforceCallerIsSystemOrRequestingPackage(@NonNull String requestingPackage)
Hunter Knepshielde601f432020-02-19 10:16:04 -08001590 throws SecurityException {
1591 final int callingUid = Binder.getCallingUid();
1592 if (callingUid == Process.ROOT_UID || callingUid == Process.SYSTEM_UID
1593 || callingUid == Process.SHELL_UID || callingUid == Process.PHONE_UID) {
1594 // Bug reports (dumpstate.cpp) run as SHELL, and let some other privileged UIDs through
1595 // as well.
1596 return;
1597 }
1598 // An app is trying to dump extra detail, block it if they aren't who they claim to be.
1599 AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class);
1600 if (appOps == null) {
1601 throw new SecurityException("No AppOps");
1602 }
1603 // Will throw a SecurityException if the UID and package don't match.
1604 appOps.checkPackage(callingUid, requestingPackage);
1605 }
1606
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001607 /**
1608 * Searches for one or more appropriate {@link CarrierService} instances to dump based on the
1609 * current connections.
1610 *
1611 * @param targetPkgName the target package name to dump carrier services for
1612 * @param considerCarrierPrivileges if true, allow a carrier service to be dumped if it shares
1613 * carrier privileges with {@code targetPkgName};
1614 * otherwise, only dump a carrier service if it is {@code
1615 * targetPkgName}
1616 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001617 private void dumpCarrierServiceIfBound(@NonNull FileDescriptor fd,
1618 @NonNull IndentingPrintWriter indentPW, @NonNull String prefix,
1619 @NonNull String targetPkgName, boolean considerCarrierPrivileges) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001620 // Null package is possible if it's early in the boot process, there was a recent crash, we
1621 // loaded the config from XML most recently, or a SIM slot is empty. Carrier apps with
1622 // long-lived bindings should typically get dumped here regardless. Even if an app is being
1623 // used for multiple phoneIds, we assume that it's smart enough to handle that on its own,
1624 // and that in most cases we'd just be dumping duplicate information and bloating a report.
1625 indentPW.increaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001626 indentPW.println(prefix + " : " + targetPkgName);
1627 Set<String> dumpedPkgNames = new ArraySet<>(mServiceConnection.length);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001628 for (CarrierServiceConnection connection : mServiceConnection) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001629 if (connection == null || !SubscriptionManager.isValidPhoneId(connection.phoneId)
1630 || TextUtils.isEmpty(connection.pkgName)) {
1631 continue;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001632 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001633 final String servicePkgName = connection.pkgName;
1634 // Note: we intentionally ignore system components here because we should NOT match the
1635 // shell caller that's typically used for bug reports via non-BugreportManager triggers.
1636 final boolean exactPackageMatch = TextUtils.equals(targetPkgName, servicePkgName);
1637 final boolean carrierPrivilegesMatch =
1638 considerCarrierPrivileges && hasCarrierPrivileges(targetPkgName,
1639 connection.phoneId);
1640 if (!exactPackageMatch && !carrierPrivilegesMatch) continue;
1641 // Make sure this service is actually alive before trying to dump it. We don't pay
1642 // attention to mServiceBound[connection.phoneId] because typically carrier apps will
1643 // request long-lived bindings, and even if we unbind the app, it may still be alive due
1644 // to CarrierServiceBindHelper. Pull it out as a reference so even if it gets set to
1645 // null within the ServiceConnection during unbinding we can avoid an NPE.
1646 final IBinder service = connection.service;
1647 if (service == null || !service.isBinderAlive() || !service.pingBinder()) continue;
1648 // We've got a live service. Last check is just to make sure we don't dump a package
1649 // multiple times.
1650 if (!dumpedPkgNames.add(servicePkgName)) continue;
1651 if (!exactPackageMatch) {
1652 logd(targetPkgName + " has carrier privileges on phoneId " + connection.phoneId
1653 + ", service provided by " + servicePkgName);
1654 indentPW.increaseIndent();
1655 indentPW.println("Proxy : " + servicePkgName);
1656 indentPW.decreaseIndent();
1657 }
1658 // Flush before we let the app output anything to ensure correct ordering of output.
1659 // Internally, Binder#dump calls flush on its printer after finishing so we don't
1660 // need to do anything after.
1661 indentPW.flush();
1662 try {
1663 logd("Dumping " + servicePkgName);
1664 // We don't need to give the carrier service any args.
1665 connection.service.dump(fd, null /* args */);
1666 logd("Done with " + servicePkgName);
1667 } catch (RemoteException e) {
1668 logd("RemoteException from " + servicePkgName, e);
1669 indentPW.increaseIndent();
1670 indentPW.println("RemoteException");
1671 indentPW.increaseIndent();
1672 e.printStackTrace(indentPW);
1673 indentPW.decreaseIndent();
1674 indentPW.decreaseIndent();
1675 // We won't retry this package again because now it's in dumpedPkgNames.
1676 }
1677 indentPW.println("");
Jordan Liu46ed5db2020-01-28 08:55:20 -08001678 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001679 if (dumpedPkgNames.isEmpty()) {
1680 indentPW.increaseIndent();
1681 indentPW.println("Not bound");
1682 indentPW.decreaseIndent();
1683 indentPW.println("");
1684 }
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001685 indentPW.decreaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001686 }
1687
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001688 private boolean hasCarrierPrivileges(@NonNull String pkgName, int phoneId) {
Jack Yu00ece8c2022-11-19 22:29:12 -08001689 int subId = SubscriptionManager.getSubscriptionId(phoneId);
1690 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001691 return false;
1692 }
Jack Yu00ece8c2022-11-19 22:29:12 -08001693 return TelephonyManager.from(mContext).createForSubscriptionId(subId)
1694 .checkCarrierPrivilegesForPackage(pkgName)
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001695 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001696 }
1697
Zach Johnson36d7aab2015-05-22 15:43:00 -07001698 private class CarrierServiceConnection implements ServiceConnection {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001699 final int phoneId;
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001700 @NonNull final String pkgName;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001701 final int eventId;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001702 IBinder service;
1703
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001704 CarrierServiceConnection(int phoneId, @NonNull String pkgName, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001705 this.phoneId = phoneId;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001706 this.pkgName = pkgName;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001707 this.eventId = eventId;
1708 }
1709
1710 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001711 public void onServiceConnected(@NonNull ComponentName name, @NonNull IBinder service) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001712 logd("Connected to config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001713 this.service = service;
1714 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1715 }
1716
1717 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001718 public void onServiceDisconnected(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001719 logd("Disconnected from config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001720 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001721 }
1722
1723 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001724 public void onBindingDied(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001725 logd("Binding died from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001726 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001727 }
1728
1729 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001730 public void onNullBinding(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001731 logd("Null binding from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001732 this.service = null;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001733 }
1734 }
1735
1736 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1737 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001738 public void onReceive(@NonNull Context context, @NonNull Intent intent) {
Rambo Wanga27fbe52022-04-12 19:09:07 +00001739 switch (intent.getAction()) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001740 case Intent.ACTION_BOOT_COMPLETED:
1741 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1742 break;
1743
Rambo Wang8f16f3e2022-04-15 01:26:23 +00001744 case TelephonyManager.ACTION_MULTI_SIM_CONFIG_CHANGED:
1745 mHandler.sendEmptyMessage(EVENT_MULTI_SIM_CONFIG_CHANGED);
1746 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001747 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001748 }
1749 }
1750
Rambo Wanga27fbe52022-04-12 19:09:07 +00001751 private class CarrierServiceChangeCallback implements
1752 TelephonyManager.CarrierPrivilegesCallback {
1753 final int mPhoneId;
1754 // CarrierPrivilegesCallback will be triggered upon registration. Filter the first callback
1755 // here since we really care of the *change* of carrier service instead of the content
1756 private boolean mHasSentServiceChangeCallback;
1757
1758 CarrierServiceChangeCallback(int phoneId) {
1759 this.mPhoneId = phoneId;
1760 this.mHasSentServiceChangeCallback = false;
1761 }
1762
1763 @Override
1764 public void onCarrierPrivilegesChanged(
1765 @androidx.annotation.NonNull Set<String> privilegedPackageNames,
1766 @androidx.annotation.NonNull Set<Integer> privilegedUids) {
1767 // Ignored, not interested here
1768 }
1769
1770 @Override
1771 public void onCarrierServiceChanged(
1772 @androidx.annotation.Nullable String carrierServicePackageName,
1773 int carrierServiceUid) {
1774 // Ignore the first callback which is triggered upon registration
1775 if (!mHasSentServiceChangeCallback) {
1776 mHasSentServiceChangeCallback = true;
1777 return;
1778 }
1779 mHandler.sendMessage(
1780 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, mPhoneId, -1,
1781 carrierServicePackageName));
1782 }
1783 }
1784
Rambo Wangab13e3e2021-04-08 15:01:06 -07001785 // Get readable string for the message code supported in this class.
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001786 @NonNull
Rambo Wangab13e3e2021-04-08 15:01:06 -07001787 private static String eventToString(int code) {
1788 switch (code) {
1789 case EVENT_CLEAR_CONFIG:
1790 return "EVENT_CLEAR_CONFIG";
1791 case EVENT_CONNECTED_TO_DEFAULT:
1792 return "EVENT_CONNECTED_TO_DEFAULT";
1793 case EVENT_CONNECTED_TO_CARRIER:
1794 return "EVENT_CONNECTED_TO_CARRIER";
1795 case EVENT_FETCH_DEFAULT_DONE:
1796 return "EVENT_FETCH_DEFAULT_DONE";
1797 case EVENT_FETCH_CARRIER_DONE:
1798 return "EVENT_FETCH_CARRIER_DONE";
1799 case EVENT_DO_FETCH_DEFAULT:
1800 return "EVENT_DO_FETCH_DEFAULT";
1801 case EVENT_DO_FETCH_CARRIER:
1802 return "EVENT_DO_FETCH_CARRIER";
1803 case EVENT_PACKAGE_CHANGED:
1804 return "EVENT_PACKAGE_CHANGED";
1805 case EVENT_BIND_DEFAULT_TIMEOUT:
1806 return "EVENT_BIND_DEFAULT_TIMEOUT";
1807 case EVENT_BIND_CARRIER_TIMEOUT:
1808 return "EVENT_BIND_CARRIER_TIMEOUT";
1809 case EVENT_CHECK_SYSTEM_UPDATE:
1810 return "EVENT_CHECK_SYSTEM_UPDATE";
1811 case EVENT_SYSTEM_UNLOCKED:
1812 return "EVENT_SYSTEM_UNLOCKED";
1813 case EVENT_FETCH_DEFAULT_TIMEOUT:
1814 return "EVENT_FETCH_DEFAULT_TIMEOUT";
1815 case EVENT_FETCH_CARRIER_TIMEOUT:
1816 return "EVENT_FETCH_CARRIER_TIMEOUT";
1817 case EVENT_SUBSCRIPTION_INFO_UPDATED:
1818 return "EVENT_SUBSCRIPTION_INFO_UPDATED";
1819 case EVENT_MULTI_SIM_CONFIG_CHANGED:
1820 return "EVENT_MULTI_SIM_CONFIG_CHANGED";
1821 case EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG:
1822 return "EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG";
1823 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE:
1824 return "EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE";
1825 case EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG:
1826 return "EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG";
1827 case EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
1828 return "EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT";
1829 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
1830 return "EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT";
1831 default:
1832 return "UNKNOWN(" + code + ")";
1833 }
1834 }
1835
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001836 private void logd(@NonNull String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001837 Log.d(LOG_TAG, msg);
1838 }
1839
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001840 private void logd(@NonNull String msg, Throwable tr) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001841 Log.d(LOG_TAG, msg, tr);
1842 }
1843
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001844 private void logdWithLocalLog(@NonNull String msg) {
chen xudb04c292019-03-26 17:01:15 -07001845 Log.d(LOG_TAG, msg);
1846 mCarrierConfigLoadingLog.log(msg);
1847 }
1848
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001849 private void loge(@NonNull String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001850 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07001851 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001852 }
1853}