blob: b969ce4da4c9c4423ab92fe2a6fe6ac8dbc5516b [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;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070045import android.preference.PreferenceManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080046import android.service.carrier.CarrierIdentifier;
Zach Johnson36d7aab2015-05-22 15:43:00 -070047import android.service.carrier.CarrierService;
48import android.service.carrier.ICarrierService;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080049import android.telephony.CarrierConfigManager;
50import android.telephony.SubscriptionManager;
51import android.telephony.TelephonyManager;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -080052import android.text.TextUtils;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080053import android.util.ArraySet;
chen xudb04c292019-03-26 17:01:15 -070054import android.util.LocalLog;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080055import android.util.Log;
56
57import com.android.internal.telephony.ICarrierConfigLoader;
58import com.android.internal.telephony.IccCardConstants;
59import com.android.internal.telephony.Phone;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080060import com.android.internal.telephony.PhoneFactory;
Nathan Harold48ac0972019-03-13 22:33:01 -070061import com.android.internal.telephony.SubscriptionInfoUpdater;
Jeff Davidsona8e4e242018-03-15 17:16:18 -070062import com.android.internal.telephony.TelephonyPermissions;
Meng Wanga320b942019-11-25 11:21:26 -080063import com.android.internal.telephony.util.ArrayUtils;
shuoq26a3a4c2016-12-16 11:06:48 -080064import com.android.internal.util.IndentingPrintWriter;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080065
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070066import java.io.File;
Junda Liu43d723a2015-05-12 17:23:45 -070067import java.io.FileDescriptor;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070068import java.io.FileInputStream;
69import java.io.FileNotFoundException;
70import java.io.FileOutputStream;
Jonathan Basseri1f743c92015-05-15 00:19:46 -070071import java.io.FilenameFilter;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070072import java.io.IOException;
Junda Liu43d723a2015-05-12 17:23:45 -070073import java.io.PrintWriter;
shuoq26a3a4c2016-12-16 11:06:48 -080074import java.util.ArrayList;
75import java.util.Arrays;
76import java.util.Collections;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080077import java.util.List;
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -080078import java.util.Set;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080079
80/**
81 * CarrierConfigLoader binds to privileged carrier apps to fetch carrier config overlays.
Jonathan Basseri6465afd2015-02-25 13:05:57 -080082 */
Jonathan Basseri6465afd2015-02-25 13:05:57 -080083public class CarrierConfigLoader extends ICarrierConfigLoader.Stub {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070084 private static final String LOG_TAG = "CarrierConfigLoader";
Meng Wang33ad2bc2017-03-16 20:21:20 -070085
86 // Package name for platform carrier config app, bundled with system image.
87 private final String mPlatformCarrierConfigPackage;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080088
89 /** The singleton instance. */
90 private static CarrierConfigLoader sInstance;
91 // The context for phone app, passed from PhoneGlobals.
92 private Context mContext;
93 // Carrier configs from default app, indexed by phoneID.
Jonathan Basseric31f1f32015-05-12 10:13:03 -070094 private PersistableBundle[] mConfigFromDefaultApp;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080095 // Carrier configs from privileged carrier config app, indexed by phoneID.
Jonathan Basseric31f1f32015-05-12 10:13:03 -070096 private PersistableBundle[] mConfigFromCarrierApp;
Torbjorn Eklund723527a2019-02-13 11:16:25 +010097 // Persistent Carrier configs that are provided via the override test API, indexed by phone ID.
98 private PersistableBundle[] mPersistentOverrideConfigs;
Hall Liu506724b2018-10-22 18:16:14 -070099 // Carrier configs that are provided via the override test API, indexed by phone ID.
100 private PersistableBundle[] mOverrideConfigs;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800101 // Service connection for binding to config app.
Zach Johnson36d7aab2015-05-22 15:43:00 -0700102 private CarrierServiceConnection[] mServiceConnection;
Jordan Liu178430d2020-02-10 14:32:15 -0800103 // Whether we are bound to a service for each phone
104 private boolean[] mServiceBound;
Junda Liu03aad762017-07-21 13:32:17 -0700105 // Whether we have sent config change bcast for each phone id.
106 private boolean[] mHasSentConfigChange;
Nathan Harold48ac0972019-03-13 22:33:01 -0700107 // SubscriptionInfoUpdater
108 private final SubscriptionInfoUpdater mSubscriptionInfoUpdater;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800109
Nanxi Chen3d670502016-03-17 16:32:09 -0700110 // Broadcast receiver for Boot intents, register intent filter in construtor.
111 private final BroadcastReceiver mBootReceiver = new ConfigLoaderBroadcastReceiver();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800112 // Broadcast receiver for SIM and pkg intents, register intent filter in constructor.
Nanxi Chen3d670502016-03-17 16:32:09 -0700113 private final BroadcastReceiver mPackageReceiver = new ConfigLoaderBroadcastReceiver();
Jack Yu67663de2019-10-17 15:19:48 -0700114 private final LocalLog mCarrierConfigLoadingLog = new LocalLog(100);
chen xudb04c292019-03-26 17:01:15 -0700115
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800116
117 // Message codes; see mHandler below.
118 // Request from SubscriptionInfoUpdater when SIM becomes absent or error.
119 private static final int EVENT_CLEAR_CONFIG = 0;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800120 // Has connected to default app.
121 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
122 // Has connected to carrier app.
123 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700124 // Config has been loaded from default app (or cache).
125 private static final int EVENT_FETCH_DEFAULT_DONE = 5;
126 // Config has been loaded from carrier app (or cache).
127 private static final int EVENT_FETCH_CARRIER_DONE = 6;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700128 // Attempt to fetch from default app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700129 private static final int EVENT_DO_FETCH_DEFAULT = 7;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700130 // Attempt to fetch from carrier app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700131 private static final int EVENT_DO_FETCH_CARRIER = 8;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700132 // A package has been installed, uninstalled, or updated.
133 private static final int EVENT_PACKAGE_CHANGED = 9;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700134 // Bind timed out for the default app.
135 private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
136 // Bind timed out for a carrier app.
137 private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700138 // Check if the system fingerprint has changed.
139 private static final int EVENT_CHECK_SYSTEM_UPDATE = 12;
Nanxi Chen3d670502016-03-17 16:32:09 -0700140 // Rerun carrier config binding after system is unlocked.
141 private static final int EVENT_SYSTEM_UNLOCKED = 13;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700142 // Fetching config timed out from the default app.
143 private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14;
144 // Fetching config timed out from a carrier app.
145 private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15;
Nathan Harold48ac0972019-03-13 22:33:01 -0700146 // SubscriptionInfoUpdater has finished updating the sub for the carrier config.
147 private static final int EVENT_SUBSCRIPTION_INFO_UPDATED = 16;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700148 // Multi-SIM config changed.
149 private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 17;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700150
Junda Liu6cb45002016-03-22 17:18:20 -0700151 private static final int BIND_TIMEOUT_MILLIS = 30000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800152
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800153 // Keys used for saving and restoring config bundle from file.
154 private static final String KEY_VERSION = "__carrier_config_package_version__";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800155
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100156 private static final String OVERRIDE_PACKAGE_ADDITION = "-override";
157
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700158 // SharedPreferences key for last known build fingerprint.
159 private static final String KEY_FINGERPRINT = "build_fingerprint";
160
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800161 // Argument for #dump that indicates we should also call the default and specified carrier
162 // service's #dump method. In multi-SIM devices, it's possible that carrier A is on SIM 1 and
163 // carrier B is on SIM 2, in which case we should not dump carrier B's service when carrier A
164 // requested the dump.
165 private static final String DUMP_ARG_REQUESTING_PACKAGE = "--requesting-package";
166
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800167 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700168 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700169 // For each phoneId, the event sequence should be:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700170 // fetch default, connected to default, fetch default (async), fetch default done,
171 // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700172 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700173 // If there is a saved config file for either the default app or the carrier app, we skip
174 // binding to the app and go straight from fetch to loaded.
175 //
176 // At any time, at most one connection is active. If events are not in this order, previous
177 // connection will be unbound, so only latest event takes effect.
178 //
179 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
180 // 1. loading from carrier app (even if read from a file)
181 // 2. loading from default app if there is no carrier app (even if read from a file)
182 // 3. clearing config (e.g. due to sim removal)
183 // 4. encountering bind or IPC error
Jonathan Basseri65273c82017-07-25 15:08:42 -0700184 private class ConfigHandler extends Handler {
185 @Override
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800186 public void handleMessage(Message msg) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700187 final int phoneId = msg.arg1;
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800188 logdWithLocalLog("mHandler: " + msg.what + " phoneId: " + phoneId);
Malcolm Chen5ea18532019-10-24 19:55:44 -0700189 if (!SubscriptionManager.isValidPhoneId(phoneId)
190 && msg.what != EVENT_MULTI_SIM_CONFIG_CHANGED) {
191 return;
192 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800193 switch (msg.what) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800194 case EVENT_CLEAR_CONFIG: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700195 clearConfigForPhone(phoneId, true);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800196 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700197 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700198
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800199 case EVENT_SYSTEM_UNLOCKED: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700200 for (int i = 0; i < TelephonyManager.from(mContext).getActiveModemCount();
201 ++i) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700202 // When user unlock device, we should only try to send broadcast again if we
203 // have sent it before unlock. This will avoid we try to load carrier config
204 // when SIM is still loading when unlock happens.
Junda Liu03aad762017-07-21 13:32:17 -0700205 if (mHasSentConfigChange[i]) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800206 logdWithLocalLog("System unlocked");
Junda Liu03aad762017-07-21 13:32:17 -0700207 updateConfigForPhoneId(i);
208 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700209 }
210 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700211 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700212
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800213 case EVENT_PACKAGE_CHANGED: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700214 final String carrierPackageName = (String) msg.obj;
215 // Only update if there are cached config removed to avoid updating config for
216 // unrelated packages.
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700217 if (clearCachedConfigForPackage(carrierPackageName)) {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700218 int numPhones = TelephonyManager.from(mContext).getActiveModemCount();
Junda Liu8a8a53a2015-05-15 16:19:57 -0700219 for (int i = 0; i < numPhones; ++i) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800220 logdWithLocalLog("Package changed: " + carrierPackageName
Jack Yu67663de2019-10-17 15:19:48 -0700221 + ", phone=" + i);
Junda Liu8a8a53a2015-05-15 16:19:57 -0700222 updateConfigForPhoneId(i);
223 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700224 }
225 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700226 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700227
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800228 case EVENT_DO_FETCH_DEFAULT: {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100229 // Restore persistent override values.
230 PersistableBundle config = restoreConfigFromXml(
231 mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId);
232 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800233 logd("Loaded persistent override config from XML. package="
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100234 + mPlatformCarrierConfigPackage
235 + " phoneId=" + phoneId);
236 mPersistentOverrideConfigs[phoneId] = config;
237 }
238
239 config = restoreConfigFromXml(mPlatformCarrierConfigPackage, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700240 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800241 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700242 "Loaded config from XML. package="
243 + mPlatformCarrierConfigPackage
244 + " phoneId="
245 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700246 mConfigFromDefaultApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700247 Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700248 newMsg.getData().putBoolean("loaded_from_xml", true);
249 mHandler.sendMessage(newMsg);
250 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700251 // No cached config, so fetch it from the default app.
252 if (bindToConfigPackage(
253 mPlatformCarrierConfigPackage,
254 phoneId,
255 EVENT_CONNECTED_TO_DEFAULT)) {
256 sendMessageDelayed(
257 obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700258 BIND_TIMEOUT_MILLIS);
259 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700260 // Send broadcast if bind fails.
Nathan Harold48ac0972019-03-13 22:33:01 -0700261 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700262 // TODO: We *must* call unbindService even if bindService returns false.
263 // (And possibly if SecurityException was thrown.)
chen xudb04c292019-03-26 17:01:15 -0700264 loge("binding to default app: "
265 + mPlatformCarrierConfigPackage + " fails");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700266 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800267 }
268 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700269 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800270
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800271 case EVENT_CONNECTED_TO_DEFAULT: {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700272 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700273 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800274 // If new service connection has been created, unbind.
275 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Jordan Liu178430d2020-02-10 14:32:15 -0800276 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800277 break;
278 }
chen xu02581692018-11-11 19:03:44 -0800279 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700280 // ResultReceiver callback will execute in this Handler's thread.
281 final ResultReceiver resultReceiver =
282 new ResultReceiver(this) {
283 @Override
284 public void onReceiveResult(int resultCode, Bundle resultData) {
Jordan Liu178430d2020-02-10 14:32:15 -0800285 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700286 // If new service connection has been created, this is stale.
287 if (mServiceConnection[phoneId] != conn) {
288 loge("Received response for stale request.");
289 return;
290 }
291 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
292 if (resultCode == RESULT_ERROR || resultData == null) {
293 // On error, abort config fetching.
294 loge("Failed to get carrier config");
Nathan Harold48ac0972019-03-13 22:33:01 -0700295 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700296 return;
297 }
298 PersistableBundle config =
299 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100300 saveConfigToXml(mPlatformCarrierConfigPackage, "", phoneId,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800301 carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700302 mConfigFromDefaultApp[phoneId] = config;
303 sendMessage(
304 obtainMessage(
305 EVENT_FETCH_DEFAULT_DONE, phoneId, -1));
306 }
307 };
308 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800309 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700310 ICarrierService carrierService =
311 ICarrierService.Stub.asInterface(conn.service);
312 carrierService.getCarrierConfig(carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800313 logdWithLocalLog("Fetch config for default app: "
chen xudb04c292019-03-26 17:01:15 -0700314 + mPlatformCarrierConfigPackage
315 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700316 } catch (RemoteException e) {
chen xudb04c292019-03-26 17:01:15 -0700317 loge("Failed to get carrier config from default app: " +
318 mPlatformCarrierConfigPackage + " err: " + e.toString());
Jordan Liu178430d2020-02-10 14:32:15 -0800319 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700320 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800321 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700322 sendMessageDelayed(
323 obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1),
324 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800325 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700326 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800327
Jonathan Basseri930701e2015-06-11 20:56:43 -0700328 case EVENT_BIND_DEFAULT_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800329 case EVENT_FETCH_DEFAULT_TIMEOUT: {
330 loge("Bind/fetch time out from " + mPlatformCarrierConfigPackage);
chen xuf60b3162019-05-01 21:31:05 -0700331 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
332 // If we attempted to bind to the app, but the service connection is null due to
333 // the race condition that clear config event happens before bind/fetch complete
334 // then config was cleared while we were waiting and we should not continue.
335 if (mServiceConnection[phoneId] != null) {
336 // If a ResponseReceiver callback is in the queue when this happens, we will
337 // unbind twice and throw an exception.
Jordan Liu178430d2020-02-10 14:32:15 -0800338 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700339 broadcastConfigChangedIntent(phoneId);
340 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700341 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700342 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700343 }
Jonathan Basseri930701e2015-06-11 20:56:43 -0700344
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800345 case EVENT_FETCH_DEFAULT_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700346 // If we attempted to bind to the app, but the service connection is null, then
347 // config was cleared while we were waiting and we should not continue.
348 if (!msg.getData().getBoolean("loaded_from_xml", false)
349 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800350 break;
351 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700352 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700353 if (carrierPackageName != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800354 logd("Found carrier config app: " + carrierPackageName);
Jordan Liu38a614c2019-03-20 15:01:17 -0700355 sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId, -1));
Jonathan Basserib919e932015-05-27 16:53:08 +0000356 } else {
Nathan Harold48ac0972019-03-13 22:33:01 -0700357 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700358 }
359 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700360 }
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700361
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800362 case EVENT_DO_FETCH_CARRIER: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700363 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700364 final PersistableBundle config =
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100365 restoreConfigFromXml(carrierPackageName, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700366 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800367 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700368 "Loaded config from XML. package="
369 + carrierPackageName
370 + " phoneId="
371 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700372 mConfigFromCarrierApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700373 Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700374 newMsg.getData().putBoolean("loaded_from_xml", true);
375 sendMessage(newMsg);
376 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700377 // No cached config, so fetch it from a carrier app.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800378 if (carrierPackageName != null && bindToConfigPackage(carrierPackageName,
379 phoneId, EVENT_CONNECTED_TO_CARRIER)) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700380 sendMessageDelayed(
381 obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700382 BIND_TIMEOUT_MILLIS);
383 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700384 // Send broadcast if bind fails.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700385 broadcastConfigChangedIntent(phoneId);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800386 loge("Bind to carrier app: " + carrierPackageName + " fails");
Nathan Harold48ac0972019-03-13 22:33:01 -0700387 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700388 }
389 }
390 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700391 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700392
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800393 case EVENT_CONNECTED_TO_CARRIER: {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700394 removeMessages(EVENT_BIND_CARRIER_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700395 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800396 // If new service connection has been created, unbind.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700397 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Jordan Liu178430d2020-02-10 14:32:15 -0800398 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800399 break;
400 }
chen xu02581692018-11-11 19:03:44 -0800401 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700402 // ResultReceiver callback will execute in this Handler's thread.
403 final ResultReceiver resultReceiver =
404 new ResultReceiver(this) {
405 @Override
406 public void onReceiveResult(int resultCode, Bundle resultData) {
Jordan Liu178430d2020-02-10 14:32:15 -0800407 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700408 // If new service connection has been created, this is stale.
409 if (mServiceConnection[phoneId] != conn) {
410 loge("Received response for stale request.");
411 return;
412 }
413 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
414 if (resultCode == RESULT_ERROR || resultData == null) {
415 // On error, abort config fetching.
chen xudb04c292019-03-26 17:01:15 -0700416 loge("Failed to get carrier config from carrier app: "
417 + getCarrierPackageForPhoneId(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700418 broadcastConfigChangedIntent(phoneId);
Nathan Harold48ac0972019-03-13 22:33:01 -0700419 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700420 return;
421 }
422 PersistableBundle config =
423 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100424 saveConfigToXml(getCarrierPackageForPhoneId(phoneId), "",
425 phoneId, carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700426 mConfigFromCarrierApp[phoneId] = config;
427 sendMessage(
428 obtainMessage(
429 EVENT_FETCH_CARRIER_DONE, phoneId, -1));
430 }
431 };
432 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800433 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700434 ICarrierService carrierService =
435 ICarrierService.Stub.asInterface(conn.service);
436 carrierService.getCarrierConfig(carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800437 logdWithLocalLog("Fetch config for carrier app: "
chen xudb04c292019-03-26 17:01:15 -0700438 + getCarrierPackageForPhoneId(phoneId)
439 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700440 } catch (RemoteException e) {
441 loge("Failed to get carrier config: " + e.toString());
Jordan Liu178430d2020-02-10 14:32:15 -0800442 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700443 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800444 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700445 sendMessageDelayed(
446 obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1),
447 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800448 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700449 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800450
Jonathan Basseri930701e2015-06-11 20:56:43 -0700451 case EVENT_BIND_CARRIER_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800452 case EVENT_FETCH_CARRIER_TIMEOUT: {
453 loge("Bind/fetch from carrier app timeout");
chen xuf60b3162019-05-01 21:31:05 -0700454 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
455 // If we attempted to bind to the app, but the service connection is null due to
456 // the race condition that clear config event happens before bind/fetch complete
457 // then config was cleared while we were waiting and we should not continue.
458 if (mServiceConnection[phoneId] != null) {
459 // If a ResponseReceiver callback is in the queue when this happens, we will
460 // unbind twice and throw an exception.
Jordan Liu178430d2020-02-10 14:32:15 -0800461 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700462 broadcastConfigChangedIntent(phoneId);
463 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700464 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700465 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700466 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800467 case EVENT_FETCH_CARRIER_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700468 // If we attempted to bind to the app, but the service connection is null, then
469 // config was cleared while we were waiting and we should not continue.
470 if (!msg.getData().getBoolean("loaded_from_xml", false)
471 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800472 break;
473 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700474 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800475 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700476 }
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700477
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800478 case EVENT_CHECK_SYSTEM_UPDATE: {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700479 SharedPreferences sharedPrefs =
480 PreferenceManager.getDefaultSharedPreferences(mContext);
481 final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
482 if (!Build.FINGERPRINT.equals(lastFingerprint)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800483 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700484 "Build fingerprint changed. old: "
485 + lastFingerprint
486 + " new: "
487 + Build.FINGERPRINT);
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700488 clearCachedConfigForPackage(null);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700489 sharedPrefs
490 .edit()
491 .putString(KEY_FINGERPRINT, Build.FINGERPRINT)
492 .apply();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700493 }
494 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700495 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700496
497 case EVENT_SUBSCRIPTION_INFO_UPDATED:
498 broadcastConfigChangedIntent(phoneId);
499 break;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700500 case EVENT_MULTI_SIM_CONFIG_CHANGED:
501 onMultiSimConfigChanged();
502 break;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800503 }
504 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700505 }
506
507 private final Handler mHandler;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800508
509 /**
510 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
511 * receiver for relevant events.
512 */
513 private CarrierConfigLoader(Context context) {
514 mContext = context;
Meng Wang33ad2bc2017-03-16 20:21:20 -0700515 mPlatformCarrierConfigPackage =
516 mContext.getString(R.string.platform_carrier_config_package);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700517 mHandler = new ConfigHandler();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800518
Nanxi Chen3d670502016-03-17 16:32:09 -0700519 IntentFilter bootFilter = new IntentFilter();
520 bootFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
521 context.registerReceiver(mBootReceiver, bootFilter);
522
Junda Liu8a8a53a2015-05-15 16:19:57 -0700523 // Register for package updates. Update app or uninstall app update will have all 3 intents,
524 // in the order or removed, added, replaced, all with extra_replace set to true.
525 IntentFilter pkgFilter = new IntentFilter();
526 pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
527 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
528 pkgFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
529 pkgFilter.addDataScheme("package");
Jordan Liu5ca24762019-07-10 12:51:09 -0700530 context.registerReceiver(mPackageReceiver, pkgFilter);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800531
Malcolm Chen978dda52019-10-08 18:15:25 -0700532 int numPhones = TelephonyManager.from(context).getSupportedModemCount();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700533 mConfigFromDefaultApp = new PersistableBundle[numPhones];
534 mConfigFromCarrierApp = new PersistableBundle[numPhones];
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100535 mPersistentOverrideConfigs = new PersistableBundle[numPhones];
Hall Liu506724b2018-10-22 18:16:14 -0700536 mOverrideConfigs = new PersistableBundle[numPhones];
Zach Johnson36d7aab2015-05-22 15:43:00 -0700537 mServiceConnection = new CarrierServiceConnection[numPhones];
Jordan Liu178430d2020-02-10 14:32:15 -0800538 mServiceBound = new boolean[numPhones];
Junda Liu03aad762017-07-21 13:32:17 -0700539 mHasSentConfigChange = new boolean[numPhones];
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800540 // Make this service available through ServiceManager.
541 ServiceManager.addService(Context.CARRIER_CONFIG_SERVICE, this);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800542 logd("CarrierConfigLoader has started");
Nathan Harold48ac0972019-03-13 22:33:01 -0700543 mSubscriptionInfoUpdater = PhoneFactory.getSubscriptionInfoUpdater();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700544 mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800545 }
546
547 /**
548 * Initialize the singleton CarrierConfigLoader instance.
549 *
550 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
551 */
552 /* package */
553 static CarrierConfigLoader init(Context context) {
554 synchronized (CarrierConfigLoader.class) {
555 if (sInstance == null) {
556 sInstance = new CarrierConfigLoader(context);
557 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700558 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800559 }
560 return sInstance;
561 }
562 }
563
Malcolm Chen5ea18532019-10-24 19:55:44 -0700564 private void clearConfigForPhone(int phoneId, boolean sendBroadcast) {
565 /* Ignore clear configuration request if device is being shutdown. */
566 Phone phone = PhoneFactory.getPhone(phoneId);
567 if (phone != null) {
568 if (phone.isShuttingDown()) {
569 return;
570 }
571 }
572
573 mConfigFromDefaultApp[phoneId] = null;
574 mConfigFromCarrierApp[phoneId] = null;
575 mServiceConnection[phoneId] = null;
576 mHasSentConfigChange[phoneId] = false;
577
578 if (sendBroadcast) broadcastConfigChangedIntent(phoneId, false);
579 }
580
Nathan Harold48ac0972019-03-13 22:33:01 -0700581 private void notifySubscriptionInfoUpdater(int phoneId) {
582 String configPackagename;
583 PersistableBundle configToSend;
584 int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
585 // Prefer the carrier privileged carrier app, but if there is not one, use the platform
586 // default carrier app.
587 if (mConfigFromCarrierApp[phoneId] != null) {
588 configPackagename = getCarrierPackageForPhoneId(phoneId);
589 configToSend = mConfigFromCarrierApp[phoneId];
590 } else {
591 configPackagename = mPlatformCarrierConfigPackage;
592 configToSend = mConfigFromDefaultApp[phoneId];
593 }
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700594
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800595 if (configToSend == null) {
596 configToSend = new PersistableBundle();
597 }
598
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700599 // mOverrideConfigs is for testing. And it will override current configs.
600 PersistableBundle config = mOverrideConfigs[phoneId];
601 if (config != null) {
Torbjorn Eklund1050cb02018-11-16 14:05:38 +0100602 configToSend = new PersistableBundle(configToSend);
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700603 configToSend.putAll(config);
604 }
605
Nathan Harold48ac0972019-03-13 22:33:01 -0700606 mSubscriptionInfoUpdater.updateSubscriptionByCarrierConfigAndNotifyComplete(
607 phoneId, configPackagename, configToSend,
608 mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1));
609 }
610
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800611 private void broadcastConfigChangedIntent(int phoneId) {
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800612 broadcastConfigChangedIntent(phoneId, true);
613 }
614
615 private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800616 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800617 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
chen xuf9db49f2019-03-31 23:04:42 -0700618 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800619 if (addSubIdExtra) {
620 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
621 int[] subIds = SubscriptionManager.getSubId(phoneId);
622 if (!ArrayUtils.isEmpty(subIds)) {
623 TelephonyManager telMgr = TelephonyManager.from(mContext)
624 .createForSubscriptionId(subIds[0]);
625 simApplicationState = telMgr.getSimApplicationState();
626 }
627 // Include subId/carrier id extra only if SIM records are loaded
628 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
629 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
630 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
631 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID,
632 getSpecificCarrierIdForPhoneId(phoneId));
633 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, getCarrierIdForPhoneId(phoneId));
634 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800635 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800636 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800637 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
Jordan Liu5ca24762019-07-10 12:51:09 -0700638 mContext.sendBroadcast(intent);
Junda Liu03aad762017-07-21 13:32:17 -0700639 mHasSentConfigChange[phoneId] = true;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800640 }
641
642 /** Binds to the default or carrier config app. */
643 private boolean bindToConfigPackage(String pkgName, int phoneId, int eventId) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800644 logdWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700645 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700646 carrierService.setPackage(pkgName);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800647 mServiceConnection[phoneId] = new CarrierServiceConnection(phoneId, pkgName, eventId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800648 try {
Jordan Liu178430d2020-02-10 14:32:15 -0800649 if (mContext.bindService(carrierService, mServiceConnection[phoneId],
650 Context.BIND_AUTO_CREATE)) {
651 mServiceBound[phoneId] = true;
652 return true;
653 } else {
654 return false;
655 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800656 } catch (SecurityException ex) {
657 return false;
658 }
659 }
660
chen xu02581692018-11-11 19:03:44 -0800661 private CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800662 String mcc = "";
663 String mnc = "";
664 String imsi = "";
665 String gid1 = "";
666 String gid2 = "";
667 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
668 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800669 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800670 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700671 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
672 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800673 mcc = simOperator.substring(0, 3);
674 mnc = simOperator.substring(3);
675 }
676 Phone phone = PhoneFactory.getPhone(phoneId);
677 if (phone != null) {
678 imsi = phone.getSubscriberId();
679 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700680 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800681 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800682 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800683 }
chen xua31f22b2019-03-06 15:28:50 -0800684 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800685 }
686
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700687 /** Returns the package name of a priveleged carrier app, or null if there is none. */
688 private String getCarrierPackageForPhoneId(int phoneId) {
689 List<String> carrierPackageNames = TelephonyManager.from(mContext)
690 .getCarrierPackageNamesForIntentAndPhone(
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700691 new Intent(CarrierService.CARRIER_SERVICE_INTERFACE), phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700692 if (carrierPackageNames != null && carrierPackageNames.size() > 0) {
693 return carrierPackageNames.get(0);
694 } else {
695 return null;
696 }
697 }
698
699 private String getIccIdForPhoneId(int phoneId) {
700 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
701 return null;
702 }
703 Phone phone = PhoneFactory.getPhone(phoneId);
704 if (phone == null) {
705 return null;
706 }
707 return phone.getIccSerialNumber();
708 }
709
chen xu02581692018-11-11 19:03:44 -0800710 /**
chen xua31f22b2019-03-06 15:28:50 -0800711 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800712 */
chen xua31f22b2019-03-06 15:28:50 -0800713 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700714 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800715 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700716 }
717 Phone phone = PhoneFactory.getPhone(phoneId);
718 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800719 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700720 }
chen xua31f22b2019-03-06 15:28:50 -0800721 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800722 }
723
724 /**
725 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
726 */
727 private int getCarrierIdForPhoneId(int phoneId) {
728 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
729 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700730 }
chen xu02581692018-11-11 19:03:44 -0800731 Phone phone = PhoneFactory.getPhone(phoneId);
732 if (phone == null) {
733 return TelephonyManager.UNKNOWN_CARRIER_ID;
734 }
735 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700736 }
737
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700738 /**
739 * Writes a bundle to an XML file.
740 *
chen xu02581692018-11-11 19:03:44 -0800741 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -0800742 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -0800743 * should have a single copy of XML file named after carrier id. However, it's still possible
744 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
745 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
746 * carrier while iccid remains the same.
747 *
748 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
749 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700750 *
751 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700752 *
753 * @param packageName the name of the package from which we fetched this bundle.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100754 * @param extraString An extra string to be used in the XML file name.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800755 * @param phoneId the phone ID.
756 * @param carrierId contains all carrier-identifying information.
757 * @param config the bundle to be written. Null will be treated as an empty bundle.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700758 */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100759 private void saveConfigToXml(String packageName, @NonNull String extraString, int phoneId,
760 CarrierIdentifier carrierId, PersistableBundle config) {
Yuchen Dongc15afed2018-04-26 17:05:22 +0800761 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
762 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700763 loge("Skip save config because SIM records are not loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800764 return;
765 }
766
767 final String iccid = getIccIdForPhoneId(phoneId);
Chen Xucd4a6372019-07-29 16:34:52 -0700768 final int cid = carrierId.getSpecificCarrierId();
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700769 if (packageName == null || iccid == null) {
770 loge("Cannot save config with null packageName or iccid.");
771 return;
772 }
Junda Liu02596502016-11-15 13:46:43 -0800773 // b/32668103 Only save to file if config isn't empty.
774 // In case of failure, not caching an empty bundle will
775 // try loading config again on next power on or sim loaded.
776 // Downside is for genuinely empty bundle, will bind and load
777 // on every power on.
778 if (config == null || config.isEmpty()) {
779 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700780 }
781
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700782 final String version = getPackageVersion(packageName);
783 if (version == null) {
784 loge("Failed to get package version for: " + packageName);
785 return;
786 }
787
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800788 logdWithLocalLog(
789 "Save config to xml, packagename: " + packageName + " phoneId: " + phoneId);
chen xudb04c292019-03-26 17:01:15 -0700790
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700791 FileOutputStream outFile = null;
792 try {
793 outFile = new FileOutputStream(
chen xu02581692018-11-11 19:03:44 -0800794 new File(mContext.getFilesDir(),
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100795 getFilenameForConfig(packageName, extraString, iccid, cid)));
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800796 config.putString(KEY_VERSION, version);
797 config.writeToStream(outFile);
798 outFile.flush();
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700799 outFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800800 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700801 loge(e.toString());
802 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700803 }
804
805 /**
806 * Reads a bundle from an XML file.
807 *
808 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +0800809 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700810 *
811 * In case of errors, or if the saved config is from a different package version than the
812 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700813 *
814 * @param packageName the name of the package from which we fetched this bundle.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100815 * @param extraString An extra string to be used in the XML file name.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800816 * @param phoneId the phone ID.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700817 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800818 * version does not match, or reading config fails.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700819 */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100820 private PersistableBundle restoreConfigFromXml(String packageName, @NonNull String extraString,
821 int phoneId) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700822 final String version = getPackageVersion(packageName);
823 if (version == null) {
824 loge("Failed to get package version for: " + packageName);
825 return null;
826 }
Yuchen Dongc15afed2018-04-26 17:05:22 +0800827 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
828 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700829 loge("Skip restoring config because SIM records are not yet loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800830 return null;
831 }
832
833 final String iccid = getIccIdForPhoneId(phoneId);
chen xua31f22b2019-03-06 15:28:50 -0800834 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700835 if (packageName == null || iccid == null) {
836 loge("Cannot restore config with null packageName or iccid.");
837 return null;
838 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700839
840 PersistableBundle restoredBundle = null;
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100841 File file = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700842 FileInputStream inFile = null;
843 try {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100844 file = new File(mContext.getFilesDir(),
845 getFilenameForConfig(packageName, extraString, iccid, cid));
846 inFile = new FileInputStream(file);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700847
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800848 restoredBundle = PersistableBundle.readFromStream(inFile);
849 String savedVersion = restoredBundle.getString(KEY_VERSION);
850 restoredBundle.remove(KEY_VERSION);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700851
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800852 if (!version.equals(savedVersion)) {
853 loge("Saved version mismatch: " + version + " vs " + savedVersion);
854 restoredBundle = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700855 }
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800856
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700857 inFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800858 } catch (FileNotFoundException e) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100859 // Missing file is normal occurrence that might occur with a new sim or when restoring
860 // an override file during boot and should not be treated as an error.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800861 if (file != null) logd("File not found: " + file.getPath());
862 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700863 loge(e.toString());
864 }
865
866 return restoredBundle;
867 }
868
Junda Liu8a8a53a2015-05-15 16:19:57 -0700869 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700870 * Clears cached carrier config.
871 * This deletes all saved XML files associated with the given package name. If packageName is
872 * null, then it deletes all saved XML files.
873 *
874 * @param packageName the name of a carrier package, or null if all cached config should be
875 * cleared.
876 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -0700877 */
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700878 private boolean clearCachedConfigForPackage(final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700879 File dir = mContext.getFilesDir();
880 File[] packageFiles = dir.listFiles(new FilenameFilter() {
881 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700882 if (packageName != null) {
883 return filename.startsWith("carrierconfig-" + packageName + "-");
884 } else {
885 return filename.startsWith("carrierconfig-");
886 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700887 }
888 });
Junda Liu8a8a53a2015-05-15 16:19:57 -0700889 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700890 for (File f : packageFiles) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800891 logd("Deleting " + f.getName());
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700892 f.delete();
893 }
Junda Liu8a8a53a2015-05-15 16:19:57 -0700894 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700895 }
896
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700897 /** Builds a canonical file name for a config file. */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100898 private String getFilenameForConfig(@NonNull String packageName, @NonNull String extraString,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800899 @NonNull String iccid, int cid) {
chen xu02581692018-11-11 19:03:44 -0800900 // the same carrier should have a single copy of XML file named after carrier id.
901 // However, it's still possible that platform doesn't recognize the current sim carrier,
902 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
903 // cases SIM OTA resolves to different carrier while iccid remains the same.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100904 return "carrierconfig-" + packageName + extraString + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700905 }
906
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700907 /** Return the current version code of a package, or null if the name is not found. */
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700908 private String getPackageVersion(String packageName) {
909 try {
910 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -0800911 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700912 } catch (PackageManager.NameNotFoundException e) {
913 return null;
914 }
915 }
916
Jonathan Basseri65273c82017-07-25 15:08:42 -0700917 /**
918 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700919 *
920 * This reads config bundles for the given phoneId. That means getting the latest bundle from
921 * the default app and a privileged carrier app, if present. This will not bind to an app if we
922 * have a saved config file to use instead.
923 */
924 private void updateConfigForPhoneId(int phoneId) {
Junda Liu8a8a53a2015-05-15 16:19:57 -0700925 // Clear in-memory cache for carrier app config, so when carrier app gets uninstalled, no
926 // stale config is left.
927 if (mConfigFromCarrierApp[phoneId] != null &&
928 getCarrierPackageForPhoneId(phoneId) == null) {
929 mConfigFromCarrierApp[phoneId] = null;
930 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700931 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700932 }
933
Malcolm Chen5ea18532019-10-24 19:55:44 -0700934 private void onMultiSimConfigChanged() {
935 for (int i = TelephonyManager.from(mContext).getActiveModemCount();
936 i < mConfigFromDefaultApp.length; i++) {
937 clearConfigForPhone(i, false);
938 }
939 }
940
Hall Liu506724b2018-10-22 18:16:14 -0700941 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800942 @NonNull
943 public PersistableBundle getConfigForSubId(int subId, String callingPackage) {
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -0800944 return getConfigForSubIdWithFeature(subId, callingPackage, null);
945 }
946
947 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800948 @NonNull
949 public PersistableBundle getConfigForSubIdWithFeature(int subId, String callingPackage,
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -0800950 String callingFeatureId) {
951 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subId, callingPackage,
952 callingFeatureId, "getCarrierConfig")) {
Malcolm Chen6d3d6b32018-03-01 13:58:03 -0800953 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -0700954 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -0700955
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800956 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700957 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800958 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700959 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800960 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700961 retConfig.putAll(config);
yinxud0104832019-08-16 14:20:08 -0700962 if (getCarrierPackageForPhoneId(phoneId) == null) {
963 retConfig.putBoolean(
964 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
965 }
yinxuc5e27622017-11-29 15:08:50 -0800966 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800967 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800968 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700969 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -0800970 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
971 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100972 config = mPersistentOverrideConfigs[phoneId];
973 if (config != null) {
974 retConfig.putAll(config);
975 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
976 }
Hall Liu506724b2018-10-22 18:16:14 -0700977 config = mOverrideConfigs[phoneId];
978 if (config != null) {
979 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -0700980 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800981 }
982 return retConfig;
983 }
984
985 @Override
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100986 public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides,
987 boolean persistent) {
Hall Liu506724b2018-10-22 18:16:14 -0700988 mContext.enforceCallingOrSelfPermission(
989 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Brad Ebingerf6281ad2019-04-25 11:02:04 -0700990 //TODO: Also check for SHELL UID to restrict this method to testing only (b/131326259)
Hall Liu506724b2018-10-22 18:16:14 -0700991 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
992 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800993 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
Hall Liu506724b2018-10-22 18:16:14 -0700994 return;
995 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100996 overrideConfig(mOverrideConfigs, phoneId, overrides);
Hall Liu506724b2018-10-22 18:16:14 -0700997
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100998 if (persistent) {
999 overrideConfig(mPersistentOverrideConfigs, phoneId, overrides);
1000
1001 if (overrides != null) {
1002 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
1003 saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId,
1004 carrierId, mPersistentOverrideConfigs[phoneId]);
1005 } else {
1006 final String iccid = getIccIdForPhoneId(phoneId);
1007 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1008 String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage,
1009 OVERRIDE_PACKAGE_ADDITION, iccid, cid);
1010 File fileToDelete = new File(mContext.getFilesDir(), fileName);
1011 fileToDelete.delete();
1012 }
Hall Liu506724b2018-10-22 18:16:14 -07001013 }
Brad Ebingerc9d1fa12019-04-17 15:21:18 -07001014 notifySubscriptionInfoUpdater(phoneId);
Hall Liu506724b2018-10-22 18:16:14 -07001015 }
1016
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001017 private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId,
1018 @Nullable PersistableBundle overrides) {
1019 if (overrides == null) {
1020 currentOverrides[phoneId] = new PersistableBundle();
1021 } else if (currentOverrides[phoneId] == null) {
1022 currentOverrides[phoneId] = overrides;
1023 } else {
1024 currentOverrides[phoneId].putAll(overrides);
1025 }
1026 }
1027
Hall Liu506724b2018-10-22 18:16:14 -07001028 @Override
Jonathan Basseri86030352015-06-08 12:27:56 -07001029 public void notifyConfigChangedForSubId(int subId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001030 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001031 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001032 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001033 return;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001034 }
Jordan Liud7d57fe2019-04-16 12:29:43 -07001035
1036 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001037 // system privileged app with MODIFY_PHONE_STATE permission.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001038 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext, subId,
1039 "Require carrier privileges or MODIFY_PHONE_STATE permission.");
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001040
1041 // This method should block until deleting has completed, so that an error which prevents us
1042 // from clearing the cache is passed back to the carrier app. With the files successfully
1043 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001044 String callingPackageName = mContext.getPackageManager().getNameForUid(
1045 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001046 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001047 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001048 }
1049
1050 @Override
1051 public void updateConfigForPhoneId(int phoneId, String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -07001052 mContext.enforceCallingOrSelfPermission(
1053 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001054 logdWithLocalLog("Update config for phoneId: " + phoneId + " simState: " + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001055 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1056 return;
1057 }
1058 // requires Java 7 for switch on string.
1059 switch (simState) {
1060 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1061 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001062 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001063 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Malcolm Chendb66de12019-12-09 18:57:07 -08001064 case IccCardConstants.INTENT_VALUE_ICC_NOT_READY:
Junda Liu9f2d2712015-05-15 14:22:45 -07001065 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001066 break;
1067 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1068 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001069 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001070 break;
1071 }
1072 }
1073
Junda Liu43d723a2015-05-12 17:23:45 -07001074 @Override
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001075 public String getDefaultCarrierServicePackageName() {
Shuo Qianefdb7962019-12-19 15:54:27 -08001076 mContext.enforceCallingOrSelfPermission(
1077 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1078 "getDefaultCarrierServicePackageName");
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001079 return mPlatformCarrierConfigPackage;
1080 }
1081
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001082 private void unbindIfBound(Context context, CarrierServiceConnection conn,
1083 int phoneId) {
1084 if (mServiceBound[phoneId]) {
1085 mServiceBound[phoneId] = false;
1086 context.unbindService(conn);
1087 }
1088 }
1089
1090 /**
1091 * If {@code args} contains {@link #DUMP_ARG_REQUESTING_PACKAGE} and a following package name,
1092 * we'll also call {@link IBinder#dump} on the default carrier service (if bound) and the
1093 * specified carrier service (if bound). Typically, this is done for connectivity bug reports
1094 * where we don't call {@code dumpsys activity service all-non-platform} because that contains
1095 * too much info, but we still want to let carrier apps include their diagnostics.
1096 */
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001097 @Override
Junda Liu43d723a2015-05-12 17:23:45 -07001098 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001099 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
Junda Liu43d723a2015-05-12 17:23:45 -07001100 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1101 != PackageManager.PERMISSION_GRANTED) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001102 indentPW.println("Permission Denial: can't dump carrierconfig from from pid="
Junda Liu43d723a2015-05-12 17:23:45 -07001103 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1104 return;
1105 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001106 String requestingPackage = null;
1107 int requestingPackageIndex = ArrayUtils.indexOf(args, DUMP_ARG_REQUESTING_PACKAGE);
1108 if (requestingPackageIndex >= 0 && requestingPackageIndex < args.length - 1
1109 && !TextUtils.isEmpty(args[requestingPackageIndex + 1])) {
1110 requestingPackage = args[requestingPackageIndex + 1];
1111 // Throws a SecurityException if the caller is impersonating another app in an effort to
1112 // dump extra info (which may contain PII the caller doesn't have a right to).
1113 enforceCallerIsSystemOrRequestingPackage(requestingPackage);
1114 }
1115
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001116 indentPW.println("CarrierConfigLoader: " + this);
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001117 for (int i = 0; i < TelephonyManager.from(mContext).getActiveModemCount(); i++) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001118 indentPW.println("Phone Id = " + i);
shuoq26a3a4c2016-12-16 11:06:48 -08001119 // display default values in CarrierConfigManager
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001120 printConfig(CarrierConfigManager.getDefaultConfig(), indentPW,
shuoq26a3a4c2016-12-16 11:06:48 -08001121 "Default Values from CarrierConfigManager");
shuoq26a3a4c2016-12-16 11:06:48 -08001122 // display ConfigFromDefaultApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001123 printConfig(mConfigFromDefaultApp[i], indentPW, "mConfigFromDefaultApp");
shuoq26a3a4c2016-12-16 11:06:48 -08001124 // display ConfigFromCarrierApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001125 printConfig(mConfigFromCarrierApp[i], indentPW, "mConfigFromCarrierApp");
1126 printConfig(mPersistentOverrideConfigs[i], indentPW, "mPersistentOverrideConfigs");
1127 printConfig(mOverrideConfigs[i], indentPW, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001128 }
chen xudb04c292019-03-26 17:01:15 -07001129
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001130 indentPW.println("CarrierConfigLoadingLog=");
1131 mCarrierConfigLoadingLog.dump(fd, indentPW, args);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001132
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001133 if (requestingPackage != null) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001134 logd("Including default and requesting package " + requestingPackage
1135 + " carrier services in dump");
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001136 indentPW.println("");
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001137 indentPW.println("Connected services");
1138 dumpCarrierServiceIfBound(fd, indentPW, "Default config package",
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001139 mPlatformCarrierConfigPackage, false /* considerCarrierPrivileges */);
1140 dumpCarrierServiceIfBound(fd, indentPW, "Requesting package", requestingPackage,
1141 true /* considerCarrierPrivileges */);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001142 }
shuoq26a3a4c2016-12-16 11:06:48 -08001143 }
1144
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001145 private void printConfig(PersistableBundle configApp, IndentingPrintWriter indentPW,
1146 String name) {
1147 indentPW.increaseIndent();
shuoq26a3a4c2016-12-16 11:06:48 -08001148 if (configApp == null) {
shuoq26a3a4c2016-12-16 11:06:48 -08001149 indentPW.println(name + " : null ");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001150 indentPW.decreaseIndent();
1151 indentPW.println("");
shuoq26a3a4c2016-12-16 11:06:48 -08001152 return;
1153 }
shuoq26a3a4c2016-12-16 11:06:48 -08001154 indentPW.println(name + " : ");
1155 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1156 Collections.sort(sortedKeys);
1157 indentPW.increaseIndent();
1158 indentPW.increaseIndent();
1159 for (String key : sortedKeys) {
1160 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1161 indentPW.println(key + " = " +
1162 Arrays.toString((Object[]) configApp.get(key)));
1163 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1164 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1165 } else {
1166 indentPW.println(key + " = " + configApp.get(key));
1167 }
Junda Liu43d723a2015-05-12 17:23:45 -07001168 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001169 indentPW.decreaseIndent();
1170 indentPW.decreaseIndent();
1171 indentPW.decreaseIndent();
1172 indentPW.println("");
Junda Liu43d723a2015-05-12 17:23:45 -07001173 }
1174
Hunter Knepshielde601f432020-02-19 10:16:04 -08001175 /**
1176 * Passes without problem when one of these conditions is true:
1177 * - The caller is a privileged UID (e.g. for dumpstate.cpp generating a bug report, where the
1178 * system knows the true caller plumbed in through the {@link android.os.BugreportManager} API).
1179 * - The caller's UID matches the supplied package.
1180 *
1181 * @throws SecurityException if none of the above conditions are met.
1182 */
1183 private void enforceCallerIsSystemOrRequestingPackage(String requestingPackage)
1184 throws SecurityException {
1185 final int callingUid = Binder.getCallingUid();
1186 if (callingUid == Process.ROOT_UID || callingUid == Process.SYSTEM_UID
1187 || callingUid == Process.SHELL_UID || callingUid == Process.PHONE_UID) {
1188 // Bug reports (dumpstate.cpp) run as SHELL, and let some other privileged UIDs through
1189 // as well.
1190 return;
1191 }
1192 // An app is trying to dump extra detail, block it if they aren't who they claim to be.
1193 AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class);
1194 if (appOps == null) {
1195 throw new SecurityException("No AppOps");
1196 }
1197 // Will throw a SecurityException if the UID and package don't match.
1198 appOps.checkPackage(callingUid, requestingPackage);
1199 }
1200
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001201 /**
1202 * Searches for one or more appropriate {@link CarrierService} instances to dump based on the
1203 * current connections.
1204 *
1205 * @param targetPkgName the target package name to dump carrier services for
1206 * @param considerCarrierPrivileges if true, allow a carrier service to be dumped if it shares
1207 * carrier privileges with {@code targetPkgName};
1208 * otherwise, only dump a carrier service if it is {@code
1209 * targetPkgName}
1210 */
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001211 private void dumpCarrierServiceIfBound(FileDescriptor fd, IndentingPrintWriter indentPW,
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001212 String prefix, String targetPkgName, boolean considerCarrierPrivileges) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001213 // Null package is possible if it's early in the boot process, there was a recent crash, we
1214 // loaded the config from XML most recently, or a SIM slot is empty. Carrier apps with
1215 // long-lived bindings should typically get dumped here regardless. Even if an app is being
1216 // used for multiple phoneIds, we assume that it's smart enough to handle that on its own,
1217 // and that in most cases we'd just be dumping duplicate information and bloating a report.
1218 indentPW.increaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001219 indentPW.println(prefix + " : " + targetPkgName);
1220 Set<String> dumpedPkgNames = new ArraySet<>(mServiceConnection.length);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001221 for (CarrierServiceConnection connection : mServiceConnection) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001222 if (connection == null || !SubscriptionManager.isValidPhoneId(connection.phoneId)
1223 || TextUtils.isEmpty(connection.pkgName)) {
1224 continue;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001225 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001226 final String servicePkgName = connection.pkgName;
1227 // Note: we intentionally ignore system components here because we should NOT match the
1228 // shell caller that's typically used for bug reports via non-BugreportManager triggers.
1229 final boolean exactPackageMatch = TextUtils.equals(targetPkgName, servicePkgName);
1230 final boolean carrierPrivilegesMatch =
1231 considerCarrierPrivileges && hasCarrierPrivileges(targetPkgName,
1232 connection.phoneId);
1233 if (!exactPackageMatch && !carrierPrivilegesMatch) continue;
1234 // Make sure this service is actually alive before trying to dump it. We don't pay
1235 // attention to mServiceBound[connection.phoneId] because typically carrier apps will
1236 // request long-lived bindings, and even if we unbind the app, it may still be alive due
1237 // to CarrierServiceBindHelper. Pull it out as a reference so even if it gets set to
1238 // null within the ServiceConnection during unbinding we can avoid an NPE.
1239 final IBinder service = connection.service;
1240 if (service == null || !service.isBinderAlive() || !service.pingBinder()) continue;
1241 // We've got a live service. Last check is just to make sure we don't dump a package
1242 // multiple times.
1243 if (!dumpedPkgNames.add(servicePkgName)) continue;
1244 if (!exactPackageMatch) {
1245 logd(targetPkgName + " has carrier privileges on phoneId " + connection.phoneId
1246 + ", service provided by " + servicePkgName);
1247 indentPW.increaseIndent();
1248 indentPW.println("Proxy : " + servicePkgName);
1249 indentPW.decreaseIndent();
1250 }
1251 // Flush before we let the app output anything to ensure correct ordering of output.
1252 // Internally, Binder#dump calls flush on its printer after finishing so we don't
1253 // need to do anything after.
1254 indentPW.flush();
1255 try {
1256 logd("Dumping " + servicePkgName);
1257 // We don't need to give the carrier service any args.
1258 connection.service.dump(fd, null /* args */);
1259 logd("Done with " + servicePkgName);
1260 } catch (RemoteException e) {
1261 logd("RemoteException from " + servicePkgName, e);
1262 indentPW.increaseIndent();
1263 indentPW.println("RemoteException");
1264 indentPW.increaseIndent();
1265 e.printStackTrace(indentPW);
1266 indentPW.decreaseIndent();
1267 indentPW.decreaseIndent();
1268 // We won't retry this package again because now it's in dumpedPkgNames.
1269 }
1270 indentPW.println("");
Jordan Liu46ed5db2020-01-28 08:55:20 -08001271 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001272 if (dumpedPkgNames.isEmpty()) {
1273 indentPW.increaseIndent();
1274 indentPW.println("Not bound");
1275 indentPW.decreaseIndent();
1276 indentPW.println("");
1277 }
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001278 indentPW.decreaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001279 }
1280
1281 private boolean hasCarrierPrivileges(String pkgName, int phoneId) {
1282 int[] subIds = SubscriptionManager.getSubId(phoneId);
1283 if (ArrayUtils.isEmpty(subIds)) {
1284 return false;
1285 }
1286 return TelephonyManager.from(mContext).createForSubscriptionId(
1287 subIds[0]).checkCarrierPrivilegesForPackage(pkgName)
1288 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001289 }
1290
Zach Johnson36d7aab2015-05-22 15:43:00 -07001291 private class CarrierServiceConnection implements ServiceConnection {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001292 final int phoneId;
1293 final String pkgName;
1294 final int eventId;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001295 IBinder service;
1296
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001297 CarrierServiceConnection(int phoneId, String pkgName, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001298 this.phoneId = phoneId;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001299 this.pkgName = pkgName;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001300 this.eventId = eventId;
1301 }
1302
1303 @Override
1304 public void onServiceConnected(ComponentName name, IBinder service) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001305 logd("Connected to config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001306 this.service = service;
1307 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1308 }
1309
1310 @Override
1311 public void onServiceDisconnected(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001312 logd("Disconnected from config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001313 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001314 }
1315
1316 @Override
1317 public void onBindingDied(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001318 logd("Binding died from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001319 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001320 }
1321
1322 @Override
1323 public void onNullBinding(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001324 logd("Null binding from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001325 this.service = null;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001326 }
1327 }
1328
1329 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1330 @Override
1331 public void onReceive(Context context, Intent intent) {
1332 String action = intent.getAction();
Junda Liu8a8a53a2015-05-15 16:19:57 -07001333 boolean replace = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1334 // If replace is true, only care ACTION_PACKAGE_REPLACED.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001335 if (replace && !Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
Junda Liu8a8a53a2015-05-15 16:19:57 -07001336 return;
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001337 }
Junda Liu8a8a53a2015-05-15 16:19:57 -07001338
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001339 switch (action) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001340 case Intent.ACTION_BOOT_COMPLETED:
1341 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1342 break;
1343
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001344 case Intent.ACTION_PACKAGE_ADDED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001345 case Intent.ACTION_PACKAGE_REMOVED:
Junda Liu8a8a53a2015-05-15 16:19:57 -07001346 case Intent.ACTION_PACKAGE_REPLACED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001347 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1348 String packageName = mContext.getPackageManager().getNameForUid(uid);
Junda Liu8a8a53a2015-05-15 16:19:57 -07001349 if (packageName != null) {
1350 // We don't have a phoneId for arg1.
1351 mHandler.sendMessage(
1352 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, packageName));
1353 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001354 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001355 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001356 }
1357 }
1358
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001359 private void logd(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001360 Log.d(LOG_TAG, msg);
1361 }
1362
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001363 private void logd(String msg, Throwable tr) {
1364 Log.d(LOG_TAG, msg, tr);
1365 }
1366
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001367 private void logdWithLocalLog(String msg) {
chen xudb04c292019-03-26 17:01:15 -07001368 Log.d(LOG_TAG, msg);
1369 mCarrierConfigLoadingLog.log(msg);
1370 }
1371
1372 private void loge(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001373 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07001374 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001375 }
1376}