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