Add loadFpDeviceMetadata API.

Design doc: go/fastpair-data-provider-sytem-apis
Test: will add unit test to cts dir in another cl.
Bug: 204780849
CTS-Coverage-Bug: 205591293

Change-Id: I14cda975db8c89508a04a02d85bec93d4ef10908
diff --git a/nearby/framework/api/system-current.txt b/nearby/framework/api/system-current.txt
index d802177..fe3cd98 100644
--- a/nearby/framework/api/system-current.txt
+++ b/nearby/framework/api/system-current.txt
@@ -1 +1,37 @@
 // Signature format: 2.0
+package android.nearby {
+
+  public abstract class FastPairDataProviderBase {
+    ctor public FastPairDataProviderBase(@NonNull String);
+    method @Nullable public final android.os.IBinder getBinder();
+    method public abstract void onLoadFastPairDeviceMetadata(@NonNull android.nearby.FastPairDataProviderBase.FastPairDeviceMetadataRequest, @NonNull android.nearby.FastPairDataProviderBase.FastPairDeviceMetadataCallback);
+  }
+
+  public static class FastPairDataProviderBase.FastPairDeviceMetadata {
+  }
+
+  public static final class FastPairDataProviderBase.FastPairDeviceMetadata.Builder {
+    ctor public FastPairDataProviderBase.FastPairDeviceMetadata.Builder();
+    method @NonNull public android.nearby.FastPairDataProviderBase.FastPairDeviceMetadata build();
+    method @NonNull public android.nearby.FastPairDataProviderBase.FastPairDeviceMetadata.Builder setAntiSpoofPublicKey(@NonNull byte[]);
+    method @NonNull public android.nearby.FastPairDataProviderBase.FastPairDeviceMetadata.Builder setBleTxPower(int);
+    method @NonNull public android.nearby.FastPairDataProviderBase.FastPairDeviceMetadata.Builder setDeviceType(int);
+    method @NonNull public android.nearby.FastPairDataProviderBase.FastPairDeviceMetadata.Builder setImage(@NonNull byte[]);
+    method @NonNull public android.nearby.FastPairDataProviderBase.FastPairDeviceMetadata.Builder setImageUrl(@NonNull String);
+    method @NonNull public android.nearby.FastPairDataProviderBase.FastPairDeviceMetadata.Builder setIntentUri(@NonNull String);
+    method @NonNull public android.nearby.FastPairDataProviderBase.FastPairDeviceMetadata.Builder setTriggerDistance(float);
+    method @NonNull public android.nearby.FastPairDataProviderBase.FastPairDeviceMetadata.Builder setTrueWirelessImageUriLeftBud(@NonNull byte[]);
+    method @NonNull public android.nearby.FastPairDataProviderBase.FastPairDeviceMetadata.Builder setTrueWirelessImageUrlCase(@NonNull byte[]);
+    method @NonNull public android.nearby.FastPairDataProviderBase.FastPairDeviceMetadata.Builder setTrueWirelessImageUrlRightBud(@NonNull byte[]);
+  }
+
+  public static interface FastPairDataProviderBase.FastPairDeviceMetadataCallback {
+    method public void onFastPairDeviceMetadataReceived(@NonNull android.nearby.FastPairDataProviderBase.FastPairDeviceMetadata);
+  }
+
+  public static class FastPairDataProviderBase.FastPairDeviceMetadataRequest {
+    method @Nullable public byte[] getModelId();
+  }
+
+}
+
diff --git a/nearby/framework/java/android/nearby/FastPairDataProviderBase.java b/nearby/framework/java/android/nearby/FastPairDataProviderBase.java
new file mode 100644
index 0000000..36c3be0
--- /dev/null
+++ b/nearby/framework/java/android/nearby/FastPairDataProviderBase.java
@@ -0,0 +1,280 @@
+/*
+ * 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 android.nearby;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.annotation.SuppressLint;
+import android.annotation.SystemApi;
+import android.os.IBinder;
+import android.os.RemoteException;
+import android.util.Log;
+
+/**
+ * Base class for fast pair providers outside the system server.
+ *
+ * Fast pair providers should be wrapped in a non-exported service which returns the result of
+ * {@link #getBinder()} from the service's {@link android.app.Service#onBind(Intent)} method. The
+ * service should not be exported so that components other than the system server cannot bind to it.
+ * Alternatively, the service may be guarded by a permission that only system server can obtain.
+ *
+ * <p>Fast Pair providers are identified by their UID / package name.
+ *
+ * @hide
+ */
+@SystemApi
+public abstract class FastPairDataProviderBase {
+
+    private final IBinder mBinder;
+    private final String mTag;
+
+    public FastPairDataProviderBase(@NonNull String tag) {
+        mBinder = new Service();
+        mTag = tag;
+    }
+
+    /**
+     * Callback to be invoked when a device metadata is loaded.
+     */
+    public interface FastPairDeviceMetadataCallback {
+
+        /**
+         * Should be invoked once the meta data is loaded.
+         */
+        void onFastPairDeviceMetadataReceived(@NonNull FastPairDeviceMetadata metadata);
+    }
+
+    /**
+     * Fullfills the load device metadata request by using callback to send back the serialized
+     * device meta data of the given modelId.
+     */
+    public abstract void onLoadFastPairDeviceMetadata(
+            @NonNull FastPairDeviceMetadataRequest request,
+            @NonNull FastPairDeviceMetadataCallback callback);
+
+    /**
+     * Returns the IBinder instance that should be returned from the {@link
+     * android.app.Service#onBind(Intent)} method of the wrapping service.
+     */
+    public final @Nullable IBinder getBinder() {
+        return mBinder;
+    }
+
+    /**
+     * Class for building FastPairDeviceMetadata.
+     */
+    public static class FastPairDeviceMetadata {
+
+        private FastPairDeviceMetadataParcel mMetadataParcel;
+
+        private FastPairDeviceMetadata(FastPairDeviceMetadataParcel metadataParcel) {
+            this.mMetadataParcel = metadataParcel;
+        }
+
+        /**
+         * Builder used to create FastPairDeviceMetadata.
+         */
+        public static final class Builder {
+
+            private final FastPairDeviceMetadataParcel mBuilderParcel;
+
+            /**
+             * Default constructor of Builder.
+             */
+            public Builder() {
+                mBuilderParcel = new FastPairDeviceMetadataParcel();
+                mBuilderParcel.imageUrl = null;
+                mBuilderParcel.intentUri = null;
+                mBuilderParcel.antiSpoofPublicKey = null;
+                mBuilderParcel.bleTxPower = 0;
+                mBuilderParcel.triggerDistance = 0;
+                mBuilderParcel.image = null;
+                mBuilderParcel.deviceType = 0;  // DEVICE_TYPE_UNSPECIFIED
+                mBuilderParcel.trueWirelessImageUrlLeftBud = null;
+                mBuilderParcel.trueWirelessImageUrlRightBud = null;
+                mBuilderParcel.trueWirelessImageUrlCase = null;
+            }
+
+            /**
+             * Set ImageUlr.
+             */
+            @SuppressLint("MissingGetterMatchingBuilder")
+            @NonNull
+            public Builder setImageUrl(@NonNull String imageUrl) {
+                mBuilderParcel.imageUrl = imageUrl;
+                return this;
+            }
+
+            /**
+             * Set IntentUri.
+             */
+            @SuppressLint("MissingGetterMatchingBuilder")
+            @NonNull
+            public Builder setIntentUri(@NonNull String intentUri) {
+                mBuilderParcel.intentUri = intentUri;
+                return this;
+            }
+
+            /**
+             * Set AntiSpoof public key.
+             */
+            @SuppressLint("MissingGetterMatchingBuilder")
+            @NonNull
+            public Builder setAntiSpoofPublicKey(@NonNull byte[] antiSpoofPublicKey) {
+                mBuilderParcel.antiSpoofPublicKey = antiSpoofPublicKey;
+                return this;
+            }
+
+            /**
+             * Set ble transmission power.
+             */
+            @SuppressLint("MissingGetterMatchingBuilder")
+            @NonNull
+            public Builder setBleTxPower(int bleTxPower) {
+                mBuilderParcel.bleTxPower = bleTxPower;
+                return this;
+            }
+
+            /**
+             * Set trigger distance.
+             */
+            @SuppressLint("MissingGetterMatchingBuilder")
+            @NonNull
+            public Builder setTriggerDistance(float triggerDistance) {
+                mBuilderParcel.triggerDistance = triggerDistance;
+                return this;
+            }
+
+            /**
+             * Set image.
+             */
+            @SuppressLint("MissingGetterMatchingBuilder")
+            @NonNull
+            public Builder setImage(@NonNull byte[] image) {
+                mBuilderParcel.image = image;
+                return this;
+            }
+
+            /**
+             * Set device type.
+             */
+            @SuppressLint("MissingGetterMatchingBuilder")
+            @NonNull
+            public Builder setDeviceType(int deviceType) {
+                mBuilderParcel.deviceType = deviceType;
+                return this;
+            }
+
+            /**
+             * Set true wireless image url for left bud.
+             */
+            @SuppressLint("MissingGetterMatchingBuilder")
+            @NonNull
+            public Builder setTrueWirelessImageUriLeftBud(
+                    @NonNull byte[] trueWirelessImageUrlLeftBud) {
+                mBuilderParcel.trueWirelessImageUrlLeftBud = trueWirelessImageUrlLeftBud;
+                return this;
+            }
+
+            /**
+             * Set true wireless image url for right bud.
+             */
+            @SuppressLint("MissingGetterMatchingBuilder")
+            @NonNull
+            public Builder setTrueWirelessImageUrlRightBud(
+                    @NonNull byte[] trueWirelessImageUrlRightBud) {
+                mBuilderParcel.trueWirelessImageUrlRightBud = trueWirelessImageUrlRightBud;
+                return this;
+            }
+
+            /**
+             * Set true wireless image url for right bud.
+             */
+            @SuppressLint("MissingGetterMatchingBuilder")
+            @NonNull
+            public Builder setTrueWirelessImageUrlCase(@NonNull byte[] trueWirelessImageUrlCase) {
+                mBuilderParcel.trueWirelessImageUrlCase = trueWirelessImageUrlCase;
+                return this;
+            }
+
+            /**
+             * Build {@link FastPairDeviceMetadataRequest} with the currently set configuration.
+             */
+            @NonNull
+            public FastPairDeviceMetadata build() {
+                return new FastPairDeviceMetadata(mBuilderParcel);
+            }
+        }
+    }
+
+    /**
+     * Class for reading FastPairDeviceMetadataRequest.
+     */
+    public static class FastPairDeviceMetadataRequest {
+
+        private final FastPairDeviceMetadataRequestParcel mMetadataRequestParcel;
+
+        private FastPairDeviceMetadataRequest(
+                final FastPairDeviceMetadataRequestParcel metaDataRequestParcel) {
+            this.mMetadataRequestParcel = metaDataRequestParcel;
+        }
+
+        public @Nullable byte[] getModelId() {
+            return this.mMetadataRequestParcel.modelId;
+        }
+    }
+
+    /**
+     * Call back class that sends back data.
+     */
+    private final class Callback implements FastPairDeviceMetadataCallback {
+
+        private IFastPairDataCallback mCallback;
+
+        private Callback(IFastPairDataCallback callback) {
+            mCallback = callback;
+        }
+
+        /**
+         * Sends back the serialized device meta data.
+         */
+        @Override
+        public void onFastPairDeviceMetadataReceived(@NonNull FastPairDeviceMetadata metadata) {
+            try {
+                mCallback.onFastPairDeviceMetadataReceived(metadata.mMetadataParcel);
+            } catch (RemoteException e) {
+                throw e.rethrowFromSystemServer();
+            } catch (RuntimeException e) {
+                Log.w(mTag, e);
+            }
+        }
+    }
+
+    private final class Service extends IFastPairDataProvider.Stub {
+
+        Service() {
+        }
+
+        @Override
+        public void loadFastPairDeviceMetadata(
+                @NonNull FastPairDeviceMetadataRequestParcel requestParcel,
+                IFastPairDataCallback callback) {
+            onLoadFastPairDeviceMetadata(new FastPairDeviceMetadataRequest(requestParcel),
+                    new Callback(callback));
+        }
+    }
+}
diff --git a/nearby/framework/java/android/nearby/FastPairDeviceMetadataParcel.aidl b/nearby/framework/java/android/nearby/FastPairDeviceMetadataParcel.aidl
new file mode 100644
index 0000000..6fafa66
--- /dev/null
+++ b/nearby/framework/java/android/nearby/FastPairDeviceMetadataParcel.aidl
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2020 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 android.nearby;
+
+/**
+ * Configuration details for requesting tethering.
+ * @hide
+ */
+parcelable FastPairDeviceMetadataParcel {
+
+    // The image to show on the notification.
+    String imageUrl;
+
+    // The intent that will be launched via the notification.
+    String intentUri;
+
+    // Anti spoof public key;
+    byte[] antiSpoofPublicKey;
+
+    // The transmit power of the device's BLE chip.
+    int bleTxPower;
+
+    // The distance that the device must be within to show a notification.
+    // If no distance is set, we default to 0.6 meters. Only Nearby admins can
+    // change this.
+    float triggerDistance;
+
+    // The image icon that shows in the notification.
+    byte[] image;
+
+    int deviceType;
+
+    // The image for device with device type "true wireless".
+    byte[] trueWirelessImageUrlLeftBud;
+    byte[] trueWirelessImageUrlRightBud;
+    byte[] trueWirelessImageUrlCase;
+}
\ No newline at end of file
diff --git a/nearby/framework/java/android/nearby/FastPairDeviceMetadataRequestParcel.aidl b/nearby/framework/java/android/nearby/FastPairDeviceMetadataRequestParcel.aidl
new file mode 100644
index 0000000..2799e5d
--- /dev/null
+++ b/nearby/framework/java/android/nearby/FastPairDeviceMetadataRequestParcel.aidl
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2020 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 android.nearby;
+
+/**
+ * Configuration details for requesting tethering.
+ * @hide
+ */
+parcelable FastPairDeviceMetadataRequestParcel {
+    byte[] modelId;
+}
\ No newline at end of file
diff --git a/nearby/framework/java/android/nearby/IFastPairDataCallback.aidl b/nearby/framework/java/android/nearby/IFastPairDataCallback.aidl
new file mode 100644
index 0000000..e4a0348
--- /dev/null
+++ b/nearby/framework/java/android/nearby/IFastPairDataCallback.aidl
@@ -0,0 +1,26 @@
+// 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 android.nearby;
+
+import android.nearby.FastPairDeviceMetadataParcel;
+
+/**
+  * Provides callback interface for OEMs to send FastPair data back.
+  *
+  * {@hide}
+  */
+interface IFastPairDataCallback {
+     void onFastPairDeviceMetadataReceived(in FastPairDeviceMetadataParcel metadata);
+ }
diff --git a/nearby/framework/java/android/nearby/IFastPairDataProvider.aidl b/nearby/framework/java/android/nearby/IFastPairDataProvider.aidl
new file mode 100644
index 0000000..c3c04d6
--- /dev/null
+++ b/nearby/framework/java/android/nearby/IFastPairDataProvider.aidl
@@ -0,0 +1,28 @@
+// 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 android.nearby;
+
+import android.nearby.FastPairDeviceMetadataRequestParcel;
+import android.nearby.IFastPairDataCallback;
+
+/**
+ * Interface for communicating with the fast pair providers.
+ *
+ * @hide
+ */
+oneway interface IFastPairDataProvider {
+    void loadFastPairDeviceMetadata(in FastPairDeviceMetadataRequestParcel modelId,
+        in IFastPairDataCallback callback);
+}