blob: 4ff731632aea77bde8212c94f6ed14886ea1bcbc [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;
39import android.os.Message;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070040import android.os.PersistableBundle;
Hunter Knepshielde601f432020-02-19 10:16:04 -080041import android.os.Process;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080042import android.os.RemoteException;
Jonathan Basseri65273c82017-07-25 15:08:42 -070043import android.os.ResultReceiver;
Meng Wang97a6a462020-01-23 16:22:16 -080044import android.os.UserHandle;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070045import android.preference.PreferenceManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080046import android.service.carrier.CarrierIdentifier;
Zach Johnson36d7aab2015-05-22 15:43:00 -070047import android.service.carrier.CarrierService;
48import android.service.carrier.ICarrierService;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080049import android.telephony.CarrierConfigManager;
50import android.telephony.SubscriptionManager;
Peter Wangc035ce42020-01-08 21:00:22 -080051import android.telephony.TelephonyFrameworkInitializer;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080052import android.telephony.TelephonyManager;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -080053import android.text.TextUtils;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080054import android.util.ArraySet;
chen xudb04c292019-03-26 17:01:15 -070055import android.util.LocalLog;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080056import android.util.Log;
57
58import com.android.internal.telephony.ICarrierConfigLoader;
59import com.android.internal.telephony.IccCardConstants;
60import com.android.internal.telephony.Phone;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080061import com.android.internal.telephony.PhoneFactory;
Nathan Harold48ac0972019-03-13 22:33:01 -070062import com.android.internal.telephony.SubscriptionInfoUpdater;
Jeff Davidsona8e4e242018-03-15 17:16:18 -070063import com.android.internal.telephony.TelephonyPermissions;
Meng Wanga320b942019-11-25 11:21:26 -080064import com.android.internal.telephony.util.ArrayUtils;
shuoq26a3a4c2016-12-16 11:06:48 -080065import com.android.internal.util.IndentingPrintWriter;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080066
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070067import java.io.File;
Junda Liu43d723a2015-05-12 17:23:45 -070068import java.io.FileDescriptor;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070069import java.io.FileInputStream;
70import java.io.FileNotFoundException;
71import java.io.FileOutputStream;
Jonathan Basseri1f743c92015-05-15 00:19:46 -070072import java.io.FilenameFilter;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070073import java.io.IOException;
Junda Liu43d723a2015-05-12 17:23:45 -070074import java.io.PrintWriter;
shuoq26a3a4c2016-12-16 11:06:48 -080075import java.util.ArrayList;
76import java.util.Arrays;
77import java.util.Collections;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080078import java.util.List;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080079import java.util.Set;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080080
81/**
82 * CarrierConfigLoader binds to privileged carrier apps to fetch carrier config overlays.
Jonathan Basseri6465afd2015-02-25 13:05:57 -080083 */
Jonathan Basseri6465afd2015-02-25 13:05:57 -080084public class CarrierConfigLoader extends ICarrierConfigLoader.Stub {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070085 private static final String LOG_TAG = "CarrierConfigLoader";
Meng Wang33ad2bc2017-03-16 20:21:20 -070086
87 // Package name for platform carrier config app, bundled with system image.
88 private final String mPlatformCarrierConfigPackage;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080089
90 /** The singleton instance. */
91 private static CarrierConfigLoader sInstance;
92 // The context for phone app, passed from PhoneGlobals.
93 private Context mContext;
94 // Carrier configs from default app, indexed by phoneID.
Jonathan Basseric31f1f32015-05-12 10:13:03 -070095 private PersistableBundle[] mConfigFromDefaultApp;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080096 // Carrier configs from privileged carrier config app, indexed by phoneID.
Jonathan Basseric31f1f32015-05-12 10:13:03 -070097 private PersistableBundle[] mConfigFromCarrierApp;
Torbjorn Eklund723527a2019-02-13 11:16:25 +010098 // Persistent Carrier configs that are provided via the override test API, indexed by phone ID.
99 private PersistableBundle[] mPersistentOverrideConfigs;
Hall Liu506724b2018-10-22 18:16:14 -0700100 // Carrier configs that are provided via the override test API, indexed by phone ID.
101 private PersistableBundle[] mOverrideConfigs;
Jayachandran C645ec612020-01-10 10:30:25 -0800102 // Carrier configs to override code default when there is no SIM inserted
103 private PersistableBundle mNoSimConfig;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800104 // Service connection for binding to config app.
Zach Johnson36d7aab2015-05-22 15:43:00 -0700105 private CarrierServiceConnection[] mServiceConnection;
Jayachandran C645ec612020-01-10 10:30:25 -0800106 // Service connection for binding to carrier config app for no SIM config.
107 private CarrierServiceConnection[] mServiceConnectionForNoSimConfig;
Jordan Liu178430d2020-02-10 14:32:15 -0800108 // Whether we are bound to a service for each phone
109 private boolean[] mServiceBound;
Jayachandran C645ec612020-01-10 10:30:25 -0800110 // Whether we are bound to a service for no SIM config
111 private boolean[] mServiceBoundForNoSimConfig;
Sarah Chin807d5c62020-03-30 17:21:09 -0700112 // Whether we have sent config change broadcast for each phone id.
Junda Liu03aad762017-07-21 13:32:17 -0700113 private boolean[] mHasSentConfigChange;
Sarah Chin807d5c62020-03-30 17:21:09 -0700114 // Whether the broadcast was sent from EVENT_SYSTEM_UNLOCKED, to track rebroadcasts
115 private boolean[] mFromSystemUnlocked;
Nathan Harold48ac0972019-03-13 22:33:01 -0700116 // SubscriptionInfoUpdater
117 private final SubscriptionInfoUpdater mSubscriptionInfoUpdater;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800118
Nanxi Chen3d670502016-03-17 16:32:09 -0700119 // Broadcast receiver for Boot intents, register intent filter in construtor.
120 private final BroadcastReceiver mBootReceiver = new ConfigLoaderBroadcastReceiver();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800121 // Broadcast receiver for SIM and pkg intents, register intent filter in constructor.
Nanxi Chen3d670502016-03-17 16:32:09 -0700122 private final BroadcastReceiver mPackageReceiver = new ConfigLoaderBroadcastReceiver();
Jack Yu67663de2019-10-17 15:19:48 -0700123 private final LocalLog mCarrierConfigLoadingLog = new LocalLog(100);
chen xudb04c292019-03-26 17:01:15 -0700124
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800125
126 // Message codes; see mHandler below.
127 // Request from SubscriptionInfoUpdater when SIM becomes absent or error.
128 private static final int EVENT_CLEAR_CONFIG = 0;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800129 // Has connected to default app.
130 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
131 // Has connected to carrier app.
132 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700133 // Config has been loaded from default app (or cache).
134 private static final int EVENT_FETCH_DEFAULT_DONE = 5;
135 // Config has been loaded from carrier app (or cache).
136 private static final int EVENT_FETCH_CARRIER_DONE = 6;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700137 // Attempt to fetch from default app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700138 private static final int EVENT_DO_FETCH_DEFAULT = 7;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700139 // Attempt to fetch from carrier app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700140 private static final int EVENT_DO_FETCH_CARRIER = 8;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700141 // A package has been installed, uninstalled, or updated.
142 private static final int EVENT_PACKAGE_CHANGED = 9;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700143 // Bind timed out for the default app.
144 private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
145 // Bind timed out for a carrier app.
146 private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700147 // Check if the system fingerprint has changed.
148 private static final int EVENT_CHECK_SYSTEM_UPDATE = 12;
Nanxi Chen3d670502016-03-17 16:32:09 -0700149 // Rerun carrier config binding after system is unlocked.
150 private static final int EVENT_SYSTEM_UNLOCKED = 13;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700151 // Fetching config timed out from the default app.
152 private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14;
153 // Fetching config timed out from a carrier app.
154 private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15;
Nathan Harold48ac0972019-03-13 22:33:01 -0700155 // SubscriptionInfoUpdater has finished updating the sub for the carrier config.
156 private static final int EVENT_SUBSCRIPTION_INFO_UPDATED = 16;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700157 // Multi-SIM config changed.
158 private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 17;
Jayachandran C645ec612020-01-10 10:30:25 -0800159 // Attempt to fetch from default app or read from XML for no SIM case.
160 private static final int EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG = 18;
161 // No SIM config has been loaded from default app (or cache).
162 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE = 19;
163 // Has connected to default app for no SIM config.
164 private static final int EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG = 20;
165 // Bind timed out for the default app when trying to fetch no SIM config.
166 private static final int EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 21;
167 // Fetching config timed out from the default app for no SIM config.
168 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 22;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700169
Junda Liu6cb45002016-03-22 17:18:20 -0700170 private static final int BIND_TIMEOUT_MILLIS = 30000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800171
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800172 // Keys used for saving and restoring config bundle from file.
173 private static final String KEY_VERSION = "__carrier_config_package_version__";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800174
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100175 private static final String OVERRIDE_PACKAGE_ADDITION = "-override";
176
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700177 // SharedPreferences key for last known build fingerprint.
178 private static final String KEY_FINGERPRINT = "build_fingerprint";
179
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800180 // Argument for #dump that indicates we should also call the default and specified carrier
181 // service's #dump method. In multi-SIM devices, it's possible that carrier A is on SIM 1 and
182 // carrier B is on SIM 2, in which case we should not dump carrier B's service when carrier A
183 // requested the dump.
184 private static final String DUMP_ARG_REQUESTING_PACKAGE = "--requesting-package";
185
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800186 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700187 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700188 // For each phoneId, the event sequence should be:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700189 // fetch default, connected to default, fetch default (async), fetch default done,
190 // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700191 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700192 // If there is a saved config file for either the default app or the carrier app, we skip
193 // binding to the app and go straight from fetch to loaded.
194 //
195 // At any time, at most one connection is active. If events are not in this order, previous
196 // connection will be unbound, so only latest event takes effect.
197 //
198 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
199 // 1. loading from carrier app (even if read from a file)
200 // 2. loading from default app if there is no carrier app (even if read from a file)
201 // 3. clearing config (e.g. due to sim removal)
202 // 4. encountering bind or IPC error
Jonathan Basseri65273c82017-07-25 15:08:42 -0700203 private class ConfigHandler extends Handler {
204 @Override
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800205 public void handleMessage(Message msg) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700206 final int phoneId = msg.arg1;
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800207 logdWithLocalLog("mHandler: " + msg.what + " phoneId: " + phoneId);
Malcolm Chen5ea18532019-10-24 19:55:44 -0700208 if (!SubscriptionManager.isValidPhoneId(phoneId)
209 && msg.what != EVENT_MULTI_SIM_CONFIG_CHANGED) {
210 return;
211 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800212 switch (msg.what) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800213 case EVENT_CLEAR_CONFIG: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700214 clearConfigForPhone(phoneId, true);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800215 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700216 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700217
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800218 case EVENT_SYSTEM_UNLOCKED: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700219 for (int i = 0; i < TelephonyManager.from(mContext).getActiveModemCount();
220 ++i) {
Sarah Chin807d5c62020-03-30 17:21:09 -0700221 // When the user unlocks the device, send the broadcast again (with a
222 // rebroadcast extra) if we have sent it before unlock. This will avoid
223 // trying to load the carrier config when the SIM is still loading when the
224 // unlock happens.
Junda Liu03aad762017-07-21 13:32:17 -0700225 if (mHasSentConfigChange[i]) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800226 logdWithLocalLog("System unlocked");
Sarah Chin807d5c62020-03-30 17:21:09 -0700227 mFromSystemUnlocked[i] = true;
Junda Liu03aad762017-07-21 13:32:17 -0700228 updateConfigForPhoneId(i);
229 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700230 }
231 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700232 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700233
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800234 case EVENT_PACKAGE_CHANGED: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700235 final String carrierPackageName = (String) msg.obj;
236 // Only update if there are cached config removed to avoid updating config for
237 // unrelated packages.
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700238 if (clearCachedConfigForPackage(carrierPackageName)) {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700239 int numPhones = TelephonyManager.from(mContext).getActiveModemCount();
Junda Liu8a8a53a2015-05-15 16:19:57 -0700240 for (int i = 0; i < numPhones; ++i) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800241 logdWithLocalLog("Package changed: " + carrierPackageName
Jack Yu67663de2019-10-17 15:19:48 -0700242 + ", phone=" + i);
Junda Liu8a8a53a2015-05-15 16:19:57 -0700243 updateConfigForPhoneId(i);
244 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700245 }
246 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700247 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700248
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800249 case EVENT_DO_FETCH_DEFAULT: {
Rambo Wang9bc7d362021-03-09 09:10:58 -0800250 // Clear in-memory cache for carrier app config, so when carrier app gets
251 // uninstalled, no stale config is left.
252 if (mConfigFromCarrierApp[phoneId] != null
253 && getCarrierPackageForPhoneId(phoneId) == null) {
254 mConfigFromCarrierApp[phoneId] = null;
255 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100256 // Restore persistent override values.
257 PersistableBundle config = restoreConfigFromXml(
258 mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId);
259 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800260 logd("Loaded persistent override config from XML. package="
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100261 + mPlatformCarrierConfigPackage
262 + " phoneId=" + phoneId);
263 mPersistentOverrideConfigs[phoneId] = config;
264 }
265
266 config = restoreConfigFromXml(mPlatformCarrierConfigPackage, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700267 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800268 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700269 "Loaded config from XML. package="
270 + mPlatformCarrierConfigPackage
271 + " phoneId="
272 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700273 mConfigFromDefaultApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700274 Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700275 newMsg.getData().putBoolean("loaded_from_xml", true);
276 mHandler.sendMessage(newMsg);
277 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700278 // No cached config, so fetch it from the default app.
279 if (bindToConfigPackage(
280 mPlatformCarrierConfigPackage,
281 phoneId,
282 EVENT_CONNECTED_TO_DEFAULT)) {
283 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800284 obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1 /*arg2*/,
285 getMessageToken(phoneId)),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700286 BIND_TIMEOUT_MILLIS);
287 } else {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800288 // Put a stub bundle in place so that the rest of the logic continues
289 // smoothly.
290 mConfigFromDefaultApp[phoneId] = new PersistableBundle();
Jonathan Basseri65273c82017-07-25 15:08:42 -0700291 // Send broadcast if bind fails.
Nathan Harold48ac0972019-03-13 22:33:01 -0700292 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700293 // TODO: We *must* call unbindService even if bindService returns false.
294 // (And possibly if SecurityException was thrown.)
chen xudb04c292019-03-26 17:01:15 -0700295 loge("binding to default app: "
296 + mPlatformCarrierConfigPackage + " fails");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700297 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800298 }
299 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700300 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800301
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800302 case EVENT_CONNECTED_TO_DEFAULT: {
Rambo Wang610fdf62021-03-08 21:37:11 -0800303 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT, getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700304 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800305 // If new service connection has been created, unbind.
306 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Jordan Liu178430d2020-02-10 14:32:15 -0800307 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800308 break;
309 }
chen xu02581692018-11-11 19:03:44 -0800310 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700311 // ResultReceiver callback will execute in this Handler's thread.
312 final ResultReceiver resultReceiver =
313 new ResultReceiver(this) {
314 @Override
315 public void onReceiveResult(int resultCode, Bundle resultData) {
Jordan Liu178430d2020-02-10 14:32:15 -0800316 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700317 // If new service connection has been created, this is stale.
318 if (mServiceConnection[phoneId] != conn) {
319 loge("Received response for stale request.");
320 return;
321 }
Rambo Wang610fdf62021-03-08 21:37:11 -0800322 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT,
323 getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700324 if (resultCode == RESULT_ERROR || resultData == null) {
325 // On error, abort config fetching.
326 loge("Failed to get carrier config");
Nathan Harold48ac0972019-03-13 22:33:01 -0700327 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700328 return;
329 }
330 PersistableBundle config =
331 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100332 saveConfigToXml(mPlatformCarrierConfigPackage, "", phoneId,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800333 carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700334 mConfigFromDefaultApp[phoneId] = config;
335 sendMessage(
336 obtainMessage(
337 EVENT_FETCH_DEFAULT_DONE, phoneId, -1));
338 }
339 };
340 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800341 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700342 ICarrierService carrierService =
343 ICarrierService.Stub.asInterface(conn.service);
344 carrierService.getCarrierConfig(carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800345 logdWithLocalLog("Fetch config for default app: "
chen xudb04c292019-03-26 17:01:15 -0700346 + mPlatformCarrierConfigPackage
347 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700348 } catch (RemoteException e) {
chen xudb04c292019-03-26 17:01:15 -0700349 loge("Failed to get carrier config from default app: " +
350 mPlatformCarrierConfigPackage + " err: " + e.toString());
Jordan Liu178430d2020-02-10 14:32:15 -0800351 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700352 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800353 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700354 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800355 obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1 /*arg2*/,
356 getMessageToken(phoneId)),
Jonathan Basseri65273c82017-07-25 15:08:42 -0700357 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800358 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700359 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800360
Jonathan Basseri930701e2015-06-11 20:56:43 -0700361 case EVENT_BIND_DEFAULT_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800362 case EVENT_FETCH_DEFAULT_TIMEOUT: {
363 loge("Bind/fetch time out from " + mPlatformCarrierConfigPackage);
Rambo Wang610fdf62021-03-08 21:37:11 -0800364 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT, getMessageToken(phoneId));
chen xuf60b3162019-05-01 21:31:05 -0700365 // If we attempted to bind to the app, but the service connection is null due to
366 // the race condition that clear config event happens before bind/fetch complete
367 // then config was cleared while we were waiting and we should not continue.
368 if (mServiceConnection[phoneId] != null) {
369 // If a ResponseReceiver callback is in the queue when this happens, we will
370 // unbind twice and throw an exception.
Jordan Liu178430d2020-02-10 14:32:15 -0800371 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700372 broadcastConfigChangedIntent(phoneId);
373 }
Nathan Harold1122e3d2021-01-22 15:45:24 -0800374 // Put a stub bundle in place so that the rest of the logic continues smoothly.
375 mConfigFromDefaultApp[phoneId] = new PersistableBundle();
Nathan Harold48ac0972019-03-13 22:33:01 -0700376 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700377 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700378 }
Jonathan Basseri930701e2015-06-11 20:56:43 -0700379
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800380 case EVENT_FETCH_DEFAULT_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700381 // If we attempted to bind to the app, but the service connection is null, then
382 // config was cleared while we were waiting and we should not continue.
383 if (!msg.getData().getBoolean("loaded_from_xml", false)
384 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800385 break;
386 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700387 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700388 if (carrierPackageName != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800389 logd("Found carrier config app: " + carrierPackageName);
Jordan Liu38a614c2019-03-20 15:01:17 -0700390 sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId, -1));
Jonathan Basserib919e932015-05-27 16:53:08 +0000391 } else {
Nathan Harold48ac0972019-03-13 22:33:01 -0700392 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700393 }
394 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700395 }
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700396
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800397 case EVENT_DO_FETCH_CARRIER: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700398 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700399 final PersistableBundle config =
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100400 restoreConfigFromXml(carrierPackageName, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700401 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800402 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700403 "Loaded config from XML. package="
404 + carrierPackageName
405 + " phoneId="
406 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700407 mConfigFromCarrierApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700408 Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700409 newMsg.getData().putBoolean("loaded_from_xml", true);
410 sendMessage(newMsg);
411 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700412 // No cached config, so fetch it from a carrier app.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800413 if (carrierPackageName != null && bindToConfigPackage(carrierPackageName,
414 phoneId, EVENT_CONNECTED_TO_CARRIER)) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700415 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800416 obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1 /*arg2*/,
417 getMessageToken(phoneId)),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700418 BIND_TIMEOUT_MILLIS);
419 } else {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800420 // Put a stub bundle in place so that the rest of the logic continues
421 // smoothly.
422 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
Jonathan Basseri65273c82017-07-25 15:08:42 -0700423 // Send broadcast if bind fails.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700424 broadcastConfigChangedIntent(phoneId);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800425 loge("Bind to carrier app: " + carrierPackageName + " fails");
Nathan Harold48ac0972019-03-13 22:33:01 -0700426 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700427 }
428 }
429 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700430 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700431
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800432 case EVENT_CONNECTED_TO_CARRIER: {
Rambo Wang610fdf62021-03-08 21:37:11 -0800433 removeMessages(EVENT_BIND_CARRIER_TIMEOUT, getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700434 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800435 // If new service connection has been created, unbind.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700436 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Jordan Liu178430d2020-02-10 14:32:15 -0800437 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800438 break;
439 }
chen xu02581692018-11-11 19:03:44 -0800440 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700441 // ResultReceiver callback will execute in this Handler's thread.
442 final ResultReceiver resultReceiver =
443 new ResultReceiver(this) {
444 @Override
445 public void onReceiveResult(int resultCode, Bundle resultData) {
Jordan Liu178430d2020-02-10 14:32:15 -0800446 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700447 // If new service connection has been created, this is stale.
448 if (mServiceConnection[phoneId] != conn) {
449 loge("Received response for stale request.");
450 return;
451 }
Rambo Wang610fdf62021-03-08 21:37:11 -0800452 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT,
453 getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700454 if (resultCode == RESULT_ERROR || resultData == null) {
455 // On error, abort config fetching.
chen xudb04c292019-03-26 17:01:15 -0700456 loge("Failed to get carrier config from carrier app: "
457 + getCarrierPackageForPhoneId(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700458 broadcastConfigChangedIntent(phoneId);
Nathan Harold48ac0972019-03-13 22:33:01 -0700459 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700460 return;
461 }
462 PersistableBundle config =
463 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100464 saveConfigToXml(getCarrierPackageForPhoneId(phoneId), "",
465 phoneId, carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700466 mConfigFromCarrierApp[phoneId] = config;
467 sendMessage(
468 obtainMessage(
469 EVENT_FETCH_CARRIER_DONE, phoneId, -1));
470 }
471 };
472 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800473 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700474 ICarrierService carrierService =
475 ICarrierService.Stub.asInterface(conn.service);
476 carrierService.getCarrierConfig(carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800477 logdWithLocalLog("Fetch config for carrier app: "
chen xudb04c292019-03-26 17:01:15 -0700478 + getCarrierPackageForPhoneId(phoneId)
479 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700480 } catch (RemoteException e) {
481 loge("Failed to get carrier config: " + e.toString());
Jordan Liu178430d2020-02-10 14:32:15 -0800482 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700483 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800484 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700485 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800486 obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1 /*arg2*/,
487 getMessageToken(phoneId)),
Jonathan Basseri65273c82017-07-25 15:08:42 -0700488 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800489 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700490 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800491
Jonathan Basseri930701e2015-06-11 20:56:43 -0700492 case EVENT_BIND_CARRIER_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800493 case EVENT_FETCH_CARRIER_TIMEOUT: {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800494 loge("Bind/fetch from carrier app timeout, package="
495 + getCarrierPackageForPhoneId(phoneId));
Rambo Wang610fdf62021-03-08 21:37:11 -0800496 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT, getMessageToken(phoneId));
chen xuf60b3162019-05-01 21:31:05 -0700497 // If we attempted to bind to the app, but the service connection is null due to
498 // the race condition that clear config event happens before bind/fetch complete
499 // then config was cleared while we were waiting and we should not continue.
500 if (mServiceConnection[phoneId] != null) {
501 // If a ResponseReceiver callback is in the queue when this happens, we will
502 // unbind twice and throw an exception.
Jordan Liu178430d2020-02-10 14:32:15 -0800503 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700504 broadcastConfigChangedIntent(phoneId);
505 }
Nathan Harold1122e3d2021-01-22 15:45:24 -0800506 // Put a stub bundle in place so that the rest of the logic continues smoothly.
507 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
Nathan Harold48ac0972019-03-13 22:33:01 -0700508 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700509 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700510 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800511 case EVENT_FETCH_CARRIER_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700512 // If we attempted to bind to the app, but the service connection is null, then
513 // config was cleared while we were waiting and we should not continue.
514 if (!msg.getData().getBoolean("loaded_from_xml", false)
515 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800516 break;
517 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700518 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800519 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700520 }
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700521
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800522 case EVENT_CHECK_SYSTEM_UPDATE: {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700523 SharedPreferences sharedPrefs =
524 PreferenceManager.getDefaultSharedPreferences(mContext);
525 final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
526 if (!Build.FINGERPRINT.equals(lastFingerprint)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800527 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700528 "Build fingerprint changed. old: "
529 + lastFingerprint
530 + " new: "
531 + Build.FINGERPRINT);
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700532 clearCachedConfigForPackage(null);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700533 sharedPrefs
534 .edit()
535 .putString(KEY_FINGERPRINT, Build.FINGERPRINT)
536 .apply();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700537 }
538 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700539 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700540
541 case EVENT_SUBSCRIPTION_INFO_UPDATED:
542 broadcastConfigChangedIntent(phoneId);
543 break;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700544 case EVENT_MULTI_SIM_CONFIG_CHANGED:
545 onMultiSimConfigChanged();
546 break;
Jayachandran C645ec612020-01-10 10:30:25 -0800547
548 case EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG: {
549 PersistableBundle config =
550 restoreNoSimConfigFromXml(mPlatformCarrierConfigPackage);
551
552 if (config != null) {
553 logd("Loaded no SIM config from XML. package="
554 + mPlatformCarrierConfigPackage);
555 mNoSimConfig = config;
556 sendMessage(
557 obtainMessage(
558 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE,
559 phoneId, -1));
560 } else {
561 // No cached config, so fetch it from the default app.
562 if (bindToConfigPackage(
563 mPlatformCarrierConfigPackage,
564 phoneId,
565 EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG)) {
566 sendMessageDelayed(
567 obtainMessage(
568 EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT,
569 phoneId, -1), BIND_TIMEOUT_MILLIS);
570 } else {
571 broadcastConfigChangedIntent(phoneId, false);
572 // TODO: We *must* call unbindService even if bindService returns false.
573 // (And possibly if SecurityException was thrown.)
574 loge("binding to default app to fetch no SIM config: "
575 + mPlatformCarrierConfigPackage + " fails");
576 }
577 }
578 break;
579 }
580
581 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE: {
582 broadcastConfigChangedIntent(phoneId, false);
583 break;
584 }
585
586 case EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
587 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT: {
588 loge("Bind/fetch time out for no SIM config from "
589 + mPlatformCarrierConfigPackage);
590 removeMessages(EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
591 // If we attempted to bind to the app, but the service connection is null due to
592 // the race condition that clear config event happens before bind/fetch complete
593 // then config was cleared while we were waiting and we should not continue.
594 if (mServiceConnectionForNoSimConfig[phoneId] != null) {
595 // If a ResponseReceiver callback is in the queue when this happens, we will
596 // unbind twice and throw an exception.
597 unbindIfBoundForNoSimConfig(mContext,
598 mServiceConnectionForNoSimConfig[phoneId], phoneId);
599 }
600 broadcastConfigChangedIntent(phoneId, false);
601 break;
602 }
603
604 case EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG: {
605 removeMessages(EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
606 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
607 // If new service connection has been created, unbind.
608 if (mServiceConnectionForNoSimConfig[phoneId] != conn || conn.service == null) {
609 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
610 break;
611 }
612
613 // ResultReceiver callback will execute in this Handler's thread.
614 final ResultReceiver resultReceiver =
615 new ResultReceiver(this) {
616 @Override
617 public void onReceiveResult(int resultCode, Bundle resultData) {
618 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
619 // If new service connection has been created, this is stale.
620 if (mServiceConnectionForNoSimConfig[phoneId] != conn) {
621 loge("Received response for stale request.");
622 return;
623 }
624 removeMessages(EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
625 if (resultCode == RESULT_ERROR || resultData == null) {
626 // On error, abort config fetching.
627 loge("Failed to get no SIM carrier config");
628 return;
629 }
630 PersistableBundle config =
631 resultData.getParcelable(KEY_CONFIG_BUNDLE);
632 saveNoSimConfigToXml(mPlatformCarrierConfigPackage, config);
633 mNoSimConfig = config;
634 sendMessage(
635 obtainMessage(
636 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE,
637 phoneId, -1));
638 }
639 };
640 // Now fetch the config asynchronously from the ICarrierService.
641 try {
642 ICarrierService carrierService =
643 ICarrierService.Stub.asInterface(conn.service);
644 carrierService.getCarrierConfig(null, resultReceiver);
645 logdWithLocalLog("Fetch no sim config from default app: "
646 + mPlatformCarrierConfigPackage);
647 } catch (RemoteException e) {
648 loge("Failed to get no sim carrier config from default app: " +
649 mPlatformCarrierConfigPackage + " err: " + e.toString());
650 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
651 break; // So we don't set a timeout.
652 }
653 sendMessageDelayed(
654 obtainMessage(
655 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT,
656 phoneId, -1), BIND_TIMEOUT_MILLIS);
657 break;
658 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800659 }
660 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700661 }
662
663 private final Handler mHandler;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800664
665 /**
666 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
667 * receiver for relevant events.
668 */
669 private CarrierConfigLoader(Context context) {
670 mContext = context;
Meng Wang33ad2bc2017-03-16 20:21:20 -0700671 mPlatformCarrierConfigPackage =
672 mContext.getString(R.string.platform_carrier_config_package);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700673 mHandler = new ConfigHandler();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800674
Nanxi Chen3d670502016-03-17 16:32:09 -0700675 IntentFilter bootFilter = new IntentFilter();
676 bootFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
677 context.registerReceiver(mBootReceiver, bootFilter);
678
Junda Liu8a8a53a2015-05-15 16:19:57 -0700679 // Register for package updates. Update app or uninstall app update will have all 3 intents,
680 // in the order or removed, added, replaced, all with extra_replace set to true.
681 IntentFilter pkgFilter = new IntentFilter();
682 pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
683 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
684 pkgFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
685 pkgFilter.addDataScheme("package");
Jordan Liu5ca24762019-07-10 12:51:09 -0700686 context.registerReceiver(mPackageReceiver, pkgFilter);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800687
Malcolm Chen978dda52019-10-08 18:15:25 -0700688 int numPhones = TelephonyManager.from(context).getSupportedModemCount();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700689 mConfigFromDefaultApp = new PersistableBundle[numPhones];
690 mConfigFromCarrierApp = new PersistableBundle[numPhones];
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100691 mPersistentOverrideConfigs = new PersistableBundle[numPhones];
Hall Liu506724b2018-10-22 18:16:14 -0700692 mOverrideConfigs = new PersistableBundle[numPhones];
Jayachandran C645ec612020-01-10 10:30:25 -0800693 mNoSimConfig = new PersistableBundle();
Zach Johnson36d7aab2015-05-22 15:43:00 -0700694 mServiceConnection = new CarrierServiceConnection[numPhones];
Jordan Liu178430d2020-02-10 14:32:15 -0800695 mServiceBound = new boolean[numPhones];
Junda Liu03aad762017-07-21 13:32:17 -0700696 mHasSentConfigChange = new boolean[numPhones];
Sarah Chin807d5c62020-03-30 17:21:09 -0700697 mFromSystemUnlocked = new boolean[numPhones];
Jayachandran C645ec612020-01-10 10:30:25 -0800698 mServiceConnectionForNoSimConfig = new CarrierServiceConnection[numPhones];
699 mServiceBoundForNoSimConfig = new boolean[numPhones];
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800700 // Make this service available through ServiceManager.
Peter Wangc035ce42020-01-08 21:00:22 -0800701 TelephonyFrameworkInitializer
702 .getTelephonyServiceManager().getCarrierConfigServiceRegisterer().register(this);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800703 logd("CarrierConfigLoader has started");
Nathan Harold48ac0972019-03-13 22:33:01 -0700704 mSubscriptionInfoUpdater = PhoneFactory.getSubscriptionInfoUpdater();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700705 mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800706 }
707
708 /**
709 * Initialize the singleton CarrierConfigLoader instance.
710 *
711 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
712 */
713 /* package */
714 static CarrierConfigLoader init(Context context) {
715 synchronized (CarrierConfigLoader.class) {
716 if (sInstance == null) {
717 sInstance = new CarrierConfigLoader(context);
718 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700719 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800720 }
721 return sInstance;
722 }
723 }
724
Jayachandran C645ec612020-01-10 10:30:25 -0800725 private void clearConfigForPhone(int phoneId, boolean fetchNoSimConfig) {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700726 /* Ignore clear configuration request if device is being shutdown. */
727 Phone phone = PhoneFactory.getPhone(phoneId);
728 if (phone != null) {
729 if (phone.isShuttingDown()) {
730 return;
731 }
732 }
733
734 mConfigFromDefaultApp[phoneId] = null;
735 mConfigFromCarrierApp[phoneId] = null;
736 mServiceConnection[phoneId] = null;
737 mHasSentConfigChange[phoneId] = false;
738
Jayachandran C645ec612020-01-10 10:30:25 -0800739 if (fetchNoSimConfig) {
740 // To fetch no SIM config
741 mHandler.sendMessage(
742 mHandler.obtainMessage(
743 EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG, phoneId, -1));
744 }
Malcolm Chen5ea18532019-10-24 19:55:44 -0700745 }
746
Nathan Harold48ac0972019-03-13 22:33:01 -0700747 private void notifySubscriptionInfoUpdater(int phoneId) {
748 String configPackagename;
749 PersistableBundle configToSend;
750 int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
751 // Prefer the carrier privileged carrier app, but if there is not one, use the platform
752 // default carrier app.
753 if (mConfigFromCarrierApp[phoneId] != null) {
754 configPackagename = getCarrierPackageForPhoneId(phoneId);
755 configToSend = mConfigFromCarrierApp[phoneId];
756 } else {
757 configPackagename = mPlatformCarrierConfigPackage;
758 configToSend = mConfigFromDefaultApp[phoneId];
759 }
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700760
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800761 if (configToSend == null) {
762 configToSend = new PersistableBundle();
763 }
764
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700765 // mOverrideConfigs is for testing. And it will override current configs.
766 PersistableBundle config = mOverrideConfigs[phoneId];
767 if (config != null) {
Torbjorn Eklund1050cb02018-11-16 14:05:38 +0100768 configToSend = new PersistableBundle(configToSend);
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700769 configToSend.putAll(config);
770 }
771
Nathan Harold48ac0972019-03-13 22:33:01 -0700772 mSubscriptionInfoUpdater.updateSubscriptionByCarrierConfigAndNotifyComplete(
773 phoneId, configPackagename, configToSend,
774 mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1));
775 }
776
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800777 private void broadcastConfigChangedIntent(int phoneId) {
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800778 broadcastConfigChangedIntent(phoneId, true);
779 }
780
781 private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800782 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800783 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
chen xuf9db49f2019-03-31 23:04:42 -0700784 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800785 if (addSubIdExtra) {
786 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
787 int[] subIds = SubscriptionManager.getSubId(phoneId);
788 if (!ArrayUtils.isEmpty(subIds)) {
789 TelephonyManager telMgr = TelephonyManager.from(mContext)
790 .createForSubscriptionId(subIds[0]);
791 simApplicationState = telMgr.getSimApplicationState();
792 }
Malcolm Chenf7635ec2020-04-15 15:45:00 -0700793 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId
794 + " simApplicationState " + simApplicationState);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800795 // Include subId/carrier id extra only if SIM records are loaded
796 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
797 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
798 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
799 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID,
800 getSpecificCarrierIdForPhoneId(phoneId));
801 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, getCarrierIdForPhoneId(phoneId));
802 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800803 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800804 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Sarah Chin807d5c62020-03-30 17:21:09 -0700805 intent.putExtra(CarrierConfigManager.EXTRA_REBROADCAST_ON_UNLOCK,
806 mFromSystemUnlocked[phoneId]);
Meng Wang97a6a462020-01-23 16:22:16 -0800807 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
Jack Yue9e95be2020-03-22 10:56:06 -0700808 int[] subIds = SubscriptionManager.getSubId(phoneId);
809 if (subIds != null && subIds.length > 0) {
810 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId + ", subId=" + subIds[0]);
811 } else {
812 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
813 }
Junda Liu03aad762017-07-21 13:32:17 -0700814 mHasSentConfigChange[phoneId] = true;
Sarah Chin807d5c62020-03-30 17:21:09 -0700815 mFromSystemUnlocked[phoneId] = false;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800816 }
817
818 /** Binds to the default or carrier config app. */
819 private boolean bindToConfigPackage(String pkgName, int phoneId, int eventId) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800820 logdWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700821 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700822 carrierService.setPackage(pkgName);
Jayachandran C645ec612020-01-10 10:30:25 -0800823 CarrierServiceConnection serviceConnection = new CarrierServiceConnection(
824 phoneId, pkgName, eventId);
825 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
826 mServiceConnectionForNoSimConfig[phoneId] = serviceConnection;
827 } else {
828 mServiceConnection[phoneId] = serviceConnection;
829 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800830 try {
Jayachandran C645ec612020-01-10 10:30:25 -0800831 if (mContext.bindService(carrierService, serviceConnection,
Jordan Liu178430d2020-02-10 14:32:15 -0800832 Context.BIND_AUTO_CREATE)) {
Jayachandran C645ec612020-01-10 10:30:25 -0800833 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
834 mServiceBoundForNoSimConfig[phoneId] = true;
835 } else {
836 mServiceBound[phoneId] = true;
837 }
Jordan Liu178430d2020-02-10 14:32:15 -0800838 return true;
839 } else {
840 return false;
841 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800842 } catch (SecurityException ex) {
843 return false;
844 }
845 }
846
chen xu02581692018-11-11 19:03:44 -0800847 private CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800848 String mcc = "";
849 String mnc = "";
850 String imsi = "";
851 String gid1 = "";
852 String gid2 = "";
853 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
854 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800855 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800856 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700857 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
858 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800859 mcc = simOperator.substring(0, 3);
860 mnc = simOperator.substring(3);
861 }
862 Phone phone = PhoneFactory.getPhone(phoneId);
863 if (phone != null) {
864 imsi = phone.getSubscriberId();
865 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700866 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800867 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800868 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800869 }
chen xua31f22b2019-03-06 15:28:50 -0800870 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800871 }
872
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700873 /** Returns the package name of a priveleged carrier app, or null if there is none. */
Nathan Harold1122e3d2021-01-22 15:45:24 -0800874 @Nullable
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700875 private String getCarrierPackageForPhoneId(int phoneId) {
876 List<String> carrierPackageNames = TelephonyManager.from(mContext)
877 .getCarrierPackageNamesForIntentAndPhone(
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700878 new Intent(CarrierService.CARRIER_SERVICE_INTERFACE), phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700879 if (carrierPackageNames != null && carrierPackageNames.size() > 0) {
880 return carrierPackageNames.get(0);
881 } else {
882 return null;
883 }
884 }
885
886 private String getIccIdForPhoneId(int phoneId) {
887 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
888 return null;
889 }
890 Phone phone = PhoneFactory.getPhone(phoneId);
891 if (phone == null) {
892 return null;
893 }
894 return phone.getIccSerialNumber();
895 }
896
chen xu02581692018-11-11 19:03:44 -0800897 /**
chen xua31f22b2019-03-06 15:28:50 -0800898 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800899 */
chen xua31f22b2019-03-06 15:28:50 -0800900 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700901 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800902 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700903 }
904 Phone phone = PhoneFactory.getPhone(phoneId);
905 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800906 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700907 }
chen xua31f22b2019-03-06 15:28:50 -0800908 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800909 }
910
911 /**
912 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
913 */
914 private int getCarrierIdForPhoneId(int phoneId) {
915 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
916 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700917 }
chen xu02581692018-11-11 19:03:44 -0800918 Phone phone = PhoneFactory.getPhone(phoneId);
919 if (phone == null) {
920 return TelephonyManager.UNKNOWN_CARRIER_ID;
921 }
922 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700923 }
924
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700925 /**
926 * Writes a bundle to an XML file.
927 *
chen xu02581692018-11-11 19:03:44 -0800928 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -0800929 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -0800930 * should have a single copy of XML file named after carrier id. However, it's still possible
931 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
932 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
933 * carrier while iccid remains the same.
934 *
935 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
936 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700937 *
938 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700939 *
Jayachandran C645ec612020-01-10 10:30:25 -0800940 * @param packageName the name of the package from which we fetched this bundle.
941 * @param extraString An extra string to be used in the XML file name.
942 * @param phoneId the phone ID.
943 * @param carrierId contains all carrier-identifying information.
944 * @param config the bundle to be written. Null will be treated as an empty bundle.
945 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700946 */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100947 private void saveConfigToXml(String packageName, @NonNull String extraString, int phoneId,
Jayachandran C645ec612020-01-10 10:30:25 -0800948 CarrierIdentifier carrierId, PersistableBundle config, boolean isNoSimConfig) {
949 if (packageName == null) {
950 loge("Cannot save config with null packageName");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800951 return;
952 }
953
Jayachandran C645ec612020-01-10 10:30:25 -0800954 String fileName;
955 if (isNoSimConfig) {
956 fileName = getFilenameForNoSimConfig(packageName);
957 } else {
958 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
959 != TelephonyManager.SIM_STATE_LOADED) {
960 loge("Skip save config because SIM records are not loaded.");
961 return;
962 }
963
964 final String iccid = getIccIdForPhoneId(phoneId);
965 final int cid = carrierId.getSpecificCarrierId();
966 if (iccid == null) {
967 loge("Cannot save config with null iccid.");
968 return;
969 }
970 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700971 }
Jayachandran C645ec612020-01-10 10:30:25 -0800972
Junda Liu02596502016-11-15 13:46:43 -0800973 // b/32668103 Only save to file if config isn't empty.
974 // In case of failure, not caching an empty bundle will
975 // try loading config again on next power on or sim loaded.
976 // Downside is for genuinely empty bundle, will bind and load
977 // on every power on.
978 if (config == null || config.isEmpty()) {
979 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700980 }
981
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700982 final String version = getPackageVersion(packageName);
983 if (version == null) {
984 loge("Failed to get package version for: " + packageName);
985 return;
986 }
987
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800988 logdWithLocalLog(
989 "Save config to xml, packagename: " + packageName + " phoneId: " + phoneId);
chen xudb04c292019-03-26 17:01:15 -0700990
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700991 FileOutputStream outFile = null;
992 try {
Jayachandran C645ec612020-01-10 10:30:25 -0800993 outFile = new FileOutputStream(new File(mContext.getFilesDir(), fileName));
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800994 config.putString(KEY_VERSION, version);
995 config.writeToStream(outFile);
996 outFile.flush();
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700997 outFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800998 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700999 loge(e.toString());
1000 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001001 }
1002
Jayachandran C645ec612020-01-10 10:30:25 -08001003 private void saveConfigToXml(String packageName, @NonNull String extraString, int phoneId,
1004 CarrierIdentifier carrierId, PersistableBundle config) {
1005 saveConfigToXml(packageName, extraString, phoneId, carrierId, config, false);
1006 }
1007
1008 private void saveNoSimConfigToXml(String packageName, PersistableBundle config) {
1009 saveConfigToXml(packageName, "", -1, null, config, true);
1010 }
1011
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001012 /**
1013 * Reads a bundle from an XML file.
1014 *
1015 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +08001016 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001017 *
1018 * In case of errors, or if the saved config is from a different package version than the
1019 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001020 *
Jayachandran C645ec612020-01-10 10:30:25 -08001021 * @param packageName the name of the package from which we fetched this bundle.
1022 * @param extraString An extra string to be used in the XML file name.
1023 * @param phoneId the phone ID.
1024 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001025 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001026 * version does not match, or reading config fails.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001027 */
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001028 private PersistableBundle restoreConfigFromXml(String packageName, @NonNull String extraString,
Jayachandran C645ec612020-01-10 10:30:25 -08001029 int phoneId, boolean isNoSimConfig) {
1030 if (packageName == null) {
1031 loge("Cannot restore config with null packageName");
1032 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001033 final String version = getPackageVersion(packageName);
1034 if (version == null) {
1035 loge("Failed to get package version for: " + packageName);
1036 return null;
1037 }
Yuchen Dongc15afed2018-04-26 17:05:22 +08001038
Jayachandran C645ec612020-01-10 10:30:25 -08001039 String fileName;
1040 if (isNoSimConfig) {
1041 fileName = getFilenameForNoSimConfig(packageName);
1042 } else {
1043 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
1044 != TelephonyManager.SIM_STATE_LOADED) {
1045 loge("Skip restore config because SIM records are not loaded.");
1046 return null;
1047 }
1048
1049 final String iccid = getIccIdForPhoneId(phoneId);
1050 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1051 if (iccid == null) {
1052 loge("Cannot restore config with null iccid.");
1053 return null;
1054 }
1055 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001056 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001057
1058 PersistableBundle restoredBundle = null;
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001059 File file = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001060 FileInputStream inFile = null;
1061 try {
Jayachandran C645ec612020-01-10 10:30:25 -08001062 file = new File(mContext.getFilesDir(),fileName);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001063 inFile = new FileInputStream(file);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001064
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001065 restoredBundle = PersistableBundle.readFromStream(inFile);
1066 String savedVersion = restoredBundle.getString(KEY_VERSION);
1067 restoredBundle.remove(KEY_VERSION);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001068
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001069 if (!version.equals(savedVersion)) {
1070 loge("Saved version mismatch: " + version + " vs " + savedVersion);
1071 restoredBundle = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001072 }
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001073
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001074 inFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001075 } catch (FileNotFoundException e) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001076 // Missing file is normal occurrence that might occur with a new sim or when restoring
1077 // an override file during boot and should not be treated as an error.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001078 if (file != null) logd("File not found: " + file.getPath());
1079 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001080 loge(e.toString());
1081 }
1082
1083 return restoredBundle;
1084 }
1085
Jayachandran C645ec612020-01-10 10:30:25 -08001086 private PersistableBundle restoreConfigFromXml(String packageName, @NonNull String extraString,
1087 int phoneId) {
1088 return restoreConfigFromXml(packageName, extraString, phoneId, false);
1089 }
1090
1091 private PersistableBundle restoreNoSimConfigFromXml(String packageName) {
1092 return restoreConfigFromXml(packageName, "", -1, true);
1093 }
1094
Junda Liu8a8a53a2015-05-15 16:19:57 -07001095 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001096 * Clears cached carrier config.
1097 * This deletes all saved XML files associated with the given package name. If packageName is
1098 * null, then it deletes all saved XML files.
1099 *
1100 * @param packageName the name of a carrier package, or null if all cached config should be
1101 * cleared.
1102 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -07001103 */
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001104 private boolean clearCachedConfigForPackage(final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001105 File dir = mContext.getFilesDir();
1106 File[] packageFiles = dir.listFiles(new FilenameFilter() {
1107 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001108 if (packageName != null) {
1109 return filename.startsWith("carrierconfig-" + packageName + "-");
1110 } else {
1111 return filename.startsWith("carrierconfig-");
1112 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001113 }
1114 });
Junda Liu8a8a53a2015-05-15 16:19:57 -07001115 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001116 for (File f : packageFiles) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001117 logd("Deleting " + f.getName());
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001118 f.delete();
1119 }
Junda Liu8a8a53a2015-05-15 16:19:57 -07001120 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001121 }
1122
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001123 /** Builds a canonical file name for a config file. */
Jayachandran C645ec612020-01-10 10:30:25 -08001124 private static String getFilenameForConfig(
1125 @NonNull String packageName, @NonNull String extraString,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001126 @NonNull String iccid, int cid) {
chen xu02581692018-11-11 19:03:44 -08001127 // the same carrier should have a single copy of XML file named after carrier id.
1128 // However, it's still possible that platform doesn't recognize the current sim carrier,
1129 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
1130 // cases SIM OTA resolves to different carrier while iccid remains the same.
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001131 return "carrierconfig-" + packageName + extraString + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001132 }
1133
Jayachandran C645ec612020-01-10 10:30:25 -08001134 /** Builds a canonical file name for no SIM config file. */
1135 private String getFilenameForNoSimConfig(@NonNull String packageName) {
1136 return "carrierconfig-" + packageName + "-" + "nosim" + ".xml";
1137 }
1138
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001139 /** Return the current version code of a package, or null if the name is not found. */
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001140 private String getPackageVersion(String packageName) {
1141 try {
1142 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -08001143 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001144 } catch (PackageManager.NameNotFoundException e) {
1145 return null;
1146 }
1147 }
1148
Jonathan Basseri65273c82017-07-25 15:08:42 -07001149 /**
1150 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001151 *
1152 * This reads config bundles for the given phoneId. That means getting the latest bundle from
1153 * the default app and a privileged carrier app, if present. This will not bind to an app if we
1154 * have a saved config file to use instead.
1155 */
1156 private void updateConfigForPhoneId(int phoneId) {
Jonathan Basseri65273c82017-07-25 15:08:42 -07001157 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001158 }
1159
Malcolm Chen5ea18532019-10-24 19:55:44 -07001160 private void onMultiSimConfigChanged() {
1161 for (int i = TelephonyManager.from(mContext).getActiveModemCount();
1162 i < mConfigFromDefaultApp.length; i++) {
1163 clearConfigForPhone(i, false);
1164 }
1165 }
1166
Hall Liu506724b2018-10-22 18:16:14 -07001167 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001168 @NonNull
1169 public PersistableBundle getConfigForSubId(int subId, String callingPackage) {
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08001170 return getConfigForSubIdWithFeature(subId, callingPackage, null);
1171 }
1172
1173 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001174 @NonNull
1175 public PersistableBundle getConfigForSubIdWithFeature(int subId, String callingPackage,
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08001176 String callingFeatureId) {
1177 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subId, callingPackage,
1178 callingFeatureId, "getCarrierConfig")) {
Malcolm Chen6d3d6b32018-03-01 13:58:03 -08001179 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -07001180 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -07001181
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001182 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001183 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001184 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001185 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001186 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001187 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001188 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001189 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001190 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001191 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001192 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001193 config = mPersistentOverrideConfigs[phoneId];
1194 if (config != null) {
1195 retConfig.putAll(config);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001196 }
Hall Liu506724b2018-10-22 18:16:14 -07001197 config = mOverrideConfigs[phoneId];
1198 if (config != null) {
1199 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -07001200 }
Nathan Harold1122e3d2021-01-22 15:45:24 -08001201 // Ignore the theoretical case of the default app not being present since that won't
1202 // work in CarrierConfigLoader today.
1203 final boolean allConfigsApplied =
1204 (mConfigFromCarrierApp[phoneId] != null
1205 || getCarrierPackageForPhoneId(phoneId) == null)
1206 && mConfigFromDefaultApp[phoneId] != null;
1207 retConfig.putBoolean(
1208 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, allConfigsApplied);
Jayachandran C645ec612020-01-10 10:30:25 -08001209 } else {
1210 if (mNoSimConfig != null) {
1211 retConfig.putAll(mNoSimConfig);
1212 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001213 }
1214 return retConfig;
1215 }
1216
1217 @Override
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001218 public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides,
1219 boolean persistent) {
Hall Liu506724b2018-10-22 18:16:14 -07001220 mContext.enforceCallingOrSelfPermission(
1221 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Brad Ebingerf6281ad2019-04-25 11:02:04 -07001222 //TODO: Also check for SHELL UID to restrict this method to testing only (b/131326259)
Hall Liu506724b2018-10-22 18:16:14 -07001223 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1224 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001225 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
Hall Liu506724b2018-10-22 18:16:14 -07001226 return;
1227 }
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001228 // Post to run on handler thread on which all states should be confined.
1229 mHandler.post(() -> {
1230 overrideConfig(mOverrideConfigs, phoneId, overrides);
Hall Liu506724b2018-10-22 18:16:14 -07001231
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001232 if (persistent) {
1233 overrideConfig(mPersistentOverrideConfigs, phoneId, overrides);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001234
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001235 if (overrides != null) {
1236 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
1237 saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION,
1238 phoneId,
1239 carrierId, mPersistentOverrideConfigs[phoneId]);
1240 } else {
1241 final String iccid = getIccIdForPhoneId(phoneId);
1242 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1243 String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage,
1244 OVERRIDE_PACKAGE_ADDITION, iccid, cid);
1245 File fileToDelete = new File(mContext.getFilesDir(), fileName);
1246 fileToDelete.delete();
1247 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001248 }
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001249 notifySubscriptionInfoUpdater(phoneId);
1250 });
Hall Liu506724b2018-10-22 18:16:14 -07001251 }
1252
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001253 private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId,
1254 @Nullable PersistableBundle overrides) {
1255 if (overrides == null) {
1256 currentOverrides[phoneId] = new PersistableBundle();
1257 } else if (currentOverrides[phoneId] == null) {
1258 currentOverrides[phoneId] = overrides;
1259 } else {
1260 currentOverrides[phoneId].putAll(overrides);
1261 }
1262 }
1263
Hall Liu506724b2018-10-22 18:16:14 -07001264 @Override
Jonathan Basseri86030352015-06-08 12:27:56 -07001265 public void notifyConfigChangedForSubId(int subId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001266 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001267 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001268 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001269 return;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001270 }
Jordan Liud7d57fe2019-04-16 12:29:43 -07001271
1272 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001273 // system privileged app with MODIFY_PHONE_STATE permission.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001274 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext, subId,
1275 "Require carrier privileges or MODIFY_PHONE_STATE permission.");
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001276
1277 // This method should block until deleting has completed, so that an error which prevents us
1278 // from clearing the cache is passed back to the carrier app. With the files successfully
1279 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001280 String callingPackageName = mContext.getPackageManager().getNameForUid(
1281 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001282 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001283 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001284 }
1285
1286 @Override
1287 public void updateConfigForPhoneId(int phoneId, String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -07001288 mContext.enforceCallingOrSelfPermission(
1289 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001290 logdWithLocalLog("Update config for phoneId: " + phoneId + " simState: " + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001291 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1292 return;
1293 }
1294 // requires Java 7 for switch on string.
1295 switch (simState) {
1296 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1297 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001298 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001299 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Malcolm Chendb66de12019-12-09 18:57:07 -08001300 case IccCardConstants.INTENT_VALUE_ICC_NOT_READY:
Junda Liu9f2d2712015-05-15 14:22:45 -07001301 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001302 break;
1303 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1304 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001305 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001306 break;
1307 }
1308 }
1309
Junda Liu43d723a2015-05-12 17:23:45 -07001310 @Override
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001311 public String getDefaultCarrierServicePackageName() {
Shuo Qianefdb7962019-12-19 15:54:27 -08001312 mContext.enforceCallingOrSelfPermission(
1313 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1314 "getDefaultCarrierServicePackageName");
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001315 return mPlatformCarrierConfigPackage;
1316 }
1317
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001318 private void unbindIfBound(Context context, CarrierServiceConnection conn,
1319 int phoneId) {
1320 if (mServiceBound[phoneId]) {
1321 mServiceBound[phoneId] = false;
1322 context.unbindService(conn);
1323 }
1324 }
1325
Jayachandran C645ec612020-01-10 10:30:25 -08001326 private void unbindIfBoundForNoSimConfig(Context context, CarrierServiceConnection conn,
1327 int phoneId) {
1328 if (mServiceBoundForNoSimConfig[phoneId]) {
1329 mServiceBoundForNoSimConfig[phoneId] = false;
1330 context.unbindService(conn);
1331 }
1332 }
1333
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001334 /**
Rambo Wang610fdf62021-03-08 21:37:11 -08001335 * Returns a boxed Integer object for phoneId, services as message token to distinguish messages
1336 * with same code when calling {@link Handler#removeMessages(int, Object)}.
1337 */
1338 private Integer getMessageToken(int phoneId) {
1339 if (phoneId < -128 || phoneId > 127) {
1340 throw new IllegalArgumentException("phoneId should be in range [-128, 127], inclusive");
1341 }
1342 // Integer#valueOf guarantees the integers within [-128, 127] are cached and thus memory
1343 // comparison (==) returns true for the same integer.
1344 return Integer.valueOf(phoneId);
1345 }
1346
1347 /**
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001348 * If {@code args} contains {@link #DUMP_ARG_REQUESTING_PACKAGE} and a following package name,
1349 * we'll also call {@link IBinder#dump} on the default carrier service (if bound) and the
1350 * specified carrier service (if bound). Typically, this is done for connectivity bug reports
1351 * where we don't call {@code dumpsys activity service all-non-platform} because that contains
1352 * too much info, but we still want to let carrier apps include their diagnostics.
1353 */
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001354 @Override
Junda Liu43d723a2015-05-12 17:23:45 -07001355 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001356 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
Junda Liu43d723a2015-05-12 17:23:45 -07001357 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1358 != PackageManager.PERMISSION_GRANTED) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001359 indentPW.println("Permission Denial: can't dump carrierconfig from from pid="
Junda Liu43d723a2015-05-12 17:23:45 -07001360 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1361 return;
1362 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001363 String requestingPackage = null;
1364 int requestingPackageIndex = ArrayUtils.indexOf(args, DUMP_ARG_REQUESTING_PACKAGE);
1365 if (requestingPackageIndex >= 0 && requestingPackageIndex < args.length - 1
1366 && !TextUtils.isEmpty(args[requestingPackageIndex + 1])) {
1367 requestingPackage = args[requestingPackageIndex + 1];
1368 // Throws a SecurityException if the caller is impersonating another app in an effort to
1369 // dump extra info (which may contain PII the caller doesn't have a right to).
1370 enforceCallerIsSystemOrRequestingPackage(requestingPackage);
1371 }
1372
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001373 indentPW.println("CarrierConfigLoader: " + this);
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001374 for (int i = 0; i < TelephonyManager.from(mContext).getActiveModemCount(); i++) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001375 indentPW.println("Phone Id = " + i);
shuoq26a3a4c2016-12-16 11:06:48 -08001376 // display default values in CarrierConfigManager
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001377 printConfig(CarrierConfigManager.getDefaultConfig(), indentPW,
shuoq26a3a4c2016-12-16 11:06:48 -08001378 "Default Values from CarrierConfigManager");
shuoq26a3a4c2016-12-16 11:06:48 -08001379 // display ConfigFromDefaultApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001380 printConfig(mConfigFromDefaultApp[i], indentPW, "mConfigFromDefaultApp");
shuoq26a3a4c2016-12-16 11:06:48 -08001381 // display ConfigFromCarrierApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001382 printConfig(mConfigFromCarrierApp[i], indentPW, "mConfigFromCarrierApp");
1383 printConfig(mPersistentOverrideConfigs[i], indentPW, "mPersistentOverrideConfigs");
1384 printConfig(mOverrideConfigs[i], indentPW, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001385 }
chen xudb04c292019-03-26 17:01:15 -07001386
Jayachandran C645ec612020-01-10 10:30:25 -08001387 printConfig(mNoSimConfig, indentPW, "mNoSimConfig");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001388 indentPW.println("CarrierConfigLoadingLog=");
1389 mCarrierConfigLoadingLog.dump(fd, indentPW, args);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001390
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001391 if (requestingPackage != null) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001392 logd("Including default and requesting package " + requestingPackage
1393 + " carrier services in dump");
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001394 indentPW.println("");
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001395 indentPW.println("Connected services");
1396 dumpCarrierServiceIfBound(fd, indentPW, "Default config package",
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001397 mPlatformCarrierConfigPackage, false /* considerCarrierPrivileges */);
1398 dumpCarrierServiceIfBound(fd, indentPW, "Requesting package", requestingPackage,
1399 true /* considerCarrierPrivileges */);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001400 }
shuoq26a3a4c2016-12-16 11:06:48 -08001401 }
1402
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001403 private void printConfig(PersistableBundle configApp, IndentingPrintWriter indentPW,
1404 String name) {
1405 indentPW.increaseIndent();
shuoq26a3a4c2016-12-16 11:06:48 -08001406 if (configApp == null) {
shuoq26a3a4c2016-12-16 11:06:48 -08001407 indentPW.println(name + " : null ");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001408 indentPW.decreaseIndent();
1409 indentPW.println("");
shuoq26a3a4c2016-12-16 11:06:48 -08001410 return;
1411 }
shuoq26a3a4c2016-12-16 11:06:48 -08001412 indentPW.println(name + " : ");
1413 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1414 Collections.sort(sortedKeys);
1415 indentPW.increaseIndent();
1416 indentPW.increaseIndent();
1417 for (String key : sortedKeys) {
1418 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1419 indentPW.println(key + " = " +
1420 Arrays.toString((Object[]) configApp.get(key)));
1421 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1422 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1423 } else {
1424 indentPW.println(key + " = " + configApp.get(key));
1425 }
Junda Liu43d723a2015-05-12 17:23:45 -07001426 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001427 indentPW.decreaseIndent();
1428 indentPW.decreaseIndent();
1429 indentPW.decreaseIndent();
1430 indentPW.println("");
Junda Liu43d723a2015-05-12 17:23:45 -07001431 }
1432
Hunter Knepshielde601f432020-02-19 10:16:04 -08001433 /**
1434 * Passes without problem when one of these conditions is true:
1435 * - The caller is a privileged UID (e.g. for dumpstate.cpp generating a bug report, where the
1436 * system knows the true caller plumbed in through the {@link android.os.BugreportManager} API).
1437 * - The caller's UID matches the supplied package.
1438 *
1439 * @throws SecurityException if none of the above conditions are met.
1440 */
1441 private void enforceCallerIsSystemOrRequestingPackage(String requestingPackage)
1442 throws SecurityException {
1443 final int callingUid = Binder.getCallingUid();
1444 if (callingUid == Process.ROOT_UID || callingUid == Process.SYSTEM_UID
1445 || callingUid == Process.SHELL_UID || callingUid == Process.PHONE_UID) {
1446 // Bug reports (dumpstate.cpp) run as SHELL, and let some other privileged UIDs through
1447 // as well.
1448 return;
1449 }
1450 // An app is trying to dump extra detail, block it if they aren't who they claim to be.
1451 AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class);
1452 if (appOps == null) {
1453 throw new SecurityException("No AppOps");
1454 }
1455 // Will throw a SecurityException if the UID and package don't match.
1456 appOps.checkPackage(callingUid, requestingPackage);
1457 }
1458
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001459 /**
1460 * Searches for one or more appropriate {@link CarrierService} instances to dump based on the
1461 * current connections.
1462 *
1463 * @param targetPkgName the target package name to dump carrier services for
1464 * @param considerCarrierPrivileges if true, allow a carrier service to be dumped if it shares
1465 * carrier privileges with {@code targetPkgName};
1466 * otherwise, only dump a carrier service if it is {@code
1467 * targetPkgName}
1468 */
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001469 private void dumpCarrierServiceIfBound(FileDescriptor fd, IndentingPrintWriter indentPW,
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001470 String prefix, String targetPkgName, boolean considerCarrierPrivileges) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001471 // Null package is possible if it's early in the boot process, there was a recent crash, we
1472 // loaded the config from XML most recently, or a SIM slot is empty. Carrier apps with
1473 // long-lived bindings should typically get dumped here regardless. Even if an app is being
1474 // used for multiple phoneIds, we assume that it's smart enough to handle that on its own,
1475 // and that in most cases we'd just be dumping duplicate information and bloating a report.
1476 indentPW.increaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001477 indentPW.println(prefix + " : " + targetPkgName);
1478 Set<String> dumpedPkgNames = new ArraySet<>(mServiceConnection.length);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001479 for (CarrierServiceConnection connection : mServiceConnection) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001480 if (connection == null || !SubscriptionManager.isValidPhoneId(connection.phoneId)
1481 || TextUtils.isEmpty(connection.pkgName)) {
1482 continue;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001483 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001484 final String servicePkgName = connection.pkgName;
1485 // Note: we intentionally ignore system components here because we should NOT match the
1486 // shell caller that's typically used for bug reports via non-BugreportManager triggers.
1487 final boolean exactPackageMatch = TextUtils.equals(targetPkgName, servicePkgName);
1488 final boolean carrierPrivilegesMatch =
1489 considerCarrierPrivileges && hasCarrierPrivileges(targetPkgName,
1490 connection.phoneId);
1491 if (!exactPackageMatch && !carrierPrivilegesMatch) continue;
1492 // Make sure this service is actually alive before trying to dump it. We don't pay
1493 // attention to mServiceBound[connection.phoneId] because typically carrier apps will
1494 // request long-lived bindings, and even if we unbind the app, it may still be alive due
1495 // to CarrierServiceBindHelper. Pull it out as a reference so even if it gets set to
1496 // null within the ServiceConnection during unbinding we can avoid an NPE.
1497 final IBinder service = connection.service;
1498 if (service == null || !service.isBinderAlive() || !service.pingBinder()) continue;
1499 // We've got a live service. Last check is just to make sure we don't dump a package
1500 // multiple times.
1501 if (!dumpedPkgNames.add(servicePkgName)) continue;
1502 if (!exactPackageMatch) {
1503 logd(targetPkgName + " has carrier privileges on phoneId " + connection.phoneId
1504 + ", service provided by " + servicePkgName);
1505 indentPW.increaseIndent();
1506 indentPW.println("Proxy : " + servicePkgName);
1507 indentPW.decreaseIndent();
1508 }
1509 // Flush before we let the app output anything to ensure correct ordering of output.
1510 // Internally, Binder#dump calls flush on its printer after finishing so we don't
1511 // need to do anything after.
1512 indentPW.flush();
1513 try {
1514 logd("Dumping " + servicePkgName);
1515 // We don't need to give the carrier service any args.
1516 connection.service.dump(fd, null /* args */);
1517 logd("Done with " + servicePkgName);
1518 } catch (RemoteException e) {
1519 logd("RemoteException from " + servicePkgName, e);
1520 indentPW.increaseIndent();
1521 indentPW.println("RemoteException");
1522 indentPW.increaseIndent();
1523 e.printStackTrace(indentPW);
1524 indentPW.decreaseIndent();
1525 indentPW.decreaseIndent();
1526 // We won't retry this package again because now it's in dumpedPkgNames.
1527 }
1528 indentPW.println("");
Jordan Liu46ed5db2020-01-28 08:55:20 -08001529 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001530 if (dumpedPkgNames.isEmpty()) {
1531 indentPW.increaseIndent();
1532 indentPW.println("Not bound");
1533 indentPW.decreaseIndent();
1534 indentPW.println("");
1535 }
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001536 indentPW.decreaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001537 }
1538
1539 private boolean hasCarrierPrivileges(String pkgName, int phoneId) {
1540 int[] subIds = SubscriptionManager.getSubId(phoneId);
1541 if (ArrayUtils.isEmpty(subIds)) {
1542 return false;
1543 }
1544 return TelephonyManager.from(mContext).createForSubscriptionId(
1545 subIds[0]).checkCarrierPrivilegesForPackage(pkgName)
1546 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001547 }
1548
Zach Johnson36d7aab2015-05-22 15:43:00 -07001549 private class CarrierServiceConnection implements ServiceConnection {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001550 final int phoneId;
1551 final String pkgName;
1552 final int eventId;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001553 IBinder service;
1554
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001555 CarrierServiceConnection(int phoneId, String pkgName, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001556 this.phoneId = phoneId;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001557 this.pkgName = pkgName;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001558 this.eventId = eventId;
1559 }
1560
1561 @Override
1562 public void onServiceConnected(ComponentName name, IBinder service) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001563 logd("Connected to config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001564 this.service = service;
1565 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1566 }
1567
1568 @Override
1569 public void onServiceDisconnected(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001570 logd("Disconnected from config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001571 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001572 }
1573
1574 @Override
1575 public void onBindingDied(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001576 logd("Binding died from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001577 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001578 }
1579
1580 @Override
1581 public void onNullBinding(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001582 logd("Null binding from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001583 this.service = null;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001584 }
1585 }
1586
1587 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1588 @Override
1589 public void onReceive(Context context, Intent intent) {
1590 String action = intent.getAction();
Junda Liu8a8a53a2015-05-15 16:19:57 -07001591 boolean replace = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1592 // If replace is true, only care ACTION_PACKAGE_REPLACED.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001593 if (replace && !Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
Junda Liu8a8a53a2015-05-15 16:19:57 -07001594 return;
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001595 }
Junda Liu8a8a53a2015-05-15 16:19:57 -07001596
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001597 switch (action) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001598 case Intent.ACTION_BOOT_COMPLETED:
1599 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1600 break;
1601
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001602 case Intent.ACTION_PACKAGE_ADDED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001603 case Intent.ACTION_PACKAGE_REMOVED:
Junda Liu8a8a53a2015-05-15 16:19:57 -07001604 case Intent.ACTION_PACKAGE_REPLACED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001605 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1606 String packageName = mContext.getPackageManager().getNameForUid(uid);
Junda Liu8a8a53a2015-05-15 16:19:57 -07001607 if (packageName != null) {
1608 // We don't have a phoneId for arg1.
1609 mHandler.sendMessage(
1610 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, packageName));
1611 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001612 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001613 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001614 }
1615 }
1616
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001617 private void logd(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001618 Log.d(LOG_TAG, msg);
1619 }
1620
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001621 private void logd(String msg, Throwable tr) {
1622 Log.d(LOG_TAG, msg, tr);
1623 }
1624
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001625 private void logdWithLocalLog(String msg) {
chen xudb04c292019-03-26 17:01:15 -07001626 Log.d(LOG_TAG, msg);
1627 mCarrierConfigLoadingLog.log(msg);
1628 }
1629
1630 private void loge(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001631 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07001632 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001633 }
1634}