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