EuiccNotification.java revision 47a7426ddd9baf14307ad396198163daf6f409c7
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 */
16package android.telephony.euicc;
17
18import android.annotation.IntDef;
19import android.annotation.Nullable;
20import android.os.Parcel;
21import android.os.Parcelable;
22
23import java.lang.annotation.Retention;
24import java.lang.annotation.RetentionPolicy;
25import java.util.Arrays;
26import java.util.Objects;
27
28/**
29 * This represents a signed notification which is defined in SGP.22. It can be either a profile
30 * installation result or a notification generated for profile operations (e.g., enabling,
31 * disabling, or deleting).
32 *
33 * @hide
34 *
35 * TODO(b/35851809): Make this a @SystemApi.
36 */
37public class EuiccNotification implements Parcelable {
38    /** Event */
39    @Retention(RetentionPolicy.SOURCE)
40    @IntDef(flag = true, prefix = { "EVENT_" }, value = {
41            EVENT_INSTALL,
42            EVENT_ENABLE,
43            EVENT_DISABLE,
44            EVENT_DELETE
45    })
46    public @interface Event {}
47
48    /** A profile is downloaded and installed. */
49    public static final int EVENT_INSTALL = 1;
50
51    /** A profile is enabled. */
52    public static final int EVENT_ENABLE = 1 << 1;
53
54    /** A profile is disabled. */
55    public static final int EVENT_DISABLE = 1 << 2;
56
57    /** A profile is deleted. */
58    public static final int EVENT_DELETE = 1 << 3;
59
60    /** Value of the bits of all above events */
61    @Event
62    public static final int ALL_EVENTS =
63            EVENT_INSTALL | EVENT_ENABLE | EVENT_DISABLE | EVENT_DELETE;
64
65    private final int mSeq;
66    private final String mTargetAddr;
67    @Event private final int mEvent;
68    @Nullable private final byte[] mData;
69
70    /**
71     * Creates an instance.
72     *
73     * @param seq The sequence number of this notification.
74     * @param targetAddr The target server where to send this notification.
75     * @param event The event which causes this notification.
76     * @param data The data which needs to be sent to the target server. This can be null for
77     *     building a list of notification metadata without data.
78     */
79    public EuiccNotification(int seq, String targetAddr, @Event int event, @Nullable byte[] data) {
80        mSeq = seq;
81        mTargetAddr = targetAddr;
82        mEvent = event;
83        mData = data;
84    }
85
86    /** @return The sequence number of this notification. */
87    public int getSeq() {
88        return mSeq;
89    }
90
91    /** @return The target server address where this notification should be sent to. */
92    public String getTargetAddr() {
93        return mTargetAddr;
94    }
95
96    /** @return The event of this notification. */
97    @Event
98    public int getEvent() {
99        return mEvent;
100    }
101
102    /** @return The notification data which needs to be sent to the target server. */
103    @Nullable
104    public byte[] getData() {
105        return mData;
106    }
107
108    @Override
109    public boolean equals(Object obj) {
110        if (this == obj) {
111            return true;
112        }
113        if (obj == null || getClass() != obj.getClass()) {
114            return false;
115        }
116
117        EuiccNotification that = (EuiccNotification) obj;
118        return mSeq == that.mSeq
119                && Objects.equals(mTargetAddr, that.mTargetAddr)
120                && mEvent == that.mEvent
121                && Arrays.equals(mData, that.mData);
122    }
123
124    @Override
125    public int hashCode() {
126        int result = 1;
127        result = 31 * result + mSeq;
128        result = 31 * result + Objects.hashCode(mTargetAddr);
129        result = 31 * result + mEvent;
130        result = 31 * result + Arrays.hashCode(mData);
131        return result;
132    }
133
134    @Override
135    public String toString() {
136        return "EuiccNotification (seq="
137                + mSeq
138                + ", targetAddr="
139                + mTargetAddr
140                + ", event="
141                + mEvent
142                + ", data="
143                + (mData == null ? "null" : "byte[" + mData.length + "]")
144                + ")";
145    }
146
147    @Override
148    public int describeContents() {
149        return 0;
150    }
151
152    @Override
153    public void writeToParcel(Parcel dest, int flags) {
154        dest.writeInt(mSeq);
155        dest.writeString(mTargetAddr);
156        dest.writeInt(mEvent);
157        dest.writeByteArray(mData);
158    }
159
160    private EuiccNotification(Parcel source) {
161        mSeq = source.readInt();
162        mTargetAddr = source.readString();
163        mEvent = source.readInt();
164        mData = source.createByteArray();
165    }
166
167    public static final Creator<EuiccNotification> CREATOR =
168            new Creator<EuiccNotification>() {
169                @Override
170                public EuiccNotification createFromParcel(Parcel source) {
171                    return new EuiccNotification(source);
172                }
173
174                @Override
175                public EuiccNotification[] newArray(int size) {
176                    return new EuiccNotification[size];
177                }
178            };
179}
180