Add Event.java
Test: unit test
Bug: 200231384
Change-Id: I3c99f8ddfeb26858dcf8522d474df4b89c2cc588
diff --git a/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/Event.java b/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/Event.java
new file mode 100644
index 0000000..c867aaa
--- /dev/null
+++ b/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/Event.java
@@ -0,0 +1,167 @@
+/*
+ * Copyright 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.nearby.common.bluetooth.fastpair;
+
+import android.bluetooth.BluetoothDevice;
+import android.os.Parcel;
+import android.os.Parcelable;
+
+import com.android.server.nearby.proto.NearbyEventCodes.NearbyEvent.EventCode;
+
+import com.google.auto.value.AutoValue;
+
+import javax.annotation.Nullable;
+
+/**
+ * Describes events that are happening during fast pairing. EventCode is required, everything else
+ * is optional.
+ */
+@AutoValue
+public abstract class Event implements Parcelable {
+
+ /**
+ * Returns event code.
+ */
+ public abstract EventCode getEventCode();
+
+ /**
+ * Returns timestamp.
+ */
+ public abstract long getTimestamp();
+
+ /**
+ * Returns profile.
+ */
+ @Nullable
+ public abstract Short getProfile();
+
+ /**
+ * Returns Bluetooth device.
+ */
+ @Nullable
+ public abstract BluetoothDevice getBluetoothDevice();
+
+ /**
+ * Returns exception.
+ */
+ @Nullable
+ public abstract Exception getException();
+
+ /**
+ * Returns whether profile is not null.
+ */
+ public boolean hasProfile() {
+ return getProfile() != null;
+ }
+
+ /**
+ * Returns whether Bluetooth device is not null.
+ */
+ public boolean hasBluetoothDevice() {
+ return getBluetoothDevice() != null;
+ }
+
+ /**
+ * Returns a builder.
+ */
+ public static Builder builder() {
+ return new AutoValue_Event.Builder();
+ }
+
+ /**
+ * Returns whether it fails.
+ */
+ public boolean isFailure() {
+ return getException() != null;
+ }
+
+ /**
+ * Builder
+ */
+ @AutoValue.Builder
+ public abstract static class Builder {
+
+ /**
+ * Set event code.
+ */
+ public abstract Builder setEventCode(EventCode eventCode);
+
+ /**
+ * Set timestamp.
+ */
+ public abstract Builder setTimestamp(long startTimestamp);
+
+ /**
+ * Set profile.
+ */
+ public abstract Builder setProfile(@Nullable Short profile);
+
+ /**
+ * Set Bluetooth device.
+ */
+ public abstract Builder setBluetoothDevice(@Nullable BluetoothDevice device);
+
+ /**
+ * Set exception.
+ */
+ public abstract Builder setException(@Nullable Exception exception);
+
+ /**
+ * Builds event.
+ */
+ public abstract Event build();
+ }
+
+ @Override
+ public final void writeToParcel(Parcel dest, int flags) {
+ dest.writeInt(getEventCode().getNumber());
+ dest.writeLong(getTimestamp());
+ dest.writeValue(getProfile());
+ dest.writeParcelable(getBluetoothDevice(), 0);
+ dest.writeSerializable(getException());
+ }
+
+ @Override
+ public final int describeContents() {
+ return 0;
+ }
+
+ /**
+ * Event Creator instance.
+ */
+ public static final Creator<Event> CREATOR =
+ new Creator<Event>() {
+ @Override
+ /** Creates Event from Parcel. */
+ public Event createFromParcel(Parcel in) {
+ return Event.builder()
+ .setEventCode(EventCode.forNumber(in.readInt()))
+ .setTimestamp(in.readLong())
+ .setProfile((Short) in.readValue(Short.class.getClassLoader()))
+ .setBluetoothDevice(
+ in.readParcelable(BluetoothDevice.class.getClassLoader()))
+ .setException((Exception) in.readSerializable())
+ .build();
+ }
+
+ @Override
+ /** Returns Event array. */
+ public Event[] newArray(int size) {
+ return new Event[size];
+ }
+ };
+}
diff --git a/nearby/tests/Android.bp b/nearby/tests/Android.bp
index ec66b32..8a07882 100644
--- a/nearby/tests/Android.bp
+++ b/nearby/tests/Android.bp
@@ -34,10 +34,11 @@
static_libs: [
"androidx.test.rules",
- "mockito-target",
+ "fast-pair-lite-protos",
"framework-nearby-pre-jarjar",
"guava",
"libprotobuf-java-lite",
+ "mockito-target",
"platform-test-annotations",
"service-nearby",
"truth-prebuilt",
diff --git a/nearby/tests/src/com/android/server/nearby/common/bluetooth/fastpair/EventTest.java b/nearby/tests/src/com/android/server/nearby/common/bluetooth/fastpair/EventTest.java
new file mode 100644
index 0000000..e2f42ee
--- /dev/null
+++ b/nearby/tests/src/com/android/server/nearby/common/bluetooth/fastpair/EventTest.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.nearby.common.bluetooth.fastpair;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothDevice;
+import android.os.Parcel;
+import android.platform.test.annotations.Presubmit;
+
+import androidx.test.filters.SmallTest;
+import androidx.test.runner.AndroidJUnit4;
+
+import com.android.server.nearby.proto.NearbyEventCodes.NearbyEvent.EventCode;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Unit tests for {@link EventTest}.
+ */
+@Presubmit
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public class EventTest {
+
+ private static final String EXCEPTION_MESSAGE = "Test exception";
+ private static final long TIMESTAMP = 1234L;
+ private static final EventCode EVENT_CODE = EventCode.CREATE_BOND;
+ private static final BluetoothDevice BLUETOOTH_DEVICE = BluetoothAdapter.getDefaultAdapter()
+ .getRemoteDevice("11:22:33:44:55:66");
+ private static final Short PROFILE = (short) 1;
+
+ @Test
+ public void createAndReadFromParcel() {
+ Event event =
+ Event.builder()
+ .setException(new Exception(EXCEPTION_MESSAGE))
+ .setTimestamp(TIMESTAMP)
+ .setEventCode(EVENT_CODE)
+ .setBluetoothDevice(BLUETOOTH_DEVICE)
+ .setProfile(PROFILE)
+ .build();
+
+ Parcel parcel = Parcel.obtain();
+ event.writeToParcel(parcel, event.describeContents());
+ parcel.setDataPosition(0);
+ Event result = Event.CREATOR.createFromParcel(parcel);
+
+ assertThat(result.getException()).hasMessageThat()
+ .isEqualTo(event.getException().getMessage());
+ assertThat(result.getTimestamp()).isEqualTo(event.getTimestamp());
+ assertThat(result.getEventCode()).isEqualTo(event.getEventCode());
+ assertThat(result.getBluetoothDevice()).isEqualTo(event.getBluetoothDevice());
+ assertThat(result.getProfile()).isEqualTo(event.getProfile());
+ }
+}