Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 1 | /** |
| 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 Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 17 | package com.android.phone; |
| 18 | |
| 19 | import static android.Manifest.permission.READ_PHONE_STATE; |
Amit Mahajan | 40b3fa5 | 2015-07-14 10:27:19 -0700 | [diff] [blame] | 20 | import static android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 21 | import static android.service.carrier.CarrierService.ICarrierServiceWrapper.KEY_CONFIG_BUNDLE; |
| 22 | import static android.service.carrier.CarrierService.ICarrierServiceWrapper.RESULT_ERROR; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 23 | |
Jonathan Basseri | 11f8957 | 2015-05-05 11:57:39 -0700 | [diff] [blame] | 24 | import android.annotation.NonNull; |
Sudheer Shanka | bb03c63 | 2016-11-10 15:29:41 -0800 | [diff] [blame] | 25 | import android.app.ActivityManager; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 26 | import android.content.BroadcastReceiver; |
| 27 | import android.content.ComponentName; |
| 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
| 30 | import android.content.IntentFilter; |
| 31 | import android.content.ServiceConnection; |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 32 | import android.content.SharedPreferences; |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 33 | import android.content.pm.PackageInfo; |
Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 34 | import android.content.pm.PackageManager; |
Junda Liu | 43d723a | 2015-05-12 17:23:45 -0700 | [diff] [blame] | 35 | import android.os.Binder; |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 36 | import android.os.Build; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 37 | import android.os.Bundle; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 38 | import android.os.Handler; |
| 39 | import android.os.IBinder; |
| 40 | import android.os.Message; |
Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 41 | import android.os.PersistableBundle; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 42 | import android.os.RemoteException; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 43 | import android.os.ResultReceiver; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 44 | import android.os.ServiceManager; |
| 45 | import android.os.UserHandle; |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 46 | import android.preference.PreferenceManager; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 47 | import android.service.carrier.CarrierIdentifier; |
Zach Johnson | 36d7aab | 2015-05-22 15:43:00 -0700 | [diff] [blame] | 48 | import android.service.carrier.CarrierService; |
| 49 | import android.service.carrier.ICarrierService; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 50 | import android.telephony.CarrierConfigManager; |
| 51 | import android.telephony.SubscriptionManager; |
| 52 | import android.telephony.TelephonyManager; |
| 53 | import android.util.Log; |
| 54 | |
| 55 | import com.android.internal.telephony.ICarrierConfigLoader; |
| 56 | import com.android.internal.telephony.IccCardConstants; |
| 57 | import com.android.internal.telephony.Phone; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 58 | import com.android.internal.telephony.PhoneFactory; |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 59 | import com.android.internal.util.FastXmlSerializer; |
shuoq | 26a3a4c | 2016-12-16 11:06:48 -0800 | [diff] [blame] | 60 | import com.android.internal.util.IndentingPrintWriter; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 61 | |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 62 | import org.xmlpull.v1.XmlPullParser; |
| 63 | import org.xmlpull.v1.XmlPullParserException; |
| 64 | import org.xmlpull.v1.XmlPullParserFactory; |
| 65 | |
| 66 | import java.io.File; |
Junda Liu | 43d723a | 2015-05-12 17:23:45 -0700 | [diff] [blame] | 67 | import java.io.FileDescriptor; |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 68 | import java.io.FileInputStream; |
| 69 | import java.io.FileNotFoundException; |
| 70 | import java.io.FileOutputStream; |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 71 | import java.io.FilenameFilter; |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 72 | import java.io.IOException; |
Junda Liu | 43d723a | 2015-05-12 17:23:45 -0700 | [diff] [blame] | 73 | import java.io.PrintWriter; |
shuoq | 26a3a4c | 2016-12-16 11:06:48 -0800 | [diff] [blame] | 74 | import java.util.ArrayList; |
| 75 | import java.util.Arrays; |
| 76 | import java.util.Collections; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 77 | import java.util.List; |
| 78 | |
| 79 | /** |
| 80 | * CarrierConfigLoader binds to privileged carrier apps to fetch carrier config overlays. |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 81 | */ |
| 82 | |
| 83 | public class CarrierConfigLoader extends ICarrierConfigLoader.Stub { |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 84 | private static final String LOG_TAG = "CarrierConfigLoader"; |
Meng Wang | 33ad2bc | 2017-03-16 20:21:20 -0700 | [diff] [blame] | 85 | |
| 86 | // Package name for platform carrier config app, bundled with system image. |
| 87 | private final String mPlatformCarrierConfigPackage; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 88 | |
| 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 Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 94 | private PersistableBundle[] mConfigFromDefaultApp; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 95 | // Carrier configs from privileged carrier config app, indexed by phoneID. |
Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 96 | private PersistableBundle[] mConfigFromCarrierApp; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 97 | // Service connection for binding to config app. |
Zach Johnson | 36d7aab | 2015-05-22 15:43:00 -0700 | [diff] [blame] | 98 | private CarrierServiceConnection[] mServiceConnection; |
Junda Liu | 03aad76 | 2017-07-21 13:32:17 -0700 | [diff] [blame] | 99 | // Whether we have sent config change bcast for each phone id. |
| 100 | private boolean[] mHasSentConfigChange; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 101 | |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 102 | // Broadcast receiver for Boot intents, register intent filter in construtor. |
| 103 | private final BroadcastReceiver mBootReceiver = new ConfigLoaderBroadcastReceiver(); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 104 | // Broadcast receiver for SIM and pkg intents, register intent filter in constructor. |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 105 | private final BroadcastReceiver mPackageReceiver = new ConfigLoaderBroadcastReceiver(); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 106 | |
| 107 | // Message codes; see mHandler below. |
| 108 | // Request from SubscriptionInfoUpdater when SIM becomes absent or error. |
| 109 | private static final int EVENT_CLEAR_CONFIG = 0; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 110 | // Has connected to default app. |
| 111 | private static final int EVENT_CONNECTED_TO_DEFAULT = 3; |
| 112 | // Has connected to carrier app. |
| 113 | private static final int EVENT_CONNECTED_TO_CARRIER = 4; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 114 | // Config has been loaded from default app (or cache). |
| 115 | private static final int EVENT_FETCH_DEFAULT_DONE = 5; |
| 116 | // Config has been loaded from carrier app (or cache). |
| 117 | private static final int EVENT_FETCH_CARRIER_DONE = 6; |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 118 | // Attempt to fetch from default app or read from XML. |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 119 | private static final int EVENT_DO_FETCH_DEFAULT = 7; |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 120 | // Attempt to fetch from carrier app or read from XML. |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 121 | private static final int EVENT_DO_FETCH_CARRIER = 8; |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 122 | // A package has been installed, uninstalled, or updated. |
| 123 | private static final int EVENT_PACKAGE_CHANGED = 9; |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 124 | // Bind timed out for the default app. |
| 125 | private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10; |
| 126 | // Bind timed out for a carrier app. |
| 127 | private static final int EVENT_BIND_CARRIER_TIMEOUT = 11; |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 128 | // Check if the system fingerprint has changed. |
| 129 | private static final int EVENT_CHECK_SYSTEM_UPDATE = 12; |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 130 | // Rerun carrier config binding after system is unlocked. |
| 131 | private static final int EVENT_SYSTEM_UNLOCKED = 13; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 132 | // Fetching config timed out from the default app. |
| 133 | private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14; |
| 134 | // Fetching config timed out from a carrier app. |
| 135 | private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15; |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 136 | |
Junda Liu | 6cb4500 | 2016-03-22 17:18:20 -0700 | [diff] [blame] | 137 | private static final int BIND_TIMEOUT_MILLIS = 30000; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 138 | |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 139 | // Tags used for saving and restoring XML documents. |
| 140 | private static final String TAG_DOCUMENT = "carrier_config"; |
| 141 | private static final String TAG_VERSION = "package_version"; |
| 142 | private static final String TAG_BUNDLE = "bundle_data"; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 143 | |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 144 | // SharedPreferences key for last known build fingerprint. |
| 145 | private static final String KEY_FINGERPRINT = "build_fingerprint"; |
| 146 | |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 147 | // Handler to process various events. |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 148 | // |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 149 | // For each phoneId, the event sequence should be: |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 150 | // fetch default, connected to default, fetch default (async), fetch default done, |
| 151 | // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done. |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 152 | // |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 153 | // If there is a saved config file for either the default app or the carrier app, we skip |
| 154 | // binding to the app and go straight from fetch to loaded. |
| 155 | // |
| 156 | // At any time, at most one connection is active. If events are not in this order, previous |
| 157 | // connection will be unbound, so only latest event takes effect. |
| 158 | // |
| 159 | // We broadcast ACTION_CARRIER_CONFIG_CHANGED after: |
| 160 | // 1. loading from carrier app (even if read from a file) |
| 161 | // 2. loading from default app if there is no carrier app (even if read from a file) |
| 162 | // 3. clearing config (e.g. due to sim removal) |
| 163 | // 4. encountering bind or IPC error |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 164 | private class ConfigHandler extends Handler { |
| 165 | @Override |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 166 | public void handleMessage(Message msg) { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 167 | final int phoneId = msg.arg1; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 168 | log("mHandler: " + msg.what + " phoneId: " + phoneId); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 169 | switch (msg.what) { |
| 170 | case EVENT_CLEAR_CONFIG: |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 171 | { |
| 172 | if (mConfigFromDefaultApp[phoneId] == null |
| 173 | && mConfigFromCarrierApp[phoneId] == null) break; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 174 | mConfigFromDefaultApp[phoneId] = null; |
| 175 | mConfigFromCarrierApp[phoneId] = null; |
| 176 | mServiceConnection[phoneId] = null; |
| 177 | broadcastConfigChangedIntent(phoneId); |
| 178 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 179 | } |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 180 | |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 181 | case EVENT_SYSTEM_UNLOCKED: |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 182 | { |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 183 | for (int i = 0; i < TelephonyManager.from(mContext).getPhoneCount(); ++i) { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 184 | // When user unlock device, we should only try to send broadcast again if we |
| 185 | // have sent it before unlock. This will avoid we try to load carrier config |
| 186 | // when SIM is still loading when unlock happens. |
Junda Liu | 03aad76 | 2017-07-21 13:32:17 -0700 | [diff] [blame] | 187 | if (mHasSentConfigChange[i]) { |
| 188 | updateConfigForPhoneId(i); |
| 189 | } |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 190 | } |
| 191 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 192 | } |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 193 | |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 194 | case EVENT_PACKAGE_CHANGED: |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 195 | { |
| 196 | final String carrierPackageName = (String) msg.obj; |
| 197 | // Only update if there are cached config removed to avoid updating config for |
| 198 | // unrelated packages. |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 199 | if (clearCachedConfigForPackage(carrierPackageName)) { |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 200 | int numPhones = TelephonyManager.from(mContext).getPhoneCount(); |
| 201 | for (int i = 0; i < numPhones; ++i) { |
| 202 | updateConfigForPhoneId(i); |
| 203 | } |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 204 | } |
| 205 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 206 | } |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 207 | |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 208 | case EVENT_DO_FETCH_DEFAULT: |
| 209 | { |
| 210 | final String iccid = getIccIdForPhoneId(phoneId); |
| 211 | final PersistableBundle config = |
| 212 | restoreConfigFromXml(mPlatformCarrierConfigPackage, iccid); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 213 | if (config != null) { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 214 | log( |
| 215 | "Loaded config from XML. package=" |
| 216 | + mPlatformCarrierConfigPackage |
| 217 | + " phoneId=" |
| 218 | + phoneId); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 219 | mConfigFromDefaultApp[phoneId] = config; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 220 | Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 221 | newMsg.getData().putBoolean("loaded_from_xml", true); |
| 222 | mHandler.sendMessage(newMsg); |
| 223 | } else { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 224 | // No cached config, so fetch it from the default app. |
| 225 | if (bindToConfigPackage( |
| 226 | mPlatformCarrierConfigPackage, |
| 227 | phoneId, |
| 228 | EVENT_CONNECTED_TO_DEFAULT)) { |
| 229 | sendMessageDelayed( |
| 230 | obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1), |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 231 | BIND_TIMEOUT_MILLIS); |
| 232 | } else { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 233 | // Send broadcast if bind fails. |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 234 | broadcastConfigChangedIntent(phoneId); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 235 | // TODO: We *must* call unbindService even if bindService returns false. |
| 236 | // (And possibly if SecurityException was thrown.) |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 237 | } |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 238 | } |
| 239 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 240 | } |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 241 | |
| 242 | case EVENT_CONNECTED_TO_DEFAULT: |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 243 | { |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 244 | removeMessages(EVENT_BIND_DEFAULT_TIMEOUT); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 245 | final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 246 | // If new service connection has been created, unbind. |
| 247 | if (mServiceConnection[phoneId] != conn || conn.service == null) { |
| 248 | mContext.unbindService(conn); |
| 249 | break; |
| 250 | } |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 251 | final CarrierIdentifier carrierId = getCarrierIdForPhoneId(phoneId); |
| 252 | final String iccid = getIccIdForPhoneId(phoneId); |
| 253 | // ResultReceiver callback will execute in this Handler's thread. |
| 254 | final ResultReceiver resultReceiver = |
| 255 | new ResultReceiver(this) { |
| 256 | @Override |
| 257 | public void onReceiveResult(int resultCode, Bundle resultData) { |
| 258 | mContext.unbindService(conn); |
| 259 | // If new service connection has been created, this is stale. |
| 260 | if (mServiceConnection[phoneId] != conn) { |
| 261 | loge("Received response for stale request."); |
| 262 | return; |
| 263 | } |
| 264 | removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT); |
| 265 | if (resultCode == RESULT_ERROR || resultData == null) { |
| 266 | // On error, abort config fetching. |
| 267 | loge("Failed to get carrier config"); |
| 268 | broadcastConfigChangedIntent(phoneId); |
| 269 | return; |
| 270 | } |
| 271 | PersistableBundle config = |
| 272 | resultData.getParcelable(KEY_CONFIG_BUNDLE); |
| 273 | saveConfigToXml( |
| 274 | mPlatformCarrierConfigPackage, iccid, config); |
| 275 | mConfigFromDefaultApp[phoneId] = config; |
| 276 | sendMessage( |
| 277 | obtainMessage( |
| 278 | EVENT_FETCH_DEFAULT_DONE, phoneId, -1)); |
| 279 | } |
| 280 | }; |
| 281 | // Now fetch the config asynchronously from the ICarrierService. |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 282 | try { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 283 | ICarrierService carrierService = |
| 284 | ICarrierService.Stub.asInterface(conn.service); |
| 285 | carrierService.getCarrierConfig(carrierId, resultReceiver); |
| 286 | } catch (RemoteException e) { |
| 287 | loge("Failed to get carrier config: " + e.toString()); |
| 288 | mContext.unbindService(conn); |
| 289 | break; // So we don't set a timeout. |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 290 | } |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 291 | sendMessageDelayed( |
| 292 | obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1), |
| 293 | BIND_TIMEOUT_MILLIS); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 294 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 295 | } |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 296 | |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 297 | case EVENT_BIND_DEFAULT_TIMEOUT: |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 298 | case EVENT_FETCH_DEFAULT_TIMEOUT: |
| 299 | { |
| 300 | // If a ResponseReceiver callback is in the queue when this happens, we will |
| 301 | // unbind twice and throw an exception. |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 302 | mContext.unbindService(mServiceConnection[phoneId]); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 303 | removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT); |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 304 | broadcastConfigChangedIntent(phoneId); |
| 305 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 306 | } |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 307 | |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 308 | case EVENT_FETCH_DEFAULT_DONE: |
| 309 | { |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 310 | // If we attempted to bind to the app, but the service connection is null, then |
| 311 | // config was cleared while we were waiting and we should not continue. |
| 312 | if (!msg.getData().getBoolean("loaded_from_xml", false) |
| 313 | && mServiceConnection[phoneId] == null) { |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 314 | break; |
| 315 | } |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 316 | final String carrierPackageName = getCarrierPackageForPhoneId(phoneId); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 317 | if (carrierPackageName != null) { |
| 318 | log("Found carrier config app: " + carrierPackageName); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 319 | sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId)); |
Jonathan Basseri | b919e93 | 2015-05-27 16:53:08 +0000 | [diff] [blame] | 320 | } else { |
| 321 | broadcastConfigChangedIntent(phoneId); |
Jonathan Basseri | 4ae2e7c | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 322 | } |
| 323 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 324 | } |
Jonathan Basseri | 4ae2e7c | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 325 | |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 326 | case EVENT_DO_FETCH_CARRIER: |
| 327 | { |
| 328 | final String carrierPackageName = getCarrierPackageForPhoneId(phoneId); |
| 329 | final String iccid = getIccIdForPhoneId(phoneId); |
| 330 | final PersistableBundle config = |
| 331 | restoreConfigFromXml(carrierPackageName, iccid); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 332 | if (config != null) { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 333 | log( |
| 334 | "Loaded config from XML. package=" |
| 335 | + carrierPackageName |
| 336 | + " phoneId=" |
| 337 | + phoneId); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 338 | mConfigFromCarrierApp[phoneId] = config; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 339 | Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 340 | newMsg.getData().putBoolean("loaded_from_xml", true); |
| 341 | sendMessage(newMsg); |
| 342 | } else { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 343 | // No cached config, so fetch it from a carrier app. |
Jonathan Basseri | 40473a5 | 2015-08-14 14:36:29 -0700 | [diff] [blame] | 344 | if (carrierPackageName != null |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 345 | && bindToConfigPackage( |
| 346 | carrierPackageName, |
| 347 | phoneId, |
| 348 | EVENT_CONNECTED_TO_CARRIER)) { |
| 349 | sendMessageDelayed( |
| 350 | obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1), |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 351 | BIND_TIMEOUT_MILLIS); |
| 352 | } else { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 353 | // Send broadcast if bind fails. |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 354 | broadcastConfigChangedIntent(phoneId); |
| 355 | } |
| 356 | } |
| 357 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 358 | } |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 359 | |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 360 | case EVENT_CONNECTED_TO_CARRIER: |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 361 | { |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 362 | removeMessages(EVENT_BIND_CARRIER_TIMEOUT); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 363 | final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 364 | // If new service connection has been created, unbind. |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 365 | if (mServiceConnection[phoneId] != conn || conn.service == null) { |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 366 | mContext.unbindService(conn); |
| 367 | break; |
| 368 | } |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 369 | final CarrierIdentifier carrierId = getCarrierIdForPhoneId(phoneId); |
| 370 | final String iccid = getIccIdForPhoneId(phoneId); |
| 371 | // ResultReceiver callback will execute in this Handler's thread. |
| 372 | final ResultReceiver resultReceiver = |
| 373 | new ResultReceiver(this) { |
| 374 | @Override |
| 375 | public void onReceiveResult(int resultCode, Bundle resultData) { |
| 376 | mContext.unbindService(conn); |
| 377 | // If new service connection has been created, this is stale. |
| 378 | if (mServiceConnection[phoneId] != conn) { |
| 379 | loge("Received response for stale request."); |
| 380 | return; |
| 381 | } |
| 382 | removeMessages(EVENT_FETCH_CARRIER_TIMEOUT); |
| 383 | if (resultCode == RESULT_ERROR || resultData == null) { |
| 384 | // On error, abort config fetching. |
| 385 | loge("Failed to get carrier config"); |
| 386 | broadcastConfigChangedIntent(phoneId); |
| 387 | return; |
| 388 | } |
| 389 | PersistableBundle config = |
| 390 | resultData.getParcelable(KEY_CONFIG_BUNDLE); |
| 391 | saveConfigToXml( |
| 392 | mPlatformCarrierConfigPackage, iccid, config); |
| 393 | mConfigFromCarrierApp[phoneId] = config; |
| 394 | sendMessage( |
| 395 | obtainMessage( |
| 396 | EVENT_FETCH_CARRIER_DONE, phoneId, -1)); |
| 397 | } |
| 398 | }; |
| 399 | // Now fetch the config asynchronously from the ICarrierService. |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 400 | try { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 401 | ICarrierService carrierService = |
| 402 | ICarrierService.Stub.asInterface(conn.service); |
| 403 | carrierService.getCarrierConfig(carrierId, resultReceiver); |
| 404 | } catch (RemoteException e) { |
| 405 | loge("Failed to get carrier config: " + e.toString()); |
| 406 | mContext.unbindService(conn); |
| 407 | break; // So we don't set a timeout. |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 408 | } |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 409 | sendMessageDelayed( |
| 410 | obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1), |
| 411 | BIND_TIMEOUT_MILLIS); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 412 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 413 | } |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 414 | |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 415 | case EVENT_BIND_CARRIER_TIMEOUT: |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 416 | case EVENT_FETCH_CARRIER_TIMEOUT: |
| 417 | { |
| 418 | // If a ResponseReceiver callback is in the queue when this happens, we will |
| 419 | // unbind twice and throw an exception. |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 420 | mContext.unbindService(mServiceConnection[phoneId]); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 421 | removeMessages(EVENT_FETCH_CARRIER_TIMEOUT); |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 422 | broadcastConfigChangedIntent(phoneId); |
| 423 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 424 | } |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 425 | |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 426 | case EVENT_FETCH_CARRIER_DONE: |
| 427 | { |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 428 | // If we attempted to bind to the app, but the service connection is null, then |
| 429 | // config was cleared while we were waiting and we should not continue. |
| 430 | if (!msg.getData().getBoolean("loaded_from_xml", false) |
| 431 | && mServiceConnection[phoneId] == null) { |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 432 | break; |
| 433 | } |
| 434 | broadcastConfigChangedIntent(phoneId); |
| 435 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 436 | } |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 437 | |
| 438 | case EVENT_CHECK_SYSTEM_UPDATE: |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 439 | { |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 440 | SharedPreferences sharedPrefs = |
| 441 | PreferenceManager.getDefaultSharedPreferences(mContext); |
| 442 | final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null); |
| 443 | if (!Build.FINGERPRINT.equals(lastFingerprint)) { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 444 | log( |
| 445 | "Build fingerprint changed. old: " |
| 446 | + lastFingerprint |
| 447 | + " new: " |
| 448 | + Build.FINGERPRINT); |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 449 | clearCachedConfigForPackage(null); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 450 | sharedPrefs |
| 451 | .edit() |
| 452 | .putString(KEY_FINGERPRINT, Build.FINGERPRINT) |
| 453 | .apply(); |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 454 | } |
| 455 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 456 | } |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 457 | } |
| 458 | } |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 459 | } |
| 460 | |
| 461 | private final Handler mHandler; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 462 | |
| 463 | /** |
| 464 | * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast |
| 465 | * receiver for relevant events. |
| 466 | */ |
| 467 | private CarrierConfigLoader(Context context) { |
| 468 | mContext = context; |
Meng Wang | 33ad2bc | 2017-03-16 20:21:20 -0700 | [diff] [blame] | 469 | mPlatformCarrierConfigPackage = |
| 470 | mContext.getString(R.string.platform_carrier_config_package); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 471 | mHandler = new ConfigHandler(); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 472 | |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 473 | IntentFilter bootFilter = new IntentFilter(); |
| 474 | bootFilter.addAction(Intent.ACTION_BOOT_COMPLETED); |
| 475 | context.registerReceiver(mBootReceiver, bootFilter); |
| 476 | |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 477 | // Register for package updates. Update app or uninstall app update will have all 3 intents, |
| 478 | // in the order or removed, added, replaced, all with extra_replace set to true. |
| 479 | IntentFilter pkgFilter = new IntentFilter(); |
| 480 | pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 481 | pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 482 | pkgFilter.addAction(Intent.ACTION_PACKAGE_REPLACED); |
| 483 | pkgFilter.addDataScheme("package"); |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 484 | context.registerReceiverAsUser(mPackageReceiver, UserHandle.ALL, pkgFilter, null, null); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 485 | |
| 486 | int numPhones = TelephonyManager.from(context).getPhoneCount(); |
Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 487 | mConfigFromDefaultApp = new PersistableBundle[numPhones]; |
| 488 | mConfigFromCarrierApp = new PersistableBundle[numPhones]; |
Zach Johnson | 36d7aab | 2015-05-22 15:43:00 -0700 | [diff] [blame] | 489 | mServiceConnection = new CarrierServiceConnection[numPhones]; |
Junda Liu | 03aad76 | 2017-07-21 13:32:17 -0700 | [diff] [blame] | 490 | mHasSentConfigChange = new boolean[numPhones]; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 491 | // Make this service available through ServiceManager. |
| 492 | ServiceManager.addService(Context.CARRIER_CONFIG_SERVICE, this); |
| 493 | log("CarrierConfigLoader has started"); |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 494 | mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Initialize the singleton CarrierConfigLoader instance. |
| 499 | * |
| 500 | * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}. |
| 501 | */ |
| 502 | /* package */ |
| 503 | static CarrierConfigLoader init(Context context) { |
| 504 | synchronized (CarrierConfigLoader.class) { |
| 505 | if (sInstance == null) { |
| 506 | sInstance = new CarrierConfigLoader(context); |
| 507 | } else { |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 508 | Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 509 | } |
| 510 | return sInstance; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | private void broadcastConfigChangedIntent(int phoneId) { |
| 515 | Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED); |
Christopher Tate | 38f55eb | 2017-02-14 14:43:49 -0800 | [diff] [blame] | 516 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT | |
| 517 | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 518 | SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId); |
Sudheer Shanka | bb03c63 | 2016-11-10 15:29:41 -0800 | [diff] [blame] | 519 | ActivityManager.broadcastStickyIntent(intent, UserHandle.USER_ALL); |
Junda Liu | 03aad76 | 2017-07-21 13:32:17 -0700 | [diff] [blame] | 520 | mHasSentConfigChange[phoneId] = true; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /** Binds to the default or carrier config app. */ |
| 524 | private boolean bindToConfigPackage(String pkgName, int phoneId, int eventId) { |
| 525 | log("Binding to " + pkgName + " for phone " + phoneId); |
Zach Johnson | fca8a8d | 2015-06-19 18:21:37 -0700 | [diff] [blame] | 526 | Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE); |
Zach Johnson | 36d7aab | 2015-05-22 15:43:00 -0700 | [diff] [blame] | 527 | carrierService.setPackage(pkgName); |
| 528 | mServiceConnection[phoneId] = new CarrierServiceConnection(phoneId, eventId); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 529 | try { |
Zach Johnson | 36d7aab | 2015-05-22 15:43:00 -0700 | [diff] [blame] | 530 | return mContext.bindService(carrierService, mServiceConnection[phoneId], |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 531 | Context.BIND_AUTO_CREATE); |
| 532 | } catch (SecurityException ex) { |
| 533 | return false; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | private CarrierIdentifier getCarrierIdForPhoneId(int phoneId) { |
| 538 | String mcc = ""; |
| 539 | String mnc = ""; |
| 540 | String imsi = ""; |
| 541 | String gid1 = ""; |
| 542 | String gid2 = ""; |
| 543 | String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId); |
| 544 | String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId); |
Jonathan Basseri | 1fa437c | 2015-04-20 11:08:18 -0700 | [diff] [blame] | 545 | // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC. |
| 546 | if (simOperator != null && simOperator.length() >= 3) { |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 547 | mcc = simOperator.substring(0, 3); |
| 548 | mnc = simOperator.substring(3); |
| 549 | } |
| 550 | Phone phone = PhoneFactory.getPhone(phoneId); |
| 551 | if (phone != null) { |
| 552 | imsi = phone.getSubscriberId(); |
| 553 | gid1 = phone.getGroupIdLevel1(); |
Junda Liu | 7318353 | 2015-05-14 13:55:40 -0700 | [diff] [blame] | 554 | gid2 = phone.getGroupIdLevel2(); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2); |
| 558 | } |
| 559 | |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 560 | /** Returns the package name of a priveleged carrier app, or null if there is none. */ |
| 561 | private String getCarrierPackageForPhoneId(int phoneId) { |
| 562 | List<String> carrierPackageNames = TelephonyManager.from(mContext) |
| 563 | .getCarrierPackageNamesForIntentAndPhone( |
Zach Johnson | fca8a8d | 2015-06-19 18:21:37 -0700 | [diff] [blame] | 564 | new Intent(CarrierService.CARRIER_SERVICE_INTERFACE), phoneId); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 565 | if (carrierPackageNames != null && carrierPackageNames.size() > 0) { |
| 566 | return carrierPackageNames.get(0); |
| 567 | } else { |
| 568 | return null; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | private String getIccIdForPhoneId(int phoneId) { |
| 573 | if (!SubscriptionManager.isValidPhoneId(phoneId)) { |
| 574 | return null; |
| 575 | } |
| 576 | Phone phone = PhoneFactory.getPhone(phoneId); |
| 577 | if (phone == null) { |
| 578 | return null; |
| 579 | } |
| 580 | return phone.getIccSerialNumber(); |
| 581 | } |
| 582 | |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 583 | /** |
| 584 | * Writes a bundle to an XML file. |
| 585 | * |
| 586 | * The bundle will be written to a file named after the package name and ICCID, so that it can |
| 587 | * be restored later with {@link @restoreConfigFromXml}. The XML output will include the bundle |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 588 | * and the current version of the specified package. |
| 589 | * |
| 590 | * In case of errors or invalid input, no file will be written. |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 591 | * |
| 592 | * @param packageName the name of the package from which we fetched this bundle. |
| 593 | * @param iccid the ICCID of the subscription for which this bundle was fetched. |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 594 | * @param config the bundle to be written. Null will be treated as an empty bundle. |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 595 | */ |
| 596 | private void saveConfigToXml(String packageName, String iccid, PersistableBundle config) { |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 597 | if (packageName == null || iccid == null) { |
| 598 | loge("Cannot save config with null packageName or iccid."); |
| 599 | return; |
| 600 | } |
Junda Liu | 0259650 | 2016-11-15 13:46:43 -0800 | [diff] [blame] | 601 | // b/32668103 Only save to file if config isn't empty. |
| 602 | // In case of failure, not caching an empty bundle will |
| 603 | // try loading config again on next power on or sim loaded. |
| 604 | // Downside is for genuinely empty bundle, will bind and load |
| 605 | // on every power on. |
| 606 | if (config == null || config.isEmpty()) { |
| 607 | return; |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 610 | final String version = getPackageVersion(packageName); |
| 611 | if (version == null) { |
| 612 | loge("Failed to get package version for: " + packageName); |
| 613 | return; |
| 614 | } |
| 615 | |
| 616 | FileOutputStream outFile = null; |
| 617 | try { |
| 618 | outFile = new FileOutputStream( |
| 619 | new File(mContext.getFilesDir(), getFilenameForConfig(packageName, iccid))); |
| 620 | FastXmlSerializer out = new FastXmlSerializer(); |
| 621 | out.setOutput(outFile, "utf-8"); |
| 622 | out.startDocument("utf-8", true); |
| 623 | out.startTag(null, TAG_DOCUMENT); |
| 624 | out.startTag(null, TAG_VERSION); |
| 625 | out.text(version); |
| 626 | out.endTag(null, TAG_VERSION); |
| 627 | out.startTag(null, TAG_BUNDLE); |
| 628 | config.saveToXml(out); |
| 629 | out.endTag(null, TAG_BUNDLE); |
| 630 | out.endTag(null, TAG_DOCUMENT); |
| 631 | out.endDocument(); |
| 632 | out.flush(); |
| 633 | outFile.close(); |
| 634 | } |
| 635 | catch (IOException e) { |
| 636 | loge(e.toString()); |
| 637 | } |
| 638 | catch (XmlPullParserException e) { |
| 639 | loge(e.toString()); |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | /** |
| 644 | * Reads a bundle from an XML file. |
| 645 | * |
| 646 | * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 647 | * config bundle for the given package and ICCID. |
| 648 | * |
| 649 | * In case of errors, or if the saved config is from a different package version than the |
| 650 | * current version, then null will be returned. |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 651 | * |
| 652 | * @param packageName the name of the package from which we fetched this bundle. |
| 653 | * @param iccid the ICCID of the subscription for which this bundle was fetched. |
| 654 | * @return the bundle from the XML file. Returns null if there is no saved config, the saved |
| 655 | * version does not match, or reading config fails. |
| 656 | */ |
| 657 | private PersistableBundle restoreConfigFromXml(String packageName, String iccid) { |
| 658 | final String version = getPackageVersion(packageName); |
| 659 | if (version == null) { |
| 660 | loge("Failed to get package version for: " + packageName); |
| 661 | return null; |
| 662 | } |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 663 | if (packageName == null || iccid == null) { |
| 664 | loge("Cannot restore config with null packageName or iccid."); |
| 665 | return null; |
| 666 | } |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 667 | |
| 668 | PersistableBundle restoredBundle = null; |
| 669 | FileInputStream inFile = null; |
| 670 | try { |
| 671 | inFile = new FileInputStream( |
| 672 | new File(mContext.getFilesDir(), getFilenameForConfig(packageName, iccid))); |
| 673 | XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser(); |
| 674 | parser.setInput(inFile, "utf-8"); |
| 675 | |
| 676 | int event; |
| 677 | while (((event = parser.next()) != XmlPullParser.END_DOCUMENT)) { |
| 678 | |
| 679 | if (event == XmlPullParser.START_TAG && TAG_VERSION.equals(parser.getName())) { |
| 680 | String savedVersion = parser.nextText(); |
| 681 | if (!version.equals(savedVersion)) { |
| 682 | log("Saved version mismatch: " + version + " vs " + savedVersion); |
| 683 | break; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | if (event == XmlPullParser.START_TAG && TAG_BUNDLE.equals(parser.getName())) { |
| 688 | restoredBundle = PersistableBundle.restoreFromXml(parser); |
| 689 | } |
| 690 | } |
| 691 | inFile.close(); |
| 692 | } |
| 693 | catch (FileNotFoundException e) { |
| 694 | loge(e.toString()); |
| 695 | } |
| 696 | catch (XmlPullParserException e) { |
| 697 | loge(e.toString()); |
| 698 | } |
| 699 | catch (IOException e) { |
| 700 | loge(e.toString()); |
| 701 | } |
| 702 | |
| 703 | return restoredBundle; |
| 704 | } |
| 705 | |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 706 | /** |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 707 | * Clears cached carrier config. |
| 708 | * This deletes all saved XML files associated with the given package name. If packageName is |
| 709 | * null, then it deletes all saved XML files. |
| 710 | * |
| 711 | * @param packageName the name of a carrier package, or null if all cached config should be |
| 712 | * cleared. |
| 713 | * @return true iff one or more files were deleted. |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 714 | */ |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 715 | private boolean clearCachedConfigForPackage(final String packageName) { |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 716 | File dir = mContext.getFilesDir(); |
| 717 | File[] packageFiles = dir.listFiles(new FilenameFilter() { |
| 718 | public boolean accept(File dir, String filename) { |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 719 | if (packageName != null) { |
| 720 | return filename.startsWith("carrierconfig-" + packageName + "-"); |
| 721 | } else { |
| 722 | return filename.startsWith("carrierconfig-"); |
| 723 | } |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 724 | } |
| 725 | }); |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 726 | if (packageFiles == null || packageFiles.length < 1) return false; |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 727 | for (File f : packageFiles) { |
| 728 | log("deleting " + f.getName()); |
| 729 | f.delete(); |
| 730 | } |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 731 | return true; |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 734 | /** Builds a canonical file name for a config file. */ |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 735 | private String getFilenameForConfig(@NonNull String packageName, @NonNull String iccid) { |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 736 | return "carrierconfig-" + packageName + "-" + iccid + ".xml"; |
| 737 | } |
| 738 | |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 739 | /** Return the current version code of a package, or null if the name is not found. */ |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 740 | private String getPackageVersion(String packageName) { |
| 741 | try { |
| 742 | PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 743 | return Integer.toString(info.versionCode); |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 744 | } catch (PackageManager.NameNotFoundException e) { |
| 745 | return null; |
| 746 | } |
| 747 | } |
| 748 | |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 749 | /** |
| 750 | * Read up to date config. |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 751 | * |
| 752 | * This reads config bundles for the given phoneId. That means getting the latest bundle from |
| 753 | * the default app and a privileged carrier app, if present. This will not bind to an app if we |
| 754 | * have a saved config file to use instead. |
| 755 | */ |
| 756 | private void updateConfigForPhoneId(int phoneId) { |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 757 | // Clear in-memory cache for carrier app config, so when carrier app gets uninstalled, no |
| 758 | // stale config is left. |
| 759 | if (mConfigFromCarrierApp[phoneId] != null && |
| 760 | getCarrierPackageForPhoneId(phoneId) == null) { |
| 761 | mConfigFromCarrierApp[phoneId] = null; |
| 762 | } |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame^] | 763 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1)); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | @Override public |
| 767 | @NonNull |
| 768 | PersistableBundle getConfigForSubId(int subId) { |
Amit Mahajan | 40b3fa5 | 2015-07-14 10:27:19 -0700 | [diff] [blame] | 769 | try { |
| 770 | mContext.enforceCallingOrSelfPermission(READ_PRIVILEGED_PHONE_STATE, null); |
| 771 | // SKIP checking run-time READ_PHONE_STATE since using PRIVILEGED |
| 772 | } catch (SecurityException e) { |
| 773 | mContext.enforceCallingOrSelfPermission(READ_PHONE_STATE, null); |
| 774 | } |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 775 | int phoneId = SubscriptionManager.getPhoneId(subId); |
Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 776 | PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig(); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 777 | if (SubscriptionManager.isValidPhoneId(phoneId)) { |
Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 778 | PersistableBundle config = mConfigFromDefaultApp[phoneId]; |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 779 | if (config != null) |
| 780 | retConfig.putAll(config); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 781 | config = mConfigFromCarrierApp[phoneId]; |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 782 | if (config != null) |
| 783 | retConfig.putAll(config); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 784 | } |
| 785 | return retConfig; |
| 786 | } |
| 787 | |
| 788 | @Override |
Jonathan Basseri | 8603035 | 2015-06-08 12:27:56 -0700 | [diff] [blame] | 789 | public void notifyConfigChangedForSubId(int subId) { |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 790 | int phoneId = SubscriptionManager.getPhoneId(subId); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 791 | if (!SubscriptionManager.isValidPhoneId(phoneId)) { |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 792 | log("Ignore invalid phoneId: " + phoneId + " for subId: " + subId); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 793 | return; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 794 | } |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 795 | String callingPackageName = mContext.getPackageManager().getNameForUid( |
| 796 | Binder.getCallingUid()); |
| 797 | // TODO: Check that the calling packages is privileged for subId specifically. |
| 798 | int privilegeStatus = TelephonyManager.from(mContext).checkCarrierPrivilegesForPackage( |
| 799 | callingPackageName); |
Junda Liu | fbd2bcb | 2016-06-15 11:15:42 -0700 | [diff] [blame] | 800 | // Requires the calling app to be either a carrier privileged app or |
| 801 | // system privileged app with MODIFY_PHONE_STATE permission. |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 802 | if (privilegeStatus != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) { |
Junda Liu | fbd2bcb | 2016-06-15 11:15:42 -0700 | [diff] [blame] | 803 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, |
| 804 | "Require carrier privileges or MODIFY_PHONE_STATE permission."); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | // This method should block until deleting has completed, so that an error which prevents us |
| 808 | // from clearing the cache is passed back to the carrier app. With the files successfully |
| 809 | // deleted, this can return and we will eventually bind to the carrier app. |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 810 | clearCachedConfigForPackage(callingPackageName); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 811 | updateConfigForPhoneId(phoneId); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | @Override |
| 815 | public void updateConfigForPhoneId(int phoneId, String simState) { |
Junda Liu | 1f2b34d | 2015-06-18 15:26:56 -0700 | [diff] [blame] | 816 | mContext.enforceCallingOrSelfPermission( |
| 817 | android.Manifest.permission.MODIFY_PHONE_STATE, null); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 818 | log("update config for phoneId: " + phoneId + " simState: " + simState); |
| 819 | if (!SubscriptionManager.isValidPhoneId(phoneId)) { |
| 820 | return; |
| 821 | } |
| 822 | // requires Java 7 for switch on string. |
| 823 | switch (simState) { |
| 824 | case IccCardConstants.INTENT_VALUE_ICC_ABSENT: |
| 825 | case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR: |
Junda Liu | 6f5fddf | 2016-05-24 16:14:41 -0700 | [diff] [blame] | 826 | case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED: |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 827 | case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN: |
Junda Liu | 9f2d271 | 2015-05-15 14:22:45 -0700 | [diff] [blame] | 828 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1)); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 829 | break; |
| 830 | case IccCardConstants.INTENT_VALUE_ICC_LOADED: |
| 831 | case IccCardConstants.INTENT_VALUE_ICC_LOCKED: |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 832 | updateConfigForPhoneId(phoneId); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 833 | break; |
| 834 | } |
| 835 | } |
| 836 | |
Junda Liu | 43d723a | 2015-05-12 17:23:45 -0700 | [diff] [blame] | 837 | @Override |
Jeff Sharkey | a6fcfed | 2017-07-20 14:18:39 -0600 | [diff] [blame] | 838 | public String getDefaultCarrierServicePackageName() { |
| 839 | return mPlatformCarrierConfigPackage; |
| 840 | } |
| 841 | |
| 842 | @Override |
Junda Liu | 43d723a | 2015-05-12 17:23:45 -0700 | [diff] [blame] | 843 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 844 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 845 | != PackageManager.PERMISSION_GRANTED) { |
| 846 | pw.println("Permission Denial: can't dump carrierconfig from from pid=" |
| 847 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()); |
| 848 | return; |
| 849 | } |
| 850 | pw.println("CarrierConfigLoader: " + this); |
| 851 | for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) { |
shuoq | 26a3a4c | 2016-12-16 11:06:48 -0800 | [diff] [blame] | 852 | pw.println("Phone Id = " + i); |
| 853 | // display default values in CarrierConfigManager |
| 854 | printConfig(CarrierConfigManager.getDefaultConfig(), pw, |
| 855 | "Default Values from CarrierConfigManager"); |
| 856 | pw.println(""); |
| 857 | // display ConfigFromDefaultApp |
| 858 | printConfig(mConfigFromDefaultApp[i], pw, "mConfigFromDefaultApp"); |
| 859 | pw.println(""); |
| 860 | // display ConfigFromCarrierApp |
| 861 | printConfig(mConfigFromCarrierApp[i], pw, "mConfigFromCarrierApp"); |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | private void printConfig(PersistableBundle configApp, PrintWriter pw, String name) { |
| 866 | IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " "); |
| 867 | if (configApp == null) { |
| 868 | indentPW.increaseIndent(); |
| 869 | indentPW.println(name + " : null "); |
| 870 | return; |
| 871 | } |
| 872 | indentPW.increaseIndent(); |
| 873 | indentPW.println(name + " : "); |
| 874 | List<String> sortedKeys = new ArrayList<String>(configApp.keySet()); |
| 875 | Collections.sort(sortedKeys); |
| 876 | indentPW.increaseIndent(); |
| 877 | indentPW.increaseIndent(); |
| 878 | for (String key : sortedKeys) { |
| 879 | if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) { |
| 880 | indentPW.println(key + " = " + |
| 881 | Arrays.toString((Object[]) configApp.get(key))); |
| 882 | } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) { |
| 883 | indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key))); |
| 884 | } else { |
| 885 | indentPW.println(key + " = " + configApp.get(key)); |
| 886 | } |
Junda Liu | 43d723a | 2015-05-12 17:23:45 -0700 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | |
Zach Johnson | 36d7aab | 2015-05-22 15:43:00 -0700 | [diff] [blame] | 890 | private class CarrierServiceConnection implements ServiceConnection { |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 891 | int phoneId; |
| 892 | int eventId; |
| 893 | IBinder service; |
| 894 | |
Zach Johnson | 36d7aab | 2015-05-22 15:43:00 -0700 | [diff] [blame] | 895 | public CarrierServiceConnection(int phoneId, int eventId) { |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 896 | this.phoneId = phoneId; |
| 897 | this.eventId = eventId; |
| 898 | } |
| 899 | |
| 900 | @Override |
| 901 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 902 | log("Connected to config app: " + name.flattenToString()); |
| 903 | this.service = service; |
| 904 | mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this)); |
| 905 | } |
| 906 | |
| 907 | @Override |
| 908 | public void onServiceDisconnected(ComponentName name) { |
| 909 | this.service = null; |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver { |
| 914 | @Override |
| 915 | public void onReceive(Context context, Intent intent) { |
| 916 | String action = intent.getAction(); |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 917 | boolean replace = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); |
| 918 | // If replace is true, only care ACTION_PACKAGE_REPLACED. |
| 919 | if (replace && !Intent.ACTION_PACKAGE_REPLACED.equals(action)) |
| 920 | return; |
| 921 | |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 922 | switch (action) { |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 923 | case Intent.ACTION_BOOT_COMPLETED: |
| 924 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null)); |
| 925 | break; |
| 926 | |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 927 | case Intent.ACTION_PACKAGE_ADDED: |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 928 | case Intent.ACTION_PACKAGE_REMOVED: |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 929 | case Intent.ACTION_PACKAGE_REPLACED: |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 930 | int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 931 | String packageName = mContext.getPackageManager().getNameForUid(uid); |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 932 | if (packageName != null) { |
| 933 | // We don't have a phoneId for arg1. |
| 934 | mHandler.sendMessage( |
| 935 | mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, packageName)); |
| 936 | } |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 937 | break; |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 938 | } |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | |
| 942 | private static void log(String msg) { |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 943 | Log.d(LOG_TAG, msg); |
| 944 | } |
| 945 | |
| 946 | private static void loge(String msg) { |
| 947 | Log.e(LOG_TAG, msg); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 948 | } |
| 949 | } |