blob: 76a16d3a2d2c8c109367c53f058893377407bd89 [file] [log] [blame]
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001/*
Jonathan Basseri6465afd2015-02-25 13:05:57 -08002 * Copyright (c) 2015, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jonathan Basseri6465afd2015-02-25 13:05:57 -080017package com.android.phone;
18
Jonathan Basseri65273c82017-07-25 15:08:42 -070019import static android.service.carrier.CarrierService.ICarrierServiceWrapper.KEY_CONFIG_BUNDLE;
20import static android.service.carrier.CarrierService.ICarrierServiceWrapper.RESULT_ERROR;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080021
Jonathan Basseri11f89572015-05-05 11:57:39 -070022import android.annotation.NonNull;
Torbjorn Eklund723527a2019-02-13 11:16:25 +010023import android.annotation.Nullable;
Hunter Knepshielde601f432020-02-19 10:16:04 -080024import android.app.AppOpsManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080025import android.content.BroadcastReceiver;
26import android.content.ComponentName;
27import android.content.Context;
28import android.content.Intent;
29import android.content.IntentFilter;
30import android.content.ServiceConnection;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070031import android.content.SharedPreferences;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070032import android.content.pm.PackageInfo;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070033import android.content.pm.PackageManager;
Junda Liu43d723a2015-05-12 17:23:45 -070034import android.os.Binder;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070035import android.os.Build;
Jonathan Basseri65273c82017-07-25 15:08:42 -070036import android.os.Bundle;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080037import android.os.Handler;
38import android.os.IBinder;
Rambo Wang7e3bc122021-03-23 09:24:38 -070039import android.os.Looper;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080040import android.os.Message;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070041import android.os.PersistableBundle;
Hunter Knepshielde601f432020-02-19 10:16:04 -080042import android.os.Process;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080043import android.os.RemoteException;
Jonathan Basseri65273c82017-07-25 15:08:42 -070044import android.os.ResultReceiver;
Meng Wang97a6a462020-01-23 16:22:16 -080045import android.os.UserHandle;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070046import android.preference.PreferenceManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080047import android.service.carrier.CarrierIdentifier;
Zach Johnson36d7aab2015-05-22 15:43:00 -070048import android.service.carrier.CarrierService;
49import android.service.carrier.ICarrierService;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080050import android.telephony.CarrierConfigManager;
51import android.telephony.SubscriptionManager;
Peter Wangc035ce42020-01-08 21:00:22 -080052import android.telephony.TelephonyFrameworkInitializer;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080053import android.telephony.TelephonyManager;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -080054import android.text.TextUtils;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080055import android.util.ArraySet;
chen xudb04c292019-03-26 17:01:15 -070056import android.util.LocalLog;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080057import android.util.Log;
58
Rambo Wang7e3bc122021-03-23 09:24:38 -070059import com.android.internal.annotations.VisibleForTesting;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080060import com.android.internal.telephony.ICarrierConfigLoader;
61import com.android.internal.telephony.IccCardConstants;
62import com.android.internal.telephony.Phone;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080063import com.android.internal.telephony.PhoneFactory;
Nathan Harold48ac0972019-03-13 22:33:01 -070064import com.android.internal.telephony.SubscriptionInfoUpdater;
Jeff Davidsona8e4e242018-03-15 17:16:18 -070065import com.android.internal.telephony.TelephonyPermissions;
Meng Wanga320b942019-11-25 11:21:26 -080066import com.android.internal.telephony.util.ArrayUtils;
shuoq26a3a4c2016-12-16 11:06:48 -080067import com.android.internal.util.IndentingPrintWriter;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080068
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070069import java.io.File;
Junda Liu43d723a2015-05-12 17:23:45 -070070import java.io.FileDescriptor;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070071import java.io.FileInputStream;
72import java.io.FileNotFoundException;
73import java.io.FileOutputStream;
Jonathan Basseri1f743c92015-05-15 00:19:46 -070074import java.io.FilenameFilter;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070075import java.io.IOException;
Junda Liu43d723a2015-05-12 17:23:45 -070076import java.io.PrintWriter;
shuoq26a3a4c2016-12-16 11:06:48 -080077import java.util.ArrayList;
78import java.util.Arrays;
79import java.util.Collections;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080080import java.util.List;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080081import java.util.Set;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080082
83/**
84 * CarrierConfigLoader binds to privileged carrier apps to fetch carrier config overlays.
Jonathan Basseri6465afd2015-02-25 13:05:57 -080085 */
Jonathan Basseri6465afd2015-02-25 13:05:57 -080086public class CarrierConfigLoader extends ICarrierConfigLoader.Stub {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070087 private static final String LOG_TAG = "CarrierConfigLoader";
Meng Wang33ad2bc2017-03-16 20:21:20 -070088
89 // Package name for platform carrier config app, bundled with system image.
90 private final String mPlatformCarrierConfigPackage;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080091
92 /** The singleton instance. */
93 private static CarrierConfigLoader sInstance;
94 // The context for phone app, passed from PhoneGlobals.
95 private Context mContext;
96 // Carrier configs from default app, indexed by phoneID.
Jonathan Basseric31f1f32015-05-12 10:13:03 -070097 private PersistableBundle[] mConfigFromDefaultApp;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080098 // Carrier configs from privileged carrier config app, indexed by phoneID.
Jonathan Basseric31f1f32015-05-12 10:13:03 -070099 private PersistableBundle[] mConfigFromCarrierApp;
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100100 // Persistent Carrier configs that are provided via the override test API, indexed by phone ID.
101 private PersistableBundle[] mPersistentOverrideConfigs;
Hall Liu506724b2018-10-22 18:16:14 -0700102 // Carrier configs that are provided via the override test API, indexed by phone ID.
103 private PersistableBundle[] mOverrideConfigs;
Jayachandran C645ec612020-01-10 10:30:25 -0800104 // Carrier configs to override code default when there is no SIM inserted
105 private PersistableBundle mNoSimConfig;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800106 // Service connection for binding to config app.
Zach Johnson36d7aab2015-05-22 15:43:00 -0700107 private CarrierServiceConnection[] mServiceConnection;
Jayachandran C645ec612020-01-10 10:30:25 -0800108 // Service connection for binding to carrier config app for no SIM config.
109 private CarrierServiceConnection[] mServiceConnectionForNoSimConfig;
Jordan Liu178430d2020-02-10 14:32:15 -0800110 // Whether we are bound to a service for each phone
111 private boolean[] mServiceBound;
Jayachandran C645ec612020-01-10 10:30:25 -0800112 // Whether we are bound to a service for no SIM config
113 private boolean[] mServiceBoundForNoSimConfig;
Sarah Chin807d5c62020-03-30 17:21:09 -0700114 // Whether we have sent config change broadcast for each phone id.
Junda Liu03aad762017-07-21 13:32:17 -0700115 private boolean[] mHasSentConfigChange;
Sarah Chin807d5c62020-03-30 17:21:09 -0700116 // Whether the broadcast was sent from EVENT_SYSTEM_UNLOCKED, to track rebroadcasts
117 private boolean[] mFromSystemUnlocked;
Nathan Harold48ac0972019-03-13 22:33:01 -0700118 // SubscriptionInfoUpdater
119 private final SubscriptionInfoUpdater mSubscriptionInfoUpdater;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800120
Nanxi Chen3d670502016-03-17 16:32:09 -0700121 // Broadcast receiver for Boot intents, register intent filter in construtor.
122 private final BroadcastReceiver mBootReceiver = new ConfigLoaderBroadcastReceiver();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800123 // Broadcast receiver for SIM and pkg intents, register intent filter in constructor.
Nanxi Chen3d670502016-03-17 16:32:09 -0700124 private final BroadcastReceiver mPackageReceiver = new ConfigLoaderBroadcastReceiver();
Jack Yu67663de2019-10-17 15:19:48 -0700125 private final LocalLog mCarrierConfigLoadingLog = new LocalLog(100);
chen xudb04c292019-03-26 17:01:15 -0700126
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800127
128 // Message codes; see mHandler below.
129 // Request from SubscriptionInfoUpdater when SIM becomes absent or error.
130 private static final int EVENT_CLEAR_CONFIG = 0;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800131 // Has connected to default app.
132 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
133 // Has connected to carrier app.
134 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700135 // Config has been loaded from default app (or cache).
136 private static final int EVENT_FETCH_DEFAULT_DONE = 5;
137 // Config has been loaded from carrier app (or cache).
138 private static final int EVENT_FETCH_CARRIER_DONE = 6;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700139 // Attempt to fetch from default app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700140 private static final int EVENT_DO_FETCH_DEFAULT = 7;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700141 // Attempt to fetch from carrier app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700142 private static final int EVENT_DO_FETCH_CARRIER = 8;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700143 // A package has been installed, uninstalled, or updated.
144 private static final int EVENT_PACKAGE_CHANGED = 9;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700145 // Bind timed out for the default app.
146 private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
147 // Bind timed out for a carrier app.
148 private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700149 // Check if the system fingerprint has changed.
150 private static final int EVENT_CHECK_SYSTEM_UPDATE = 12;
Nanxi Chen3d670502016-03-17 16:32:09 -0700151 // Rerun carrier config binding after system is unlocked.
152 private static final int EVENT_SYSTEM_UNLOCKED = 13;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700153 // Fetching config timed out from the default app.
154 private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14;
155 // Fetching config timed out from a carrier app.
156 private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15;
Nathan Harold48ac0972019-03-13 22:33:01 -0700157 // SubscriptionInfoUpdater has finished updating the sub for the carrier config.
158 private static final int EVENT_SUBSCRIPTION_INFO_UPDATED = 16;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700159 // Multi-SIM config changed.
160 private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 17;
Jayachandran C645ec612020-01-10 10:30:25 -0800161 // Attempt to fetch from default app or read from XML for no SIM case.
162 private static final int EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG = 18;
163 // No SIM config has been loaded from default app (or cache).
164 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE = 19;
165 // Has connected to default app for no SIM config.
166 private static final int EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG = 20;
167 // Bind timed out for the default app when trying to fetch no SIM config.
168 private static final int EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 21;
169 // Fetching config timed out from the default app for no SIM config.
170 private static final int EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT = 22;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700171
Junda Liu6cb45002016-03-22 17:18:20 -0700172 private static final int BIND_TIMEOUT_MILLIS = 30000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800173
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800174 // Keys used for saving and restoring config bundle from file.
175 private static final String KEY_VERSION = "__carrier_config_package_version__";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800176
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100177 private static final String OVERRIDE_PACKAGE_ADDITION = "-override";
178
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700179 // SharedPreferences key for last known build fingerprint.
180 private static final String KEY_FINGERPRINT = "build_fingerprint";
181
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800182 // Argument for #dump that indicates we should also call the default and specified carrier
183 // service's #dump method. In multi-SIM devices, it's possible that carrier A is on SIM 1 and
184 // carrier B is on SIM 2, in which case we should not dump carrier B's service when carrier A
185 // requested the dump.
186 private static final String DUMP_ARG_REQUESTING_PACKAGE = "--requesting-package";
187
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800188 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700189 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700190 // For each phoneId, the event sequence should be:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700191 // fetch default, connected to default, fetch default (async), fetch default done,
192 // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700193 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700194 // If there is a saved config file for either the default app or the carrier app, we skip
195 // binding to the app and go straight from fetch to loaded.
196 //
197 // At any time, at most one connection is active. If events are not in this order, previous
198 // connection will be unbound, so only latest event takes effect.
199 //
200 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
201 // 1. loading from carrier app (even if read from a file)
202 // 2. loading from default app if there is no carrier app (even if read from a file)
203 // 3. clearing config (e.g. due to sim removal)
204 // 4. encountering bind or IPC error
Jonathan Basseri65273c82017-07-25 15:08:42 -0700205 private class ConfigHandler extends Handler {
Rambo Wang7e3bc122021-03-23 09:24:38 -0700206 ConfigHandler(@NonNull Looper looper) {
207 super(looper);
208 }
209
Jonathan Basseri65273c82017-07-25 15:08:42 -0700210 @Override
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800211 public void handleMessage(Message msg) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700212 final int phoneId = msg.arg1;
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800213 logdWithLocalLog("mHandler: " + msg.what + " phoneId: " + phoneId);
Malcolm Chen5ea18532019-10-24 19:55:44 -0700214 if (!SubscriptionManager.isValidPhoneId(phoneId)
215 && msg.what != EVENT_MULTI_SIM_CONFIG_CHANGED) {
216 return;
217 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800218 switch (msg.what) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800219 case EVENT_CLEAR_CONFIG: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700220 clearConfigForPhone(phoneId, true);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800221 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700222 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700223
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800224 case EVENT_SYSTEM_UNLOCKED: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700225 for (int i = 0; i < TelephonyManager.from(mContext).getActiveModemCount();
226 ++i) {
Sarah Chin807d5c62020-03-30 17:21:09 -0700227 // When the user unlocks the device, send the broadcast again (with a
228 // rebroadcast extra) if we have sent it before unlock. This will avoid
229 // trying to load the carrier config when the SIM is still loading when the
230 // unlock happens.
Junda Liu03aad762017-07-21 13:32:17 -0700231 if (mHasSentConfigChange[i]) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800232 logdWithLocalLog("System unlocked");
Sarah Chin807d5c62020-03-30 17:21:09 -0700233 mFromSystemUnlocked[i] = true;
Junda Liu03aad762017-07-21 13:32:17 -0700234 updateConfigForPhoneId(i);
235 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700236 }
237 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700238 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700239
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800240 case EVENT_PACKAGE_CHANGED: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700241 final String carrierPackageName = (String) msg.obj;
242 // Only update if there are cached config removed to avoid updating config for
243 // unrelated packages.
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700244 if (clearCachedConfigForPackage(carrierPackageName)) {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700245 int numPhones = TelephonyManager.from(mContext).getActiveModemCount();
Junda Liu8a8a53a2015-05-15 16:19:57 -0700246 for (int i = 0; i < numPhones; ++i) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800247 logdWithLocalLog("Package changed: " + carrierPackageName
Jack Yu67663de2019-10-17 15:19:48 -0700248 + ", phone=" + i);
Junda Liu8a8a53a2015-05-15 16:19:57 -0700249 updateConfigForPhoneId(i);
250 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700251 }
252 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700253 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700254
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800255 case EVENT_DO_FETCH_DEFAULT: {
Rambo Wang9bc7d362021-03-09 09:10:58 -0800256 // Clear in-memory cache for carrier app config, so when carrier app gets
257 // uninstalled, no stale config is left.
258 if (mConfigFromCarrierApp[phoneId] != null
259 && getCarrierPackageForPhoneId(phoneId) == null) {
260 mConfigFromCarrierApp[phoneId] = null;
261 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100262 // Restore persistent override values.
263 PersistableBundle config = restoreConfigFromXml(
264 mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId);
265 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800266 logd("Loaded persistent override config from XML. package="
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100267 + mPlatformCarrierConfigPackage
268 + " phoneId=" + phoneId);
269 mPersistentOverrideConfigs[phoneId] = config;
270 }
271
272 config = restoreConfigFromXml(mPlatformCarrierConfigPackage, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700273 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800274 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700275 "Loaded config from XML. package="
276 + mPlatformCarrierConfigPackage
277 + " phoneId="
278 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700279 mConfigFromDefaultApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700280 Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700281 newMsg.getData().putBoolean("loaded_from_xml", true);
282 mHandler.sendMessage(newMsg);
283 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700284 // No cached config, so fetch it from the default app.
285 if (bindToConfigPackage(
286 mPlatformCarrierConfigPackage,
287 phoneId,
288 EVENT_CONNECTED_TO_DEFAULT)) {
289 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800290 obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1 /*arg2*/,
291 getMessageToken(phoneId)),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700292 BIND_TIMEOUT_MILLIS);
293 } else {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800294 // Put a stub bundle in place so that the rest of the logic continues
295 // smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000296 mConfigFromDefaultApp[phoneId] = new PersistableBundle();
Jonathan Basseri65273c82017-07-25 15:08:42 -0700297 // Send broadcast if bind fails.
Nathan Harold48ac0972019-03-13 22:33:01 -0700298 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700299 // TODO: We *must* call unbindService even if bindService returns false.
300 // (And possibly if SecurityException was thrown.)
chen xudb04c292019-03-26 17:01:15 -0700301 loge("binding to default app: "
302 + mPlatformCarrierConfigPackage + " fails");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700303 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800304 }
305 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700306 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800307
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800308 case EVENT_CONNECTED_TO_DEFAULT: {
Rambo Wang610fdf62021-03-08 21:37:11 -0800309 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT, getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700310 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800311 // If new service connection has been created, unbind.
312 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Jordan Liu178430d2020-02-10 14:32:15 -0800313 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800314 break;
315 }
chen xu02581692018-11-11 19:03:44 -0800316 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700317 // ResultReceiver callback will execute in this Handler's thread.
318 final ResultReceiver resultReceiver =
319 new ResultReceiver(this) {
320 @Override
321 public void onReceiveResult(int resultCode, Bundle resultData) {
Jordan Liu178430d2020-02-10 14:32:15 -0800322 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700323 // If new service connection has been created, this is stale.
324 if (mServiceConnection[phoneId] != conn) {
325 loge("Received response for stale request.");
326 return;
327 }
Rambo Wang610fdf62021-03-08 21:37:11 -0800328 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT,
329 getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700330 if (resultCode == RESULT_ERROR || resultData == null) {
331 // On error, abort config fetching.
332 loge("Failed to get carrier config");
Nathan Harold48ac0972019-03-13 22:33:01 -0700333 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700334 return;
335 }
336 PersistableBundle config =
337 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100338 saveConfigToXml(mPlatformCarrierConfigPackage, "", phoneId,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800339 carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700340 mConfigFromDefaultApp[phoneId] = config;
341 sendMessage(
342 obtainMessage(
343 EVENT_FETCH_DEFAULT_DONE, phoneId, -1));
344 }
345 };
346 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800347 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700348 ICarrierService carrierService =
349 ICarrierService.Stub.asInterface(conn.service);
350 carrierService.getCarrierConfig(carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800351 logdWithLocalLog("Fetch config for default app: "
chen xudb04c292019-03-26 17:01:15 -0700352 + mPlatformCarrierConfigPackage
353 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700354 } catch (RemoteException e) {
chen xudb04c292019-03-26 17:01:15 -0700355 loge("Failed to get carrier config from default app: " +
356 mPlatformCarrierConfigPackage + " err: " + e.toString());
Jordan Liu178430d2020-02-10 14:32:15 -0800357 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700358 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800359 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700360 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800361 obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1 /*arg2*/,
362 getMessageToken(phoneId)),
Jonathan Basseri65273c82017-07-25 15:08:42 -0700363 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800364 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700365 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800366
Jonathan Basseri930701e2015-06-11 20:56:43 -0700367 case EVENT_BIND_DEFAULT_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800368 case EVENT_FETCH_DEFAULT_TIMEOUT: {
369 loge("Bind/fetch time out from " + mPlatformCarrierConfigPackage);
Rambo Wang610fdf62021-03-08 21:37:11 -0800370 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT, getMessageToken(phoneId));
chen xuf60b3162019-05-01 21:31:05 -0700371 // If we attempted to bind to the app, but the service connection is null due to
372 // the race condition that clear config event happens before bind/fetch complete
373 // then config was cleared while we were waiting and we should not continue.
374 if (mServiceConnection[phoneId] != null) {
375 // If a ResponseReceiver callback is in the queue when this happens, we will
376 // unbind twice and throw an exception.
Jordan Liu178430d2020-02-10 14:32:15 -0800377 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700378 broadcastConfigChangedIntent(phoneId);
379 }
Nathan Harold1122e3d2021-01-22 15:45:24 -0800380 // Put a stub bundle in place so that the rest of the logic continues smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000381 mConfigFromDefaultApp[phoneId] = new PersistableBundle();
Nathan Harold48ac0972019-03-13 22:33:01 -0700382 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700383 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700384 }
Jonathan Basseri930701e2015-06-11 20:56:43 -0700385
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800386 case EVENT_FETCH_DEFAULT_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700387 // If we attempted to bind to the app, but the service connection is null, then
388 // config was cleared while we were waiting and we should not continue.
389 if (!msg.getData().getBoolean("loaded_from_xml", false)
390 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800391 break;
392 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700393 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700394 if (carrierPackageName != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800395 logd("Found carrier config app: " + carrierPackageName);
Jordan Liu38a614c2019-03-20 15:01:17 -0700396 sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId, -1));
Jonathan Basserib919e932015-05-27 16:53:08 +0000397 } else {
Nathan Harold48ac0972019-03-13 22:33:01 -0700398 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700399 }
400 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700401 }
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700402
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800403 case EVENT_DO_FETCH_CARRIER: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700404 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700405 final PersistableBundle config =
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100406 restoreConfigFromXml(carrierPackageName, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700407 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800408 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700409 "Loaded config from XML. package="
410 + carrierPackageName
411 + " phoneId="
412 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700413 mConfigFromCarrierApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700414 Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700415 newMsg.getData().putBoolean("loaded_from_xml", true);
416 sendMessage(newMsg);
417 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700418 // No cached config, so fetch it from a carrier app.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800419 if (carrierPackageName != null && bindToConfigPackage(carrierPackageName,
420 phoneId, EVENT_CONNECTED_TO_CARRIER)) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700421 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800422 obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1 /*arg2*/,
423 getMessageToken(phoneId)),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700424 BIND_TIMEOUT_MILLIS);
425 } else {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800426 // Put a stub bundle in place so that the rest of the logic continues
427 // smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000428 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
Jonathan Basseri65273c82017-07-25 15:08:42 -0700429 // Send broadcast if bind fails.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700430 broadcastConfigChangedIntent(phoneId);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800431 loge("Bind to carrier app: " + carrierPackageName + " fails");
Nathan Harold48ac0972019-03-13 22:33:01 -0700432 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700433 }
434 }
435 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700436 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700437
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800438 case EVENT_CONNECTED_TO_CARRIER: {
Rambo Wang610fdf62021-03-08 21:37:11 -0800439 removeMessages(EVENT_BIND_CARRIER_TIMEOUT, getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700440 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800441 // If new service connection has been created, unbind.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700442 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Jordan Liu178430d2020-02-10 14:32:15 -0800443 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800444 break;
445 }
chen xu02581692018-11-11 19:03:44 -0800446 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700447 // ResultReceiver callback will execute in this Handler's thread.
448 final ResultReceiver resultReceiver =
449 new ResultReceiver(this) {
450 @Override
451 public void onReceiveResult(int resultCode, Bundle resultData) {
Jordan Liu178430d2020-02-10 14:32:15 -0800452 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700453 // If new service connection has been created, this is stale.
454 if (mServiceConnection[phoneId] != conn) {
455 loge("Received response for stale request.");
456 return;
457 }
Rambo Wang610fdf62021-03-08 21:37:11 -0800458 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT,
459 getMessageToken(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700460 if (resultCode == RESULT_ERROR || resultData == null) {
461 // On error, abort config fetching.
chen xudb04c292019-03-26 17:01:15 -0700462 loge("Failed to get carrier config from carrier app: "
463 + getCarrierPackageForPhoneId(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700464 broadcastConfigChangedIntent(phoneId);
Nathan Harold48ac0972019-03-13 22:33:01 -0700465 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700466 return;
467 }
468 PersistableBundle config =
469 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100470 saveConfigToXml(getCarrierPackageForPhoneId(phoneId), "",
471 phoneId, carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700472 mConfigFromCarrierApp[phoneId] = config;
473 sendMessage(
474 obtainMessage(
475 EVENT_FETCH_CARRIER_DONE, phoneId, -1));
476 }
477 };
478 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800479 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700480 ICarrierService carrierService =
481 ICarrierService.Stub.asInterface(conn.service);
482 carrierService.getCarrierConfig(carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800483 logdWithLocalLog("Fetch config for carrier app: "
chen xudb04c292019-03-26 17:01:15 -0700484 + getCarrierPackageForPhoneId(phoneId)
485 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700486 } catch (RemoteException e) {
487 loge("Failed to get carrier config: " + e.toString());
Jordan Liu178430d2020-02-10 14:32:15 -0800488 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700489 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800490 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700491 sendMessageDelayed(
Rambo Wang610fdf62021-03-08 21:37:11 -0800492 obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1 /*arg2*/,
493 getMessageToken(phoneId)),
Jonathan Basseri65273c82017-07-25 15:08:42 -0700494 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800495 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700496 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800497
Jonathan Basseri930701e2015-06-11 20:56:43 -0700498 case EVENT_BIND_CARRIER_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800499 case EVENT_FETCH_CARRIER_TIMEOUT: {
Nathan Harold1122e3d2021-01-22 15:45:24 -0800500 loge("Bind/fetch from carrier app timeout, package="
501 + getCarrierPackageForPhoneId(phoneId));
Rambo Wang610fdf62021-03-08 21:37:11 -0800502 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT, getMessageToken(phoneId));
chen xuf60b3162019-05-01 21:31:05 -0700503 // If we attempted to bind to the app, but the service connection is null due to
504 // the race condition that clear config event happens before bind/fetch complete
505 // then config was cleared while we were waiting and we should not continue.
506 if (mServiceConnection[phoneId] != null) {
507 // If a ResponseReceiver callback is in the queue when this happens, we will
508 // unbind twice and throw an exception.
Jordan Liu178430d2020-02-10 14:32:15 -0800509 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700510 broadcastConfigChangedIntent(phoneId);
511 }
Nathan Harold1122e3d2021-01-22 15:45:24 -0800512 // Put a stub bundle in place so that the rest of the logic continues smoothly.
Rambo Wang42026112021-04-07 20:57:28 +0000513 mConfigFromCarrierApp[phoneId] = new PersistableBundle();
Nathan Harold48ac0972019-03-13 22:33:01 -0700514 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700515 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700516 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800517 case EVENT_FETCH_CARRIER_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700518 // If we attempted to bind to the app, but the service connection is null, then
519 // config was cleared while we were waiting and we should not continue.
520 if (!msg.getData().getBoolean("loaded_from_xml", false)
521 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800522 break;
523 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700524 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800525 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700526 }
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700527
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800528 case EVENT_CHECK_SYSTEM_UPDATE: {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700529 SharedPreferences sharedPrefs =
530 PreferenceManager.getDefaultSharedPreferences(mContext);
531 final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
532 if (!Build.FINGERPRINT.equals(lastFingerprint)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800533 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700534 "Build fingerprint changed. old: "
535 + lastFingerprint
536 + " new: "
537 + Build.FINGERPRINT);
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700538 clearCachedConfigForPackage(null);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700539 sharedPrefs
540 .edit()
541 .putString(KEY_FINGERPRINT, Build.FINGERPRINT)
542 .apply();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700543 }
544 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700545 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700546
547 case EVENT_SUBSCRIPTION_INFO_UPDATED:
548 broadcastConfigChangedIntent(phoneId);
549 break;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700550 case EVENT_MULTI_SIM_CONFIG_CHANGED:
551 onMultiSimConfigChanged();
552 break;
Jayachandran C645ec612020-01-10 10:30:25 -0800553
554 case EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG: {
555 PersistableBundle config =
556 restoreNoSimConfigFromXml(mPlatformCarrierConfigPackage);
557
558 if (config != null) {
559 logd("Loaded no SIM config from XML. package="
560 + mPlatformCarrierConfigPackage);
561 mNoSimConfig = config;
562 sendMessage(
563 obtainMessage(
564 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE,
565 phoneId, -1));
566 } else {
567 // No cached config, so fetch it from the default app.
568 if (bindToConfigPackage(
569 mPlatformCarrierConfigPackage,
570 phoneId,
571 EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG)) {
572 sendMessageDelayed(
573 obtainMessage(
574 EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT,
575 phoneId, -1), BIND_TIMEOUT_MILLIS);
576 } else {
577 broadcastConfigChangedIntent(phoneId, false);
578 // TODO: We *must* call unbindService even if bindService returns false.
579 // (And possibly if SecurityException was thrown.)
580 loge("binding to default app to fetch no SIM config: "
581 + mPlatformCarrierConfigPackage + " fails");
582 }
583 }
584 break;
585 }
586
587 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE: {
588 broadcastConfigChangedIntent(phoneId, false);
589 break;
590 }
591
592 case EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT:
593 case EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT: {
594 loge("Bind/fetch time out for no SIM config from "
595 + mPlatformCarrierConfigPackage);
596 removeMessages(EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
597 // If we attempted to bind to the app, but the service connection is null due to
598 // the race condition that clear config event happens before bind/fetch complete
599 // then config was cleared while we were waiting and we should not continue.
600 if (mServiceConnectionForNoSimConfig[phoneId] != null) {
601 // If a ResponseReceiver callback is in the queue when this happens, we will
602 // unbind twice and throw an exception.
603 unbindIfBoundForNoSimConfig(mContext,
604 mServiceConnectionForNoSimConfig[phoneId], phoneId);
605 }
606 broadcastConfigChangedIntent(phoneId, false);
607 break;
608 }
609
610 case EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG: {
611 removeMessages(EVENT_BIND_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
612 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
613 // If new service connection has been created, unbind.
614 if (mServiceConnectionForNoSimConfig[phoneId] != conn || conn.service == null) {
615 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
616 break;
617 }
618
619 // ResultReceiver callback will execute in this Handler's thread.
620 final ResultReceiver resultReceiver =
621 new ResultReceiver(this) {
622 @Override
623 public void onReceiveResult(int resultCode, Bundle resultData) {
624 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
625 // If new service connection has been created, this is stale.
626 if (mServiceConnectionForNoSimConfig[phoneId] != conn) {
627 loge("Received response for stale request.");
628 return;
629 }
630 removeMessages(EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT);
631 if (resultCode == RESULT_ERROR || resultData == null) {
632 // On error, abort config fetching.
633 loge("Failed to get no SIM carrier config");
634 return;
635 }
636 PersistableBundle config =
637 resultData.getParcelable(KEY_CONFIG_BUNDLE);
638 saveNoSimConfigToXml(mPlatformCarrierConfigPackage, config);
639 mNoSimConfig = config;
640 sendMessage(
641 obtainMessage(
642 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_DONE,
643 phoneId, -1));
644 }
645 };
646 // Now fetch the config asynchronously from the ICarrierService.
647 try {
648 ICarrierService carrierService =
649 ICarrierService.Stub.asInterface(conn.service);
650 carrierService.getCarrierConfig(null, resultReceiver);
651 logdWithLocalLog("Fetch no sim config from default app: "
652 + mPlatformCarrierConfigPackage);
653 } catch (RemoteException e) {
654 loge("Failed to get no sim carrier config from default app: " +
655 mPlatformCarrierConfigPackage + " err: " + e.toString());
656 unbindIfBoundForNoSimConfig(mContext, conn, phoneId);
657 break; // So we don't set a timeout.
658 }
659 sendMessageDelayed(
660 obtainMessage(
661 EVENT_FETCH_DEFAULT_FOR_NO_SIM_CONFIG_TIMEOUT,
662 phoneId, -1), BIND_TIMEOUT_MILLIS);
663 break;
664 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800665 }
666 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700667 }
668
669 private final Handler mHandler;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800670
671 /**
672 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
673 * receiver for relevant events.
674 */
Rambo Wang7e3bc122021-03-23 09:24:38 -0700675 @VisibleForTesting
676 /* package */ CarrierConfigLoader(Context context,
677 SubscriptionInfoUpdater subscriptionInfoUpdater, @NonNull Looper looper) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800678 mContext = context;
Meng Wang33ad2bc2017-03-16 20:21:20 -0700679 mPlatformCarrierConfigPackage =
680 mContext.getString(R.string.platform_carrier_config_package);
Rambo Wang7e3bc122021-03-23 09:24:38 -0700681 mHandler = new ConfigHandler(looper);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800682
Nanxi Chen3d670502016-03-17 16:32:09 -0700683 IntentFilter bootFilter = new IntentFilter();
684 bootFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
685 context.registerReceiver(mBootReceiver, bootFilter);
686
Junda Liu8a8a53a2015-05-15 16:19:57 -0700687 // Register for package updates. Update app or uninstall app update will have all 3 intents,
688 // in the order or removed, added, replaced, all with extra_replace set to true.
689 IntentFilter pkgFilter = new IntentFilter();
690 pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
691 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
692 pkgFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
693 pkgFilter.addDataScheme("package");
Jordan Liu5ca24762019-07-10 12:51:09 -0700694 context.registerReceiver(mPackageReceiver, pkgFilter);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800695
Malcolm Chen978dda52019-10-08 18:15:25 -0700696 int numPhones = TelephonyManager.from(context).getSupportedModemCount();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700697 mConfigFromDefaultApp = new PersistableBundle[numPhones];
698 mConfigFromCarrierApp = new PersistableBundle[numPhones];
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100699 mPersistentOverrideConfigs = new PersistableBundle[numPhones];
Hall Liu506724b2018-10-22 18:16:14 -0700700 mOverrideConfigs = new PersistableBundle[numPhones];
Rambo Wang42026112021-04-07 20:57:28 +0000701 mNoSimConfig = new PersistableBundle();
Zach Johnson36d7aab2015-05-22 15:43:00 -0700702 mServiceConnection = new CarrierServiceConnection[numPhones];
Jordan Liu178430d2020-02-10 14:32:15 -0800703 mServiceBound = new boolean[numPhones];
Junda Liu03aad762017-07-21 13:32:17 -0700704 mHasSentConfigChange = new boolean[numPhones];
Sarah Chin807d5c62020-03-30 17:21:09 -0700705 mFromSystemUnlocked = new boolean[numPhones];
Jayachandran C645ec612020-01-10 10:30:25 -0800706 mServiceConnectionForNoSimConfig = new CarrierServiceConnection[numPhones];
707 mServiceBoundForNoSimConfig = new boolean[numPhones];
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800708 // Make this service available through ServiceManager.
Peter Wangc035ce42020-01-08 21:00:22 -0800709 TelephonyFrameworkInitializer
710 .getTelephonyServiceManager().getCarrierConfigServiceRegisterer().register(this);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800711 logd("CarrierConfigLoader has started");
Rambo Wang7e3bc122021-03-23 09:24:38 -0700712 mSubscriptionInfoUpdater = subscriptionInfoUpdater;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700713 mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800714 }
715
716 /**
717 * Initialize the singleton CarrierConfigLoader instance.
718 *
719 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
720 */
Rambo Wang7e3bc122021-03-23 09:24:38 -0700721 /* package */ static CarrierConfigLoader init(Context context) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800722 synchronized (CarrierConfigLoader.class) {
723 if (sInstance == null) {
Rambo Wang7e3bc122021-03-23 09:24:38 -0700724 sInstance = new CarrierConfigLoader(context,
725 PhoneFactory.getSubscriptionInfoUpdater(), Looper.myLooper());
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800726 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700727 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800728 }
729 return sInstance;
730 }
731 }
732
Rambo Wang7e3bc122021-03-23 09:24:38 -0700733 @VisibleForTesting
734 /* package */ void clearConfigForPhone(int phoneId, boolean fetchNoSimConfig) {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700735 /* Ignore clear configuration request if device is being shutdown. */
736 Phone phone = PhoneFactory.getPhone(phoneId);
737 if (phone != null) {
738 if (phone.isShuttingDown()) {
739 return;
740 }
741 }
742
743 mConfigFromDefaultApp[phoneId] = null;
744 mConfigFromCarrierApp[phoneId] = null;
745 mServiceConnection[phoneId] = null;
746 mHasSentConfigChange[phoneId] = false;
747
Jayachandran C645ec612020-01-10 10:30:25 -0800748 if (fetchNoSimConfig) {
749 // To fetch no SIM config
750 mHandler.sendMessage(
751 mHandler.obtainMessage(
752 EVENT_DO_FETCH_DEFAULT_FOR_NO_SIM_CONFIG, phoneId, -1));
753 }
Malcolm Chen5ea18532019-10-24 19:55:44 -0700754 }
755
Nathan Harold48ac0972019-03-13 22:33:01 -0700756 private void notifySubscriptionInfoUpdater(int phoneId) {
757 String configPackagename;
758 PersistableBundle configToSend;
759 int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
760 // Prefer the carrier privileged carrier app, but if there is not one, use the platform
761 // default carrier app.
762 if (mConfigFromCarrierApp[phoneId] != null) {
763 configPackagename = getCarrierPackageForPhoneId(phoneId);
764 configToSend = mConfigFromCarrierApp[phoneId];
765 } else {
766 configPackagename = mPlatformCarrierConfigPackage;
767 configToSend = mConfigFromDefaultApp[phoneId];
768 }
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700769
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800770 if (configToSend == null) {
Rambo Wang42026112021-04-07 20:57:28 +0000771 configToSend = new PersistableBundle();
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800772 }
773
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700774 // mOverrideConfigs is for testing. And it will override current configs.
775 PersistableBundle config = mOverrideConfigs[phoneId];
776 if (config != null) {
Torbjorn Eklund1050cb02018-11-16 14:05:38 +0100777 configToSend = new PersistableBundle(configToSend);
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700778 configToSend.putAll(config);
779 }
780
Nathan Harold48ac0972019-03-13 22:33:01 -0700781 mSubscriptionInfoUpdater.updateSubscriptionByCarrierConfigAndNotifyComplete(
782 phoneId, configPackagename, configToSend,
783 mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1));
784 }
785
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800786 private void broadcastConfigChangedIntent(int phoneId) {
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800787 broadcastConfigChangedIntent(phoneId, true);
788 }
789
790 private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800791 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800792 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
chen xuf9db49f2019-03-31 23:04:42 -0700793 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800794 if (addSubIdExtra) {
795 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
796 int[] subIds = SubscriptionManager.getSubId(phoneId);
797 if (!ArrayUtils.isEmpty(subIds)) {
798 TelephonyManager telMgr = TelephonyManager.from(mContext)
799 .createForSubscriptionId(subIds[0]);
800 simApplicationState = telMgr.getSimApplicationState();
801 }
Malcolm Chenf7635ec2020-04-15 15:45:00 -0700802 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId
803 + " simApplicationState " + simApplicationState);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800804 // Include subId/carrier id extra only if SIM records are loaded
805 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
806 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
807 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
808 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID,
809 getSpecificCarrierIdForPhoneId(phoneId));
810 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, getCarrierIdForPhoneId(phoneId));
811 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800812 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800813 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Sarah Chin807d5c62020-03-30 17:21:09 -0700814 intent.putExtra(CarrierConfigManager.EXTRA_REBROADCAST_ON_UNLOCK,
815 mFromSystemUnlocked[phoneId]);
Meng Wang97a6a462020-01-23 16:22:16 -0800816 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
Jack Yue9e95be2020-03-22 10:56:06 -0700817 int[] subIds = SubscriptionManager.getSubId(phoneId);
818 if (subIds != null && subIds.length > 0) {
819 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId + ", subId=" + subIds[0]);
820 } else {
821 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
822 }
Junda Liu03aad762017-07-21 13:32:17 -0700823 mHasSentConfigChange[phoneId] = true;
Sarah Chin807d5c62020-03-30 17:21:09 -0700824 mFromSystemUnlocked[phoneId] = false;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800825 }
826
827 /** Binds to the default or carrier config app. */
828 private boolean bindToConfigPackage(String pkgName, int phoneId, int eventId) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800829 logdWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700830 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700831 carrierService.setPackage(pkgName);
Jayachandran C645ec612020-01-10 10:30:25 -0800832 CarrierServiceConnection serviceConnection = new CarrierServiceConnection(
833 phoneId, pkgName, eventId);
834 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
835 mServiceConnectionForNoSimConfig[phoneId] = serviceConnection;
836 } else {
837 mServiceConnection[phoneId] = serviceConnection;
838 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800839 try {
Jayachandran C645ec612020-01-10 10:30:25 -0800840 if (mContext.bindService(carrierService, serviceConnection,
Jordan Liu178430d2020-02-10 14:32:15 -0800841 Context.BIND_AUTO_CREATE)) {
Jayachandran C645ec612020-01-10 10:30:25 -0800842 if (eventId == EVENT_CONNECTED_TO_DEFAULT_FOR_NO_SIM_CONFIG) {
843 mServiceBoundForNoSimConfig[phoneId] = true;
844 } else {
845 mServiceBound[phoneId] = true;
846 }
Jordan Liu178430d2020-02-10 14:32:15 -0800847 return true;
848 } else {
849 return false;
850 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800851 } catch (SecurityException ex) {
852 return false;
853 }
854 }
855
Rambo Wang7e3bc122021-03-23 09:24:38 -0700856 @VisibleForTesting
857 /* package */ CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800858 String mcc = "";
859 String mnc = "";
860 String imsi = "";
861 String gid1 = "";
862 String gid2 = "";
863 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
864 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800865 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800866 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700867 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
868 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800869 mcc = simOperator.substring(0, 3);
870 mnc = simOperator.substring(3);
871 }
872 Phone phone = PhoneFactory.getPhone(phoneId);
873 if (phone != null) {
874 imsi = phone.getSubscriberId();
875 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700876 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800877 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800878 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800879 }
chen xua31f22b2019-03-06 15:28:50 -0800880 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800881 }
882
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700883 /** Returns the package name of a priveleged carrier app, or null if there is none. */
Nathan Harold1122e3d2021-01-22 15:45:24 -0800884 @Nullable
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700885 private String getCarrierPackageForPhoneId(int phoneId) {
886 List<String> carrierPackageNames = TelephonyManager.from(mContext)
887 .getCarrierPackageNamesForIntentAndPhone(
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700888 new Intent(CarrierService.CARRIER_SERVICE_INTERFACE), phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700889 if (carrierPackageNames != null && carrierPackageNames.size() > 0) {
890 return carrierPackageNames.get(0);
891 } else {
892 return null;
893 }
894 }
895
896 private String getIccIdForPhoneId(int phoneId) {
897 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
898 return null;
899 }
900 Phone phone = PhoneFactory.getPhone(phoneId);
901 if (phone == null) {
902 return null;
903 }
904 return phone.getIccSerialNumber();
905 }
906
chen xu02581692018-11-11 19:03:44 -0800907 /**
chen xua31f22b2019-03-06 15:28:50 -0800908 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800909 */
chen xua31f22b2019-03-06 15:28:50 -0800910 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700911 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800912 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700913 }
914 Phone phone = PhoneFactory.getPhone(phoneId);
915 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800916 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700917 }
chen xua31f22b2019-03-06 15:28:50 -0800918 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800919 }
920
921 /**
922 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
923 */
924 private int getCarrierIdForPhoneId(int phoneId) {
925 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
926 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700927 }
chen xu02581692018-11-11 19:03:44 -0800928 Phone phone = PhoneFactory.getPhone(phoneId);
929 if (phone == null) {
930 return TelephonyManager.UNKNOWN_CARRIER_ID;
931 }
932 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700933 }
934
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700935 /**
936 * Writes a bundle to an XML file.
937 *
chen xu02581692018-11-11 19:03:44 -0800938 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -0800939 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -0800940 * should have a single copy of XML file named after carrier id. However, it's still possible
941 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
942 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
943 * carrier while iccid remains the same.
944 *
945 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
946 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700947 *
948 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700949 *
Jayachandran C645ec612020-01-10 10:30:25 -0800950 * @param packageName the name of the package from which we fetched this bundle.
951 * @param extraString An extra string to be used in the XML file name.
952 * @param phoneId the phone ID.
953 * @param carrierId contains all carrier-identifying information.
954 * @param config the bundle to be written. Null will be treated as an empty bundle.
955 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700956 */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100957 private void saveConfigToXml(String packageName, @NonNull String extraString, int phoneId,
Jayachandran C645ec612020-01-10 10:30:25 -0800958 CarrierIdentifier carrierId, PersistableBundle config, boolean isNoSimConfig) {
959 if (packageName == null) {
960 loge("Cannot save config with null packageName");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800961 return;
962 }
963
Jayachandran C645ec612020-01-10 10:30:25 -0800964 String fileName;
965 if (isNoSimConfig) {
966 fileName = getFilenameForNoSimConfig(packageName);
967 } else {
968 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
969 != TelephonyManager.SIM_STATE_LOADED) {
970 loge("Skip save config because SIM records are not loaded.");
971 return;
972 }
973
974 final String iccid = getIccIdForPhoneId(phoneId);
975 final int cid = carrierId.getSpecificCarrierId();
976 if (iccid == null) {
977 loge("Cannot save config with null iccid.");
978 return;
979 }
980 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700981 }
Jayachandran C645ec612020-01-10 10:30:25 -0800982
Junda Liu02596502016-11-15 13:46:43 -0800983 // b/32668103 Only save to file if config isn't empty.
984 // In case of failure, not caching an empty bundle will
985 // try loading config again on next power on or sim loaded.
986 // Downside is for genuinely empty bundle, will bind and load
987 // on every power on.
988 if (config == null || config.isEmpty()) {
989 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700990 }
991
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700992 final String version = getPackageVersion(packageName);
993 if (version == null) {
994 loge("Failed to get package version for: " + packageName);
995 return;
996 }
997
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800998 logdWithLocalLog(
999 "Save config to xml, packagename: " + packageName + " phoneId: " + phoneId);
chen xudb04c292019-03-26 17:01:15 -07001000
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001001 FileOutputStream outFile = null;
1002 try {
Jayachandran C645ec612020-01-10 10:30:25 -08001003 outFile = new FileOutputStream(new File(mContext.getFilesDir(), fileName));
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001004 config.putString(KEY_VERSION, version);
1005 config.writeToStream(outFile);
1006 outFile.flush();
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001007 outFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001008 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001009 loge(e.toString());
1010 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001011 }
1012
Rambo Wang7e3bc122021-03-23 09:24:38 -07001013 @VisibleForTesting
1014 /* package */ void saveConfigToXml(String packageName, @NonNull String extraString, int phoneId,
Jayachandran C645ec612020-01-10 10:30:25 -08001015 CarrierIdentifier carrierId, PersistableBundle config) {
1016 saveConfigToXml(packageName, extraString, phoneId, carrierId, config, false);
1017 }
1018
Rambo Wang7e3bc122021-03-23 09:24:38 -07001019 @VisibleForTesting
1020 /* package */ void saveNoSimConfigToXml(String packageName, PersistableBundle config) {
Jayachandran C645ec612020-01-10 10:30:25 -08001021 saveConfigToXml(packageName, "", -1, null, config, true);
1022 }
1023
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001024 /**
1025 * Reads a bundle from an XML file.
1026 *
1027 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +08001028 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001029 *
1030 * In case of errors, or if the saved config is from a different package version than the
1031 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001032 *
Jayachandran C645ec612020-01-10 10:30:25 -08001033 * @param packageName the name of the package from which we fetched this bundle.
1034 * @param extraString An extra string to be used in the XML file name.
1035 * @param phoneId the phone ID.
1036 * @param isNoSimConfig whether this is invoked for noSimConfig or not.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001037 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001038 * version does not match, or reading config fails.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001039 */
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001040 private PersistableBundle restoreConfigFromXml(String packageName, @NonNull String extraString,
Jayachandran C645ec612020-01-10 10:30:25 -08001041 int phoneId, boolean isNoSimConfig) {
1042 if (packageName == null) {
1043 loge("Cannot restore config with null packageName");
1044 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001045 final String version = getPackageVersion(packageName);
1046 if (version == null) {
1047 loge("Failed to get package version for: " + packageName);
1048 return null;
1049 }
Yuchen Dongc15afed2018-04-26 17:05:22 +08001050
Jayachandran C645ec612020-01-10 10:30:25 -08001051 String fileName;
1052 if (isNoSimConfig) {
1053 fileName = getFilenameForNoSimConfig(packageName);
1054 } else {
1055 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
1056 != TelephonyManager.SIM_STATE_LOADED) {
1057 loge("Skip restore config because SIM records are not loaded.");
1058 return null;
1059 }
1060
1061 final String iccid = getIccIdForPhoneId(phoneId);
1062 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1063 if (iccid == null) {
1064 loge("Cannot restore config with null iccid.");
1065 return null;
1066 }
1067 fileName = getFilenameForConfig(packageName, extraString, iccid, cid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001068 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001069
1070 PersistableBundle restoredBundle = null;
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001071 File file = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001072 FileInputStream inFile = null;
1073 try {
Jayachandran C645ec612020-01-10 10:30:25 -08001074 file = new File(mContext.getFilesDir(),fileName);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001075 inFile = new FileInputStream(file);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001076
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001077 restoredBundle = PersistableBundle.readFromStream(inFile);
1078 String savedVersion = restoredBundle.getString(KEY_VERSION);
1079 restoredBundle.remove(KEY_VERSION);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001080
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001081 if (!version.equals(savedVersion)) {
1082 loge("Saved version mismatch: " + version + " vs " + savedVersion);
1083 restoredBundle = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001084 }
Meng Wang0f6b2ea2020-01-06 18:31:16 -08001085
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001086 inFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001087 } catch (FileNotFoundException e) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001088 // Missing file is normal occurrence that might occur with a new sim or when restoring
1089 // an override file during boot and should not be treated as an error.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001090 if (file != null) logd("File not found: " + file.getPath());
1091 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001092 loge(e.toString());
1093 }
1094
1095 return restoredBundle;
1096 }
1097
Jayachandran C645ec612020-01-10 10:30:25 -08001098 private PersistableBundle restoreConfigFromXml(String packageName, @NonNull String extraString,
1099 int phoneId) {
1100 return restoreConfigFromXml(packageName, extraString, phoneId, false);
1101 }
1102
1103 private PersistableBundle restoreNoSimConfigFromXml(String packageName) {
1104 return restoreConfigFromXml(packageName, "", -1, true);
1105 }
1106
Junda Liu8a8a53a2015-05-15 16:19:57 -07001107 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001108 * Clears cached carrier config.
1109 * This deletes all saved XML files associated with the given package name. If packageName is
1110 * null, then it deletes all saved XML files.
1111 *
1112 * @param packageName the name of a carrier package, or null if all cached config should be
1113 * cleared.
1114 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -07001115 */
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001116 private boolean clearCachedConfigForPackage(final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001117 File dir = mContext.getFilesDir();
1118 File[] packageFiles = dir.listFiles(new FilenameFilter() {
1119 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001120 if (packageName != null) {
1121 return filename.startsWith("carrierconfig-" + packageName + "-");
1122 } else {
1123 return filename.startsWith("carrierconfig-");
1124 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001125 }
1126 });
Junda Liu8a8a53a2015-05-15 16:19:57 -07001127 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001128 for (File f : packageFiles) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001129 logd("Deleting " + f.getName());
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001130 f.delete();
1131 }
Junda Liu8a8a53a2015-05-15 16:19:57 -07001132 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001133 }
1134
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001135 /** Builds a canonical file name for a config file. */
Jayachandran C645ec612020-01-10 10:30:25 -08001136 private static String getFilenameForConfig(
1137 @NonNull String packageName, @NonNull String extraString,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001138 @NonNull String iccid, int cid) {
chen xu02581692018-11-11 19:03:44 -08001139 // the same carrier should have a single copy of XML file named after carrier id.
1140 // However, it's still possible that platform doesn't recognize the current sim carrier,
1141 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
1142 // cases SIM OTA resolves to different carrier while iccid remains the same.
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001143 return "carrierconfig-" + packageName + extraString + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001144 }
1145
Jayachandran C645ec612020-01-10 10:30:25 -08001146 /** Builds a canonical file name for no SIM config file. */
1147 private String getFilenameForNoSimConfig(@NonNull String packageName) {
1148 return "carrierconfig-" + packageName + "-" + "nosim" + ".xml";
1149 }
1150
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001151 /** Return the current version code of a package, or null if the name is not found. */
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001152 private String getPackageVersion(String packageName) {
1153 try {
1154 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -08001155 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001156 } catch (PackageManager.NameNotFoundException e) {
1157 return null;
1158 }
1159 }
1160
Jonathan Basseri65273c82017-07-25 15:08:42 -07001161 /**
1162 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001163 *
1164 * This reads config bundles for the given phoneId. That means getting the latest bundle from
1165 * the default app and a privileged carrier app, if present. This will not bind to an app if we
1166 * have a saved config file to use instead.
1167 */
1168 private void updateConfigForPhoneId(int phoneId) {
Jonathan Basseri65273c82017-07-25 15:08:42 -07001169 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001170 }
1171
Malcolm Chen5ea18532019-10-24 19:55:44 -07001172 private void onMultiSimConfigChanged() {
1173 for (int i = TelephonyManager.from(mContext).getActiveModemCount();
1174 i < mConfigFromDefaultApp.length; i++) {
1175 clearConfigForPhone(i, false);
1176 }
1177 }
1178
Hall Liu506724b2018-10-22 18:16:14 -07001179 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001180 @NonNull
Rambo Wangd2b004b2021-03-30 10:10:23 -07001181 public PersistableBundle getConfigForSubId(int subscriptionId, String callingPackage) {
1182 return getConfigForSubIdWithFeature(subscriptionId, callingPackage, null);
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08001183 }
1184
1185 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001186 @NonNull
Rambo Wangd2b004b2021-03-30 10:10:23 -07001187 public PersistableBundle getConfigForSubIdWithFeature(int subscriptionId, String callingPackage,
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -08001188 String callingFeatureId) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001189 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subscriptionId,
1190 callingPackage, callingFeatureId, "getCarrierConfig")) {
Rambo Wang42026112021-04-07 20:57:28 +00001191 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -07001192 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -07001193
Rambo Wangd2b004b2021-03-30 10:10:23 -07001194 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001195 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001196 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -07001197 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001198 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001199 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001200 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001201 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -08001202 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001203 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -08001204 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001205 config = mPersistentOverrideConfigs[phoneId];
1206 if (config != null) {
1207 retConfig.putAll(config);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001208 }
Hall Liu506724b2018-10-22 18:16:14 -07001209 config = mOverrideConfigs[phoneId];
1210 if (config != null) {
1211 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -07001212 }
Nathan Harold1122e3d2021-01-22 15:45:24 -08001213 // Ignore the theoretical case of the default app not being present since that won't
1214 // work in CarrierConfigLoader today.
1215 final boolean allConfigsApplied =
1216 (mConfigFromCarrierApp[phoneId] != null
1217 || getCarrierPackageForPhoneId(phoneId) == null)
1218 && mConfigFromDefaultApp[phoneId] != null;
1219 retConfig.putBoolean(
1220 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, allConfigsApplied);
Jayachandran C645ec612020-01-10 10:30:25 -08001221 } else {
1222 if (mNoSimConfig != null) {
1223 retConfig.putAll(mNoSimConfig);
1224 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001225 }
1226 return retConfig;
1227 }
1228
1229 @Override
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001230 public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides,
1231 boolean persistent) {
Hall Liu506724b2018-10-22 18:16:14 -07001232 mContext.enforceCallingOrSelfPermission(
1233 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Brad Ebingerf6281ad2019-04-25 11:02:04 -07001234 //TODO: Also check for SHELL UID to restrict this method to testing only (b/131326259)
Hall Liu506724b2018-10-22 18:16:14 -07001235 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1236 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001237 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
Rambo Wangd2b004b2021-03-30 10:10:23 -07001238 throw new IllegalArgumentException(
1239 "Invalid phoneId " + phoneId + " for subId " + subscriptionId);
Hall Liu506724b2018-10-22 18:16:14 -07001240 }
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001241 // Post to run on handler thread on which all states should be confined.
1242 mHandler.post(() -> {
1243 overrideConfig(mOverrideConfigs, phoneId, overrides);
Hall Liu506724b2018-10-22 18:16:14 -07001244
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001245 if (persistent) {
1246 overrideConfig(mPersistentOverrideConfigs, phoneId, overrides);
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001247
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001248 if (overrides != null) {
1249 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
1250 saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION,
1251 phoneId,
1252 carrierId, mPersistentOverrideConfigs[phoneId]);
1253 } else {
1254 final String iccid = getIccIdForPhoneId(phoneId);
1255 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1256 String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage,
1257 OVERRIDE_PACKAGE_ADDITION, iccid, cid);
1258 File fileToDelete = new File(mContext.getFilesDir(), fileName);
1259 fileToDelete.delete();
1260 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001261 }
Rambo Wang38eb5cd2021-03-29 16:39:14 -07001262 notifySubscriptionInfoUpdater(phoneId);
1263 });
Hall Liu506724b2018-10-22 18:16:14 -07001264 }
1265
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001266 private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId,
1267 @Nullable PersistableBundle overrides) {
1268 if (overrides == null) {
Rambo Wang42026112021-04-07 20:57:28 +00001269 currentOverrides[phoneId] = new PersistableBundle();
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001270 } else if (currentOverrides[phoneId] == null) {
1271 currentOverrides[phoneId] = overrides;
1272 } else {
1273 currentOverrides[phoneId].putAll(overrides);
1274 }
1275 }
1276
Hall Liu506724b2018-10-22 18:16:14 -07001277 @Override
Rambo Wangd2b004b2021-03-30 10:10:23 -07001278 public void notifyConfigChangedForSubId(int subscriptionId) {
Jordan Liud7d57fe2019-04-16 12:29:43 -07001279 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001280 // system privileged app with MODIFY_PHONE_STATE permission.
Rambo Wangd2b004b2021-03-30 10:10:23 -07001281 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext,
1282 subscriptionId, "Require carrier privileges or MODIFY_PHONE_STATE permission.");
1283
1284 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1285 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1286 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
1287 throw new IllegalArgumentException(
1288 "Invalid phoneId " + phoneId + " for subId " + subscriptionId);
1289 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001290
1291 // This method should block until deleting has completed, so that an error which prevents us
1292 // from clearing the cache is passed back to the carrier app. With the files successfully
1293 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001294 String callingPackageName = mContext.getPackageManager().getNameForUid(
1295 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001296 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001297 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001298 }
1299
1300 @Override
1301 public void updateConfigForPhoneId(int phoneId, String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -07001302 mContext.enforceCallingOrSelfPermission(
1303 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001304 logdWithLocalLog("Update config for phoneId: " + phoneId + " simState: " + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001305 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Rambo Wangd2b004b2021-03-30 10:10:23 -07001306 throw new IllegalArgumentException("Invalid phoneId: " + phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001307 }
1308 // requires Java 7 for switch on string.
1309 switch (simState) {
1310 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1311 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001312 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001313 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Malcolm Chendb66de12019-12-09 18:57:07 -08001314 case IccCardConstants.INTENT_VALUE_ICC_NOT_READY:
Junda Liu9f2d2712015-05-15 14:22:45 -07001315 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001316 break;
1317 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1318 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001319 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001320 break;
1321 }
1322 }
1323
Junda Liu43d723a2015-05-12 17:23:45 -07001324 @Override
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001325 public String getDefaultCarrierServicePackageName() {
Shuo Qianefdb7962019-12-19 15:54:27 -08001326 mContext.enforceCallingOrSelfPermission(
1327 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1328 "getDefaultCarrierServicePackageName");
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001329 return mPlatformCarrierConfigPackage;
1330 }
1331
Rambo Wang7e3bc122021-03-23 09:24:38 -07001332 @VisibleForTesting
1333 /* package */ Handler getHandler() {
1334 return mHandler;
1335 }
1336
1337 @VisibleForTesting
1338 /* package */ PersistableBundle getConfigFromDefaultApp(int phoneId) {
1339 return mConfigFromDefaultApp[phoneId];
1340 }
1341
1342 @VisibleForTesting
1343 /* package */ PersistableBundle getConfigFromCarrierApp(int phoneId) {
1344 return mConfigFromCarrierApp[phoneId];
1345 }
1346
1347 @VisibleForTesting
1348 /* package */ PersistableBundle getNoSimConfig() {
1349 return mNoSimConfig;
1350 }
1351
1352 @VisibleForTesting
1353 /* package */ PersistableBundle getOverrideConfig(int phoneId) {
1354 return mOverrideConfigs[phoneId];
1355 }
1356
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001357 private void unbindIfBound(Context context, CarrierServiceConnection conn,
1358 int phoneId) {
1359 if (mServiceBound[phoneId]) {
1360 mServiceBound[phoneId] = false;
1361 context.unbindService(conn);
1362 }
1363 }
1364
Jayachandran C645ec612020-01-10 10:30:25 -08001365 private void unbindIfBoundForNoSimConfig(Context context, CarrierServiceConnection conn,
1366 int phoneId) {
1367 if (mServiceBoundForNoSimConfig[phoneId]) {
1368 mServiceBoundForNoSimConfig[phoneId] = false;
1369 context.unbindService(conn);
1370 }
1371 }
1372
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001373 /**
Rambo Wang610fdf62021-03-08 21:37:11 -08001374 * Returns a boxed Integer object for phoneId, services as message token to distinguish messages
1375 * with same code when calling {@link Handler#removeMessages(int, Object)}.
1376 */
1377 private Integer getMessageToken(int phoneId) {
1378 if (phoneId < -128 || phoneId > 127) {
1379 throw new IllegalArgumentException("phoneId should be in range [-128, 127], inclusive");
1380 }
1381 // Integer#valueOf guarantees the integers within [-128, 127] are cached and thus memory
1382 // comparison (==) returns true for the same integer.
1383 return Integer.valueOf(phoneId);
1384 }
1385
1386 /**
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001387 * If {@code args} contains {@link #DUMP_ARG_REQUESTING_PACKAGE} and a following package name,
1388 * we'll also call {@link IBinder#dump} on the default carrier service (if bound) and the
1389 * specified carrier service (if bound). Typically, this is done for connectivity bug reports
1390 * where we don't call {@code dumpsys activity service all-non-platform} because that contains
1391 * too much info, but we still want to let carrier apps include their diagnostics.
1392 */
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001393 @Override
Junda Liu43d723a2015-05-12 17:23:45 -07001394 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001395 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
Junda Liu43d723a2015-05-12 17:23:45 -07001396 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1397 != PackageManager.PERMISSION_GRANTED) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001398 indentPW.println("Permission Denial: can't dump carrierconfig from from pid="
Junda Liu43d723a2015-05-12 17:23:45 -07001399 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1400 return;
1401 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001402 String requestingPackage = null;
1403 int requestingPackageIndex = ArrayUtils.indexOf(args, DUMP_ARG_REQUESTING_PACKAGE);
1404 if (requestingPackageIndex >= 0 && requestingPackageIndex < args.length - 1
1405 && !TextUtils.isEmpty(args[requestingPackageIndex + 1])) {
1406 requestingPackage = args[requestingPackageIndex + 1];
1407 // Throws a SecurityException if the caller is impersonating another app in an effort to
1408 // dump extra info (which may contain PII the caller doesn't have a right to).
1409 enforceCallerIsSystemOrRequestingPackage(requestingPackage);
1410 }
1411
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001412 indentPW.println("CarrierConfigLoader: " + this);
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001413 for (int i = 0; i < TelephonyManager.from(mContext).getActiveModemCount(); i++) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001414 indentPW.println("Phone Id = " + i);
shuoq26a3a4c2016-12-16 11:06:48 -08001415 // display default values in CarrierConfigManager
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001416 printConfig(CarrierConfigManager.getDefaultConfig(), indentPW,
shuoq26a3a4c2016-12-16 11:06:48 -08001417 "Default Values from CarrierConfigManager");
shuoq26a3a4c2016-12-16 11:06:48 -08001418 // display ConfigFromDefaultApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001419 printConfig(mConfigFromDefaultApp[i], indentPW, "mConfigFromDefaultApp");
shuoq26a3a4c2016-12-16 11:06:48 -08001420 // display ConfigFromCarrierApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001421 printConfig(mConfigFromCarrierApp[i], indentPW, "mConfigFromCarrierApp");
1422 printConfig(mPersistentOverrideConfigs[i], indentPW, "mPersistentOverrideConfigs");
1423 printConfig(mOverrideConfigs[i], indentPW, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001424 }
chen xudb04c292019-03-26 17:01:15 -07001425
Jayachandran C645ec612020-01-10 10:30:25 -08001426 printConfig(mNoSimConfig, indentPW, "mNoSimConfig");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001427 indentPW.println("CarrierConfigLoadingLog=");
1428 mCarrierConfigLoadingLog.dump(fd, indentPW, args);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001429
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001430 if (requestingPackage != null) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001431 logd("Including default and requesting package " + requestingPackage
1432 + " carrier services in dump");
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001433 indentPW.println("");
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001434 indentPW.println("Connected services");
1435 dumpCarrierServiceIfBound(fd, indentPW, "Default config package",
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001436 mPlatformCarrierConfigPackage, false /* considerCarrierPrivileges */);
1437 dumpCarrierServiceIfBound(fd, indentPW, "Requesting package", requestingPackage,
1438 true /* considerCarrierPrivileges */);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001439 }
shuoq26a3a4c2016-12-16 11:06:48 -08001440 }
1441
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001442 private void printConfig(PersistableBundle configApp, IndentingPrintWriter indentPW,
1443 String name) {
1444 indentPW.increaseIndent();
shuoq26a3a4c2016-12-16 11:06:48 -08001445 if (configApp == null) {
shuoq26a3a4c2016-12-16 11:06:48 -08001446 indentPW.println(name + " : null ");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001447 indentPW.decreaseIndent();
1448 indentPW.println("");
shuoq26a3a4c2016-12-16 11:06:48 -08001449 return;
1450 }
shuoq26a3a4c2016-12-16 11:06:48 -08001451 indentPW.println(name + " : ");
1452 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1453 Collections.sort(sortedKeys);
1454 indentPW.increaseIndent();
1455 indentPW.increaseIndent();
1456 for (String key : sortedKeys) {
1457 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1458 indentPW.println(key + " = " +
1459 Arrays.toString((Object[]) configApp.get(key)));
1460 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1461 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1462 } else {
1463 indentPW.println(key + " = " + configApp.get(key));
1464 }
Junda Liu43d723a2015-05-12 17:23:45 -07001465 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001466 indentPW.decreaseIndent();
1467 indentPW.decreaseIndent();
1468 indentPW.decreaseIndent();
1469 indentPW.println("");
Junda Liu43d723a2015-05-12 17:23:45 -07001470 }
1471
Hunter Knepshielde601f432020-02-19 10:16:04 -08001472 /**
1473 * Passes without problem when one of these conditions is true:
1474 * - The caller is a privileged UID (e.g. for dumpstate.cpp generating a bug report, where the
1475 * system knows the true caller plumbed in through the {@link android.os.BugreportManager} API).
1476 * - The caller's UID matches the supplied package.
1477 *
1478 * @throws SecurityException if none of the above conditions are met.
1479 */
1480 private void enforceCallerIsSystemOrRequestingPackage(String requestingPackage)
1481 throws SecurityException {
1482 final int callingUid = Binder.getCallingUid();
1483 if (callingUid == Process.ROOT_UID || callingUid == Process.SYSTEM_UID
1484 || callingUid == Process.SHELL_UID || callingUid == Process.PHONE_UID) {
1485 // Bug reports (dumpstate.cpp) run as SHELL, and let some other privileged UIDs through
1486 // as well.
1487 return;
1488 }
1489 // An app is trying to dump extra detail, block it if they aren't who they claim to be.
1490 AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class);
1491 if (appOps == null) {
1492 throw new SecurityException("No AppOps");
1493 }
1494 // Will throw a SecurityException if the UID and package don't match.
1495 appOps.checkPackage(callingUid, requestingPackage);
1496 }
1497
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001498 /**
1499 * Searches for one or more appropriate {@link CarrierService} instances to dump based on the
1500 * current connections.
1501 *
1502 * @param targetPkgName the target package name to dump carrier services for
1503 * @param considerCarrierPrivileges if true, allow a carrier service to be dumped if it shares
1504 * carrier privileges with {@code targetPkgName};
1505 * otherwise, only dump a carrier service if it is {@code
1506 * targetPkgName}
1507 */
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001508 private void dumpCarrierServiceIfBound(FileDescriptor fd, IndentingPrintWriter indentPW,
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001509 String prefix, String targetPkgName, boolean considerCarrierPrivileges) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001510 // Null package is possible if it's early in the boot process, there was a recent crash, we
1511 // loaded the config from XML most recently, or a SIM slot is empty. Carrier apps with
1512 // long-lived bindings should typically get dumped here regardless. Even if an app is being
1513 // used for multiple phoneIds, we assume that it's smart enough to handle that on its own,
1514 // and that in most cases we'd just be dumping duplicate information and bloating a report.
1515 indentPW.increaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001516 indentPW.println(prefix + " : " + targetPkgName);
1517 Set<String> dumpedPkgNames = new ArraySet<>(mServiceConnection.length);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001518 for (CarrierServiceConnection connection : mServiceConnection) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001519 if (connection == null || !SubscriptionManager.isValidPhoneId(connection.phoneId)
1520 || TextUtils.isEmpty(connection.pkgName)) {
1521 continue;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001522 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001523 final String servicePkgName = connection.pkgName;
1524 // Note: we intentionally ignore system components here because we should NOT match the
1525 // shell caller that's typically used for bug reports via non-BugreportManager triggers.
1526 final boolean exactPackageMatch = TextUtils.equals(targetPkgName, servicePkgName);
1527 final boolean carrierPrivilegesMatch =
1528 considerCarrierPrivileges && hasCarrierPrivileges(targetPkgName,
1529 connection.phoneId);
1530 if (!exactPackageMatch && !carrierPrivilegesMatch) continue;
1531 // Make sure this service is actually alive before trying to dump it. We don't pay
1532 // attention to mServiceBound[connection.phoneId] because typically carrier apps will
1533 // request long-lived bindings, and even if we unbind the app, it may still be alive due
1534 // to CarrierServiceBindHelper. Pull it out as a reference so even if it gets set to
1535 // null within the ServiceConnection during unbinding we can avoid an NPE.
1536 final IBinder service = connection.service;
1537 if (service == null || !service.isBinderAlive() || !service.pingBinder()) continue;
1538 // We've got a live service. Last check is just to make sure we don't dump a package
1539 // multiple times.
1540 if (!dumpedPkgNames.add(servicePkgName)) continue;
1541 if (!exactPackageMatch) {
1542 logd(targetPkgName + " has carrier privileges on phoneId " + connection.phoneId
1543 + ", service provided by " + servicePkgName);
1544 indentPW.increaseIndent();
1545 indentPW.println("Proxy : " + servicePkgName);
1546 indentPW.decreaseIndent();
1547 }
1548 // Flush before we let the app output anything to ensure correct ordering of output.
1549 // Internally, Binder#dump calls flush on its printer after finishing so we don't
1550 // need to do anything after.
1551 indentPW.flush();
1552 try {
1553 logd("Dumping " + servicePkgName);
1554 // We don't need to give the carrier service any args.
1555 connection.service.dump(fd, null /* args */);
1556 logd("Done with " + servicePkgName);
1557 } catch (RemoteException e) {
1558 logd("RemoteException from " + servicePkgName, e);
1559 indentPW.increaseIndent();
1560 indentPW.println("RemoteException");
1561 indentPW.increaseIndent();
1562 e.printStackTrace(indentPW);
1563 indentPW.decreaseIndent();
1564 indentPW.decreaseIndent();
1565 // We won't retry this package again because now it's in dumpedPkgNames.
1566 }
1567 indentPW.println("");
Jordan Liu46ed5db2020-01-28 08:55:20 -08001568 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001569 if (dumpedPkgNames.isEmpty()) {
1570 indentPW.increaseIndent();
1571 indentPW.println("Not bound");
1572 indentPW.decreaseIndent();
1573 indentPW.println("");
1574 }
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001575 indentPW.decreaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001576 }
1577
1578 private boolean hasCarrierPrivileges(String pkgName, int phoneId) {
1579 int[] subIds = SubscriptionManager.getSubId(phoneId);
1580 if (ArrayUtils.isEmpty(subIds)) {
1581 return false;
1582 }
1583 return TelephonyManager.from(mContext).createForSubscriptionId(
1584 subIds[0]).checkCarrierPrivilegesForPackage(pkgName)
1585 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001586 }
1587
Zach Johnson36d7aab2015-05-22 15:43:00 -07001588 private class CarrierServiceConnection implements ServiceConnection {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001589 final int phoneId;
1590 final String pkgName;
1591 final int eventId;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001592 IBinder service;
1593
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001594 CarrierServiceConnection(int phoneId, String pkgName, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001595 this.phoneId = phoneId;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001596 this.pkgName = pkgName;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001597 this.eventId = eventId;
1598 }
1599
1600 @Override
1601 public void onServiceConnected(ComponentName name, IBinder service) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001602 logd("Connected to config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001603 this.service = service;
1604 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1605 }
1606
1607 @Override
1608 public void onServiceDisconnected(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001609 logd("Disconnected from config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001610 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001611 }
1612
1613 @Override
1614 public void onBindingDied(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001615 logd("Binding died from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001616 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001617 }
1618
1619 @Override
1620 public void onNullBinding(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001621 logd("Null binding from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001622 this.service = null;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001623 }
1624 }
1625
1626 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1627 @Override
1628 public void onReceive(Context context, Intent intent) {
1629 String action = intent.getAction();
Junda Liu8a8a53a2015-05-15 16:19:57 -07001630 boolean replace = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1631 // If replace is true, only care ACTION_PACKAGE_REPLACED.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001632 if (replace && !Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
Junda Liu8a8a53a2015-05-15 16:19:57 -07001633 return;
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001634 }
Junda Liu8a8a53a2015-05-15 16:19:57 -07001635
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001636 switch (action) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001637 case Intent.ACTION_BOOT_COMPLETED:
1638 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1639 break;
1640
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001641 case Intent.ACTION_PACKAGE_ADDED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001642 case Intent.ACTION_PACKAGE_REMOVED:
Junda Liu8a8a53a2015-05-15 16:19:57 -07001643 case Intent.ACTION_PACKAGE_REPLACED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001644 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1645 String packageName = mContext.getPackageManager().getNameForUid(uid);
Junda Liu8a8a53a2015-05-15 16:19:57 -07001646 if (packageName != null) {
1647 // We don't have a phoneId for arg1.
1648 mHandler.sendMessage(
1649 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, packageName));
1650 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001651 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001652 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001653 }
1654 }
1655
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001656 private void logd(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001657 Log.d(LOG_TAG, msg);
1658 }
1659
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001660 private void logd(String msg, Throwable tr) {
1661 Log.d(LOG_TAG, msg, tr);
1662 }
1663
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001664 private void logdWithLocalLog(String msg) {
chen xudb04c292019-03-26 17:01:15 -07001665 Log.d(LOG_TAG, msg);
1666 mCarrierConfigLoadingLog.log(msg);
1667 }
1668
1669 private void loge(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001670 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07001671 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001672 }
1673}