blob: f181e9f4e0ba50cd8569a99fdd713b9e514341d8 [file] [log] [blame]
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001/*
Jonathan Basseri6465afd2015-02-25 13:05:57 -08002 * Copyright (c) 2015, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jonathan Basseri6465afd2015-02-25 13:05:57 -080017package com.android.phone;
18
Jonathan Basseri65273c82017-07-25 15:08:42 -070019import static android.service.carrier.CarrierService.ICarrierServiceWrapper.KEY_CONFIG_BUNDLE;
20import static android.service.carrier.CarrierService.ICarrierServiceWrapper.RESULT_ERROR;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080021
Jonathan Basseri11f89572015-05-05 11:57:39 -070022import android.annotation.NonNull;
Torbjorn Eklund723527a2019-02-13 11:16:25 +010023import android.annotation.Nullable;
Hunter Knepshielde601f432020-02-19 10:16:04 -080024import android.app.AppOpsManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080025import android.content.BroadcastReceiver;
26import android.content.ComponentName;
27import android.content.Context;
28import android.content.Intent;
29import android.content.IntentFilter;
30import android.content.ServiceConnection;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070031import android.content.SharedPreferences;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070032import android.content.pm.PackageInfo;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070033import android.content.pm.PackageManager;
Junda Liu43d723a2015-05-12 17:23:45 -070034import android.os.Binder;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070035import android.os.Build;
Jonathan Basseri65273c82017-07-25 15:08:42 -070036import android.os.Bundle;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080037import android.os.Handler;
38import android.os.IBinder;
39import android.os.Message;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070040import android.os.PersistableBundle;
Hunter Knepshielde601f432020-02-19 10:16:04 -080041import android.os.Process;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080042import android.os.RemoteException;
Jonathan Basseri65273c82017-07-25 15:08:42 -070043import android.os.ResultReceiver;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080044import android.os.ServiceManager;
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;
52import android.telephony.TelephonyManager;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -080053import android.text.TextUtils;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080054import android.util.ArraySet;
chen xudb04c292019-03-26 17:01:15 -070055import android.util.LocalLog;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080056import android.util.Log;
57
58import com.android.internal.telephony.ICarrierConfigLoader;
59import com.android.internal.telephony.IccCardConstants;
60import com.android.internal.telephony.Phone;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080061import com.android.internal.telephony.PhoneFactory;
Nathan Harold48ac0972019-03-13 22:33:01 -070062import com.android.internal.telephony.SubscriptionInfoUpdater;
Jeff Davidsona8e4e242018-03-15 17:16:18 -070063import com.android.internal.telephony.TelephonyPermissions;
Meng Wanga320b942019-11-25 11:21:26 -080064import com.android.internal.telephony.util.ArrayUtils;
shuoq26a3a4c2016-12-16 11:06:48 -080065import com.android.internal.util.IndentingPrintWriter;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080066
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070067import java.io.File;
Junda Liu43d723a2015-05-12 17:23:45 -070068import java.io.FileDescriptor;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070069import java.io.FileInputStream;
70import java.io.FileNotFoundException;
71import java.io.FileOutputStream;
Jonathan Basseri1f743c92015-05-15 00:19:46 -070072import java.io.FilenameFilter;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070073import java.io.IOException;
Junda Liu43d723a2015-05-12 17:23:45 -070074import java.io.PrintWriter;
shuoq26a3a4c2016-12-16 11:06:48 -080075import java.util.ArrayList;
76import java.util.Arrays;
77import java.util.Collections;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080078import java.util.List;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080079import java.util.Set;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080080
81/**
82 * CarrierConfigLoader binds to privileged carrier apps to fetch carrier config overlays.
Jonathan Basseri6465afd2015-02-25 13:05:57 -080083 */
Jonathan Basseri6465afd2015-02-25 13:05:57 -080084public class CarrierConfigLoader extends ICarrierConfigLoader.Stub {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070085 private static final String LOG_TAG = "CarrierConfigLoader";
Meng Wang33ad2bc2017-03-16 20:21:20 -070086
87 // Package name for platform carrier config app, bundled with system image.
88 private final String mPlatformCarrierConfigPackage;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080089
90 /** The singleton instance. */
91 private static CarrierConfigLoader sInstance;
92 // The context for phone app, passed from PhoneGlobals.
93 private Context mContext;
94 // Carrier configs from default app, indexed by phoneID.
Jonathan Basseric31f1f32015-05-12 10:13:03 -070095 private PersistableBundle[] mConfigFromDefaultApp;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080096 // Carrier configs from privileged carrier config app, indexed by phoneID.
Jonathan Basseric31f1f32015-05-12 10:13:03 -070097 private PersistableBundle[] mConfigFromCarrierApp;
Torbjorn Eklund723527a2019-02-13 11:16:25 +010098 // Persistent Carrier configs that are provided via the override test API, indexed by phone ID.
99 private PersistableBundle[] mPersistentOverrideConfigs;
Hall Liu506724b2018-10-22 18:16:14 -0700100 // Carrier configs that are provided via the override test API, indexed by phone ID.
101 private PersistableBundle[] mOverrideConfigs;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800102 // Service connection for binding to config app.
Zach Johnson36d7aab2015-05-22 15:43:00 -0700103 private CarrierServiceConnection[] mServiceConnection;
Jordan Liu178430d2020-02-10 14:32:15 -0800104 // Whether we are bound to a service for each phone
105 private boolean[] mServiceBound;
Sarah Chin807d5c62020-03-30 17:21:09 -0700106 // Whether we have sent config change broadcast for each phone id.
Junda Liu03aad762017-07-21 13:32:17 -0700107 private boolean[] mHasSentConfigChange;
Sarah Chin807d5c62020-03-30 17:21:09 -0700108 // Whether the broadcast was sent from EVENT_SYSTEM_UNLOCKED, to track rebroadcasts
109 private boolean[] mFromSystemUnlocked;
Nathan Harold48ac0972019-03-13 22:33:01 -0700110 // SubscriptionInfoUpdater
111 private final SubscriptionInfoUpdater mSubscriptionInfoUpdater;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800112
Nanxi Chen3d670502016-03-17 16:32:09 -0700113 // Broadcast receiver for Boot intents, register intent filter in construtor.
114 private final BroadcastReceiver mBootReceiver = new ConfigLoaderBroadcastReceiver();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800115 // Broadcast receiver for SIM and pkg intents, register intent filter in constructor.
Nanxi Chen3d670502016-03-17 16:32:09 -0700116 private final BroadcastReceiver mPackageReceiver = new ConfigLoaderBroadcastReceiver();
Jack Yu67663de2019-10-17 15:19:48 -0700117 private final LocalLog mCarrierConfigLoadingLog = new LocalLog(100);
chen xudb04c292019-03-26 17:01:15 -0700118
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800119
120 // Message codes; see mHandler below.
121 // Request from SubscriptionInfoUpdater when SIM becomes absent or error.
122 private static final int EVENT_CLEAR_CONFIG = 0;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800123 // Has connected to default app.
124 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
125 // Has connected to carrier app.
126 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700127 // Config has been loaded from default app (or cache).
128 private static final int EVENT_FETCH_DEFAULT_DONE = 5;
129 // Config has been loaded from carrier app (or cache).
130 private static final int EVENT_FETCH_CARRIER_DONE = 6;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700131 // Attempt to fetch from default app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700132 private static final int EVENT_DO_FETCH_DEFAULT = 7;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700133 // Attempt to fetch from carrier app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700134 private static final int EVENT_DO_FETCH_CARRIER = 8;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700135 // A package has been installed, uninstalled, or updated.
136 private static final int EVENT_PACKAGE_CHANGED = 9;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700137 // Bind timed out for the default app.
138 private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
139 // Bind timed out for a carrier app.
140 private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700141 // Check if the system fingerprint has changed.
142 private static final int EVENT_CHECK_SYSTEM_UPDATE = 12;
Nanxi Chen3d670502016-03-17 16:32:09 -0700143 // Rerun carrier config binding after system is unlocked.
144 private static final int EVENT_SYSTEM_UNLOCKED = 13;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700145 // Fetching config timed out from the default app.
146 private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14;
147 // Fetching config timed out from a carrier app.
148 private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15;
Nathan Harold48ac0972019-03-13 22:33:01 -0700149 // SubscriptionInfoUpdater has finished updating the sub for the carrier config.
150 private static final int EVENT_SUBSCRIPTION_INFO_UPDATED = 16;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700151 // Multi-SIM config changed.
152 private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 17;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700153
Junda Liu6cb45002016-03-22 17:18:20 -0700154 private static final int BIND_TIMEOUT_MILLIS = 30000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800155
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800156 // Keys used for saving and restoring config bundle from file.
157 private static final String KEY_VERSION = "__carrier_config_package_version__";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800158
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100159 private static final String OVERRIDE_PACKAGE_ADDITION = "-override";
160
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700161 // SharedPreferences key for last known build fingerprint.
162 private static final String KEY_FINGERPRINT = "build_fingerprint";
163
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800164 // Argument for #dump that indicates we should also call the default and specified carrier
165 // service's #dump method. In multi-SIM devices, it's possible that carrier A is on SIM 1 and
166 // carrier B is on SIM 2, in which case we should not dump carrier B's service when carrier A
167 // requested the dump.
168 private static final String DUMP_ARG_REQUESTING_PACKAGE = "--requesting-package";
169
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800170 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700171 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700172 // For each phoneId, the event sequence should be:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700173 // fetch default, connected to default, fetch default (async), fetch default done,
174 // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700175 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700176 // If there is a saved config file for either the default app or the carrier app, we skip
177 // binding to the app and go straight from fetch to loaded.
178 //
179 // At any time, at most one connection is active. If events are not in this order, previous
180 // connection will be unbound, so only latest event takes effect.
181 //
182 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
183 // 1. loading from carrier app (even if read from a file)
184 // 2. loading from default app if there is no carrier app (even if read from a file)
185 // 3. clearing config (e.g. due to sim removal)
186 // 4. encountering bind or IPC error
Jonathan Basseri65273c82017-07-25 15:08:42 -0700187 private class ConfigHandler extends Handler {
188 @Override
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800189 public void handleMessage(Message msg) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700190 final int phoneId = msg.arg1;
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800191 logdWithLocalLog("mHandler: " + msg.what + " phoneId: " + phoneId);
Malcolm Chen5ea18532019-10-24 19:55:44 -0700192 if (!SubscriptionManager.isValidPhoneId(phoneId)
193 && msg.what != EVENT_MULTI_SIM_CONFIG_CHANGED) {
194 return;
195 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800196 switch (msg.what) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800197 case EVENT_CLEAR_CONFIG: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700198 clearConfigForPhone(phoneId, true);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800199 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700200 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700201
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800202 case EVENT_SYSTEM_UNLOCKED: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700203 for (int i = 0; i < TelephonyManager.from(mContext).getActiveModemCount();
204 ++i) {
Sarah Chin807d5c62020-03-30 17:21:09 -0700205 // When the user unlocks the device, send the broadcast again (with a
206 // rebroadcast extra) if we have sent it before unlock. This will avoid
207 // trying to load the carrier config when the SIM is still loading when the
208 // unlock happens.
Junda Liu03aad762017-07-21 13:32:17 -0700209 if (mHasSentConfigChange[i]) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800210 logdWithLocalLog("System unlocked");
Sarah Chin807d5c62020-03-30 17:21:09 -0700211 mFromSystemUnlocked[i] = true;
Junda Liu03aad762017-07-21 13:32:17 -0700212 updateConfigForPhoneId(i);
213 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700214 }
215 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700216 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700217
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800218 case EVENT_PACKAGE_CHANGED: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700219 final String carrierPackageName = (String) msg.obj;
220 // Only update if there are cached config removed to avoid updating config for
221 // unrelated packages.
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700222 if (clearCachedConfigForPackage(carrierPackageName)) {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700223 int numPhones = TelephonyManager.from(mContext).getActiveModemCount();
Junda Liu8a8a53a2015-05-15 16:19:57 -0700224 for (int i = 0; i < numPhones; ++i) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800225 logdWithLocalLog("Package changed: " + carrierPackageName
Jack Yu67663de2019-10-17 15:19:48 -0700226 + ", phone=" + i);
Junda Liu8a8a53a2015-05-15 16:19:57 -0700227 updateConfigForPhoneId(i);
228 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700229 }
230 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700231 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700232
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800233 case EVENT_DO_FETCH_DEFAULT: {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100234 // Restore persistent override values.
235 PersistableBundle config = restoreConfigFromXml(
236 mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId);
237 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800238 logd("Loaded persistent override config from XML. package="
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100239 + mPlatformCarrierConfigPackage
240 + " phoneId=" + phoneId);
241 mPersistentOverrideConfigs[phoneId] = config;
242 }
243
244 config = restoreConfigFromXml(mPlatformCarrierConfigPackage, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700245 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800246 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700247 "Loaded config from XML. package="
248 + mPlatformCarrierConfigPackage
249 + " phoneId="
250 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700251 mConfigFromDefaultApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700252 Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700253 newMsg.getData().putBoolean("loaded_from_xml", true);
254 mHandler.sendMessage(newMsg);
255 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700256 // No cached config, so fetch it from the default app.
257 if (bindToConfigPackage(
258 mPlatformCarrierConfigPackage,
259 phoneId,
260 EVENT_CONNECTED_TO_DEFAULT)) {
261 sendMessageDelayed(
262 obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700263 BIND_TIMEOUT_MILLIS);
264 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700265 // Send broadcast if bind fails.
Nathan Harold48ac0972019-03-13 22:33:01 -0700266 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700267 // TODO: We *must* call unbindService even if bindService returns false.
268 // (And possibly if SecurityException was thrown.)
chen xudb04c292019-03-26 17:01:15 -0700269 loge("binding to default app: "
270 + mPlatformCarrierConfigPackage + " fails");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700271 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800272 }
273 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700274 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800275
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800276 case EVENT_CONNECTED_TO_DEFAULT: {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700277 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700278 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800279 // If new service connection has been created, unbind.
280 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Jordan Liu178430d2020-02-10 14:32:15 -0800281 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800282 break;
283 }
chen xu02581692018-11-11 19:03:44 -0800284 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700285 // ResultReceiver callback will execute in this Handler's thread.
286 final ResultReceiver resultReceiver =
287 new ResultReceiver(this) {
288 @Override
289 public void onReceiveResult(int resultCode, Bundle resultData) {
Jordan Liu178430d2020-02-10 14:32:15 -0800290 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700291 // If new service connection has been created, this is stale.
292 if (mServiceConnection[phoneId] != conn) {
293 loge("Received response for stale request.");
294 return;
295 }
296 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
297 if (resultCode == RESULT_ERROR || resultData == null) {
298 // On error, abort config fetching.
299 loge("Failed to get carrier config");
Nathan Harold48ac0972019-03-13 22:33:01 -0700300 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700301 return;
302 }
303 PersistableBundle config =
304 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100305 saveConfigToXml(mPlatformCarrierConfigPackage, "", phoneId,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800306 carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700307 mConfigFromDefaultApp[phoneId] = config;
308 sendMessage(
309 obtainMessage(
310 EVENT_FETCH_DEFAULT_DONE, phoneId, -1));
311 }
312 };
313 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800314 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700315 ICarrierService carrierService =
316 ICarrierService.Stub.asInterface(conn.service);
317 carrierService.getCarrierConfig(carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800318 logdWithLocalLog("Fetch config for default app: "
chen xudb04c292019-03-26 17:01:15 -0700319 + mPlatformCarrierConfigPackage
320 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700321 } catch (RemoteException e) {
chen xudb04c292019-03-26 17:01:15 -0700322 loge("Failed to get carrier config from default app: " +
323 mPlatformCarrierConfigPackage + " err: " + e.toString());
Jordan Liu178430d2020-02-10 14:32:15 -0800324 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700325 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800326 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700327 sendMessageDelayed(
328 obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1),
329 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800330 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700331 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800332
Jonathan Basseri930701e2015-06-11 20:56:43 -0700333 case EVENT_BIND_DEFAULT_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800334 case EVENT_FETCH_DEFAULT_TIMEOUT: {
335 loge("Bind/fetch time out from " + mPlatformCarrierConfigPackage);
chen xuf60b3162019-05-01 21:31:05 -0700336 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
337 // If we attempted to bind to the app, but the service connection is null due to
338 // the race condition that clear config event happens before bind/fetch complete
339 // then config was cleared while we were waiting and we should not continue.
340 if (mServiceConnection[phoneId] != null) {
341 // If a ResponseReceiver callback is in the queue when this happens, we will
342 // unbind twice and throw an exception.
Jordan Liu178430d2020-02-10 14:32:15 -0800343 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700344 broadcastConfigChangedIntent(phoneId);
345 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700346 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700347 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700348 }
Jonathan Basseri930701e2015-06-11 20:56:43 -0700349
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800350 case EVENT_FETCH_DEFAULT_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700351 // If we attempted to bind to the app, but the service connection is null, then
352 // config was cleared while we were waiting and we should not continue.
353 if (!msg.getData().getBoolean("loaded_from_xml", false)
354 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800355 break;
356 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700357 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700358 if (carrierPackageName != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800359 logd("Found carrier config app: " + carrierPackageName);
Jordan Liu38a614c2019-03-20 15:01:17 -0700360 sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId, -1));
Jonathan Basserib919e932015-05-27 16:53:08 +0000361 } else {
Nathan Harold48ac0972019-03-13 22:33:01 -0700362 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700363 }
364 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700365 }
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700366
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800367 case EVENT_DO_FETCH_CARRIER: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700368 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700369 final PersistableBundle config =
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100370 restoreConfigFromXml(carrierPackageName, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700371 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800372 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700373 "Loaded config from XML. package="
374 + carrierPackageName
375 + " phoneId="
376 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700377 mConfigFromCarrierApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700378 Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700379 newMsg.getData().putBoolean("loaded_from_xml", true);
380 sendMessage(newMsg);
381 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700382 // No cached config, so fetch it from a carrier app.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800383 if (carrierPackageName != null && bindToConfigPackage(carrierPackageName,
384 phoneId, EVENT_CONNECTED_TO_CARRIER)) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700385 sendMessageDelayed(
386 obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700387 BIND_TIMEOUT_MILLIS);
388 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700389 // Send broadcast if bind fails.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700390 broadcastConfigChangedIntent(phoneId);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800391 loge("Bind to carrier app: " + carrierPackageName + " fails");
Nathan Harold48ac0972019-03-13 22:33:01 -0700392 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700393 }
394 }
395 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700396 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700397
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800398 case EVENT_CONNECTED_TO_CARRIER: {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700399 removeMessages(EVENT_BIND_CARRIER_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700400 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800401 // If new service connection has been created, unbind.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700402 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Jordan Liu178430d2020-02-10 14:32:15 -0800403 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800404 break;
405 }
chen xu02581692018-11-11 19:03:44 -0800406 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700407 // ResultReceiver callback will execute in this Handler's thread.
408 final ResultReceiver resultReceiver =
409 new ResultReceiver(this) {
410 @Override
411 public void onReceiveResult(int resultCode, Bundle resultData) {
Jordan Liu178430d2020-02-10 14:32:15 -0800412 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700413 // If new service connection has been created, this is stale.
414 if (mServiceConnection[phoneId] != conn) {
415 loge("Received response for stale request.");
416 return;
417 }
418 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
419 if (resultCode == RESULT_ERROR || resultData == null) {
420 // On error, abort config fetching.
chen xudb04c292019-03-26 17:01:15 -0700421 loge("Failed to get carrier config from carrier app: "
422 + getCarrierPackageForPhoneId(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700423 broadcastConfigChangedIntent(phoneId);
Nathan Harold48ac0972019-03-13 22:33:01 -0700424 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700425 return;
426 }
427 PersistableBundle config =
428 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100429 saveConfigToXml(getCarrierPackageForPhoneId(phoneId), "",
430 phoneId, carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700431 mConfigFromCarrierApp[phoneId] = config;
432 sendMessage(
433 obtainMessage(
434 EVENT_FETCH_CARRIER_DONE, phoneId, -1));
435 }
436 };
437 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800438 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700439 ICarrierService carrierService =
440 ICarrierService.Stub.asInterface(conn.service);
441 carrierService.getCarrierConfig(carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800442 logdWithLocalLog("Fetch config for carrier app: "
chen xudb04c292019-03-26 17:01:15 -0700443 + getCarrierPackageForPhoneId(phoneId)
444 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700445 } catch (RemoteException e) {
446 loge("Failed to get carrier config: " + e.toString());
Jordan Liu178430d2020-02-10 14:32:15 -0800447 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700448 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800449 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700450 sendMessageDelayed(
451 obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1),
452 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800453 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700454 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800455
Jonathan Basseri930701e2015-06-11 20:56:43 -0700456 case EVENT_BIND_CARRIER_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800457 case EVENT_FETCH_CARRIER_TIMEOUT: {
458 loge("Bind/fetch from carrier app timeout");
chen xuf60b3162019-05-01 21:31:05 -0700459 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
460 // If we attempted to bind to the app, but the service connection is null due to
461 // the race condition that clear config event happens before bind/fetch complete
462 // then config was cleared while we were waiting and we should not continue.
463 if (mServiceConnection[phoneId] != null) {
464 // If a ResponseReceiver callback is in the queue when this happens, we will
465 // unbind twice and throw an exception.
Jordan Liu178430d2020-02-10 14:32:15 -0800466 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700467 broadcastConfigChangedIntent(phoneId);
468 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700469 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700470 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700471 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800472 case EVENT_FETCH_CARRIER_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700473 // If we attempted to bind to the app, but the service connection is null, then
474 // config was cleared while we were waiting and we should not continue.
475 if (!msg.getData().getBoolean("loaded_from_xml", false)
476 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800477 break;
478 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700479 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800480 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700481 }
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700482
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800483 case EVENT_CHECK_SYSTEM_UPDATE: {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700484 SharedPreferences sharedPrefs =
485 PreferenceManager.getDefaultSharedPreferences(mContext);
486 final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
487 if (!Build.FINGERPRINT.equals(lastFingerprint)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800488 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700489 "Build fingerprint changed. old: "
490 + lastFingerprint
491 + " new: "
492 + Build.FINGERPRINT);
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700493 clearCachedConfigForPackage(null);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700494 sharedPrefs
495 .edit()
496 .putString(KEY_FINGERPRINT, Build.FINGERPRINT)
497 .apply();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700498 }
499 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700500 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700501
502 case EVENT_SUBSCRIPTION_INFO_UPDATED:
503 broadcastConfigChangedIntent(phoneId);
504 break;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700505 case EVENT_MULTI_SIM_CONFIG_CHANGED:
506 onMultiSimConfigChanged();
507 break;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800508 }
509 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700510 }
511
512 private final Handler mHandler;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800513
514 /**
515 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
516 * receiver for relevant events.
517 */
518 private CarrierConfigLoader(Context context) {
519 mContext = context;
Meng Wang33ad2bc2017-03-16 20:21:20 -0700520 mPlatformCarrierConfigPackage =
521 mContext.getString(R.string.platform_carrier_config_package);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700522 mHandler = new ConfigHandler();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800523
Nanxi Chen3d670502016-03-17 16:32:09 -0700524 IntentFilter bootFilter = new IntentFilter();
525 bootFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
526 context.registerReceiver(mBootReceiver, bootFilter);
527
Junda Liu8a8a53a2015-05-15 16:19:57 -0700528 // Register for package updates. Update app or uninstall app update will have all 3 intents,
529 // in the order or removed, added, replaced, all with extra_replace set to true.
530 IntentFilter pkgFilter = new IntentFilter();
531 pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
532 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
533 pkgFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
534 pkgFilter.addDataScheme("package");
Jordan Liu5ca24762019-07-10 12:51:09 -0700535 context.registerReceiver(mPackageReceiver, pkgFilter);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800536
Malcolm Chen978dda52019-10-08 18:15:25 -0700537 int numPhones = TelephonyManager.from(context).getSupportedModemCount();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700538 mConfigFromDefaultApp = new PersistableBundle[numPhones];
539 mConfigFromCarrierApp = new PersistableBundle[numPhones];
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100540 mPersistentOverrideConfigs = new PersistableBundle[numPhones];
Hall Liu506724b2018-10-22 18:16:14 -0700541 mOverrideConfigs = new PersistableBundle[numPhones];
Zach Johnson36d7aab2015-05-22 15:43:00 -0700542 mServiceConnection = new CarrierServiceConnection[numPhones];
Jordan Liu178430d2020-02-10 14:32:15 -0800543 mServiceBound = new boolean[numPhones];
Junda Liu03aad762017-07-21 13:32:17 -0700544 mHasSentConfigChange = new boolean[numPhones];
Sarah Chin807d5c62020-03-30 17:21:09 -0700545 mFromSystemUnlocked = new boolean[numPhones];
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800546 // Make this service available through ServiceManager.
547 ServiceManager.addService(Context.CARRIER_CONFIG_SERVICE, this);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800548 logd("CarrierConfigLoader has started");
Nathan Harold48ac0972019-03-13 22:33:01 -0700549 mSubscriptionInfoUpdater = PhoneFactory.getSubscriptionInfoUpdater();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700550 mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800551 }
552
553 /**
554 * Initialize the singleton CarrierConfigLoader instance.
555 *
556 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
557 */
558 /* package */
559 static CarrierConfigLoader init(Context context) {
560 synchronized (CarrierConfigLoader.class) {
561 if (sInstance == null) {
562 sInstance = new CarrierConfigLoader(context);
563 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700564 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800565 }
566 return sInstance;
567 }
568 }
569
Malcolm Chen5ea18532019-10-24 19:55:44 -0700570 private void clearConfigForPhone(int phoneId, boolean sendBroadcast) {
571 /* Ignore clear configuration request if device is being shutdown. */
572 Phone phone = PhoneFactory.getPhone(phoneId);
573 if (phone != null) {
574 if (phone.isShuttingDown()) {
575 return;
576 }
577 }
578
579 mConfigFromDefaultApp[phoneId] = null;
580 mConfigFromCarrierApp[phoneId] = null;
581 mServiceConnection[phoneId] = null;
582 mHasSentConfigChange[phoneId] = false;
583
584 if (sendBroadcast) broadcastConfigChangedIntent(phoneId, false);
585 }
586
Nathan Harold48ac0972019-03-13 22:33:01 -0700587 private void notifySubscriptionInfoUpdater(int phoneId) {
588 String configPackagename;
589 PersistableBundle configToSend;
590 int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
591 // Prefer the carrier privileged carrier app, but if there is not one, use the platform
592 // default carrier app.
593 if (mConfigFromCarrierApp[phoneId] != null) {
594 configPackagename = getCarrierPackageForPhoneId(phoneId);
595 configToSend = mConfigFromCarrierApp[phoneId];
596 } else {
597 configPackagename = mPlatformCarrierConfigPackage;
598 configToSend = mConfigFromDefaultApp[phoneId];
599 }
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700600
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800601 if (configToSend == null) {
602 configToSend = new PersistableBundle();
603 }
604
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700605 // mOverrideConfigs is for testing. And it will override current configs.
606 PersistableBundle config = mOverrideConfigs[phoneId];
607 if (config != null) {
Torbjorn Eklund1050cb02018-11-16 14:05:38 +0100608 configToSend = new PersistableBundle(configToSend);
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700609 configToSend.putAll(config);
610 }
611
Nathan Harold48ac0972019-03-13 22:33:01 -0700612 mSubscriptionInfoUpdater.updateSubscriptionByCarrierConfigAndNotifyComplete(
613 phoneId, configPackagename, configToSend,
614 mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1));
615 }
616
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800617 private void broadcastConfigChangedIntent(int phoneId) {
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800618 broadcastConfigChangedIntent(phoneId, true);
619 }
620
621 private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800622 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800623 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
chen xuf9db49f2019-03-31 23:04:42 -0700624 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800625 if (addSubIdExtra) {
626 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
627 int[] subIds = SubscriptionManager.getSubId(phoneId);
628 if (!ArrayUtils.isEmpty(subIds)) {
629 TelephonyManager telMgr = TelephonyManager.from(mContext)
630 .createForSubscriptionId(subIds[0]);
631 simApplicationState = telMgr.getSimApplicationState();
632 }
Malcolm Chenf7635ec2020-04-15 15:45:00 -0700633 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId
634 + " simApplicationState " + simApplicationState);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800635 // Include subId/carrier id extra only if SIM records are loaded
636 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
637 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
638 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
639 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID,
640 getSpecificCarrierIdForPhoneId(phoneId));
641 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, getCarrierIdForPhoneId(phoneId));
642 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800643 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800644 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Sarah Chin807d5c62020-03-30 17:21:09 -0700645 intent.putExtra(CarrierConfigManager.EXTRA_REBROADCAST_ON_UNLOCK,
646 mFromSystemUnlocked[phoneId]);
Meng Wang97a6a462020-01-23 16:22:16 -0800647 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
Jack Yue9e95be2020-03-22 10:56:06 -0700648 int[] subIds = SubscriptionManager.getSubId(phoneId);
649 if (subIds != null && subIds.length > 0) {
650 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId + ", subId=" + subIds[0]);
651 } else {
652 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
653 }
Junda Liu03aad762017-07-21 13:32:17 -0700654 mHasSentConfigChange[phoneId] = true;
Sarah Chin807d5c62020-03-30 17:21:09 -0700655 mFromSystemUnlocked[phoneId] = false;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800656 }
657
658 /** Binds to the default or carrier config app. */
659 private boolean bindToConfigPackage(String pkgName, int phoneId, int eventId) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800660 logdWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700661 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700662 carrierService.setPackage(pkgName);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800663 mServiceConnection[phoneId] = new CarrierServiceConnection(phoneId, pkgName, eventId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800664 try {
Jordan Liu178430d2020-02-10 14:32:15 -0800665 if (mContext.bindService(carrierService, mServiceConnection[phoneId],
666 Context.BIND_AUTO_CREATE)) {
667 mServiceBound[phoneId] = true;
668 return true;
669 } else {
670 return false;
671 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800672 } catch (SecurityException ex) {
673 return false;
674 }
675 }
676
chen xu02581692018-11-11 19:03:44 -0800677 private CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800678 String mcc = "";
679 String mnc = "";
680 String imsi = "";
681 String gid1 = "";
682 String gid2 = "";
683 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
684 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800685 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800686 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700687 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
688 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800689 mcc = simOperator.substring(0, 3);
690 mnc = simOperator.substring(3);
691 }
692 Phone phone = PhoneFactory.getPhone(phoneId);
693 if (phone != null) {
694 imsi = phone.getSubscriberId();
695 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700696 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800697 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800698 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800699 }
chen xua31f22b2019-03-06 15:28:50 -0800700 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800701 }
702
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700703 /** Returns the package name of a priveleged carrier app, or null if there is none. */
704 private String getCarrierPackageForPhoneId(int phoneId) {
705 List<String> carrierPackageNames = TelephonyManager.from(mContext)
706 .getCarrierPackageNamesForIntentAndPhone(
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700707 new Intent(CarrierService.CARRIER_SERVICE_INTERFACE), phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700708 if (carrierPackageNames != null && carrierPackageNames.size() > 0) {
709 return carrierPackageNames.get(0);
710 } else {
711 return null;
712 }
713 }
714
715 private String getIccIdForPhoneId(int phoneId) {
716 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
717 return null;
718 }
719 Phone phone = PhoneFactory.getPhone(phoneId);
720 if (phone == null) {
721 return null;
722 }
723 return phone.getIccSerialNumber();
724 }
725
chen xu02581692018-11-11 19:03:44 -0800726 /**
chen xua31f22b2019-03-06 15:28:50 -0800727 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800728 */
chen xua31f22b2019-03-06 15:28:50 -0800729 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700730 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800731 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700732 }
733 Phone phone = PhoneFactory.getPhone(phoneId);
734 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800735 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700736 }
chen xua31f22b2019-03-06 15:28:50 -0800737 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800738 }
739
740 /**
741 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
742 */
743 private int getCarrierIdForPhoneId(int phoneId) {
744 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
745 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700746 }
chen xu02581692018-11-11 19:03:44 -0800747 Phone phone = PhoneFactory.getPhone(phoneId);
748 if (phone == null) {
749 return TelephonyManager.UNKNOWN_CARRIER_ID;
750 }
751 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700752 }
753
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700754 /**
755 * Writes a bundle to an XML file.
756 *
chen xu02581692018-11-11 19:03:44 -0800757 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -0800758 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -0800759 * should have a single copy of XML file named after carrier id. However, it's still possible
760 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
761 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
762 * carrier while iccid remains the same.
763 *
764 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
765 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700766 *
767 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700768 *
769 * @param packageName the name of the package from which we fetched this bundle.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100770 * @param extraString An extra string to be used in the XML file name.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800771 * @param phoneId the phone ID.
772 * @param carrierId contains all carrier-identifying information.
773 * @param config the bundle to be written. Null will be treated as an empty bundle.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700774 */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100775 private void saveConfigToXml(String packageName, @NonNull String extraString, int phoneId,
776 CarrierIdentifier carrierId, PersistableBundle config) {
Yuchen Dongc15afed2018-04-26 17:05:22 +0800777 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
778 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700779 loge("Skip save config because SIM records are not loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800780 return;
781 }
782
783 final String iccid = getIccIdForPhoneId(phoneId);
Chen Xucd4a6372019-07-29 16:34:52 -0700784 final int cid = carrierId.getSpecificCarrierId();
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700785 if (packageName == null || iccid == null) {
786 loge("Cannot save config with null packageName or iccid.");
787 return;
788 }
Junda Liu02596502016-11-15 13:46:43 -0800789 // b/32668103 Only save to file if config isn't empty.
790 // In case of failure, not caching an empty bundle will
791 // try loading config again on next power on or sim loaded.
792 // Downside is for genuinely empty bundle, will bind and load
793 // on every power on.
794 if (config == null || config.isEmpty()) {
795 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700796 }
797
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700798 final String version = getPackageVersion(packageName);
799 if (version == null) {
800 loge("Failed to get package version for: " + packageName);
801 return;
802 }
803
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800804 logdWithLocalLog(
805 "Save config to xml, packagename: " + packageName + " phoneId: " + phoneId);
chen xudb04c292019-03-26 17:01:15 -0700806
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700807 FileOutputStream outFile = null;
808 try {
809 outFile = new FileOutputStream(
chen xu02581692018-11-11 19:03:44 -0800810 new File(mContext.getFilesDir(),
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100811 getFilenameForConfig(packageName, extraString, iccid, cid)));
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800812 config.putString(KEY_VERSION, version);
813 config.writeToStream(outFile);
814 outFile.flush();
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700815 outFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800816 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700817 loge(e.toString());
818 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700819 }
820
821 /**
822 * Reads a bundle from an XML file.
823 *
824 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +0800825 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700826 *
827 * In case of errors, or if the saved config is from a different package version than the
828 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700829 *
830 * @param packageName the name of the package from which we fetched this bundle.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100831 * @param extraString An extra string to be used in the XML file name.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800832 * @param phoneId the phone ID.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700833 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800834 * version does not match, or reading config fails.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700835 */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100836 private PersistableBundle restoreConfigFromXml(String packageName, @NonNull String extraString,
837 int phoneId) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700838 final String version = getPackageVersion(packageName);
839 if (version == null) {
840 loge("Failed to get package version for: " + packageName);
841 return null;
842 }
Yuchen Dongc15afed2018-04-26 17:05:22 +0800843 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
844 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700845 loge("Skip restoring config because SIM records are not yet loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800846 return null;
847 }
848
849 final String iccid = getIccIdForPhoneId(phoneId);
chen xua31f22b2019-03-06 15:28:50 -0800850 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700851 if (packageName == null || iccid == null) {
852 loge("Cannot restore config with null packageName or iccid.");
853 return null;
854 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700855
856 PersistableBundle restoredBundle = null;
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100857 File file = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700858 FileInputStream inFile = null;
859 try {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100860 file = new File(mContext.getFilesDir(),
861 getFilenameForConfig(packageName, extraString, iccid, cid));
862 inFile = new FileInputStream(file);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700863
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800864 restoredBundle = PersistableBundle.readFromStream(inFile);
865 String savedVersion = restoredBundle.getString(KEY_VERSION);
866 restoredBundle.remove(KEY_VERSION);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700867
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800868 if (!version.equals(savedVersion)) {
869 loge("Saved version mismatch: " + version + " vs " + savedVersion);
870 restoredBundle = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700871 }
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800872
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700873 inFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800874 } catch (FileNotFoundException e) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100875 // Missing file is normal occurrence that might occur with a new sim or when restoring
876 // an override file during boot and should not be treated as an error.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800877 if (file != null) logd("File not found: " + file.getPath());
878 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700879 loge(e.toString());
880 }
881
882 return restoredBundle;
883 }
884
Junda Liu8a8a53a2015-05-15 16:19:57 -0700885 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700886 * Clears cached carrier config.
887 * This deletes all saved XML files associated with the given package name. If packageName is
888 * null, then it deletes all saved XML files.
889 *
890 * @param packageName the name of a carrier package, or null if all cached config should be
891 * cleared.
892 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -0700893 */
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700894 private boolean clearCachedConfigForPackage(final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700895 File dir = mContext.getFilesDir();
896 File[] packageFiles = dir.listFiles(new FilenameFilter() {
897 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700898 if (packageName != null) {
899 return filename.startsWith("carrierconfig-" + packageName + "-");
900 } else {
901 return filename.startsWith("carrierconfig-");
902 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700903 }
904 });
Junda Liu8a8a53a2015-05-15 16:19:57 -0700905 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700906 for (File f : packageFiles) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800907 logd("Deleting " + f.getName());
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700908 f.delete();
909 }
Junda Liu8a8a53a2015-05-15 16:19:57 -0700910 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700911 }
912
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700913 /** Builds a canonical file name for a config file. */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100914 private String getFilenameForConfig(@NonNull String packageName, @NonNull String extraString,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800915 @NonNull String iccid, int cid) {
chen xu02581692018-11-11 19:03:44 -0800916 // the same carrier should have a single copy of XML file named after carrier id.
917 // However, it's still possible that platform doesn't recognize the current sim carrier,
918 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
919 // cases SIM OTA resolves to different carrier while iccid remains the same.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100920 return "carrierconfig-" + packageName + extraString + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700921 }
922
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700923 /** Return the current version code of a package, or null if the name is not found. */
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700924 private String getPackageVersion(String packageName) {
925 try {
926 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -0800927 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700928 } catch (PackageManager.NameNotFoundException e) {
929 return null;
930 }
931 }
932
Jonathan Basseri65273c82017-07-25 15:08:42 -0700933 /**
934 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700935 *
936 * This reads config bundles for the given phoneId. That means getting the latest bundle from
937 * the default app and a privileged carrier app, if present. This will not bind to an app if we
938 * have a saved config file to use instead.
939 */
940 private void updateConfigForPhoneId(int phoneId) {
Junda Liu8a8a53a2015-05-15 16:19:57 -0700941 // Clear in-memory cache for carrier app config, so when carrier app gets uninstalled, no
942 // stale config is left.
943 if (mConfigFromCarrierApp[phoneId] != null &&
944 getCarrierPackageForPhoneId(phoneId) == null) {
945 mConfigFromCarrierApp[phoneId] = null;
946 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700947 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700948 }
949
Malcolm Chen5ea18532019-10-24 19:55:44 -0700950 private void onMultiSimConfigChanged() {
951 for (int i = TelephonyManager.from(mContext).getActiveModemCount();
952 i < mConfigFromDefaultApp.length; i++) {
953 clearConfigForPhone(i, false);
954 }
955 }
956
Hall Liu506724b2018-10-22 18:16:14 -0700957 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800958 @NonNull
959 public PersistableBundle getConfigForSubId(int subId, String callingPackage) {
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -0800960 return getConfigForSubIdWithFeature(subId, callingPackage, null);
961 }
962
963 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800964 @NonNull
965 public PersistableBundle getConfigForSubIdWithFeature(int subId, String callingPackage,
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -0800966 String callingFeatureId) {
967 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subId, callingPackage,
968 callingFeatureId, "getCarrierConfig")) {
Malcolm Chen6d3d6b32018-03-01 13:58:03 -0800969 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -0700970 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -0700971
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800972 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700973 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800974 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700975 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800976 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700977 retConfig.putAll(config);
yinxud0104832019-08-16 14:20:08 -0700978 if (getCarrierPackageForPhoneId(phoneId) == null) {
979 retConfig.putBoolean(
980 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
981 }
yinxuc5e27622017-11-29 15:08:50 -0800982 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800983 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800984 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700985 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -0800986 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
987 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100988 config = mPersistentOverrideConfigs[phoneId];
989 if (config != null) {
990 retConfig.putAll(config);
991 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
992 }
Hall Liu506724b2018-10-22 18:16:14 -0700993 config = mOverrideConfigs[phoneId];
994 if (config != null) {
995 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -0700996 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800997 }
998 return retConfig;
999 }
1000
1001 @Override
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001002 public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides,
1003 boolean persistent) {
Hall Liu506724b2018-10-22 18:16:14 -07001004 mContext.enforceCallingOrSelfPermission(
1005 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Brad Ebingerf6281ad2019-04-25 11:02:04 -07001006 //TODO: Also check for SHELL UID to restrict this method to testing only (b/131326259)
Hall Liu506724b2018-10-22 18:16:14 -07001007 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1008 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001009 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
Hall Liu506724b2018-10-22 18:16:14 -07001010 return;
1011 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001012 overrideConfig(mOverrideConfigs, phoneId, overrides);
Hall Liu506724b2018-10-22 18:16:14 -07001013
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001014 if (persistent) {
1015 overrideConfig(mPersistentOverrideConfigs, phoneId, overrides);
1016
1017 if (overrides != null) {
1018 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
1019 saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId,
1020 carrierId, mPersistentOverrideConfigs[phoneId]);
1021 } else {
1022 final String iccid = getIccIdForPhoneId(phoneId);
1023 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1024 String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage,
1025 OVERRIDE_PACKAGE_ADDITION, iccid, cid);
1026 File fileToDelete = new File(mContext.getFilesDir(), fileName);
1027 fileToDelete.delete();
1028 }
Hall Liu506724b2018-10-22 18:16:14 -07001029 }
Brad Ebingerc9d1fa12019-04-17 15:21:18 -07001030 notifySubscriptionInfoUpdater(phoneId);
Hall Liu506724b2018-10-22 18:16:14 -07001031 }
1032
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001033 private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId,
1034 @Nullable PersistableBundle overrides) {
1035 if (overrides == null) {
1036 currentOverrides[phoneId] = new PersistableBundle();
1037 } else if (currentOverrides[phoneId] == null) {
1038 currentOverrides[phoneId] = overrides;
1039 } else {
1040 currentOverrides[phoneId].putAll(overrides);
1041 }
1042 }
1043
Hall Liu506724b2018-10-22 18:16:14 -07001044 @Override
Jonathan Basseri86030352015-06-08 12:27:56 -07001045 public void notifyConfigChangedForSubId(int subId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001046 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001047 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001048 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001049 return;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001050 }
Jordan Liud7d57fe2019-04-16 12:29:43 -07001051
1052 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001053 // system privileged app with MODIFY_PHONE_STATE permission.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001054 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext, subId,
1055 "Require carrier privileges or MODIFY_PHONE_STATE permission.");
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001056
1057 // This method should block until deleting has completed, so that an error which prevents us
1058 // from clearing the cache is passed back to the carrier app. With the files successfully
1059 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001060 String callingPackageName = mContext.getPackageManager().getNameForUid(
1061 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001062 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001063 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001064 }
1065
1066 @Override
1067 public void updateConfigForPhoneId(int phoneId, String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -07001068 mContext.enforceCallingOrSelfPermission(
1069 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001070 logdWithLocalLog("Update config for phoneId: " + phoneId + " simState: " + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001071 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1072 return;
1073 }
1074 // requires Java 7 for switch on string.
1075 switch (simState) {
1076 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1077 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001078 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001079 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Malcolm Chendb66de12019-12-09 18:57:07 -08001080 case IccCardConstants.INTENT_VALUE_ICC_NOT_READY:
Junda Liu9f2d2712015-05-15 14:22:45 -07001081 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001082 break;
1083 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1084 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001085 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001086 break;
1087 }
1088 }
1089
Junda Liu43d723a2015-05-12 17:23:45 -07001090 @Override
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001091 public String getDefaultCarrierServicePackageName() {
Shuo Qianefdb7962019-12-19 15:54:27 -08001092 mContext.enforceCallingOrSelfPermission(
1093 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1094 "getDefaultCarrierServicePackageName");
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001095 return mPlatformCarrierConfigPackage;
1096 }
1097
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001098 private void unbindIfBound(Context context, CarrierServiceConnection conn,
1099 int phoneId) {
1100 if (mServiceBound[phoneId]) {
1101 mServiceBound[phoneId] = false;
1102 context.unbindService(conn);
1103 }
1104 }
1105
1106 /**
1107 * If {@code args} contains {@link #DUMP_ARG_REQUESTING_PACKAGE} and a following package name,
1108 * we'll also call {@link IBinder#dump} on the default carrier service (if bound) and the
1109 * specified carrier service (if bound). Typically, this is done for connectivity bug reports
1110 * where we don't call {@code dumpsys activity service all-non-platform} because that contains
1111 * too much info, but we still want to let carrier apps include their diagnostics.
1112 */
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001113 @Override
Junda Liu43d723a2015-05-12 17:23:45 -07001114 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001115 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
Junda Liu43d723a2015-05-12 17:23:45 -07001116 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1117 != PackageManager.PERMISSION_GRANTED) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001118 indentPW.println("Permission Denial: can't dump carrierconfig from from pid="
Junda Liu43d723a2015-05-12 17:23:45 -07001119 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1120 return;
1121 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001122 String requestingPackage = null;
1123 int requestingPackageIndex = ArrayUtils.indexOf(args, DUMP_ARG_REQUESTING_PACKAGE);
1124 if (requestingPackageIndex >= 0 && requestingPackageIndex < args.length - 1
1125 && !TextUtils.isEmpty(args[requestingPackageIndex + 1])) {
1126 requestingPackage = args[requestingPackageIndex + 1];
1127 // Throws a SecurityException if the caller is impersonating another app in an effort to
1128 // dump extra info (which may contain PII the caller doesn't have a right to).
1129 enforceCallerIsSystemOrRequestingPackage(requestingPackage);
1130 }
1131
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001132 indentPW.println("CarrierConfigLoader: " + this);
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001133 for (int i = 0; i < TelephonyManager.from(mContext).getActiveModemCount(); i++) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001134 indentPW.println("Phone Id = " + i);
shuoq26a3a4c2016-12-16 11:06:48 -08001135 // display default values in CarrierConfigManager
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001136 printConfig(CarrierConfigManager.getDefaultConfig(), indentPW,
shuoq26a3a4c2016-12-16 11:06:48 -08001137 "Default Values from CarrierConfigManager");
shuoq26a3a4c2016-12-16 11:06:48 -08001138 // display ConfigFromDefaultApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001139 printConfig(mConfigFromDefaultApp[i], indentPW, "mConfigFromDefaultApp");
shuoq26a3a4c2016-12-16 11:06:48 -08001140 // display ConfigFromCarrierApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001141 printConfig(mConfigFromCarrierApp[i], indentPW, "mConfigFromCarrierApp");
1142 printConfig(mPersistentOverrideConfigs[i], indentPW, "mPersistentOverrideConfigs");
1143 printConfig(mOverrideConfigs[i], indentPW, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001144 }
chen xudb04c292019-03-26 17:01:15 -07001145
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001146 indentPW.println("CarrierConfigLoadingLog=");
1147 mCarrierConfigLoadingLog.dump(fd, indentPW, args);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001148
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001149 if (requestingPackage != null) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001150 logd("Including default and requesting package " + requestingPackage
1151 + " carrier services in dump");
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001152 indentPW.println("");
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001153 indentPW.println("Connected services");
1154 dumpCarrierServiceIfBound(fd, indentPW, "Default config package",
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001155 mPlatformCarrierConfigPackage, false /* considerCarrierPrivileges */);
1156 dumpCarrierServiceIfBound(fd, indentPW, "Requesting package", requestingPackage,
1157 true /* considerCarrierPrivileges */);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001158 }
shuoq26a3a4c2016-12-16 11:06:48 -08001159 }
1160
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001161 private void printConfig(PersistableBundle configApp, IndentingPrintWriter indentPW,
1162 String name) {
1163 indentPW.increaseIndent();
shuoq26a3a4c2016-12-16 11:06:48 -08001164 if (configApp == null) {
shuoq26a3a4c2016-12-16 11:06:48 -08001165 indentPW.println(name + " : null ");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001166 indentPW.decreaseIndent();
1167 indentPW.println("");
shuoq26a3a4c2016-12-16 11:06:48 -08001168 return;
1169 }
shuoq26a3a4c2016-12-16 11:06:48 -08001170 indentPW.println(name + " : ");
1171 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1172 Collections.sort(sortedKeys);
1173 indentPW.increaseIndent();
1174 indentPW.increaseIndent();
1175 for (String key : sortedKeys) {
1176 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1177 indentPW.println(key + " = " +
1178 Arrays.toString((Object[]) configApp.get(key)));
1179 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1180 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1181 } else {
1182 indentPW.println(key + " = " + configApp.get(key));
1183 }
Junda Liu43d723a2015-05-12 17:23:45 -07001184 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001185 indentPW.decreaseIndent();
1186 indentPW.decreaseIndent();
1187 indentPW.decreaseIndent();
1188 indentPW.println("");
Junda Liu43d723a2015-05-12 17:23:45 -07001189 }
1190
Hunter Knepshielde601f432020-02-19 10:16:04 -08001191 /**
1192 * Passes without problem when one of these conditions is true:
1193 * - The caller is a privileged UID (e.g. for dumpstate.cpp generating a bug report, where the
1194 * system knows the true caller plumbed in through the {@link android.os.BugreportManager} API).
1195 * - The caller's UID matches the supplied package.
1196 *
1197 * @throws SecurityException if none of the above conditions are met.
1198 */
1199 private void enforceCallerIsSystemOrRequestingPackage(String requestingPackage)
1200 throws SecurityException {
1201 final int callingUid = Binder.getCallingUid();
1202 if (callingUid == Process.ROOT_UID || callingUid == Process.SYSTEM_UID
1203 || callingUid == Process.SHELL_UID || callingUid == Process.PHONE_UID) {
1204 // Bug reports (dumpstate.cpp) run as SHELL, and let some other privileged UIDs through
1205 // as well.
1206 return;
1207 }
1208 // An app is trying to dump extra detail, block it if they aren't who they claim to be.
1209 AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class);
1210 if (appOps == null) {
1211 throw new SecurityException("No AppOps");
1212 }
1213 // Will throw a SecurityException if the UID and package don't match.
1214 appOps.checkPackage(callingUid, requestingPackage);
1215 }
1216
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001217 /**
1218 * Searches for one or more appropriate {@link CarrierService} instances to dump based on the
1219 * current connections.
1220 *
1221 * @param targetPkgName the target package name to dump carrier services for
1222 * @param considerCarrierPrivileges if true, allow a carrier service to be dumped if it shares
1223 * carrier privileges with {@code targetPkgName};
1224 * otherwise, only dump a carrier service if it is {@code
1225 * targetPkgName}
1226 */
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001227 private void dumpCarrierServiceIfBound(FileDescriptor fd, IndentingPrintWriter indentPW,
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001228 String prefix, String targetPkgName, boolean considerCarrierPrivileges) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001229 // Null package is possible if it's early in the boot process, there was a recent crash, we
1230 // loaded the config from XML most recently, or a SIM slot is empty. Carrier apps with
1231 // long-lived bindings should typically get dumped here regardless. Even if an app is being
1232 // used for multiple phoneIds, we assume that it's smart enough to handle that on its own,
1233 // and that in most cases we'd just be dumping duplicate information and bloating a report.
1234 indentPW.increaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001235 indentPW.println(prefix + " : " + targetPkgName);
1236 Set<String> dumpedPkgNames = new ArraySet<>(mServiceConnection.length);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001237 for (CarrierServiceConnection connection : mServiceConnection) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001238 if (connection == null || !SubscriptionManager.isValidPhoneId(connection.phoneId)
1239 || TextUtils.isEmpty(connection.pkgName)) {
1240 continue;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001241 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001242 final String servicePkgName = connection.pkgName;
1243 // Note: we intentionally ignore system components here because we should NOT match the
1244 // shell caller that's typically used for bug reports via non-BugreportManager triggers.
1245 final boolean exactPackageMatch = TextUtils.equals(targetPkgName, servicePkgName);
1246 final boolean carrierPrivilegesMatch =
1247 considerCarrierPrivileges && hasCarrierPrivileges(targetPkgName,
1248 connection.phoneId);
1249 if (!exactPackageMatch && !carrierPrivilegesMatch) continue;
1250 // Make sure this service is actually alive before trying to dump it. We don't pay
1251 // attention to mServiceBound[connection.phoneId] because typically carrier apps will
1252 // request long-lived bindings, and even if we unbind the app, it may still be alive due
1253 // to CarrierServiceBindHelper. Pull it out as a reference so even if it gets set to
1254 // null within the ServiceConnection during unbinding we can avoid an NPE.
1255 final IBinder service = connection.service;
1256 if (service == null || !service.isBinderAlive() || !service.pingBinder()) continue;
1257 // We've got a live service. Last check is just to make sure we don't dump a package
1258 // multiple times.
1259 if (!dumpedPkgNames.add(servicePkgName)) continue;
1260 if (!exactPackageMatch) {
1261 logd(targetPkgName + " has carrier privileges on phoneId " + connection.phoneId
1262 + ", service provided by " + servicePkgName);
1263 indentPW.increaseIndent();
1264 indentPW.println("Proxy : " + servicePkgName);
1265 indentPW.decreaseIndent();
1266 }
1267 // Flush before we let the app output anything to ensure correct ordering of output.
1268 // Internally, Binder#dump calls flush on its printer after finishing so we don't
1269 // need to do anything after.
1270 indentPW.flush();
1271 try {
1272 logd("Dumping " + servicePkgName);
1273 // We don't need to give the carrier service any args.
1274 connection.service.dump(fd, null /* args */);
1275 logd("Done with " + servicePkgName);
1276 } catch (RemoteException e) {
1277 logd("RemoteException from " + servicePkgName, e);
1278 indentPW.increaseIndent();
1279 indentPW.println("RemoteException");
1280 indentPW.increaseIndent();
1281 e.printStackTrace(indentPW);
1282 indentPW.decreaseIndent();
1283 indentPW.decreaseIndent();
1284 // We won't retry this package again because now it's in dumpedPkgNames.
1285 }
1286 indentPW.println("");
Jordan Liu46ed5db2020-01-28 08:55:20 -08001287 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001288 if (dumpedPkgNames.isEmpty()) {
1289 indentPW.increaseIndent();
1290 indentPW.println("Not bound");
1291 indentPW.decreaseIndent();
1292 indentPW.println("");
1293 }
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001294 indentPW.decreaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001295 }
1296
1297 private boolean hasCarrierPrivileges(String pkgName, int phoneId) {
1298 int[] subIds = SubscriptionManager.getSubId(phoneId);
1299 if (ArrayUtils.isEmpty(subIds)) {
1300 return false;
1301 }
1302 return TelephonyManager.from(mContext).createForSubscriptionId(
1303 subIds[0]).checkCarrierPrivilegesForPackage(pkgName)
1304 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001305 }
1306
Zach Johnson36d7aab2015-05-22 15:43:00 -07001307 private class CarrierServiceConnection implements ServiceConnection {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001308 final int phoneId;
1309 final String pkgName;
1310 final int eventId;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001311 IBinder service;
1312
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001313 CarrierServiceConnection(int phoneId, String pkgName, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001314 this.phoneId = phoneId;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001315 this.pkgName = pkgName;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001316 this.eventId = eventId;
1317 }
1318
1319 @Override
1320 public void onServiceConnected(ComponentName name, IBinder service) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001321 logd("Connected to config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001322 this.service = service;
1323 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1324 }
1325
1326 @Override
1327 public void onServiceDisconnected(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001328 logd("Disconnected from config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001329 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001330 }
1331
1332 @Override
1333 public void onBindingDied(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001334 logd("Binding died from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001335 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001336 }
1337
1338 @Override
1339 public void onNullBinding(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001340 logd("Null binding from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001341 this.service = null;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001342 }
1343 }
1344
1345 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1346 @Override
1347 public void onReceive(Context context, Intent intent) {
1348 String action = intent.getAction();
Junda Liu8a8a53a2015-05-15 16:19:57 -07001349 boolean replace = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1350 // If replace is true, only care ACTION_PACKAGE_REPLACED.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001351 if (replace && !Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
Junda Liu8a8a53a2015-05-15 16:19:57 -07001352 return;
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001353 }
Junda Liu8a8a53a2015-05-15 16:19:57 -07001354
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001355 switch (action) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001356 case Intent.ACTION_BOOT_COMPLETED:
1357 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1358 break;
1359
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001360 case Intent.ACTION_PACKAGE_ADDED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001361 case Intent.ACTION_PACKAGE_REMOVED:
Junda Liu8a8a53a2015-05-15 16:19:57 -07001362 case Intent.ACTION_PACKAGE_REPLACED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001363 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1364 String packageName = mContext.getPackageManager().getNameForUid(uid);
Junda Liu8a8a53a2015-05-15 16:19:57 -07001365 if (packageName != null) {
1366 // We don't have a phoneId for arg1.
1367 mHandler.sendMessage(
1368 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, packageName));
1369 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001370 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001371 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001372 }
1373 }
1374
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001375 private void logd(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001376 Log.d(LOG_TAG, msg);
1377 }
1378
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001379 private void logd(String msg, Throwable tr) {
1380 Log.d(LOG_TAG, msg, tr);
1381 }
1382
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001383 private void logdWithLocalLog(String msg) {
chen xudb04c292019-03-26 17:01:15 -07001384 Log.d(LOG_TAG, msg);
1385 mCarrierConfigLoadingLog.log(msg);
1386 }
1387
1388 private void loge(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001389 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07001390 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001391 }
1392}