blob: 0052cd0f27da55022745b7c06f4c14632689ebd4 [file] [log] [blame]
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001/**
2 * 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 Eklundcbd73752019-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 Basseri7697cae2015-07-06 15:49:23 -070042import android.preference.PreferenceManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080043import android.service.carrier.CarrierIdentifier;
Zach Johnson36d7aab2015-05-22 15:43:00 -070044import android.service.carrier.CarrierService;
45import android.service.carrier.ICarrierService;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080046import android.telephony.CarrierConfigManager;
47import android.telephony.SubscriptionManager;
Peter Wangc035ce42020-01-08 21:00:22 -080048import android.telephony.TelephonyFrameworkInitializer;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080049import android.telephony.TelephonyManager;
chen xudb04c292019-03-26 17:01:15 -070050import android.util.LocalLog;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080051import android.util.Log;
52
53import com.android.internal.telephony.ICarrierConfigLoader;
54import com.android.internal.telephony.IccCardConstants;
55import com.android.internal.telephony.Phone;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080056import com.android.internal.telephony.PhoneFactory;
Nathan Harold48ac0972019-03-13 22:33:01 -070057import com.android.internal.telephony.SubscriptionInfoUpdater;
Jeff Davidsona8e4e242018-03-15 17:16:18 -070058import com.android.internal.telephony.TelephonyPermissions;
Meng Wang103220b2019-11-25 11:21:26 -080059import com.android.internal.telephony.util.ArrayUtils;
shuoq26a3a4c2016-12-16 11:06:48 -080060import com.android.internal.util.IndentingPrintWriter;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080061
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070062import java.io.File;
Junda Liu43d723a2015-05-12 17:23:45 -070063import java.io.FileDescriptor;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070064import java.io.FileInputStream;
65import java.io.FileNotFoundException;
66import java.io.FileOutputStream;
Jonathan Basseri1f743c92015-05-15 00:19:46 -070067import java.io.FilenameFilter;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070068import java.io.IOException;
Junda Liu43d723a2015-05-12 17:23:45 -070069import java.io.PrintWriter;
shuoq26a3a4c2016-12-16 11:06:48 -080070import java.util.ArrayList;
71import java.util.Arrays;
72import java.util.Collections;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080073import java.util.List;
74
75/**
76 * CarrierConfigLoader binds to privileged carrier apps to fetch carrier config overlays.
Jonathan Basseri6465afd2015-02-25 13:05:57 -080077 */
Jonathan Basseri6465afd2015-02-25 13:05:57 -080078public class CarrierConfigLoader extends ICarrierConfigLoader.Stub {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070079 private static final String LOG_TAG = "CarrierConfigLoader";
Meng Wang33ad2bc2017-03-16 20:21:20 -070080
81 // Package name for platform carrier config app, bundled with system image.
82 private final String mPlatformCarrierConfigPackage;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080083
84 /** The singleton instance. */
85 private static CarrierConfigLoader sInstance;
86 // The context for phone app, passed from PhoneGlobals.
87 private Context mContext;
88 // Carrier configs from default app, indexed by phoneID.
Jonathan Basseric31f1f32015-05-12 10:13:03 -070089 private PersistableBundle[] mConfigFromDefaultApp;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080090 // Carrier configs from privileged carrier config app, indexed by phoneID.
Jonathan Basseric31f1f32015-05-12 10:13:03 -070091 private PersistableBundle[] mConfigFromCarrierApp;
Torbjorn Eklundcbd73752019-02-13 11:16:25 +010092 // Persistent Carrier configs that are provided via the override test API, indexed by phone ID.
93 private PersistableBundle[] mPersistentOverrideConfigs;
Hall Liu506724b2018-10-22 18:16:14 -070094 // Carrier configs that are provided via the override test API, indexed by phone ID.
95 private PersistableBundle[] mOverrideConfigs;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080096 // Service connection for binding to config app.
Zach Johnson36d7aab2015-05-22 15:43:00 -070097 private CarrierServiceConnection[] mServiceConnection;
Junda Liu03aad762017-07-21 13:32:17 -070098 // Whether we have sent config change bcast for each phone id.
99 private boolean[] mHasSentConfigChange;
Nathan Harold48ac0972019-03-13 22:33:01 -0700100 // SubscriptionInfoUpdater
101 private final SubscriptionInfoUpdater mSubscriptionInfoUpdater;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800102
Nanxi Chen3d670502016-03-17 16:32:09 -0700103 // Broadcast receiver for Boot intents, register intent filter in construtor.
104 private final BroadcastReceiver mBootReceiver = new ConfigLoaderBroadcastReceiver();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800105 // Broadcast receiver for SIM and pkg intents, register intent filter in constructor.
Nanxi Chen3d670502016-03-17 16:32:09 -0700106 private final BroadcastReceiver mPackageReceiver = new ConfigLoaderBroadcastReceiver();
Jack Yu37d219a2019-10-17 15:19:48 -0700107 private final LocalLog mCarrierConfigLoadingLog = new LocalLog(100);
chen xudb04c292019-03-26 17:01:15 -0700108
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800109
110 // Message codes; see mHandler below.
111 // Request from SubscriptionInfoUpdater when SIM becomes absent or error.
112 private static final int EVENT_CLEAR_CONFIG = 0;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800113 // Has connected to default app.
114 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
115 // Has connected to carrier app.
116 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700117 // Config has been loaded from default app (or cache).
118 private static final int EVENT_FETCH_DEFAULT_DONE = 5;
119 // Config has been loaded from carrier app (or cache).
120 private static final int EVENT_FETCH_CARRIER_DONE = 6;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700121 // Attempt to fetch from default app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700122 private static final int EVENT_DO_FETCH_DEFAULT = 7;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700123 // Attempt to fetch from carrier app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700124 private static final int EVENT_DO_FETCH_CARRIER = 8;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700125 // A package has been installed, uninstalled, or updated.
126 private static final int EVENT_PACKAGE_CHANGED = 9;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700127 // Bind timed out for the default app.
128 private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
129 // Bind timed out for a carrier app.
130 private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700131 // Check if the system fingerprint has changed.
132 private static final int EVENT_CHECK_SYSTEM_UPDATE = 12;
Nanxi Chen3d670502016-03-17 16:32:09 -0700133 // Rerun carrier config binding after system is unlocked.
134 private static final int EVENT_SYSTEM_UNLOCKED = 13;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700135 // Fetching config timed out from the default app.
136 private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14;
137 // Fetching config timed out from a carrier app.
138 private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15;
Nathan Harold48ac0972019-03-13 22:33:01 -0700139 // SubscriptionInfoUpdater has finished updating the sub for the carrier config.
140 private static final int EVENT_SUBSCRIPTION_INFO_UPDATED = 16;
Malcolm Chen5acb9cf2019-10-24 19:55:44 -0700141 // Multi-SIM config changed.
142 private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 17;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700143
Junda Liu6cb45002016-03-22 17:18:20 -0700144 private static final int BIND_TIMEOUT_MILLIS = 30000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800145
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800146 // Keys used for saving and restoring config bundle from file.
147 private static final String KEY_VERSION = "__carrier_config_package_version__";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800148
Torbjorn Eklundcbd73752019-02-13 11:16:25 +0100149 private static final String OVERRIDE_PACKAGE_ADDITION = "-override";
150
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700151 // SharedPreferences key for last known build fingerprint.
152 private static final String KEY_FINGERPRINT = "build_fingerprint";
153
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800154 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700155 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700156 // For each phoneId, the event sequence should be:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700157 // fetch default, connected to default, fetch default (async), fetch default done,
158 // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700159 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700160 // If there is a saved config file for either the default app or the carrier app, we skip
161 // binding to the app and go straight from fetch to loaded.
162 //
163 // At any time, at most one connection is active. If events are not in this order, previous
164 // connection will be unbound, so only latest event takes effect.
165 //
166 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
167 // 1. loading from carrier app (even if read from a file)
168 // 2. loading from default app if there is no carrier app (even if read from a file)
169 // 3. clearing config (e.g. due to sim removal)
170 // 4. encountering bind or IPC error
Jonathan Basseri65273c82017-07-25 15:08:42 -0700171 private class ConfigHandler extends Handler {
172 @Override
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800173 public void handleMessage(Message msg) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700174 final int phoneId = msg.arg1;
chen xuc4810972019-04-17 23:44:43 -0700175 logWithLocalLog("mHandler: " + msg.what + " phoneId: " + phoneId);
Malcolm Chen5acb9cf2019-10-24 19:55:44 -0700176 if (!SubscriptionManager.isValidPhoneId(phoneId)
177 && msg.what != EVENT_MULTI_SIM_CONFIG_CHANGED) {
178 return;
179 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800180 switch (msg.what) {
181 case EVENT_CLEAR_CONFIG:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700182 {
Malcolm Chen5acb9cf2019-10-24 19:55:44 -0700183 clearConfigForPhone(phoneId, true);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800184 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700185 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700186
Nanxi Chen3d670502016-03-17 16:32:09 -0700187 case EVENT_SYSTEM_UNLOCKED:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700188 {
Malcolm Chen5acb9cf2019-10-24 19:55:44 -0700189 for (int i = 0; i < TelephonyManager.from(mContext).getActiveModemCount();
190 ++i) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700191 // When user unlock device, we should only try to send broadcast again if we
192 // have sent it before unlock. This will avoid we try to load carrier config
193 // when SIM is still loading when unlock happens.
Junda Liu03aad762017-07-21 13:32:17 -0700194 if (mHasSentConfigChange[i]) {
Jack Yu37d219a2019-10-17 15:19:48 -0700195 logWithLocalLog("System unlocked");
Junda Liu03aad762017-07-21 13:32:17 -0700196 updateConfigForPhoneId(i);
197 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700198 }
199 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700200 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700201
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700202 case EVENT_PACKAGE_CHANGED:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700203 {
204 final String carrierPackageName = (String) msg.obj;
205 // Only update if there are cached config removed to avoid updating config for
206 // unrelated packages.
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700207 if (clearCachedConfigForPackage(carrierPackageName)) {
Malcolm Chen5acb9cf2019-10-24 19:55:44 -0700208 int numPhones = TelephonyManager.from(mContext).getActiveModemCount();
Junda Liu8a8a53a2015-05-15 16:19:57 -0700209 for (int i = 0; i < numPhones; ++i) {
Jack Yu37d219a2019-10-17 15:19:48 -0700210 logWithLocalLog("Package changed: " + carrierPackageName
211 + ", phone=" + i);
Junda Liu8a8a53a2015-05-15 16:19:57 -0700212 updateConfigForPhoneId(i);
213 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700214 }
215 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700216 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700217
Jonathan Basseri65273c82017-07-25 15:08:42 -0700218 case EVENT_DO_FETCH_DEFAULT:
219 {
Torbjorn Eklundcbd73752019-02-13 11:16:25 +0100220 // Restore persistent override values.
221 PersistableBundle config = restoreConfigFromXml(
222 mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId);
223 if (config != null) {
224 log("Loaded persistent override config from XML. package="
225 + mPlatformCarrierConfigPackage
226 + " phoneId=" + phoneId);
227 mPersistentOverrideConfigs[phoneId] = config;
228 }
229
230 config = restoreConfigFromXml(mPlatformCarrierConfigPackage, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700231 if (config != null) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700232 log(
233 "Loaded config from XML. package="
234 + mPlatformCarrierConfigPackage
235 + " phoneId="
236 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700237 mConfigFromDefaultApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700238 Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700239 newMsg.getData().putBoolean("loaded_from_xml", true);
240 mHandler.sendMessage(newMsg);
241 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700242 // No cached config, so fetch it from the default app.
243 if (bindToConfigPackage(
244 mPlatformCarrierConfigPackage,
245 phoneId,
246 EVENT_CONNECTED_TO_DEFAULT)) {
247 sendMessageDelayed(
248 obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700249 BIND_TIMEOUT_MILLIS);
250 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700251 // Send broadcast if bind fails.
Nathan Harold48ac0972019-03-13 22:33:01 -0700252 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700253 // TODO: We *must* call unbindService even if bindService returns false.
254 // (And possibly if SecurityException was thrown.)
chen xudb04c292019-03-26 17:01:15 -0700255 loge("binding to default app: "
256 + mPlatformCarrierConfigPackage + " fails");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700257 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800258 }
259 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700260 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800261
262 case EVENT_CONNECTED_TO_DEFAULT:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700263 {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700264 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700265 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800266 // If new service connection has been created, unbind.
267 if (mServiceConnection[phoneId] != conn || conn.service == null) {
268 mContext.unbindService(conn);
269 break;
270 }
chen xu02581692018-11-11 19:03:44 -0800271 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700272 // ResultReceiver callback will execute in this Handler's thread.
273 final ResultReceiver resultReceiver =
274 new ResultReceiver(this) {
275 @Override
276 public void onReceiveResult(int resultCode, Bundle resultData) {
277 mContext.unbindService(conn);
278 // If new service connection has been created, this is stale.
279 if (mServiceConnection[phoneId] != conn) {
280 loge("Received response for stale request.");
281 return;
282 }
283 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
284 if (resultCode == RESULT_ERROR || resultData == null) {
285 // On error, abort config fetching.
286 loge("Failed to get carrier config");
Nathan Harold48ac0972019-03-13 22:33:01 -0700287 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700288 return;
289 }
290 PersistableBundle config =
291 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklundcbd73752019-02-13 11:16:25 +0100292 saveConfigToXml(mPlatformCarrierConfigPackage, "", phoneId,
Chen Xu49b1de32019-07-29 16:34:52 -0700293 carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700294 mConfigFromDefaultApp[phoneId] = config;
295 sendMessage(
296 obtainMessage(
297 EVENT_FETCH_DEFAULT_DONE, phoneId, -1));
298 }
299 };
300 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800301 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700302 ICarrierService carrierService =
303 ICarrierService.Stub.asInterface(conn.service);
304 carrierService.getCarrierConfig(carrierId, resultReceiver);
chen xudb04c292019-03-26 17:01:15 -0700305 logWithLocalLog("fetch config for default app: "
306 + mPlatformCarrierConfigPackage
307 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700308 } catch (RemoteException e) {
chen xudb04c292019-03-26 17:01:15 -0700309 loge("Failed to get carrier config from default app: " +
310 mPlatformCarrierConfigPackage + " err: " + e.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700311 mContext.unbindService(conn);
312 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800313 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700314 sendMessageDelayed(
315 obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1),
316 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800317 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700318 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800319
Jonathan Basseri930701e2015-06-11 20:56:43 -0700320 case EVENT_BIND_DEFAULT_TIMEOUT:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700321 case EVENT_FETCH_DEFAULT_TIMEOUT:
322 {
chen xudb04c292019-03-26 17:01:15 -0700323 loge("bind/fetch time out from " + mPlatformCarrierConfigPackage);
chen xuf60b3162019-05-01 21:31:05 -0700324 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
325 // If we attempted to bind to the app, but the service connection is null due to
326 // the race condition that clear config event happens before bind/fetch complete
327 // then config was cleared while we were waiting and we should not continue.
328 if (mServiceConnection[phoneId] != null) {
329 // If a ResponseReceiver callback is in the queue when this happens, we will
330 // unbind twice and throw an exception.
331 mContext.unbindService(mServiceConnection[phoneId]);
332 broadcastConfigChangedIntent(phoneId);
333 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700334 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700335 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700336 }
Jonathan Basseri930701e2015-06-11 20:56:43 -0700337
Jonathan Basseri65273c82017-07-25 15:08:42 -0700338 case EVENT_FETCH_DEFAULT_DONE:
339 {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700340 // If we attempted to bind to the app, but the service connection is null, then
341 // config was cleared while we were waiting and we should not continue.
342 if (!msg.getData().getBoolean("loaded_from_xml", false)
343 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800344 break;
345 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700346 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700347 if (carrierPackageName != null) {
348 log("Found carrier config app: " + carrierPackageName);
Jordan Liu38a614c2019-03-20 15:01:17 -0700349 sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId, -1));
Jonathan Basserib919e932015-05-27 16:53:08 +0000350 } else {
Nathan Harold48ac0972019-03-13 22:33:01 -0700351 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700352 }
353 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700354 }
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700355
Jonathan Basseri65273c82017-07-25 15:08:42 -0700356 case EVENT_DO_FETCH_CARRIER:
357 {
358 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700359 final PersistableBundle config =
Torbjorn Eklundcbd73752019-02-13 11:16:25 +0100360 restoreConfigFromXml(carrierPackageName, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700361 if (config != null) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700362 log(
363 "Loaded config from XML. package="
364 + carrierPackageName
365 + " phoneId="
366 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700367 mConfigFromCarrierApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700368 Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700369 newMsg.getData().putBoolean("loaded_from_xml", true);
370 sendMessage(newMsg);
371 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700372 // No cached config, so fetch it from a carrier app.
Jonathan Basseri40473a52015-08-14 14:36:29 -0700373 if (carrierPackageName != null
Jonathan Basseri65273c82017-07-25 15:08:42 -0700374 && bindToConfigPackage(
375 carrierPackageName,
376 phoneId,
377 EVENT_CONNECTED_TO_CARRIER)) {
378 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);
chen xudb04c292019-03-26 17:01:15 -0700384 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
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800391 case EVENT_CONNECTED_TO_CARRIER:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700392 {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700393 removeMessages(EVENT_BIND_CARRIER_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700394 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800395 // If new service connection has been created, unbind.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700396 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800397 mContext.unbindService(conn);
398 break;
399 }
chen xu02581692018-11-11 19:03:44 -0800400 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700401 // ResultReceiver callback will execute in this Handler's thread.
402 final ResultReceiver resultReceiver =
403 new ResultReceiver(this) {
404 @Override
405 public void onReceiveResult(int resultCode, Bundle resultData) {
406 mContext.unbindService(conn);
407 // If new service connection has been created, this is stale.
408 if (mServiceConnection[phoneId] != conn) {
409 loge("Received response for stale request.");
410 return;
411 }
412 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
413 if (resultCode == RESULT_ERROR || resultData == null) {
414 // On error, abort config fetching.
chen xudb04c292019-03-26 17:01:15 -0700415 loge("Failed to get carrier config from carrier app: "
416 + getCarrierPackageForPhoneId(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700417 broadcastConfigChangedIntent(phoneId);
Nathan Harold48ac0972019-03-13 22:33:01 -0700418 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700419 return;
420 }
421 PersistableBundle config =
422 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklundcbd73752019-02-13 11:16:25 +0100423 saveConfigToXml(getCarrierPackageForPhoneId(phoneId), "",
424 phoneId, carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700425 mConfigFromCarrierApp[phoneId] = config;
426 sendMessage(
427 obtainMessage(
428 EVENT_FETCH_CARRIER_DONE, phoneId, -1));
429 }
430 };
431 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800432 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700433 ICarrierService carrierService =
434 ICarrierService.Stub.asInterface(conn.service);
435 carrierService.getCarrierConfig(carrierId, resultReceiver);
chen xudb04c292019-03-26 17:01:15 -0700436 logWithLocalLog("fetch config for carrier app: "
437 + getCarrierPackageForPhoneId(phoneId)
438 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700439 } catch (RemoteException e) {
440 loge("Failed to get carrier config: " + e.toString());
441 mContext.unbindService(conn);
442 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800443 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700444 sendMessageDelayed(
445 obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1),
446 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800447 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700448 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800449
Jonathan Basseri930701e2015-06-11 20:56:43 -0700450 case EVENT_BIND_CARRIER_TIMEOUT:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700451 case EVENT_FETCH_CARRIER_TIMEOUT:
452 {
chen xudb04c292019-03-26 17:01:15 -0700453 loge("bind/fetch from carrier app timeout");
chen xuf60b3162019-05-01 21:31:05 -0700454 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
455 // If we attempted to bind to the app, but the service connection is null due to
456 // the race condition that clear config event happens before bind/fetch complete
457 // then config was cleared while we were waiting and we should not continue.
458 if (mServiceConnection[phoneId] != null) {
459 // If a ResponseReceiver callback is in the queue when this happens, we will
460 // unbind twice and throw an exception.
461 mContext.unbindService(mServiceConnection[phoneId]);
462 broadcastConfigChangedIntent(phoneId);
463 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700464 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700465 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700466 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700467 case EVENT_FETCH_CARRIER_DONE:
468 {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700469 // If we attempted to bind to the app, but the service connection is null, then
470 // config was cleared while we were waiting and we should not continue.
471 if (!msg.getData().getBoolean("loaded_from_xml", false)
472 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800473 break;
474 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700475 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800476 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700477 }
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700478
479 case EVENT_CHECK_SYSTEM_UPDATE:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700480 {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700481 SharedPreferences sharedPrefs =
482 PreferenceManager.getDefaultSharedPreferences(mContext);
483 final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
484 if (!Build.FINGERPRINT.equals(lastFingerprint)) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700485 log(
486 "Build fingerprint changed. old: "
487 + lastFingerprint
488 + " new: "
489 + Build.FINGERPRINT);
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700490 clearCachedConfigForPackage(null);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700491 sharedPrefs
492 .edit()
493 .putString(KEY_FINGERPRINT, Build.FINGERPRINT)
494 .apply();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700495 }
496 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700497 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700498
499 case EVENT_SUBSCRIPTION_INFO_UPDATED:
500 broadcastConfigChangedIntent(phoneId);
501 break;
Malcolm Chen5acb9cf2019-10-24 19:55:44 -0700502 case EVENT_MULTI_SIM_CONFIG_CHANGED:
503 onMultiSimConfigChanged();
504 break;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800505 }
506 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700507 }
508
509 private final Handler mHandler;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800510
511 /**
512 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
513 * receiver for relevant events.
514 */
515 private CarrierConfigLoader(Context context) {
516 mContext = context;
Meng Wang33ad2bc2017-03-16 20:21:20 -0700517 mPlatformCarrierConfigPackage =
518 mContext.getString(R.string.platform_carrier_config_package);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700519 mHandler = new ConfigHandler();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800520
Nanxi Chen3d670502016-03-17 16:32:09 -0700521 IntentFilter bootFilter = new IntentFilter();
522 bootFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
523 context.registerReceiver(mBootReceiver, bootFilter);
524
Junda Liu8a8a53a2015-05-15 16:19:57 -0700525 // Register for package updates. Update app or uninstall app update will have all 3 intents,
526 // in the order or removed, added, replaced, all with extra_replace set to true.
527 IntentFilter pkgFilter = new IntentFilter();
528 pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
529 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
530 pkgFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
531 pkgFilter.addDataScheme("package");
Jordan Liu5ca24762019-07-10 12:51:09 -0700532 context.registerReceiver(mPackageReceiver, pkgFilter);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800533
Malcolm Chenb39578a2019-10-08 18:15:25 -0700534 int numPhones = TelephonyManager.from(context).getSupportedModemCount();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700535 mConfigFromDefaultApp = new PersistableBundle[numPhones];
536 mConfigFromCarrierApp = new PersistableBundle[numPhones];
Torbjorn Eklundcbd73752019-02-13 11:16:25 +0100537 mPersistentOverrideConfigs = new PersistableBundle[numPhones];
Hall Liu506724b2018-10-22 18:16:14 -0700538 mOverrideConfigs = new PersistableBundle[numPhones];
Zach Johnson36d7aab2015-05-22 15:43:00 -0700539 mServiceConnection = new CarrierServiceConnection[numPhones];
Junda Liu03aad762017-07-21 13:32:17 -0700540 mHasSentConfigChange = new boolean[numPhones];
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800541 // Make this service available through ServiceManager.
Peter Wangc035ce42020-01-08 21:00:22 -0800542 TelephonyFrameworkInitializer
543 .getTelephonyServiceManager().getCarrierConfigServiceRegisterer().register(this);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800544 log("CarrierConfigLoader has started");
Nathan Harold48ac0972019-03-13 22:33:01 -0700545 mSubscriptionInfoUpdater = PhoneFactory.getSubscriptionInfoUpdater();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700546 mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800547 }
548
549 /**
550 * Initialize the singleton CarrierConfigLoader instance.
551 *
552 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
553 */
554 /* package */
555 static CarrierConfigLoader init(Context context) {
556 synchronized (CarrierConfigLoader.class) {
557 if (sInstance == null) {
558 sInstance = new CarrierConfigLoader(context);
559 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700560 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800561 }
562 return sInstance;
563 }
564 }
565
Malcolm Chen5acb9cf2019-10-24 19:55:44 -0700566 private void clearConfigForPhone(int phoneId, boolean sendBroadcast) {
567 /* Ignore clear configuration request if device is being shutdown. */
568 Phone phone = PhoneFactory.getPhone(phoneId);
569 if (phone != null) {
570 if (phone.isShuttingDown()) {
571 return;
572 }
573 }
574
575 mConfigFromDefaultApp[phoneId] = null;
576 mConfigFromCarrierApp[phoneId] = null;
577 mServiceConnection[phoneId] = null;
578 mHasSentConfigChange[phoneId] = false;
579
580 if (sendBroadcast) broadcastConfigChangedIntent(phoneId, false);
581 }
582
Nathan Harold48ac0972019-03-13 22:33:01 -0700583 private void notifySubscriptionInfoUpdater(int phoneId) {
584 String configPackagename;
585 PersistableBundle configToSend;
586 int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
587 // Prefer the carrier privileged carrier app, but if there is not one, use the platform
588 // default carrier app.
589 if (mConfigFromCarrierApp[phoneId] != null) {
590 configPackagename = getCarrierPackageForPhoneId(phoneId);
591 configToSend = mConfigFromCarrierApp[phoneId];
592 } else {
593 configPackagename = mPlatformCarrierConfigPackage;
594 configToSend = mConfigFromDefaultApp[phoneId];
595 }
Nazanin Bakhshic93a1702019-09-18 17:54:01 -0700596
597 // 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 Bakhshic93a1702019-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_INCLUDE_BACKGROUND |
617 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800618 if (addSubIdExtra) {
619 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
620 int[] subIds = SubscriptionManager.getSubId(phoneId);
621 if (!ArrayUtils.isEmpty(subIds)) {
622 TelephonyManager telMgr = TelephonyManager.from(mContext)
623 .createForSubscriptionId(subIds[0]);
624 simApplicationState = telMgr.getSimApplicationState();
625 }
626 // Include subId/carrier id extra only if SIM records are loaded
627 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
628 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
629 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
630 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID,
631 getSpecificCarrierIdForPhoneId(phoneId));
632 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, getCarrierIdForPhoneId(phoneId));
633 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800634 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800635 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Jack Yuf1661322019-04-06 00:37:23 -0700636 log("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
Jordan Liu5ca24762019-07-10 12:51:09 -0700637 mContext.sendBroadcast(intent);
Junda Liu03aad762017-07-21 13:32:17 -0700638 mHasSentConfigChange[phoneId] = true;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800639 }
640
641 /** Binds to the default or carrier config app. */
642 private boolean bindToConfigPackage(String pkgName, int phoneId, int eventId) {
chen xudb04c292019-03-26 17:01:15 -0700643 logWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700644 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700645 carrierService.setPackage(pkgName);
646 mServiceConnection[phoneId] = new CarrierServiceConnection(phoneId, eventId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800647 try {
Zach Johnson36d7aab2015-05-22 15:43:00 -0700648 return mContext.bindService(carrierService, mServiceConnection[phoneId],
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800649 Context.BIND_AUTO_CREATE);
650 } catch (SecurityException ex) {
651 return false;
652 }
653 }
654
chen xu02581692018-11-11 19:03:44 -0800655 private CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800656 String mcc = "";
657 String mnc = "";
658 String imsi = "";
659 String gid1 = "";
660 String gid2 = "";
661 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
662 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800663 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800664 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700665 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
666 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800667 mcc = simOperator.substring(0, 3);
668 mnc = simOperator.substring(3);
669 }
670 Phone phone = PhoneFactory.getPhone(phoneId);
671 if (phone != null) {
672 imsi = phone.getSubscriberId();
673 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700674 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800675 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800676 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800677 }
chen xua31f22b2019-03-06 15:28:50 -0800678 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800679 }
680
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700681 /** Returns the package name of a priveleged carrier app, or null if there is none. */
682 private String getCarrierPackageForPhoneId(int phoneId) {
683 List<String> carrierPackageNames = TelephonyManager.from(mContext)
684 .getCarrierPackageNamesForIntentAndPhone(
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700685 new Intent(CarrierService.CARRIER_SERVICE_INTERFACE), phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700686 if (carrierPackageNames != null && carrierPackageNames.size() > 0) {
687 return carrierPackageNames.get(0);
688 } else {
689 return null;
690 }
691 }
692
693 private String getIccIdForPhoneId(int phoneId) {
694 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
695 return null;
696 }
697 Phone phone = PhoneFactory.getPhone(phoneId);
698 if (phone == null) {
699 return null;
700 }
701 return phone.getIccSerialNumber();
702 }
703
chen xu02581692018-11-11 19:03:44 -0800704 /**
chen xua31f22b2019-03-06 15:28:50 -0800705 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800706 */
chen xua31f22b2019-03-06 15:28:50 -0800707 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700708 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800709 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700710 }
711 Phone phone = PhoneFactory.getPhone(phoneId);
712 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800713 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700714 }
chen xua31f22b2019-03-06 15:28:50 -0800715 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800716 }
717
718 /**
719 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
720 */
721 private int getCarrierIdForPhoneId(int phoneId) {
722 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
723 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700724 }
chen xu02581692018-11-11 19:03:44 -0800725 Phone phone = PhoneFactory.getPhone(phoneId);
726 if (phone == null) {
727 return TelephonyManager.UNKNOWN_CARRIER_ID;
728 }
729 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700730 }
731
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700732 /**
733 * Writes a bundle to an XML file.
734 *
chen xu02581692018-11-11 19:03:44 -0800735 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -0800736 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -0800737 * should have a single copy of XML file named after carrier id. However, it's still possible
738 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
739 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
740 * carrier while iccid remains the same.
741 *
742 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
743 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700744 *
745 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700746 *
747 * @param packageName the name of the package from which we fetched this bundle.
Torbjorn Eklundcbd73752019-02-13 11:16:25 +0100748 * @param extraString An extra string to be used in the XML file name.
Yuchen Dongc15afed2018-04-26 17:05:22 +0800749 * @param phoneId the phone ID.
Chen Xu49b1de32019-07-29 16:34:52 -0700750 * @param carrierId contains all carrier-identifying information.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700751 * @param config the bundle to be written. Null will be treated as an empty bundle.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700752 */
Torbjorn Eklundcbd73752019-02-13 11:16:25 +0100753 private void saveConfigToXml(String packageName, @NonNull String extraString, int phoneId,
754 CarrierIdentifier carrierId, PersistableBundle config) {
Yuchen Dongc15afed2018-04-26 17:05:22 +0800755 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
756 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700757 loge("Skip save config because SIM records are not loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800758 return;
759 }
760
761 final String iccid = getIccIdForPhoneId(phoneId);
Chen Xu49b1de32019-07-29 16:34:52 -0700762 final int cid = carrierId.getSpecificCarrierId();
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700763 if (packageName == null || iccid == null) {
764 loge("Cannot save config with null packageName or iccid.");
765 return;
766 }
Junda Liu02596502016-11-15 13:46:43 -0800767 // b/32668103 Only save to file if config isn't empty.
768 // In case of failure, not caching an empty bundle will
769 // try loading config again on next power on or sim loaded.
770 // Downside is for genuinely empty bundle, will bind and load
771 // on every power on.
772 if (config == null || config.isEmpty()) {
773 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700774 }
775
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700776 final String version = getPackageVersion(packageName);
777 if (version == null) {
778 loge("Failed to get package version for: " + packageName);
779 return;
780 }
781
chen xudb04c292019-03-26 17:01:15 -0700782 logWithLocalLog("save config to xml, packagename: " + packageName + " phoneId: " + phoneId);
783
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700784 FileOutputStream outFile = null;
785 try {
786 outFile = new FileOutputStream(
chen xu02581692018-11-11 19:03:44 -0800787 new File(mContext.getFilesDir(),
Torbjorn Eklundcbd73752019-02-13 11:16:25 +0100788 getFilenameForConfig(packageName, extraString, iccid, cid)));
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800789 config.putString(KEY_VERSION, version);
790 config.writeToStream(outFile);
791 outFile.flush();
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700792 outFile.close();
793 }
794 catch (IOException e) {
795 loge(e.toString());
796 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700797 }
798
799 /**
800 * Reads a bundle from an XML file.
801 *
802 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +0800803 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700804 *
805 * In case of errors, or if the saved config is from a different package version than the
806 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700807 *
808 * @param packageName the name of the package from which we fetched this bundle.
Torbjorn Eklundcbd73752019-02-13 11:16:25 +0100809 * @param extraString An extra string to be used in the XML file name.
Yuchen Dongc15afed2018-04-26 17:05:22 +0800810 * @param phoneId the phone ID.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700811 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
812 * version does not match, or reading config fails.
813 */
Torbjorn Eklundcbd73752019-02-13 11:16:25 +0100814 private PersistableBundle restoreConfigFromXml(String packageName, @NonNull String extraString,
815 int phoneId) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700816 final String version = getPackageVersion(packageName);
817 if (version == null) {
818 loge("Failed to get package version for: " + packageName);
819 return null;
820 }
Yuchen Dongc15afed2018-04-26 17:05:22 +0800821 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
822 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700823 loge("Skip restoring config because SIM records are not yet loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800824 return null;
825 }
826
827 final String iccid = getIccIdForPhoneId(phoneId);
chen xua31f22b2019-03-06 15:28:50 -0800828 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700829 if (packageName == null || iccid == null) {
830 loge("Cannot restore config with null packageName or iccid.");
831 return null;
832 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700833
834 PersistableBundle restoredBundle = null;
Torbjorn Eklundcbd73752019-02-13 11:16:25 +0100835 File file = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700836 FileInputStream inFile = null;
837 try {
Torbjorn Eklundcbd73752019-02-13 11:16:25 +0100838 file = new File(mContext.getFilesDir(),
839 getFilenameForConfig(packageName, extraString, iccid, cid));
840 inFile = new FileInputStream(file);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700841
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800842 restoredBundle = PersistableBundle.readFromStream(inFile);
843 String savedVersion = restoredBundle.getString(KEY_VERSION);
844 restoredBundle.remove(KEY_VERSION);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700845
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800846 if (!version.equals(savedVersion)) {
847 loge("Saved version mismatch: " + version + " vs " + savedVersion);
848 restoredBundle = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700849 }
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800850
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700851 inFile.close();
852 }
853 catch (FileNotFoundException e) {
Torbjorn Eklundcbd73752019-02-13 11:16:25 +0100854 // Missing file is normal occurrence that might occur with a new sim or when restoring
855 // an override file during boot and should not be treated as an error.
856 if (file != null) log("File not found: " + file.getPath());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700857 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700858 catch (IOException e) {
859 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) {
887 log("deleting " + f.getName());
888 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 Eklundcbd73752019-02-13 11:16:25 +0100894 private String getFilenameForConfig(@NonNull String packageName, @NonNull String extraString,
895 @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 Eklundcbd73752019-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 Chen5acb9cf2019-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
938 public @NonNull PersistableBundle getConfigForSubId(int subId, String callingPackage) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -0700939 return getConfigForSubIdWithFeature(subId, callingPackage, null);
940 }
941
942 @Override
943 public @NonNull PersistableBundle getConfigForSubIdWithFeature(int subId, String callingPackage,
944 String callingFeatureId) {
945 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subId, callingPackage,
946 callingFeatureId, "getCarrierConfig")) {
Malcolm Chen6d3d6b32018-03-01 13:58:03 -0800947 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -0700948 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -0700949
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800950 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700951 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800952 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700953 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800954 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700955 retConfig.putAll(config);
yinxu0e3c3dc2019-08-16 14:20:08 -0700956 if (getCarrierPackageForPhoneId(phoneId) == null) {
957 retConfig.putBoolean(
958 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
959 }
yinxuc5e27622017-11-29 15:08:50 -0800960 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800961 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800962 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700963 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -0800964 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
965 }
Torbjorn Eklundcbd73752019-02-13 11:16:25 +0100966 config = mPersistentOverrideConfigs[phoneId];
967 if (config != null) {
968 retConfig.putAll(config);
969 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
970 }
Hall Liu506724b2018-10-22 18:16:14 -0700971 config = mOverrideConfigs[phoneId];
972 if (config != null) {
973 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -0700974 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800975 }
976 return retConfig;
977 }
978
979 @Override
Torbjorn Eklundcbd73752019-02-13 11:16:25 +0100980 public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides,
981 boolean persistent) {
Hall Liu506724b2018-10-22 18:16:14 -0700982 mContext.enforceCallingOrSelfPermission(
983 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Brad Ebingerf6281ad2019-04-25 11:02:04 -0700984 //TODO: Also check for SHELL UID to restrict this method to testing only (b/131326259)
Hall Liu506724b2018-10-22 18:16:14 -0700985 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
986 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
987 log("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
988 return;
989 }
Torbjorn Eklundcbd73752019-02-13 11:16:25 +0100990 overrideConfig(mOverrideConfigs, phoneId, overrides);
Hall Liu506724b2018-10-22 18:16:14 -0700991
Torbjorn Eklundcbd73752019-02-13 11:16:25 +0100992 if (persistent) {
993 overrideConfig(mPersistentOverrideConfigs, phoneId, overrides);
994
995 if (overrides != null) {
996 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
997 saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId,
998 carrierId, mPersistentOverrideConfigs[phoneId]);
999 } else {
1000 final String iccid = getIccIdForPhoneId(phoneId);
1001 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1002 String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage,
1003 OVERRIDE_PACKAGE_ADDITION, iccid, cid);
1004 File fileToDelete = new File(mContext.getFilesDir(), fileName);
1005 fileToDelete.delete();
1006 }
Hall Liu506724b2018-10-22 18:16:14 -07001007 }
Brad Ebingerc9d1fa12019-04-17 15:21:18 -07001008 notifySubscriptionInfoUpdater(phoneId);
Hall Liu506724b2018-10-22 18:16:14 -07001009 }
1010
Torbjorn Eklundcbd73752019-02-13 11:16:25 +01001011 private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId,
1012 @Nullable PersistableBundle overrides) {
1013 if (overrides == null) {
1014 currentOverrides[phoneId] = new PersistableBundle();
1015 } else if (currentOverrides[phoneId] == null) {
1016 currentOverrides[phoneId] = overrides;
1017 } else {
1018 currentOverrides[phoneId].putAll(overrides);
1019 }
1020 }
1021
Hall Liu506724b2018-10-22 18:16:14 -07001022 @Override
Jonathan Basseri86030352015-06-08 12:27:56 -07001023 public void notifyConfigChangedForSubId(int subId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001024 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001025 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001026 log("Ignore invalid phoneId: " + phoneId + " for subId: " + subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001027 return;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001028 }
Jordan Liud7d57fe2019-04-16 12:29:43 -07001029
1030 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001031 // system privileged app with MODIFY_PHONE_STATE permission.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001032 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext, subId,
1033 "Require carrier privileges or MODIFY_PHONE_STATE permission.");
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001034
1035 // This method should block until deleting has completed, so that an error which prevents us
1036 // from clearing the cache is passed back to the carrier app. With the files successfully
1037 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001038 String callingPackageName = mContext.getPackageManager().getNameForUid(
1039 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001040 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001041 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001042 }
1043
1044 @Override
1045 public void updateConfigForPhoneId(int phoneId, String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -07001046 mContext.enforceCallingOrSelfPermission(
1047 android.Manifest.permission.MODIFY_PHONE_STATE, null);
chen xuc4810972019-04-17 23:44:43 -07001048 logWithLocalLog("update config for phoneId: " + phoneId + " simState: " + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001049 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1050 return;
1051 }
1052 // requires Java 7 for switch on string.
1053 switch (simState) {
1054 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1055 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001056 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001057 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Malcolm Chen61a40662019-12-09 18:57:07 -08001058 case IccCardConstants.INTENT_VALUE_ICC_NOT_READY:
Junda Liu9f2d2712015-05-15 14:22:45 -07001059 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001060 break;
1061 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1062 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001063 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001064 break;
1065 }
1066 }
1067
Junda Liu43d723a2015-05-12 17:23:45 -07001068 @Override
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001069 public String getDefaultCarrierServicePackageName() {
Shuo Qianefdb7962019-12-19 15:54:27 -08001070 mContext.enforceCallingOrSelfPermission(
1071 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1072 "getDefaultCarrierServicePackageName");
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001073 return mPlatformCarrierConfigPackage;
1074 }
1075
1076 @Override
Junda Liu43d723a2015-05-12 17:23:45 -07001077 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1078 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1079 != PackageManager.PERMISSION_GRANTED) {
1080 pw.println("Permission Denial: can't dump carrierconfig from from pid="
1081 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1082 return;
1083 }
1084 pw.println("CarrierConfigLoader: " + this);
1085 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
shuoq26a3a4c2016-12-16 11:06:48 -08001086 pw.println("Phone Id = " + i);
1087 // display default values in CarrierConfigManager
1088 printConfig(CarrierConfigManager.getDefaultConfig(), pw,
1089 "Default Values from CarrierConfigManager");
1090 pw.println("");
1091 // display ConfigFromDefaultApp
1092 printConfig(mConfigFromDefaultApp[i], pw, "mConfigFromDefaultApp");
1093 pw.println("");
1094 // display ConfigFromCarrierApp
1095 printConfig(mConfigFromCarrierApp[i], pw, "mConfigFromCarrierApp");
Hall Liu506724b2018-10-22 18:16:14 -07001096 pw.println("");
Torbjorn Eklundcbd73752019-02-13 11:16:25 +01001097 printConfig(mPersistentOverrideConfigs[i], pw, "mPersistentOverrideConfigs");
1098 pw.println("");
Hall Liu506724b2018-10-22 18:16:14 -07001099 printConfig(mOverrideConfigs[i], pw, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001100 }
chen xudb04c292019-03-26 17:01:15 -07001101
1102 pw.println("CarrierConfigLoadingLog=");
1103 mCarrierConfigLoadingLog.dump(fd, pw, args);
shuoq26a3a4c2016-12-16 11:06:48 -08001104 }
1105
1106 private void printConfig(PersistableBundle configApp, PrintWriter pw, String name) {
1107 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
1108 if (configApp == null) {
1109 indentPW.increaseIndent();
1110 indentPW.println(name + " : null ");
1111 return;
1112 }
1113 indentPW.increaseIndent();
1114 indentPW.println(name + " : ");
1115 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1116 Collections.sort(sortedKeys);
1117 indentPW.increaseIndent();
1118 indentPW.increaseIndent();
1119 for (String key : sortedKeys) {
1120 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1121 indentPW.println(key + " = " +
1122 Arrays.toString((Object[]) configApp.get(key)));
1123 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1124 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1125 } else {
1126 indentPW.println(key + " = " + configApp.get(key));
1127 }
Junda Liu43d723a2015-05-12 17:23:45 -07001128 }
1129 }
1130
Zach Johnson36d7aab2015-05-22 15:43:00 -07001131 private class CarrierServiceConnection implements ServiceConnection {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001132 int phoneId;
1133 int eventId;
1134 IBinder service;
1135
Zach Johnson36d7aab2015-05-22 15:43:00 -07001136 public CarrierServiceConnection(int phoneId, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001137 this.phoneId = phoneId;
1138 this.eventId = eventId;
1139 }
1140
1141 @Override
1142 public void onServiceConnected(ComponentName name, IBinder service) {
1143 log("Connected to config app: " + name.flattenToString());
1144 this.service = service;
1145 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1146 }
1147
1148 @Override
1149 public void onServiceDisconnected(ComponentName name) {
1150 this.service = null;
1151 }
1152 }
1153
1154 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1155 @Override
1156 public void onReceive(Context context, Intent intent) {
1157 String action = intent.getAction();
Junda Liu8a8a53a2015-05-15 16:19:57 -07001158 boolean replace = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1159 // If replace is true, only care ACTION_PACKAGE_REPLACED.
1160 if (replace && !Intent.ACTION_PACKAGE_REPLACED.equals(action))
1161 return;
1162
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001163 switch (action) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001164 case Intent.ACTION_BOOT_COMPLETED:
1165 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1166 break;
1167
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001168 case Intent.ACTION_PACKAGE_ADDED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001169 case Intent.ACTION_PACKAGE_REMOVED:
Junda Liu8a8a53a2015-05-15 16:19:57 -07001170 case Intent.ACTION_PACKAGE_REPLACED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001171 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1172 String packageName = mContext.getPackageManager().getNameForUid(uid);
Junda Liu8a8a53a2015-05-15 16:19:57 -07001173 if (packageName != null) {
1174 // We don't have a phoneId for arg1.
1175 mHandler.sendMessage(
1176 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, packageName));
1177 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001178 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001179 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001180 }
1181 }
1182
chen xudb04c292019-03-26 17:01:15 -07001183 private void log(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001184 Log.d(LOG_TAG, msg);
1185 }
1186
chen xudb04c292019-03-26 17:01:15 -07001187 private void logWithLocalLog(String msg) {
1188 Log.d(LOG_TAG, msg);
1189 mCarrierConfigLoadingLog.log(msg);
1190 }
1191
1192 private void loge(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001193 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07001194 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001195 }
1196}