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