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