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