Jim Miller | 109f1fd | 2012-09-19 20:44:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 17 | package com.android.keyguard; |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 18 | |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 19 | import java.util.List; |
| 20 | import java.util.Locale; |
Sanket Padawe | b8726b7 | 2015-04-01 14:02:16 -0700 | [diff] [blame] | 21 | import java.util.Objects; |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 22 | |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Jason Monk | de3260b | 2015-02-24 08:55:26 -0500 | [diff] [blame] | 24 | import android.content.Intent; |
| 25 | import android.content.IntentFilter; |
Jorim Jaggi | 2580793 | 2014-04-24 23:29:46 +0200 | [diff] [blame] | 26 | import android.content.res.TypedArray; |
Jason Monk | 83bc2aaf | 2014-12-05 10:16:24 -0500 | [diff] [blame] | 27 | import android.net.ConnectivityManager; |
Etan Cohen | 47051d8 | 2015-07-06 16:19:04 -0700 | [diff] [blame] | 28 | import android.net.wifi.WifiManager; |
| 29 | import android.telephony.ServiceState; |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 30 | import android.telephony.SubscriptionInfo; |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 31 | import android.text.TextUtils; |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 32 | import android.text.method.SingleLineTransformationMethod; |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 33 | import android.util.AttributeSet; |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 34 | import android.util.Log; |
Victoria Lease | 1d80e97 | 2013-11-01 16:34:03 -0700 | [diff] [blame] | 35 | import android.view.View; |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 36 | import android.widget.TextView; |
| 37 | |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 38 | import com.android.internal.telephony.IccCardConstants; |
| 39 | import com.android.internal.telephony.IccCardConstants.State; |
Jason Monk | de3260b | 2015-02-24 08:55:26 -0500 | [diff] [blame] | 40 | import com.android.internal.telephony.TelephonyIntents; |
Jason Monk | 726fb28 | 2015-03-31 12:56:11 -0400 | [diff] [blame] | 41 | import com.android.settingslib.WirelessUtils; |
Amin Shaikh | 0e00331 | 2018-03-08 11:39:01 -0500 | [diff] [blame] | 42 | |
Wileen Chiu | 6b8b22e | 2014-10-29 22:48:21 +0530 | [diff] [blame] | 43 | import android.telephony.TelephonyManager; |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 44 | |
| 45 | public class CarrierText extends TextView { |
Amin Shaikh | 0e00331 | 2018-03-08 11:39:01 -0500 | [diff] [blame] | 46 | /** Do not show missing sim message. */ |
| 47 | public static final int FLAG_HIDE_MISSING_SIM = 1 << 0; |
| 48 | /** Do not show airplane mode message. */ |
| 49 | public static final int FLAG_HIDE_AIRPLANE_MODE = 1 << 1; |
| 50 | |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 51 | private static final boolean DEBUG = KeyguardConstants.DEBUG; |
| 52 | private static final String TAG = "CarrierText"; |
| 53 | |
Jim Miller | e9be140 | 2012-11-01 16:14:20 -0700 | [diff] [blame] | 54 | private static CharSequence mSeparator; |
| 55 | |
Adrian Roos | c2e0168 | 2015-01-15 23:20:20 +0100 | [diff] [blame] | 56 | private final boolean mIsEmergencyCallCapable; |
| 57 | |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 58 | private KeyguardUpdateMonitor mKeyguardUpdateMonitor; |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 59 | |
Etan Cohen | 47051d8 | 2015-07-06 16:19:04 -0700 | [diff] [blame] | 60 | private WifiManager mWifiManager; |
| 61 | |
Wileen Chiu | 6b8b22e | 2014-10-29 22:48:21 +0530 | [diff] [blame] | 62 | private boolean[] mSimErrorState = new boolean[TelephonyManager.getDefault().getPhoneCount()]; |
| 63 | |
Amin Shaikh | 0e00331 | 2018-03-08 11:39:01 -0500 | [diff] [blame] | 64 | private int mFlags; |
| 65 | |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 66 | private KeyguardUpdateMonitorCallback mCallback = new KeyguardUpdateMonitorCallback() { |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 67 | @Override |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 68 | public void onRefreshCarrierInfo() { |
| 69 | updateCarrierText(); |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 70 | } |
Jim Miller | 20daffd | 2013-10-07 14:59:53 -0700 | [diff] [blame] | 71 | |
Jorim Jaggi | 0d210f6 | 2015-07-10 14:24:44 -0700 | [diff] [blame] | 72 | public void onFinishedGoingToSleep(int why) { |
Jim Miller | 20daffd | 2013-10-07 14:59:53 -0700 | [diff] [blame] | 73 | setSelected(false); |
| 74 | }; |
| 75 | |
Jorim Jaggi | 0d210f6 | 2015-07-10 14:24:44 -0700 | [diff] [blame] | 76 | public void onStartedWakingUp() { |
Jim Miller | 20daffd | 2013-10-07 14:59:53 -0700 | [diff] [blame] | 77 | setSelected(true); |
| 78 | }; |
Wileen Chiu | 6b8b22e | 2014-10-29 22:48:21 +0530 | [diff] [blame] | 79 | |
| 80 | public void onSimStateChanged(int subId, int slotId, IccCardConstants.State simState) { |
| 81 | if (slotId < 0) { |
| 82 | Log.d(TAG, "onSimStateChanged() - slotId invalid: " + slotId); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | if (DEBUG) Log.d(TAG,"onSimStateChanged: " + getStatusForIccState(simState)); |
| 87 | if (getStatusForIccState(simState) == StatusMode.SimIoError) { |
| 88 | mSimErrorState[slotId] = true; |
| 89 | updateCarrierText(); |
| 90 | } else if (mSimErrorState[slotId]) { |
| 91 | mSimErrorState[slotId] = false; |
| 92 | updateCarrierText(); |
| 93 | } |
| 94 | }; |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 95 | }; |
Amin Shaikh | 0e00331 | 2018-03-08 11:39:01 -0500 | [diff] [blame] | 96 | |
| 97 | public void setDisplayFlags(int flags) { |
| 98 | mFlags = flags; |
| 99 | } |
| 100 | |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 101 | /** |
| 102 | * The status of this lock screen. Primarily used for widgets on LockScreen. |
| 103 | */ |
Jim Miller | 109f1fd | 2012-09-19 20:44:16 -0700 | [diff] [blame] | 104 | private static enum StatusMode { |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 105 | Normal, // Normal case (sim card present, it's not locked) |
| 106 | NetworkLocked, // SIM card is 'network locked'. |
| 107 | SimMissing, // SIM card is missing. |
| 108 | SimMissingLocked, // SIM card is missing, and device isn't provisioned; don't allow access |
| 109 | SimPukLocked, // SIM card is PUK locked because SIM entered wrong too many times |
| 110 | SimLocked, // SIM card is currently locked |
Jim Miller | 109f1fd | 2012-09-19 20:44:16 -0700 | [diff] [blame] | 111 | SimPermDisabled, // SIM card is permanently disabled due to PUK unlock failure |
Wileen Chiu | 6b8b22e | 2014-10-29 22:48:21 +0530 | [diff] [blame] | 112 | SimNotReady, // SIM is not ready yet. May never be on devices w/o a SIM. |
| 113 | SimIoError; // SIM card is faulty |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | public CarrierText(Context context) { |
| 117 | this(context, null); |
| 118 | } |
| 119 | |
| 120 | public CarrierText(Context context, AttributeSet attrs) { |
| 121 | super(context, attrs); |
Adrian Roos | c2e0168 | 2015-01-15 23:20:20 +0100 | [diff] [blame] | 122 | mIsEmergencyCallCapable = context.getResources().getBoolean( |
| 123 | com.android.internal.R.bool.config_voice_capable); |
Jorim Jaggi | 2580793 | 2014-04-24 23:29:46 +0200 | [diff] [blame] | 124 | boolean useAllCaps; |
| 125 | TypedArray a = context.getTheme().obtainStyledAttributes( |
| 126 | attrs, R.styleable.CarrierText, 0, 0); |
| 127 | try { |
| 128 | useAllCaps = a.getBoolean(R.styleable.CarrierText_allCaps, false); |
| 129 | } finally { |
| 130 | a.recycle(); |
| 131 | } |
Victoria Lease | 1d80e97 | 2013-11-01 16:34:03 -0700 | [diff] [blame] | 132 | setTransformationMethod(new CarrierTextTransformationMethod(mContext, useAllCaps)); |
Etan Cohen | 47051d8 | 2015-07-06 16:19:04 -0700 | [diff] [blame] | 133 | |
| 134 | mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Wileen Chiu | 6b8b22e | 2014-10-29 22:48:21 +0530 | [diff] [blame] | 137 | /** |
| 138 | * Checks if there are faulty cards. Adds the text depending on the slot of the card |
| 139 | * @param text: current carrier text based on the sim state |
| 140 | * @param noSims: whether a valid sim card is inserted |
| 141 | * @return text |
| 142 | */ |
| 143 | private CharSequence updateCarrierTextWithSimIoError(CharSequence text, boolean noSims) { |
| 144 | final CharSequence carrier = ""; |
| 145 | CharSequence carrierTextForSimIOError = getCarrierTextForSimState( |
| 146 | IccCardConstants.State.CARD_IO_ERROR, carrier); |
| 147 | for (int index = 0; index < mSimErrorState.length; index++) { |
| 148 | if (mSimErrorState[index]) { |
| 149 | // In the case when no sim cards are detected but a faulty card is inserted |
| 150 | // overwrite the text and only show "Invalid card" |
| 151 | if (noSims) { |
| 152 | return concatenate(carrierTextForSimIOError, |
| 153 | getContext().getText(com.android.internal.R.string.emergency_calls_only)); |
| 154 | } else if (index == 0) { |
| 155 | // prepend "Invalid card" when faulty card is inserted in slot 0 |
| 156 | text = concatenate(carrierTextForSimIOError, text); |
| 157 | } else { |
| 158 | // concatenate "Invalid card" when faulty card is inserted in slot 1 |
| 159 | text = concatenate(text, carrierTextForSimIOError); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | return text; |
| 164 | } |
| 165 | |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 166 | protected void updateCarrierText() { |
| 167 | boolean allSimsMissing = true; |
Etan Cohen | 47051d8 | 2015-07-06 16:19:04 -0700 | [diff] [blame] | 168 | boolean anySimReadyAndInService = false; |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 169 | CharSequence displayText = null; |
| 170 | |
| 171 | List<SubscriptionInfo> subs = mKeyguardUpdateMonitor.getSubscriptionInfo(false); |
| 172 | final int N = subs.size(); |
| 173 | if (DEBUG) Log.d(TAG, "updateCarrierText(): " + N); |
| 174 | for (int i = 0; i < N; i++) { |
Etan Cohen | 47051d8 | 2015-07-06 16:19:04 -0700 | [diff] [blame] | 175 | int subId = subs.get(i).getSubscriptionId(); |
| 176 | State simState = mKeyguardUpdateMonitor.getSimState(subId); |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 177 | CharSequence carrierName = subs.get(i).getCarrierName(); |
| 178 | CharSequence carrierTextForSimState = getCarrierTextForSimState(simState, carrierName); |
Etan Cohen | 47051d8 | 2015-07-06 16:19:04 -0700 | [diff] [blame] | 179 | if (DEBUG) { |
| 180 | Log.d(TAG, "Handling (subId=" + subId + "): " + simState + " " + carrierName); |
| 181 | } |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 182 | if (carrierTextForSimState != null) { |
| 183 | allSimsMissing = false; |
| 184 | displayText = concatenate(displayText, carrierTextForSimState); |
| 185 | } |
Etan Cohen | 47051d8 | 2015-07-06 16:19:04 -0700 | [diff] [blame] | 186 | if (simState == IccCardConstants.State.READY) { |
| 187 | ServiceState ss = mKeyguardUpdateMonitor.mServiceStates.get(subId); |
| 188 | if (ss != null && ss.getDataRegState() == ServiceState.STATE_IN_SERVICE) { |
| 189 | // hack for WFC (IWLAN) not turning off immediately once |
| 190 | // Wi-Fi is disassociated or disabled |
| 191 | if (ss.getRilDataRadioTechnology() != ServiceState.RIL_RADIO_TECHNOLOGY_IWLAN |
| 192 | || (mWifiManager.isWifiEnabled() |
| 193 | && mWifiManager.getConnectionInfo() != null |
| 194 | && mWifiManager.getConnectionInfo().getBSSID() != null)) { |
| 195 | if (DEBUG) { |
| 196 | Log.d(TAG, "SIM ready and in service: subId=" + subId + ", ss=" + ss); |
| 197 | } |
| 198 | anySimReadyAndInService = true; |
| 199 | } |
| 200 | } |
| 201 | } |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 202 | } |
| 203 | if (allSimsMissing) { |
| 204 | if (N != 0) { |
| 205 | // Shows "No SIM card | Emergency calls only" on devices that are voice-capable. |
| 206 | // This depends on mPlmn containing the text "Emergency calls only" when the radio |
| 207 | // has some connectivity. Otherwise, it should be null or empty and just show |
| 208 | // "No SIM card" |
| 209 | // Grab the first subscripton, because they all should contain the emergency text, |
| 210 | // described above. |
| 211 | displayText = makeCarrierStringOnEmergencyCapable( |
Amin Shaikh | 0e00331 | 2018-03-08 11:39:01 -0500 | [diff] [blame] | 212 | getMissingSimMessage(), subs.get(0).getCarrierName()); |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 213 | } else { |
| 214 | // We don't have a SubscriptionInfo to get the emergency calls only from. |
Jason Monk | de3260b | 2015-02-24 08:55:26 -0500 | [diff] [blame] | 215 | // Grab it from the old sticky broadcast if possible instead. We can use it |
| 216 | // here because no subscriptions are active, so we don't have |
| 217 | // to worry about MSIM clashing. |
| 218 | CharSequence text = |
| 219 | getContext().getText(com.android.internal.R.string.emergency_calls_only); |
| 220 | Intent i = getContext().registerReceiver(null, |
| 221 | new IntentFilter(TelephonyIntents.SPN_STRINGS_UPDATED_ACTION)); |
| 222 | if (i != null) { |
| 223 | String spn = ""; |
| 224 | String plmn = ""; |
| 225 | if (i.getBooleanExtra(TelephonyIntents.EXTRA_SHOW_SPN, false)) { |
| 226 | spn = i.getStringExtra(TelephonyIntents.EXTRA_SPN); |
| 227 | } |
| 228 | if (i.getBooleanExtra(TelephonyIntents.EXTRA_SHOW_PLMN, false)) { |
| 229 | plmn = i.getStringExtra(TelephonyIntents.EXTRA_PLMN); |
| 230 | } |
| 231 | if (DEBUG) Log.d(TAG, "Getting plmn/spn sticky brdcst " + plmn + "/" + spn); |
Sanket Padawe | b8726b7 | 2015-04-01 14:02:16 -0700 | [diff] [blame] | 232 | if (Objects.equals(plmn, spn)) { |
| 233 | text = plmn; |
| 234 | } else { |
| 235 | text = concatenate(plmn, spn); |
| 236 | } |
Jason Monk | de3260b | 2015-02-24 08:55:26 -0500 | [diff] [blame] | 237 | } |
Amin Shaikh | 0e00331 | 2018-03-08 11:39:01 -0500 | [diff] [blame] | 238 | displayText = makeCarrierStringOnEmergencyCapable(getMissingSimMessage(), text); |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 239 | } |
| 240 | } |
Etan Cohen | 47051d8 | 2015-07-06 16:19:04 -0700 | [diff] [blame] | 241 | |
Wileen Chiu | 6b8b22e | 2014-10-29 22:48:21 +0530 | [diff] [blame] | 242 | displayText = updateCarrierTextWithSimIoError(displayText, allSimsMissing); |
Etan Cohen | 47051d8 | 2015-07-06 16:19:04 -0700 | [diff] [blame] | 243 | // APM (airplane mode) != no carrier state. There are carrier services |
| 244 | // (e.g. WFC = Wi-Fi calling) which may operate in APM. |
| 245 | if (!anySimReadyAndInService && WirelessUtils.isAirplaneModeOn(mContext)) { |
Amin Shaikh | 0e00331 | 2018-03-08 11:39:01 -0500 | [diff] [blame] | 246 | displayText = getAirplaneModeMessage(); |
Jason Monk | 726fb28 | 2015-03-31 12:56:11 -0400 | [diff] [blame] | 247 | } |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 248 | setText(displayText); |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Amin Shaikh | 0e00331 | 2018-03-08 11:39:01 -0500 | [diff] [blame] | 251 | private String getMissingSimMessage() { |
| 252 | return (mFlags & FLAG_HIDE_MISSING_SIM) == 0 |
| 253 | ? getContext().getString(R.string.keyguard_missing_sim_message_short) : ""; |
| 254 | } |
| 255 | |
| 256 | private String getAirplaneModeMessage() { |
| 257 | return (mFlags & FLAG_HIDE_AIRPLANE_MODE) == 0 |
| 258 | ? getContext().getString(R.string.airplane_mode) : ""; |
| 259 | } |
| 260 | |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 261 | @Override |
| 262 | protected void onFinishInflate() { |
| 263 | super.onFinishInflate(); |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 264 | mSeparator = getResources().getString( |
| 265 | com.android.internal.R.string.kg_text_message_separator); |
Jorim Jaggi | 0d210f6 | 2015-07-10 14:24:44 -0700 | [diff] [blame] | 266 | boolean shouldMarquee = KeyguardUpdateMonitor.getInstance(mContext).isDeviceInteractive(); |
| 267 | setSelected(shouldMarquee); // Allow marquee to work. |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Jim Miller | 0928e01 | 2012-11-06 18:43:22 -0800 | [diff] [blame] | 270 | @Override |
| 271 | protected void onAttachedToWindow() { |
| 272 | super.onAttachedToWindow(); |
Jason Monk | 83bc2aaf | 2014-12-05 10:16:24 -0500 | [diff] [blame] | 273 | if (ConnectivityManager.from(mContext).isNetworkSupported( |
| 274 | ConnectivityManager.TYPE_MOBILE)) { |
| 275 | mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(mContext); |
| 276 | mKeyguardUpdateMonitor.registerCallback(mCallback); |
| 277 | } else { |
| 278 | // Don't listen and clear out the text when the device isn't a phone. |
| 279 | mKeyguardUpdateMonitor = null; |
| 280 | setText(""); |
| 281 | } |
Jim Miller | 0928e01 | 2012-11-06 18:43:22 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | @Override |
| 285 | protected void onDetachedFromWindow() { |
| 286 | super.onDetachedFromWindow(); |
Jason Monk | 83bc2aaf | 2014-12-05 10:16:24 -0500 | [diff] [blame] | 287 | if (mKeyguardUpdateMonitor != null) { |
| 288 | mKeyguardUpdateMonitor.removeCallback(mCallback); |
| 289 | } |
Jim Miller | 0928e01 | 2012-11-06 18:43:22 -0800 | [diff] [blame] | 290 | } |
| 291 | |
Matthew Ng | 3a6db67 | 2017-11-28 14:35:25 -0800 | [diff] [blame] | 292 | @Override |
| 293 | protected void onVisibilityChanged(View changedView, int visibility) { |
| 294 | super.onVisibilityChanged(changedView, visibility); |
| 295 | |
| 296 | // Only show marquee when visible |
| 297 | if (visibility == VISIBLE) { |
| 298 | setEllipsize(TextUtils.TruncateAt.MARQUEE); |
| 299 | } else { |
| 300 | setEllipsize(TextUtils.TruncateAt.END); |
| 301 | } |
| 302 | } |
| 303 | |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 304 | /** |
| 305 | * Top-level function for creating carrier text. Makes text based on simState, PLMN |
| 306 | * and SPN as well as device capabilities, such as being emergency call capable. |
| 307 | * |
| 308 | * @param simState |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 309 | * @param text |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 310 | * @param spn |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 311 | * @return Carrier text if not in missing state, null otherwise. |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 312 | */ |
| 313 | private CharSequence getCarrierTextForSimState(IccCardConstants.State simState, |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 314 | CharSequence text) { |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 315 | CharSequence carrierText = null; |
| 316 | StatusMode status = getStatusForIccState(simState); |
| 317 | switch (status) { |
| 318 | case Normal: |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 319 | carrierText = text; |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 320 | break; |
| 321 | |
Jim Miller | 109f1fd | 2012-09-19 20:44:16 -0700 | [diff] [blame] | 322 | case SimNotReady: |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 323 | // Null is reserved for denoting missing, in this case we have nothing to display. |
| 324 | carrierText = ""; // nothing to display yet. |
Jim Miller | 109f1fd | 2012-09-19 20:44:16 -0700 | [diff] [blame] | 325 | break; |
| 326 | |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 327 | case NetworkLocked: |
| 328 | carrierText = makeCarrierStringOnEmergencyCapable( |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 329 | mContext.getText(R.string.keyguard_network_locked_message), text); |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 330 | break; |
| 331 | |
| 332 | case SimMissing: |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 333 | carrierText = null; |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 334 | break; |
| 335 | |
| 336 | case SimPermDisabled: |
Dan Sandler | f058cc6 | 2018-06-08 15:34:52 -0400 | [diff] [blame] | 337 | carrierText = makeCarrierStringOnEmergencyCapable( |
| 338 | getContext().getText( |
| 339 | R.string.keyguard_permanent_disabled_sim_message_short), |
| 340 | text); |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 341 | break; |
| 342 | |
| 343 | case SimMissingLocked: |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 344 | carrierText = null; |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 345 | break; |
| 346 | |
| 347 | case SimLocked: |
| 348 | carrierText = makeCarrierStringOnEmergencyCapable( |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 349 | getContext().getText(R.string.keyguard_sim_locked_message), |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 350 | text); |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 351 | break; |
| 352 | |
| 353 | case SimPukLocked: |
| 354 | carrierText = makeCarrierStringOnEmergencyCapable( |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 355 | getContext().getText(R.string.keyguard_sim_puk_locked_message), |
Jason Monk | 9ff69bd | 2014-12-02 16:43:17 -0500 | [diff] [blame] | 356 | text); |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 357 | break; |
Wileen Chiu | 6b8b22e | 2014-10-29 22:48:21 +0530 | [diff] [blame] | 358 | case SimIoError: |
| 359 | carrierText = makeCarrierStringOnEmergencyCapable( |
| 360 | getContext().getText(R.string.keyguard_sim_error_message_short), |
| 361 | text); |
| 362 | break; |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | return carrierText; |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | * Add emergencyCallMessage to carrier string only if phone supports emergency calls. |
| 370 | */ |
| 371 | private CharSequence makeCarrierStringOnEmergencyCapable( |
| 372 | CharSequence simMessage, CharSequence emergencyCallMessage) { |
Adrian Roos | c2e0168 | 2015-01-15 23:20:20 +0100 | [diff] [blame] | 373 | if (mIsEmergencyCallCapable) { |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 374 | return concatenate(simMessage, emergencyCallMessage); |
| 375 | } |
| 376 | return simMessage; |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Determine the current status of the lock screen given the SIM state and other stuff. |
| 381 | */ |
| 382 | private StatusMode getStatusForIccState(IccCardConstants.State simState) { |
| 383 | // Since reading the SIM may take a while, we assume it is present until told otherwise. |
| 384 | if (simState == null) { |
| 385 | return StatusMode.Normal; |
| 386 | } |
| 387 | |
| 388 | final boolean missingAndNotProvisioned = |
| 389 | !KeyguardUpdateMonitor.getInstance(mContext).isDeviceProvisioned() |
| 390 | && (simState == IccCardConstants.State.ABSENT || |
| 391 | simState == IccCardConstants.State.PERM_DISABLED); |
| 392 | |
| 393 | // Assume we're NETWORK_LOCKED if not provisioned |
| 394 | simState = missingAndNotProvisioned ? IccCardConstants.State.NETWORK_LOCKED : simState; |
| 395 | switch (simState) { |
| 396 | case ABSENT: |
| 397 | return StatusMode.SimMissing; |
| 398 | case NETWORK_LOCKED: |
| 399 | return StatusMode.SimMissingLocked; |
| 400 | case NOT_READY: |
Jim Miller | 109f1fd | 2012-09-19 20:44:16 -0700 | [diff] [blame] | 401 | return StatusMode.SimNotReady; |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 402 | case PIN_REQUIRED: |
| 403 | return StatusMode.SimLocked; |
| 404 | case PUK_REQUIRED: |
| 405 | return StatusMode.SimPukLocked; |
| 406 | case READY: |
| 407 | return StatusMode.Normal; |
| 408 | case PERM_DISABLED: |
| 409 | return StatusMode.SimPermDisabled; |
| 410 | case UNKNOWN: |
| 411 | return StatusMode.SimMissing; |
Wileen Chiu | 6b8b22e | 2014-10-29 22:48:21 +0530 | [diff] [blame] | 412 | case CARD_IO_ERROR: |
| 413 | return StatusMode.SimIoError; |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 414 | } |
| 415 | return StatusMode.SimMissing; |
| 416 | } |
| 417 | |
| 418 | private static CharSequence concatenate(CharSequence plmn, CharSequence spn) { |
| 419 | final boolean plmnValid = !TextUtils.isEmpty(plmn); |
| 420 | final boolean spnValid = !TextUtils.isEmpty(spn); |
| 421 | if (plmnValid && spnValid) { |
Sanket Padawe | b8726b7 | 2015-04-01 14:02:16 -0700 | [diff] [blame] | 422 | return new StringBuilder().append(plmn).append(mSeparator).append(spn).toString(); |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 423 | } else if (plmnValid) { |
| 424 | return plmn; |
| 425 | } else if (spnValid) { |
| 426 | return spn; |
| 427 | } else { |
| 428 | return ""; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | private CharSequence getCarrierHelpTextForSimState(IccCardConstants.State simState, |
| 433 | String plmn, String spn) { |
| 434 | int carrierHelpTextId = 0; |
| 435 | StatusMode status = getStatusForIccState(simState); |
| 436 | switch (status) { |
| 437 | case NetworkLocked: |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 438 | carrierHelpTextId = R.string.keyguard_instructions_when_pattern_disabled; |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 439 | break; |
| 440 | |
| 441 | case SimMissing: |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 442 | carrierHelpTextId = R.string.keyguard_missing_sim_instructions_long; |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 443 | break; |
| 444 | |
| 445 | case SimPermDisabled: |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 446 | carrierHelpTextId = R.string.keyguard_permanent_disabled_sim_instructions; |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 447 | break; |
| 448 | |
| 449 | case SimMissingLocked: |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 450 | carrierHelpTextId = R.string.keyguard_missing_sim_instructions; |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 451 | break; |
| 452 | |
| 453 | case Normal: |
| 454 | case SimLocked: |
| 455 | case SimPukLocked: |
| 456 | break; |
| 457 | } |
| 458 | |
| 459 | return mContext.getText(carrierHelpTextId); |
| 460 | } |
Victoria Lease | 1d80e97 | 2013-11-01 16:34:03 -0700 | [diff] [blame] | 461 | |
| 462 | private class CarrierTextTransformationMethod extends SingleLineTransformationMethod { |
| 463 | private final Locale mLocale; |
| 464 | private final boolean mAllCaps; |
| 465 | |
| 466 | public CarrierTextTransformationMethod(Context context, boolean allCaps) { |
| 467 | mLocale = context.getResources().getConfiguration().locale; |
| 468 | mAllCaps = allCaps; |
| 469 | } |
| 470 | |
| 471 | @Override |
| 472 | public CharSequence getTransformation(CharSequence source, View view) { |
| 473 | source = super.getTransformation(source, view); |
| 474 | |
| 475 | if (mAllCaps && source != null) { |
| 476 | source = source.toString().toUpperCase(mLocale); |
| 477 | } |
| 478 | |
| 479 | return source; |
| 480 | } |
| 481 | } |
Jim Miller | f9cb7ba | 2012-09-17 22:31:56 -0700 | [diff] [blame] | 482 | } |