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 | |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 19 | import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 20 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 21 | import android.content.BroadcastReceiver; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.content.IntentFilter; |
| 25 | import android.content.res.Resources; |
| 26 | import android.net.ConnectivityManager; |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 27 | import android.net.NetworkCapabilities; |
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 | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 34 | import android.telephony.SubscriptionInfo; |
| 35 | import android.telephony.SubscriptionManager; |
| 36 | import android.telephony.SubscriptionManager.OnSubscriptionsChangedListener; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 37 | import android.telephony.TelephonyManager; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 38 | import android.text.TextUtils; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 39 | import android.util.Log; |
John Spurlock | cf053c1 | 2015-05-14 16:33:59 -0400 | [diff] [blame] | 40 | import android.util.MathUtils; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 41 | |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 42 | import com.android.internal.annotations.VisibleForTesting; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 43 | import com.android.internal.telephony.PhoneConstants; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 44 | import com.android.internal.telephony.TelephonyIntents; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 45 | import com.android.systemui.DemoMode; |
| 46 | import com.android.systemui.R; |
| 47 | |
| 48 | import java.io.FileDescriptor; |
| 49 | import java.io.PrintWriter; |
| 50 | import java.util.ArrayList; |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 51 | import java.util.BitSet; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 52 | import java.util.Collections; |
| 53 | import java.util.Comparator; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 54 | import java.util.HashMap; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 55 | import java.util.List; |
| 56 | import java.util.Locale; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 57 | import java.util.Map; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 58 | |
| 59 | /** Platform implementation of the network controller. **/ |
| 60 | public class NetworkControllerImpl extends BroadcastReceiver |
| 61 | implements NetworkController, DemoMode { |
| 62 | // debug |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 63 | static final String TAG = "NetworkController"; |
| 64 | static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 65 | // additional diagnostics, but not logspew |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 66 | static final boolean CHATTY = Log.isLoggable(TAG + "Chat", Log.DEBUG); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 67 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 68 | private final Context mContext; |
| 69 | private final TelephonyManager mPhone; |
| 70 | private final WifiManager mWifiManager; |
| 71 | private final ConnectivityManager mConnectivityManager; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 72 | private final SubscriptionManager mSubscriptionManager; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 73 | private final boolean mHasMobileDataFeature; |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 74 | private final SubscriptionDefaults mSubDefaults; |
Jason Monk | 3aaabd7 | 2014-12-12 11:11:44 -0500 | [diff] [blame] | 75 | private Config mConfig; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 76 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 77 | // Subcontrollers. |
| 78 | @VisibleForTesting |
| 79 | final WifiSignalController mWifiSignalController; |
Lorenzo Colitti | d46bb58 | 2015-04-27 20:32:01 +0900 | [diff] [blame] | 80 | |
| 81 | @VisibleForTesting |
| 82 | final EthernetSignalController mEthernetSignalController; |
| 83 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 84 | @VisibleForTesting |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 85 | final Map<Integer, MobileSignalController> mMobileSignalControllers = |
| 86 | new HashMap<Integer, MobileSignalController>(); |
| 87 | // When no SIMs are around at setup, and one is added later, it seems to default to the first |
| 88 | // SIM for most actions. This may be null if there aren't any SIMs around. |
| 89 | private MobileSignalController mDefaultSignalController; |
Jason Monk | 0e2400f | 2014-11-21 11:08:55 -0500 | [diff] [blame] | 90 | private final AccessPointControllerImpl mAccessPoints; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 91 | private final MobileDataControllerImpl mMobileDataController; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 92 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 93 | private boolean mInetCondition; // Used for Logging and demo. |
Lorenzo Colitti | 23dd176 | 2014-09-10 22:58:54 +0900 | [diff] [blame] | 94 | |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 95 | // BitSets indicating which network transport types (e.g., TRANSPORT_WIFI, TRANSPORT_MOBILE) are |
| 96 | // connected and validated, respectively. |
| 97 | private final BitSet mConnectedTransports = new BitSet(); |
| 98 | private final BitSet mValidatedTransports = new BitSet(); |
| 99 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 100 | // States that don't belong to a subcontroller. |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 101 | private boolean mAirplaneMode = false; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 102 | private boolean mHasNoSims; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 103 | private Locale mLocale = null; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 104 | // This list holds our ordering. |
John Spurlock | cf053c1 | 2015-05-14 16:33:59 -0400 | [diff] [blame] | 105 | private List<SubscriptionInfo> mCurrentSubscriptions = new ArrayList<>(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 106 | |
Jason Monk | c014dec | 2014-12-12 11:49:55 -0500 | [diff] [blame] | 107 | @VisibleForTesting |
| 108 | boolean mListening; |
John Spurlock | 7f8f22a | 2014-07-02 18:54:17 -0400 | [diff] [blame] | 109 | |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 110 | // The current user ID. |
| 111 | private int mCurrentUserId; |
| 112 | |
Jason Monk | 8fcab35 | 2015-06-29 10:57:00 -0400 | [diff] [blame] | 113 | private OnSubscriptionsChangedListener mSubscriptionListener; |
| 114 | |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 115 | // Handler that all broadcasts are received on. |
| 116 | private final Handler mReceiverHandler; |
| 117 | // Handler that all callbacks are made on. |
| 118 | private final CallbackHandler mCallbackHandler; |
| 119 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 120 | /** |
| 121 | * Construct this controller object and register for updates. |
| 122 | */ |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 123 | public NetworkControllerImpl(Context context, Looper bgLooper) { |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 124 | this(context, (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE), |
| 125 | (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE), |
Jason Monk | 29f7a7b | 2014-11-17 14:40:56 -0500 | [diff] [blame] | 126 | (WifiManager) context.getSystemService(Context.WIFI_SERVICE), |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 127 | SubscriptionManager.from(context), Config.readConfig(context), bgLooper, |
| 128 | new CallbackHandler(), |
Jason Monk | 30d8004 | 2015-05-08 16:54:18 -0400 | [diff] [blame] | 129 | new AccessPointControllerImpl(context, bgLooper), |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 130 | new MobileDataControllerImpl(context), |
| 131 | new SubscriptionDefaults()); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 132 | mReceiverHandler.post(mRegisterListeners); |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @VisibleForTesting |
| 136 | NetworkControllerImpl(Context context, ConnectivityManager connectivityManager, |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 137 | TelephonyManager telephonyManager, WifiManager wifiManager, |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 138 | SubscriptionManager subManager, Config config, Looper bgLooper, |
| 139 | CallbackHandler callbackHandler, |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 140 | AccessPointControllerImpl accessPointController, |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 141 | MobileDataControllerImpl mobileDataController, |
| 142 | SubscriptionDefaults defaultsHandler) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 143 | mContext = context; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 144 | mConfig = config; |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 145 | mReceiverHandler = new Handler(bgLooper); |
| 146 | mCallbackHandler = callbackHandler; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 147 | |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 148 | mSubscriptionManager = subManager; |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 149 | mSubDefaults = defaultsHandler; |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 150 | mConnectivityManager = connectivityManager; |
| 151 | mHasMobileDataFeature = |
| 152 | mConnectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 153 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 154 | // telephony |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 155 | mPhone = telephonyManager; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 156 | |
| 157 | // wifi |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 158 | mWifiManager = wifiManager; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 159 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 160 | mLocale = mContext.getResources().getConfiguration().locale; |
Jason Monk | 29f7a7b | 2014-11-17 14:40:56 -0500 | [diff] [blame] | 161 | mAccessPoints = accessPointController; |
| 162 | mMobileDataController = mobileDataController; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 163 | mMobileDataController.setNetworkController(this); |
| 164 | // TODO: Find a way to move this into MobileDataController. |
| 165 | mMobileDataController.setCallback(new MobileDataControllerImpl.Callback() { |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 166 | @Override |
| 167 | public void onMobileDataEnabled(boolean enabled) { |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 168 | mCallbackHandler.setMobileDataEnabled(enabled); |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 169 | } |
| 170 | }); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 171 | mWifiSignalController = new WifiSignalController(mContext, mHasMobileDataFeature, |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 172 | mCallbackHandler, this); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 173 | |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 174 | mEthernetSignalController = new EthernetSignalController(mContext, mCallbackHandler, this); |
Lorenzo Colitti | d46bb58 | 2015-04-27 20:32:01 +0900 | [diff] [blame] | 175 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 176 | // 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] | 177 | updateAirplaneMode(true /* force callback */); |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 178 | } |
| 179 | |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 180 | private void registerListeners() { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 181 | for (MobileSignalController mobileSignalController : mMobileSignalControllers.values()) { |
| 182 | mobileSignalController.registerListener(); |
| 183 | } |
Jason Monk | 8fcab35 | 2015-06-29 10:57:00 -0400 | [diff] [blame] | 184 | if (mSubscriptionListener == null) { |
| 185 | mSubscriptionListener = new SubListener(); |
| 186 | } |
Wink Saville | 071743f | 2015-01-12 17:11:04 -0800 | [diff] [blame] | 187 | mSubscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionListener); |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 188 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 189 | // broadcasts |
| 190 | IntentFilter filter = new IntentFilter(); |
| 191 | filter.addAction(WifiManager.RSSI_CHANGED_ACTION); |
| 192 | filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); |
| 193 | filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); |
| 194 | filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 195 | filter.addAction(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED); |
| 196 | filter.addAction(TelephonyIntents.ACTION_DEFAULT_VOICE_SUBSCRIPTION_CHANGED); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 197 | filter.addAction(TelephonyIntents.SPN_STRINGS_UPDATED_ACTION); |
Erik Kline | 32830cc | 2015-04-21 13:09:15 +0900 | [diff] [blame] | 198 | filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 199 | filter.addAction(ConnectivityManager.INET_CONDITION_ACTION); |
| 200 | filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
| 201 | filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 202 | mContext.registerReceiver(this, filter, null, mReceiverHandler); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 203 | mListening = true; |
| 204 | |
| 205 | updateMobileControllers(); |
John Spurlock | b98f747 | 2014-07-08 17:09:42 -0400 | [diff] [blame] | 206 | } |
| 207 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 208 | private void unregisterListeners() { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 209 | mListening = false; |
| 210 | for (MobileSignalController mobileSignalController : mMobileSignalControllers.values()) { |
| 211 | mobileSignalController.unregisterListener(); |
| 212 | } |
Wink Saville | 071743f | 2015-01-12 17:11:04 -0800 | [diff] [blame] | 213 | mSubscriptionManager.removeOnSubscriptionsChangedListener(mSubscriptionListener); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 214 | mContext.unregisterReceiver(this); |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 215 | } |
| 216 | |
Jason Monk | 0e2400f | 2014-11-21 11:08:55 -0500 | [diff] [blame] | 217 | public int getConnectedWifiLevel() { |
| 218 | return mWifiSignalController.getState().level; |
| 219 | } |
| 220 | |
Jason Monk | 5d32507 | 2014-10-27 11:38:47 -0400 | [diff] [blame] | 221 | @Override |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 222 | public AccessPointController getAccessPointController() { |
| 223 | return mAccessPoints; |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public MobileDataController getMobileDataController() { |
| 228 | return mMobileDataController; |
| 229 | } |
| 230 | |
| 231 | public void addEmergencyListener(EmergencyListener listener) { |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 232 | mCallbackHandler.setListening(listener, true); |
| 233 | mCallbackHandler.setEmergencyCallsOnly(isEmergencyOnly()); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | public boolean hasMobileDataFeature() { |
| 237 | return mHasMobileDataFeature; |
| 238 | } |
| 239 | |
| 240 | public boolean hasVoiceCallingFeature() { |
| 241 | return mPhone.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE; |
| 242 | } |
| 243 | |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 244 | private MobileSignalController getDataController() { |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 245 | int dataSubId = mSubDefaults.getDefaultDataSubId(); |
Wink Saville | a54bf65 | 2014-12-11 13:37:50 -0800 | [diff] [blame] | 246 | if (!SubscriptionManager.isValidSubscriptionId(dataSubId)) { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 247 | if (DEBUG) Log.e(TAG, "No data sim selected"); |
| 248 | return mDefaultSignalController; |
| 249 | } |
| 250 | if (mMobileSignalControllers.containsKey(dataSubId)) { |
| 251 | return mMobileSignalControllers.get(dataSubId); |
| 252 | } |
Dan Sandler | c846632 | 2014-12-08 15:42:52 -0500 | [diff] [blame] | 253 | if (DEBUG) Log.e(TAG, "Cannot find controller for data sub: " + dataSubId); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 254 | return mDefaultSignalController; |
| 255 | } |
| 256 | |
Pavel Zhamaitsiak | 5504d39 | 2015-03-27 09:24:47 -0700 | [diff] [blame] | 257 | public String getMobileDataNetworkName() { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 258 | MobileSignalController controller = getDataController(); |
Pavel Zhamaitsiak | 5504d39 | 2015-03-27 09:24:47 -0700 | [diff] [blame] | 259 | return controller != null ? controller.getState().networkNameData : ""; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 260 | } |
| 261 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 262 | public boolean isEmergencyOnly() { |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 263 | int voiceSubId = mSubDefaults.getDefaultVoiceSubId(); |
Wink Saville | a54bf65 | 2014-12-11 13:37:50 -0800 | [diff] [blame] | 264 | if (!SubscriptionManager.isValidSubscriptionId(voiceSubId)) { |
| 265 | for (MobileSignalController mobileSignalController : |
| 266 | mMobileSignalControllers.values()) { |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 267 | if (!mobileSignalController.getState().isEmergency) { |
| 268 | if (DEBUG) Log.d(TAG, "Found emergency " + mobileSignalController.mTag); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 269 | return false; |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | if (mMobileSignalControllers.containsKey(voiceSubId)) { |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 274 | if (DEBUG) Log.d(TAG, "Getting emergency from " + voiceSubId); |
| 275 | return mMobileSignalControllers.get(voiceSubId).getState().isEmergency; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 276 | } |
Dan Sandler | c846632 | 2014-12-08 15:42:52 -0500 | [diff] [blame] | 277 | if (DEBUG) Log.e(TAG, "Cannot find controller for voice sub: " + voiceSubId); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 278 | // Something is wrong, better assume we can't make calls... |
| 279 | return true; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 280 | } |
| 281 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 282 | /** |
| 283 | * Emergency status may have changed (triggered by MobileSignalController), |
| 284 | * so we should recheck and send out the state to listeners. |
| 285 | */ |
| 286 | void recalculateEmergency() { |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 287 | mCallbackHandler.setEmergencyCallsOnly(isEmergencyOnly()); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 288 | } |
| 289 | |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 290 | public void addSignalCallback(SignalCallback cb) { |
| 291 | mCallbackHandler.setListening(cb, true); |
| 292 | mCallbackHandler.setSubs(mCurrentSubscriptions); |
| 293 | mCallbackHandler.setIsAirplaneMode(new IconState(mAirplaneMode, |
| 294 | TelephonyIcons.FLIGHT_MODE_ICON, R.string.accessibility_airplane_mode, mContext)); |
| 295 | mCallbackHandler.setNoSims(mHasNoSims); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 296 | mWifiSignalController.notifyListeners(); |
Lorenzo Colitti | d46bb58 | 2015-04-27 20:32:01 +0900 | [diff] [blame] | 297 | mEthernetSignalController.notifyListeners(); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 298 | for (MobileSignalController mobileSignalController : mMobileSignalControllers.values()) { |
| 299 | mobileSignalController.notifyListeners(); |
| 300 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 301 | } |
| 302 | |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 303 | @Override |
| 304 | public void removeSignalCallback(SignalCallback cb) { |
| 305 | mCallbackHandler.setListening(cb, false); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | @Override |
| 309 | public void setWifiEnabled(final boolean enabled) { |
| 310 | new AsyncTask<Void, Void, Void>() { |
| 311 | @Override |
| 312 | protected Void doInBackground(Void... args) { |
| 313 | // Disable tethering if enabling Wifi |
| 314 | final int wifiApState = mWifiManager.getWifiApState(); |
| 315 | if (enabled && ((wifiApState == WifiManager.WIFI_AP_STATE_ENABLING) || |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 316 | (wifiApState == WifiManager.WIFI_AP_STATE_ENABLED))) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 317 | mWifiManager.setWifiApEnabled(null, false); |
| 318 | } |
| 319 | |
| 320 | mWifiManager.setWifiEnabled(enabled); |
| 321 | return null; |
| 322 | } |
| 323 | }.execute(); |
| 324 | } |
| 325 | |
John Spurlock | b98f747 | 2014-07-08 17:09:42 -0400 | [diff] [blame] | 326 | @Override |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 327 | public void onUserSwitched(int newUserId) { |
| 328 | mCurrentUserId = newUserId; |
| 329 | mAccessPoints.onUserSwitched(newUserId); |
| 330 | updateConnectivity(); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 334 | public void onReceive(Context context, Intent intent) { |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 335 | if (CHATTY) { |
| 336 | Log.d(TAG, "onReceive: intent=" + intent); |
| 337 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 338 | final String action = intent.getAction(); |
Erik Kline | 32830cc | 2015-04-21 13:09:15 +0900 | [diff] [blame] | 339 | if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION) || |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 340 | action.equals(ConnectivityManager.INET_CONDITION_ACTION)) { |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 341 | updateConnectivity(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 342 | } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { |
Jason Monk | 3aaabd7 | 2014-12-12 11:11:44 -0500 | [diff] [blame] | 343 | handleConfigurationChanged(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 344 | } else if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) { |
| 345 | refreshLocale(); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 346 | updateAirplaneMode(false); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 347 | } else if (action.equals(TelephonyIntents.ACTION_DEFAULT_VOICE_SUBSCRIPTION_CHANGED)) { |
| 348 | // We are using different subs now, we might be able to make calls. |
| 349 | recalculateEmergency(); |
| 350 | } else if (action.equals(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED)) { |
| 351 | // Notify every MobileSignalController so they can know whether they are the |
| 352 | // data sim or not. |
| 353 | for (MobileSignalController controller : mMobileSignalControllers.values()) { |
| 354 | controller.handleBroadcast(intent); |
| 355 | } |
| 356 | } else if (action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) { |
| 357 | // Might have different subscriptions now. |
| 358 | updateMobileControllers(); |
| 359 | } else { |
| 360 | int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY, |
| 361 | SubscriptionManager.INVALID_SUBSCRIPTION_ID); |
Wink Saville | a54bf65 | 2014-12-11 13:37:50 -0800 | [diff] [blame] | 362 | if (SubscriptionManager.isValidSubscriptionId(subId)) { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 363 | if (mMobileSignalControllers.containsKey(subId)) { |
| 364 | mMobileSignalControllers.get(subId).handleBroadcast(intent); |
| 365 | } else { |
| 366 | // Can't find this subscription... We must be out of date. |
| 367 | updateMobileControllers(); |
| 368 | } |
| 369 | } else { |
| 370 | // No sub id, must be for the wifi. |
| 371 | mWifiSignalController.handleBroadcast(intent); |
| 372 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 373 | } |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 374 | } |
| 375 | |
Sungmin Choi | 6be4f6b | 2015-07-07 13:06:59 +0900 | [diff] [blame^] | 376 | public void handleConfigurationChanged() { |
| 377 | mConfig = Config.readConfig(mContext); |
Jason Monk | 3aaabd7 | 2014-12-12 11:11:44 -0500 | [diff] [blame] | 378 | for (MobileSignalController mobileSignalController : mMobileSignalControllers.values()) { |
| 379 | mobileSignalController.setConfiguration(mConfig); |
| 380 | } |
| 381 | refreshLocale(); |
Jason Monk | 3aaabd7 | 2014-12-12 11:11:44 -0500 | [diff] [blame] | 382 | } |
| 383 | |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 384 | private void updateMobileControllers() { |
| 385 | if (!mListening) { |
| 386 | return; |
| 387 | } |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 388 | doUpdateMobileControllers(); |
| 389 | } |
| 390 | |
| 391 | @VisibleForTesting |
| 392 | void doUpdateMobileControllers() { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 393 | List<SubscriptionInfo> subscriptions = mSubscriptionManager.getActiveSubscriptionInfoList(); |
Jason Monk | c9f0571 | 2014-12-15 12:24:10 -0500 | [diff] [blame] | 394 | if (subscriptions == null) { |
| 395 | subscriptions = Collections.emptyList(); |
| 396 | } |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 397 | // If there have been no relevant changes to any of the subscriptions, we can leave as is. |
| 398 | if (hasCorrectMobileControllers(subscriptions)) { |
| 399 | // Even if the controllers are correct, make sure we have the right no sims state. |
| 400 | // Such as on boot, don't need any controllers, because there are no sims, |
| 401 | // but we still need to update the no sim state. |
| 402 | updateNoSims(); |
| 403 | return; |
| 404 | } |
| 405 | setCurrentSubscriptions(subscriptions); |
| 406 | updateNoSims(); |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 407 | recalculateEmergency(); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 408 | } |
| 409 | |
Jason Monk | 21d05a0 | 2014-12-02 12:46:58 -0500 | [diff] [blame] | 410 | @VisibleForTesting |
| 411 | protected void updateNoSims() { |
| 412 | boolean hasNoSims = mHasMobileDataFeature && mMobileSignalControllers.size() == 0; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 413 | if (hasNoSims != mHasNoSims) { |
| 414 | mHasNoSims = hasNoSims; |
Jason Monk | 259e06f | 2015-06-18 14:07:26 -0400 | [diff] [blame] | 415 | mCallbackHandler.setNoSims(mHasNoSims); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
| 419 | @VisibleForTesting |
| 420 | void setCurrentSubscriptions(List<SubscriptionInfo> subscriptions) { |
| 421 | Collections.sort(subscriptions, new Comparator<SubscriptionInfo>() { |
| 422 | @Override |
| 423 | public int compare(SubscriptionInfo lhs, SubscriptionInfo rhs) { |
| 424 | return lhs.getSimSlotIndex() == rhs.getSimSlotIndex() |
| 425 | ? lhs.getSubscriptionId() - rhs.getSubscriptionId() |
| 426 | : lhs.getSimSlotIndex() - rhs.getSimSlotIndex(); |
| 427 | } |
| 428 | }); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 429 | mCurrentSubscriptions = subscriptions; |
| 430 | |
| 431 | HashMap<Integer, MobileSignalController> cachedControllers = |
| 432 | new HashMap<Integer, MobileSignalController>(mMobileSignalControllers); |
| 433 | mMobileSignalControllers.clear(); |
| 434 | final int num = subscriptions.size(); |
| 435 | for (int i = 0; i < num; i++) { |
| 436 | int subId = subscriptions.get(i).getSubscriptionId(); |
| 437 | // If we have a copy of this controller already reuse it, otherwise make a new one. |
| 438 | if (cachedControllers.containsKey(subId)) { |
Jason Monk | c014dec | 2014-12-12 11:49:55 -0500 | [diff] [blame] | 439 | mMobileSignalControllers.put(subId, cachedControllers.remove(subId)); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 440 | } else { |
| 441 | MobileSignalController controller = new MobileSignalController(mContext, mConfig, |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 442 | mHasMobileDataFeature, mPhone, mCallbackHandler, |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 443 | this, subscriptions.get(i), mSubDefaults, mReceiverHandler.getLooper()); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 444 | mMobileSignalControllers.put(subId, controller); |
| 445 | if (subscriptions.get(i).getSimSlotIndex() == 0) { |
| 446 | mDefaultSignalController = controller; |
| 447 | } |
| 448 | if (mListening) { |
| 449 | controller.registerListener(); |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | if (mListening) { |
| 454 | for (Integer key : cachedControllers.keySet()) { |
| 455 | if (cachedControllers.get(key) == mDefaultSignalController) { |
| 456 | mDefaultSignalController = null; |
| 457 | } |
| 458 | cachedControllers.get(key).unregisterListener(); |
| 459 | } |
| 460 | } |
Jason Monk | 8fcab35 | 2015-06-29 10:57:00 -0400 | [diff] [blame] | 461 | mCallbackHandler.setSubs(subscriptions); |
| 462 | notifyAllListeners(); |
| 463 | |
Jason Monk | 25d8a48 | 2014-12-09 12:27:24 -0500 | [diff] [blame] | 464 | // There may be new MobileSignalControllers around, make sure they get the current |
| 465 | // inet condition and airplane mode. |
| 466 | pushConnectivityToSignals(); |
| 467 | updateAirplaneMode(true /* force */); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 468 | } |
| 469 | |
Jason Monk | c014dec | 2014-12-12 11:49:55 -0500 | [diff] [blame] | 470 | @VisibleForTesting |
| 471 | boolean hasCorrectMobileControllers(List<SubscriptionInfo> allSubscriptions) { |
Jason Monk | c9f0571 | 2014-12-15 12:24:10 -0500 | [diff] [blame] | 472 | if (allSubscriptions.size() != mMobileSignalControllers.size()) { |
| 473 | return false; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 474 | } |
| 475 | for (SubscriptionInfo info : allSubscriptions) { |
| 476 | if (!mMobileSignalControllers.containsKey(info.getSubscriptionId())) { |
| 477 | return false; |
| 478 | } |
| 479 | } |
| 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 | private void updateAirplaneMode(boolean force) { |
| 484 | boolean airplaneMode = (Settings.Global.getInt(mContext.getContentResolver(), |
| 485 | Settings.Global.AIRPLANE_MODE_ON, 0) == 1); |
| 486 | if (airplaneMode != mAirplaneMode || force) { |
| 487 | mAirplaneMode = airplaneMode; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 488 | for (MobileSignalController mobileSignalController : mMobileSignalControllers.values()) { |
| 489 | mobileSignalController.setAirplaneMode(mAirplaneMode); |
| 490 | } |
| 491 | notifyListeners(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 492 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | private void refreshLocale() { |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 496 | Locale current = mContext.getResources().getConfiguration().locale; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 497 | if (!current.equals(mLocale)) { |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 498 | mLocale = current; |
| 499 | notifyAllListeners(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 503 | /** |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 504 | * Forces update of all callbacks on both SignalClusters and |
| 505 | * NetworkSignalChangedCallbacks. |
| 506 | */ |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 507 | private void notifyAllListeners() { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 508 | notifyListeners(); |
| 509 | for (MobileSignalController mobileSignalController : mMobileSignalControllers.values()) { |
| 510 | mobileSignalController.notifyListeners(); |
| 511 | } |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 512 | mWifiSignalController.notifyListeners(); |
Lorenzo Colitti | d46bb58 | 2015-04-27 20:32:01 +0900 | [diff] [blame] | 513 | mEthernetSignalController.notifyListeners(); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 514 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 515 | |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 516 | /** |
| 517 | * Notifies listeners of changes in state of to the NetworkController, but |
| 518 | * does not notify for any info on SignalControllers, for that call |
| 519 | * notifyAllListeners. |
| 520 | */ |
| 521 | private void notifyListeners() { |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 522 | mCallbackHandler.setIsAirplaneMode(new IconState(mAirplaneMode, |
| 523 | TelephonyIcons.FLIGHT_MODE_ICON, R.string.accessibility_airplane_mode, mContext)); |
| 524 | mCallbackHandler.setNoSims(mHasNoSims); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 525 | } |
| 526 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 527 | /** |
| 528 | * Update the Inet conditions and what network we are connected to. |
| 529 | */ |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 530 | private void updateConnectivity() { |
| 531 | mConnectedTransports.clear(); |
| 532 | mValidatedTransports.clear(); |
| 533 | for (NetworkCapabilities nc : |
| 534 | mConnectivityManager.getDefaultNetworkCapabilitiesForUser(mCurrentUserId)) { |
| 535 | for (int transportType : nc.getTransportTypes()) { |
| 536 | mConnectedTransports.set(transportType); |
| 537 | if (nc.hasCapability(NET_CAPABILITY_VALIDATED)) { |
| 538 | mValidatedTransports.set(transportType); |
| 539 | } |
| 540 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 541 | } |
| 542 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 543 | if (CHATTY) { |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 544 | Log.d(TAG, "updateConnectivity: mConnectedTransports=" + mConnectedTransports); |
| 545 | Log.d(TAG, "updateConnectivity: mValidatedTransports=" + mValidatedTransports); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 546 | } |
| 547 | |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 548 | mInetCondition = !mValidatedTransports.isEmpty(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 549 | |
Jason Monk | 25d8a48 | 2014-12-09 12:27:24 -0500 | [diff] [blame] | 550 | pushConnectivityToSignals(); |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Pushes the current connectivity state to all SignalControllers. |
| 555 | */ |
| 556 | private void pushConnectivityToSignals() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 557 | // We want to update all the icons, all at once, for any condition change |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 558 | for (MobileSignalController mobileSignalController : mMobileSignalControllers.values()) { |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 559 | mobileSignalController.updateConnectivity(mConnectedTransports, mValidatedTransports); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 560 | } |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 561 | mWifiSignalController.updateConnectivity(mConnectedTransports, mValidatedTransports); |
| 562 | mEthernetSignalController.updateConnectivity(mConnectedTransports, mValidatedTransports); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 563 | } |
| 564 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 565 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 566 | pw.println("NetworkController state:"); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 567 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 568 | pw.println(" - telephony ------"); |
| 569 | pw.print(" hasVoiceCallingFeature()="); |
| 570 | pw.println(hasVoiceCallingFeature()); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 571 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 572 | pw.println(" - connectivity ------"); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 573 | pw.print(" mConnectedTransports="); |
| 574 | pw.println(mConnectedTransports); |
| 575 | pw.print(" mValidatedTransports="); |
| 576 | pw.println(mValidatedTransports); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 577 | pw.print(" mInetCondition="); |
| 578 | pw.println(mInetCondition); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 579 | pw.print(" mAirplaneMode="); |
| 580 | pw.println(mAirplaneMode); |
| 581 | pw.print(" mLocale="); |
| 582 | pw.println(mLocale); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 583 | |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 584 | for (MobileSignalController mobileSignalController : mMobileSignalControllers.values()) { |
| 585 | mobileSignalController.dump(pw); |
| 586 | } |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 587 | mWifiSignalController.dump(pw); |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 588 | |
Lorenzo Colitti | d46bb58 | 2015-04-27 20:32:01 +0900 | [diff] [blame] | 589 | mEthernetSignalController.dump(pw); |
| 590 | |
Jason Monk | d52356a | 2015-01-28 10:40:41 -0500 | [diff] [blame] | 591 | mAccessPoints.dump(pw); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | private boolean mDemoMode; |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 595 | private boolean mDemoInetCondition; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 596 | private WifiSignalController.WifiState mDemoWifiState; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 597 | |
| 598 | @Override |
| 599 | public void dispatchDemoCommand(String command, Bundle args) { |
| 600 | if (!mDemoMode && command.equals(COMMAND_ENTER)) { |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 601 | if (DEBUG) Log.d(TAG, "Entering demo mode"); |
| 602 | unregisterListeners(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 603 | mDemoMode = true; |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 604 | mDemoInetCondition = mInetCondition; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 605 | mDemoWifiState = mWifiSignalController.getState(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 606 | } else if (mDemoMode && command.equals(COMMAND_EXIT)) { |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 607 | if (DEBUG) Log.d(TAG, "Exiting demo mode"); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 608 | mDemoMode = false; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 609 | // Update what MobileSignalControllers, because they may change |
| 610 | // to set the number of sim slots. |
| 611 | updateMobileControllers(); |
| 612 | for (MobileSignalController controller : mMobileSignalControllers.values()) { |
| 613 | controller.resetLastState(); |
| 614 | } |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 615 | mWifiSignalController.resetLastState(); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 616 | mReceiverHandler.post(mRegisterListeners); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 617 | notifyAllListeners(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 618 | } else if (mDemoMode && command.equals(COMMAND_NETWORK)) { |
| 619 | String airplane = args.getString("airplane"); |
| 620 | if (airplane != null) { |
| 621 | boolean show = airplane.equals("show"); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 622 | mCallbackHandler.setIsAirplaneMode(new IconState(show, |
| 623 | TelephonyIcons.FLIGHT_MODE_ICON, R.string.accessibility_airplane_mode, |
| 624 | mContext)); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 625 | } |
| 626 | String fully = args.getString("fully"); |
| 627 | if (fully != null) { |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 628 | mDemoInetCondition = Boolean.parseBoolean(fully); |
| 629 | BitSet connected = new BitSet(); |
| 630 | |
| 631 | if (mDemoInetCondition) { |
| 632 | connected.set(mWifiSignalController.mTransportType); |
| 633 | } |
| 634 | mWifiSignalController.updateConnectivity(connected, connected); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 635 | for (MobileSignalController controller : mMobileSignalControllers.values()) { |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 636 | if (mDemoInetCondition) { |
| 637 | connected.set(controller.mTransportType); |
| 638 | } |
| 639 | controller.updateConnectivity(connected, connected); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 640 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 641 | } |
| 642 | String wifi = args.getString("wifi"); |
| 643 | if (wifi != null) { |
| 644 | boolean show = wifi.equals("show"); |
| 645 | String level = args.getString("level"); |
| 646 | if (level != null) { |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 647 | mDemoWifiState.level = level.equals("null") ? -1 |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 648 | : Math.min(Integer.parseInt(level), WifiIcons.WIFI_LEVEL_COUNT - 1); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 649 | mDemoWifiState.connected = mDemoWifiState.level >= 0; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 650 | } |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 651 | mDemoWifiState.enabled = show; |
| 652 | mWifiSignalController.notifyListeners(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 653 | } |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 654 | String sims = args.getString("sims"); |
| 655 | if (sims != null) { |
John Spurlock | cf053c1 | 2015-05-14 16:33:59 -0400 | [diff] [blame] | 656 | int num = MathUtils.constrain(Integer.parseInt(sims), 1, 8); |
| 657 | List<SubscriptionInfo> subs = new ArrayList<>(); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 658 | if (num != mMobileSignalControllers.size()) { |
| 659 | mMobileSignalControllers.clear(); |
| 660 | int start = mSubscriptionManager.getActiveSubscriptionInfoCountMax(); |
| 661 | 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] | 662 | subs.add(addSignalController(i, i)); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 663 | } |
| 664 | } |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 665 | mCallbackHandler.setSubs(subs); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 666 | } |
| 667 | String nosim = args.getString("nosim"); |
| 668 | if (nosim != null) { |
Jason Monk | 259e06f | 2015-06-18 14:07:26 -0400 | [diff] [blame] | 669 | mHasNoSims = nosim.equals("show"); |
| 670 | mCallbackHandler.setNoSims(mHasNoSims); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 671 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 672 | String mobile = args.getString("mobile"); |
| 673 | if (mobile != null) { |
| 674 | boolean show = mobile.equals("show"); |
| 675 | String datatype = args.getString("datatype"); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 676 | String slotString = args.getString("slot"); |
| 677 | int slot = TextUtils.isEmpty(slotString) ? 0 : Integer.parseInt(slotString); |
John Spurlock | cf053c1 | 2015-05-14 16:33:59 -0400 | [diff] [blame] | 678 | slot = MathUtils.constrain(slot, 0, 8); |
| 679 | // Ensure we have enough sim slots |
| 680 | List<SubscriptionInfo> subs = new ArrayList<>(); |
| 681 | while (mMobileSignalControllers.size() <= slot) { |
| 682 | int nextSlot = mMobileSignalControllers.size(); |
| 683 | subs.add(addSignalController(nextSlot, nextSlot)); |
| 684 | } |
| 685 | if (!subs.isEmpty()) { |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 686 | mCallbackHandler.setSubs(subs); |
John Spurlock | cf053c1 | 2015-05-14 16:33:59 -0400 | [diff] [blame] | 687 | } |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 688 | // Hack to index linearly for easy use. |
| 689 | MobileSignalController controller = mMobileSignalControllers |
| 690 | .values().toArray(new MobileSignalController[0])[slot]; |
| 691 | controller.getState().dataSim = datatype != null; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 692 | if (datatype != null) { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 693 | controller.getState().iconGroup = |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 694 | datatype.equals("1x") ? TelephonyIcons.ONE_X : |
| 695 | datatype.equals("3g") ? TelephonyIcons.THREE_G : |
| 696 | datatype.equals("4g") ? TelephonyIcons.FOUR_G : |
| 697 | datatype.equals("e") ? TelephonyIcons.E : |
| 698 | datatype.equals("g") ? TelephonyIcons.G : |
| 699 | datatype.equals("h") ? TelephonyIcons.H : |
| 700 | datatype.equals("lte") ? TelephonyIcons.LTE : |
| 701 | datatype.equals("roam") ? TelephonyIcons.ROAMING : |
| 702 | TelephonyIcons.UNKNOWN; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 703 | } |
| 704 | int[][] icons = TelephonyIcons.TELEPHONY_SIGNAL_STRENGTH; |
| 705 | String level = args.getString("level"); |
| 706 | if (level != null) { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 707 | controller.getState().level = level.equals("null") ? -1 |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 708 | : Math.min(Integer.parseInt(level), icons[0].length - 1); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 709 | controller.getState().connected = controller.getState().level >= 0; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 710 | } |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 711 | controller.getState().enabled = show; |
| 712 | controller.notifyListeners(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 713 | } |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 714 | String carrierNetworkChange = args.getString("carriernetworkchange"); |
| 715 | if (carrierNetworkChange != null) { |
| 716 | boolean show = carrierNetworkChange.equals("show"); |
| 717 | for (MobileSignalController controller : mMobileSignalControllers.values()) { |
| 718 | controller.setCarrierNetworkChangeMode(show); |
| 719 | } |
| 720 | } |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | |
John Spurlock | cf053c1 | 2015-05-14 16:33:59 -0400 | [diff] [blame] | 724 | private SubscriptionInfo addSignalController(int id, int simSlotIndex) { |
| 725 | SubscriptionInfo info = new SubscriptionInfo(id, "", simSlotIndex, "", "", 0, 0, "", 0, |
| 726 | null, 0, 0, ""); |
| 727 | mMobileSignalControllers.put(id, new MobileSignalController(mContext, |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 728 | mConfig, mHasMobileDataFeature, mPhone, mCallbackHandler, this, info, |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 729 | mSubDefaults, mReceiverHandler.getLooper())); |
John Spurlock | cf053c1 | 2015-05-14 16:33:59 -0400 | [diff] [blame] | 730 | return info; |
| 731 | } |
| 732 | |
Jason Monk | 8fcab35 | 2015-06-29 10:57:00 -0400 | [diff] [blame] | 733 | private class SubListener extends OnSubscriptionsChangedListener { |
Jason Monk | c9f0571 | 2014-12-15 12:24:10 -0500 | [diff] [blame] | 734 | @Override |
| 735 | public void onSubscriptionsChanged() { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 736 | updateMobileControllers(); |
Jason Monk | 8fcab35 | 2015-06-29 10:57:00 -0400 | [diff] [blame] | 737 | } |
| 738 | } |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 739 | |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 740 | /** |
| 741 | * Used to register listeners from the BG Looper, this way the PhoneStateListeners that |
| 742 | * get created will also run on the BG Looper. |
| 743 | */ |
| 744 | private final Runnable mRegisterListeners = new Runnable() { |
| 745 | @Override |
| 746 | public void run() { |
| 747 | registerListeners(); |
| 748 | } |
| 749 | }; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 750 | |
| 751 | public interface EmergencyListener { |
| 752 | void setEmergencyCallsOnly(boolean emergencyOnly); |
| 753 | } |
| 754 | |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame] | 755 | public static class SubscriptionDefaults { |
| 756 | public int getDefaultVoiceSubId() { |
| 757 | return SubscriptionManager.getDefaultVoiceSubId(); |
| 758 | } |
| 759 | |
| 760 | public int getDefaultDataSubId() { |
| 761 | return SubscriptionManager.getDefaultDataSubId(); |
| 762 | } |
| 763 | } |
| 764 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 765 | @VisibleForTesting |
| 766 | static class Config { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 767 | boolean showAtLeast3G = false; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 768 | boolean alwaysShowCdmaRssi = false; |
| 769 | boolean show4gForLte = false; |
| 770 | boolean hspaDataDistinguishable; |
| 771 | |
| 772 | static Config readConfig(Context context) { |
| 773 | Config config = new Config(); |
| 774 | Resources res = context.getResources(); |
| 775 | |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 776 | config.showAtLeast3G = res.getBoolean(R.bool.config_showMin3G); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 777 | config.alwaysShowCdmaRssi = |
| 778 | res.getBoolean(com.android.internal.R.bool.config_alwaysUseCdmaRssi); |
| 779 | config.show4gForLte = res.getBoolean(R.bool.config_show4GForLTE); |
| 780 | config.hspaDataDistinguishable = |
| 781 | res.getBoolean(R.bool.config_hspa_data_distinguishable); |
| 782 | return config; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | } |