blob: 2ec491b42df45fe6c92b7c5865a76abfb120b563 [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;
38import android.os.IBinder;
Rambo Wang7e3bc122021-03-23 09:24:38 -070039import android.os.Looper;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080040import android.os.Message;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070041import android.os.PersistableBundle;
Hunter Knepshielde601f432020-02-19 10:16:04 -080042import android.os.Process;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080043import android.os.RemoteException;
Jonathan Basseri65273c82017-07-25 15:08:42 -070044import android.os.ResultReceiver;
Meng Wang97a6a462020-01-23 16:22:16 -080045import android.os.UserHandle;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070046import android.preference.PreferenceManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080047import android.service.carrier.CarrierIdentifier;
Zach Johnson36d7aab2015-05-22 15:43:00 -070048import android.service.carrier.CarrierService;
49import android.service.carrier.ICarrierService;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080050import android.telephony.CarrierConfigManager;
51import android.telephony.SubscriptionManager;
Peter Wangc035ce42020-01-08 21:00:22 -080052import android.telephony.TelephonyFrameworkInitializer;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080053import android.telephony.TelephonyManager;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -080054import android.text.TextUtils;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080055import android.util.ArraySet;
chen xudb04c292019-03-26 17:01:15 -070056import android.util.LocalLog;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080057import android.util.Log;
58
Rambo Wang7e3bc122021-03-23 09:24:38 -070059import com.android.internal.annotations.VisibleForTesting;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080060import com.android.internal.telephony.ICarrierConfigLoader;
61import com.android.internal.telephony.IccCardConstants;
62import com.android.internal.telephony.Phone;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080063import com.android.internal.telephony.PhoneFactory;
Nathan Harold48ac0972019-03-13 22:33:01 -070064import com.android.internal.telephony.SubscriptionInfoUpdater;
Jeff Davidsona8e4e242018-03-15 17:16:18 -070065import com.android.internal.telephony.TelephonyPermissions;
Meng Wanga320b942019-11-25 11:21:26 -080066import com.android.internal.telephony.util.ArrayUtils;
shuoq26a3a4c2016-12-16 11:06:48 -080067import com.android.internal.util.IndentingPrintWriter;
arunvoddu664c36a2021-10-11 20:09:28 +000068import com.android.telephony.Rlog;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080069
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070070import java.io.File;
Junda Liu43d723a2015-05-12 17:23:45 -070071import java.io.FileDescriptor;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070072import java.io.FileInputStream;
73import java.io.FileNotFoundException;
74import java.io.FileOutputStream;
Jonathan Basseri1f743c92015-05-15 00:19:46 -070075import java.io.FilenameFilter;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070076import java.io.IOException;
Junda Liu43d723a2015-05-12 17:23:45 -070077import java.io.PrintWriter;
shuoq26a3a4c2016-12-16 11:06:48 -080078import java.util.ArrayList;
79import java.util.Arrays;
80import java.util.Collections;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080081import java.util.List;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080082import java.util.Set;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080083
84/**
85 * CarrierConfigLoader binds to privileged carrier apps to fetch carrier config overlays.
Jonathan Basseri6465afd2015-02-25 13:05:57 -080086 */
Jonathan Basseri6465afd2015-02-25 13:05:57 -080087public class CarrierConfigLoader extends ICarrierConfigLoader.Stub {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070088 private static final String LOG_TAG = "CarrierConfigLoader";
Meng Wang33ad2bc2017-03-16 20:21:20 -070089
90 // Package name for platform carrier config app, bundled with system image.
Rambo Wangfe0d7c12022-04-15 03:00:32 +000091 @NonNull private final String mPlatformCarrierConfigPackage;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080092
93 /** The singleton instance. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +000094 @Nullable private static CarrierConfigLoader sInstance;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080095 // The context for phone app, passed from PhoneGlobals.
Rambo Wangfe0d7c12022-04-15 03:00:32 +000096 @NonNull private Context mContext;
97
98 // All the states below (array indexed by phoneId) are non-null. But the member of the array
99 // is nullable, when e.g. the config for the phone is not loaded yet
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800100 // Carrier configs from default app, indexed by phoneID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000101 @NonNull private PersistableBundle[] mConfigFromDefaultApp;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800102 // Carrier configs from privileged carrier config app, indexed by phoneID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000103 @NonNull private PersistableBundle[] mConfigFromCarrierApp;
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100104 // Persistent Carrier configs that are provided via the override test API, indexed by phone ID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000105 @NonNull private PersistableBundle[] mPersistentOverrideConfigs;
Hall Liu506724b2018-10-22 18:16:14 -0700106 // Carrier configs that are provided via the override test API, indexed by phone ID.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000107 @NonNull private PersistableBundle[] mOverrideConfigs;
Jayachandran C645ec612020-01-10 10:30:25 -0800108 // Carrier configs to override code default when there is no SIM inserted
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000109 @NonNull private PersistableBundle mNoSimConfig;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800110 // Service connection for binding to config app.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000111 @NonNull private CarrierServiceConnection[] mServiceConnection;
Jayachandran C645ec612020-01-10 10:30:25 -0800112 // Service connection for binding to carrier config app for no SIM config.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000113 @NonNull private CarrierServiceConnection[] mServiceConnectionForNoSimConfig;
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700114 // Whether we are bound to a service for each phone
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000115 @NonNull private boolean[] mServiceBound;
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700116 // Whether we are bound to a service for no SIM config
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000117 @NonNull private boolean[] mServiceBoundForNoSimConfig;
Sarah Chin807d5c62020-03-30 17:21:09 -0700118 // Whether we have sent config change broadcast for each phone id.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000119 @NonNull private boolean[] mHasSentConfigChange;
Sarah Chin807d5c62020-03-30 17:21:09 -0700120 // Whether the broadcast was sent from EVENT_SYSTEM_UNLOCKED, to track rebroadcasts
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000121 @NonNull private boolean[] mFromSystemUnlocked;
122
Nathan Harold48ac0972019-03-13 22:33:01 -0700123 // SubscriptionInfoUpdater
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000124 @NonNull private final SubscriptionInfoUpdater mSubscriptionInfoUpdater;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800125
Nanxi Chen3d670502016-03-17 16:32:09 -0700126 // Broadcast receiver for Boot intents, register intent filter in construtor.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000127 @NonNull private final BroadcastReceiver mBootReceiver = new ConfigLoaderBroadcastReceiver();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800128 // Broadcast receiver for SIM and pkg intents, register intent filter in constructor.
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000129 @NonNull private final BroadcastReceiver mPackageReceiver = new ConfigLoaderBroadcastReceiver();
130 @NonNull private final LocalLog mCarrierConfigLoadingLog = new LocalLog(100);
chen xudb04c292019-03-26 17:01:15 -0700131
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800132
133 // Message codes; see mHandler below.
134 // Request from SubscriptionInfoUpdater when SIM becomes absent or error.
135 private static final int EVENT_CLEAR_CONFIG = 0;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800136 // Has connected to default app.
137 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
138 // Has connected to carrier app.
139 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700140 // Config has been loaded from default app (or cache).
141 private static final int EVENT_FETCH_DEFAULT_DONE = 5;
142 // Config has been loaded from carrier app (or cache).
143 private static final int EVENT_FETCH_CARRIER_DONE = 6;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700144 // Attempt to fetch from default app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700145 private static final int EVENT_DO_FETCH_DEFAULT = 7;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700146 // Attempt to fetch from carrier app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700147 private static final int EVENT_DO_FETCH_CARRIER = 8;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700148 // A package has been installed, uninstalled, or updated.
149 private static final int EVENT_PACKAGE_CHANGED = 9;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700150 // Bind timed out for the default app.
151 private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
152 // Bind timed out for a carrier app.
153 private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700154 // Check if the system fingerprint has changed.
155 private static final int EVENT_CHECK_SYSTEM_UPDATE = 12;
Nanxi Chen3d670502016-03-17 16:32:09 -0700156 // Rerun carrier config binding after system is unlocked.
157 private static final int EVENT_SYSTEM_UNLOCKED = 13;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700158 // Fetching config timed out from the default app.
159 private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14;
160 // Fetching config timed out from a carrier app.
161 private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15;
Nathan Harold48ac0972019-03-13 22:33:01 -0700162 // SubscriptionInfoUpdater has finished updating the sub for the carrier config.
163 private static final int EVENT_SUBSCRIPTION_INFO_UPDATED = 16;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700164 // Multi-SIM config changed.
165 private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 17;
Jayachandran C645ec612020-01-10 10:30:25 -0800166 // Attempt to fetch from default app or read from XML for no SIM case.
167 private static final int EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG = 18;
168 // No SIM config has been loaded from default app (or cache).
169 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE = 19;
170 // Has connected to default app for no SIM config.
171 private static final int EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG = 20;
172 // Bind timed out for the default app when trying to fetch no SIM config.
173 private static final int EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 21;
174 // Fetching config timed out from the default app for no SIM config.
175 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 22;
Rambo Wangab13e3e2021-04-08 15:01:06 -0700176 // NOTE: any new EVENT_* values must be added to method eventToString().
Jonathan Basseri930701e2015-06-11 20:56:43 -0700177
Junda Liu6cb45002016-03-22 17:18:20 -0700178 private static final int BIND_TIMEOUT_MILLIS = 30000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800179
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800180 // Keys used for saving and restoring config bundle from file.
181 private static final String KEY_VERSION = "__carrier_config_package_version__";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800182
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100183 private static final String OVERRIDE_PACKAGE_ADDITION = "-override";
184
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700185 // SharedPreferences key for last known build fingerprint.
186 private static final String KEY_FINGERPRINT = "build_fingerprint";
187
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800188 // Argument for #dump that indicates we should also call the default and specified carrier
189 // service's #dump method. In multi-SIM devices, it's possible that carrier A is on SIM 1 and
190 // carrier B is on SIM 2, in which case we should not dump carrier B's service when carrier A
191 // requested the dump.
192 private static final String DUMP_ARG_REQUESTING_PACKAGE = "--requesting-package";
193
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800194 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700195 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700196 // For each phoneId, the event sequence should be:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700197 // fetch default, connected to default, fetch default (async), fetch default done,
198 // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700199 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700200 // If there is a saved config file for either the default app or the carrier app, we skip
201 // binding to the app and go straight from fetch to loaded.
202 //
203 // At any time, at most one connection is active. If events are not in this order, previous
204 // connection will be unbound, so only latest event takes effect.
205 //
206 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
207 // 1. loading from carrier app (even if read from a file)
208 // 2. loading from default app if there is no carrier app (even if read from a file)
209 // 3. clearing config (e.g. due to sim removal)
210 // 4. encountering bind or IPC error
Jonathan Basseri65273c82017-07-25 15:08:42 -0700211 private class ConfigHandler extends Handler {
Rambo Wang7e3bc122021-03-23 09:24:38 -0700212 ConfigHandler(@NonNull Looper looper) {
213 super(looper);
214 }
215
Jonathan Basseri65273c82017-07-25 15:08:42 -0700216 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000217 public void handleMessage(@NonNull Message msg) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700218 final int phoneId = msg.arg1;
Rambo Wangab13e3e2021-04-08 15:01:06 -0700219 logdWithLocalLog("mHandler: " + eventToString(msg.what) + " phoneId: " + phoneId);
Malcolm Chen5ea18532019-10-24 19:55:44 -0700220 if (!SubscriptionManager.isValidPhoneId(phoneId)
221 && msg.what != EVENT_MULTI_SIM_CONFIG_CHANGED) {
222 return;
223 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800224 switch (msg.what) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800225 case EVENT_CLEAR_CONFIG: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700226 clearConfigForPhone(phoneId, true);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800227 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700228 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700229
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800230 case EVENT_SYSTEM_UNLOCKED: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700231 for (int i = 0; i < TelephonyManager.from(mContext).getActiveModemCount();
232 ++i) {
Sarah Chin807d5c62020-03-30 17:21:09 -0700233 // When the user unlocks the device, send the broadcast again (with a
234 // rebroadcast extra) if we have sent it before unlock. This will avoid
235 // trying to load the carrier config when the SIM is still loading when the
236 // unlock happens.
Junda Liu03aad762017-07-21 13:32:17 -0700237 if (mHasSentConfigChange[i]) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800238 logdWithLocalLog("System unlocked");
Sarah Chin807d5c62020-03-30 17:21:09 -0700239 mFromSystemUnlocked[i] = true;
Junda Liu03aad762017-07-21 13:32:17 -0700240 updateConfigForPhoneId(i);
241 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700242 }
243 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700244 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700245
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800246 case EVENT_PACKAGE_CHANGED: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700247 final String carrierPackageName = (String) msg.obj;
248 // Only update if there are cached config removed to avoid updating config for
249 // unrelated packages.
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700250 if (clearCachedConfigForPackage(carrierPackageName)) {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700251 int numPhones = TelephonyManager.from(mContext).getActiveModemCount();
Junda Liu8a8a53a2015-05-15 16:19:57 -0700252 for (int i = 0; i < numPhones; ++i) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800253 logdWithLocalLog("Package changed: " + carrierPackageName
Jack Yu67663de2019-10-17 15:19:48 -0700254 + ", phone=" + i);
Junda Liu8a8a53a2015-05-15 16:19:57 -0700255 updateConfigForPhoneId(i);
256 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700257 }
258 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700259 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700260
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800261 case EVENT_DO_FETCH_DEFAULT: {
Rambo Wang9bc7d362021-03-09 09:10:58 -0800262 // Clear in-memory cache for carrier app config, so when carrier app gets
263 // uninstalled, no stale config is left.
264 if (mConfigFromCarrierApp[phoneId] != null
265 && getCarrierPackageForPhoneId(phoneId) == null) {
266 mConfigFromCarrierApp[phoneId] = null;
267 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100268 // Restore persistent override values.
269 PersistableBundle config = restoreConfigFromXml(
270 mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId);
271 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800272 logd("Loaded persistent override config from XML. package="
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100273 + mPlatformCarrierConfigPackage
274 + " phoneId=" + phoneId);
275 mPersistentOverrideConfigs[phoneId] = config;
276 }
277
278 config = restoreConfigFromXml(mPlatformCarrierConfigPackage, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700279 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800280 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700281 "Loaded config from XML. package="
282 + mPlatformCarrierConfigPackage
283 + " phoneId="
284 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700285 mConfigFromDefaultApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700286 Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700287 newMsg.getData().putBoolean("loaded_from_xml", true);
288 mHandler.sendMessage(newMsg);
289 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700290 // No cached config, so fetch it from the default app.
291 if (bindToConfigPackage(
292 mPlatformCarrierConfigPackage,
293 phoneId,
294 EVENT_CONNECTED_TO_DEFAULT)) {
295 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800296 obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1 /*arg2*/,
297 getMessageToken(phoneId)),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700298 BIND_TIMEOUT_MILLIS);
299 } else {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800300 // Put a stub bundle in place so that the rest of the logic continues
301 // smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000302 mConfigFromDefaultApp[phoneId] = new PersistableBundle();
Jonathan Basseri65273c82017-07-25 15:08:42 -0700303 // Send broadcast if bind fails.
Nathan Harold48ac0972019-03-13 22:33:01 -0700304 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700305 // TODO: We *must* call unbindService even if bindService returns false.
306 // (And possibly if SecurityException was thrown.)
chen xudb04c292019-03-26 17:01:15 -0700307 loge("binding to default app: "
308 + mPlatformCarrierConfigPackage + " fails");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700309 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800310 }
311 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700312 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800313
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800314 case EVENT_CONNECTED_TO_DEFAULT: {
Rambo Wang610fdf62021-03-08 21:37:11 -0800315 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT, getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700316 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800317 // If new service connection has been created, unbind.
318 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700319 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800320 break;
321 }
chen xu02581692018-11-11 19:03:44 -0800322 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700323 // ResultReceiver callback will execute in this Handler's thread.
324 final ResultReceiver resultReceiver =
325 new ResultReceiver(this) {
326 @Override
327 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700328 unbindIfBound(mContext, conn, phoneId);
Rambo Wanga9d27a52021-04-09 16:17:33 -0700329 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT,
330 getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700331 // If new service connection has been created, this is stale.
332 if (mServiceConnection[phoneId] != conn) {
333 loge("Received response for stale request.");
334 return;
335 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700336 if (resultCode == RESULT_ERROR || resultData == null) {
337 // On error, abort config fetching.
338 loge("Failed to get carrier config");
Nathan Harold48ac0972019-03-13 22:33:01 -0700339 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700340 return;
341 }
342 PersistableBundle config =
343 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100344 saveConfigToXml(mPlatformCarrierConfigPackage, "", phoneId,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800345 carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700346 mConfigFromDefaultApp[phoneId] = config;
347 sendMessage(
348 obtainMessage(
349 EVENT_FETCH_DEFAULT_DONE, phoneId, -1));
350 }
351 };
352 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800353 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700354 ICarrierService carrierService =
355 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800356 carrierService.getCarrierConfig(phoneId, carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800357 logdWithLocalLog("Fetch config for default app: "
chen xudb04c292019-03-26 17:01:15 -0700358 + mPlatformCarrierConfigPackage
359 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700360 } catch (RemoteException e) {
chen xudb04c292019-03-26 17:01:15 -0700361 loge("Failed to get carrier config from default app: " +
362 mPlatformCarrierConfigPackage + " err: " + e.toString());
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700363 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700364 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800365 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700366 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800367 obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1 /*arg2*/,
368 getMessageToken(phoneId)),
Jonathan Basseri65273c82017-07-25 15:08:42 -0700369 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800370 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700371 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800372
Jonathan Basseri930701e2015-06-11 20:56:43 -0700373 case EVENT_BIND_DEFAULT_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800374 case EVENT_FETCH_DEFAULT_TIMEOUT: {
375 loge("Bind/fetch time out from " + mPlatformCarrierConfigPackage);
Rambo Wang610fdf62021-03-08 21:37:11 -0800376 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT, getMessageToken(phoneId));
chen xuf60b3162019-05-01 21:31:05 -0700377 // If we attempted to bind to the app, but the service connection is null due to
378 // the race condition that clear config event happens before bind/fetch complete
379 // then config was cleared while we were waiting and we should not continue.
380 if (mServiceConnection[phoneId] != null) {
381 // If a ResponseReceiver callback is in the queue when this happens, we will
382 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700383 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700384 broadcastConfigChangedIntent(phoneId);
385 }
Nathan Harold1122e3d2021-01-22 15:45:24 -0800386 // Put a stub bundle in place so that the rest of the logic continues smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000387 mConfigFromDefaultApp[phoneId] = new PersistableBundle();
Nathan Harold48ac0972019-03-13 22:33:01 -0700388 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700389 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700390 }
Jonathan Basseri930701e2015-06-11 20:56:43 -0700391
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800392 case EVENT_FETCH_DEFAULT_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700393 // If we attempted to bind to the app, but the service connection is null, then
394 // config was cleared while we were waiting and we should not continue.
395 if (!msg.getData().getBoolean("loaded_from_xml", false)
396 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800397 break;
398 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700399 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700400 if (carrierPackageName != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800401 logd("Found carrier config app: " + carrierPackageName);
Jordan Liu38a614c2019-03-20 15:01:17 -0700402 sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId, -1));
Jonathan Basserib919e932015-05-27 16:53:08 +0000403 } else {
Nathan Harold48ac0972019-03-13 22:33:01 -0700404 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700405 }
406 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700407 }
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700408
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800409 case EVENT_DO_FETCH_CARRIER: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700410 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700411 final PersistableBundle config =
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100412 restoreConfigFromXml(carrierPackageName, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700413 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800414 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700415 "Loaded config from XML. package="
416 + carrierPackageName
417 + " phoneId="
418 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700419 mConfigFromCarrierApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700420 Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700421 newMsg.getData().putBoolean("loaded_from_xml", true);
422 sendMessage(newMsg);
423 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700424 // No cached config, so fetch it from a carrier app.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800425 if (carrierPackageName != null && bindToConfigPackage(carrierPackageName,
426 phoneId, EVENT_CONNECTED_TO_CARRIER)) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700427 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800428 obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1 /*arg2*/,
429 getMessageToken(phoneId)),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700430 BIND_TIMEOUT_MILLIS);
431 } else {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800432 // Put a stub bundle in place so that the rest of the logic continues
433 // smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000434 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
Jonathan Basseri65273c82017-07-25 15:08:42 -0700435 // Send broadcast if bind fails.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700436 broadcastConfigChangedIntent(phoneId);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800437 loge("Bind to carrier app: " + carrierPackageName + " fails");
Nathan Harold48ac0972019-03-13 22:33:01 -0700438 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700439 }
440 }
441 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700442 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700443
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800444 case EVENT_CONNECTED_TO_CARRIER: {
Rambo Wang610fdf62021-03-08 21:37:11 -0800445 removeMessages(EVENT_BIND_CARRIER_TIMEOUT, getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700446 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800447 // If new service connection has been created, unbind.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700448 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700449 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800450 break;
451 }
chen xu02581692018-11-11 19:03:44 -0800452 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700453 // ResultReceiver callback will execute in this Handler's thread.
454 final ResultReceiver resultReceiver =
455 new ResultReceiver(this) {
456 @Override
457 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700458 unbindIfBound(mContext, conn, phoneId);
Rambo Wanga9d27a52021-04-09 16:17:33 -0700459 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT,
460 getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700461 // If new service connection has been created, this is stale.
462 if (mServiceConnection[phoneId] != conn) {
463 loge("Received response for stale request.");
464 return;
465 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700466 if (resultCode == RESULT_ERROR || resultData == null) {
467 // On error, abort config fetching.
chen xudb04c292019-03-26 17:01:15 -0700468 loge("Failed to get carrier config from carrier app: "
469 + getCarrierPackageForPhoneId(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700470 broadcastConfigChangedIntent(phoneId);
Nathan Harold48ac0972019-03-13 22:33:01 -0700471 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700472 return;
473 }
474 PersistableBundle config =
475 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100476 saveConfigToXml(getCarrierPackageForPhoneId(phoneId), "",
477 phoneId, carrierId, config);
Amit Mahajan304e92b2021-04-08 14:03:30 -0700478 if (config != null) {
479 mConfigFromCarrierApp[phoneId] = config;
480 } else {
481 logdWithLocalLog("Config from carrier app is null "
482 + "for phoneId " + phoneId);
483 // Put a stub bundle in place so that the rest of the logic
484 // continues smoothly.
485 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
486 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700487 sendMessage(
488 obtainMessage(
489 EVENT_FETCH_CARRIER_DONE, phoneId, -1));
490 }
491 };
492 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800493 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700494 ICarrierService carrierService =
495 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800496 carrierService.getCarrierConfig(phoneId, carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800497 logdWithLocalLog("Fetch config for carrier app: "
chen xudb04c292019-03-26 17:01:15 -0700498 + getCarrierPackageForPhoneId(phoneId)
499 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700500 } catch (RemoteException e) {
501 loge("Failed to get carrier config: " + e.toString());
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700502 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700503 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800504 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700505 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800506 obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1 /*arg2*/,
507 getMessageToken(phoneId)),
Jonathan Basseri65273c82017-07-25 15:08:42 -0700508 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800509 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700510 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800511
Jonathan Basseri930701e2015-06-11 20:56:43 -0700512 case EVENT_BIND_CARRIER_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800513 case EVENT_FETCH_CARRIER_TIMEOUT: {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800514 loge("Bind/fetch from carrier app timeout, package="
515 + getCarrierPackageForPhoneId(phoneId));
Rambo Wang610fdf62021-03-08 21:37:11 -0800516 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT, getMessageToken(phoneId));
chen xuf60b3162019-05-01 21:31:05 -0700517 // If we attempted to bind to the app, but the service connection is null due to
518 // the race condition that clear config event happens before bind/fetch complete
519 // then config was cleared while we were waiting and we should not continue.
520 if (mServiceConnection[phoneId] != null) {
521 // If a ResponseReceiver callback is in the queue when this happens, we will
522 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700523 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700524 broadcastConfigChangedIntent(phoneId);
525 }
Nathan Harold1122e3d2021-01-22 15:45:24 -0800526 // Put a stub bundle in place so that the rest of the logic continues smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000527 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
Nathan Harold48ac0972019-03-13 22:33:01 -0700528 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700529 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700530 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800531 case EVENT_FETCH_CARRIER_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700532 // If we attempted to bind to the app, but the service connection is null, then
533 // config was cleared while we were waiting and we should not continue.
534 if (!msg.getData().getBoolean("loaded_from_xml", false)
535 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800536 break;
537 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700538 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800539 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700540 }
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700541
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800542 case EVENT_CHECK_SYSTEM_UPDATE: {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700543 SharedPreferences sharedPrefs =
544 PreferenceManager.getDefaultSharedPreferences(mContext);
545 final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
546 if (!Build.FINGERPRINT.equals(lastFingerprint)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800547 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700548 "Build fingerprint changed. old: "
549 + lastFingerprint
550 + " new: "
551 + Build.FINGERPRINT);
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700552 clearCachedConfigForPackage(null);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700553 sharedPrefs
554 .edit()
555 .putString(KEY_FINGERPRINT, Build.FINGERPRINT)
556 .apply();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700557 }
558 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700559 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700560
561 case EVENT_SUBSCRIPTION_INFO_UPDATED:
562 broadcastConfigChangedIntent(phoneId);
563 break;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700564 case EVENT_MULTI_SIM_CONFIG_CHANGED:
565 onMultiSimConfigChanged();
566 break;
Jayachandran C645ec612020-01-10 10:30:25 -0800567
568 case EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG: {
569 PersistableBundle config =
570 restoreNoSimConfigFromXml(mPlatformCarrierConfigPackage);
571
572 if (config != null) {
573 logd("Loaded no SIM config from XML. package="
574 + mPlatformCarrierConfigPackage);
575 mNoSimConfig = config;
576 sendMessage(
577 obtainMessage(
578 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE,
579 phoneId, -1));
580 } else {
581 // No cached config, so fetch it from the default app.
582 if (bindToConfigPackage(
583 mPlatformCarrierConfigPackage,
584 phoneId,
585 EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG)) {
586 sendMessageDelayed(
587 obtainMessage(
588 EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT,
589 phoneId, -1), BIND_TIMEOUT_MILLIS);
590 } else {
591 broadcastConfigChangedIntent(phoneId, false);
592 // TODO: We *must* call unbindService even if bindService returns false.
593 // (And possibly if SecurityException was thrown.)
594 loge("binding to default app to fetch no SIM config: "
595 + mPlatformCarrierConfigPackage + " fails");
596 }
597 }
598 break;
599 }
600
601 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE: {
602 broadcastConfigChangedIntent(phoneId, false);
603 break;
604 }
605
606 case EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
607 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT: {
608 loge("Bind/fetch time out for no SIM config from "
609 + mPlatformCarrierConfigPackage);
610 removeMessages(EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
611 // If we attempted to bind to the app, but the service connection is null due to
612 // the race condition that clear config event happens before bind/fetch complete
613 // then config was cleared while we were waiting and we should not continue.
614 if (mServiceConnectionForNoSimConfig[phoneId] != null) {
615 // If a ResponseReceiver callback is in the queue when this happens, we will
616 // unbind twice and throw an exception.
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700617 unbindIfBoundForNoSimConfig(mContext,
618 mServiceConnectionForNoSimConfig[phoneId], phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800619 }
620 broadcastConfigChangedIntent(phoneId, false);
621 break;
622 }
623
624 case EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG: {
625 removeMessages(EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
626 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
627 // If new service connection has been created, unbind.
628 if (mServiceConnectionForNoSimConfig[phoneId] != conn || conn.service == null) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700629 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800630 break;
631 }
632
633 // ResultReceiver callback will execute in this Handler's thread.
634 final ResultReceiver resultReceiver =
635 new ResultReceiver(this) {
636 @Override
637 public void onReceiveResult(int resultCode, Bundle resultData) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700638 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800639 // If new service connection has been created, this is stale.
640 if (mServiceConnectionForNoSimConfig[phoneId] != conn) {
641 loge("Received response for stale request.");
642 return;
643 }
644 removeMessages(EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
645 if (resultCode == RESULT_ERROR || resultData == null) {
646 // On error, abort config fetching.
647 loge("Failed to get no SIM carrier config");
648 return;
649 }
650 PersistableBundle config =
651 resultData.getParcelable(KEY_CONFIG_BUNDLE);
652 saveNoSimConfigToXml(mPlatformCarrierConfigPackage, config);
653 mNoSimConfig = config;
654 sendMessage(
655 obtainMessage(
656 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE,
657 phoneId, -1));
658 }
659 };
660 // Now fetch the config asynchronously from the ICarrierService.
661 try {
662 ICarrierService carrierService =
663 ICarrierService.Stub.asInterface(conn.service);
Rambo Wang38bbdbe2021-11-23 13:03:34 -0800664 carrierService.getCarrierConfig(phoneId, null, resultReceiver);
Jayachandran C645ec612020-01-10 10:30:25 -0800665 logdWithLocalLog("Fetch no sim config from default app: "
666 + mPlatformCarrierConfigPackage);
667 } catch (RemoteException e) {
668 loge("Failed to get no sim carrier config from default app: " +
669 mPlatformCarrierConfigPackage + " err: " + e.toString());
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700670 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -0800671 break; // So we don't set a timeout.
672 }
673 sendMessageDelayed(
674 obtainMessage(
675 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT,
676 phoneId, -1), BIND_TIMEOUT_MILLIS);
677 break;
678 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800679 }
680 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700681 }
682
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000683 @NonNull private final Handler mHandler;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800684
685 /**
686 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
687 * receiver for relevant events.
688 */
Rambo Wang7e3bc122021-03-23 09:24:38 -0700689 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000690 /* package */ CarrierConfigLoader(@NonNull Context context,
691 @NonNull SubscriptionInfoUpdater subscriptionInfoUpdater, @NonNull Looper looper) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800692 mContext = context;
Meng Wang33ad2bc2017-03-16 20:21:20 -0700693 mPlatformCarrierConfigPackage =
694 mContext.getString(R.string.platform_carrier_config_package);
Rambo Wang7e3bc122021-03-23 09:24:38 -0700695 mHandler = new ConfigHandler(looper);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800696
Nanxi Chen3d670502016-03-17 16:32:09 -0700697 IntentFilter bootFilter = new IntentFilter();
698 bootFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
699 context.registerReceiver(mBootReceiver, bootFilter);
700
Junda Liu8a8a53a2015-05-15 16:19:57 -0700701 // Register for package updates. Update app or uninstall app update will have all 3 intents,
702 // in the order or removed, added, replaced, all with extra_replace set to true.
703 IntentFilter pkgFilter = new IntentFilter();
704 pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
705 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
706 pkgFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
707 pkgFilter.addDataScheme("package");
Jordan Liu5ca24762019-07-10 12:51:09 -0700708 context.registerReceiver(mPackageReceiver, pkgFilter);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800709
Malcolm Chen978dda52019-10-08 18:15:25 -0700710 int numPhones = TelephonyManager.from(context).getSupportedModemCount();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700711 mConfigFromDefaultApp = new PersistableBundle[numPhones];
712 mConfigFromCarrierApp = new PersistableBundle[numPhones];
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100713 mPersistentOverrideConfigs = new PersistableBundle[numPhones];
Hall Liu506724b2018-10-22 18:16:14 -0700714 mOverrideConfigs = new PersistableBundle[numPhones];
Rambo Wang42026112021-04-07 20:57:28 +0000715 mNoSimConfig = new PersistableBundle();
Zach Johnson36d7aab2015-05-22 15:43:00 -0700716 mServiceConnection = new CarrierServiceConnection[numPhones];
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700717 mServiceBound = new boolean[numPhones];
Junda Liu03aad762017-07-21 13:32:17 -0700718 mHasSentConfigChange = new boolean[numPhones];
Sarah Chin807d5c62020-03-30 17:21:09 -0700719 mFromSystemUnlocked = new boolean[numPhones];
Jayachandran C645ec612020-01-10 10:30:25 -0800720 mServiceConnectionForNoSimConfig = new CarrierServiceConnection[numPhones];
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700721 mServiceBoundForNoSimConfig = new boolean[numPhones];
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800722 logd("CarrierConfigLoader has started");
Rambo Wang7e3bc122021-03-23 09:24:38 -0700723 mSubscriptionInfoUpdater = subscriptionInfoUpdater;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700724 mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800725 }
726
727 /**
728 * Initialize the singleton CarrierConfigLoader instance.
729 *
730 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
731 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000732 @NonNull
733 /* package */ static CarrierConfigLoader init(@NonNull Context context) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800734 synchronized (CarrierConfigLoader.class) {
735 if (sInstance == null) {
Rambo Wang7e3bc122021-03-23 09:24:38 -0700736 sInstance = new CarrierConfigLoader(context,
737 PhoneFactory.getSubscriptionInfoUpdater(), Looper.myLooper());
Brad Ebingerfa6575f2021-05-04 00:29:50 +0000738 // Make this service available through ServiceManager.
739 TelephonyFrameworkInitializer.getTelephonyServiceManager()
740 .getCarrierConfigServiceRegisterer().register(sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800741 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700742 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800743 }
744 return sInstance;
745 }
746 }
747
Rambo Wang7e3bc122021-03-23 09:24:38 -0700748 @VisibleForTesting
749 /* package */ void clearConfigForPhone(int phoneId, boolean fetchNoSimConfig) {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700750 /* Ignore clear configuration request if device is being shutdown. */
751 Phone phone = PhoneFactory.getPhone(phoneId);
752 if (phone != null) {
753 if (phone.isShuttingDown()) {
754 return;
755 }
756 }
757
758 mConfigFromDefaultApp[phoneId] = null;
759 mConfigFromCarrierApp[phoneId] = null;
760 mServiceConnection[phoneId] = null;
761 mHasSentConfigChange[phoneId] = false;
762
Jayachandran C645ec612020-01-10 10:30:25 -0800763 if (fetchNoSimConfig) {
764 // To fetch no SIM config
765 mHandler.sendMessage(
766 mHandler.obtainMessage(
767 EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG, phoneId, -1));
768 }
Malcolm Chen5ea18532019-10-24 19:55:44 -0700769 }
770
Nathan Harold48ac0972019-03-13 22:33:01 -0700771 private void notifySubscriptionInfoUpdater(int phoneId) {
772 String configPackagename;
773 PersistableBundle configToSend;
774 int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
775 // Prefer the carrier privileged carrier app, but if there is not one, use the platform
776 // default carrier app.
777 if (mConfigFromCarrierApp[phoneId] != null) {
778 configPackagename = getCarrierPackageForPhoneId(phoneId);
779 configToSend = mConfigFromCarrierApp[phoneId];
780 } else {
781 configPackagename = mPlatformCarrierConfigPackage;
782 configToSend = mConfigFromDefaultApp[phoneId];
783 }
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700784
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800785 if (configToSend == null) {
Rambo Wang42026112021-04-07 20:57:28 +0000786 configToSend = new PersistableBundle();
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800787 }
788
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700789 // mOverrideConfigs is for testing. And it will override current configs.
790 PersistableBundle config = mOverrideConfigs[phoneId];
791 if (config != null) {
Torbjorn Eklund1050cb02018-11-16 14:05:38 +0100792 configToSend = new PersistableBundle(configToSend);
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700793 configToSend.putAll(config);
794 }
795
Nathan Harold48ac0972019-03-13 22:33:01 -0700796 mSubscriptionInfoUpdater.updateSubscriptionByCarrierConfigAndNotifyComplete(
797 phoneId, configPackagename, configToSend,
798 mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1));
799 }
800
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800801 private void broadcastConfigChangedIntent(int phoneId) {
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800802 broadcastConfigChangedIntent(phoneId, true);
803 }
804
805 private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800806 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800807 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
chen xuf9db49f2019-03-31 23:04:42 -0700808 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800809 if (addSubIdExtra) {
810 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
811 int[] subIds = SubscriptionManager.getSubId(phoneId);
812 if (!ArrayUtils.isEmpty(subIds)) {
813 TelephonyManager telMgr = TelephonyManager.from(mContext)
814 .createForSubscriptionId(subIds[0]);
815 simApplicationState = telMgr.getSimApplicationState();
816 }
Malcolm Chenf7635ec2020-04-15 15:45:00 -0700817 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId
818 + " simApplicationState " + simApplicationState);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800819 // Include subId/carrier id extra only if SIM records are loaded
820 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
821 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
822 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
823 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID,
824 getSpecificCarrierIdForPhoneId(phoneId));
825 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, getCarrierIdForPhoneId(phoneId));
826 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800827 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800828 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Sarah Chin807d5c62020-03-30 17:21:09 -0700829 intent.putExtra(CarrierConfigManager.EXTRA_REBROADCAST_ON_UNLOCK,
830 mFromSystemUnlocked[phoneId]);
Meng Wang97a6a462020-01-23 16:22:16 -0800831 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
Jack Yue9e95be2020-03-22 10:56:06 -0700832 int[] subIds = SubscriptionManager.getSubId(phoneId);
833 if (subIds != null && subIds.length > 0) {
834 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId + ", subId=" + subIds[0]);
835 } else {
836 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
837 }
Junda Liu03aad762017-07-21 13:32:17 -0700838 mHasSentConfigChange[phoneId] = true;
Sarah Chin807d5c62020-03-30 17:21:09 -0700839 mFromSystemUnlocked[phoneId] = false;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800840 }
841
842 /** Binds to the default or carrier config app. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000843 private boolean bindToConfigPackage(@NonNull String pkgName, int phoneId, int eventId) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800844 logdWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700845 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700846 carrierService.setPackage(pkgName);
Jayachandran C645ec612020-01-10 10:30:25 -0800847 CarrierServiceConnection serviceConnection = new CarrierServiceConnection(
848 phoneId, pkgName, eventId);
849 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
850 mServiceConnectionForNoSimConfig[phoneId] = serviceConnection;
851 } else {
852 mServiceConnection[phoneId] = serviceConnection;
853 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800854 try {
Jayachandran C645ec612020-01-10 10:30:25 -0800855 if (mContext.bindService(carrierService, serviceConnection,
Jordan Liu178430d2020-02-10 14:32:15 -0800856 Context.BIND_AUTO_CREATE)) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700857 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
858 mServiceBoundForNoSimConfig[phoneId] = true;
859 } else {
860 mServiceBound[phoneId] = true;
861 }
Jordan Liu178430d2020-02-10 14:32:15 -0800862 return true;
863 } else {
864 return false;
865 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800866 } catch (SecurityException ex) {
867 return false;
868 }
869 }
870
Rambo Wang7e3bc122021-03-23 09:24:38 -0700871 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000872 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -0700873 /* package */ CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800874 String mcc = "";
875 String mnc = "";
876 String imsi = "";
877 String gid1 = "";
878 String gid2 = "";
879 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
880 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800881 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800882 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700883 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
884 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800885 mcc = simOperator.substring(0, 3);
886 mnc = simOperator.substring(3);
887 }
888 Phone phone = PhoneFactory.getPhone(phoneId);
889 if (phone != null) {
890 imsi = phone.getSubscriberId();
891 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700892 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800893 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800894 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800895 }
chen xua31f22b2019-03-06 15:28:50 -0800896 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800897 }
898
Rambo Wangf7c214a2022-03-17 12:36:22 -0700899 /** Returns the package name of a privileged carrier app, or null if there is none. */
Nathan Harold1122e3d2021-01-22 15:45:24 -0800900 @Nullable
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700901 private String getCarrierPackageForPhoneId(int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -0700902 final long token = Binder.clearCallingIdentity();
903 try {
Rambo Wangf7c214a2022-03-17 12:36:22 -0700904 return TelephonyManager.from(mContext)
905 .getCarrierServicePackageNameForLogicalSlot(phoneId);
Nazanin1adf4562021-03-29 15:35:30 -0700906 } finally {
907 Binder.restoreCallingIdentity(token);
908 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700909 }
910
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000911 @Nullable
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700912 private String getIccIdForPhoneId(int phoneId) {
913 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
914 return null;
915 }
916 Phone phone = PhoneFactory.getPhone(phoneId);
917 if (phone == null) {
918 return null;
919 }
920 return phone.getIccSerialNumber();
921 }
922
chen xu02581692018-11-11 19:03:44 -0800923 /**
chen xua31f22b2019-03-06 15:28:50 -0800924 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800925 */
chen xua31f22b2019-03-06 15:28:50 -0800926 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700927 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800928 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700929 }
930 Phone phone = PhoneFactory.getPhone(phoneId);
931 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800932 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700933 }
chen xua31f22b2019-03-06 15:28:50 -0800934 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800935 }
936
937 /**
938 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
939 */
940 private int getCarrierIdForPhoneId(int phoneId) {
941 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
942 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700943 }
chen xu02581692018-11-11 19:03:44 -0800944 Phone phone = PhoneFactory.getPhone(phoneId);
945 if (phone == null) {
946 return TelephonyManager.UNKNOWN_CARRIER_ID;
947 }
948 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700949 }
950
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700951 /**
952 * Writes a bundle to an XML file.
953 *
chen xu02581692018-11-11 19:03:44 -0800954 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -0800955 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -0800956 * should have a single copy of XML file named after carrier id. However, it's still possible
957 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
958 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
959 * carrier while iccid remains the same.
960 *
961 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
962 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700963 *
964 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700965 *
Jayachandran C645ec612020-01-10 10:30:25 -0800966 * @param packageName the name of the package from which we fetched this bundle.
967 * @param extraString An extra string to be used in the XML file name.
968 * @param phoneId the phone ID.
969 * @param carrierId contains all carrier-identifying information.
970 * @param config the bundle to be written. Null will be treated as an empty bundle.
971 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700972 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000973 private void saveConfigToXml(@Nullable String packageName, @NonNull String extraString,
974 int phoneId, @Nullable CarrierIdentifier carrierId, @NonNull PersistableBundle config,
975 boolean isNoSimConfig) {
Jayachandran C645ec612020-01-10 10:30:25 -0800976 if (packageName == null) {
977 loge("Cannot save config with null packageName");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800978 return;
979 }
980
Jayachandran C645ec612020-01-10 10:30:25 -0800981 String fileName;
982 if (isNoSimConfig) {
983 fileName = getFilenameForNoSimConfig(packageName);
984 } else {
985 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
986 != TelephonyManager.SIM_STATE_LOADED) {
987 loge("Skip save config because SIM records are not loaded.");
988 return;
989 }
990
991 final String iccid = getIccIdForPhoneId(phoneId);
Rambo Wangfe0d7c12022-04-15 03:00:32 +0000992 final int cid = carrierId != null ? carrierId.getSpecificCarrierId()
993 : TelephonyManager.UNKNOWN_CARRIER_ID;
Jayachandran C645ec612020-01-10 10:30:25 -0800994 if (iccid == null) {
995 loge("Cannot save config with null iccid.");
996 return;
997 }
998 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700999 }
Jayachandran C645ec612020-01-10 10:30:25 -08001000
Junda Liu02596502016-11-15 13:46:43 -08001001 // b/32668103 Only save to file if config isn't empty.
1002 // In case of failure, not caching an empty bundle will
1003 // try loading config again on next power on or sim loaded.
1004 // Downside is for genuinely empty bundle, will bind and load
1005 // on every power on.
1006 if (config == null || config.isEmpty()) {
1007 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001008 }
1009
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001010 final String version = getPackageVersion(packageName);
1011 if (version == null) {
1012 loge("Failed to get package version for: " + packageName);
1013 return;
1014 }
1015
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001016 logdWithLocalLog(
1017 "Save config to xml, packagename: " + packageName + " phoneId: " + phoneId);
chen xudb04c292019-03-26 17:01:15 -07001018
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001019 FileOutputStream outFile = null;
1020 try {
Jayachandran C645ec612020-01-10 10:30:25 -08001021 outFile = new FileOutputStream(new File(mContext.getFilesDir(), fileName));
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001022 config.putString(KEY_VERSION, version);
1023 config.writeToStream(outFile);
1024 outFile.flush();
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001025 outFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001026 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001027 loge(e.toString());
1028 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001029 }
1030
Rambo Wang7e3bc122021-03-23 09:24:38 -07001031 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001032 /* package */ void saveConfigToXml(@Nullable String packageName, @NonNull String extraString,
1033 int phoneId, @NonNull CarrierIdentifier carrierId, @NonNull PersistableBundle config) {
Jayachandran C645ec612020-01-10 10:30:25 -08001034 saveConfigToXml(packageName, extraString, phoneId, carrierId, config, false);
1035 }
1036
Rambo Wang7e3bc122021-03-23 09:24:38 -07001037 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001038 /* package */ void saveNoSimConfigToXml(@Nullable String packageName,
1039 @NonNull PersistableBundle config) {
Jayachandran C645ec612020-01-10 10:30:25 -08001040 saveConfigToXml(packageName, "", -1, null, config, true);
1041 }
1042
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001043 /**
1044 * Reads a bundle from an XML file.
1045 *
1046 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +08001047 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001048 *
1049 * In case of errors, or if the saved config is from a different package version than the
1050 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001051 *
Jayachandran C645ec612020-01-10 10:30:25 -08001052 * @param packageName the name of the package from which we fetched this bundle.
1053 * @param extraString An extra string to be used in the XML file name.
1054 * @param phoneId the phone ID.
1055 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001056 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001057 * version does not match, or reading config fails.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001058 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001059 @Nullable
1060 private PersistableBundle restoreConfigFromXml(@Nullable String packageName,
1061 @NonNull String extraString, int phoneId, boolean isNoSimConfig) {
Jayachandran C645ec612020-01-10 10:30:25 -08001062 if (packageName == null) {
1063 loge("Cannot restore config with null packageName");
1064 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001065 final String version = getPackageVersion(packageName);
1066 if (version == null) {
1067 loge("Failed to get package version for: " + packageName);
1068 return null;
1069 }
Yuchen Dongc15afed2018-04-26 17:05:22 +08001070
Jayachandran C645ec612020-01-10 10:30:25 -08001071 String fileName;
arunvoddu664c36a2021-10-11 20:09:28 +00001072 String iccid = null;
Jayachandran C645ec612020-01-10 10:30:25 -08001073 if (isNoSimConfig) {
1074 fileName = getFilenameForNoSimConfig(packageName);
1075 } else {
1076 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
1077 != TelephonyManager.SIM_STATE_LOADED) {
1078 loge("Skip restore config because SIM records are not loaded.");
1079 return null;
1080 }
1081
arunvoddu664c36a2021-10-11 20:09:28 +00001082 iccid = getIccIdForPhoneId(phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001083 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1084 if (iccid == null) {
1085 loge("Cannot restore config with null iccid.");
1086 return null;
1087 }
1088 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001089 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001090
1091 PersistableBundle restoredBundle = null;
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001092 File file = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001093 FileInputStream inFile = null;
1094 try {
Jayachandran C645ec612020-01-10 10:30:25 -08001095 file = new File(mContext.getFilesDir(),fileName);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001096 inFile = new FileInputStream(file);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001097
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001098 restoredBundle = PersistableBundle.readFromStream(inFile);
1099 String savedVersion = restoredBundle.getString(KEY_VERSION);
1100 restoredBundle.remove(KEY_VERSION);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001101
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001102 if (!version.equals(savedVersion)) {
1103 loge("Saved version mismatch: " + version + " vs " + savedVersion);
1104 restoredBundle = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001105 }
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001106
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001107 inFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001108 } catch (FileNotFoundException e) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001109 // Missing file is normal occurrence that might occur with a new sim or when restoring
1110 // an override file during boot and should not be treated as an error.
arunvoddu664c36a2021-10-11 20:09:28 +00001111 if (file != null) {
1112 if (isNoSimConfig) {
1113 logd("File not found: " + file.getPath());
1114 } else {
1115 String filePath = file.getPath();
1116 filePath = getFilePathForLogging(filePath, iccid);
1117 logd("File not found : " + filePath);
1118 }
1119 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001120 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001121 loge(e.toString());
1122 }
1123
1124 return restoredBundle;
1125 }
1126
arunvoddu664c36a2021-10-11 20:09:28 +00001127 /**
1128 * This method will mask most part of iccid in the filepath for logging on userbuild
1129 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001130 @NonNull
1131 private String getFilePathForLogging(@Nullable String filePath, @Nullable String iccid) {
arunvoddu664c36a2021-10-11 20:09:28 +00001132 // If loggable then return with actual file path
1133 if (Rlog.isLoggable(LOG_TAG, Log.VERBOSE)) {
1134 return filePath;
1135 }
1136 String path = filePath;
1137 int length = (iccid != null) ? iccid.length() : 0;
1138 if (length > 5 && filePath != null) {
1139 path = filePath.replace(iccid.substring(5), "***************");
1140 }
1141 return path;
1142 }
1143
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001144 @Nullable
1145 private PersistableBundle restoreConfigFromXml(@Nullable String packageName,
1146 @NonNull String extraString, int phoneId) {
Jayachandran C645ec612020-01-10 10:30:25 -08001147 return restoreConfigFromXml(packageName, extraString, phoneId, false);
1148 }
1149
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001150 @Nullable
1151 private PersistableBundle restoreNoSimConfigFromXml(@Nullable String packageName) {
Jayachandran C645ec612020-01-10 10:30:25 -08001152 return restoreConfigFromXml(packageName, "", -1, true);
1153 }
1154
Junda Liu8a8a53a2015-05-15 16:19:57 -07001155 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001156 * Clears cached carrier config.
1157 * This deletes all saved XML files associated with the given package name. If packageName is
1158 * null, then it deletes all saved XML files.
1159 *
1160 * @param packageName the name of a carrier package, or null if all cached config should be
1161 * cleared.
1162 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -07001163 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001164 private boolean clearCachedConfigForPackage(@Nullable final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001165 File dir = mContext.getFilesDir();
1166 File[] packageFiles = dir.listFiles(new FilenameFilter() {
1167 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001168 if (packageName != null) {
1169 return filename.startsWith("carrierconfig-" + packageName + "-");
1170 } else {
1171 return filename.startsWith("carrierconfig-");
1172 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001173 }
1174 });
Junda Liu8a8a53a2015-05-15 16:19:57 -07001175 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001176 for (File f : packageFiles) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001177 logd("Deleting " + f.getName());
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001178 f.delete();
1179 }
Junda Liu8a8a53a2015-05-15 16:19:57 -07001180 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001181 }
1182
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001183 /** Builds a canonical file name for a config file. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001184 @NonNull
Jayachandran C645ec612020-01-10 10:30:25 -08001185 private static String getFilenameForConfig(
1186 @NonNull String packageName, @NonNull String extraString,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001187 @NonNull String iccid, int cid) {
chen xu02581692018-11-11 19:03:44 -08001188 // the same carrier should have a single copy of XML file named after carrier id.
1189 // However, it's still possible that platform doesn't recognize the current sim carrier,
1190 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
1191 // cases SIM OTA resolves to different carrier while iccid remains the same.
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001192 return "carrierconfig-" + packageName + extraString + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001193 }
1194
Jayachandran C645ec612020-01-10 10:30:25 -08001195 /** Builds a canonical file name for no SIM config file. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001196 @NonNull
Jayachandran C645ec612020-01-10 10:30:25 -08001197 private String getFilenameForNoSimConfig(@NonNull String packageName) {
1198 return "carrierconfig-" + packageName + "-" + "nosim" + ".xml";
1199 }
1200
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001201 /** Return the current version code of a package, or null if the name is not found. */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001202 @Nullable
1203 private String getPackageVersion(@NonNull String packageName) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001204 try {
1205 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -08001206 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001207 } catch (PackageManager.NameNotFoundException e) {
1208 return null;
1209 }
1210 }
1211
Jonathan Basseri65273c82017-07-25 15:08:42 -07001212 /**
1213 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001214 *
1215 * This reads config bundles for the given phoneId. That means getting the latest bundle from
1216 * the default app and a privileged carrier app, if present. This will not bind to an app if we
1217 * have a saved config file to use instead.
1218 */
1219 private void updateConfigForPhoneId(int phoneId) {
Jonathan Basseri65273c82017-07-25 15:08:42 -07001220 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001221 }
1222
Malcolm Chen5ea18532019-10-24 19:55:44 -07001223 private void onMultiSimConfigChanged() {
1224 for (int i = TelephonyManager.from(mContext).getActiveModemCount();
1225 i < mConfigFromDefaultApp.length; i++) {
1226 clearConfigForPhone(i, false);
1227 }
1228 }
1229
Hall Liu506724b2018-10-22 18:16:14 -07001230 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001231 @NonNull
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001232 public PersistableBundle getConfigForSubId(int subscriptionId, @NonNull String callingPackage) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001233 return getConfigForSubIdWithFeature(subscriptionId, callingPackage, null);
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08001234 }
1235
1236 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001237 @NonNull
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001238 public PersistableBundle getConfigForSubIdWithFeature(int subscriptionId,
1239 @NonNull String callingPackage, @Nullable String callingFeatureId) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001240 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subscriptionId,
1241 callingPackage, callingFeatureId, "getCarrierConfig")) {
Rambo Wang42026112021-04-07 20:57:28 +00001242 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -07001243 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -07001244
Rambo Wangd2b004b2021-03-30 10:10:23 -07001245 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001246 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001247 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001248 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001249 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001250 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001251 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001252 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001253 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001254 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001255 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001256 config = mPersistentOverrideConfigs[phoneId];
1257 if (config != null) {
1258 retConfig.putAll(config);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001259 }
Hall Liu506724b2018-10-22 18:16:14 -07001260 config = mOverrideConfigs[phoneId];
1261 if (config != null) {
1262 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -07001263 }
Nathan Harold1122e3d2021-01-22 15:45:24 -08001264 // Ignore the theoretical case of the default app not being present since that won't
1265 // work in CarrierConfigLoader today.
1266 final boolean allConfigsApplied =
1267 (mConfigFromCarrierApp[phoneId] != null
1268 || getCarrierPackageForPhoneId(phoneId) == null)
1269 && mConfigFromDefaultApp[phoneId] != null;
1270 retConfig.putBoolean(
1271 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, allConfigsApplied);
Jayachandran C645ec612020-01-10 10:30:25 -08001272 } else {
1273 if (mNoSimConfig != null) {
1274 retConfig.putAll(mNoSimConfig);
1275 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001276 }
1277 return retConfig;
1278 }
1279
1280 @Override
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001281 public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides,
1282 boolean persistent) {
Hall Liu506724b2018-10-22 18:16:14 -07001283 mContext.enforceCallingOrSelfPermission(
1284 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Brad Ebingerf6281ad2019-04-25 11:02:04 -07001285 //TODO: Also check for SHELL UID to restrict this method to testing only (b/131326259)
Hall Liu506724b2018-10-22 18:16:14 -07001286 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1287 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001288 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
Rambo Wangd2b004b2021-03-30 10:10:23 -07001289 throw new IllegalArgumentException(
1290 "Invalid phoneId " + phoneId + " for subId " + subscriptionId);
Hall Liu506724b2018-10-22 18:16:14 -07001291 }
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001292 // Post to run on handler thread on which all states should be confined.
1293 mHandler.post(() -> {
1294 overrideConfig(mOverrideConfigs, phoneId, overrides);
Hall Liu506724b2018-10-22 18:16:14 -07001295
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001296 if (persistent) {
1297 overrideConfig(mPersistentOverrideConfigs, phoneId, overrides);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001298
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001299 if (overrides != null) {
1300 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
1301 saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION,
1302 phoneId,
1303 carrierId, mPersistentOverrideConfigs[phoneId]);
1304 } else {
1305 final String iccid = getIccIdForPhoneId(phoneId);
1306 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1307 String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage,
1308 OVERRIDE_PACKAGE_ADDITION, iccid, cid);
1309 File fileToDelete = new File(mContext.getFilesDir(), fileName);
1310 fileToDelete.delete();
1311 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001312 }
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001313 notifySubscriptionInfoUpdater(phoneId);
1314 });
Hall Liu506724b2018-10-22 18:16:14 -07001315 }
1316
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001317 private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId,
1318 @Nullable PersistableBundle overrides) {
1319 if (overrides == null) {
Rambo Wang42026112021-04-07 20:57:28 +00001320 currentOverrides[phoneId] = new PersistableBundle();
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001321 } else if (currentOverrides[phoneId] == null) {
1322 currentOverrides[phoneId] = overrides;
1323 } else {
1324 currentOverrides[phoneId].putAll(overrides);
1325 }
1326 }
1327
Hall Liu506724b2018-10-22 18:16:14 -07001328 @Override
Rambo Wangd2b004b2021-03-30 10:10:23 -07001329 public void notifyConfigChangedForSubId(int subscriptionId) {
Jordan Liud7d57fe2019-04-16 12:29:43 -07001330 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001331 // system privileged app with MODIFY_PHONE_STATE permission.
Rambo Wangd2b004b2021-03-30 10:10:23 -07001332 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext,
1333 subscriptionId, "Require carrier privileges or MODIFY_PHONE_STATE permission.");
1334
1335 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1336 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1337 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
1338 throw new IllegalArgumentException(
1339 "Invalid phoneId " + phoneId + " for subId " + subscriptionId);
1340 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001341
1342 // This method should block until deleting has completed, so that an error which prevents us
1343 // from clearing the cache is passed back to the carrier app. With the files successfully
1344 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001345 String callingPackageName = mContext.getPackageManager().getNameForUid(
1346 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001347 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001348 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001349 }
1350
1351 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001352 public void updateConfigForPhoneId(int phoneId, @NonNull String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -07001353 mContext.enforceCallingOrSelfPermission(
1354 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001355 logdWithLocalLog("Update config for phoneId: " + phoneId + " simState: " + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001356 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001357 throw new IllegalArgumentException("Invalid phoneId: " + phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001358 }
1359 // requires Java 7 for switch on string.
1360 switch (simState) {
1361 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1362 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001363 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001364 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Malcolm Chendb66de12019-12-09 18:57:07 -08001365 case IccCardConstants.INTENT_VALUE_ICC_NOT_READY:
Junda Liu9f2d2712015-05-15 14:22:45 -07001366 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001367 break;
1368 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1369 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001370 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001371 break;
1372 }
1373 }
1374
Junda Liu43d723a2015-05-12 17:23:45 -07001375 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001376 @NonNull
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001377 public String getDefaultCarrierServicePackageName() {
Shuo Qianefdb7962019-12-19 15:54:27 -08001378 mContext.enforceCallingOrSelfPermission(
1379 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1380 "getDefaultCarrierServicePackageName");
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001381 return mPlatformCarrierConfigPackage;
1382 }
1383
Rambo Wang7e3bc122021-03-23 09:24:38 -07001384 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001385 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -07001386 /* package */ Handler getHandler() {
1387 return mHandler;
1388 }
1389
1390 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001391 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001392 /* package */ PersistableBundle getConfigFromDefaultApp(int phoneId) {
1393 return mConfigFromDefaultApp[phoneId];
1394 }
1395
1396 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001397 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001398 /* package */ PersistableBundle getConfigFromCarrierApp(int phoneId) {
1399 return mConfigFromCarrierApp[phoneId];
1400 }
1401
1402 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001403 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -07001404 /* package */ PersistableBundle getNoSimConfig() {
1405 return mNoSimConfig;
1406 }
1407
1408 @VisibleForTesting
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001409 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001410 /* package */ PersistableBundle getOverrideConfig(int phoneId) {
1411 return mOverrideConfigs[phoneId];
1412 }
1413
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001414 private void unbindIfBound(@NonNull Context context, @NonNull CarrierServiceConnection conn,
Rambo Wangb0f3e2f2021-10-28 12:40:12 -07001415 int phoneId) {
1416 if (mServiceBound[phoneId]) {
1417 mServiceBound[phoneId] = false;
1418 context.unbindService(conn);
1419 }
1420 }
1421
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001422 private void unbindIfBoundForNoSimConfig(@NonNull Context context,
1423 @NonNull CarrierServiceConnection conn, int phoneId) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -07001424 if (mServiceBoundForNoSimConfig[phoneId]) {
1425 mServiceBoundForNoSimConfig[phoneId] = false;
Jayachandran C645ec612020-01-10 10:30:25 -08001426 context.unbindService(conn);
1427 }
1428 }
1429
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001430 /**
Rambo Wang610fdf62021-03-08 21:37:11 -08001431 * Returns a boxed Integer object for phoneId, services as message token to distinguish messages
1432 * with same code when calling {@link Handler#removeMessages(int, Object)}.
1433 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001434 @NonNull
Rambo Wang610fdf62021-03-08 21:37:11 -08001435 private Integer getMessageToken(int phoneId) {
1436 if (phoneId < -128 || phoneId > 127) {
1437 throw new IllegalArgumentException("phoneId should be in range [-128, 127], inclusive");
1438 }
1439 // Integer#valueOf guarantees the integers within [-128, 127] are cached and thus memory
1440 // comparison (==) returns true for the same integer.
1441 return Integer.valueOf(phoneId);
1442 }
1443
1444 /**
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001445 * If {@code args} contains {@link #DUMP_ARG_REQUESTING_PACKAGE} and a following package name,
1446 * we'll also call {@link IBinder#dump} on the default carrier service (if bound) and the
1447 * specified carrier service (if bound). Typically, this is done for connectivity bug reports
1448 * where we don't call {@code dumpsys activity service all-non-platform} because that contains
1449 * too much info, but we still want to let carrier apps include their diagnostics.
1450 */
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001451 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001452 public void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter pw, @NonNull String[] args) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001453 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
Junda Liu43d723a2015-05-12 17:23:45 -07001454 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1455 != PackageManager.PERMISSION_GRANTED) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001456 indentPW.println("Permission Denial: can't dump carrierconfig from from pid="
Junda Liu43d723a2015-05-12 17:23:45 -07001457 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1458 return;
1459 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001460 String requestingPackage = null;
1461 int requestingPackageIndex = ArrayUtils.indexOf(args, DUMP_ARG_REQUESTING_PACKAGE);
1462 if (requestingPackageIndex >= 0 && requestingPackageIndex < args.length - 1
1463 && !TextUtils.isEmpty(args[requestingPackageIndex + 1])) {
1464 requestingPackage = args[requestingPackageIndex + 1];
1465 // Throws a SecurityException if the caller is impersonating another app in an effort to
1466 // dump extra info (which may contain PII the caller doesn't have a right to).
1467 enforceCallerIsSystemOrRequestingPackage(requestingPackage);
1468 }
1469
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001470 indentPW.println("CarrierConfigLoader: " + this);
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001471 for (int i = 0; i < TelephonyManager.from(mContext).getActiveModemCount(); i++) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001472 indentPW.println("Phone Id = " + i);
shuoq26a3a4c2016-12-16 11:06:48 -08001473 // display default values in CarrierConfigManager
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001474 printConfig(CarrierConfigManager.getDefaultConfig(), indentPW,
shuoq26a3a4c2016-12-16 11:06:48 -08001475 "Default Values from CarrierConfigManager");
shuoq26a3a4c2016-12-16 11:06:48 -08001476 // display ConfigFromDefaultApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001477 printConfig(mConfigFromDefaultApp[i], indentPW, "mConfigFromDefaultApp");
shuoq26a3a4c2016-12-16 11:06:48 -08001478 // display ConfigFromCarrierApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001479 printConfig(mConfigFromCarrierApp[i], indentPW, "mConfigFromCarrierApp");
1480 printConfig(mPersistentOverrideConfigs[i], indentPW, "mPersistentOverrideConfigs");
1481 printConfig(mOverrideConfigs[i], indentPW, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001482 }
chen xudb04c292019-03-26 17:01:15 -07001483
Jayachandran C645ec612020-01-10 10:30:25 -08001484 printConfig(mNoSimConfig, indentPW, "mNoSimConfig");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001485 indentPW.println("CarrierConfigLoadingLog=");
1486 mCarrierConfigLoadingLog.dump(fd, indentPW, args);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001487
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001488 if (requestingPackage != null) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001489 logd("Including default and requesting package " + requestingPackage
1490 + " carrier services in dump");
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001491 indentPW.println("");
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001492 indentPW.println("Connected services");
1493 dumpCarrierServiceIfBound(fd, indentPW, "Default config package",
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001494 mPlatformCarrierConfigPackage, false /* considerCarrierPrivileges */);
1495 dumpCarrierServiceIfBound(fd, indentPW, "Requesting package", requestingPackage,
1496 true /* considerCarrierPrivileges */);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001497 }
shuoq26a3a4c2016-12-16 11:06:48 -08001498 }
1499
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001500 private void printConfig(@NonNull PersistableBundle configApp,
1501 @NonNull IndentingPrintWriter indentPW, @NonNull String name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001502 indentPW.increaseIndent();
shuoq26a3a4c2016-12-16 11:06:48 -08001503 if (configApp == null) {
shuoq26a3a4c2016-12-16 11:06:48 -08001504 indentPW.println(name + " : null ");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001505 indentPW.decreaseIndent();
1506 indentPW.println("");
shuoq26a3a4c2016-12-16 11:06:48 -08001507 return;
1508 }
shuoq26a3a4c2016-12-16 11:06:48 -08001509 indentPW.println(name + " : ");
1510 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1511 Collections.sort(sortedKeys);
1512 indentPW.increaseIndent();
1513 indentPW.increaseIndent();
1514 for (String key : sortedKeys) {
1515 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1516 indentPW.println(key + " = " +
1517 Arrays.toString((Object[]) configApp.get(key)));
1518 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1519 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1520 } else {
1521 indentPW.println(key + " = " + configApp.get(key));
1522 }
Junda Liu43d723a2015-05-12 17:23:45 -07001523 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001524 indentPW.decreaseIndent();
1525 indentPW.decreaseIndent();
1526 indentPW.decreaseIndent();
1527 indentPW.println("");
Junda Liu43d723a2015-05-12 17:23:45 -07001528 }
1529
Hunter Knepshielde601f432020-02-19 10:16:04 -08001530 /**
1531 * Passes without problem when one of these conditions is true:
1532 * - The caller is a privileged UID (e.g. for dumpstate.cpp generating a bug report, where the
1533 * system knows the true caller plumbed in through the {@link android.os.BugreportManager} API).
1534 * - The caller's UID matches the supplied package.
1535 *
1536 * @throws SecurityException if none of the above conditions are met.
1537 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001538 private void enforceCallerIsSystemOrRequestingPackage(@NonNull String requestingPackage)
Hunter Knepshielde601f432020-02-19 10:16:04 -08001539 throws SecurityException {
1540 final int callingUid = Binder.getCallingUid();
1541 if (callingUid == Process.ROOT_UID || callingUid == Process.SYSTEM_UID
1542 || callingUid == Process.SHELL_UID || callingUid == Process.PHONE_UID) {
1543 // Bug reports (dumpstate.cpp) run as SHELL, and let some other privileged UIDs through
1544 // as well.
1545 return;
1546 }
1547 // An app is trying to dump extra detail, block it if they aren't who they claim to be.
1548 AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class);
1549 if (appOps == null) {
1550 throw new SecurityException("No AppOps");
1551 }
1552 // Will throw a SecurityException if the UID and package don't match.
1553 appOps.checkPackage(callingUid, requestingPackage);
1554 }
1555
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001556 /**
1557 * Searches for one or more appropriate {@link CarrierService} instances to dump based on the
1558 * current connections.
1559 *
1560 * @param targetPkgName the target package name to dump carrier services for
1561 * @param considerCarrierPrivileges if true, allow a carrier service to be dumped if it shares
1562 * carrier privileges with {@code targetPkgName};
1563 * otherwise, only dump a carrier service if it is {@code
1564 * targetPkgName}
1565 */
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001566 private void dumpCarrierServiceIfBound(@NonNull FileDescriptor fd,
1567 @NonNull IndentingPrintWriter indentPW, @NonNull String prefix,
1568 @NonNull String targetPkgName, boolean considerCarrierPrivileges) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001569 // Null package is possible if it's early in the boot process, there was a recent crash, we
1570 // loaded the config from XML most recently, or a SIM slot is empty. Carrier apps with
1571 // long-lived bindings should typically get dumped here regardless. Even if an app is being
1572 // used for multiple phoneIds, we assume that it's smart enough to handle that on its own,
1573 // and that in most cases we'd just be dumping duplicate information and bloating a report.
1574 indentPW.increaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001575 indentPW.println(prefix + " : " + targetPkgName);
1576 Set<String> dumpedPkgNames = new ArraySet<>(mServiceConnection.length);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001577 for (CarrierServiceConnection connection : mServiceConnection) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001578 if (connection == null || !SubscriptionManager.isValidPhoneId(connection.phoneId)
1579 || TextUtils.isEmpty(connection.pkgName)) {
1580 continue;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001581 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001582 final String servicePkgName = connection.pkgName;
1583 // Note: we intentionally ignore system components here because we should NOT match the
1584 // shell caller that's typically used for bug reports via non-BugreportManager triggers.
1585 final boolean exactPackageMatch = TextUtils.equals(targetPkgName, servicePkgName);
1586 final boolean carrierPrivilegesMatch =
1587 considerCarrierPrivileges && hasCarrierPrivileges(targetPkgName,
1588 connection.phoneId);
1589 if (!exactPackageMatch && !carrierPrivilegesMatch) continue;
1590 // Make sure this service is actually alive before trying to dump it. We don't pay
1591 // attention to mServiceBound[connection.phoneId] because typically carrier apps will
1592 // request long-lived bindings, and even if we unbind the app, it may still be alive due
1593 // to CarrierServiceBindHelper. Pull it out as a reference so even if it gets set to
1594 // null within the ServiceConnection during unbinding we can avoid an NPE.
1595 final IBinder service = connection.service;
1596 if (service == null || !service.isBinderAlive() || !service.pingBinder()) continue;
1597 // We've got a live service. Last check is just to make sure we don't dump a package
1598 // multiple times.
1599 if (!dumpedPkgNames.add(servicePkgName)) continue;
1600 if (!exactPackageMatch) {
1601 logd(targetPkgName + " has carrier privileges on phoneId " + connection.phoneId
1602 + ", service provided by " + servicePkgName);
1603 indentPW.increaseIndent();
1604 indentPW.println("Proxy : " + servicePkgName);
1605 indentPW.decreaseIndent();
1606 }
1607 // Flush before we let the app output anything to ensure correct ordering of output.
1608 // Internally, Binder#dump calls flush on its printer after finishing so we don't
1609 // need to do anything after.
1610 indentPW.flush();
1611 try {
1612 logd("Dumping " + servicePkgName);
1613 // We don't need to give the carrier service any args.
1614 connection.service.dump(fd, null /* args */);
1615 logd("Done with " + servicePkgName);
1616 } catch (RemoteException e) {
1617 logd("RemoteException from " + servicePkgName, e);
1618 indentPW.increaseIndent();
1619 indentPW.println("RemoteException");
1620 indentPW.increaseIndent();
1621 e.printStackTrace(indentPW);
1622 indentPW.decreaseIndent();
1623 indentPW.decreaseIndent();
1624 // We won't retry this package again because now it's in dumpedPkgNames.
1625 }
1626 indentPW.println("");
Jordan Liu46ed5db2020-01-28 08:55:20 -08001627 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001628 if (dumpedPkgNames.isEmpty()) {
1629 indentPW.increaseIndent();
1630 indentPW.println("Not bound");
1631 indentPW.decreaseIndent();
1632 indentPW.println("");
1633 }
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001634 indentPW.decreaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001635 }
1636
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001637 private boolean hasCarrierPrivileges(@NonNull String pkgName, int phoneId) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001638 int[] subIds = SubscriptionManager.getSubId(phoneId);
1639 if (ArrayUtils.isEmpty(subIds)) {
1640 return false;
1641 }
1642 return TelephonyManager.from(mContext).createForSubscriptionId(
1643 subIds[0]).checkCarrierPrivilegesForPackage(pkgName)
1644 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001645 }
1646
Zach Johnson36d7aab2015-05-22 15:43:00 -07001647 private class CarrierServiceConnection implements ServiceConnection {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001648 final int phoneId;
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001649 @NonNull final String pkgName;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001650 final int eventId;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001651 IBinder service;
1652
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001653 CarrierServiceConnection(int phoneId, @NonNull String pkgName, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001654 this.phoneId = phoneId;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001655 this.pkgName = pkgName;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001656 this.eventId = eventId;
1657 }
1658
1659 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001660 public void onServiceConnected(@NonNull ComponentName name, @NonNull IBinder service) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001661 logd("Connected to config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001662 this.service = service;
1663 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1664 }
1665
1666 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001667 public void onServiceDisconnected(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001668 logd("Disconnected from config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001669 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001670 }
1671
1672 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001673 public void onBindingDied(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001674 logd("Binding died from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001675 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001676 }
1677
1678 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001679 public void onNullBinding(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001680 logd("Null binding from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001681 this.service = null;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001682 }
1683 }
1684
1685 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1686 @Override
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001687 public void onReceive(@NonNull Context context, @NonNull Intent intent) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001688 String action = intent.getAction();
Junda Liu8a8a53a2015-05-15 16:19:57 -07001689 boolean replace = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1690 // If replace is true, only care ACTION_PACKAGE_REPLACED.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001691 if (replace && !Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
Junda Liu8a8a53a2015-05-15 16:19:57 -07001692 return;
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001693 }
Junda Liu8a8a53a2015-05-15 16:19:57 -07001694
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001695 switch (action) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001696 case Intent.ACTION_BOOT_COMPLETED:
1697 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1698 break;
1699
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001700 case Intent.ACTION_PACKAGE_ADDED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001701 case Intent.ACTION_PACKAGE_REMOVED:
Junda Liu8a8a53a2015-05-15 16:19:57 -07001702 case Intent.ACTION_PACKAGE_REPLACED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001703 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1704 String packageName = mContext.getPackageManager().getNameForUid(uid);
Junda Liu8a8a53a2015-05-15 16:19:57 -07001705 if (packageName != null) {
1706 // We don't have a phoneId for arg1.
1707 mHandler.sendMessage(
1708 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, packageName));
1709 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001710 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001711 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001712 }
1713 }
1714
Rambo Wangab13e3e2021-04-08 15:01:06 -07001715 // Get readable string for the message code supported in this class.
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001716 @NonNull
Rambo Wangab13e3e2021-04-08 15:01:06 -07001717 private static String eventToString(int code) {
1718 switch (code) {
1719 case EVENT_CLEAR_CONFIG:
1720 return "EVENT_CLEAR_CONFIG";
1721 case EVENT_CONNECTED_TO_DEFAULT:
1722 return "EVENT_CONNECTED_TO_DEFAULT";
1723 case EVENT_CONNECTED_TO_CARRIER:
1724 return "EVENT_CONNECTED_TO_CARRIER";
1725 case EVENT_FETCH_DEFAULT_DONE:
1726 return "EVENT_FETCH_DEFAULT_DONE";
1727 case EVENT_FETCH_CARRIER_DONE:
1728 return "EVENT_FETCH_CARRIER_DONE";
1729 case EVENT_DO_FETCH_DEFAULT:
1730 return "EVENT_DO_FETCH_DEFAULT";
1731 case EVENT_DO_FETCH_CARRIER:
1732 return "EVENT_DO_FETCH_CARRIER";
1733 case EVENT_PACKAGE_CHANGED:
1734 return "EVENT_PACKAGE_CHANGED";
1735 case EVENT_BIND_DEFAULT_TIMEOUT:
1736 return "EVENT_BIND_DEFAULT_TIMEOUT";
1737 case EVENT_BIND_CARRIER_TIMEOUT:
1738 return "EVENT_BIND_CARRIER_TIMEOUT";
1739 case EVENT_CHECK_SYSTEM_UPDATE:
1740 return "EVENT_CHECK_SYSTEM_UPDATE";
1741 case EVENT_SYSTEM_UNLOCKED:
1742 return "EVENT_SYSTEM_UNLOCKED";
1743 case EVENT_FETCH_DEFAULT_TIMEOUT:
1744 return "EVENT_FETCH_DEFAULT_TIMEOUT";
1745 case EVENT_FETCH_CARRIER_TIMEOUT:
1746 return "EVENT_FETCH_CARRIER_TIMEOUT";
1747 case EVENT_SUBSCRIPTION_INFO_UPDATED:
1748 return "EVENT_SUBSCRIPTION_INFO_UPDATED";
1749 case EVENT_MULTI_SIM_CONFIG_CHANGED:
1750 return "EVENT_MULTI_SIM_CONFIG_CHANGED";
1751 case EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG:
1752 return "EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG";
1753 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE:
1754 return "EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE";
1755 case EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG:
1756 return "EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG";
1757 case EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
1758 return "EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT";
1759 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
1760 return "EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT";
1761 default:
1762 return "UNKNOWN(" + code + ")";
1763 }
1764 }
1765
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001766 private void logd(@NonNull String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001767 Log.d(LOG_TAG, msg);
1768 }
1769
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001770 private void logd(@NonNull String msg, Throwable tr) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001771 Log.d(LOG_TAG, msg, tr);
1772 }
1773
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001774 private void logdWithLocalLog(@NonNull String msg) {
chen xudb04c292019-03-26 17:01:15 -07001775 Log.d(LOG_TAG, msg);
1776 mCarrierConfigLoadingLog.log(msg);
1777 }
1778
Rambo Wangfe0d7c12022-04-15 03:00:32 +00001779 private void loge(@NonNull String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001780 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07001781 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001782 }
1783}