1 /* 2 * Copyright (C) 2018 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 17 package com.android.settings.network; 18 19 import static android.telephony.UiccSlotInfo.CARD_STATE_INFO_PRESENT; 20 21 import static com.android.internal.util.CollectionUtils.emptyIfNull; 22 23 import android.content.Context; 24 import android.telephony.SubscriptionInfo; 25 import android.telephony.SubscriptionManager; 26 import android.telephony.TelephonyManager; 27 import android.telephony.UiccSlotInfo; 28 29 import androidx.annotation.VisibleForTesting; 30 31 import java.util.ArrayList; 32 import java.util.List; 33 34 public class SubscriptionUtil { 35 private static final String TAG = "SubscriptionUtil"; 36 private static List<SubscriptionInfo> sAvailableResultsForTesting; 37 private static List<SubscriptionInfo> sActiveResultsForTesting; 38 39 @VisibleForTesting setAvailableSubscriptionsForTesting(List<SubscriptionInfo> results)40 public static void setAvailableSubscriptionsForTesting(List<SubscriptionInfo> results) { 41 sAvailableResultsForTesting = results; 42 } 43 44 @VisibleForTesting setActiveSubscriptionsForTesting(List<SubscriptionInfo> results)45 public static void setActiveSubscriptionsForTesting(List<SubscriptionInfo> results) { 46 sActiveResultsForTesting = results; 47 } 48 getActiveSubscriptions(SubscriptionManager manager)49 public static List<SubscriptionInfo> getActiveSubscriptions(SubscriptionManager manager) { 50 if (sActiveResultsForTesting != null) { 51 return sActiveResultsForTesting; 52 } 53 final List<SubscriptionInfo> subscriptions = manager.getActiveSubscriptionInfoList(true); 54 if (subscriptions == null) { 55 return new ArrayList<>(); 56 } 57 return subscriptions; 58 } 59 60 @VisibleForTesting isInactiveInsertedPSim(UiccSlotInfo slotInfo)61 static boolean isInactiveInsertedPSim(UiccSlotInfo slotInfo) { 62 if (slotInfo == null) { 63 return false; 64 } 65 return !slotInfo.getIsEuicc() && !slotInfo.getIsActive() && 66 slotInfo.getCardStateInfo() == CARD_STATE_INFO_PRESENT; 67 } 68 getAvailableSubscriptions(Context context)69 public static List<SubscriptionInfo> getAvailableSubscriptions(Context context) { 70 if (sAvailableResultsForTesting != null) { 71 return sAvailableResultsForTesting; 72 } 73 final SubscriptionManager subMgr = context.getSystemService(SubscriptionManager.class); 74 final TelephonyManager telMgr = context.getSystemService(TelephonyManager.class); 75 76 List<SubscriptionInfo> subscriptions = 77 new ArrayList<>(emptyIfNull(subMgr.getSelectableSubscriptionInfoList())); 78 79 // Look for inactive but present physical SIMs that are missing from the selectable list. 80 final List<UiccSlotInfo> missing = new ArrayList<>(); 81 UiccSlotInfo[] slotsInfo = telMgr.getUiccSlotsInfo(); 82 for (int i = 0; slotsInfo != null && i < slotsInfo.length; i++) { 83 final UiccSlotInfo slotInfo = slotsInfo[i]; 84 if (isInactiveInsertedPSim(slotInfo)) { 85 final int index = slotInfo.getLogicalSlotIdx(); 86 final String cardId = slotInfo.getCardId(); 87 88 final boolean found = subscriptions.stream().anyMatch(info -> 89 index == info.getSimSlotIndex() && cardId.equals(info.getCardString())); 90 if (!found) { 91 missing.add(slotInfo); 92 } 93 } 94 } 95 if (!missing.isEmpty()) { 96 for (SubscriptionInfo info : subMgr.getAllSubscriptionInfoList()) { 97 for (UiccSlotInfo slotInfo : missing) { 98 if (info.getSimSlotIndex() == slotInfo.getLogicalSlotIdx() && 99 info.getCardString().equals(slotInfo.getCardId())) { 100 subscriptions.add(info); 101 break; 102 } 103 } 104 } 105 } 106 return subscriptions; 107 } 108 getDisplayName(SubscriptionInfo info)109 public static String getDisplayName(SubscriptionInfo info) { 110 final CharSequence name = info.getDisplayName(); 111 if (name != null) { 112 return name.toString(); 113 } 114 return ""; 115 } 116 } 117