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