blob: 209074812d7a0c0d4ea131715811a79e7021f1e7 [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];
Sooraj Sasindranb7d633b2019-05-02 13:47:21 -0700177 updateDisplayOpportunisticSubscriptionCarrierText(SystemProperties.getBoolean(
178 TelephonyProperties.DISPLAY_OPPORTUNISTIC_SUBSCRIPTION_CARRIER_TEXT_PROPERTY_NAME,
179 false));
Fabian Kozynski02941af2019-01-17 17:57:37 -0500180 }
181
182 /**
183 * Checks if there are faulty cards. Adds the text depending on the slot of the card
184 *
185 * @param text: current carrier text based on the sim state
Fabian Kozynskib176f422019-02-05 09:36:59 -0500186 * @param carrierNames names order by subscription order
187 * @param subOrderBySlot array containing the sub index for each slot ID
Fabian Kozynski02941af2019-01-17 17:57:37 -0500188 * @param noSims: whether a valid sim card is inserted
189 * @return text
190 */
Fabian Kozynskib176f422019-02-05 09:36:59 -0500191 private CharSequence updateCarrierTextWithSimIoError(CharSequence text,
192 CharSequence[] carrierNames, int[] subOrderBySlot, boolean noSims) {
Fabian Kozynski02941af2019-01-17 17:57:37 -0500193 final CharSequence carrier = "";
194 CharSequence carrierTextForSimIOError = getCarrierTextForSimState(
195 IccCardConstants.State.CARD_IO_ERROR, carrier);
Fabian Kozynskib176f422019-02-05 09:36:59 -0500196 // mSimErrorState has the state of each sim indexed by slotID.
Fabian Kozynski02941af2019-01-17 17:57:37 -0500197 for (int index = 0; index < mSimErrorState.length; index++) {
Fabian Kozynskib176f422019-02-05 09:36:59 -0500198 if (!mSimErrorState[index]) {
199 continue;
Fabian Kozynski02941af2019-01-17 17:57:37 -0500200 }
Fabian Kozynskib176f422019-02-05 09:36:59 -0500201 // In the case when no sim cards are detected but a faulty card is inserted
202 // overwrite the text and only show "Invalid card"
203 if (noSims) {
204 return concatenate(carrierTextForSimIOError,
205 getContext().getText(
206 com.android.internal.R.string.emergency_calls_only),
207 mSeparator);
208 } else if (subOrderBySlot[index] != -1) {
209 int subIndex = subOrderBySlot[index];
210 // prepend "Invalid card" when faulty card is inserted in slot 0 or 1
211 carrierNames[subIndex] = concatenate(carrierTextForSimIOError,
212 carrierNames[subIndex],
213 mSeparator);
214 } else {
215 // concatenate "Invalid card" when faulty card is inserted in other slot
216 text = concatenate(text, carrierTextForSimIOError, mSeparator);
217 }
218
Fabian Kozynski02941af2019-01-17 17:57:37 -0500219 }
220 return text;
221 }
222
223 /**
224 * Sets the listening status of this controller. If the callback is null, it is set to
225 * not listening
Fabian Kozynski1823f112019-01-18 11:43:29 -0500226 *
Fabian Kozynski02941af2019-01-17 17:57:37 -0500227 * @param callback Callback to provide text updates
228 */
229 public void setListening(CarrierTextCallback callback) {
Malcolm Chen06ec3572019-04-09 15:55:29 -0700230 TelephonyManager telephonyManager = ((TelephonyManager) mContext
231 .getSystemService(Context.TELEPHONY_SERVICE));
Fabian Kozynski02941af2019-01-17 17:57:37 -0500232 if (callback != null) {
233 mCarrierTextCallback = callback;
234 if (ConnectivityManager.from(mContext).isNetworkSupported(
235 ConnectivityManager.TYPE_MOBILE)) {
236 mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
237 mKeyguardUpdateMonitor.registerCallback(mCallback);
238 mWakefulnessLifecycle.addObserver(mWakefulnessObserver);
Malcolm Chen06ec3572019-04-09 15:55:29 -0700239 telephonyManager.listen(mPhoneStateListener,
240 LISTEN_ACTIVE_DATA_SUBSCRIPTION_ID_CHANGE);
Fabian Kozynski02941af2019-01-17 17:57:37 -0500241 } else {
242 // Don't listen and clear out the text when the device isn't a phone.
243 mKeyguardUpdateMonitor = null;
Fabian Kozynski1823f112019-01-18 11:43:29 -0500244 callback.updateCarrierInfo(new CarrierTextCallbackInfo("", null, false, null));
Fabian Kozynski02941af2019-01-17 17:57:37 -0500245 }
246 } else {
247 mCarrierTextCallback = null;
248 if (mKeyguardUpdateMonitor != null) {
249 mKeyguardUpdateMonitor.removeCallback(mCallback);
250 mWakefulnessLifecycle.removeObserver(mWakefulnessObserver);
251 }
Malcolm Chen06ec3572019-04-09 15:55:29 -0700252 telephonyManager.listen(mPhoneStateListener, LISTEN_NONE);
253 }
254 }
255
256 /**
Malcolm Chen06ec3572019-04-09 15:55:29 -0700257 * @param subscriptions
258 */
259 private void filterMobileSubscriptionInSameGroup(List<SubscriptionInfo> subscriptions) {
260 if (subscriptions.size() == MAX_PHONE_COUNT_DUAL_SIM) {
261 SubscriptionInfo info1 = subscriptions.get(0);
262 SubscriptionInfo info2 = subscriptions.get(1);
263 if (info1.getGroupUuid() != null && info1.getGroupUuid().equals(info2.getGroupUuid())) {
264 // If both subscriptions are primary, show both.
265 if (!info1.isOpportunistic() && !info2.isOpportunistic()) return;
266
267 // If carrier required, always show signal bar of primary subscription.
268 // Otherwise, show whichever subscription is currently active for Internet.
269 boolean alwaysShowPrimary = CarrierConfigManager.getDefaultConfig()
270 .getBoolean(CarrierConfigManager
271 .KEY_ALWAYS_SHOW_PRIMARY_SIGNAL_BAR_IN_OPPORTUNISTIC_NETWORK_BOOLEAN);
272 if (alwaysShowPrimary) {
273 subscriptions.remove(info1.isOpportunistic() ? info1 : info2);
274 } else {
275 subscriptions.remove(info1.getSubscriptionId() == mActiveMobileDataSubscription
276 ? info2 : info1);
277 }
278
279 }
Fabian Kozynski02941af2019-01-17 17:57:37 -0500280 }
281 }
282
Sooraj Sasindran0d45da72019-04-25 15:12:21 -0700283 /**
284 * updates if opportunistic sub carrier text should be displayed or not
285 *
286 */
287 @VisibleForTesting
Sooraj Sasindranb7d633b2019-05-02 13:47:21 -0700288 public void updateDisplayOpportunisticSubscriptionCarrierText(boolean isEnable) {
289 mDisplayOpportunisticSubscriptionCarrierText = isEnable;
Sooraj Sasindran0d45da72019-04-25 15:12:21 -0700290 }
291
292 protected List<SubscriptionInfo> getSubscriptionInfo() {
293 List<SubscriptionInfo> subs;
294 if (mDisplayOpportunisticSubscriptionCarrierText) {
295 SubscriptionManager subscriptionManager = ((SubscriptionManager) mContext
296 .getSystemService(
297 Context.TELEPHONY_SUBSCRIPTION_SERVICE));
298 subs = subscriptionManager.getActiveSubscriptionInfoList(false);
299 if (subs == null) {
300 subs = new ArrayList<>();
301 } else {
302 filterMobileSubscriptionInSameGroup(subs);
303 }
304 } else {
305 subs = mKeyguardUpdateMonitor.getSubscriptionInfo(false);
306 }
307 return subs;
308 }
309
Fabian Kozynski02941af2019-01-17 17:57:37 -0500310 protected void updateCarrierText() {
311 boolean allSimsMissing = true;
312 boolean anySimReadyAndInService = false;
313 CharSequence displayText = null;
Sooraj Sasindran0d45da72019-04-25 15:12:21 -0700314 List<SubscriptionInfo> subs = getSubscriptionInfo();
Malcolm Chen06ec3572019-04-09 15:55:29 -0700315
Fabian Kozynski02941af2019-01-17 17:57:37 -0500316 final int numSubs = subs.size();
Fabian Kozynski1823f112019-01-18 11:43:29 -0500317 final int[] subsIds = new int[numSubs];
Fabian Kozynskib176f422019-02-05 09:36:59 -0500318 // This array will contain in position i, the index of subscription in slot ID i.
319 // -1 if no subscription in that slot
320 final int[] subOrderBySlot = new int[mSimSlotsNumber];
321 for (int i = 0; i < mSimSlotsNumber; i++) {
322 subOrderBySlot[i] = -1;
323 }
324 final CharSequence[] carrierNames = new CharSequence[numSubs];
Fabian Kozynski02941af2019-01-17 17:57:37 -0500325 if (DEBUG) Log.d(TAG, "updateCarrierText(): " + numSubs);
Fabian Kozynskib176f422019-02-05 09:36:59 -0500326
Fabian Kozynski02941af2019-01-17 17:57:37 -0500327 for (int i = 0; i < numSubs; i++) {
328 int subId = subs.get(i).getSubscriptionId();
Fabian Kozynskib176f422019-02-05 09:36:59 -0500329 carrierNames[i] = "";
Fabian Kozynski1823f112019-01-18 11:43:29 -0500330 subsIds[i] = subId;
Fabian Kozynskib176f422019-02-05 09:36:59 -0500331 subOrderBySlot[subs.get(i).getSimSlotIndex()] = i;
Fabian Kozynski02941af2019-01-17 17:57:37 -0500332 IccCardConstants.State simState = mKeyguardUpdateMonitor.getSimState(subId);
333 CharSequence carrierName = subs.get(i).getCarrierName();
334 CharSequence carrierTextForSimState = getCarrierTextForSimState(simState, carrierName);
335 if (DEBUG) {
336 Log.d(TAG, "Handling (subId=" + subId + "): " + simState + " " + carrierName);
337 }
338 if (carrierTextForSimState != null) {
339 allSimsMissing = false;
Fabian Kozynskib176f422019-02-05 09:36:59 -0500340 carrierNames[i] = carrierTextForSimState;
Fabian Kozynski02941af2019-01-17 17:57:37 -0500341 }
342 if (simState == IccCardConstants.State.READY) {
Bonian Chena7e9e8c2019-06-02 23:59:31 +0000343 ServiceState ss = mKeyguardUpdateMonitor.mServiceStates.get(subId);
344 if (ss != null && ss.getDataRegState() == ServiceState.STATE_IN_SERVICE) {
Fabian Kozynski02941af2019-01-17 17:57:37 -0500345 // hack for WFC (IWLAN) not turning off immediately once
346 // Wi-Fi is disassociated or disabled
Bonian Chena7e9e8c2019-06-02 23:59:31 +0000347 if (ss.getRilDataRadioTechnology() != ServiceState.RIL_RADIO_TECHNOLOGY_IWLAN
Fabian Kozynski02941af2019-01-17 17:57:37 -0500348 || (mWifiManager.isWifiEnabled()
349 && mWifiManager.getConnectionInfo() != null
350 && mWifiManager.getConnectionInfo().getBSSID() != null)) {
351 if (DEBUG) {
Bonian Chena7e9e8c2019-06-02 23:59:31 +0000352 Log.d(TAG, "SIM ready and in service: subId=" + subId + ", ss=" + ss);
Fabian Kozynski02941af2019-01-17 17:57:37 -0500353 }
354 anySimReadyAndInService = true;
355 }
356 }
357 }
358 }
359 if (allSimsMissing) {
360 if (numSubs != 0) {
361 // Shows "No SIM card | Emergency calls only" on devices that are voice-capable.
362 // This depends on mPlmn containing the text "Emergency calls only" when the radio
363 // has some connectivity. Otherwise, it should be null or empty and just show
364 // "No SIM card"
365 // Grab the first subscripton, because they all should contain the emergency text,
366 // described above.
367 displayText = makeCarrierStringOnEmergencyCapable(
368 getMissingSimMessage(), subs.get(0).getCarrierName());
369 } else {
370 // We don't have a SubscriptionInfo to get the emergency calls only from.
371 // Grab it from the old sticky broadcast if possible instead. We can use it
372 // here because no subscriptions are active, so we don't have
373 // to worry about MSIM clashing.
374 CharSequence text =
375 getContext().getText(com.android.internal.R.string.emergency_calls_only);
376 Intent i = getContext().registerReceiver(null,
377 new IntentFilter(TelephonyIntents.SPN_STRINGS_UPDATED_ACTION));
378 if (i != null) {
379 String spn = "";
380 String plmn = "";
381 if (i.getBooleanExtra(TelephonyIntents.EXTRA_SHOW_SPN, false)) {
382 spn = i.getStringExtra(TelephonyIntents.EXTRA_SPN);
383 }
384 if (i.getBooleanExtra(TelephonyIntents.EXTRA_SHOW_PLMN, false)) {
385 plmn = i.getStringExtra(TelephonyIntents.EXTRA_PLMN);
386 }
387 if (DEBUG) Log.d(TAG, "Getting plmn/spn sticky brdcst " + plmn + "/" + spn);
388 if (Objects.equals(plmn, spn)) {
389 text = plmn;
390 } else {
391 text = concatenate(plmn, spn, mSeparator);
392 }
393 }
394 displayText = makeCarrierStringOnEmergencyCapable(getMissingSimMessage(), text);
395 }
396 }
397
Fabian Kozynskib176f422019-02-05 09:36:59 -0500398 displayText = updateCarrierTextWithSimIoError(displayText, carrierNames, subOrderBySlot,
399 allSimsMissing);
Fabian Kozynskib38edbb2019-04-12 12:20:13 -0400400 boolean airplaneMode = false;
Fabian Kozynski02941af2019-01-17 17:57:37 -0500401 // APM (airplane mode) != no carrier state. There are carrier services
402 // (e.g. WFC = Wi-Fi calling) which may operate in APM.
403 if (!anySimReadyAndInService && WirelessUtils.isAirplaneModeOn(mContext)) {
404 displayText = getAirplaneModeMessage();
Fabian Kozynskib38edbb2019-04-12 12:20:13 -0400405 airplaneMode = true;
Fabian Kozynski02941af2019-01-17 17:57:37 -0500406 }
407
Fabian Kozynskib38edbb2019-04-12 12:20:13 -0400408 if (TextUtils.isEmpty(displayText) && !airplaneMode) {
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400409 displayText = joinNotEmpty(mSeparator, carrierNames);
Fabian Kozynskib176f422019-02-05 09:36:59 -0500410 }
Fabian Kozynskibf6fef32019-02-04 09:21:38 -0500411 final CarrierTextCallbackInfo info = new CarrierTextCallbackInfo(
412 displayText,
Fabian Kozynskib176f422019-02-05 09:36:59 -0500413 carrierNames,
414 !allSimsMissing,
Fabian Kozynskib38edbb2019-04-12 12:20:13 -0400415 subsIds,
416 airplaneMode);
Fabian Kozynskib176f422019-02-05 09:36:59 -0500417 postToCallback(info);
418 }
419
420 @VisibleForTesting
421 protected void postToCallback(CarrierTextCallbackInfo info) {
422 Handler handler = Dependency.get(Dependency.MAIN_HANDLER);
Fabian Kozynski48343c32019-02-07 10:28:50 -0500423 final CarrierTextCallback callback = mCarrierTextCallback;
424 if (callback != null) {
425 handler.post(() -> callback.updateCarrierInfo(info));
Fabian Kozynski02941af2019-01-17 17:57:37 -0500426 }
Fabian Kozynski02941af2019-01-17 17:57:37 -0500427 }
428
429 private Context getContext() {
430 return mContext;
431 }
432
433 private String getMissingSimMessage() {
434 return mShowMissingSim && mTelephonyCapable
435 ? getContext().getString(R.string.keyguard_missing_sim_message_short) : "";
436 }
437
438 private String getAirplaneModeMessage() {
439 return mShowAirplaneMode
440 ? getContext().getString(R.string.airplane_mode) : "";
441 }
442
443 /**
444 * Top-level function for creating carrier text. Makes text based on simState, PLMN
445 * and SPN as well as device capabilities, such as being emergency call capable.
446 *
447 * @return Carrier text if not in missing state, null otherwise.
448 */
449 private CharSequence getCarrierTextForSimState(IccCardConstants.State simState,
450 CharSequence text) {
451 CharSequence carrierText = null;
452 CarrierTextController.StatusMode status = getStatusForIccState(simState);
453 switch (status) {
454 case Normal:
455 carrierText = text;
456 break;
457
458 case SimNotReady:
459 // Null is reserved for denoting missing, in this case we have nothing to display.
460 carrierText = ""; // nothing to display yet.
461 break;
462
463 case NetworkLocked:
464 carrierText = makeCarrierStringOnEmergencyCapable(
465 mContext.getText(R.string.keyguard_network_locked_message), text);
466 break;
467
468 case SimMissing:
469 carrierText = null;
470 break;
471
472 case SimPermDisabled:
473 carrierText = makeCarrierStringOnEmergencyCapable(
474 getContext().getText(
475 R.string.keyguard_permanent_disabled_sim_message_short),
476 text);
477 break;
478
479 case SimMissingLocked:
480 carrierText = null;
481 break;
482
483 case SimLocked:
Fabian Kozynski2fb343a2019-05-08 16:06:51 -0400484 carrierText = makeCarrierStringOnLocked(
Fabian Kozynski02941af2019-01-17 17:57:37 -0500485 getContext().getText(R.string.keyguard_sim_locked_message),
486 text);
487 break;
488
489 case SimPukLocked:
Fabian Kozynski2fb343a2019-05-08 16:06:51 -0400490 carrierText = makeCarrierStringOnLocked(
Fabian Kozynski02941af2019-01-17 17:57:37 -0500491 getContext().getText(R.string.keyguard_sim_puk_locked_message),
492 text);
493 break;
494 case SimIoError:
495 carrierText = makeCarrierStringOnEmergencyCapable(
496 getContext().getText(R.string.keyguard_sim_error_message_short),
497 text);
498 break;
499 case SimUnknown:
500 carrierText = null;
501 break;
502 }
503
504 return carrierText;
505 }
506
507 /*
508 * Add emergencyCallMessage to carrier string only if phone supports emergency calls.
509 */
510 private CharSequence makeCarrierStringOnEmergencyCapable(
511 CharSequence simMessage, CharSequence emergencyCallMessage) {
512 if (mIsEmergencyCallCapable) {
513 return concatenate(simMessage, emergencyCallMessage, mSeparator);
514 }
515 return simMessage;
516 }
517
Fabian Kozynski2fb343a2019-05-08 16:06:51 -0400518 /*
519 * Add "SIM card is locked" in parenthesis after carrier name, so it is easily associated in
520 * DSDS
521 */
522 private CharSequence makeCarrierStringOnLocked(CharSequence simMessage,
523 CharSequence carrierName) {
524 final boolean simMessageValid = !TextUtils.isEmpty(simMessage);
525 final boolean carrierNameValid = !TextUtils.isEmpty(carrierName);
526 if (simMessageValid && carrierNameValid) {
527 return mContext.getString(R.string.keyguard_carrier_name_with_sim_locked_template,
528 carrierName, simMessage);
529 } else if (simMessageValid) {
530 return simMessage;
531 } else if (carrierNameValid) {
532 return carrierName;
533 } else {
534 return "";
535 }
536 }
537
Fabian Kozynski02941af2019-01-17 17:57:37 -0500538 /**
539 * Determine the current status of the lock screen given the SIM state and other stuff.
540 */
541 private CarrierTextController.StatusMode getStatusForIccState(IccCardConstants.State simState) {
542 // Since reading the SIM may take a while, we assume it is present until told otherwise.
543 if (simState == null) {
544 return CarrierTextController.StatusMode.Normal;
545 }
546
547 final boolean missingAndNotProvisioned =
548 !KeyguardUpdateMonitor.getInstance(mContext).isDeviceProvisioned()
549 && (simState == IccCardConstants.State.ABSENT
550 || simState == IccCardConstants.State.PERM_DISABLED);
551
552 // Assume we're NETWORK_LOCKED if not provisioned
553 simState = missingAndNotProvisioned ? IccCardConstants.State.NETWORK_LOCKED : simState;
554 switch (simState) {
555 case ABSENT:
556 return CarrierTextController.StatusMode.SimMissing;
557 case NETWORK_LOCKED:
558 return CarrierTextController.StatusMode.SimMissingLocked;
559 case NOT_READY:
560 return CarrierTextController.StatusMode.SimNotReady;
561 case PIN_REQUIRED:
562 return CarrierTextController.StatusMode.SimLocked;
563 case PUK_REQUIRED:
564 return CarrierTextController.StatusMode.SimPukLocked;
565 case READY:
566 return CarrierTextController.StatusMode.Normal;
567 case PERM_DISABLED:
568 return CarrierTextController.StatusMode.SimPermDisabled;
569 case UNKNOWN:
570 return CarrierTextController.StatusMode.SimUnknown;
571 case CARD_IO_ERROR:
572 return CarrierTextController.StatusMode.SimIoError;
573 }
574 return CarrierTextController.StatusMode.SimUnknown;
575 }
576
577 private static CharSequence concatenate(CharSequence plmn, CharSequence spn,
578 CharSequence separator) {
579 final boolean plmnValid = !TextUtils.isEmpty(plmn);
580 final boolean spnValid = !TextUtils.isEmpty(spn);
581 if (plmnValid && spnValid) {
582 return new StringBuilder().append(plmn).append(separator).append(spn).toString();
583 } else if (plmnValid) {
584 return plmn;
585 } else if (spnValid) {
586 return spn;
587 } else {
588 return "";
589 }
590 }
591
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400592 /**
593 * Joins the strings in a sequence using a separator. Empty strings are discarded with no extra
594 * separator added so there are no extra separators that are not needed.
595 */
596 private static CharSequence joinNotEmpty(CharSequence separator, CharSequence[] sequences) {
597 int length = sequences.length;
598 if (length == 0) return "";
599 StringBuilder sb = new StringBuilder();
600 for (int i = 0; i < length; i++) {
601 if (!TextUtils.isEmpty(sequences[i])) {
602 if (!TextUtils.isEmpty(sb)) {
603 sb.append(separator);
604 }
605 sb.append(sequences[i]);
606 }
607 }
608 return sb.toString();
609 }
610
Fabian Kozynski02941af2019-01-17 17:57:37 -0500611 private static List<CharSequence> append(List<CharSequence> list, CharSequence string) {
612 if (!TextUtils.isEmpty(string)) {
613 list.add(string);
614 }
615 return list;
616 }
617
618 private CharSequence getCarrierHelpTextForSimState(IccCardConstants.State simState,
619 String plmn, String spn) {
620 int carrierHelpTextId = 0;
621 CarrierTextController.StatusMode status = getStatusForIccState(simState);
622 switch (status) {
623 case NetworkLocked:
624 carrierHelpTextId = R.string.keyguard_instructions_when_pattern_disabled;
625 break;
626
627 case SimMissing:
628 carrierHelpTextId = R.string.keyguard_missing_sim_instructions_long;
629 break;
630
631 case SimPermDisabled:
632 carrierHelpTextId = R.string.keyguard_permanent_disabled_sim_instructions;
633 break;
634
635 case SimMissingLocked:
636 carrierHelpTextId = R.string.keyguard_missing_sim_instructions;
637 break;
638
639 case Normal:
640 case SimLocked:
641 case SimPukLocked:
642 break;
643 }
644
645 return mContext.getText(carrierHelpTextId);
646 }
647
648 /**
Fabian Kozynski1823f112019-01-18 11:43:29 -0500649 * Data structure for passing information to CarrierTextController subscribers
650 */
651 public static final class CarrierTextCallbackInfo {
652 public final CharSequence carrierText;
653 public final CharSequence[] listOfCarriers;
654 public final boolean anySimReady;
655 public final int[] subscriptionIds;
Fabian Kozynskib38edbb2019-04-12 12:20:13 -0400656 public boolean airplaneMode;
Fabian Kozynski1823f112019-01-18 11:43:29 -0500657
Fabian Kozynskibf6fef32019-02-04 09:21:38 -0500658 @VisibleForTesting
659 public CarrierTextCallbackInfo(CharSequence carrierText, CharSequence[] listOfCarriers,
Fabian Kozynski1823f112019-01-18 11:43:29 -0500660 boolean anySimReady, int[] subscriptionIds) {
Fabian Kozynskib38edbb2019-04-12 12:20:13 -0400661 this(carrierText, listOfCarriers, anySimReady, subscriptionIds, false);
662 }
663
664 @VisibleForTesting
665 public CarrierTextCallbackInfo(CharSequence carrierText, CharSequence[] listOfCarriers,
666 boolean anySimReady, int[] subscriptionIds, boolean airplaneMode) {
Fabian Kozynski1823f112019-01-18 11:43:29 -0500667 this.carrierText = carrierText;
668 this.listOfCarriers = listOfCarriers;
669 this.anySimReady = anySimReady;
670 this.subscriptionIds = subscriptionIds;
Fabian Kozynskib38edbb2019-04-12 12:20:13 -0400671 this.airplaneMode = airplaneMode;
Fabian Kozynski1823f112019-01-18 11:43:29 -0500672 }
673 }
674
675 /**
Fabian Kozynski02941af2019-01-17 17:57:37 -0500676 * Callback to communicate to Views
677 */
678 public interface CarrierTextCallback {
679 /**
Fabian Kozynski1823f112019-01-18 11:43:29 -0500680 * Provides updated carrier information.
Fabian Kozynski02941af2019-01-17 17:57:37 -0500681 */
Fabian Kozynski1823f112019-01-18 11:43:29 -0500682 default void updateCarrierInfo(CarrierTextCallbackInfo info) {};
Fabian Kozynski02941af2019-01-17 17:57:37 -0500683
684 /**
685 * Notifies the View that the device is going to sleep
686 */
687 default void startedGoingToSleep() {};
688
689 /**
690 * Notifies the View that the device finished waking up
691 */
692 default void finishedWakingUp() {};
693 }
694}