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