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.content.Context;
19 import android.telephony.SubscriptionInfo;
20 import android.telephony.SubscriptionManager;
21 
22 import androidx.annotation.Keep;
23 import androidx.annotation.VisibleForTesting;
24 
25 import com.android.settings.network.SubscriptionUtil;
26 
27 import java.util.List;
28 
29 /**
30  * This is a class helps providing additional info required by UI
31  * based on SubscriptionInfo.
32  */
33 public class SubscriptionAnnotation {
34     private static final String TAG = "SubscriptionAnnotation";
35 
36     private SubscriptionInfo mSubInfo;
37     private int mType = TYPE_UNKNOWN;
38     private boolean mIsExisted;
39     private boolean mIsActive;
40     private boolean mIsAllowToDisplay;
41 
42     public static final int TYPE_UNKNOWN = 0x0;
43     public static final int TYPE_PSIM = 0x1;
44     public static final int TYPE_ESIM = 0x2;
45 
46     /**
47      * Builder class for SubscriptionAnnotation
48      */
49     public static class Builder {
50 
51         private final List<SubscriptionInfo> mSubInfoList;
52         private final int mIndexWithinList;
53 
54         /**
55          * Constructor of builder
56          * @param subInfoList list of subscription info
57          * @param indexWithinList target index within list provided
58          */
Builder(List<SubscriptionInfo> subInfoList, int indexWithinList)59         public Builder(List<SubscriptionInfo> subInfoList, int indexWithinList) {
60             mSubInfoList = subInfoList;
61             mIndexWithinList = indexWithinList;
62         }
63 
build(Context context, List<Integer> activeSimSlotIndex)64         public SubscriptionAnnotation build(Context context, List<Integer> activeSimSlotIndex) {
65             return new SubscriptionAnnotation(mSubInfoList, mIndexWithinList, context,
66                     activeSimSlotIndex);
67         }
68     }
69 
70     /**
71      * Constructor of class
72      */
73     @Keep
74     @VisibleForTesting
SubscriptionAnnotation(List<SubscriptionInfo> subInfoList, int subInfoIndex, Context context, List<Integer> activeSimSlotIndexList)75     protected SubscriptionAnnotation(List<SubscriptionInfo> subInfoList, int subInfoIndex,
76             Context context, List<Integer> activeSimSlotIndexList) {
77         if ((subInfoIndex < 0) || (subInfoIndex >= subInfoList.size())) {
78             return;
79         }
80         mSubInfo = subInfoList.get(subInfoIndex);
81         if (mSubInfo == null) {
82             return;
83         }
84 
85         mType = mSubInfo.isEmbedded() ? TYPE_ESIM : TYPE_PSIM;
86         mIsExisted = true;
87         if (mType == TYPE_ESIM) {
88             int cardId = mSubInfo.getCardId();
89             mIsActive = activeSimSlotIndexList.contains(mSubInfo.getSimSlotIndex());
90             mIsAllowToDisplay = (cardId < 0)    // always allow when eSIM not in slot
91                   || isDisplayAllowed(context);
92             return;
93         }
94 
95         mIsActive = (mSubInfo.getSimSlotIndex() > SubscriptionManager.INVALID_SIM_SLOT_INDEX)
96             && activeSimSlotIndexList.contains(mSubInfo.getSimSlotIndex());
97         mIsAllowToDisplay = isDisplayAllowed(context);
98     }
99 
100     // type of subscription
101     @Keep
getType()102     public int getType() {
103         return mType;
104     }
105 
106     // if a subscription is existed within device
107     @Keep
isExisted()108     public boolean isExisted() {
109         return mIsExisted;
110     }
111 
112     // if a subscription is currently ON
113     @Keep
isActive()114     public boolean isActive() {
115         return mIsActive;
116     }
117 
118     // if display of subscription is allowed
119     @Keep
isDisplayAllowed()120     public boolean isDisplayAllowed() {
121         return mIsAllowToDisplay;
122     }
123 
124     // the subscription ID
125     @Keep
getSubscriptionId()126     public int getSubscriptionId() {
127         return (mSubInfo == null) ? SubscriptionManager.INVALID_SUBSCRIPTION_ID :
128                 mSubInfo.getSubscriptionId();
129     }
130 
131     // the SubscriptionInfo
132     @Keep
getSubInfo()133     public SubscriptionInfo getSubInfo() {
134         return mSubInfo;
135     }
136 
isDisplayAllowed(Context context)137     private boolean isDisplayAllowed(Context context) {
138         return SubscriptionUtil.isSubscriptionVisible(
139                 context.getSystemService(SubscriptionManager.class), context, mSubInfo);
140     }
141 
toString()142     public String toString() {
143         return TAG + "{" + "subId=" + getSubscriptionId()
144                 + ",type=" + getType() + ",exist=" + isExisted()
145                 + ",active=" + isActive() + ",displayAllow=" + isDisplayAllowed()
146                 + "}";
147     }
148 }