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 | |
| 19 | import android.content.BroadcastReceiver; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.IntentFilter; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 23 | import android.content.res.Configuration; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 24 | import android.content.res.Resources; |
| 25 | import android.net.ConnectivityManager; |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 26 | import android.net.NetworkCapabilities; |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 27 | import android.net.NetworkScoreManager; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 28 | import android.net.wifi.WifiManager; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 29 | import android.os.AsyncTask; |
| 30 | import android.os.Bundle; |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 31 | import android.os.Handler; |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 32 | import android.os.Looper; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 33 | import android.provider.Settings; |
Jason Monk | b0808aa | 2015-07-22 16:34:36 -0400 | [diff] [blame] | 34 | import android.telephony.ServiceState; |
Jason Monk | 48edc0c | 2017-04-10 15:01:27 -0400 | [diff] [blame^] | 35 | import android.telephony.SignalStrength; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 36 | import android.telephony.SubscriptionInfo; |
| 37 | import android.telephony.SubscriptionManager; |
| 38 | import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 39 | import android.telephony.TelephonyManager; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 40 | import android.text.TextUtils; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 41 | import android.util.Log; |
John Spurlock | cf053c1 | 2015-05-14 16:33:59 -0400 | [diff] [blame] | 42 | import android.util.MathUtils; |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 43 | import android.util.SparseArray; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 44 | |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 45 | import com.android.internal.annotations.VisibleForTesting; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 46 | import com.android.internal.telephony.PhoneConstants; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 47 | import com.android.internal.telephony.TelephonyIntents; |
Jason Monk | f668d7c | 2016-01-14 10:38:41 -0500 | [diff] [blame] | 48 | import com.android.settingslib.net.DataUsageController; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 49 | import com.android.systemui.ConfigurationChangedReceiver; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 50 | import com.android.systemui.DemoMode; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 51 | import com.android.systemui.Dumpable; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 52 | import com.android.systemui.R; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 53 | import com.android.systemui.settings.CurrentUserTracker; |
| 54 | import com.android.systemui.statusbar.policy.DeviceProvisionedController.DeviceProvisionedListener; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 55 | |
| 56 | import java.io.FileDescriptor; |
| 57 | import java.io.PrintWriter; |
| 58 | import java.util.ArrayList; |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 59 | import java.util.BitSet; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 60 | import java.util.Collections; |
| 61 | import java.util.Comparator; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 62 | import java.util.HashMap; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 63 | import java.util.List; |
| 64 | import java.util.Locale; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 65 | import java.util.Map; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 66 | |
Jason Monk | 4cf95ae | 2015-11-16 15:59:53 -0500 | [diff] [blame] | 67 | import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED; |
| 68 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 69 | /** Platform implementation of the network controller. **/ |
| 70 | public class NetworkControllerImpl extends BroadcastReceiver |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 71 | implements NetworkController, DemoMode, DataUsageController.NetworkNameProvider, |
| 72 | ConfigurationChangedReceiver, Dumpable { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 73 | // debug |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 74 | static final String TAG = "NetworkController"; |
| 75 | static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 76 | // additional diagnostics, but not logspew |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 77 | static final boolean CHATTY = Log.isLoggable(TAG + "Chat", Log.DEBUG); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 78 | |
Jason Monk | 66845a3 | 2015-08-03 11:09:41 -0400 | [diff] [blame] | 79 | private static final int EMERGENCY_NO_CONTROLLERS = 0; |
| 80 | private static final int EMERGENCY_FIRST_CONTROLLER = 100; |
| 81 | private static final int EMERGENCY_VOICE_CONTROLLER = 200; |
| 82 | private static final int EMERGENCY_NO_SUB = 300; |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 83 | private static final int EMERGENCY_ASSUMED_VOICE_CONTROLLER = 400; |
Jason Monk | 66845a3 | 2015-08-03 11:09:41 -0400 | [diff] [blame] | 84 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 85 | private final Context mContext; |
| 86 | private final TelephonyManager mPhone; |
| 87 | private final WifiManager mWifiManager; |
| 88 | private final ConnectivityManager mConnectivityManager; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 89 | private final SubscriptionManager mSubscriptionManager; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 90 | private final boolean mHasMobileDataFeature; |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 91 | private final SubscriptionDefaults mSubDefaults; |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 92 | private final DataSaverController mDataSaverController; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 93 | private final CurrentUserTracker mUserTracker; |
Jason Monk | 3aaabd7 | 2014-12-12 11:11:44 -0500 | [diff] [blame] | 94 | private Config mConfig; |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 95 | private final NetworkScoreManager mNetworkScoreManager; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 96 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 97 | // Subcontrollers. |
| 98 | @VisibleForTesting |
| 99 | final WifiSignalController mWifiSignalController; |
Lorenzo Colitti | d46bb58 | 2015-04-27 20:32:01 +0900 | [diff] [blame] | 100 | |
| 101 | @VisibleForTesting |
| 102 | final EthernetSignalController mEthernetSignalController; |
| 103 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 104 | @VisibleForTesting |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 105 | final SparseArray<MobileSignalController> mMobileSignalControllers = new SparseArray<>(); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 106 | // When no SIMs are around at setup, and one is added later, it seems to default to the first |
| 107 | // SIM for most actions. This may be null if there aren't any SIMs around. |
| 108 | private MobileSignalController mDefaultSignalController; |
Jason Monk | 0e2400f | 2014-11-21 11:08:55 -0500 | [diff] [blame] | 109 | private final AccessPointControllerImpl mAccessPoints; |
Jason Monk | f668d7c | 2016-01-14 10:38:41 -0500 | [diff] [blame] | 110 | private final DataUsageController mDataUsageController; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 111 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 112 | private boolean mInetCondition; // Used for Logging and demo. |
Lorenzo Colitti | 23dd176 | 2014-09-10 22:58:54 +0900 | [diff] [blame] | 113 | |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 114 | // BitSets indicating which network transport types (e.g., TRANSPORT_WIFI, TRANSPORT_MOBILE) are |
| 115 | // connected and validated, respectively. |
| 116 | private final BitSet mConnectedTransports = new BitSet(); |
| 117 | private final BitSet mValidatedTransports = new BitSet(); |
| 118 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 119 | // States that don't belong to a subcontroller. |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 120 | private boolean mAirplaneMode = false; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 121 | private boolean mHasNoSims; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 122 | private Locale mLocale = null; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 123 | // This list holds our ordering. |
John Spurlock | cf053c1 | 2015-05-14 16:33:59 -0400 | [diff] [blame] | 124 | private List<SubscriptionInfo> mCurrentSubscriptions = new ArrayList<>(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 125 | |
Jason Monk | c014dec | 2014-12-12 11:49:55 -0500 | [diff] [blame] | 126 | @VisibleForTesting |
| 127 | boolean mListening; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 128 | |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 129 | // The current user ID. |
| 130 | private int mCurrentUserId; |
| 131 | |
Jason Monk | 8fcab35 | 2015-06-29 10:57:00 -0400 | [diff] [blame] | 132 | private OnSubscriptionsChangedListener mSubscriptionListener; |
| 133 | |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 134 | // Handler that all broadcasts are received on. |
| 135 | private final Handler mReceiverHandler; |
| 136 | // Handler that all callbacks are made on. |
| 137 | private final CallbackHandler mCallbackHandler; |
| 138 | |
Jason Monk | 66845a3 | 2015-08-03 11:09:41 -0400 | [diff] [blame] | 139 | private int mEmergencySource; |
| 140 | private boolean mIsEmergency; |
| 141 | |
Jason Monk | b0808aa | 2015-07-22 16:34:36 -0400 | [diff] [blame] | 142 | @VisibleForTesting |
| 143 | ServiceState mLastServiceState; |
Jason Monk | fd57ea7 | 2016-04-29 13:37:58 -0400 | [diff] [blame] | 144 | private boolean mUserSetup; |
Jason Monk | b0808aa | 2015-07-22 16:34:36 -0400 | [diff] [blame] | 145 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 146 | /** |
| 147 | * Construct this controller object and register for updates. |
| 148 | */ |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 149 | public NetworkControllerImpl(Context context, Looper bgLooper, |
| 150 | DeviceProvisionedController deviceProvisionedController) { |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 151 | this(context, (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE), |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 152 | context.getSystemService(NetworkScoreManager.class), |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 153 | (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE), |
Jason Monk | 29f7a7b | 2014-11-17 14:40:56 -0500 | [diff] [blame] | 154 | (WifiManager) context.getSystemService(Context.WIFI_SERVICE), |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 155 | SubscriptionManager.from(context), |
| 156 | Config.readConfig(context), |
| 157 | bgLooper, |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 158 | new CallbackHandler(), |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 159 | new AccessPointControllerImpl(context, bgLooper), |
Jason Monk | f668d7c | 2016-01-14 10:38:41 -0500 | [diff] [blame] | 160 | new DataUsageController(context), |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 161 | new SubscriptionDefaults(), |
| 162 | deviceProvisionedController); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 163 | mReceiverHandler.post(mRegisterListeners); |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @VisibleForTesting |
| 167 | NetworkControllerImpl(Context context, ConnectivityManager connectivityManager, |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 168 | NetworkScoreManager networkScoreManager, |
| 169 | TelephonyManager telephonyManager, |
| 170 | WifiManager wifiManager, |
| 171 | SubscriptionManager subManager, |
| 172 | Config config, |
| 173 | Looper bgLooper, |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 174 | CallbackHandler callbackHandler, |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 175 | AccessPointControllerImpl accessPointController, |
Jason Monk | f668d7c | 2016-01-14 10:38:41 -0500 | [diff] [blame] | 176 | DataUsageController dataUsageController, |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 177 | SubscriptionDefaults defaultsHandler, |
| 178 | DeviceProvisionedController deviceProvisionedController) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 179 | mContext = context; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 180 | mConfig = config; |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 181 | mReceiverHandler = new Handler(bgLooper); |
| 182 | mCallbackHandler = callbackHandler; |
Jason Monk | 9abca5e | 2016-11-11 16:18:14 -0500 | [diff] [blame] | 183 | mDataSaverController = new DataSaverControllerImpl(context); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 184 | |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 185 | mSubscriptionManager = subManager; |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 186 | mSubDefaults = defaultsHandler; |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 187 | mConnectivityManager = connectivityManager; |
| 188 | mHasMobileDataFeature = |
| 189 | mConnectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 190 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 191 | // telephony |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 192 | mPhone = telephonyManager; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 193 | |
| 194 | // wifi |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 195 | mWifiManager = wifiManager; |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 196 | mNetworkScoreManager = networkScoreManager; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 197 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 198 | mLocale = mContext.getResources().getConfiguration().locale; |
Jason Monk | 29f7a7b | 2014-11-17 14:40:56 -0500 | [diff] [blame] | 199 | mAccessPoints = accessPointController; |
Jason Monk | f668d7c | 2016-01-14 10:38:41 -0500 | [diff] [blame] | 200 | mDataUsageController = dataUsageController; |
| 201 | mDataUsageController.setNetworkController(this); |
| 202 | // TODO: Find a way to move this into DataUsageController. |
| 203 | mDataUsageController.setCallback(new DataUsageController.Callback() { |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 204 | @Override |
| 205 | public void onMobileDataEnabled(boolean enabled) { |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 206 | mCallbackHandler.setMobileDataEnabled(enabled); |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 207 | } |
| 208 | }); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 209 | mWifiSignalController = new WifiSignalController(mContext, mHasMobileDataFeature, |
Sundeep Ghuman | d57f324 | 2017-01-13 15:31:48 -0800 | [diff] [blame] | 210 | mCallbackHandler, this, mNetworkScoreManager); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 211 | |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 212 | mEthernetSignalController = new EthernetSignalController(mContext, mCallbackHandler, this); |
Lorenzo Colitti | d46bb58 | 2015-04-27 20:32:01 +0900 | [diff] [blame] | 213 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 214 | // 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] | 215 | updateAirplaneMode(true /* force callback */); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 216 | mUserTracker = new CurrentUserTracker(mContext) { |
| 217 | @Override |
| 218 | public void onUserSwitched(int newUserId) { |
| 219 | NetworkControllerImpl.this.onUserSwitched(newUserId); |
| 220 | } |
| 221 | }; |
| 222 | mUserTracker.startTracking(); |
| 223 | deviceProvisionedController.addCallback(new DeviceProvisionedListener() { |
| 224 | @Override |
| 225 | public void onUserSetupChanged() { |
| 226 | setUserSetupComplete(deviceProvisionedController.isUserSetup( |
| 227 | deviceProvisionedController.getCurrentUser())); |
| 228 | } |
| 229 | }); |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 230 | } |
| 231 | |
Jason Monk | 9a4ce13 | 2016-01-21 15:27:17 -0500 | [diff] [blame] | 232 | public DataSaverController getDataSaverController() { |
| 233 | return mDataSaverController; |
| 234 | } |
| 235 | |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 236 | private void registerListeners() { |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 237 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 238 | MobileSignalController mobileSignalController = mMobileSignalControllers.valueAt(i); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 239 | mobileSignalController.registerListener(); |
| 240 | } |
Jason Monk | 8fcab35 | 2015-06-29 10:57:00 -0400 | [diff] [blame] | 241 | if (mSubscriptionListener == null) { |
| 242 | mSubscriptionListener = new SubListener(); |
| 243 | } |
Wink Saville | 071743f | 2015-01-12 17:11:04 -0800 | [diff] [blame] | 244 | mSubscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionListener); |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 245 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 246 | // broadcasts |
| 247 | IntentFilter filter = new IntentFilter(); |
| 248 | filter.addAction(WifiManager.RSSI_CHANGED_ACTION); |
| 249 | filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); |
| 250 | filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); |
| 251 | filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 252 | filter.addAction(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED); |
| 253 | filter.addAction(TelephonyIntents.ACTION_DEFAULT_VOICE_SUBSCRIPTION_CHANGED); |
Jason Monk | b0808aa | 2015-07-22 16:34:36 -0400 | [diff] [blame] | 254 | filter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 255 | filter.addAction(TelephonyIntents.SPN_STRINGS_UPDATED_ACTION); |
Erik Kline | 32830cc | 2015-04-21 13:09:15 +0900 | [diff] [blame] | 256 | filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 257 | filter.addAction(ConnectivityManager.INET_CONDITION_ACTION); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 258 | filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 259 | mContext.registerReceiver(this, filter, null, mReceiverHandler); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 260 | mListening = true; |
| 261 | |
| 262 | updateMobileControllers(); |
John Spurlock | b98f747 | 2014-07-08 17:09:42 -0400 | [diff] [blame] | 263 | } |
| 264 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 265 | private void unregisterListeners() { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 266 | mListening = false; |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 267 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 268 | MobileSignalController mobileSignalController = mMobileSignalControllers.valueAt(i); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 269 | mobileSignalController.unregisterListener(); |
| 270 | } |
Wink Saville | 071743f | 2015-01-12 17:11:04 -0800 | [diff] [blame] | 271 | mSubscriptionManager.removeOnSubscriptionsChangedListener(mSubscriptionListener); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 272 | mContext.unregisterReceiver(this); |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 273 | } |
| 274 | |
Jason Monk | 0e2400f | 2014-11-21 11:08:55 -0500 | [diff] [blame] | 275 | public int getConnectedWifiLevel() { |
| 276 | return mWifiSignalController.getState().level; |
| 277 | } |
| 278 | |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 279 | @Override |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 280 | public AccessPointController getAccessPointController() { |
| 281 | return mAccessPoints; |
| 282 | } |
| 283 | |
| 284 | @Override |
Jason Monk | f668d7c | 2016-01-14 10:38:41 -0500 | [diff] [blame] | 285 | public DataUsageController getMobileDataController() { |
| 286 | return mDataUsageController; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | public void addEmergencyListener(EmergencyListener listener) { |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 290 | mCallbackHandler.setListening(listener, true); |
| 291 | mCallbackHandler.setEmergencyCallsOnly(isEmergencyOnly()); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 292 | } |
| 293 | |
Jason Monk | 46dbfb4 | 2016-02-25 14:59:20 -0500 | [diff] [blame] | 294 | public void removeEmergencyListener(EmergencyListener listener) { |
| 295 | mCallbackHandler.setListening(listener, false); |
| 296 | } |
| 297 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 298 | public boolean hasMobileDataFeature() { |
| 299 | return mHasMobileDataFeature; |
| 300 | } |
| 301 | |
| 302 | public boolean hasVoiceCallingFeature() { |
| 303 | return mPhone.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE; |
| 304 | } |
| 305 | |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 306 | private MobileSignalController getDataController() { |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 307 | int dataSubId = mSubDefaults.getDefaultDataSubId(); |
Wink Saville | a54bf65 | 2014-12-11 13:37:50 -0800 | [diff] [blame] | 308 | if (!SubscriptionManager.isValidSubscriptionId(dataSubId)) { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 309 | if (DEBUG) Log.e(TAG, "No data sim selected"); |
| 310 | return mDefaultSignalController; |
| 311 | } |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 312 | if (mMobileSignalControllers.indexOfKey(dataSubId) >= 0) { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 313 | return mMobileSignalControllers.get(dataSubId); |
| 314 | } |
Dan Sandler | c846632 | 2014-12-08 15:42:52 -0500 | [diff] [blame] | 315 | if (DEBUG) Log.e(TAG, "Cannot find controller for data sub: " + dataSubId); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 316 | return mDefaultSignalController; |
| 317 | } |
| 318 | |
Pavel Zhamaitsiak | 5504d39 | 2015-03-27 09:24:47 -0700 | [diff] [blame] | 319 | public String getMobileDataNetworkName() { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 320 | MobileSignalController controller = getDataController(); |
Pavel Zhamaitsiak | 5504d39 | 2015-03-27 09:24:47 -0700 | [diff] [blame] | 321 | return controller != null ? controller.getState().networkNameData : ""; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 322 | } |
| 323 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 324 | public boolean isEmergencyOnly() { |
Jason Monk | b0808aa | 2015-07-22 16:34:36 -0400 | [diff] [blame] | 325 | if (mMobileSignalControllers.size() == 0) { |
| 326 | // When there are no active subscriptions, determine emengency state from last |
| 327 | // broadcast. |
Jason Monk | 66845a3 | 2015-08-03 11:09:41 -0400 | [diff] [blame] | 328 | mEmergencySource = EMERGENCY_NO_CONTROLLERS; |
Jason Monk | b0808aa | 2015-07-22 16:34:36 -0400 | [diff] [blame] | 329 | return mLastServiceState != null && mLastServiceState.isEmergencyOnly(); |
| 330 | } |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 331 | int voiceSubId = mSubDefaults.getDefaultVoiceSubId(); |
Wink Saville | a54bf65 | 2014-12-11 13:37:50 -0800 | [diff] [blame] | 332 | if (!SubscriptionManager.isValidSubscriptionId(voiceSubId)) { |
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 | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 335 | if (!mobileSignalController.getState().isEmergency) { |
Jason Monk | 66845a3 | 2015-08-03 11:09:41 -0400 | [diff] [blame] | 336 | mEmergencySource = EMERGENCY_FIRST_CONTROLLER |
| 337 | + mobileSignalController.mSubscriptionInfo.getSubscriptionId(); |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 338 | if (DEBUG) Log.d(TAG, "Found emergency " + mobileSignalController.mTag); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 339 | return false; |
| 340 | } |
| 341 | } |
| 342 | } |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 343 | if (mMobileSignalControllers.indexOfKey(voiceSubId) >= 0) { |
Jason Monk | 66845a3 | 2015-08-03 11:09:41 -0400 | [diff] [blame] | 344 | mEmergencySource = EMERGENCY_VOICE_CONTROLLER + voiceSubId; |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 345 | if (DEBUG) Log.d(TAG, "Getting emergency from " + voiceSubId); |
| 346 | return mMobileSignalControllers.get(voiceSubId).getState().isEmergency; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 347 | } |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 348 | // If we have the wrong subId but there is only one sim anyway, assume it should be the |
| 349 | // default. |
| 350 | if (mMobileSignalControllers.size() == 1) { |
| 351 | mEmergencySource = EMERGENCY_ASSUMED_VOICE_CONTROLLER |
| 352 | + mMobileSignalControllers.keyAt(0); |
| 353 | if (DEBUG) Log.d(TAG, "Getting assumed emergency from " |
| 354 | + mMobileSignalControllers.keyAt(0)); |
| 355 | return mMobileSignalControllers.valueAt(0).getState().isEmergency; |
| 356 | } |
Dan Sandler | c846632 | 2014-12-08 15:42:52 -0500 | [diff] [blame] | 357 | if (DEBUG) Log.e(TAG, "Cannot find controller for voice sub: " + voiceSubId); |
Jason Monk | 66845a3 | 2015-08-03 11:09:41 -0400 | [diff] [blame] | 358 | mEmergencySource = EMERGENCY_NO_SUB + voiceSubId; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 359 | // Something is wrong, better assume we can't make calls... |
| 360 | return true; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 361 | } |
| 362 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 363 | /** |
| 364 | * Emergency status may have changed (triggered by MobileSignalController), |
| 365 | * so we should recheck and send out the state to listeners. |
| 366 | */ |
| 367 | void recalculateEmergency() { |
Jason Monk | 66845a3 | 2015-08-03 11:09:41 -0400 | [diff] [blame] | 368 | mIsEmergency = isEmergencyOnly(); |
| 369 | mCallbackHandler.setEmergencyCallsOnly(mIsEmergency); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 370 | } |
| 371 | |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 372 | public void addCallback(SignalCallback cb) { |
Jason Monk | e06b065 | 2016-03-02 16:35:27 -0500 | [diff] [blame] | 373 | cb.setSubs(mCurrentSubscriptions); |
| 374 | cb.setIsAirplaneMode(new IconState(mAirplaneMode, |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 375 | TelephonyIcons.FLIGHT_MODE_ICON, R.string.accessibility_airplane_mode, mContext)); |
Jason Monk | e06b065 | 2016-03-02 16:35:27 -0500 | [diff] [blame] | 376 | cb.setNoSims(mHasNoSims); |
| 377 | mWifiSignalController.notifyListeners(cb); |
| 378 | mEthernetSignalController.notifyListeners(cb); |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 379 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 380 | MobileSignalController mobileSignalController = mMobileSignalControllers.valueAt(i); |
Jason Monk | e06b065 | 2016-03-02 16:35:27 -0500 | [diff] [blame] | 381 | mobileSignalController.notifyListeners(cb); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 382 | } |
Jason Monk | e06b065 | 2016-03-02 16:35:27 -0500 | [diff] [blame] | 383 | mCallbackHandler.setListening(cb, true); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 384 | } |
| 385 | |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 386 | @Override |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 387 | public void removeCallback(SignalCallback cb) { |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 388 | mCallbackHandler.setListening(cb, false); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | @Override |
| 392 | public void setWifiEnabled(final boolean enabled) { |
| 393 | new AsyncTask<Void, Void, Void>() { |
| 394 | @Override |
| 395 | protected Void doInBackground(Void... args) { |
| 396 | // Disable tethering if enabling Wifi |
| 397 | final int wifiApState = mWifiManager.getWifiApState(); |
| 398 | if (enabled && ((wifiApState == WifiManager.WIFI_AP_STATE_ENABLING) || |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 399 | (wifiApState == WifiManager.WIFI_AP_STATE_ENABLED))) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 400 | mWifiManager.setWifiApEnabled(null, false); |
| 401 | } |
| 402 | |
| 403 | mWifiManager.setWifiEnabled(enabled); |
| 404 | return null; |
| 405 | } |
| 406 | }.execute(); |
| 407 | } |
| 408 | |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 409 | private void onUserSwitched(int newUserId) { |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 410 | mCurrentUserId = newUserId; |
| 411 | mAccessPoints.onUserSwitched(newUserId); |
| 412 | updateConnectivity(); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 416 | public void onReceive(Context context, Intent intent) { |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 417 | if (CHATTY) { |
| 418 | Log.d(TAG, "onReceive: intent=" + intent); |
| 419 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 420 | final String action = intent.getAction(); |
Erik Kline | 32830cc | 2015-04-21 13:09:15 +0900 | [diff] [blame] | 421 | if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION) || |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 422 | action.equals(ConnectivityManager.INET_CONDITION_ACTION)) { |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 423 | updateConnectivity(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 424 | } else if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) { |
| 425 | refreshLocale(); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 426 | updateAirplaneMode(false); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 427 | } else if (action.equals(TelephonyIntents.ACTION_DEFAULT_VOICE_SUBSCRIPTION_CHANGED)) { |
| 428 | // We are using different subs now, we might be able to make calls. |
| 429 | recalculateEmergency(); |
| 430 | } else if (action.equals(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED)) { |
| 431 | // Notify every MobileSignalController so they can know whether they are the |
| 432 | // data sim or not. |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 433 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 434 | MobileSignalController controller = mMobileSignalControllers.valueAt(i); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 435 | controller.handleBroadcast(intent); |
| 436 | } |
| 437 | } else if (action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) { |
| 438 | // Might have different subscriptions now. |
| 439 | updateMobileControllers(); |
Jason Monk | b0808aa | 2015-07-22 16:34:36 -0400 | [diff] [blame] | 440 | } else if (action.equals(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED)) { |
| 441 | mLastServiceState = ServiceState.newFromBundle(intent.getExtras()); |
| 442 | if (mMobileSignalControllers.size() == 0) { |
| 443 | // If none of the subscriptions are active, we might need to recalculate |
| 444 | // emergency state. |
| 445 | recalculateEmergency(); |
| 446 | } |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 447 | } else { |
| 448 | int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY, |
| 449 | SubscriptionManager.INVALID_SUBSCRIPTION_ID); |
Wink Saville | a54bf65 | 2014-12-11 13:37:50 -0800 | [diff] [blame] | 450 | if (SubscriptionManager.isValidSubscriptionId(subId)) { |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 451 | if (mMobileSignalControllers.indexOfKey(subId) >= 0) { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 452 | mMobileSignalControllers.get(subId).handleBroadcast(intent); |
| 453 | } else { |
| 454 | // Can't find this subscription... We must be out of date. |
| 455 | updateMobileControllers(); |
| 456 | } |
| 457 | } else { |
| 458 | // No sub id, must be for the wifi. |
| 459 | mWifiSignalController.handleBroadcast(intent); |
| 460 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 461 | } |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 462 | } |
| 463 | |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 464 | public void onConfigurationChanged(Configuration newConfig) { |
Sungmin Choi | 6be4f6b | 2015-07-07 13:06:59 +0900 | [diff] [blame] | 465 | mConfig = Config.readConfig(mContext); |
Jason Monk | 1c040db | 2015-07-20 09:45:54 -0400 | [diff] [blame] | 466 | mReceiverHandler.post(new Runnable() { |
| 467 | @Override |
| 468 | public void run() { |
| 469 | handleConfigurationChanged(); |
| 470 | } |
| 471 | }); |
| 472 | } |
| 473 | |
| 474 | @VisibleForTesting |
| 475 | void handleConfigurationChanged() { |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 476 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 477 | MobileSignalController controller = mMobileSignalControllers.valueAt(i); |
| 478 | controller.setConfiguration(mConfig); |
Jason Monk | 3aaabd7 | 2014-12-12 11:11:44 -0500 | [diff] [blame] | 479 | } |
| 480 | refreshLocale(); |
Jason Monk | 3aaabd7 | 2014-12-12 11:11:44 -0500 | [diff] [blame] | 481 | } |
| 482 | |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 483 | private void updateMobileControllers() { |
| 484 | if (!mListening) { |
| 485 | return; |
| 486 | } |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 487 | doUpdateMobileControllers(); |
| 488 | } |
| 489 | |
| 490 | @VisibleForTesting |
| 491 | void doUpdateMobileControllers() { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 492 | List<SubscriptionInfo> subscriptions = mSubscriptionManager.getActiveSubscriptionInfoList(); |
Jason Monk | c9f0571 | 2014-12-15 12:24:10 -0500 | [diff] [blame] | 493 | if (subscriptions == null) { |
| 494 | subscriptions = Collections.emptyList(); |
| 495 | } |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 496 | // If there have been no relevant changes to any of the subscriptions, we can leave as is. |
| 497 | if (hasCorrectMobileControllers(subscriptions)) { |
| 498 | // Even if the controllers are correct, make sure we have the right no sims state. |
| 499 | // Such as on boot, don't need any controllers, because there are no sims, |
| 500 | // but we still need to update the no sim state. |
| 501 | updateNoSims(); |
| 502 | return; |
| 503 | } |
| 504 | setCurrentSubscriptions(subscriptions); |
| 505 | updateNoSims(); |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 506 | recalculateEmergency(); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 507 | } |
| 508 | |
Jason Monk | 21d05a0 | 2014-12-02 12:46:58 -0500 | [diff] [blame] | 509 | @VisibleForTesting |
| 510 | protected void updateNoSims() { |
| 511 | boolean hasNoSims = mHasMobileDataFeature && mMobileSignalControllers.size() == 0; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 512 | if (hasNoSims != mHasNoSims) { |
| 513 | mHasNoSims = hasNoSims; |
Jason Monk | 259e06f | 2015-06-18 14:07:26 -0400 | [diff] [blame] | 514 | mCallbackHandler.setNoSims(mHasNoSims); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | |
| 518 | @VisibleForTesting |
| 519 | void setCurrentSubscriptions(List<SubscriptionInfo> subscriptions) { |
| 520 | Collections.sort(subscriptions, new Comparator<SubscriptionInfo>() { |
| 521 | @Override |
| 522 | public int compare(SubscriptionInfo lhs, SubscriptionInfo rhs) { |
| 523 | return lhs.getSimSlotIndex() == rhs.getSimSlotIndex() |
| 524 | ? lhs.getSubscriptionId() - rhs.getSubscriptionId() |
| 525 | : lhs.getSimSlotIndex() - rhs.getSimSlotIndex(); |
| 526 | } |
| 527 | }); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 528 | mCurrentSubscriptions = subscriptions; |
| 529 | |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 530 | SparseArray<MobileSignalController> cachedControllers = |
| 531 | new SparseArray<MobileSignalController>(); |
| 532 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 533 | cachedControllers.put(mMobileSignalControllers.keyAt(i), |
| 534 | mMobileSignalControllers.valueAt(i)); |
| 535 | } |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 536 | mMobileSignalControllers.clear(); |
| 537 | final int num = subscriptions.size(); |
| 538 | for (int i = 0; i < num; i++) { |
| 539 | int subId = subscriptions.get(i).getSubscriptionId(); |
| 540 | // 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] | 541 | if (cachedControllers.indexOfKey(subId) >= 0) { |
| 542 | mMobileSignalControllers.put(subId, cachedControllers.get(subId)); |
| 543 | cachedControllers.remove(subId); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 544 | } else { |
| 545 | MobileSignalController controller = new MobileSignalController(mContext, mConfig, |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 546 | mHasMobileDataFeature, mPhone, mCallbackHandler, |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 547 | this, subscriptions.get(i), mSubDefaults, mReceiverHandler.getLooper()); |
Jason Monk | fd57ea7 | 2016-04-29 13:37:58 -0400 | [diff] [blame] | 548 | controller.setUserSetupComplete(mUserSetup); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 549 | mMobileSignalControllers.put(subId, controller); |
| 550 | if (subscriptions.get(i).getSimSlotIndex() == 0) { |
| 551 | mDefaultSignalController = controller; |
| 552 | } |
| 553 | if (mListening) { |
| 554 | controller.registerListener(); |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | if (mListening) { |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 559 | for (int i = 0; i < cachedControllers.size(); i++) { |
| 560 | int key = cachedControllers.keyAt(i); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 561 | if (cachedControllers.get(key) == mDefaultSignalController) { |
| 562 | mDefaultSignalController = null; |
| 563 | } |
| 564 | cachedControllers.get(key).unregisterListener(); |
| 565 | } |
| 566 | } |
Jason Monk | 8fcab35 | 2015-06-29 10:57:00 -0400 | [diff] [blame] | 567 | mCallbackHandler.setSubs(subscriptions); |
| 568 | notifyAllListeners(); |
| 569 | |
Jason Monk | 25d8a48 | 2014-12-09 12:27:24 -0500 | [diff] [blame] | 570 | // There may be new MobileSignalControllers around, make sure they get the current |
| 571 | // inet condition and airplane mode. |
| 572 | pushConnectivityToSignals(); |
| 573 | updateAirplaneMode(true /* force */); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 574 | } |
| 575 | |
Jason Monk | 0288de0 | 2017-02-23 14:48:05 -0500 | [diff] [blame] | 576 | private void setUserSetupComplete(final boolean userSetup) { |
| 577 | mReceiverHandler.post(() -> handleSetUserSetupComplete(userSetup)); |
Jason Monk | fd57ea7 | 2016-04-29 13:37:58 -0400 | [diff] [blame] | 578 | } |
| 579 | |
Jason Monk | 0288de0 | 2017-02-23 14:48:05 -0500 | [diff] [blame] | 580 | private void handleSetUserSetupComplete(boolean userSetup) { |
Jason Monk | fd57ea7 | 2016-04-29 13:37:58 -0400 | [diff] [blame] | 581 | mUserSetup = userSetup; |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 582 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 583 | MobileSignalController controller = mMobileSignalControllers.valueAt(i); |
Jason Monk | fd57ea7 | 2016-04-29 13:37:58 -0400 | [diff] [blame] | 584 | controller.setUserSetupComplete(mUserSetup); |
| 585 | } |
| 586 | } |
| 587 | |
Jason Monk | c014dec | 2014-12-12 11:49:55 -0500 | [diff] [blame] | 588 | @VisibleForTesting |
| 589 | boolean hasCorrectMobileControllers(List<SubscriptionInfo> allSubscriptions) { |
Jason Monk | c9f0571 | 2014-12-15 12:24:10 -0500 | [diff] [blame] | 590 | if (allSubscriptions.size() != mMobileSignalControllers.size()) { |
| 591 | return false; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 592 | } |
| 593 | for (SubscriptionInfo info : allSubscriptions) { |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 594 | if (mMobileSignalControllers.indexOfKey(info.getSubscriptionId()) < 0) { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 595 | return false; |
| 596 | } |
| 597 | } |
| 598 | return true; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 599 | } |
| 600 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 601 | private void updateAirplaneMode(boolean force) { |
| 602 | boolean airplaneMode = (Settings.Global.getInt(mContext.getContentResolver(), |
| 603 | Settings.Global.AIRPLANE_MODE_ON, 0) == 1); |
| 604 | if (airplaneMode != mAirplaneMode || force) { |
| 605 | mAirplaneMode = airplaneMode; |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 606 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 607 | MobileSignalController mobileSignalController = mMobileSignalControllers.valueAt(i); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 608 | mobileSignalController.setAirplaneMode(mAirplaneMode); |
| 609 | } |
| 610 | notifyListeners(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 611 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | private void refreshLocale() { |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 615 | Locale current = mContext.getResources().getConfiguration().locale; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 616 | if (!current.equals(mLocale)) { |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 617 | mLocale = current; |
| 618 | notifyAllListeners(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 622 | /** |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 623 | * Forces update of all callbacks on both SignalClusters and |
| 624 | * NetworkSignalChangedCallbacks. |
| 625 | */ |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 626 | private void notifyAllListeners() { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 627 | notifyListeners(); |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 628 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 629 | MobileSignalController mobileSignalController = mMobileSignalControllers.valueAt(i); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 630 | mobileSignalController.notifyListeners(); |
| 631 | } |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 632 | mWifiSignalController.notifyListeners(); |
Lorenzo Colitti | d46bb58 | 2015-04-27 20:32:01 +0900 | [diff] [blame] | 633 | mEthernetSignalController.notifyListeners(); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 634 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 635 | |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 636 | /** |
| 637 | * Notifies listeners of changes in state of to the NetworkController, but |
| 638 | * does not notify for any info on SignalControllers, for that call |
| 639 | * notifyAllListeners. |
| 640 | */ |
| 641 | private void notifyListeners() { |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 642 | mCallbackHandler.setIsAirplaneMode(new IconState(mAirplaneMode, |
| 643 | TelephonyIcons.FLIGHT_MODE_ICON, R.string.accessibility_airplane_mode, mContext)); |
| 644 | mCallbackHandler.setNoSims(mHasNoSims); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 645 | } |
| 646 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 647 | /** |
| 648 | * Update the Inet conditions and what network we are connected to. |
| 649 | */ |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 650 | private void updateConnectivity() { |
| 651 | mConnectedTransports.clear(); |
| 652 | mValidatedTransports.clear(); |
| 653 | for (NetworkCapabilities nc : |
| 654 | mConnectivityManager.getDefaultNetworkCapabilitiesForUser(mCurrentUserId)) { |
| 655 | for (int transportType : nc.getTransportTypes()) { |
| 656 | mConnectedTransports.set(transportType); |
| 657 | if (nc.hasCapability(NET_CAPABILITY_VALIDATED)) { |
| 658 | mValidatedTransports.set(transportType); |
| 659 | } |
| 660 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 661 | } |
| 662 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 663 | if (CHATTY) { |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 664 | Log.d(TAG, "updateConnectivity: mConnectedTransports=" + mConnectedTransports); |
| 665 | Log.d(TAG, "updateConnectivity: mValidatedTransports=" + mValidatedTransports); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 666 | } |
| 667 | |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 668 | mInetCondition = !mValidatedTransports.isEmpty(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 669 | |
Jason Monk | 25d8a48 | 2014-12-09 12:27:24 -0500 | [diff] [blame] | 670 | pushConnectivityToSignals(); |
| 671 | } |
| 672 | |
| 673 | /** |
| 674 | * Pushes the current connectivity state to all SignalControllers. |
| 675 | */ |
| 676 | private void pushConnectivityToSignals() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 677 | // 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] | 678 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 679 | MobileSignalController mobileSignalController = mMobileSignalControllers.valueAt(i); |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 680 | mobileSignalController.updateConnectivity(mConnectedTransports, mValidatedTransports); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 681 | } |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 682 | mWifiSignalController.updateConnectivity(mConnectedTransports, mValidatedTransports); |
| 683 | mEthernetSignalController.updateConnectivity(mConnectedTransports, mValidatedTransports); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 684 | } |
| 685 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 686 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 687 | pw.println("NetworkController state:"); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 688 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 689 | pw.println(" - telephony ------"); |
| 690 | pw.print(" hasVoiceCallingFeature()="); |
| 691 | pw.println(hasVoiceCallingFeature()); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 692 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 693 | pw.println(" - connectivity ------"); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 694 | pw.print(" mConnectedTransports="); |
| 695 | pw.println(mConnectedTransports); |
| 696 | pw.print(" mValidatedTransports="); |
| 697 | pw.println(mValidatedTransports); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 698 | pw.print(" mInetCondition="); |
| 699 | pw.println(mInetCondition); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 700 | pw.print(" mAirplaneMode="); |
| 701 | pw.println(mAirplaneMode); |
| 702 | pw.print(" mLocale="); |
| 703 | pw.println(mLocale); |
Jason Monk | b0808aa | 2015-07-22 16:34:36 -0400 | [diff] [blame] | 704 | pw.print(" mLastServiceState="); |
| 705 | pw.println(mLastServiceState); |
Jason Monk | 66845a3 | 2015-08-03 11:09:41 -0400 | [diff] [blame] | 706 | pw.print(" mIsEmergency="); |
| 707 | pw.println(mIsEmergency); |
| 708 | pw.print(" mEmergencySource="); |
| 709 | pw.println(emergencyToString(mEmergencySource)); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 710 | |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 711 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 712 | MobileSignalController mobileSignalController = mMobileSignalControllers.valueAt(i); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 713 | mobileSignalController.dump(pw); |
| 714 | } |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 715 | mWifiSignalController.dump(pw); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 716 | |
Lorenzo Colitti | d46bb58 | 2015-04-27 20:32:01 +0900 | [diff] [blame] | 717 | mEthernetSignalController.dump(pw); |
| 718 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 719 | mAccessPoints.dump(pw); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 720 | } |
| 721 | |
Jason Monk | 66845a3 | 2015-08-03 11:09:41 -0400 | [diff] [blame] | 722 | private static final String emergencyToString(int emergencySource) { |
| 723 | if (emergencySource > EMERGENCY_NO_SUB) { |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 724 | return "ASSUMED_VOICE_CONTROLLER(" + (emergencySource - EMERGENCY_VOICE_CONTROLLER) |
| 725 | + ")"; |
| 726 | } else if (emergencySource > EMERGENCY_NO_SUB) { |
Jason Monk | 66845a3 | 2015-08-03 11:09:41 -0400 | [diff] [blame] | 727 | return "NO_SUB(" + (emergencySource - EMERGENCY_NO_SUB) + ")"; |
| 728 | } else if (emergencySource > EMERGENCY_VOICE_CONTROLLER) { |
| 729 | return "VOICE_CONTROLLER(" + (emergencySource - EMERGENCY_VOICE_CONTROLLER) + ")"; |
| 730 | } else if (emergencySource > EMERGENCY_FIRST_CONTROLLER) { |
| 731 | return "FIRST_CONTROLLER(" + (emergencySource - EMERGENCY_FIRST_CONTROLLER) + ")"; |
| 732 | } else if (emergencySource == EMERGENCY_NO_CONTROLLERS) { |
| 733 | return "NO_CONTROLLERS"; |
| 734 | } |
| 735 | return "UNKNOWN_SOURCE"; |
| 736 | } |
| 737 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 738 | private boolean mDemoMode; |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 739 | private boolean mDemoInetCondition; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 740 | private WifiSignalController.WifiState mDemoWifiState; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 741 | |
| 742 | @Override |
| 743 | public void dispatchDemoCommand(String command, Bundle args) { |
| 744 | if (!mDemoMode && command.equals(COMMAND_ENTER)) { |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 745 | if (DEBUG) Log.d(TAG, "Entering demo mode"); |
| 746 | unregisterListeners(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 747 | mDemoMode = true; |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 748 | mDemoInetCondition = mInetCondition; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 749 | mDemoWifiState = mWifiSignalController.getState(); |
Kenneth Westin | 5c88ffb | 2013-11-28 16:29:40 +0100 | [diff] [blame] | 750 | mDemoWifiState.ssid = "DemoMode"; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 751 | } else if (mDemoMode && command.equals(COMMAND_EXIT)) { |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 752 | if (DEBUG) Log.d(TAG, "Exiting demo mode"); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 753 | mDemoMode = false; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 754 | // Update what MobileSignalControllers, because they may change |
| 755 | // to set the number of sim slots. |
| 756 | updateMobileControllers(); |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 757 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 758 | MobileSignalController controller = mMobileSignalControllers.valueAt(i); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 759 | controller.resetLastState(); |
| 760 | } |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 761 | mWifiSignalController.resetLastState(); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 762 | mReceiverHandler.post(mRegisterListeners); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 763 | notifyAllListeners(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 764 | } else if (mDemoMode && command.equals(COMMAND_NETWORK)) { |
| 765 | String airplane = args.getString("airplane"); |
| 766 | if (airplane != null) { |
| 767 | boolean show = airplane.equals("show"); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 768 | mCallbackHandler.setIsAirplaneMode(new IconState(show, |
| 769 | TelephonyIcons.FLIGHT_MODE_ICON, R.string.accessibility_airplane_mode, |
| 770 | mContext)); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 771 | } |
| 772 | String fully = args.getString("fully"); |
| 773 | if (fully != null) { |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 774 | mDemoInetCondition = Boolean.parseBoolean(fully); |
| 775 | BitSet connected = new BitSet(); |
| 776 | |
| 777 | if (mDemoInetCondition) { |
| 778 | connected.set(mWifiSignalController.mTransportType); |
| 779 | } |
| 780 | mWifiSignalController.updateConnectivity(connected, connected); |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 781 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 782 | MobileSignalController controller = mMobileSignalControllers.valueAt(i); |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 783 | if (mDemoInetCondition) { |
| 784 | connected.set(controller.mTransportType); |
| 785 | } |
| 786 | controller.updateConnectivity(connected, connected); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 787 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 788 | } |
| 789 | String wifi = args.getString("wifi"); |
| 790 | if (wifi != null) { |
| 791 | boolean show = wifi.equals("show"); |
| 792 | String level = args.getString("level"); |
| 793 | if (level != null) { |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 794 | mDemoWifiState.level = level.equals("null") ? -1 |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 795 | : Math.min(Integer.parseInt(level), WifiIcons.WIFI_LEVEL_COUNT - 1); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 796 | mDemoWifiState.connected = mDemoWifiState.level >= 0; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 797 | } |
Kenneth Westin | 5c88ffb | 2013-11-28 16:29:40 +0100 | [diff] [blame] | 798 | String activity = args.getString("activity"); |
| 799 | if (activity != null) { |
| 800 | switch (activity) { |
| 801 | case "inout": |
| 802 | mWifiSignalController.setActivity(WifiManager.DATA_ACTIVITY_INOUT); |
| 803 | break; |
| 804 | case "in": |
| 805 | mWifiSignalController.setActivity(WifiManager.DATA_ACTIVITY_IN); |
| 806 | break; |
| 807 | case "out": |
| 808 | mWifiSignalController.setActivity(WifiManager.DATA_ACTIVITY_OUT); |
| 809 | break; |
| 810 | default: |
| 811 | mWifiSignalController.setActivity(WifiManager.DATA_ACTIVITY_NONE); |
| 812 | break; |
| 813 | } |
| 814 | } else { |
| 815 | mWifiSignalController.setActivity(WifiManager.DATA_ACTIVITY_NONE); |
| 816 | } |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 817 | mDemoWifiState.enabled = show; |
| 818 | mWifiSignalController.notifyListeners(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 819 | } |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 820 | String sims = args.getString("sims"); |
| 821 | if (sims != null) { |
John Spurlock | cf053c1 | 2015-05-14 16:33:59 -0400 | [diff] [blame] | 822 | int num = MathUtils.constrain(Integer.parseInt(sims), 1, 8); |
| 823 | List<SubscriptionInfo> subs = new ArrayList<>(); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 824 | if (num != mMobileSignalControllers.size()) { |
| 825 | mMobileSignalControllers.clear(); |
| 826 | int start = mSubscriptionManager.getActiveSubscriptionInfoCountMax(); |
| 827 | 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] | 828 | subs.add(addSignalController(i, i)); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 829 | } |
Jason Monk | 33b60bb | 2015-07-13 10:42:23 -0400 | [diff] [blame] | 830 | mCallbackHandler.setSubs(subs); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 831 | } |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 832 | } |
| 833 | String nosim = args.getString("nosim"); |
| 834 | if (nosim != null) { |
Jason Monk | 259e06f | 2015-06-18 14:07:26 -0400 | [diff] [blame] | 835 | mHasNoSims = nosim.equals("show"); |
| 836 | mCallbackHandler.setNoSims(mHasNoSims); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 837 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 838 | String mobile = args.getString("mobile"); |
| 839 | if (mobile != null) { |
| 840 | boolean show = mobile.equals("show"); |
| 841 | String datatype = args.getString("datatype"); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 842 | String slotString = args.getString("slot"); |
| 843 | int slot = TextUtils.isEmpty(slotString) ? 0 : Integer.parseInt(slotString); |
John Spurlock | cf053c1 | 2015-05-14 16:33:59 -0400 | [diff] [blame] | 844 | slot = MathUtils.constrain(slot, 0, 8); |
| 845 | // Ensure we have enough sim slots |
| 846 | List<SubscriptionInfo> subs = new ArrayList<>(); |
| 847 | while (mMobileSignalControllers.size() <= slot) { |
| 848 | int nextSlot = mMobileSignalControllers.size(); |
| 849 | subs.add(addSignalController(nextSlot, nextSlot)); |
| 850 | } |
| 851 | if (!subs.isEmpty()) { |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 852 | mCallbackHandler.setSubs(subs); |
John Spurlock | cf053c1 | 2015-05-14 16:33:59 -0400 | [diff] [blame] | 853 | } |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 854 | // Hack to index linearly for easy use. |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 855 | MobileSignalController controller = mMobileSignalControllers.valueAt(slot); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 856 | controller.getState().dataSim = datatype != null; |
Jason Monk | b595098 | 2017-01-24 15:53:53 -0500 | [diff] [blame] | 857 | controller.getState().isDefault = datatype != null; |
| 858 | controller.getState().dataConnected = datatype != null; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 859 | if (datatype != null) { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 860 | controller.getState().iconGroup = |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 861 | datatype.equals("1x") ? TelephonyIcons.ONE_X : |
| 862 | datatype.equals("3g") ? TelephonyIcons.THREE_G : |
| 863 | datatype.equals("4g") ? TelephonyIcons.FOUR_G : |
Ajay Nambi | 7965fa4 | 2015-05-14 18:48:33 -0700 | [diff] [blame] | 864 | datatype.equals("4g+") ? TelephonyIcons.FOUR_G_PLUS : |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 865 | datatype.equals("e") ? TelephonyIcons.E : |
| 866 | datatype.equals("g") ? TelephonyIcons.G : |
| 867 | datatype.equals("h") ? TelephonyIcons.H : |
| 868 | datatype.equals("lte") ? TelephonyIcons.LTE : |
Jason Monk | aa730be | 2016-07-13 14:22:52 -0400 | [diff] [blame] | 869 | datatype.equals("lte+") ? TelephonyIcons.LTE_PLUS : |
Jason Monk | b595098 | 2017-01-24 15:53:53 -0500 | [diff] [blame] | 870 | datatype.equals("dis") ? TelephonyIcons.DATA_DISABLED : |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 871 | TelephonyIcons.UNKNOWN; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 872 | } |
Jason Monk | 0f0de13 | 2016-12-19 15:36:13 -0500 | [diff] [blame] | 873 | if (args.containsKey("roam")) { |
| 874 | controller.getState().roaming = "show".equals(args.getString("roam")); |
| 875 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 876 | String level = args.getString("level"); |
| 877 | if (level != null) { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 878 | controller.getState().level = level.equals("null") ? -1 |
Jason Monk | 48edc0c | 2017-04-10 15:01:27 -0400 | [diff] [blame^] | 879 | : Math.min(Integer.parseInt(level), |
| 880 | SignalStrength.NUM_SIGNAL_STRENGTH_BINS); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 881 | controller.getState().connected = controller.getState().level >= 0; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 882 | } |
Jason Monk | 0f0de13 | 2016-12-19 15:36:13 -0500 | [diff] [blame] | 883 | String activity = args.getString("activity"); |
| 884 | if (activity != null) { |
Kenneth Westin | 5c88ffb | 2013-11-28 16:29:40 +0100 | [diff] [blame] | 885 | controller.getState().dataConnected = true; |
| 886 | switch (activity) { |
| 887 | case "inout": |
| 888 | controller.setActivity(TelephonyManager.DATA_ACTIVITY_INOUT); |
| 889 | break; |
| 890 | case "in": |
| 891 | controller.setActivity(TelephonyManager.DATA_ACTIVITY_IN); |
| 892 | break; |
| 893 | case "out": |
| 894 | controller.setActivity(TelephonyManager.DATA_ACTIVITY_OUT); |
| 895 | break; |
| 896 | default: |
| 897 | controller.setActivity(TelephonyManager.DATA_ACTIVITY_NONE); |
| 898 | break; |
| 899 | } |
| 900 | } else { |
| 901 | controller.setActivity(TelephonyManager.DATA_ACTIVITY_NONE); |
Jason Monk | 0f0de13 | 2016-12-19 15:36:13 -0500 | [diff] [blame] | 902 | } |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 903 | controller.getState().enabled = show; |
| 904 | controller.notifyListeners(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 905 | } |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 906 | String carrierNetworkChange = args.getString("carriernetworkchange"); |
| 907 | if (carrierNetworkChange != null) { |
| 908 | boolean show = carrierNetworkChange.equals("show"); |
Jason Monk | 8996b94 | 2017-03-02 13:58:19 -0500 | [diff] [blame] | 909 | for (int i = 0; i < mMobileSignalControllers.size(); i++) { |
| 910 | MobileSignalController controller = mMobileSignalControllers.valueAt(i); |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 911 | controller.setCarrierNetworkChangeMode(show); |
| 912 | } |
| 913 | } |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 914 | } |
| 915 | } |
| 916 | |
John Spurlock | cf053c1 | 2015-05-14 16:33:59 -0400 | [diff] [blame] | 917 | private SubscriptionInfo addSignalController(int id, int simSlotIndex) { |
| 918 | SubscriptionInfo info = new SubscriptionInfo(id, "", simSlotIndex, "", "", 0, 0, "", 0, |
fionaxu | 1bf6ec2 | 2016-05-23 16:33:16 -0700 | [diff] [blame] | 919 | null, 0, 0, ""); |
Jason Monk | b595098 | 2017-01-24 15:53:53 -0500 | [diff] [blame] | 920 | MobileSignalController controller = new MobileSignalController(mContext, |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 921 | mConfig, mHasMobileDataFeature, mPhone, mCallbackHandler, this, info, |
Jason Monk | b595098 | 2017-01-24 15:53:53 -0500 | [diff] [blame] | 922 | mSubDefaults, mReceiverHandler.getLooper()); |
| 923 | mMobileSignalControllers.put(id, controller); |
| 924 | controller.getState().userSetup = true; |
John Spurlock | cf053c1 | 2015-05-14 16:33:59 -0400 | [diff] [blame] | 925 | return info; |
| 926 | } |
| 927 | |
Adrian Roos | 316bf54 | 2016-08-23 17:53:07 +0200 | [diff] [blame] | 928 | public boolean hasEmergencyCryptKeeperText() { |
| 929 | return EncryptionHelper.IS_DATA_ENCRYPTED; |
| 930 | } |
| 931 | |
| 932 | public boolean isRadioOn() { |
| 933 | return !mAirplaneMode; |
| 934 | } |
| 935 | |
Jason Monk | 8fcab35 | 2015-06-29 10:57:00 -0400 | [diff] [blame] | 936 | private class SubListener extends OnSubscriptionsChangedListener { |
Jason Monk | c9f0571 | 2014-12-15 12:24:10 -0500 | [diff] [blame] | 937 | @Override |
| 938 | public void onSubscriptionsChanged() { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 939 | updateMobileControllers(); |
Jason Monk | 8fcab35 | 2015-06-29 10:57:00 -0400 | [diff] [blame] | 940 | } |
| 941 | } |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 942 | |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 943 | /** |
| 944 | * Used to register listeners from the BG Looper, this way the PhoneStateListeners that |
| 945 | * get created will also run on the BG Looper. |
| 946 | */ |
| 947 | private final Runnable mRegisterListeners = new Runnable() { |
| 948 | @Override |
| 949 | public void run() { |
| 950 | registerListeners(); |
| 951 | } |
| 952 | }; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 953 | |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 954 | public static class SubscriptionDefaults { |
| 955 | public int getDefaultVoiceSubId() { |
Shishir Agrawal | 7ea3e8b | 2016-01-25 13:03:07 -0800 | [diff] [blame] | 956 | return SubscriptionManager.getDefaultVoiceSubscriptionId(); |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | public int getDefaultDataSubId() { |
Shishir Agrawal | 7ea3e8b | 2016-01-25 13:03:07 -0800 | [diff] [blame] | 960 | return SubscriptionManager.getDefaultDataSubscriptionId(); |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 961 | } |
| 962 | } |
| 963 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 964 | @VisibleForTesting |
| 965 | static class Config { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 966 | boolean showAtLeast3G = false; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 967 | boolean alwaysShowCdmaRssi = false; |
| 968 | boolean show4gForLte = false; |
Robert Greenwalt | 37d34ba | 2016-07-27 14:54:34 -0700 | [diff] [blame] | 969 | boolean hideLtePlus = false; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 970 | boolean hspaDataDistinguishable; |
| 971 | |
| 972 | static Config readConfig(Context context) { |
| 973 | Config config = new Config(); |
| 974 | Resources res = context.getResources(); |
| 975 | |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 976 | config.showAtLeast3G = res.getBoolean(R.bool.config_showMin3G); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 977 | config.alwaysShowCdmaRssi = |
| 978 | res.getBoolean(com.android.internal.R.bool.config_alwaysUseCdmaRssi); |
| 979 | config.show4gForLte = res.getBoolean(R.bool.config_show4GForLTE); |
| 980 | config.hspaDataDistinguishable = |
| 981 | res.getBoolean(R.bool.config_hspa_data_distinguishable); |
Robert Greenwalt | 37d34ba | 2016-07-27 14:54:34 -0700 | [diff] [blame] | 982 | config.hideLtePlus = res.getBoolean(R.bool.config_hideLtePlus); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 983 | return config; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 984 | } |
| 985 | } |
| 986 | } |