blob: 2268d7e8e353888648b22ee1863ce6b85d24dbf4 [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;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080024import android.content.BroadcastReceiver;
25import android.content.ComponentName;
26import android.content.Context;
27import android.content.Intent;
28import android.content.IntentFilter;
29import android.content.ServiceConnection;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070030import android.content.SharedPreferences;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070031import android.content.pm.PackageInfo;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070032import android.content.pm.PackageManager;
Junda Liu43d723a2015-05-12 17:23:45 -070033import android.os.Binder;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070034import android.os.Build;
Jonathan Basseri65273c82017-07-25 15:08:42 -070035import android.os.Bundle;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080036import android.os.Handler;
37import android.os.IBinder;
38import android.os.Message;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070039import android.os.PersistableBundle;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080040import android.os.RemoteException;
Jonathan Basseri65273c82017-07-25 15:08:42 -070041import android.os.ResultReceiver;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080042import android.os.ServiceManager;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070043import android.preference.PreferenceManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080044import android.service.carrier.CarrierIdentifier;
Zach Johnson36d7aab2015-05-22 15:43:00 -070045import android.service.carrier.CarrierService;
46import android.service.carrier.ICarrierService;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080047import android.telephony.CarrierConfigManager;
48import android.telephony.SubscriptionManager;
49import android.telephony.TelephonyManager;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -080050import android.text.TextUtils;
chen xudb04c292019-03-26 17:01:15 -070051import android.util.LocalLog;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080052import android.util.Log;
53
54import com.android.internal.telephony.ICarrierConfigLoader;
55import com.android.internal.telephony.IccCardConstants;
56import com.android.internal.telephony.Phone;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080057import com.android.internal.telephony.PhoneFactory;
Nathan Harold48ac0972019-03-13 22:33:01 -070058import com.android.internal.telephony.SubscriptionInfoUpdater;
Jeff Davidsona8e4e242018-03-15 17:16:18 -070059import com.android.internal.telephony.TelephonyPermissions;
Meng Wanga320b942019-11-25 11:21:26 -080060import com.android.internal.telephony.util.ArrayUtils;
shuoq26a3a4c2016-12-16 11:06:48 -080061import com.android.internal.util.IndentingPrintWriter;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080062
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070063import java.io.File;
Junda Liu43d723a2015-05-12 17:23:45 -070064import java.io.FileDescriptor;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070065import java.io.FileInputStream;
66import java.io.FileNotFoundException;
67import java.io.FileOutputStream;
Jonathan Basseri1f743c92015-05-15 00:19:46 -070068import java.io.FilenameFilter;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070069import java.io.IOException;
Junda Liu43d723a2015-05-12 17:23:45 -070070import java.io.PrintWriter;
shuoq26a3a4c2016-12-16 11:06:48 -080071import java.util.ArrayList;
72import java.util.Arrays;
73import java.util.Collections;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080074import java.util.List;
75
76/**
77 * CarrierConfigLoader binds to privileged carrier apps to fetch carrier config overlays.
Jonathan Basseri6465afd2015-02-25 13:05:57 -080078 */
Jonathan Basseri6465afd2015-02-25 13:05:57 -080079public class CarrierConfigLoader extends ICarrierConfigLoader.Stub {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070080 private static final String LOG_TAG = "CarrierConfigLoader";
Meng Wang33ad2bc2017-03-16 20:21:20 -070081
82 // Package name for platform carrier config app, bundled with system image.
83 private final String mPlatformCarrierConfigPackage;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080084
85 /** The singleton instance. */
86 private static CarrierConfigLoader sInstance;
87 // The context for phone app, passed from PhoneGlobals.
88 private Context mContext;
89 // Carrier configs from default app, indexed by phoneID.
Jonathan Basseric31f1f32015-05-12 10:13:03 -070090 private PersistableBundle[] mConfigFromDefaultApp;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080091 // Carrier configs from privileged carrier config app, indexed by phoneID.
Jonathan Basseric31f1f32015-05-12 10:13:03 -070092 private PersistableBundle[] mConfigFromCarrierApp;
Torbjorn Eklund723527a2019-02-13 11:16:25 +010093 // Persistent Carrier configs that are provided via the override test API, indexed by phone ID.
94 private PersistableBundle[] mPersistentOverrideConfigs;
Hall Liu506724b2018-10-22 18:16:14 -070095 // Carrier configs that are provided via the override test API, indexed by phone ID.
96 private PersistableBundle[] mOverrideConfigs;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080097 // Service connection for binding to config app.
Zach Johnson36d7aab2015-05-22 15:43:00 -070098 private CarrierServiceConnection[] mServiceConnection;
Jordan Liu178430d2020-02-10 14:32:15 -080099 // Whether we are bound to a service for each phone
100 private boolean[] mServiceBound;
Junda Liu03aad762017-07-21 13:32:17 -0700101 // Whether we have sent config change bcast for each phone id.
102 private boolean[] mHasSentConfigChange;
Nathan Harold48ac0972019-03-13 22:33:01 -0700103 // SubscriptionInfoUpdater
104 private final SubscriptionInfoUpdater mSubscriptionInfoUpdater;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800105
Nanxi Chen3d670502016-03-17 16:32:09 -0700106 // Broadcast receiver for Boot intents, register intent filter in construtor.
107 private final BroadcastReceiver mBootReceiver = new ConfigLoaderBroadcastReceiver();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800108 // Broadcast receiver for SIM and pkg intents, register intent filter in constructor.
Nanxi Chen3d670502016-03-17 16:32:09 -0700109 private final BroadcastReceiver mPackageReceiver = new ConfigLoaderBroadcastReceiver();
Jack Yu67663de2019-10-17 15:19:48 -0700110 private final LocalLog mCarrierConfigLoadingLog = new LocalLog(100);
chen xudb04c292019-03-26 17:01:15 -0700111
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800112
113 // Message codes; see mHandler below.
114 // Request from SubscriptionInfoUpdater when SIM becomes absent or error.
115 private static final int EVENT_CLEAR_CONFIG = 0;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800116 // Has connected to default app.
117 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
118 // Has connected to carrier app.
119 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700120 // Config has been loaded from default app (or cache).
121 private static final int EVENT_FETCH_DEFAULT_DONE = 5;
122 // Config has been loaded from carrier app (or cache).
123 private static final int EVENT_FETCH_CARRIER_DONE = 6;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700124 // Attempt to fetch from default app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700125 private static final int EVENT_DO_FETCH_DEFAULT = 7;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700126 // Attempt to fetch from carrier app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700127 private static final int EVENT_DO_FETCH_CARRIER = 8;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700128 // A package has been installed, uninstalled, or updated.
129 private static final int EVENT_PACKAGE_CHANGED = 9;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700130 // Bind timed out for the default app.
131 private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
132 // Bind timed out for a carrier app.
133 private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700134 // Check if the system fingerprint has changed.
135 private static final int EVENT_CHECK_SYSTEM_UPDATE = 12;
Nanxi Chen3d670502016-03-17 16:32:09 -0700136 // Rerun carrier config binding after system is unlocked.
137 private static final int EVENT_SYSTEM_UNLOCKED = 13;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700138 // Fetching config timed out from the default app.
139 private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14;
140 // Fetching config timed out from a carrier app.
141 private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15;
Nathan Harold48ac0972019-03-13 22:33:01 -0700142 // SubscriptionInfoUpdater has finished updating the sub for the carrier config.
143 private static final int EVENT_SUBSCRIPTION_INFO_UPDATED = 16;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700144 // Multi-SIM config changed.
145 private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 17;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700146
Junda Liu6cb45002016-03-22 17:18:20 -0700147 private static final int BIND_TIMEOUT_MILLIS = 30000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800148
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800149 // Keys used for saving and restoring config bundle from file.
150 private static final String KEY_VERSION = "__carrier_config_package_version__";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800151
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100152 private static final String OVERRIDE_PACKAGE_ADDITION = "-override";
153
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700154 // SharedPreferences key for last known build fingerprint.
155 private static final String KEY_FINGERPRINT = "build_fingerprint";
156
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800157 // Argument for #dump that indicates we should also call the default and specified carrier
158 // service's #dump method. In multi-SIM devices, it's possible that carrier A is on SIM 1 and
159 // carrier B is on SIM 2, in which case we should not dump carrier B's service when carrier A
160 // requested the dump.
161 private static final String DUMP_ARG_REQUESTING_PACKAGE = "--requesting-package";
162
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800163 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700164 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700165 // For each phoneId, the event sequence should be:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700166 // fetch default, connected to default, fetch default (async), fetch default done,
167 // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700168 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700169 // If there is a saved config file for either the default app or the carrier app, we skip
170 // binding to the app and go straight from fetch to loaded.
171 //
172 // At any time, at most one connection is active. If events are not in this order, previous
173 // connection will be unbound, so only latest event takes effect.
174 //
175 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
176 // 1. loading from carrier app (even if read from a file)
177 // 2. loading from default app if there is no carrier app (even if read from a file)
178 // 3. clearing config (e.g. due to sim removal)
179 // 4. encountering bind or IPC error
Jonathan Basseri65273c82017-07-25 15:08:42 -0700180 private class ConfigHandler extends Handler {
181 @Override
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800182 public void handleMessage(Message msg) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700183 final int phoneId = msg.arg1;
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800184 logdWithLocalLog("mHandler: " + msg.what + " phoneId: " + phoneId);
Malcolm Chen5ea18532019-10-24 19:55:44 -0700185 if (!SubscriptionManager.isValidPhoneId(phoneId)
186 && msg.what != EVENT_MULTI_SIM_CONFIG_CHANGED) {
187 return;
188 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800189 switch (msg.what) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800190 case EVENT_CLEAR_CONFIG: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700191 clearConfigForPhone(phoneId, true);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800192 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700193 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700194
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800195 case EVENT_SYSTEM_UNLOCKED: {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700196 for (int i = 0; i < TelephonyManager.from(mContext).getActiveModemCount();
197 ++i) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700198 // When user unlock device, we should only try to send broadcast again if we
199 // have sent it before unlock. This will avoid we try to load carrier config
200 // when SIM is still loading when unlock happens.
Junda Liu03aad762017-07-21 13:32:17 -0700201 if (mHasSentConfigChange[i]) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800202 logdWithLocalLog("System unlocked");
Junda Liu03aad762017-07-21 13:32:17 -0700203 updateConfigForPhoneId(i);
204 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700205 }
206 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700207 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700208
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800209 case EVENT_PACKAGE_CHANGED: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700210 final String carrierPackageName = (String) msg.obj;
211 // Only update if there are cached config removed to avoid updating config for
212 // unrelated packages.
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700213 if (clearCachedConfigForPackage(carrierPackageName)) {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700214 int numPhones = TelephonyManager.from(mContext).getActiveModemCount();
Junda Liu8a8a53a2015-05-15 16:19:57 -0700215 for (int i = 0; i < numPhones; ++i) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800216 logdWithLocalLog("Package changed: " + carrierPackageName
Jack Yu67663de2019-10-17 15:19:48 -0700217 + ", phone=" + i);
Junda Liu8a8a53a2015-05-15 16:19:57 -0700218 updateConfigForPhoneId(i);
219 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700220 }
221 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700222 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700223
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800224 case EVENT_DO_FETCH_DEFAULT: {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100225 // Restore persistent override values.
226 PersistableBundle config = restoreConfigFromXml(
227 mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId);
228 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800229 logd("Loaded persistent override config from XML. package="
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100230 + mPlatformCarrierConfigPackage
231 + " phoneId=" + phoneId);
232 mPersistentOverrideConfigs[phoneId] = config;
233 }
234
235 config = restoreConfigFromXml(mPlatformCarrierConfigPackage, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700236 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800237 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700238 "Loaded config from XML. package="
239 + mPlatformCarrierConfigPackage
240 + " phoneId="
241 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700242 mConfigFromDefaultApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700243 Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700244 newMsg.getData().putBoolean("loaded_from_xml", true);
245 mHandler.sendMessage(newMsg);
246 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700247 // No cached config, so fetch it from the default app.
248 if (bindToConfigPackage(
249 mPlatformCarrierConfigPackage,
250 phoneId,
251 EVENT_CONNECTED_TO_DEFAULT)) {
252 sendMessageDelayed(
253 obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700254 BIND_TIMEOUT_MILLIS);
255 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700256 // Send broadcast if bind fails.
Nathan Harold48ac0972019-03-13 22:33:01 -0700257 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700258 // TODO: We *must* call unbindService even if bindService returns false.
259 // (And possibly if SecurityException was thrown.)
chen xudb04c292019-03-26 17:01:15 -0700260 loge("binding to default app: "
261 + mPlatformCarrierConfigPackage + " fails");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700262 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800263 }
264 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700265 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800266
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800267 case EVENT_CONNECTED_TO_DEFAULT: {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700268 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700269 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800270 // If new service connection has been created, unbind.
271 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Jordan Liu178430d2020-02-10 14:32:15 -0800272 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800273 break;
274 }
chen xu02581692018-11-11 19:03:44 -0800275 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700276 // ResultReceiver callback will execute in this Handler's thread.
277 final ResultReceiver resultReceiver =
278 new ResultReceiver(this) {
279 @Override
280 public void onReceiveResult(int resultCode, Bundle resultData) {
Jordan Liu178430d2020-02-10 14:32:15 -0800281 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700282 // If new service connection has been created, this is stale.
283 if (mServiceConnection[phoneId] != conn) {
284 loge("Received response for stale request.");
285 return;
286 }
287 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
288 if (resultCode == RESULT_ERROR || resultData == null) {
289 // On error, abort config fetching.
290 loge("Failed to get carrier config");
Nathan Harold48ac0972019-03-13 22:33:01 -0700291 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700292 return;
293 }
294 PersistableBundle config =
295 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100296 saveConfigToXml(mPlatformCarrierConfigPackage, "", phoneId,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800297 carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700298 mConfigFromDefaultApp[phoneId] = config;
299 sendMessage(
300 obtainMessage(
301 EVENT_FETCH_DEFAULT_DONE, phoneId, -1));
302 }
303 };
304 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800305 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700306 ICarrierService carrierService =
307 ICarrierService.Stub.asInterface(conn.service);
308 carrierService.getCarrierConfig(carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800309 logdWithLocalLog("Fetch config for default app: "
chen xudb04c292019-03-26 17:01:15 -0700310 + mPlatformCarrierConfigPackage
311 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700312 } catch (RemoteException e) {
chen xudb04c292019-03-26 17:01:15 -0700313 loge("Failed to get carrier config from default app: " +
314 mPlatformCarrierConfigPackage + " err: " + e.toString());
Jordan Liu178430d2020-02-10 14:32:15 -0800315 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700316 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800317 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700318 sendMessageDelayed(
319 obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1),
320 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800321 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700322 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800323
Jonathan Basseri930701e2015-06-11 20:56:43 -0700324 case EVENT_BIND_DEFAULT_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800325 case EVENT_FETCH_DEFAULT_TIMEOUT: {
326 loge("Bind/fetch time out from " + mPlatformCarrierConfigPackage);
chen xuf60b3162019-05-01 21:31:05 -0700327 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
328 // If we attempted to bind to the app, but the service connection is null due to
329 // the race condition that clear config event happens before bind/fetch complete
330 // then config was cleared while we were waiting and we should not continue.
331 if (mServiceConnection[phoneId] != null) {
332 // If a ResponseReceiver callback is in the queue when this happens, we will
333 // unbind twice and throw an exception.
Jordan Liu178430d2020-02-10 14:32:15 -0800334 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700335 broadcastConfigChangedIntent(phoneId);
336 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700337 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700338 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700339 }
Jonathan Basseri930701e2015-06-11 20:56:43 -0700340
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800341 case EVENT_FETCH_DEFAULT_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700342 // If we attempted to bind to the app, but the service connection is null, then
343 // config was cleared while we were waiting and we should not continue.
344 if (!msg.getData().getBoolean("loaded_from_xml", false)
345 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800346 break;
347 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700348 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700349 if (carrierPackageName != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800350 logd("Found carrier config app: " + carrierPackageName);
Jordan Liu38a614c2019-03-20 15:01:17 -0700351 sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId, -1));
Jonathan Basserib919e932015-05-27 16:53:08 +0000352 } else {
Nathan Harold48ac0972019-03-13 22:33:01 -0700353 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700354 }
355 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700356 }
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700357
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800358 case EVENT_DO_FETCH_CARRIER: {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700359 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700360 final PersistableBundle config =
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100361 restoreConfigFromXml(carrierPackageName, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700362 if (config != null) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800363 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700364 "Loaded config from XML. package="
365 + carrierPackageName
366 + " phoneId="
367 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700368 mConfigFromCarrierApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700369 Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700370 newMsg.getData().putBoolean("loaded_from_xml", true);
371 sendMessage(newMsg);
372 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700373 // No cached config, so fetch it from a carrier app.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800374 if (carrierPackageName != null && bindToConfigPackage(carrierPackageName,
375 phoneId, EVENT_CONNECTED_TO_CARRIER)) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700376 sendMessageDelayed(
377 obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700378 BIND_TIMEOUT_MILLIS);
379 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700380 // Send broadcast if bind fails.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700381 broadcastConfigChangedIntent(phoneId);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800382 loge("Bind to carrier app: " + carrierPackageName + " fails");
Nathan Harold48ac0972019-03-13 22:33:01 -0700383 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700384 }
385 }
386 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700387 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700388
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800389 case EVENT_CONNECTED_TO_CARRIER: {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700390 removeMessages(EVENT_BIND_CARRIER_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700391 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800392 // If new service connection has been created, unbind.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700393 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Jordan Liu178430d2020-02-10 14:32:15 -0800394 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800395 break;
396 }
chen xu02581692018-11-11 19:03:44 -0800397 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700398 // ResultReceiver callback will execute in this Handler's thread.
399 final ResultReceiver resultReceiver =
400 new ResultReceiver(this) {
401 @Override
402 public void onReceiveResult(int resultCode, Bundle resultData) {
Jordan Liu178430d2020-02-10 14:32:15 -0800403 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700404 // If new service connection has been created, this is stale.
405 if (mServiceConnection[phoneId] != conn) {
406 loge("Received response for stale request.");
407 return;
408 }
409 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
410 if (resultCode == RESULT_ERROR || resultData == null) {
411 // On error, abort config fetching.
chen xudb04c292019-03-26 17:01:15 -0700412 loge("Failed to get carrier config from carrier app: "
413 + getCarrierPackageForPhoneId(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700414 broadcastConfigChangedIntent(phoneId);
Nathan Harold48ac0972019-03-13 22:33:01 -0700415 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700416 return;
417 }
418 PersistableBundle config =
419 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100420 saveConfigToXml(getCarrierPackageForPhoneId(phoneId), "",
421 phoneId, carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700422 mConfigFromCarrierApp[phoneId] = config;
423 sendMessage(
424 obtainMessage(
425 EVENT_FETCH_CARRIER_DONE, phoneId, -1));
426 }
427 };
428 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800429 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700430 ICarrierService carrierService =
431 ICarrierService.Stub.asInterface(conn.service);
432 carrierService.getCarrierConfig(carrierId, resultReceiver);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800433 logdWithLocalLog("Fetch config for carrier app: "
chen xudb04c292019-03-26 17:01:15 -0700434 + getCarrierPackageForPhoneId(phoneId)
435 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700436 } catch (RemoteException e) {
437 loge("Failed to get carrier config: " + e.toString());
Jordan Liu178430d2020-02-10 14:32:15 -0800438 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700439 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800440 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700441 sendMessageDelayed(
442 obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1),
443 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800444 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700445 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800446
Jonathan Basseri930701e2015-06-11 20:56:43 -0700447 case EVENT_BIND_CARRIER_TIMEOUT:
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800448 case EVENT_FETCH_CARRIER_TIMEOUT: {
449 loge("Bind/fetch from carrier app timeout");
chen xuf60b3162019-05-01 21:31:05 -0700450 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
451 // If we attempted to bind to the app, but the service connection is null due to
452 // the race condition that clear config event happens before bind/fetch complete
453 // then config was cleared while we were waiting and we should not continue.
454 if (mServiceConnection[phoneId] != null) {
455 // If a ResponseReceiver callback is in the queue when this happens, we will
456 // unbind twice and throw an exception.
Jordan Liu178430d2020-02-10 14:32:15 -0800457 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700458 broadcastConfigChangedIntent(phoneId);
459 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700460 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700461 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700462 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800463 case EVENT_FETCH_CARRIER_DONE: {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700464 // If we attempted to bind to the app, but the service connection is null, then
465 // config was cleared while we were waiting and we should not continue.
466 if (!msg.getData().getBoolean("loaded_from_xml", false)
467 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800468 break;
469 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700470 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800471 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700472 }
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700473
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800474 case EVENT_CHECK_SYSTEM_UPDATE: {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700475 SharedPreferences sharedPrefs =
476 PreferenceManager.getDefaultSharedPreferences(mContext);
477 final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
478 if (!Build.FINGERPRINT.equals(lastFingerprint)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800479 logd(
Jonathan Basseri65273c82017-07-25 15:08:42 -0700480 "Build fingerprint changed. old: "
481 + lastFingerprint
482 + " new: "
483 + Build.FINGERPRINT);
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700484 clearCachedConfigForPackage(null);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700485 sharedPrefs
486 .edit()
487 .putString(KEY_FINGERPRINT, Build.FINGERPRINT)
488 .apply();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700489 }
490 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700491 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700492
493 case EVENT_SUBSCRIPTION_INFO_UPDATED:
494 broadcastConfigChangedIntent(phoneId);
495 break;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700496 case EVENT_MULTI_SIM_CONFIG_CHANGED:
497 onMultiSimConfigChanged();
498 break;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800499 }
500 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700501 }
502
503 private final Handler mHandler;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800504
505 /**
506 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
507 * receiver for relevant events.
508 */
509 private CarrierConfigLoader(Context context) {
510 mContext = context;
Meng Wang33ad2bc2017-03-16 20:21:20 -0700511 mPlatformCarrierConfigPackage =
512 mContext.getString(R.string.platform_carrier_config_package);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700513 mHandler = new ConfigHandler();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800514
Nanxi Chen3d670502016-03-17 16:32:09 -0700515 IntentFilter bootFilter = new IntentFilter();
516 bootFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
517 context.registerReceiver(mBootReceiver, bootFilter);
518
Junda Liu8a8a53a2015-05-15 16:19:57 -0700519 // Register for package updates. Update app or uninstall app update will have all 3 intents,
520 // in the order or removed, added, replaced, all with extra_replace set to true.
521 IntentFilter pkgFilter = new IntentFilter();
522 pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
523 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
524 pkgFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
525 pkgFilter.addDataScheme("package");
Jordan Liu5ca24762019-07-10 12:51:09 -0700526 context.registerReceiver(mPackageReceiver, pkgFilter);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800527
Malcolm Chen978dda52019-10-08 18:15:25 -0700528 int numPhones = TelephonyManager.from(context).getSupportedModemCount();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700529 mConfigFromDefaultApp = new PersistableBundle[numPhones];
530 mConfigFromCarrierApp = new PersistableBundle[numPhones];
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100531 mPersistentOverrideConfigs = new PersistableBundle[numPhones];
Hall Liu506724b2018-10-22 18:16:14 -0700532 mOverrideConfigs = new PersistableBundle[numPhones];
Zach Johnson36d7aab2015-05-22 15:43:00 -0700533 mServiceConnection = new CarrierServiceConnection[numPhones];
Jordan Liu178430d2020-02-10 14:32:15 -0800534 mServiceBound = new boolean[numPhones];
Junda Liu03aad762017-07-21 13:32:17 -0700535 mHasSentConfigChange = new boolean[numPhones];
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800536 // Make this service available through ServiceManager.
537 ServiceManager.addService(Context.CARRIER_CONFIG_SERVICE, this);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800538 logd("CarrierConfigLoader has started");
Nathan Harold48ac0972019-03-13 22:33:01 -0700539 mSubscriptionInfoUpdater = PhoneFactory.getSubscriptionInfoUpdater();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700540 mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800541 }
542
543 /**
544 * Initialize the singleton CarrierConfigLoader instance.
545 *
546 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
547 */
548 /* package */
549 static CarrierConfigLoader init(Context context) {
550 synchronized (CarrierConfigLoader.class) {
551 if (sInstance == null) {
552 sInstance = new CarrierConfigLoader(context);
553 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700554 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800555 }
556 return sInstance;
557 }
558 }
559
Malcolm Chen5ea18532019-10-24 19:55:44 -0700560 private void clearConfigForPhone(int phoneId, boolean sendBroadcast) {
561 /* Ignore clear configuration request if device is being shutdown. */
562 Phone phone = PhoneFactory.getPhone(phoneId);
563 if (phone != null) {
564 if (phone.isShuttingDown()) {
565 return;
566 }
567 }
568
569 mConfigFromDefaultApp[phoneId] = null;
570 mConfigFromCarrierApp[phoneId] = null;
571 mServiceConnection[phoneId] = null;
572 mHasSentConfigChange[phoneId] = false;
573
574 if (sendBroadcast) broadcastConfigChangedIntent(phoneId, false);
575 }
576
Nathan Harold48ac0972019-03-13 22:33:01 -0700577 private void notifySubscriptionInfoUpdater(int phoneId) {
578 String configPackagename;
579 PersistableBundle configToSend;
580 int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
581 // Prefer the carrier privileged carrier app, but if there is not one, use the platform
582 // default carrier app.
583 if (mConfigFromCarrierApp[phoneId] != null) {
584 configPackagename = getCarrierPackageForPhoneId(phoneId);
585 configToSend = mConfigFromCarrierApp[phoneId];
586 } else {
587 configPackagename = mPlatformCarrierConfigPackage;
588 configToSend = mConfigFromDefaultApp[phoneId];
589 }
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700590
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800591 if (configToSend == null) {
592 configToSend = new PersistableBundle();
593 }
594
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700595 // mOverrideConfigs is for testing. And it will override current configs.
596 PersistableBundle config = mOverrideConfigs[phoneId];
597 if (config != null) {
Torbjorn Eklund1050cb02018-11-16 14:05:38 +0100598 configToSend = new PersistableBundle(configToSend);
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700599 configToSend.putAll(config);
600 }
601
Nathan Harold48ac0972019-03-13 22:33:01 -0700602 mSubscriptionInfoUpdater.updateSubscriptionByCarrierConfigAndNotifyComplete(
603 phoneId, configPackagename, configToSend,
604 mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1));
605 }
606
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800607 private void broadcastConfigChangedIntent(int phoneId) {
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800608 broadcastConfigChangedIntent(phoneId, true);
609 }
610
611 private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800612 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800613 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
chen xuf9db49f2019-03-31 23:04:42 -0700614 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800615 if (addSubIdExtra) {
616 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
617 int[] subIds = SubscriptionManager.getSubId(phoneId);
618 if (!ArrayUtils.isEmpty(subIds)) {
619 TelephonyManager telMgr = TelephonyManager.from(mContext)
620 .createForSubscriptionId(subIds[0]);
621 simApplicationState = telMgr.getSimApplicationState();
622 }
623 // Include subId/carrier id extra only if SIM records are loaded
624 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
625 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
626 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
627 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID,
628 getSpecificCarrierIdForPhoneId(phoneId));
629 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, getCarrierIdForPhoneId(phoneId));
630 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800631 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800632 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800633 logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
Jordan Liu5ca24762019-07-10 12:51:09 -0700634 mContext.sendBroadcast(intent);
Junda Liu03aad762017-07-21 13:32:17 -0700635 mHasSentConfigChange[phoneId] = true;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800636 }
637
638 /** Binds to the default or carrier config app. */
639 private boolean bindToConfigPackage(String pkgName, int phoneId, int eventId) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800640 logdWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700641 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700642 carrierService.setPackage(pkgName);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800643 mServiceConnection[phoneId] = new CarrierServiceConnection(phoneId, pkgName, eventId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800644 try {
Jordan Liu178430d2020-02-10 14:32:15 -0800645 if (mContext.bindService(carrierService, mServiceConnection[phoneId],
646 Context.BIND_AUTO_CREATE)) {
647 mServiceBound[phoneId] = true;
648 return true;
649 } else {
650 return false;
651 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800652 } catch (SecurityException ex) {
653 return false;
654 }
655 }
656
chen xu02581692018-11-11 19:03:44 -0800657 private CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800658 String mcc = "";
659 String mnc = "";
660 String imsi = "";
661 String gid1 = "";
662 String gid2 = "";
663 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
664 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800665 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800666 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700667 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
668 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800669 mcc = simOperator.substring(0, 3);
670 mnc = simOperator.substring(3);
671 }
672 Phone phone = PhoneFactory.getPhone(phoneId);
673 if (phone != null) {
674 imsi = phone.getSubscriberId();
675 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700676 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800677 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800678 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800679 }
chen xua31f22b2019-03-06 15:28:50 -0800680 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800681 }
682
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700683 /** Returns the package name of a priveleged carrier app, or null if there is none. */
684 private String getCarrierPackageForPhoneId(int phoneId) {
685 List<String> carrierPackageNames = TelephonyManager.from(mContext)
686 .getCarrierPackageNamesForIntentAndPhone(
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700687 new Intent(CarrierService.CARRIER_SERVICE_INTERFACE), phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700688 if (carrierPackageNames != null && carrierPackageNames.size() > 0) {
689 return carrierPackageNames.get(0);
690 } else {
691 return null;
692 }
693 }
694
695 private String getIccIdForPhoneId(int phoneId) {
696 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
697 return null;
698 }
699 Phone phone = PhoneFactory.getPhone(phoneId);
700 if (phone == null) {
701 return null;
702 }
703 return phone.getIccSerialNumber();
704 }
705
chen xu02581692018-11-11 19:03:44 -0800706 /**
chen xua31f22b2019-03-06 15:28:50 -0800707 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800708 */
chen xua31f22b2019-03-06 15:28:50 -0800709 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700710 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800711 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700712 }
713 Phone phone = PhoneFactory.getPhone(phoneId);
714 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800715 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700716 }
chen xua31f22b2019-03-06 15:28:50 -0800717 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800718 }
719
720 /**
721 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
722 */
723 private int getCarrierIdForPhoneId(int phoneId) {
724 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
725 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700726 }
chen xu02581692018-11-11 19:03:44 -0800727 Phone phone = PhoneFactory.getPhone(phoneId);
728 if (phone == null) {
729 return TelephonyManager.UNKNOWN_CARRIER_ID;
730 }
731 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700732 }
733
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700734 /**
735 * Writes a bundle to an XML file.
736 *
chen xu02581692018-11-11 19:03:44 -0800737 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -0800738 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -0800739 * should have a single copy of XML file named after carrier id. However, it's still possible
740 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
741 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
742 * carrier while iccid remains the same.
743 *
744 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
745 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700746 *
747 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700748 *
749 * @param packageName the name of the package from which we fetched this bundle.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100750 * @param extraString An extra string to be used in the XML file name.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800751 * @param phoneId the phone ID.
752 * @param carrierId contains all carrier-identifying information.
753 * @param config the bundle to be written. Null will be treated as an empty bundle.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700754 */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100755 private void saveConfigToXml(String packageName, @NonNull String extraString, int phoneId,
756 CarrierIdentifier carrierId, PersistableBundle config) {
Yuchen Dongc15afed2018-04-26 17:05:22 +0800757 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
758 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700759 loge("Skip save config because SIM records are not loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800760 return;
761 }
762
763 final String iccid = getIccIdForPhoneId(phoneId);
Chen Xucd4a6372019-07-29 16:34:52 -0700764 final int cid = carrierId.getSpecificCarrierId();
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700765 if (packageName == null || iccid == null) {
766 loge("Cannot save config with null packageName or iccid.");
767 return;
768 }
Junda Liu02596502016-11-15 13:46:43 -0800769 // b/32668103 Only save to file if config isn't empty.
770 // In case of failure, not caching an empty bundle will
771 // try loading config again on next power on or sim loaded.
772 // Downside is for genuinely empty bundle, will bind and load
773 // on every power on.
774 if (config == null || config.isEmpty()) {
775 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700776 }
777
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700778 final String version = getPackageVersion(packageName);
779 if (version == null) {
780 loge("Failed to get package version for: " + packageName);
781 return;
782 }
783
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800784 logdWithLocalLog(
785 "Save config to xml, packagename: " + packageName + " phoneId: " + phoneId);
chen xudb04c292019-03-26 17:01:15 -0700786
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700787 FileOutputStream outFile = null;
788 try {
789 outFile = new FileOutputStream(
chen xu02581692018-11-11 19:03:44 -0800790 new File(mContext.getFilesDir(),
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100791 getFilenameForConfig(packageName, extraString, iccid, cid)));
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800792 config.putString(KEY_VERSION, version);
793 config.writeToStream(outFile);
794 outFile.flush();
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700795 outFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800796 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700797 loge(e.toString());
798 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700799 }
800
801 /**
802 * Reads a bundle from an XML file.
803 *
804 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +0800805 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700806 *
807 * In case of errors, or if the saved config is from a different package version than the
808 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700809 *
810 * @param packageName the name of the package from which we fetched this bundle.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100811 * @param extraString An extra string to be used in the XML file name.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800812 * @param phoneId the phone ID.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700813 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800814 * version does not match, or reading config fails.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700815 */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100816 private PersistableBundle restoreConfigFromXml(String packageName, @NonNull String extraString,
817 int phoneId) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700818 final String version = getPackageVersion(packageName);
819 if (version == null) {
820 loge("Failed to get package version for: " + packageName);
821 return null;
822 }
Yuchen Dongc15afed2018-04-26 17:05:22 +0800823 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
824 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700825 loge("Skip restoring config because SIM records are not yet loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800826 return null;
827 }
828
829 final String iccid = getIccIdForPhoneId(phoneId);
chen xua31f22b2019-03-06 15:28:50 -0800830 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700831 if (packageName == null || iccid == null) {
832 loge("Cannot restore config with null packageName or iccid.");
833 return null;
834 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700835
836 PersistableBundle restoredBundle = null;
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100837 File file = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700838 FileInputStream inFile = null;
839 try {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100840 file = new File(mContext.getFilesDir(),
841 getFilenameForConfig(packageName, extraString, iccid, cid));
842 inFile = new FileInputStream(file);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700843
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800844 restoredBundle = PersistableBundle.readFromStream(inFile);
845 String savedVersion = restoredBundle.getString(KEY_VERSION);
846 restoredBundle.remove(KEY_VERSION);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700847
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800848 if (!version.equals(savedVersion)) {
849 loge("Saved version mismatch: " + version + " vs " + savedVersion);
850 restoredBundle = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700851 }
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800852
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700853 inFile.close();
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800854 } catch (FileNotFoundException e) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100855 // Missing file is normal occurrence that might occur with a new sim or when restoring
856 // an override file during boot and should not be treated as an error.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800857 if (file != null) logd("File not found: " + file.getPath());
858 } catch (IOException e) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700859 loge(e.toString());
860 }
861
862 return restoredBundle;
863 }
864
Junda Liu8a8a53a2015-05-15 16:19:57 -0700865 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700866 * Clears cached carrier config.
867 * This deletes all saved XML files associated with the given package name. If packageName is
868 * null, then it deletes all saved XML files.
869 *
870 * @param packageName the name of a carrier package, or null if all cached config should be
871 * cleared.
872 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -0700873 */
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700874 private boolean clearCachedConfigForPackage(final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700875 File dir = mContext.getFilesDir();
876 File[] packageFiles = dir.listFiles(new FilenameFilter() {
877 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700878 if (packageName != null) {
879 return filename.startsWith("carrierconfig-" + packageName + "-");
880 } else {
881 return filename.startsWith("carrierconfig-");
882 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700883 }
884 });
Junda Liu8a8a53a2015-05-15 16:19:57 -0700885 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700886 for (File f : packageFiles) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800887 logd("Deleting " + f.getName());
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700888 f.delete();
889 }
Junda Liu8a8a53a2015-05-15 16:19:57 -0700890 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700891 }
892
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700893 /** Builds a canonical file name for a config file. */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100894 private String getFilenameForConfig(@NonNull String packageName, @NonNull String extraString,
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800895 @NonNull String iccid, int cid) {
chen xu02581692018-11-11 19:03:44 -0800896 // the same carrier should have a single copy of XML file named after carrier id.
897 // However, it's still possible that platform doesn't recognize the current sim carrier,
898 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
899 // cases SIM OTA resolves to different carrier while iccid remains the same.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100900 return "carrierconfig-" + packageName + extraString + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700901 }
902
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700903 /** Return the current version code of a package, or null if the name is not found. */
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700904 private String getPackageVersion(String packageName) {
905 try {
906 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -0800907 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700908 } catch (PackageManager.NameNotFoundException e) {
909 return null;
910 }
911 }
912
Jonathan Basseri65273c82017-07-25 15:08:42 -0700913 /**
914 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700915 *
916 * This reads config bundles for the given phoneId. That means getting the latest bundle from
917 * the default app and a privileged carrier app, if present. This will not bind to an app if we
918 * have a saved config file to use instead.
919 */
920 private void updateConfigForPhoneId(int phoneId) {
Junda Liu8a8a53a2015-05-15 16:19:57 -0700921 // Clear in-memory cache for carrier app config, so when carrier app gets uninstalled, no
922 // stale config is left.
923 if (mConfigFromCarrierApp[phoneId] != null &&
924 getCarrierPackageForPhoneId(phoneId) == null) {
925 mConfigFromCarrierApp[phoneId] = null;
926 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700927 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700928 }
929
Malcolm Chen5ea18532019-10-24 19:55:44 -0700930 private void onMultiSimConfigChanged() {
931 for (int i = TelephonyManager.from(mContext).getActiveModemCount();
932 i < mConfigFromDefaultApp.length; i++) {
933 clearConfigForPhone(i, false);
934 }
935 }
936
Hall Liu506724b2018-10-22 18:16:14 -0700937 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800938 @NonNull
939 public PersistableBundle getConfigForSubId(int subId, String callingPackage) {
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -0800940 return getConfigForSubIdWithFeature(subId, callingPackage, null);
941 }
942
943 @Override
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -0800944 @NonNull
945 public PersistableBundle getConfigForSubIdWithFeature(int subId, String callingPackage,
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -0800946 String callingFeatureId) {
947 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subId, callingPackage,
948 callingFeatureId, "getCarrierConfig")) {
Malcolm Chen6d3d6b32018-03-01 13:58:03 -0800949 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -0700950 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -0700951
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800952 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700953 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800954 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700955 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800956 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700957 retConfig.putAll(config);
yinxud0104832019-08-16 14:20:08 -0700958 if (getCarrierPackageForPhoneId(phoneId) == null) {
959 retConfig.putBoolean(
960 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
961 }
yinxuc5e27622017-11-29 15:08:50 -0800962 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800963 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800964 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700965 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -0800966 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
967 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100968 config = mPersistentOverrideConfigs[phoneId];
969 if (config != null) {
970 retConfig.putAll(config);
971 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
972 }
Hall Liu506724b2018-10-22 18:16:14 -0700973 config = mOverrideConfigs[phoneId];
974 if (config != null) {
975 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -0700976 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800977 }
978 return retConfig;
979 }
980
981 @Override
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100982 public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides,
983 boolean persistent) {
Hall Liu506724b2018-10-22 18:16:14 -0700984 mContext.enforceCallingOrSelfPermission(
985 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Brad Ebingerf6281ad2019-04-25 11:02:04 -0700986 //TODO: Also check for SHELL UID to restrict this method to testing only (b/131326259)
Hall Liu506724b2018-10-22 18:16:14 -0700987 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
988 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -0800989 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
Hall Liu506724b2018-10-22 18:16:14 -0700990 return;
991 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100992 overrideConfig(mOverrideConfigs, phoneId, overrides);
Hall Liu506724b2018-10-22 18:16:14 -0700993
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100994 if (persistent) {
995 overrideConfig(mPersistentOverrideConfigs, phoneId, overrides);
996
997 if (overrides != null) {
998 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
999 saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId,
1000 carrierId, mPersistentOverrideConfigs[phoneId]);
1001 } else {
1002 final String iccid = getIccIdForPhoneId(phoneId);
1003 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1004 String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage,
1005 OVERRIDE_PACKAGE_ADDITION, iccid, cid);
1006 File fileToDelete = new File(mContext.getFilesDir(), fileName);
1007 fileToDelete.delete();
1008 }
Hall Liu506724b2018-10-22 18:16:14 -07001009 }
Brad Ebingerc9d1fa12019-04-17 15:21:18 -07001010 notifySubscriptionInfoUpdater(phoneId);
Hall Liu506724b2018-10-22 18:16:14 -07001011 }
1012
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001013 private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId,
1014 @Nullable PersistableBundle overrides) {
1015 if (overrides == null) {
1016 currentOverrides[phoneId] = new PersistableBundle();
1017 } else if (currentOverrides[phoneId] == null) {
1018 currentOverrides[phoneId] = overrides;
1019 } else {
1020 currentOverrides[phoneId].putAll(overrides);
1021 }
1022 }
1023
Hall Liu506724b2018-10-22 18:16:14 -07001024 @Override
Jonathan Basseri86030352015-06-08 12:27:56 -07001025 public void notifyConfigChangedForSubId(int subId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001026 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001027 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001028 logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001029 return;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001030 }
Jordan Liud7d57fe2019-04-16 12:29:43 -07001031
1032 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001033 // system privileged app with MODIFY_PHONE_STATE permission.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001034 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext, subId,
1035 "Require carrier privileges or MODIFY_PHONE_STATE permission.");
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001036
1037 // This method should block until deleting has completed, so that an error which prevents us
1038 // from clearing the cache is passed back to the carrier app. With the files successfully
1039 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001040 String callingPackageName = mContext.getPackageManager().getNameForUid(
1041 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001042 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001043 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001044 }
1045
1046 @Override
1047 public void updateConfigForPhoneId(int phoneId, String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -07001048 mContext.enforceCallingOrSelfPermission(
1049 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001050 logdWithLocalLog("Update config for phoneId: " + phoneId + " simState: " + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001051 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1052 return;
1053 }
1054 // requires Java 7 for switch on string.
1055 switch (simState) {
1056 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1057 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001058 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001059 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Malcolm Chendb66de12019-12-09 18:57:07 -08001060 case IccCardConstants.INTENT_VALUE_ICC_NOT_READY:
Junda Liu9f2d2712015-05-15 14:22:45 -07001061 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001062 break;
1063 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1064 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001065 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001066 break;
1067 }
1068 }
1069
Junda Liu43d723a2015-05-12 17:23:45 -07001070 @Override
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001071 public String getDefaultCarrierServicePackageName() {
Shuo Qianefdb7962019-12-19 15:54:27 -08001072 mContext.enforceCallingOrSelfPermission(
1073 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1074 "getDefaultCarrierServicePackageName");
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001075 return mPlatformCarrierConfigPackage;
1076 }
1077
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001078 private void unbindIfBound(Context context, CarrierServiceConnection conn,
1079 int phoneId) {
1080 if (mServiceBound[phoneId]) {
1081 mServiceBound[phoneId] = false;
1082 context.unbindService(conn);
1083 }
1084 }
1085
1086 /**
1087 * If {@code args} contains {@link #DUMP_ARG_REQUESTING_PACKAGE} and a following package name,
1088 * we'll also call {@link IBinder#dump} on the default carrier service (if bound) and the
1089 * specified carrier service (if bound). Typically, this is done for connectivity bug reports
1090 * where we don't call {@code dumpsys activity service all-non-platform} because that contains
1091 * too much info, but we still want to let carrier apps include their diagnostics.
1092 */
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001093 @Override
Junda Liu43d723a2015-05-12 17:23:45 -07001094 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001095 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
Junda Liu43d723a2015-05-12 17:23:45 -07001096 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1097 != PackageManager.PERMISSION_GRANTED) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001098 indentPW.println("Permission Denial: can't dump carrierconfig from from pid="
Junda Liu43d723a2015-05-12 17:23:45 -07001099 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1100 return;
1101 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001102 indentPW.println("CarrierConfigLoader: " + this);
Junda Liu43d723a2015-05-12 17:23:45 -07001103 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001104 indentPW.println("Phone Id = " + i);
shuoq26a3a4c2016-12-16 11:06:48 -08001105 // display default values in CarrierConfigManager
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001106 printConfig(CarrierConfigManager.getDefaultConfig(), indentPW,
shuoq26a3a4c2016-12-16 11:06:48 -08001107 "Default Values from CarrierConfigManager");
shuoq26a3a4c2016-12-16 11:06:48 -08001108 // display ConfigFromDefaultApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001109 printConfig(mConfigFromDefaultApp[i], indentPW, "mConfigFromDefaultApp");
shuoq26a3a4c2016-12-16 11:06:48 -08001110 // display ConfigFromCarrierApp
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001111 printConfig(mConfigFromCarrierApp[i], indentPW, "mConfigFromCarrierApp");
1112 printConfig(mPersistentOverrideConfigs[i], indentPW, "mPersistentOverrideConfigs");
1113 printConfig(mOverrideConfigs[i], indentPW, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001114 }
chen xudb04c292019-03-26 17:01:15 -07001115
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001116 indentPW.println("CarrierConfigLoadingLog=");
1117 mCarrierConfigLoadingLog.dump(fd, indentPW, args);
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001118
1119 int requestingPackageIndex = ArrayUtils.indexOf(args, DUMP_ARG_REQUESTING_PACKAGE);
1120 if (requestingPackageIndex != -1 && requestingPackageIndex < args.length - 1
1121 && !TextUtils.isEmpty(args[requestingPackageIndex + 1])) {
1122 String requestingPackage = args[requestingPackageIndex + 1];
1123 indentPW.println("");
1124 logd("Including default and requesting package " + requestingPackage
1125 + " carrier services in dump");
1126 indentPW.println("Connected services");
1127 dumpCarrierServiceIfBound(fd, indentPW, "Default config package",
1128 mPlatformCarrierConfigPackage);
1129 dumpCarrierServiceIfBound(fd, indentPW, "Requesting package", requestingPackage);
1130 }
shuoq26a3a4c2016-12-16 11:06:48 -08001131 }
1132
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001133 private void printConfig(PersistableBundle configApp, IndentingPrintWriter indentPW,
1134 String name) {
1135 indentPW.increaseIndent();
shuoq26a3a4c2016-12-16 11:06:48 -08001136 if (configApp == null) {
shuoq26a3a4c2016-12-16 11:06:48 -08001137 indentPW.println(name + " : null ");
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001138 indentPW.decreaseIndent();
1139 indentPW.println("");
shuoq26a3a4c2016-12-16 11:06:48 -08001140 return;
1141 }
shuoq26a3a4c2016-12-16 11:06:48 -08001142 indentPW.println(name + " : ");
1143 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1144 Collections.sort(sortedKeys);
1145 indentPW.increaseIndent();
1146 indentPW.increaseIndent();
1147 for (String key : sortedKeys) {
1148 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1149 indentPW.println(key + " = " +
1150 Arrays.toString((Object[]) configApp.get(key)));
1151 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1152 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1153 } else {
1154 indentPW.println(key + " = " + configApp.get(key));
1155 }
Junda Liu43d723a2015-05-12 17:23:45 -07001156 }
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001157 indentPW.decreaseIndent();
1158 indentPW.decreaseIndent();
1159 indentPW.decreaseIndent();
1160 indentPW.println("");
Junda Liu43d723a2015-05-12 17:23:45 -07001161 }
1162
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001163 private void dumpCarrierServiceIfBound(FileDescriptor fd, IndentingPrintWriter indentPW,
1164 String prefix, String pkgName) {
1165 // Null package is possible if it's early in the boot process, there was a recent crash, we
1166 // loaded the config from XML most recently, or a SIM slot is empty. Carrier apps with
1167 // long-lived bindings should typically get dumped here regardless. Even if an app is being
1168 // used for multiple phoneIds, we assume that it's smart enough to handle that on its own,
1169 // and that in most cases we'd just be dumping duplicate information and bloating a report.
1170 indentPW.increaseIndent();
1171 indentPW.println(prefix + " : " + pkgName);
1172 for (CarrierServiceConnection connection : mServiceConnection) {
1173 try {
1174 // We don't pay attention to mServiceBound[connection.phoneId] because typically
1175 // carrier apps will request long-lived bindings, and even if we unbind the app, it
1176 // may still be alive due to CarrierServiceBindHelper.
1177 if (connection == null || connection.service == null || !TextUtils.equals(pkgName,
1178 connection.pkgName) || !connection.service.isBinderAlive()
1179 || !connection.service.pingBinder()) {
1180 continue;
1181 }
1182 // Flush before we let the app output anything to ensure correct ordering of output.
1183 // Internally, Binder#dump calls flush on its printer after finishing so we don't
1184 // need to after the call finishes.
1185 indentPW.flush();
1186 try {
1187 logd("Dumping " + pkgName);
1188 // We don't need to give the carrier service any args.
1189 connection.service.dump(fd, null /* args */);
1190 logd("Done with " + pkgName);
1191 indentPW.decreaseIndent();
1192 indentPW.println("");
1193 return;
1194 } catch (RemoteException e) {
1195 indentPW.println("RemoteException");
1196 logd("RemoteException from " + pkgName, e);
1197 e.printStackTrace(indentPW);
1198 indentPW.decreaseIndent();
1199 indentPW.println("");
1200 return;
1201 }
1202 } catch (NullPointerException e) {
1203 // Highly unlikely, but possible if the carrier app was just unbound right before we
1204 // we tried to dump it so the binder was reset to null. Loop in case we have more
1205 // candidates on other phoneIds.
1206 logd("NullPointerException from " + pkgName, e);
1207 }
Jordan Liu46ed5db2020-01-28 08:55:20 -08001208 }
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001209 indentPW.increaseIndent();
1210 indentPW.println("Not bound");
1211 indentPW.decreaseIndent();
1212 indentPW.decreaseIndent();
1213 indentPW.println("");
Jordan Liu46ed5db2020-01-28 08:55:20 -08001214 }
1215
Zach Johnson36d7aab2015-05-22 15:43:00 -07001216 private class CarrierServiceConnection implements ServiceConnection {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001217 int phoneId;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001218 String pkgName;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001219 int eventId;
1220 IBinder service;
1221
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001222 CarrierServiceConnection(int phoneId, String pkgName, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001223 this.phoneId = phoneId;
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001224 this.pkgName = pkgName;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001225 this.eventId = eventId;
1226 }
1227
1228 @Override
1229 public void onServiceConnected(ComponentName name, IBinder service) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001230 logd("Connected to config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001231 this.service = service;
1232 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1233 }
1234
1235 @Override
1236 public void onServiceDisconnected(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001237 logd("Disconnected from config app: " + name.flattenToShortString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001238 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001239 }
1240
1241 @Override
1242 public void onBindingDied(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001243 logd("Binding died from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001244 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001245 }
1246
1247 @Override
1248 public void onNullBinding(ComponentName name) {
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001249 logd("Null binding from config app: " + name.flattenToShortString());
Jordan Liu46ed5db2020-01-28 08:55:20 -08001250 this.service = null;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001251 }
1252 }
1253
1254 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1255 @Override
1256 public void onReceive(Context context, Intent intent) {
1257 String action = intent.getAction();
Junda Liu8a8a53a2015-05-15 16:19:57 -07001258 boolean replace = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1259 // If replace is true, only care ACTION_PACKAGE_REPLACED.
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001260 if (replace && !Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
Junda Liu8a8a53a2015-05-15 16:19:57 -07001261 return;
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001262 }
Junda Liu8a8a53a2015-05-15 16:19:57 -07001263
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001264 switch (action) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001265 case Intent.ACTION_BOOT_COMPLETED:
1266 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1267 break;
1268
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001269 case Intent.ACTION_PACKAGE_ADDED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001270 case Intent.ACTION_PACKAGE_REMOVED:
Junda Liu8a8a53a2015-05-15 16:19:57 -07001271 case Intent.ACTION_PACKAGE_REPLACED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001272 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1273 String packageName = mContext.getPackageManager().getNameForUid(uid);
Junda Liu8a8a53a2015-05-15 16:19:57 -07001274 if (packageName != null) {
1275 // We don't have a phoneId for arg1.
1276 mHandler.sendMessage(
1277 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, packageName));
1278 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001279 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001280 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001281 }
1282 }
1283
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001284 private void logd(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001285 Log.d(LOG_TAG, msg);
1286 }
1287
Hunter Knepshieldd0ed6212020-01-28 17:43:16 -08001288 private void logd(String msg, Throwable tr) {
1289 Log.d(LOG_TAG, msg, tr);
1290 }
1291
Hunter Knepshield9f091fc2020-01-28 17:41:43 -08001292 private void logdWithLocalLog(String msg) {
chen xudb04c292019-03-26 17:01:15 -07001293 Log.d(LOG_TAG, msg);
1294 mCarrierConfigLoadingLog.log(msg);
1295 }
1296
1297 private void loge(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001298 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07001299 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001300 }
1301}