blob: 1ccfe7bca4b457d7f987821f4cc493f06b120760 [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;
chen xudb04c292019-03-26 17:01:15 -070053import android.util.LocalLog;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080054import android.util.Log;
55
56import com.android.internal.telephony.ICarrierConfigLoader;
57import com.android.internal.telephony.IccCardConstants;
58import com.android.internal.telephony.Phone;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080059import com.android.internal.telephony.PhoneFactory;
Nathan Harold48ac0972019-03-13 22:33:01 -070060import com.android.internal.telephony.SubscriptionInfoUpdater;
Jeff Davidsona8e4e242018-03-15 17:16:18 -070061import com.android.internal.telephony.TelephonyPermissions;
Meng Wanga320b942019-11-25 11:21:26 -080062import com.android.internal.telephony.util.ArrayUtils;
shuoq26a3a4c2016-12-16 11:06:48 -080063import com.android.internal.util.IndentingPrintWriter;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080064
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070065import java.io.File;
Junda Liu43d723a2015-05-12 17:23:45 -070066import java.io.FileDescriptor;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070067import java.io.FileInputStream;
68import java.io.FileNotFoundException;
69import java.io.FileOutputStream;
Jonathan Basseri1f743c92015-05-15 00:19:46 -070070import java.io.FilenameFilter;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070071import java.io.IOException;
Junda Liu43d723a2015-05-12 17:23:45 -070072import java.io.PrintWriter;
shuoq26a3a4c2016-12-16 11:06:48 -080073import java.util.ArrayList;
74import java.util.Arrays;
75import java.util.Collections;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080076import java.util.List;
77
78/**
79 * CarrierConfigLoader binds to privileged carrier apps to fetch carrier config overlays.
Jonathan Basseri6465afd2015-02-25 13:05:57 -080080 */
Jonathan Basseri6465afd2015-02-25 13:05:57 -080081public class CarrierConfigLoader extends ICarrierConfigLoader.Stub {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070082 private static final String LOG_TAG = "CarrierConfigLoader";
Meng Wang33ad2bc2017-03-16 20:21:20 -070083
84 // Package name for platform carrier config app, bundled with system image.
85 private final String mPlatformCarrierConfigPackage;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080086
87 /** The singleton instance. */
88 private static CarrierConfigLoader sInstance;
89 // The context for phone app, passed from PhoneGlobals.
90 private Context mContext;
91 // Carrier configs from default app, indexed by phoneID.
Jonathan Basseric31f1f32015-05-12 10:13:03 -070092 private PersistableBundle[] mConfigFromDefaultApp;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080093 // Carrier configs from privileged carrier config app, indexed by phoneID.
Jonathan Basseric31f1f32015-05-12 10:13:03 -070094 private PersistableBundle[] mConfigFromCarrierApp;
Torbjorn Eklund723527a2019-02-13 11:16:25 +010095 // Persistent Carrier configs that are provided via the override test API, indexed by phone ID.
96 private PersistableBundle[] mPersistentOverrideConfigs;
Hall Liu506724b2018-10-22 18:16:14 -070097 // Carrier configs that are provided via the override test API, indexed by phone ID.
98 private PersistableBundle[] mOverrideConfigs;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080099 // Service connection for binding to config app.
Zach Johnson36d7aab2015-05-22 15:43:00 -0700100 private CarrierServiceConnection[] mServiceConnection;
Jordan Liu178430d2020-02-10 14:32:15 -0800101 // Whether we are bound to a service for each phone
102 private boolean[] mServiceBound;
Junda Liu03aad762017-07-21 13:32:17 -0700103 // Whether we have sent config change bcast for each phone id.
104 private boolean[] mHasSentConfigChange;
Nathan Harold48ac0972019-03-13 22:33:01 -0700105 // SubscriptionInfoUpdater
106 private final SubscriptionInfoUpdater mSubscriptionInfoUpdater;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800107
Nanxi Chen3d670502016-03-17 16:32:09 -0700108 // Broadcast receiver for Boot intents, register intent filter in construtor.
109 private final BroadcastReceiver mBootReceiver = new ConfigLoaderBroadcastReceiver();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800110 // Broadcast receiver for SIM and pkg intents, register intent filter in constructor.
Nanxi Chen3d670502016-03-17 16:32:09 -0700111 private final BroadcastReceiver mPackageReceiver = new ConfigLoaderBroadcastReceiver();
Jack Yu67663de2019-10-17 15:19:48 -0700112 private final LocalLog mCarrierConfigLoadingLog = new LocalLog(100);
chen xudb04c292019-03-26 17:01:15 -0700113
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800114
115 // Message codes; see mHandler below.
116 // Request from SubscriptionInfoUpdater when SIM becomes absent or error.
117 private static final int EVENT_CLEAR_CONFIG = 0;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800118 // Has connected to default app.
119 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
120 // Has connected to carrier app.
121 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700122 // Config has been loaded from default app (or cache).
123 private static final int EVENT_FETCH_DEFAULT_DONE = 5;
124 // Config has been loaded from carrier app (or cache).
125 private static final int EVENT_FETCH_CARRIER_DONE = 6;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700126 // Attempt to fetch from default app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700127 private static final int EVENT_DO_FETCH_DEFAULT = 7;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700128 // Attempt to fetch from carrier app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700129 private static final int EVENT_DO_FETCH_CARRIER = 8;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700130 // A package has been installed, uninstalled, or updated.
131 private static final int EVENT_PACKAGE_CHANGED = 9;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700132 // Bind timed out for the default app.
133 private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
134 // Bind timed out for a carrier app.
135 private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700136 // Check if the system fingerprint has changed.
137 private static final int EVENT_CHECK_SYSTEM_UPDATE = 12;
Nanxi Chen3d670502016-03-17 16:32:09 -0700138 // Rerun carrier config binding after system is unlocked.
139 private static final int EVENT_SYSTEM_UNLOCKED = 13;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700140 // Fetching config timed out from the default app.
141 private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14;
142 // Fetching config timed out from a carrier app.
143 private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15;
Nathan Harold48ac0972019-03-13 22:33:01 -0700144 // SubscriptionInfoUpdater has finished updating the sub for the carrier config.
145 private static final int EVENT_SUBSCRIPTION_INFO_UPDATED = 16;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700146 // Multi-SIM config changed.
147 private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 17;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700148
Junda Liu6cb45002016-03-22 17:18:20 -0700149 private static final int BIND_TIMEOUT_MILLIS = 30000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800150
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800151 // Keys used for saving and restoring config bundle from file.
152 private static final String KEY_VERSION = "__carrier_config_package_version__";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800153
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100154 private static final String OVERRIDE_PACKAGE_ADDITION = "-override";
155
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700156 // SharedPreferences key for last known build fingerprint.
157 private static final String KEY_FINGERPRINT = "build_fingerprint";
158
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800159 // Argument for #dump that indicates we should also call the default and specified carrier
160 // service's #dump method. In multi-SIM devices, it's possible that carrier A is on SIM 1 and
161 // carrier B is on SIM 2, in which case we should not dump carrier B's service when carrier A
162 // requested the dump.
163 private static final String DUMP_ARG_REQUESTING_PACKAGE = "--requesting-package";
164
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800165 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700166 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700167 // For each phoneId, the event sequence should be:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700168 // fetch default, connected to default, fetch default (async), fetch default done,
169 // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700170 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700171 // If there is a saved config file for either the default app or the carrier app, we skip
172 // binding to the app and go straight from fetch to loaded.
173 //
174 // At any time, at most one connection is active. If events are not in this order, previous
175 // connection will be unbound, so only latest event takes effect.
176 //
177 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
178 // 1. loading from carrier app (even if read from a file)
179 // 2. loading from default app if there is no carrier app (even if read from a file)
180 // 3. clearing config (e.g. due to sim removal)
181 // 4. encountering bind or IPC error
Jonathan Basseri65273c82017-07-25 15:08:42 -0700182 private class ConfigHandler extends Handler {
183 @Override
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800184 public void handleMessage(Message msg) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700185 final int phoneId = msg.arg1;
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800186 logdWithLocalLog("mHandler: " + msg.what + " phoneId: " + phoneId);
Malcolm Chen5ea18532019-10-24 19:55:44 -0700187 if (!SubscriptionManager.isValidPhoneId(phoneId)
188 && msg.what != EVENT_MULTI_SIM_CONFIG_CHANGED) {
189 return;
190 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800191 switch (msg.what) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800192 case EVENT_CLEAR_CONFIG: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700193 clearConfigForPhone(phoneId, true);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800194 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700195 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700196
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800197 case EVENT_SYSTEM_UNLOCKED: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700198 for (int i = 0; i < TelephonyManager.from(mContext).getActiveModemCount();
199 ++i) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700200 // When user unlock device, we should only try to send broadcast again if we
201 // have sent it before unlock. This will avoid we try to load carrier config
202 // when SIM is still loading when unlock happens.
Junda Liu03aad762017-07-21 13:32:17 -0700203 if (mHasSentConfigChange[i]) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800204 logdWithLocalLog("System unlocked");
Junda Liu03aad762017-07-21 13:32:17 -0700205 updateConfigForPhoneId(i);
206 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700207 }
208 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700209 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700210
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800211 case EVENT_PACKAGE_CHANGED: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700212 final String carrierPackageName = (String) msg.obj;
213 // Only update if there are cached config removed to avoid updating config for
214 // unrelated packages.
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700215 if (clearCachedConfigForPackage(carrierPackageName)) {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700216 int numPhones = TelephonyManager.from(mContext).getActiveModemCount();
Junda Liu8a8a53a2015-05-15 16:19:57 -0700217 for (int i = 0; i < numPhones; ++i) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800218 logdWithLocalLog("Package changed: " + carrierPackageName
Jack Yu67663de2019-10-17 15:19:48 -0700219 + ", phone=" + i);
Junda Liu8a8a53a2015-05-15 16:19:57 -0700220 updateConfigForPhoneId(i);
221 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700222 }
223 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700224 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700225
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800226 case EVENT_DO_FETCH_DEFAULT: {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100227 // Restore persistent override values.
228 PersistableBundle config = restoreConfigFromXml(
229 mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId);
230 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800231 logd("Loaded persistent override config from XML. package="
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100232 + mPlatformCarrierConfigPackage
233 + " phoneId=" + phoneId);
234 mPersistentOverrideConfigs[phoneId] = config;
235 }
236
237 config = restoreConfigFromXml(mPlatformCarrierConfigPackage, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700238 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800239 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700240 "Loaded config from XML. package="
241 + mPlatformCarrierConfigPackage
242 + " phoneId="
243 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700244 mConfigFromDefaultApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700245 Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700246 newMsg.getData().putBoolean("loaded_from_xml", true);
247 mHandler.sendMessage(newMsg);
248 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700249 // No cached config, so fetch it from the default app.
250 if (bindToConfigPackage(
251 mPlatformCarrierConfigPackage,
252 phoneId,
253 EVENT_CONNECTED_TO_DEFAULT)) {
254 sendMessageDelayed(
255 obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700256 BIND_TIMEOUT_MILLIS);
257 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700258 // Send broadcast if bind fails.
Nathan Harold48ac0972019-03-13 22:33:01 -0700259 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700260 // TODO: We *must* call unbindService even if bindService returns false.
261 // (And possibly if SecurityException was thrown.)
chen xudb04c292019-03-26 17:01:15 -0700262 loge("binding to default app: "
263 + mPlatformCarrierConfigPackage + " fails");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700264 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800265 }
266 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700267 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800268
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800269 case EVENT_CONNECTED_TO_DEFAULT: {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700270 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700271 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800272 // If new service connection has been created, unbind.
273 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Jordan Liu178430d2020-02-10 14:32:15 -0800274 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800275 break;
276 }
chen xu02581692018-11-11 19:03:44 -0800277 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700278 // ResultReceiver callback will execute in this Handler's thread.
279 final ResultReceiver resultReceiver =
280 new ResultReceiver(this) {
281 @Override
282 public void onReceiveResult(int resultCode, Bundle resultData) {
Jordan Liu178430d2020-02-10 14:32:15 -0800283 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700284 // If new service connection has been created, this is stale.
285 if (mServiceConnection[phoneId] != conn) {
286 loge("Received response for stale request.");
287 return;
288 }
289 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
290 if (resultCode == RESULT_ERROR || resultData == null) {
291 // On error, abort config fetching.
292 loge("Failed to get carrier config");
Nathan Harold48ac0972019-03-13 22:33:01 -0700293 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700294 return;
295 }
296 PersistableBundle config =
297 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100298 saveConfigToXml(mPlatformCarrierConfigPackage, "", phoneId,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800299 carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700300 mConfigFromDefaultApp[phoneId] = config;
301 sendMessage(
302 obtainMessage(
303 EVENT_FETCH_DEFAULT_DONE, phoneId, -1));
304 }
305 };
306 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800307 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700308 ICarrierService carrierService =
309 ICarrierService.Stub.asInterface(conn.service);
310 carrierService.getCarrierConfig(carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800311 logdWithLocalLog("Fetch config for default app: "
chen xudb04c292019-03-26 17:01:15 -0700312 + mPlatformCarrierConfigPackage
313 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700314 } catch (RemoteException e) {
chen xudb04c292019-03-26 17:01:15 -0700315 loge("Failed to get carrier config from default app: " +
316 mPlatformCarrierConfigPackage + " err: " + e.toString());
Jordan Liu178430d2020-02-10 14:32:15 -0800317 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700318 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800319 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700320 sendMessageDelayed(
321 obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1),
322 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800323 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700324 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800325
Jonathan Basseri930701e2015-06-11 20:56:43 -0700326 case EVENT_BIND_DEFAULT_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800327 case EVENT_FETCH_DEFAULT_TIMEOUT: {
328 loge("Bind/fetch time out from " + mPlatformCarrierConfigPackage);
chen xuf60b3162019-05-01 21:31:05 -0700329 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
330 // If we attempted to bind to the app, but the service connection is null due to
331 // the race condition that clear config event happens before bind/fetch complete
332 // then config was cleared while we were waiting and we should not continue.
333 if (mServiceConnection[phoneId] != null) {
334 // If a ResponseReceiver callback is in the queue when this happens, we will
335 // unbind twice and throw an exception.
Jordan Liu178430d2020-02-10 14:32:15 -0800336 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700337 broadcastConfigChangedIntent(phoneId);
338 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700339 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700340 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700341 }
Jonathan Basseri930701e2015-06-11 20:56:43 -0700342
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800343 case EVENT_FETCH_DEFAULT_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700344 // If we attempted to bind to the app, but the service connection is null, then
345 // config was cleared while we were waiting and we should not continue.
346 if (!msg.getData().getBoolean("loaded_from_xml", false)
347 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800348 break;
349 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700350 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700351 if (carrierPackageName != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800352 logd("Found carrier config app: " + carrierPackageName);
Jordan Liu38a614c2019-03-20 15:01:17 -0700353 sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId, -1));
Jonathan Basserib919e932015-05-27 16:53:08 +0000354 } else {
Nathan Harold48ac0972019-03-13 22:33:01 -0700355 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700356 }
357 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700358 }
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700359
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800360 case EVENT_DO_FETCH_CARRIER: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700361 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700362 final PersistableBundle config =
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100363 restoreConfigFromXml(carrierPackageName, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700364 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800365 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700366 "Loaded config from XML. package="
367 + carrierPackageName
368 + " phoneId="
369 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700370 mConfigFromCarrierApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700371 Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700372 newMsg.getData().putBoolean("loaded_from_xml", true);
373 sendMessage(newMsg);
374 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700375 // No cached config, so fetch it from a carrier app.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800376 if (carrierPackageName != null && bindToConfigPackage(carrierPackageName,
377 phoneId, EVENT_CONNECTED_TO_CARRIER)) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700378 sendMessageDelayed(
379 obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700380 BIND_TIMEOUT_MILLIS);
381 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700382 // Send broadcast if bind fails.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700383 broadcastConfigChangedIntent(phoneId);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800384 loge("Bind to carrier app: " + carrierPackageName + " fails");
Nathan Harold48ac0972019-03-13 22:33:01 -0700385 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700386 }
387 }
388 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700389 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700390
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800391 case EVENT_CONNECTED_TO_CARRIER: {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700392 removeMessages(EVENT_BIND_CARRIER_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700393 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800394 // If new service connection has been created, unbind.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700395 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Jordan Liu178430d2020-02-10 14:32:15 -0800396 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800397 break;
398 }
chen xu02581692018-11-11 19:03:44 -0800399 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700400 // ResultReceiver callback will execute in this Handler's thread.
401 final ResultReceiver resultReceiver =
402 new ResultReceiver(this) {
403 @Override
404 public void onReceiveResult(int resultCode, Bundle resultData) {
Jordan Liu178430d2020-02-10 14:32:15 -0800405 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700406 // If new service connection has been created, this is stale.
407 if (mServiceConnection[phoneId] != conn) {
408 loge("Received response for stale request.");
409 return;
410 }
411 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
412 if (resultCode == RESULT_ERROR || resultData == null) {
413 // On error, abort config fetching.
chen xudb04c292019-03-26 17:01:15 -0700414 loge("Failed to get carrier config from carrier app: "
415 + getCarrierPackageForPhoneId(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700416 broadcastConfigChangedIntent(phoneId);
Nathan Harold48ac0972019-03-13 22:33:01 -0700417 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700418 return;
419 }
420 PersistableBundle config =
421 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100422 saveConfigToXml(getCarrierPackageForPhoneId(phoneId), "",
423 phoneId, carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700424 mConfigFromCarrierApp[phoneId] = config;
425 sendMessage(
426 obtainMessage(
427 EVENT_FETCH_CARRIER_DONE, phoneId, -1));
428 }
429 };
430 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800431 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700432 ICarrierService carrierService =
433 ICarrierService.Stub.asInterface(conn.service);
434 carrierService.getCarrierConfig(carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800435 logdWithLocalLog("Fetch config for carrier app: "
chen xudb04c292019-03-26 17:01:15 -0700436 + getCarrierPackageForPhoneId(phoneId)
437 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700438 } catch (RemoteException e) {
439 loge("Failed to get carrier config: " + e.toString());
Jordan Liu178430d2020-02-10 14:32:15 -0800440 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700441 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800442 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700443 sendMessageDelayed(
444 obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1),
445 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800446 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700447 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800448
Jonathan Basseri930701e2015-06-11 20:56:43 -0700449 case EVENT_BIND_CARRIER_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800450 case EVENT_FETCH_CARRIER_TIMEOUT: {
451 loge("Bind/fetch from carrier app timeout");
chen xuf60b3162019-05-01 21:31:05 -0700452 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
453 // If we attempted to bind to the app, but the service connection is null due to
454 // the race condition that clear config event happens before bind/fetch complete
455 // then config was cleared while we were waiting and we should not continue.
456 if (mServiceConnection[phoneId] != null) {
457 // If a ResponseReceiver callback is in the queue when this happens, we will
458 // unbind twice and throw an exception.
Jordan Liu178430d2020-02-10 14:32:15 -0800459 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700460 broadcastConfigChangedIntent(phoneId);
461 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700462 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700463 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700464 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800465 case EVENT_FETCH_CARRIER_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700466 // If we attempted to bind to the app, but the service connection is null, then
467 // config was cleared while we were waiting and we should not continue.
468 if (!msg.getData().getBoolean("loaded_from_xml", false)
469 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800470 break;
471 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700472 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800473 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700474 }
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700475
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800476 case EVENT_CHECK_SYSTEM_UPDATE: {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700477 SharedPreferences sharedPrefs =
478 PreferenceManager.getDefaultSharedPreferences(mContext);
479 final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
480 if (!Build.FINGERPRINT.equals(lastFingerprint)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800481 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700482 "Build fingerprint changed. old: "
483 + lastFingerprint
484 + " new: "
485 + Build.FINGERPRINT);
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700486 clearCachedConfigForPackage(null);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700487 sharedPrefs
488 .edit()
489 .putString(KEY_FINGERPRINT, Build.FINGERPRINT)
490 .apply();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700491 }
492 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700493 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700494
495 case EVENT_SUBSCRIPTION_INFO_UPDATED:
496 broadcastConfigChangedIntent(phoneId);
497 break;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700498 case EVENT_MULTI_SIM_CONFIG_CHANGED:
499 onMultiSimConfigChanged();
500 break;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800501 }
502 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700503 }
504
505 private final Handler mHandler;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800506
507 /**
508 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
509 * receiver for relevant events.
510 */
511 private CarrierConfigLoader(Context context) {
512 mContext = context;
Meng Wang33ad2bc2017-03-16 20:21:20 -0700513 mPlatformCarrierConfigPackage =
514 mContext.getString(R.string.platform_carrier_config_package);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700515 mHandler = new ConfigHandler();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800516
Nanxi Chen3d670502016-03-17 16:32:09 -0700517 IntentFilter bootFilter = new IntentFilter();
518 bootFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
519 context.registerReceiver(mBootReceiver, bootFilter);
520
Junda Liu8a8a53a2015-05-15 16:19:57 -0700521 // Register for package updates. Update app or uninstall app update will have all 3 intents,
522 // in the order or removed, added, replaced, all with extra_replace set to true.
523 IntentFilter pkgFilter = new IntentFilter();
524 pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
525 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
526 pkgFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
527 pkgFilter.addDataScheme("package");
Jordan Liu5ca24762019-07-10 12:51:09 -0700528 context.registerReceiver(mPackageReceiver, pkgFilter);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800529
Malcolm Chen978dda52019-10-08 18:15:25 -0700530 int numPhones = TelephonyManager.from(context).getSupportedModemCount();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700531 mConfigFromDefaultApp = new PersistableBundle[numPhones];
532 mConfigFromCarrierApp = new PersistableBundle[numPhones];
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100533 mPersistentOverrideConfigs = new PersistableBundle[numPhones];
Hall Liu506724b2018-10-22 18:16:14 -0700534 mOverrideConfigs = new PersistableBundle[numPhones];
Zach Johnson36d7aab2015-05-22 15:43:00 -0700535 mServiceConnection = new CarrierServiceConnection[numPhones];
Jordan Liu178430d2020-02-10 14:32:15 -0800536 mServiceBound = new boolean[numPhones];
Junda Liu03aad762017-07-21 13:32:17 -0700537 mHasSentConfigChange = new boolean[numPhones];
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800538 // Make this service available through ServiceManager.
539 ServiceManager.addService(Context.CARRIER_CONFIG_SERVICE, this);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800540 logd("CarrierConfigLoader has started");
Nathan Harold48ac0972019-03-13 22:33:01 -0700541 mSubscriptionInfoUpdater = PhoneFactory.getSubscriptionInfoUpdater();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700542 mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800543 }
544
545 /**
546 * Initialize the singleton CarrierConfigLoader instance.
547 *
548 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
549 */
550 /* package */
551 static CarrierConfigLoader init(Context context) {
552 synchronized (CarrierConfigLoader.class) {
553 if (sInstance == null) {
554 sInstance = new CarrierConfigLoader(context);
555 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700556 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800557 }
558 return sInstance;
559 }
560 }
561
Malcolm Chen5ea18532019-10-24 19:55:44 -0700562 private void clearConfigForPhone(int phoneId, boolean sendBroadcast) {
563 /* Ignore clear configuration request if device is being shutdown. */
564 Phone phone = PhoneFactory.getPhone(phoneId);
565 if (phone != null) {
566 if (phone.isShuttingDown()) {
567 return;
568 }
569 }
570
571 mConfigFromDefaultApp[phoneId] = null;
572 mConfigFromCarrierApp[phoneId] = null;
573 mServiceConnection[phoneId] = null;
574 mHasSentConfigChange[phoneId] = false;
575
576 if (sendBroadcast) broadcastConfigChangedIntent(phoneId, false);
577 }
578
Nathan Harold48ac0972019-03-13 22:33:01 -0700579 private void notifySubscriptionInfoUpdater(int phoneId) {
580 String configPackagename;
581 PersistableBundle configToSend;
582 int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
583 // Prefer the carrier privileged carrier app, but if there is not one, use the platform
584 // default carrier app.
585 if (mConfigFromCarrierApp[phoneId] != null) {
586 configPackagename = getCarrierPackageForPhoneId(phoneId);
587 configToSend = mConfigFromCarrierApp[phoneId];
588 } else {
589 configPackagename = mPlatformCarrierConfigPackage;
590 configToSend = mConfigFromDefaultApp[phoneId];
591 }
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700592
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800593 if (configToSend == null) {
594 configToSend = new PersistableBundle();
595 }
596
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700597 // mOverrideConfigs is for testing. And it will override current configs.
598 PersistableBundle config = mOverrideConfigs[phoneId];
599 if (config != null) {
Torbjorn Eklund1050cb02018-11-16 14:05:38 +0100600 configToSend = new PersistableBundle(configToSend);
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700601 configToSend.putAll(config);
602 }
603
Nathan Harold48ac0972019-03-13 22:33:01 -0700604 mSubscriptionInfoUpdater.updateSubscriptionByCarrierConfigAndNotifyComplete(
605 phoneId, configPackagename, configToSend,
606 mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1));
607 }
608
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800609 private void broadcastConfigChangedIntent(int phoneId) {
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800610 broadcastConfigChangedIntent(phoneId, true);
611 }
612
613 private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800614 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800615 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
chen xuf9db49f2019-03-31 23:04:42 -0700616 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800617 if (addSubIdExtra) {
618 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
619 int[] subIds = SubscriptionManager.getSubId(phoneId);
620 if (!ArrayUtils.isEmpty(subIds)) {
621 TelephonyManager telMgr = TelephonyManager.from(mContext)
622 .createForSubscriptionId(subIds[0]);
623 simApplicationState = telMgr.getSimApplicationState();
624 }
625 // Include subId/carrier id extra only if SIM records are loaded
626 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
627 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
628 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
629 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID,
630 getSpecificCarrierIdForPhoneId(phoneId));
631 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, getCarrierIdForPhoneId(phoneId));
632 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800633 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800634 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800635 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
Jordan Liu5ca24762019-07-10 12:51:09 -0700636 mContext.sendBroadcast(intent);
Junda Liu03aad762017-07-21 13:32:17 -0700637 mHasSentConfigChange[phoneId] = true;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800638 }
639
640 /** Binds to the default or carrier config app. */
641 private boolean bindToConfigPackage(String pkgName, int phoneId, int eventId) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800642 logdWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700643 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700644 carrierService.setPackage(pkgName);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800645 mServiceConnection[phoneId] = new CarrierServiceConnection(phoneId, pkgName, eventId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800646 try {
Jordan Liu178430d2020-02-10 14:32:15 -0800647 if (mContext.bindService(carrierService, mServiceConnection[phoneId],
648 Context.BIND_AUTO_CREATE)) {
649 mServiceBound[phoneId] = true;
650 return true;
651 } else {
652 return false;
653 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800654 } catch (SecurityException ex) {
655 return false;
656 }
657 }
658
chen xu02581692018-11-11 19:03:44 -0800659 private CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800660 String mcc = "";
661 String mnc = "";
662 String imsi = "";
663 String gid1 = "";
664 String gid2 = "";
665 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
666 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800667 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800668 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700669 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
670 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800671 mcc = simOperator.substring(0, 3);
672 mnc = simOperator.substring(3);
673 }
674 Phone phone = PhoneFactory.getPhone(phoneId);
675 if (phone != null) {
676 imsi = phone.getSubscriberId();
677 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700678 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800679 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800680 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800681 }
chen xua31f22b2019-03-06 15:28:50 -0800682 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800683 }
684
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700685 /** Returns the package name of a priveleged carrier app, or null if there is none. */
686 private String getCarrierPackageForPhoneId(int phoneId) {
687 List<String> carrierPackageNames = TelephonyManager.from(mContext)
688 .getCarrierPackageNamesForIntentAndPhone(
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700689 new Intent(CarrierService.CARRIER_SERVICE_INTERFACE), phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700690 if (carrierPackageNames != null && carrierPackageNames.size() > 0) {
691 return carrierPackageNames.get(0);
692 } else {
693 return null;
694 }
695 }
696
697 private String getIccIdForPhoneId(int phoneId) {
698 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
699 return null;
700 }
701 Phone phone = PhoneFactory.getPhone(phoneId);
702 if (phone == null) {
703 return null;
704 }
705 return phone.getIccSerialNumber();
706 }
707
chen xu02581692018-11-11 19:03:44 -0800708 /**
chen xua31f22b2019-03-06 15:28:50 -0800709 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800710 */
chen xua31f22b2019-03-06 15:28:50 -0800711 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700712 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800713 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700714 }
715 Phone phone = PhoneFactory.getPhone(phoneId);
716 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800717 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700718 }
chen xua31f22b2019-03-06 15:28:50 -0800719 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800720 }
721
722 /**
723 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
724 */
725 private int getCarrierIdForPhoneId(int phoneId) {
726 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
727 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700728 }
chen xu02581692018-11-11 19:03:44 -0800729 Phone phone = PhoneFactory.getPhone(phoneId);
730 if (phone == null) {
731 return TelephonyManager.UNKNOWN_CARRIER_ID;
732 }
733 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700734 }
735
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700736 /**
737 * Writes a bundle to an XML file.
738 *
chen xu02581692018-11-11 19:03:44 -0800739 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -0800740 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -0800741 * should have a single copy of XML file named after carrier id. However, it's still possible
742 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
743 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
744 * carrier while iccid remains the same.
745 *
746 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
747 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700748 *
749 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700750 *
751 * @param packageName the name of the package from which we fetched this bundle.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100752 * @param extraString An extra string to be used in the XML file name.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800753 * @param phoneId the phone ID.
754 * @param carrierId contains all carrier-identifying information.
755 * @param config the bundle to be written. Null will be treated as an empty bundle.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700756 */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100757 private void saveConfigToXml(String packageName, @NonNull String extraString, int phoneId,
758 CarrierIdentifier carrierId, PersistableBundle config) {
Yuchen Dongc15afed2018-04-26 17:05:22 +0800759 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
760 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700761 loge("Skip save config because SIM records are not loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800762 return;
763 }
764
765 final String iccid = getIccIdForPhoneId(phoneId);
Chen Xucd4a6372019-07-29 16:34:52 -0700766 final int cid = carrierId.getSpecificCarrierId();
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700767 if (packageName == null || iccid == null) {
768 loge("Cannot save config with null packageName or iccid.");
769 return;
770 }
Junda Liu02596502016-11-15 13:46:43 -0800771 // b/32668103 Only save to file if config isn't empty.
772 // In case of failure, not caching an empty bundle will
773 // try loading config again on next power on or sim loaded.
774 // Downside is for genuinely empty bundle, will bind and load
775 // on every power on.
776 if (config == null || config.isEmpty()) {
777 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700778 }
779
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700780 final String version = getPackageVersion(packageName);
781 if (version == null) {
782 loge("Failed to get package version for: " + packageName);
783 return;
784 }
785
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800786 logdWithLocalLog(
787 "Save config to xml, packagename: " + packageName + " phoneId: " + phoneId);
chen xudb04c292019-03-26 17:01:15 -0700788
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700789 FileOutputStream outFile = null;
790 try {
791 outFile = new FileOutputStream(
chen xu02581692018-11-11 19:03:44 -0800792 new File(mContext.getFilesDir(),
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100793 getFilenameForConfig(packageName, extraString, iccid, cid)));
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800794 config.putString(KEY_VERSION, version);
795 config.writeToStream(outFile);
796 outFile.flush();
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700797 outFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800798 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700799 loge(e.toString());
800 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700801 }
802
803 /**
804 * Reads a bundle from an XML file.
805 *
806 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +0800807 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700808 *
809 * In case of errors, or if the saved config is from a different package version than the
810 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700811 *
812 * @param packageName the name of the package from which we fetched this bundle.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100813 * @param extraString An extra string to be used in the XML file name.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800814 * @param phoneId the phone ID.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700815 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800816 * version does not match, or reading config fails.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700817 */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100818 private PersistableBundle restoreConfigFromXml(String packageName, @NonNull String extraString,
819 int phoneId) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700820 final String version = getPackageVersion(packageName);
821 if (version == null) {
822 loge("Failed to get package version for: " + packageName);
823 return null;
824 }
Yuchen Dongc15afed2018-04-26 17:05:22 +0800825 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
826 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700827 loge("Skip restoring config because SIM records are not yet loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800828 return null;
829 }
830
831 final String iccid = getIccIdForPhoneId(phoneId);
chen xua31f22b2019-03-06 15:28:50 -0800832 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700833 if (packageName == null || iccid == null) {
834 loge("Cannot restore config with null packageName or iccid.");
835 return null;
836 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700837
838 PersistableBundle restoredBundle = null;
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100839 File file = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700840 FileInputStream inFile = null;
841 try {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100842 file = new File(mContext.getFilesDir(),
843 getFilenameForConfig(packageName, extraString, iccid, cid));
844 inFile = new FileInputStream(file);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700845
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800846 restoredBundle = PersistableBundle.readFromStream(inFile);
847 String savedVersion = restoredBundle.getString(KEY_VERSION);
848 restoredBundle.remove(KEY_VERSION);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700849
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800850 if (!version.equals(savedVersion)) {
851 loge("Saved version mismatch: " + version + " vs " + savedVersion);
852 restoredBundle = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700853 }
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800854
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700855 inFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800856 } catch (FileNotFoundException e) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100857 // Missing file is normal occurrence that might occur with a new sim or when restoring
858 // an override file during boot and should not be treated as an error.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800859 if (file != null) logd("File not found: " + file.getPath());
860 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700861 loge(e.toString());
862 }
863
864 return restoredBundle;
865 }
866
Junda Liu8a8a53a2015-05-15 16:19:57 -0700867 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700868 * Clears cached carrier config.
869 * This deletes all saved XML files associated with the given package name. If packageName is
870 * null, then it deletes all saved XML files.
871 *
872 * @param packageName the name of a carrier package, or null if all cached config should be
873 * cleared.
874 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -0700875 */
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700876 private boolean clearCachedConfigForPackage(final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700877 File dir = mContext.getFilesDir();
878 File[] packageFiles = dir.listFiles(new FilenameFilter() {
879 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700880 if (packageName != null) {
881 return filename.startsWith("carrierconfig-" + packageName + "-");
882 } else {
883 return filename.startsWith("carrierconfig-");
884 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700885 }
886 });
Junda Liu8a8a53a2015-05-15 16:19:57 -0700887 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700888 for (File f : packageFiles) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800889 logd("Deleting " + f.getName());
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700890 f.delete();
891 }
Junda Liu8a8a53a2015-05-15 16:19:57 -0700892 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700893 }
894
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700895 /** Builds a canonical file name for a config file. */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100896 private String getFilenameForConfig(@NonNull String packageName, @NonNull String extraString,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800897 @NonNull String iccid, int cid) {
chen xu02581692018-11-11 19:03:44 -0800898 // the same carrier should have a single copy of XML file named after carrier id.
899 // However, it's still possible that platform doesn't recognize the current sim carrier,
900 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
901 // cases SIM OTA resolves to different carrier while iccid remains the same.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100902 return "carrierconfig-" + packageName + extraString + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700903 }
904
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700905 /** Return the current version code of a package, or null if the name is not found. */
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700906 private String getPackageVersion(String packageName) {
907 try {
908 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -0800909 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700910 } catch (PackageManager.NameNotFoundException e) {
911 return null;
912 }
913 }
914
Jonathan Basseri65273c82017-07-25 15:08:42 -0700915 /**
916 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700917 *
918 * This reads config bundles for the given phoneId. That means getting the latest bundle from
919 * the default app and a privileged carrier app, if present. This will not bind to an app if we
920 * have a saved config file to use instead.
921 */
922 private void updateConfigForPhoneId(int phoneId) {
Junda Liu8a8a53a2015-05-15 16:19:57 -0700923 // Clear in-memory cache for carrier app config, so when carrier app gets uninstalled, no
924 // stale config is left.
925 if (mConfigFromCarrierApp[phoneId] != null &&
926 getCarrierPackageForPhoneId(phoneId) == null) {
927 mConfigFromCarrierApp[phoneId] = null;
928 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700929 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700930 }
931
Malcolm Chen5ea18532019-10-24 19:55:44 -0700932 private void onMultiSimConfigChanged() {
933 for (int i = TelephonyManager.from(mContext).getActiveModemCount();
934 i < mConfigFromDefaultApp.length; i++) {
935 clearConfigForPhone(i, false);
936 }
937 }
938
Hall Liu506724b2018-10-22 18:16:14 -0700939 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800940 @NonNull
941 public PersistableBundle getConfigForSubId(int subId, String callingPackage) {
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -0800942 return getConfigForSubIdWithFeature(subId, callingPackage, null);
943 }
944
945 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800946 @NonNull
947 public PersistableBundle getConfigForSubIdWithFeature(int subId, String callingPackage,
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -0800948 String callingFeatureId) {
949 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subId, callingPackage,
950 callingFeatureId, "getCarrierConfig")) {
Malcolm Chen6d3d6b32018-03-01 13:58:03 -0800951 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -0700952 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -0700953
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800954 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700955 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800956 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700957 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800958 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700959 retConfig.putAll(config);
yinxud0104832019-08-16 14:20:08 -0700960 if (getCarrierPackageForPhoneId(phoneId) == null) {
961 retConfig.putBoolean(
962 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
963 }
yinxuc5e27622017-11-29 15:08:50 -0800964 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800965 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800966 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700967 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -0800968 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
969 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100970 config = mPersistentOverrideConfigs[phoneId];
971 if (config != null) {
972 retConfig.putAll(config);
973 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
974 }
Hall Liu506724b2018-10-22 18:16:14 -0700975 config = mOverrideConfigs[phoneId];
976 if (config != null) {
977 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -0700978 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800979 }
980 return retConfig;
981 }
982
983 @Override
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100984 public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides,
985 boolean persistent) {
Hall Liu506724b2018-10-22 18:16:14 -0700986 mContext.enforceCallingOrSelfPermission(
987 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Brad Ebingerf6281ad2019-04-25 11:02:04 -0700988 //TODO: Also check for SHELL UID to restrict this method to testing only (b/131326259)
Hall Liu506724b2018-10-22 18:16:14 -0700989 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
990 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800991 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
Hall Liu506724b2018-10-22 18:16:14 -0700992 return;
993 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100994 overrideConfig(mOverrideConfigs, phoneId, overrides);
Hall Liu506724b2018-10-22 18:16:14 -0700995
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100996 if (persistent) {
997 overrideConfig(mPersistentOverrideConfigs, phoneId, overrides);
998
999 if (overrides != null) {
1000 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
1001 saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId,
1002 carrierId, mPersistentOverrideConfigs[phoneId]);
1003 } else {
1004 final String iccid = getIccIdForPhoneId(phoneId);
1005 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1006 String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage,
1007 OVERRIDE_PACKAGE_ADDITION, iccid, cid);
1008 File fileToDelete = new File(mContext.getFilesDir(), fileName);
1009 fileToDelete.delete();
1010 }
Hall Liu506724b2018-10-22 18:16:14 -07001011 }
Brad Ebingerc9d1fa12019-04-17 15:21:18 -07001012 notifySubscriptionInfoUpdater(phoneId);
Hall Liu506724b2018-10-22 18:16:14 -07001013 }
1014
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001015 private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId,
1016 @Nullable PersistableBundle overrides) {
1017 if (overrides == null) {
1018 currentOverrides[phoneId] = new PersistableBundle();
1019 } else if (currentOverrides[phoneId] == null) {
1020 currentOverrides[phoneId] = overrides;
1021 } else {
1022 currentOverrides[phoneId].putAll(overrides);
1023 }
1024 }
1025
Hall Liu506724b2018-10-22 18:16:14 -07001026 @Override
Jonathan Basseri86030352015-06-08 12:27:56 -07001027 public void notifyConfigChangedForSubId(int subId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001028 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001029 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001030 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001031 return;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001032 }
Jordan Liud7d57fe2019-04-16 12:29:43 -07001033
1034 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001035 // system privileged app with MODIFY_PHONE_STATE permission.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001036 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext, subId,
1037 "Require carrier privileges or MODIFY_PHONE_STATE permission.");
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001038
1039 // This method should block until deleting has completed, so that an error which prevents us
1040 // from clearing the cache is passed back to the carrier app. With the files successfully
1041 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001042 String callingPackageName = mContext.getPackageManager().getNameForUid(
1043 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001044 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001045 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001046 }
1047
1048 @Override
1049 public void updateConfigForPhoneId(int phoneId, String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -07001050 mContext.enforceCallingOrSelfPermission(
1051 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001052 logdWithLocalLog("Update config for phoneId: " + phoneId + " simState: " + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001053 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1054 return;
1055 }
1056 // requires Java 7 for switch on string.
1057 switch (simState) {
1058 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1059 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001060 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001061 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Malcolm Chendb66de12019-12-09 18:57:07 -08001062 case IccCardConstants.INTENT_VALUE_ICC_NOT_READY:
Junda Liu9f2d2712015-05-15 14:22:45 -07001063 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001064 break;
1065 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1066 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001067 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001068 break;
1069 }
1070 }
1071
Junda Liu43d723a2015-05-12 17:23:45 -07001072 @Override
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001073 public String getDefaultCarrierServicePackageName() {
Shuo Qianefdb7962019-12-19 15:54:27 -08001074 mContext.enforceCallingOrSelfPermission(
1075 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1076 "getDefaultCarrierServicePackageName");
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001077 return mPlatformCarrierConfigPackage;
1078 }
1079
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001080 private void unbindIfBound(Context context, CarrierServiceConnection conn,
1081 int phoneId) {
1082 if (mServiceBound[phoneId]) {
1083 mServiceBound[phoneId] = false;
1084 context.unbindService(conn);
1085 }
1086 }
1087
1088 /**
1089 * If {@code args} contains {@link #DUMP_ARG_REQUESTING_PACKAGE} and a following package name,
1090 * we'll also call {@link IBinder#dump} on the default carrier service (if bound) and the
1091 * specified carrier service (if bound). Typically, this is done for connectivity bug reports
1092 * where we don't call {@code dumpsys activity service all-non-platform} because that contains
1093 * too much info, but we still want to let carrier apps include their diagnostics.
1094 */
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001095 @Override
Junda Liu43d723a2015-05-12 17:23:45 -07001096 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001097 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
Junda Liu43d723a2015-05-12 17:23:45 -07001098 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1099 != PackageManager.PERMISSION_GRANTED) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001100 indentPW.println("Permission Denial: can't dump carrierconfig from from pid="
Junda Liu43d723a2015-05-12 17:23:45 -07001101 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1102 return;
1103 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001104 indentPW.println("CarrierConfigLoader: " + this);
Junda Liu43d723a2015-05-12 17:23:45 -07001105 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001106 indentPW.println("Phone Id = " + i);
shuoq26a3a4c2016-12-16 11:06:48 -08001107 // display default values in CarrierConfigManager
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001108 printConfig(CarrierConfigManager.getDefaultConfig(), indentPW,
shuoq26a3a4c2016-12-16 11:06:48 -08001109 "Default Values from CarrierConfigManager");
shuoq26a3a4c2016-12-16 11:06:48 -08001110 // display ConfigFromDefaultApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001111 printConfig(mConfigFromDefaultApp[i], indentPW, "mConfigFromDefaultApp");
shuoq26a3a4c2016-12-16 11:06:48 -08001112 // display ConfigFromCarrierApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001113 printConfig(mConfigFromCarrierApp[i], indentPW, "mConfigFromCarrierApp");
1114 printConfig(mPersistentOverrideConfigs[i], indentPW, "mPersistentOverrideConfigs");
1115 printConfig(mOverrideConfigs[i], indentPW, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001116 }
chen xudb04c292019-03-26 17:01:15 -07001117
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001118 indentPW.println("CarrierConfigLoadingLog=");
1119 mCarrierConfigLoadingLog.dump(fd, indentPW, args);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001120
1121 int requestingPackageIndex = ArrayUtils.indexOf(args, DUMP_ARG_REQUESTING_PACKAGE);
1122 if (requestingPackageIndex != -1 && requestingPackageIndex < args.length - 1
1123 && !TextUtils.isEmpty(args[requestingPackageIndex + 1])) {
1124 String requestingPackage = args[requestingPackageIndex + 1];
1125 indentPW.println("");
Hunter Knepshielde601f432020-02-19 10:16:04 -08001126 // Throws a SecurityException if the caller is impersonating another app in an effort to
1127 // dump extra info (which may contain PII the caller doesn't have a right to).
1128 enforceCallerIsSystemOrRequestingPackage(requestingPackage);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001129 logd("Including default and requesting package " + requestingPackage
1130 + " carrier services in dump");
1131 indentPW.println("Connected services");
1132 dumpCarrierServiceIfBound(fd, indentPW, "Default config package",
1133 mPlatformCarrierConfigPackage);
1134 dumpCarrierServiceIfBound(fd, indentPW, "Requesting package", requestingPackage);
1135 }
shuoq26a3a4c2016-12-16 11:06:48 -08001136 }
1137
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001138 private void printConfig(PersistableBundle configApp, IndentingPrintWriter indentPW,
1139 String name) {
1140 indentPW.increaseIndent();
shuoq26a3a4c2016-12-16 11:06:48 -08001141 if (configApp == null) {
shuoq26a3a4c2016-12-16 11:06:48 -08001142 indentPW.println(name + " : null ");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001143 indentPW.decreaseIndent();
1144 indentPW.println("");
shuoq26a3a4c2016-12-16 11:06:48 -08001145 return;
1146 }
shuoq26a3a4c2016-12-16 11:06:48 -08001147 indentPW.println(name + " : ");
1148 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1149 Collections.sort(sortedKeys);
1150 indentPW.increaseIndent();
1151 indentPW.increaseIndent();
1152 for (String key : sortedKeys) {
1153 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1154 indentPW.println(key + " = " +
1155 Arrays.toString((Object[]) configApp.get(key)));
1156 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1157 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1158 } else {
1159 indentPW.println(key + " = " + configApp.get(key));
1160 }
Junda Liu43d723a2015-05-12 17:23:45 -07001161 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001162 indentPW.decreaseIndent();
1163 indentPW.decreaseIndent();
1164 indentPW.decreaseIndent();
1165 indentPW.println("");
Junda Liu43d723a2015-05-12 17:23:45 -07001166 }
1167
Hunter Knepshielde601f432020-02-19 10:16:04 -08001168 /**
1169 * Passes without problem when one of these conditions is true:
1170 * - The caller is a privileged UID (e.g. for dumpstate.cpp generating a bug report, where the
1171 * system knows the true caller plumbed in through the {@link android.os.BugreportManager} API).
1172 * - The caller's UID matches the supplied package.
1173 *
1174 * @throws SecurityException if none of the above conditions are met.
1175 */
1176 private void enforceCallerIsSystemOrRequestingPackage(String requestingPackage)
1177 throws SecurityException {
1178 final int callingUid = Binder.getCallingUid();
1179 if (callingUid == Process.ROOT_UID || callingUid == Process.SYSTEM_UID
1180 || callingUid == Process.SHELL_UID || callingUid == Process.PHONE_UID) {
1181 // Bug reports (dumpstate.cpp) run as SHELL, and let some other privileged UIDs through
1182 // as well.
1183 return;
1184 }
1185 // An app is trying to dump extra detail, block it if they aren't who they claim to be.
1186 AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class);
1187 if (appOps == null) {
1188 throw new SecurityException("No AppOps");
1189 }
1190 // Will throw a SecurityException if the UID and package don't match.
1191 appOps.checkPackage(callingUid, requestingPackage);
1192 }
1193
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001194 private void dumpCarrierServiceIfBound(FileDescriptor fd, IndentingPrintWriter indentPW,
1195 String prefix, String pkgName) {
1196 // Null package is possible if it's early in the boot process, there was a recent crash, we
1197 // loaded the config from XML most recently, or a SIM slot is empty. Carrier apps with
1198 // long-lived bindings should typically get dumped here regardless. Even if an app is being
1199 // used for multiple phoneIds, we assume that it's smart enough to handle that on its own,
1200 // and that in most cases we'd just be dumping duplicate information and bloating a report.
1201 indentPW.increaseIndent();
1202 indentPW.println(prefix + " : " + pkgName);
1203 for (CarrierServiceConnection connection : mServiceConnection) {
1204 try {
1205 // We don't pay attention to mServiceBound[connection.phoneId] because typically
1206 // carrier apps will request long-lived bindings, and even if we unbind the app, it
1207 // may still be alive due to CarrierServiceBindHelper.
1208 if (connection == null || connection.service == null || !TextUtils.equals(pkgName,
1209 connection.pkgName) || !connection.service.isBinderAlive()
1210 || !connection.service.pingBinder()) {
1211 continue;
1212 }
1213 // Flush before we let the app output anything to ensure correct ordering of output.
1214 // Internally, Binder#dump calls flush on its printer after finishing so we don't
1215 // need to after the call finishes.
1216 indentPW.flush();
1217 try {
1218 logd("Dumping " + pkgName);
1219 // We don't need to give the carrier service any args.
1220 connection.service.dump(fd, null /* args */);
1221 logd("Done with " + pkgName);
1222 indentPW.decreaseIndent();
1223 indentPW.println("");
1224 return;
1225 } catch (RemoteException e) {
1226 indentPW.println("RemoteException");
1227 logd("RemoteException from " + pkgName, e);
1228 e.printStackTrace(indentPW);
1229 indentPW.decreaseIndent();
1230 indentPW.println("");
1231 return;
1232 }
1233 } catch (NullPointerException e) {
1234 // Highly unlikely, but possible if the carrier app was just unbound right before we
1235 // we tried to dump it so the binder was reset to null. Loop in case we have more
1236 // candidates on other phoneIds.
1237 logd("NullPointerException from " + pkgName, e);
1238 }
Jordan Liu46ed5db2020-01-28 08:55:20 -08001239 }
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001240 indentPW.increaseIndent();
1241 indentPW.println("Not bound");
1242 indentPW.decreaseIndent();
1243 indentPW.decreaseIndent();
1244 indentPW.println("");
Jordan Liu46ed5db2020-01-28 08:55:20 -08001245 }
1246
Zach Johnson36d7aab2015-05-22 15:43:00 -07001247 private class CarrierServiceConnection implements ServiceConnection {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001248 int phoneId;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001249 String pkgName;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001250 int eventId;
1251 IBinder service;
1252
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001253 CarrierServiceConnection(int phoneId, String pkgName, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001254 this.phoneId = phoneId;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001255 this.pkgName = pkgName;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001256 this.eventId = eventId;
1257 }
1258
1259 @Override
1260 public void onServiceConnected(ComponentName name, IBinder service) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001261 logd("Connected to config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001262 this.service = service;
1263 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1264 }
1265
1266 @Override
1267 public void onServiceDisconnected(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001268 logd("Disconnected from config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001269 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001270 }
1271
1272 @Override
1273 public void onBindingDied(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001274 logd("Binding died from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001275 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001276 }
1277
1278 @Override
1279 public void onNullBinding(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001280 logd("Null binding from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001281 this.service = null;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001282 }
1283 }
1284
1285 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1286 @Override
1287 public void onReceive(Context context, Intent intent) {
1288 String action = intent.getAction();
Junda Liu8a8a53a2015-05-15 16:19:57 -07001289 boolean replace = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1290 // If replace is true, only care ACTION_PACKAGE_REPLACED.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001291 if (replace && !Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
Junda Liu8a8a53a2015-05-15 16:19:57 -07001292 return;
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001293 }
Junda Liu8a8a53a2015-05-15 16:19:57 -07001294
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001295 switch (action) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001296 case Intent.ACTION_BOOT_COMPLETED:
1297 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1298 break;
1299
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001300 case Intent.ACTION_PACKAGE_ADDED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001301 case Intent.ACTION_PACKAGE_REMOVED:
Junda Liu8a8a53a2015-05-15 16:19:57 -07001302 case Intent.ACTION_PACKAGE_REPLACED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001303 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1304 String packageName = mContext.getPackageManager().getNameForUid(uid);
Junda Liu8a8a53a2015-05-15 16:19:57 -07001305 if (packageName != null) {
1306 // We don't have a phoneId for arg1.
1307 mHandler.sendMessage(
1308 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, packageName));
1309 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001310 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001311 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001312 }
1313 }
1314
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001315 private void logd(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001316 Log.d(LOG_TAG, msg);
1317 }
1318
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001319 private void logd(String msg, Throwable tr) {
1320 Log.d(LOG_TAG, msg, tr);
1321 }
1322
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001323 private void logdWithLocalLog(String msg) {
chen xudb04c292019-03-26 17:01:15 -07001324 Log.d(LOG_TAG, msg);
1325 mCarrierConfigLoadingLog.log(msg);
1326 }
1327
1328 private void loge(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001329 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07001330 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001331 }
1332}