blob: 12d1f7c3118d4ab5a653de469ffe739d3200a8fe [file] [log] [blame]
Fabian Kozynski02941af2019-01-17 17:57:37 -05001/*
2 * Copyright (C) 2019 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
Malcolm Chen06ec3572019-04-09 15:55:29 -070019import static android.telephony.PhoneStateListener.LISTEN_ACTIVE_DATA_SUBSCRIPTION_ID_CHANGE;
20import static android.telephony.PhoneStateListener.LISTEN_NONE;
21
22import static com.android.internal.telephony.PhoneConstants.MAX_PHONE_COUNT_DUAL_SIM;
23
Fabian Kozynski02941af2019-01-17 17:57:37 -050024import android.content.Context;
25import android.content.Intent;
26import android.content.IntentFilter;
27import android.net.ConnectivityManager;
28import android.net.wifi.WifiManager;
Fabian Kozynskibf6fef32019-02-04 09:21:38 -050029import android.os.Handler;
Sooraj Sasindran0d45da72019-04-25 15:12:21 -070030import android.os.SystemProperties;
Malcolm Chen06ec3572019-04-09 15:55:29 -070031import android.telephony.CarrierConfigManager;
32import android.telephony.PhoneStateListener;
Fabian Kozynski02941af2019-01-17 17:57:37 -050033import android.telephony.ServiceState;
34import android.telephony.SubscriptionInfo;
Malcolm Chen06ec3572019-04-09 15:55:29 -070035import android.telephony.SubscriptionManager;
Fabian Kozynski02941af2019-01-17 17:57:37 -050036import android.telephony.TelephonyManager;
37import android.text.TextUtils;
38import android.util.Log;
39
Fabian Kozynskibf6fef32019-02-04 09:21:38 -050040import androidx.annotation.VisibleForTesting;
41
Fabian Kozynski02941af2019-01-17 17:57:37 -050042import com.android.internal.telephony.IccCardConstants;
43import com.android.internal.telephony.TelephonyIntents;
Sooraj Sasindran0d45da72019-04-25 15:12:21 -070044import com.android.internal.telephony.TelephonyProperties;
Fabian Kozynski02941af2019-01-17 17:57:37 -050045import com.android.settingslib.WirelessUtils;
46import com.android.systemui.Dependency;
47import com.android.systemui.keyguard.WakefulnessLifecycle;
48
Malcolm Chen06ec3572019-04-09 15:55:29 -070049import java.util.ArrayList;
Fabian Kozynski02941af2019-01-17 17:57:37 -050050import java.util.List;
51import java.util.Objects;
52
53/**
54 * Controller that generates text including the carrier names and/or the status of all the SIM
55 * interfaces in the device. Through a callback, the updates can be retrieved either as a list or
56 * separated by a given separator {@link CharSequence}.
57 */
58public class CarrierTextController {
59 private static final boolean DEBUG = KeyguardConstants.DEBUG;
60 private static final String TAG = "CarrierTextController";
61
62 private final boolean mIsEmergencyCallCapable;
Fabian Kozynski02941af2019-01-17 17:57:37 -050063 private boolean mTelephonyCapable;
Fabian Kozynski02941af2019-01-17 17:57:37 -050064 private boolean mShowMissingSim;
Fabian Kozynski02941af2019-01-17 17:57:37 -050065 private boolean mShowAirplaneMode;
Fabian Kozynskib176f422019-02-05 09:36:59 -050066 @VisibleForTesting
67 protected KeyguardUpdateMonitor mKeyguardUpdateMonitor;
Fabian Kozynski02941af2019-01-17 17:57:37 -050068 private WifiManager mWifiManager;
Fabian Kozynskib176f422019-02-05 09:36:59 -050069 private boolean[] mSimErrorState;
70 private final int mSimSlotsNumber;
Fabian Kozynski02941af2019-01-17 17:57:37 -050071 private CarrierTextCallback mCarrierTextCallback;
72 private Context mContext;
73 private CharSequence mSeparator;
74 private WakefulnessLifecycle mWakefulnessLifecycle;
Sooraj Sasindran0d45da72019-04-25 15:12:21 -070075 @VisibleForTesting
76 protected boolean mDisplayOpportunisticSubscriptionCarrierText;
Fabian Kozynski02941af2019-01-17 17:57:37 -050077 private final WakefulnessLifecycle.Observer mWakefulnessObserver =
78 new WakefulnessLifecycle.Observer() {
79 @Override
80 public void onFinishedWakingUp() {
81 mCarrierTextCallback.finishedWakingUp();
82 }
83
84 @Override
85 public void onStartedGoingToSleep() {
86 mCarrierTextCallback.startedGoingToSleep();
87 }
88 };
89
Fabian Kozynskib176f422019-02-05 09:36:59 -050090 @VisibleForTesting
91 protected final KeyguardUpdateMonitorCallback mCallback = new KeyguardUpdateMonitorCallback() {
Fabian Kozynski02941af2019-01-17 17:57:37 -050092 @Override
93 public void onRefreshCarrierInfo() {
94 if (DEBUG) {
95 Log.d(TAG, "onRefreshCarrierInfo(), mTelephonyCapable: "
96 + Boolean.toString(mTelephonyCapable));
97 }
98 updateCarrierText();
99 }
100
101 @Override
102 public void onTelephonyCapable(boolean capable) {
103 if (DEBUG) {
104 Log.d(TAG, "onTelephonyCapable() mTelephonyCapable: "
105 + Boolean.toString(capable));
106 }
107 mTelephonyCapable = capable;
108 updateCarrierText();
109 }
110
111 public void onSimStateChanged(int subId, int slotId, IccCardConstants.State simState) {
Fabian Kozynskib176f422019-02-05 09:36:59 -0500112 if (slotId < 0 || slotId >= mSimSlotsNumber) {
Fabian Kozynski02941af2019-01-17 17:57:37 -0500113 Log.d(TAG, "onSimStateChanged() - slotId invalid: " + slotId
114 + " mTelephonyCapable: " + Boolean.toString(mTelephonyCapable));
115 return;
116 }
117
118 if (DEBUG) Log.d(TAG, "onSimStateChanged: " + getStatusForIccState(simState));
119 if (getStatusForIccState(simState) == CarrierTextController.StatusMode.SimIoError) {
120 mSimErrorState[slotId] = true;
121 updateCarrierText();
122 } else if (mSimErrorState[slotId]) {
123 mSimErrorState[slotId] = false;
124 updateCarrierText();
125 }
126 }
127 };
128
Malcolm Chen06ec3572019-04-09 15:55:29 -0700129 private int mActiveMobileDataSubscription = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
130 private PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
131 @Override
132 public void onActiveDataSubscriptionIdChanged(int subId) {
133 mActiveMobileDataSubscription = subId;
134 if (mKeyguardUpdateMonitor != null) {
135 updateCarrierText();
136 }
137 }
138 };
139
Fabian Kozynski02941af2019-01-17 17:57:37 -0500140 /**
141 * The status of this lock screen. Primarily used for widgets on LockScreen.
142 */
143 private enum StatusMode {
144 Normal, // Normal case (sim card present, it's not locked)
145 NetworkLocked, // SIM card is 'network locked'.
146 SimMissing, // SIM card is missing.
147 SimMissingLocked, // SIM card is missing, and device isn't provisioned; don't allow access
148 SimPukLocked, // SIM card is PUK locked because SIM entered wrong too many times
149 SimLocked, // SIM card is currently locked
150 SimPermDisabled, // SIM card is permanently disabled due to PUK unlock failure
151 SimNotReady, // SIM is not ready yet. May never be on devices w/o a SIM.
152 SimIoError, // SIM card is faulty
153 SimUnknown // SIM card is unknown
154 }
155
156 /**
157 * Controller that provides updates on text with carriers names or SIM status.
158 * Used by {@link CarrierText}.
Fabian Kozynski1823f112019-01-18 11:43:29 -0500159 *
Fabian Kozynski02941af2019-01-17 17:57:37 -0500160 * @param separator Separator between different parts of the text
Fabian Kozynski02941af2019-01-17 17:57:37 -0500161 */
162 public CarrierTextController(Context context, CharSequence separator, boolean showAirplaneMode,
163 boolean showMissingSim) {
164 mContext = context;
165 mIsEmergencyCallCapable = context.getResources().getBoolean(
166 com.android.internal.R.bool.config_voice_capable);
167
168 mShowAirplaneMode = showAirplaneMode;
169 mShowMissingSim = showMissingSim;
170
171 mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
172 mSeparator = separator;
173 mWakefulnessLifecycle = Dependency.get(WakefulnessLifecycle.class);
Fabian Kozynskib176f422019-02-05 09:36:59 -0500174 mSimSlotsNumber = ((TelephonyManager) context.getSystemService(
175 Context.TELEPHONY_SERVICE)).getPhoneCount();
176 mSimErrorState = new boolean[mSimSlotsNumber];
Fabian Kozynski02941af2019-01-17 17:57:37 -0500177 }
178
179 /**
180 * Checks if there are faulty cards. Adds the text depending on the slot of the card
181 *
182 * @param text: current carrier text based on the sim state
Fabian Kozynskib176f422019-02-05 09:36:59 -0500183 * @param carrierNames names order by subscription order
184 * @param subOrderBySlot array containing the sub index for each slot ID
Fabian Kozynski02941af2019-01-17 17:57:37 -0500185 * @param noSims: whether a valid sim card is inserted
186 * @return text
187 */
Fabian Kozynskib176f422019-02-05 09:36:59 -0500188 private CharSequence updateCarrierTextWithSimIoError(CharSequence text,
189 CharSequence[] carrierNames, int[] subOrderBySlot, boolean noSims) {
Fabian Kozynski02941af2019-01-17 17:57:37 -0500190 final CharSequence carrier = "";
191 CharSequence carrierTextForSimIOError = getCarrierTextForSimState(
192 IccCardConstants.State.CARD_IO_ERROR, carrier);
Fabian Kozynskib176f422019-02-05 09:36:59 -0500193 // mSimErrorState has the state of each sim indexed by slotID.
Fabian Kozynski02941af2019-01-17 17:57:37 -0500194 for (int index = 0; index < mSimErrorState.length; index++) {
Fabian Kozynskib176f422019-02-05 09:36:59 -0500195 if (!mSimErrorState[index]) {
196 continue;
Fabian Kozynski02941af2019-01-17 17:57:37 -0500197 }
Fabian Kozynskib176f422019-02-05 09:36:59 -0500198 // In the case when no sim cards are detected but a faulty card is inserted
199 // overwrite the text and only show "Invalid card"
200 if (noSims) {
201 return concatenate(carrierTextForSimIOError,
202 getContext().getText(
203 com.android.internal.R.string.emergency_calls_only),
204 mSeparator);
205 } else if (subOrderBySlot[index] != -1) {
206 int subIndex = subOrderBySlot[index];
207 // prepend "Invalid card" when faulty card is inserted in slot 0 or 1
208 carrierNames[subIndex] = concatenate(carrierTextForSimIOError,
209 carrierNames[subIndex],
210 mSeparator);
211 } else {
212 // concatenate "Invalid card" when faulty card is inserted in other slot
213 text = concatenate(text, carrierTextForSimIOError, mSeparator);
214 }
215
Fabian Kozynski02941af2019-01-17 17:57:37 -0500216 }
217 return text;
218 }
219
220 /**
221 * Sets the listening status of this controller. If the callback is null, it is set to
222 * not listening
Fabian Kozynski1823f112019-01-18 11:43:29 -0500223 *
Fabian Kozynski02941af2019-01-17 17:57:37 -0500224 * @param callback Callback to provide text updates
225 */
226 public void setListening(CarrierTextCallback callback) {
Malcolm Chen06ec3572019-04-09 15:55:29 -0700227 TelephonyManager telephonyManager = ((TelephonyManager) mContext
228 .getSystemService(Context.TELEPHONY_SERVICE));
Fabian Kozynski02941af2019-01-17 17:57:37 -0500229 if (callback != null) {
230 mCarrierTextCallback = callback;
231 if (ConnectivityManager.from(mContext).isNetworkSupported(
232 ConnectivityManager.TYPE_MOBILE)) {
233 mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
234 mKeyguardUpdateMonitor.registerCallback(mCallback);
235 mWakefulnessLifecycle.addObserver(mWakefulnessObserver);
Malcolm Chen06ec3572019-04-09 15:55:29 -0700236 telephonyManager.listen(mPhoneStateListener,
237 LISTEN_ACTIVE_DATA_SUBSCRIPTION_ID_CHANGE);
Fabian Kozynski02941af2019-01-17 17:57:37 -0500238 } else {
239 // Don't listen and clear out the text when the device isn't a phone.
240 mKeyguardUpdateMonitor = null;
Fabian Kozynski1823f112019-01-18 11:43:29 -0500241 callback.updateCarrierInfo(new CarrierTextCallbackInfo("", null, false, null));
Fabian Kozynski02941af2019-01-17 17:57:37 -0500242 }
243 } else {
244 mCarrierTextCallback = null;
245 if (mKeyguardUpdateMonitor != null) {
246 mKeyguardUpdateMonitor.removeCallback(mCallback);
247 mWakefulnessLifecycle.removeObserver(mWakefulnessObserver);
248 }
Malcolm Chen06ec3572019-04-09 15:55:29 -0700249 telephonyManager.listen(mPhoneStateListener, LISTEN_NONE);
250 }
251 }
252
253 /**
Malcolm Chen06ec3572019-04-09 15:55:29 -0700254 * @param subscriptions
255 */
256 private void filterMobileSubscriptionInSameGroup(List<SubscriptionInfo> subscriptions) {
257 if (subscriptions.size() == MAX_PHONE_COUNT_DUAL_SIM) {
258 SubscriptionInfo info1 = subscriptions.get(0);
259 SubscriptionInfo info2 = subscriptions.get(1);
260 if (info1.getGroupUuid() != null && info1.getGroupUuid().equals(info2.getGroupUuid())) {
261 // If both subscriptions are primary, show both.
262 if (!info1.isOpportunistic() && !info2.isOpportunistic()) return;
263
264 // If carrier required, always show signal bar of primary subscription.
265 // Otherwise, show whichever subscription is currently active for Internet.
266 boolean alwaysShowPrimary = CarrierConfigManager.getDefaultConfig()
267 .getBoolean(CarrierConfigManager
268 .KEY_ALWAYS_SHOW_PRIMARY_SIGNAL_BAR_IN_OPPORTUNISTIC_NETWORK_BOOLEAN);
269 if (alwaysShowPrimary) {
270 subscriptions.remove(info1.isOpportunistic() ? info1 : info2);
271 } else {
272 subscriptions.remove(info1.getSubscriptionId() == mActiveMobileDataSubscription
273 ? info2 : info1);
274 }
275
276 }
Fabian Kozynski02941af2019-01-17 17:57:37 -0500277 }
278 }
279
Sooraj Sasindran0d45da72019-04-25 15:12:21 -0700280 /**
281 * updates if opportunistic sub carrier text should be displayed or not
282 *
283 */
284 @VisibleForTesting
285 public void updateDisplayOpportunisticSubscriptionCarrierText() {
286 mDisplayOpportunisticSubscriptionCarrierText = SystemProperties
287 .getBoolean(TelephonyProperties
288 .DISPLAY_OPPORTUNISTIC_SUBSCRIPTION_CARRIER_TEXT_PROPERTY_NAME, false);
289 }
290
291 protected List<SubscriptionInfo> getSubscriptionInfo() {
292 List<SubscriptionInfo> subs;
293 if (mDisplayOpportunisticSubscriptionCarrierText) {
294 SubscriptionManager subscriptionManager = ((SubscriptionManager) mContext
295 .getSystemService(
296 Context.TELEPHONY_SUBSCRIPTION_SERVICE));
297 subs = subscriptionManager.getActiveSubscriptionInfoList(false);
298 if (subs == null) {
299 subs = new ArrayList<>();
300 } else {
301 filterMobileSubscriptionInSameGroup(subs);
302 }
303 } else {
304 subs = mKeyguardUpdateMonitor.getSubscriptionInfo(false);
305 }
306 return subs;
307 }
308
Fabian Kozynski02941af2019-01-17 17:57:37 -0500309 protected void updateCarrierText() {
310 boolean allSimsMissing = true;
311 boolean anySimReadyAndInService = false;
312 CharSequence displayText = null;
Sooraj Sasindran0d45da72019-04-25 15:12:21 -0700313 List<SubscriptionInfo> subs = getSubscriptionInfo();
Malcolm Chen06ec3572019-04-09 15:55:29 -0700314
Fabian Kozynski02941af2019-01-17 17:57:37 -0500315 final int numSubs = subs.size();
Fabian Kozynski1823f112019-01-18 11:43:29 -0500316 final int[] subsIds = new int[numSubs];
Fabian Kozynskib176f422019-02-05 09:36:59 -0500317 // This array will contain in position i, the index of subscription in slot ID i.
318 // -1 if no subscription in that slot
319 final int[] subOrderBySlot = new int[mSimSlotsNumber];
320 for (int i = 0; i < mSimSlotsNumber; i++) {
321 subOrderBySlot[i] = -1;
322 }
323 final CharSequence[] carrierNames = new CharSequence[numSubs];
Fabian Kozynski02941af2019-01-17 17:57:37 -0500324 if (DEBUG) Log.d(TAG, "updateCarrierText(): " + numSubs);
Fabian Kozynskib176f422019-02-05 09:36:59 -0500325
Fabian Kozynski02941af2019-01-17 17:57:37 -0500326 for (int i = 0; i < numSubs; i++) {
327 int subId = subs.get(i).getSubscriptionId();
Fabian Kozynskib176f422019-02-05 09:36:59 -0500328 carrierNames[i] = "";
Fabian Kozynski1823f112019-01-18 11:43:29 -0500329 subsIds[i] = subId;
Fabian Kozynskib176f422019-02-05 09:36:59 -0500330 subOrderBySlot[subs.get(i).getSimSlotIndex()] = i;
Fabian Kozynski02941af2019-01-17 17:57:37 -0500331 IccCardConstants.State simState = mKeyguardUpdateMonitor.getSimState(subId);
332 CharSequence carrierName = subs.get(i).getCarrierName();
333 CharSequence carrierTextForSimState = getCarrierTextForSimState(simState, carrierName);
334 if (DEBUG) {
335 Log.d(TAG, "Handling (subId=" + subId + "): " + simState + " " + carrierName);
336 }
337 if (carrierTextForSimState != null) {
338 allSimsMissing = false;
Fabian Kozynskib176f422019-02-05 09:36:59 -0500339 carrierNames[i] = carrierTextForSimState;
Fabian Kozynski02941af2019-01-17 17:57:37 -0500340 }
341 if (simState == IccCardConstants.State.READY) {
342 ServiceState ss = mKeyguardUpdateMonitor.mServiceStates.get(subId);
343 if (ss != null && ss.getDataRegState() == ServiceState.STATE_IN_SERVICE) {
344 // hack for WFC (IWLAN) not turning off immediately once
345 // Wi-Fi is disassociated or disabled
346 if (ss.getRilDataRadioTechnology() != ServiceState.RIL_RADIO_TECHNOLOGY_IWLAN
347 || (mWifiManager.isWifiEnabled()
348 && mWifiManager.getConnectionInfo() != null
349 && mWifiManager.getConnectionInfo().getBSSID() != null)) {
350 if (DEBUG) {
351 Log.d(TAG, "SIM ready and in service: subId=" + subId + ", ss=" + ss);
352 }
353 anySimReadyAndInService = true;
354 }
355 }
356 }
357 }
358 if (allSimsMissing) {
359 if (numSubs != 0) {
360 // Shows "No SIM card | Emergency calls only" on devices that are voice-capable.
361 // This depends on mPlmn containing the text "Emergency calls only" when the radio
362 // has some connectivity. Otherwise, it should be null or empty and just show
363 // "No SIM card"
364 // Grab the first subscripton, because they all should contain the emergency text,
365 // described above.
366 displayText = makeCarrierStringOnEmergencyCapable(
367 getMissingSimMessage(), subs.get(0).getCarrierName());
368 } else {
369 // We don't have a SubscriptionInfo to get the emergency calls only from.
370 // Grab it from the old sticky broadcast if possible instead. We can use it
371 // here because no subscriptions are active, so we don't have
372 // to worry about MSIM clashing.
373 CharSequence text =
374 getContext().getText(com.android.internal.R.string.emergency_calls_only);
375 Intent i = getContext().registerReceiver(null,
376 new IntentFilter(TelephonyIntents.SPN_STRINGS_UPDATED_ACTION));
377 if (i != null) {
378 String spn = "";
379 String plmn = "";
380 if (i.getBooleanExtra(TelephonyIntents.EXTRA_SHOW_SPN, false)) {
381 spn = i.getStringExtra(TelephonyIntents.EXTRA_SPN);
382 }
383 if (i.getBooleanExtra(TelephonyIntents.EXTRA_SHOW_PLMN, false)) {
384 plmn = i.getStringExtra(TelephonyIntents.EXTRA_PLMN);
385 }
386 if (DEBUG) Log.d(TAG, "Getting plmn/spn sticky brdcst " + plmn + "/" + spn);
387 if (Objects.equals(plmn, spn)) {
388 text = plmn;
389 } else {
390 text = concatenate(plmn, spn, mSeparator);
391 }
392 }
393 displayText = makeCarrierStringOnEmergencyCapable(getMissingSimMessage(), text);
394 }
395 }
396
Fabian Kozynskib176f422019-02-05 09:36:59 -0500397 displayText = updateCarrierTextWithSimIoError(displayText, carrierNames, subOrderBySlot,
398 allSimsMissing);
Fabian Kozynskib38edbb2019-04-12 12:20:13 -0400399 boolean airplaneMode = false;
Fabian Kozynski02941af2019-01-17 17:57:37 -0500400 // APM (airplane mode) != no carrier state. There are carrier services
401 // (e.g. WFC = Wi-Fi calling) which may operate in APM.
402 if (!anySimReadyAndInService && WirelessUtils.isAirplaneModeOn(mContext)) {
403 displayText = getAirplaneModeMessage();
Fabian Kozynskib38edbb2019-04-12 12:20:13 -0400404 airplaneMode = true;
Fabian Kozynski02941af2019-01-17 17:57:37 -0500405 }
406
Fabian Kozynskib38edbb2019-04-12 12:20:13 -0400407 if (TextUtils.isEmpty(displayText) && !airplaneMode) {
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400408 displayText = joinNotEmpty(mSeparator, carrierNames);
Fabian Kozynskib176f422019-02-05 09:36:59 -0500409 }
Fabian Kozynskibf6fef32019-02-04 09:21:38 -0500410 final CarrierTextCallbackInfo info = new CarrierTextCallbackInfo(
411 displayText,
Fabian Kozynskib176f422019-02-05 09:36:59 -0500412 carrierNames,
413 !allSimsMissing,
Fabian Kozynskib38edbb2019-04-12 12:20:13 -0400414 subsIds,
415 airplaneMode);
Fabian Kozynskib176f422019-02-05 09:36:59 -0500416 postToCallback(info);
417 }
418
419 @VisibleForTesting
420 protected void postToCallback(CarrierTextCallbackInfo info) {
421 Handler handler = Dependency.get(Dependency.MAIN_HANDLER);
Fabian Kozynski48343c32019-02-07 10:28:50 -0500422 final CarrierTextCallback callback = mCarrierTextCallback;
423 if (callback != null) {
424 handler.post(() -> callback.updateCarrierInfo(info));
Fabian Kozynski02941af2019-01-17 17:57:37 -0500425 }
Fabian Kozynski02941af2019-01-17 17:57:37 -0500426 }
427
428 private Context getContext() {
429 return mContext;
430 }
431
432 private String getMissingSimMessage() {
433 return mShowMissingSim && mTelephonyCapable
434 ? getContext().getString(R.string.keyguard_missing_sim_message_short) : "";
435 }
436
437 private String getAirplaneModeMessage() {
438 return mShowAirplaneMode
439 ? getContext().getString(R.string.airplane_mode) : "";
440 }
441
442 /**
443 * Top-level function for creating carrier text. Makes text based on simState, PLMN
444 * and SPN as well as device capabilities, such as being emergency call capable.
445 *
446 * @return Carrier text if not in missing state, null otherwise.
447 */
448 private CharSequence getCarrierTextForSimState(IccCardConstants.State simState,
449 CharSequence text) {
450 CharSequence carrierText = null;
451 CarrierTextController.StatusMode status = getStatusForIccState(simState);
452 switch (status) {
453 case Normal:
454 carrierText = text;
455 break;
456
457 case SimNotReady:
458 // Null is reserved for denoting missing, in this case we have nothing to display.
459 carrierText = ""; // nothing to display yet.
460 break;
461
462 case NetworkLocked:
463 carrierText = makeCarrierStringOnEmergencyCapable(
464 mContext.getText(R.string.keyguard_network_locked_message), text);
465 break;
466
467 case SimMissing:
468 carrierText = null;
469 break;
470
471 case SimPermDisabled:
472 carrierText = makeCarrierStringOnEmergencyCapable(
473 getContext().getText(
474 R.string.keyguard_permanent_disabled_sim_message_short),
475 text);
476 break;
477
478 case SimMissingLocked:
479 carrierText = null;
480 break;
481
482 case SimLocked:
483 carrierText = makeCarrierStringOnEmergencyCapable(
484 getContext().getText(R.string.keyguard_sim_locked_message),
485 text);
486 break;
487
488 case SimPukLocked:
489 carrierText = makeCarrierStringOnEmergencyCapable(
490 getContext().getText(R.string.keyguard_sim_puk_locked_message),
491 text);
492 break;
493 case SimIoError:
494 carrierText = makeCarrierStringOnEmergencyCapable(
495 getContext().getText(R.string.keyguard_sim_error_message_short),
496 text);
497 break;
498 case SimUnknown:
499 carrierText = null;
500 break;
501 }
502
503 return carrierText;
504 }
505
506 /*
507 * Add emergencyCallMessage to carrier string only if phone supports emergency calls.
508 */
509 private CharSequence makeCarrierStringOnEmergencyCapable(
510 CharSequence simMessage, CharSequence emergencyCallMessage) {
511 if (mIsEmergencyCallCapable) {
512 return concatenate(simMessage, emergencyCallMessage, mSeparator);
513 }
514 return simMessage;
515 }
516
517 /**
518 * Determine the current status of the lock screen given the SIM state and other stuff.
519 */
520 private CarrierTextController.StatusMode getStatusForIccState(IccCardConstants.State simState) {
521 // Since reading the SIM may take a while, we assume it is present until told otherwise.
522 if (simState == null) {
523 return CarrierTextController.StatusMode.Normal;
524 }
525
526 final boolean missingAndNotProvisioned =
527 !KeyguardUpdateMonitor.getInstance(mContext).isDeviceProvisioned()
528 && (simState == IccCardConstants.State.ABSENT
529 || simState == IccCardConstants.State.PERM_DISABLED);
530
531 // Assume we're NETWORK_LOCKED if not provisioned
532 simState = missingAndNotProvisioned ? IccCardConstants.State.NETWORK_LOCKED : simState;
533 switch (simState) {
534 case ABSENT:
535 return CarrierTextController.StatusMode.SimMissing;
536 case NETWORK_LOCKED:
537 return CarrierTextController.StatusMode.SimMissingLocked;
538 case NOT_READY:
539 return CarrierTextController.StatusMode.SimNotReady;
540 case PIN_REQUIRED:
541 return CarrierTextController.StatusMode.SimLocked;
542 case PUK_REQUIRED:
543 return CarrierTextController.StatusMode.SimPukLocked;
544 case READY:
545 return CarrierTextController.StatusMode.Normal;
546 case PERM_DISABLED:
547 return CarrierTextController.StatusMode.SimPermDisabled;
548 case UNKNOWN:
549 return CarrierTextController.StatusMode.SimUnknown;
550 case CARD_IO_ERROR:
551 return CarrierTextController.StatusMode.SimIoError;
552 }
553 return CarrierTextController.StatusMode.SimUnknown;
554 }
555
556 private static CharSequence concatenate(CharSequence plmn, CharSequence spn,
557 CharSequence separator) {
558 final boolean plmnValid = !TextUtils.isEmpty(plmn);
559 final boolean spnValid = !TextUtils.isEmpty(spn);
560 if (plmnValid && spnValid) {
561 return new StringBuilder().append(plmn).append(separator).append(spn).toString();
562 } else if (plmnValid) {
563 return plmn;
564 } else if (spnValid) {
565 return spn;
566 } else {
567 return "";
568 }
569 }
570
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400571 /**
572 * Joins the strings in a sequence using a separator. Empty strings are discarded with no extra
573 * separator added so there are no extra separators that are not needed.
574 */
575 private static CharSequence joinNotEmpty(CharSequence separator, CharSequence[] sequences) {
576 int length = sequences.length;
577 if (length == 0) return "";
578 StringBuilder sb = new StringBuilder();
579 for (int i = 0; i < length; i++) {
580 if (!TextUtils.isEmpty(sequences[i])) {
581 if (!TextUtils.isEmpty(sb)) {
582 sb.append(separator);
583 }
584 sb.append(sequences[i]);
585 }
586 }
587 return sb.toString();
588 }
589
Fabian Kozynski02941af2019-01-17 17:57:37 -0500590 private static List<CharSequence> append(List<CharSequence> list, CharSequence string) {
591 if (!TextUtils.isEmpty(string)) {
592 list.add(string);
593 }
594 return list;
595 }
596
597 private CharSequence getCarrierHelpTextForSimState(IccCardConstants.State simState,
598 String plmn, String spn) {
599 int carrierHelpTextId = 0;
600 CarrierTextController.StatusMode status = getStatusForIccState(simState);
601 switch (status) {
602 case NetworkLocked:
603 carrierHelpTextId = R.string.keyguard_instructions_when_pattern_disabled;
604 break;
605
606 case SimMissing:
607 carrierHelpTextId = R.string.keyguard_missing_sim_instructions_long;
608 break;
609
610 case SimPermDisabled:
611 carrierHelpTextId = R.string.keyguard_permanent_disabled_sim_instructions;
612 break;
613
614 case SimMissingLocked:
615 carrierHelpTextId = R.string.keyguard_missing_sim_instructions;
616 break;
617
618 case Normal:
619 case SimLocked:
620 case SimPukLocked:
621 break;
622 }
623
624 return mContext.getText(carrierHelpTextId);
625 }
626
627 /**
Fabian Kozynski1823f112019-01-18 11:43:29 -0500628 * Data structure for passing information to CarrierTextController subscribers
629 */
630 public static final class CarrierTextCallbackInfo {
631 public final CharSequence carrierText;
632 public final CharSequence[] listOfCarriers;
633 public final boolean anySimReady;
634 public final int[] subscriptionIds;
Fabian Kozynskib38edbb2019-04-12 12:20:13 -0400635 public boolean airplaneMode;
Fabian Kozynski1823f112019-01-18 11:43:29 -0500636
Fabian Kozynskibf6fef32019-02-04 09:21:38 -0500637 @VisibleForTesting
638 public CarrierTextCallbackInfo(CharSequence carrierText, CharSequence[] listOfCarriers,
Fabian Kozynski1823f112019-01-18 11:43:29 -0500639 boolean anySimReady, int[] subscriptionIds) {
Fabian Kozynskib38edbb2019-04-12 12:20:13 -0400640 this(carrierText, listOfCarriers, anySimReady, subscriptionIds, false);
641 }
642
643 @VisibleForTesting
644 public CarrierTextCallbackInfo(CharSequence carrierText, CharSequence[] listOfCarriers,
645 boolean anySimReady, int[] subscriptionIds, boolean airplaneMode) {
Fabian Kozynski1823f112019-01-18 11:43:29 -0500646 this.carrierText = carrierText;
647 this.listOfCarriers = listOfCarriers;
648 this.anySimReady = anySimReady;
649 this.subscriptionIds = subscriptionIds;
Fabian Kozynskib38edbb2019-04-12 12:20:13 -0400650 this.airplaneMode = airplaneMode;
Fabian Kozynski1823f112019-01-18 11:43:29 -0500651 }
652 }
653
654 /**
Fabian Kozynski02941af2019-01-17 17:57:37 -0500655 * Callback to communicate to Views
656 */
657 public interface CarrierTextCallback {
658 /**
Fabian Kozynski1823f112019-01-18 11:43:29 -0500659 * Provides updated carrier information.
Fabian Kozynski02941af2019-01-17 17:57:37 -0500660 */
Fabian Kozynski1823f112019-01-18 11:43:29 -0500661 default void updateCarrierInfo(CarrierTextCallbackInfo info) {};
Fabian Kozynski02941af2019-01-17 17:57:37 -0500662
663 /**
664 * Notifies the View that the device is going to sleep
665 */
666 default void startedGoingToSleep() {};
667
668 /**
669 * Notifies the View that the device finished waking up
670 */
671 default void finishedWakingUp() {};
672 }
673}