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