1 /*
2  * Copyright (C) 2017 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.service.euicc;
17 
18 import android.annotation.IntDef;
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemApi;
22 import android.compat.annotation.UnsupportedAppUsage;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 import android.service.carrier.CarrierIdentifier;
26 import android.telephony.UiccAccessRule;
27 import android.text.TextUtils;
28 
29 import java.lang.annotation.Retention;
30 import java.lang.annotation.RetentionPolicy;
31 import java.util.Arrays;
32 import java.util.List;
33 import java.util.Objects;
34 
35 /**
36  * Information about an embedded profile (subscription) on an eUICC.
37  *
38  * @hide
39  */
40 @SystemApi
41 public final class EuiccProfileInfo implements Parcelable {
42 
43     /** Profile policy rules (bit mask) */
44     @Retention(RetentionPolicy.SOURCE)
45     @IntDef(flag = true, prefix = { "POLICY_RULE_" }, value = {
46             POLICY_RULE_DO_NOT_DISABLE,
47             POLICY_RULE_DO_NOT_DELETE,
48             POLICY_RULE_DELETE_AFTER_DISABLING
49     })
50     /** @hide */
51     public @interface PolicyRule {}
52     /** Once this profile is enabled, it cannot be disabled. */
53     public static final int POLICY_RULE_DO_NOT_DISABLE = 1;
54     /** This profile cannot be deleted. */
55     public static final int POLICY_RULE_DO_NOT_DELETE = 1 << 1;
56     /** This profile should be deleted after being disabled. */
57     public static final int POLICY_RULE_DELETE_AFTER_DISABLING = 1 << 2;
58 
59     /** Class of the profile */
60     @Retention(RetentionPolicy.SOURCE)
61     @IntDef(prefix = { "PROFILE_CLASS_" }, value = {
62             PROFILE_CLASS_TESTING,
63             PROFILE_CLASS_PROVISIONING,
64             PROFILE_CLASS_OPERATIONAL,
65             PROFILE_CLASS_UNSET
66     })
67     /** @hide */
68     public @interface ProfileClass {}
69     /** Testing profiles */
70     public static final int PROFILE_CLASS_TESTING = 0;
71     /** Provisioning profiles which are pre-loaded on eUICC */
72     public static final int PROFILE_CLASS_PROVISIONING = 1;
73     /** Operational profiles which can be pre-loaded or downloaded */
74     public static final int PROFILE_CLASS_OPERATIONAL = 2;
75     /**
76      * Profile class not set.
77      * @hide
78      */
79     public static final int PROFILE_CLASS_UNSET = -1;
80 
81     /** State of the profile */
82     @Retention(RetentionPolicy.SOURCE)
83     @IntDef(prefix = { "PROFILE_STATE_" }, value = {
84             PROFILE_STATE_DISABLED,
85             PROFILE_STATE_ENABLED,
86             PROFILE_STATE_UNSET
87     })
88     /** @hide */
89     public @interface ProfileState {}
90     /** Disabled profiles */
91     public static final int PROFILE_STATE_DISABLED = 0;
92     /** Enabled profile */
93     public static final int PROFILE_STATE_ENABLED = 1;
94     /**
95      * Profile state not set.
96      * @hide
97      */
98     public static final int PROFILE_STATE_UNSET = -1;
99 
100     /** The iccid of the subscription. */
101     private final String mIccid;
102 
103     /** An optional nickname for the subscription. */
104     private final @Nullable String mNickname;
105 
106     /** The service provider name for the subscription. */
107     private final String mServiceProviderName;
108 
109     /** The profile name for the subscription. */
110     private final String mProfileName;
111 
112     /** Profile class for the subscription. */
113     @ProfileClass private final int mProfileClass;
114 
115     /** The profile state of the subscription. */
116     @ProfileState private final int mState;
117 
118     /** The operator Id of the subscription. */
119     private final CarrierIdentifier mCarrierIdentifier;
120 
121     /** The policy rules of the subscription. */
122     @PolicyRule private final int mPolicyRules;
123 
124     /**
125      * Optional access rules defining which apps can manage this subscription. If unset, only the
126      * platform can manage it.
127      */
128     private final @Nullable UiccAccessRule[] mAccessRules;
129 
130     public static final @android.annotation.NonNull Creator<EuiccProfileInfo> CREATOR = new Creator<EuiccProfileInfo>() {
131         @Override
132         public EuiccProfileInfo createFromParcel(Parcel in) {
133             return new EuiccProfileInfo(in);
134         }
135 
136         @Override
137         public EuiccProfileInfo[] newArray(int size) {
138             return new EuiccProfileInfo[size];
139         }
140     };
141 
142     // TODO(b/70292228): Remove this method when LPA can be updated.
143     /**
144      * @hide
145      * @deprecated - Do not use.
146      */
147     @Deprecated
148     @UnsupportedAppUsage
EuiccProfileInfo(String iccid, @Nullable UiccAccessRule[] accessRules, @Nullable String nickname)149     public EuiccProfileInfo(String iccid, @Nullable UiccAccessRule[] accessRules,
150             @Nullable String nickname) {
151         if (!TextUtils.isDigitsOnly(iccid)) {
152             throw new IllegalArgumentException("iccid contains invalid characters: " + iccid);
153         }
154         this.mIccid = iccid;
155         this.mAccessRules = accessRules;
156         this.mNickname = nickname;
157 
158         this.mServiceProviderName = null;
159         this.mProfileName = null;
160         this.mProfileClass = PROFILE_CLASS_UNSET;
161         this.mState = PROFILE_STATE_UNSET;
162         this.mCarrierIdentifier = null;
163         this.mPolicyRules = 0;
164     }
165 
EuiccProfileInfo(Parcel in)166     private EuiccProfileInfo(Parcel in) {
167         mIccid = in.readString();
168         mNickname = in.readString();
169         mServiceProviderName = in.readString();
170         mProfileName = in.readString();
171         mProfileClass = in.readInt();
172         mState = in.readInt();
173         byte exist = in.readByte();
174         if (exist == (byte) 1) {
175             mCarrierIdentifier = CarrierIdentifier.CREATOR.createFromParcel(in);
176         } else {
177             mCarrierIdentifier = null;
178         }
179         mPolicyRules = in.readInt();
180         mAccessRules = in.createTypedArray(UiccAccessRule.CREATOR);
181     }
182 
183     @Override
writeToParcel(Parcel dest, int flags)184     public void writeToParcel(Parcel dest, int flags) {
185         dest.writeString(mIccid);
186         dest.writeString(mNickname);
187         dest.writeString(mServiceProviderName);
188         dest.writeString(mProfileName);
189         dest.writeInt(mProfileClass);
190         dest.writeInt(mState);
191         if (mCarrierIdentifier != null) {
192             dest.writeByte((byte) 1);
193             mCarrierIdentifier.writeToParcel(dest, flags);
194         } else {
195             dest.writeByte((byte) 0);
196         }
197         dest.writeInt(mPolicyRules);
198         dest.writeTypedArray(mAccessRules, flags);
199     }
200 
201     @Override
describeContents()202     public int describeContents() {
203         return 0;
204     }
205 
206     /** The builder to build a new {@link EuiccProfileInfo} instance. */
207     public static final class Builder {
208         private String mIccid;
209         private List<UiccAccessRule> mAccessRules;
210         private String mNickname;
211         private String mServiceProviderName;
212         private String mProfileName;
213         @ProfileClass private int mProfileClass;
214         @ProfileState private int mState;
215         private CarrierIdentifier mCarrierIdentifier;
216         @PolicyRule private int mPolicyRules;
217 
Builder(String value)218         public Builder(String value) {
219             if (!TextUtils.isDigitsOnly(value)) {
220                 throw new IllegalArgumentException("iccid contains invalid characters: " + value);
221             }
222             mIccid = value;
223         }
224 
Builder(EuiccProfileInfo baseProfile)225         public Builder(EuiccProfileInfo baseProfile) {
226             mIccid = baseProfile.mIccid;
227             mNickname = baseProfile.mNickname;
228             mServiceProviderName = baseProfile.mServiceProviderName;
229             mProfileName = baseProfile.mProfileName;
230             mProfileClass = baseProfile.mProfileClass;
231             mState = baseProfile.mState;
232             mCarrierIdentifier = baseProfile.mCarrierIdentifier;
233             mPolicyRules = baseProfile.mPolicyRules;
234             mAccessRules = Arrays.asList(baseProfile.mAccessRules);
235         }
236 
237         /** Builds the profile instance. */
build()238         public EuiccProfileInfo build() {
239             if (mIccid == null) {
240                 throw new IllegalStateException("ICCID must be set for a profile.");
241             }
242             return new EuiccProfileInfo(
243                     mIccid,
244                     mNickname,
245                     mServiceProviderName,
246                     mProfileName,
247                     mProfileClass,
248                     mState,
249                     mCarrierIdentifier,
250                     mPolicyRules,
251                     mAccessRules);
252         }
253 
254         /** Sets the iccId of the subscription. */
setIccid(String value)255         public Builder setIccid(String value) {
256             if (!TextUtils.isDigitsOnly(value)) {
257                 throw new IllegalArgumentException("iccid contains invalid characters: " + value);
258             }
259             mIccid = value;
260             return this;
261         }
262 
263         /** Sets the nickname of the subscription. */
setNickname(String value)264         public Builder setNickname(String value) {
265             mNickname = value;
266             return this;
267         }
268 
269         /** Sets the service provider name of the subscription. */
setServiceProviderName(String value)270         public Builder setServiceProviderName(String value) {
271             mServiceProviderName = value;
272             return this;
273         }
274 
275         /** Sets the profile name of the subscription. */
setProfileName(String value)276         public Builder setProfileName(String value) {
277             mProfileName = value;
278             return this;
279         }
280 
281         /** Sets the profile class of the subscription. */
setProfileClass(@rofileClass int value)282         public Builder setProfileClass(@ProfileClass int value) {
283             mProfileClass = value;
284             return this;
285         }
286 
287         /** Sets the state of the subscription. */
setState(@rofileState int value)288         public Builder setState(@ProfileState int value) {
289             mState = value;
290             return this;
291         }
292 
293         /** Sets the carrier identifier of the subscription. */
setCarrierIdentifier(CarrierIdentifier value)294         public Builder setCarrierIdentifier(CarrierIdentifier value) {
295             mCarrierIdentifier = value;
296             return this;
297         }
298 
299         /** Sets the policy rules of the subscription. */
setPolicyRules(@olicyRule int value)300         public Builder setPolicyRules(@PolicyRule int value) {
301             mPolicyRules = value;
302             return this;
303         }
304 
305         /** Sets the access rules of the subscription. */
setUiccAccessRule(@ullable List<UiccAccessRule> value)306         public Builder setUiccAccessRule(@Nullable List<UiccAccessRule> value) {
307             mAccessRules = value;
308             return this;
309         }
310     }
311 
EuiccProfileInfo( String iccid, @Nullable String nickname, String serviceProviderName, String profileName, @ProfileClass int profileClass, @ProfileState int state, CarrierIdentifier carrierIdentifier, @PolicyRule int policyRules, @Nullable List<UiccAccessRule> accessRules)312     private EuiccProfileInfo(
313             String iccid,
314             @Nullable String nickname,
315             String serviceProviderName,
316             String profileName,
317             @ProfileClass int profileClass,
318             @ProfileState int state,
319             CarrierIdentifier carrierIdentifier,
320             @PolicyRule int policyRules,
321             @Nullable List<UiccAccessRule> accessRules) {
322         this.mIccid = iccid;
323         this.mNickname = nickname;
324         this.mServiceProviderName = serviceProviderName;
325         this.mProfileName = profileName;
326         this.mProfileClass = profileClass;
327         this.mState = state;
328         this.mCarrierIdentifier = carrierIdentifier;
329         this.mPolicyRules = policyRules;
330         if (accessRules != null && accessRules.size() > 0) {
331             this.mAccessRules = accessRules.toArray(new UiccAccessRule[accessRules.size()]);
332         } else {
333             this.mAccessRules = null;
334         }
335     }
336 
337     /** Gets the ICCID string. */
getIccid()338     public String getIccid() {
339         return mIccid;
340     }
341 
342     /** Gets the access rules. */
343     @Nullable
getUiccAccessRules()344     public List<UiccAccessRule> getUiccAccessRules() {
345         if (mAccessRules == null) return null;
346         return Arrays.asList(mAccessRules);
347     }
348 
349     /** Gets the nickname. */
350     @Nullable
getNickname()351     public String getNickname() {
352         return mNickname;
353     }
354 
355     /** Gets the service provider name. */
getServiceProviderName()356     public String getServiceProviderName() {
357         return mServiceProviderName;
358     }
359 
360     /** Gets the profile name. */
getProfileName()361     public String getProfileName() {
362         return mProfileName;
363     }
364 
365     /** Gets the profile class. */
366     @ProfileClass
getProfileClass()367     public int getProfileClass() {
368         return mProfileClass;
369     }
370 
371     /** Gets the state of the subscription. */
372     @ProfileState
getState()373     public int getState() {
374         return mState;
375     }
376 
377     /** Gets the carrier identifier. */
getCarrierIdentifier()378     public CarrierIdentifier getCarrierIdentifier() {
379         return mCarrierIdentifier;
380     }
381 
382     /** Gets the policy rules. */
383     @PolicyRule
getPolicyRules()384     public int getPolicyRules() {
385         return mPolicyRules;
386     }
387 
388     /** Returns whether any policy rule exists. */
hasPolicyRules()389     public boolean hasPolicyRules() {
390         return mPolicyRules != 0;
391     }
392 
393     /** Checks whether a certain policy rule exists. */
hasPolicyRule(@olicyRule int policy)394     public boolean hasPolicyRule(@PolicyRule int policy) {
395         return (mPolicyRules & policy) != 0;
396     }
397 
398     @Override
equals(@ullable Object obj)399     public boolean equals(@Nullable Object obj) {
400         if (this == obj) {
401             return true;
402         }
403         if (obj == null || getClass() != obj.getClass()) {
404             return false;
405         }
406 
407         EuiccProfileInfo that = (EuiccProfileInfo) obj;
408         return Objects.equals(mIccid, that.mIccid)
409                 && Objects.equals(mNickname, that.mNickname)
410                 && Objects.equals(mServiceProviderName, that.mServiceProviderName)
411                 && Objects.equals(mProfileName, that.mProfileName)
412                 && mProfileClass == that.mProfileClass
413                 && mState == that.mState
414                 && Objects.equals(mCarrierIdentifier, that.mCarrierIdentifier)
415                 && mPolicyRules == that.mPolicyRules
416                 && Arrays.equals(mAccessRules, that.mAccessRules);
417     }
418 
419     @Override
hashCode()420     public int hashCode() {
421         int result = 1;
422         result = 31 * result + Objects.hashCode(mIccid);
423         result = 31 * result + Objects.hashCode(mNickname);
424         result = 31 * result + Objects.hashCode(mServiceProviderName);
425         result = 31 * result + Objects.hashCode(mProfileName);
426         result = 31 * result + mProfileClass;
427         result = 31 * result + mState;
428         result = 31 * result + Objects.hashCode(mCarrierIdentifier);
429         result = 31 * result + mPolicyRules;
430         result = 31 * result + Arrays.hashCode(mAccessRules);
431         return result;
432     }
433 
434     @NonNull
435     @Override
toString()436     public String toString() {
437         return "EuiccProfileInfo (nickname="
438                 + mNickname
439                 + ", serviceProviderName="
440                 + mServiceProviderName
441                 + ", profileName="
442                 + mProfileName
443                 + ", profileClass="
444                 + mProfileClass
445                 + ", state="
446                 + mState
447                 + ", CarrierIdentifier="
448                 + mCarrierIdentifier
449                 + ", policyRules="
450                 + mPolicyRules
451                 + ", accessRules="
452                 + Arrays.toString(mAccessRules)
453                 + ")";
454     }
455 }
456