1 /*
2  * Copyright (C) 2021 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 package com.android.settings.network.helper;
17 
18 import android.telephony.SubscriptionManager;
19 import android.telephony.TelephonyManager;
20 import android.telephony.UiccSlotInfo;
21 
22 import java.util.Arrays;
23 import java.util.concurrent.Callable;
24 import java.util.concurrent.atomic.AtomicIntegerArray;
25 
26 /**
27  * This is a Callable class which query slot index within device
28  */
29 public class QuerySimSlotIndex implements Callable<AtomicIntegerArray> {
30     private static final String TAG = "QuerySimSlotIndex";
31 
32     private TelephonyManager mTelephonyManager;
33     private boolean mDisabledSlotsIncluded;
34     private boolean mOnlySlotWithSim;
35 
36     /**
37      * Constructor of class
38      * @param TelephonyManager
39      * @param disabledSlotsIncluded query both active and inactive slots when true,
40      *                              only query active slot when false.
41      * @param onlySlotWithSim query slot index with SIM available when true,
42      *                        include absent ones when false.
43      */
QuerySimSlotIndex(TelephonyManager telephonyManager, boolean disabledSlotsIncluded, boolean onlySlotWithSim)44     public QuerySimSlotIndex(TelephonyManager telephonyManager,
45             boolean disabledSlotsIncluded, boolean onlySlotWithSim) {
46         mTelephonyManager = telephonyManager;
47         mDisabledSlotsIncluded = disabledSlotsIncluded;
48         mOnlySlotWithSim = onlySlotWithSim;
49     }
50 
51     /**
52      * Implementation of Callable
53      * @return slot index in AtomicIntegerArray
54      */
call()55     public AtomicIntegerArray call() {
56         UiccSlotInfo [] slotInfo = mTelephonyManager.getUiccSlotsInfo();
57         if (slotInfo == null) {
58             return new AtomicIntegerArray(0);
59         }
60         int slotIndexFilter = mOnlySlotWithSim ? 0 : SubscriptionManager.INVALID_SIM_SLOT_INDEX;
61         return new AtomicIntegerArray(Arrays.stream(slotInfo)
62                 .filter(slot -> filterSlot(slot))
63                 .mapToInt(slot -> mapToSlotIndex(slot))
64                 .filter(slotIndex -> (slotIndex >= slotIndexFilter))
65                 .toArray());
66     }
67 
filterSlot(UiccSlotInfo slotInfo)68     protected boolean filterSlot(UiccSlotInfo slotInfo) {
69         if (mDisabledSlotsIncluded) {
70             return true;
71         }
72         if (slotInfo == null) {
73             return false;
74         }
75         return slotInfo.getIsActive();
76     }
77 
mapToSlotIndex(UiccSlotInfo slotInfo)78     protected int mapToSlotIndex(UiccSlotInfo slotInfo) {
79         if (slotInfo == null) {
80             return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
81         }
82         if (slotInfo.getCardStateInfo() == UiccSlotInfo.CARD_STATE_INFO_ABSENT) {
83             return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
84         }
85         return slotInfo.getLogicalSlotIdx();
86     }
87 }