blob: d213fd9f33efebb52ab8c220c32bb9c2613380d6 [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;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070060import com.android.internal.util.FastXmlSerializer;
shuoq26a3a4c2016-12-16 11:06:48 -080061import com.android.internal.util.IndentingPrintWriter;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080062
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070063import org.xmlpull.v1.XmlPullParser;
64import org.xmlpull.v1.XmlPullParserException;
65import org.xmlpull.v1.XmlPullParserFactory;
66
67import java.io.File;
Junda Liu43d723a2015-05-12 17:23:45 -070068import java.io.FileDescriptor;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070069import java.io.FileInputStream;
70import java.io.FileNotFoundException;
71import java.io.FileOutputStream;
Jonathan Basseri1f743c92015-05-15 00:19:46 -070072import java.io.FilenameFilter;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070073import java.io.IOException;
Junda Liu43d723a2015-05-12 17:23:45 -070074import java.io.PrintWriter;
shuoq26a3a4c2016-12-16 11:06:48 -080075import java.util.ArrayList;
76import java.util.Arrays;
77import java.util.Collections;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080078import java.util.List;
79
80/**
81 * CarrierConfigLoader binds to privileged carrier apps to fetch carrier config overlays.
Jonathan Basseri6465afd2015-02-25 13:05:57 -080082 */
83
84public class CarrierConfigLoader extends ICarrierConfigLoader.Stub {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070085 private static final String LOG_TAG = "CarrierConfigLoader";
Meng Wang33ad2bc2017-03-16 20:21:20 -070086
87 // Package name for platform carrier config app, bundled with system image.
88 private final String mPlatformCarrierConfigPackage;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080089
90 /** The singleton instance. */
91 private static CarrierConfigLoader sInstance;
92 // The context for phone app, passed from PhoneGlobals.
93 private Context mContext;
94 // Carrier configs from default app, indexed by phoneID.
Jonathan Basseric31f1f32015-05-12 10:13:03 -070095 private PersistableBundle[] mConfigFromDefaultApp;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080096 // Carrier configs from privileged carrier config app, indexed by phoneID.
Jonathan Basseric31f1f32015-05-12 10:13:03 -070097 private PersistableBundle[] mConfigFromCarrierApp;
Torbjorn Eklund723527a2019-02-13 11:16:25 +010098 // Persistent Carrier configs that are provided via the override test API, indexed by phone ID.
99 private PersistableBundle[] mPersistentOverrideConfigs;
Hall Liu506724b2018-10-22 18:16:14 -0700100 // Carrier configs that are provided via the override test API, indexed by phone ID.
101 private PersistableBundle[] mOverrideConfigs;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800102 // Service connection for binding to config app.
Zach Johnson36d7aab2015-05-22 15:43:00 -0700103 private CarrierServiceConnection[] mServiceConnection;
Junda Liu03aad762017-07-21 13:32:17 -0700104 // Whether we have sent config change bcast for each phone id.
105 private boolean[] mHasSentConfigChange;
Nathan Harold48ac0972019-03-13 22:33:01 -0700106 // SubscriptionInfoUpdater
107 private final SubscriptionInfoUpdater mSubscriptionInfoUpdater;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800108
Nanxi Chen3d670502016-03-17 16:32:09 -0700109 // Broadcast receiver for Boot intents, register intent filter in construtor.
110 private final BroadcastReceiver mBootReceiver = new ConfigLoaderBroadcastReceiver();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800111 // Broadcast receiver for SIM and pkg intents, register intent filter in constructor.
Nanxi Chen3d670502016-03-17 16:32:09 -0700112 private final BroadcastReceiver mPackageReceiver = new ConfigLoaderBroadcastReceiver();
Jack Yu67663de2019-10-17 15:19:48 -0700113 private final LocalLog mCarrierConfigLoadingLog = new LocalLog(100);
chen xudb04c292019-03-26 17:01:15 -0700114
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800115
116 // Message codes; see mHandler below.
117 // Request from SubscriptionInfoUpdater when SIM becomes absent or error.
118 private static final int EVENT_CLEAR_CONFIG = 0;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800119 // Has connected to default app.
120 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
121 // Has connected to carrier app.
122 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700123 // Config has been loaded from default app (or cache).
124 private static final int EVENT_FETCH_DEFAULT_DONE = 5;
125 // Config has been loaded from carrier app (or cache).
126 private static final int EVENT_FETCH_CARRIER_DONE = 6;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700127 // Attempt to fetch from default app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700128 private static final int EVENT_DO_FETCH_DEFAULT = 7;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700129 // Attempt to fetch from carrier app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700130 private static final int EVENT_DO_FETCH_CARRIER = 8;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700131 // A package has been installed, uninstalled, or updated.
132 private static final int EVENT_PACKAGE_CHANGED = 9;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700133 // Bind timed out for the default app.
134 private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
135 // Bind timed out for a carrier app.
136 private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700137 // Check if the system fingerprint has changed.
138 private static final int EVENT_CHECK_SYSTEM_UPDATE = 12;
Nanxi Chen3d670502016-03-17 16:32:09 -0700139 // Rerun carrier config binding after system is unlocked.
140 private static final int EVENT_SYSTEM_UNLOCKED = 13;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700141 // Fetching config timed out from the default app.
142 private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14;
143 // Fetching config timed out from a carrier app.
144 private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15;
Nathan Harold48ac0972019-03-13 22:33:01 -0700145 // SubscriptionInfoUpdater has finished updating the sub for the carrier config.
146 private static final int EVENT_SUBSCRIPTION_INFO_UPDATED = 16;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700147 // Multi-SIM config changed.
148 private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 17;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700149
Junda Liu6cb45002016-03-22 17:18:20 -0700150 private static final int BIND_TIMEOUT_MILLIS = 30000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800151
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700152 // Tags used for saving and restoring XML documents.
153 private static final String TAG_DOCUMENT = "carrier_config";
154 private static final String TAG_VERSION = "package_version";
155 private static final String TAG_BUNDLE = "bundle_data";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800156
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100157 private static final String OVERRIDE_PACKAGE_ADDITION = "-override";
158
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700159 // SharedPreferences key for last known build fingerprint.
160 private static final String KEY_FINGERPRINT = "build_fingerprint";
161
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800162 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700163 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700164 // For each phoneId, the event sequence should be:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700165 // fetch default, connected to default, fetch default (async), fetch default done,
166 // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700167 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700168 // If there is a saved config file for either the default app or the carrier app, we skip
169 // binding to the app and go straight from fetch to loaded.
170 //
171 // At any time, at most one connection is active. If events are not in this order, previous
172 // connection will be unbound, so only latest event takes effect.
173 //
174 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
175 // 1. loading from carrier app (even if read from a file)
176 // 2. loading from default app if there is no carrier app (even if read from a file)
177 // 3. clearing config (e.g. due to sim removal)
178 // 4. encountering bind or IPC error
Jonathan Basseri65273c82017-07-25 15:08:42 -0700179 private class ConfigHandler extends Handler {
180 @Override
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800181 public void handleMessage(Message msg) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700182 final int phoneId = msg.arg1;
chen xuc4810972019-04-17 23:44:43 -0700183 logWithLocalLog("mHandler: " + msg.what + " phoneId: " + phoneId);
Malcolm Chen5ea18532019-10-24 19:55:44 -0700184 if (!SubscriptionManager.isValidPhoneId(phoneId)
185 && msg.what != EVENT_MULTI_SIM_CONFIG_CHANGED) {
186 return;
187 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800188 switch (msg.what) {
189 case EVENT_CLEAR_CONFIG:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700190 {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700191 clearConfigForPhone(phoneId, true);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800192 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700193 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700194
Nanxi Chen3d670502016-03-17 16:32:09 -0700195 case EVENT_SYSTEM_UNLOCKED:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700196 {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700197 for (int i = 0; i < TelephonyManager.from(mContext).getActiveModemCount();
198 ++i) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700199 // When user unlock device, we should only try to send broadcast again if we
200 // have sent it before unlock. This will avoid we try to load carrier config
201 // when SIM is still loading when unlock happens.
Junda Liu03aad762017-07-21 13:32:17 -0700202 if (mHasSentConfigChange[i]) {
Jack Yu67663de2019-10-17 15:19:48 -0700203 logWithLocalLog("System unlocked");
Junda Liu03aad762017-07-21 13:32:17 -0700204 updateConfigForPhoneId(i);
205 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700206 }
207 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700208 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700209
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700210 case EVENT_PACKAGE_CHANGED:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700211 {
212 final String carrierPackageName = (String) msg.obj;
213 // Only update if there are cached config removed to avoid updating config for
214 // unrelated packages.
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700215 if (clearCachedConfigForPackage(carrierPackageName)) {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700216 int numPhones = TelephonyManager.from(mContext).getActiveModemCount();
Junda Liu8a8a53a2015-05-15 16:19:57 -0700217 for (int i = 0; i < numPhones; ++i) {
Jack Yu67663de2019-10-17 15:19:48 -0700218 logWithLocalLog("Package changed: " + carrierPackageName
219 + ", phone=" + i);
Junda Liu8a8a53a2015-05-15 16:19:57 -0700220 updateConfigForPhoneId(i);
221 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700222 }
223 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700224 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700225
Jonathan Basseri65273c82017-07-25 15:08:42 -0700226 case EVENT_DO_FETCH_DEFAULT:
227 {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100228 // Restore persistent override values.
229 PersistableBundle config = restoreConfigFromXml(
230 mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId);
231 if (config != null) {
232 log("Loaded persistent override config from XML. package="
233 + mPlatformCarrierConfigPackage
234 + " phoneId=" + phoneId);
235 mPersistentOverrideConfigs[phoneId] = config;
236 }
237
238 config = restoreConfigFromXml(mPlatformCarrierConfigPackage, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700239 if (config != null) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700240 log(
241 "Loaded config from XML. package="
242 + mPlatformCarrierConfigPackage
243 + " phoneId="
244 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700245 mConfigFromDefaultApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700246 Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700247 newMsg.getData().putBoolean("loaded_from_xml", true);
248 mHandler.sendMessage(newMsg);
249 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700250 // No cached config, so fetch it from the default app.
251 if (bindToConfigPackage(
252 mPlatformCarrierConfigPackage,
253 phoneId,
254 EVENT_CONNECTED_TO_DEFAULT)) {
255 sendMessageDelayed(
256 obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700257 BIND_TIMEOUT_MILLIS);
258 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700259 // Send broadcast if bind fails.
Nathan Harold48ac0972019-03-13 22:33:01 -0700260 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700261 // TODO: We *must* call unbindService even if bindService returns false.
262 // (And possibly if SecurityException was thrown.)
chen xudb04c292019-03-26 17:01:15 -0700263 loge("binding to default app: "
264 + mPlatformCarrierConfigPackage + " fails");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700265 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800266 }
267 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700268 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800269
270 case EVENT_CONNECTED_TO_DEFAULT:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700271 {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700272 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700273 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800274 // If new service connection has been created, unbind.
275 if (mServiceConnection[phoneId] != conn || conn.service == null) {
276 mContext.unbindService(conn);
277 break;
278 }
chen xu02581692018-11-11 19:03:44 -0800279 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700280 // ResultReceiver callback will execute in this Handler's thread.
281 final ResultReceiver resultReceiver =
282 new ResultReceiver(this) {
283 @Override
284 public void onReceiveResult(int resultCode, Bundle resultData) {
285 mContext.unbindService(conn);
286 // If new service connection has been created, this is stale.
287 if (mServiceConnection[phoneId] != conn) {
288 loge("Received response for stale request.");
289 return;
290 }
291 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
292 if (resultCode == RESULT_ERROR || resultData == null) {
293 // On error, abort config fetching.
294 loge("Failed to get carrier config");
Nathan Harold48ac0972019-03-13 22:33:01 -0700295 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700296 return;
297 }
298 PersistableBundle config =
299 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100300 saveConfigToXml(mPlatformCarrierConfigPackage, "", phoneId,
Chen Xucd4a6372019-07-29 16:34:52 -0700301 carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700302 mConfigFromDefaultApp[phoneId] = config;
303 sendMessage(
304 obtainMessage(
305 EVENT_FETCH_DEFAULT_DONE, phoneId, -1));
306 }
307 };
308 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800309 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700310 ICarrierService carrierService =
311 ICarrierService.Stub.asInterface(conn.service);
312 carrierService.getCarrierConfig(carrierId, resultReceiver);
chen xudb04c292019-03-26 17:01:15 -0700313 logWithLocalLog("fetch config for default app: "
314 + mPlatformCarrierConfigPackage
315 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700316 } catch (RemoteException e) {
chen xudb04c292019-03-26 17:01:15 -0700317 loge("Failed to get carrier config from default app: " +
318 mPlatformCarrierConfigPackage + " err: " + e.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700319 mContext.unbindService(conn);
320 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800321 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700322 sendMessageDelayed(
323 obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1),
324 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800325 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700326 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800327
Jonathan Basseri930701e2015-06-11 20:56:43 -0700328 case EVENT_BIND_DEFAULT_TIMEOUT:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700329 case EVENT_FETCH_DEFAULT_TIMEOUT:
330 {
chen xudb04c292019-03-26 17:01:15 -0700331 loge("bind/fetch time out from " + mPlatformCarrierConfigPackage);
chen xuf60b3162019-05-01 21:31:05 -0700332 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
333 // If we attempted to bind to the app, but the service connection is null due to
334 // the race condition that clear config event happens before bind/fetch complete
335 // then config was cleared while we were waiting and we should not continue.
336 if (mServiceConnection[phoneId] != null) {
337 // If a ResponseReceiver callback is in the queue when this happens, we will
338 // unbind twice and throw an exception.
339 mContext.unbindService(mServiceConnection[phoneId]);
340 broadcastConfigChangedIntent(phoneId);
341 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700342 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700343 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700344 }
Jonathan Basseri930701e2015-06-11 20:56:43 -0700345
Jonathan Basseri65273c82017-07-25 15:08:42 -0700346 case EVENT_FETCH_DEFAULT_DONE:
347 {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700348 // If we attempted to bind to the app, but the service connection is null, then
349 // config was cleared while we were waiting and we should not continue.
350 if (!msg.getData().getBoolean("loaded_from_xml", false)
351 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800352 break;
353 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700354 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700355 if (carrierPackageName != null) {
356 log("Found carrier config app: " + carrierPackageName);
Jordan Liu38a614c2019-03-20 15:01:17 -0700357 sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId, -1));
Jonathan Basserib919e932015-05-27 16:53:08 +0000358 } else {
Nathan Harold48ac0972019-03-13 22:33:01 -0700359 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700360 }
361 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700362 }
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700363
Jonathan Basseri65273c82017-07-25 15:08:42 -0700364 case EVENT_DO_FETCH_CARRIER:
365 {
366 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700367 final PersistableBundle config =
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100368 restoreConfigFromXml(carrierPackageName, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700369 if (config != null) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700370 log(
371 "Loaded config from XML. package="
372 + carrierPackageName
373 + " phoneId="
374 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700375 mConfigFromCarrierApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700376 Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700377 newMsg.getData().putBoolean("loaded_from_xml", true);
378 sendMessage(newMsg);
379 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700380 // No cached config, so fetch it from a carrier app.
Jonathan Basseri40473a52015-08-14 14:36:29 -0700381 if (carrierPackageName != null
Jonathan Basseri65273c82017-07-25 15:08:42 -0700382 && bindToConfigPackage(
383 carrierPackageName,
384 phoneId,
385 EVENT_CONNECTED_TO_CARRIER)) {
386 sendMessageDelayed(
387 obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700388 BIND_TIMEOUT_MILLIS);
389 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700390 // Send broadcast if bind fails.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700391 broadcastConfigChangedIntent(phoneId);
chen xudb04c292019-03-26 17:01:15 -0700392 loge("bind to carrier app: " + carrierPackageName + " fails");
Nathan Harold48ac0972019-03-13 22:33:01 -0700393 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700394 }
395 }
396 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700397 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700398
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800399 case EVENT_CONNECTED_TO_CARRIER:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700400 {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700401 removeMessages(EVENT_BIND_CARRIER_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700402 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800403 // If new service connection has been created, unbind.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700404 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800405 mContext.unbindService(conn);
406 break;
407 }
chen xu02581692018-11-11 19:03:44 -0800408 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700409 // ResultReceiver callback will execute in this Handler's thread.
410 final ResultReceiver resultReceiver =
411 new ResultReceiver(this) {
412 @Override
413 public void onReceiveResult(int resultCode, Bundle resultData) {
414 mContext.unbindService(conn);
415 // If new service connection has been created, this is stale.
416 if (mServiceConnection[phoneId] != conn) {
417 loge("Received response for stale request.");
418 return;
419 }
420 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
421 if (resultCode == RESULT_ERROR || resultData == null) {
422 // On error, abort config fetching.
chen xudb04c292019-03-26 17:01:15 -0700423 loge("Failed to get carrier config from carrier app: "
424 + getCarrierPackageForPhoneId(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700425 broadcastConfigChangedIntent(phoneId);
Nathan Harold48ac0972019-03-13 22:33:01 -0700426 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700427 return;
428 }
429 PersistableBundle config =
430 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100431 saveConfigToXml(getCarrierPackageForPhoneId(phoneId), "",
432 phoneId, carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700433 mConfigFromCarrierApp[phoneId] = config;
434 sendMessage(
435 obtainMessage(
436 EVENT_FETCH_CARRIER_DONE, phoneId, -1));
437 }
438 };
439 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800440 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700441 ICarrierService carrierService =
442 ICarrierService.Stub.asInterface(conn.service);
443 carrierService.getCarrierConfig(carrierId, resultReceiver);
chen xudb04c292019-03-26 17:01:15 -0700444 logWithLocalLog("fetch config for carrier app: "
445 + getCarrierPackageForPhoneId(phoneId)
446 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700447 } catch (RemoteException e) {
448 loge("Failed to get carrier config: " + e.toString());
449 mContext.unbindService(conn);
450 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800451 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700452 sendMessageDelayed(
453 obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1),
454 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800455 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700456 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800457
Jonathan Basseri930701e2015-06-11 20:56:43 -0700458 case EVENT_BIND_CARRIER_TIMEOUT:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700459 case EVENT_FETCH_CARRIER_TIMEOUT:
460 {
chen xudb04c292019-03-26 17:01:15 -0700461 loge("bind/fetch from carrier app timeout");
chen xuf60b3162019-05-01 21:31:05 -0700462 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
463 // If we attempted to bind to the app, but the service connection is null due to
464 // the race condition that clear config event happens before bind/fetch complete
465 // then config was cleared while we were waiting and we should not continue.
466 if (mServiceConnection[phoneId] != null) {
467 // If a ResponseReceiver callback is in the queue when this happens, we will
468 // unbind twice and throw an exception.
469 mContext.unbindService(mServiceConnection[phoneId]);
470 broadcastConfigChangedIntent(phoneId);
471 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700472 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700473 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700474 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700475 case EVENT_FETCH_CARRIER_DONE:
476 {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700477 // If we attempted to bind to the app, but the service connection is null, then
478 // config was cleared while we were waiting and we should not continue.
479 if (!msg.getData().getBoolean("loaded_from_xml", false)
480 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800481 break;
482 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700483 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800484 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700485 }
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700486
487 case EVENT_CHECK_SYSTEM_UPDATE:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700488 {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700489 SharedPreferences sharedPrefs =
490 PreferenceManager.getDefaultSharedPreferences(mContext);
491 final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
492 if (!Build.FINGERPRINT.equals(lastFingerprint)) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700493 log(
494 "Build fingerprint changed. old: "
495 + lastFingerprint
496 + " new: "
497 + Build.FINGERPRINT);
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700498 clearCachedConfigForPackage(null);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700499 sharedPrefs
500 .edit()
501 .putString(KEY_FINGERPRINT, Build.FINGERPRINT)
502 .apply();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700503 }
504 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700505 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700506
507 case EVENT_SUBSCRIPTION_INFO_UPDATED:
508 broadcastConfigChangedIntent(phoneId);
509 break;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700510 case EVENT_MULTI_SIM_CONFIG_CHANGED:
511 onMultiSimConfigChanged();
512 break;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800513 }
514 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700515 }
516
517 private final Handler mHandler;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800518
519 /**
520 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
521 * receiver for relevant events.
522 */
523 private CarrierConfigLoader(Context context) {
524 mContext = context;
Meng Wang33ad2bc2017-03-16 20:21:20 -0700525 mPlatformCarrierConfigPackage =
526 mContext.getString(R.string.platform_carrier_config_package);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700527 mHandler = new ConfigHandler();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800528
Nanxi Chen3d670502016-03-17 16:32:09 -0700529 IntentFilter bootFilter = new IntentFilter();
530 bootFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
531 context.registerReceiver(mBootReceiver, bootFilter);
532
Junda Liu8a8a53a2015-05-15 16:19:57 -0700533 // Register for package updates. Update app or uninstall app update will have all 3 intents,
534 // in the order or removed, added, replaced, all with extra_replace set to true.
535 IntentFilter pkgFilter = new IntentFilter();
536 pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
537 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
538 pkgFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
539 pkgFilter.addDataScheme("package");
Jordan Liu5ca24762019-07-10 12:51:09 -0700540 context.registerReceiver(mPackageReceiver, pkgFilter);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800541
Malcolm Chen978dda52019-10-08 18:15:25 -0700542 int numPhones = TelephonyManager.from(context).getSupportedModemCount();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700543 mConfigFromDefaultApp = new PersistableBundle[numPhones];
544 mConfigFromCarrierApp = new PersistableBundle[numPhones];
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100545 mPersistentOverrideConfigs = new PersistableBundle[numPhones];
Hall Liu506724b2018-10-22 18:16:14 -0700546 mOverrideConfigs = new PersistableBundle[numPhones];
Zach Johnson36d7aab2015-05-22 15:43:00 -0700547 mServiceConnection = new CarrierServiceConnection[numPhones];
Junda Liu03aad762017-07-21 13:32:17 -0700548 mHasSentConfigChange = new boolean[numPhones];
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800549 // Make this service available through ServiceManager.
550 ServiceManager.addService(Context.CARRIER_CONFIG_SERVICE, this);
551 log("CarrierConfigLoader has started");
Nathan Harold48ac0972019-03-13 22:33:01 -0700552 mSubscriptionInfoUpdater = PhoneFactory.getSubscriptionInfoUpdater();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700553 mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800554 }
555
556 /**
557 * Initialize the singleton CarrierConfigLoader instance.
558 *
559 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
560 */
561 /* package */
562 static CarrierConfigLoader init(Context context) {
563 synchronized (CarrierConfigLoader.class) {
564 if (sInstance == null) {
565 sInstance = new CarrierConfigLoader(context);
566 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700567 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800568 }
569 return sInstance;
570 }
571 }
572
Malcolm Chen5ea18532019-10-24 19:55:44 -0700573 private void clearConfigForPhone(int phoneId, boolean sendBroadcast) {
574 /* Ignore clear configuration request if device is being shutdown. */
575 Phone phone = PhoneFactory.getPhone(phoneId);
576 if (phone != null) {
577 if (phone.isShuttingDown()) {
578 return;
579 }
580 }
581
582 mConfigFromDefaultApp[phoneId] = null;
583 mConfigFromCarrierApp[phoneId] = null;
584 mServiceConnection[phoneId] = null;
585 mHasSentConfigChange[phoneId] = false;
586
587 if (sendBroadcast) broadcastConfigChangedIntent(phoneId, false);
588 }
589
Nathan Harold48ac0972019-03-13 22:33:01 -0700590 private void notifySubscriptionInfoUpdater(int phoneId) {
591 String configPackagename;
592 PersistableBundle configToSend;
593 int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
594 // Prefer the carrier privileged carrier app, but if there is not one, use the platform
595 // default carrier app.
596 if (mConfigFromCarrierApp[phoneId] != null) {
597 configPackagename = getCarrierPackageForPhoneId(phoneId);
598 configToSend = mConfigFromCarrierApp[phoneId];
599 } else {
600 configPackagename = mPlatformCarrierConfigPackage;
601 configToSend = mConfigFromDefaultApp[phoneId];
602 }
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700603
604 // mOverrideConfigs is for testing. And it will override current configs.
605 PersistableBundle config = mOverrideConfigs[phoneId];
606 if (config != null) {
Torbjorn Eklund1050cb02018-11-16 14:05:38 +0100607 configToSend = new PersistableBundle(configToSend);
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700608 configToSend.putAll(config);
609 }
610
Nathan Harold48ac0972019-03-13 22:33:01 -0700611 mSubscriptionInfoUpdater.updateSubscriptionByCarrierConfigAndNotifyComplete(
612 phoneId, configPackagename, configToSend,
613 mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1));
614 }
615
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800616 private void broadcastConfigChangedIntent(int phoneId) {
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800617 broadcastConfigChangedIntent(phoneId, true);
618 }
619
620 private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800621 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800622 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
chen xuf9db49f2019-03-31 23:04:42 -0700623 Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND |
624 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800625 if (addSubIdExtra) {
626 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
627 int[] subIds = SubscriptionManager.getSubId(phoneId);
628 if (!ArrayUtils.isEmpty(subIds)) {
629 TelephonyManager telMgr = TelephonyManager.from(mContext)
630 .createForSubscriptionId(subIds[0]);
631 simApplicationState = telMgr.getSimApplicationState();
632 }
633 // Include subId/carrier id extra only if SIM records are loaded
634 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
635 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
636 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
637 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID,
638 getSpecificCarrierIdForPhoneId(phoneId));
639 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, getCarrierIdForPhoneId(phoneId));
640 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800641 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800642 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Jack Yuf1661322019-04-06 00:37:23 -0700643 log("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
Jordan Liu5ca24762019-07-10 12:51:09 -0700644 mContext.sendBroadcast(intent);
Junda Liu03aad762017-07-21 13:32:17 -0700645 mHasSentConfigChange[phoneId] = true;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800646 }
647
648 /** Binds to the default or carrier config app. */
649 private boolean bindToConfigPackage(String pkgName, int phoneId, int eventId) {
chen xudb04c292019-03-26 17:01:15 -0700650 logWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700651 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700652 carrierService.setPackage(pkgName);
653 mServiceConnection[phoneId] = new CarrierServiceConnection(phoneId, eventId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800654 try {
Zach Johnson36d7aab2015-05-22 15:43:00 -0700655 return mContext.bindService(carrierService, mServiceConnection[phoneId],
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800656 Context.BIND_AUTO_CREATE);
657 } catch (SecurityException ex) {
658 return false;
659 }
660 }
661
chen xu02581692018-11-11 19:03:44 -0800662 private CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800663 String mcc = "";
664 String mnc = "";
665 String imsi = "";
666 String gid1 = "";
667 String gid2 = "";
668 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
669 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800670 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800671 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700672 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
673 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800674 mcc = simOperator.substring(0, 3);
675 mnc = simOperator.substring(3);
676 }
677 Phone phone = PhoneFactory.getPhone(phoneId);
678 if (phone != null) {
679 imsi = phone.getSubscriberId();
680 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700681 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800682 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800683 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800684 }
chen xua31f22b2019-03-06 15:28:50 -0800685 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800686 }
687
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700688 /** Returns the package name of a priveleged carrier app, or null if there is none. */
689 private String getCarrierPackageForPhoneId(int phoneId) {
690 List<String> carrierPackageNames = TelephonyManager.from(mContext)
691 .getCarrierPackageNamesForIntentAndPhone(
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700692 new Intent(CarrierService.CARRIER_SERVICE_INTERFACE), phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700693 if (carrierPackageNames != null && carrierPackageNames.size() > 0) {
694 return carrierPackageNames.get(0);
695 } else {
696 return null;
697 }
698 }
699
700 private String getIccIdForPhoneId(int phoneId) {
701 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
702 return null;
703 }
704 Phone phone = PhoneFactory.getPhone(phoneId);
705 if (phone == null) {
706 return null;
707 }
708 return phone.getIccSerialNumber();
709 }
710
chen xu02581692018-11-11 19:03:44 -0800711 /**
chen xua31f22b2019-03-06 15:28:50 -0800712 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800713 */
chen xua31f22b2019-03-06 15:28:50 -0800714 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700715 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800716 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700717 }
718 Phone phone = PhoneFactory.getPhone(phoneId);
719 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800720 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700721 }
chen xua31f22b2019-03-06 15:28:50 -0800722 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800723 }
724
725 /**
726 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
727 */
728 private int getCarrierIdForPhoneId(int phoneId) {
729 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
730 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700731 }
chen xu02581692018-11-11 19:03:44 -0800732 Phone phone = PhoneFactory.getPhone(phoneId);
733 if (phone == null) {
734 return TelephonyManager.UNKNOWN_CARRIER_ID;
735 }
736 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700737 }
738
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700739 /**
740 * Writes a bundle to an XML file.
741 *
chen xu02581692018-11-11 19:03:44 -0800742 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -0800743 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -0800744 * should have a single copy of XML file named after carrier id. However, it's still possible
745 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
746 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
747 * carrier while iccid remains the same.
748 *
749 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
750 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700751 *
752 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700753 *
754 * @param packageName the name of the package from which we fetched this bundle.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100755 * @param extraString An extra string to be used in the XML file name.
Yuchen Dongc15afed2018-04-26 17:05:22 +0800756 * @param phoneId the phone ID.
Chen Xucd4a6372019-07-29 16:34:52 -0700757 * @param carrierId contains all carrier-identifying information.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700758 * @param config the bundle to be written. Null will be treated as an empty bundle.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700759 */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100760 private void saveConfigToXml(String packageName, @NonNull String extraString, int phoneId,
761 CarrierIdentifier carrierId, PersistableBundle config) {
Yuchen Dongc15afed2018-04-26 17:05:22 +0800762 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
763 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700764 loge("Skip save config because SIM records are not loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800765 return;
766 }
767
768 final String iccid = getIccIdForPhoneId(phoneId);
Chen Xucd4a6372019-07-29 16:34:52 -0700769 final int cid = carrierId.getSpecificCarrierId();
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700770 if (packageName == null || iccid == null) {
771 loge("Cannot save config with null packageName or iccid.");
772 return;
773 }
Junda Liu02596502016-11-15 13:46:43 -0800774 // b/32668103 Only save to file if config isn't empty.
775 // In case of failure, not caching an empty bundle will
776 // try loading config again on next power on or sim loaded.
777 // Downside is for genuinely empty bundle, will bind and load
778 // on every power on.
779 if (config == null || config.isEmpty()) {
780 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700781 }
782
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700783 final String version = getPackageVersion(packageName);
784 if (version == null) {
785 loge("Failed to get package version for: " + packageName);
786 return;
787 }
788
chen xudb04c292019-03-26 17:01:15 -0700789 logWithLocalLog("save config to xml, packagename: " + packageName + " phoneId: " + phoneId);
790
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700791 FileOutputStream outFile = null;
792 try {
793 outFile = new FileOutputStream(
chen xu02581692018-11-11 19:03:44 -0800794 new File(mContext.getFilesDir(),
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100795 getFilenameForConfig(packageName, extraString, iccid, cid)));
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700796 FastXmlSerializer out = new FastXmlSerializer();
797 out.setOutput(outFile, "utf-8");
798 out.startDocument("utf-8", true);
799 out.startTag(null, TAG_DOCUMENT);
800 out.startTag(null, TAG_VERSION);
801 out.text(version);
802 out.endTag(null, TAG_VERSION);
803 out.startTag(null, TAG_BUNDLE);
804 config.saveToXml(out);
805 out.endTag(null, TAG_BUNDLE);
806 out.endTag(null, TAG_DOCUMENT);
807 out.endDocument();
808 out.flush();
809 outFile.close();
810 }
811 catch (IOException e) {
812 loge(e.toString());
813 }
814 catch (XmlPullParserException e) {
815 loge(e.toString());
816 }
817 }
818
819 /**
820 * Reads a bundle from an XML file.
821 *
822 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +0800823 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700824 *
825 * In case of errors, or if the saved config is from a different package version than the
826 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700827 *
828 * @param packageName the name of the package from which we fetched this bundle.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100829 * @param extraString An extra string to be used in the XML file name.
Yuchen Dongc15afed2018-04-26 17:05:22 +0800830 * @param phoneId the phone ID.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700831 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
832 * version does not match, or reading config fails.
833 */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100834 private PersistableBundle restoreConfigFromXml(String packageName, @NonNull String extraString,
835 int phoneId) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700836 final String version = getPackageVersion(packageName);
837 if (version == null) {
838 loge("Failed to get package version for: " + packageName);
839 return null;
840 }
Yuchen Dongc15afed2018-04-26 17:05:22 +0800841 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
842 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700843 loge("Skip restoring config because SIM records are not yet loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800844 return null;
845 }
846
847 final String iccid = getIccIdForPhoneId(phoneId);
chen xua31f22b2019-03-06 15:28:50 -0800848 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700849 if (packageName == null || iccid == null) {
850 loge("Cannot restore config with null packageName or iccid.");
851 return null;
852 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700853
854 PersistableBundle restoredBundle = null;
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100855 File file = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700856 FileInputStream inFile = null;
857 try {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100858 file = new File(mContext.getFilesDir(),
859 getFilenameForConfig(packageName, extraString, iccid, cid));
860 inFile = new FileInputStream(file);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700861 XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
862 parser.setInput(inFile, "utf-8");
863
864 int event;
865 while (((event = parser.next()) != XmlPullParser.END_DOCUMENT)) {
866
867 if (event == XmlPullParser.START_TAG && TAG_VERSION.equals(parser.getName())) {
868 String savedVersion = parser.nextText();
869 if (!version.equals(savedVersion)) {
chen xudb04c292019-03-26 17:01:15 -0700870 loge("Saved version mismatch: " + version + " vs " + savedVersion);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700871 break;
872 }
873 }
874
875 if (event == XmlPullParser.START_TAG && TAG_BUNDLE.equals(parser.getName())) {
876 restoredBundle = PersistableBundle.restoreFromXml(parser);
877 }
878 }
879 inFile.close();
880 }
881 catch (FileNotFoundException e) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100882 // Missing file is normal occurrence that might occur with a new sim or when restoring
883 // an override file during boot and should not be treated as an error.
884 if (file != null) log("File not found: " + file.getPath());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700885 }
886 catch (XmlPullParserException e) {
887 loge(e.toString());
888 }
889 catch (IOException e) {
890 loge(e.toString());
891 }
892
893 return restoredBundle;
894 }
895
Junda Liu8a8a53a2015-05-15 16:19:57 -0700896 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700897 * Clears cached carrier config.
898 * This deletes all saved XML files associated with the given package name. If packageName is
899 * null, then it deletes all saved XML files.
900 *
901 * @param packageName the name of a carrier package, or null if all cached config should be
902 * cleared.
903 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -0700904 */
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700905 private boolean clearCachedConfigForPackage(final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700906 File dir = mContext.getFilesDir();
907 File[] packageFiles = dir.listFiles(new FilenameFilter() {
908 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700909 if (packageName != null) {
910 return filename.startsWith("carrierconfig-" + packageName + "-");
911 } else {
912 return filename.startsWith("carrierconfig-");
913 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700914 }
915 });
Junda Liu8a8a53a2015-05-15 16:19:57 -0700916 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700917 for (File f : packageFiles) {
918 log("deleting " + f.getName());
919 f.delete();
920 }
Junda Liu8a8a53a2015-05-15 16:19:57 -0700921 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700922 }
923
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700924 /** Builds a canonical file name for a config file. */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100925 private String getFilenameForConfig(@NonNull String packageName, @NonNull String extraString,
926 @NonNull String iccid, int cid) {
chen xu02581692018-11-11 19:03:44 -0800927 // the same carrier should have a single copy of XML file named after carrier id.
928 // However, it's still possible that platform doesn't recognize the current sim carrier,
929 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
930 // cases SIM OTA resolves to different carrier while iccid remains the same.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100931 return "carrierconfig-" + packageName + extraString + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700932 }
933
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700934 /** Return the current version code of a package, or null if the name is not found. */
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700935 private String getPackageVersion(String packageName) {
936 try {
937 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -0800938 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700939 } catch (PackageManager.NameNotFoundException e) {
940 return null;
941 }
942 }
943
Jonathan Basseri65273c82017-07-25 15:08:42 -0700944 /**
945 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700946 *
947 * This reads config bundles for the given phoneId. That means getting the latest bundle from
948 * the default app and a privileged carrier app, if present. This will not bind to an app if we
949 * have a saved config file to use instead.
950 */
951 private void updateConfigForPhoneId(int phoneId) {
Junda Liu8a8a53a2015-05-15 16:19:57 -0700952 // Clear in-memory cache for carrier app config, so when carrier app gets uninstalled, no
953 // stale config is left.
954 if (mConfigFromCarrierApp[phoneId] != null &&
955 getCarrierPackageForPhoneId(phoneId) == null) {
956 mConfigFromCarrierApp[phoneId] = null;
957 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700958 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700959 }
960
Malcolm Chen5ea18532019-10-24 19:55:44 -0700961 private void onMultiSimConfigChanged() {
962 for (int i = TelephonyManager.from(mContext).getActiveModemCount();
963 i < mConfigFromDefaultApp.length; i++) {
964 clearConfigForPhone(i, false);
965 }
966 }
967
Hall Liu506724b2018-10-22 18:16:14 -0700968 @Override
969 public @NonNull PersistableBundle getConfigForSubId(int subId, String callingPackage) {
Philip P. Moltmann9797cbb2020-01-07 13:45:00 -0800970 return getConfigForSubIdWithFeature(subId, callingPackage, null);
971 }
972
973 @Override
974 public @NonNull PersistableBundle getConfigForSubIdWithFeature(int subId, String callingPackage,
975 String callingFeatureId) {
976 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subId, callingPackage,
977 callingFeatureId, "getCarrierConfig")) {
Malcolm Chen6d3d6b32018-03-01 13:58:03 -0800978 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -0700979 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -0700980
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800981 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700982 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800983 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700984 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800985 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700986 retConfig.putAll(config);
yinxud0104832019-08-16 14:20:08 -0700987 if (getCarrierPackageForPhoneId(phoneId) == null) {
988 retConfig.putBoolean(
989 CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
990 }
yinxuc5e27622017-11-29 15:08:50 -0800991 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800992 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800993 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700994 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -0800995 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
996 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100997 config = mPersistentOverrideConfigs[phoneId];
998 if (config != null) {
999 retConfig.putAll(config);
1000 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
1001 }
Hall Liu506724b2018-10-22 18:16:14 -07001002 config = mOverrideConfigs[phoneId];
1003 if (config != null) {
1004 retConfig.putAll(config);
Hall Liu506724b2018-10-22 18:16:14 -07001005 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001006 }
1007 return retConfig;
1008 }
1009
1010 @Override
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001011 public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides,
1012 boolean persistent) {
Hall Liu506724b2018-10-22 18:16:14 -07001013 mContext.enforceCallingOrSelfPermission(
1014 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Brad Ebingerf6281ad2019-04-25 11:02:04 -07001015 //TODO: Also check for SHELL UID to restrict this method to testing only (b/131326259)
Hall Liu506724b2018-10-22 18:16:14 -07001016 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1017 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1018 log("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
1019 return;
1020 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001021 overrideConfig(mOverrideConfigs, phoneId, overrides);
Hall Liu506724b2018-10-22 18:16:14 -07001022
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001023 if (persistent) {
1024 overrideConfig(mPersistentOverrideConfigs, phoneId, overrides);
1025
1026 if (overrides != null) {
1027 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
1028 saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId,
1029 carrierId, mPersistentOverrideConfigs[phoneId]);
1030 } else {
1031 final String iccid = getIccIdForPhoneId(phoneId);
1032 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1033 String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage,
1034 OVERRIDE_PACKAGE_ADDITION, iccid, cid);
1035 File fileToDelete = new File(mContext.getFilesDir(), fileName);
1036 fileToDelete.delete();
1037 }
Hall Liu506724b2018-10-22 18:16:14 -07001038 }
Brad Ebingerc9d1fa12019-04-17 15:21:18 -07001039 notifySubscriptionInfoUpdater(phoneId);
Hall Liu506724b2018-10-22 18:16:14 -07001040 }
1041
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001042 private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId,
1043 @Nullable PersistableBundle overrides) {
1044 if (overrides == null) {
1045 currentOverrides[phoneId] = new PersistableBundle();
1046 } else if (currentOverrides[phoneId] == null) {
1047 currentOverrides[phoneId] = overrides;
1048 } else {
1049 currentOverrides[phoneId].putAll(overrides);
1050 }
1051 }
1052
Hall Liu506724b2018-10-22 18:16:14 -07001053 @Override
Jonathan Basseri86030352015-06-08 12:27:56 -07001054 public void notifyConfigChangedForSubId(int subId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001055 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001056 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001057 log("Ignore invalid phoneId: " + phoneId + " for subId: " + subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001058 return;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001059 }
Jordan Liud7d57fe2019-04-16 12:29:43 -07001060
1061 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001062 // system privileged app with MODIFY_PHONE_STATE permission.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001063 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext, subId,
1064 "Require carrier privileges or MODIFY_PHONE_STATE permission.");
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001065
1066 // This method should block until deleting has completed, so that an error which prevents us
1067 // from clearing the cache is passed back to the carrier app. With the files successfully
1068 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001069 String callingPackageName = mContext.getPackageManager().getNameForUid(
1070 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001071 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001072 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001073 }
1074
1075 @Override
1076 public void updateConfigForPhoneId(int phoneId, String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -07001077 mContext.enforceCallingOrSelfPermission(
1078 android.Manifest.permission.MODIFY_PHONE_STATE, null);
chen xuc4810972019-04-17 23:44:43 -07001079 logWithLocalLog("update config for phoneId: " + phoneId + " simState: " + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001080 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1081 return;
1082 }
1083 // requires Java 7 for switch on string.
1084 switch (simState) {
1085 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1086 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001087 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001088 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Junda Liu9f2d2712015-05-15 14:22:45 -07001089 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001090 break;
1091 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1092 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001093 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001094 break;
1095 }
1096 }
1097
Junda Liu43d723a2015-05-12 17:23:45 -07001098 @Override
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001099 public String getDefaultCarrierServicePackageName() {
Shuo Qianefdb7962019-12-19 15:54:27 -08001100 mContext.enforceCallingOrSelfPermission(
1101 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
1102 "getDefaultCarrierServicePackageName");
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001103 return mPlatformCarrierConfigPackage;
1104 }
1105
1106 @Override
Junda Liu43d723a2015-05-12 17:23:45 -07001107 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1108 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1109 != PackageManager.PERMISSION_GRANTED) {
1110 pw.println("Permission Denial: can't dump carrierconfig from from pid="
1111 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1112 return;
1113 }
1114 pw.println("CarrierConfigLoader: " + this);
1115 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
shuoq26a3a4c2016-12-16 11:06:48 -08001116 pw.println("Phone Id = " + i);
1117 // display default values in CarrierConfigManager
1118 printConfig(CarrierConfigManager.getDefaultConfig(), pw,
1119 "Default Values from CarrierConfigManager");
1120 pw.println("");
1121 // display ConfigFromDefaultApp
1122 printConfig(mConfigFromDefaultApp[i], pw, "mConfigFromDefaultApp");
1123 pw.println("");
1124 // display ConfigFromCarrierApp
1125 printConfig(mConfigFromCarrierApp[i], pw, "mConfigFromCarrierApp");
Hall Liu506724b2018-10-22 18:16:14 -07001126 pw.println("");
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001127 printConfig(mPersistentOverrideConfigs[i], pw, "mPersistentOverrideConfigs");
1128 pw.println("");
Hall Liu506724b2018-10-22 18:16:14 -07001129 printConfig(mOverrideConfigs[i], pw, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001130 }
chen xudb04c292019-03-26 17:01:15 -07001131
1132 pw.println("CarrierConfigLoadingLog=");
1133 mCarrierConfigLoadingLog.dump(fd, pw, args);
shuoq26a3a4c2016-12-16 11:06:48 -08001134 }
1135
1136 private void printConfig(PersistableBundle configApp, PrintWriter pw, String name) {
1137 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
1138 if (configApp == null) {
1139 indentPW.increaseIndent();
1140 indentPW.println(name + " : null ");
1141 return;
1142 }
1143 indentPW.increaseIndent();
1144 indentPW.println(name + " : ");
1145 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1146 Collections.sort(sortedKeys);
1147 indentPW.increaseIndent();
1148 indentPW.increaseIndent();
1149 for (String key : sortedKeys) {
1150 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1151 indentPW.println(key + " = " +
1152 Arrays.toString((Object[]) configApp.get(key)));
1153 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1154 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1155 } else {
1156 indentPW.println(key + " = " + configApp.get(key));
1157 }
Junda Liu43d723a2015-05-12 17:23:45 -07001158 }
1159 }
1160
Zach Johnson36d7aab2015-05-22 15:43:00 -07001161 private class CarrierServiceConnection implements ServiceConnection {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001162 int phoneId;
1163 int eventId;
1164 IBinder service;
1165
Zach Johnson36d7aab2015-05-22 15:43:00 -07001166 public CarrierServiceConnection(int phoneId, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001167 this.phoneId = phoneId;
1168 this.eventId = eventId;
1169 }
1170
1171 @Override
1172 public void onServiceConnected(ComponentName name, IBinder service) {
1173 log("Connected to config app: " + name.flattenToString());
1174 this.service = service;
1175 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1176 }
1177
1178 @Override
1179 public void onServiceDisconnected(ComponentName name) {
1180 this.service = null;
1181 }
1182 }
1183
1184 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1185 @Override
1186 public void onReceive(Context context, Intent intent) {
1187 String action = intent.getAction();
Junda Liu8a8a53a2015-05-15 16:19:57 -07001188 boolean replace = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1189 // If replace is true, only care ACTION_PACKAGE_REPLACED.
1190 if (replace && !Intent.ACTION_PACKAGE_REPLACED.equals(action))
1191 return;
1192
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001193 switch (action) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001194 case Intent.ACTION_BOOT_COMPLETED:
1195 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1196 break;
1197
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001198 case Intent.ACTION_PACKAGE_ADDED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001199 case Intent.ACTION_PACKAGE_REMOVED:
Junda Liu8a8a53a2015-05-15 16:19:57 -07001200 case Intent.ACTION_PACKAGE_REPLACED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001201 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1202 String packageName = mContext.getPackageManager().getNameForUid(uid);
Junda Liu8a8a53a2015-05-15 16:19:57 -07001203 if (packageName != null) {
1204 // We don't have a phoneId for arg1.
1205 mHandler.sendMessage(
1206 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, packageName));
1207 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001208 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001209 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001210 }
1211 }
1212
chen xudb04c292019-03-26 17:01:15 -07001213 private void log(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001214 Log.d(LOG_TAG, msg);
1215 }
1216
chen xudb04c292019-03-26 17:01:15 -07001217 private void logWithLocalLog(String msg) {
1218 Log.d(LOG_TAG, msg);
1219 mCarrierConfigLoadingLog.log(msg);
1220 }
1221
1222 private void loge(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001223 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07001224 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001225 }
1226}