blob: 997f06c23881645ee06eec087495f84d4adaacb7 [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 }
633 // Include subId/carrier id extra only if SIM records are loaded
634 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
635 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
636 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
637 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID,
638 getSpecificCarrierIdForPhoneId(phoneId));
639 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, getCarrierIdForPhoneId(phoneId));
640 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800641 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800642 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Sarah Chin807d5c62020-03-30 17:21:09 -0700643 intent.putExtra(CarrierConfigManager.EXTRA_REBROADCAST_ON_UNLOCK,
644 mFromSystemUnlocked[phoneId]);
Meng Wang97a6a462020-01-23 16:22:16 -0800645 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
Jack Yue9e95be2020-03-22 10:56:06 -0700646 int[] subIds = SubscriptionManager.getSubId(phoneId);
647 if (subIds != null && subIds.length > 0) {
648 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId + ", subId=" + subIds[0]);
649 } else {
650 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
651 }
Junda Liu03aad762017-07-21 13:32:17 -0700652 mHasSentConfigChange[phoneId] = true;
Sarah Chin807d5c62020-03-30 17:21:09 -0700653 mFromSystemUnlocked[phoneId] = false;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800654 }
655
656 /** Binds to the default or carrier config app. */
657 private boolean bindToConfigPackage(String pkgName, int phoneId, int eventId) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800658 logdWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700659 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700660 carrierService.setPackage(pkgName);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800661 mServiceConnection[phoneId] = new CarrierServiceConnection(phoneId, pkgName, eventId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800662 try {
Jordan Liu178430d2020-02-10 14:32:15 -0800663 if (mContext.bindService(carrierService, mServiceConnection[phoneId],
664 Context.BIND_AUTO_CREATE)) {
665 mServiceBound[phoneId] = true;
666 return true;
667 } else {
668 return false;
669 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800670 } catch (SecurityException ex) {
671 return false;
672 }
673 }
674
chen xu02581692018-11-11 19:03:44 -0800675 private CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800676 String mcc = "";
677 String mnc = "";
678 String imsi = "";
679 String gid1 = "";
680 String gid2 = "";
681 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
682 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800683 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800684 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700685 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
686 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800687 mcc = simOperator.substring(0, 3);
688 mnc = simOperator.substring(3);
689 }
690 Phone phone = PhoneFactory.getPhone(phoneId);
691 if (phone != null) {
692 imsi = phone.getSubscriberId();
693 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700694 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800695 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800696 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800697 }
chen xua31f22b2019-03-06 15:28:50 -0800698 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800699 }
700
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700701 /** Returns the package name of a priveleged carrier app, or null if there is none. */
702 private String getCarrierPackageForPhoneId(int phoneId) {
703 List<String> carrierPackageNames = TelephonyManager.from(mContext)
704 .getCarrierPackageNamesForIntentAndPhone(
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700705 new Intent(CarrierService.CARRIER_SERVICE_INTERFACE), phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700706 if (carrierPackageNames != null && carrierPackageNames.size() > 0) {
707 return carrierPackageNames.get(0);
708 } else {
709 return null;
710 }
711 }
712
713 private String getIccIdForPhoneId(int phoneId) {
714 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
715 return null;
716 }
717 Phone phone = PhoneFactory.getPhone(phoneId);
718 if (phone == null) {
719 return null;
720 }
721 return phone.getIccSerialNumber();
722 }
723
chen xu02581692018-11-11 19:03:44 -0800724 /**
chen xua31f22b2019-03-06 15:28:50 -0800725 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800726 */
chen xua31f22b2019-03-06 15:28:50 -0800727 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700728 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800729 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700730 }
731 Phone phone = PhoneFactory.getPhone(phoneId);
732 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800733 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700734 }
chen xua31f22b2019-03-06 15:28:50 -0800735 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800736 }
737
738 /**
739 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
740 */
741 private int getCarrierIdForPhoneId(int phoneId) {
742 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
743 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700744 }
chen xu02581692018-11-11 19:03:44 -0800745 Phone phone = PhoneFactory.getPhone(phoneId);
746 if (phone == null) {
747 return TelephonyManager.UNKNOWN_CARRIER_ID;
748 }
749 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700750 }
751
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700752 /**
753 * Writes a bundle to an XML file.
754 *
chen xu02581692018-11-11 19:03:44 -0800755 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -0800756 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -0800757 * should have a single copy of XML file named after carrier id. However, it's still possible
758 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
759 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
760 * carrier while iccid remains the same.
761 *
762 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
763 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700764 *
765 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700766 *
767 * @param packageName the name of the package from which we fetched this bundle.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100768 * @param extraString An extra string to be used in the XML file name.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800769 * @param phoneId the phone ID.
770 * @param carrierId contains all carrier-identifying information.
771 * @param config the bundle to be written. Null will be treated as an empty bundle.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700772 */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100773 private void saveConfigToXml(String packageName, @NonNull String extraString, int phoneId,
774 CarrierIdentifier carrierId, PersistableBundle config) {
Yuchen Dongc15afed2018-04-26 17:05:22 +0800775 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
776 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700777 loge("Skip save config because SIM records are not loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800778 return;
779 }
780
781 final String iccid = getIccIdForPhoneId(phoneId);
Chen Xucd4a6372019-07-29 16:34:52 -0700782 final int cid = carrierId.getSpecificCarrierId();
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700783 if (packageName == null || iccid == null) {
784 loge("Cannot save config with null packageName or iccid.");
785 return;
786 }
Junda Liu02596502016-11-15 13:46:43 -0800787 // b/32668103 Only save to file if config isn't empty.
788 // In case of failure, not caching an empty bundle will
789 // try loading config again on next power on or sim loaded.
790 // Downside is for genuinely empty bundle, will bind and load
791 // on every power on.
792 if (config == null || config.isEmpty()) {
793 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700794 }
795
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700796 final String version = getPackageVersion(packageName);
797 if (version == null) {
798 loge("Failed to get package version for: " + packageName);
799 return;
800 }
801
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800802 logdWithLocalLog(
803 "Save config to xml, packagename: " + packageName + " phoneId: " + phoneId);
chen xudb04c292019-03-26 17:01:15 -0700804
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700805 FileOutputStream outFile = null;
806 try {
807 outFile = new FileOutputStream(
chen xu02581692018-11-11 19:03:44 -0800808 new File(mContext.getFilesDir(),
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100809 getFilenameForConfig(packageName, extraString, iccid, cid)));
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800810 config.putString(KEY_VERSION, version);
811 config.writeToStream(outFile);
812 outFile.flush();
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700813 outFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800814 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700815 loge(e.toString());
816 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700817 }
818
819 /**
820 * Reads a bundle from an XML file.
821 *
822 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +0800823 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700824 *
825 * In case of errors, or if the saved config is from a different package version than the
826 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700827 *
828 * @param packageName the name of the package from which we fetched this bundle.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100829 * @param extraString An extra string to be used in the XML file name.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800830 * @param phoneId the phone ID.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700831 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800832 * version does not match, or reading config fails.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700833 */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100834 private PersistableBundle restoreConfigFromXml(String packageName, @NonNull String extraString,
835 int phoneId) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700836 final String version = getPackageVersion(packageName);
837 if (version == null) {
838 loge("Failed to get package version for: " + packageName);
839 return null;
840 }
Yuchen Dongc15afed2018-04-26 17:05:22 +0800841 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
842 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700843 loge("Skip restoring config because SIM records are not yet loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800844 return null;
845 }
846
847 final String iccid = getIccIdForPhoneId(phoneId);
chen xua31f22b2019-03-06 15:28:50 -0800848 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700849 if (packageName == null || iccid == null) {
850 loge("Cannot restore config with null packageName or iccid.");
851 return null;
852 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700853
854 PersistableBundle restoredBundle = null;
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100855 File file = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700856 FileInputStream inFile = null;
857 try {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100858 file = new File(mContext.getFilesDir(),
859 getFilenameForConfig(packageName, extraString, iccid, cid));
860 inFile = new FileInputStream(file);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700861
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800862 restoredBundle = PersistableBundle.readFromStream(inFile);
863 String savedVersion = restoredBundle.getString(KEY_VERSION);
864 restoredBundle.remove(KEY_VERSION);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700865
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800866 if (!version.equals(savedVersion)) {
867 loge("Saved version mismatch: " + version + " vs " + savedVersion);
868 restoredBundle = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700869 }
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800870
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700871 inFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800872 } catch (FileNotFoundException e) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100873 // Missing file is normal occurrence that might occur with a new sim or when restoring
874 // an override file during boot and should not be treated as an error.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800875 if (file != null) logd("File not found: " + file.getPath());
876 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700877 loge(e.toString());
878 }
879
880 return restoredBundle;
881 }
882
Junda Liu8a8a53a2015-05-15 16:19:57 -0700883 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700884 * Clears cached carrier config.
885 * This deletes all saved XML files associated with the given package name. If packageName is
886 * null, then it deletes all saved XML files.
887 *
888 * @param packageName the name of a carrier package, or null if all cached config should be
889 * cleared.
890 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -0700891 */
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700892 private boolean clearCachedConfigForPackage(final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700893 File dir = mContext.getFilesDir();
894 File[] packageFiles = dir.listFiles(new FilenameFilter() {
895 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700896 if (packageName != null) {
897 return filename.startsWith("carrierconfig-" + packageName + "-");
898 } else {
899 return filename.startsWith("carrierconfig-");
900 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700901 }
902 });
Junda Liu8a8a53a2015-05-15 16:19:57 -0700903 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700904 for (File f : packageFiles) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800905 logd("Deleting " + f.getName());
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700906 f.delete();
907 }
Junda Liu8a8a53a2015-05-15 16:19:57 -0700908 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700909 }
910
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700911 /** Builds a canonical file name for a config file. */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100912 private String getFilenameForConfig(@NonNull String packageName, @NonNull String extraString,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800913 @NonNull String iccid, int cid) {
chen xu02581692018-11-11 19:03:44 -0800914 // the same carrier should have a single copy of XML file named after carrier id.
915 // However, it's still possible that platform doesn't recognize the current sim carrier,
916 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
917 // cases SIM OTA resolves to different carrier while iccid remains the same.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100918 return "carrierconfig-" + packageName + extraString + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700919 }
920
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700921 /** Return the current version code of a package, or null if the name is not found. */
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700922 private String getPackageVersion(String packageName) {
923 try {
924 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -0800925 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700926 } catch (PackageManager.NameNotFoundException e) {
927 return null;
928 }
929 }
930
Jonathan Basseri65273c82017-07-25 15:08:42 -0700931 /**
932 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700933 *
934 * This reads config bundles for the given phoneId. That means getting the latest bundle from
935 * the default app and a privileged carrier app, if present. This will not bind to an app if we
936 * have a saved config file to use instead.
937 */
938 private void updateConfigForPhoneId(int phoneId) {
Junda Liu8a8a53a2015-05-15 16:19:57 -0700939 // Clear in-memory cache for carrier app config, so when carrier app gets uninstalled, no
940 // stale config is left.
941 if (mConfigFromCarrierApp[phoneId] != null &&
942 getCarrierPackageForPhoneId(phoneId) == null) {
943 mConfigFromCarrierApp[phoneId] = null;
944 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700945 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700946 }
947
Malcolm Chen5ea18532019-10-24 19:55:44 -0700948 private void onMultiSimConfigChanged() {
949 for (int i = TelephonyManager.from(mContext).getActiveModemCount();
950 i < mConfigFromDefaultApp.length; i++) {
951 clearConfigForPhone(i, false);
952 }
953 }
954
Hall Liu506724b2018-10-22 18:16:14 -0700955 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800956 @NonNull
957 public PersistableBundle getConfigForSubId(int subId, String callingPackage) {
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -0800958 return getConfigForSubIdWithFeature(subId, callingPackage, null);
959 }
960
961 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800962 @NonNull
963 public PersistableBundle getConfigForSubIdWithFeature(int subId, String callingPackage,
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -0800964 String callingFeatureId) {
965 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subId, callingPackage,
966 callingFeatureId, "getCarrierConfig")) {
Malcolm Chen6d3d6b32018-03-01 13:58:03 -0800967 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -0700968 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -0700969
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800970 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700971 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800972 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700973 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800974 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700975 retConfig.putAll(config);
yinxud0104832019-08-16 14:20:08 -0700976 if (getCarrierPackageForPhoneId(phoneId) == null) {
977 retConfig.putBoolean(
978 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
979 }
yinxuc5e27622017-11-29 15:08:50 -0800980 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800981 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800982 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700983 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -0800984 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
985 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100986 config = mPersistentOverrideConfigs[phoneId];
987 if (config != null) {
988 retConfig.putAll(config);
989 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
990 }
Hall Liu506724b2018-10-22 18:16:14 -0700991 config = mOverrideConfigs[phoneId];
992 if (config != null) {
993 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -0700994 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800995 }
996 return retConfig;
997 }
998
999 @Override
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001000 public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides,
1001 boolean persistent) {
Hall Liu506724b2018-10-22 18:16:14 -07001002 mContext.enforceCallingOrSelfPermission(
1003 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Brad Ebingerf6281ad2019-04-25 11:02:04 -07001004 //TODO: Also check for SHELL UID to restrict this method to testing only (b/131326259)
Hall Liu506724b2018-10-22 18:16:14 -07001005 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1006 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001007 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
Hall Liu506724b2018-10-22 18:16:14 -07001008 return;
1009 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001010 overrideConfig(mOverrideConfigs, phoneId, overrides);
Hall Liu506724b2018-10-22 18:16:14 -07001011
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001012 if (persistent) {
1013 overrideConfig(mPersistentOverrideConfigs, phoneId, overrides);
1014
1015 if (overrides != null) {
1016 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
1017 saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId,
1018 carrierId, mPersistentOverrideConfigs[phoneId]);
1019 } else {
1020 final String iccid = getIccIdForPhoneId(phoneId);
1021 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1022 String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage,
1023 OVERRIDE_PACKAGE_ADDITION, iccid, cid);
1024 File fileToDelete = new File(mContext.getFilesDir(), fileName);
1025 fileToDelete.delete();
1026 }
Hall Liu506724b2018-10-22 18:16:14 -07001027 }
Brad Ebingerc9d1fa12019-04-17 15:21:18 -07001028 notifySubscriptionInfoUpdater(phoneId);
Hall Liu506724b2018-10-22 18:16:14 -07001029 }
1030
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001031 private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId,
1032 @Nullable PersistableBundle overrides) {
1033 if (overrides == null) {
1034 currentOverrides[phoneId] = new PersistableBundle();
1035 } else if (currentOverrides[phoneId] == null) {
1036 currentOverrides[phoneId] = overrides;
1037 } else {
1038 currentOverrides[phoneId].putAll(overrides);
1039 }
1040 }
1041
Hall Liu506724b2018-10-22 18:16:14 -07001042 @Override
Jonathan Basseri86030352015-06-08 12:27:56 -07001043 public void notifyConfigChangedForSubId(int subId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001044 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001045 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001046 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001047 return;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001048 }
Jordan Liud7d57fe2019-04-16 12:29:43 -07001049
1050 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001051 // system privileged app with MODIFY_PHONE_STATE permission.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001052 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext, subId,
1053 "Require carrier privileges or MODIFY_PHONE_STATE permission.");
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001054
1055 // This method should block until deleting has completed, so that an error which prevents us
1056 // from clearing the cache is passed back to the carrier app. With the files successfully
1057 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001058 String callingPackageName = mContext.getPackageManager().getNameForUid(
1059 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001060 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001061 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001062 }
1063
1064 @Override
1065 public void updateConfigForPhoneId(int phoneId, String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -07001066 mContext.enforceCallingOrSelfPermission(
1067 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001068 logdWithLocalLog("Update config for phoneId: " + phoneId + " simState: " + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001069 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1070 return;
1071 }
1072 // requires Java 7 for switch on string.
1073 switch (simState) {
1074 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1075 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001076 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001077 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Malcolm Chendb66de12019-12-09 18:57:07 -08001078 case IccCardConstants.INTENT_VALUE_ICC_NOT_READY:
Junda Liu9f2d2712015-05-15 14:22:45 -07001079 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001080 break;
1081 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1082 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001083 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001084 break;
1085 }
1086 }
1087
Junda Liu43d723a2015-05-12 17:23:45 -07001088 @Override
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001089 public String getDefaultCarrierServicePackageName() {
Shuo Qianefdb7962019-12-19 15:54:27 -08001090 mContext.enforceCallingOrSelfPermission(
1091 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1092 "getDefaultCarrierServicePackageName");
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001093 return mPlatformCarrierConfigPackage;
1094 }
1095
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001096 private void unbindIfBound(Context context, CarrierServiceConnection conn,
1097 int phoneId) {
1098 if (mServiceBound[phoneId]) {
1099 mServiceBound[phoneId] = false;
1100 context.unbindService(conn);
1101 }
1102 }
1103
1104 /**
1105 * If {@code args} contains {@link #DUMP_ARG_REQUESTING_PACKAGE} and a following package name,
1106 * we'll also call {@link IBinder#dump} on the default carrier service (if bound) and the
1107 * specified carrier service (if bound). Typically, this is done for connectivity bug reports
1108 * where we don't call {@code dumpsys activity service all-non-platform} because that contains
1109 * too much info, but we still want to let carrier apps include their diagnostics.
1110 */
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001111 @Override
Junda Liu43d723a2015-05-12 17:23:45 -07001112 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001113 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
Junda Liu43d723a2015-05-12 17:23:45 -07001114 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1115 != PackageManager.PERMISSION_GRANTED) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001116 indentPW.println("Permission Denial: can't dump carrierconfig from from pid="
Junda Liu43d723a2015-05-12 17:23:45 -07001117 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1118 return;
1119 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001120 String requestingPackage = null;
1121 int requestingPackageIndex = ArrayUtils.indexOf(args, DUMP_ARG_REQUESTING_PACKAGE);
1122 if (requestingPackageIndex >= 0 && requestingPackageIndex < args.length - 1
1123 && !TextUtils.isEmpty(args[requestingPackageIndex + 1])) {
1124 requestingPackage = args[requestingPackageIndex + 1];
1125 // Throws a SecurityException if the caller is impersonating another app in an effort to
1126 // dump extra info (which may contain PII the caller doesn't have a right to).
1127 enforceCallerIsSystemOrRequestingPackage(requestingPackage);
1128 }
1129
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001130 indentPW.println("CarrierConfigLoader: " + this);
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001131 for (int i = 0; i < TelephonyManager.from(mContext).getActiveModemCount(); i++) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001132 indentPW.println("Phone Id = " + i);
shuoq26a3a4c2016-12-16 11:06:48 -08001133 // display default values in CarrierConfigManager
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001134 printConfig(CarrierConfigManager.getDefaultConfig(), indentPW,
shuoq26a3a4c2016-12-16 11:06:48 -08001135 "Default Values from CarrierConfigManager");
shuoq26a3a4c2016-12-16 11:06:48 -08001136 // display ConfigFromDefaultApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001137 printConfig(mConfigFromDefaultApp[i], indentPW, "mConfigFromDefaultApp");
shuoq26a3a4c2016-12-16 11:06:48 -08001138 // display ConfigFromCarrierApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001139 printConfig(mConfigFromCarrierApp[i], indentPW, "mConfigFromCarrierApp");
1140 printConfig(mPersistentOverrideConfigs[i], indentPW, "mPersistentOverrideConfigs");
1141 printConfig(mOverrideConfigs[i], indentPW, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001142 }
chen xudb04c292019-03-26 17:01:15 -07001143
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001144 indentPW.println("CarrierConfigLoadingLog=");
1145 mCarrierConfigLoadingLog.dump(fd, indentPW, args);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001146
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001147 if (requestingPackage != null) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001148 logd("Including default and requesting package " + requestingPackage
1149 + " carrier services in dump");
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001150 indentPW.println("");
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001151 indentPW.println("Connected services");
1152 dumpCarrierServiceIfBound(fd, indentPW, "Default config package",
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001153 mPlatformCarrierConfigPackage, false /* considerCarrierPrivileges */);
1154 dumpCarrierServiceIfBound(fd, indentPW, "Requesting package", requestingPackage,
1155 true /* considerCarrierPrivileges */);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001156 }
shuoq26a3a4c2016-12-16 11:06:48 -08001157 }
1158
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001159 private void printConfig(PersistableBundle configApp, IndentingPrintWriter indentPW,
1160 String name) {
1161 indentPW.increaseIndent();
shuoq26a3a4c2016-12-16 11:06:48 -08001162 if (configApp == null) {
shuoq26a3a4c2016-12-16 11:06:48 -08001163 indentPW.println(name + " : null ");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001164 indentPW.decreaseIndent();
1165 indentPW.println("");
shuoq26a3a4c2016-12-16 11:06:48 -08001166 return;
1167 }
shuoq26a3a4c2016-12-16 11:06:48 -08001168 indentPW.println(name + " : ");
1169 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1170 Collections.sort(sortedKeys);
1171 indentPW.increaseIndent();
1172 indentPW.increaseIndent();
1173 for (String key : sortedKeys) {
1174 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1175 indentPW.println(key + " = " +
1176 Arrays.toString((Object[]) configApp.get(key)));
1177 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1178 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1179 } else {
1180 indentPW.println(key + " = " + configApp.get(key));
1181 }
Junda Liu43d723a2015-05-12 17:23:45 -07001182 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001183 indentPW.decreaseIndent();
1184 indentPW.decreaseIndent();
1185 indentPW.decreaseIndent();
1186 indentPW.println("");
Junda Liu43d723a2015-05-12 17:23:45 -07001187 }
1188
Hunter Knepshielde601f432020-02-19 10:16:04 -08001189 /**
1190 * Passes without problem when one of these conditions is true:
1191 * - The caller is a privileged UID (e.g. for dumpstate.cpp generating a bug report, where the
1192 * system knows the true caller plumbed in through the {@link android.os.BugreportManager} API).
1193 * - The caller's UID matches the supplied package.
1194 *
1195 * @throws SecurityException if none of the above conditions are met.
1196 */
1197 private void enforceCallerIsSystemOrRequestingPackage(String requestingPackage)
1198 throws SecurityException {
1199 final int callingUid = Binder.getCallingUid();
1200 if (callingUid == Process.ROOT_UID || callingUid == Process.SYSTEM_UID
1201 || callingUid == Process.SHELL_UID || callingUid == Process.PHONE_UID) {
1202 // Bug reports (dumpstate.cpp) run as SHELL, and let some other privileged UIDs through
1203 // as well.
1204 return;
1205 }
1206 // An app is trying to dump extra detail, block it if they aren't who they claim to be.
1207 AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class);
1208 if (appOps == null) {
1209 throw new SecurityException("No AppOps");
1210 }
1211 // Will throw a SecurityException if the UID and package don't match.
1212 appOps.checkPackage(callingUid, requestingPackage);
1213 }
1214
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001215 /**
1216 * Searches for one or more appropriate {@link CarrierService} instances to dump based on the
1217 * current connections.
1218 *
1219 * @param targetPkgName the target package name to dump carrier services for
1220 * @param considerCarrierPrivileges if true, allow a carrier service to be dumped if it shares
1221 * carrier privileges with {@code targetPkgName};
1222 * otherwise, only dump a carrier service if it is {@code
1223 * targetPkgName}
1224 */
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001225 private void dumpCarrierServiceIfBound(FileDescriptor fd, IndentingPrintWriter indentPW,
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001226 String prefix, String targetPkgName, boolean considerCarrierPrivileges) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001227 // Null package is possible if it's early in the boot process, there was a recent crash, we
1228 // loaded the config from XML most recently, or a SIM slot is empty. Carrier apps with
1229 // long-lived bindings should typically get dumped here regardless. Even if an app is being
1230 // used for multiple phoneIds, we assume that it's smart enough to handle that on its own,
1231 // and that in most cases we'd just be dumping duplicate information and bloating a report.
1232 indentPW.increaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001233 indentPW.println(prefix + " : " + targetPkgName);
1234 Set<String> dumpedPkgNames = new ArraySet<>(mServiceConnection.length);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001235 for (CarrierServiceConnection connection : mServiceConnection) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001236 if (connection == null || !SubscriptionManager.isValidPhoneId(connection.phoneId)
1237 || TextUtils.isEmpty(connection.pkgName)) {
1238 continue;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001239 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001240 final String servicePkgName = connection.pkgName;
1241 // Note: we intentionally ignore system components here because we should NOT match the
1242 // shell caller that's typically used for bug reports via non-BugreportManager triggers.
1243 final boolean exactPackageMatch = TextUtils.equals(targetPkgName, servicePkgName);
1244 final boolean carrierPrivilegesMatch =
1245 considerCarrierPrivileges && hasCarrierPrivileges(targetPkgName,
1246 connection.phoneId);
1247 if (!exactPackageMatch && !carrierPrivilegesMatch) continue;
1248 // Make sure this service is actually alive before trying to dump it. We don't pay
1249 // attention to mServiceBound[connection.phoneId] because typically carrier apps will
1250 // request long-lived bindings, and even if we unbind the app, it may still be alive due
1251 // to CarrierServiceBindHelper. Pull it out as a reference so even if it gets set to
1252 // null within the ServiceConnection during unbinding we can avoid an NPE.
1253 final IBinder service = connection.service;
1254 if (service == null || !service.isBinderAlive() || !service.pingBinder()) continue;
1255 // We've got a live service. Last check is just to make sure we don't dump a package
1256 // multiple times.
1257 if (!dumpedPkgNames.add(servicePkgName)) continue;
1258 if (!exactPackageMatch) {
1259 logd(targetPkgName + " has carrier privileges on phoneId " + connection.phoneId
1260 + ", service provided by " + servicePkgName);
1261 indentPW.increaseIndent();
1262 indentPW.println("Proxy : " + servicePkgName);
1263 indentPW.decreaseIndent();
1264 }
1265 // Flush before we let the app output anything to ensure correct ordering of output.
1266 // Internally, Binder#dump calls flush on its printer after finishing so we don't
1267 // need to do anything after.
1268 indentPW.flush();
1269 try {
1270 logd("Dumping " + servicePkgName);
1271 // We don't need to give the carrier service any args.
1272 connection.service.dump(fd, null /* args */);
1273 logd("Done with " + servicePkgName);
1274 } catch (RemoteException e) {
1275 logd("RemoteException from " + servicePkgName, e);
1276 indentPW.increaseIndent();
1277 indentPW.println("RemoteException");
1278 indentPW.increaseIndent();
1279 e.printStackTrace(indentPW);
1280 indentPW.decreaseIndent();
1281 indentPW.decreaseIndent();
1282 // We won't retry this package again because now it's in dumpedPkgNames.
1283 }
1284 indentPW.println("");
Jordan Liu46ed5db2020-01-28 08:55:20 -08001285 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001286 if (dumpedPkgNames.isEmpty()) {
1287 indentPW.increaseIndent();
1288 indentPW.println("Not bound");
1289 indentPW.decreaseIndent();
1290 indentPW.println("");
1291 }
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001292 indentPW.decreaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001293 }
1294
1295 private boolean hasCarrierPrivileges(String pkgName, int phoneId) {
1296 int[] subIds = SubscriptionManager.getSubId(phoneId);
1297 if (ArrayUtils.isEmpty(subIds)) {
1298 return false;
1299 }
1300 return TelephonyManager.from(mContext).createForSubscriptionId(
1301 subIds[0]).checkCarrierPrivilegesForPackage(pkgName)
1302 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001303 }
1304
Zach Johnson36d7aab2015-05-22 15:43:00 -07001305 private class CarrierServiceConnection implements ServiceConnection {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001306 final int phoneId;
1307 final String pkgName;
1308 final int eventId;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001309 IBinder service;
1310
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001311 CarrierServiceConnection(int phoneId, String pkgName, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001312 this.phoneId = phoneId;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001313 this.pkgName = pkgName;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001314 this.eventId = eventId;
1315 }
1316
1317 @Override
1318 public void onServiceConnected(ComponentName name, IBinder service) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001319 logd("Connected to config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001320 this.service = service;
1321 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1322 }
1323
1324 @Override
1325 public void onServiceDisconnected(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001326 logd("Disconnected from config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001327 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001328 }
1329
1330 @Override
1331 public void onBindingDied(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001332 logd("Binding died from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001333 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001334 }
1335
1336 @Override
1337 public void onNullBinding(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001338 logd("Null binding from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001339 this.service = null;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001340 }
1341 }
1342
1343 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1344 @Override
1345 public void onReceive(Context context, Intent intent) {
1346 String action = intent.getAction();
Junda Liu8a8a53a2015-05-15 16:19:57 -07001347 boolean replace = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1348 // If replace is true, only care ACTION_PACKAGE_REPLACED.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001349 if (replace && !Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
Junda Liu8a8a53a2015-05-15 16:19:57 -07001350 return;
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001351 }
Junda Liu8a8a53a2015-05-15 16:19:57 -07001352
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001353 switch (action) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001354 case Intent.ACTION_BOOT_COMPLETED:
1355 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1356 break;
1357
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001358 case Intent.ACTION_PACKAGE_ADDED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001359 case Intent.ACTION_PACKAGE_REMOVED:
Junda Liu8a8a53a2015-05-15 16:19:57 -07001360 case Intent.ACTION_PACKAGE_REPLACED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001361 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1362 String packageName = mContext.getPackageManager().getNameForUid(uid);
Junda Liu8a8a53a2015-05-15 16:19:57 -07001363 if (packageName != null) {
1364 // We don't have a phoneId for arg1.
1365 mHandler.sendMessage(
1366 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, packageName));
1367 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001368 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001369 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001370 }
1371 }
1372
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001373 private void logd(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001374 Log.d(LOG_TAG, msg);
1375 }
1376
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001377 private void logd(String msg, Throwable tr) {
1378 Log.d(LOG_TAG, msg, tr);
1379 }
1380
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001381 private void logdWithLocalLog(String msg) {
chen xudb04c292019-03-26 17:01:15 -07001382 Log.d(LOG_TAG, msg);
1383 mCarrierConfigLoadingLog.log(msg);
1384 }
1385
1386 private void loge(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001387 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07001388 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001389 }
1390}