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