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.TelephonyManager;
19 import android.telephony.UiccCardInfo;
20 
21 import java.util.List;
22 import java.util.Objects;
23 import java.util.concurrent.Callable;
24 import java.util.concurrent.atomic.AtomicIntegerArray;
25 
26 /**
27  * This is a Callable class which queries valid card ID for eSIM
28  */
29 public class QueryEsimCardId implements Callable<AtomicIntegerArray> {
30     private static final String TAG = "QueryEsimCardId";
31 
32     private TelephonyManager mTelephonyManager;
33 
34     /**
35      * Constructor of class
36      * @param TelephonyManager
37      */
QueryEsimCardId(TelephonyManager telephonyManager)38     public QueryEsimCardId(TelephonyManager telephonyManager) {
39         mTelephonyManager = telephonyManager;
40     }
41 
42     /**
43      * Implementation of Callable
44      * @return card ID(s) in AtomicIntegerArray
45      */
call()46     public AtomicIntegerArray call() {
47         List<UiccCardInfo> cardInfos = mTelephonyManager.getUiccCardsInfo();
48         if (cardInfos == null) {
49             return new AtomicIntegerArray(0);
50         }
51         return new AtomicIntegerArray(cardInfos.stream()
52                 .filter(Objects::nonNull)
53                 .filter(cardInfo -> (!cardInfo.isRemovable() && (cardInfo.getCardId() >= 0)))
54                 .mapToInt(UiccCardInfo::getCardId)
55                 .toArray());
56     }
57 }