CarrierText.java revision bd61b2a1fd6a8369a986a8bb1d840589b752c94e
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 17package com.android.keyguard; 18 19import java.util.List; 20import java.util.Locale; 21 22import android.content.Context; 23import android.content.Intent; 24import android.content.IntentFilter; 25import android.content.res.TypedArray; 26import android.net.ConnectivityManager; 27import android.telephony.SubscriptionInfo; 28import android.telephony.SubscriptionManager; 29import android.text.TextUtils; 30import android.text.method.SingleLineTransformationMethod; 31import android.util.AttributeSet; 32import android.util.Log; 33import android.view.View; 34import android.widget.TextView; 35 36import com.android.internal.telephony.IccCardConstants; 37import com.android.internal.telephony.IccCardConstants.State; 38import com.android.internal.telephony.TelephonyIntents; 39import com.android.internal.widget.LockPatternUtils; 40 41public class CarrierText extends TextView { 42 private static final boolean DEBUG = KeyguardConstants.DEBUG; 43 private static final String TAG = "CarrierText"; 44 45 private static CharSequence mSeparator; 46 47 private final boolean mIsEmergencyCallCapable; 48 49 private KeyguardUpdateMonitor mKeyguardUpdateMonitor; 50 51 private KeyguardUpdateMonitorCallback mCallback = new KeyguardUpdateMonitorCallback() { 52 @Override 53 public void onRefreshCarrierInfo() { 54 updateCarrierText(); 55 } 56 57 public void onScreenTurnedOff(int why) { 58 setSelected(false); 59 }; 60 61 public void onScreenTurnedOn() { 62 setSelected(true); 63 }; 64 }; 65 /** 66 * The status of this lock screen. Primarily used for widgets on LockScreen. 67 */ 68 private static enum StatusMode { 69 Normal, // Normal case (sim card present, it's not locked) 70 NetworkLocked, // SIM card is 'network locked'. 71 SimMissing, // SIM card is missing. 72 SimMissingLocked, // SIM card is missing, and device isn't provisioned; don't allow access 73 SimPukLocked, // SIM card is PUK locked because SIM entered wrong too many times 74 SimLocked, // SIM card is currently locked 75 SimPermDisabled, // SIM card is permanently disabled due to PUK unlock failure 76 SimNotReady; // SIM is not ready yet. May never be on devices w/o a SIM. 77 } 78 79 public CarrierText(Context context) { 80 this(context, null); 81 } 82 83 public CarrierText(Context context, AttributeSet attrs) { 84 super(context, attrs); 85 mIsEmergencyCallCapable = context.getResources().getBoolean( 86 com.android.internal.R.bool.config_voice_capable); 87 boolean useAllCaps; 88 TypedArray a = context.getTheme().obtainStyledAttributes( 89 attrs, R.styleable.CarrierText, 0, 0); 90 try { 91 useAllCaps = a.getBoolean(R.styleable.CarrierText_allCaps, false); 92 } finally { 93 a.recycle(); 94 } 95 setTransformationMethod(new CarrierTextTransformationMethod(mContext, useAllCaps)); 96 } 97 98 protected void updateCarrierText() { 99 boolean allSimsMissing = true; 100 CharSequence displayText = null; 101 102 List<SubscriptionInfo> subs = mKeyguardUpdateMonitor.getSubscriptionInfo(false); 103 final int N = subs.size(); 104 if (DEBUG) Log.d(TAG, "updateCarrierText(): " + N); 105 for (int i = 0; i < N; i++) { 106 State simState = mKeyguardUpdateMonitor.getSimState(subs.get(i).getSubscriptionId()); 107 CharSequence carrierName = subs.get(i).getCarrierName(); 108 CharSequence carrierTextForSimState = getCarrierTextForSimState(simState, carrierName); 109 if (DEBUG) Log.d(TAG, "Handling " + simState + " " + carrierName); 110 if (carrierTextForSimState != null) { 111 allSimsMissing = false; 112 displayText = concatenate(displayText, carrierTextForSimState); 113 } 114 } 115 if (allSimsMissing) { 116 if (N != 0) { 117 // Shows "No SIM card | Emergency calls only" on devices that are voice-capable. 118 // This depends on mPlmn containing the text "Emergency calls only" when the radio 119 // has some connectivity. Otherwise, it should be null or empty and just show 120 // "No SIM card" 121 // Grab the first subscripton, because they all should contain the emergency text, 122 // described above. 123 displayText = makeCarrierStringOnEmergencyCapable( 124 getContext().getText(R.string.keyguard_missing_sim_message_short), 125 subs.get(0).getCarrierName()); 126 } else { 127 // We don't have a SubscriptionInfo to get the emergency calls only from. 128 // Grab it from the old sticky broadcast if possible instead. We can use it 129 // here because no subscriptions are active, so we don't have 130 // to worry about MSIM clashing. 131 CharSequence text = 132 getContext().getText(com.android.internal.R.string.emergency_calls_only); 133 Intent i = getContext().registerReceiver(null, 134 new IntentFilter(TelephonyIntents.SPN_STRINGS_UPDATED_ACTION)); 135 if (i != null) { 136 String spn = ""; 137 String plmn = ""; 138 if (i.getBooleanExtra(TelephonyIntents.EXTRA_SHOW_SPN, false)) { 139 spn = i.getStringExtra(TelephonyIntents.EXTRA_SPN); 140 } 141 if (i.getBooleanExtra(TelephonyIntents.EXTRA_SHOW_PLMN, false)) { 142 plmn = i.getStringExtra(TelephonyIntents.EXTRA_PLMN); 143 } 144 if (DEBUG) Log.d(TAG, "Getting plmn/spn sticky brdcst " + plmn + "/" + spn); 145 text = concatenate(plmn, spn); 146 } 147 displayText = makeCarrierStringOnEmergencyCapable( 148 getContext().getText(R.string.keyguard_missing_sim_message_short), text); 149 } 150 } 151 setText(displayText); 152 } 153 154 @Override 155 protected void onFinishInflate() { 156 super.onFinishInflate(); 157 mSeparator = getResources().getString( 158 com.android.internal.R.string.kg_text_message_separator); 159 final boolean screenOn = KeyguardUpdateMonitor.getInstance(mContext).isScreenOn(); 160 setSelected(screenOn); // Allow marquee to work. 161 } 162 163 @Override 164 protected void onAttachedToWindow() { 165 super.onAttachedToWindow(); 166 if (ConnectivityManager.from(mContext).isNetworkSupported( 167 ConnectivityManager.TYPE_MOBILE)) { 168 mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(mContext); 169 mKeyguardUpdateMonitor.registerCallback(mCallback); 170 } else { 171 // Don't listen and clear out the text when the device isn't a phone. 172 mKeyguardUpdateMonitor = null; 173 setText(""); 174 } 175 } 176 177 @Override 178 protected void onDetachedFromWindow() { 179 super.onDetachedFromWindow(); 180 if (mKeyguardUpdateMonitor != null) { 181 mKeyguardUpdateMonitor.removeCallback(mCallback); 182 } 183 } 184 185 /** 186 * Top-level function for creating carrier text. Makes text based on simState, PLMN 187 * and SPN as well as device capabilities, such as being emergency call capable. 188 * 189 * @param simState 190 * @param text 191 * @param spn 192 * @return Carrier text if not in missing state, null otherwise. 193 */ 194 private CharSequence getCarrierTextForSimState(IccCardConstants.State simState, 195 CharSequence text) { 196 CharSequence carrierText = null; 197 StatusMode status = getStatusForIccState(simState); 198 switch (status) { 199 case Normal: 200 carrierText = text; 201 break; 202 203 case SimNotReady: 204 // Null is reserved for denoting missing, in this case we have nothing to display. 205 carrierText = ""; // nothing to display yet. 206 break; 207 208 case NetworkLocked: 209 carrierText = makeCarrierStringOnEmergencyCapable( 210 mContext.getText(R.string.keyguard_network_locked_message), text); 211 break; 212 213 case SimMissing: 214 carrierText = null; 215 break; 216 217 case SimPermDisabled: 218 carrierText = getContext().getText( 219 R.string.keyguard_permanent_disabled_sim_message_short); 220 break; 221 222 case SimMissingLocked: 223 carrierText = null; 224 break; 225 226 case SimLocked: 227 carrierText = makeCarrierStringOnEmergencyCapable( 228 getContext().getText(R.string.keyguard_sim_locked_message), 229 text); 230 break; 231 232 case SimPukLocked: 233 carrierText = makeCarrierStringOnEmergencyCapable( 234 getContext().getText(R.string.keyguard_sim_puk_locked_message), 235 text); 236 break; 237 } 238 239 return carrierText; 240 } 241 242 /* 243 * Add emergencyCallMessage to carrier string only if phone supports emergency calls. 244 */ 245 private CharSequence makeCarrierStringOnEmergencyCapable( 246 CharSequence simMessage, CharSequence emergencyCallMessage) { 247 if (mIsEmergencyCallCapable) { 248 return concatenate(simMessage, emergencyCallMessage); 249 } 250 return simMessage; 251 } 252 253 /** 254 * Determine the current status of the lock screen given the SIM state and other stuff. 255 */ 256 private StatusMode getStatusForIccState(IccCardConstants.State simState) { 257 // Since reading the SIM may take a while, we assume it is present until told otherwise. 258 if (simState == null) { 259 return StatusMode.Normal; 260 } 261 262 final boolean missingAndNotProvisioned = 263 !KeyguardUpdateMonitor.getInstance(mContext).isDeviceProvisioned() 264 && (simState == IccCardConstants.State.ABSENT || 265 simState == IccCardConstants.State.PERM_DISABLED); 266 267 // Assume we're NETWORK_LOCKED if not provisioned 268 simState = missingAndNotProvisioned ? IccCardConstants.State.NETWORK_LOCKED : simState; 269 switch (simState) { 270 case ABSENT: 271 return StatusMode.SimMissing; 272 case NETWORK_LOCKED: 273 return StatusMode.SimMissingLocked; 274 case NOT_READY: 275 return StatusMode.SimNotReady; 276 case PIN_REQUIRED: 277 return StatusMode.SimLocked; 278 case PUK_REQUIRED: 279 return StatusMode.SimPukLocked; 280 case READY: 281 return StatusMode.Normal; 282 case PERM_DISABLED: 283 return StatusMode.SimPermDisabled; 284 case UNKNOWN: 285 return StatusMode.SimMissing; 286 } 287 return StatusMode.SimMissing; 288 } 289 290 private static CharSequence concatenate(CharSequence plmn, CharSequence spn) { 291 final boolean plmnValid = !TextUtils.isEmpty(plmn); 292 final boolean spnValid = !TextUtils.isEmpty(spn); 293 if (plmnValid && spnValid) { 294 if (plmn.equals(spn)) { 295 return plmn; 296 } else { 297 return new StringBuilder().append(plmn).append(mSeparator).append(spn).toString(); 298 } 299 } else if (plmnValid) { 300 return plmn; 301 } else if (spnValid) { 302 return spn; 303 } else { 304 return ""; 305 } 306 } 307 308 private CharSequence getCarrierHelpTextForSimState(IccCardConstants.State simState, 309 String plmn, String spn) { 310 int carrierHelpTextId = 0; 311 StatusMode status = getStatusForIccState(simState); 312 switch (status) { 313 case NetworkLocked: 314 carrierHelpTextId = R.string.keyguard_instructions_when_pattern_disabled; 315 break; 316 317 case SimMissing: 318 carrierHelpTextId = R.string.keyguard_missing_sim_instructions_long; 319 break; 320 321 case SimPermDisabled: 322 carrierHelpTextId = R.string.keyguard_permanent_disabled_sim_instructions; 323 break; 324 325 case SimMissingLocked: 326 carrierHelpTextId = R.string.keyguard_missing_sim_instructions; 327 break; 328 329 case Normal: 330 case SimLocked: 331 case SimPukLocked: 332 break; 333 } 334 335 return mContext.getText(carrierHelpTextId); 336 } 337 338 private class CarrierTextTransformationMethod extends SingleLineTransformationMethod { 339 private final Locale mLocale; 340 private final boolean mAllCaps; 341 342 public CarrierTextTransformationMethod(Context context, boolean allCaps) { 343 mLocale = context.getResources().getConfiguration().locale; 344 mAllCaps = allCaps; 345 } 346 347 @Override 348 public CharSequence getTransformation(CharSequence source, View view) { 349 source = super.getTransformation(source, view); 350 351 if (mAllCaps && source != null) { 352 source = source.toString().toUpperCase(mLocale); 353 } 354 355 return source; 356 } 357 } 358} 359