blob: a816c89058b70db896080f9edb72342c1f8705f0 [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 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;
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 Wanga320b942019-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 Eklund723527a2019-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;
Jordan Liu178430d2020-02-10 14:32:15 -080098 // Whether we are bound to a service for each phone
99 private boolean[] mServiceBound;
Junda Liu03aad762017-07-21 13:32:17 -0700100 // Whether we have sent config change bcast for each phone id.
101 private boolean[] mHasSentConfigChange;
Nathan Harold48ac0972019-03-13 22:33:01 -0700102 // SubscriptionInfoUpdater
103 private final SubscriptionInfoUpdater mSubscriptionInfoUpdater;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800104
Nanxi Chen3d670502016-03-17 16:32:09 -0700105 // Broadcast receiver for Boot intents, register intent filter in construtor.
106 private final BroadcastReceiver mBootReceiver = new ConfigLoaderBroadcastReceiver();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800107 // Broadcast receiver for SIM and pkg intents, register intent filter in constructor.
Nanxi Chen3d670502016-03-17 16:32:09 -0700108 private final BroadcastReceiver mPackageReceiver = new ConfigLoaderBroadcastReceiver();
Jack Yu67663de2019-10-17 15:19:48 -0700109 private final LocalLog mCarrierConfigLoadingLog = new LocalLog(100);
chen xudb04c292019-03-26 17:01:15 -0700110
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800111
112 // Message codes; see mHandler below.
113 // Request from SubscriptionInfoUpdater when SIM becomes absent or error.
114 private static final int EVENT_CLEAR_CONFIG = 0;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800115 // Has connected to default app.
116 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
117 // Has connected to carrier app.
118 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700119 // Config has been loaded from default app (or cache).
120 private static final int EVENT_FETCH_DEFAULT_DONE = 5;
121 // Config has been loaded from carrier app (or cache).
122 private static final int EVENT_FETCH_CARRIER_DONE = 6;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700123 // Attempt to fetch from default app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700124 private static final int EVENT_DO_FETCH_DEFAULT = 7;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700125 // Attempt to fetch from carrier app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700126 private static final int EVENT_DO_FETCH_CARRIER = 8;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700127 // A package has been installed, uninstalled, or updated.
128 private static final int EVENT_PACKAGE_CHANGED = 9;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700129 // Bind timed out for the default app.
130 private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
131 // Bind timed out for a carrier app.
132 private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700133 // Check if the system fingerprint has changed.
134 private static final int EVENT_CHECK_SYSTEM_UPDATE = 12;
Nanxi Chen3d670502016-03-17 16:32:09 -0700135 // Rerun carrier config binding after system is unlocked.
136 private static final int EVENT_SYSTEM_UNLOCKED = 13;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700137 // Fetching config timed out from the default app.
138 private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14;
139 // Fetching config timed out from a carrier app.
140 private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15;
Nathan Harold48ac0972019-03-13 22:33:01 -0700141 // SubscriptionInfoUpdater has finished updating the sub for the carrier config.
142 private static final int EVENT_SUBSCRIPTION_INFO_UPDATED = 16;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700143 // Multi-SIM config changed.
144 private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 17;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700145
Junda Liu6cb45002016-03-22 17:18:20 -0700146 private static final int BIND_TIMEOUT_MILLIS = 30000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800147
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800148 // Keys used for saving and restoring config bundle from file.
149 private static final String KEY_VERSION = "__carrier_config_package_version__";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800150
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100151 private static final String OVERRIDE_PACKAGE_ADDITION = "-override";
152
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700153 // SharedPreferences key for last known build fingerprint.
154 private static final String KEY_FINGERPRINT = "build_fingerprint";
155
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800156 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700157 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700158 // For each phoneId, the event sequence should be:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700159 // fetch default, connected to default, fetch default (async), fetch default done,
160 // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700161 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700162 // If there is a saved config file for either the default app or the carrier app, we skip
163 // binding to the app and go straight from fetch to loaded.
164 //
165 // At any time, at most one connection is active. If events are not in this order, previous
166 // connection will be unbound, so only latest event takes effect.
167 //
168 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
169 // 1. loading from carrier app (even if read from a file)
170 // 2. loading from default app if there is no carrier app (even if read from a file)
171 // 3. clearing config (e.g. due to sim removal)
172 // 4. encountering bind or IPC error
Jonathan Basseri65273c82017-07-25 15:08:42 -0700173 private class ConfigHandler extends Handler {
174 @Override
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800175 public void handleMessage(Message msg) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700176 final int phoneId = msg.arg1;
chen xuc4810972019-04-17 23:44:43 -0700177 logWithLocalLog("mHandler: " + msg.what + " phoneId: " + phoneId);
Malcolm Chen5ea18532019-10-24 19:55:44 -0700178 if (!SubscriptionManager.isValidPhoneId(phoneId)
179 && msg.what != EVENT_MULTI_SIM_CONFIG_CHANGED) {
180 return;
181 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800182 switch (msg.what) {
183 case EVENT_CLEAR_CONFIG:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700184 {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700185 clearConfigForPhone(phoneId, true);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800186 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700187 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700188
Nanxi Chen3d670502016-03-17 16:32:09 -0700189 case EVENT_SYSTEM_UNLOCKED:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700190 {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700191 for (int i = 0; i < TelephonyManager.from(mContext).getActiveModemCount();
192 ++i) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700193 // When user unlock device, we should only try to send broadcast again if we
194 // have sent it before unlock. This will avoid we try to load carrier config
195 // when SIM is still loading when unlock happens.
Junda Liu03aad762017-07-21 13:32:17 -0700196 if (mHasSentConfigChange[i]) {
Jack Yu67663de2019-10-17 15:19:48 -0700197 logWithLocalLog("System unlocked");
Junda Liu03aad762017-07-21 13:32:17 -0700198 updateConfigForPhoneId(i);
199 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700200 }
201 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700202 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700203
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700204 case EVENT_PACKAGE_CHANGED:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700205 {
206 final String carrierPackageName = (String) msg.obj;
207 // Only update if there are cached config removed to avoid updating config for
208 // unrelated packages.
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700209 if (clearCachedConfigForPackage(carrierPackageName)) {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700210 int numPhones = TelephonyManager.from(mContext).getActiveModemCount();
Junda Liu8a8a53a2015-05-15 16:19:57 -0700211 for (int i = 0; i < numPhones; ++i) {
Jack Yu67663de2019-10-17 15:19:48 -0700212 logWithLocalLog("Package changed: " + carrierPackageName
213 + ", phone=" + i);
Junda Liu8a8a53a2015-05-15 16:19:57 -0700214 updateConfigForPhoneId(i);
215 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700216 }
217 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700218 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700219
Jonathan Basseri65273c82017-07-25 15:08:42 -0700220 case EVENT_DO_FETCH_DEFAULT:
221 {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100222 // Restore persistent override values.
223 PersistableBundle config = restoreConfigFromXml(
224 mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId);
225 if (config != null) {
226 log("Loaded persistent override config from XML. package="
227 + mPlatformCarrierConfigPackage
228 + " phoneId=" + phoneId);
229 mPersistentOverrideConfigs[phoneId] = config;
230 }
231
232 config = restoreConfigFromXml(mPlatformCarrierConfigPackage, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700233 if (config != null) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700234 log(
235 "Loaded config from XML. package="
236 + mPlatformCarrierConfigPackage
237 + " phoneId="
238 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700239 mConfigFromDefaultApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700240 Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700241 newMsg.getData().putBoolean("loaded_from_xml", true);
242 mHandler.sendMessage(newMsg);
243 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700244 // No cached config, so fetch it from the default app.
245 if (bindToConfigPackage(
246 mPlatformCarrierConfigPackage,
247 phoneId,
248 EVENT_CONNECTED_TO_DEFAULT)) {
249 sendMessageDelayed(
250 obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700251 BIND_TIMEOUT_MILLIS);
252 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700253 // Send broadcast if bind fails.
Nathan Harold48ac0972019-03-13 22:33:01 -0700254 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700255 // TODO: We *must* call unbindService even if bindService returns false.
256 // (And possibly if SecurityException was thrown.)
chen xudb04c292019-03-26 17:01:15 -0700257 loge("binding to default app: "
258 + mPlatformCarrierConfigPackage + " fails");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700259 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800260 }
261 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700262 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800263
264 case EVENT_CONNECTED_TO_DEFAULT:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700265 {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700266 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700267 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800268 // If new service connection has been created, unbind.
269 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Jordan Liu178430d2020-02-10 14:32:15 -0800270 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800271 break;
272 }
chen xu02581692018-11-11 19:03:44 -0800273 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700274 // ResultReceiver callback will execute in this Handler's thread.
275 final ResultReceiver resultReceiver =
276 new ResultReceiver(this) {
277 @Override
278 public void onReceiveResult(int resultCode, Bundle resultData) {
Jordan Liu178430d2020-02-10 14:32:15 -0800279 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700280 // If new service connection has been created, this is stale.
281 if (mServiceConnection[phoneId] != conn) {
282 loge("Received response for stale request.");
283 return;
284 }
285 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
286 if (resultCode == RESULT_ERROR || resultData == null) {
287 // On error, abort config fetching.
288 loge("Failed to get carrier config");
Nathan Harold48ac0972019-03-13 22:33:01 -0700289 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700290 return;
291 }
292 PersistableBundle config =
293 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100294 saveConfigToXml(mPlatformCarrierConfigPackage, "", phoneId,
Chen Xucd4a6372019-07-29 16:34:52 -0700295 carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700296 mConfigFromDefaultApp[phoneId] = config;
297 sendMessage(
298 obtainMessage(
299 EVENT_FETCH_DEFAULT_DONE, phoneId, -1));
300 }
301 };
302 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800303 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700304 ICarrierService carrierService =
305 ICarrierService.Stub.asInterface(conn.service);
306 carrierService.getCarrierConfig(carrierId, resultReceiver);
chen xudb04c292019-03-26 17:01:15 -0700307 logWithLocalLog("fetch config for default app: "
308 + mPlatformCarrierConfigPackage
309 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700310 } catch (RemoteException e) {
chen xudb04c292019-03-26 17:01:15 -0700311 loge("Failed to get carrier config from default app: " +
312 mPlatformCarrierConfigPackage + " err: " + e.toString());
Jordan Liu178430d2020-02-10 14:32:15 -0800313 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700314 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800315 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700316 sendMessageDelayed(
317 obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1),
318 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800319 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700320 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800321
Jonathan Basseri930701e2015-06-11 20:56:43 -0700322 case EVENT_BIND_DEFAULT_TIMEOUT:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700323 case EVENT_FETCH_DEFAULT_TIMEOUT:
324 {
chen xudb04c292019-03-26 17:01:15 -0700325 loge("bind/fetch time out from " + mPlatformCarrierConfigPackage);
chen xuf60b3162019-05-01 21:31:05 -0700326 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
327 // If we attempted to bind to the app, but the service connection is null due to
328 // the race condition that clear config event happens before bind/fetch complete
329 // then config was cleared while we were waiting and we should not continue.
330 if (mServiceConnection[phoneId] != null) {
331 // If a ResponseReceiver callback is in the queue when this happens, we will
332 // unbind twice and throw an exception.
Jordan Liu178430d2020-02-10 14:32:15 -0800333 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700334 broadcastConfigChangedIntent(phoneId);
335 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700336 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700337 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700338 }
Jonathan Basseri930701e2015-06-11 20:56:43 -0700339
Jonathan Basseri65273c82017-07-25 15:08:42 -0700340 case EVENT_FETCH_DEFAULT_DONE:
341 {
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) {
350 log("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
Jonathan Basseri65273c82017-07-25 15:08:42 -0700358 case EVENT_DO_FETCH_CARRIER:
359 {
360 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700361 final PersistableBundle config =
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100362 restoreConfigFromXml(carrierPackageName, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700363 if (config != null) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700364 log(
365 "Loaded config from XML. package="
366 + carrierPackageName
367 + " phoneId="
368 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700369 mConfigFromCarrierApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700370 Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700371 newMsg.getData().putBoolean("loaded_from_xml", true);
372 sendMessage(newMsg);
373 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700374 // No cached config, so fetch it from a carrier app.
Jonathan Basseri40473a52015-08-14 14:36:29 -0700375 if (carrierPackageName != null
Jonathan Basseri65273c82017-07-25 15:08:42 -0700376 && bindToConfigPackage(
377 carrierPackageName,
378 phoneId,
379 EVENT_CONNECTED_TO_CARRIER)) {
380 sendMessageDelayed(
381 obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700382 BIND_TIMEOUT_MILLIS);
383 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700384 // Send broadcast if bind fails.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700385 broadcastConfigChangedIntent(phoneId);
chen xudb04c292019-03-26 17:01:15 -0700386 loge("bind to carrier app: " + carrierPackageName + " fails");
Nathan Harold48ac0972019-03-13 22:33:01 -0700387 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700388 }
389 }
390 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700391 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700392
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800393 case EVENT_CONNECTED_TO_CARRIER:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700394 {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700395 removeMessages(EVENT_BIND_CARRIER_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700396 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800397 // If new service connection has been created, unbind.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700398 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Jordan Liu178430d2020-02-10 14:32:15 -0800399 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800400 break;
401 }
chen xu02581692018-11-11 19:03:44 -0800402 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700403 // ResultReceiver callback will execute in this Handler's thread.
404 final ResultReceiver resultReceiver =
405 new ResultReceiver(this) {
406 @Override
407 public void onReceiveResult(int resultCode, Bundle resultData) {
Jordan Liu178430d2020-02-10 14:32:15 -0800408 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700409 // If new service connection has been created, this is stale.
410 if (mServiceConnection[phoneId] != conn) {
411 loge("Received response for stale request.");
412 return;
413 }
414 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
415 if (resultCode == RESULT_ERROR || resultData == null) {
416 // On error, abort config fetching.
chen xudb04c292019-03-26 17:01:15 -0700417 loge("Failed to get carrier config from carrier app: "
418 + getCarrierPackageForPhoneId(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700419 broadcastConfigChangedIntent(phoneId);
Nathan Harold48ac0972019-03-13 22:33:01 -0700420 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700421 return;
422 }
423 PersistableBundle config =
424 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100425 saveConfigToXml(getCarrierPackageForPhoneId(phoneId), "",
426 phoneId, carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700427 mConfigFromCarrierApp[phoneId] = config;
428 sendMessage(
429 obtainMessage(
430 EVENT_FETCH_CARRIER_DONE, phoneId, -1));
431 }
432 };
433 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800434 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700435 ICarrierService carrierService =
436 ICarrierService.Stub.asInterface(conn.service);
437 carrierService.getCarrierConfig(carrierId, resultReceiver);
chen xudb04c292019-03-26 17:01:15 -0700438 logWithLocalLog("fetch config for carrier app: "
439 + getCarrierPackageForPhoneId(phoneId)
440 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700441 } catch (RemoteException e) {
442 loge("Failed to get carrier config: " + e.toString());
Jordan Liu178430d2020-02-10 14:32:15 -0800443 unbindIfBound(mContext, conn, phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700444 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800445 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700446 sendMessageDelayed(
447 obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1),
448 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800449 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700450 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800451
Jonathan Basseri930701e2015-06-11 20:56:43 -0700452 case EVENT_BIND_CARRIER_TIMEOUT:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700453 case EVENT_FETCH_CARRIER_TIMEOUT:
454 {
chen xudb04c292019-03-26 17:01:15 -0700455 loge("bind/fetch from carrier app timeout");
chen xuf60b3162019-05-01 21:31:05 -0700456 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
457 // If we attempted to bind to the app, but the service connection is null due to
458 // the race condition that clear config event happens before bind/fetch complete
459 // then config was cleared while we were waiting and we should not continue.
460 if (mServiceConnection[phoneId] != null) {
461 // If a ResponseReceiver callback is in the queue when this happens, we will
462 // unbind twice and throw an exception.
Jordan Liu178430d2020-02-10 14:32:15 -0800463 unbindIfBound(mContext, mServiceConnection[phoneId], phoneId);
chen xuf60b3162019-05-01 21:31:05 -0700464 broadcastConfigChangedIntent(phoneId);
465 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700466 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700467 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700468 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700469 case EVENT_FETCH_CARRIER_DONE:
470 {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700471 // If we attempted to bind to the app, but the service connection is null, then
472 // config was cleared while we were waiting and we should not continue.
473 if (!msg.getData().getBoolean("loaded_from_xml", false)
474 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800475 break;
476 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700477 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800478 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700479 }
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700480
481 case EVENT_CHECK_SYSTEM_UPDATE:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700482 {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700483 SharedPreferences sharedPrefs =
484 PreferenceManager.getDefaultSharedPreferences(mContext);
485 final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
486 if (!Build.FINGERPRINT.equals(lastFingerprint)) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700487 log(
488 "Build fingerprint changed. old: "
489 + lastFingerprint
490 + " new: "
491 + Build.FINGERPRINT);
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700492 clearCachedConfigForPackage(null);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700493 sharedPrefs
494 .edit()
495 .putString(KEY_FINGERPRINT, Build.FINGERPRINT)
496 .apply();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700497 }
498 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700499 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700500
501 case EVENT_SUBSCRIPTION_INFO_UPDATED:
502 broadcastConfigChangedIntent(phoneId);
503 break;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700504 case EVENT_MULTI_SIM_CONFIG_CHANGED:
505 onMultiSimConfigChanged();
506 break;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800507 }
508 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700509 }
510
511 private final Handler mHandler;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800512
513 /**
514 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
515 * receiver for relevant events.
516 */
517 private CarrierConfigLoader(Context context) {
518 mContext = context;
Meng Wang33ad2bc2017-03-16 20:21:20 -0700519 mPlatformCarrierConfigPackage =
520 mContext.getString(R.string.platform_carrier_config_package);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700521 mHandler = new ConfigHandler();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800522
Nanxi Chen3d670502016-03-17 16:32:09 -0700523 IntentFilter bootFilter = new IntentFilter();
524 bootFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
525 context.registerReceiver(mBootReceiver, bootFilter);
526
Junda Liu8a8a53a2015-05-15 16:19:57 -0700527 // Register for package updates. Update app or uninstall app update will have all 3 intents,
528 // in the order or removed, added, replaced, all with extra_replace set to true.
529 IntentFilter pkgFilter = new IntentFilter();
530 pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
531 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
532 pkgFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
533 pkgFilter.addDataScheme("package");
Jordan Liu5ca24762019-07-10 12:51:09 -0700534 context.registerReceiver(mPackageReceiver, pkgFilter);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800535
Malcolm Chen978dda52019-10-08 18:15:25 -0700536 int numPhones = TelephonyManager.from(context).getSupportedModemCount();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700537 mConfigFromDefaultApp = new PersistableBundle[numPhones];
538 mConfigFromCarrierApp = new PersistableBundle[numPhones];
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100539 mPersistentOverrideConfigs = new PersistableBundle[numPhones];
Hall Liu506724b2018-10-22 18:16:14 -0700540 mOverrideConfigs = new PersistableBundle[numPhones];
Zach Johnson36d7aab2015-05-22 15:43:00 -0700541 mServiceConnection = new CarrierServiceConnection[numPhones];
Jordan Liu178430d2020-02-10 14:32:15 -0800542 mServiceBound = new boolean[numPhones];
Junda Liu03aad762017-07-21 13:32:17 -0700543 mHasSentConfigChange = new boolean[numPhones];
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800544 // Make this service available through ServiceManager.
545 ServiceManager.addService(Context.CARRIER_CONFIG_SERVICE, this);
546 log("CarrierConfigLoader has started");
Nathan Harold48ac0972019-03-13 22:33:01 -0700547 mSubscriptionInfoUpdater = PhoneFactory.getSubscriptionInfoUpdater();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700548 mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800549 }
550
551 /**
552 * Initialize the singleton CarrierConfigLoader instance.
553 *
554 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
555 */
556 /* package */
557 static CarrierConfigLoader init(Context context) {
558 synchronized (CarrierConfigLoader.class) {
559 if (sInstance == null) {
560 sInstance = new CarrierConfigLoader(context);
561 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700562 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800563 }
564 return sInstance;
565 }
566 }
567
Malcolm Chen5ea18532019-10-24 19:55:44 -0700568 private void clearConfigForPhone(int phoneId, boolean sendBroadcast) {
569 /* Ignore clear configuration request if device is being shutdown. */
570 Phone phone = PhoneFactory.getPhone(phoneId);
571 if (phone != null) {
572 if (phone.isShuttingDown()) {
573 return;
574 }
575 }
576
577 mConfigFromDefaultApp[phoneId] = null;
578 mConfigFromCarrierApp[phoneId] = null;
579 mServiceConnection[phoneId] = null;
580 mHasSentConfigChange[phoneId] = false;
581
582 if (sendBroadcast) broadcastConfigChangedIntent(phoneId, false);
583 }
584
Nathan Harold48ac0972019-03-13 22:33:01 -0700585 private void notifySubscriptionInfoUpdater(int phoneId) {
586 String configPackagename;
587 PersistableBundle configToSend;
588 int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
589 // Prefer the carrier privileged carrier app, but if there is not one, use the platform
590 // default carrier app.
591 if (mConfigFromCarrierApp[phoneId] != null) {
592 configPackagename = getCarrierPackageForPhoneId(phoneId);
593 configToSend = mConfigFromCarrierApp[phoneId];
594 } else {
595 configPackagename = mPlatformCarrierConfigPackage;
596 configToSend = mConfigFromDefaultApp[phoneId];
597 }
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700598
Malcolm Chenefafd1c2020-02-20 13:27:08 -0800599 if (configToSend == null) {
600 configToSend = new PersistableBundle();
601 }
602
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700603 // mOverrideConfigs is for testing. And it will override current configs.
604 PersistableBundle config = mOverrideConfigs[phoneId];
605 if (config != null) {
Torbjorn Eklund1050cb02018-11-16 14:05:38 +0100606 configToSend = new PersistableBundle(configToSend);
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700607 configToSend.putAll(config);
608 }
609
Nathan Harold48ac0972019-03-13 22:33:01 -0700610 mSubscriptionInfoUpdater.updateSubscriptionByCarrierConfigAndNotifyComplete(
611 phoneId, configPackagename, configToSend,
612 mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1));
613 }
614
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800615 private void broadcastConfigChangedIntent(int phoneId) {
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800616 broadcastConfigChangedIntent(phoneId, true);
617 }
618
619 private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800620 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800621 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
chen xuf9db49f2019-03-31 23:04:42 -0700622 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800623 if (addSubIdExtra) {
624 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
625 int[] subIds = SubscriptionManager.getSubId(phoneId);
626 if (!ArrayUtils.isEmpty(subIds)) {
627 TelephonyManager telMgr = TelephonyManager.from(mContext)
628 .createForSubscriptionId(subIds[0]);
629 simApplicationState = telMgr.getSimApplicationState();
630 }
631 // Include subId/carrier id extra only if SIM records are loaded
632 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
633 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
634 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
635 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID,
636 getSpecificCarrierIdForPhoneId(phoneId));
637 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, getCarrierIdForPhoneId(phoneId));
638 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800639 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800640 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Jack Yuf1661322019-04-06 00:37:23 -0700641 log("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
Jordan Liu5ca24762019-07-10 12:51:09 -0700642 mContext.sendBroadcast(intent);
Junda Liu03aad762017-07-21 13:32:17 -0700643 mHasSentConfigChange[phoneId] = true;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800644 }
645
646 /** Binds to the default or carrier config app. */
647 private boolean bindToConfigPackage(String pkgName, int phoneId, int eventId) {
chen xudb04c292019-03-26 17:01:15 -0700648 logWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700649 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700650 carrierService.setPackage(pkgName);
651 mServiceConnection[phoneId] = new CarrierServiceConnection(phoneId, eventId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800652 try {
Jordan Liu178430d2020-02-10 14:32:15 -0800653 if (mContext.bindService(carrierService, mServiceConnection[phoneId],
654 Context.BIND_AUTO_CREATE)) {
655 mServiceBound[phoneId] = true;
656 return true;
657 } else {
658 return false;
659 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800660 } catch (SecurityException ex) {
661 return false;
662 }
663 }
664
chen xu02581692018-11-11 19:03:44 -0800665 private CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800666 String mcc = "";
667 String mnc = "";
668 String imsi = "";
669 String gid1 = "";
670 String gid2 = "";
671 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
672 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800673 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800674 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700675 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
676 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800677 mcc = simOperator.substring(0, 3);
678 mnc = simOperator.substring(3);
679 }
680 Phone phone = PhoneFactory.getPhone(phoneId);
681 if (phone != null) {
682 imsi = phone.getSubscriberId();
683 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700684 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800685 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800686 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800687 }
chen xua31f22b2019-03-06 15:28:50 -0800688 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800689 }
690
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700691 /** Returns the package name of a priveleged carrier app, or null if there is none. */
692 private String getCarrierPackageForPhoneId(int phoneId) {
693 List<String> carrierPackageNames = TelephonyManager.from(mContext)
694 .getCarrierPackageNamesForIntentAndPhone(
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700695 new Intent(CarrierService.CARRIER_SERVICE_INTERFACE), phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700696 if (carrierPackageNames != null && carrierPackageNames.size() > 0) {
697 return carrierPackageNames.get(0);
698 } else {
699 return null;
700 }
701 }
702
703 private String getIccIdForPhoneId(int phoneId) {
704 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
705 return null;
706 }
707 Phone phone = PhoneFactory.getPhone(phoneId);
708 if (phone == null) {
709 return null;
710 }
711 return phone.getIccSerialNumber();
712 }
713
chen xu02581692018-11-11 19:03:44 -0800714 /**
chen xua31f22b2019-03-06 15:28:50 -0800715 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800716 */
chen xua31f22b2019-03-06 15:28:50 -0800717 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700718 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800719 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700720 }
721 Phone phone = PhoneFactory.getPhone(phoneId);
722 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800723 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700724 }
chen xua31f22b2019-03-06 15:28:50 -0800725 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800726 }
727
728 /**
729 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
730 */
731 private int getCarrierIdForPhoneId(int phoneId) {
732 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
733 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700734 }
chen xu02581692018-11-11 19:03:44 -0800735 Phone phone = PhoneFactory.getPhone(phoneId);
736 if (phone == null) {
737 return TelephonyManager.UNKNOWN_CARRIER_ID;
738 }
739 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700740 }
741
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700742 /**
743 * Writes a bundle to an XML file.
744 *
chen xu02581692018-11-11 19:03:44 -0800745 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -0800746 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -0800747 * should have a single copy of XML file named after carrier id. However, it's still possible
748 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
749 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
750 * carrier while iccid remains the same.
751 *
752 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
753 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700754 *
755 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700756 *
757 * @param packageName the name of the package from which we fetched this bundle.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100758 * @param extraString An extra string to be used in the XML file name.
Yuchen Dongc15afed2018-04-26 17:05:22 +0800759 * @param phoneId the phone ID.
Chen Xucd4a6372019-07-29 16:34:52 -0700760 * @param carrierId contains all carrier-identifying information.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700761 * @param config the bundle to be written. Null will be treated as an empty bundle.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700762 */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100763 private void saveConfigToXml(String packageName, @NonNull String extraString, int phoneId,
764 CarrierIdentifier carrierId, PersistableBundle config) {
Yuchen Dongc15afed2018-04-26 17:05:22 +0800765 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
766 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700767 loge("Skip save config because SIM records are not loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800768 return;
769 }
770
771 final String iccid = getIccIdForPhoneId(phoneId);
Chen Xucd4a6372019-07-29 16:34:52 -0700772 final int cid = carrierId.getSpecificCarrierId();
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700773 if (packageName == null || iccid == null) {
774 loge("Cannot save config with null packageName or iccid.");
775 return;
776 }
Junda Liu02596502016-11-15 13:46:43 -0800777 // b/32668103 Only save to file if config isn't empty.
778 // In case of failure, not caching an empty bundle will
779 // try loading config again on next power on or sim loaded.
780 // Downside is for genuinely empty bundle, will bind and load
781 // on every power on.
782 if (config == null || config.isEmpty()) {
783 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700784 }
785
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700786 final String version = getPackageVersion(packageName);
787 if (version == null) {
788 loge("Failed to get package version for: " + packageName);
789 return;
790 }
791
chen xudb04c292019-03-26 17:01:15 -0700792 logWithLocalLog("save config to xml, packagename: " + packageName + " phoneId: " + phoneId);
793
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700794 FileOutputStream outFile = null;
795 try {
796 outFile = new FileOutputStream(
chen xu02581692018-11-11 19:03:44 -0800797 new File(mContext.getFilesDir(),
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100798 getFilenameForConfig(packageName, extraString, iccid, cid)));
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800799 config.putString(KEY_VERSION, version);
800 config.writeToStream(outFile);
801 outFile.flush();
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700802 outFile.close();
803 }
804 catch (IOException e) {
805 loge(e.toString());
806 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700807 }
808
809 /**
810 * Reads a bundle from an XML file.
811 *
812 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +0800813 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700814 *
815 * In case of errors, or if the saved config is from a different package version than the
816 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700817 *
818 * @param packageName the name of the package from which we fetched this bundle.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100819 * @param extraString An extra string to be used in the XML file name.
Yuchen Dongc15afed2018-04-26 17:05:22 +0800820 * @param phoneId the phone ID.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700821 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
822 * version does not match, or reading config fails.
823 */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100824 private PersistableBundle restoreConfigFromXml(String packageName, @NonNull String extraString,
825 int phoneId) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700826 final String version = getPackageVersion(packageName);
827 if (version == null) {
828 loge("Failed to get package version for: " + packageName);
829 return null;
830 }
Yuchen Dongc15afed2018-04-26 17:05:22 +0800831 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
832 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700833 loge("Skip restoring config because SIM records are not yet loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800834 return null;
835 }
836
837 final String iccid = getIccIdForPhoneId(phoneId);
chen xua31f22b2019-03-06 15:28:50 -0800838 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700839 if (packageName == null || iccid == null) {
840 loge("Cannot restore config with null packageName or iccid.");
841 return null;
842 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700843
844 PersistableBundle restoredBundle = null;
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100845 File file = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700846 FileInputStream inFile = null;
847 try {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100848 file = new File(mContext.getFilesDir(),
849 getFilenameForConfig(packageName, extraString, iccid, cid));
850 inFile = new FileInputStream(file);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700851
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800852 restoredBundle = PersistableBundle.readFromStream(inFile);
853 String savedVersion = restoredBundle.getString(KEY_VERSION);
854 restoredBundle.remove(KEY_VERSION);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700855
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800856 if (!version.equals(savedVersion)) {
857 loge("Saved version mismatch: " + version + " vs " + savedVersion);
858 restoredBundle = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700859 }
Meng Wang0f6b2ea2020-01-06 18:31:16 -0800860
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700861 inFile.close();
862 }
863 catch (FileNotFoundException e) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100864 // Missing file is normal occurrence that might occur with a new sim or when restoring
865 // an override file during boot and should not be treated as an error.
866 if (file != null) log("File not found: " + file.getPath());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700867 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700868 catch (IOException e) {
869 loge(e.toString());
870 }
871
872 return restoredBundle;
873 }
874
Junda Liu8a8a53a2015-05-15 16:19:57 -0700875 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700876 * Clears cached carrier config.
877 * This deletes all saved XML files associated with the given package name. If packageName is
878 * null, then it deletes all saved XML files.
879 *
880 * @param packageName the name of a carrier package, or null if all cached config should be
881 * cleared.
882 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -0700883 */
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700884 private boolean clearCachedConfigForPackage(final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700885 File dir = mContext.getFilesDir();
886 File[] packageFiles = dir.listFiles(new FilenameFilter() {
887 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700888 if (packageName != null) {
889 return filename.startsWith("carrierconfig-" + packageName + "-");
890 } else {
891 return filename.startsWith("carrierconfig-");
892 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700893 }
894 });
Junda Liu8a8a53a2015-05-15 16:19:57 -0700895 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700896 for (File f : packageFiles) {
897 log("deleting " + f.getName());
898 f.delete();
899 }
Junda Liu8a8a53a2015-05-15 16:19:57 -0700900 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700901 }
902
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700903 /** Builds a canonical file name for a config file. */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100904 private String getFilenameForConfig(@NonNull String packageName, @NonNull String extraString,
905 @NonNull String iccid, int cid) {
chen xu02581692018-11-11 19:03:44 -0800906 // the same carrier should have a single copy of XML file named after carrier id.
907 // However, it's still possible that platform doesn't recognize the current sim carrier,
908 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
909 // cases SIM OTA resolves to different carrier while iccid remains the same.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100910 return "carrierconfig-" + packageName + extraString + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700911 }
912
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700913 /** Return the current version code of a package, or null if the name is not found. */
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700914 private String getPackageVersion(String packageName) {
915 try {
916 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -0800917 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700918 } catch (PackageManager.NameNotFoundException e) {
919 return null;
920 }
921 }
922
Jonathan Basseri65273c82017-07-25 15:08:42 -0700923 /**
924 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700925 *
926 * This reads config bundles for the given phoneId. That means getting the latest bundle from
927 * the default app and a privileged carrier app, if present. This will not bind to an app if we
928 * have a saved config file to use instead.
929 */
930 private void updateConfigForPhoneId(int phoneId) {
Junda Liu8a8a53a2015-05-15 16:19:57 -0700931 // Clear in-memory cache for carrier app config, so when carrier app gets uninstalled, no
932 // stale config is left.
933 if (mConfigFromCarrierApp[phoneId] != null &&
934 getCarrierPackageForPhoneId(phoneId) == null) {
935 mConfigFromCarrierApp[phoneId] = null;
936 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700937 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700938 }
939
Malcolm Chen5ea18532019-10-24 19:55:44 -0700940 private void onMultiSimConfigChanged() {
941 for (int i = TelephonyManager.from(mContext).getActiveModemCount();
942 i < mConfigFromDefaultApp.length; i++) {
943 clearConfigForPhone(i, false);
944 }
945 }
946
Hall Liu506724b2018-10-22 18:16:14 -0700947 @Override
948 public @NonNull PersistableBundle getConfigForSubId(int subId, String callingPackage) {
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -0800949 return getConfigForSubIdWithFeature(subId, callingPackage, null);
950 }
951
952 @Override
953 public @NonNull PersistableBundle getConfigForSubIdWithFeature(int subId, String callingPackage,
954 String callingFeatureId) {
955 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subId, callingPackage,
956 callingFeatureId, "getCarrierConfig")) {
Malcolm Chen6d3d6b32018-03-01 13:58:03 -0800957 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -0700958 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -0700959
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800960 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700961 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800962 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700963 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800964 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700965 retConfig.putAll(config);
yinxud0104832019-08-16 14:20:08 -0700966 if (getCarrierPackageForPhoneId(phoneId) == null) {
967 retConfig.putBoolean(
968 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
969 }
yinxuc5e27622017-11-29 15:08:50 -0800970 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800971 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800972 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700973 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -0800974 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
975 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100976 config = mPersistentOverrideConfigs[phoneId];
977 if (config != null) {
978 retConfig.putAll(config);
979 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
980 }
Hall Liu506724b2018-10-22 18:16:14 -0700981 config = mOverrideConfigs[phoneId];
982 if (config != null) {
983 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -0700984 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800985 }
986 return retConfig;
987 }
988
989 @Override
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100990 public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides,
991 boolean persistent) {
Hall Liu506724b2018-10-22 18:16:14 -0700992 mContext.enforceCallingOrSelfPermission(
993 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Brad Ebingerf6281ad2019-04-25 11:02:04 -0700994 //TODO: Also check for SHELL UID to restrict this method to testing only (b/131326259)
Hall Liu506724b2018-10-22 18:16:14 -0700995 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
996 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
997 log("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
998 return;
999 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001000 overrideConfig(mOverrideConfigs, phoneId, overrides);
Hall Liu506724b2018-10-22 18:16:14 -07001001
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001002 if (persistent) {
1003 overrideConfig(mPersistentOverrideConfigs, phoneId, overrides);
1004
1005 if (overrides != null) {
1006 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
1007 saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId,
1008 carrierId, mPersistentOverrideConfigs[phoneId]);
1009 } else {
1010 final String iccid = getIccIdForPhoneId(phoneId);
1011 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1012 String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage,
1013 OVERRIDE_PACKAGE_ADDITION, iccid, cid);
1014 File fileToDelete = new File(mContext.getFilesDir(), fileName);
1015 fileToDelete.delete();
1016 }
Hall Liu506724b2018-10-22 18:16:14 -07001017 }
Brad Ebingerc9d1fa12019-04-17 15:21:18 -07001018 notifySubscriptionInfoUpdater(phoneId);
Hall Liu506724b2018-10-22 18:16:14 -07001019 }
1020
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001021 private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId,
1022 @Nullable PersistableBundle overrides) {
1023 if (overrides == null) {
1024 currentOverrides[phoneId] = new PersistableBundle();
1025 } else if (currentOverrides[phoneId] == null) {
1026 currentOverrides[phoneId] = overrides;
1027 } else {
1028 currentOverrides[phoneId].putAll(overrides);
1029 }
1030 }
1031
Hall Liu506724b2018-10-22 18:16:14 -07001032 @Override
Jonathan Basseri86030352015-06-08 12:27:56 -07001033 public void notifyConfigChangedForSubId(int subId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001034 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001035 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001036 log("Ignore invalid phoneId: " + phoneId + " for subId: " + subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001037 return;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001038 }
Jordan Liud7d57fe2019-04-16 12:29:43 -07001039
1040 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001041 // system privileged app with MODIFY_PHONE_STATE permission.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001042 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext, subId,
1043 "Require carrier privileges or MODIFY_PHONE_STATE permission.");
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001044
1045 // This method should block until deleting has completed, so that an error which prevents us
1046 // from clearing the cache is passed back to the carrier app. With the files successfully
1047 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001048 String callingPackageName = mContext.getPackageManager().getNameForUid(
1049 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001050 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001051 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001052 }
1053
1054 @Override
1055 public void updateConfigForPhoneId(int phoneId, String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -07001056 mContext.enforceCallingOrSelfPermission(
1057 android.Manifest.permission.MODIFY_PHONE_STATE, null);
chen xuc4810972019-04-17 23:44:43 -07001058 logWithLocalLog("update config for phoneId: " + phoneId + " simState: " + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001059 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1060 return;
1061 }
1062 // requires Java 7 for switch on string.
1063 switch (simState) {
1064 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1065 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001066 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001067 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Malcolm Chendb66de12019-12-09 18:57:07 -08001068 case IccCardConstants.INTENT_VALUE_ICC_NOT_READY:
Junda Liu9f2d2712015-05-15 14:22:45 -07001069 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001070 break;
1071 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1072 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001073 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001074 break;
1075 }
1076 }
1077
Junda Liu43d723a2015-05-12 17:23:45 -07001078 @Override
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001079 public String getDefaultCarrierServicePackageName() {
Shuo Qianefdb7962019-12-19 15:54:27 -08001080 mContext.enforceCallingOrSelfPermission(
1081 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1082 "getDefaultCarrierServicePackageName");
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001083 return mPlatformCarrierConfigPackage;
1084 }
1085
1086 @Override
Junda Liu43d723a2015-05-12 17:23:45 -07001087 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1088 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1089 != PackageManager.PERMISSION_GRANTED) {
1090 pw.println("Permission Denial: can't dump carrierconfig from from pid="
1091 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1092 return;
1093 }
1094 pw.println("CarrierConfigLoader: " + this);
1095 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
shuoq26a3a4c2016-12-16 11:06:48 -08001096 pw.println("Phone Id = " + i);
1097 // display default values in CarrierConfigManager
1098 printConfig(CarrierConfigManager.getDefaultConfig(), pw,
1099 "Default Values from CarrierConfigManager");
1100 pw.println("");
1101 // display ConfigFromDefaultApp
1102 printConfig(mConfigFromDefaultApp[i], pw, "mConfigFromDefaultApp");
1103 pw.println("");
1104 // display ConfigFromCarrierApp
1105 printConfig(mConfigFromCarrierApp[i], pw, "mConfigFromCarrierApp");
Hall Liu506724b2018-10-22 18:16:14 -07001106 pw.println("");
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001107 printConfig(mPersistentOverrideConfigs[i], pw, "mPersistentOverrideConfigs");
1108 pw.println("");
Hall Liu506724b2018-10-22 18:16:14 -07001109 printConfig(mOverrideConfigs[i], pw, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001110 }
chen xudb04c292019-03-26 17:01:15 -07001111
1112 pw.println("CarrierConfigLoadingLog=");
1113 mCarrierConfigLoadingLog.dump(fd, pw, args);
shuoq26a3a4c2016-12-16 11:06:48 -08001114 }
1115
1116 private void printConfig(PersistableBundle configApp, PrintWriter pw, String name) {
1117 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
1118 if (configApp == null) {
1119 indentPW.increaseIndent();
1120 indentPW.println(name + " : null ");
1121 return;
1122 }
1123 indentPW.increaseIndent();
1124 indentPW.println(name + " : ");
1125 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1126 Collections.sort(sortedKeys);
1127 indentPW.increaseIndent();
1128 indentPW.increaseIndent();
1129 for (String key : sortedKeys) {
1130 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1131 indentPW.println(key + " = " +
1132 Arrays.toString((Object[]) configApp.get(key)));
1133 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1134 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1135 } else {
1136 indentPW.println(key + " = " + configApp.get(key));
1137 }
Junda Liu43d723a2015-05-12 17:23:45 -07001138 }
1139 }
1140
Jordan Liu178430d2020-02-10 14:32:15 -08001141 private void unbindIfBound(Context context, CarrierServiceConnection conn,
1142 int phoneId) {
1143 if (mServiceBound[phoneId]) {
1144 mServiceBound[phoneId] = false;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001145 context.unbindService(conn);
1146 }
1147 }
1148
Zach Johnson36d7aab2015-05-22 15:43:00 -07001149 private class CarrierServiceConnection implements ServiceConnection {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001150 int phoneId;
1151 int eventId;
1152 IBinder service;
1153
Zach Johnson36d7aab2015-05-22 15:43:00 -07001154 public CarrierServiceConnection(int phoneId, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001155 this.phoneId = phoneId;
1156 this.eventId = eventId;
1157 }
1158
1159 @Override
1160 public void onServiceConnected(ComponentName name, IBinder service) {
1161 log("Connected to config app: " + name.flattenToString());
1162 this.service = service;
1163 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1164 }
1165
1166 @Override
1167 public void onServiceDisconnected(ComponentName name) {
Jordan Liu46ed5db2020-01-28 08:55:20 -08001168 log("Disconnected from config app: " + name.flattenToString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001169 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001170 }
1171
1172 @Override
1173 public void onBindingDied(ComponentName name) {
1174 log("Binding died from config app: " + name.flattenToString());
1175 this.service = null;
Jordan Liu46ed5db2020-01-28 08:55:20 -08001176 }
1177
1178 @Override
1179 public void onNullBinding(ComponentName name) {
1180 log("Null binding from config app: " + name.flattenToString());
1181 this.service = null;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001182 }
1183 }
1184
1185 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1186 @Override
1187 public void onReceive(Context context, Intent intent) {
1188 String action = intent.getAction();
Junda Liu8a8a53a2015-05-15 16:19:57 -07001189 boolean replace = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1190 // If replace is true, only care ACTION_PACKAGE_REPLACED.
1191 if (replace && !Intent.ACTION_PACKAGE_REPLACED.equals(action))
1192 return;
1193
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001194 switch (action) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001195 case Intent.ACTION_BOOT_COMPLETED:
1196 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1197 break;
1198
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001199 case Intent.ACTION_PACKAGE_ADDED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001200 case Intent.ACTION_PACKAGE_REMOVED:
Junda Liu8a8a53a2015-05-15 16:19:57 -07001201 case Intent.ACTION_PACKAGE_REPLACED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001202 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1203 String packageName = mContext.getPackageManager().getNameForUid(uid);
Junda Liu8a8a53a2015-05-15 16:19:57 -07001204 if (packageName != null) {
1205 // We don't have a phoneId for arg1.
1206 mHandler.sendMessage(
1207 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, packageName));
1208 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001209 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001210 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001211 }
1212 }
1213
chen xudb04c292019-03-26 17:01:15 -07001214 private void log(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001215 Log.d(LOG_TAG, msg);
1216 }
1217
chen xudb04c292019-03-26 17:01:15 -07001218 private void logWithLocalLog(String msg) {
1219 Log.d(LOG_TAG, msg);
1220 mCarrierConfigLoadingLog.log(msg);
1221 }
1222
1223 private void loge(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001224 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07001225 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001226 }
1227}