John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
| 17 | package com.android.systemui.statusbar.policy; |
| 18 | |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 19 | import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED; |
Malcolm Chen | 0c63c4d | 2020-02-25 18:13:40 -0800 | [diff] [blame] | 20 | import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR; |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 21 | import static android.net.wifi.WifiManager.TrafficStateCallback.DATA_ACTIVITY_IN; |
| 22 | import static android.net.wifi.WifiManager.TrafficStateCallback.DATA_ACTIVITY_INOUT; |
| 23 | import static android.net.wifi.WifiManager.TrafficStateCallback.DATA_ACTIVITY_NONE; |
| 24 | import static android.net.wifi.WifiManager.TrafficStateCallback.DATA_ACTIVITY_OUT; |
Malcolm Chen | 50ba963 | 2019-02-26 16:49:56 -0800 | [diff] [blame] | 25 | import static android.telephony.PhoneStateListener.LISTEN_ACTIVE_DATA_SUBSCRIPTION_ID_CHANGE; |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 26 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 27 | import android.content.BroadcastReceiver; |
| 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
| 30 | import android.content.IntentFilter; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 31 | import android.content.res.Configuration; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 32 | import android.content.res.Resources; |
| 33 | import android.net.ConnectivityManager; |
Anarghya Mitra | 678722f | 2018-05-11 14:29:54 -0700 | [diff] [blame] | 34 | import android.net.Network; |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 35 | import android.net.NetworkCapabilities; |
Fabian Kozynski | 57ba519 | 2020-04-01 15:24:12 -0400 | [diff] [blame] | 36 | import android.net.NetworkScoreManager; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 37 | import android.net.wifi.WifiManager; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 38 | import android.os.AsyncTask; |
| 39 | import android.os.Bundle; |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 40 | import android.os.Handler; |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 41 | import android.os.Looper; |
Wenting Xiong | 63364fe | 2017-02-07 19:12:27 +0800 | [diff] [blame] | 42 | import android.os.PersistableBundle; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 43 | import android.provider.Settings; |
Wenting Xiong | 63364fe | 2017-02-07 19:12:27 +0800 | [diff] [blame] | 44 | import android.telephony.CarrierConfigManager; |
Amit Mahajan | 75eab47 | 2020-01-29 10:47:48 -0800 | [diff] [blame] | 45 | import android.telephony.CellSignalStrength; |
Malcolm Chen | 50ba963 | 2019-02-26 16:49:56 -0800 | [diff] [blame] | 46 | import android.telephony.PhoneStateListener; |
Jason Monk | b0808aa | 2015-07-22 16:34:36 -0400 | [diff] [blame] | 47 | import android.telephony.ServiceState; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 48 | import android.telephony.SubscriptionInfo; |
| 49 | import android.telephony.SubscriptionManager; |
| 50 | import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 51 | import android.telephony.TelephonyManager; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 52 | import android.text.TextUtils; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 53 | import android.util.Log; |
John Spurlock | cf053c1 | 2015-05-14 16:33:59 -0400 | [diff] [blame] | 54 | import android.util.MathUtils; |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 55 | import android.util.SparseArray; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 56 | |
Evan Laird | 470ae84 | 2019-04-09 16:24:27 -0400 | [diff] [blame] | 57 | import com.android.internal.annotations.GuardedBy; |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 58 | import com.android.internal.annotations.VisibleForTesting; |
Jason Monk | f668d7c | 2016-01-14 10:38:41 -0500 | [diff] [blame] | 59 | import com.android.settingslib.net.DataUsageController; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 60 | import com.android.systemui.DemoMode; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 61 | import com.android.systemui.Dumpable; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 62 | import com.android.systemui.R; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 63 | import com.android.systemui.broadcast.BroadcastDispatcher; |
Dave Mankoff | 00e8a2f | 2019-12-18 16:59:49 -0500 | [diff] [blame] | 64 | import com.android.systemui.dagger.qualifiers.Background; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 65 | import com.android.systemui.settings.CurrentUserTracker; |
| 66 | import com.android.systemui.statusbar.policy.DeviceProvisionedController.DeviceProvisionedListener; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 67 | |
| 68 | import java.io.FileDescriptor; |
| 69 | import java.io.PrintWriter; |
| 70 | import java.util.ArrayList; |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 71 | import java.util.BitSet; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 72 | import java.util.Collections; |
| 73 | import java.util.Comparator; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 74 | import java.util.List; |
| 75 | import java.util.Locale; |
| 76 | |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 77 | import javax.inject.Inject; |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 78 | import javax.inject.Singleton; |
| 79 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 80 | /** Platform implementation of the network controller. **/ |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 81 | @Singleton |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 82 | public class NetworkControllerImpl extends BroadcastReceiver |
Dave Mankoff | bba732d | 2019-11-20 13:04:54 -0500 | [diff] [blame] | 83 | implements NetworkController, DemoMode, DataUsageController.NetworkNameProvider, Dumpable { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 84 | // debug |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 85 | static final String TAG = "NetworkController"; |
| 86 | static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 87 | // additional diagnostics, but not logspew |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 88 | static final boolean CHATTY = Log.isLoggable(TAG + "Chat", Log.DEBUG); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 89 | |
Jason Monk | 66845a3 | 2015-08-03 11:09:41 -0400 | [diff] [blame] | 90 | private static final int EMERGENCY_NO_CONTROLLERS = 0; |
| 91 | private static final int EMERGENCY_FIRST_CONTROLLER = 100; |
| 92 | private static final int EMERGENCY_VOICE_CONTROLLER = 200; |
| 93 | private static final int EMERGENCY_NO_SUB = 300; |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 94 | private static final int EMERGENCY_ASSUMED_VOICE_CONTROLLER = 400; |
Jason Monk | 66845a3 | 2015-08-03 11:09:41 -0400 | [diff] [blame] | 95 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 96 | private final Context mContext; |
| 97 | private final TelephonyManager mPhone; |
| 98 | private final WifiManager mWifiManager; |
| 99 | private final ConnectivityManager mConnectivityManager; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 100 | private final SubscriptionManager mSubscriptionManager; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 101 | private final boolean mHasMobileDataFeature; |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 102 | private final SubscriptionDefaults mSubDefaults; |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 103 | private final DataSaverController mDataSaverController; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 104 | private final CurrentUserTracker mUserTracker; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 105 | private final BroadcastDispatcher mBroadcastDispatcher; |
Evan Laird | 470ae84 | 2019-04-09 16:24:27 -0400 | [diff] [blame] | 106 | private final Object mLock = new Object(); |
Jason Monk | 3aaabd7 | 2014-12-12 11:11:44 -0500 | [diff] [blame] | 107 | private Config mConfig; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 108 | |
Evan Laird | 470ae84 | 2019-04-09 16:24:27 -0400 | [diff] [blame] | 109 | private PhoneStateListener mPhoneStateListener; |
Malcolm Chen | 50ba963 | 2019-02-26 16:49:56 -0800 | [diff] [blame] | 110 | private int mActiveMobileDataSubscription = SubscriptionManager.INVALID_SUBSCRIPTION_ID; |
| 111 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 112 | // Subcontrollers. |
| 113 | @VisibleForTesting |
| 114 | final WifiSignalController mWifiSignalController; |
Lorenzo Colitti | d46bb58 | 2015-04-27 20:32:01 +0900 | [diff] [blame] | 115 | |
| 116 | @VisibleForTesting |
| 117 | final EthernetSignalController mEthernetSignalController; |
| 118 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 119 | @VisibleForTesting |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 120 | final SparseArray<MobileSignalController> mMobileSignalControllers = new SparseArray<>(); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 121 | // When no SIMs are around at setup, and one is added later, it seems to default to the first |
| 122 | // SIM for most actions. This may be null if there aren't any SIMs around. |
| 123 | private MobileSignalController mDefaultSignalController; |
Jason Monk | 0e2400f | 2014-11-21 11:08:55 -0500 | [diff] [blame] | 124 | private final AccessPointControllerImpl mAccessPoints; |
Jason Monk | f668d7c | 2016-01-14 10:38:41 -0500 | [diff] [blame] | 125 | private final DataUsageController mDataUsageController; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 126 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 127 | private boolean mInetCondition; // Used for Logging and demo. |
Lorenzo Colitti | 23dd176 | 2014-09-10 22:58:54 +0900 | [diff] [blame] | 128 | |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 129 | // BitSets indicating which network transport types (e.g., TRANSPORT_WIFI, TRANSPORT_MOBILE) are |
| 130 | // connected and validated, respectively. |
| 131 | private final BitSet mConnectedTransports = new BitSet(); |
| 132 | private final BitSet mValidatedTransports = new BitSet(); |
| 133 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 134 | // States that don't belong to a subcontroller. |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 135 | private boolean mAirplaneMode = false; |
Jason Monk | 1ff7766 | 2017-09-29 11:17:05 -0400 | [diff] [blame] | 136 | private boolean mHasNoSubs; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 137 | private Locale mLocale = null; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 138 | // This list holds our ordering. |
John Spurlock | cf053c1 | 2015-05-14 16:33:59 -0400 | [diff] [blame] | 139 | private List<SubscriptionInfo> mCurrentSubscriptions = new ArrayList<>(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 140 | |
Jason Monk | c014dec | 2014-12-12 11:49:55 -0500 | [diff] [blame] | 141 | @VisibleForTesting |
| 142 | boolean mListening; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 143 | |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 144 | // The current user ID. |
| 145 | private int mCurrentUserId; |
| 146 | |
Jason Monk | 8fcab35 | 2015-06-29 10:57:00 -0400 | [diff] [blame] | 147 | private OnSubscriptionsChangedListener mSubscriptionListener; |
| 148 | |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 149 | // Handler that all broadcasts are received on. |
| 150 | private final Handler mReceiverHandler; |
| 151 | // Handler that all callbacks are made on. |
| 152 | private final CallbackHandler mCallbackHandler; |
| 153 | |
Jason Monk | 66845a3 | 2015-08-03 11:09:41 -0400 | [diff] [blame] | 154 | private int mEmergencySource; |
| 155 | private boolean mIsEmergency; |
| 156 | |
Jason Monk | b0808aa | 2015-07-22 16:34:36 -0400 | [diff] [blame] | 157 | @VisibleForTesting |
| 158 | ServiceState mLastServiceState; |
Jason Monk | fd57ea7 | 2016-04-29 13:37:58 -0400 | [diff] [blame] | 159 | private boolean mUserSetup; |
Jason Monk | 1ff7766 | 2017-09-29 11:17:05 -0400 | [diff] [blame] | 160 | private boolean mSimDetected; |
Malcolm Chen | 0c63c4d | 2020-02-25 18:13:40 -0800 | [diff] [blame] | 161 | private boolean mForceCellularValidated; |
Jason Monk | b0808aa | 2015-07-22 16:34:36 -0400 | [diff] [blame] | 162 | |
Dave Mankoff | bba732d | 2019-11-20 13:04:54 -0500 | [diff] [blame] | 163 | private ConfigurationController.ConfigurationListener mConfigurationListener = |
| 164 | new ConfigurationController.ConfigurationListener() { |
| 165 | @Override |
| 166 | public void onConfigChanged(Configuration newConfig) { |
| 167 | mConfig = Config.readConfig(mContext); |
| 168 | mReceiverHandler.post(() -> handleConfigurationChanged()); |
| 169 | } |
| 170 | }; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 171 | /** |
| 172 | * Construct this controller object and register for updates. |
| 173 | */ |
Jason Monk | 196d639 | 2018-12-20 13:25:34 -0500 | [diff] [blame] | 174 | @Inject |
Dave Mankoff | 00e8a2f | 2019-12-18 16:59:49 -0500 | [diff] [blame] | 175 | public NetworkControllerImpl(Context context, @Background Looper bgLooper, |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 176 | DeviceProvisionedController deviceProvisionedController, |
Fabian Kozynski | 57ba519 | 2020-04-01 15:24:12 -0400 | [diff] [blame] | 177 | BroadcastDispatcher broadcastDispatcher, ConnectivityManager connectivityManager, |
| 178 | TelephonyManager telephonyManager, WifiManager wifiManager, |
| 179 | NetworkScoreManager networkScoreManager) { |
| 180 | this(context, connectivityManager, |
| 181 | telephonyManager, |
| 182 | wifiManager, |
| 183 | networkScoreManager, |
Sundeep Ghuman | 9d10a3c | 2017-06-16 00:05:16 +0000 | [diff] [blame] | 184 | SubscriptionManager.from(context), Config.readConfig(context), bgLooper, |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 185 | new CallbackHandler(), |
Tony Mantler | 0edf09b | 2017-09-28 15:03:37 -0700 | [diff] [blame] | 186 | new AccessPointControllerImpl(context), |
Jason Monk | f668d7c | 2016-01-14 10:38:41 -0500 | [diff] [blame] | 187 | new DataUsageController(context), |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 188 | new SubscriptionDefaults(), |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 189 | deviceProvisionedController, |
| 190 | broadcastDispatcher); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 191 | mReceiverHandler.post(mRegisterListeners); |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | @VisibleForTesting |
| 195 | NetworkControllerImpl(Context context, ConnectivityManager connectivityManager, |
Sundeep Ghuman | 9d10a3c | 2017-06-16 00:05:16 +0000 | [diff] [blame] | 196 | TelephonyManager telephonyManager, WifiManager wifiManager, |
Fabian Kozynski | 57ba519 | 2020-04-01 15:24:12 -0400 | [diff] [blame] | 197 | NetworkScoreManager networkScoreManager, |
Sundeep Ghuman | 9d10a3c | 2017-06-16 00:05:16 +0000 | [diff] [blame] | 198 | SubscriptionManager subManager, Config config, Looper bgLooper, |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 199 | CallbackHandler callbackHandler, |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 200 | AccessPointControllerImpl accessPointController, |
Jason Monk | f668d7c | 2016-01-14 10:38:41 -0500 | [diff] [blame] | 201 | DataUsageController dataUsageController, |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 202 | SubscriptionDefaults defaultsHandler, |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 203 | DeviceProvisionedController deviceProvisionedController, |
| 204 | BroadcastDispatcher broadcastDispatcher) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 205 | mContext = context; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 206 | mConfig = config; |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 207 | mReceiverHandler = new Handler(bgLooper); |
| 208 | mCallbackHandler = callbackHandler; |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 209 | mDataSaverController = new DataSaverControllerImpl(context); |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 210 | mBroadcastDispatcher = broadcastDispatcher; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 211 | |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 212 | mSubscriptionManager = subManager; |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 213 | mSubDefaults = defaultsHandler; |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 214 | mConnectivityManager = connectivityManager; |
| 215 | mHasMobileDataFeature = |
| 216 | mConnectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 217 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 218 | // telephony |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 219 | mPhone = telephonyManager; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 220 | |
| 221 | // wifi |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 222 | mWifiManager = wifiManager; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 223 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 224 | mLocale = mContext.getResources().getConfiguration().locale; |
Jason Monk | 29f7a7b | 2014-11-17 14:40:56 -0500 | [diff] [blame] | 225 | mAccessPoints = accessPointController; |
Jason Monk | f668d7c | 2016-01-14 10:38:41 -0500 | [diff] [blame] | 226 | mDataUsageController = dataUsageController; |
| 227 | mDataUsageController.setNetworkController(this); |
| 228 | // TODO: Find a way to move this into DataUsageController. |
| 229 | mDataUsageController.setCallback(new DataUsageController.Callback() { |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 230 | @Override |
| 231 | public void onMobileDataEnabled(boolean enabled) { |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 232 | mCallbackHandler.setMobileDataEnabled(enabled); |
Evan Laird | 2ea99c1 | 2019-09-19 16:52:58 -0400 | [diff] [blame] | 233 | notifyControllersMobileDataChanged(); |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 234 | } |
| 235 | }); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 236 | mWifiSignalController = new WifiSignalController(mContext, mHasMobileDataFeature, |
Fabian Kozynski | 57ba519 | 2020-04-01 15:24:12 -0400 | [diff] [blame] | 237 | mCallbackHandler, this, mWifiManager, mConnectivityManager, networkScoreManager); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 238 | |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 239 | mEthernetSignalController = new EthernetSignalController(mContext, mCallbackHandler, this); |
Lorenzo Colitti | d46bb58 | 2015-04-27 20:32:01 +0900 | [diff] [blame] | 240 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 241 | // AIRPLANE_MODE_CHANGED is sent at boot; we've probably already missed it |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 242 | updateAirplaneMode(true /* force callback */); |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 243 | mUserTracker = new CurrentUserTracker(broadcastDispatcher) { |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 244 | @Override |
| 245 | public void onUserSwitched(int newUserId) { |
| 246 | NetworkControllerImpl.this.onUserSwitched(newUserId); |
| 247 | } |
| 248 | }; |
| 249 | mUserTracker.startTracking(); |
| 250 | deviceProvisionedController.addCallback(new DeviceProvisionedListener() { |
| 251 | @Override |
| 252 | public void onUserSetupChanged() { |
| 253 | setUserSetupComplete(deviceProvisionedController.isUserSetup( |
| 254 | deviceProvisionedController.getCurrentUser())); |
| 255 | } |
| 256 | }); |
Anarghya Mitra | 678722f | 2018-05-11 14:29:54 -0700 | [diff] [blame] | 257 | |
| 258 | ConnectivityManager.NetworkCallback callback = new ConnectivityManager.NetworkCallback(){ |
| 259 | private Network mLastNetwork; |
| 260 | private NetworkCapabilities mLastNetworkCapabilities; |
| 261 | |
| 262 | @Override |
| 263 | public void onCapabilitiesChanged( |
| 264 | Network network, NetworkCapabilities networkCapabilities) { |
| 265 | boolean lastValidated = (mLastNetworkCapabilities != null) && |
| 266 | mLastNetworkCapabilities.hasCapability(NET_CAPABILITY_VALIDATED); |
| 267 | boolean validated = |
| 268 | networkCapabilities.hasCapability(NET_CAPABILITY_VALIDATED); |
| 269 | |
| 270 | // This callback is invoked a lot (i.e. when RSSI changes), so avoid updating |
| 271 | // icons when connectivity state has remained the same. |
| 272 | if (network.equals(mLastNetwork) && |
| 273 | networkCapabilities.equalsTransportTypes(mLastNetworkCapabilities) && |
| 274 | validated == lastValidated) { |
| 275 | return; |
| 276 | } |
| 277 | mLastNetwork = network; |
| 278 | mLastNetworkCapabilities = networkCapabilities; |
| 279 | updateConnectivity(); |
| 280 | } |
| 281 | }; |
| 282 | // Even though this callback runs on the receiver handler thread which also processes the |
| 283 | // CONNECTIVITY_ACTION broadcasts, the broadcast and callback might come in at different |
| 284 | // times. This is safe since updateConnectivity() builds the list of transports from |
| 285 | // scratch. |
| 286 | // TODO: Move off of the deprecated CONNECTIVITY_ACTION broadcast and rely on callbacks |
| 287 | // exclusively for status bar icons. |
| 288 | mConnectivityManager.registerDefaultNetworkCallback(callback, mReceiverHandler); |
Evan Laird | 470ae84 | 2019-04-09 16:24:27 -0400 | [diff] [blame] | 289 | // Register the listener on our bg looper |
Jayachandran C | ff50c8e | 2019-10-30 16:41:59 -0700 | [diff] [blame] | 290 | mPhoneStateListener = new PhoneStateListener(mReceiverHandler::post) { |
Evan Laird | 470ae84 | 2019-04-09 16:24:27 -0400 | [diff] [blame] | 291 | @Override |
| 292 | public void onActiveDataSubscriptionIdChanged(int subId) { |
Malcolm Chen | 0c63c4d | 2020-02-25 18:13:40 -0800 | [diff] [blame] | 293 | // For data switching from A to B, we assume B is validated for up to 2 seconds iff: |
| 294 | // 1) A and B are in the same subscription group e.g. CBRS data switch. And |
| 295 | // 2) A was validated before the switch. |
| 296 | // This is to provide smooth transition for UI without showing cross during data |
| 297 | // switch. |
| 298 | if (keepCellularValidationBitInSwitch(mActiveMobileDataSubscription, subId)) { |
| 299 | if (DEBUG) Log.d(TAG, ": mForceCellularValidated to true."); |
| 300 | mForceCellularValidated = true; |
| 301 | mReceiverHandler.removeCallbacks(mClearForceValidated); |
| 302 | mReceiverHandler.postDelayed(mClearForceValidated, 2000); |
| 303 | } |
Evan Laird | 470ae84 | 2019-04-09 16:24:27 -0400 | [diff] [blame] | 304 | mActiveMobileDataSubscription = subId; |
| 305 | doUpdateMobileControllers(); |
| 306 | } |
| 307 | }; |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 308 | } |
| 309 | |
Malcolm Chen | 0c63c4d | 2020-02-25 18:13:40 -0800 | [diff] [blame] | 310 | private final Runnable mClearForceValidated = () -> { |
| 311 | if (DEBUG) Log.d(TAG, ": mClearForceValidated"); |
| 312 | mForceCellularValidated = false; |
| 313 | updateConnectivity(); |
| 314 | }; |
| 315 | |
| 316 | boolean isInGroupDataSwitch(int subId1, int subId2) { |
| 317 | SubscriptionInfo info1 = mSubscriptionManager.getActiveSubscriptionInfo(subId1); |
| 318 | SubscriptionInfo info2 = mSubscriptionManager.getActiveSubscriptionInfo(subId2); |
| 319 | return (info1 != null && info2 != null && info1.getGroupUuid() != null |
| 320 | && info1.getGroupUuid().equals(info2.getGroupUuid())); |
| 321 | } |
| 322 | |
| 323 | boolean keepCellularValidationBitInSwitch(int sourceSubId, int destSubId) { |
| 324 | return mValidatedTransports.get(TRANSPORT_CELLULAR) |
| 325 | && isInGroupDataSwitch(sourceSubId, destSubId); |
| 326 | } |
| 327 | |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 328 | public DataSaverController getDataSaverController() { |
| 329 | return mDataSaverController; |
| 330 | } |
| 331 | |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 332 | private void registerListeners() { |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 333 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 334 | MobileSignalController mobileSignalController = mMobileSignalControllers.valueAt(i); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 335 | mobileSignalController.registerListener(); |
| 336 | } |
Jason Monk | 8fcab35 | 2015-06-29 10:57:00 -0400 | [diff] [blame] | 337 | if (mSubscriptionListener == null) { |
| 338 | mSubscriptionListener = new SubListener(); |
| 339 | } |
Wink Saville | 071743f | 2015-01-12 17:11:04 -0800 | [diff] [blame] | 340 | mSubscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionListener); |
Malcolm Chen | 50ba963 | 2019-02-26 16:49:56 -0800 | [diff] [blame] | 341 | mPhone.listen(mPhoneStateListener, LISTEN_ACTIVE_DATA_SUBSCRIPTION_ID_CHANGE); |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 342 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 343 | // broadcasts |
| 344 | IntentFilter filter = new IntentFilter(); |
| 345 | filter.addAction(WifiManager.RSSI_CHANGED_ACTION); |
| 346 | filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); |
| 347 | filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); |
Daniel Bright | 350f7f9 | 2020-01-22 14:24:11 -0800 | [diff] [blame] | 348 | filter.addAction(Intent.ACTION_SIM_STATE_CHANGED); |
Jayachandran C | 041e769 | 2019-12-20 16:20:02 -0800 | [diff] [blame] | 349 | filter.addAction(TelephonyManager.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED); |
| 350 | filter.addAction(TelephonyManager.ACTION_DEFAULT_VOICE_SUBSCRIPTION_CHANGED); |
| 351 | filter.addAction(Intent.ACTION_SERVICE_STATE); |
Meng Wang | 57f5655 | 2020-01-17 16:58:40 -0800 | [diff] [blame] | 352 | filter.addAction(TelephonyManager.ACTION_SERVICE_PROVIDERS_UPDATED); |
Erik Kline | 32830cc | 2015-04-21 13:09:15 +0900 | [diff] [blame] | 353 | filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 354 | filter.addAction(ConnectivityManager.INET_CONDITION_ACTION); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 355 | filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
Wenting Xiong | 63364fe | 2017-02-07 19:12:27 +0800 | [diff] [blame] | 356 | filter.addAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED); |
Fabian Kozynski | 5e92c6f | 2020-01-03 13:56:17 -0500 | [diff] [blame] | 357 | mBroadcastDispatcher.registerReceiverWithHandler(this, filter, mReceiverHandler); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 358 | mListening = true; |
| 359 | |
| 360 | updateMobileControllers(); |
John Spurlock | b98f747 | 2014-07-08 17:09:42 -0400 | [diff] [blame] | 361 | } |
| 362 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 363 | private void unregisterListeners() { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 364 | mListening = false; |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 365 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 366 | MobileSignalController mobileSignalController = mMobileSignalControllers.valueAt(i); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 367 | mobileSignalController.unregisterListener(); |
| 368 | } |
Wink Saville | 071743f | 2015-01-12 17:11:04 -0800 | [diff] [blame] | 369 | mSubscriptionManager.removeOnSubscriptionsChangedListener(mSubscriptionListener); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 370 | mContext.unregisterReceiver(this); |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 371 | } |
| 372 | |
Jason Monk | 0e2400f | 2014-11-21 11:08:55 -0500 | [diff] [blame] | 373 | public int getConnectedWifiLevel() { |
| 374 | return mWifiSignalController.getState().level; |
| 375 | } |
| 376 | |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 377 | @Override |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 378 | public AccessPointController getAccessPointController() { |
| 379 | return mAccessPoints; |
| 380 | } |
| 381 | |
| 382 | @Override |
Jason Monk | f668d7c | 2016-01-14 10:38:41 -0500 | [diff] [blame] | 383 | public DataUsageController getMobileDataController() { |
| 384 | return mDataUsageController; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | public void addEmergencyListener(EmergencyListener listener) { |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 388 | mCallbackHandler.setListening(listener, true); |
| 389 | mCallbackHandler.setEmergencyCallsOnly(isEmergencyOnly()); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 390 | } |
| 391 | |
Jason Monk | 46dbfb4 | 2016-02-25 14:59:20 -0500 | [diff] [blame] | 392 | public void removeEmergencyListener(EmergencyListener listener) { |
| 393 | mCallbackHandler.setListening(listener, false); |
| 394 | } |
| 395 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 396 | public boolean hasMobileDataFeature() { |
| 397 | return mHasMobileDataFeature; |
| 398 | } |
| 399 | |
| 400 | public boolean hasVoiceCallingFeature() { |
| 401 | return mPhone.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE; |
| 402 | } |
| 403 | |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 404 | private MobileSignalController getDataController() { |
Fabian Kozynski | ccde55d | 2019-06-26 10:06:09 -0400 | [diff] [blame] | 405 | int dataSubId = mSubDefaults.getActiveDataSubId(); |
Wink Saville | a54bf65 | 2014-12-11 13:37:50 -0800 | [diff] [blame] | 406 | if (!SubscriptionManager.isValidSubscriptionId(dataSubId)) { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 407 | if (DEBUG) Log.e(TAG, "No data sim selected"); |
| 408 | return mDefaultSignalController; |
| 409 | } |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 410 | if (mMobileSignalControllers.indexOfKey(dataSubId) >= 0) { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 411 | return mMobileSignalControllers.get(dataSubId); |
| 412 | } |
Dan Sandler | c846632 | 2014-12-08 15:42:52 -0500 | [diff] [blame] | 413 | if (DEBUG) Log.e(TAG, "Cannot find controller for data sub: " + dataSubId); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 414 | return mDefaultSignalController; |
| 415 | } |
| 416 | |
Amin Shaikh | 329c828 | 2018-03-09 14:50:59 -0500 | [diff] [blame] | 417 | @Override |
Pavel Zhamaitsiak | 5504d39 | 2015-03-27 09:24:47 -0700 | [diff] [blame] | 418 | public String getMobileDataNetworkName() { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 419 | MobileSignalController controller = getDataController(); |
Pavel Zhamaitsiak | 5504d39 | 2015-03-27 09:24:47 -0700 | [diff] [blame] | 420 | return controller != null ? controller.getState().networkNameData : ""; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 421 | } |
| 422 | |
Fabian Kozynski | f1c32a0 | 2019-03-22 15:09:30 -0400 | [diff] [blame] | 423 | @Override |
| 424 | public int getNumberSubscriptions() { |
| 425 | return mMobileSignalControllers.size(); |
| 426 | } |
| 427 | |
Evan Laird | 2ea99c1 | 2019-09-19 16:52:58 -0400 | [diff] [blame] | 428 | boolean isDataControllerDisabled() { |
| 429 | MobileSignalController dataController = getDataController(); |
| 430 | if (dataController == null) { |
| 431 | return false; |
| 432 | } |
| 433 | |
| 434 | return dataController.isDataDisabled(); |
| 435 | } |
| 436 | |
| 437 | private void notifyControllersMobileDataChanged() { |
| 438 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 439 | MobileSignalController mobileSignalController = mMobileSignalControllers.valueAt(i); |
| 440 | mobileSignalController.onMobileDataChanged(); |
| 441 | } |
| 442 | } |
| 443 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 444 | public boolean isEmergencyOnly() { |
Jason Monk | b0808aa | 2015-07-22 16:34:36 -0400 | [diff] [blame] | 445 | if (mMobileSignalControllers.size() == 0) { |
| 446 | // When there are no active subscriptions, determine emengency state from last |
| 447 | // broadcast. |
Jason Monk | 66845a3 | 2015-08-03 11:09:41 -0400 | [diff] [blame] | 448 | mEmergencySource = EMERGENCY_NO_CONTROLLERS; |
Jason Monk | b0808aa | 2015-07-22 16:34:36 -0400 | [diff] [blame] | 449 | return mLastServiceState != null && mLastServiceState.isEmergencyOnly(); |
| 450 | } |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 451 | int voiceSubId = mSubDefaults.getDefaultVoiceSubId(); |
Wink Saville | a54bf65 | 2014-12-11 13:37:50 -0800 | [diff] [blame] | 452 | if (!SubscriptionManager.isValidSubscriptionId(voiceSubId)) { |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 453 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 454 | MobileSignalController mobileSignalController = mMobileSignalControllers.valueAt(i); |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 455 | if (!mobileSignalController.getState().isEmergency) { |
Jason Monk | 66845a3 | 2015-08-03 11:09:41 -0400 | [diff] [blame] | 456 | mEmergencySource = EMERGENCY_FIRST_CONTROLLER |
| 457 | + mobileSignalController.mSubscriptionInfo.getSubscriptionId(); |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 458 | if (DEBUG) Log.d(TAG, "Found emergency " + mobileSignalController.mTag); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 459 | return false; |
| 460 | } |
| 461 | } |
| 462 | } |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 463 | if (mMobileSignalControllers.indexOfKey(voiceSubId) >= 0) { |
Jason Monk | 66845a3 | 2015-08-03 11:09:41 -0400 | [diff] [blame] | 464 | mEmergencySource = EMERGENCY_VOICE_CONTROLLER + voiceSubId; |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 465 | if (DEBUG) Log.d(TAG, "Getting emergency from " + voiceSubId); |
| 466 | return mMobileSignalControllers.get(voiceSubId).getState().isEmergency; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 467 | } |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 468 | // If we have the wrong subId but there is only one sim anyway, assume it should be the |
| 469 | // default. |
| 470 | if (mMobileSignalControllers.size() == 1) { |
| 471 | mEmergencySource = EMERGENCY_ASSUMED_VOICE_CONTROLLER |
| 472 | + mMobileSignalControllers.keyAt(0); |
| 473 | if (DEBUG) Log.d(TAG, "Getting assumed emergency from " |
| 474 | + mMobileSignalControllers.keyAt(0)); |
| 475 | return mMobileSignalControllers.valueAt(0).getState().isEmergency; |
| 476 | } |
Dan Sandler | c846632 | 2014-12-08 15:42:52 -0500 | [diff] [blame] | 477 | if (DEBUG) Log.e(TAG, "Cannot find controller for voice sub: " + voiceSubId); |
Jason Monk | 66845a3 | 2015-08-03 11:09:41 -0400 | [diff] [blame] | 478 | mEmergencySource = EMERGENCY_NO_SUB + voiceSubId; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 479 | // Something is wrong, better assume we can't make calls... |
| 480 | return true; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 481 | } |
| 482 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 483 | /** |
| 484 | * Emergency status may have changed (triggered by MobileSignalController), |
| 485 | * so we should recheck and send out the state to listeners. |
| 486 | */ |
| 487 | void recalculateEmergency() { |
Jason Monk | 66845a3 | 2015-08-03 11:09:41 -0400 | [diff] [blame] | 488 | mIsEmergency = isEmergencyOnly(); |
| 489 | mCallbackHandler.setEmergencyCallsOnly(mIsEmergency); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 490 | } |
| 491 | |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 492 | public void addCallback(SignalCallback cb) { |
Jason Monk | e06b065 | 2016-03-02 16:35:27 -0500 | [diff] [blame] | 493 | cb.setSubs(mCurrentSubscriptions); |
| 494 | cb.setIsAirplaneMode(new IconState(mAirplaneMode, |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 495 | TelephonyIcons.FLIGHT_MODE_ICON, R.string.accessibility_airplane_mode, mContext)); |
Jason Monk | 1ff7766 | 2017-09-29 11:17:05 -0400 | [diff] [blame] | 496 | cb.setNoSims(mHasNoSubs, mSimDetected); |
Jason Monk | e06b065 | 2016-03-02 16:35:27 -0500 | [diff] [blame] | 497 | mWifiSignalController.notifyListeners(cb); |
| 498 | mEthernetSignalController.notifyListeners(cb); |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 499 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 500 | MobileSignalController mobileSignalController = mMobileSignalControllers.valueAt(i); |
Jason Monk | e06b065 | 2016-03-02 16:35:27 -0500 | [diff] [blame] | 501 | mobileSignalController.notifyListeners(cb); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 502 | } |
Jason Monk | e06b065 | 2016-03-02 16:35:27 -0500 | [diff] [blame] | 503 | mCallbackHandler.setListening(cb, true); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 504 | } |
| 505 | |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 506 | @Override |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 507 | public void removeCallback(SignalCallback cb) { |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 508 | mCallbackHandler.setListening(cb, false); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | @Override |
| 512 | public void setWifiEnabled(final boolean enabled) { |
| 513 | new AsyncTask<Void, Void, Void>() { |
| 514 | @Override |
| 515 | protected Void doInBackground(Void... args) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 516 | mWifiManager.setWifiEnabled(enabled); |
| 517 | return null; |
| 518 | } |
| 519 | }.execute(); |
| 520 | } |
| 521 | |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 522 | private void onUserSwitched(int newUserId) { |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 523 | mCurrentUserId = newUserId; |
| 524 | mAccessPoints.onUserSwitched(newUserId); |
| 525 | updateConnectivity(); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 529 | public void onReceive(Context context, Intent intent) { |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 530 | if (CHATTY) { |
| 531 | Log.d(TAG, "onReceive: intent=" + intent); |
| 532 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 533 | final String action = intent.getAction(); |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 534 | switch (action) { |
| 535 | case ConnectivityManager.CONNECTIVITY_ACTION: |
| 536 | case ConnectivityManager.INET_CONDITION_ACTION: |
| 537 | updateConnectivity(); |
| 538 | break; |
| 539 | case Intent.ACTION_AIRPLANE_MODE_CHANGED: |
| 540 | refreshLocale(); |
| 541 | updateAirplaneMode(false); |
| 542 | break; |
Jayachandran C | 041e769 | 2019-12-20 16:20:02 -0800 | [diff] [blame] | 543 | case TelephonyManager.ACTION_DEFAULT_VOICE_SUBSCRIPTION_CHANGED: |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 544 | // We are using different subs now, we might be able to make calls. |
Jason Monk | b0808aa | 2015-07-22 16:34:36 -0400 | [diff] [blame] | 545 | recalculateEmergency(); |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 546 | break; |
Jayachandran C | 041e769 | 2019-12-20 16:20:02 -0800 | [diff] [blame] | 547 | case TelephonyManager.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED: |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 548 | // Notify every MobileSignalController so they can know whether they are the |
| 549 | // data sim or not. |
| 550 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 551 | MobileSignalController controller = mMobileSignalControllers.valueAt(i); |
| 552 | controller.handleBroadcast(intent); |
Wenting Xiong | 63364fe | 2017-02-07 19:12:27 +0800 | [diff] [blame] | 553 | } |
Niklas Lindgren | a02964d | 2018-09-21 14:01:57 +0200 | [diff] [blame] | 554 | mConfig = Config.readConfig(mContext); |
| 555 | mReceiverHandler.post(this::handleConfigurationChanged); |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 556 | break; |
Daniel Bright | 350f7f9 | 2020-01-22 14:24:11 -0800 | [diff] [blame] | 557 | case Intent.ACTION_SIM_STATE_CHANGED: |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 558 | // Avoid rebroadcast because SysUI is direct boot aware. |
Meng Wang | 0e8d91a | 2020-01-17 14:58:01 -0800 | [diff] [blame] | 559 | if (intent.getBooleanExtra(Intent.EXTRA_REBROADCAST_ON_UNLOCK, false)) { |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 560 | break; |
| 561 | } |
| 562 | // Might have different subscriptions now. |
| 563 | updateMobileControllers(); |
| 564 | break; |
Jayachandran C | 041e769 | 2019-12-20 16:20:02 -0800 | [diff] [blame] | 565 | case Intent.ACTION_SERVICE_STATE: |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 566 | mLastServiceState = ServiceState.newFromBundle(intent.getExtras()); |
| 567 | if (mMobileSignalControllers.size() == 0) { |
| 568 | // If none of the subscriptions are active, we might need to recalculate |
| 569 | // emergency state. |
| 570 | recalculateEmergency(); |
| 571 | } |
| 572 | break; |
| 573 | case CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED: |
| 574 | mConfig = Config.readConfig(mContext); |
| 575 | mReceiverHandler.post(this::handleConfigurationChanged); |
| 576 | break; |
| 577 | default: |
Daniel Bright | 63514be | 2020-01-15 12:10:53 -0800 | [diff] [blame] | 578 | int subId = intent.getIntExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 579 | SubscriptionManager.INVALID_SUBSCRIPTION_ID); |
| 580 | if (SubscriptionManager.isValidSubscriptionId(subId)) { |
| 581 | if (mMobileSignalControllers.indexOfKey(subId) >= 0) { |
| 582 | mMobileSignalControllers.get(subId).handleBroadcast(intent); |
| 583 | } else { |
| 584 | // Can't find this subscription... We must be out of date. |
| 585 | updateMobileControllers(); |
| 586 | } |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 587 | } else { |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 588 | // No sub id, must be for the wifi. |
| 589 | mWifiSignalController.handleBroadcast(intent); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 590 | } |
Lucas Dupin | 5e0f0d2 | 2018-02-26 13:32:16 -0800 | [diff] [blame] | 591 | break; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 592 | } |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 593 | } |
| 594 | |
Jason Monk | 1c040db | 2015-07-20 09:45:54 -0400 | [diff] [blame] | 595 | @VisibleForTesting |
| 596 | void handleConfigurationChanged() { |
Malcolm Chen | 50ba963 | 2019-02-26 16:49:56 -0800 | [diff] [blame] | 597 | updateMobileControllers(); |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 598 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 599 | MobileSignalController controller = mMobileSignalControllers.valueAt(i); |
| 600 | controller.setConfiguration(mConfig); |
Jason Monk | 3aaabd7 | 2014-12-12 11:11:44 -0500 | [diff] [blame] | 601 | } |
| 602 | refreshLocale(); |
Jason Monk | 3aaabd7 | 2014-12-12 11:11:44 -0500 | [diff] [blame] | 603 | } |
| 604 | |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 605 | private void updateMobileControllers() { |
| 606 | if (!mListening) { |
| 607 | return; |
| 608 | } |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 609 | doUpdateMobileControllers(); |
| 610 | } |
| 611 | |
Malcolm Chen | 50ba963 | 2019-02-26 16:49:56 -0800 | [diff] [blame] | 612 | private void filterMobileSubscriptionInSameGroup(List<SubscriptionInfo> subscriptions) { |
Malcolm Chen | e6befe2 | 2020-02-25 13:34:09 -0800 | [diff] [blame] | 613 | if (subscriptions.size() == 2) { |
Malcolm Chen | 50ba963 | 2019-02-26 16:49:56 -0800 | [diff] [blame] | 614 | SubscriptionInfo info1 = subscriptions.get(0); |
| 615 | SubscriptionInfo info2 = subscriptions.get(1); |
| 616 | if (info1.getGroupUuid() != null && info1.getGroupUuid().equals(info2.getGroupUuid())) { |
| 617 | // If both subscriptions are primary, show both. |
| 618 | if (!info1.isOpportunistic() && !info2.isOpportunistic()) return; |
| 619 | |
| 620 | // If carrier required, always show signal bar of primary subscription. |
| 621 | // Otherwise, show whichever subscription is currently active for Internet. |
| 622 | boolean alwaysShowPrimary = CarrierConfigManager.getDefaultConfig() |
| 623 | .getBoolean(CarrierConfigManager |
| 624 | .KEY_ALWAYS_SHOW_PRIMARY_SIGNAL_BAR_IN_OPPORTUNISTIC_NETWORK_BOOLEAN); |
| 625 | if (alwaysShowPrimary) { |
| 626 | subscriptions.remove(info1.isOpportunistic() ? info1 : info2); |
| 627 | } else { |
| 628 | subscriptions.remove(info1.getSubscriptionId() == mActiveMobileDataSubscription |
| 629 | ? info2 : info1); |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 635 | @VisibleForTesting |
| 636 | void doUpdateMobileControllers() { |
Malcolm Chen | d2d400cb | 2018-12-21 17:11:10 -0800 | [diff] [blame] | 637 | List<SubscriptionInfo> subscriptions = mSubscriptionManager |
Sooraj Sasindran | 4046806 | 2020-01-06 19:10:50 -0800 | [diff] [blame] | 638 | .getCompleteActiveSubscriptionInfoList(); |
Jason Monk | c9f0571 | 2014-12-15 12:24:10 -0500 | [diff] [blame] | 639 | if (subscriptions == null) { |
| 640 | subscriptions = Collections.emptyList(); |
| 641 | } |
Malcolm Chen | 50ba963 | 2019-02-26 16:49:56 -0800 | [diff] [blame] | 642 | |
| 643 | filterMobileSubscriptionInSameGroup(subscriptions); |
| 644 | |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 645 | // If there have been no relevant changes to any of the subscriptions, we can leave as is. |
| 646 | if (hasCorrectMobileControllers(subscriptions)) { |
| 647 | // Even if the controllers are correct, make sure we have the right no sims state. |
| 648 | // Such as on boot, don't need any controllers, because there are no sims, |
| 649 | // but we still need to update the no sim state. |
| 650 | updateNoSims(); |
| 651 | return; |
| 652 | } |
Evan Laird | 470ae84 | 2019-04-09 16:24:27 -0400 | [diff] [blame] | 653 | synchronized (mLock) { |
| 654 | setCurrentSubscriptionsLocked(subscriptions); |
| 655 | } |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 656 | updateNoSims(); |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 657 | recalculateEmergency(); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 658 | } |
| 659 | |
Jason Monk | 21d05a0 | 2014-12-02 12:46:58 -0500 | [diff] [blame] | 660 | @VisibleForTesting |
| 661 | protected void updateNoSims() { |
Jason Monk | 1ff7766 | 2017-09-29 11:17:05 -0400 | [diff] [blame] | 662 | boolean hasNoSubs = mHasMobileDataFeature && mMobileSignalControllers.size() == 0; |
| 663 | boolean simDetected = hasAnySim(); |
| 664 | if (hasNoSubs != mHasNoSubs || simDetected != mSimDetected) { |
| 665 | mHasNoSubs = hasNoSubs; |
| 666 | mSimDetected = simDetected; |
| 667 | mCallbackHandler.setNoSims(mHasNoSubs, mSimDetected); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | |
Jason Monk | 1ff7766 | 2017-09-29 11:17:05 -0400 | [diff] [blame] | 671 | private boolean hasAnySim() { |
Jayachandran C | a43d0a7 | 2019-11-01 17:39:38 -0700 | [diff] [blame] | 672 | int simCount = mPhone.getActiveModemCount(); |
Jason Monk | 1ff7766 | 2017-09-29 11:17:05 -0400 | [diff] [blame] | 673 | for (int i = 0; i < simCount; i++) { |
| 674 | int state = mPhone.getSimState(i); |
| 675 | if (state != TelephonyManager.SIM_STATE_ABSENT |
| 676 | && state != TelephonyManager.SIM_STATE_UNKNOWN) { |
| 677 | return true; |
| 678 | } |
| 679 | } |
| 680 | return false; |
| 681 | } |
| 682 | |
Evan Laird | 470ae84 | 2019-04-09 16:24:27 -0400 | [diff] [blame] | 683 | @GuardedBy("mLock") |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 684 | @VisibleForTesting |
Evan Laird | 470ae84 | 2019-04-09 16:24:27 -0400 | [diff] [blame] | 685 | public void setCurrentSubscriptionsLocked(List<SubscriptionInfo> subscriptions) { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 686 | Collections.sort(subscriptions, new Comparator<SubscriptionInfo>() { |
| 687 | @Override |
| 688 | public int compare(SubscriptionInfo lhs, SubscriptionInfo rhs) { |
| 689 | return lhs.getSimSlotIndex() == rhs.getSimSlotIndex() |
| 690 | ? lhs.getSubscriptionId() - rhs.getSubscriptionId() |
| 691 | : lhs.getSimSlotIndex() - rhs.getSimSlotIndex(); |
| 692 | } |
| 693 | }); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 694 | mCurrentSubscriptions = subscriptions; |
| 695 | |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 696 | SparseArray<MobileSignalController> cachedControllers = |
| 697 | new SparseArray<MobileSignalController>(); |
| 698 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 699 | cachedControllers.put(mMobileSignalControllers.keyAt(i), |
| 700 | mMobileSignalControllers.valueAt(i)); |
| 701 | } |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 702 | mMobileSignalControllers.clear(); |
| 703 | final int num = subscriptions.size(); |
| 704 | for (int i = 0; i < num; i++) { |
| 705 | int subId = subscriptions.get(i).getSubscriptionId(); |
| 706 | // If we have a copy of this controller already reuse it, otherwise make a new one. |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 707 | if (cachedControllers.indexOfKey(subId) >= 0) { |
| 708 | mMobileSignalControllers.put(subId, cachedControllers.get(subId)); |
| 709 | cachedControllers.remove(subId); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 710 | } else { |
| 711 | MobileSignalController controller = new MobileSignalController(mContext, mConfig, |
chen xu | 84a9087 | 2019-03-28 15:44:12 -0700 | [diff] [blame] | 712 | mHasMobileDataFeature, mPhone.createForSubscriptionId(subId), |
| 713 | mCallbackHandler, this, subscriptions.get(i), |
| 714 | mSubDefaults, mReceiverHandler.getLooper()); |
Jason Monk | fd57ea7 | 2016-04-29 13:37:58 -0400 | [diff] [blame] | 715 | controller.setUserSetupComplete(mUserSetup); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 716 | mMobileSignalControllers.put(subId, controller); |
| 717 | if (subscriptions.get(i).getSimSlotIndex() == 0) { |
| 718 | mDefaultSignalController = controller; |
| 719 | } |
| 720 | if (mListening) { |
| 721 | controller.registerListener(); |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | if (mListening) { |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 726 | for (int i = 0; i < cachedControllers.size(); i++) { |
| 727 | int key = cachedControllers.keyAt(i); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 728 | if (cachedControllers.get(key) == mDefaultSignalController) { |
| 729 | mDefaultSignalController = null; |
| 730 | } |
| 731 | cachedControllers.get(key).unregisterListener(); |
| 732 | } |
| 733 | } |
Jason Monk | 8fcab35 | 2015-06-29 10:57:00 -0400 | [diff] [blame] | 734 | mCallbackHandler.setSubs(subscriptions); |
| 735 | notifyAllListeners(); |
| 736 | |
Jason Monk | 25d8a48 | 2014-12-09 12:27:24 -0500 | [diff] [blame] | 737 | // There may be new MobileSignalControllers around, make sure they get the current |
| 738 | // inet condition and airplane mode. |
| 739 | pushConnectivityToSignals(); |
| 740 | updateAirplaneMode(true /* force */); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 741 | } |
| 742 | |
Jason Monk | 0288de0 | 2017-02-23 14:48:05 -0500 | [diff] [blame] | 743 | private void setUserSetupComplete(final boolean userSetup) { |
| 744 | mReceiverHandler.post(() -> handleSetUserSetupComplete(userSetup)); |
Jason Monk | fd57ea7 | 2016-04-29 13:37:58 -0400 | [diff] [blame] | 745 | } |
| 746 | |
Jason Monk | 0288de0 | 2017-02-23 14:48:05 -0500 | [diff] [blame] | 747 | private void handleSetUserSetupComplete(boolean userSetup) { |
Jason Monk | fd57ea7 | 2016-04-29 13:37:58 -0400 | [diff] [blame] | 748 | mUserSetup = userSetup; |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 749 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 750 | MobileSignalController controller = mMobileSignalControllers.valueAt(i); |
Jason Monk | fd57ea7 | 2016-04-29 13:37:58 -0400 | [diff] [blame] | 751 | controller.setUserSetupComplete(mUserSetup); |
| 752 | } |
| 753 | } |
| 754 | |
Jason Monk | c014dec | 2014-12-12 11:49:55 -0500 | [diff] [blame] | 755 | @VisibleForTesting |
| 756 | boolean hasCorrectMobileControllers(List<SubscriptionInfo> allSubscriptions) { |
Jason Monk | c9f0571 | 2014-12-15 12:24:10 -0500 | [diff] [blame] | 757 | if (allSubscriptions.size() != mMobileSignalControllers.size()) { |
| 758 | return false; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 759 | } |
| 760 | for (SubscriptionInfo info : allSubscriptions) { |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 761 | if (mMobileSignalControllers.indexOfKey(info.getSubscriptionId()) < 0) { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 762 | return false; |
| 763 | } |
| 764 | } |
| 765 | return true; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 766 | } |
| 767 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 768 | private void updateAirplaneMode(boolean force) { |
| 769 | boolean airplaneMode = (Settings.Global.getInt(mContext.getContentResolver(), |
| 770 | Settings.Global.AIRPLANE_MODE_ON, 0) == 1); |
| 771 | if (airplaneMode != mAirplaneMode || force) { |
| 772 | mAirplaneMode = airplaneMode; |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 773 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 774 | MobileSignalController mobileSignalController = mMobileSignalControllers.valueAt(i); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 775 | mobileSignalController.setAirplaneMode(mAirplaneMode); |
| 776 | } |
| 777 | notifyListeners(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 778 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | private void refreshLocale() { |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 782 | Locale current = mContext.getResources().getConfiguration().locale; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 783 | if (!current.equals(mLocale)) { |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 784 | mLocale = current; |
Fabian Kozynski | d9cb384 | 2018-09-28 13:34:14 -0400 | [diff] [blame] | 785 | mWifiSignalController.refreshLocale(); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 786 | notifyAllListeners(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 790 | /** |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 791 | * Forces update of all callbacks on both SignalClusters and |
| 792 | * NetworkSignalChangedCallbacks. |
| 793 | */ |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 794 | private void notifyAllListeners() { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 795 | notifyListeners(); |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 796 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 797 | MobileSignalController mobileSignalController = mMobileSignalControllers.valueAt(i); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 798 | mobileSignalController.notifyListeners(); |
| 799 | } |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 800 | mWifiSignalController.notifyListeners(); |
Lorenzo Colitti | d46bb58 | 2015-04-27 20:32:01 +0900 | [diff] [blame] | 801 | mEthernetSignalController.notifyListeners(); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 802 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 803 | |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 804 | /** |
| 805 | * Notifies listeners of changes in state of to the NetworkController, but |
| 806 | * does not notify for any info on SignalControllers, for that call |
| 807 | * notifyAllListeners. |
| 808 | */ |
| 809 | private void notifyListeners() { |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 810 | mCallbackHandler.setIsAirplaneMode(new IconState(mAirplaneMode, |
| 811 | TelephonyIcons.FLIGHT_MODE_ICON, R.string.accessibility_airplane_mode, mContext)); |
Jason Monk | 1ff7766 | 2017-09-29 11:17:05 -0400 | [diff] [blame] | 812 | mCallbackHandler.setNoSims(mHasNoSubs, mSimDetected); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 813 | } |
| 814 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 815 | /** |
| 816 | * Update the Inet conditions and what network we are connected to. |
| 817 | */ |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 818 | private void updateConnectivity() { |
| 819 | mConnectedTransports.clear(); |
| 820 | mValidatedTransports.clear(); |
| 821 | for (NetworkCapabilities nc : |
| 822 | mConnectivityManager.getDefaultNetworkCapabilitiesForUser(mCurrentUserId)) { |
| 823 | for (int transportType : nc.getTransportTypes()) { |
| 824 | mConnectedTransports.set(transportType); |
| 825 | if (nc.hasCapability(NET_CAPABILITY_VALIDATED)) { |
| 826 | mValidatedTransports.set(transportType); |
| 827 | } |
| 828 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 829 | } |
| 830 | |
Malcolm Chen | 0c63c4d | 2020-02-25 18:13:40 -0800 | [diff] [blame] | 831 | if (mForceCellularValidated) mValidatedTransports.set(TRANSPORT_CELLULAR); |
| 832 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 833 | if (CHATTY) { |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 834 | Log.d(TAG, "updateConnectivity: mConnectedTransports=" + mConnectedTransports); |
| 835 | Log.d(TAG, "updateConnectivity: mValidatedTransports=" + mValidatedTransports); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 836 | } |
| 837 | |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 838 | mInetCondition = !mValidatedTransports.isEmpty(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 839 | |
Jason Monk | 25d8a48 | 2014-12-09 12:27:24 -0500 | [diff] [blame] | 840 | pushConnectivityToSignals(); |
| 841 | } |
| 842 | |
| 843 | /** |
| 844 | * Pushes the current connectivity state to all SignalControllers. |
| 845 | */ |
| 846 | private void pushConnectivityToSignals() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 847 | // We want to update all the icons, all at once, for any condition change |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 848 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 849 | MobileSignalController mobileSignalController = mMobileSignalControllers.valueAt(i); |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 850 | mobileSignalController.updateConnectivity(mConnectedTransports, mValidatedTransports); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 851 | } |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 852 | mWifiSignalController.updateConnectivity(mConnectedTransports, mValidatedTransports); |
| 853 | mEthernetSignalController.updateConnectivity(mConnectedTransports, mValidatedTransports); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 854 | } |
| 855 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 856 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 857 | pw.println("NetworkController state:"); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 858 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 859 | pw.println(" - telephony ------"); |
| 860 | pw.print(" hasVoiceCallingFeature()="); |
| 861 | pw.println(hasVoiceCallingFeature()); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 862 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 863 | pw.println(" - connectivity ------"); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 864 | pw.print(" mConnectedTransports="); |
| 865 | pw.println(mConnectedTransports); |
| 866 | pw.print(" mValidatedTransports="); |
| 867 | pw.println(mValidatedTransports); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 868 | pw.print(" mInetCondition="); |
| 869 | pw.println(mInetCondition); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 870 | pw.print(" mAirplaneMode="); |
| 871 | pw.println(mAirplaneMode); |
| 872 | pw.print(" mLocale="); |
| 873 | pw.println(mLocale); |
Jason Monk | b0808aa | 2015-07-22 16:34:36 -0400 | [diff] [blame] | 874 | pw.print(" mLastServiceState="); |
| 875 | pw.println(mLastServiceState); |
Jason Monk | 66845a3 | 2015-08-03 11:09:41 -0400 | [diff] [blame] | 876 | pw.print(" mIsEmergency="); |
| 877 | pw.println(mIsEmergency); |
| 878 | pw.print(" mEmergencySource="); |
| 879 | pw.println(emergencyToString(mEmergencySource)); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 880 | |
Bill Lin | 767c0db | 2019-08-15 20:42:06 +0800 | [diff] [blame] | 881 | pw.println(" - config ------"); |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 882 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 883 | MobileSignalController mobileSignalController = mMobileSignalControllers.valueAt(i); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 884 | mobileSignalController.dump(pw); |
| 885 | } |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 886 | mWifiSignalController.dump(pw); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 887 | |
Lorenzo Colitti | d46bb58 | 2015-04-27 20:32:01 +0900 | [diff] [blame] | 888 | mEthernetSignalController.dump(pw); |
| 889 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 890 | mAccessPoints.dump(pw); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 891 | } |
| 892 | |
Jason Monk | 66845a3 | 2015-08-03 11:09:41 -0400 | [diff] [blame] | 893 | private static final String emergencyToString(int emergencySource) { |
| 894 | if (emergencySource > EMERGENCY_NO_SUB) { |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 895 | return "ASSUMED_VOICE_CONTROLLER(" + (emergencySource - EMERGENCY_VOICE_CONTROLLER) |
| 896 | + ")"; |
| 897 | } else if (emergencySource > EMERGENCY_NO_SUB) { |
Jason Monk | 66845a3 | 2015-08-03 11:09:41 -0400 | [diff] [blame] | 898 | return "NO_SUB(" + (emergencySource - EMERGENCY_NO_SUB) + ")"; |
| 899 | } else if (emergencySource > EMERGENCY_VOICE_CONTROLLER) { |
| 900 | return "VOICE_CONTROLLER(" + (emergencySource - EMERGENCY_VOICE_CONTROLLER) + ")"; |
| 901 | } else if (emergencySource > EMERGENCY_FIRST_CONTROLLER) { |
| 902 | return "FIRST_CONTROLLER(" + (emergencySource - EMERGENCY_FIRST_CONTROLLER) + ")"; |
| 903 | } else if (emergencySource == EMERGENCY_NO_CONTROLLERS) { |
| 904 | return "NO_CONTROLLERS"; |
| 905 | } |
| 906 | return "UNKNOWN_SOURCE"; |
| 907 | } |
| 908 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 909 | private boolean mDemoMode; |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 910 | private boolean mDemoInetCondition; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 911 | private WifiSignalController.WifiState mDemoWifiState; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 912 | |
| 913 | @Override |
| 914 | public void dispatchDemoCommand(String command, Bundle args) { |
| 915 | if (!mDemoMode && command.equals(COMMAND_ENTER)) { |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 916 | if (DEBUG) Log.d(TAG, "Entering demo mode"); |
| 917 | unregisterListeners(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 918 | mDemoMode = true; |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 919 | mDemoInetCondition = mInetCondition; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 920 | mDemoWifiState = mWifiSignalController.getState(); |
Kenneth Westin | 5c88ffb | 2013-11-28 16:29:40 +0100 | [diff] [blame] | 921 | mDemoWifiState.ssid = "DemoMode"; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 922 | } else if (mDemoMode && command.equals(COMMAND_EXIT)) { |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 923 | if (DEBUG) Log.d(TAG, "Exiting demo mode"); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 924 | mDemoMode = false; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 925 | // Update what MobileSignalControllers, because they may change |
| 926 | // to set the number of sim slots. |
| 927 | updateMobileControllers(); |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 928 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 929 | MobileSignalController controller = mMobileSignalControllers.valueAt(i); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 930 | controller.resetLastState(); |
| 931 | } |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 932 | mWifiSignalController.resetLastState(); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 933 | mReceiverHandler.post(mRegisterListeners); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 934 | notifyAllListeners(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 935 | } else if (mDemoMode && command.equals(COMMAND_NETWORK)) { |
| 936 | String airplane = args.getString("airplane"); |
| 937 | if (airplane != null) { |
| 938 | boolean show = airplane.equals("show"); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 939 | mCallbackHandler.setIsAirplaneMode(new IconState(show, |
| 940 | TelephonyIcons.FLIGHT_MODE_ICON, R.string.accessibility_airplane_mode, |
| 941 | mContext)); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 942 | } |
| 943 | String fully = args.getString("fully"); |
| 944 | if (fully != null) { |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 945 | mDemoInetCondition = Boolean.parseBoolean(fully); |
| 946 | BitSet connected = new BitSet(); |
| 947 | |
| 948 | if (mDemoInetCondition) { |
| 949 | connected.set(mWifiSignalController.mTransportType); |
| 950 | } |
| 951 | mWifiSignalController.updateConnectivity(connected, connected); |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 952 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 953 | MobileSignalController controller = mMobileSignalControllers.valueAt(i); |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 954 | if (mDemoInetCondition) { |
| 955 | connected.set(controller.mTransportType); |
| 956 | } |
| 957 | controller.updateConnectivity(connected, connected); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 958 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 959 | } |
| 960 | String wifi = args.getString("wifi"); |
| 961 | if (wifi != null) { |
| 962 | boolean show = wifi.equals("show"); |
| 963 | String level = args.getString("level"); |
| 964 | if (level != null) { |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 965 | mDemoWifiState.level = level.equals("null") ? -1 |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 966 | : Math.min(Integer.parseInt(level), WifiIcons.WIFI_LEVEL_COUNT - 1); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 967 | mDemoWifiState.connected = mDemoWifiState.level >= 0; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 968 | } |
Kenneth Westin | 5c88ffb | 2013-11-28 16:29:40 +0100 | [diff] [blame] | 969 | String activity = args.getString("activity"); |
| 970 | if (activity != null) { |
| 971 | switch (activity) { |
| 972 | case "inout": |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 973 | mWifiSignalController.setActivity(DATA_ACTIVITY_INOUT); |
Kenneth Westin | 5c88ffb | 2013-11-28 16:29:40 +0100 | [diff] [blame] | 974 | break; |
| 975 | case "in": |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 976 | mWifiSignalController.setActivity(DATA_ACTIVITY_IN); |
Kenneth Westin | 5c88ffb | 2013-11-28 16:29:40 +0100 | [diff] [blame] | 977 | break; |
| 978 | case "out": |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 979 | mWifiSignalController.setActivity(DATA_ACTIVITY_OUT); |
Kenneth Westin | 5c88ffb | 2013-11-28 16:29:40 +0100 | [diff] [blame] | 980 | break; |
| 981 | default: |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 982 | mWifiSignalController.setActivity(DATA_ACTIVITY_NONE); |
Kenneth Westin | 5c88ffb | 2013-11-28 16:29:40 +0100 | [diff] [blame] | 983 | break; |
| 984 | } |
| 985 | } else { |
Roshan Pius | 80fb137 | 2018-07-16 15:43:03 -0700 | [diff] [blame] | 986 | mWifiSignalController.setActivity(DATA_ACTIVITY_NONE); |
Kenneth Westin | 5c88ffb | 2013-11-28 16:29:40 +0100 | [diff] [blame] | 987 | } |
Evan Laird | 27a9957 | 2017-09-27 14:09:30 -0400 | [diff] [blame] | 988 | String ssid = args.getString("ssid"); |
| 989 | if (ssid != null) { |
| 990 | mDemoWifiState.ssid = ssid; |
| 991 | } |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 992 | mDemoWifiState.enabled = show; |
| 993 | mWifiSignalController.notifyListeners(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 994 | } |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 995 | String sims = args.getString("sims"); |
| 996 | if (sims != null) { |
John Spurlock | cf053c1 | 2015-05-14 16:33:59 -0400 | [diff] [blame] | 997 | int num = MathUtils.constrain(Integer.parseInt(sims), 1, 8); |
| 998 | List<SubscriptionInfo> subs = new ArrayList<>(); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 999 | if (num != mMobileSignalControllers.size()) { |
| 1000 | mMobileSignalControllers.clear(); |
| 1001 | int start = mSubscriptionManager.getActiveSubscriptionInfoCountMax(); |
| 1002 | for (int i = start /* get out of normal index range */; i < start + num; i++) { |
John Spurlock | cf053c1 | 2015-05-14 16:33:59 -0400 | [diff] [blame] | 1003 | subs.add(addSignalController(i, i)); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 1004 | } |
Jason Monk | 33b60bb | 2015-07-13 10:42:23 -0400 | [diff] [blame] | 1005 | mCallbackHandler.setSubs(subs); |
Evan Laird | e1d13c9 | 2018-03-20 16:58:01 -0400 | [diff] [blame] | 1006 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 1007 | int key = mMobileSignalControllers.keyAt(i); |
| 1008 | MobileSignalController controller = mMobileSignalControllers.get(key); |
| 1009 | controller.notifyListeners(); |
| 1010 | } |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 1011 | } |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 1012 | } |
| 1013 | String nosim = args.getString("nosim"); |
| 1014 | if (nosim != null) { |
Jason Monk | 1ff7766 | 2017-09-29 11:17:05 -0400 | [diff] [blame] | 1015 | mHasNoSubs = nosim.equals("show"); |
| 1016 | mCallbackHandler.setNoSims(mHasNoSubs, mSimDetected); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 1017 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 1018 | String mobile = args.getString("mobile"); |
| 1019 | if (mobile != null) { |
| 1020 | boolean show = mobile.equals("show"); |
| 1021 | String datatype = args.getString("datatype"); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 1022 | String slotString = args.getString("slot"); |
| 1023 | int slot = TextUtils.isEmpty(slotString) ? 0 : Integer.parseInt(slotString); |
John Spurlock | cf053c1 | 2015-05-14 16:33:59 -0400 | [diff] [blame] | 1024 | slot = MathUtils.constrain(slot, 0, 8); |
| 1025 | // Ensure we have enough sim slots |
| 1026 | List<SubscriptionInfo> subs = new ArrayList<>(); |
| 1027 | while (mMobileSignalControllers.size() <= slot) { |
| 1028 | int nextSlot = mMobileSignalControllers.size(); |
| 1029 | subs.add(addSignalController(nextSlot, nextSlot)); |
| 1030 | } |
| 1031 | if (!subs.isEmpty()) { |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 1032 | mCallbackHandler.setSubs(subs); |
John Spurlock | cf053c1 | 2015-05-14 16:33:59 -0400 | [diff] [blame] | 1033 | } |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 1034 | // Hack to index linearly for easy use. |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 1035 | MobileSignalController controller = mMobileSignalControllers.valueAt(slot); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 1036 | controller.getState().dataSim = datatype != null; |
Jason Monk | b595098 | 2017-01-24 15:53:53 -0500 | [diff] [blame] | 1037 | controller.getState().isDefault = datatype != null; |
| 1038 | controller.getState().dataConnected = datatype != null; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 1039 | if (datatype != null) { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 1040 | controller.getState().iconGroup = |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 1041 | datatype.equals("1x") ? TelephonyIcons.ONE_X : |
| 1042 | datatype.equals("3g") ? TelephonyIcons.THREE_G : |
| 1043 | datatype.equals("4g") ? TelephonyIcons.FOUR_G : |
Ajay Nambi | 7965fa4 | 2015-05-14 18:48:33 -0700 | [diff] [blame] | 1044 | datatype.equals("4g+") ? TelephonyIcons.FOUR_G_PLUS : |
Bill Lin | 767c0db | 2019-08-15 20:42:06 +0800 | [diff] [blame] | 1045 | datatype.equals("5g") ? TelephonyIcons.NR_5G : |
Evan Laird | 5784a6b | 2019-09-24 19:14:05 -0400 | [diff] [blame] | 1046 | datatype.equals("5ge") ? TelephonyIcons.LTE_CA_5G_E : |
Bill Lin | 767c0db | 2019-08-15 20:42:06 +0800 | [diff] [blame] | 1047 | datatype.equals("5g+") ? TelephonyIcons.NR_5G_PLUS : |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 1048 | datatype.equals("e") ? TelephonyIcons.E : |
| 1049 | datatype.equals("g") ? TelephonyIcons.G : |
| 1050 | datatype.equals("h") ? TelephonyIcons.H : |
Amin Shaikh | f5830ca | 2018-03-09 14:06:50 -0500 | [diff] [blame] | 1051 | datatype.equals("h+") ? TelephonyIcons.H_PLUS : |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 1052 | datatype.equals("lte") ? TelephonyIcons.LTE : |
Jason Monk | aa730be | 2016-07-13 14:22:52 -0400 | [diff] [blame] | 1053 | datatype.equals("lte+") ? TelephonyIcons.LTE_PLUS : |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 1054 | datatype.equals("dis") ? TelephonyIcons.DATA_DISABLED : |
Fabian Kozynski | c7bc84b | 2019-03-11 13:57:31 -0400 | [diff] [blame] | 1055 | datatype.equals("not") ? TelephonyIcons.NOT_DEFAULT_DATA : |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 1056 | TelephonyIcons.UNKNOWN; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 1057 | } |
Jason Monk | 0f0de13 | 2016-12-19 15:36:13 -0500 | [diff] [blame] | 1058 | if (args.containsKey("roam")) { |
| 1059 | controller.getState().roaming = "show".equals(args.getString("roam")); |
| 1060 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 1061 | String level = args.getString("level"); |
| 1062 | if (level != null) { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 1063 | controller.getState().level = level.equals("null") ? -1 |
Jason Monk | 48edc0c | 2017-04-10 15:01:27 -0400 | [diff] [blame] | 1064 | : Math.min(Integer.parseInt(level), |
Amit Mahajan | 75eab47 | 2020-01-29 10:47:48 -0800 | [diff] [blame] | 1065 | CellSignalStrength.getNumSignalStrengthLevels()); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 1066 | controller.getState().connected = controller.getState().level >= 0; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 1067 | } |
Amin Shaikh | 0ad7e51 | 2019-04-29 07:36:34 -0400 | [diff] [blame] | 1068 | if (args.containsKey("inflate")) { |
| 1069 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 1070 | mMobileSignalControllers.valueAt(i).mInflateSignalStrengths = |
| 1071 | "true".equals(args.getString("inflate")); |
| 1072 | } |
| 1073 | } |
Jason Monk | 0f0de13 | 2016-12-19 15:36:13 -0500 | [diff] [blame] | 1074 | String activity = args.getString("activity"); |
| 1075 | if (activity != null) { |
Kenneth Westin | 5c88ffb | 2013-11-28 16:29:40 +0100 | [diff] [blame] | 1076 | controller.getState().dataConnected = true; |
| 1077 | switch (activity) { |
| 1078 | case "inout": |
| 1079 | controller.setActivity(TelephonyManager.DATA_ACTIVITY_INOUT); |
| 1080 | break; |
| 1081 | case "in": |
| 1082 | controller.setActivity(TelephonyManager.DATA_ACTIVITY_IN); |
| 1083 | break; |
| 1084 | case "out": |
| 1085 | controller.setActivity(TelephonyManager.DATA_ACTIVITY_OUT); |
| 1086 | break; |
| 1087 | default: |
| 1088 | controller.setActivity(TelephonyManager.DATA_ACTIVITY_NONE); |
| 1089 | break; |
| 1090 | } |
| 1091 | } else { |
| 1092 | controller.setActivity(TelephonyManager.DATA_ACTIVITY_NONE); |
Jason Monk | 0f0de13 | 2016-12-19 15:36:13 -0500 | [diff] [blame] | 1093 | } |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 1094 | controller.getState().enabled = show; |
| 1095 | controller.notifyListeners(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 1096 | } |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 1097 | String carrierNetworkChange = args.getString("carriernetworkchange"); |
| 1098 | if (carrierNetworkChange != null) { |
| 1099 | boolean show = carrierNetworkChange.equals("show"); |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 1100 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 1101 | MobileSignalController controller = mMobileSignalControllers.valueAt(i); |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 1102 | controller.setCarrierNetworkChangeMode(show); |
| 1103 | } |
| 1104 | } |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 1105 | } |
| 1106 | } |
| 1107 | |
John Spurlock | cf053c1 | 2015-05-14 16:33:59 -0400 | [diff] [blame] | 1108 | private SubscriptionInfo addSignalController(int id, int simSlotIndex) { |
| 1109 | SubscriptionInfo info = new SubscriptionInfo(id, "", simSlotIndex, "", "", 0, 0, "", 0, |
Malcolm Chen | e89e0ce | 2018-08-09 17:01:35 -0700 | [diff] [blame] | 1110 | null, null, null, "", false, null, null); |
Jason Monk | b595098 | 2017-01-24 15:53:53 -0500 | [diff] [blame] | 1111 | MobileSignalController controller = new MobileSignalController(mContext, |
chen xu | 84a9087 | 2019-03-28 15:44:12 -0700 | [diff] [blame] | 1112 | mConfig, mHasMobileDataFeature, |
| 1113 | mPhone.createForSubscriptionId(info.getSubscriptionId()), mCallbackHandler, this, info, |
Jason Monk | b595098 | 2017-01-24 15:53:53 -0500 | [diff] [blame] | 1114 | mSubDefaults, mReceiverHandler.getLooper()); |
| 1115 | mMobileSignalControllers.put(id, controller); |
| 1116 | controller.getState().userSetup = true; |
John Spurlock | cf053c1 | 2015-05-14 16:33:59 -0400 | [diff] [blame] | 1117 | return info; |
| 1118 | } |
| 1119 | |
Adrian Roos | 316bf54 | 2016-08-23 17:53:07 +0200 | [diff] [blame] | 1120 | public boolean hasEmergencyCryptKeeperText() { |
| 1121 | return EncryptionHelper.IS_DATA_ENCRYPTED; |
| 1122 | } |
| 1123 | |
| 1124 | public boolean isRadioOn() { |
| 1125 | return !mAirplaneMode; |
| 1126 | } |
| 1127 | |
Jason Monk | 8fcab35 | 2015-06-29 10:57:00 -0400 | [diff] [blame] | 1128 | private class SubListener extends OnSubscriptionsChangedListener { |
Jason Monk | c9f0571 | 2014-12-15 12:24:10 -0500 | [diff] [blame] | 1129 | @Override |
| 1130 | public void onSubscriptionsChanged() { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 1131 | updateMobileControllers(); |
Jason Monk | 8fcab35 | 2015-06-29 10:57:00 -0400 | [diff] [blame] | 1132 | } |
| 1133 | } |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 1134 | |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 1135 | /** |
| 1136 | * Used to register listeners from the BG Looper, this way the PhoneStateListeners that |
| 1137 | * get created will also run on the BG Looper. |
| 1138 | */ |
| 1139 | private final Runnable mRegisterListeners = new Runnable() { |
| 1140 | @Override |
| 1141 | public void run() { |
| 1142 | registerListeners(); |
| 1143 | } |
| 1144 | }; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 1145 | |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 1146 | public static class SubscriptionDefaults { |
| 1147 | public int getDefaultVoiceSubId() { |
Shishir Agrawal | 7ea3e8b | 2016-01-25 13:03:07 -0800 | [diff] [blame] | 1148 | return SubscriptionManager.getDefaultVoiceSubscriptionId(); |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | public int getDefaultDataSubId() { |
Shishir Agrawal | 7ea3e8b | 2016-01-25 13:03:07 -0800 | [diff] [blame] | 1152 | return SubscriptionManager.getDefaultDataSubscriptionId(); |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 1153 | } |
Fabian Kozynski | ccde55d | 2019-06-26 10:06:09 -0400 | [diff] [blame] | 1154 | |
| 1155 | public int getActiveDataSubId() { |
| 1156 | return SubscriptionManager.getActiveDataSubscriptionId(); |
| 1157 | } |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 1158 | } |
| 1159 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 1160 | @VisibleForTesting |
| 1161 | static class Config { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 1162 | boolean showAtLeast3G = false; |
vagdevi | 3f2b9a5 | 2019-08-09 09:45:14 -0700 | [diff] [blame] | 1163 | boolean show4gFor3g = false; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 1164 | boolean alwaysShowCdmaRssi = false; |
| 1165 | boolean show4gForLte = false; |
Robert Greenwalt | 37d34ba | 2016-07-27 14:54:34 -0700 | [diff] [blame] | 1166 | boolean hideLtePlus = false; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 1167 | boolean hspaDataDistinguishable; |
Jason Monk | 43c14d1 | 2017-06-22 11:20:04 -0400 | [diff] [blame] | 1168 | boolean inflateSignalStrengths = false; |
Wenting Xiong | 63364fe | 2017-02-07 19:12:27 +0800 | [diff] [blame] | 1169 | boolean alwaysShowDataRatIcon = false; |
Pengquan Meng | a62d32b | 2018-12-06 17:38:38 -0800 | [diff] [blame] | 1170 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 1171 | static Config readConfig(Context context) { |
| 1172 | Config config = new Config(); |
| 1173 | Resources res = context.getResources(); |
| 1174 | |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 1175 | config.showAtLeast3G = res.getBoolean(R.bool.config_showMin3G); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 1176 | config.alwaysShowCdmaRssi = |
| 1177 | res.getBoolean(com.android.internal.R.bool.config_alwaysUseCdmaRssi); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 1178 | config.hspaDataDistinguishable = |
| 1179 | res.getBoolean(R.bool.config_hspa_data_distinguishable); |
Antony Sargent | c026de3 | 2019-05-21 16:29:04 -0700 | [diff] [blame] | 1180 | config.inflateSignalStrengths = res.getBoolean( |
| 1181 | com.android.internal.R.bool.config_inflateSignalStrength); |
Wenting Xiong | 63364fe | 2017-02-07 19:12:27 +0800 | [diff] [blame] | 1182 | |
| 1183 | CarrierConfigManager configMgr = (CarrierConfigManager) |
| 1184 | context.getSystemService(Context.CARRIER_CONFIG_SERVICE); |
Niklas Lindgren | a02964d | 2018-09-21 14:01:57 +0200 | [diff] [blame] | 1185 | // Handle specific carrier config values for the default data SIM |
| 1186 | int defaultDataSubId = SubscriptionManager.from(context) |
| 1187 | .getDefaultDataSubscriptionId(); |
| 1188 | PersistableBundle b = configMgr.getConfigForSubId(defaultDataSubId); |
Wenting Xiong | 63364fe | 2017-02-07 19:12:27 +0800 | [diff] [blame] | 1189 | if (b != null) { |
| 1190 | config.alwaysShowDataRatIcon = b.getBoolean( |
| 1191 | CarrierConfigManager.KEY_ALWAYS_SHOW_DATA_RAT_ICON_BOOL); |
Niklas Lindgren | a02964d | 2018-09-21 14:01:57 +0200 | [diff] [blame] | 1192 | config.show4gForLte = b.getBoolean( |
| 1193 | CarrierConfigManager.KEY_SHOW_4G_FOR_LTE_DATA_ICON_BOOL); |
vagdevi | 3f2b9a5 | 2019-08-09 09:45:14 -0700 | [diff] [blame] | 1194 | config.show4gFor3g = b.getBoolean( |
| 1195 | CarrierConfigManager.KEY_SHOW_4G_FOR_3G_DATA_ICON_BOOL); |
Niklas Lindgren | a02964d | 2018-09-21 14:01:57 +0200 | [diff] [blame] | 1196 | config.hideLtePlus = b.getBoolean( |
| 1197 | CarrierConfigManager.KEY_HIDE_LTE_PLUS_DATA_ICON_BOOL); |
Wenting Xiong | 63364fe | 2017-02-07 19:12:27 +0800 | [diff] [blame] | 1198 | } |
Pengquan Meng | a62d32b | 2018-12-06 17:38:38 -0800 | [diff] [blame] | 1199 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 1200 | return config; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 1201 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 1202 | } |
| 1203 | } |