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 
17 package com.android.settings.accessibility.rtt;
18 
19 import android.content.Context;
20 import android.telecom.PhoneAccountHandle;
21 import android.telecom.TelecomManager;
22 import android.telephony.SubscriptionInfo;
23 import android.telephony.SubscriptionManager;
24 import android.text.TextUtils;
25 import android.util.Log;
26 
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Optional;
30 
31 /**
32  * A util class checks some SIM card information and permissions.
33  */
34 public abstract class TelecomUtil {
35 
36     private static final String TAG = "TelecomUtil";
37 
38     /** Get a list of phone accounts which are call capable. */
getCallCapablePhoneAccounts(Context context)39     public static List<PhoneAccountHandle> getCallCapablePhoneAccounts(Context context) {
40         return Optional.ofNullable(getTelecomManager(context).getCallCapablePhoneAccounts())
41                     .orElse(new ArrayList<>());
42     }
43 
44     /** Returns a {@link TelecomManager} instance. */
getTelecomManager(Context context)45     public static TelecomManager getTelecomManager(Context context) {
46         return context.getApplicationContext().getSystemService(TelecomManager.class);
47     }
48 
49     /** Returns a subscription id of the SIM. */
getSubIdForPhoneAccountHandle( Context context, PhoneAccountHandle phoneAccountHandle)50     public static int getSubIdForPhoneAccountHandle(
51             Context context, PhoneAccountHandle phoneAccountHandle) {
52         Optional<SubscriptionInfo> info = getSubscriptionInfo(context, phoneAccountHandle);
53         return info.map(SubscriptionInfo::getSubscriptionId)
54                 .orElse(SubscriptionManager.INVALID_SUBSCRIPTION_ID);
55     }
56 
57     /**
58      * @return the {@link SubscriptionInfo} of the SIM if {@code phoneAccountHandle} corresponds
59      * to a valid SIM. Absent otherwise.
60      */
getSubscriptionInfo( Context context, PhoneAccountHandle phoneAccountHandle)61     private static Optional<SubscriptionInfo> getSubscriptionInfo(
62             Context context, PhoneAccountHandle phoneAccountHandle) {
63         if (TextUtils.isEmpty(phoneAccountHandle.getId())) {
64             return Optional.empty();
65         }
66         SubscriptionManager subscriptionManager = context.getSystemService(
67                 SubscriptionManager.class).createForAllUserProfiles();
68         List<SubscriptionInfo> subscriptionInfos =
69                 subscriptionManager.getActiveSubscriptionInfoList();
70         if (subscriptionInfos == null) {
71             return Optional.empty();
72         }
73         for (SubscriptionInfo info : subscriptionInfos) {
74             if (phoneAccountHandle.getId().startsWith(info.getIccId())) {
75                 return Optional.of(info);
76             }
77         }
78         Log.d(TAG, "Failed to find SubscriptionInfo for phoneAccountHandle");
79         return Optional.empty();
80     }
81 }
82