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