• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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 
17 package com.android.server.nearby.common.bluetooth.fastpair;
18 
19 import android.bluetooth.BluetoothDevice;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 
23 import com.android.server.nearby.intdefs.NearbyEventIntDefs.EventCode;
24 
25 import java.util.Objects;
26 
27 import javax.annotation.Nullable;
28 
29 /**
30  * Describes events that are happening during fast pairing. EventCode is required, everything else
31  * is optional.
32  */
33 public class Event implements Parcelable {
34 
35     private final @EventCode int mEventCode;
36     private final long mTimestamp;
37     private final Short mProfile;
38     private final BluetoothDevice mBluetoothDevice;
39     private final Exception mException;
40 
Event(@ventCode int eventCode, long timestamp, @Nullable Short profile, @Nullable BluetoothDevice bluetoothDevice, @Nullable Exception exception)41     private Event(@EventCode int eventCode, long timestamp, @Nullable Short profile,
42             @Nullable BluetoothDevice bluetoothDevice, @Nullable Exception exception) {
43         mEventCode = eventCode;
44         mTimestamp = timestamp;
45         mProfile = profile;
46         mBluetoothDevice = bluetoothDevice;
47         mException = exception;
48     }
49 
50     /**
51      * Returns event code.
52      */
getEventCode()53     public @EventCode int getEventCode() {
54         return mEventCode;
55     }
56 
57     /**
58      * Returns timestamp.
59      */
getTimestamp()60     public long getTimestamp() {
61         return mTimestamp;
62     }
63 
64     /**
65      * Returns profile.
66      */
67     @Nullable
getProfile()68     public Short getProfile() {
69         return mProfile;
70     }
71 
72     /**
73      * Returns Bluetooth device.
74      */
75     @Nullable
getBluetoothDevice()76     public BluetoothDevice getBluetoothDevice() {
77         return mBluetoothDevice;
78     }
79 
80     /**
81      * Returns exception.
82      */
83     @Nullable
getException()84     public Exception getException() {
85         return mException;
86     }
87 
88     /**
89      * Returns whether profile is not null.
90      */
hasProfile()91     public boolean hasProfile() {
92         return getProfile() != null;
93     }
94 
95     /**
96      * Returns whether Bluetooth device is not null.
97      */
hasBluetoothDevice()98     public boolean hasBluetoothDevice() {
99         return getBluetoothDevice() != null;
100     }
101 
102     /**
103      * Returns a builder.
104      */
builder()105     public static Builder builder() {
106         return new Event.Builder();
107     }
108 
109     /**
110      * Returns whether it fails.
111      */
isFailure()112     public boolean isFailure() {
113         return getException() != null;
114     }
115 
116     @Override
toString()117     public String toString() {
118         return "Event{"
119                 + "eventCode=" + mEventCode + ", "
120                 + "timestamp=" + mTimestamp + ", "
121                 + "profile=" + mProfile + ", "
122                 + "bluetoothDevice=" + mBluetoothDevice + ", "
123                 + "exception=" + mException
124                 + "}";
125     }
126 
127     @Override
equals(@ullable Object o)128     public boolean equals(@Nullable Object o) {
129         if (o == this) {
130             return true;
131         }
132         if (o instanceof Event) {
133             Event that = (Event) o;
134             return this.mEventCode == that.getEventCode()
135                     && this.mTimestamp == that.getTimestamp()
136                     && (this.mBluetoothDevice == null
137                     ? that.getBluetoothDevice() == null :
138                     this.mBluetoothDevice.equals(that.getBluetoothDevice()))
139                     && (this.mProfile == null
140                     ? that.getProfile() == null : this.mProfile.equals(that.getProfile()));
141         }
142         return false;
143     }
144 
145     @Override
hashCode()146     public int hashCode() {
147         return Objects.hash(mEventCode, mTimestamp, mProfile, mBluetoothDevice, mException);
148     }
149 
150     /**
151      * Builder
152      */
153     public static class Builder {
154         private @EventCode int mEventCode;
155         private long mTimestamp;
156         private Short mProfile;
157         private BluetoothDevice mBluetoothDevice;
158         private Exception mException;
159 
160         /**
161          * Set event code.
162          */
setEventCode(@ventCode int eventCode)163         public Builder setEventCode(@EventCode int eventCode) {
164             this.mEventCode = eventCode;
165             return this;
166         }
167 
168         /**
169          * Set timestamp.
170          */
setTimestamp(long timestamp)171         public Builder setTimestamp(long timestamp) {
172             this.mTimestamp = timestamp;
173             return this;
174         }
175 
176         /**
177          * Set profile.
178          */
setProfile(@ullable Short profile)179         public Builder setProfile(@Nullable Short profile) {
180             this.mProfile = profile;
181             return this;
182         }
183 
184         /**
185          * Set Bluetooth device.
186          */
setBluetoothDevice(@ullable BluetoothDevice device)187         public Builder setBluetoothDevice(@Nullable BluetoothDevice device) {
188             this.mBluetoothDevice = device;
189             return this;
190         }
191 
192         /**
193          * Set exception.
194          */
setException(@ullable Exception exception)195         public Builder setException(@Nullable Exception exception) {
196             this.mException = exception;
197             return this;
198         }
199 
200         /**
201          * Builds event.
202          */
build()203         public Event build() {
204             return new Event(mEventCode, mTimestamp, mProfile, mBluetoothDevice, mException);
205         }
206     }
207 
208     @Override
writeToParcel(Parcel dest, int flags)209     public final void writeToParcel(Parcel dest, int flags) {
210         dest.writeInt(getEventCode());
211         dest.writeLong(getTimestamp());
212         dest.writeValue(getProfile());
213         dest.writeParcelable(getBluetoothDevice(), 0);
214         dest.writeSerializable(getException());
215     }
216 
217     @Override
describeContents()218     public final int describeContents() {
219         return 0;
220     }
221 
222     /**
223      * Event Creator instance.
224      */
225     public static final Creator<Event> CREATOR =
226             new Creator<Event>() {
227                 @Override
228                 /** Creates Event from Parcel. */
229                 public Event createFromParcel(Parcel in) {
230                     return Event.builder()
231                             .setEventCode(in.readInt())
232                             .setTimestamp(in.readLong())
233                             .setProfile((Short) in.readValue(Short.class.getClassLoader()))
234                             .setBluetoothDevice(
235                                     in.readParcelable(BluetoothDevice.class.getClassLoader()))
236                             .setException((Exception) in.readSerializable())
237                             .build();
238                 }
239 
240                 @Override
241                 /** Returns Event array. */
242                 public Event[] newArray(int size) {
243                     return new Event[size];
244                 }
245             };
246 }
247