Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.systemui.statusbar.policy; |
| 17 | |
| 18 | import android.content.Context; |
| 19 | import android.content.Intent; |
| 20 | import android.net.NetworkCapabilities; |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 21 | import android.os.Looper; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 22 | import android.telephony.PhoneStateListener; |
| 23 | import android.telephony.ServiceState; |
| 24 | import android.telephony.SignalStrength; |
| 25 | import android.telephony.SubscriptionInfo; |
| 26 | import android.telephony.SubscriptionManager; |
| 27 | import android.telephony.TelephonyManager; |
| 28 | import android.util.Log; |
| 29 | import android.util.SparseArray; |
| 30 | |
| 31 | import com.android.internal.annotations.VisibleForTesting; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 32 | import com.android.internal.telephony.TelephonyIntents; |
| 33 | import com.android.internal.telephony.cdma.EriInfo; |
| 34 | import com.android.systemui.R; |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 35 | import com.android.systemui.statusbar.policy.NetworkController.IconState; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 36 | import com.android.systemui.statusbar.policy.NetworkControllerImpl.Config; |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame^] | 37 | import com.android.systemui.statusbar.policy.NetworkControllerImpl.SubscriptionDefaults; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 38 | |
| 39 | import java.io.PrintWriter; |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 40 | import java.util.BitSet; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 41 | import java.util.Objects; |
| 42 | |
| 43 | |
| 44 | public class MobileSignalController extends SignalController< |
| 45 | MobileSignalController.MobileState, MobileSignalController.MobileIconGroup> { |
| 46 | private final TelephonyManager mPhone; |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame^] | 47 | private final SubscriptionDefaults mDefaults; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 48 | private final String mNetworkNameDefault; |
| 49 | private final String mNetworkNameSeparator; |
| 50 | @VisibleForTesting |
| 51 | final PhoneStateListener mPhoneStateListener; |
| 52 | // Save entire info for logging, we only use the id. |
| 53 | private final SubscriptionInfo mSubscriptionInfo; |
| 54 | |
| 55 | // @VisibleForDemoMode |
| 56 | final SparseArray<MobileIconGroup> mNetworkToIconLookup; |
| 57 | |
| 58 | // Since some pieces of the phone state are interdependent we store it locally, |
| 59 | // this could potentially become part of MobileState for simplification/complication |
| 60 | // of code. |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 61 | private int mDataNetType = TelephonyManager.NETWORK_TYPE_UNKNOWN; |
| 62 | private int mDataState = TelephonyManager.DATA_DISCONNECTED; |
| 63 | private ServiceState mServiceState; |
| 64 | private SignalStrength mSignalStrength; |
| 65 | private MobileIconGroup mDefaultIcons; |
| 66 | private Config mConfig; |
| 67 | |
| 68 | // TODO: Reduce number of vars passed in, if we have the NetworkController, probably don't |
| 69 | // need listener lists anymore. |
| 70 | public MobileSignalController(Context context, Config config, boolean hasMobileData, |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 71 | TelephonyManager phone, CallbackHandler callbackHandler, |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame^] | 72 | NetworkControllerImpl networkController, SubscriptionInfo info, |
| 73 | SubscriptionDefaults defaults, Looper receiverLooper) { |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 74 | super("MobileSignalController(" + info.getSubscriptionId() + ")", context, |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 75 | NetworkCapabilities.TRANSPORT_CELLULAR, callbackHandler, |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 76 | networkController); |
| 77 | mNetworkToIconLookup = new SparseArray<>(); |
| 78 | mConfig = config; |
| 79 | mPhone = phone; |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame^] | 80 | mDefaults = defaults; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 81 | mSubscriptionInfo = info; |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 82 | mPhoneStateListener = new MobilePhoneStateListener(info.getSubscriptionId(), |
| 83 | receiverLooper); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 84 | mNetworkNameSeparator = getStringIfExists(R.string.status_bar_network_name_separator); |
| 85 | mNetworkNameDefault = getStringIfExists( |
| 86 | com.android.internal.R.string.lockscreen_carrier_default); |
| 87 | |
| 88 | mapIconSets(); |
| 89 | |
| 90 | mLastState.networkName = mCurrentState.networkName = mNetworkNameDefault; |
Pavel Zhamaitsiak | 5504d39 | 2015-03-27 09:24:47 -0700 | [diff] [blame] | 91 | mLastState.networkNameData = mCurrentState.networkNameData = mNetworkNameDefault; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 92 | mLastState.enabled = mCurrentState.enabled = hasMobileData; |
| 93 | mLastState.iconGroup = mCurrentState.iconGroup = mDefaultIcons; |
| 94 | // Get initial data sim state. |
| 95 | updateDataSim(); |
| 96 | } |
| 97 | |
| 98 | public void setConfiguration(Config config) { |
| 99 | mConfig = config; |
| 100 | mapIconSets(); |
| 101 | updateTelephony(); |
| 102 | } |
| 103 | |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 104 | public int getDataContentDescription() { |
| 105 | return getIcons().mDataContentDescription; |
| 106 | } |
| 107 | |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 108 | public void setAirplaneMode(boolean airplaneMode) { |
| 109 | mCurrentState.airplaneMode = airplaneMode; |
| 110 | notifyListenersIfNecessary(); |
| 111 | } |
| 112 | |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 113 | @Override |
| 114 | public void updateConnectivity(BitSet connectedTransports, BitSet validatedTransports) { |
| 115 | boolean isValidated = validatedTransports.get(mTransportType); |
| 116 | mCurrentState.isDefault = connectedTransports.get(mTransportType); |
| 117 | // Only show this as not having connectivity if we are default. |
| 118 | mCurrentState.inetCondition = (isValidated || !mCurrentState.isDefault) ? 1 : 0; |
| 119 | notifyListenersIfNecessary(); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 120 | } |
| 121 | |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 122 | public void setCarrierNetworkChangeMode(boolean carrierNetworkChangeMode) { |
| 123 | mCurrentState.carrierNetworkChangeMode = carrierNetworkChangeMode; |
Andrew Flynn | 2fdbe12 | 2015-06-01 16:34:21 -0400 | [diff] [blame] | 124 | updateTelephony(); |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 125 | } |
| 126 | |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 127 | /** |
| 128 | * Start listening for phone state changes. |
| 129 | */ |
| 130 | public void registerListener() { |
| 131 | mPhone.listen(mPhoneStateListener, |
| 132 | PhoneStateListener.LISTEN_SERVICE_STATE |
| 133 | | PhoneStateListener.LISTEN_SIGNAL_STRENGTHS |
| 134 | | PhoneStateListener.LISTEN_CALL_STATE |
| 135 | | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 136 | | PhoneStateListener.LISTEN_DATA_ACTIVITY |
| 137 | | PhoneStateListener.LISTEN_CARRIER_NETWORK_CHANGE); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Stop listening for phone state changes. |
| 142 | */ |
| 143 | public void unregisterListener() { |
| 144 | mPhone.listen(mPhoneStateListener, 0); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Produce a mapping of data network types to icon groups for simple and quick use in |
| 149 | * updateTelephony. |
| 150 | */ |
| 151 | private void mapIconSets() { |
| 152 | mNetworkToIconLookup.clear(); |
| 153 | |
| 154 | mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_EVDO_0, TelephonyIcons.THREE_G); |
| 155 | mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_EVDO_A, TelephonyIcons.THREE_G); |
| 156 | mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_EVDO_B, TelephonyIcons.THREE_G); |
| 157 | mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_EHRPD, TelephonyIcons.THREE_G); |
| 158 | mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_UMTS, TelephonyIcons.THREE_G); |
| 159 | |
| 160 | if (!mConfig.showAtLeast3G) { |
| 161 | mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_UNKNOWN, |
| 162 | TelephonyIcons.UNKNOWN); |
| 163 | mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_EDGE, TelephonyIcons.E); |
| 164 | mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_CDMA, TelephonyIcons.ONE_X); |
| 165 | mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_1xRTT, TelephonyIcons.ONE_X); |
| 166 | |
| 167 | mDefaultIcons = TelephonyIcons.G; |
| 168 | } else { |
| 169 | mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_UNKNOWN, |
| 170 | TelephonyIcons.THREE_G); |
| 171 | mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_EDGE, |
| 172 | TelephonyIcons.THREE_G); |
| 173 | mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_CDMA, |
| 174 | TelephonyIcons.THREE_G); |
| 175 | mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_1xRTT, |
| 176 | TelephonyIcons.THREE_G); |
| 177 | mDefaultIcons = TelephonyIcons.THREE_G; |
| 178 | } |
| 179 | |
| 180 | MobileIconGroup hGroup = TelephonyIcons.THREE_G; |
| 181 | if (mConfig.hspaDataDistinguishable) { |
| 182 | hGroup = TelephonyIcons.H; |
| 183 | } |
| 184 | mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_HSDPA, hGroup); |
| 185 | mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_HSUPA, hGroup); |
| 186 | mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_HSPA, hGroup); |
| 187 | mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_HSPAP, hGroup); |
| 188 | |
| 189 | if (mConfig.show4gForLte) { |
| 190 | mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_LTE, TelephonyIcons.FOUR_G); |
| 191 | } else { |
| 192 | mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_LTE, TelephonyIcons.LTE); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public void notifyListeners() { |
| 198 | MobileIconGroup icons = getIcons(); |
| 199 | |
| 200 | String contentDescription = getStringIfExists(getContentDescription()); |
| 201 | String dataContentDescription = getStringIfExists(icons.mDataContentDescription); |
Jeff Davidson | 5c7daaf | 2015-01-27 16:24:32 -0800 | [diff] [blame] | 202 | |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 203 | // Show icon in QS when we are connected or need to show roaming. |
| 204 | boolean showDataIcon = mCurrentState.dataConnected |
Jeff Davidson | 5c7daaf | 2015-01-27 16:24:32 -0800 | [diff] [blame] | 205 | || mCurrentState.iconGroup == TelephonyIcons.ROAMING; |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 206 | IconState statusIcon = new IconState(mCurrentState.enabled && !mCurrentState.airplaneMode, |
| 207 | getCurrentIconId(), contentDescription); |
| 208 | |
| 209 | int qsTypeIcon = 0; |
| 210 | IconState qsIcon = null; |
| 211 | String description = null; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 212 | // Only send data sim callbacks to QS. |
| 213 | if (mCurrentState.dataSim) { |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 214 | qsTypeIcon = showDataIcon ? icons.mQsDataType : 0; |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 215 | qsIcon = new IconState(mCurrentState.enabled |
| 216 | && !mCurrentState.isEmergency, getQsCurrentIconId(), contentDescription); |
| 217 | description = mCurrentState.isEmergency ? null : mCurrentState.networkName; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 218 | } |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 219 | boolean activityIn = mCurrentState.dataConnected |
| 220 | && !mCurrentState.carrierNetworkChangeMode |
| 221 | && mCurrentState.activityIn; |
| 222 | boolean activityOut = mCurrentState.dataConnected |
| 223 | && !mCurrentState.carrierNetworkChangeMode |
| 224 | && mCurrentState.activityOut; |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 225 | showDataIcon &= mCurrentState.isDefault; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 226 | int typeIcon = showDataIcon ? icons.mDataType : 0; |
Andrew Flynn | 2fdbe12 | 2015-06-01 16:34:21 -0400 | [diff] [blame] | 227 | mCallbackHandler.setMobileDataIndicators(statusIcon, qsIcon, typeIcon, qsTypeIcon, |
| 228 | activityIn, activityOut, dataContentDescription, description, icons.mIsWide, |
| 229 | mSubscriptionInfo.getSubscriptionId()); |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 230 | } |
| 231 | |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 232 | @Override |
| 233 | protected MobileState cleanState() { |
| 234 | return new MobileState(); |
| 235 | } |
| 236 | |
| 237 | private boolean hasService() { |
| 238 | if (mServiceState != null) { |
| 239 | // Consider the device to be in service if either voice or data |
| 240 | // service is available. Some SIM cards are marketed as data-only |
| 241 | // and do not support voice service, and on these SIM cards, we |
| 242 | // want to show signal bars for data service as well as the "no |
| 243 | // service" or "emergency calls only" text that indicates that voice |
| 244 | // is not available. |
| 245 | switch (mServiceState.getVoiceRegState()) { |
| 246 | case ServiceState.STATE_POWER_OFF: |
| 247 | return false; |
| 248 | case ServiceState.STATE_OUT_OF_SERVICE: |
| 249 | case ServiceState.STATE_EMERGENCY_ONLY: |
| 250 | return mServiceState.getDataRegState() == ServiceState.STATE_IN_SERVICE; |
| 251 | default: |
| 252 | return true; |
| 253 | } |
| 254 | } else { |
| 255 | return false; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | private boolean isCdma() { |
| 260 | return (mSignalStrength != null) && !mSignalStrength.isGsm(); |
| 261 | } |
| 262 | |
| 263 | public boolean isEmergencyOnly() { |
| 264 | return (mServiceState != null && mServiceState.isEmergencyOnly()); |
| 265 | } |
| 266 | |
| 267 | private boolean isRoaming() { |
| 268 | if (isCdma()) { |
| 269 | final int iconMode = mServiceState.getCdmaEriIconMode(); |
| 270 | return mServiceState.getCdmaEriIconIndex() != EriInfo.ROAMING_INDICATOR_OFF |
| 271 | && (iconMode == EriInfo.ROAMING_ICON_MODE_NORMAL |
| 272 | || iconMode == EriInfo.ROAMING_ICON_MODE_FLASH); |
| 273 | } else { |
| 274 | return mServiceState != null && mServiceState.getRoaming(); |
| 275 | } |
| 276 | } |
| 277 | |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 278 | private boolean isCarrierNetworkChangeActive() { |
Andrew Flynn | 55fc800 | 2015-05-29 08:33:46 -0400 | [diff] [blame] | 279 | return mCurrentState.carrierNetworkChangeMode; |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 280 | } |
| 281 | |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 282 | public void handleBroadcast(Intent intent) { |
| 283 | String action = intent.getAction(); |
| 284 | if (action.equals(TelephonyIntents.SPN_STRINGS_UPDATED_ACTION)) { |
| 285 | updateNetworkName(intent.getBooleanExtra(TelephonyIntents.EXTRA_SHOW_SPN, false), |
| 286 | intent.getStringExtra(TelephonyIntents.EXTRA_SPN), |
Pavel Zhamaitsiak | 5504d39 | 2015-03-27 09:24:47 -0700 | [diff] [blame] | 287 | intent.getStringExtra(TelephonyIntents.EXTRA_DATA_SPN), |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 288 | intent.getBooleanExtra(TelephonyIntents.EXTRA_SHOW_PLMN, false), |
| 289 | intent.getStringExtra(TelephonyIntents.EXTRA_PLMN)); |
| 290 | notifyListenersIfNecessary(); |
| 291 | } else if (action.equals(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED)) { |
| 292 | updateDataSim(); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | private void updateDataSim() { |
Jason Monk | c6cc626 | 2015-06-11 11:10:15 -0400 | [diff] [blame^] | 297 | int defaultDataSub = mDefaults.getDefaultDataSubId(); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 298 | if (SubscriptionManager.isValidSubscriptionId(defaultDataSub)) { |
| 299 | mCurrentState.dataSim = defaultDataSub == mSubscriptionInfo.getSubscriptionId(); |
| 300 | } else { |
| 301 | // There doesn't seem to be a data sim selected, however if |
| 302 | // there isn't a MobileSignalController with dataSim set, then |
| 303 | // QS won't get any callbacks and will be blank. Instead |
| 304 | // lets just assume we are the data sim (which will basically |
| 305 | // show one at random) in QS until one is selected. The user |
| 306 | // should pick one soon after, so we shouldn't be in this state |
| 307 | // for long. |
| 308 | mCurrentState.dataSim = true; |
| 309 | } |
| 310 | notifyListenersIfNecessary(); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Updates the network's name based on incoming spn and plmn. |
| 315 | */ |
Pavel Zhamaitsiak | 5504d39 | 2015-03-27 09:24:47 -0700 | [diff] [blame] | 316 | void updateNetworkName(boolean showSpn, String spn, String dataSpn, |
| 317 | boolean showPlmn, String plmn) { |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 318 | if (CHATTY) { |
Pavel Zhamaitsiak | 5504d39 | 2015-03-27 09:24:47 -0700 | [diff] [blame] | 319 | Log.d("CarrierLabel", "updateNetworkName showSpn=" + showSpn |
| 320 | + " spn=" + spn + " dataSpn=" + dataSpn |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 321 | + " showPlmn=" + showPlmn + " plmn=" + plmn); |
| 322 | } |
| 323 | StringBuilder str = new StringBuilder(); |
Pavel Zhamaitsiak | 5504d39 | 2015-03-27 09:24:47 -0700 | [diff] [blame] | 324 | StringBuilder strData = new StringBuilder(); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 325 | if (showPlmn && plmn != null) { |
| 326 | str.append(plmn); |
Pavel Zhamaitsiak | 5504d39 | 2015-03-27 09:24:47 -0700 | [diff] [blame] | 327 | strData.append(plmn); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 328 | } |
| 329 | if (showSpn && spn != null) { |
| 330 | if (str.length() != 0) { |
| 331 | str.append(mNetworkNameSeparator); |
| 332 | } |
| 333 | str.append(spn); |
| 334 | } |
| 335 | if (str.length() != 0) { |
| 336 | mCurrentState.networkName = str.toString(); |
| 337 | } else { |
| 338 | mCurrentState.networkName = mNetworkNameDefault; |
| 339 | } |
Pavel Zhamaitsiak | 5504d39 | 2015-03-27 09:24:47 -0700 | [diff] [blame] | 340 | if (showSpn && dataSpn != null) { |
| 341 | if (strData.length() != 0) { |
| 342 | strData.append(mNetworkNameSeparator); |
| 343 | } |
| 344 | strData.append(dataSpn); |
| 345 | } |
| 346 | if (strData.length() != 0) { |
| 347 | mCurrentState.networkNameData = strData.toString(); |
| 348 | } else { |
| 349 | mCurrentState.networkNameData = mNetworkNameDefault; |
| 350 | } |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Updates the current state based on mServiceState, mSignalStrength, mDataNetType, |
| 355 | * mDataState, and mSimState. It should be called any time one of these is updated. |
| 356 | * This will call listeners if necessary. |
| 357 | */ |
| 358 | private final void updateTelephony() { |
| 359 | if (DEBUG) { |
| 360 | Log.d(mTag, "updateTelephonySignalStrength: hasService=" + hasService() |
| 361 | + " ss=" + mSignalStrength); |
| 362 | } |
| 363 | mCurrentState.connected = hasService() && mSignalStrength != null; |
| 364 | if (mCurrentState.connected) { |
| 365 | if (!mSignalStrength.isGsm() && mConfig.alwaysShowCdmaRssi) { |
| 366 | mCurrentState.level = mSignalStrength.getCdmaLevel(); |
| 367 | } else { |
| 368 | mCurrentState.level = mSignalStrength.getLevel(); |
| 369 | } |
| 370 | } |
| 371 | if (mNetworkToIconLookup.indexOfKey(mDataNetType) >= 0) { |
| 372 | mCurrentState.iconGroup = mNetworkToIconLookup.get(mDataNetType); |
| 373 | } else { |
| 374 | mCurrentState.iconGroup = mDefaultIcons; |
| 375 | } |
| 376 | mCurrentState.dataConnected = mCurrentState.connected |
| 377 | && mDataState == TelephonyManager.DATA_CONNECTED; |
| 378 | |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 379 | if (isCarrierNetworkChangeActive()) { |
| 380 | mCurrentState.iconGroup = TelephonyIcons.CARRIER_NETWORK_CHANGE; |
| 381 | } else if (isRoaming()) { |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 382 | mCurrentState.iconGroup = TelephonyIcons.ROAMING; |
| 383 | } |
| 384 | if (isEmergencyOnly() != mCurrentState.isEmergency) { |
| 385 | mCurrentState.isEmergency = isEmergencyOnly(); |
| 386 | mNetworkController.recalculateEmergency(); |
| 387 | } |
| 388 | // Fill in the network name if we think we have it. |
| 389 | if (mCurrentState.networkName == mNetworkNameDefault && mServiceState != null |
| 390 | && mServiceState.getOperatorAlphaShort() != null) { |
| 391 | mCurrentState.networkName = mServiceState.getOperatorAlphaShort(); |
| 392 | } |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 393 | |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 394 | notifyListenersIfNecessary(); |
| 395 | } |
| 396 | |
| 397 | @VisibleForTesting |
| 398 | void setActivity(int activity) { |
| 399 | mCurrentState.activityIn = activity == TelephonyManager.DATA_ACTIVITY_INOUT |
| 400 | || activity == TelephonyManager.DATA_ACTIVITY_IN; |
| 401 | mCurrentState.activityOut = activity == TelephonyManager.DATA_ACTIVITY_INOUT |
| 402 | || activity == TelephonyManager.DATA_ACTIVITY_OUT; |
| 403 | notifyListenersIfNecessary(); |
| 404 | } |
| 405 | |
| 406 | @Override |
| 407 | public void dump(PrintWriter pw) { |
| 408 | super.dump(pw); |
| 409 | pw.println(" mSubscription=" + mSubscriptionInfo + ","); |
| 410 | pw.println(" mServiceState=" + mServiceState + ","); |
| 411 | pw.println(" mSignalStrength=" + mSignalStrength + ","); |
| 412 | pw.println(" mDataState=" + mDataState + ","); |
| 413 | pw.println(" mDataNetType=" + mDataNetType + ","); |
| 414 | } |
| 415 | |
| 416 | class MobilePhoneStateListener extends PhoneStateListener { |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 417 | public MobilePhoneStateListener(int subId, Looper looper) { |
| 418 | super(subId, looper); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | @Override |
| 422 | public void onSignalStrengthsChanged(SignalStrength signalStrength) { |
| 423 | if (DEBUG) { |
| 424 | Log.d(mTag, "onSignalStrengthsChanged signalStrength=" + signalStrength + |
| 425 | ((signalStrength == null) ? "" : (" level=" + signalStrength.getLevel()))); |
| 426 | } |
| 427 | mSignalStrength = signalStrength; |
| 428 | updateTelephony(); |
| 429 | } |
| 430 | |
| 431 | @Override |
| 432 | public void onServiceStateChanged(ServiceState state) { |
| 433 | if (DEBUG) { |
| 434 | Log.d(mTag, "onServiceStateChanged voiceState=" + state.getVoiceRegState() |
| 435 | + " dataState=" + state.getDataRegState()); |
| 436 | } |
| 437 | mServiceState = state; |
| 438 | updateTelephony(); |
| 439 | } |
| 440 | |
| 441 | @Override |
| 442 | public void onDataConnectionStateChanged(int state, int networkType) { |
| 443 | if (DEBUG) { |
| 444 | Log.d(mTag, "onDataConnectionStateChanged: state=" + state |
| 445 | + " type=" + networkType); |
| 446 | } |
| 447 | mDataState = state; |
| 448 | mDataNetType = networkType; |
| 449 | updateTelephony(); |
| 450 | } |
| 451 | |
| 452 | @Override |
| 453 | public void onDataActivity(int direction) { |
| 454 | if (DEBUG) { |
| 455 | Log.d(mTag, "onDataActivity: direction=" + direction); |
| 456 | } |
| 457 | setActivity(direction); |
| 458 | } |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 459 | |
| 460 | @Override |
| 461 | public void onCarrierNetworkChange(boolean active) { |
| 462 | if (DEBUG) { |
| 463 | Log.d(mTag, "onCarrierNetworkChange: active=" + active); |
| 464 | } |
| 465 | mCurrentState.carrierNetworkChangeMode = active; |
| 466 | |
| 467 | updateTelephony(); |
| 468 | } |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 469 | }; |
| 470 | |
| 471 | static class MobileIconGroup extends SignalController.IconGroup { |
| 472 | final int mDataContentDescription; // mContentDescriptionDataType |
| 473 | final int mDataType; |
| 474 | final boolean mIsWide; |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 475 | final int mQsDataType; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 476 | |
| 477 | public MobileIconGroup(String name, int[][] sbIcons, int[][] qsIcons, int[] contentDesc, |
| 478 | int sbNullState, int qsNullState, int sbDiscState, int qsDiscState, |
| 479 | int discContentDesc, int dataContentDesc, int dataType, boolean isWide, |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 480 | int qsDataType) { |
Andrew Flynn | 2fdbe12 | 2015-06-01 16:34:21 -0400 | [diff] [blame] | 481 | super(name, sbIcons, qsIcons, contentDesc, sbNullState, qsNullState, sbDiscState, |
| 482 | qsDiscState, discContentDesc); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 483 | mDataContentDescription = dataContentDesc; |
| 484 | mDataType = dataType; |
| 485 | mIsWide = isWide; |
| 486 | mQsDataType = qsDataType; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | static class MobileState extends SignalController.State { |
| 491 | String networkName; |
Pavel Zhamaitsiak | 5504d39 | 2015-03-27 09:24:47 -0700 | [diff] [blame] | 492 | String networkNameData; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 493 | boolean dataSim; |
| 494 | boolean dataConnected; |
| 495 | boolean isEmergency; |
| 496 | boolean airplaneMode; |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 497 | boolean carrierNetworkChangeMode; |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 498 | boolean isDefault; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 499 | |
| 500 | @Override |
| 501 | public void copyFrom(State s) { |
| 502 | super.copyFrom(s); |
| 503 | MobileState state = (MobileState) s; |
| 504 | dataSim = state.dataSim; |
| 505 | networkName = state.networkName; |
Pavel Zhamaitsiak | 5504d39 | 2015-03-27 09:24:47 -0700 | [diff] [blame] | 506 | networkNameData = state.networkNameData; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 507 | dataConnected = state.dataConnected; |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 508 | isDefault = state.isDefault; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 509 | isEmergency = state.isEmergency; |
| 510 | airplaneMode = state.airplaneMode; |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 511 | carrierNetworkChangeMode = state.carrierNetworkChangeMode; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | @Override |
| 515 | protected void toString(StringBuilder builder) { |
| 516 | super.toString(builder); |
| 517 | builder.append(','); |
| 518 | builder.append("dataSim=").append(dataSim).append(','); |
| 519 | builder.append("networkName=").append(networkName).append(','); |
Pavel Zhamaitsiak | 5504d39 | 2015-03-27 09:24:47 -0700 | [diff] [blame] | 520 | builder.append("networkNameData=").append(networkNameData).append(','); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 521 | builder.append("dataConnected=").append(dataConnected).append(','); |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 522 | builder.append("isDefault=").append(isDefault).append(','); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 523 | builder.append("isEmergency=").append(isEmergency).append(','); |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 524 | builder.append("airplaneMode=").append(airplaneMode).append(','); |
| 525 | builder.append("carrierNetworkChangeMode=").append(carrierNetworkChangeMode); |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | @Override |
| 529 | public boolean equals(Object o) { |
| 530 | return super.equals(o) |
| 531 | && Objects.equals(((MobileState) o).networkName, networkName) |
Pavel Zhamaitsiak | 5504d39 | 2015-03-27 09:24:47 -0700 | [diff] [blame] | 532 | && Objects.equals(((MobileState) o).networkNameData, networkNameData) |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 533 | && ((MobileState) o).dataSim == dataSim |
| 534 | && ((MobileState) o).dataConnected == dataConnected |
| 535 | && ((MobileState) o).isEmergency == isEmergency |
| 536 | && ((MobileState) o).airplaneMode == airplaneMode |
Andrew Flynn | a478d70 | 2015-04-14 23:33:45 -0400 | [diff] [blame] | 537 | && ((MobileState) o).carrierNetworkChangeMode == carrierNetworkChangeMode |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 538 | && ((MobileState) o).isDefault == isDefault; |
Jason Monk | da68f59 | 2015-01-07 10:55:58 -0500 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | } |