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