blob: 3a870483309b55c78186a604d3351f1415420510 [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;
Junda Liu03aad762017-07-21 13:32:17 -0700106 // Whether we have sent config change bcast for each phone id.
107 private boolean[] mHasSentConfigChange;
Nathan Harold48ac0972019-03-13 22:33:01 -0700108 // SubscriptionInfoUpdater
109 private final SubscriptionInfoUpdater mSubscriptionInfoUpdater;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800110
Nanxi Chen3d670502016-03-17 16:32:09 -0700111 // Broadcast receiver for Boot intents, register intent filter in construtor.
112 private final BroadcastReceiver mBootReceiver = new ConfigLoaderBroadcastReceiver();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800113 // Broadcast receiver for SIM and pkg intents, register intent filter in constructor.
Nanxi Chen3d670502016-03-17 16:32:09 -0700114 private final BroadcastReceiver mPackageReceiver = new ConfigLoaderBroadcastReceiver();
Jack Yu67663de2019-10-17 15:19:48 -0700115 private final LocalLog mCarrierConfigLoadingLog = new LocalLog(100);
chen xudb04c292019-03-26 17:01:15 -0700116
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800117
118 // Message codes; see mHandler below.
119 // Request from SubscriptionInfoUpdater when SIM becomes absent or error.
120 private static final int EVENT_CLEAR_CONFIG = 0;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800121 // Has connected to default app.
122 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
123 // Has connected to carrier app.
124 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700125 // Config has been loaded from default app (or cache).
126 private static final int EVENT_FETCH_DEFAULT_DONE = 5;
127 // Config has been loaded from carrier app (or cache).
128 private static final int EVENT_FETCH_CARRIER_DONE = 6;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700129 // Attempt to fetch from default app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700130 private static final int EVENT_DO_FETCH_DEFAULT = 7;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700131 // Attempt to fetch from carrier app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700132 private static final int EVENT_DO_FETCH_CARRIER = 8;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700133 // A package has been installed, uninstalled, or updated.
134 private static final int EVENT_PACKAGE_CHANGED = 9;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700135 // Bind timed out for the default app.
136 private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
137 // Bind timed out for a carrier app.
138 private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700139 // Check if the system fingerprint has changed.
140 private static final int EVENT_CHECK_SYSTEM_UPDATE = 12;
Nanxi Chen3d670502016-03-17 16:32:09 -0700141 // Rerun carrier config binding after system is unlocked.
142 private static final int EVENT_SYSTEM_UNLOCKED = 13;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700143 // Fetching config timed out from the default app.
144 private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14;
145 // Fetching config timed out from a carrier app.
146 private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15;
Nathan Harold48ac0972019-03-13 22:33:01 -0700147 // SubscriptionInfoUpdater has finished updating the sub for the carrier config.
148 private static final int EVENT_SUBSCRIPTION_INFO_UPDATED = 16;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700149 // Multi-SIM config changed.
150 private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 17;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700151
Junda Liu6cb45002016-03-22 17:18:20 -0700152 private static final int BIND_TIMEOUT_MILLIS = 30000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800153
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800154 // Keys used for saving and restoring config bundle from file.
155 private static final String KEY_VERSION = "__carrier_config_package_version__";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800156
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100157 private static final String OVERRIDE_PACKAGE_ADDITION = "-override";
158
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700159 // SharedPreferences key for last known build fingerprint.
160 private static final String KEY_FINGERPRINT = "build_fingerprint";
161
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800162 // Argument for #dump that indicates we should also call the default and specified carrier
163 // service's #dump method. In multi-SIM devices, it's possible that carrier A is on SIM 1 and
164 // carrier B is on SIM 2, in which case we should not dump carrier B's service when carrier A
165 // requested the dump.
166 private static final String DUMP_ARG_REQUESTING_PACKAGE = "--requesting-package";
167
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800168 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700169 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700170 // For each phoneId, the event sequence should be:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700171 // fetch default, connected to default, fetch default (async), fetch default done,
172 // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700173 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700174 // If there is a saved config file for either the default app or the carrier app, we skip
175 // binding to the app and go straight from fetch to loaded.
176 //
177 // At any time, at most one connection is active. If events are not in this order, previous
178 // connection will be unbound, so only latest event takes effect.
179 //
180 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
181 // 1. loading from carrier app (even if read from a file)
182 // 2. loading from default app if there is no carrier app (even if read from a file)
183 // 3. clearing config (e.g. due to sim removal)
184 // 4. encountering bind or IPC error
Jonathan Basseri65273c82017-07-25 15:08:42 -0700185 private class ConfigHandler extends Handler {
186 @Override
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800187 public void handleMessage(Message msg) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700188 final int phoneId = msg.arg1;
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800189 logdWithLocalLog("mHandler: " + msg.what + " phoneId: " + phoneId);
Malcolm Chen5ea18532019-10-24 19:55:44 -0700190 if (!SubscriptionManager.isValidPhoneId(phoneId)
191 && msg.what != EVENT_MULTI_SIM_CONFIG_CHANGED) {
192 return;
193 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800194 switch (msg.what) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800195 case EVENT_CLEAR_CONFIG: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700196 clearConfigForPhone(phoneId, true);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800197 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700198 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700199
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800200 case EVENT_SYSTEM_UNLOCKED: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700201 for (int i = 0; i < TelephonyManager.from(mContext).getActiveModemCount();
202 ++i) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700203 // When user unlock device, we should only try to send broadcast again if we
204 // have sent it before unlock. This will avoid we try to load carrier config
205 // when SIM is still loading when unlock happens.
Junda Liu03aad762017-07-21 13:32:17 -0700206 if (mHasSentConfigChange[i]) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800207 logdWithLocalLog("System unlocked");
Junda Liu03aad762017-07-21 13:32:17 -0700208 updateConfigForPhoneId(i);
209 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700210 }
211 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700212 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700213
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800214 case EVENT_PACKAGE_CHANGED: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700215 final String carrierPackageName = (String) msg.obj;
216 // Only update if there are cached config removed to avoid updating config for
217 // unrelated packages.
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700218 if (clearCachedConfigForPackage(carrierPackageName)) {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700219 int numPhones = TelephonyManager.from(mContext).getActiveModemCount();
Junda Liu8a8a53a2015-05-15 16:19:57 -0700220 for (int i = 0; i < numPhones; ++i) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800221 logdWithLocalLog("Package changed: " + carrierPackageName
Jack Yu67663de2019-10-17 15:19:48 -0700222 + ", phone=" + i);
Junda Liu8a8a53a2015-05-15 16:19:57 -0700223 updateConfigForPhoneId(i);
224 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700225 }
226 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700227 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700228
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800229 case EVENT_DO_FETCH_DEFAULT: {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100230 // Restore persistent override values.
231 PersistableBundle config = restoreConfigFromXml(
232 mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId);
233 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800234 logd("Loaded persistent override config from XML. package="
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100235 + mPlatformCarrierConfigPackage
236 + " phoneId=" + phoneId);
237 mPersistentOverrideConfigs[phoneId] = config;
238 }
239
240 config = restoreConfigFromXml(mPlatformCarrierConfigPackage, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700241 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800242 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700243 "Loaded config from XML. package="
244 + mPlatformCarrierConfigPackage
245 + " phoneId="
246 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700247 mConfigFromDefaultApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700248 Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700249 newMsg.getData().putBoolean("loaded_from_xml", true);
250 mHandler.sendMessage(newMsg);
251 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700252 // No cached config, so fetch it from the default app.
253 if (bindToConfigPackage(
254 mPlatformCarrierConfigPackage,
255 phoneId,
256 EVENT_CONNECTED_TO_DEFAULT)) {
257 sendMessageDelayed(
258 obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700259 BIND_TIMEOUT_MILLIS);
260 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700261 // Send broadcast if bind fails.
Nathan Harold48ac0972019-03-13 22:33:01 -0700262 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700263 // TODO: We *must* call unbindService even if bindService returns false.
264 // (And possibly if SecurityException was thrown.)
chen xudb04c292019-03-26 17:01:15 -0700265 loge("binding to default app: "
266 + mPlatformCarrierConfigPackage + " fails");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700267 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800268 }
269 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700270 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800271
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800272 case EVENT_CONNECTED_TO_DEFAULT: {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700273 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700274 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800275 // If new service connection has been created, unbind.
276 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Jordan Liu178430d2020-02-10 14:32:15 -0800277 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800278 break;
279 }
chen xu02581692018-11-11 19:03:44 -0800280 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700281 // ResultReceiver callback will execute in this Handler's thread.
282 final ResultReceiver resultReceiver =
283 new ResultReceiver(this) {
284 @Override
285 public void onReceiveResult(int resultCode, Bundle resultData) {
Jordan Liu178430d2020-02-10 14:32:15 -0800286 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700287 // If new service connection has been created, this is stale.
288 if (mServiceConnection[phoneId] != conn) {
289 loge("Received response for stale request.");
290 return;
291 }
292 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
293 if (resultCode == RESULT_ERROR || resultData == null) {
294 // On error, abort config fetching.
295 loge("Failed to get carrier config");
Nathan Harold48ac0972019-03-13 22:33:01 -0700296 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700297 return;
298 }
299 PersistableBundle config =
300 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100301 saveConfigToXml(mPlatformCarrierConfigPackage, "", phoneId,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800302 carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700303 mConfigFromDefaultApp[phoneId] = config;
304 sendMessage(
305 obtainMessage(
306 EVENT_FETCH_DEFAULT_DONE, phoneId, -1));
307 }
308 };
309 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800310 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700311 ICarrierService carrierService =
312 ICarrierService.Stub.asInterface(conn.service);
313 carrierService.getCarrierConfig(carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800314 logdWithLocalLog("Fetch config for default app: "
chen xudb04c292019-03-26 17:01:15 -0700315 + mPlatformCarrierConfigPackage
316 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700317 } catch (RemoteException e) {
chen xudb04c292019-03-26 17:01:15 -0700318 loge("Failed to get carrier config from default app: " +
319 mPlatformCarrierConfigPackage + " err: " + e.toString());
Jordan Liu178430d2020-02-10 14:32:15 -0800320 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700321 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800322 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700323 sendMessageDelayed(
324 obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1),
325 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800326 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700327 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800328
Jonathan Basseri930701e2015-06-11 20:56:43 -0700329 case EVENT_BIND_DEFAULT_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800330 case EVENT_FETCH_DEFAULT_TIMEOUT: {
331 loge("Bind/fetch time out from " + mPlatformCarrierConfigPackage);
chen xuf60b3162019-05-01 21:31:05 -0700332 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
333 // If we attempted to bind to the app, but the service connection is null due to
334 // the race condition that clear config event happens before bind/fetch complete
335 // then config was cleared while we were waiting and we should not continue.
336 if (mServiceConnection[phoneId] != null) {
337 // If a ResponseReceiver callback is in the queue when this happens, we will
338 // unbind twice and throw an exception.
Jordan Liu178430d2020-02-10 14:32:15 -0800339 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700340 broadcastConfigChangedIntent(phoneId);
341 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700342 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700343 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700344 }
Jonathan Basseri930701e2015-06-11 20:56:43 -0700345
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800346 case EVENT_FETCH_DEFAULT_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700347 // If we attempted to bind to the app, but the service connection is null, then
348 // config was cleared while we were waiting and we should not continue.
349 if (!msg.getData().getBoolean("loaded_from_xml", false)
350 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800351 break;
352 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700353 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700354 if (carrierPackageName != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800355 logd("Found carrier config app: " + carrierPackageName);
Jordan Liu38a614c2019-03-20 15:01:17 -0700356 sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId, -1));
Jonathan Basserib919e932015-05-27 16:53:08 +0000357 } else {
Nathan Harold48ac0972019-03-13 22:33:01 -0700358 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700359 }
360 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700361 }
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700362
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800363 case EVENT_DO_FETCH_CARRIER: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700364 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700365 final PersistableBundle config =
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100366 restoreConfigFromXml(carrierPackageName, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700367 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800368 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700369 "Loaded config from XML. package="
370 + carrierPackageName
371 + " phoneId="
372 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700373 mConfigFromCarrierApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700374 Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700375 newMsg.getData().putBoolean("loaded_from_xml", true);
376 sendMessage(newMsg);
377 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700378 // No cached config, so fetch it from a carrier app.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800379 if (carrierPackageName != null && bindToConfigPackage(carrierPackageName,
380 phoneId, EVENT_CONNECTED_TO_CARRIER)) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700381 sendMessageDelayed(
382 obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700383 BIND_TIMEOUT_MILLIS);
384 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700385 // Send broadcast if bind fails.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700386 broadcastConfigChangedIntent(phoneId);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800387 loge("Bind to carrier app: " + carrierPackageName + " fails");
Nathan Harold48ac0972019-03-13 22:33:01 -0700388 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700389 }
390 }
391 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700392 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700393
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800394 case EVENT_CONNECTED_TO_CARRIER: {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700395 removeMessages(EVENT_BIND_CARRIER_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700396 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800397 // If new service connection has been created, unbind.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700398 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Jordan Liu178430d2020-02-10 14:32:15 -0800399 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800400 break;
401 }
chen xu02581692018-11-11 19:03:44 -0800402 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700403 // ResultReceiver callback will execute in this Handler's thread.
404 final ResultReceiver resultReceiver =
405 new ResultReceiver(this) {
406 @Override
407 public void onReceiveResult(int resultCode, Bundle resultData) {
Jordan Liu178430d2020-02-10 14:32:15 -0800408 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700409 // If new service connection has been created, this is stale.
410 if (mServiceConnection[phoneId] != conn) {
411 loge("Received response for stale request.");
412 return;
413 }
414 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
415 if (resultCode == RESULT_ERROR || resultData == null) {
416 // On error, abort config fetching.
chen xudb04c292019-03-26 17:01:15 -0700417 loge("Failed to get carrier config from carrier app: "
418 + getCarrierPackageForPhoneId(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700419 broadcastConfigChangedIntent(phoneId);
Nathan Harold48ac0972019-03-13 22:33:01 -0700420 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700421 return;
422 }
423 PersistableBundle config =
424 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100425 saveConfigToXml(getCarrierPackageForPhoneId(phoneId), "",
426 phoneId, carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700427 mConfigFromCarrierApp[phoneId] = config;
428 sendMessage(
429 obtainMessage(
430 EVENT_FETCH_CARRIER_DONE, phoneId, -1));
431 }
432 };
433 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800434 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700435 ICarrierService carrierService =
436 ICarrierService.Stub.asInterface(conn.service);
437 carrierService.getCarrierConfig(carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800438 logdWithLocalLog("Fetch config for carrier app: "
chen xudb04c292019-03-26 17:01:15 -0700439 + getCarrierPackageForPhoneId(phoneId)
440 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700441 } catch (RemoteException e) {
442 loge("Failed to get carrier config: " + e.toString());
Jordan Liu178430d2020-02-10 14:32:15 -0800443 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700444 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800445 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700446 sendMessageDelayed(
447 obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1),
448 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800449 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700450 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800451
Jonathan Basseri930701e2015-06-11 20:56:43 -0700452 case EVENT_BIND_CARRIER_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800453 case EVENT_FETCH_CARRIER_TIMEOUT: {
454 loge("Bind/fetch from carrier app timeout");
chen xuf60b3162019-05-01 21:31:05 -0700455 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
456 // If we attempted to bind to the app, but the service connection is null due to
457 // the race condition that clear config event happens before bind/fetch complete
458 // then config was cleared while we were waiting and we should not continue.
459 if (mServiceConnection[phoneId] != null) {
460 // If a ResponseReceiver callback is in the queue when this happens, we will
461 // unbind twice and throw an exception.
Jordan Liu178430d2020-02-10 14:32:15 -0800462 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700463 broadcastConfigChangedIntent(phoneId);
464 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700465 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700466 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700467 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800468 case EVENT_FETCH_CARRIER_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700469 // If we attempted to bind to the app, but the service connection is null, then
470 // config was cleared while we were waiting and we should not continue.
471 if (!msg.getData().getBoolean("loaded_from_xml", false)
472 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800473 break;
474 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700475 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800476 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700477 }
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700478
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800479 case EVENT_CHECK_SYSTEM_UPDATE: {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700480 SharedPreferences sharedPrefs =
481 PreferenceManager.getDefaultSharedPreferences(mContext);
482 final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
483 if (!Build.FINGERPRINT.equals(lastFingerprint)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800484 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700485 "Build fingerprint changed. old: "
486 + lastFingerprint
487 + " new: "
488 + Build.FINGERPRINT);
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700489 clearCachedConfigForPackage(null);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700490 sharedPrefs
491 .edit()
492 .putString(KEY_FINGERPRINT, Build.FINGERPRINT)
493 .apply();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700494 }
495 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700496 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700497
498 case EVENT_SUBSCRIPTION_INFO_UPDATED:
499 broadcastConfigChangedIntent(phoneId);
500 break;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700501 case EVENT_MULTI_SIM_CONFIG_CHANGED:
502 onMultiSimConfigChanged();
503 break;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800504 }
505 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700506 }
507
508 private final Handler mHandler;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800509
510 /**
511 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
512 * receiver for relevant events.
513 */
514 private CarrierConfigLoader(Context context) {
515 mContext = context;
Meng Wang33ad2bc2017-03-16 20:21:20 -0700516 mPlatformCarrierConfigPackage =
517 mContext.getString(R.string.platform_carrier_config_package);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700518 mHandler = new ConfigHandler();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800519
Nanxi Chen3d670502016-03-17 16:32:09 -0700520 IntentFilter bootFilter = new IntentFilter();
521 bootFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
522 context.registerReceiver(mBootReceiver, bootFilter);
523
Junda Liu8a8a53a2015-05-15 16:19:57 -0700524 // Register for package updates. Update app or uninstall app update will have all 3 intents,
525 // in the order or removed, added, replaced, all with extra_replace set to true.
526 IntentFilter pkgFilter = new IntentFilter();
527 pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
528 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
529 pkgFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
530 pkgFilter.addDataScheme("package");
Jordan Liu5ca24762019-07-10 12:51:09 -0700531 context.registerReceiver(mPackageReceiver, pkgFilter);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800532
Malcolm Chen978dda52019-10-08 18:15:25 -0700533 int numPhones = TelephonyManager.from(context).getSupportedModemCount();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700534 mConfigFromDefaultApp = new PersistableBundle[numPhones];
535 mConfigFromCarrierApp = new PersistableBundle[numPhones];
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100536 mPersistentOverrideConfigs = new PersistableBundle[numPhones];
Hall Liu506724b2018-10-22 18:16:14 -0700537 mOverrideConfigs = new PersistableBundle[numPhones];
Zach Johnson36d7aab2015-05-22 15:43:00 -0700538 mServiceConnection = new CarrierServiceConnection[numPhones];
Jordan Liu178430d2020-02-10 14:32:15 -0800539 mServiceBound = new boolean[numPhones];
Junda Liu03aad762017-07-21 13:32:17 -0700540 mHasSentConfigChange = new boolean[numPhones];
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800541 // Make this service available through ServiceManager.
542 ServiceManager.addService(Context.CARRIER_CONFIG_SERVICE, this);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800543 logd("CarrierConfigLoader has started");
Nathan Harold48ac0972019-03-13 22:33:01 -0700544 mSubscriptionInfoUpdater = PhoneFactory.getSubscriptionInfoUpdater();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700545 mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800546 }
547
548 /**
549 * Initialize the singleton CarrierConfigLoader instance.
550 *
551 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
552 */
553 /* package */
554 static CarrierConfigLoader init(Context context) {
555 synchronized (CarrierConfigLoader.class) {
556 if (sInstance == null) {
557 sInstance = new CarrierConfigLoader(context);
558 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700559 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800560 }
561 return sInstance;
562 }
563 }
564
Malcolm Chen5ea18532019-10-24 19:55:44 -0700565 private void clearConfigForPhone(int phoneId, boolean sendBroadcast) {
566 /* Ignore clear configuration request if device is being shutdown. */
567 Phone phone = PhoneFactory.getPhone(phoneId);
568 if (phone != null) {
569 if (phone.isShuttingDown()) {
570 return;
571 }
572 }
573
574 mConfigFromDefaultApp[phoneId] = null;
575 mConfigFromCarrierApp[phoneId] = null;
576 mServiceConnection[phoneId] = null;
577 mHasSentConfigChange[phoneId] = false;
578
579 if (sendBroadcast) broadcastConfigChangedIntent(phoneId, false);
580 }
581
Nathan Harold48ac0972019-03-13 22:33:01 -0700582 private void notifySubscriptionInfoUpdater(int phoneId) {
583 String configPackagename;
584 PersistableBundle configToSend;
585 int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
586 // Prefer the carrier privileged carrier app, but if there is not one, use the platform
587 // default carrier app.
588 if (mConfigFromCarrierApp[phoneId] != null) {
589 configPackagename = getCarrierPackageForPhoneId(phoneId);
590 configToSend = mConfigFromCarrierApp[phoneId];
591 } else {
592 configPackagename = mPlatformCarrierConfigPackage;
593 configToSend = mConfigFromDefaultApp[phoneId];
594 }
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700595
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800596 if (configToSend == null) {
597 configToSend = new PersistableBundle();
598 }
599
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700600 // mOverrideConfigs is for testing. And it will override current configs.
601 PersistableBundle config = mOverrideConfigs[phoneId];
602 if (config != null) {
Torbjorn Eklund1050cb02018-11-16 14:05:38 +0100603 configToSend = new PersistableBundle(configToSend);
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700604 configToSend.putAll(config);
605 }
606
Nathan Harold48ac0972019-03-13 22:33:01 -0700607 mSubscriptionInfoUpdater.updateSubscriptionByCarrierConfigAndNotifyComplete(
608 phoneId, configPackagename, configToSend,
609 mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1));
610 }
611
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800612 private void broadcastConfigChangedIntent(int phoneId) {
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800613 broadcastConfigChangedIntent(phoneId, true);
614 }
615
616 private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800617 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800618 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
chen xuf9db49f2019-03-31 23:04:42 -0700619 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800620 if (addSubIdExtra) {
621 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
622 int[] subIds = SubscriptionManager.getSubId(phoneId);
623 if (!ArrayUtils.isEmpty(subIds)) {
624 TelephonyManager telMgr = TelephonyManager.from(mContext)
625 .createForSubscriptionId(subIds[0]);
626 simApplicationState = telMgr.getSimApplicationState();
627 }
628 // Include subId/carrier id extra only if SIM records are loaded
629 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
630 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
631 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
632 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID,
633 getSpecificCarrierIdForPhoneId(phoneId));
634 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, getCarrierIdForPhoneId(phoneId));
635 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800636 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800637 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Meng Wang97a6a462020-01-23 16:22:16 -0800638 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
Jack Yue9e95be2020-03-22 10:56:06 -0700639 int[] subIds = SubscriptionManager.getSubId(phoneId);
640 if (subIds != null && subIds.length > 0) {
641 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId + ", subId=" + subIds[0]);
642 } else {
643 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
644 }
645
Junda Liu03aad762017-07-21 13:32:17 -0700646 mHasSentConfigChange[phoneId] = true;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800647 }
648
649 /** Binds to the default or carrier config app. */
650 private boolean bindToConfigPackage(String pkgName, int phoneId, int eventId) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800651 logdWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700652 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700653 carrierService.setPackage(pkgName);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800654 mServiceConnection[phoneId] = new CarrierServiceConnection(phoneId, pkgName, eventId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800655 try {
Jordan Liu178430d2020-02-10 14:32:15 -0800656 if (mContext.bindService(carrierService, mServiceConnection[phoneId],
657 Context.BIND_AUTO_CREATE)) {
658 mServiceBound[phoneId] = true;
659 return true;
660 } else {
661 return false;
662 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800663 } catch (SecurityException ex) {
664 return false;
665 }
666 }
667
chen xu02581692018-11-11 19:03:44 -0800668 private CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800669 String mcc = "";
670 String mnc = "";
671 String imsi = "";
672 String gid1 = "";
673 String gid2 = "";
674 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
675 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800676 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800677 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700678 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
679 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800680 mcc = simOperator.substring(0, 3);
681 mnc = simOperator.substring(3);
682 }
683 Phone phone = PhoneFactory.getPhone(phoneId);
684 if (phone != null) {
685 imsi = phone.getSubscriberId();
686 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700687 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800688 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800689 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800690 }
chen xua31f22b2019-03-06 15:28:50 -0800691 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800692 }
693
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700694 /** Returns the package name of a priveleged carrier app, or null if there is none. */
695 private String getCarrierPackageForPhoneId(int phoneId) {
696 List<String> carrierPackageNames = TelephonyManager.from(mContext)
697 .getCarrierPackageNamesForIntentAndPhone(
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700698 new Intent(CarrierService.CARRIER_SERVICE_INTERFACE), phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700699 if (carrierPackageNames != null && carrierPackageNames.size() > 0) {
700 return carrierPackageNames.get(0);
701 } else {
702 return null;
703 }
704 }
705
706 private String getIccIdForPhoneId(int phoneId) {
707 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
708 return null;
709 }
710 Phone phone = PhoneFactory.getPhone(phoneId);
711 if (phone == null) {
712 return null;
713 }
714 return phone.getIccSerialNumber();
715 }
716
chen xu02581692018-11-11 19:03:44 -0800717 /**
chen xua31f22b2019-03-06 15:28:50 -0800718 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800719 */
chen xua31f22b2019-03-06 15:28:50 -0800720 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700721 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800722 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700723 }
724 Phone phone = PhoneFactory.getPhone(phoneId);
725 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800726 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700727 }
chen xua31f22b2019-03-06 15:28:50 -0800728 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800729 }
730
731 /**
732 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
733 */
734 private int getCarrierIdForPhoneId(int phoneId) {
735 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
736 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700737 }
chen xu02581692018-11-11 19:03:44 -0800738 Phone phone = PhoneFactory.getPhone(phoneId);
739 if (phone == null) {
740 return TelephonyManager.UNKNOWN_CARRIER_ID;
741 }
742 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700743 }
744
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700745 /**
746 * Writes a bundle to an XML file.
747 *
chen xu02581692018-11-11 19:03:44 -0800748 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -0800749 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -0800750 * should have a single copy of XML file named after carrier id. However, it's still possible
751 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
752 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
753 * carrier while iccid remains the same.
754 *
755 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
756 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700757 *
758 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700759 *
760 * @param packageName the name of the package from which we fetched this bundle.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100761 * @param extraString An extra string to be used in the XML file name.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800762 * @param phoneId the phone ID.
763 * @param carrierId contains all carrier-identifying information.
764 * @param config the bundle to be written. Null will be treated as an empty bundle.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700765 */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100766 private void saveConfigToXml(String packageName, @NonNull String extraString, int phoneId,
767 CarrierIdentifier carrierId, PersistableBundle config) {
Yuchen Dongc15afed2018-04-26 17:05:22 +0800768 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
769 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700770 loge("Skip save config because SIM records are not loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800771 return;
772 }
773
774 final String iccid = getIccIdForPhoneId(phoneId);
Chen Xucd4a6372019-07-29 16:34:52 -0700775 final int cid = carrierId.getSpecificCarrierId();
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700776 if (packageName == null || iccid == null) {
777 loge("Cannot save config with null packageName or iccid.");
778 return;
779 }
Junda Liu02596502016-11-15 13:46:43 -0800780 // b/32668103 Only save to file if config isn't empty.
781 // In case of failure, not caching an empty bundle will
782 // try loading config again on next power on or sim loaded.
783 // Downside is for genuinely empty bundle, will bind and load
784 // on every power on.
785 if (config == null || config.isEmpty()) {
786 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700787 }
788
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700789 final String version = getPackageVersion(packageName);
790 if (version == null) {
791 loge("Failed to get package version for: " + packageName);
792 return;
793 }
794
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800795 logdWithLocalLog(
796 "Save config to xml, packagename: " + packageName + " phoneId: " + phoneId);
chen xudb04c292019-03-26 17:01:15 -0700797
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700798 FileOutputStream outFile = null;
799 try {
800 outFile = new FileOutputStream(
chen xu02581692018-11-11 19:03:44 -0800801 new File(mContext.getFilesDir(),
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100802 getFilenameForConfig(packageName, extraString, iccid, cid)));
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800803 config.putString(KEY_VERSION, version);
804 config.writeToStream(outFile);
805 outFile.flush();
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700806 outFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800807 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700808 loge(e.toString());
809 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700810 }
811
812 /**
813 * Reads a bundle from an XML file.
814 *
815 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +0800816 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700817 *
818 * In case of errors, or if the saved config is from a different package version than the
819 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700820 *
821 * @param packageName the name of the package from which we fetched this bundle.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100822 * @param extraString An extra string to be used in the XML file name.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800823 * @param phoneId the phone ID.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700824 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800825 * version does not match, or reading config fails.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700826 */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100827 private PersistableBundle restoreConfigFromXml(String packageName, @NonNull String extraString,
828 int phoneId) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700829 final String version = getPackageVersion(packageName);
830 if (version == null) {
831 loge("Failed to get package version for: " + packageName);
832 return null;
833 }
Yuchen Dongc15afed2018-04-26 17:05:22 +0800834 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
835 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700836 loge("Skip restoring config because SIM records are not yet loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800837 return null;
838 }
839
840 final String iccid = getIccIdForPhoneId(phoneId);
chen xua31f22b2019-03-06 15:28:50 -0800841 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700842 if (packageName == null || iccid == null) {
843 loge("Cannot restore config with null packageName or iccid.");
844 return null;
845 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700846
847 PersistableBundle restoredBundle = null;
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100848 File file = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700849 FileInputStream inFile = null;
850 try {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100851 file = new File(mContext.getFilesDir(),
852 getFilenameForConfig(packageName, extraString, iccid, cid));
853 inFile = new FileInputStream(file);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700854
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800855 restoredBundle = PersistableBundle.readFromStream(inFile);
856 String savedVersion = restoredBundle.getString(KEY_VERSION);
857 restoredBundle.remove(KEY_VERSION);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700858
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800859 if (!version.equals(savedVersion)) {
860 loge("Saved version mismatch: " + version + " vs " + savedVersion);
861 restoredBundle = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700862 }
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800863
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700864 inFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800865 } catch (FileNotFoundException e) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100866 // Missing file is normal occurrence that might occur with a new sim or when restoring
867 // an override file during boot and should not be treated as an error.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800868 if (file != null) logd("File not found: " + file.getPath());
869 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700870 loge(e.toString());
871 }
872
873 return restoredBundle;
874 }
875
Junda Liu8a8a53a2015-05-15 16:19:57 -0700876 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700877 * Clears cached carrier config.
878 * This deletes all saved XML files associated with the given package name. If packageName is
879 * null, then it deletes all saved XML files.
880 *
881 * @param packageName the name of a carrier package, or null if all cached config should be
882 * cleared.
883 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -0700884 */
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700885 private boolean clearCachedConfigForPackage(final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700886 File dir = mContext.getFilesDir();
887 File[] packageFiles = dir.listFiles(new FilenameFilter() {
888 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700889 if (packageName != null) {
890 return filename.startsWith("carrierconfig-" + packageName + "-");
891 } else {
892 return filename.startsWith("carrierconfig-");
893 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700894 }
895 });
Junda Liu8a8a53a2015-05-15 16:19:57 -0700896 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700897 for (File f : packageFiles) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800898 logd("Deleting " + f.getName());
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700899 f.delete();
900 }
Junda Liu8a8a53a2015-05-15 16:19:57 -0700901 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700902 }
903
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700904 /** Builds a canonical file name for a config file. */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100905 private String getFilenameForConfig(@NonNull String packageName, @NonNull String extraString,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800906 @NonNull String iccid, int cid) {
chen xu02581692018-11-11 19:03:44 -0800907 // the same carrier should have a single copy of XML file named after carrier id.
908 // However, it's still possible that platform doesn't recognize the current sim carrier,
909 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
910 // cases SIM OTA resolves to different carrier while iccid remains the same.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100911 return "carrierconfig-" + packageName + extraString + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700912 }
913
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700914 /** Return the current version code of a package, or null if the name is not found. */
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700915 private String getPackageVersion(String packageName) {
916 try {
917 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -0800918 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700919 } catch (PackageManager.NameNotFoundException e) {
920 return null;
921 }
922 }
923
Jonathan Basseri65273c82017-07-25 15:08:42 -0700924 /**
925 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700926 *
927 * This reads config bundles for the given phoneId. That means getting the latest bundle from
928 * the default app and a privileged carrier app, if present. This will not bind to an app if we
929 * have a saved config file to use instead.
930 */
931 private void updateConfigForPhoneId(int phoneId) {
Junda Liu8a8a53a2015-05-15 16:19:57 -0700932 // Clear in-memory cache for carrier app config, so when carrier app gets uninstalled, no
933 // stale config is left.
934 if (mConfigFromCarrierApp[phoneId] != null &&
935 getCarrierPackageForPhoneId(phoneId) == null) {
936 mConfigFromCarrierApp[phoneId] = null;
937 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700938 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700939 }
940
Malcolm Chen5ea18532019-10-24 19:55:44 -0700941 private void onMultiSimConfigChanged() {
942 for (int i = TelephonyManager.from(mContext).getActiveModemCount();
943 i < mConfigFromDefaultApp.length; i++) {
944 clearConfigForPhone(i, false);
945 }
946 }
947
Hall Liu506724b2018-10-22 18:16:14 -0700948 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800949 @NonNull
950 public PersistableBundle getConfigForSubId(int subId, String callingPackage) {
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -0800951 return getConfigForSubIdWithFeature(subId, callingPackage, null);
952 }
953
954 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800955 @NonNull
956 public PersistableBundle getConfigForSubIdWithFeature(int subId, String callingPackage,
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -0800957 String callingFeatureId) {
958 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subId, callingPackage,
959 callingFeatureId, "getCarrierConfig")) {
Malcolm Chen6d3d6b32018-03-01 13:58:03 -0800960 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -0700961 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -0700962
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800963 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700964 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800965 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700966 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800967 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700968 retConfig.putAll(config);
yinxud0104832019-08-16 14:20:08 -0700969 if (getCarrierPackageForPhoneId(phoneId) == null) {
970 retConfig.putBoolean(
971 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
972 }
yinxuc5e27622017-11-29 15:08:50 -0800973 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800974 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800975 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700976 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -0800977 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
978 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100979 config = mPersistentOverrideConfigs[phoneId];
980 if (config != null) {
981 retConfig.putAll(config);
982 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
983 }
Hall Liu506724b2018-10-22 18:16:14 -0700984 config = mOverrideConfigs[phoneId];
985 if (config != null) {
986 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -0700987 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800988 }
989 return retConfig;
990 }
991
992 @Override
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100993 public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides,
994 boolean persistent) {
Hall Liu506724b2018-10-22 18:16:14 -0700995 mContext.enforceCallingOrSelfPermission(
996 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Brad Ebingerf6281ad2019-04-25 11:02:04 -0700997 //TODO: Also check for SHELL UID to restrict this method to testing only (b/131326259)
Hall Liu506724b2018-10-22 18:16:14 -0700998 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
999 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001000 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
Hall Liu506724b2018-10-22 18:16:14 -07001001 return;
1002 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001003 overrideConfig(mOverrideConfigs, phoneId, overrides);
Hall Liu506724b2018-10-22 18:16:14 -07001004
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001005 if (persistent) {
1006 overrideConfig(mPersistentOverrideConfigs, phoneId, overrides);
1007
1008 if (overrides != null) {
1009 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
1010 saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId,
1011 carrierId, mPersistentOverrideConfigs[phoneId]);
1012 } else {
1013 final String iccid = getIccIdForPhoneId(phoneId);
1014 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1015 String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage,
1016 OVERRIDE_PACKAGE_ADDITION, iccid, cid);
1017 File fileToDelete = new File(mContext.getFilesDir(), fileName);
1018 fileToDelete.delete();
1019 }
Hall Liu506724b2018-10-22 18:16:14 -07001020 }
Brad Ebingerc9d1fa12019-04-17 15:21:18 -07001021 notifySubscriptionInfoUpdater(phoneId);
Hall Liu506724b2018-10-22 18:16:14 -07001022 }
1023
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001024 private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId,
1025 @Nullable PersistableBundle overrides) {
1026 if (overrides == null) {
1027 currentOverrides[phoneId] = new PersistableBundle();
1028 } else if (currentOverrides[phoneId] == null) {
1029 currentOverrides[phoneId] = overrides;
1030 } else {
1031 currentOverrides[phoneId].putAll(overrides);
1032 }
1033 }
1034
Hall Liu506724b2018-10-22 18:16:14 -07001035 @Override
Jonathan Basseri86030352015-06-08 12:27:56 -07001036 public void notifyConfigChangedForSubId(int subId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001037 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001038 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001039 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001040 return;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001041 }
Jordan Liud7d57fe2019-04-16 12:29:43 -07001042
1043 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001044 // system privileged app with MODIFY_PHONE_STATE permission.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001045 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext, subId,
1046 "Require carrier privileges or MODIFY_PHONE_STATE permission.");
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001047
1048 // This method should block until deleting has completed, so that an error which prevents us
1049 // from clearing the cache is passed back to the carrier app. With the files successfully
1050 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001051 String callingPackageName = mContext.getPackageManager().getNameForUid(
1052 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001053 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001054 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001055 }
1056
1057 @Override
1058 public void updateConfigForPhoneId(int phoneId, String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -07001059 mContext.enforceCallingOrSelfPermission(
1060 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001061 logdWithLocalLog("Update config for phoneId: " + phoneId + " simState: " + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001062 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1063 return;
1064 }
1065 // requires Java 7 for switch on string.
1066 switch (simState) {
1067 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1068 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001069 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001070 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Malcolm Chendb66de12019-12-09 18:57:07 -08001071 case IccCardConstants.INTENT_VALUE_ICC_NOT_READY:
Junda Liu9f2d2712015-05-15 14:22:45 -07001072 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001073 break;
1074 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1075 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001076 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001077 break;
1078 }
1079 }
1080
Junda Liu43d723a2015-05-12 17:23:45 -07001081 @Override
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001082 public String getDefaultCarrierServicePackageName() {
Shuo Qianefdb7962019-12-19 15:54:27 -08001083 mContext.enforceCallingOrSelfPermission(
1084 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1085 "getDefaultCarrierServicePackageName");
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001086 return mPlatformCarrierConfigPackage;
1087 }
1088
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001089 private void unbindIfBound(Context context, CarrierServiceConnection conn,
1090 int phoneId) {
1091 if (mServiceBound[phoneId]) {
1092 mServiceBound[phoneId] = false;
1093 context.unbindService(conn);
1094 }
1095 }
1096
1097 /**
1098 * If {@code args} contains {@link #DUMP_ARG_REQUESTING_PACKAGE} and a following package name,
1099 * we'll also call {@link IBinder#dump} on the default carrier service (if bound) and the
1100 * specified carrier service (if bound). Typically, this is done for connectivity bug reports
1101 * where we don't call {@code dumpsys activity service all-non-platform} because that contains
1102 * too much info, but we still want to let carrier apps include their diagnostics.
1103 */
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001104 @Override
Junda Liu43d723a2015-05-12 17:23:45 -07001105 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001106 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
Junda Liu43d723a2015-05-12 17:23:45 -07001107 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1108 != PackageManager.PERMISSION_GRANTED) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001109 indentPW.println("Permission Denial: can't dump carrierconfig from from pid="
Junda Liu43d723a2015-05-12 17:23:45 -07001110 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1111 return;
1112 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001113 String requestingPackage = null;
1114 int requestingPackageIndex = ArrayUtils.indexOf(args, DUMP_ARG_REQUESTING_PACKAGE);
1115 if (requestingPackageIndex >= 0 && requestingPackageIndex < args.length - 1
1116 && !TextUtils.isEmpty(args[requestingPackageIndex + 1])) {
1117 requestingPackage = args[requestingPackageIndex + 1];
1118 // Throws a SecurityException if the caller is impersonating another app in an effort to
1119 // dump extra info (which may contain PII the caller doesn't have a right to).
1120 enforceCallerIsSystemOrRequestingPackage(requestingPackage);
1121 }
1122
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001123 indentPW.println("CarrierConfigLoader: " + this);
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001124 for (int i = 0; i < TelephonyManager.from(mContext).getActiveModemCount(); i++) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001125 indentPW.println("Phone Id = " + i);
shuoq26a3a4c2016-12-16 11:06:48 -08001126 // display default values in CarrierConfigManager
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001127 printConfig(CarrierConfigManager.getDefaultConfig(), indentPW,
shuoq26a3a4c2016-12-16 11:06:48 -08001128 "Default Values from CarrierConfigManager");
shuoq26a3a4c2016-12-16 11:06:48 -08001129 // display ConfigFromDefaultApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001130 printConfig(mConfigFromDefaultApp[i], indentPW, "mConfigFromDefaultApp");
shuoq26a3a4c2016-12-16 11:06:48 -08001131 // display ConfigFromCarrierApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001132 printConfig(mConfigFromCarrierApp[i], indentPW, "mConfigFromCarrierApp");
1133 printConfig(mPersistentOverrideConfigs[i], indentPW, "mPersistentOverrideConfigs");
1134 printConfig(mOverrideConfigs[i], indentPW, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001135 }
chen xudb04c292019-03-26 17:01:15 -07001136
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001137 indentPW.println("CarrierConfigLoadingLog=");
1138 mCarrierConfigLoadingLog.dump(fd, indentPW, args);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001139
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001140 if (requestingPackage != null) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001141 logd("Including default and requesting package " + requestingPackage
1142 + " carrier services in dump");
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001143 indentPW.println("");
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001144 indentPW.println("Connected services");
1145 dumpCarrierServiceIfBound(fd, indentPW, "Default config package",
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001146 mPlatformCarrierConfigPackage, false /* considerCarrierPrivileges */);
1147 dumpCarrierServiceIfBound(fd, indentPW, "Requesting package", requestingPackage,
1148 true /* considerCarrierPrivileges */);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001149 }
shuoq26a3a4c2016-12-16 11:06:48 -08001150 }
1151
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001152 private void printConfig(PersistableBundle configApp, IndentingPrintWriter indentPW,
1153 String name) {
1154 indentPW.increaseIndent();
shuoq26a3a4c2016-12-16 11:06:48 -08001155 if (configApp == null) {
shuoq26a3a4c2016-12-16 11:06:48 -08001156 indentPW.println(name + " : null ");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001157 indentPW.decreaseIndent();
1158 indentPW.println("");
shuoq26a3a4c2016-12-16 11:06:48 -08001159 return;
1160 }
shuoq26a3a4c2016-12-16 11:06:48 -08001161 indentPW.println(name + " : ");
1162 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1163 Collections.sort(sortedKeys);
1164 indentPW.increaseIndent();
1165 indentPW.increaseIndent();
1166 for (String key : sortedKeys) {
1167 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1168 indentPW.println(key + " = " +
1169 Arrays.toString((Object[]) configApp.get(key)));
1170 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1171 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1172 } else {
1173 indentPW.println(key + " = " + configApp.get(key));
1174 }
Junda Liu43d723a2015-05-12 17:23:45 -07001175 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001176 indentPW.decreaseIndent();
1177 indentPW.decreaseIndent();
1178 indentPW.decreaseIndent();
1179 indentPW.println("");
Junda Liu43d723a2015-05-12 17:23:45 -07001180 }
1181
Hunter Knepshielde601f432020-02-19 10:16:04 -08001182 /**
1183 * Passes without problem when one of these conditions is true:
1184 * - The caller is a privileged UID (e.g. for dumpstate.cpp generating a bug report, where the
1185 * system knows the true caller plumbed in through the {@link android.os.BugreportManager} API).
1186 * - The caller's UID matches the supplied package.
1187 *
1188 * @throws SecurityException if none of the above conditions are met.
1189 */
1190 private void enforceCallerIsSystemOrRequestingPackage(String requestingPackage)
1191 throws SecurityException {
1192 final int callingUid = Binder.getCallingUid();
1193 if (callingUid == Process.ROOT_UID || callingUid == Process.SYSTEM_UID
1194 || callingUid == Process.SHELL_UID || callingUid == Process.PHONE_UID) {
1195 // Bug reports (dumpstate.cpp) run as SHELL, and let some other privileged UIDs through
1196 // as well.
1197 return;
1198 }
1199 // An app is trying to dump extra detail, block it if they aren't who they claim to be.
1200 AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class);
1201 if (appOps == null) {
1202 throw new SecurityException("No AppOps");
1203 }
1204 // Will throw a SecurityException if the UID and package don't match.
1205 appOps.checkPackage(callingUid, requestingPackage);
1206 }
1207
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001208 /**
1209 * Searches for one or more appropriate {@link CarrierService} instances to dump based on the
1210 * current connections.
1211 *
1212 * @param targetPkgName the target package name to dump carrier services for
1213 * @param considerCarrierPrivileges if true, allow a carrier service to be dumped if it shares
1214 * carrier privileges with {@code targetPkgName};
1215 * otherwise, only dump a carrier service if it is {@code
1216 * targetPkgName}
1217 */
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001218 private void dumpCarrierServiceIfBound(FileDescriptor fd, IndentingPrintWriter indentPW,
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001219 String prefix, String targetPkgName, boolean considerCarrierPrivileges) {
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001220 // Null package is possible if it's early in the boot process, there was a recent crash, we
1221 // loaded the config from XML most recently, or a SIM slot is empty. Carrier apps with
1222 // long-lived bindings should typically get dumped here regardless. Even if an app is being
1223 // used for multiple phoneIds, we assume that it's smart enough to handle that on its own,
1224 // and that in most cases we'd just be dumping duplicate information and bloating a report.
1225 indentPW.increaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001226 indentPW.println(prefix + " : " + targetPkgName);
1227 Set<String> dumpedPkgNames = new ArraySet<>(mServiceConnection.length);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001228 for (CarrierServiceConnection connection : mServiceConnection) {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001229 if (connection == null || !SubscriptionManager.isValidPhoneId(connection.phoneId)
1230 || TextUtils.isEmpty(connection.pkgName)) {
1231 continue;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001232 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001233 final String servicePkgName = connection.pkgName;
1234 // Note: we intentionally ignore system components here because we should NOT match the
1235 // shell caller that's typically used for bug reports via non-BugreportManager triggers.
1236 final boolean exactPackageMatch = TextUtils.equals(targetPkgName, servicePkgName);
1237 final boolean carrierPrivilegesMatch =
1238 considerCarrierPrivileges && hasCarrierPrivileges(targetPkgName,
1239 connection.phoneId);
1240 if (!exactPackageMatch && !carrierPrivilegesMatch) continue;
1241 // Make sure this service is actually alive before trying to dump it. We don't pay
1242 // attention to mServiceBound[connection.phoneId] because typically carrier apps will
1243 // request long-lived bindings, and even if we unbind the app, it may still be alive due
1244 // to CarrierServiceBindHelper. Pull it out as a reference so even if it gets set to
1245 // null within the ServiceConnection during unbinding we can avoid an NPE.
1246 final IBinder service = connection.service;
1247 if (service == null || !service.isBinderAlive() || !service.pingBinder()) continue;
1248 // We've got a live service. Last check is just to make sure we don't dump a package
1249 // multiple times.
1250 if (!dumpedPkgNames.add(servicePkgName)) continue;
1251 if (!exactPackageMatch) {
1252 logd(targetPkgName + " has carrier privileges on phoneId " + connection.phoneId
1253 + ", service provided by " + servicePkgName);
1254 indentPW.increaseIndent();
1255 indentPW.println("Proxy : " + servicePkgName);
1256 indentPW.decreaseIndent();
1257 }
1258 // Flush before we let the app output anything to ensure correct ordering of output.
1259 // Internally, Binder#dump calls flush on its printer after finishing so we don't
1260 // need to do anything after.
1261 indentPW.flush();
1262 try {
1263 logd("Dumping " + servicePkgName);
1264 // We don't need to give the carrier service any args.
1265 connection.service.dump(fd, null /* args */);
1266 logd("Done with " + servicePkgName);
1267 } catch (RemoteException e) {
1268 logd("RemoteException from " + servicePkgName, e);
1269 indentPW.increaseIndent();
1270 indentPW.println("RemoteException");
1271 indentPW.increaseIndent();
1272 e.printStackTrace(indentPW);
1273 indentPW.decreaseIndent();
1274 indentPW.decreaseIndent();
1275 // We won't retry this package again because now it's in dumpedPkgNames.
1276 }
1277 indentPW.println("");
Jordan Liu46ed5db2020-01-28 08:55:20 -08001278 }
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001279 if (dumpedPkgNames.isEmpty()) {
1280 indentPW.increaseIndent();
1281 indentPW.println("Not bound");
1282 indentPW.decreaseIndent();
1283 indentPW.println("");
1284 }
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001285 indentPW.decreaseIndent();
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001286 }
1287
1288 private boolean hasCarrierPrivileges(String pkgName, int phoneId) {
1289 int[] subIds = SubscriptionManager.getSubId(phoneId);
1290 if (ArrayUtils.isEmpty(subIds)) {
1291 return false;
1292 }
1293 return TelephonyManager.from(mContext).createForSubscriptionId(
1294 subIds[0]).checkCarrierPrivilegesForPackage(pkgName)
1295 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001296 }
1297
Zach Johnson36d7aab2015-05-22 15:43:00 -07001298 private class CarrierServiceConnection implements ServiceConnection {
Hunter Knepshieldeefb31b2020-02-19 15:30:54 -08001299 final int phoneId;
1300 final String pkgName;
1301 final int eventId;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001302 IBinder service;
1303
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001304 CarrierServiceConnection(int phoneId, String pkgName, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001305 this.phoneId = phoneId;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001306 this.pkgName = pkgName;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001307 this.eventId = eventId;
1308 }
1309
1310 @Override
1311 public void onServiceConnected(ComponentName name, IBinder service) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001312 logd("Connected to config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001313 this.service = service;
1314 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1315 }
1316
1317 @Override
1318 public void onServiceDisconnected(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001319 logd("Disconnected from config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001320 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001321 }
1322
1323 @Override
1324 public void onBindingDied(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001325 logd("Binding died from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001326 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001327 }
1328
1329 @Override
1330 public void onNullBinding(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001331 logd("Null binding from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001332 this.service = null;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001333 }
1334 }
1335
1336 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1337 @Override
1338 public void onReceive(Context context, Intent intent) {
1339 String action = intent.getAction();
Junda Liu8a8a53a2015-05-15 16:19:57 -07001340 boolean replace = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1341 // If replace is true, only care ACTION_PACKAGE_REPLACED.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001342 if (replace && !Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
Junda Liu8a8a53a2015-05-15 16:19:57 -07001343 return;
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001344 }
Junda Liu8a8a53a2015-05-15 16:19:57 -07001345
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001346 switch (action) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001347 case Intent.ACTION_BOOT_COMPLETED:
1348 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1349 break;
1350
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001351 case Intent.ACTION_PACKAGE_ADDED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001352 case Intent.ACTION_PACKAGE_REMOVED:
Junda Liu8a8a53a2015-05-15 16:19:57 -07001353 case Intent.ACTION_PACKAGE_REPLACED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001354 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1355 String packageName = mContext.getPackageManager().getNameForUid(uid);
Junda Liu8a8a53a2015-05-15 16:19:57 -07001356 if (packageName != null) {
1357 // We don't have a phoneId for arg1.
1358 mHandler.sendMessage(
1359 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, packageName));
1360 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001361 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001362 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001363 }
1364 }
1365
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001366 private void logd(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001367 Log.d(LOG_TAG, msg);
1368 }
1369
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001370 private void logd(String msg, Throwable tr) {
1371 Log.d(LOG_TAG, msg, tr);
1372 }
1373
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001374 private void logdWithLocalLog(String msg) {
chen xudb04c292019-03-26 17:01:15 -07001375 Log.d(LOG_TAG, msg);
1376 mCarrierConfigLoadingLog.log(msg);
1377 }
1378
1379 private void loge(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001380 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07001381 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001382 }
1383}