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