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