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.Nullable;
19import android.annotation.SystemApi;
20import android.os.Parcel;
21import android.os.Parcelable;
22
23import java.util.Arrays;
24import java.util.List;
25
26/**
27 * Result of a {@link EuiccService#onGetEuiccProfileInfoList} operation.
28 * @hide
29 */
30@SystemApi
31public final class GetEuiccProfileInfoListResult implements Parcelable {
32
33    public static final Creator<GetEuiccProfileInfoListResult> CREATOR =
34            new Creator<GetEuiccProfileInfoListResult>() {
35                @Override
36                public GetEuiccProfileInfoListResult createFromParcel(Parcel in) {
37                    return new GetEuiccProfileInfoListResult(in);
38                }
39
40                @Override
41                public GetEuiccProfileInfoListResult[] newArray(int size) {
42                    return new GetEuiccProfileInfoListResult[size];
43                }
44            };
45
46    /**
47     * @hide
48     * @deprecated - Do no use. Use getResult() instead.
49     */
50    @Deprecated
51    public final int result;
52
53    @Nullable
54    private final EuiccProfileInfo[] mProfiles;
55
56    private final boolean mIsRemovable;
57
58    /**
59     * Gets the result of the operation.
60     *
61     * <p>May be one of the predefined {@code RESULT_} constants in EuiccService or any
62     * implementation-specific code starting with {@link EuiccService#RESULT_FIRST_USER}.
63     */
64    public int getResult() {
65        return result;
66    }
67
68    /** Gets the profile list (only upon success). */
69    @Nullable
70    public List<EuiccProfileInfo> getProfiles() {
71        if (mProfiles == null) return null;
72        return Arrays.asList(mProfiles);
73    }
74
75    /** Gets whether the eUICC is removable. */
76    public boolean getIsRemovable() {
77        return mIsRemovable;
78    }
79
80    /**
81     * Construct a new {@link GetEuiccProfileInfoListResult}.
82     *
83     * @param result Result of the operation. May be one of the predefined {@code RESULT_} constants
84     *     in EuiccService or any implementation-specific code starting with
85     *     {@link EuiccService#RESULT_FIRST_USER}.
86     * @param profiles the list of profiles. Should only be provided if the result is
87     *     {@link EuiccService#RESULT_OK}.
88     * @param isRemovable whether the eUICC in this slot is removable. If true, the profiles
89     *     returned here will only be considered accessible as long as this eUICC is present.
90     *     Otherwise, they will remain accessible until the next time a response with isRemovable
91     *     set to false is returned.
92     */
93    public GetEuiccProfileInfoListResult(
94            int result, @Nullable EuiccProfileInfo[] profiles, boolean isRemovable) {
95        this.result = result;
96        this.mIsRemovable = isRemovable;
97        if (this.result == EuiccService.RESULT_OK) {
98            this.mProfiles = profiles;
99        } else {
100            if (profiles != null) {
101                throw new IllegalArgumentException(
102                        "Error result with non-null profiles: " + result);
103            }
104            this.mProfiles = null;
105        }
106    }
107
108    private GetEuiccProfileInfoListResult(Parcel in) {
109        this.result = in.readInt();
110        this.mProfiles = in.createTypedArray(EuiccProfileInfo.CREATOR);
111        this.mIsRemovable = in.readBoolean();
112    }
113
114    @Override
115    public void writeToParcel(Parcel dest, int flags) {
116        dest.writeInt(result);
117        dest.writeTypedArray(mProfiles, flags);
118        dest.writeBoolean(mIsRemovable);
119    }
120
121    @Override
122    public int describeContents() {
123        return 0;
124    }
125}
126