blob: ab783570c8b08f1b9f44f98e82789c59568de5a7 [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;
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +080059import com.android.internal.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();
chen xudb04c292019-03-26 17:01:15 -0700113 private final LocalLog mCarrierConfigLoadingLog = new LocalLog(50);
114
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]) {
203 updateConfigForPhoneId(i);
204 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700205 }
206 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700207 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700208
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700209 case EVENT_PACKAGE_CHANGED:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700210 {
211 final String carrierPackageName = (String) msg.obj;
212 // Only update if there are cached config removed to avoid updating config for
213 // unrelated packages.
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700214 if (clearCachedConfigForPackage(carrierPackageName)) {
Malcolm Chen5ea18532019-10-24 19:55:44 -0700215 int numPhones = TelephonyManager.from(mContext).getActiveModemCount();
Junda Liu8a8a53a2015-05-15 16:19:57 -0700216 for (int i = 0; i < numPhones; ++i) {
217 updateConfigForPhoneId(i);
218 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700219 }
220 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700221 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700222
Jonathan Basseri65273c82017-07-25 15:08:42 -0700223 case EVENT_DO_FETCH_DEFAULT:
224 {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100225 // Restore persistent override values.
226 PersistableBundle config = restoreConfigFromXml(
227 mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId);
228 if (config != null) {
229 log("Loaded persistent override config from XML. package="
230 + mPlatformCarrierConfigPackage
231 + " phoneId=" + phoneId);
232 mPersistentOverrideConfigs[phoneId] = config;
233 }
234
235 config = restoreConfigFromXml(mPlatformCarrierConfigPackage, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700236 if (config != null) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700237 log(
238 "Loaded config from XML. package="
239 + mPlatformCarrierConfigPackage
240 + " phoneId="
241 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700242 mConfigFromDefaultApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700243 Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700244 newMsg.getData().putBoolean("loaded_from_xml", true);
245 mHandler.sendMessage(newMsg);
246 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700247 // No cached config, so fetch it from the default app.
248 if (bindToConfigPackage(
249 mPlatformCarrierConfigPackage,
250 phoneId,
251 EVENT_CONNECTED_TO_DEFAULT)) {
252 sendMessageDelayed(
253 obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700254 BIND_TIMEOUT_MILLIS);
255 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700256 // Send broadcast if bind fails.
Nathan Harold48ac0972019-03-13 22:33:01 -0700257 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700258 // TODO: We *must* call unbindService even if bindService returns false.
259 // (And possibly if SecurityException was thrown.)
chen xudb04c292019-03-26 17:01:15 -0700260 loge("binding to default app: "
261 + mPlatformCarrierConfigPackage + " fails");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700262 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800263 }
264 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700265 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800266
267 case EVENT_CONNECTED_TO_DEFAULT:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700268 {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700269 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700270 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800271 // If new service connection has been created, unbind.
272 if (mServiceConnection[phoneId] != conn || conn.service == null) {
273 mContext.unbindService(conn);
274 break;
275 }
chen xu02581692018-11-11 19:03:44 -0800276 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700277 // ResultReceiver callback will execute in this Handler's thread.
278 final ResultReceiver resultReceiver =
279 new ResultReceiver(this) {
280 @Override
281 public void onReceiveResult(int resultCode, Bundle resultData) {
282 mContext.unbindService(conn);
283 // If new service connection has been created, this is stale.
284 if (mServiceConnection[phoneId] != conn) {
285 loge("Received response for stale request.");
286 return;
287 }
288 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
289 if (resultCode == RESULT_ERROR || resultData == null) {
290 // On error, abort config fetching.
291 loge("Failed to get carrier config");
Nathan Harold48ac0972019-03-13 22:33:01 -0700292 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700293 return;
294 }
295 PersistableBundle config =
296 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100297 saveConfigToXml(mPlatformCarrierConfigPackage, "", phoneId,
Chen Xucd4a6372019-07-29 16:34:52 -0700298 carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700299 mConfigFromDefaultApp[phoneId] = config;
300 sendMessage(
301 obtainMessage(
302 EVENT_FETCH_DEFAULT_DONE, phoneId, -1));
303 }
304 };
305 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800306 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700307 ICarrierService carrierService =
308 ICarrierService.Stub.asInterface(conn.service);
309 carrierService.getCarrierConfig(carrierId, resultReceiver);
chen xudb04c292019-03-26 17:01:15 -0700310 logWithLocalLog("fetch config for default app: "
311 + mPlatformCarrierConfigPackage
312 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700313 } catch (RemoteException e) {
chen xudb04c292019-03-26 17:01:15 -0700314 loge("Failed to get carrier config from default app: " +
315 mPlatformCarrierConfigPackage + " err: " + e.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700316 mContext.unbindService(conn);
317 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800318 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700319 sendMessageDelayed(
320 obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1),
321 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800322 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700323 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800324
Jonathan Basseri930701e2015-06-11 20:56:43 -0700325 case EVENT_BIND_DEFAULT_TIMEOUT:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700326 case EVENT_FETCH_DEFAULT_TIMEOUT:
327 {
chen xudb04c292019-03-26 17:01:15 -0700328 loge("bind/fetch time out from " + mPlatformCarrierConfigPackage);
chen xuf60b3162019-05-01 21:31:05 -0700329 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
330 // If we attempted to bind to the app, but the service connection is null due to
331 // the race condition that clear config event happens before bind/fetch complete
332 // then config was cleared while we were waiting and we should not continue.
333 if (mServiceConnection[phoneId] != null) {
334 // If a ResponseReceiver callback is in the queue when this happens, we will
335 // unbind twice and throw an exception.
336 mContext.unbindService(mServiceConnection[phoneId]);
337 broadcastConfigChangedIntent(phoneId);
338 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700339 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700340 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700341 }
Jonathan Basseri930701e2015-06-11 20:56:43 -0700342
Jonathan Basseri65273c82017-07-25 15:08:42 -0700343 case EVENT_FETCH_DEFAULT_DONE:
344 {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700345 // If we attempted to bind to the app, but the service connection is null, then
346 // config was cleared while we were waiting and we should not continue.
347 if (!msg.getData().getBoolean("loaded_from_xml", false)
348 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800349 break;
350 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700351 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700352 if (carrierPackageName != null) {
353 log("Found carrier config app: " + carrierPackageName);
Jordan Liu38a614c2019-03-20 15:01:17 -0700354 sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId, -1));
Jonathan Basserib919e932015-05-27 16:53:08 +0000355 } else {
Nathan Harold48ac0972019-03-13 22:33:01 -0700356 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700357 }
358 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700359 }
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700360
Jonathan Basseri65273c82017-07-25 15:08:42 -0700361 case EVENT_DO_FETCH_CARRIER:
362 {
363 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700364 final PersistableBundle config =
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100365 restoreConfigFromXml(carrierPackageName, "", phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700366 if (config != null) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700367 log(
368 "Loaded config from XML. package="
369 + carrierPackageName
370 + " phoneId="
371 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700372 mConfigFromCarrierApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700373 Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700374 newMsg.getData().putBoolean("loaded_from_xml", true);
375 sendMessage(newMsg);
376 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700377 // No cached config, so fetch it from a carrier app.
Jonathan Basseri40473a52015-08-14 14:36:29 -0700378 if (carrierPackageName != null
Jonathan Basseri65273c82017-07-25 15:08:42 -0700379 && bindToConfigPackage(
380 carrierPackageName,
381 phoneId,
382 EVENT_CONNECTED_TO_CARRIER)) {
383 sendMessageDelayed(
384 obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700385 BIND_TIMEOUT_MILLIS);
386 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700387 // Send broadcast if bind fails.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700388 broadcastConfigChangedIntent(phoneId);
chen xudb04c292019-03-26 17:01:15 -0700389 loge("bind to carrier app: " + carrierPackageName + " fails");
Nathan Harold48ac0972019-03-13 22:33:01 -0700390 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700391 }
392 }
393 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700394 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700395
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800396 case EVENT_CONNECTED_TO_CARRIER:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700397 {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700398 removeMessages(EVENT_BIND_CARRIER_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700399 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800400 // If new service connection has been created, unbind.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700401 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800402 mContext.unbindService(conn);
403 break;
404 }
chen xu02581692018-11-11 19:03:44 -0800405 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700406 // ResultReceiver callback will execute in this Handler's thread.
407 final ResultReceiver resultReceiver =
408 new ResultReceiver(this) {
409 @Override
410 public void onReceiveResult(int resultCode, Bundle resultData) {
411 mContext.unbindService(conn);
412 // If new service connection has been created, this is stale.
413 if (mServiceConnection[phoneId] != conn) {
414 loge("Received response for stale request.");
415 return;
416 }
417 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
418 if (resultCode == RESULT_ERROR || resultData == null) {
419 // On error, abort config fetching.
chen xudb04c292019-03-26 17:01:15 -0700420 loge("Failed to get carrier config from carrier app: "
421 + getCarrierPackageForPhoneId(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700422 broadcastConfigChangedIntent(phoneId);
Nathan Harold48ac0972019-03-13 22:33:01 -0700423 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700424 return;
425 }
426 PersistableBundle config =
427 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100428 saveConfigToXml(getCarrierPackageForPhoneId(phoneId), "",
429 phoneId, carrierId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700430 mConfigFromCarrierApp[phoneId] = config;
431 sendMessage(
432 obtainMessage(
433 EVENT_FETCH_CARRIER_DONE, phoneId, -1));
434 }
435 };
436 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800437 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700438 ICarrierService carrierService =
439 ICarrierService.Stub.asInterface(conn.service);
440 carrierService.getCarrierConfig(carrierId, resultReceiver);
chen xudb04c292019-03-26 17:01:15 -0700441 logWithLocalLog("fetch config for carrier app: "
442 + getCarrierPackageForPhoneId(phoneId)
443 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700444 } catch (RemoteException e) {
445 loge("Failed to get carrier config: " + e.toString());
446 mContext.unbindService(conn);
447 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800448 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700449 sendMessageDelayed(
450 obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1),
451 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800452 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700453 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800454
Jonathan Basseri930701e2015-06-11 20:56:43 -0700455 case EVENT_BIND_CARRIER_TIMEOUT:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700456 case EVENT_FETCH_CARRIER_TIMEOUT:
457 {
chen xudb04c292019-03-26 17:01:15 -0700458 loge("bind/fetch from carrier app timeout");
chen xuf60b3162019-05-01 21:31:05 -0700459 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
460 // If we attempted to bind to the app, but the service connection is null due to
461 // the race condition that clear config event happens before bind/fetch complete
462 // then config was cleared while we were waiting and we should not continue.
463 if (mServiceConnection[phoneId] != null) {
464 // If a ResponseReceiver callback is in the queue when this happens, we will
465 // unbind twice and throw an exception.
466 mContext.unbindService(mServiceConnection[phoneId]);
467 broadcastConfigChangedIntent(phoneId);
468 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700469 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700470 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700471 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700472 case EVENT_FETCH_CARRIER_DONE:
473 {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700474 // If we attempted to bind to the app, but the service connection is null, then
475 // config was cleared while we were waiting and we should not continue.
476 if (!msg.getData().getBoolean("loaded_from_xml", false)
477 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800478 break;
479 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700480 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800481 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700482 }
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700483
484 case EVENT_CHECK_SYSTEM_UPDATE:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700485 {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700486 SharedPreferences sharedPrefs =
487 PreferenceManager.getDefaultSharedPreferences(mContext);
488 final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
489 if (!Build.FINGERPRINT.equals(lastFingerprint)) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700490 log(
491 "Build fingerprint changed. old: "
492 + lastFingerprint
493 + " new: "
494 + Build.FINGERPRINT);
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700495 clearCachedConfigForPackage(null);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700496 sharedPrefs
497 .edit()
498 .putString(KEY_FINGERPRINT, Build.FINGERPRINT)
499 .apply();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700500 }
501 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700502 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700503
504 case EVENT_SUBSCRIPTION_INFO_UPDATED:
505 broadcastConfigChangedIntent(phoneId);
506 break;
Malcolm Chen5ea18532019-10-24 19:55:44 -0700507 case EVENT_MULTI_SIM_CONFIG_CHANGED:
508 onMultiSimConfigChanged();
509 break;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800510 }
511 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700512 }
513
514 private final Handler mHandler;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800515
516 /**
517 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
518 * receiver for relevant events.
519 */
520 private CarrierConfigLoader(Context context) {
521 mContext = context;
Meng Wang33ad2bc2017-03-16 20:21:20 -0700522 mPlatformCarrierConfigPackage =
523 mContext.getString(R.string.platform_carrier_config_package);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700524 mHandler = new ConfigHandler();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800525
Nanxi Chen3d670502016-03-17 16:32:09 -0700526 IntentFilter bootFilter = new IntentFilter();
527 bootFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
528 context.registerReceiver(mBootReceiver, bootFilter);
529
Junda Liu8a8a53a2015-05-15 16:19:57 -0700530 // Register for package updates. Update app or uninstall app update will have all 3 intents,
531 // in the order or removed, added, replaced, all with extra_replace set to true.
532 IntentFilter pkgFilter = new IntentFilter();
533 pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
534 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
535 pkgFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
536 pkgFilter.addDataScheme("package");
Jordan Liu5ca24762019-07-10 12:51:09 -0700537 context.registerReceiver(mPackageReceiver, pkgFilter);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800538
Malcolm Chen978dda52019-10-08 18:15:25 -0700539 int numPhones = TelephonyManager.from(context).getSupportedModemCount();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700540 mConfigFromDefaultApp = new PersistableBundle[numPhones];
541 mConfigFromCarrierApp = new PersistableBundle[numPhones];
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100542 mPersistentOverrideConfigs = new PersistableBundle[numPhones];
Hall Liu506724b2018-10-22 18:16:14 -0700543 mOverrideConfigs = new PersistableBundle[numPhones];
Zach Johnson36d7aab2015-05-22 15:43:00 -0700544 mServiceConnection = new CarrierServiceConnection[numPhones];
Junda Liu03aad762017-07-21 13:32:17 -0700545 mHasSentConfigChange = new boolean[numPhones];
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800546 // Make this service available through ServiceManager.
547 ServiceManager.addService(Context.CARRIER_CONFIG_SERVICE, this);
548 log("CarrierConfigLoader has started");
Nathan Harold48ac0972019-03-13 22:33:01 -0700549 mSubscriptionInfoUpdater = PhoneFactory.getSubscriptionInfoUpdater();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700550 mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800551 }
552
553 /**
554 * Initialize the singleton CarrierConfigLoader instance.
555 *
556 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
557 */
558 /* package */
559 static CarrierConfigLoader init(Context context) {
560 synchronized (CarrierConfigLoader.class) {
561 if (sInstance == null) {
562 sInstance = new CarrierConfigLoader(context);
563 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700564 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800565 }
566 return sInstance;
567 }
568 }
569
Malcolm Chen5ea18532019-10-24 19:55:44 -0700570 private void clearConfigForPhone(int phoneId, boolean sendBroadcast) {
571 /* Ignore clear configuration request if device is being shutdown. */
572 Phone phone = PhoneFactory.getPhone(phoneId);
573 if (phone != null) {
574 if (phone.isShuttingDown()) {
575 return;
576 }
577 }
578
579 mConfigFromDefaultApp[phoneId] = null;
580 mConfigFromCarrierApp[phoneId] = null;
581 mServiceConnection[phoneId] = null;
582 mHasSentConfigChange[phoneId] = false;
583
584 if (sendBroadcast) broadcastConfigChangedIntent(phoneId, false);
585 }
586
Nathan Harold48ac0972019-03-13 22:33:01 -0700587 private void notifySubscriptionInfoUpdater(int phoneId) {
588 String configPackagename;
589 PersistableBundle configToSend;
590 int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
591 // Prefer the carrier privileged carrier app, but if there is not one, use the platform
592 // default carrier app.
593 if (mConfigFromCarrierApp[phoneId] != null) {
594 configPackagename = getCarrierPackageForPhoneId(phoneId);
595 configToSend = mConfigFromCarrierApp[phoneId];
596 } else {
597 configPackagename = mPlatformCarrierConfigPackage;
598 configToSend = mConfigFromDefaultApp[phoneId];
599 }
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700600
601 // mOverrideConfigs is for testing. And it will override current configs.
602 PersistableBundle config = mOverrideConfigs[phoneId];
603 if (config != null) {
Torbjorn Eklund1050cb02018-11-16 14:05:38 +0100604 configToSend = new PersistableBundle(configToSend);
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700605 configToSend.putAll(config);
606 }
607
Nathan Harold48ac0972019-03-13 22:33:01 -0700608 mSubscriptionInfoUpdater.updateSubscriptionByCarrierConfigAndNotifyComplete(
609 phoneId, configPackagename, configToSend,
610 mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1));
611 }
612
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800613 private void broadcastConfigChangedIntent(int phoneId) {
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800614 broadcastConfigChangedIntent(phoneId, true);
615 }
616
617 private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800618 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800619 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
chen xuf9db49f2019-03-31 23:04:42 -0700620 Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND |
621 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800622 if (addSubIdExtra) {
623 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
624 int[] subIds = SubscriptionManager.getSubId(phoneId);
625 if (!ArrayUtils.isEmpty(subIds)) {
626 TelephonyManager telMgr = TelephonyManager.from(mContext)
627 .createForSubscriptionId(subIds[0]);
628 simApplicationState = telMgr.getSimApplicationState();
629 }
630 // Include subId/carrier id extra only if SIM records are loaded
631 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
632 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
633 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
634 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID,
635 getSpecificCarrierIdForPhoneId(phoneId));
636 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, getCarrierIdForPhoneId(phoneId));
637 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800638 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800639 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Jack Yuf1661322019-04-06 00:37:23 -0700640 log("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
Jordan Liu5ca24762019-07-10 12:51:09 -0700641 mContext.sendBroadcast(intent);
Junda Liu03aad762017-07-21 13:32:17 -0700642 mHasSentConfigChange[phoneId] = true;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800643 }
644
645 /** Binds to the default or carrier config app. */
646 private boolean bindToConfigPackage(String pkgName, int phoneId, int eventId) {
chen xudb04c292019-03-26 17:01:15 -0700647 logWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700648 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700649 carrierService.setPackage(pkgName);
650 mServiceConnection[phoneId] = new CarrierServiceConnection(phoneId, eventId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800651 try {
Zach Johnson36d7aab2015-05-22 15:43:00 -0700652 return mContext.bindService(carrierService, mServiceConnection[phoneId],
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800653 Context.BIND_AUTO_CREATE);
654 } catch (SecurityException ex) {
655 return false;
656 }
657 }
658
chen xu02581692018-11-11 19:03:44 -0800659 private CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800660 String mcc = "";
661 String mnc = "";
662 String imsi = "";
663 String gid1 = "";
664 String gid2 = "";
665 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
666 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800667 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800668 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700669 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
670 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800671 mcc = simOperator.substring(0, 3);
672 mnc = simOperator.substring(3);
673 }
674 Phone phone = PhoneFactory.getPhone(phoneId);
675 if (phone != null) {
676 imsi = phone.getSubscriberId();
677 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700678 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800679 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800680 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800681 }
chen xua31f22b2019-03-06 15:28:50 -0800682 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800683 }
684
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700685 /** Returns the package name of a priveleged carrier app, or null if there is none. */
686 private String getCarrierPackageForPhoneId(int phoneId) {
687 List<String> carrierPackageNames = TelephonyManager.from(mContext)
688 .getCarrierPackageNamesForIntentAndPhone(
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700689 new Intent(CarrierService.CARRIER_SERVICE_INTERFACE), phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700690 if (carrierPackageNames != null && carrierPackageNames.size() > 0) {
691 return carrierPackageNames.get(0);
692 } else {
693 return null;
694 }
695 }
696
697 private String getIccIdForPhoneId(int phoneId) {
698 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
699 return null;
700 }
701 Phone phone = PhoneFactory.getPhone(phoneId);
702 if (phone == null) {
703 return null;
704 }
705 return phone.getIccSerialNumber();
706 }
707
chen xu02581692018-11-11 19:03:44 -0800708 /**
chen xua31f22b2019-03-06 15:28:50 -0800709 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800710 */
chen xua31f22b2019-03-06 15:28:50 -0800711 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700712 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800713 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700714 }
715 Phone phone = PhoneFactory.getPhone(phoneId);
716 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800717 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700718 }
chen xua31f22b2019-03-06 15:28:50 -0800719 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800720 }
721
722 /**
723 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
724 */
725 private int getCarrierIdForPhoneId(int phoneId) {
726 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
727 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700728 }
chen xu02581692018-11-11 19:03:44 -0800729 Phone phone = PhoneFactory.getPhone(phoneId);
730 if (phone == null) {
731 return TelephonyManager.UNKNOWN_CARRIER_ID;
732 }
733 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700734 }
735
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700736 /**
737 * Writes a bundle to an XML file.
738 *
chen xu02581692018-11-11 19:03:44 -0800739 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -0800740 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -0800741 * should have a single copy of XML file named after carrier id. However, it's still possible
742 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
743 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
744 * carrier while iccid remains the same.
745 *
746 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
747 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700748 *
749 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700750 *
751 * @param packageName the name of the package from which we fetched this bundle.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100752 * @param extraString An extra string to be used in the XML file name.
Yuchen Dongc15afed2018-04-26 17:05:22 +0800753 * @param phoneId the phone ID.
Chen Xucd4a6372019-07-29 16:34:52 -0700754 * @param carrierId contains all carrier-identifying information.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700755 * @param config the bundle to be written. Null will be treated as an empty bundle.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700756 */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100757 private void saveConfigToXml(String packageName, @NonNull String extraString, int phoneId,
758 CarrierIdentifier carrierId, PersistableBundle config) {
Yuchen Dongc15afed2018-04-26 17:05:22 +0800759 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
760 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700761 loge("Skip save config because SIM records are not loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800762 return;
763 }
764
765 final String iccid = getIccIdForPhoneId(phoneId);
Chen Xucd4a6372019-07-29 16:34:52 -0700766 final int cid = carrierId.getSpecificCarrierId();
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700767 if (packageName == null || iccid == null) {
768 loge("Cannot save config with null packageName or iccid.");
769 return;
770 }
Junda Liu02596502016-11-15 13:46:43 -0800771 // b/32668103 Only save to file if config isn't empty.
772 // In case of failure, not caching an empty bundle will
773 // try loading config again on next power on or sim loaded.
774 // Downside is for genuinely empty bundle, will bind and load
775 // on every power on.
776 if (config == null || config.isEmpty()) {
777 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700778 }
779
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700780 final String version = getPackageVersion(packageName);
781 if (version == null) {
782 loge("Failed to get package version for: " + packageName);
783 return;
784 }
785
chen xudb04c292019-03-26 17:01:15 -0700786 logWithLocalLog("save config to xml, packagename: " + packageName + " phoneId: " + phoneId);
787
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700788 FileOutputStream outFile = null;
789 try {
790 outFile = new FileOutputStream(
chen xu02581692018-11-11 19:03:44 -0800791 new File(mContext.getFilesDir(),
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100792 getFilenameForConfig(packageName, extraString, iccid, cid)));
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700793 FastXmlSerializer out = new FastXmlSerializer();
794 out.setOutput(outFile, "utf-8");
795 out.startDocument("utf-8", true);
796 out.startTag(null, TAG_DOCUMENT);
797 out.startTag(null, TAG_VERSION);
798 out.text(version);
799 out.endTag(null, TAG_VERSION);
800 out.startTag(null, TAG_BUNDLE);
801 config.saveToXml(out);
802 out.endTag(null, TAG_BUNDLE);
803 out.endTag(null, TAG_DOCUMENT);
804 out.endDocument();
805 out.flush();
806 outFile.close();
807 }
808 catch (IOException e) {
809 loge(e.toString());
810 }
811 catch (XmlPullParserException e) {
812 loge(e.toString());
813 }
814 }
815
816 /**
817 * Reads a bundle from an XML file.
818 *
819 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +0800820 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700821 *
822 * In case of errors, or if the saved config is from a different package version than the
823 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700824 *
825 * @param packageName the name of the package from which we fetched this bundle.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100826 * @param extraString An extra string to be used in the XML file name.
Yuchen Dongc15afed2018-04-26 17:05:22 +0800827 * @param phoneId the phone ID.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700828 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
829 * version does not match, or reading config fails.
830 */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100831 private PersistableBundle restoreConfigFromXml(String packageName, @NonNull String extraString,
832 int phoneId) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700833 final String version = getPackageVersion(packageName);
834 if (version == null) {
835 loge("Failed to get package version for: " + packageName);
836 return null;
837 }
Yuchen Dongc15afed2018-04-26 17:05:22 +0800838 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
839 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700840 loge("Skip restoring config because SIM records are not yet loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800841 return null;
842 }
843
844 final String iccid = getIccIdForPhoneId(phoneId);
chen xua31f22b2019-03-06 15:28:50 -0800845 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700846 if (packageName == null || iccid == null) {
847 loge("Cannot restore config with null packageName or iccid.");
848 return null;
849 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700850
851 PersistableBundle restoredBundle = null;
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100852 File file = null;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700853 FileInputStream inFile = null;
854 try {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100855 file = new File(mContext.getFilesDir(),
856 getFilenameForConfig(packageName, extraString, iccid, cid));
857 inFile = new FileInputStream(file);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700858 XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
859 parser.setInput(inFile, "utf-8");
860
861 int event;
862 while (((event = parser.next()) != XmlPullParser.END_DOCUMENT)) {
863
864 if (event == XmlPullParser.START_TAG && TAG_VERSION.equals(parser.getName())) {
865 String savedVersion = parser.nextText();
866 if (!version.equals(savedVersion)) {
chen xudb04c292019-03-26 17:01:15 -0700867 loge("Saved version mismatch: " + version + " vs " + savedVersion);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700868 break;
869 }
870 }
871
872 if (event == XmlPullParser.START_TAG && TAG_BUNDLE.equals(parser.getName())) {
873 restoredBundle = PersistableBundle.restoreFromXml(parser);
874 }
875 }
876 inFile.close();
877 }
878 catch (FileNotFoundException e) {
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100879 // Missing file is normal occurrence that might occur with a new sim or when restoring
880 // an override file during boot and should not be treated as an error.
881 if (file != null) log("File not found: " + file.getPath());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700882 }
883 catch (XmlPullParserException e) {
884 loge(e.toString());
885 }
886 catch (IOException e) {
887 loge(e.toString());
888 }
889
890 return restoredBundle;
891 }
892
Junda Liu8a8a53a2015-05-15 16:19:57 -0700893 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700894 * Clears cached carrier config.
895 * This deletes all saved XML files associated with the given package name. If packageName is
896 * null, then it deletes all saved XML files.
897 *
898 * @param packageName the name of a carrier package, or null if all cached config should be
899 * cleared.
900 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -0700901 */
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700902 private boolean clearCachedConfigForPackage(final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700903 File dir = mContext.getFilesDir();
904 File[] packageFiles = dir.listFiles(new FilenameFilter() {
905 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700906 if (packageName != null) {
907 return filename.startsWith("carrierconfig-" + packageName + "-");
908 } else {
909 return filename.startsWith("carrierconfig-");
910 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700911 }
912 });
Junda Liu8a8a53a2015-05-15 16:19:57 -0700913 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700914 for (File f : packageFiles) {
915 log("deleting " + f.getName());
916 f.delete();
917 }
Junda Liu8a8a53a2015-05-15 16:19:57 -0700918 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700919 }
920
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700921 /** Builds a canonical file name for a config file. */
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100922 private String getFilenameForConfig(@NonNull String packageName, @NonNull String extraString,
923 @NonNull String iccid, int cid) {
chen xu02581692018-11-11 19:03:44 -0800924 // the same carrier should have a single copy of XML file named after carrier id.
925 // However, it's still possible that platform doesn't recognize the current sim carrier,
926 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
927 // cases SIM OTA resolves to different carrier while iccid remains the same.
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100928 return "carrierconfig-" + packageName + extraString + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700929 }
930
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700931 /** Return the current version code of a package, or null if the name is not found. */
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700932 private String getPackageVersion(String packageName) {
933 try {
934 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -0800935 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700936 } catch (PackageManager.NameNotFoundException e) {
937 return null;
938 }
939 }
940
Jonathan Basseri65273c82017-07-25 15:08:42 -0700941 /**
942 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700943 *
944 * This reads config bundles for the given phoneId. That means getting the latest bundle from
945 * the default app and a privileged carrier app, if present. This will not bind to an app if we
946 * have a saved config file to use instead.
947 */
948 private void updateConfigForPhoneId(int phoneId) {
Junda Liu8a8a53a2015-05-15 16:19:57 -0700949 // Clear in-memory cache for carrier app config, so when carrier app gets uninstalled, no
950 // stale config is left.
951 if (mConfigFromCarrierApp[phoneId] != null &&
952 getCarrierPackageForPhoneId(phoneId) == null) {
953 mConfigFromCarrierApp[phoneId] = null;
954 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700955 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700956 }
957
Malcolm Chen5ea18532019-10-24 19:55:44 -0700958 private void onMultiSimConfigChanged() {
959 for (int i = TelephonyManager.from(mContext).getActiveModemCount();
960 i < mConfigFromDefaultApp.length; i++) {
961 clearConfigForPhone(i, false);
962 }
963 }
964
Hall Liu506724b2018-10-22 18:16:14 -0700965 @Override
966 public @NonNull PersistableBundle getConfigForSubId(int subId, String callingPackage) {
Malcolm Chen6d3d6b32018-03-01 13:58:03 -0800967 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
968 mContext, subId, callingPackage, "getCarrierConfig")) {
969 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -0700970 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -0700971
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800972 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700973 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800974 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700975 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800976 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700977 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -0800978 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
979 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800980 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800981 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700982 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -0800983 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
984 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +0100985 config = mPersistentOverrideConfigs[phoneId];
986 if (config != null) {
987 retConfig.putAll(config);
988 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
989 }
Hall Liu506724b2018-10-22 18:16:14 -0700990 config = mOverrideConfigs[phoneId];
991 if (config != null) {
992 retConfig.putAll(config);
993 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
994 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800995 }
996 return retConfig;
997 }
998
999 @Override
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001000 public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides,
1001 boolean persistent) {
Hall Liu506724b2018-10-22 18:16:14 -07001002 mContext.enforceCallingOrSelfPermission(
1003 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Brad Ebingerf6281ad2019-04-25 11:02:04 -07001004 //TODO: Also check for SHELL UID to restrict this method to testing only (b/131326259)
Hall Liu506724b2018-10-22 18:16:14 -07001005 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
1006 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1007 log("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
1008 return;
1009 }
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001010 overrideConfig(mOverrideConfigs, phoneId, overrides);
Hall Liu506724b2018-10-22 18:16:14 -07001011
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001012 if (persistent) {
1013 overrideConfig(mPersistentOverrideConfigs, phoneId, overrides);
1014
1015 if (overrides != null) {
1016 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
1017 saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId,
1018 carrierId, mPersistentOverrideConfigs[phoneId]);
1019 } else {
1020 final String iccid = getIccIdForPhoneId(phoneId);
1021 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
1022 String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage,
1023 OVERRIDE_PACKAGE_ADDITION, iccid, cid);
1024 File fileToDelete = new File(mContext.getFilesDir(), fileName);
1025 fileToDelete.delete();
1026 }
Hall Liu506724b2018-10-22 18:16:14 -07001027 }
Brad Ebingerc9d1fa12019-04-17 15:21:18 -07001028 notifySubscriptionInfoUpdater(phoneId);
Hall Liu506724b2018-10-22 18:16:14 -07001029 }
1030
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001031 private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId,
1032 @Nullable PersistableBundle overrides) {
1033 if (overrides == null) {
1034 currentOverrides[phoneId] = new PersistableBundle();
1035 } else if (currentOverrides[phoneId] == null) {
1036 currentOverrides[phoneId] = overrides;
1037 } else {
1038 currentOverrides[phoneId].putAll(overrides);
1039 }
1040 }
1041
Hall Liu506724b2018-10-22 18:16:14 -07001042 @Override
Jonathan Basseri86030352015-06-08 12:27:56 -07001043 public void notifyConfigChangedForSubId(int subId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001044 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001045 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001046 log("Ignore invalid phoneId: " + phoneId + " for subId: " + subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001047 return;
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001048 }
Jordan Liud7d57fe2019-04-16 12:29:43 -07001049
1050 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -07001051 // system privileged app with MODIFY_PHONE_STATE permission.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001052 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext, subId,
1053 "Require carrier privileges or MODIFY_PHONE_STATE permission.");
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001054
1055 // This method should block until deleting has completed, so that an error which prevents us
1056 // from clearing the cache is passed back to the carrier app. With the files successfully
1057 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -07001058 String callingPackageName = mContext.getPackageManager().getNameForUid(
1059 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -07001060 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001061 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001062 }
1063
1064 @Override
1065 public void updateConfigForPhoneId(int phoneId, String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -07001066 mContext.enforceCallingOrSelfPermission(
1067 android.Manifest.permission.MODIFY_PHONE_STATE, null);
chen xuc4810972019-04-17 23:44:43 -07001068 logWithLocalLog("update config for phoneId: " + phoneId + " simState: " + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001069 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
1070 return;
1071 }
1072 // requires Java 7 for switch on string.
1073 switch (simState) {
1074 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1075 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001076 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001077 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Junda Liu9f2d2712015-05-15 14:22:45 -07001078 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001079 break;
1080 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1081 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001082 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001083 break;
1084 }
1085 }
1086
Junda Liu43d723a2015-05-12 17:23:45 -07001087 @Override
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001088 public String getDefaultCarrierServicePackageName() {
1089 return mPlatformCarrierConfigPackage;
1090 }
1091
1092 @Override
Junda Liu43d723a2015-05-12 17:23:45 -07001093 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1094 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1095 != PackageManager.PERMISSION_GRANTED) {
1096 pw.println("Permission Denial: can't dump carrierconfig from from pid="
1097 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1098 return;
1099 }
1100 pw.println("CarrierConfigLoader: " + this);
1101 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
shuoq26a3a4c2016-12-16 11:06:48 -08001102 pw.println("Phone Id = " + i);
1103 // display default values in CarrierConfigManager
1104 printConfig(CarrierConfigManager.getDefaultConfig(), pw,
1105 "Default Values from CarrierConfigManager");
1106 pw.println("");
1107 // display ConfigFromDefaultApp
1108 printConfig(mConfigFromDefaultApp[i], pw, "mConfigFromDefaultApp");
1109 pw.println("");
1110 // display ConfigFromCarrierApp
1111 printConfig(mConfigFromCarrierApp[i], pw, "mConfigFromCarrierApp");
Hall Liu506724b2018-10-22 18:16:14 -07001112 pw.println("");
Torbjorn Eklund723527a2019-02-13 11:16:25 +01001113 printConfig(mPersistentOverrideConfigs[i], pw, "mPersistentOverrideConfigs");
1114 pw.println("");
Hall Liu506724b2018-10-22 18:16:14 -07001115 printConfig(mOverrideConfigs[i], pw, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001116 }
chen xudb04c292019-03-26 17:01:15 -07001117
1118 pw.println("CarrierConfigLoadingLog=");
1119 mCarrierConfigLoadingLog.dump(fd, pw, args);
shuoq26a3a4c2016-12-16 11:06:48 -08001120 }
1121
1122 private void printConfig(PersistableBundle configApp, PrintWriter pw, String name) {
1123 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
1124 if (configApp == null) {
1125 indentPW.increaseIndent();
1126 indentPW.println(name + " : null ");
1127 return;
1128 }
1129 indentPW.increaseIndent();
1130 indentPW.println(name + " : ");
1131 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1132 Collections.sort(sortedKeys);
1133 indentPW.increaseIndent();
1134 indentPW.increaseIndent();
1135 for (String key : sortedKeys) {
1136 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1137 indentPW.println(key + " = " +
1138 Arrays.toString((Object[]) configApp.get(key)));
1139 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1140 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1141 } else {
1142 indentPW.println(key + " = " + configApp.get(key));
1143 }
Junda Liu43d723a2015-05-12 17:23:45 -07001144 }
1145 }
1146
Zach Johnson36d7aab2015-05-22 15:43:00 -07001147 private class CarrierServiceConnection implements ServiceConnection {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001148 int phoneId;
1149 int eventId;
1150 IBinder service;
1151
Zach Johnson36d7aab2015-05-22 15:43:00 -07001152 public CarrierServiceConnection(int phoneId, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001153 this.phoneId = phoneId;
1154 this.eventId = eventId;
1155 }
1156
1157 @Override
1158 public void onServiceConnected(ComponentName name, IBinder service) {
1159 log("Connected to config app: " + name.flattenToString());
1160 this.service = service;
1161 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1162 }
1163
1164 @Override
1165 public void onServiceDisconnected(ComponentName name) {
1166 this.service = null;
1167 }
1168 }
1169
1170 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1171 @Override
1172 public void onReceive(Context context, Intent intent) {
1173 String action = intent.getAction();
Junda Liu8a8a53a2015-05-15 16:19:57 -07001174 boolean replace = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1175 // If replace is true, only care ACTION_PACKAGE_REPLACED.
1176 if (replace && !Intent.ACTION_PACKAGE_REPLACED.equals(action))
1177 return;
1178
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001179 switch (action) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001180 case Intent.ACTION_BOOT_COMPLETED:
1181 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1182 break;
1183
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001184 case Intent.ACTION_PACKAGE_ADDED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001185 case Intent.ACTION_PACKAGE_REMOVED:
Junda Liu8a8a53a2015-05-15 16:19:57 -07001186 case Intent.ACTION_PACKAGE_REPLACED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001187 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1188 String packageName = mContext.getPackageManager().getNameForUid(uid);
Junda Liu8a8a53a2015-05-15 16:19:57 -07001189 if (packageName != null) {
1190 // We don't have a phoneId for arg1.
1191 mHandler.sendMessage(
1192 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, packageName));
1193 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001194 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001195 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001196 }
1197 }
1198
chen xudb04c292019-03-26 17:01:15 -07001199 private void log(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001200 Log.d(LOG_TAG, msg);
1201 }
1202
chen xudb04c292019-03-26 17:01:15 -07001203 private void logWithLocalLog(String msg) {
1204 Log.d(LOG_TAG, msg);
1205 mCarrierConfigLoadingLog.log(msg);
1206 }
1207
1208 private void loge(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001209 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07001210 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001211 }
1212}