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