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