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