1 /*
2  * Copyright (C) 2018 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 android.telephony;
17 
18 import android.annotation.NonNull;
19 import android.annotation.Nullable;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 
23 import java.util.Objects;
24 
25 /**
26  * The UiccCardInfo represents information about a currently inserted UICC or embedded eUICC.
27  */
28 public final class UiccCardInfo implements Parcelable {
29 
30     private final boolean mIsEuicc;
31     private final int mCardId;
32     private final String mEid;
33     private final String mIccId;
34     private final int mSlotIndex;
35     private final boolean mIsRemovable;
36 
37     public static final @android.annotation.NonNull Creator<UiccCardInfo> CREATOR = new Creator<UiccCardInfo>() {
38         @Override
39         public UiccCardInfo createFromParcel(Parcel in) {
40             return new UiccCardInfo(in);
41         }
42 
43         @Override
44         public UiccCardInfo[] newArray(int size) {
45             return new UiccCardInfo[size];
46         }
47     };
48 
UiccCardInfo(Parcel in)49     private UiccCardInfo(Parcel in) {
50         mIsEuicc = in.readByte() != 0;
51         mCardId = in.readInt();
52         mEid = in.readString();
53         mIccId = in.readString();
54         mSlotIndex = in.readInt();
55         mIsRemovable = in.readByte() != 0;
56     }
57 
58     @Override
writeToParcel(Parcel dest, int flags)59     public void writeToParcel(Parcel dest, int flags) {
60         dest.writeByte((byte) (mIsEuicc ? 1 : 0));
61         dest.writeInt(mCardId);
62         dest.writeString(mEid);
63         dest.writeString(mIccId);
64         dest.writeInt(mSlotIndex);
65         dest.writeByte((byte) (mIsRemovable ? 1 : 0));
66     }
67 
68     @Override
describeContents()69     public int describeContents() {
70         return 0;
71     }
72 
73     /**
74      * @hide
75      */
UiccCardInfo(boolean isEuicc, int cardId, String eid, String iccId, int slotIndex, boolean isRemovable)76     public UiccCardInfo(boolean isEuicc, int cardId, String eid, String iccId, int slotIndex,
77             boolean isRemovable) {
78         this.mIsEuicc = isEuicc;
79         this.mCardId = cardId;
80         this.mEid = eid;
81         this.mIccId = iccId;
82         this.mSlotIndex = slotIndex;
83         this.mIsRemovable = isRemovable;
84     }
85 
86     /**
87      * Return whether the UICC is an eUICC.
88      * @return true if the UICC is an eUICC.
89      */
isEuicc()90     public boolean isEuicc() {
91         return mIsEuicc;
92     }
93 
94     /**
95      * Get the card ID of the UICC. See {@link TelephonyManager#getCardIdForDefaultEuicc()} for more
96      * details on card ID.
97      */
getCardId()98     public int getCardId() {
99         return mCardId;
100     }
101 
102     /**
103      * Get the embedded ID (EID) of the eUICC. If the UiccCardInfo is not an eUICC
104      * (see {@link #isEuicc()}), or the EID is not available, returns null.
105      * <p>
106      * Note that this field may be omitted if the caller does not have the correct permissions
107      * (see {@link TelephonyManager#getUiccCardsInfo()}).
108      */
109     @Nullable
getEid()110     public String getEid() {
111         if (!mIsEuicc) {
112             return null;
113         }
114         return mEid;
115     }
116 
117     /**
118      * Get the ICCID of the UICC. If the ICCID is not availble, returns null.
119      * <p>
120      * Note that this field may be omitted if the caller does not have the correct permissions
121      * (see {@link TelephonyManager#getUiccCardsInfo()}).
122      */
123     @Nullable
getIccId()124     public String getIccId() {
125         return mIccId;
126     }
127 
128     /**
129      * Gets the slot index for the slot that the UICC is currently inserted in.
130      */
getSlotIndex()131     public int getSlotIndex() {
132         return mSlotIndex;
133     }
134 
135     /**
136      * Returns a copy of the UiccCardinfo with the EID and ICCID set to null. These values are
137      * generally private and require carrier privileges to view.
138      *
139      * @hide
140      */
141     @NonNull
getUnprivileged()142     public UiccCardInfo getUnprivileged() {
143         return new UiccCardInfo(mIsEuicc, mCardId, null, null, mSlotIndex, mIsRemovable);
144     }
145 
146     /**
147      * Return whether the UICC or eUICC is removable.
148      * <p>
149      * UICCs are generally removable, but eUICCs may be removable or built in to the device.
150      * @return true if the UICC or eUICC is removable
151      */
isRemovable()152     public boolean isRemovable() {
153         return mIsRemovable;
154     }
155 
156     @Override
equals(Object obj)157     public boolean equals(Object obj) {
158         if (this == obj) {
159             return true;
160         }
161         if (obj == null || getClass() != obj.getClass()) {
162             return false;
163         }
164 
165         UiccCardInfo that = (UiccCardInfo) obj;
166         return ((mIsEuicc == that.mIsEuicc)
167                 && (mCardId == that.mCardId)
168                 && (Objects.equals(mEid, that.mEid))
169                 && (Objects.equals(mIccId, that.mIccId))
170                 && (mSlotIndex == that.mSlotIndex)
171                 && (mIsRemovable == that.mIsRemovable));
172     }
173 
174     @Override
hashCode()175     public int hashCode() {
176         return Objects.hash(mIsEuicc, mCardId, mEid, mIccId, mSlotIndex, mIsRemovable);
177     }
178 
179     @Override
toString()180     public String toString() {
181         return "UiccCardInfo (mIsEuicc="
182                 + mIsEuicc
183                 + ", mCardId="
184                 + mCardId
185                 + ", mEid="
186                 + mEid
187                 + ", mIccId="
188                 + mIccId
189                 + ", mSlotIndex="
190                 + mSlotIndex
191                 + ", mIsRemovable="
192                 + mIsRemovable
193                 + ")";
194     }
195 }
196