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; |
Sudheer Shanka | bb03c63 | 2016-11-10 15:29:41 -0800 | [diff] [blame] | 23 | import android.app.ActivityManager; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 24 | import android.content.BroadcastReceiver; |
| 25 | import android.content.ComponentName; |
| 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
| 28 | import android.content.IntentFilter; |
| 29 | import android.content.ServiceConnection; |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 30 | import android.content.SharedPreferences; |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 31 | import android.content.pm.PackageInfo; |
Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 32 | import android.content.pm.PackageManager; |
Junda Liu | 43d723a | 2015-05-12 17:23:45 -0700 | [diff] [blame] | 33 | import android.os.Binder; |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 34 | import android.os.Build; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 35 | import android.os.Bundle; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 36 | import android.os.Handler; |
| 37 | import android.os.IBinder; |
| 38 | import android.os.Message; |
Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 39 | import android.os.PersistableBundle; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 40 | import android.os.RemoteException; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 41 | import android.os.ResultReceiver; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 42 | import android.os.ServiceManager; |
| 43 | import android.os.UserHandle; |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 44 | import android.preference.PreferenceManager; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 45 | import android.service.carrier.CarrierIdentifier; |
Zach Johnson | 36d7aab | 2015-05-22 15:43:00 -0700 | [diff] [blame] | 46 | import android.service.carrier.CarrierService; |
| 47 | import android.service.carrier.ICarrierService; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 48 | import android.telephony.CarrierConfigManager; |
| 49 | import android.telephony.SubscriptionManager; |
| 50 | import android.telephony.TelephonyManager; |
| 51 | import android.util.Log; |
| 52 | |
| 53 | import com.android.internal.telephony.ICarrierConfigLoader; |
| 54 | import com.android.internal.telephony.IccCardConstants; |
| 55 | import com.android.internal.telephony.Phone; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 56 | import com.android.internal.telephony.PhoneFactory; |
Jeff Davidson | a8e4e24 | 2018-03-15 17:16:18 -0700 | [diff] [blame] | 57 | import com.android.internal.telephony.TelephonyPermissions; |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 58 | import com.android.internal.util.FastXmlSerializer; |
shuoq | 26a3a4c | 2016-12-16 11:06:48 -0800 | [diff] [blame] | 59 | import com.android.internal.util.IndentingPrintWriter; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 60 | |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 61 | import org.xmlpull.v1.XmlPullParser; |
| 62 | import org.xmlpull.v1.XmlPullParserException; |
| 63 | import org.xmlpull.v1.XmlPullParserFactory; |
| 64 | |
| 65 | import java.io.File; |
Junda Liu | 43d723a | 2015-05-12 17:23:45 -0700 | [diff] [blame] | 66 | import java.io.FileDescriptor; |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 67 | import java.io.FileInputStream; |
| 68 | import java.io.FileNotFoundException; |
| 69 | import java.io.FileOutputStream; |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 70 | import java.io.FilenameFilter; |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 71 | import java.io.IOException; |
Junda Liu | 43d723a | 2015-05-12 17:23:45 -0700 | [diff] [blame] | 72 | import java.io.PrintWriter; |
Naina Nalluri | 86fc7b0 | 2018-08-03 10:38:30 -0700 | [diff] [blame^] | 73 | import java.lang.Math; |
shuoq | 26a3a4c | 2016-12-16 11:06:48 -0800 | [diff] [blame] | 74 | import java.util.ArrayList; |
| 75 | import java.util.Arrays; |
| 76 | import java.util.Collections; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 77 | import java.util.List; |
| 78 | |
| 79 | /** |
| 80 | * CarrierConfigLoader binds to privileged carrier apps to fetch carrier config overlays. |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 81 | */ |
| 82 | |
| 83 | public class CarrierConfigLoader extends ICarrierConfigLoader.Stub { |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 84 | private static final String LOG_TAG = "CarrierConfigLoader"; |
Meng Wang | 33ad2bc | 2017-03-16 20:21:20 -0700 | [diff] [blame] | 85 | |
| 86 | // Package name for platform carrier config app, bundled with system image. |
| 87 | private final String mPlatformCarrierConfigPackage; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 88 | |
| 89 | /** The singleton instance. */ |
| 90 | private static CarrierConfigLoader sInstance; |
| 91 | // The context for phone app, passed from PhoneGlobals. |
| 92 | private Context mContext; |
| 93 | // Carrier configs from default app, indexed by phoneID. |
Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 94 | private PersistableBundle[] mConfigFromDefaultApp; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 95 | // Carrier configs from privileged carrier config app, indexed by phoneID. |
Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 96 | private PersistableBundle[] mConfigFromCarrierApp; |
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; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 101 | |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 102 | // Broadcast receiver for Boot intents, register intent filter in construtor. |
| 103 | private final BroadcastReceiver mBootReceiver = new ConfigLoaderBroadcastReceiver(); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 104 | // Broadcast receiver for SIM and pkg intents, register intent filter in constructor. |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 105 | private final BroadcastReceiver mPackageReceiver = new ConfigLoaderBroadcastReceiver(); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 106 | |
| 107 | // Message codes; see mHandler below. |
| 108 | // Request from SubscriptionInfoUpdater when SIM becomes absent or error. |
| 109 | private static final int EVENT_CLEAR_CONFIG = 0; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 110 | // Has connected to default app. |
| 111 | private static final int EVENT_CONNECTED_TO_DEFAULT = 3; |
| 112 | // Has connected to carrier app. |
| 113 | private static final int EVENT_CONNECTED_TO_CARRIER = 4; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 114 | // Config has been loaded from default app (or cache). |
| 115 | private static final int EVENT_FETCH_DEFAULT_DONE = 5; |
| 116 | // Config has been loaded from carrier app (or cache). |
| 117 | private static final int EVENT_FETCH_CARRIER_DONE = 6; |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 118 | // Attempt to fetch from default app or read from XML. |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 119 | private static final int EVENT_DO_FETCH_DEFAULT = 7; |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 120 | // Attempt to fetch from carrier app or read from XML. |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 121 | private static final int EVENT_DO_FETCH_CARRIER = 8; |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 122 | // A package has been installed, uninstalled, or updated. |
| 123 | private static final int EVENT_PACKAGE_CHANGED = 9; |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 124 | // Bind timed out for the default app. |
| 125 | private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10; |
| 126 | // Bind timed out for a carrier app. |
| 127 | private static final int EVENT_BIND_CARRIER_TIMEOUT = 11; |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 128 | // Check if the system fingerprint has changed. |
| 129 | private static final int EVENT_CHECK_SYSTEM_UPDATE = 12; |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 130 | // Rerun carrier config binding after system is unlocked. |
| 131 | private static final int EVENT_SYSTEM_UNLOCKED = 13; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 132 | // Fetching config timed out from the default app. |
| 133 | private static final int EVENT_FETCH_DEFAULT_TIMEOUT = 14; |
| 134 | // Fetching config timed out from a carrier app. |
| 135 | private static final int EVENT_FETCH_CARRIER_TIMEOUT = 15; |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 136 | |
Junda Liu | 6cb4500 | 2016-03-22 17:18:20 -0700 | [diff] [blame] | 137 | private static final int BIND_TIMEOUT_MILLIS = 30000; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 138 | |
Naina Nalluri | 86fc7b0 | 2018-08-03 10:38:30 -0700 | [diff] [blame^] | 139 | // Length limit of gid1 for naming config file. |
| 140 | private static final int GID1_LENGTH_LIMIT = 20; |
| 141 | |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 142 | // Tags used for saving and restoring XML documents. |
| 143 | private static final String TAG_DOCUMENT = "carrier_config"; |
| 144 | private static final String TAG_VERSION = "package_version"; |
| 145 | private static final String TAG_BUNDLE = "bundle_data"; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 146 | |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 147 | // SharedPreferences key for last known build fingerprint. |
| 148 | private static final String KEY_FINGERPRINT = "build_fingerprint"; |
| 149 | |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 150 | // Handler to process various events. |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 151 | // |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 152 | // For each phoneId, the event sequence should be: |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 153 | // fetch default, connected to default, fetch default (async), fetch default done, |
| 154 | // fetch carrier, connected to carrier, fetch carrier (async), fetch carrier done. |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 155 | // |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 156 | // If there is a saved config file for either the default app or the carrier app, we skip |
| 157 | // binding to the app and go straight from fetch to loaded. |
| 158 | // |
| 159 | // At any time, at most one connection is active. If events are not in this order, previous |
| 160 | // connection will be unbound, so only latest event takes effect. |
| 161 | // |
| 162 | // We broadcast ACTION_CARRIER_CONFIG_CHANGED after: |
| 163 | // 1. loading from carrier app (even if read from a file) |
| 164 | // 2. loading from default app if there is no carrier app (even if read from a file) |
| 165 | // 3. clearing config (e.g. due to sim removal) |
| 166 | // 4. encountering bind or IPC error |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 167 | private class ConfigHandler extends Handler { |
| 168 | @Override |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 169 | public void handleMessage(Message msg) { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 170 | final int phoneId = msg.arg1; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 171 | log("mHandler: " + msg.what + " phoneId: " + phoneId); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 172 | switch (msg.what) { |
| 173 | case EVENT_CLEAR_CONFIG: |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 174 | { |
Sooraj Sasindran | 52119cd | 2017-10-23 16:24:28 -0700 | [diff] [blame] | 175 | /* Ignore clear configuration request if device is being shutdown. */ |
| 176 | Phone phone = PhoneFactory.getPhone(phoneId); |
| 177 | if (phone != null) { |
| 178 | if (phone.isShuttingDown()) { |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 183 | if (mConfigFromDefaultApp[phoneId] == null |
| 184 | && mConfigFromCarrierApp[phoneId] == null) break; |
Sooraj Sasindran | 52119cd | 2017-10-23 16:24:28 -0700 | [diff] [blame] | 185 | |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 186 | mConfigFromDefaultApp[phoneId] = null; |
| 187 | mConfigFromCarrierApp[phoneId] = null; |
| 188 | mServiceConnection[phoneId] = null; |
Amit Mahajan | f8088ab | 2018-03-02 15:24:07 -0800 | [diff] [blame] | 189 | broadcastConfigChangedIntent(phoneId, false); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 190 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 191 | } |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 192 | |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 193 | case EVENT_SYSTEM_UNLOCKED: |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 194 | { |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 195 | for (int i = 0; i < TelephonyManager.from(mContext).getPhoneCount(); ++i) { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 196 | // When user unlock device, we should only try to send broadcast again if we |
| 197 | // have sent it before unlock. This will avoid we try to load carrier config |
| 198 | // when SIM is still loading when unlock happens. |
Junda Liu | 03aad76 | 2017-07-21 13:32:17 -0700 | [diff] [blame] | 199 | if (mHasSentConfigChange[i]) { |
| 200 | updateConfigForPhoneId(i); |
| 201 | } |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 202 | } |
| 203 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 204 | } |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 205 | |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 206 | case EVENT_PACKAGE_CHANGED: |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 207 | { |
| 208 | final String carrierPackageName = (String) msg.obj; |
| 209 | // Only update if there are cached config removed to avoid updating config for |
| 210 | // unrelated packages. |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 211 | if (clearCachedConfigForPackage(carrierPackageName)) { |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 212 | int numPhones = TelephonyManager.from(mContext).getPhoneCount(); |
| 213 | for (int i = 0; i < numPhones; ++i) { |
| 214 | updateConfigForPhoneId(i); |
| 215 | } |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 216 | } |
| 217 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 218 | } |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 219 | |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 220 | case EVENT_DO_FETCH_DEFAULT: |
| 221 | { |
| 222 | final String iccid = getIccIdForPhoneId(phoneId); |
Naina Nalluri | 86fc7b0 | 2018-08-03 10:38:30 -0700 | [diff] [blame^] | 223 | final String gid1 = getGid1ForPhoneId(phoneId); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 224 | final PersistableBundle config = |
Naina Nalluri | 86fc7b0 | 2018-08-03 10:38:30 -0700 | [diff] [blame^] | 225 | restoreConfigFromXml(mPlatformCarrierConfigPackage, iccid, gid1); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 226 | if (config != null) { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 227 | log( |
| 228 | "Loaded config from XML. package=" |
| 229 | + mPlatformCarrierConfigPackage |
| 230 | + " phoneId=" |
| 231 | + phoneId); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 232 | mConfigFromDefaultApp[phoneId] = config; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 233 | Message newMsg = obtainMessage(EVENT_FETCH_DEFAULT_DONE, phoneId, -1); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 234 | newMsg.getData().putBoolean("loaded_from_xml", true); |
| 235 | mHandler.sendMessage(newMsg); |
| 236 | } else { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 237 | // No cached config, so fetch it from the default app. |
| 238 | if (bindToConfigPackage( |
| 239 | mPlatformCarrierConfigPackage, |
| 240 | phoneId, |
| 241 | EVENT_CONNECTED_TO_DEFAULT)) { |
| 242 | sendMessageDelayed( |
| 243 | obtainMessage(EVENT_BIND_DEFAULT_TIMEOUT, phoneId, -1), |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 244 | BIND_TIMEOUT_MILLIS); |
| 245 | } else { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 246 | // Send broadcast if bind fails. |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 247 | broadcastConfigChangedIntent(phoneId); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 248 | // TODO: We *must* call unbindService even if bindService returns false. |
| 249 | // (And possibly if SecurityException was thrown.) |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 250 | } |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 251 | } |
| 252 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 253 | } |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 254 | |
| 255 | case EVENT_CONNECTED_TO_DEFAULT: |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 256 | { |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 257 | removeMessages(EVENT_BIND_DEFAULT_TIMEOUT); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 258 | final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 259 | // If new service connection has been created, unbind. |
| 260 | if (mServiceConnection[phoneId] != conn || conn.service == null) { |
| 261 | mContext.unbindService(conn); |
| 262 | break; |
| 263 | } |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 264 | final CarrierIdentifier carrierId = getCarrierIdForPhoneId(phoneId); |
| 265 | final String iccid = getIccIdForPhoneId(phoneId); |
Naina Nalluri | 86fc7b0 | 2018-08-03 10:38:30 -0700 | [diff] [blame^] | 266 | final String gid1 = getGid1ForPhoneId(phoneId); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 267 | // ResultReceiver callback will execute in this Handler's thread. |
| 268 | final ResultReceiver resultReceiver = |
| 269 | new ResultReceiver(this) { |
| 270 | @Override |
| 271 | public void onReceiveResult(int resultCode, Bundle resultData) { |
| 272 | mContext.unbindService(conn); |
| 273 | // If new service connection has been created, this is stale. |
| 274 | if (mServiceConnection[phoneId] != conn) { |
| 275 | loge("Received response for stale request."); |
| 276 | return; |
| 277 | } |
| 278 | removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT); |
| 279 | if (resultCode == RESULT_ERROR || resultData == null) { |
| 280 | // On error, abort config fetching. |
| 281 | loge("Failed to get carrier config"); |
| 282 | broadcastConfigChangedIntent(phoneId); |
| 283 | return; |
| 284 | } |
| 285 | PersistableBundle config = |
| 286 | resultData.getParcelable(KEY_CONFIG_BUNDLE); |
Naina Nalluri | 86fc7b0 | 2018-08-03 10:38:30 -0700 | [diff] [blame^] | 287 | saveConfigToXml(mPlatformCarrierConfigPackage, |
| 288 | iccid, gid1, config); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 289 | mConfigFromDefaultApp[phoneId] = config; |
| 290 | sendMessage( |
| 291 | obtainMessage( |
| 292 | EVENT_FETCH_DEFAULT_DONE, phoneId, -1)); |
| 293 | } |
| 294 | }; |
| 295 | // Now fetch the config asynchronously from the ICarrierService. |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 296 | try { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 297 | ICarrierService carrierService = |
| 298 | ICarrierService.Stub.asInterface(conn.service); |
| 299 | carrierService.getCarrierConfig(carrierId, resultReceiver); |
| 300 | } catch (RemoteException e) { |
| 301 | loge("Failed to get carrier config: " + e.toString()); |
| 302 | mContext.unbindService(conn); |
| 303 | break; // So we don't set a timeout. |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 304 | } |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 305 | sendMessageDelayed( |
| 306 | obtainMessage(EVENT_FETCH_DEFAULT_TIMEOUT, phoneId, -1), |
| 307 | BIND_TIMEOUT_MILLIS); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 308 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 309 | } |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 310 | |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 311 | case EVENT_BIND_DEFAULT_TIMEOUT: |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 312 | case EVENT_FETCH_DEFAULT_TIMEOUT: |
| 313 | { |
| 314 | // If a ResponseReceiver callback is in the queue when this happens, we will |
| 315 | // unbind twice and throw an exception. |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 316 | mContext.unbindService(mServiceConnection[phoneId]); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 317 | removeMessages(EVENT_FETCH_DEFAULT_TIMEOUT); |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 318 | broadcastConfigChangedIntent(phoneId); |
| 319 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 320 | } |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 321 | |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 322 | case EVENT_FETCH_DEFAULT_DONE: |
| 323 | { |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 324 | // If we attempted to bind to the app, but the service connection is null, then |
| 325 | // config was cleared while we were waiting and we should not continue. |
| 326 | if (!msg.getData().getBoolean("loaded_from_xml", false) |
| 327 | && mServiceConnection[phoneId] == null) { |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 328 | break; |
| 329 | } |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 330 | final String carrierPackageName = getCarrierPackageForPhoneId(phoneId); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 331 | if (carrierPackageName != null) { |
| 332 | log("Found carrier config app: " + carrierPackageName); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 333 | sendMessage(obtainMessage(EVENT_DO_FETCH_CARRIER, phoneId)); |
Jonathan Basseri | b919e93 | 2015-05-27 16:53:08 +0000 | [diff] [blame] | 334 | } else { |
| 335 | broadcastConfigChangedIntent(phoneId); |
Jonathan Basseri | 4ae2e7c | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 336 | } |
| 337 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 338 | } |
Jonathan Basseri | 4ae2e7c | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 339 | |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 340 | case EVENT_DO_FETCH_CARRIER: |
| 341 | { |
| 342 | final String carrierPackageName = getCarrierPackageForPhoneId(phoneId); |
| 343 | final String iccid = getIccIdForPhoneId(phoneId); |
Naina Nalluri | 86fc7b0 | 2018-08-03 10:38:30 -0700 | [diff] [blame^] | 344 | final String gid1 = getGid1ForPhoneId(phoneId); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 345 | final PersistableBundle config = |
Naina Nalluri | 86fc7b0 | 2018-08-03 10:38:30 -0700 | [diff] [blame^] | 346 | restoreConfigFromXml(carrierPackageName, iccid, gid1); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 347 | if (config != null) { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 348 | log( |
| 349 | "Loaded config from XML. package=" |
| 350 | + carrierPackageName |
| 351 | + " phoneId=" |
| 352 | + phoneId); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 353 | mConfigFromCarrierApp[phoneId] = config; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 354 | Message newMsg = obtainMessage(EVENT_FETCH_CARRIER_DONE, phoneId, -1); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 355 | newMsg.getData().putBoolean("loaded_from_xml", true); |
| 356 | sendMessage(newMsg); |
| 357 | } else { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 358 | // No cached config, so fetch it from a carrier app. |
Jonathan Basseri | 40473a5 | 2015-08-14 14:36:29 -0700 | [diff] [blame] | 359 | if (carrierPackageName != null |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 360 | && bindToConfigPackage( |
| 361 | carrierPackageName, |
| 362 | phoneId, |
| 363 | EVENT_CONNECTED_TO_CARRIER)) { |
| 364 | sendMessageDelayed( |
| 365 | obtainMessage(EVENT_BIND_CARRIER_TIMEOUT, phoneId, -1), |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 366 | BIND_TIMEOUT_MILLIS); |
| 367 | } else { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 368 | // Send broadcast if bind fails. |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 369 | broadcastConfigChangedIntent(phoneId); |
| 370 | } |
| 371 | } |
| 372 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 373 | } |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 374 | |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 375 | case EVENT_CONNECTED_TO_CARRIER: |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 376 | { |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 377 | removeMessages(EVENT_BIND_CARRIER_TIMEOUT); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 378 | final CarrierServiceConnection conn = (CarrierServiceConnection) msg.obj; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 379 | // If new service connection has been created, unbind. |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 380 | if (mServiceConnection[phoneId] != conn || conn.service == null) { |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 381 | mContext.unbindService(conn); |
| 382 | break; |
| 383 | } |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 384 | final CarrierIdentifier carrierId = getCarrierIdForPhoneId(phoneId); |
| 385 | final String iccid = getIccIdForPhoneId(phoneId); |
Naina Nalluri | 86fc7b0 | 2018-08-03 10:38:30 -0700 | [diff] [blame^] | 386 | final String gid1 = getGid1ForPhoneId(phoneId); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 387 | // ResultReceiver callback will execute in this Handler's thread. |
| 388 | final ResultReceiver resultReceiver = |
| 389 | new ResultReceiver(this) { |
| 390 | @Override |
| 391 | public void onReceiveResult(int resultCode, Bundle resultData) { |
| 392 | mContext.unbindService(conn); |
| 393 | // If new service connection has been created, this is stale. |
| 394 | if (mServiceConnection[phoneId] != conn) { |
| 395 | loge("Received response for stale request."); |
| 396 | return; |
| 397 | } |
| 398 | removeMessages(EVENT_FETCH_CARRIER_TIMEOUT); |
| 399 | if (resultCode == RESULT_ERROR || resultData == null) { |
| 400 | // On error, abort config fetching. |
| 401 | loge("Failed to get carrier config"); |
| 402 | broadcastConfigChangedIntent(phoneId); |
| 403 | return; |
| 404 | } |
| 405 | PersistableBundle config = |
| 406 | resultData.getParcelable(KEY_CONFIG_BUNDLE); |
Naina Nalluri | 86fc7b0 | 2018-08-03 10:38:30 -0700 | [diff] [blame^] | 407 | saveConfigToXml(getCarrierPackageForPhoneId(phoneId), |
| 408 | iccid, gid1, config); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 409 | mConfigFromCarrierApp[phoneId] = config; |
| 410 | sendMessage( |
| 411 | obtainMessage( |
| 412 | EVENT_FETCH_CARRIER_DONE, phoneId, -1)); |
| 413 | } |
| 414 | }; |
| 415 | // Now fetch the config asynchronously from the ICarrierService. |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 416 | try { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 417 | ICarrierService carrierService = |
| 418 | ICarrierService.Stub.asInterface(conn.service); |
| 419 | carrierService.getCarrierConfig(carrierId, resultReceiver); |
| 420 | } catch (RemoteException e) { |
| 421 | loge("Failed to get carrier config: " + e.toString()); |
| 422 | mContext.unbindService(conn); |
| 423 | break; // So we don't set a timeout. |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 424 | } |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 425 | sendMessageDelayed( |
| 426 | obtainMessage(EVENT_FETCH_CARRIER_TIMEOUT, phoneId, -1), |
| 427 | BIND_TIMEOUT_MILLIS); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 428 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 429 | } |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 430 | |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 431 | case EVENT_BIND_CARRIER_TIMEOUT: |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 432 | case EVENT_FETCH_CARRIER_TIMEOUT: |
| 433 | { |
| 434 | // If a ResponseReceiver callback is in the queue when this happens, we will |
| 435 | // unbind twice and throw an exception. |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 436 | mContext.unbindService(mServiceConnection[phoneId]); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 437 | removeMessages(EVENT_FETCH_CARRIER_TIMEOUT); |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 438 | broadcastConfigChangedIntent(phoneId); |
| 439 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 440 | } |
Jonathan Basseri | 930701e | 2015-06-11 20:56:43 -0700 | [diff] [blame] | 441 | |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 442 | case EVENT_FETCH_CARRIER_DONE: |
| 443 | { |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 444 | // If we attempted to bind to the app, but the service connection is null, then |
| 445 | // config was cleared while we were waiting and we should not continue. |
| 446 | if (!msg.getData().getBoolean("loaded_from_xml", false) |
| 447 | && mServiceConnection[phoneId] == null) { |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 448 | break; |
| 449 | } |
| 450 | broadcastConfigChangedIntent(phoneId); |
| 451 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 452 | } |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 453 | |
| 454 | case EVENT_CHECK_SYSTEM_UPDATE: |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 455 | { |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 456 | SharedPreferences sharedPrefs = |
| 457 | PreferenceManager.getDefaultSharedPreferences(mContext); |
| 458 | final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null); |
| 459 | if (!Build.FINGERPRINT.equals(lastFingerprint)) { |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 460 | log( |
| 461 | "Build fingerprint changed. old: " |
| 462 | + lastFingerprint |
| 463 | + " new: " |
| 464 | + Build.FINGERPRINT); |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 465 | clearCachedConfigForPackage(null); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 466 | sharedPrefs |
| 467 | .edit() |
| 468 | .putString(KEY_FINGERPRINT, Build.FINGERPRINT) |
| 469 | .apply(); |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 470 | } |
| 471 | break; |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 472 | } |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 473 | } |
| 474 | } |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | private final Handler mHandler; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 478 | |
| 479 | /** |
| 480 | * Constructs a CarrierConfigLoader, registers it as a service, and registers a broadcast |
| 481 | * receiver for relevant events. |
| 482 | */ |
| 483 | private CarrierConfigLoader(Context context) { |
| 484 | mContext = context; |
Meng Wang | 33ad2bc | 2017-03-16 20:21:20 -0700 | [diff] [blame] | 485 | mPlatformCarrierConfigPackage = |
| 486 | mContext.getString(R.string.platform_carrier_config_package); |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 487 | mHandler = new ConfigHandler(); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 488 | |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 489 | IntentFilter bootFilter = new IntentFilter(); |
| 490 | bootFilter.addAction(Intent.ACTION_BOOT_COMPLETED); |
| 491 | context.registerReceiver(mBootReceiver, bootFilter); |
| 492 | |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 493 | // Register for package updates. Update app or uninstall app update will have all 3 intents, |
| 494 | // in the order or removed, added, replaced, all with extra_replace set to true. |
| 495 | IntentFilter pkgFilter = new IntentFilter(); |
| 496 | pkgFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 497 | pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 498 | pkgFilter.addAction(Intent.ACTION_PACKAGE_REPLACED); |
| 499 | pkgFilter.addDataScheme("package"); |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 500 | context.registerReceiverAsUser(mPackageReceiver, UserHandle.ALL, pkgFilter, null, null); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 501 | |
| 502 | int numPhones = TelephonyManager.from(context).getPhoneCount(); |
Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 503 | mConfigFromDefaultApp = new PersistableBundle[numPhones]; |
| 504 | mConfigFromCarrierApp = new PersistableBundle[numPhones]; |
Zach Johnson | 36d7aab | 2015-05-22 15:43:00 -0700 | [diff] [blame] | 505 | mServiceConnection = new CarrierServiceConnection[numPhones]; |
Junda Liu | 03aad76 | 2017-07-21 13:32:17 -0700 | [diff] [blame] | 506 | mHasSentConfigChange = new boolean[numPhones]; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 507 | // Make this service available through ServiceManager. |
| 508 | ServiceManager.addService(Context.CARRIER_CONFIG_SERVICE, this); |
| 509 | log("CarrierConfigLoader has started"); |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 510 | mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | /** |
| 514 | * Initialize the singleton CarrierConfigLoader instance. |
| 515 | * |
| 516 | * This is only done once, at startup, from {@link com.android.phone.PhoneApp#onCreate}. |
| 517 | */ |
| 518 | /* package */ |
| 519 | static CarrierConfigLoader init(Context context) { |
| 520 | synchronized (CarrierConfigLoader.class) { |
| 521 | if (sInstance == null) { |
| 522 | sInstance = new CarrierConfigLoader(context); |
| 523 | } else { |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 524 | Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 525 | } |
| 526 | return sInstance; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | private void broadcastConfigChangedIntent(int phoneId) { |
Amit Mahajan | f8088ab | 2018-03-02 15:24:07 -0800 | [diff] [blame] | 531 | broadcastConfigChangedIntent(phoneId, true); |
| 532 | } |
| 533 | |
| 534 | private void broadcastConfigChangedIntent(int phoneId, boolean addSubIdExtra) { |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 535 | Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED); |
Christopher Tate | 38f55eb | 2017-02-14 14:43:49 -0800 | [diff] [blame] | 536 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT | |
| 537 | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); |
Amit Mahajan | f8088ab | 2018-03-02 15:24:07 -0800 | [diff] [blame] | 538 | // Include subId extra only if SIM records are loaded |
| 539 | TelephonyManager telephonyManager = TelephonyManager.from(mContext); |
| 540 | int simApplicationState = telephonyManager.getSimApplicationState(); |
| 541 | if (addSubIdExtra && (simApplicationState != TelephonyManager.SIM_STATE_UNKNOWN |
| 542 | && simApplicationState != TelephonyManager.SIM_STATE_NOT_READY)) { |
| 543 | SubscriptionManager.putPhoneIdAndSubIdExtra(intent, phoneId); |
| 544 | } |
Amit Mahajan | b33bcda | 2018-01-24 12:56:44 -0800 | [diff] [blame] | 545 | intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, phoneId); |
Sudheer Shanka | bb03c63 | 2016-11-10 15:29:41 -0800 | [diff] [blame] | 546 | ActivityManager.broadcastStickyIntent(intent, UserHandle.USER_ALL); |
Junda Liu | 03aad76 | 2017-07-21 13:32:17 -0700 | [diff] [blame] | 547 | mHasSentConfigChange[phoneId] = true; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | /** Binds to the default or carrier config app. */ |
| 551 | private boolean bindToConfigPackage(String pkgName, int phoneId, int eventId) { |
| 552 | log("Binding to " + pkgName + " for phone " + phoneId); |
Zach Johnson | fca8a8d | 2015-06-19 18:21:37 -0700 | [diff] [blame] | 553 | Intent carrierService = new Intent(CarrierService.CARRIER_SERVICE_INTERFACE); |
Zach Johnson | 36d7aab | 2015-05-22 15:43:00 -0700 | [diff] [blame] | 554 | carrierService.setPackage(pkgName); |
| 555 | mServiceConnection[phoneId] = new CarrierServiceConnection(phoneId, eventId); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 556 | try { |
Zach Johnson | 36d7aab | 2015-05-22 15:43:00 -0700 | [diff] [blame] | 557 | return mContext.bindService(carrierService, mServiceConnection[phoneId], |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 558 | Context.BIND_AUTO_CREATE); |
| 559 | } catch (SecurityException ex) { |
| 560 | return false; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | private CarrierIdentifier getCarrierIdForPhoneId(int phoneId) { |
| 565 | String mcc = ""; |
| 566 | String mnc = ""; |
| 567 | String imsi = ""; |
| 568 | String gid1 = ""; |
| 569 | String gid2 = ""; |
| 570 | String spn = TelephonyManager.from(mContext).getSimOperatorNameForPhone(phoneId); |
| 571 | String simOperator = TelephonyManager.from(mContext).getSimOperatorNumericForPhone(phoneId); |
Jonathan Basseri | 1fa437c | 2015-04-20 11:08:18 -0700 | [diff] [blame] | 572 | // A valid simOperator should be 5 or 6 digits, depending on the length of the MNC. |
| 573 | if (simOperator != null && simOperator.length() >= 3) { |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 574 | mcc = simOperator.substring(0, 3); |
| 575 | mnc = simOperator.substring(3); |
| 576 | } |
| 577 | Phone phone = PhoneFactory.getPhone(phoneId); |
| 578 | if (phone != null) { |
| 579 | imsi = phone.getSubscriberId(); |
| 580 | gid1 = phone.getGroupIdLevel1(); |
Junda Liu | 7318353 | 2015-05-14 13:55:40 -0700 | [diff] [blame] | 581 | gid2 = phone.getGroupIdLevel2(); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | return new CarrierIdentifier(mcc, mnc, spn, imsi, gid1, gid2); |
| 585 | } |
| 586 | |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 587 | /** Returns the package name of a priveleged carrier app, or null if there is none. */ |
| 588 | private String getCarrierPackageForPhoneId(int phoneId) { |
| 589 | List<String> carrierPackageNames = TelephonyManager.from(mContext) |
| 590 | .getCarrierPackageNamesForIntentAndPhone( |
Zach Johnson | fca8a8d | 2015-06-19 18:21:37 -0700 | [diff] [blame] | 591 | new Intent(CarrierService.CARRIER_SERVICE_INTERFACE), phoneId); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 592 | if (carrierPackageNames != null && carrierPackageNames.size() > 0) { |
| 593 | return carrierPackageNames.get(0); |
| 594 | } else { |
| 595 | return null; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | private String getIccIdForPhoneId(int phoneId) { |
| 600 | if (!SubscriptionManager.isValidPhoneId(phoneId)) { |
| 601 | return null; |
| 602 | } |
| 603 | Phone phone = PhoneFactory.getPhone(phoneId); |
| 604 | if (phone == null) { |
| 605 | return null; |
| 606 | } |
| 607 | return phone.getIccSerialNumber(); |
| 608 | } |
| 609 | |
Naina Nalluri | 86fc7b0 | 2018-08-03 10:38:30 -0700 | [diff] [blame^] | 610 | private String getGid1ForPhoneId(int phoneId) { |
| 611 | if (!SubscriptionManager.isValidPhoneId(phoneId)) { |
| 612 | return null; |
| 613 | } |
| 614 | Phone phone = PhoneFactory.getPhone(phoneId); |
| 615 | if (phone == null) { |
| 616 | return null; |
| 617 | } |
| 618 | String gid1 = phone.getGroupIdLevel1(); |
| 619 | if (gid1 == null) { |
| 620 | return null; |
| 621 | } |
| 622 | return gid1.substring(0, Math.min(gid1.length(), GID1_LENGTH_LIMIT)); |
| 623 | } |
| 624 | |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 625 | /** |
| 626 | * Writes a bundle to an XML file. |
| 627 | * |
| 628 | * The bundle will be written to a file named after the package name and ICCID, so that it can |
| 629 | * be restored later with {@link @restoreConfigFromXml}. The XML output will include the bundle |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 630 | * and the current version of the specified package. |
| 631 | * |
| 632 | * In case of errors or invalid input, no file will be written. |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 633 | * |
| 634 | * @param packageName the name of the package from which we fetched this bundle. |
| 635 | * @param iccid the ICCID of the subscription for which this bundle was fetched. |
Naina Nalluri | 86fc7b0 | 2018-08-03 10:38:30 -0700 | [diff] [blame^] | 636 | * @param extras First 20 characters of gid1 of the subscription for which the bundle |
| 637 | * was fetched. |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 638 | * @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] | 639 | */ |
Naina Nalluri | 86fc7b0 | 2018-08-03 10:38:30 -0700 | [diff] [blame^] | 640 | private void saveConfigToXml(String packageName, String iccid, String extras, |
| 641 | PersistableBundle config) { |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 642 | if (packageName == null || iccid == null) { |
| 643 | loge("Cannot save config with null packageName or iccid."); |
| 644 | return; |
| 645 | } |
Junda Liu | 0259650 | 2016-11-15 13:46:43 -0800 | [diff] [blame] | 646 | // b/32668103 Only save to file if config isn't empty. |
| 647 | // In case of failure, not caching an empty bundle will |
| 648 | // try loading config again on next power on or sim loaded. |
| 649 | // Downside is for genuinely empty bundle, will bind and load |
| 650 | // on every power on. |
| 651 | if (config == null || config.isEmpty()) { |
| 652 | return; |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 655 | final String version = getPackageVersion(packageName); |
| 656 | if (version == null) { |
| 657 | loge("Failed to get package version for: " + packageName); |
| 658 | return; |
| 659 | } |
| 660 | |
| 661 | FileOutputStream outFile = null; |
| 662 | try { |
| 663 | outFile = new FileOutputStream( |
Naina Nalluri | 86fc7b0 | 2018-08-03 10:38:30 -0700 | [diff] [blame^] | 664 | new File(mContext.getFilesDir(), getFilenameForConfig(packageName, |
| 665 | iccid, extras))); |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 666 | FastXmlSerializer out = new FastXmlSerializer(); |
| 667 | out.setOutput(outFile, "utf-8"); |
| 668 | out.startDocument("utf-8", true); |
| 669 | out.startTag(null, TAG_DOCUMENT); |
| 670 | out.startTag(null, TAG_VERSION); |
| 671 | out.text(version); |
| 672 | out.endTag(null, TAG_VERSION); |
| 673 | out.startTag(null, TAG_BUNDLE); |
| 674 | config.saveToXml(out); |
| 675 | out.endTag(null, TAG_BUNDLE); |
| 676 | out.endTag(null, TAG_DOCUMENT); |
| 677 | out.endDocument(); |
| 678 | out.flush(); |
| 679 | outFile.close(); |
| 680 | } |
| 681 | catch (IOException e) { |
| 682 | loge(e.toString()); |
| 683 | } |
| 684 | catch (XmlPullParserException e) { |
| 685 | loge(e.toString()); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | /** |
| 690 | * Reads a bundle from an XML file. |
| 691 | * |
| 692 | * This restores a bundle that was written with {@link #saveConfigToXml}. This returns the saved |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 693 | * config bundle for the given package and ICCID. |
| 694 | * |
| 695 | * In case of errors, or if the saved config is from a different package version than the |
| 696 | * current version, then null will be returned. |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 697 | * |
| 698 | * @param packageName the name of the package from which we fetched this bundle. |
| 699 | * @param iccid the ICCID of the subscription for which this bundle was fetched. |
Naina Nalluri | 86fc7b0 | 2018-08-03 10:38:30 -0700 | [diff] [blame^] | 700 | * @param extras First 20 characters of gid1 of the subscription for which the bundle |
| 701 | * was fetched. |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 702 | * @return the bundle from the XML file. Returns null if there is no saved config, the saved |
| 703 | * version does not match, or reading config fails. |
| 704 | */ |
Naina Nalluri | 86fc7b0 | 2018-08-03 10:38:30 -0700 | [diff] [blame^] | 705 | private PersistableBundle restoreConfigFromXml(String packageName, String iccid, |
| 706 | String extras) { |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 707 | final String version = getPackageVersion(packageName); |
| 708 | if (version == null) { |
| 709 | loge("Failed to get package version for: " + packageName); |
| 710 | return null; |
| 711 | } |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 712 | if (packageName == null || iccid == null) { |
| 713 | loge("Cannot restore config with null packageName or iccid."); |
| 714 | return null; |
| 715 | } |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 716 | |
| 717 | PersistableBundle restoredBundle = null; |
| 718 | FileInputStream inFile = null; |
| 719 | try { |
| 720 | inFile = new FileInputStream( |
Naina Nalluri | 86fc7b0 | 2018-08-03 10:38:30 -0700 | [diff] [blame^] | 721 | new File(mContext.getFilesDir(), getFilenameForConfig(packageName, iccid, |
| 722 | extras))); |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 723 | XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser(); |
| 724 | parser.setInput(inFile, "utf-8"); |
| 725 | |
| 726 | int event; |
| 727 | while (((event = parser.next()) != XmlPullParser.END_DOCUMENT)) { |
| 728 | |
| 729 | if (event == XmlPullParser.START_TAG && TAG_VERSION.equals(parser.getName())) { |
| 730 | String savedVersion = parser.nextText(); |
| 731 | if (!version.equals(savedVersion)) { |
| 732 | log("Saved version mismatch: " + version + " vs " + savedVersion); |
| 733 | break; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | if (event == XmlPullParser.START_TAG && TAG_BUNDLE.equals(parser.getName())) { |
| 738 | restoredBundle = PersistableBundle.restoreFromXml(parser); |
| 739 | } |
| 740 | } |
| 741 | inFile.close(); |
| 742 | } |
| 743 | catch (FileNotFoundException e) { |
| 744 | loge(e.toString()); |
| 745 | } |
| 746 | catch (XmlPullParserException e) { |
| 747 | loge(e.toString()); |
| 748 | } |
| 749 | catch (IOException e) { |
| 750 | loge(e.toString()); |
| 751 | } |
| 752 | |
| 753 | return restoredBundle; |
| 754 | } |
| 755 | |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 756 | /** |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 757 | * Clears cached carrier config. |
| 758 | * This deletes all saved XML files associated with the given package name. If packageName is |
| 759 | * null, then it deletes all saved XML files. |
| 760 | * |
| 761 | * @param packageName the name of a carrier package, or null if all cached config should be |
| 762 | * cleared. |
| 763 | * @return true iff one or more files were deleted. |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 764 | */ |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 765 | private boolean clearCachedConfigForPackage(final String packageName) { |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 766 | File dir = mContext.getFilesDir(); |
| 767 | File[] packageFiles = dir.listFiles(new FilenameFilter() { |
| 768 | public boolean accept(File dir, String filename) { |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 769 | if (packageName != null) { |
| 770 | return filename.startsWith("carrierconfig-" + packageName + "-"); |
| 771 | } else { |
| 772 | return filename.startsWith("carrierconfig-"); |
| 773 | } |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 774 | } |
| 775 | }); |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 776 | if (packageFiles == null || packageFiles.length < 1) return false; |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 777 | for (File f : packageFiles) { |
| 778 | log("deleting " + f.getName()); |
| 779 | f.delete(); |
| 780 | } |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 781 | return true; |
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 | /** Builds a canonical file name for a config file. */ |
Naina Nalluri | 86fc7b0 | 2018-08-03 10:38:30 -0700 | [diff] [blame^] | 785 | private String getFilenameForConfig(@NonNull String packageName, @NonNull String iccid, |
| 786 | String extras) { |
| 787 | if (extras != null) { |
| 788 | return "carrierconfig-" + packageName + "-" + iccid + "-" + extras + ".xml"; |
| 789 | } |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 790 | return "carrierconfig-" + packageName + "-" + iccid + ".xml"; |
| 791 | } |
| 792 | |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 793 | /** 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] | 794 | private String getPackageVersion(String packageName) { |
| 795 | try { |
| 796 | PackageInfo info = mContext.getPackageManager().getPackageInfo(packageName, 0); |
Dianne Hackborn | b76ab20 | 2017-11-28 17:44:50 -0800 | [diff] [blame] | 797 | return Long.toString(info.getLongVersionCode()); |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 798 | } catch (PackageManager.NameNotFoundException e) { |
| 799 | return null; |
| 800 | } |
| 801 | } |
| 802 | |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 803 | /** |
| 804 | * Read up to date config. |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 805 | * |
| 806 | * This reads config bundles for the given phoneId. That means getting the latest bundle from |
| 807 | * the default app and a privileged carrier app, if present. This will not bind to an app if we |
| 808 | * have a saved config file to use instead. |
| 809 | */ |
| 810 | private void updateConfigForPhoneId(int phoneId) { |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 811 | // Clear in-memory cache for carrier app config, so when carrier app gets uninstalled, no |
| 812 | // stale config is left. |
| 813 | if (mConfigFromCarrierApp[phoneId] != null && |
| 814 | getCarrierPackageForPhoneId(phoneId) == null) { |
| 815 | mConfigFromCarrierApp[phoneId] = null; |
| 816 | } |
Jonathan Basseri | 65273c8 | 2017-07-25 15:08:42 -0700 | [diff] [blame] | 817 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_DO_FETCH_DEFAULT, phoneId, -1)); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | @Override public |
| 821 | @NonNull |
Malcolm Chen | 6d3d6b3 | 2018-03-01 13:58:03 -0800 | [diff] [blame] | 822 | PersistableBundle getConfigForSubId(int subId, String callingPackage) { |
| 823 | if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState( |
| 824 | mContext, subId, callingPackage, "getCarrierConfig")) { |
| 825 | return new PersistableBundle(); |
Amit Mahajan | 40b3fa5 | 2015-07-14 10:27:19 -0700 | [diff] [blame] | 826 | } |
Jeff Davidson | a8e4e24 | 2018-03-15 17:16:18 -0700 | [diff] [blame] | 827 | |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 828 | int phoneId = SubscriptionManager.getPhoneId(subId); |
Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 829 | PersistableBundle retConfig = CarrierConfigManager.getDefaultConfig(); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 830 | if (SubscriptionManager.isValidPhoneId(phoneId)) { |
Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 831 | PersistableBundle config = mConfigFromDefaultApp[phoneId]; |
yinxu | c5e2762 | 2017-11-29 15:08:50 -0800 | [diff] [blame] | 832 | if (config != null) { |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 833 | retConfig.putAll(config); |
yinxu | c5e2762 | 2017-11-29 15:08:50 -0800 | [diff] [blame] | 834 | retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true); |
| 835 | } |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 836 | config = mConfigFromCarrierApp[phoneId]; |
yinxu | c5e2762 | 2017-11-29 15:08:50 -0800 | [diff] [blame] | 837 | if (config != null) { |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 838 | retConfig.putAll(config); |
yinxu | c5e2762 | 2017-11-29 15:08:50 -0800 | [diff] [blame] | 839 | retConfig.putBoolean(CarrierConfigManager.KEY_CARRIER_CONFIG_APPLIED_BOOL, true); |
| 840 | } |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 841 | } |
| 842 | return retConfig; |
| 843 | } |
| 844 | |
| 845 | @Override |
Jonathan Basseri | 8603035 | 2015-06-08 12:27:56 -0700 | [diff] [blame] | 846 | public void notifyConfigChangedForSubId(int subId) { |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 847 | int phoneId = SubscriptionManager.getPhoneId(subId); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 848 | if (!SubscriptionManager.isValidPhoneId(phoneId)) { |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 849 | log("Ignore invalid phoneId: " + phoneId + " for subId: " + subId); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 850 | return; |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 851 | } |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 852 | String callingPackageName = mContext.getPackageManager().getNameForUid( |
| 853 | Binder.getCallingUid()); |
| 854 | // TODO: Check that the calling packages is privileged for subId specifically. |
| 855 | int privilegeStatus = TelephonyManager.from(mContext).checkCarrierPrivilegesForPackage( |
| 856 | callingPackageName); |
Junda Liu | fbd2bcb | 2016-06-15 11:15:42 -0700 | [diff] [blame] | 857 | // Requires the calling app to be either a carrier privileged app or |
| 858 | // system privileged app with MODIFY_PHONE_STATE permission. |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 859 | if (privilegeStatus != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) { |
Junda Liu | fbd2bcb | 2016-06-15 11:15:42 -0700 | [diff] [blame] | 860 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, |
| 861 | "Require carrier privileges or MODIFY_PHONE_STATE permission."); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | // This method should block until deleting has completed, so that an error which prevents us |
| 865 | // from clearing the cache is passed back to the carrier app. With the files successfully |
| 866 | // deleted, this can return and we will eventually bind to the carrier app. |
Jonathan Basseri | 7697cae | 2015-07-06 15:49:23 -0700 | [diff] [blame] | 867 | clearCachedConfigForPackage(callingPackageName); |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 868 | updateConfigForPhoneId(phoneId); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | @Override |
| 872 | public void updateConfigForPhoneId(int phoneId, String simState) { |
Junda Liu | 1f2b34d | 2015-06-18 15:26:56 -0700 | [diff] [blame] | 873 | mContext.enforceCallingOrSelfPermission( |
| 874 | android.Manifest.permission.MODIFY_PHONE_STATE, null); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 875 | log("update config for phoneId: " + phoneId + " simState: " + simState); |
| 876 | if (!SubscriptionManager.isValidPhoneId(phoneId)) { |
| 877 | return; |
| 878 | } |
| 879 | // requires Java 7 for switch on string. |
| 880 | switch (simState) { |
| 881 | case IccCardConstants.INTENT_VALUE_ICC_ABSENT: |
| 882 | case IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR: |
Junda Liu | 6f5fddf | 2016-05-24 16:14:41 -0700 | [diff] [blame] | 883 | case IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED: |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 884 | case IccCardConstants.INTENT_VALUE_ICC_UNKNOWN: |
Junda Liu | 9f2d271 | 2015-05-15 14:22:45 -0700 | [diff] [blame] | 885 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_CLEAR_CONFIG, phoneId, -1)); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 886 | break; |
| 887 | case IccCardConstants.INTENT_VALUE_ICC_LOADED: |
| 888 | case IccCardConstants.INTENT_VALUE_ICC_LOCKED: |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 889 | updateConfigForPhoneId(phoneId); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 890 | break; |
| 891 | } |
| 892 | } |
| 893 | |
Junda Liu | 43d723a | 2015-05-12 17:23:45 -0700 | [diff] [blame] | 894 | @Override |
Jeff Sharkey | a6fcfed | 2017-07-20 14:18:39 -0600 | [diff] [blame] | 895 | public String getDefaultCarrierServicePackageName() { |
| 896 | return mPlatformCarrierConfigPackage; |
| 897 | } |
| 898 | |
| 899 | @Override |
Junda Liu | 43d723a | 2015-05-12 17:23:45 -0700 | [diff] [blame] | 900 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 901 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 902 | != PackageManager.PERMISSION_GRANTED) { |
| 903 | pw.println("Permission Denial: can't dump carrierconfig from from pid=" |
| 904 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()); |
| 905 | return; |
| 906 | } |
| 907 | pw.println("CarrierConfigLoader: " + this); |
| 908 | for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) { |
shuoq | 26a3a4c | 2016-12-16 11:06:48 -0800 | [diff] [blame] | 909 | pw.println("Phone Id = " + i); |
| 910 | // display default values in CarrierConfigManager |
| 911 | printConfig(CarrierConfigManager.getDefaultConfig(), pw, |
| 912 | "Default Values from CarrierConfigManager"); |
| 913 | pw.println(""); |
| 914 | // display ConfigFromDefaultApp |
| 915 | printConfig(mConfigFromDefaultApp[i], pw, "mConfigFromDefaultApp"); |
| 916 | pw.println(""); |
| 917 | // display ConfigFromCarrierApp |
| 918 | printConfig(mConfigFromCarrierApp[i], pw, "mConfigFromCarrierApp"); |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | private void printConfig(PersistableBundle configApp, PrintWriter pw, String name) { |
| 923 | IndentingPrintWriter indentPW = new IndentingPrintWriter(pw, " "); |
| 924 | if (configApp == null) { |
| 925 | indentPW.increaseIndent(); |
| 926 | indentPW.println(name + " : null "); |
| 927 | return; |
| 928 | } |
| 929 | indentPW.increaseIndent(); |
| 930 | indentPW.println(name + " : "); |
| 931 | List<String> sortedKeys = new ArrayList<String>(configApp.keySet()); |
| 932 | Collections.sort(sortedKeys); |
| 933 | indentPW.increaseIndent(); |
| 934 | indentPW.increaseIndent(); |
| 935 | for (String key : sortedKeys) { |
| 936 | if (configApp.get(key) != null && configApp.get(key) instanceof Object[]) { |
| 937 | indentPW.println(key + " = " + |
| 938 | Arrays.toString((Object[]) configApp.get(key))); |
| 939 | } else if (configApp.get(key) != null && configApp.get(key) instanceof int[]) { |
| 940 | indentPW.println(key + " = " + Arrays.toString((int[]) configApp.get(key))); |
| 941 | } else { |
| 942 | indentPW.println(key + " = " + configApp.get(key)); |
| 943 | } |
Junda Liu | 43d723a | 2015-05-12 17:23:45 -0700 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | |
Zach Johnson | 36d7aab | 2015-05-22 15:43:00 -0700 | [diff] [blame] | 947 | private class CarrierServiceConnection implements ServiceConnection { |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 948 | int phoneId; |
| 949 | int eventId; |
| 950 | IBinder service; |
| 951 | |
Zach Johnson | 36d7aab | 2015-05-22 15:43:00 -0700 | [diff] [blame] | 952 | public CarrierServiceConnection(int phoneId, int eventId) { |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 953 | this.phoneId = phoneId; |
| 954 | this.eventId = eventId; |
| 955 | } |
| 956 | |
| 957 | @Override |
| 958 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 959 | log("Connected to config app: " + name.flattenToString()); |
| 960 | this.service = service; |
| 961 | mHandler.sendMessage(mHandler.obtainMessage(eventId, phoneId, -1, this)); |
| 962 | } |
| 963 | |
| 964 | @Override |
| 965 | public void onServiceDisconnected(ComponentName name) { |
| 966 | this.service = null; |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | private class ConfigLoaderBroadcastReceiver extends BroadcastReceiver { |
| 971 | @Override |
| 972 | public void onReceive(Context context, Intent intent) { |
| 973 | String action = intent.getAction(); |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 974 | boolean replace = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); |
| 975 | // If replace is true, only care ACTION_PACKAGE_REPLACED. |
| 976 | if (replace && !Intent.ACTION_PACKAGE_REPLACED.equals(action)) |
| 977 | return; |
| 978 | |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 979 | switch (action) { |
Nanxi Chen | 3d67050 | 2016-03-17 16:32:09 -0700 | [diff] [blame] | 980 | case Intent.ACTION_BOOT_COMPLETED: |
| 981 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_UNLOCKED, null)); |
| 982 | break; |
| 983 | |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 984 | case Intent.ACTION_PACKAGE_ADDED: |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 985 | case Intent.ACTION_PACKAGE_REMOVED: |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 986 | case Intent.ACTION_PACKAGE_REPLACED: |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 987 | int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 988 | String packageName = mContext.getPackageManager().getNameForUid(uid); |
Junda Liu | 8a8a53a | 2015-05-15 16:19:57 -0700 | [diff] [blame] | 989 | if (packageName != null) { |
| 990 | // We don't have a phoneId for arg1. |
| 991 | mHandler.sendMessage( |
| 992 | mHandler.obtainMessage(EVENT_PACKAGE_CHANGED, packageName)); |
| 993 | } |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 994 | break; |
Jonathan Basseri | 1f743c9 | 2015-05-15 00:19:46 -0700 | [diff] [blame] | 995 | } |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 996 | } |
| 997 | } |
| 998 | |
| 999 | private static void log(String msg) { |
Jonathan Basseri | 6b50e9f | 2015-05-12 20:18:31 -0700 | [diff] [blame] | 1000 | Log.d(LOG_TAG, msg); |
| 1001 | } |
| 1002 | |
| 1003 | private static void loge(String msg) { |
| 1004 | Log.e(LOG_TAG, msg); |
Jonathan Basseri | 6465afd | 2015-02-25 13:05:57 -0800 | [diff] [blame] | 1005 | } |
| 1006 | } |