blob: 8728720433c88ff24405efe81b5edf933ae08e6f [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 Wang1b89db32022-04-15 03:00:32 +000091 @NonNull private final String mPlatformCarrierConfigPackage;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080092
93 /** The singleton instance. */
Rambo Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-04-15 03:00:32 +0000121 @NonNull private boolean[] mFromSystemUnlocked;
122
Nathan Harold48ac0972019-03-13 22:33:01 -0700123 // SubscriptionInfoUpdater
Rambo Wang1b89db32022-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 Wang1b89db32022-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 Wang1b89db32022-04-15 03:00:32 +0000129 @NonNull private final BroadcastReceiver mPackageReceiver = new ConfigLoaderBroadcastReceiver();
130 @NonNull private final LocalLog mCarrierConfigLoadingLog = new LocalLog(100);
Rambo Wang6f8328d2022-04-15 01:26:23 +0000131 // Number of phone instances (active modem count)
132 private int mNumPhones;
chen xudb04c292019-03-26 17:01:15 -0700133
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800134
135 // Message codes; see mHandler below.
136 // Request from SubscriptionInfoUpdater when SIM becomes absent or error.
137 private static final int EVENT_CLEAR_CONFIG = 0;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800138 // Has connected to default app.
139 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
140 // Has connected to carrier app.
141 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700142 // Config has been loaded from default app (or cache).
143 private static final int EVENT_FETCH_DEFAULT_DONE = 5;
144 // Config has been loaded from carrier app (or cache).
145 private static final int EVENT_FETCH_CARRIER_DONE = 6;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700146 // Attempt to fetch from default app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700147 private static final int EVENT_DO_FETCH_DEFAULT = 7;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700148 // Attempt to fetch from carrier app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700149 private static final int EVENT_DO_FETCH_CARRIER = 8;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700150 // A package has been installed, uninstalled, or updated.
151 private static final int EVENT_PACKAGE_CHANGED = 9;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700152 // Bind timed out for the default app.
153 private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
154 // Bind timed out for a carrier app.
155 private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700156 // Check if the system fingerprint has changed.
157 private static final int EVENT_CHECK_SYSTEM_UPDATE = 12;
Nanxi Chen3d670502016-03-17 16:32:09 -0700158 // Rerun carrier config binding after system is unlocked.
159 private static final int EVENT_SYSTEM_UNLOCKED = 13;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700160 // Fetching config timed out from the default app.
161 private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14;
162 // Fetching config timed out from a carrier app.
163 private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15;
Nathan Harold48ac0972019-03-13 22:33:01 -0700164 // SubscriptionInfoUpdater has finished updating the sub for the carrier config.
165 private static final int EVENT_SUBSCRIPTION_INFO_UPDATED = 16;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700166 // Multi-SIM config changed.
167 private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 17;
Jayachandran C645ec612020-01-10 10:30:25 -0800168 // Attempt to fetch from default app or read from XML for no SIM case.
169 private static final int EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG = 18;
170 // No SIM config has been loaded from default app (or cache).
171 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE = 19;
172 // Has connected to default app for no SIM config.
173 private static final int EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG = 20;
174 // Bind timed out for the default app when trying to fetch no SIM config.
175 private static final int EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 21;
176 // Fetching config timed out from the default app for no SIM config.
177 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 22;
Rambo Wangab13e3e2021-04-08 15:01:06 -0700178 // NOTE: any new EVENT_* values must be added to method eventToString().
Jonathan Basseri930701e2015-06-11 20:56:43 -0700179
Junda Liu6cb45002016-03-22 17:18:20 -0700180 private static final int BIND_TIMEOUT_MILLIS = 30000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800181
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800182 // Keys used for saving and restoring config bundle from file.
183 private static final String KEY_VERSION = "__carrier_config_package_version__";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800184
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100185 private static final String OVERRIDE_PACKAGE_ADDITION = "-override";
186
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700187 // SharedPreferences key for last known build fingerprint.
188 private static final String KEY_FINGERPRINT = "build_fingerprint";
189
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800190 // Argument for #dump that indicates we should also call the default and specified carrier
191 // service's #dump method. In multi-SIM devices, it's possible that carrier A is on SIM 1 and
192 // carrier B is on SIM 2, in which case we should not dump carrier B's service when carrier A
193 // requested the dump.
194 private static final String DUMP_ARG_REQUESTING_PACKAGE = "--requesting-package";
195
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800196 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700197 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700198 // For each phoneId, the event sequence should be:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700199 // fetch default, connected to default, fetch default (async), fetch default done,
200 // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700201 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700202 // If there is a saved config file for either the default app or the carrier app, we skip
203 // binding to the app and go straight from fetch to loaded.
204 //
205 // At any time, at most one connection is active. If events are not in this order, previous
206 // connection will be unbound, so only latest event takes effect.
207 //
208 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
209 // 1. loading from carrier app (even if read from a file)
210 // 2. loading from default app if there is no carrier app (even if read from a file)
211 // 3. clearing config (e.g. due to sim removal)
212 // 4. encountering bind or IPC error
Jonathan Basseri65273c82017-07-25 15:08:42 -0700213 private class ConfigHandler extends Handler {
Rambo Wang7e3bc122021-03-23 09:24:38 -0700214 ConfigHandler(@NonNull Looper looper) {
215 super(looper);
216 }
217
Jonathan Basseri65273c82017-07-25 15:08:42 -0700218 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +0000219 public void handleMessage(@NonNull Message msg) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700220 final int phoneId = msg.arg1;
Rambo Wangab13e3e2021-04-08 15:01:06 -0700221 logdWithLocalLog("mHandler: " + eventToString(msg.what) + " phoneId: " + phoneId);
Malcolm Chen5ea18532019-10-24 19:55:44 -0700222 if (!SubscriptionManager.isValidPhoneId(phoneId)
223 && msg.what != EVENT_MULTI_SIM_CONFIG_CHANGED) {
224 return;
225 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800226 switch (msg.what) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800227 case EVENT_CLEAR_CONFIG: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700228 clearConfigForPhone(phoneId, true);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800229 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700230 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700231
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800232 case EVENT_SYSTEM_UNLOCKED: {
Rambo Wang6f8328d2022-04-15 01:26:23 +0000233 for (int i = 0; i < mNumPhones; ++i) {
Sarah Chin807d5c62020-03-30 17:21:09 -0700234 // When the user unlocks the device, send the broadcast again (with a
235 // rebroadcast extra) if we have sent it before unlock. This will avoid
236 // trying to load the carrier config when the SIM is still loading when the
237 // unlock happens.
Junda Liu03aad762017-07-21 13:32:17 -0700238 if (mHasSentConfigChange[i]) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800239 logdWithLocalLog("System unlocked");
Sarah Chin807d5c62020-03-30 17:21:09 -0700240 mFromSystemUnlocked[i] = true;
Junda Liu03aad762017-07-21 13:32:17 -0700241 updateConfigForPhoneId(i);
242 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700243 }
244 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700245 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700246
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800247 case EVENT_PACKAGE_CHANGED: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700248 final String carrierPackageName = (String) msg.obj;
249 // Only update if there are cached config removed to avoid updating config for
250 // unrelated packages.
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700251 if (clearCachedConfigForPackage(carrierPackageName)) {
Rambo Wang6f8328d2022-04-15 01:26:23 +0000252 for (int i = 0; i < mNumPhones; ++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 Wang1b89db32022-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 Wang1b89db32022-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
Rambo Wang6f8328d2022-04-15 01:26:23 +0000697 IntentFilter systemEventsFilter = new IntentFilter();
698 systemEventsFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
699 systemEventsFilter.addAction(TelephonyManager.ACTION_MULTI_SIM_CONFIG_CHANGED);
700 context.registerReceiver(mBootReceiver, systemEventsFilter);
Nanxi Chen3d670502016-03-17 16:32:09 -0700701
Rambo Wang6f8328d2022-04-15 01:26:23 +0000702 // TODO(b/200277555): Switch to CarrierPrivilegesCallback to monitor carrier app change
Junda Liu8a8a53a2015-05-15 16:19:57 -0700703 // Register for package updates. Update app or uninstall app update will have all 3 intents,
704 // in the order or removed, added, replaced, all with extra_replace set to true.
705 IntentFilter pkgFilter = new IntentFilter();
706 pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
707 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
708 pkgFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
709 pkgFilter.addDataScheme("package");
Jordan Liu5ca24762019-07-10 12:51:09 -0700710 context.registerReceiver(mPackageReceiver, pkgFilter);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800711
Rambo Wang6f8328d2022-04-15 01:26:23 +0000712 mNumPhones = TelephonyManager.from(context).getActiveModemCount();
713 mConfigFromDefaultApp = new PersistableBundle[mNumPhones];
714 mConfigFromCarrierApp = new PersistableBundle[mNumPhones];
715 mPersistentOverrideConfigs = new PersistableBundle[mNumPhones];
716 mOverrideConfigs = new PersistableBundle[mNumPhones];
Rambo Wang42026112021-04-07 20:57:28 +0000717 mNoSimConfig = new PersistableBundle();
Rambo Wang6f8328d2022-04-15 01:26:23 +0000718 mServiceConnection = new CarrierServiceConnection[mNumPhones];
719 mServiceBound = new boolean[mNumPhones];
720 mHasSentConfigChange = new boolean[mNumPhones];
721 mFromSystemUnlocked = new boolean[mNumPhones];
722 mServiceConnectionForNoSimConfig = new CarrierServiceConnection[mNumPhones];
723 mServiceBoundForNoSimConfig = new boolean[mNumPhones];
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800724 logd("CarrierConfigLoader has started");
Rambo Wang7e3bc122021-03-23 09:24:38 -0700725 mSubscriptionInfoUpdater = subscriptionInfoUpdater;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700726 mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800727 }
728
729 /**
730 * Initialize the singleton CarrierConfigLoader instance.
731 *
732 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
733 */
Rambo Wang1b89db32022-04-15 03:00:32 +0000734 @NonNull
735 /* package */ static CarrierConfigLoader init(@NonNull Context context) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800736 synchronized (CarrierConfigLoader.class) {
737 if (sInstance == null) {
Rambo Wang7e3bc122021-03-23 09:24:38 -0700738 sInstance = new CarrierConfigLoader(context,
739 PhoneFactory.getSubscriptionInfoUpdater(), Looper.myLooper());
Brad Ebingerfa6575f2021-05-04 00:29:50 +0000740 // Make this service available through ServiceManager.
741 TelephonyFrameworkInitializer.getTelephonyServiceManager()
742 .getCarrierConfigServiceRegisterer().register(sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800743 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700744 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800745 }
746 return sInstance;
747 }
748 }
749
Rambo Wang7e3bc122021-03-23 09:24:38 -0700750 @VisibleForTesting
751 /* package */ void clearConfigForPhone(int phoneId, boolean fetchNoSimConfig) {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700752 /* Ignore clear configuration request if device is being shutdown. */
753 Phone phone = PhoneFactory.getPhone(phoneId);
754 if (phone != null) {
755 if (phone.isShuttingDown()) {
756 return;
757 }
758 }
759
760 mConfigFromDefaultApp[phoneId] = null;
761 mConfigFromCarrierApp[phoneId] = null;
762 mServiceConnection[phoneId] = null;
763 mHasSentConfigChange[phoneId] = false;
764
Jayachandran C645ec612020-01-10 10:30:25 -0800765 if (fetchNoSimConfig) {
766 // To fetch no SIM config
767 mHandler.sendMessage(
768 mHandler.obtainMessage(
769 EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG, phoneId, -1));
770 }
Malcolm Chen5ea18532019-10-24 19:55:44 -0700771 }
772
Nathan Harold48ac0972019-03-13 22:33:01 -0700773 private void notifySubscriptionInfoUpdater(int phoneId) {
774 String configPackagename;
775 PersistableBundle configToSend;
776 int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
777 // Prefer the carrier privileged carrier app, but if there is not one, use the platform
778 // default carrier app.
779 if (mConfigFromCarrierApp[phoneId] != null) {
780 configPackagename = getCarrierPackageForPhoneId(phoneId);
781 configToSend = mConfigFromCarrierApp[phoneId];
782 } else {
783 configPackagename = mPlatformCarrierConfigPackage;
784 configToSend = mConfigFromDefaultApp[phoneId];
785 }
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700786
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800787 if (configToSend == null) {
Rambo Wang42026112021-04-07 20:57:28 +0000788 configToSend = new PersistableBundle();
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800789 }
790
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700791 // mOverrideConfigs is for testing. And it will override current configs.
792 PersistableBundle config = mOverrideConfigs[phoneId];
793 if (config != null) {
Torbjorn Eklund1050cb02018-11-16 14:05:38 +0100794 configToSend = new PersistableBundle(configToSend);
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700795 configToSend.putAll(config);
796 }
797
Nathan Harold48ac0972019-03-13 22:33:01 -0700798 mSubscriptionInfoUpdater.updateSubscriptionByCarrierConfigAndNotifyComplete(
799 phoneId, configPackagename, configToSend,
800 mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1));
801 }
802
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800803 private void broadcastConfigChangedIntent(int phoneId) {
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800804 broadcastConfigChangedIntent(phoneId, true);
805 }
806
807 private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800808 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800809 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
chen xuf9db49f2019-03-31 23:04:42 -0700810 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800811 if (addSubIdExtra) {
812 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
813 int[] subIds = SubscriptionManager.getSubId(phoneId);
814 if (!ArrayUtils.isEmpty(subIds)) {
815 TelephonyManager telMgr = TelephonyManager.from(mContext)
816 .createForSubscriptionId(subIds[0]);
817 simApplicationState = telMgr.getSimApplicationState();
818 }
Malcolm Chenf7635ec2020-04-15 15:45:00 -0700819 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId
820 + " simApplicationState " + simApplicationState);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800821 // Include subId/carrier id extra only if SIM records are loaded
822 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
823 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
824 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
825 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID,
826 getSpecificCarrierIdForPhoneId(phoneId));
827 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, getCarrierIdForPhoneId(phoneId));
828 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800829 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800830 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Sarah Chin807d5c62020-03-30 17:21:09 -0700831 intent.putExtra(CarrierConfigManager.EXTRA_REBROADCAST_ON_UNLOCK,
832 mFromSystemUnlocked[phoneId]);
Meng Wang97a6a462020-01-23 16:22:16 -0800833 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
Jack Yue9e95be2020-03-22 10:56:06 -0700834 int[] subIds = SubscriptionManager.getSubId(phoneId);
835 if (subIds != null && subIds.length > 0) {
836 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId + ", subId=" + subIds[0]);
837 } else {
838 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
839 }
Junda Liu03aad762017-07-21 13:32:17 -0700840 mHasSentConfigChange[phoneId] = true;
Sarah Chin807d5c62020-03-30 17:21:09 -0700841 mFromSystemUnlocked[phoneId] = false;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800842 }
843
844 /** Binds to the default or carrier config app. */
Rambo Wang1b89db32022-04-15 03:00:32 +0000845 private boolean bindToConfigPackage(@NonNull String pkgName, int phoneId, int eventId) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800846 logdWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700847 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700848 carrierService.setPackage(pkgName);
Jayachandran C645ec612020-01-10 10:30:25 -0800849 CarrierServiceConnection serviceConnection = new CarrierServiceConnection(
850 phoneId, pkgName, eventId);
851 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
852 mServiceConnectionForNoSimConfig[phoneId] = serviceConnection;
853 } else {
854 mServiceConnection[phoneId] = serviceConnection;
855 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800856 try {
Jayachandran C645ec612020-01-10 10:30:25 -0800857 if (mContext.bindService(carrierService, serviceConnection,
Jordan Liu178430d2020-02-10 14:32:15 -0800858 Context.BIND_AUTO_CREATE)) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -0700859 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
860 mServiceBoundForNoSimConfig[phoneId] = true;
861 } else {
862 mServiceBound[phoneId] = true;
863 }
Jordan Liu178430d2020-02-10 14:32:15 -0800864 return true;
865 } else {
866 return false;
867 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800868 } catch (SecurityException ex) {
869 return false;
870 }
871 }
872
Rambo Wang7e3bc122021-03-23 09:24:38 -0700873 @VisibleForTesting
Rambo Wang1b89db32022-04-15 03:00:32 +0000874 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -0700875 /* package */ CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800876 String mcc = "";
877 String mnc = "";
878 String imsi = "";
879 String gid1 = "";
880 String gid2 = "";
881 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
882 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800883 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800884 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700885 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
886 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800887 mcc = simOperator.substring(0, 3);
888 mnc = simOperator.substring(3);
889 }
890 Phone phone = PhoneFactory.getPhone(phoneId);
891 if (phone != null) {
892 imsi = phone.getSubscriberId();
893 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700894 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800895 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800896 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800897 }
chen xua31f22b2019-03-06 15:28:50 -0800898 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800899 }
900
Rambo Wang76e83b52022-03-17 12:36:22 -0700901 /** Returns the package name of a privileged carrier app, or null if there is none. */
Nathan Harold1122e3d2021-01-22 15:45:24 -0800902 @Nullable
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700903 private String getCarrierPackageForPhoneId(int phoneId) {
Nazanin1adf4562021-03-29 15:35:30 -0700904 final long token = Binder.clearCallingIdentity();
905 try {
Rambo Wang76e83b52022-03-17 12:36:22 -0700906 return TelephonyManager.from(mContext)
907 .getCarrierServicePackageNameForLogicalSlot(phoneId);
Nazanin1adf4562021-03-29 15:35:30 -0700908 } finally {
909 Binder.restoreCallingIdentity(token);
910 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700911 }
912
Rambo Wang1b89db32022-04-15 03:00:32 +0000913 @Nullable
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700914 private String getIccIdForPhoneId(int phoneId) {
915 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
916 return null;
917 }
918 Phone phone = PhoneFactory.getPhone(phoneId);
919 if (phone == null) {
920 return null;
921 }
922 return phone.getIccSerialNumber();
923 }
924
chen xu02581692018-11-11 19:03:44 -0800925 /**
chen xua31f22b2019-03-06 15:28:50 -0800926 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800927 */
chen xua31f22b2019-03-06 15:28:50 -0800928 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700929 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800930 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700931 }
932 Phone phone = PhoneFactory.getPhone(phoneId);
933 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800934 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700935 }
chen xua31f22b2019-03-06 15:28:50 -0800936 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800937 }
938
939 /**
940 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
941 */
942 private int getCarrierIdForPhoneId(int phoneId) {
943 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
944 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700945 }
chen xu02581692018-11-11 19:03:44 -0800946 Phone phone = PhoneFactory.getPhone(phoneId);
947 if (phone == null) {
948 return TelephonyManager.UNKNOWN_CARRIER_ID;
949 }
950 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700951 }
952
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700953 /**
954 * Writes a bundle to an XML file.
955 *
chen xu02581692018-11-11 19:03:44 -0800956 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -0800957 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -0800958 * should have a single copy of XML file named after carrier id. However, it's still possible
959 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
960 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
961 * carrier while iccid remains the same.
962 *
963 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
964 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700965 *
966 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700967 *
Jayachandran C645ec612020-01-10 10:30:25 -0800968 * @param packageName the name of the package from which we fetched this bundle.
969 * @param extraString An extra string to be used in the XML file name.
970 * @param phoneId the phone ID.
971 * @param carrierId contains all carrier-identifying information.
972 * @param config the bundle to be written. Null will be treated as an empty bundle.
973 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700974 */
Rambo Wang1b89db32022-04-15 03:00:32 +0000975 private void saveConfigToXml(@Nullable String packageName, @NonNull String extraString,
976 int phoneId, @Nullable CarrierIdentifier carrierId, @NonNull PersistableBundle config,
977 boolean isNoSimConfig) {
Jayachandran C645ec612020-01-10 10:30:25 -0800978 if (packageName == null) {
979 loge("Cannot save config with null packageName");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800980 return;
981 }
982
Jayachandran C645ec612020-01-10 10:30:25 -0800983 String fileName;
984 if (isNoSimConfig) {
985 fileName = getFilenameForNoSimConfig(packageName);
986 } else {
987 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
988 != TelephonyManager.SIM_STATE_LOADED) {
989 loge("Skip save config because SIM records are not loaded.");
990 return;
991 }
992
993 final String iccid = getIccIdForPhoneId(phoneId);
Rambo Wang1b89db32022-04-15 03:00:32 +0000994 final int cid = carrierId != null ? carrierId.getSpecificCarrierId()
995 : TelephonyManager.UNKNOWN_CARRIER_ID;
Jayachandran C645ec612020-01-10 10:30:25 -0800996 if (iccid == null) {
997 loge("Cannot save config with null iccid.");
998 return;
999 }
1000 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001001 }
Jayachandran C645ec612020-01-10 10:30:25 -08001002
Junda Liu02596502016-11-15 13:46:43 -08001003 // b/32668103 Only save to file if config isn't empty.
1004 // In case of failure, not caching an empty bundle will
1005 // try loading config again on next power on or sim loaded.
1006 // Downside is for genuinely empty bundle, will bind and load
1007 // on every power on.
1008 if (config == null || config.isEmpty()) {
1009 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001010 }
1011
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001012 final String version = getPackageVersion(packageName);
1013 if (version == null) {
1014 loge("Failed to get package version for: " + packageName);
1015 return;
1016 }
1017
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001018 logdWithLocalLog(
1019 "Save config to xml, packagename: " + packageName + " phoneId: " + phoneId);
chen xudb04c292019-03-26 17:01:15 -07001020
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001021 FileOutputStream outFile = null;
1022 try {
Jayachandran C645ec612020-01-10 10:30:25 -08001023 outFile = new FileOutputStream(new File(mContext.getFilesDir(), fileName));
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001024 config.putString(KEY_VERSION, version);
1025 config.writeToStream(outFile);
1026 outFile.flush();
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001027 outFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001028 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001029 loge(e.toString());
1030 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001031 }
1032
Rambo Wang7e3bc122021-03-23 09:24:38 -07001033 @VisibleForTesting
Rambo Wang1b89db32022-04-15 03:00:32 +00001034 /* package */ void saveConfigToXml(@Nullable String packageName, @NonNull String extraString,
1035 int phoneId, @NonNull CarrierIdentifier carrierId, @NonNull PersistableBundle config) {
Jayachandran C645ec612020-01-10 10:30:25 -08001036 saveConfigToXml(packageName, extraString, phoneId, carrierId, config, false);
1037 }
1038
Rambo Wang7e3bc122021-03-23 09:24:38 -07001039 @VisibleForTesting
Rambo Wang1b89db32022-04-15 03:00:32 +00001040 /* package */ void saveNoSimConfigToXml(@Nullable String packageName,
1041 @NonNull PersistableBundle config) {
Jayachandran C645ec612020-01-10 10:30:25 -08001042 saveConfigToXml(packageName, "", -1, null, config, true);
1043 }
1044
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001045 /**
1046 * Reads a bundle from an XML file.
1047 *
1048 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +08001049 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001050 *
1051 * In case of errors, or if the saved config is from a different package version than the
1052 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001053 *
Jayachandran C645ec612020-01-10 10:30:25 -08001054 * @param packageName the name of the package from which we fetched this bundle.
1055 * @param extraString An extra string to be used in the XML file name.
1056 * @param phoneId the phone ID.
1057 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001058 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001059 * version does not match, or reading config fails.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001060 */
Rambo Wang1b89db32022-04-15 03:00:32 +00001061 @Nullable
1062 private PersistableBundle restoreConfigFromXml(@Nullable String packageName,
1063 @NonNull String extraString, int phoneId, boolean isNoSimConfig) {
Jayachandran C645ec612020-01-10 10:30:25 -08001064 if (packageName == null) {
1065 loge("Cannot restore config with null packageName");
1066 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001067 final String version = getPackageVersion(packageName);
1068 if (version == null) {
1069 loge("Failed to get package version for: " + packageName);
1070 return null;
1071 }
Yuchen Dongc15afed2018-04-26 17:05:22 +08001072
Jayachandran C645ec612020-01-10 10:30:25 -08001073 String fileName;
arunvoddu664c36a2021-10-11 20:09:28 +00001074 String iccid = null;
Jayachandran C645ec612020-01-10 10:30:25 -08001075 if (isNoSimConfig) {
1076 fileName = getFilenameForNoSimConfig(packageName);
1077 } else {
1078 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
1079 != TelephonyManager.SIM_STATE_LOADED) {
1080 loge("Skip restore config because SIM records are not loaded.");
1081 return null;
1082 }
1083
arunvoddu664c36a2021-10-11 20:09:28 +00001084 iccid = getIccIdForPhoneId(phoneId);
Jayachandran C645ec612020-01-10 10:30:25 -08001085 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1086 if (iccid == null) {
1087 loge("Cannot restore config with null iccid.");
1088 return null;
1089 }
1090 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001091 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001092
1093 PersistableBundle restoredBundle = null;
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001094 File file = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001095 FileInputStream inFile = null;
1096 try {
Jayachandran C645ec612020-01-10 10:30:25 -08001097 file = new File(mContext.getFilesDir(),fileName);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001098 inFile = new FileInputStream(file);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001099
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001100 restoredBundle = PersistableBundle.readFromStream(inFile);
1101 String savedVersion = restoredBundle.getString(KEY_VERSION);
1102 restoredBundle.remove(KEY_VERSION);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001103
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001104 if (!version.equals(savedVersion)) {
1105 loge("Saved version mismatch: " + version + " vs " + savedVersion);
1106 restoredBundle = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001107 }
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001108
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001109 inFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001110 } catch (FileNotFoundException e) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001111 // Missing file is normal occurrence that might occur with a new sim or when restoring
1112 // an override file during boot and should not be treated as an error.
arunvoddu664c36a2021-10-11 20:09:28 +00001113 if (file != null) {
1114 if (isNoSimConfig) {
1115 logd("File not found: " + file.getPath());
1116 } else {
1117 String filePath = file.getPath();
1118 filePath = getFilePathForLogging(filePath, iccid);
1119 logd("File not found : " + filePath);
1120 }
1121 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001122 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001123 loge(e.toString());
1124 }
1125
1126 return restoredBundle;
1127 }
1128
arunvoddu664c36a2021-10-11 20:09:28 +00001129 /**
1130 * This method will mask most part of iccid in the filepath for logging on userbuild
1131 */
Rambo Wang1b89db32022-04-15 03:00:32 +00001132 @NonNull
1133 private String getFilePathForLogging(@Nullable String filePath, @Nullable String iccid) {
arunvoddu664c36a2021-10-11 20:09:28 +00001134 // If loggable then return with actual file path
1135 if (Rlog.isLoggable(LOG_TAG, Log.VERBOSE)) {
1136 return filePath;
1137 }
1138 String path = filePath;
1139 int length = (iccid != null) ? iccid.length() : 0;
1140 if (length > 5 && filePath != null) {
1141 path = filePath.replace(iccid.substring(5), "***************");
1142 }
1143 return path;
1144 }
1145
Rambo Wang1b89db32022-04-15 03:00:32 +00001146 @Nullable
1147 private PersistableBundle restoreConfigFromXml(@Nullable String packageName,
1148 @NonNull String extraString, int phoneId) {
Jayachandran C645ec612020-01-10 10:30:25 -08001149 return restoreConfigFromXml(packageName, extraString, phoneId, false);
1150 }
1151
Rambo Wang1b89db32022-04-15 03:00:32 +00001152 @Nullable
1153 private PersistableBundle restoreNoSimConfigFromXml(@Nullable String packageName) {
Jayachandran C645ec612020-01-10 10:30:25 -08001154 return restoreConfigFromXml(packageName, "", -1, true);
1155 }
1156
Junda Liu8a8a53a2015-05-15 16:19:57 -07001157 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001158 * Clears cached carrier config.
1159 * This deletes all saved XML files associated with the given package name. If packageName is
1160 * null, then it deletes all saved XML files.
1161 *
1162 * @param packageName the name of a carrier package, or null if all cached config should be
1163 * cleared.
1164 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -07001165 */
Rambo Wang1b89db32022-04-15 03:00:32 +00001166 private boolean clearCachedConfigForPackage(@Nullable final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001167 File dir = mContext.getFilesDir();
1168 File[] packageFiles = dir.listFiles(new FilenameFilter() {
1169 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001170 if (packageName != null) {
1171 return filename.startsWith("carrierconfig-" + packageName + "-");
1172 } else {
1173 return filename.startsWith("carrierconfig-");
1174 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001175 }
1176 });
Junda Liu8a8a53a2015-05-15 16:19:57 -07001177 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001178 for (File f : packageFiles) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001179 logd("Deleting " + f.getName());
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001180 f.delete();
1181 }
Junda Liu8a8a53a2015-05-15 16:19:57 -07001182 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001183 }
1184
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001185 /** Builds a canonical file name for a config file. */
Rambo Wang1b89db32022-04-15 03:00:32 +00001186 @NonNull
Jayachandran C645ec612020-01-10 10:30:25 -08001187 private static String getFilenameForConfig(
1188 @NonNull String packageName, @NonNull String extraString,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001189 @NonNull String iccid, int cid) {
chen xu02581692018-11-11 19:03:44 -08001190 // the same carrier should have a single copy of XML file named after carrier id.
1191 // However, it's still possible that platform doesn't recognize the current sim carrier,
1192 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
1193 // cases SIM OTA resolves to different carrier while iccid remains the same.
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001194 return "carrierconfig-" + packageName + extraString + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001195 }
1196
Jayachandran C645ec612020-01-10 10:30:25 -08001197 /** Builds a canonical file name for no SIM config file. */
Rambo Wang1b89db32022-04-15 03:00:32 +00001198 @NonNull
Jayachandran C645ec612020-01-10 10:30:25 -08001199 private String getFilenameForNoSimConfig(@NonNull String packageName) {
1200 return "carrierconfig-" + packageName + "-" + "nosim" + ".xml";
1201 }
1202
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001203 /** Return the current version code of a package, or null if the name is not found. */
Rambo Wang1b89db32022-04-15 03:00:32 +00001204 @Nullable
1205 private String getPackageVersion(@NonNull String packageName) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001206 try {
1207 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -08001208 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001209 } catch (PackageManager.NameNotFoundException e) {
1210 return null;
1211 }
1212 }
1213
Jonathan Basseri65273c82017-07-25 15:08:42 -07001214 /**
1215 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001216 *
1217 * This reads config bundles for the given phoneId. That means getting the latest bundle from
1218 * the default app and a privileged carrier app, if present. This will not bind to an app if we
1219 * have a saved config file to use instead.
1220 */
1221 private void updateConfigForPhoneId(int phoneId) {
Jonathan Basseri65273c82017-07-25 15:08:42 -07001222 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001223 }
1224
Malcolm Chen5ea18532019-10-24 19:55:44 -07001225 private void onMultiSimConfigChanged() {
Rambo Wang6f8328d2022-04-15 01:26:23 +00001226 int oldNumPhones = mNumPhones;
1227 mNumPhones = TelephonyManager.from(mContext).getActiveModemCount();
1228 if (mNumPhones == oldNumPhones) {
1229 return;
1230 }
1231 logdWithLocalLog("mNumPhones change from " + oldNumPhones + " to " + mNumPhones);
1232
1233 // If DS -> SS switch, release the resources BEFORE truncating the arrays
1234 for (int phoneId = mNumPhones; phoneId < oldNumPhones; phoneId++) {
1235 if (mServiceConnection[phoneId] != null) {
1236 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
1237 }
1238 if (mServiceConnectionForNoSimConfig[phoneId] != null) {
1239 unbindIfBoundForNoSimConfig(mContext, mServiceConnectionForNoSimConfig[phoneId],
1240 phoneId);
1241 }
1242 }
1243
1244 // Copy the original arrays, truncate or padding with zeros (if necessary) to new length
1245 mConfigFromDefaultApp = Arrays.copyOf(mConfigFromDefaultApp, mNumPhones);
1246 mConfigFromCarrierApp = Arrays.copyOf(mConfigFromCarrierApp, mNumPhones);
1247 mPersistentOverrideConfigs = Arrays.copyOf(mPersistentOverrideConfigs, mNumPhones);
1248 mOverrideConfigs = Arrays.copyOf(mOverrideConfigs, mNumPhones);
1249 mServiceConnection = Arrays.copyOf(mServiceConnection, mNumPhones);
1250 mServiceConnectionForNoSimConfig =
1251 Arrays.copyOf(mServiceConnectionForNoSimConfig, mNumPhones);
1252 mServiceBound = Arrays.copyOf(mServiceBound, mNumPhones);
1253 mServiceBoundForNoSimConfig = Arrays.copyOf(mServiceBoundForNoSimConfig, mNumPhones);
1254 mHasSentConfigChange = Arrays.copyOf(mHasSentConfigChange, mNumPhones);
1255 mFromSystemUnlocked = Arrays.copyOf(mFromSystemUnlocked, mNumPhones);
1256
1257 // If SS -> DS switch, load the config for all the phones AFTER padding the arrays above.
1258 // The logical modems for old phones may not map to the same slot.
1259 for (int phoneId = 0; phoneId < mNumPhones; phoneId++) {
1260 updateConfigForPhoneId(phoneId);
Malcolm Chen5ea18532019-10-24 19:55:44 -07001261 }
1262 }
1263
Hall Liu506724b2018-10-22 18:16:14 -07001264 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001265 @NonNull
Rambo Wang1b89db32022-04-15 03:00:32 +00001266 public PersistableBundle getConfigForSubId(int subscriptionId, @NonNull String callingPackage) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001267 return getConfigForSubIdWithFeature(subscriptionId, callingPackage, null);
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08001268 }
1269
1270 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001271 @NonNull
Rambo Wang1b89db32022-04-15 03:00:32 +00001272 public PersistableBundle getConfigForSubIdWithFeature(int subscriptionId,
1273 @NonNull String callingPackage, @Nullable String callingFeatureId) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001274 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subscriptionId,
1275 callingPackage, callingFeatureId, "getCarrierConfig")) {
Rambo Wang42026112021-04-07 20:57:28 +00001276 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -07001277 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -07001278
Rambo Wangd2b004b2021-03-30 10:10:23 -07001279 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001280 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001281 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001282 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001283 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001284 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001285 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001286 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001287 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001288 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001289 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001290 config = mPersistentOverrideConfigs[phoneId];
1291 if (config != null) {
1292 retConfig.putAll(config);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001293 }
Hall Liu506724b2018-10-22 18:16:14 -07001294 config = mOverrideConfigs[phoneId];
1295 if (config != null) {
1296 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -07001297 }
Nathan Harold1122e3d2021-01-22 15:45:24 -08001298 // Ignore the theoretical case of the default app not being present since that won't
1299 // work in CarrierConfigLoader today.
1300 final boolean allConfigsApplied =
1301 (mConfigFromCarrierApp[phoneId] != null
1302 || getCarrierPackageForPhoneId(phoneId) == null)
1303 && mConfigFromDefaultApp[phoneId] != null;
1304 retConfig.putBoolean(
1305 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, allConfigsApplied);
Jayachandran C645ec612020-01-10 10:30:25 -08001306 } else {
1307 if (mNoSimConfig != null) {
1308 retConfig.putAll(mNoSimConfig);
1309 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001310 }
1311 return retConfig;
1312 }
1313
1314 @Override
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001315 public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides,
1316 boolean persistent) {
Hall Liu506724b2018-10-22 18:16:14 -07001317 mContext.enforceCallingOrSelfPermission(
1318 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Brad Ebingerf6281ad2019-04-25 11:02:04 -07001319 //TODO: Also check for SHELL UID to restrict this method to testing only (b/131326259)
Hall Liu506724b2018-10-22 18:16:14 -07001320 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1321 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001322 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
Rambo Wangd2b004b2021-03-30 10:10:23 -07001323 throw new IllegalArgumentException(
1324 "Invalid phoneId " + phoneId + " for subId " + subscriptionId);
Hall Liu506724b2018-10-22 18:16:14 -07001325 }
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001326 // Post to run on handler thread on which all states should be confined.
1327 mHandler.post(() -> {
1328 overrideConfig(mOverrideConfigs, phoneId, overrides);
Hall Liu506724b2018-10-22 18:16:14 -07001329
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001330 if (persistent) {
1331 overrideConfig(mPersistentOverrideConfigs, phoneId, overrides);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001332
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001333 if (overrides != null) {
1334 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
1335 saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION,
1336 phoneId,
1337 carrierId, mPersistentOverrideConfigs[phoneId]);
1338 } else {
1339 final String iccid = getIccIdForPhoneId(phoneId);
1340 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1341 String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage,
1342 OVERRIDE_PACKAGE_ADDITION, iccid, cid);
1343 File fileToDelete = new File(mContext.getFilesDir(), fileName);
1344 fileToDelete.delete();
1345 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001346 }
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001347 notifySubscriptionInfoUpdater(phoneId);
1348 });
Hall Liu506724b2018-10-22 18:16:14 -07001349 }
1350
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001351 private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId,
1352 @Nullable PersistableBundle overrides) {
1353 if (overrides == null) {
Rambo Wang42026112021-04-07 20:57:28 +00001354 currentOverrides[phoneId] = new PersistableBundle();
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001355 } else if (currentOverrides[phoneId] == null) {
1356 currentOverrides[phoneId] = overrides;
1357 } else {
1358 currentOverrides[phoneId].putAll(overrides);
1359 }
1360 }
1361
Hall Liu506724b2018-10-22 18:16:14 -07001362 @Override
Rambo Wangd2b004b2021-03-30 10:10:23 -07001363 public void notifyConfigChangedForSubId(int subscriptionId) {
Jordan Liud7d57fe2019-04-16 12:29:43 -07001364 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001365 // system privileged app with MODIFY_PHONE_STATE permission.
Rambo Wangd2b004b2021-03-30 10:10:23 -07001366 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext,
1367 subscriptionId, "Require carrier privileges or MODIFY_PHONE_STATE permission.");
1368
1369 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1370 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1371 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
1372 throw new IllegalArgumentException(
1373 "Invalid phoneId " + phoneId + " for subId " + subscriptionId);
1374 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001375
1376 // This method should block until deleting has completed, so that an error which prevents us
1377 // from clearing the cache is passed back to the carrier app. With the files successfully
1378 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001379 String callingPackageName = mContext.getPackageManager().getNameForUid(
1380 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001381 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001382 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001383 }
1384
1385 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +00001386 public void updateConfigForPhoneId(int phoneId, @NonNull String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -07001387 mContext.enforceCallingOrSelfPermission(
1388 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001389 logdWithLocalLog("Update config for phoneId: " + phoneId + " simState: " + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001390 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001391 throw new IllegalArgumentException("Invalid phoneId: " + phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001392 }
1393 // requires Java 7 for switch on string.
1394 switch (simState) {
1395 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1396 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001397 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001398 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Malcolm Chendb66de12019-12-09 18:57:07 -08001399 case IccCardConstants.INTENT_VALUE_ICC_NOT_READY:
Junda Liu9f2d2712015-05-15 14:22:45 -07001400 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001401 break;
1402 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1403 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001404 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001405 break;
1406 }
1407 }
1408
Junda Liu43d723a2015-05-12 17:23:45 -07001409 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +00001410 @NonNull
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001411 public String getDefaultCarrierServicePackageName() {
Shuo Qianefdb7962019-12-19 15:54:27 -08001412 mContext.enforceCallingOrSelfPermission(
1413 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1414 "getDefaultCarrierServicePackageName");
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001415 return mPlatformCarrierConfigPackage;
1416 }
1417
Rambo Wang7e3bc122021-03-23 09:24:38 -07001418 @VisibleForTesting
Rambo Wang1b89db32022-04-15 03:00:32 +00001419 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -07001420 /* package */ Handler getHandler() {
1421 return mHandler;
1422 }
1423
1424 @VisibleForTesting
Rambo Wang1b89db32022-04-15 03:00:32 +00001425 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001426 /* package */ PersistableBundle getConfigFromDefaultApp(int phoneId) {
1427 return mConfigFromDefaultApp[phoneId];
1428 }
1429
1430 @VisibleForTesting
Rambo Wang1b89db32022-04-15 03:00:32 +00001431 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001432 /* package */ PersistableBundle getConfigFromCarrierApp(int phoneId) {
1433 return mConfigFromCarrierApp[phoneId];
1434 }
1435
1436 @VisibleForTesting
Rambo Wang1b89db32022-04-15 03:00:32 +00001437 @NonNull
Rambo Wang7e3bc122021-03-23 09:24:38 -07001438 /* package */ PersistableBundle getNoSimConfig() {
1439 return mNoSimConfig;
1440 }
1441
1442 @VisibleForTesting
Rambo Wang1b89db32022-04-15 03:00:32 +00001443 @Nullable
Rambo Wang7e3bc122021-03-23 09:24:38 -07001444 /* package */ PersistableBundle getOverrideConfig(int phoneId) {
1445 return mOverrideConfigs[phoneId];
1446 }
1447
Rambo Wang6f8328d2022-04-15 01:26:23 +00001448 // TODO(b/185129900): always call unbindService after bind, no matter if it succeeded
Rambo Wang1b89db32022-04-15 03:00:32 +00001449 private void unbindIfBound(@NonNull Context context, @NonNull CarrierServiceConnection conn,
Rambo Wangb0f3e2f2021-10-28 12:40:12 -07001450 int phoneId) {
1451 if (mServiceBound[phoneId]) {
1452 mServiceBound[phoneId] = false;
1453 context.unbindService(conn);
1454 }
1455 }
1456
Rambo Wang1b89db32022-04-15 03:00:32 +00001457 private void unbindIfBoundForNoSimConfig(@NonNull Context context,
1458 @NonNull CarrierServiceConnection conn, int phoneId) {
Rambo Wangb0f3e2f2021-10-28 12:40:12 -07001459 if (mServiceBoundForNoSimConfig[phoneId]) {
1460 mServiceBoundForNoSimConfig[phoneId] = false;
Jayachandran C645ec612020-01-10 10:30:25 -08001461 context.unbindService(conn);
1462 }
1463 }
1464
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001465 /**
Rambo Wang610fdf62021-03-08 21:37:11 -08001466 * Returns a boxed Integer object for phoneId, services as message token to distinguish messages
1467 * with same code when calling {@link Handler#removeMessages(int, Object)}.
1468 */
Rambo Wang1b89db32022-04-15 03:00:32 +00001469 @NonNull
Rambo Wang610fdf62021-03-08 21:37:11 -08001470 private Integer getMessageToken(int phoneId) {
1471 if (phoneId < -128 || phoneId > 127) {
1472 throw new IllegalArgumentException("phoneId should be in range [-128, 127], inclusive");
1473 }
1474 // Integer#valueOf guarantees the integers within [-128, 127] are cached and thus memory
1475 // comparison (==) returns true for the same integer.
1476 return Integer.valueOf(phoneId);
1477 }
1478
1479 /**
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001480 * If {@code args} contains {@link #DUMP_ARG_REQUESTING_PACKAGE} and a following package name,
1481 * we'll also call {@link IBinder#dump} on the default carrier service (if bound) and the
1482 * specified carrier service (if bound). Typically, this is done for connectivity bug reports
1483 * where we don't call {@code dumpsys activity service all-non-platform} because that contains
1484 * too much info, but we still want to let carrier apps include their diagnostics.
1485 */
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001486 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +00001487 public void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter pw, @NonNull String[] args) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001488 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
Junda Liu43d723a2015-05-12 17:23:45 -07001489 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1490 != PackageManager.PERMISSION_GRANTED) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001491 indentPW.println("Permission Denial: can't dump carrierconfig from from pid="
Junda Liu43d723a2015-05-12 17:23:45 -07001492 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1493 return;
1494 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001495 String requestingPackage = null;
1496 int requestingPackageIndex = ArrayUtils.indexOf(args, DUMP_ARG_REQUESTING_PACKAGE);
1497 if (requestingPackageIndex >= 0 && requestingPackageIndex < args.length - 1
1498 && !TextUtils.isEmpty(args[requestingPackageIndex + 1])) {
1499 requestingPackage = args[requestingPackageIndex + 1];
1500 // Throws a SecurityException if the caller is impersonating another app in an effort to
1501 // dump extra info (which may contain PII the caller doesn't have a right to).
1502 enforceCallerIsSystemOrRequestingPackage(requestingPackage);
1503 }
1504
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001505 indentPW.println("CarrierConfigLoader: " + this);
Rambo Wang6f8328d2022-04-15 01:26:23 +00001506 for (int i = 0; i < mNumPhones; i++) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001507 indentPW.println("Phone Id = " + i);
shuoq26a3a4c2016-12-16 11:06:48 -08001508 // display default values in CarrierConfigManager
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001509 printConfig(CarrierConfigManager.getDefaultConfig(), indentPW,
shuoq26a3a4c2016-12-16 11:06:48 -08001510 "Default Values from CarrierConfigManager");
shuoq26a3a4c2016-12-16 11:06:48 -08001511 // display ConfigFromDefaultApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001512 printConfig(mConfigFromDefaultApp[i], indentPW, "mConfigFromDefaultApp");
shuoq26a3a4c2016-12-16 11:06:48 -08001513 // display ConfigFromCarrierApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001514 printConfig(mConfigFromCarrierApp[i], indentPW, "mConfigFromCarrierApp");
1515 printConfig(mPersistentOverrideConfigs[i], indentPW, "mPersistentOverrideConfigs");
1516 printConfig(mOverrideConfigs[i], indentPW, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001517 }
chen xudb04c292019-03-26 17:01:15 -07001518
Jayachandran C645ec612020-01-10 10:30:25 -08001519 printConfig(mNoSimConfig, indentPW, "mNoSimConfig");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001520 indentPW.println("CarrierConfigLoadingLog=");
1521 mCarrierConfigLoadingLog.dump(fd, indentPW, args);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001522
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001523 if (requestingPackage != null) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001524 logd("Including default and requesting package " + requestingPackage
1525 + " carrier services in dump");
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001526 indentPW.println("");
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001527 indentPW.println("Connected services");
1528 dumpCarrierServiceIfBound(fd, indentPW, "Default config package",
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001529 mPlatformCarrierConfigPackage, false /* considerCarrierPrivileges */);
1530 dumpCarrierServiceIfBound(fd, indentPW, "Requesting package", requestingPackage,
1531 true /* considerCarrierPrivileges */);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001532 }
shuoq26a3a4c2016-12-16 11:06:48 -08001533 }
1534
Rambo Wang1b89db32022-04-15 03:00:32 +00001535 private void printConfig(@NonNull PersistableBundle configApp,
1536 @NonNull IndentingPrintWriter indentPW, @NonNull String name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001537 indentPW.increaseIndent();
shuoq26a3a4c2016-12-16 11:06:48 -08001538 if (configApp == null) {
shuoq26a3a4c2016-12-16 11:06:48 -08001539 indentPW.println(name + " : null ");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001540 indentPW.decreaseIndent();
1541 indentPW.println("");
shuoq26a3a4c2016-12-16 11:06:48 -08001542 return;
1543 }
shuoq26a3a4c2016-12-16 11:06:48 -08001544 indentPW.println(name + " : ");
1545 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1546 Collections.sort(sortedKeys);
1547 indentPW.increaseIndent();
1548 indentPW.increaseIndent();
1549 for (String key : sortedKeys) {
1550 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1551 indentPW.println(key + " = " +
1552 Arrays.toString((Object[]) configApp.get(key)));
1553 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1554 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1555 } else {
1556 indentPW.println(key + " = " + configApp.get(key));
1557 }
Junda Liu43d723a2015-05-12 17:23:45 -07001558 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001559 indentPW.decreaseIndent();
1560 indentPW.decreaseIndent();
1561 indentPW.decreaseIndent();
1562 indentPW.println("");
Junda Liu43d723a2015-05-12 17:23:45 -07001563 }
1564
Hunter Knepshielde601f432020-02-19 10:16:04 -08001565 /**
1566 * Passes without problem when one of these conditions is true:
1567 * - The caller is a privileged UID (e.g. for dumpstate.cpp generating a bug report, where the
1568 * system knows the true caller plumbed in through the {@link android.os.BugreportManager} API).
1569 * - The caller's UID matches the supplied package.
1570 *
1571 * @throws SecurityException if none of the above conditions are met.
1572 */
Rambo Wang1b89db32022-04-15 03:00:32 +00001573 private void enforceCallerIsSystemOrRequestingPackage(@NonNull String requestingPackage)
Hunter Knepshielde601f432020-02-19 10:16:04 -08001574 throws SecurityException {
1575 final int callingUid = Binder.getCallingUid();
1576 if (callingUid == Process.ROOT_UID || callingUid == Process.SYSTEM_UID
1577 || callingUid == Process.SHELL_UID || callingUid == Process.PHONE_UID) {
1578 // Bug reports (dumpstate.cpp) run as SHELL, and let some other privileged UIDs through
1579 // as well.
1580 return;
1581 }
1582 // An app is trying to dump extra detail, block it if they aren't who they claim to be.
1583 AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class);
1584 if (appOps == null) {
1585 throw new SecurityException("No AppOps");
1586 }
1587 // Will throw a SecurityException if the UID and package don't match.
1588 appOps.checkPackage(callingUid, requestingPackage);
1589 }
1590
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001591 /**
1592 * Searches for one or more appropriate {@link CarrierService} instances to dump based on the
1593 * current connections.
1594 *
1595 * @param targetPkgName the target package name to dump carrier services for
1596 * @param considerCarrierPrivileges if true, allow a carrier service to be dumped if it shares
1597 * carrier privileges with {@code targetPkgName};
1598 * otherwise, only dump a carrier service if it is {@code
1599 * targetPkgName}
1600 */
Rambo Wang1b89db32022-04-15 03:00:32 +00001601 private void dumpCarrierServiceIfBound(@NonNull FileDescriptor fd,
1602 @NonNull IndentingPrintWriter indentPW, @NonNull String prefix,
1603 @NonNull String targetPkgName, boolean considerCarrierPrivileges) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001604 // Null package is possible if it's early in the boot process, there was a recent crash, we
1605 // loaded the config from XML most recently, or a SIM slot is empty. Carrier apps with
1606 // long-lived bindings should typically get dumped here regardless. Even if an app is being
1607 // used for multiple phoneIds, we assume that it's smart enough to handle that on its own,
1608 // and that in most cases we'd just be dumping duplicate information and bloating a report.
1609 indentPW.increaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001610 indentPW.println(prefix + " : " + targetPkgName);
1611 Set<String> dumpedPkgNames = new ArraySet<>(mServiceConnection.length);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001612 for (CarrierServiceConnection connection : mServiceConnection) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001613 if (connection == null || !SubscriptionManager.isValidPhoneId(connection.phoneId)
1614 || TextUtils.isEmpty(connection.pkgName)) {
1615 continue;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001616 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001617 final String servicePkgName = connection.pkgName;
1618 // Note: we intentionally ignore system components here because we should NOT match the
1619 // shell caller that's typically used for bug reports via non-BugreportManager triggers.
1620 final boolean exactPackageMatch = TextUtils.equals(targetPkgName, servicePkgName);
1621 final boolean carrierPrivilegesMatch =
1622 considerCarrierPrivileges && hasCarrierPrivileges(targetPkgName,
1623 connection.phoneId);
1624 if (!exactPackageMatch && !carrierPrivilegesMatch) continue;
1625 // Make sure this service is actually alive before trying to dump it. We don't pay
1626 // attention to mServiceBound[connection.phoneId] because typically carrier apps will
1627 // request long-lived bindings, and even if we unbind the app, it may still be alive due
1628 // to CarrierServiceBindHelper. Pull it out as a reference so even if it gets set to
1629 // null within the ServiceConnection during unbinding we can avoid an NPE.
1630 final IBinder service = connection.service;
1631 if (service == null || !service.isBinderAlive() || !service.pingBinder()) continue;
1632 // We've got a live service. Last check is just to make sure we don't dump a package
1633 // multiple times.
1634 if (!dumpedPkgNames.add(servicePkgName)) continue;
1635 if (!exactPackageMatch) {
1636 logd(targetPkgName + " has carrier privileges on phoneId " + connection.phoneId
1637 + ", service provided by " + servicePkgName);
1638 indentPW.increaseIndent();
1639 indentPW.println("Proxy : " + servicePkgName);
1640 indentPW.decreaseIndent();
1641 }
1642 // Flush before we let the app output anything to ensure correct ordering of output.
1643 // Internally, Binder#dump calls flush on its printer after finishing so we don't
1644 // need to do anything after.
1645 indentPW.flush();
1646 try {
1647 logd("Dumping " + servicePkgName);
1648 // We don't need to give the carrier service any args.
1649 connection.service.dump(fd, null /* args */);
1650 logd("Done with " + servicePkgName);
1651 } catch (RemoteException e) {
1652 logd("RemoteException from " + servicePkgName, e);
1653 indentPW.increaseIndent();
1654 indentPW.println("RemoteException");
1655 indentPW.increaseIndent();
1656 e.printStackTrace(indentPW);
1657 indentPW.decreaseIndent();
1658 indentPW.decreaseIndent();
1659 // We won't retry this package again because now it's in dumpedPkgNames.
1660 }
1661 indentPW.println("");
Jordan Liu46ed5db2020-01-28 08:55:20 -08001662 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001663 if (dumpedPkgNames.isEmpty()) {
1664 indentPW.increaseIndent();
1665 indentPW.println("Not bound");
1666 indentPW.decreaseIndent();
1667 indentPW.println("");
1668 }
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001669 indentPW.decreaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001670 }
1671
Rambo Wang1b89db32022-04-15 03:00:32 +00001672 private boolean hasCarrierPrivileges(@NonNull String pkgName, int phoneId) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001673 int[] subIds = SubscriptionManager.getSubId(phoneId);
1674 if (ArrayUtils.isEmpty(subIds)) {
1675 return false;
1676 }
1677 return TelephonyManager.from(mContext).createForSubscriptionId(
1678 subIds[0]).checkCarrierPrivilegesForPackage(pkgName)
1679 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001680 }
1681
Zach Johnson36d7aab2015-05-22 15:43:00 -07001682 private class CarrierServiceConnection implements ServiceConnection {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001683 final int phoneId;
Rambo Wang1b89db32022-04-15 03:00:32 +00001684 @NonNull final String pkgName;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001685 final int eventId;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001686 IBinder service;
1687
Rambo Wang1b89db32022-04-15 03:00:32 +00001688 CarrierServiceConnection(int phoneId, @NonNull String pkgName, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001689 this.phoneId = phoneId;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001690 this.pkgName = pkgName;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001691 this.eventId = eventId;
1692 }
1693
1694 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +00001695 public void onServiceConnected(@NonNull ComponentName name, @NonNull IBinder service) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001696 logd("Connected to config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001697 this.service = service;
1698 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1699 }
1700
1701 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +00001702 public void onServiceDisconnected(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001703 logd("Disconnected from config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001704 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001705 }
1706
1707 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +00001708 public void onBindingDied(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001709 logd("Binding died from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001710 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001711 }
1712
1713 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +00001714 public void onNullBinding(@NonNull ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001715 logd("Null binding from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001716 this.service = null;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001717 }
1718 }
1719
1720 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1721 @Override
Rambo Wang1b89db32022-04-15 03:00:32 +00001722 public void onReceive(@NonNull Context context, @NonNull Intent intent) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001723 String action = intent.getAction();
Junda Liu8a8a53a2015-05-15 16:19:57 -07001724 boolean replace = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1725 // If replace is true, only care ACTION_PACKAGE_REPLACED.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001726 if (replace && !Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
Junda Liu8a8a53a2015-05-15 16:19:57 -07001727 return;
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001728 }
Junda Liu8a8a53a2015-05-15 16:19:57 -07001729
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001730 switch (action) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001731 case Intent.ACTION_BOOT_COMPLETED:
1732 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1733 break;
1734
Rambo Wang6f8328d2022-04-15 01:26:23 +00001735 case TelephonyManager.ACTION_MULTI_SIM_CONFIG_CHANGED:
1736 mHandler.sendEmptyMessage(EVENT_MULTI_SIM_CONFIG_CHANGED);
1737 break;
1738
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001739 case Intent.ACTION_PACKAGE_ADDED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001740 case Intent.ACTION_PACKAGE_REMOVED:
Junda Liu8a8a53a2015-05-15 16:19:57 -07001741 case Intent.ACTION_PACKAGE_REPLACED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001742 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1743 String packageName = mContext.getPackageManager().getNameForUid(uid);
Junda Liu8a8a53a2015-05-15 16:19:57 -07001744 if (packageName != null) {
1745 // We don't have a phoneId for arg1.
1746 mHandler.sendMessage(
1747 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, packageName));
1748 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001749 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001750 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001751 }
1752 }
1753
Rambo Wangab13e3e2021-04-08 15:01:06 -07001754 // Get readable string for the message code supported in this class.
Rambo Wang1b89db32022-04-15 03:00:32 +00001755 @NonNull
Rambo Wangab13e3e2021-04-08 15:01:06 -07001756 private static String eventToString(int code) {
1757 switch (code) {
1758 case EVENT_CLEAR_CONFIG:
1759 return "EVENT_CLEAR_CONFIG";
1760 case EVENT_CONNECTED_TO_DEFAULT:
1761 return "EVENT_CONNECTED_TO_DEFAULT";
1762 case EVENT_CONNECTED_TO_CARRIER:
1763 return "EVENT_CONNECTED_TO_CARRIER";
1764 case EVENT_FETCH_DEFAULT_DONE:
1765 return "EVENT_FETCH_DEFAULT_DONE";
1766 case EVENT_FETCH_CARRIER_DONE:
1767 return "EVENT_FETCH_CARRIER_DONE";
1768 case EVENT_DO_FETCH_DEFAULT:
1769 return "EVENT_DO_FETCH_DEFAULT";
1770 case EVENT_DO_FETCH_CARRIER:
1771 return "EVENT_DO_FETCH_CARRIER";
1772 case EVENT_PACKAGE_CHANGED:
1773 return "EVENT_PACKAGE_CHANGED";
1774 case EVENT_BIND_DEFAULT_TIMEOUT:
1775 return "EVENT_BIND_DEFAULT_TIMEOUT";
1776 case EVENT_BIND_CARRIER_TIMEOUT:
1777 return "EVENT_BIND_CARRIER_TIMEOUT";
1778 case EVENT_CHECK_SYSTEM_UPDATE:
1779 return "EVENT_CHECK_SYSTEM_UPDATE";
1780 case EVENT_SYSTEM_UNLOCKED:
1781 return "EVENT_SYSTEM_UNLOCKED";
1782 case EVENT_FETCH_DEFAULT_TIMEOUT:
1783 return "EVENT_FETCH_DEFAULT_TIMEOUT";
1784 case EVENT_FETCH_CARRIER_TIMEOUT:
1785 return "EVENT_FETCH_CARRIER_TIMEOUT";
1786 case EVENT_SUBSCRIPTION_INFO_UPDATED:
1787 return "EVENT_SUBSCRIPTION_INFO_UPDATED";
1788 case EVENT_MULTI_SIM_CONFIG_CHANGED:
1789 return "EVENT_MULTI_SIM_CONFIG_CHANGED";
1790 case EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG:
1791 return "EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG";
1792 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE:
1793 return "EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE";
1794 case EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG:
1795 return "EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG";
1796 case EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
1797 return "EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT";
1798 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
1799 return "EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT";
1800 default:
1801 return "UNKNOWN(" + code + ")";
1802 }
1803 }
1804
Rambo Wang1b89db32022-04-15 03:00:32 +00001805 private void logd(@NonNull String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001806 Log.d(LOG_TAG, msg);
1807 }
1808
Rambo Wang1b89db32022-04-15 03:00:32 +00001809 private void logd(@NonNull String msg, Throwable tr) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001810 Log.d(LOG_TAG, msg, tr);
1811 }
1812
Rambo Wang1b89db32022-04-15 03:00:32 +00001813 private void logdWithLocalLog(@NonNull String msg) {
chen xudb04c292019-03-26 17:01:15 -07001814 Log.d(LOG_TAG, msg);
1815 mCarrierConfigLoadingLog.log(msg);
1816 }
1817
Rambo Wang1b89db32022-04-15 03:00:32 +00001818 private void loge(@NonNull String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001819 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07001820 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001821 }
1822}