Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 1 | /* |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 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; |
Torbjorn Eklund | 723527a | 2019-02-13 11:16:25 +0100 | [diff] [blame] | 23 | import android.annotation.Nullable; |
Hunter Knepshield | e601f43 | 2020-02-19 10:16:04 -0800 | [diff] [blame] | 24 | import android.app.AppOpsManager; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 25 | import android.content.BroadcastReceiver; |
| 26 | import android.content.ComponentName; |
| 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
| 29 | import android.content.IntentFilter; |
| 30 | import android.content.ServiceConnection; |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 31 | import android.content.SharedPreferences; |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 32 | import android.content.pm.PackageInfo; |
Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 33 | import android.content.pm.PackageManager; |
Junda Liu | 43d723a | 2015-05-12 17:23:45 -0700 | [diff] [blame] | 34 | import android.os.Binder; |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 35 | import android.os.Build; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 36 | import android.os.Bundle; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 37 | import android.os.Handler; |
| 38 | import android.os.IBinder; |
| 39 | import android.os.Message; |
Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 40 | import android.os.PersistableBundle; |
Hunter Knepshield | e601f43 | 2020-02-19 10:16:04 -0800 | [diff] [blame] | 41 | import android.os.Process; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 42 | import android.os.RemoteException; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 43 | import android.os.ResultReceiver; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 44 | import android.os.ServiceManager; |
Meng Wang | 97a6a46 | 2020-01-23 16:22:16 -0800 | [diff] [blame] | 45 | import android.os.UserHandle; |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 46 | import android.preference.PreferenceManager; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 47 | import android.service.carrier.CarrierIdentifier; |
Zach Johnson | 36d7aab | 2015-05-22 15:43:00 -0700 | [diff] [blame] | 48 | import android.service.carrier.CarrierService; |
| 49 | import android.service.carrier.ICarrierService; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 50 | import android.telephony.CarrierConfigManager; |
| 51 | import android.telephony.SubscriptionManager; |
| 52 | import android.telephony.TelephonyManager; |
Hunter Knepshield | d0ed621 | 2020-01-28 17:43:16 -0800 | [diff] [blame] | 53 | import android.text.TextUtils; |
Hunter Knepshield | eefb31b | 2020-02-19 15:30:54 -0800 | [diff] [blame] | 54 | import android.util.ArraySet; |
chen xu | db04c29 | 2019-03-26 17:01:15 -0700 | [diff] [blame] | 55 | import android.util.LocalLog; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 56 | import android.util.Log; |
| 57 | |
| 58 | import com.android.internal.telephony.ICarrierConfigLoader; |
| 59 | import com.android.internal.telephony.IccCardConstants; |
| 60 | import com.android.internal.telephony.Phone; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 61 | import com.android.internal.telephony.PhoneFactory; |
Nathan Harold | 48ac097 | 2019-03-13 22:33:01 -0700 | [diff] [blame] | 62 | import com.android.internal.telephony.SubscriptionInfoUpdater; |
Jeff Davidson | a8e4e24 | 2018-03-15 17:16:18 -0700 | [diff] [blame] | 63 | import com.android.internal.telephony.TelephonyPermissions; |
Meng Wang | a320b94 | 2019-11-25 11:21:26 -0800 | [diff] [blame] | 64 | import com.android.internal.telephony.util.ArrayUtils; |
shuoq | 26a3a4c | 2016-12-16 11:06:48 -0800 | [diff] [blame] | 65 | import com.android.internal.util.IndentingPrintWriter; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 66 | |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 67 | import java.io.File; |
Junda Liu | 43d723a | 2015-05-12 17:23:45 -0700 | [diff] [blame] | 68 | import java.io.FileDescriptor; |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 69 | import java.io.FileInputStream; |
| 70 | import java.io.FileNotFoundException; |
| 71 | import java.io.FileOutputStream; |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 72 | import java.io.FilenameFilter; |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 73 | import java.io.IOException; |
Junda Liu | 43d723a | 2015-05-12 17:23:45 -0700 | [diff] [blame] | 74 | import java.io.PrintWriter; |
shuoq | 26a3a4c | 2016-12-16 11:06:48 -0800 | [diff] [blame] | 75 | import java.util.ArrayList; |
| 76 | import java.util.Arrays; |
| 77 | import java.util.Collections; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 78 | import java.util.List; |
Hunter Knepshield | eefb31b | 2020-02-19 15:30:54 -0800 | [diff] [blame] | 79 | import java.util.Set; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * CarrierConfigLoader binds to privileged carrier apps to fetch carrier config overlays. |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 83 | */ |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 84 | public class CarrierConfigLoader extends ICarrierConfigLoader.Stub { |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 85 | private static final String LOG_TAG = "CarrierConfigLoader"; |
Meng Wang | 33ad2bc | 2017-03-16 20:21:20 -0700 | [diff] [blame] | 86 | |
| 87 | // Package name for platform carrier config app, bundled with system image. |
| 88 | private final String mPlatformCarrierConfigPackage; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 89 | |
| 90 | /** The singleton instance. */ |
| 91 | private static CarrierConfigLoader sInstance; |
| 92 | // The context for phone app, passed from PhoneGlobals. |
| 93 | private Context mContext; |
| 94 | // Carrier configs from default app, indexed by phoneID. |
Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 95 | private PersistableBundle[] mConfigFromDefaultApp; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 96 | // Carrier configs from privileged carrier config app, indexed by phoneID. |
Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 97 | private PersistableBundle[] mConfigFromCarrierApp; |
Torbjorn Eklund | 723527a | 2019-02-13 11:16:25 +0100 | [diff] [blame] | 98 | // Persistent Carrier configs that are provided via the override test API, indexed by phone ID. |
| 99 | private PersistableBundle[] mPersistentOverrideConfigs; |
Hall Liu | 506724b | 2018-10-22 18:16:14 -0700 | [diff] [blame] | 100 | // Carrier configs that are provided via the override test API, indexed by phone ID. |
| 101 | private PersistableBundle[] mOverrideConfigs; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 102 | // Service connection for binding to config app. |
Zach Johnson | 36d7aab | 2015-05-22 15:43:00 -0700 | [diff] [blame] | 103 | private CarrierServiceConnection[] mServiceConnection; |
Jordan Liu | 178430d | 2020-02-10 14:32:15 -0800 | [diff] [blame] | 104 | // Whether we are bound to a service for each phone |
| 105 | private boolean[] mServiceBound; |
Sarah Chin | 807d5c6 | 2020-03-30 17:21:09 -0700 | [diff] [blame] | 106 | // Whether we have sent config change broadcast for each phone id. |
Junda Liu | 03aad76 | 2017-07-21 13:32:17 -0700 | [diff] [blame] | 107 | private boolean[] mHasSentConfigChange; |
Sarah Chin | 807d5c6 | 2020-03-30 17:21:09 -0700 | [diff] [blame] | 108 | // Whether the broadcast was sent from EVENT_SYSTEM_UNLOCKED, to track rebroadcasts |
| 109 | private boolean[] mFromSystemUnlocked; |
Nathan Harold | 48ac097 | 2019-03-13 22:33:01 -0700 | [diff] [blame] | 110 | // SubscriptionInfoUpdater |
| 111 | private final SubscriptionInfoUpdater mSubscriptionInfoUpdater; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 112 | |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 113 | // Broadcast receiver for Boot intents, register intent filter in construtor. |
| 114 | private final BroadcastReceiver mBootReceiver = new ConfigLoaderBroadcastReceiver(); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 115 | // Broadcast receiver for SIM and pkg intents, register intent filter in constructor. |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 116 | private final BroadcastReceiver mPackageReceiver = new ConfigLoaderBroadcastReceiver(); |
Jack Yu | 67663de | 2019-10-17 15:19:48 -0700 | [diff] [blame] | 117 | private final LocalLog mCarrierConfigLoadingLog = new LocalLog(100); |
chen xu | db04c29 | 2019-03-26 17:01:15 -0700 | [diff] [blame] | 118 | |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 119 | |
| 120 | // Message codes; see mHandler below. |
| 121 | // Request from SubscriptionInfoUpdater when SIM becomes absent or error. |
| 122 | private static final int EVENT_CLEAR_CONFIG = 0; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 123 | // Has connected to default app. |
| 124 | private static final int EVENT_CONNECTED_TO_DEFAULT = 3; |
| 125 | // Has connected to carrier app. |
| 126 | private static final int EVENT_CONNECTED_TO_CARRIER = 4; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 127 | // Config has been loaded from default app (or cache). |
| 128 | private static final int EVENT_FETCH_DEFAULT_DONE = 5; |
| 129 | // Config has been loaded from carrier app (or cache). |
| 130 | private static final int EVENT_FETCH_CARRIER_DONE = 6; |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 131 | // Attempt to fetch from default app or read from XML. |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 132 | private static final int EVENT_DO_FETCH_DEFAULT = 7; |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 133 | // Attempt to fetch from carrier app or read from XML. |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 134 | private static final int EVENT_DO_FETCH_CARRIER = 8; |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 135 | // A package has been installed, uninstalled, or updated. |
| 136 | private static final int EVENT_PACKAGE_CHANGED = 9; |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 137 | // Bind timed out for the default app. |
| 138 | private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10; |
| 139 | // Bind timed out for a carrier app. |
| 140 | private static final int EVENT_BIND_CARRIER_TIMEOUT = 11; |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 141 | // Check if the system fingerprint has changed. |
| 142 | private static final int EVENT_CHECK_SYSTEM_UPDATE = 12; |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 143 | // Rerun carrier config binding after system is unlocked. |
| 144 | private static final int EVENT_SYSTEM_UNLOCKED = 13; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 145 | // Fetching config timed out from the default app. |
| 146 | private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14; |
| 147 | // Fetching config timed out from a carrier app. |
| 148 | private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15; |
Nathan Harold | 48ac097 | 2019-03-13 22:33:01 -0700 | [diff] [blame] | 149 | // SubscriptionInfoUpdater has finished updating the sub for the carrier config. |
| 150 | private static final int EVENT_SUBSCRIPTION_INFO_UPDATED = 16; |
Malcolm Chen | 5ea1853 | 2019-10-24 19:55:44 -0700 | [diff] [blame] | 151 | // Multi-SIM config changed. |
| 152 | private static final int EVENT_MULTI_SIM_CONFIG_CHANGED = 17; |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 153 | |
Junda Liu | 6cb4500 | 2016-03-22 17:18:20 -0700 | [diff] [blame] | 154 | private static final int BIND_TIMEOUT_MILLIS = 30000; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 155 | |
Meng Wang | 0f6b2ea | 2020-01-06 18:31:16 -0800 | [diff] [blame] | 156 | // Keys used for saving and restoring config bundle from file. |
| 157 | private static final String KEY_VERSION = "__carrier_config_package_version__"; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 158 | |
Torbjorn Eklund | 723527a | 2019-02-13 11:16:25 +0100 | [diff] [blame] | 159 | private static final String OVERRIDE_PACKAGE_ADDITION = "-override"; |
| 160 | |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 161 | // SharedPreferences key for last known build fingerprint. |
| 162 | private static final String KEY_FINGERPRINT = "build_fingerprint"; |
| 163 | |
Hunter Knepshield | d0ed621 | 2020-01-28 17:43:16 -0800 | [diff] [blame] | 164 | // Argument for #dump that indicates we should also call the default and specified carrier |
| 165 | // service's #dump method. In multi-SIM devices, it's possible that carrier A is on SIM 1 and |
| 166 | // carrier B is on SIM 2, in which case we should not dump carrier B's service when carrier A |
| 167 | // requested the dump. |
| 168 | private static final String DUMP_ARG_REQUESTING_PACKAGE = "--requesting-package"; |
| 169 | |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 170 | // Handler to process various events. |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 171 | // |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 172 | // For each phoneId, the event sequence should be: |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 173 | // fetch default, connected to default, fetch default (async), fetch default done, |
| 174 | // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done. |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 175 | // |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 176 | // If there is a saved config file for either the default app or the carrier app, we skip |
| 177 | // binding to the app and go straight from fetch to loaded. |
| 178 | // |
| 179 | // At any time, at most one connection is active. If events are not in this order, previous |
| 180 | // connection will be unbound, so only latest event takes effect. |
| 181 | // |
| 182 | // We broadcast ACTION_CARRIER_CONFIG_CHANGED after: |
| 183 | // 1. loading from carrier app (even if read from a file) |
| 184 | // 2. loading from default app if there is no carrier app (even if read from a file) |
| 185 | // 3. clearing config (e.g. due to sim removal) |
| 186 | // 4. encountering bind or IPC error |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 187 | private class ConfigHandler extends Handler { |
| 188 | @Override |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 189 | public void handleMessage(Message msg) { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 190 | final int phoneId = msg.arg1; |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 191 | logdWithLocalLog("mHandler: " + msg.what + " phoneId: " + phoneId); |
Malcolm Chen | 5ea1853 | 2019-10-24 19:55:44 -0700 | [diff] [blame] | 192 | if (!SubscriptionManager.isValidPhoneId(phoneId) |
| 193 | && msg.what != EVENT_MULTI_SIM_CONFIG_CHANGED) { |
| 194 | return; |
| 195 | } |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 196 | switch (msg.what) { |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 197 | case EVENT_CLEAR_CONFIG: { |
Malcolm Chen | 5ea1853 | 2019-10-24 19:55:44 -0700 | [diff] [blame] | 198 | clearConfigForPhone(phoneId, true); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 199 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 200 | } |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 201 | |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 202 | case EVENT_SYSTEM_UNLOCKED: { |
Malcolm Chen | 5ea1853 | 2019-10-24 19:55:44 -0700 | [diff] [blame] | 203 | for (int i = 0; i < TelephonyManager.from(mContext).getActiveModemCount(); |
| 204 | ++i) { |
Sarah Chin | 807d5c6 | 2020-03-30 17:21:09 -0700 | [diff] [blame] | 205 | // When the user unlocks the device, send the broadcast again (with a |
| 206 | // rebroadcast extra) if we have sent it before unlock. This will avoid |
| 207 | // trying to load the carrier config when the SIM is still loading when the |
| 208 | // unlock happens. |
Junda Liu | 03aad76 | 2017-07-21 13:32:17 -0700 | [diff] [blame] | 209 | if (mHasSentConfigChange[i]) { |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 210 | logdWithLocalLog("System unlocked"); |
Sarah Chin | 807d5c6 | 2020-03-30 17:21:09 -0700 | [diff] [blame] | 211 | mFromSystemUnlocked[i] = true; |
Junda Liu | 03aad76 | 2017-07-21 13:32:17 -0700 | [diff] [blame] | 212 | updateConfigForPhoneId(i); |
| 213 | } |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 214 | } |
| 215 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 216 | } |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 217 | |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 218 | case EVENT_PACKAGE_CHANGED: { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 219 | final String carrierPackageName = (String) msg.obj; |
| 220 | // Only update if there are cached config removed to avoid updating config for |
| 221 | // unrelated packages. |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 222 | if (clearCachedConfigForPackage(carrierPackageName)) { |
Malcolm Chen | 5ea1853 | 2019-10-24 19:55:44 -0700 | [diff] [blame] | 223 | int numPhones = TelephonyManager.from(mContext).getActiveModemCount(); |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 224 | for (int i = 0; i < numPhones; ++i) { |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 225 | logdWithLocalLog("Package changed: " + carrierPackageName |
Jack Yu | 67663de | 2019-10-17 15:19:48 -0700 | [diff] [blame] | 226 | + ", phone=" + i); |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 227 | updateConfigForPhoneId(i); |
| 228 | } |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 229 | } |
| 230 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 231 | } |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 232 | |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 233 | case EVENT_DO_FETCH_DEFAULT: { |
Torbjorn Eklund | 723527a | 2019-02-13 11:16:25 +0100 | [diff] [blame] | 234 | // Restore persistent override values. |
| 235 | PersistableBundle config = restoreConfigFromXml( |
| 236 | mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId); |
| 237 | if (config != null) { |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 238 | logd("Loaded persistent override config from XML. package=" |
Torbjorn Eklund | 723527a | 2019-02-13 11:16:25 +0100 | [diff] [blame] | 239 | + mPlatformCarrierConfigPackage |
| 240 | + " phoneId=" + phoneId); |
| 241 | mPersistentOverrideConfigs[phoneId] = config; |
| 242 | } |
| 243 | |
| 244 | config = restoreConfigFromXml(mPlatformCarrierConfigPackage, "", phoneId); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 245 | if (config != null) { |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 246 | logd( |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 247 | "Loaded config from XML. package=" |
| 248 | + mPlatformCarrierConfigPackage |
| 249 | + " phoneId=" |
| 250 | + phoneId); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 251 | mConfigFromDefaultApp[phoneId] = config; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 252 | Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 253 | newMsg.getData().putBoolean("loaded_from_xml", true); |
| 254 | mHandler.sendMessage(newMsg); |
| 255 | } else { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 256 | // No cached config, so fetch it from the default app. |
| 257 | if (bindToConfigPackage( |
| 258 | mPlatformCarrierConfigPackage, |
| 259 | phoneId, |
| 260 | EVENT_CONNECTED_TO_DEFAULT)) { |
| 261 | sendMessageDelayed( |
| 262 | obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1), |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 263 | BIND_TIMEOUT_MILLIS); |
| 264 | } else { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 265 | // Send broadcast if bind fails. |
Nathan Harold | 48ac097 | 2019-03-13 22:33:01 -0700 | [diff] [blame] | 266 | notifySubscriptionInfoUpdater(phoneId); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 267 | // TODO: We *must* call unbindService even if bindService returns false. |
| 268 | // (And possibly if SecurityException was thrown.) |
chen xu | db04c29 | 2019-03-26 17:01:15 -0700 | [diff] [blame] | 269 | loge("binding to default app: " |
| 270 | + mPlatformCarrierConfigPackage + " fails"); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 271 | } |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 272 | } |
| 273 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 274 | } |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 275 | |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 276 | case EVENT_CONNECTED_TO_DEFAULT: { |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 277 | removeMessages(EVENT_BIND_DEFAULT_TIMEOUT); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 278 | final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 279 | // If new service connection has been created, unbind. |
| 280 | if (mServiceConnection[phoneId] != conn || conn.service == null) { |
Jordan Liu | 178430d | 2020-02-10 14:32:15 -0800 | [diff] [blame] | 281 | unbindIfBound(mContext, conn, phoneId); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 282 | break; |
| 283 | } |
chen xu | 0258169 | 2018-11-11 19:03:44 -0800 | [diff] [blame] | 284 | final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 285 | // ResultReceiver callback will execute in this Handler's thread. |
| 286 | final ResultReceiver resultReceiver = |
| 287 | new ResultReceiver(this) { |
| 288 | @Override |
| 289 | public void onReceiveResult(int resultCode, Bundle resultData) { |
Jordan Liu | 178430d | 2020-02-10 14:32:15 -0800 | [diff] [blame] | 290 | unbindIfBound(mContext, conn, phoneId); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 291 | // If new service connection has been created, this is stale. |
| 292 | if (mServiceConnection[phoneId] != conn) { |
| 293 | loge("Received response for stale request."); |
| 294 | return; |
| 295 | } |
| 296 | removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT); |
| 297 | if (resultCode == RESULT_ERROR || resultData == null) { |
| 298 | // On error, abort config fetching. |
| 299 | loge("Failed to get carrier config"); |
Nathan Harold | 48ac097 | 2019-03-13 22:33:01 -0700 | [diff] [blame] | 300 | notifySubscriptionInfoUpdater(phoneId); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 301 | return; |
| 302 | } |
| 303 | PersistableBundle config = |
| 304 | resultData.getParcelable(KEY_CONFIG_BUNDLE); |
Torbjorn Eklund | 723527a | 2019-02-13 11:16:25 +0100 | [diff] [blame] | 305 | saveConfigToXml(mPlatformCarrierConfigPackage, "", phoneId, |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 306 | carrierId, config); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 307 | mConfigFromDefaultApp[phoneId] = config; |
| 308 | sendMessage( |
| 309 | obtainMessage( |
| 310 | EVENT_FETCH_DEFAULT_DONE, phoneId, -1)); |
| 311 | } |
| 312 | }; |
| 313 | // Now fetch the config asynchronously from the ICarrierService. |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 314 | try { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 315 | ICarrierService carrierService = |
| 316 | ICarrierService.Stub.asInterface(conn.service); |
| 317 | carrierService.getCarrierConfig(carrierId, resultReceiver); |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 318 | logdWithLocalLog("Fetch config for default app: " |
chen xu | db04c29 | 2019-03-26 17:01:15 -0700 | [diff] [blame] | 319 | + mPlatformCarrierConfigPackage |
| 320 | + " carrierid: " + carrierId.toString()); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 321 | } catch (RemoteException e) { |
chen xu | db04c29 | 2019-03-26 17:01:15 -0700 | [diff] [blame] | 322 | loge("Failed to get carrier config from default app: " + |
| 323 | mPlatformCarrierConfigPackage + " err: " + e.toString()); |
Jordan Liu | 178430d | 2020-02-10 14:32:15 -0800 | [diff] [blame] | 324 | unbindIfBound(mContext, conn, phoneId); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 325 | break; // So we don't set a timeout. |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 326 | } |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 327 | sendMessageDelayed( |
| 328 | obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1), |
| 329 | BIND_TIMEOUT_MILLIS); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 330 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 331 | } |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 332 | |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 333 | case EVENT_BIND_DEFAULT_TIMEOUT: |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 334 | case EVENT_FETCH_DEFAULT_TIMEOUT: { |
| 335 | loge("Bind/fetch time out from " + mPlatformCarrierConfigPackage); |
chen xu | f60b316 | 2019-05-01 21:31:05 -0700 | [diff] [blame] | 336 | removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT); |
| 337 | // If we attempted to bind to the app, but the service connection is null due to |
| 338 | // the race condition that clear config event happens before bind/fetch complete |
| 339 | // then config was cleared while we were waiting and we should not continue. |
| 340 | if (mServiceConnection[phoneId] != null) { |
| 341 | // If a ResponseReceiver callback is in the queue when this happens, we will |
| 342 | // unbind twice and throw an exception. |
Jordan Liu | 178430d | 2020-02-10 14:32:15 -0800 | [diff] [blame] | 343 | unbindIfBound(mContext, mServiceConnection[phoneId], phoneId); |
chen xu | f60b316 | 2019-05-01 21:31:05 -0700 | [diff] [blame] | 344 | broadcastConfigChangedIntent(phoneId); |
| 345 | } |
Nathan Harold | 48ac097 | 2019-03-13 22:33:01 -0700 | [diff] [blame] | 346 | notifySubscriptionInfoUpdater(phoneId); |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 347 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 348 | } |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 349 | |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 350 | case EVENT_FETCH_DEFAULT_DONE: { |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 351 | // If we attempted to bind to the app, but the service connection is null, then |
| 352 | // config was cleared while we were waiting and we should not continue. |
| 353 | if (!msg.getData().getBoolean("loaded_from_xml", false) |
| 354 | && mServiceConnection[phoneId] == null) { |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 355 | break; |
| 356 | } |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 357 | final String carrierPackageName = getCarrierPackageForPhoneId(phoneId); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 358 | if (carrierPackageName != null) { |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 359 | logd("Found carrier config app: " + carrierPackageName); |
Jordan Liu | 38a614c | 2019-03-20 15:01:17 -0700 | [diff] [blame] | 360 | sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId, -1)); |
Jonathan Basseri | b919e93 | 2015-05-27 16:53:08 +0000 | [diff] [blame] | 361 | } else { |
Nathan Harold | 48ac097 | 2019-03-13 22:33:01 -0700 | [diff] [blame] | 362 | notifySubscriptionInfoUpdater(phoneId); |
Jonathan Basseri | 4ae2e7c | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 363 | } |
| 364 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 365 | } |
Jonathan Basseri | 4ae2e7c | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 366 | |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 367 | case EVENT_DO_FETCH_CARRIER: { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 368 | final String carrierPackageName = getCarrierPackageForPhoneId(phoneId); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 369 | final PersistableBundle config = |
Torbjorn Eklund | 723527a | 2019-02-13 11:16:25 +0100 | [diff] [blame] | 370 | restoreConfigFromXml(carrierPackageName, "", phoneId); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 371 | if (config != null) { |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 372 | logd( |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 373 | "Loaded config from XML. package=" |
| 374 | + carrierPackageName |
| 375 | + " phoneId=" |
| 376 | + phoneId); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 377 | mConfigFromCarrierApp[phoneId] = config; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 378 | Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 379 | newMsg.getData().putBoolean("loaded_from_xml", true); |
| 380 | sendMessage(newMsg); |
| 381 | } else { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 382 | // No cached config, so fetch it from a carrier app. |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 383 | if (carrierPackageName != null && bindToConfigPackage(carrierPackageName, |
| 384 | phoneId, EVENT_CONNECTED_TO_CARRIER)) { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 385 | sendMessageDelayed( |
| 386 | obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1), |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 387 | BIND_TIMEOUT_MILLIS); |
| 388 | } else { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 389 | // Send broadcast if bind fails. |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 390 | broadcastConfigChangedIntent(phoneId); |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 391 | loge("Bind to carrier app: " + carrierPackageName + " fails"); |
Nathan Harold | 48ac097 | 2019-03-13 22:33:01 -0700 | [diff] [blame] | 392 | notifySubscriptionInfoUpdater(phoneId); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 396 | } |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 397 | |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 398 | case EVENT_CONNECTED_TO_CARRIER: { |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 399 | removeMessages(EVENT_BIND_CARRIER_TIMEOUT); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 400 | final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 401 | // If new service connection has been created, unbind. |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 402 | if (mServiceConnection[phoneId] != conn || conn.service == null) { |
Jordan Liu | 178430d | 2020-02-10 14:32:15 -0800 | [diff] [blame] | 403 | unbindIfBound(mContext, conn, phoneId); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 404 | break; |
| 405 | } |
chen xu | 0258169 | 2018-11-11 19:03:44 -0800 | [diff] [blame] | 406 | final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 407 | // ResultReceiver callback will execute in this Handler's thread. |
| 408 | final ResultReceiver resultReceiver = |
| 409 | new ResultReceiver(this) { |
| 410 | @Override |
| 411 | public void onReceiveResult(int resultCode, Bundle resultData) { |
Jordan Liu | 178430d | 2020-02-10 14:32:15 -0800 | [diff] [blame] | 412 | unbindIfBound(mContext, conn, phoneId); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 413 | // If new service connection has been created, this is stale. |
| 414 | if (mServiceConnection[phoneId] != conn) { |
| 415 | loge("Received response for stale request."); |
| 416 | return; |
| 417 | } |
| 418 | removeMessages(EVENT_FETCH_CARRIER_TIMEOUT); |
| 419 | if (resultCode == RESULT_ERROR || resultData == null) { |
| 420 | // On error, abort config fetching. |
chen xu | db04c29 | 2019-03-26 17:01:15 -0700 | [diff] [blame] | 421 | loge("Failed to get carrier config from carrier app: " |
| 422 | + getCarrierPackageForPhoneId(phoneId)); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 423 | broadcastConfigChangedIntent(phoneId); |
Nathan Harold | 48ac097 | 2019-03-13 22:33:01 -0700 | [diff] [blame] | 424 | notifySubscriptionInfoUpdater(phoneId); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 425 | return; |
| 426 | } |
| 427 | PersistableBundle config = |
| 428 | resultData.getParcelable(KEY_CONFIG_BUNDLE); |
Torbjorn Eklund | 723527a | 2019-02-13 11:16:25 +0100 | [diff] [blame] | 429 | saveConfigToXml(getCarrierPackageForPhoneId(phoneId), "", |
| 430 | phoneId, carrierId, config); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 431 | mConfigFromCarrierApp[phoneId] = config; |
| 432 | sendMessage( |
| 433 | obtainMessage( |
| 434 | EVENT_FETCH_CARRIER_DONE, phoneId, -1)); |
| 435 | } |
| 436 | }; |
| 437 | // Now fetch the config asynchronously from the ICarrierService. |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 438 | try { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 439 | ICarrierService carrierService = |
| 440 | ICarrierService.Stub.asInterface(conn.service); |
| 441 | carrierService.getCarrierConfig(carrierId, resultReceiver); |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 442 | logdWithLocalLog("Fetch config for carrier app: " |
chen xu | db04c29 | 2019-03-26 17:01:15 -0700 | [diff] [blame] | 443 | + getCarrierPackageForPhoneId(phoneId) |
| 444 | + " carrierid: " + carrierId.toString()); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 445 | } catch (RemoteException e) { |
| 446 | loge("Failed to get carrier config: " + e.toString()); |
Jordan Liu | 178430d | 2020-02-10 14:32:15 -0800 | [diff] [blame] | 447 | unbindIfBound(mContext, conn, phoneId); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 448 | break; // So we don't set a timeout. |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 449 | } |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 450 | sendMessageDelayed( |
| 451 | obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1), |
| 452 | BIND_TIMEOUT_MILLIS); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 453 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 454 | } |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 455 | |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 456 | case EVENT_BIND_CARRIER_TIMEOUT: |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 457 | case EVENT_FETCH_CARRIER_TIMEOUT: { |
| 458 | loge("Bind/fetch from carrier app timeout"); |
chen xu | f60b316 | 2019-05-01 21:31:05 -0700 | [diff] [blame] | 459 | removeMessages(EVENT_FETCH_CARRIER_TIMEOUT); |
| 460 | // If we attempted to bind to the app, but the service connection is null due to |
| 461 | // the race condition that clear config event happens before bind/fetch complete |
| 462 | // then config was cleared while we were waiting and we should not continue. |
| 463 | if (mServiceConnection[phoneId] != null) { |
| 464 | // If a ResponseReceiver callback is in the queue when this happens, we will |
| 465 | // unbind twice and throw an exception. |
Jordan Liu | 178430d | 2020-02-10 14:32:15 -0800 | [diff] [blame] | 466 | unbindIfBound(mContext, mServiceConnection[phoneId], phoneId); |
chen xu | f60b316 | 2019-05-01 21:31:05 -0700 | [diff] [blame] | 467 | broadcastConfigChangedIntent(phoneId); |
| 468 | } |
Nathan Harold | 48ac097 | 2019-03-13 22:33:01 -0700 | [diff] [blame] | 469 | notifySubscriptionInfoUpdater(phoneId); |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 470 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 471 | } |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 472 | case EVENT_FETCH_CARRIER_DONE: { |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 473 | // If we attempted to bind to the app, but the service connection is null, then |
| 474 | // config was cleared while we were waiting and we should not continue. |
| 475 | if (!msg.getData().getBoolean("loaded_from_xml", false) |
| 476 | && mServiceConnection[phoneId] == null) { |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 477 | break; |
| 478 | } |
Nathan Harold | 48ac097 | 2019-03-13 22:33:01 -0700 | [diff] [blame] | 479 | notifySubscriptionInfoUpdater(phoneId); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 480 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 481 | } |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 482 | |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 483 | case EVENT_CHECK_SYSTEM_UPDATE: { |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 484 | SharedPreferences sharedPrefs = |
| 485 | PreferenceManager.getDefaultSharedPreferences(mContext); |
| 486 | final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null); |
| 487 | if (!Build.FINGERPRINT.equals(lastFingerprint)) { |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 488 | logd( |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 489 | "Build fingerprint changed. old: " |
| 490 | + lastFingerprint |
| 491 | + " new: " |
| 492 | + Build.FINGERPRINT); |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 493 | clearCachedConfigForPackage(null); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 494 | sharedPrefs |
| 495 | .edit() |
| 496 | .putString(KEY_FINGERPRINT, Build.FINGERPRINT) |
| 497 | .apply(); |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 498 | } |
| 499 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 500 | } |
Nathan Harold | 48ac097 | 2019-03-13 22:33:01 -0700 | [diff] [blame] | 501 | |
| 502 | case EVENT_SUBSCRIPTION_INFO_UPDATED: |
| 503 | broadcastConfigChangedIntent(phoneId); |
| 504 | break; |
Malcolm Chen | 5ea1853 | 2019-10-24 19:55:44 -0700 | [diff] [blame] | 505 | case EVENT_MULTI_SIM_CONFIG_CHANGED: |
| 506 | onMultiSimConfigChanged(); |
| 507 | break; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 508 | } |
| 509 | } |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | private final Handler mHandler; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 513 | |
| 514 | /** |
| 515 | * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast |
| 516 | * receiver for relevant events. |
| 517 | */ |
| 518 | private CarrierConfigLoader(Context context) { |
| 519 | mContext = context; |
Meng Wang | 33ad2bc | 2017-03-16 20:21:20 -0700 | [diff] [blame] | 520 | mPlatformCarrierConfigPackage = |
| 521 | mContext.getString(R.string.platform_carrier_config_package); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 522 | mHandler = new ConfigHandler(); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 523 | |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 524 | IntentFilter bootFilter = new IntentFilter(); |
| 525 | bootFilter.addAction(Intent.ACTION_BOOT_COMPLETED); |
| 526 | context.registerReceiver(mBootReceiver, bootFilter); |
| 527 | |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 528 | // Register for package updates. Update app or uninstall app update will have all 3 intents, |
| 529 | // in the order or removed, added, replaced, all with extra_replace set to true. |
| 530 | IntentFilter pkgFilter = new IntentFilter(); |
| 531 | pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 532 | pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 533 | pkgFilter.addAction(Intent.ACTION_PACKAGE_REPLACED); |
| 534 | pkgFilter.addDataScheme("package"); |
Jordan Liu | 5ca2476 | 2019-07-10 12:51:09 -0700 | [diff] [blame] | 535 | context.registerReceiver(mPackageReceiver, pkgFilter); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 536 | |
Malcolm Chen | 978dda5 | 2019-10-08 18:15:25 -0700 | [diff] [blame] | 537 | int numPhones = TelephonyManager.from(context).getSupportedModemCount(); |
Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 538 | mConfigFromDefaultApp = new PersistableBundle[numPhones]; |
| 539 | mConfigFromCarrierApp = new PersistableBundle[numPhones]; |
Torbjorn Eklund | 723527a | 2019-02-13 11:16:25 +0100 | [diff] [blame] | 540 | mPersistentOverrideConfigs = new PersistableBundle[numPhones]; |
Hall Liu | 506724b | 2018-10-22 18:16:14 -0700 | [diff] [blame] | 541 | mOverrideConfigs = new PersistableBundle[numPhones]; |
Zach Johnson | 36d7aab | 2015-05-22 15:43:00 -0700 | [diff] [blame] | 542 | mServiceConnection = new CarrierServiceConnection[numPhones]; |
Jordan Liu | 178430d | 2020-02-10 14:32:15 -0800 | [diff] [blame] | 543 | mServiceBound = new boolean[numPhones]; |
Junda Liu | 03aad76 | 2017-07-21 13:32:17 -0700 | [diff] [blame] | 544 | mHasSentConfigChange = new boolean[numPhones]; |
Sarah Chin | 807d5c6 | 2020-03-30 17:21:09 -0700 | [diff] [blame] | 545 | mFromSystemUnlocked = new boolean[numPhones]; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 546 | // Make this service available through ServiceManager. |
| 547 | ServiceManager.addService(Context.CARRIER_CONFIG_SERVICE, this); |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 548 | logd("CarrierConfigLoader has started"); |
Nathan Harold | 48ac097 | 2019-03-13 22:33:01 -0700 | [diff] [blame] | 549 | mSubscriptionInfoUpdater = PhoneFactory.getSubscriptionInfoUpdater(); |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 550 | mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Initialize the singleton CarrierConfigLoader instance. |
| 555 | * |
| 556 | * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}. |
| 557 | */ |
| 558 | /* package */ |
| 559 | static CarrierConfigLoader init(Context context) { |
| 560 | synchronized (CarrierConfigLoader.class) { |
| 561 | if (sInstance == null) { |
| 562 | sInstance = new CarrierConfigLoader(context); |
| 563 | } else { |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 564 | Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 565 | } |
| 566 | return sInstance; |
| 567 | } |
| 568 | } |
| 569 | |
Malcolm Chen | 5ea1853 | 2019-10-24 19:55:44 -0700 | [diff] [blame] | 570 | private void clearConfigForPhone(int phoneId, boolean sendBroadcast) { |
| 571 | /* Ignore clear configuration request if device is being shutdown. */ |
| 572 | Phone phone = PhoneFactory.getPhone(phoneId); |
| 573 | if (phone != null) { |
| 574 | if (phone.isShuttingDown()) { |
| 575 | return; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | mConfigFromDefaultApp[phoneId] = null; |
| 580 | mConfigFromCarrierApp[phoneId] = null; |
| 581 | mServiceConnection[phoneId] = null; |
| 582 | mHasSentConfigChange[phoneId] = false; |
| 583 | |
| 584 | if (sendBroadcast) broadcastConfigChangedIntent(phoneId, false); |
| 585 | } |
| 586 | |
Nathan Harold | 48ac097 | 2019-03-13 22:33:01 -0700 | [diff] [blame] | 587 | private void notifySubscriptionInfoUpdater(int phoneId) { |
| 588 | String configPackagename; |
| 589 | PersistableBundle configToSend; |
| 590 | int carrierId = getSpecificCarrierIdForPhoneId(phoneId); |
| 591 | // Prefer the carrier privileged carrier app, but if there is not one, use the platform |
| 592 | // default carrier app. |
| 593 | if (mConfigFromCarrierApp[phoneId] != null) { |
| 594 | configPackagename = getCarrierPackageForPhoneId(phoneId); |
| 595 | configToSend = mConfigFromCarrierApp[phoneId]; |
| 596 | } else { |
| 597 | configPackagename = mPlatformCarrierConfigPackage; |
| 598 | configToSend = mConfigFromDefaultApp[phoneId]; |
| 599 | } |
Nazanin Bakhshi | 6fcc334 | 2019-09-18 17:54:01 -0700 | [diff] [blame] | 600 | |
Malcolm Chen | efafd1c | 2020-02-20 13:27:08 -0800 | [diff] [blame] | 601 | if (configToSend == null) { |
| 602 | configToSend = new PersistableBundle(); |
| 603 | } |
| 604 | |
Nazanin Bakhshi | 6fcc334 | 2019-09-18 17:54:01 -0700 | [diff] [blame] | 605 | // mOverrideConfigs is for testing. And it will override current configs. |
| 606 | PersistableBundle config = mOverrideConfigs[phoneId]; |
| 607 | if (config != null) { |
Torbjorn Eklund | 1050cb0 | 2018-11-16 14:05:38 +0100 | [diff] [blame] | 608 | configToSend = new PersistableBundle(configToSend); |
Nazanin Bakhshi | 6fcc334 | 2019-09-18 17:54:01 -0700 | [diff] [blame] | 609 | configToSend.putAll(config); |
| 610 | } |
| 611 | |
Nathan Harold | 48ac097 | 2019-03-13 22:33:01 -0700 | [diff] [blame] | 612 | mSubscriptionInfoUpdater.updateSubscriptionByCarrierConfigAndNotifyComplete( |
| 613 | phoneId, configPackagename, configToSend, |
| 614 | mHandler.obtainMessage(EVENT_SUBSCRIPTION_INFO_UPDATED, phoneId, -1)); |
| 615 | } |
| 616 | |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 617 | private void broadcastConfigChangedIntent(int phoneId) { |
Amit Mahajan | f8088ab | 2018-03-02 15:24:07 -0800 | [diff] [blame] | 618 | broadcastConfigChangedIntent(phoneId, true); |
| 619 | } |
| 620 | |
| 621 | private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) { |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 622 | Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED); |
Christopher Tate | 38f55eb | 2017-02-14 14:43:49 -0800 | [diff] [blame] | 623 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT | |
chen xu | f9db49f | 2019-03-31 23:04:42 -0700 | [diff] [blame] | 624 | Intent.FLAG_RECEIVER_FOREGROUND); |
Qiongcheng Luo | e7de61b | 2018-08-06 10:20:09 +0800 | [diff] [blame] | 625 | if (addSubIdExtra) { |
| 626 | int simApplicationState = TelephonyManager.SIM_STATE_UNKNOWN; |
| 627 | int[] subIds = SubscriptionManager.getSubId(phoneId); |
| 628 | if (!ArrayUtils.isEmpty(subIds)) { |
| 629 | TelephonyManager telMgr = TelephonyManager.from(mContext) |
| 630 | .createForSubscriptionId(subIds[0]); |
| 631 | simApplicationState = telMgr.getSimApplicationState(); |
| 632 | } |
| 633 | // Include subId/carrier id extra only if SIM records are loaded |
| 634 | if (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN |
| 635 | && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY) { |
| 636 | SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId); |
| 637 | intent.putExtra(TelephonyManager.EXTRA_SPECIFIC_CARRIER_ID, |
| 638 | getSpecificCarrierIdForPhoneId(phoneId)); |
| 639 | intent.putExtra(TelephonyManager.EXTRA_CARRIER_ID, getCarrierIdForPhoneId(phoneId)); |
| 640 | } |
Amit Mahajan | f8088ab | 2018-03-02 15:24:07 -0800 | [diff] [blame] | 641 | } |
Amit Mahajan | b33bcda | 2018-01-24 12:56:44 -0800 | [diff] [blame] | 642 | intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId); |
Sarah Chin | 807d5c6 | 2020-03-30 17:21:09 -0700 | [diff] [blame] | 643 | intent.putExtra(CarrierConfigManager.EXTRA_REBROADCAST_ON_UNLOCK, |
| 644 | mFromSystemUnlocked[phoneId]); |
Meng Wang | 97a6a46 | 2020-01-23 16:22:16 -0800 | [diff] [blame] | 645 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
Jack Yu | e9e95be | 2020-03-22 10:56:06 -0700 | [diff] [blame] | 646 | int[] subIds = SubscriptionManager.getSubId(phoneId); |
| 647 | if (subIds != null && subIds.length > 0) { |
| 648 | logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId + ", subId=" + subIds[0]); |
| 649 | } else { |
| 650 | logd("Broadcast CARRIER_CONFIG_CHANGED for phone " + phoneId); |
| 651 | } |
Junda Liu | 03aad76 | 2017-07-21 13:32:17 -0700 | [diff] [blame] | 652 | mHasSentConfigChange[phoneId] = true; |
Sarah Chin | 807d5c6 | 2020-03-30 17:21:09 -0700 | [diff] [blame] | 653 | mFromSystemUnlocked[phoneId] = false; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | /** Binds to the default or carrier config app. */ |
| 657 | private boolean bindToConfigPackage(String pkgName, int phoneId, int eventId) { |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 658 | logdWithLocalLog("Binding to " + pkgName + " for phone " + phoneId); |
Zach Johnson | fca8a8d | 2015-06-19 18:21:37 -0700 | [diff] [blame] | 659 | Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE); |
Zach Johnson | 36d7aab | 2015-05-22 15:43:00 -0700 | [diff] [blame] | 660 | carrierService.setPackage(pkgName); |
Hunter Knepshield | d0ed621 | 2020-01-28 17:43:16 -0800 | [diff] [blame] | 661 | mServiceConnection[phoneId] = new CarrierServiceConnection(phoneId, pkgName, eventId); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 662 | try { |
Jordan Liu | 178430d | 2020-02-10 14:32:15 -0800 | [diff] [blame] | 663 | if (mContext.bindService(carrierService, mServiceConnection[phoneId], |
| 664 | Context.BIND_AUTO_CREATE)) { |
| 665 | mServiceBound[phoneId] = true; |
| 666 | return true; |
| 667 | } else { |
| 668 | return false; |
| 669 | } |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 670 | } catch (SecurityException ex) { |
| 671 | return false; |
| 672 | } |
| 673 | } |
| 674 | |
chen xu | 0258169 | 2018-11-11 19:03:44 -0800 | [diff] [blame] | 675 | private CarrierIdentifier getCarrierIdentifierForPhoneId(int phoneId) { |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 676 | String mcc = ""; |
| 677 | String mnc = ""; |
| 678 | String imsi = ""; |
| 679 | String gid1 = ""; |
| 680 | String gid2 = ""; |
| 681 | String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId); |
| 682 | String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId); |
chen xu | 0258169 | 2018-11-11 19:03:44 -0800 | [diff] [blame] | 683 | int carrierId = TelephonyManager.UNKNOWN_CARRIER_ID; |
chen xu | a31f22b | 2019-03-06 15:28:50 -0800 | [diff] [blame] | 684 | int specificCarrierId = TelephonyManager.UNKNOWN_CARRIER_ID; |
Jonathan Basseri | 1fa437c | 2015-04-20 11:08:18 -0700 | [diff] [blame] | 685 | // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC. |
| 686 | if (simOperator != null && simOperator.length() >= 3) { |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 687 | mcc = simOperator.substring(0, 3); |
| 688 | mnc = simOperator.substring(3); |
| 689 | } |
| 690 | Phone phone = PhoneFactory.getPhone(phoneId); |
| 691 | if (phone != null) { |
| 692 | imsi = phone.getSubscriberId(); |
| 693 | gid1 = phone.getGroupIdLevel1(); |
Junda Liu | 7318353 | 2015-05-14 13:55:40 -0700 | [diff] [blame] | 694 | gid2 = phone.getGroupIdLevel2(); |
chen xu | 0258169 | 2018-11-11 19:03:44 -0800 | [diff] [blame] | 695 | carrierId = phone.getCarrierId(); |
chen xu | a31f22b | 2019-03-06 15:28:50 -0800 | [diff] [blame] | 696 | specificCarrierId = phone.getSpecificCarrierId(); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 697 | } |
chen xu | a31f22b | 2019-03-06 15:28:50 -0800 | [diff] [blame] | 698 | return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2, carrierId, specificCarrierId); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 699 | } |
| 700 | |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 701 | /** Returns the package name of a priveleged carrier app, or null if there is none. */ |
| 702 | private String getCarrierPackageForPhoneId(int phoneId) { |
| 703 | List<String> carrierPackageNames = TelephonyManager.from(mContext) |
| 704 | .getCarrierPackageNamesForIntentAndPhone( |
Zach Johnson | fca8a8d | 2015-06-19 18:21:37 -0700 | [diff] [blame] | 705 | new Intent(CarrierService.CARRIER_SERVICE_INTERFACE), phoneId); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 706 | if (carrierPackageNames != null && carrierPackageNames.size() > 0) { |
| 707 | return carrierPackageNames.get(0); |
| 708 | } else { |
| 709 | return null; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | private String getIccIdForPhoneId(int phoneId) { |
| 714 | if (!SubscriptionManager.isValidPhoneId(phoneId)) { |
| 715 | return null; |
| 716 | } |
| 717 | Phone phone = PhoneFactory.getPhone(phoneId); |
| 718 | if (phone == null) { |
| 719 | return null; |
| 720 | } |
| 721 | return phone.getIccSerialNumber(); |
| 722 | } |
| 723 | |
chen xu | 0258169 | 2018-11-11 19:03:44 -0800 | [diff] [blame] | 724 | /** |
chen xu | a31f22b | 2019-03-06 15:28:50 -0800 | [diff] [blame] | 725 | * Get the sim specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()} |
chen xu | 0258169 | 2018-11-11 19:03:44 -0800 | [diff] [blame] | 726 | */ |
chen xu | a31f22b | 2019-03-06 15:28:50 -0800 | [diff] [blame] | 727 | private int getSpecificCarrierIdForPhoneId(int phoneId) { |
Naina Nalluri | 86fc7b0 | 2018-08-03 10:38:30 -0700 | [diff] [blame] | 728 | if (!SubscriptionManager.isValidPhoneId(phoneId)) { |
chen xu | 0258169 | 2018-11-11 19:03:44 -0800 | [diff] [blame] | 729 | return TelephonyManager.UNKNOWN_CARRIER_ID; |
Naina Nalluri | 86fc7b0 | 2018-08-03 10:38:30 -0700 | [diff] [blame] | 730 | } |
| 731 | Phone phone = PhoneFactory.getPhone(phoneId); |
| 732 | if (phone == null) { |
chen xu | 0258169 | 2018-11-11 19:03:44 -0800 | [diff] [blame] | 733 | return TelephonyManager.UNKNOWN_CARRIER_ID; |
Naina Nalluri | 86fc7b0 | 2018-08-03 10:38:30 -0700 | [diff] [blame] | 734 | } |
chen xu | a31f22b | 2019-03-06 15:28:50 -0800 | [diff] [blame] | 735 | return phone.getSpecificCarrierId(); |
chen xu | 0258169 | 2018-11-11 19:03:44 -0800 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | /** |
| 739 | * Get the sim carrier id {@link TelephonyManager#getSimCarrierId() } |
| 740 | */ |
| 741 | private int getCarrierIdForPhoneId(int phoneId) { |
| 742 | if (!SubscriptionManager.isValidPhoneId(phoneId)) { |
| 743 | return TelephonyManager.UNKNOWN_CARRIER_ID; |
Naina Nalluri | 86fc7b0 | 2018-08-03 10:38:30 -0700 | [diff] [blame] | 744 | } |
chen xu | 0258169 | 2018-11-11 19:03:44 -0800 | [diff] [blame] | 745 | Phone phone = PhoneFactory.getPhone(phoneId); |
| 746 | if (phone == null) { |
| 747 | return TelephonyManager.UNKNOWN_CARRIER_ID; |
| 748 | } |
| 749 | return phone.getCarrierId(); |
Naina Nalluri | 86fc7b0 | 2018-08-03 10:38:30 -0700 | [diff] [blame] | 750 | } |
| 751 | |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 752 | /** |
| 753 | * Writes a bundle to an XML file. |
| 754 | * |
chen xu | 0258169 | 2018-11-11 19:03:44 -0800 | [diff] [blame] | 755 | * 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] | 756 | * specific carrier id {@link TelephonyManager#getSimSpecificCarrierId()}. the same carrier |
chen xu | 0258169 | 2018-11-11 19:03:44 -0800 | [diff] [blame] | 757 | * should have a single copy of XML file named after carrier id. However, it's still possible |
| 758 | * that platform doesn't recognize the current sim carrier, we will use iccid + carrierid as |
| 759 | * the canonical file name. carrierid can also handle the cases SIM OTA resolves to different |
| 760 | * carrier while iccid remains the same. |
| 761 | * |
| 762 | * The file can be restored later with {@link @restoreConfigFromXml}. The XML output will |
| 763 | * include the bundle and the current version of the specified package. |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 764 | * |
| 765 | * In case of errors or invalid input, no file will be written. |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 766 | * |
| 767 | * @param packageName the name of the package from which we fetched this bundle. |
Torbjorn Eklund | 723527a | 2019-02-13 11:16:25 +0100 | [diff] [blame] | 768 | * @param extraString An extra string to be used in the XML file name. |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 769 | * @param phoneId the phone ID. |
| 770 | * @param carrierId contains all carrier-identifying information. |
| 771 | * @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] | 772 | */ |
Torbjorn Eklund | 723527a | 2019-02-13 11:16:25 +0100 | [diff] [blame] | 773 | private void saveConfigToXml(String packageName, @NonNull String extraString, int phoneId, |
| 774 | CarrierIdentifier carrierId, PersistableBundle config) { |
Yuchen Dong | c15afed | 2018-04-26 17:05:22 +0800 | [diff] [blame] | 775 | if (SubscriptionManager.getSimStateForSlotIndex(phoneId) |
| 776 | != TelephonyManager.SIM_STATE_LOADED) { |
chen xu | db04c29 | 2019-03-26 17:01:15 -0700 | [diff] [blame] | 777 | loge("Skip save config because SIM records are not loaded."); |
Yuchen Dong | c15afed | 2018-04-26 17:05:22 +0800 | [diff] [blame] | 778 | return; |
| 779 | } |
| 780 | |
| 781 | final String iccid = getIccIdForPhoneId(phoneId); |
Chen Xu | cd4a637 | 2019-07-29 16:34:52 -0700 | [diff] [blame] | 782 | final int cid = carrierId.getSpecificCarrierId(); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 783 | if (packageName == null || iccid == null) { |
| 784 | loge("Cannot save config with null packageName or iccid."); |
| 785 | return; |
| 786 | } |
Junda Liu | 0259650 | 2016-11-15 13:46:43 -0800 | [diff] [blame] | 787 | // b/32668103 Only save to file if config isn't empty. |
| 788 | // In case of failure, not caching an empty bundle will |
| 789 | // try loading config again on next power on or sim loaded. |
| 790 | // Downside is for genuinely empty bundle, will bind and load |
| 791 | // on every power on. |
| 792 | if (config == null || config.isEmpty()) { |
| 793 | return; |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 794 | } |
| 795 | |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 796 | final String version = getPackageVersion(packageName); |
| 797 | if (version == null) { |
| 798 | loge("Failed to get package version for: " + packageName); |
| 799 | return; |
| 800 | } |
| 801 | |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 802 | logdWithLocalLog( |
| 803 | "Save config to xml, packagename: " + packageName + " phoneId: " + phoneId); |
chen xu | db04c29 | 2019-03-26 17:01:15 -0700 | [diff] [blame] | 804 | |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 805 | FileOutputStream outFile = null; |
| 806 | try { |
| 807 | outFile = new FileOutputStream( |
chen xu | 0258169 | 2018-11-11 19:03:44 -0800 | [diff] [blame] | 808 | new File(mContext.getFilesDir(), |
Torbjorn Eklund | 723527a | 2019-02-13 11:16:25 +0100 | [diff] [blame] | 809 | getFilenameForConfig(packageName, extraString, iccid, cid))); |
Meng Wang | 0f6b2ea | 2020-01-06 18:31:16 -0800 | [diff] [blame] | 810 | config.putString(KEY_VERSION, version); |
| 811 | config.writeToStream(outFile); |
| 812 | outFile.flush(); |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 813 | outFile.close(); |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 814 | } catch (IOException e) { |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 815 | loge(e.toString()); |
| 816 | } |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | /** |
| 820 | * Reads a bundle from an XML file. |
| 821 | * |
| 822 | * 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] | 823 | * config bundle for the given package and phone ID. |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 824 | * |
| 825 | * In case of errors, or if the saved config is from a different package version than the |
| 826 | * current version, then null will be returned. |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 827 | * |
| 828 | * @param packageName the name of the package from which we fetched this bundle. |
Torbjorn Eklund | 723527a | 2019-02-13 11:16:25 +0100 | [diff] [blame] | 829 | * @param extraString An extra string to be used in the XML file name. |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 830 | * @param phoneId the phone ID. |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 831 | * @return the bundle from the XML file. Returns null if there is no saved config, the saved |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 832 | * version does not match, or reading config fails. |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 833 | */ |
Torbjorn Eklund | 723527a | 2019-02-13 11:16:25 +0100 | [diff] [blame] | 834 | private PersistableBundle restoreConfigFromXml(String packageName, @NonNull String extraString, |
| 835 | int phoneId) { |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 836 | final String version = getPackageVersion(packageName); |
| 837 | if (version == null) { |
| 838 | loge("Failed to get package version for: " + packageName); |
| 839 | return null; |
| 840 | } |
Yuchen Dong | c15afed | 2018-04-26 17:05:22 +0800 | [diff] [blame] | 841 | if (SubscriptionManager.getSimStateForSlotIndex(phoneId) |
| 842 | != TelephonyManager.SIM_STATE_LOADED) { |
chen xu | db04c29 | 2019-03-26 17:01:15 -0700 | [diff] [blame] | 843 | loge("Skip restoring config because SIM records are not yet loaded."); |
Yuchen Dong | c15afed | 2018-04-26 17:05:22 +0800 | [diff] [blame] | 844 | return null; |
| 845 | } |
| 846 | |
| 847 | final String iccid = getIccIdForPhoneId(phoneId); |
chen xu | a31f22b | 2019-03-06 15:28:50 -0800 | [diff] [blame] | 848 | final int cid = getSpecificCarrierIdForPhoneId(phoneId); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 849 | if (packageName == null || iccid == null) { |
| 850 | loge("Cannot restore config with null packageName or iccid."); |
| 851 | return null; |
| 852 | } |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 853 | |
| 854 | PersistableBundle restoredBundle = null; |
Torbjorn Eklund | 723527a | 2019-02-13 11:16:25 +0100 | [diff] [blame] | 855 | File file = null; |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 856 | FileInputStream inFile = null; |
| 857 | try { |
Torbjorn Eklund | 723527a | 2019-02-13 11:16:25 +0100 | [diff] [blame] | 858 | file = new File(mContext.getFilesDir(), |
| 859 | getFilenameForConfig(packageName, extraString, iccid, cid)); |
| 860 | inFile = new FileInputStream(file); |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 861 | |
Meng Wang | 0f6b2ea | 2020-01-06 18:31:16 -0800 | [diff] [blame] | 862 | restoredBundle = PersistableBundle.readFromStream(inFile); |
| 863 | String savedVersion = restoredBundle.getString(KEY_VERSION); |
| 864 | restoredBundle.remove(KEY_VERSION); |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 865 | |
Meng Wang | 0f6b2ea | 2020-01-06 18:31:16 -0800 | [diff] [blame] | 866 | if (!version.equals(savedVersion)) { |
| 867 | loge("Saved version mismatch: " + version + " vs " + savedVersion); |
| 868 | restoredBundle = null; |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 869 | } |
Meng Wang | 0f6b2ea | 2020-01-06 18:31:16 -0800 | [diff] [blame] | 870 | |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 871 | inFile.close(); |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 872 | } catch (FileNotFoundException e) { |
Torbjorn Eklund | 723527a | 2019-02-13 11:16:25 +0100 | [diff] [blame] | 873 | // Missing file is normal occurrence that might occur with a new sim or when restoring |
| 874 | // an override file during boot and should not be treated as an error. |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 875 | if (file != null) logd("File not found: " + file.getPath()); |
| 876 | } catch (IOException e) { |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 877 | loge(e.toString()); |
| 878 | } |
| 879 | |
| 880 | return restoredBundle; |
| 881 | } |
| 882 | |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 883 | /** |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 884 | * Clears cached carrier config. |
| 885 | * This deletes all saved XML files associated with the given package name. If packageName is |
| 886 | * null, then it deletes all saved XML files. |
| 887 | * |
| 888 | * @param packageName the name of a carrier package, or null if all cached config should be |
| 889 | * cleared. |
| 890 | * @return true iff one or more files were deleted. |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 891 | */ |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 892 | private boolean clearCachedConfigForPackage(final String packageName) { |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 893 | File dir = mContext.getFilesDir(); |
| 894 | File[] packageFiles = dir.listFiles(new FilenameFilter() { |
| 895 | public boolean accept(File dir, String filename) { |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 896 | if (packageName != null) { |
| 897 | return filename.startsWith("carrierconfig-" + packageName + "-"); |
| 898 | } else { |
| 899 | return filename.startsWith("carrierconfig-"); |
| 900 | } |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 901 | } |
| 902 | }); |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 903 | if (packageFiles == null || packageFiles.length < 1) return false; |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 904 | for (File f : packageFiles) { |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 905 | logd("Deleting " + f.getName()); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 906 | f.delete(); |
| 907 | } |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 908 | return true; |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 909 | } |
| 910 | |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 911 | /** Builds a canonical file name for a config file. */ |
Torbjorn Eklund | 723527a | 2019-02-13 11:16:25 +0100 | [diff] [blame] | 912 | private String getFilenameForConfig(@NonNull String packageName, @NonNull String extraString, |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 913 | @NonNull String iccid, int cid) { |
chen xu | 0258169 | 2018-11-11 19:03:44 -0800 | [diff] [blame] | 914 | // the same carrier should have a single copy of XML file named after carrier id. |
| 915 | // However, it's still possible that platform doesn't recognize the current sim carrier, |
| 916 | // we will use iccid + carrierid as the canonical file name. carrierid can also handle the |
| 917 | // cases SIM OTA resolves to different carrier while iccid remains the same. |
Torbjorn Eklund | 723527a | 2019-02-13 11:16:25 +0100 | [diff] [blame] | 918 | return "carrierconfig-" + packageName + extraString + "-" + iccid + "-" + cid + ".xml"; |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 919 | } |
| 920 | |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 921 | /** 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] | 922 | private String getPackageVersion(String packageName) { |
| 923 | try { |
| 924 | PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0); |
Dianne Hackborn | b76ab20 | 2017-11-28 17:44:50 -0800 | [diff] [blame] | 925 | return Long.toString(info.getLongVersionCode()); |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 926 | } catch (PackageManager.NameNotFoundException e) { |
| 927 | return null; |
| 928 | } |
| 929 | } |
| 930 | |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 931 | /** |
| 932 | * Read up to date config. |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 933 | * |
| 934 | * This reads config bundles for the given phoneId. That means getting the latest bundle from |
| 935 | * the default app and a privileged carrier app, if present. This will not bind to an app if we |
| 936 | * have a saved config file to use instead. |
| 937 | */ |
| 938 | private void updateConfigForPhoneId(int phoneId) { |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 939 | // Clear in-memory cache for carrier app config, so when carrier app gets uninstalled, no |
| 940 | // stale config is left. |
| 941 | if (mConfigFromCarrierApp[phoneId] != null && |
| 942 | getCarrierPackageForPhoneId(phoneId) == null) { |
| 943 | mConfigFromCarrierApp[phoneId] = null; |
| 944 | } |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 945 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1)); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 946 | } |
| 947 | |
Malcolm Chen | 5ea1853 | 2019-10-24 19:55:44 -0700 | [diff] [blame] | 948 | private void onMultiSimConfigChanged() { |
| 949 | for (int i = TelephonyManager.from(mContext).getActiveModemCount(); |
| 950 | i < mConfigFromDefaultApp.length; i++) { |
| 951 | clearConfigForPhone(i, false); |
| 952 | } |
| 953 | } |
| 954 | |
Hall Liu | 506724b | 2018-10-22 18:16:14 -0700 | [diff] [blame] | 955 | @Override |
Hunter Knepshield | d0ed621 | 2020-01-28 17:43:16 -0800 | [diff] [blame] | 956 | @NonNull |
| 957 | public PersistableBundle getConfigForSubId(int subId, String callingPackage) { |
Philip P. Moltmann | 9797cbb | 2020-01-07 13:45:00 -0800 | [diff] [blame] | 958 | return getConfigForSubIdWithFeature(subId, callingPackage, null); |
| 959 | } |
| 960 | |
| 961 | @Override |
Hunter Knepshield | d0ed621 | 2020-01-28 17:43:16 -0800 | [diff] [blame] | 962 | @NonNull |
| 963 | public PersistableBundle getConfigForSubIdWithFeature(int subId, String callingPackage, |
Philip P. Moltmann | 9797cbb | 2020-01-07 13:45:00 -0800 | [diff] [blame] | 964 | String callingFeatureId) { |
| 965 | if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mContext, subId, callingPackage, |
| 966 | callingFeatureId, "getCarrierConfig")) { |
Malcolm Chen | 6d3d6b3 | 2018-03-01 13:58:03 -0800 | [diff] [blame] | 967 | return new PersistableBundle(); |
Amit Mahajan | 40b3fa5 | 2015-07-14 10:27:19 -0700 | [diff] [blame] | 968 | } |
Jeff Davidson | a8e4e24 | 2018-03-15 17:16:18 -0700 | [diff] [blame] | 969 | |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 970 | int phoneId = SubscriptionManager.getPhoneId(subId); |
Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 971 | PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig(); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 972 | if (SubscriptionManager.isValidPhoneId(phoneId)) { |
Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 973 | PersistableBundle config = mConfigFromDefaultApp[phoneId]; |
yinxu | c5e2762 | 2017-11-29 15:08:50 -0800 | [diff] [blame] | 974 | if (config != null) { |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 975 | retConfig.putAll(config); |
yinxu | d010483 | 2019-08-16 14:20:08 -0700 | [diff] [blame] | 976 | if (getCarrierPackageForPhoneId(phoneId) == null) { |
| 977 | retConfig.putBoolean( |
| 978 | CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true); |
| 979 | } |
yinxu | c5e2762 | 2017-11-29 15:08:50 -0800 | [diff] [blame] | 980 | } |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 981 | config = mConfigFromCarrierApp[phoneId]; |
yinxu | c5e2762 | 2017-11-29 15:08:50 -0800 | [diff] [blame] | 982 | if (config != null) { |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 983 | retConfig.putAll(config); |
yinxu | c5e2762 | 2017-11-29 15:08:50 -0800 | [diff] [blame] | 984 | retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true); |
| 985 | } |
Torbjorn Eklund | 723527a | 2019-02-13 11:16:25 +0100 | [diff] [blame] | 986 | config = mPersistentOverrideConfigs[phoneId]; |
| 987 | if (config != null) { |
| 988 | retConfig.putAll(config); |
| 989 | retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true); |
| 990 | } |
Hall Liu | 506724b | 2018-10-22 18:16:14 -0700 | [diff] [blame] | 991 | config = mOverrideConfigs[phoneId]; |
| 992 | if (config != null) { |
| 993 | retConfig.putAll(config); |
Hall Liu | 506724b | 2018-10-22 18:16:14 -0700 | [diff] [blame] | 994 | } |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 995 | } |
| 996 | return retConfig; |
| 997 | } |
| 998 | |
| 999 | @Override |
Torbjorn Eklund | 723527a | 2019-02-13 11:16:25 +0100 | [diff] [blame] | 1000 | public void overrideConfig(int subscriptionId, @Nullable PersistableBundle overrides, |
| 1001 | boolean persistent) { |
Hall Liu | 506724b | 2018-10-22 18:16:14 -0700 | [diff] [blame] | 1002 | mContext.enforceCallingOrSelfPermission( |
| 1003 | android.Manifest.permission.MODIFY_PHONE_STATE, null); |
Brad Ebinger | f6281ad | 2019-04-25 11:02:04 -0700 | [diff] [blame] | 1004 | //TODO: Also check for SHELL UID to restrict this method to testing only (b/131326259) |
Hall Liu | 506724b | 2018-10-22 18:16:14 -0700 | [diff] [blame] | 1005 | int phoneId = SubscriptionManager.getPhoneId(subscriptionId); |
| 1006 | if (!SubscriptionManager.isValidPhoneId(phoneId)) { |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 1007 | logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subscriptionId); |
Hall Liu | 506724b | 2018-10-22 18:16:14 -0700 | [diff] [blame] | 1008 | return; |
| 1009 | } |
Torbjorn Eklund | 723527a | 2019-02-13 11:16:25 +0100 | [diff] [blame] | 1010 | overrideConfig(mOverrideConfigs, phoneId, overrides); |
Hall Liu | 506724b | 2018-10-22 18:16:14 -0700 | [diff] [blame] | 1011 | |
Torbjorn Eklund | 723527a | 2019-02-13 11:16:25 +0100 | [diff] [blame] | 1012 | if (persistent) { |
| 1013 | overrideConfig(mPersistentOverrideConfigs, phoneId, overrides); |
| 1014 | |
| 1015 | if (overrides != null) { |
| 1016 | final CarrierIdentifier carrierId = getCarrierIdentifierForPhoneId(phoneId); |
| 1017 | saveConfigToXml(mPlatformCarrierConfigPackage, OVERRIDE_PACKAGE_ADDITION, phoneId, |
| 1018 | carrierId, mPersistentOverrideConfigs[phoneId]); |
| 1019 | } else { |
| 1020 | final String iccid = getIccIdForPhoneId(phoneId); |
| 1021 | final int cid = getSpecificCarrierIdForPhoneId(phoneId); |
| 1022 | String fileName = getFilenameForConfig(mPlatformCarrierConfigPackage, |
| 1023 | OVERRIDE_PACKAGE_ADDITION, iccid, cid); |
| 1024 | File fileToDelete = new File(mContext.getFilesDir(), fileName); |
| 1025 | fileToDelete.delete(); |
| 1026 | } |
Hall Liu | 506724b | 2018-10-22 18:16:14 -0700 | [diff] [blame] | 1027 | } |
Brad Ebinger | c9d1fa1 | 2019-04-17 15:21:18 -0700 | [diff] [blame] | 1028 | notifySubscriptionInfoUpdater(phoneId); |
Hall Liu | 506724b | 2018-10-22 18:16:14 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
Torbjorn Eklund | 723527a | 2019-02-13 11:16:25 +0100 | [diff] [blame] | 1031 | private void overrideConfig(@NonNull PersistableBundle[] currentOverrides, int phoneId, |
| 1032 | @Nullable PersistableBundle overrides) { |
| 1033 | if (overrides == null) { |
| 1034 | currentOverrides[phoneId] = new PersistableBundle(); |
| 1035 | } else if (currentOverrides[phoneId] == null) { |
| 1036 | currentOverrides[phoneId] = overrides; |
| 1037 | } else { |
| 1038 | currentOverrides[phoneId].putAll(overrides); |
| 1039 | } |
| 1040 | } |
| 1041 | |
Hall Liu | 506724b | 2018-10-22 18:16:14 -0700 | [diff] [blame] | 1042 | @Override |
Jonathan Basseri | 8603035 | 2015-06-08 12:27:56 -0700 | [diff] [blame] | 1043 | public void notifyConfigChangedForSubId(int subId) { |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 1044 | int phoneId = SubscriptionManager.getPhoneId(subId); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 1045 | if (!SubscriptionManager.isValidPhoneId(phoneId)) { |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 1046 | logd("Ignore invalid phoneId: " + phoneId + " for subId: " + subId); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 1047 | return; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 1048 | } |
Jordan Liu | d7d57fe | 2019-04-16 12:29:43 -0700 | [diff] [blame] | 1049 | |
| 1050 | // Requires the calling app to be either a carrier privileged app for this subId or |
Junda Liu | fbd2bcb | 2016-06-15 11:15:42 -0700 | [diff] [blame] | 1051 | // system privileged app with MODIFY_PHONE_STATE permission. |
Jordan Liu | d7d57fe | 2019-04-16 12:29:43 -0700 | [diff] [blame] | 1052 | TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mContext, subId, |
| 1053 | "Require carrier privileges or MODIFY_PHONE_STATE permission."); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 1054 | |
| 1055 | // This method should block until deleting has completed, so that an error which prevents us |
| 1056 | // from clearing the cache is passed back to the carrier app. With the files successfully |
| 1057 | // deleted, this can return and we will eventually bind to the carrier app. |
Jordan Liu | d7d57fe | 2019-04-16 12:29:43 -0700 | [diff] [blame] | 1058 | String callingPackageName = mContext.getPackageManager().getNameForUid( |
| 1059 | Binder.getCallingUid()); |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 1060 | clearCachedConfigForPackage(callingPackageName); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 1061 | updateConfigForPhoneId(phoneId); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | @Override |
| 1065 | public void updateConfigForPhoneId(int phoneId, String simState) { |
Junda Liu | 1f2b34d | 2015-06-18 15:26:56 -0700 | [diff] [blame] | 1066 | mContext.enforceCallingOrSelfPermission( |
| 1067 | android.Manifest.permission.MODIFY_PHONE_STATE, null); |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 1068 | logdWithLocalLog("Update config for phoneId: " + phoneId + " simState: " + simState); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 1069 | if (!SubscriptionManager.isValidPhoneId(phoneId)) { |
| 1070 | return; |
| 1071 | } |
| 1072 | // requires Java 7 for switch on string. |
| 1073 | switch (simState) { |
| 1074 | case IccCardConstants.INTENT_VALUE_ICC_ABSENT: |
| 1075 | case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR: |
Junda Liu | 6f5fddf | 2016-05-24 16:14:41 -0700 | [diff] [blame] | 1076 | case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED: |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 1077 | case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN: |
Malcolm Chen | db66de1 | 2019-12-09 18:57:07 -0800 | [diff] [blame] | 1078 | case IccCardConstants.INTENT_VALUE_ICC_NOT_READY: |
Junda Liu | 9f2d271 | 2015-05-15 14:22:45 -0700 | [diff] [blame] | 1079 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1)); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 1080 | break; |
| 1081 | case IccCardConstants.INTENT_VALUE_ICC_LOADED: |
| 1082 | case IccCardConstants.INTENT_VALUE_ICC_LOCKED: |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 1083 | updateConfigForPhoneId(phoneId); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 1084 | break; |
| 1085 | } |
| 1086 | } |
| 1087 | |
Junda Liu | 43d723a | 2015-05-12 17:23:45 -0700 | [diff] [blame] | 1088 | @Override |
Jeff Sharkey | a6fcfed | 2017-07-20 14:18:39 -0600 | [diff] [blame] | 1089 | public String getDefaultCarrierServicePackageName() { |
Shuo Qian | efdb796 | 2019-12-19 15:54:27 -0800 | [diff] [blame] | 1090 | mContext.enforceCallingOrSelfPermission( |
| 1091 | android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, |
| 1092 | "getDefaultCarrierServicePackageName"); |
Jeff Sharkey | a6fcfed | 2017-07-20 14:18:39 -0600 | [diff] [blame] | 1093 | return mPlatformCarrierConfigPackage; |
| 1094 | } |
| 1095 | |
Hunter Knepshield | d0ed621 | 2020-01-28 17:43:16 -0800 | [diff] [blame] | 1096 | private void unbindIfBound(Context context, CarrierServiceConnection conn, |
| 1097 | int phoneId) { |
| 1098 | if (mServiceBound[phoneId]) { |
| 1099 | mServiceBound[phoneId] = false; |
| 1100 | context.unbindService(conn); |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | /** |
| 1105 | * If {@code args} contains {@link #DUMP_ARG_REQUESTING_PACKAGE} and a following package name, |
| 1106 | * we'll also call {@link IBinder#dump} on the default carrier service (if bound) and the |
| 1107 | * specified carrier service (if bound). Typically, this is done for connectivity bug reports |
| 1108 | * where we don't call {@code dumpsys activity service all-non-platform} because that contains |
| 1109 | * too much info, but we still want to let carrier apps include their diagnostics. |
| 1110 | */ |
Jeff Sharkey | a6fcfed | 2017-07-20 14:18:39 -0600 | [diff] [blame] | 1111 | @Override |
Junda Liu | 43d723a | 2015-05-12 17:23:45 -0700 | [diff] [blame] | 1112 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 1113 | IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " "); |
Junda Liu | 43d723a | 2015-05-12 17:23:45 -0700 | [diff] [blame] | 1114 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 1115 | != PackageManager.PERMISSION_GRANTED) { |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 1116 | indentPW.println("Permission Denial: can't dump carrierconfig from from pid=" |
Junda Liu | 43d723a | 2015-05-12 17:23:45 -0700 | [diff] [blame] | 1117 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()); |
| 1118 | return; |
| 1119 | } |
Hunter Knepshield | eefb31b | 2020-02-19 15:30:54 -0800 | [diff] [blame] | 1120 | String requestingPackage = null; |
| 1121 | int requestingPackageIndex = ArrayUtils.indexOf(args, DUMP_ARG_REQUESTING_PACKAGE); |
| 1122 | if (requestingPackageIndex >= 0 && requestingPackageIndex < args.length - 1 |
| 1123 | && !TextUtils.isEmpty(args[requestingPackageIndex + 1])) { |
| 1124 | requestingPackage = args[requestingPackageIndex + 1]; |
| 1125 | // Throws a SecurityException if the caller is impersonating another app in an effort to |
| 1126 | // dump extra info (which may contain PII the caller doesn't have a right to). |
| 1127 | enforceCallerIsSystemOrRequestingPackage(requestingPackage); |
| 1128 | } |
| 1129 | |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 1130 | indentPW.println("CarrierConfigLoader: " + this); |
Hunter Knepshield | eefb31b | 2020-02-19 15:30:54 -0800 | [diff] [blame] | 1131 | for (int i = 0; i < TelephonyManager.from(mContext).getActiveModemCount(); i++) { |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 1132 | indentPW.println("Phone Id = " + i); |
shuoq | 26a3a4c | 2016-12-16 11:06:48 -0800 | [diff] [blame] | 1133 | // display default values in CarrierConfigManager |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 1134 | printConfig(CarrierConfigManager.getDefaultConfig(), indentPW, |
shuoq | 26a3a4c | 2016-12-16 11:06:48 -0800 | [diff] [blame] | 1135 | "Default Values from CarrierConfigManager"); |
shuoq | 26a3a4c | 2016-12-16 11:06:48 -0800 | [diff] [blame] | 1136 | // display ConfigFromDefaultApp |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 1137 | printConfig(mConfigFromDefaultApp[i], indentPW, "mConfigFromDefaultApp"); |
shuoq | 26a3a4c | 2016-12-16 11:06:48 -0800 | [diff] [blame] | 1138 | // display ConfigFromCarrierApp |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 1139 | printConfig(mConfigFromCarrierApp[i], indentPW, "mConfigFromCarrierApp"); |
| 1140 | printConfig(mPersistentOverrideConfigs[i], indentPW, "mPersistentOverrideConfigs"); |
| 1141 | printConfig(mOverrideConfigs[i], indentPW, "mOverrideConfigs"); |
shuoq | 26a3a4c | 2016-12-16 11:06:48 -0800 | [diff] [blame] | 1142 | } |
chen xu | db04c29 | 2019-03-26 17:01:15 -0700 | [diff] [blame] | 1143 | |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 1144 | indentPW.println("CarrierConfigLoadingLog="); |
| 1145 | mCarrierConfigLoadingLog.dump(fd, indentPW, args); |
Hunter Knepshield | d0ed621 | 2020-01-28 17:43:16 -0800 | [diff] [blame] | 1146 | |
Hunter Knepshield | eefb31b | 2020-02-19 15:30:54 -0800 | [diff] [blame] | 1147 | if (requestingPackage != null) { |
Hunter Knepshield | d0ed621 | 2020-01-28 17:43:16 -0800 | [diff] [blame] | 1148 | logd("Including default and requesting package " + requestingPackage |
| 1149 | + " carrier services in dump"); |
Hunter Knepshield | eefb31b | 2020-02-19 15:30:54 -0800 | [diff] [blame] | 1150 | indentPW.println(""); |
Hunter Knepshield | d0ed621 | 2020-01-28 17:43:16 -0800 | [diff] [blame] | 1151 | indentPW.println("Connected services"); |
| 1152 | dumpCarrierServiceIfBound(fd, indentPW, "Default config package", |
Hunter Knepshield | eefb31b | 2020-02-19 15:30:54 -0800 | [diff] [blame] | 1153 | mPlatformCarrierConfigPackage, false /* considerCarrierPrivileges */); |
| 1154 | dumpCarrierServiceIfBound(fd, indentPW, "Requesting package", requestingPackage, |
| 1155 | true /* considerCarrierPrivileges */); |
Hunter Knepshield | d0ed621 | 2020-01-28 17:43:16 -0800 | [diff] [blame] | 1156 | } |
shuoq | 26a3a4c | 2016-12-16 11:06:48 -0800 | [diff] [blame] | 1157 | } |
| 1158 | |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 1159 | private void printConfig(PersistableBundle configApp, IndentingPrintWriter indentPW, |
| 1160 | String name) { |
| 1161 | indentPW.increaseIndent(); |
shuoq | 26a3a4c | 2016-12-16 11:06:48 -0800 | [diff] [blame] | 1162 | if (configApp == null) { |
shuoq | 26a3a4c | 2016-12-16 11:06:48 -0800 | [diff] [blame] | 1163 | indentPW.println(name + " : null "); |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 1164 | indentPW.decreaseIndent(); |
| 1165 | indentPW.println(""); |
shuoq | 26a3a4c | 2016-12-16 11:06:48 -0800 | [diff] [blame] | 1166 | return; |
| 1167 | } |
shuoq | 26a3a4c | 2016-12-16 11:06:48 -0800 | [diff] [blame] | 1168 | indentPW.println(name + " : "); |
| 1169 | List<String> sortedKeys = new ArrayList<String>(configApp.keySet()); |
| 1170 | Collections.sort(sortedKeys); |
| 1171 | indentPW.increaseIndent(); |
| 1172 | indentPW.increaseIndent(); |
| 1173 | for (String key : sortedKeys) { |
| 1174 | if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) { |
| 1175 | indentPW.println(key + " = " + |
| 1176 | Arrays.toString((Object[]) configApp.get(key))); |
| 1177 | } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) { |
| 1178 | indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key))); |
| 1179 | } else { |
| 1180 | indentPW.println(key + " = " + configApp.get(key)); |
| 1181 | } |
Junda Liu | 43d723a | 2015-05-12 17:23:45 -0700 | [diff] [blame] | 1182 | } |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 1183 | indentPW.decreaseIndent(); |
| 1184 | indentPW.decreaseIndent(); |
| 1185 | indentPW.decreaseIndent(); |
| 1186 | indentPW.println(""); |
Junda Liu | 43d723a | 2015-05-12 17:23:45 -0700 | [diff] [blame] | 1187 | } |
| 1188 | |
Hunter Knepshield | e601f43 | 2020-02-19 10:16:04 -0800 | [diff] [blame] | 1189 | /** |
| 1190 | * Passes without problem when one of these conditions is true: |
| 1191 | * - The caller is a privileged UID (e.g. for dumpstate.cpp generating a bug report, where the |
| 1192 | * system knows the true caller plumbed in through the {@link android.os.BugreportManager} API). |
| 1193 | * - The caller's UID matches the supplied package. |
| 1194 | * |
| 1195 | * @throws SecurityException if none of the above conditions are met. |
| 1196 | */ |
| 1197 | private void enforceCallerIsSystemOrRequestingPackage(String requestingPackage) |
| 1198 | throws SecurityException { |
| 1199 | final int callingUid = Binder.getCallingUid(); |
| 1200 | if (callingUid == Process.ROOT_UID || callingUid == Process.SYSTEM_UID |
| 1201 | || callingUid == Process.SHELL_UID || callingUid == Process.PHONE_UID) { |
| 1202 | // Bug reports (dumpstate.cpp) run as SHELL, and let some other privileged UIDs through |
| 1203 | // as well. |
| 1204 | return; |
| 1205 | } |
| 1206 | // An app is trying to dump extra detail, block it if they aren't who they claim to be. |
| 1207 | AppOpsManager appOps = mContext.getSystemService(AppOpsManager.class); |
| 1208 | if (appOps == null) { |
| 1209 | throw new SecurityException("No AppOps"); |
| 1210 | } |
| 1211 | // Will throw a SecurityException if the UID and package don't match. |
| 1212 | appOps.checkPackage(callingUid, requestingPackage); |
| 1213 | } |
| 1214 | |
Hunter Knepshield | eefb31b | 2020-02-19 15:30:54 -0800 | [diff] [blame] | 1215 | /** |
| 1216 | * Searches for one or more appropriate {@link CarrierService} instances to dump based on the |
| 1217 | * current connections. |
| 1218 | * |
| 1219 | * @param targetPkgName the target package name to dump carrier services for |
| 1220 | * @param considerCarrierPrivileges if true, allow a carrier service to be dumped if it shares |
| 1221 | * carrier privileges with {@code targetPkgName}; |
| 1222 | * otherwise, only dump a carrier service if it is {@code |
| 1223 | * targetPkgName} |
| 1224 | */ |
Hunter Knepshield | d0ed621 | 2020-01-28 17:43:16 -0800 | [diff] [blame] | 1225 | private void dumpCarrierServiceIfBound(FileDescriptor fd, IndentingPrintWriter indentPW, |
Hunter Knepshield | eefb31b | 2020-02-19 15:30:54 -0800 | [diff] [blame] | 1226 | String prefix, String targetPkgName, boolean considerCarrierPrivileges) { |
Hunter Knepshield | d0ed621 | 2020-01-28 17:43:16 -0800 | [diff] [blame] | 1227 | // Null package is possible if it's early in the boot process, there was a recent crash, we |
| 1228 | // loaded the config from XML most recently, or a SIM slot is empty. Carrier apps with |
| 1229 | // long-lived bindings should typically get dumped here regardless. Even if an app is being |
| 1230 | // used for multiple phoneIds, we assume that it's smart enough to handle that on its own, |
| 1231 | // and that in most cases we'd just be dumping duplicate information and bloating a report. |
| 1232 | indentPW.increaseIndent(); |
Hunter Knepshield | eefb31b | 2020-02-19 15:30:54 -0800 | [diff] [blame] | 1233 | indentPW.println(prefix + " : " + targetPkgName); |
| 1234 | Set<String> dumpedPkgNames = new ArraySet<>(mServiceConnection.length); |
Hunter Knepshield | d0ed621 | 2020-01-28 17:43:16 -0800 | [diff] [blame] | 1235 | for (CarrierServiceConnection connection : mServiceConnection) { |
Hunter Knepshield | eefb31b | 2020-02-19 15:30:54 -0800 | [diff] [blame] | 1236 | if (connection == null || !SubscriptionManager.isValidPhoneId(connection.phoneId) |
| 1237 | || TextUtils.isEmpty(connection.pkgName)) { |
| 1238 | continue; |
Hunter Knepshield | d0ed621 | 2020-01-28 17:43:16 -0800 | [diff] [blame] | 1239 | } |
Hunter Knepshield | eefb31b | 2020-02-19 15:30:54 -0800 | [diff] [blame] | 1240 | final String servicePkgName = connection.pkgName; |
| 1241 | // Note: we intentionally ignore system components here because we should NOT match the |
| 1242 | // shell caller that's typically used for bug reports via non-BugreportManager triggers. |
| 1243 | final boolean exactPackageMatch = TextUtils.equals(targetPkgName, servicePkgName); |
| 1244 | final boolean carrierPrivilegesMatch = |
| 1245 | considerCarrierPrivileges && hasCarrierPrivileges(targetPkgName, |
| 1246 | connection.phoneId); |
| 1247 | if (!exactPackageMatch && !carrierPrivilegesMatch) continue; |
| 1248 | // Make sure this service is actually alive before trying to dump it. We don't pay |
| 1249 | // attention to mServiceBound[connection.phoneId] because typically carrier apps will |
| 1250 | // request long-lived bindings, and even if we unbind the app, it may still be alive due |
| 1251 | // to CarrierServiceBindHelper. Pull it out as a reference so even if it gets set to |
| 1252 | // null within the ServiceConnection during unbinding we can avoid an NPE. |
| 1253 | final IBinder service = connection.service; |
| 1254 | if (service == null || !service.isBinderAlive() || !service.pingBinder()) continue; |
| 1255 | // We've got a live service. Last check is just to make sure we don't dump a package |
| 1256 | // multiple times. |
| 1257 | if (!dumpedPkgNames.add(servicePkgName)) continue; |
| 1258 | if (!exactPackageMatch) { |
| 1259 | logd(targetPkgName + " has carrier privileges on phoneId " + connection.phoneId |
| 1260 | + ", service provided by " + servicePkgName); |
| 1261 | indentPW.increaseIndent(); |
| 1262 | indentPW.println("Proxy : " + servicePkgName); |
| 1263 | indentPW.decreaseIndent(); |
| 1264 | } |
| 1265 | // Flush before we let the app output anything to ensure correct ordering of output. |
| 1266 | // Internally, Binder#dump calls flush on its printer after finishing so we don't |
| 1267 | // need to do anything after. |
| 1268 | indentPW.flush(); |
| 1269 | try { |
| 1270 | logd("Dumping " + servicePkgName); |
| 1271 | // We don't need to give the carrier service any args. |
| 1272 | connection.service.dump(fd, null /* args */); |
| 1273 | logd("Done with " + servicePkgName); |
| 1274 | } catch (RemoteException e) { |
| 1275 | logd("RemoteException from " + servicePkgName, e); |
| 1276 | indentPW.increaseIndent(); |
| 1277 | indentPW.println("RemoteException"); |
| 1278 | indentPW.increaseIndent(); |
| 1279 | e.printStackTrace(indentPW); |
| 1280 | indentPW.decreaseIndent(); |
| 1281 | indentPW.decreaseIndent(); |
| 1282 | // We won't retry this package again because now it's in dumpedPkgNames. |
| 1283 | } |
| 1284 | indentPW.println(""); |
Jordan Liu | 46ed5db | 2020-01-28 08:55:20 -0800 | [diff] [blame] | 1285 | } |
Hunter Knepshield | eefb31b | 2020-02-19 15:30:54 -0800 | [diff] [blame] | 1286 | if (dumpedPkgNames.isEmpty()) { |
| 1287 | indentPW.increaseIndent(); |
| 1288 | indentPW.println("Not bound"); |
| 1289 | indentPW.decreaseIndent(); |
| 1290 | indentPW.println(""); |
| 1291 | } |
Hunter Knepshield | d0ed621 | 2020-01-28 17:43:16 -0800 | [diff] [blame] | 1292 | indentPW.decreaseIndent(); |
Hunter Knepshield | eefb31b | 2020-02-19 15:30:54 -0800 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | private boolean hasCarrierPrivileges(String pkgName, int phoneId) { |
| 1296 | int[] subIds = SubscriptionManager.getSubId(phoneId); |
| 1297 | if (ArrayUtils.isEmpty(subIds)) { |
| 1298 | return false; |
| 1299 | } |
| 1300 | return TelephonyManager.from(mContext).createForSubscriptionId( |
| 1301 | subIds[0]).checkCarrierPrivilegesForPackage(pkgName) |
| 1302 | == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS; |
Jordan Liu | 46ed5db | 2020-01-28 08:55:20 -0800 | [diff] [blame] | 1303 | } |
| 1304 | |
Zach Johnson | 36d7aab | 2015-05-22 15:43:00 -0700 | [diff] [blame] | 1305 | private class CarrierServiceConnection implements ServiceConnection { |
Hunter Knepshield | eefb31b | 2020-02-19 15:30:54 -0800 | [diff] [blame] | 1306 | final int phoneId; |
| 1307 | final String pkgName; |
| 1308 | final int eventId; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 1309 | IBinder service; |
| 1310 | |
Hunter Knepshield | d0ed621 | 2020-01-28 17:43:16 -0800 | [diff] [blame] | 1311 | CarrierServiceConnection(int phoneId, String pkgName, int eventId) { |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 1312 | this.phoneId = phoneId; |
Hunter Knepshield | d0ed621 | 2020-01-28 17:43:16 -0800 | [diff] [blame] | 1313 | this.pkgName = pkgName; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 1314 | this.eventId = eventId; |
| 1315 | } |
| 1316 | |
| 1317 | @Override |
| 1318 | public void onServiceConnected(ComponentName name, IBinder service) { |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 1319 | logd("Connected to config app: " + name.flattenToShortString()); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 1320 | this.service = service; |
| 1321 | mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this)); |
| 1322 | } |
| 1323 | |
| 1324 | @Override |
| 1325 | public void onServiceDisconnected(ComponentName name) { |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 1326 | logd("Disconnected from config app: " + name.flattenToShortString()); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 1327 | this.service = null; |
Jordan Liu | 46ed5db | 2020-01-28 08:55:20 -0800 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | @Override |
| 1331 | public void onBindingDied(ComponentName name) { |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 1332 | logd("Binding died from config app: " + name.flattenToShortString()); |
Jordan Liu | 46ed5db | 2020-01-28 08:55:20 -0800 | [diff] [blame] | 1333 | this.service = null; |
Jordan Liu | 46ed5db | 2020-01-28 08:55:20 -0800 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | @Override |
| 1337 | public void onNullBinding(ComponentName name) { |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 1338 | logd("Null binding from config app: " + name.flattenToShortString()); |
Jordan Liu | 46ed5db | 2020-01-28 08:55:20 -0800 | [diff] [blame] | 1339 | this.service = null; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver { |
| 1344 | @Override |
| 1345 | public void onReceive(Context context, Intent intent) { |
| 1346 | String action = intent.getAction(); |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 1347 | boolean replace = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); |
| 1348 | // If replace is true, only care ACTION_PACKAGE_REPLACED. |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 1349 | if (replace && !Intent.ACTION_PACKAGE_REPLACED.equals(action)) { |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 1350 | return; |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 1351 | } |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 1352 | |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 1353 | switch (action) { |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 1354 | case Intent.ACTION_BOOT_COMPLETED: |
| 1355 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null)); |
| 1356 | break; |
| 1357 | |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 1358 | case Intent.ACTION_PACKAGE_ADDED: |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 1359 | case Intent.ACTION_PACKAGE_REMOVED: |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 1360 | case Intent.ACTION_PACKAGE_REPLACED: |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 1361 | int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 1362 | String packageName = mContext.getPackageManager().getNameForUid(uid); |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 1363 | if (packageName != null) { |
| 1364 | // We don't have a phoneId for arg1. |
| 1365 | mHandler.sendMessage( |
| 1366 | mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, packageName)); |
| 1367 | } |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 1368 | break; |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 1369 | } |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 1370 | } |
| 1371 | } |
| 1372 | |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 1373 | private void logd(String msg) { |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 1374 | Log.d(LOG_TAG, msg); |
| 1375 | } |
| 1376 | |
Hunter Knepshield | d0ed621 | 2020-01-28 17:43:16 -0800 | [diff] [blame] | 1377 | private void logd(String msg, Throwable tr) { |
| 1378 | Log.d(LOG_TAG, msg, tr); |
| 1379 | } |
| 1380 | |
Hunter Knepshield | 9f091fc | 2020-01-28 17:41:43 -0800 | [diff] [blame] | 1381 | private void logdWithLocalLog(String msg) { |
chen xu | db04c29 | 2019-03-26 17:01:15 -0700 | [diff] [blame] | 1382 | Log.d(LOG_TAG, msg); |
| 1383 | mCarrierConfigLoadingLog.log(msg); |
| 1384 | } |
| 1385 | |
| 1386 | private void loge(String msg) { |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 1387 | Log.e(LOG_TAG, msg); |
chen xu | db04c29 | 2019-03-26 17:01:15 -0700 | [diff] [blame] | 1388 | mCarrierConfigLoadingLog.log(msg); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 1389 | } |
| 1390 | } |