|
| 1 | +package com.android.internal.policy.impl.keyguard; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.text.TextUtils; |
| 5 | +import android.util.AttributeSet; |
| 6 | +import android.widget.TextView; |
| 7 | + |
| 8 | +import com.android.internal.R; |
| 9 | +import com.android.internal.telephony.IccCardConstants; |
| 10 | +import com.android.internal.telephony.IccCardConstants.State; |
| 11 | +import com.android.internal.widget.LockPatternUtils; |
| 12 | + |
| 13 | +public class CarrierText extends TextView { |
| 14 | + private LockPatternUtils mLockPatternUtils; |
| 15 | + |
| 16 | + private KeyguardUpdateMonitorCallback mCallback = new KeyguardUpdateMonitorCallback() { |
| 17 | + private CharSequence mPlmn; |
| 18 | + private CharSequence mSpn; |
| 19 | + private State mSimState; |
| 20 | + |
| 21 | + @Override |
| 22 | + public void onRefreshCarrierInfo(CharSequence plmn, CharSequence spn) { |
| 23 | + mPlmn = plmn; |
| 24 | + mSpn = spn; |
| 25 | + updateCarrierText(mSimState, mPlmn, mSpn); |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public void onSimStateChanged(IccCardConstants.State simState) { |
| 30 | + mSimState = simState; |
| 31 | + updateCarrierText(mSimState, mPlmn, mSpn); |
| 32 | + } |
| 33 | + }; |
| 34 | + /** |
| 35 | + * The status of this lock screen. Primarily used for widgets on LockScreen. |
| 36 | + */ |
| 37 | + enum StatusMode { |
| 38 | + Normal, // Normal case (sim card present, it's not locked) |
| 39 | + NetworkLocked, // SIM card is 'network locked'. |
| 40 | + SimMissing, // SIM card is missing. |
| 41 | + SimMissingLocked, // SIM card is missing, and device isn't provisioned; don't allow access |
| 42 | + SimPukLocked, // SIM card is PUK locked because SIM entered wrong too many times |
| 43 | + SimLocked, // SIM card is currently locked |
| 44 | + SimPermDisabled; // SIM card is permanently disabled due to PUK unlock failure |
| 45 | + } |
| 46 | + |
| 47 | + public CarrierText(Context context) { |
| 48 | + this(context, null); |
| 49 | + } |
| 50 | + |
| 51 | + public CarrierText(Context context, AttributeSet attrs) { |
| 52 | + super(context, attrs); |
| 53 | + mLockPatternUtils = new LockPatternUtils(mContext); |
| 54 | + } |
| 55 | + |
| 56 | + protected void updateCarrierText(State simState, CharSequence plmn, CharSequence spn) { |
| 57 | + setText(getCarrierTextForSimState(simState, plmn, spn)); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + protected void onFinishInflate() { |
| 62 | + super.onFinishInflate(); |
| 63 | + KeyguardUpdateMonitor.getInstance(mContext).registerCallback(mCallback); |
| 64 | + setSelected(true); // Allow marquee to work. |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Top-level function for creating carrier text. Makes text based on simState, PLMN |
| 69 | + * and SPN as well as device capabilities, such as being emergency call capable. |
| 70 | + * |
| 71 | + * @param simState |
| 72 | + * @param plmn |
| 73 | + * @param spn |
| 74 | + * @return |
| 75 | + */ |
| 76 | + private CharSequence getCarrierTextForSimState(IccCardConstants.State simState, |
| 77 | + CharSequence plmn, CharSequence spn) { |
| 78 | + CharSequence carrierText = null; |
| 79 | + StatusMode status = getStatusForIccState(simState); |
| 80 | + switch (status) { |
| 81 | + case Normal: |
| 82 | + carrierText = concatenate(plmn, spn); |
| 83 | + break; |
| 84 | + |
| 85 | + case NetworkLocked: |
| 86 | + carrierText = makeCarrierStringOnEmergencyCapable( |
| 87 | + mContext.getText(R.string.lockscreen_network_locked_message), plmn); |
| 88 | + break; |
| 89 | + |
| 90 | + case SimMissing: |
| 91 | + // Shows "No SIM card | Emergency calls only" on devices that are voice-capable. |
| 92 | + // This depends on mPlmn containing the text "Emergency calls only" when the radio |
| 93 | + // has some connectivity. Otherwise, it should be null or empty and just show |
| 94 | + // "No SIM card" |
| 95 | + carrierText = makeCarrierStringOnEmergencyCapable( |
| 96 | + getContext().getText(R.string.lockscreen_missing_sim_message_short), |
| 97 | + plmn); |
| 98 | + break; |
| 99 | + |
| 100 | + case SimPermDisabled: |
| 101 | + carrierText = getContext().getText( |
| 102 | + R.string.lockscreen_permanent_disabled_sim_message_short); |
| 103 | + break; |
| 104 | + |
| 105 | + case SimMissingLocked: |
| 106 | + carrierText = makeCarrierStringOnEmergencyCapable( |
| 107 | + getContext().getText(R.string.lockscreen_missing_sim_message_short), |
| 108 | + plmn); |
| 109 | + break; |
| 110 | + |
| 111 | + case SimLocked: |
| 112 | + carrierText = makeCarrierStringOnEmergencyCapable( |
| 113 | + getContext().getText(R.string.lockscreen_sim_locked_message), |
| 114 | + plmn); |
| 115 | + break; |
| 116 | + |
| 117 | + case SimPukLocked: |
| 118 | + carrierText = makeCarrierStringOnEmergencyCapable( |
| 119 | + getContext().getText(R.string.lockscreen_sim_puk_locked_message), |
| 120 | + plmn); |
| 121 | + break; |
| 122 | + } |
| 123 | + |
| 124 | + return carrierText; |
| 125 | + } |
| 126 | + |
| 127 | + /* |
| 128 | + * Add emergencyCallMessage to carrier string only if phone supports emergency calls. |
| 129 | + */ |
| 130 | + private CharSequence makeCarrierStringOnEmergencyCapable( |
| 131 | + CharSequence simMessage, CharSequence emergencyCallMessage) { |
| 132 | + if (mLockPatternUtils.isEmergencyCallCapable()) { |
| 133 | + return concatenate(simMessage, emergencyCallMessage); |
| 134 | + } |
| 135 | + return simMessage; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Determine the current status of the lock screen given the SIM state and other stuff. |
| 140 | + */ |
| 141 | + private StatusMode getStatusForIccState(IccCardConstants.State simState) { |
| 142 | + // Since reading the SIM may take a while, we assume it is present until told otherwise. |
| 143 | + if (simState == null) { |
| 144 | + return StatusMode.Normal; |
| 145 | + } |
| 146 | + |
| 147 | + final boolean missingAndNotProvisioned = |
| 148 | + !KeyguardUpdateMonitor.getInstance(mContext).isDeviceProvisioned() |
| 149 | + && (simState == IccCardConstants.State.ABSENT || |
| 150 | + simState == IccCardConstants.State.PERM_DISABLED); |
| 151 | + |
| 152 | + // Assume we're NETWORK_LOCKED if not provisioned |
| 153 | + simState = missingAndNotProvisioned ? IccCardConstants.State.NETWORK_LOCKED : simState; |
| 154 | + switch (simState) { |
| 155 | + case ABSENT: |
| 156 | + return StatusMode.SimMissing; |
| 157 | + case NETWORK_LOCKED: |
| 158 | + return StatusMode.SimMissingLocked; |
| 159 | + case NOT_READY: |
| 160 | + return StatusMode.SimMissing; |
| 161 | + case PIN_REQUIRED: |
| 162 | + return StatusMode.SimLocked; |
| 163 | + case PUK_REQUIRED: |
| 164 | + return StatusMode.SimPukLocked; |
| 165 | + case READY: |
| 166 | + return StatusMode.Normal; |
| 167 | + case PERM_DISABLED: |
| 168 | + return StatusMode.SimPermDisabled; |
| 169 | + case UNKNOWN: |
| 170 | + return StatusMode.SimMissing; |
| 171 | + } |
| 172 | + return StatusMode.SimMissing; |
| 173 | + } |
| 174 | + |
| 175 | + private static CharSequence concatenate(CharSequence plmn, CharSequence spn) { |
| 176 | + final boolean plmnValid = !TextUtils.isEmpty(plmn); |
| 177 | + final boolean spnValid = !TextUtils.isEmpty(spn); |
| 178 | + if (plmnValid && spnValid) { |
| 179 | + return plmn + "|" + spn; |
| 180 | + } else if (plmnValid) { |
| 181 | + return plmn; |
| 182 | + } else if (spnValid) { |
| 183 | + return spn; |
| 184 | + } else { |
| 185 | + return ""; |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + private CharSequence getCarrierHelpTextForSimState(IccCardConstants.State simState, |
| 190 | + String plmn, String spn) { |
| 191 | + int carrierHelpTextId = 0; |
| 192 | + StatusMode status = getStatusForIccState(simState); |
| 193 | + switch (status) { |
| 194 | + case NetworkLocked: |
| 195 | + carrierHelpTextId = R.string.lockscreen_instructions_when_pattern_disabled; |
| 196 | + break; |
| 197 | + |
| 198 | + case SimMissing: |
| 199 | + carrierHelpTextId = R.string.lockscreen_missing_sim_instructions_long; |
| 200 | + break; |
| 201 | + |
| 202 | + case SimPermDisabled: |
| 203 | + carrierHelpTextId = R.string.lockscreen_permanent_disabled_sim_instructions; |
| 204 | + break; |
| 205 | + |
| 206 | + case SimMissingLocked: |
| 207 | + carrierHelpTextId = R.string.lockscreen_missing_sim_instructions; |
| 208 | + break; |
| 209 | + |
| 210 | + case Normal: |
| 211 | + case SimLocked: |
| 212 | + case SimPukLocked: |
| 213 | + break; |
| 214 | + } |
| 215 | + |
| 216 | + return mContext.getText(carrierHelpTextId); |
| 217 | + } |
| 218 | +} |
0 commit comments