blob: 98e5b926cb416638d7ccdd4e078b6ef5df97ac86 [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;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080023import android.content.BroadcastReceiver;
24import android.content.ComponentName;
25import android.content.Context;
26import android.content.Intent;
27import android.content.IntentFilter;
28import android.content.ServiceConnection;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070029import android.content.SharedPreferences;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070030import android.content.pm.PackageInfo;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070031import android.content.pm.PackageManager;
Junda Liu43d723a2015-05-12 17:23:45 -070032import android.os.Binder;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070033import android.os.Build;
Jonathan Basseri65273c82017-07-25 15:08:42 -070034import android.os.Bundle;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080035import android.os.Handler;
36import android.os.IBinder;
37import android.os.Message;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070038import android.os.PersistableBundle;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080039import android.os.RemoteException;
Jonathan Basseri65273c82017-07-25 15:08:42 -070040import android.os.ResultReceiver;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080041import android.os.ServiceManager;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070042import android.preference.PreferenceManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080043import android.service.carrier.CarrierIdentifier;
Zach Johnson36d7aab2015-05-22 15:43:00 -070044import android.service.carrier.CarrierService;
45import android.service.carrier.ICarrierService;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080046import android.telephony.CarrierConfigManager;
47import android.telephony.SubscriptionManager;
48import android.telephony.TelephonyManager;
chen xudb04c292019-03-26 17:01:15 -070049import android.util.LocalLog;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080050import android.util.Log;
51
52import com.android.internal.telephony.ICarrierConfigLoader;
53import com.android.internal.telephony.IccCardConstants;
54import com.android.internal.telephony.Phone;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080055import com.android.internal.telephony.PhoneFactory;
Nathan Harold48ac0972019-03-13 22:33:01 -070056import com.android.internal.telephony.SubscriptionInfoUpdater;
Jeff Davidsona8e4e242018-03-15 17:16:18 -070057import com.android.internal.telephony.TelephonyPermissions;
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +080058import com.android.internal.util.ArrayUtils;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070059import com.android.internal.util.FastXmlSerializer;
shuoq26a3a4c2016-12-16 11:06:48 -080060import com.android.internal.util.IndentingPrintWriter;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080061
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070062import org.xmlpull.v1.XmlPullParser;
63import org.xmlpull.v1.XmlPullParserException;
64import org.xmlpull.v1.XmlPullParserFactory;
65
66import java.io.File;
Junda Liu43d723a2015-05-12 17:23:45 -070067import java.io.FileDescriptor;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070068import java.io.FileInputStream;
69import java.io.FileNotFoundException;
70import java.io.FileOutputStream;
Jonathan Basseri1f743c92015-05-15 00:19:46 -070071import java.io.FilenameFilter;
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070072import java.io.IOException;
Junda Liu43d723a2015-05-12 17:23:45 -070073import java.io.PrintWriter;
shuoq26a3a4c2016-12-16 11:06:48 -080074import java.util.ArrayList;
75import java.util.Arrays;
76import java.util.Collections;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080077import java.util.List;
78
79/**
80 * CarrierConfigLoader binds to privileged carrier apps to fetch carrier config overlays.
Jonathan Basseri6465afd2015-02-25 13:05:57 -080081 */
82
83public class CarrierConfigLoader extends ICarrierConfigLoader.Stub {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -070084 private static final String LOG_TAG = "CarrierConfigLoader";
Meng Wang33ad2bc2017-03-16 20:21:20 -070085
86 // Package name for platform carrier config app, bundled with system image.
87 private final String mPlatformCarrierConfigPackage;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080088
89 /** The singleton instance. */
90 private static CarrierConfigLoader sInstance;
91 // The context for phone app, passed from PhoneGlobals.
92 private Context mContext;
93 // Carrier configs from default app, indexed by phoneID.
Jonathan Basseric31f1f32015-05-12 10:13:03 -070094 private PersistableBundle[] mConfigFromDefaultApp;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080095 // Carrier configs from privileged carrier config app, indexed by phoneID.
Jonathan Basseric31f1f32015-05-12 10:13:03 -070096 private PersistableBundle[] mConfigFromCarrierApp;
Hall Liu506724b2018-10-22 18:16:14 -070097 // Carrier configs that are provided via the override test API, indexed by phone ID.
98 private PersistableBundle[] mOverrideConfigs;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080099 // Service connection for binding to config app.
Zach Johnson36d7aab2015-05-22 15:43:00 -0700100 private CarrierServiceConnection[] mServiceConnection;
Junda Liu03aad762017-07-21 13:32:17 -0700101 // Whether we have sent config change bcast for each phone id.
102 private boolean[] mHasSentConfigChange;
Nathan Harold48ac0972019-03-13 22:33:01 -0700103 // SubscriptionInfoUpdater
104 private final SubscriptionInfoUpdater mSubscriptionInfoUpdater;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800105
Nanxi Chen3d670502016-03-17 16:32:09 -0700106 // Broadcast receiver for Boot intents, register intent filter in construtor.
107 private final BroadcastReceiver mBootReceiver = new ConfigLoaderBroadcastReceiver();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800108 // Broadcast receiver for SIM and pkg intents, register intent filter in constructor.
Nanxi Chen3d670502016-03-17 16:32:09 -0700109 private final BroadcastReceiver mPackageReceiver = new ConfigLoaderBroadcastReceiver();
chen xudb04c292019-03-26 17:01:15 -0700110 private final LocalLog mCarrierConfigLoadingLog = new LocalLog(50);
111
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800112
113 // Message codes; see mHandler below.
114 // Request from SubscriptionInfoUpdater when SIM becomes absent or error.
115 private static final int EVENT_CLEAR_CONFIG = 0;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800116 // Has connected to default app.
117 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
118 // Has connected to carrier app.
119 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700120 // Config has been loaded from default app (or cache).
121 private static final int EVENT_FETCH_DEFAULT_DONE = 5;
122 // Config has been loaded from carrier app (or cache).
123 private static final int EVENT_FETCH_CARRIER_DONE = 6;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700124 // Attempt to fetch from default app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700125 private static final int EVENT_DO_FETCH_DEFAULT = 7;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700126 // Attempt to fetch from carrier app or read from XML.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700127 private static final int EVENT_DO_FETCH_CARRIER = 8;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700128 // A package has been installed, uninstalled, or updated.
129 private static final int EVENT_PACKAGE_CHANGED = 9;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700130 // Bind timed out for the default app.
131 private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
132 // Bind timed out for a carrier app.
133 private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700134 // Check if the system fingerprint has changed.
135 private static final int EVENT_CHECK_SYSTEM_UPDATE = 12;
Nanxi Chen3d670502016-03-17 16:32:09 -0700136 // Rerun carrier config binding after system is unlocked.
137 private static final int EVENT_SYSTEM_UNLOCKED = 13;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700138 // Fetching config timed out from the default app.
139 private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14;
140 // Fetching config timed out from a carrier app.
141 private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15;
Nathan Harold48ac0972019-03-13 22:33:01 -0700142 // SubscriptionInfoUpdater has finished updating the sub for the carrier config.
143 private static final int EVENT_SUBSCRIPTION_INFO_UPDATED = 16;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700144
Junda Liu6cb45002016-03-22 17:18:20 -0700145 private static final int BIND_TIMEOUT_MILLIS = 30000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800146
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700147 // Tags used for saving and restoring XML documents.
148 private static final String TAG_DOCUMENT = "carrier_config";
149 private static final String TAG_VERSION = "package_version";
150 private static final String TAG_BUNDLE = "bundle_data";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800151
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700152 // SharedPreferences key for last known build fingerprint.
153 private static final String KEY_FINGERPRINT = "build_fingerprint";
154
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800155 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700156 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700157 // For each phoneId, the event sequence should be:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700158 // fetch default, connected to default, fetch default (async), fetch default done,
159 // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700160 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700161 // If there is a saved config file for either the default app or the carrier app, we skip
162 // binding to the app and go straight from fetch to loaded.
163 //
164 // At any time, at most one connection is active. If events are not in this order, previous
165 // connection will be unbound, so only latest event takes effect.
166 //
167 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
168 // 1. loading from carrier app (even if read from a file)
169 // 2. loading from default app if there is no carrier app (even if read from a file)
170 // 3. clearing config (e.g. due to sim removal)
171 // 4. encountering bind or IPC error
Jonathan Basseri65273c82017-07-25 15:08:42 -0700172 private class ConfigHandler extends Handler {
173 @Override
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800174 public void handleMessage(Message msg) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700175 final int phoneId = msg.arg1;
chen xuc4810972019-04-17 23:44:43 -0700176 logWithLocalLog("mHandler: " + msg.what + " phoneId: " + phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800177 switch (msg.what) {
178 case EVENT_CLEAR_CONFIG:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700179 {
Sooraj Sasindran52119cd2017-10-23 16:24:28 -0700180 /* Ignore clear configuration request if device is being shutdown. */
181 Phone phone = PhoneFactory.getPhone(phoneId);
182 if (phone != null) {
183 if (phone.isShuttingDown()) {
184 break;
185 }
186 }
187
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800188 mConfigFromDefaultApp[phoneId] = null;
189 mConfigFromCarrierApp[phoneId] = null;
190 mServiceConnection[phoneId] = null;
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800191 broadcastConfigChangedIntent(phoneId, false);
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 {
Nanxi Chen3d670502016-03-17 16:32:09 -0700197 for (int i = 0; i < TelephonyManager.from(mContext).getPhoneCount(); ++i) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700198 // When user unlock device, we should only try to send broadcast again if we
199 // have sent it before unlock. This will avoid we try to load carrier config
200 // when SIM is still loading when unlock happens.
Junda Liu03aad762017-07-21 13:32:17 -0700201 if (mHasSentConfigChange[i]) {
202 updateConfigForPhoneId(i);
203 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700204 }
205 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700206 }
Nanxi Chen3d670502016-03-17 16:32:09 -0700207
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700208 case EVENT_PACKAGE_CHANGED:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700209 {
210 final String carrierPackageName = (String) msg.obj;
211 // Only update if there are cached config removed to avoid updating config for
212 // unrelated packages.
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700213 if (clearCachedConfigForPackage(carrierPackageName)) {
Junda Liu8a8a53a2015-05-15 16:19:57 -0700214 int numPhones = TelephonyManager.from(mContext).getPhoneCount();
215 for (int i = 0; i < numPhones; ++i) {
216 updateConfigForPhoneId(i);
217 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700218 }
219 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700220 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700221
Jonathan Basseri65273c82017-07-25 15:08:42 -0700222 case EVENT_DO_FETCH_DEFAULT:
223 {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700224 final PersistableBundle config =
Yuchen Dongc15afed2018-04-26 17:05:22 +0800225 restoreConfigFromXml(mPlatformCarrierConfigPackage, phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700226 if (config != null) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700227 log(
228 "Loaded config from XML. package="
229 + mPlatformCarrierConfigPackage
230 + " phoneId="
231 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700232 mConfigFromDefaultApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700233 Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700234 newMsg.getData().putBoolean("loaded_from_xml", true);
235 mHandler.sendMessage(newMsg);
236 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700237 // No cached config, so fetch it from the default app.
238 if (bindToConfigPackage(
239 mPlatformCarrierConfigPackage,
240 phoneId,
241 EVENT_CONNECTED_TO_DEFAULT)) {
242 sendMessageDelayed(
243 obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700244 BIND_TIMEOUT_MILLIS);
245 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700246 // Send broadcast if bind fails.
Nathan Harold48ac0972019-03-13 22:33:01 -0700247 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700248 // TODO: We *must* call unbindService even if bindService returns false.
249 // (And possibly if SecurityException was thrown.)
chen xudb04c292019-03-26 17:01:15 -0700250 loge("binding to default app: "
251 + mPlatformCarrierConfigPackage + " fails");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700252 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800253 }
254 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700255 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800256
257 case EVENT_CONNECTED_TO_DEFAULT:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700258 {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700259 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700260 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800261 // If new service connection has been created, unbind.
262 if (mServiceConnection[phoneId] != conn || conn.service == null) {
263 mContext.unbindService(conn);
264 break;
265 }
chen xu02581692018-11-11 19:03:44 -0800266 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700267 // ResultReceiver callback will execute in this Handler's thread.
268 final ResultReceiver resultReceiver =
269 new ResultReceiver(this) {
270 @Override
271 public void onReceiveResult(int resultCode, Bundle resultData) {
272 mContext.unbindService(conn);
273 // If new service connection has been created, this is stale.
274 if (mServiceConnection[phoneId] != conn) {
275 loge("Received response for stale request.");
276 return;
277 }
278 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
279 if (resultCode == RESULT_ERROR || resultData == null) {
280 // On error, abort config fetching.
281 loge("Failed to get carrier config");
Nathan Harold48ac0972019-03-13 22:33:01 -0700282 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700283 return;
284 }
285 PersistableBundle config =
286 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Yuchen Dongc15afed2018-04-26 17:05:22 +0800287 saveConfigToXml(
288 mPlatformCarrierConfigPackage, phoneId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700289 mConfigFromDefaultApp[phoneId] = config;
290 sendMessage(
291 obtainMessage(
292 EVENT_FETCH_DEFAULT_DONE, phoneId, -1));
293 }
294 };
295 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800296 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700297 ICarrierService carrierService =
298 ICarrierService.Stub.asInterface(conn.service);
299 carrierService.getCarrierConfig(carrierId, resultReceiver);
chen xudb04c292019-03-26 17:01:15 -0700300 logWithLocalLog("fetch config for default app: "
301 + mPlatformCarrierConfigPackage
302 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700303 } catch (RemoteException e) {
chen xudb04c292019-03-26 17:01:15 -0700304 loge("Failed to get carrier config from default app: " +
305 mPlatformCarrierConfigPackage + " err: " + e.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700306 mContext.unbindService(conn);
307 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800308 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700309 sendMessageDelayed(
310 obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1),
311 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800312 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700313 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800314
Jonathan Basseri930701e2015-06-11 20:56:43 -0700315 case EVENT_BIND_DEFAULT_TIMEOUT:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700316 case EVENT_FETCH_DEFAULT_TIMEOUT:
317 {
chen xudb04c292019-03-26 17:01:15 -0700318 loge("bind/fetch time out from " + mPlatformCarrierConfigPackage);
chen xuf60b3162019-05-01 21:31:05 -0700319 removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT);
320 // If we attempted to bind to the app, but the service connection is null due to
321 // the race condition that clear config event happens before bind/fetch complete
322 // then config was cleared while we were waiting and we should not continue.
323 if (mServiceConnection[phoneId] != null) {
324 // If a ResponseReceiver callback is in the queue when this happens, we will
325 // unbind twice and throw an exception.
326 mContext.unbindService(mServiceConnection[phoneId]);
327 broadcastConfigChangedIntent(phoneId);
328 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700329 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700330 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700331 }
Jonathan Basseri930701e2015-06-11 20:56:43 -0700332
Jonathan Basseri65273c82017-07-25 15:08:42 -0700333 case EVENT_FETCH_DEFAULT_DONE:
334 {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700335 // If we attempted to bind to the app, but the service connection is null, then
336 // config was cleared while we were waiting and we should not continue.
337 if (!msg.getData().getBoolean("loaded_from_xml", false)
338 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800339 break;
340 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700341 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700342 if (carrierPackageName != null) {
343 log("Found carrier config app: " + carrierPackageName);
Jordan Liu38a614c2019-03-20 15:01:17 -0700344 sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId, -1));
Jonathan Basserib919e932015-05-27 16:53:08 +0000345 } else {
Nathan Harold48ac0972019-03-13 22:33:01 -0700346 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700347 }
348 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700349 }
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700350
Jonathan Basseri65273c82017-07-25 15:08:42 -0700351 case EVENT_DO_FETCH_CARRIER:
352 {
353 final String carrierPackageName = getCarrierPackageForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700354 final PersistableBundle config =
Yuchen Dongc15afed2018-04-26 17:05:22 +0800355 restoreConfigFromXml(carrierPackageName, phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700356 if (config != null) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700357 log(
358 "Loaded config from XML. package="
359 + carrierPackageName
360 + " phoneId="
361 + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700362 mConfigFromCarrierApp[phoneId] = config;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700363 Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700364 newMsg.getData().putBoolean("loaded_from_xml", true);
365 sendMessage(newMsg);
366 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700367 // No cached config, so fetch it from a carrier app.
Jonathan Basseri40473a52015-08-14 14:36:29 -0700368 if (carrierPackageName != null
Jonathan Basseri65273c82017-07-25 15:08:42 -0700369 && bindToConfigPackage(
370 carrierPackageName,
371 phoneId,
372 EVENT_CONNECTED_TO_CARRIER)) {
373 sendMessageDelayed(
374 obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1),
Jonathan Basseri930701e2015-06-11 20:56:43 -0700375 BIND_TIMEOUT_MILLIS);
376 } else {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700377 // Send broadcast if bind fails.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700378 broadcastConfigChangedIntent(phoneId);
chen xudb04c292019-03-26 17:01:15 -0700379 loge("bind to carrier app: " + carrierPackageName + " fails");
Nathan Harold48ac0972019-03-13 22:33:01 -0700380 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700381 }
382 }
383 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700384 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700385
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800386 case EVENT_CONNECTED_TO_CARRIER:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700387 {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700388 removeMessages(EVENT_BIND_CARRIER_TIMEOUT);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700389 final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800390 // If new service connection has been created, unbind.
Jonathan Basseri65273c82017-07-25 15:08:42 -0700391 if (mServiceConnection[phoneId] != conn || conn.service == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800392 mContext.unbindService(conn);
393 break;
394 }
chen xu02581692018-11-11 19:03:44 -0800395 final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700396 // ResultReceiver callback will execute in this Handler's thread.
397 final ResultReceiver resultReceiver =
398 new ResultReceiver(this) {
399 @Override
400 public void onReceiveResult(int resultCode, Bundle resultData) {
401 mContext.unbindService(conn);
402 // If new service connection has been created, this is stale.
403 if (mServiceConnection[phoneId] != conn) {
404 loge("Received response for stale request.");
405 return;
406 }
407 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
408 if (resultCode == RESULT_ERROR || resultData == null) {
409 // On error, abort config fetching.
chen xudb04c292019-03-26 17:01:15 -0700410 loge("Failed to get carrier config from carrier app: "
411 + getCarrierPackageForPhoneId(phoneId));
Jonathan Basseri65273c82017-07-25 15:08:42 -0700412 broadcastConfigChangedIntent(phoneId);
Nathan Harold48ac0972019-03-13 22:33:01 -0700413 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700414 return;
415 }
416 PersistableBundle config =
417 resultData.getParcelable(KEY_CONFIG_BUNDLE);
Yuchen Dongc15afed2018-04-26 17:05:22 +0800418 saveConfigToXml(
419 getCarrierPackageForPhoneId(phoneId), phoneId, config);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700420 mConfigFromCarrierApp[phoneId] = config;
421 sendMessage(
422 obtainMessage(
423 EVENT_FETCH_CARRIER_DONE, phoneId, -1));
424 }
425 };
426 // Now fetch the config asynchronously from the ICarrierService.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800427 try {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700428 ICarrierService carrierService =
429 ICarrierService.Stub.asInterface(conn.service);
430 carrierService.getCarrierConfig(carrierId, resultReceiver);
chen xudb04c292019-03-26 17:01:15 -0700431 logWithLocalLog("fetch config for carrier app: "
432 + getCarrierPackageForPhoneId(phoneId)
433 + " carrierid: " + carrierId.toString());
Jonathan Basseri65273c82017-07-25 15:08:42 -0700434 } catch (RemoteException e) {
435 loge("Failed to get carrier config: " + e.toString());
436 mContext.unbindService(conn);
437 break; // So we don't set a timeout.
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800438 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700439 sendMessageDelayed(
440 obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1),
441 BIND_TIMEOUT_MILLIS);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800442 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700443 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800444
Jonathan Basseri930701e2015-06-11 20:56:43 -0700445 case EVENT_BIND_CARRIER_TIMEOUT:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700446 case EVENT_FETCH_CARRIER_TIMEOUT:
447 {
chen xudb04c292019-03-26 17:01:15 -0700448 loge("bind/fetch from carrier app timeout");
chen xuf60b3162019-05-01 21:31:05 -0700449 removeMessages(EVENT_FETCH_CARRIER_TIMEOUT);
450 // If we attempted to bind to the app, but the service connection is null due to
451 // the race condition that clear config event happens before bind/fetch complete
452 // then config was cleared while we were waiting and we should not continue.
453 if (mServiceConnection[phoneId] != null) {
454 // If a ResponseReceiver callback is in the queue when this happens, we will
455 // unbind twice and throw an exception.
456 mContext.unbindService(mServiceConnection[phoneId]);
457 broadcastConfigChangedIntent(phoneId);
458 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700459 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri930701e2015-06-11 20:56:43 -0700460 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700461 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700462 case EVENT_FETCH_CARRIER_DONE:
463 {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700464 // If we attempted to bind to the app, but the service connection is null, then
465 // config was cleared while we were waiting and we should not continue.
466 if (!msg.getData().getBoolean("loaded_from_xml", false)
467 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800468 break;
469 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700470 notifySubscriptionInfoUpdater(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800471 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700472 }
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700473
474 case EVENT_CHECK_SYSTEM_UPDATE:
Jonathan Basseri65273c82017-07-25 15:08:42 -0700475 {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700476 SharedPreferences sharedPrefs =
477 PreferenceManager.getDefaultSharedPreferences(mContext);
478 final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
479 if (!Build.FINGERPRINT.equals(lastFingerprint)) {
Jonathan Basseri65273c82017-07-25 15:08:42 -0700480 log(
481 "Build fingerprint changed. old: "
482 + lastFingerprint
483 + " new: "
484 + Build.FINGERPRINT);
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700485 clearCachedConfigForPackage(null);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700486 sharedPrefs
487 .edit()
488 .putString(KEY_FINGERPRINT, Build.FINGERPRINT)
489 .apply();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700490 }
491 break;
Jonathan Basseri65273c82017-07-25 15:08:42 -0700492 }
Nathan Harold48ac0972019-03-13 22:33:01 -0700493
494 case EVENT_SUBSCRIPTION_INFO_UPDATED:
495 broadcastConfigChangedIntent(phoneId);
496 break;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800497 }
498 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700499 }
500
501 private final Handler mHandler;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800502
503 /**
504 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
505 * receiver for relevant events.
506 */
507 private CarrierConfigLoader(Context context) {
508 mContext = context;
Meng Wang33ad2bc2017-03-16 20:21:20 -0700509 mPlatformCarrierConfigPackage =
510 mContext.getString(R.string.platform_carrier_config_package);
Jonathan Basseri65273c82017-07-25 15:08:42 -0700511 mHandler = new ConfigHandler();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800512
Nanxi Chen3d670502016-03-17 16:32:09 -0700513 IntentFilter bootFilter = new IntentFilter();
514 bootFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
515 context.registerReceiver(mBootReceiver, bootFilter);
516
Junda Liu8a8a53a2015-05-15 16:19:57 -0700517 // Register for package updates. Update app or uninstall app update will have all 3 intents,
518 // in the order or removed, added, replaced, all with extra_replace set to true.
519 IntentFilter pkgFilter = new IntentFilter();
520 pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
521 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
522 pkgFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
523 pkgFilter.addDataScheme("package");
Jordan Liu5ca24762019-07-10 12:51:09 -0700524 context.registerReceiver(mPackageReceiver, pkgFilter);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800525
526 int numPhones = TelephonyManager.from(context).getPhoneCount();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700527 mConfigFromDefaultApp = new PersistableBundle[numPhones];
528 mConfigFromCarrierApp = new PersistableBundle[numPhones];
Hall Liu506724b2018-10-22 18:16:14 -0700529 mOverrideConfigs = new PersistableBundle[numPhones];
Zach Johnson36d7aab2015-05-22 15:43:00 -0700530 mServiceConnection = new CarrierServiceConnection[numPhones];
Junda Liu03aad762017-07-21 13:32:17 -0700531 mHasSentConfigChange = new boolean[numPhones];
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800532 // Make this service available through ServiceManager.
533 ServiceManager.addService(Context.CARRIER_CONFIG_SERVICE, this);
534 log("CarrierConfigLoader has started");
Nathan Harold48ac0972019-03-13 22:33:01 -0700535 mSubscriptionInfoUpdater = PhoneFactory.getSubscriptionInfoUpdater();
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700536 mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800537 }
538
539 /**
540 * Initialize the singleton CarrierConfigLoader instance.
541 *
542 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
543 */
544 /* package */
545 static CarrierConfigLoader init(Context context) {
546 synchronized (CarrierConfigLoader.class) {
547 if (sInstance == null) {
548 sInstance = new CarrierConfigLoader(context);
549 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700550 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800551 }
552 return sInstance;
553 }
554 }
555
Nathan Harold48ac0972019-03-13 22:33:01 -0700556 private void notifySubscriptionInfoUpdater(int phoneId) {
557 String configPackagename;
558 PersistableBundle configToSend;
559 int carrierId = getSpecificCarrierIdForPhoneId(phoneId);
560 // Prefer the carrier privileged carrier app, but if there is not one, use the platform
561 // default carrier app.
562 if (mConfigFromCarrierApp[phoneId] != null) {
563 configPackagename = getCarrierPackageForPhoneId(phoneId);
564 configToSend = mConfigFromCarrierApp[phoneId];
565 } else {
566 configPackagename = mPlatformCarrierConfigPackage;
567 configToSend = mConfigFromDefaultApp[phoneId];
568 }
Nazanin Bakhshi6fcc3342019-09-18 17:54:01 -0700569
570 // mOverrideConfigs is for testing. And it will override current configs.
571 PersistableBundle config = mOverrideConfigs[phoneId];
572 if (config != null) {
573 configToSend.putAll(config);
574 }
575
Nathan Harold48ac0972019-03-13 22:33:01 -0700576 mSubscriptionInfoUpdater.updateSubscriptionByCarrierConfigAndNotifyComplete(
577 phoneId, configPackagename, configToSend,
578 mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1));
579 }
580
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800581 private void broadcastConfigChangedIntent(int phoneId) {
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800582 broadcastConfigChangedIntent(phoneId, true);
583 }
584
585 private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800586 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800587 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
chen xuf9db49f2019-03-31 23:04:42 -0700588 Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND |
589 Intent.FLAG_RECEIVER_FOREGROUND);
Qiongcheng Luoe7de61b2018-08-06 10:20:09 +0800590 if (addSubIdExtra) {
591 int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN;
592 int[] subIds = SubscriptionManager.getSubId(phoneId);
593 if (!ArrayUtils.isEmpty(subIds)) {
594 TelephonyManager telMgr = TelephonyManager.from(mContext)
595 .createForSubscriptionId(subIds[0]);
596 simApplicationState = telMgr.getSimApplicationState();
597 }
598 // Include subId/carrier id extra only if SIM records are loaded
599 if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN
600 && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) {
601 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
602 intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID,
603 getSpecificCarrierIdForPhoneId(phoneId));
604 intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, getCarrierIdForPhoneId(phoneId));
605 }
Amit Mahajanf8088ab2018-03-02 15:24:07 -0800606 }
Amit Mahajanb33bcda2018-01-24 12:56:44 -0800607 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId);
Jack Yuf1661322019-04-06 00:37:23 -0700608 log("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId);
Jordan Liu5ca24762019-07-10 12:51:09 -0700609 mContext.sendBroadcast(intent);
Junda Liu03aad762017-07-21 13:32:17 -0700610 mHasSentConfigChange[phoneId] = true;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800611 }
612
613 /** Binds to the default or carrier config app. */
614 private boolean bindToConfigPackage(String pkgName, int phoneId, int eventId) {
chen xudb04c292019-03-26 17:01:15 -0700615 logWithLocalLog("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700616 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700617 carrierService.setPackage(pkgName);
618 mServiceConnection[phoneId] = new CarrierServiceConnection(phoneId, eventId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800619 try {
Zach Johnson36d7aab2015-05-22 15:43:00 -0700620 return mContext.bindService(carrierService, mServiceConnection[phoneId],
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800621 Context.BIND_AUTO_CREATE);
622 } catch (SecurityException ex) {
623 return false;
624 }
625 }
626
chen xu02581692018-11-11 19:03:44 -0800627 private CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800628 String mcc = "";
629 String mnc = "";
630 String imsi = "";
631 String gid1 = "";
632 String gid2 = "";
633 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
634 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
chen xu02581692018-11-11 19:03:44 -0800635 int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
chen xua31f22b2019-03-06 15:28:50 -0800636 int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID;
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700637 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
638 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800639 mcc = simOperator.substring(0, 3);
640 mnc = simOperator.substring(3);
641 }
642 Phone phone = PhoneFactory.getPhone(phoneId);
643 if (phone != null) {
644 imsi = phone.getSubscriberId();
645 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700646 gid2 = phone.getGroupIdLevel2();
chen xu02581692018-11-11 19:03:44 -0800647 carrierId = phone.getCarrierId();
chen xua31f22b2019-03-06 15:28:50 -0800648 specificCarrierId = phone.getSpecificCarrierId();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800649 }
chen xua31f22b2019-03-06 15:28:50 -0800650 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800651 }
652
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700653 /** Returns the package name of a priveleged carrier app, or null if there is none. */
654 private String getCarrierPackageForPhoneId(int phoneId) {
655 List<String> carrierPackageNames = TelephonyManager.from(mContext)
656 .getCarrierPackageNamesForIntentAndPhone(
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700657 new Intent(CarrierService.CARRIER_SERVICE_INTERFACE), phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700658 if (carrierPackageNames != null && carrierPackageNames.size() > 0) {
659 return carrierPackageNames.get(0);
660 } else {
661 return null;
662 }
663 }
664
665 private String getIccIdForPhoneId(int phoneId) {
666 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
667 return null;
668 }
669 Phone phone = PhoneFactory.getPhone(phoneId);
670 if (phone == null) {
671 return null;
672 }
673 return phone.getIccSerialNumber();
674 }
675
chen xu02581692018-11-11 19:03:44 -0800676 /**
chen xua31f22b2019-03-06 15:28:50 -0800677 * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}
chen xu02581692018-11-11 19:03:44 -0800678 */
chen xua31f22b2019-03-06 15:28:50 -0800679 private int getSpecificCarrierIdForPhoneId(int phoneId) {
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700680 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
chen xu02581692018-11-11 19:03:44 -0800681 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700682 }
683 Phone phone = PhoneFactory.getPhone(phoneId);
684 if (phone == null) {
chen xu02581692018-11-11 19:03:44 -0800685 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700686 }
chen xua31f22b2019-03-06 15:28:50 -0800687 return phone.getSpecificCarrierId();
chen xu02581692018-11-11 19:03:44 -0800688 }
689
690 /**
691 * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() }
692 */
693 private int getCarrierIdForPhoneId(int phoneId) {
694 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
695 return TelephonyManager.UNKNOWN_CARRIER_ID;
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700696 }
chen xu02581692018-11-11 19:03:44 -0800697 Phone phone = PhoneFactory.getPhone(phoneId);
698 if (phone == null) {
699 return TelephonyManager.UNKNOWN_CARRIER_ID;
700 }
701 return phone.getCarrierId();
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700702 }
703
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700704 /**
705 * Writes a bundle to an XML file.
706 *
chen xu02581692018-11-11 19:03:44 -0800707 * The bundle will be written to a file named after the package name, ICCID and
chen xua31f22b2019-03-06 15:28:50 -0800708 * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier
chen xu02581692018-11-11 19:03:44 -0800709 * should have a single copy of XML file named after carrier id. However, it's still possible
710 * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as
711 * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different
712 * carrier while iccid remains the same.
713 *
714 * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will
715 * include the bundle and the current version of the specified package.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700716 *
717 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700718 *
719 * @param packageName the name of the package from which we fetched this bundle.
Yuchen Dongc15afed2018-04-26 17:05:22 +0800720 * @param phoneId the phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700721 * @param config the bundle to be written. Null will be treated as an empty bundle.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700722 */
Yuchen Dongc15afed2018-04-26 17:05:22 +0800723 private void saveConfigToXml(String packageName, int phoneId, PersistableBundle config) {
724 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
725 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700726 loge("Skip save config because SIM records are not loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800727 return;
728 }
729
730 final String iccid = getIccIdForPhoneId(phoneId);
chen xua31f22b2019-03-06 15:28:50 -0800731 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700732 if (packageName == null || iccid == null) {
733 loge("Cannot save config with null packageName or iccid.");
734 return;
735 }
Junda Liu02596502016-11-15 13:46:43 -0800736 // b/32668103 Only save to file if config isn't empty.
737 // In case of failure, not caching an empty bundle will
738 // try loading config again on next power on or sim loaded.
739 // Downside is for genuinely empty bundle, will bind and load
740 // on every power on.
741 if (config == null || config.isEmpty()) {
742 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700743 }
744
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700745 final String version = getPackageVersion(packageName);
746 if (version == null) {
747 loge("Failed to get package version for: " + packageName);
748 return;
749 }
750
chen xudb04c292019-03-26 17:01:15 -0700751 logWithLocalLog("save config to xml, packagename: " + packageName + " phoneId: " + phoneId);
752
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700753 FileOutputStream outFile = null;
754 try {
755 outFile = new FileOutputStream(
chen xu02581692018-11-11 19:03:44 -0800756 new File(mContext.getFilesDir(),
757 getFilenameForConfig(packageName, iccid, cid)));
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700758 FastXmlSerializer out = new FastXmlSerializer();
759 out.setOutput(outFile, "utf-8");
760 out.startDocument("utf-8", true);
761 out.startTag(null, TAG_DOCUMENT);
762 out.startTag(null, TAG_VERSION);
763 out.text(version);
764 out.endTag(null, TAG_VERSION);
765 out.startTag(null, TAG_BUNDLE);
766 config.saveToXml(out);
767 out.endTag(null, TAG_BUNDLE);
768 out.endTag(null, TAG_DOCUMENT);
769 out.endDocument();
770 out.flush();
771 outFile.close();
772 }
773 catch (IOException e) {
774 loge(e.toString());
775 }
776 catch (XmlPullParserException e) {
777 loge(e.toString());
778 }
779 }
780
781 /**
782 * Reads a bundle from an XML file.
783 *
784 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Yuchen Dongc15afed2018-04-26 17:05:22 +0800785 * config bundle for the given package and phone ID.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700786 *
787 * In case of errors, or if the saved config is from a different package version than the
788 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700789 *
790 * @param packageName the name of the package from which we fetched this bundle.
Yuchen Dongc15afed2018-04-26 17:05:22 +0800791 * @param phoneId the phone ID.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700792 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
793 * version does not match, or reading config fails.
794 */
Yuchen Dongc15afed2018-04-26 17:05:22 +0800795 private PersistableBundle restoreConfigFromXml(String packageName, int phoneId) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700796 final String version = getPackageVersion(packageName);
797 if (version == null) {
798 loge("Failed to get package version for: " + packageName);
799 return null;
800 }
Yuchen Dongc15afed2018-04-26 17:05:22 +0800801 if (SubscriptionManager.getSimStateForSlotIndex(phoneId)
802 != TelephonyManager.SIM_STATE_LOADED) {
chen xudb04c292019-03-26 17:01:15 -0700803 loge("Skip restoring config because SIM records are not yet loaded.");
Yuchen Dongc15afed2018-04-26 17:05:22 +0800804 return null;
805 }
806
807 final String iccid = getIccIdForPhoneId(phoneId);
chen xua31f22b2019-03-06 15:28:50 -0800808 final int cid = getSpecificCarrierIdForPhoneId(phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700809 if (packageName == null || iccid == null) {
810 loge("Cannot restore config with null packageName or iccid.");
811 return null;
812 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700813
814 PersistableBundle restoredBundle = null;
815 FileInputStream inFile = null;
816 try {
817 inFile = new FileInputStream(
chen xu02581692018-11-11 19:03:44 -0800818 new File(mContext.getFilesDir(),
819 getFilenameForConfig(packageName, iccid, cid)));
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700820 XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
821 parser.setInput(inFile, "utf-8");
822
823 int event;
824 while (((event = parser.next()) != XmlPullParser.END_DOCUMENT)) {
825
826 if (event == XmlPullParser.START_TAG && TAG_VERSION.equals(parser.getName())) {
827 String savedVersion = parser.nextText();
828 if (!version.equals(savedVersion)) {
chen xudb04c292019-03-26 17:01:15 -0700829 loge("Saved version mismatch: " + version + " vs " + savedVersion);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700830 break;
831 }
832 }
833
834 if (event == XmlPullParser.START_TAG && TAG_BUNDLE.equals(parser.getName())) {
835 restoredBundle = PersistableBundle.restoreFromXml(parser);
836 }
837 }
838 inFile.close();
839 }
840 catch (FileNotFoundException e) {
841 loge(e.toString());
842 }
843 catch (XmlPullParserException e) {
844 loge(e.toString());
845 }
846 catch (IOException e) {
847 loge(e.toString());
848 }
849
850 return restoredBundle;
851 }
852
Junda Liu8a8a53a2015-05-15 16:19:57 -0700853 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700854 * Clears cached carrier config.
855 * This deletes all saved XML files associated with the given package name. If packageName is
856 * null, then it deletes all saved XML files.
857 *
858 * @param packageName the name of a carrier package, or null if all cached config should be
859 * cleared.
860 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -0700861 */
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700862 private boolean clearCachedConfigForPackage(final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700863 File dir = mContext.getFilesDir();
864 File[] packageFiles = dir.listFiles(new FilenameFilter() {
865 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700866 if (packageName != null) {
867 return filename.startsWith("carrierconfig-" + packageName + "-");
868 } else {
869 return filename.startsWith("carrierconfig-");
870 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700871 }
872 });
Junda Liu8a8a53a2015-05-15 16:19:57 -0700873 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700874 for (File f : packageFiles) {
875 log("deleting " + f.getName());
876 f.delete();
877 }
Junda Liu8a8a53a2015-05-15 16:19:57 -0700878 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700879 }
880
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700881 /** Builds a canonical file name for a config file. */
Naina Nalluri86fc7b02018-08-03 10:38:30 -0700882 private String getFilenameForConfig(@NonNull String packageName, @NonNull String iccid,
chen xu02581692018-11-11 19:03:44 -0800883 int cid) {
884 // the same carrier should have a single copy of XML file named after carrier id.
885 // However, it's still possible that platform doesn't recognize the current sim carrier,
886 // we will use iccid + carrierid as the canonical file name. carrierid can also handle the
887 // cases SIM OTA resolves to different carrier while iccid remains the same.
888 return "carrierconfig-" + packageName + "-" + iccid + "-" + cid + ".xml";
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700889 }
890
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700891 /** Return the current version code of a package, or null if the name is not found. */
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700892 private String getPackageVersion(String packageName) {
893 try {
894 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Dianne Hackbornb76ab202017-11-28 17:44:50 -0800895 return Long.toString(info.getLongVersionCode());
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700896 } catch (PackageManager.NameNotFoundException e) {
897 return null;
898 }
899 }
900
Jonathan Basseri65273c82017-07-25 15:08:42 -0700901 /**
902 * Read up to date config.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700903 *
904 * This reads config bundles for the given phoneId. That means getting the latest bundle from
905 * the default app and a privileged carrier app, if present. This will not bind to an app if we
906 * have a saved config file to use instead.
907 */
908 private void updateConfigForPhoneId(int phoneId) {
Junda Liu8a8a53a2015-05-15 16:19:57 -0700909 // Clear in-memory cache for carrier app config, so when carrier app gets uninstalled, no
910 // stale config is left.
911 if (mConfigFromCarrierApp[phoneId] != null &&
912 getCarrierPackageForPhoneId(phoneId) == null) {
913 mConfigFromCarrierApp[phoneId] = null;
914 }
Jonathan Basseri65273c82017-07-25 15:08:42 -0700915 mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1));
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700916 }
917
Hall Liu506724b2018-10-22 18:16:14 -0700918 @Override
919 public @NonNull PersistableBundle getConfigForSubId(int subId, String callingPackage) {
Malcolm Chen6d3d6b32018-03-01 13:58:03 -0800920 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
921 mContext, subId, callingPackage, "getCarrierConfig")) {
922 return new PersistableBundle();
Amit Mahajan40b3fa52015-07-14 10:27:19 -0700923 }
Jeff Davidsona8e4e242018-03-15 17:16:18 -0700924
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800925 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700926 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800927 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700928 PersistableBundle config = mConfigFromDefaultApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800929 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700930 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -0800931 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
932 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800933 config = mConfigFromCarrierApp[phoneId];
yinxuc5e27622017-11-29 15:08:50 -0800934 if (config != null) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700935 retConfig.putAll(config);
yinxuc5e27622017-11-29 15:08:50 -0800936 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
937 }
Hall Liu506724b2018-10-22 18:16:14 -0700938 config = mOverrideConfigs[phoneId];
939 if (config != null) {
940 retConfig.putAll(config);
941 retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true);
942 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800943 }
944 return retConfig;
945 }
946
947 @Override
Hall Liu506724b2018-10-22 18:16:14 -0700948 public void overrideConfig(int subscriptionId, PersistableBundle overrides) {
949 mContext.enforceCallingOrSelfPermission(
950 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Brad Ebingerf6281ad2019-04-25 11:02:04 -0700951 //TODO: Also check for SHELL UID to restrict this method to testing only (b/131326259)
Hall Liu506724b2018-10-22 18:16:14 -0700952 int phoneId = SubscriptionManager.getPhoneId(subscriptionId);
953 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
954 log("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId);
955 return;
956 }
957
958 if (overrides == null) {
959 mOverrideConfigs[phoneId] = new PersistableBundle();
Brad Ebinger725edf62019-05-15 18:24:13 -0700960 } else if (mOverrideConfigs[phoneId] == null) {
Hall Liu506724b2018-10-22 18:16:14 -0700961 mOverrideConfigs[phoneId] = overrides;
962 } else {
963 mOverrideConfigs[phoneId].putAll(overrides);
964 }
Brad Ebingerc9d1fa12019-04-17 15:21:18 -0700965
966 notifySubscriptionInfoUpdater(phoneId);
Hall Liu506724b2018-10-22 18:16:14 -0700967 }
968
969 @Override
Jonathan Basseri86030352015-06-08 12:27:56 -0700970 public void notifyConfigChangedForSubId(int subId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800971 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700972 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800973 log("Ignore invalid phoneId: " + phoneId + " for subId: " + subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700974 return;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800975 }
Jordan Liud7d57fe2019-04-16 12:29:43 -0700976
977 // Requires the calling app to be either a carrier privileged app for this subId or
Junda Liufbd2bcb2016-06-15 11:15:42 -0700978 // system privileged app with MODIFY_PHONE_STATE permission.
Jordan Liud7d57fe2019-04-16 12:29:43 -0700979 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext, subId,
980 "Require carrier privileges or MODIFY_PHONE_STATE permission.");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700981
982 // This method should block until deleting has completed, so that an error which prevents us
983 // from clearing the cache is passed back to the carrier app. With the files successfully
984 // deleted, this can return and we will eventually bind to the carrier app.
Jordan Liud7d57fe2019-04-16 12:29:43 -0700985 String callingPackageName = mContext.getPackageManager().getNameForUid(
986 Binder.getCallingUid());
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700987 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700988 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800989 }
990
991 @Override
992 public void updateConfigForPhoneId(int phoneId, String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -0700993 mContext.enforceCallingOrSelfPermission(
994 android.Manifest.permission.MODIFY_PHONE_STATE, null);
chen xuc4810972019-04-17 23:44:43 -0700995 logWithLocalLog("update config for phoneId: " + phoneId + " simState: " + simState);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800996 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
997 return;
998 }
999 // requires Java 7 for switch on string.
1000 switch (simState) {
1001 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
1002 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -07001003 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001004 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Junda Liu9f2d2712015-05-15 14:22:45 -07001005 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001006 break;
1007 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
1008 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001009 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001010 break;
1011 }
1012 }
1013
Junda Liu43d723a2015-05-12 17:23:45 -07001014 @Override
Jeff Sharkeya6fcfed2017-07-20 14:18:39 -06001015 public String getDefaultCarrierServicePackageName() {
1016 return mPlatformCarrierConfigPackage;
1017 }
1018
1019 @Override
Junda Liu43d723a2015-05-12 17:23:45 -07001020 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1021 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1022 != PackageManager.PERMISSION_GRANTED) {
1023 pw.println("Permission Denial: can't dump carrierconfig from from pid="
1024 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
1025 return;
1026 }
1027 pw.println("CarrierConfigLoader: " + this);
1028 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
shuoq26a3a4c2016-12-16 11:06:48 -08001029 pw.println("Phone Id = " + i);
1030 // display default values in CarrierConfigManager
1031 printConfig(CarrierConfigManager.getDefaultConfig(), pw,
1032 "Default Values from CarrierConfigManager");
1033 pw.println("");
1034 // display ConfigFromDefaultApp
1035 printConfig(mConfigFromDefaultApp[i], pw, "mConfigFromDefaultApp");
1036 pw.println("");
1037 // display ConfigFromCarrierApp
1038 printConfig(mConfigFromCarrierApp[i], pw, "mConfigFromCarrierApp");
Hall Liu506724b2018-10-22 18:16:14 -07001039 pw.println("");
1040 printConfig(mOverrideConfigs[i], pw, "mOverrideConfigs");
shuoq26a3a4c2016-12-16 11:06:48 -08001041 }
chen xudb04c292019-03-26 17:01:15 -07001042
1043 pw.println("CarrierConfigLoadingLog=");
1044 mCarrierConfigLoadingLog.dump(fd, pw, args);
shuoq26a3a4c2016-12-16 11:06:48 -08001045 }
1046
1047 private void printConfig(PersistableBundle configApp, PrintWriter pw, String name) {
1048 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
1049 if (configApp == null) {
1050 indentPW.increaseIndent();
1051 indentPW.println(name + " : null ");
1052 return;
1053 }
1054 indentPW.increaseIndent();
1055 indentPW.println(name + " : ");
1056 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
1057 Collections.sort(sortedKeys);
1058 indentPW.increaseIndent();
1059 indentPW.increaseIndent();
1060 for (String key : sortedKeys) {
1061 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
1062 indentPW.println(key + " = " +
1063 Arrays.toString((Object[]) configApp.get(key)));
1064 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
1065 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
1066 } else {
1067 indentPW.println(key + " = " + configApp.get(key));
1068 }
Junda Liu43d723a2015-05-12 17:23:45 -07001069 }
1070 }
1071
Zach Johnson36d7aab2015-05-22 15:43:00 -07001072 private class CarrierServiceConnection implements ServiceConnection {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001073 int phoneId;
1074 int eventId;
1075 IBinder service;
1076
Zach Johnson36d7aab2015-05-22 15:43:00 -07001077 public CarrierServiceConnection(int phoneId, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001078 this.phoneId = phoneId;
1079 this.eventId = eventId;
1080 }
1081
1082 @Override
1083 public void onServiceConnected(ComponentName name, IBinder service) {
1084 log("Connected to config app: " + name.flattenToString());
1085 this.service = service;
1086 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
1087 }
1088
1089 @Override
1090 public void onServiceDisconnected(ComponentName name) {
1091 this.service = null;
1092 }
1093 }
1094
1095 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
1096 @Override
1097 public void onReceive(Context context, Intent intent) {
1098 String action = intent.getAction();
Junda Liu8a8a53a2015-05-15 16:19:57 -07001099 boolean replace = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
1100 // If replace is true, only care ACTION_PACKAGE_REPLACED.
1101 if (replace && !Intent.ACTION_PACKAGE_REPLACED.equals(action))
1102 return;
1103
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001104 switch (action) {
Nanxi Chen3d670502016-03-17 16:32:09 -07001105 case Intent.ACTION_BOOT_COMPLETED:
1106 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
1107 break;
1108
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001109 case Intent.ACTION_PACKAGE_ADDED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001110 case Intent.ACTION_PACKAGE_REMOVED:
Junda Liu8a8a53a2015-05-15 16:19:57 -07001111 case Intent.ACTION_PACKAGE_REPLACED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001112 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
1113 String packageName = mContext.getPackageManager().getNameForUid(uid);
Junda Liu8a8a53a2015-05-15 16:19:57 -07001114 if (packageName != null) {
1115 // We don't have a phoneId for arg1.
1116 mHandler.sendMessage(
1117 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, packageName));
1118 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001119 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -07001120 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001121 }
1122 }
1123
chen xudb04c292019-03-26 17:01:15 -07001124 private void log(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001125 Log.d(LOG_TAG, msg);
1126 }
1127
chen xudb04c292019-03-26 17:01:15 -07001128 private void logWithLocalLog(String msg) {
1129 Log.d(LOG_TAG, msg);
1130 mCarrierConfigLoadingLog.log(msg);
1131 }
1132
1133 private void loge(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -07001134 Log.e(LOG_TAG, msg);
chen xudb04c292019-03-26 17:01:15 -07001135 mCarrierConfigLoadingLog.log(msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -08001136 }
1137}