blob: a33324b7e08197e3c229cc6251dbdacf86a90fe3 [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
19import static android.Manifest.permission.READ_PHONE_STATE;
Amit Mahajan40b3fa52015-07-14 10:27:19 -070020import static android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080021
Jonathan Basseri11f89572015-05-05 11:57:39 -070022import android.annotation.NonNull;
Sudheer Shankabb03c632016-11-10 15:29:41 -080023import android.app.ActivityManager;
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;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080033import android.database.sqlite.SQLiteDatabase;
34import android.database.sqlite.SQLiteOpenHelper;
35import android.os.AsyncResult;
Junda Liu43d723a2015-05-12 17:23:45 -070036import android.os.Binder;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070037import android.os.Build;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080038import android.os.Handler;
39import android.os.IBinder;
40import android.os.Message;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070041import android.os.PersistableBundle;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080042import android.os.RemoteException;
43import android.os.ServiceManager;
44import android.os.UserHandle;
Jonathan Basseri7697cae2015-07-06 15:49:23 -070045import android.preference.PreferenceManager;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080046import android.service.carrier.CarrierIdentifier;
Zach Johnson36d7aab2015-05-22 15:43:00 -070047import android.service.carrier.CarrierService;
48import android.service.carrier.ICarrierService;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080049import android.telephony.CarrierConfigManager;
50import android.telephony.SubscriptionManager;
51import android.telephony.TelephonyManager;
52import android.util.Log;
53
54import com.android.internal.telephony.ICarrierConfigLoader;
55import com.android.internal.telephony.IccCardConstants;
56import com.android.internal.telephony.Phone;
57import com.android.internal.telephony.PhoneConstants;
58import com.android.internal.telephony.PhoneFactory;
59import com.android.internal.telephony.TelephonyIntents;
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;
Jonathan Basseri6465afd2015-02-25 13:05:57 -080098 // Service connection for binding to config app.
Zach Johnson36d7aab2015-05-22 15:43:00 -070099 private CarrierServiceConnection[] mServiceConnection;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800100
Nanxi Chen3d670502016-03-17 16:32:09 -0700101 // Broadcast receiver for Boot intents, register intent filter in construtor.
102 private final BroadcastReceiver mBootReceiver = new ConfigLoaderBroadcastReceiver();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800103 // Broadcast receiver for SIM and pkg intents, register intent filter in constructor.
Nanxi Chen3d670502016-03-17 16:32:09 -0700104 private final BroadcastReceiver mPackageReceiver = new ConfigLoaderBroadcastReceiver();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800105
106 // Message codes; see mHandler below.
107 // Request from SubscriptionInfoUpdater when SIM becomes absent or error.
108 private static final int EVENT_CLEAR_CONFIG = 0;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800109 // Has connected to default app.
110 private static final int EVENT_CONNECTED_TO_DEFAULT = 3;
111 // Has connected to carrier app.
112 private static final int EVENT_CONNECTED_TO_CARRIER = 4;
113 // Config has been loaded from default app.
114 private static final int EVENT_LOADED_FROM_DEFAULT = 5;
115 // Config has been loaded from carrier app.
116 private static final int EVENT_LOADED_FROM_CARRIER = 6;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700117 // Attempt to fetch from default app or read from XML.
118 private static final int EVENT_FETCH_DEFAULT = 7;
119 // Attempt to fetch from carrier app or read from XML.
120 private static final int EVENT_FETCH_CARRIER = 8;
121 // A package has been installed, uninstalled, or updated.
122 private static final int EVENT_PACKAGE_CHANGED = 9;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700123 // Bind timed out for the default app.
124 private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
125 // Bind timed out for a carrier app.
126 private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700127 // Check if the system fingerprint has changed.
128 private static final int EVENT_CHECK_SYSTEM_UPDATE = 12;
Nanxi Chen3d670502016-03-17 16:32:09 -0700129 // Rerun carrier config binding after system is unlocked.
130 private static final int EVENT_SYSTEM_UNLOCKED = 13;
Jonathan Basseri930701e2015-06-11 20:56:43 -0700131
Junda Liu6cb45002016-03-22 17:18:20 -0700132 private static final int BIND_TIMEOUT_MILLIS = 30000;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800133
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700134 // Tags used for saving and restoring XML documents.
135 private static final String TAG_DOCUMENT = "carrier_config";
136 private static final String TAG_VERSION = "package_version";
137 private static final String TAG_BUNDLE = "bundle_data";
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800138
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700139 // SharedPreferences key for last known build fingerprint.
140 private static final String KEY_FINGERPRINT = "build_fingerprint";
141
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800142 // Handler to process various events.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700143 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700144 // For each phoneId, the event sequence should be:
145 // fetch default, connected to default, loaded from default,
146 // fetch carrier, connected to carrier, loaded from carrier.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700147 //
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700148 // If there is a saved config file for either the default app or the carrier app, we skip
149 // binding to the app and go straight from fetch to loaded.
150 //
151 // At any time, at most one connection is active. If events are not in this order, previous
152 // connection will be unbound, so only latest event takes effect.
153 //
154 // We broadcast ACTION_CARRIER_CONFIG_CHANGED after:
155 // 1. loading from carrier app (even if read from a file)
156 // 2. loading from default app if there is no carrier app (even if read from a file)
157 // 3. clearing config (e.g. due to sim removal)
158 // 4. encountering bind or IPC error
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800159 private Handler mHandler = new Handler() {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700160 @Override
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800161 public void handleMessage(Message msg) {
162 int phoneId = msg.arg1;
163 log("mHandler: " + msg.what + " phoneId: " + phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700164 String iccid;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800165 CarrierIdentifier carrierId;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700166 String carrierPackageName;
Zach Johnson36d7aab2015-05-22 15:43:00 -0700167 CarrierServiceConnection conn;
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700168 PersistableBundle config;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800169 switch (msg.what) {
170 case EVENT_CLEAR_CONFIG:
Junda Liu0486bdb2015-05-22 15:01:35 -0700171 if (mConfigFromDefaultApp[phoneId] == null &&
172 mConfigFromCarrierApp[phoneId] == null)
173 break;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800174 mConfigFromDefaultApp[phoneId] = null;
175 mConfigFromCarrierApp[phoneId] = null;
176 mServiceConnection[phoneId] = null;
177 broadcastConfigChangedIntent(phoneId);
178 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700179
Nanxi Chen3d670502016-03-17 16:32:09 -0700180 case EVENT_SYSTEM_UNLOCKED:
181 for (int i = 0; i < TelephonyManager.from(mContext).getPhoneCount(); ++i) {
182 updateConfigForPhoneId(i);
183 }
184 break;
185
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700186 case EVENT_PACKAGE_CHANGED:
187 carrierPackageName = (String) msg.obj;
Junda Liu8a8a53a2015-05-15 16:19:57 -0700188 // Only update if there are cached config removed to avoid updating config
189 // for unrelated packages.
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700190 if (clearCachedConfigForPackage(carrierPackageName)) {
Junda Liu8a8a53a2015-05-15 16:19:57 -0700191 int numPhones = TelephonyManager.from(mContext).getPhoneCount();
192 for (int i = 0; i < numPhones; ++i) {
193 updateConfigForPhoneId(i);
194 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700195 }
196 break;
197
198 case EVENT_FETCH_DEFAULT:
199 iccid = getIccIdForPhoneId(phoneId);
Meng Wang33ad2bc2017-03-16 20:21:20 -0700200 config = restoreConfigFromXml(mPlatformCarrierConfigPackage, iccid);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700201 if (config != null) {
Meng Wang33ad2bc2017-03-16 20:21:20 -0700202 log("Loaded config from XML. package=" + mPlatformCarrierConfigPackage
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700203 + " phoneId=" + phoneId);
204 mConfigFromDefaultApp[phoneId] = config;
205 Message newMsg = obtainMessage(EVENT_LOADED_FROM_DEFAULT, phoneId, -1);
206 newMsg.getData().putBoolean("loaded_from_xml", true);
207 mHandler.sendMessage(newMsg);
208 } else {
Meng Wang33ad2bc2017-03-16 20:21:20 -0700209 if (bindToConfigPackage(mPlatformCarrierConfigPackage,
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700210 phoneId, EVENT_CONNECTED_TO_DEFAULT)) {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700211 sendMessageDelayed(obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1),
212 BIND_TIMEOUT_MILLIS);
213 } else {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700214 // Send bcast if bind fails
215 broadcastConfigChangedIntent(phoneId);
216 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800217 }
218 break;
219
220 case EVENT_CONNECTED_TO_DEFAULT:
Jonathan Basseri930701e2015-06-11 20:56:43 -0700221 removeMessages(EVENT_BIND_DEFAULT_TIMEOUT);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800222 carrierId = getCarrierIdForPhoneId(phoneId);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700223 conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800224 // If new service connection has been created, unbind.
225 if (mServiceConnection[phoneId] != conn || conn.service == null) {
226 mContext.unbindService(conn);
227 break;
228 }
229 try {
Zach Johnson36d7aab2015-05-22 15:43:00 -0700230 ICarrierService carrierService = ICarrierService.Stub
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700231 .asInterface(conn.service);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700232 config = carrierService.getCarrierConfig(carrierId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700233 iccid = getIccIdForPhoneId(phoneId);
Meng Wang33ad2bc2017-03-16 20:21:20 -0700234 saveConfigToXml(mPlatformCarrierConfigPackage, iccid, config);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800235 mConfigFromDefaultApp[phoneId] = config;
Junda Liu9f2d2712015-05-15 14:22:45 -0700236 sendMessage(obtainMessage(EVENT_LOADED_FROM_DEFAULT, phoneId, -1));
Junda Liu66d9f2c2016-03-28 17:00:25 -0700237 } catch (Exception ex) {
238 // The bound app could throw exceptions that binder will pass to us.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700239 loge("Failed to get carrier config: " + ex.toString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800240 } finally {
241 mContext.unbindService(mServiceConnection[phoneId]);
242 }
243 break;
244
Jonathan Basseri930701e2015-06-11 20:56:43 -0700245 case EVENT_BIND_DEFAULT_TIMEOUT:
246 mContext.unbindService(mServiceConnection[phoneId]);
247 broadcastConfigChangedIntent(phoneId);
248 break;
249
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800250 case EVENT_LOADED_FROM_DEFAULT:
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700251 // If we attempted to bind to the app, but the service connection is null, then
252 // config was cleared while we were waiting and we should not continue.
253 if (!msg.getData().getBoolean("loaded_from_xml", false)
254 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800255 break;
256 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700257 carrierPackageName = getCarrierPackageForPhoneId(phoneId);
258 if (carrierPackageName != null) {
259 log("Found carrier config app: " + carrierPackageName);
260 sendMessage(obtainMessage(EVENT_FETCH_CARRIER, phoneId));
Jonathan Basserib919e932015-05-27 16:53:08 +0000261 } else {
262 broadcastConfigChangedIntent(phoneId);
Jonathan Basseri4ae2e7c2015-05-15 00:19:46 -0700263 }
264 break;
265
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700266 case EVENT_FETCH_CARRIER:
267 carrierPackageName = getCarrierPackageForPhoneId(phoneId);
268 iccid = getIccIdForPhoneId(phoneId);
269 config = restoreConfigFromXml(carrierPackageName, iccid);
270 if (config != null) {
271 log("Loaded config from XML. package=" + carrierPackageName + " phoneId="
272 + phoneId);
273 mConfigFromCarrierApp[phoneId] = config;
274 Message newMsg = obtainMessage(EVENT_LOADED_FROM_CARRIER, phoneId, -1);
275 newMsg.getData().putBoolean("loaded_from_xml", true);
276 sendMessage(newMsg);
277 } else {
Jonathan Basseri40473a52015-08-14 14:36:29 -0700278 if (carrierPackageName != null
279 && bindToConfigPackage(carrierPackageName, phoneId,
280 EVENT_CONNECTED_TO_CARRIER)) {
Jonathan Basseri930701e2015-06-11 20:56:43 -0700281 sendMessageDelayed(obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1),
282 BIND_TIMEOUT_MILLIS);
283 } else {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700284 // Send bcast if bind fails
285 broadcastConfigChangedIntent(phoneId);
286 }
287 }
288 break;
289
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800290 case EVENT_CONNECTED_TO_CARRIER:
Jonathan Basseri930701e2015-06-11 20:56:43 -0700291 removeMessages(EVENT_BIND_CARRIER_TIMEOUT);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800292 carrierId = getCarrierIdForPhoneId(phoneId);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700293 conn = (CarrierServiceConnection) msg.obj;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800294 // If new service connection has been created, unbind.
295 if (mServiceConnection[phoneId] != conn ||
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700296 conn.service == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800297 mContext.unbindService(conn);
298 break;
299 }
300 try {
Zach Johnson36d7aab2015-05-22 15:43:00 -0700301 ICarrierService carrierService = ICarrierService.Stub
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700302 .asInterface(conn.service);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700303 config = carrierService.getCarrierConfig(carrierId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700304 carrierPackageName = getCarrierPackageForPhoneId(phoneId);
305 iccid = getIccIdForPhoneId(phoneId);
306 saveConfigToXml(carrierPackageName, iccid, config);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800307 mConfigFromCarrierApp[phoneId] = config;
Junda Liu9f2d2712015-05-15 14:22:45 -0700308 sendMessage(obtainMessage(EVENT_LOADED_FROM_CARRIER, phoneId, -1));
Junda Liu66d9f2c2016-03-28 17:00:25 -0700309 } catch (Exception ex) {
310 // The bound app could throw exceptions that binder will pass to us.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700311 loge("Failed to get carrier config: " + ex.toString());
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800312 } finally {
313 mContext.unbindService(mServiceConnection[phoneId]);
314 }
315 break;
316
Jonathan Basseri930701e2015-06-11 20:56:43 -0700317 case EVENT_BIND_CARRIER_TIMEOUT:
318 mContext.unbindService(mServiceConnection[phoneId]);
319 broadcastConfigChangedIntent(phoneId);
320 break;
321
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800322 case EVENT_LOADED_FROM_CARRIER:
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700323 // If we attempted to bind to the app, but the service connection is null, then
324 // config was cleared while we were waiting and we should not continue.
325 if (!msg.getData().getBoolean("loaded_from_xml", false)
326 && mServiceConnection[phoneId] == null) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800327 break;
328 }
329 broadcastConfigChangedIntent(phoneId);
330 break;
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700331
332 case EVENT_CHECK_SYSTEM_UPDATE:
333 SharedPreferences sharedPrefs =
334 PreferenceManager.getDefaultSharedPreferences(mContext);
335 final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
336 if (!Build.FINGERPRINT.equals(lastFingerprint)) {
337 log("Build fingerprint changed. old: "
338 + lastFingerprint + " new: " + Build.FINGERPRINT);
339 clearCachedConfigForPackage(null);
340 sharedPrefs.edit().putString(KEY_FINGERPRINT, Build.FINGERPRINT).apply();
341 }
342 break;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800343 }
344 }
345 };
346
347 /**
348 * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast
349 * receiver for relevant events.
350 */
351 private CarrierConfigLoader(Context context) {
352 mContext = context;
Meng Wang33ad2bc2017-03-16 20:21:20 -0700353 mPlatformCarrierConfigPackage =
354 mContext.getString(R.string.platform_carrier_config_package);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800355
Nanxi Chen3d670502016-03-17 16:32:09 -0700356 IntentFilter bootFilter = new IntentFilter();
357 bootFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
358 context.registerReceiver(mBootReceiver, bootFilter);
359
Junda Liu8a8a53a2015-05-15 16:19:57 -0700360 // Register for package updates. Update app or uninstall app update will have all 3 intents,
361 // in the order or removed, added, replaced, all with extra_replace set to true.
362 IntentFilter pkgFilter = new IntentFilter();
363 pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
364 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
365 pkgFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
366 pkgFilter.addDataScheme("package");
Nanxi Chen3d670502016-03-17 16:32:09 -0700367 context.registerReceiverAsUser(mPackageReceiver, UserHandle.ALL, pkgFilter, null, null);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800368
369 int numPhones = TelephonyManager.from(context).getPhoneCount();
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700370 mConfigFromDefaultApp = new PersistableBundle[numPhones];
371 mConfigFromCarrierApp = new PersistableBundle[numPhones];
Zach Johnson36d7aab2015-05-22 15:43:00 -0700372 mServiceConnection = new CarrierServiceConnection[numPhones];
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800373 // Make this service available through ServiceManager.
374 ServiceManager.addService(Context.CARRIER_CONFIG_SERVICE, this);
375 log("CarrierConfigLoader has started");
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700376 mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800377 }
378
379 /**
380 * Initialize the singleton CarrierConfigLoader instance.
381 *
382 * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}.
383 */
384 /* package */
385 static CarrierConfigLoader init(Context context) {
386 synchronized (CarrierConfigLoader.class) {
387 if (sInstance == null) {
388 sInstance = new CarrierConfigLoader(context);
389 } else {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700390 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800391 }
392 return sInstance;
393 }
394 }
395
396 private void broadcastConfigChangedIntent(int phoneId) {
397 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
Christopher Tate38f55eb2017-02-14 14:43:49 -0800398 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT |
399 Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800400 SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId);
Sudheer Shankabb03c632016-11-10 15:29:41 -0800401 ActivityManager.broadcastStickyIntent(intent, UserHandle.USER_ALL);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800402 }
403
404 /** Binds to the default or carrier config app. */
405 private boolean bindToConfigPackage(String pkgName, int phoneId, int eventId) {
406 log("Binding to " + pkgName + " for phone " + phoneId);
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700407 Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE);
Zach Johnson36d7aab2015-05-22 15:43:00 -0700408 carrierService.setPackage(pkgName);
409 mServiceConnection[phoneId] = new CarrierServiceConnection(phoneId, eventId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800410 try {
Zach Johnson36d7aab2015-05-22 15:43:00 -0700411 return mContext.bindService(carrierService, mServiceConnection[phoneId],
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800412 Context.BIND_AUTO_CREATE);
413 } catch (SecurityException ex) {
414 return false;
415 }
416 }
417
418 private CarrierIdentifier getCarrierIdForPhoneId(int phoneId) {
419 String mcc = "";
420 String mnc = "";
421 String imsi = "";
422 String gid1 = "";
423 String gid2 = "";
424 String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId);
425 String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId);
Jonathan Basseri1fa437c2015-04-20 11:08:18 -0700426 // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC.
427 if (simOperator != null && simOperator.length() >= 3) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800428 mcc = simOperator.substring(0, 3);
429 mnc = simOperator.substring(3);
430 }
431 Phone phone = PhoneFactory.getPhone(phoneId);
432 if (phone != null) {
433 imsi = phone.getSubscriberId();
434 gid1 = phone.getGroupIdLevel1();
Junda Liu73183532015-05-14 13:55:40 -0700435 gid2 = phone.getGroupIdLevel2();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800436 }
437
438 return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2);
439 }
440
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700441 /** Returns the package name of a priveleged carrier app, or null if there is none. */
442 private String getCarrierPackageForPhoneId(int phoneId) {
443 List<String> carrierPackageNames = TelephonyManager.from(mContext)
444 .getCarrierPackageNamesForIntentAndPhone(
Zach Johnsonfca8a8d2015-06-19 18:21:37 -0700445 new Intent(CarrierService.CARRIER_SERVICE_INTERFACE), phoneId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700446 if (carrierPackageNames != null && carrierPackageNames.size() > 0) {
447 return carrierPackageNames.get(0);
448 } else {
449 return null;
450 }
451 }
452
453 private String getIccIdForPhoneId(int phoneId) {
454 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
455 return null;
456 }
457 Phone phone = PhoneFactory.getPhone(phoneId);
458 if (phone == null) {
459 return null;
460 }
461 return phone.getIccSerialNumber();
462 }
463
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700464 /**
465 * Writes a bundle to an XML file.
466 *
467 * The bundle will be written to a file named after the package name and ICCID, so that it can
468 * be restored later with {@link @restoreConfigFromXml}. The XML output will include the bundle
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700469 * and the current version of the specified package.
470 *
471 * In case of errors or invalid input, no file will be written.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700472 *
473 * @param packageName the name of the package from which we fetched this bundle.
474 * @param iccid the ICCID of the subscription for which this bundle was fetched.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700475 * @param config the bundle to be written. Null will be treated as an empty bundle.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700476 */
477 private void saveConfigToXml(String packageName, String iccid, PersistableBundle config) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700478 if (packageName == null || iccid == null) {
479 loge("Cannot save config with null packageName or iccid.");
480 return;
481 }
Junda Liu02596502016-11-15 13:46:43 -0800482 // b/32668103 Only save to file if config isn't empty.
483 // In case of failure, not caching an empty bundle will
484 // try loading config again on next power on or sim loaded.
485 // Downside is for genuinely empty bundle, will bind and load
486 // on every power on.
487 if (config == null || config.isEmpty()) {
488 return;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700489 }
490
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700491 final String version = getPackageVersion(packageName);
492 if (version == null) {
493 loge("Failed to get package version for: " + packageName);
494 return;
495 }
496
497 FileOutputStream outFile = null;
498 try {
499 outFile = new FileOutputStream(
500 new File(mContext.getFilesDir(), getFilenameForConfig(packageName, iccid)));
501 FastXmlSerializer out = new FastXmlSerializer();
502 out.setOutput(outFile, "utf-8");
503 out.startDocument("utf-8", true);
504 out.startTag(null, TAG_DOCUMENT);
505 out.startTag(null, TAG_VERSION);
506 out.text(version);
507 out.endTag(null, TAG_VERSION);
508 out.startTag(null, TAG_BUNDLE);
509 config.saveToXml(out);
510 out.endTag(null, TAG_BUNDLE);
511 out.endTag(null, TAG_DOCUMENT);
512 out.endDocument();
513 out.flush();
514 outFile.close();
515 }
516 catch (IOException e) {
517 loge(e.toString());
518 }
519 catch (XmlPullParserException e) {
520 loge(e.toString());
521 }
522 }
523
524 /**
525 * Reads a bundle from an XML file.
526 *
527 * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700528 * config bundle for the given package and ICCID.
529 *
530 * In case of errors, or if the saved config is from a different package version than the
531 * current version, then null will be returned.
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700532 *
533 * @param packageName the name of the package from which we fetched this bundle.
534 * @param iccid the ICCID of the subscription for which this bundle was fetched.
535 * @return the bundle from the XML file. Returns null if there is no saved config, the saved
536 * version does not match, or reading config fails.
537 */
538 private PersistableBundle restoreConfigFromXml(String packageName, String iccid) {
539 final String version = getPackageVersion(packageName);
540 if (version == null) {
541 loge("Failed to get package version for: " + packageName);
542 return null;
543 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700544 if (packageName == null || iccid == null) {
545 loge("Cannot restore config with null packageName or iccid.");
546 return null;
547 }
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700548
549 PersistableBundle restoredBundle = null;
550 FileInputStream inFile = null;
551 try {
552 inFile = new FileInputStream(
553 new File(mContext.getFilesDir(), getFilenameForConfig(packageName, iccid)));
554 XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
555 parser.setInput(inFile, "utf-8");
556
557 int event;
558 while (((event = parser.next()) != XmlPullParser.END_DOCUMENT)) {
559
560 if (event == XmlPullParser.START_TAG && TAG_VERSION.equals(parser.getName())) {
561 String savedVersion = parser.nextText();
562 if (!version.equals(savedVersion)) {
563 log("Saved version mismatch: " + version + " vs " + savedVersion);
564 break;
565 }
566 }
567
568 if (event == XmlPullParser.START_TAG && TAG_BUNDLE.equals(parser.getName())) {
569 restoredBundle = PersistableBundle.restoreFromXml(parser);
570 }
571 }
572 inFile.close();
573 }
574 catch (FileNotFoundException e) {
575 loge(e.toString());
576 }
577 catch (XmlPullParserException e) {
578 loge(e.toString());
579 }
580 catch (IOException e) {
581 loge(e.toString());
582 }
583
584 return restoredBundle;
585 }
586
Junda Liu8a8a53a2015-05-15 16:19:57 -0700587 /**
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700588 * Clears cached carrier config.
589 * This deletes all saved XML files associated with the given package name. If packageName is
590 * null, then it deletes all saved XML files.
591 *
592 * @param packageName the name of a carrier package, or null if all cached config should be
593 * cleared.
594 * @return true iff one or more files were deleted.
Junda Liu8a8a53a2015-05-15 16:19:57 -0700595 */
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700596 private boolean clearCachedConfigForPackage(final String packageName) {
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700597 File dir = mContext.getFilesDir();
598 File[] packageFiles = dir.listFiles(new FilenameFilter() {
599 public boolean accept(File dir, String filename) {
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700600 if (packageName != null) {
601 return filename.startsWith("carrierconfig-" + packageName + "-");
602 } else {
603 return filename.startsWith("carrierconfig-");
604 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700605 }
606 });
Junda Liu8a8a53a2015-05-15 16:19:57 -0700607 if (packageFiles == null || packageFiles.length < 1) return false;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700608 for (File f : packageFiles) {
609 log("deleting " + f.getName());
610 f.delete();
611 }
Junda Liu8a8a53a2015-05-15 16:19:57 -0700612 return true;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700613 }
614
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700615 /** Builds a canonical file name for a config file. */
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700616 private String getFilenameForConfig(@NonNull String packageName, @NonNull String iccid) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700617 return "carrierconfig-" + packageName + "-" + iccid + ".xml";
618 }
619
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700620 /** Return the current version code of a package, or null if the name is not found. */
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700621 private String getPackageVersion(String packageName) {
622 try {
623 PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700624 return Integer.toString(info.versionCode);
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700625 } catch (PackageManager.NameNotFoundException e) {
626 return null;
627 }
628 }
629
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700630 /** Read up to date config.
631 *
632 * This reads config bundles for the given phoneId. That means getting the latest bundle from
633 * the default app and a privileged carrier app, if present. This will not bind to an app if we
634 * have a saved config file to use instead.
635 */
636 private void updateConfigForPhoneId(int phoneId) {
Junda Liu8a8a53a2015-05-15 16:19:57 -0700637 // Clear in-memory cache for carrier app config, so when carrier app gets uninstalled, no
638 // stale config is left.
639 if (mConfigFromCarrierApp[phoneId] != null &&
640 getCarrierPackageForPhoneId(phoneId) == null) {
641 mConfigFromCarrierApp[phoneId] = null;
642 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700643 mHandler.sendMessage(mHandler.obtainMessage(EVENT_FETCH_DEFAULT, phoneId, -1));
644 }
645
646 @Override public
647 @NonNull
648 PersistableBundle getConfigForSubId(int subId) {
Amit Mahajan40b3fa52015-07-14 10:27:19 -0700649 try {
650 mContext.enforceCallingOrSelfPermission(READ_PRIVILEGED_PHONE_STATE, null);
651 // SKIP checking run-time READ_PHONE_STATE since using PRIVILEGED
652 } catch (SecurityException e) {
653 mContext.enforceCallingOrSelfPermission(READ_PHONE_STATE, null);
654 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800655 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700656 PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig();
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800657 if (SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseric31f1f32015-05-12 10:13:03 -0700658 PersistableBundle config = mConfigFromDefaultApp[phoneId];
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700659 if (config != null)
660 retConfig.putAll(config);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800661 config = mConfigFromCarrierApp[phoneId];
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700662 if (config != null)
663 retConfig.putAll(config);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800664 }
665 return retConfig;
666 }
667
668 @Override
Jonathan Basseri86030352015-06-08 12:27:56 -0700669 public void notifyConfigChangedForSubId(int subId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800670 int phoneId = SubscriptionManager.getPhoneId(subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700671 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800672 log("Ignore invalid phoneId: " + phoneId + " for subId: " + subId);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700673 return;
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800674 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700675 String callingPackageName = mContext.getPackageManager().getNameForUid(
676 Binder.getCallingUid());
677 // TODO: Check that the calling packages is privileged for subId specifically.
678 int privilegeStatus = TelephonyManager.from(mContext).checkCarrierPrivilegesForPackage(
679 callingPackageName);
Junda Liufbd2bcb2016-06-15 11:15:42 -0700680 // Requires the calling app to be either a carrier privileged app or
681 // system privileged app with MODIFY_PHONE_STATE permission.
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700682 if (privilegeStatus != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Junda Liufbd2bcb2016-06-15 11:15:42 -0700683 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
684 "Require carrier privileges or MODIFY_PHONE_STATE permission.");
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700685 }
686
687 // This method should block until deleting has completed, so that an error which prevents us
688 // from clearing the cache is passed back to the carrier app. With the files successfully
689 // deleted, this can return and we will eventually bind to the carrier app.
Jonathan Basseri7697cae2015-07-06 15:49:23 -0700690 clearCachedConfigForPackage(callingPackageName);
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700691 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800692 }
693
694 @Override
695 public void updateConfigForPhoneId(int phoneId, String simState) {
Junda Liu1f2b34d2015-06-18 15:26:56 -0700696 mContext.enforceCallingOrSelfPermission(
697 android.Manifest.permission.MODIFY_PHONE_STATE, null);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800698 log("update config for phoneId: " + phoneId + " simState: " + simState);
699 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
700 return;
701 }
702 // requires Java 7 for switch on string.
703 switch (simState) {
704 case IccCardConstants.INTENT_VALUE_ICC_ABSENT:
705 case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR:
Junda Liu6f5fddf2016-05-24 16:14:41 -0700706 case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED:
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800707 case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN:
Junda Liu9f2d2712015-05-15 14:22:45 -0700708 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1));
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800709 break;
710 case IccCardConstants.INTENT_VALUE_ICC_LOADED:
711 case IccCardConstants.INTENT_VALUE_ICC_LOCKED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700712 updateConfigForPhoneId(phoneId);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800713 break;
714 }
715 }
716
Junda Liu43d723a2015-05-12 17:23:45 -0700717 @Override
718 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
719 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
720 != PackageManager.PERMISSION_GRANTED) {
721 pw.println("Permission Denial: can't dump carrierconfig from from pid="
722 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
723 return;
724 }
725 pw.println("CarrierConfigLoader: " + this);
726 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
shuoq26a3a4c2016-12-16 11:06:48 -0800727 pw.println("Phone Id = " + i);
728 // display default values in CarrierConfigManager
729 printConfig(CarrierConfigManager.getDefaultConfig(), pw,
730 "Default Values from CarrierConfigManager");
731 pw.println("");
732 // display ConfigFromDefaultApp
733 printConfig(mConfigFromDefaultApp[i], pw, "mConfigFromDefaultApp");
734 pw.println("");
735 // display ConfigFromCarrierApp
736 printConfig(mConfigFromCarrierApp[i], pw, "mConfigFromCarrierApp");
737 }
738 }
739
740 private void printConfig(PersistableBundle configApp, PrintWriter pw, String name) {
741 IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " ");
742 if (configApp == null) {
743 indentPW.increaseIndent();
744 indentPW.println(name + " : null ");
745 return;
746 }
747 indentPW.increaseIndent();
748 indentPW.println(name + " : ");
749 List<String> sortedKeys = new ArrayList<String>(configApp.keySet());
750 Collections.sort(sortedKeys);
751 indentPW.increaseIndent();
752 indentPW.increaseIndent();
753 for (String key : sortedKeys) {
754 if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) {
755 indentPW.println(key + " = " +
756 Arrays.toString((Object[]) configApp.get(key)));
757 } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) {
758 indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key)));
759 } else {
760 indentPW.println(key + " = " + configApp.get(key));
761 }
Junda Liu43d723a2015-05-12 17:23:45 -0700762 }
763 }
764
Zach Johnson36d7aab2015-05-22 15:43:00 -0700765 private class CarrierServiceConnection implements ServiceConnection {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800766 int phoneId;
767 int eventId;
768 IBinder service;
769
Zach Johnson36d7aab2015-05-22 15:43:00 -0700770 public CarrierServiceConnection(int phoneId, int eventId) {
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800771 this.phoneId = phoneId;
772 this.eventId = eventId;
773 }
774
775 @Override
776 public void onServiceConnected(ComponentName name, IBinder service) {
777 log("Connected to config app: " + name.flattenToString());
778 this.service = service;
779 mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this));
780 }
781
782 @Override
783 public void onServiceDisconnected(ComponentName name) {
784 this.service = null;
785 }
786 }
787
788 private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver {
789 @Override
790 public void onReceive(Context context, Intent intent) {
791 String action = intent.getAction();
Junda Liu8a8a53a2015-05-15 16:19:57 -0700792 boolean replace = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
793 // If replace is true, only care ACTION_PACKAGE_REPLACED.
794 if (replace && !Intent.ACTION_PACKAGE_REPLACED.equals(action))
795 return;
796
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700797 switch (action) {
Nanxi Chen3d670502016-03-17 16:32:09 -0700798 case Intent.ACTION_BOOT_COMPLETED:
799 mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null));
800 break;
801
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700802 case Intent.ACTION_PACKAGE_ADDED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700803 case Intent.ACTION_PACKAGE_REMOVED:
Junda Liu8a8a53a2015-05-15 16:19:57 -0700804 case Intent.ACTION_PACKAGE_REPLACED:
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700805 int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);
806 String packageName = mContext.getPackageManager().getNameForUid(uid);
Junda Liu8a8a53a2015-05-15 16:19:57 -0700807 if (packageName != null) {
808 // We don't have a phoneId for arg1.
809 mHandler.sendMessage(
810 mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, packageName));
811 }
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700812 break;
Jonathan Basseri1f743c92015-05-15 00:19:46 -0700813 }
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800814 }
815 }
816
817 private static void log(String msg) {
Jonathan Basseri6b50e9f2015-05-12 20:18:31 -0700818 Log.d(LOG_TAG, msg);
819 }
820
821 private static void loge(String msg) {
822 Log.e(LOG_TAG, msg);
Jonathan Basseri6465afd2015-02-25 13:05:57 -0800823 }
824}