• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 android.nearby;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.nearby.aidl.FastPairAccountKeyDeviceMetadataParcel;
22 
23 /**
24  * Class for metadata of a Fast Pair device associated with an account.
25  *
26  * @hide
27  */
28 public class FastPairAccountKeyDeviceMetadata {
29 
30     FastPairAccountKeyDeviceMetadataParcel mMetadataParcel;
31 
FastPairAccountKeyDeviceMetadata(FastPairAccountKeyDeviceMetadataParcel metadataParcel)32     FastPairAccountKeyDeviceMetadata(FastPairAccountKeyDeviceMetadataParcel metadataParcel) {
33         this.mMetadataParcel = metadataParcel;
34     }
35 
36     /**
37      * Get Device Account Key, which uniquely identifies a Fast Pair device associated with an
38      * account. AccountKey is 16 bytes: first byte is 0x04. Other 15 bytes are randomly generated.
39      *
40      * @return 16-byte Account Key.
41      * @hide
42      */
43     @Nullable
getDeviceAccountKey()44     public byte[] getDeviceAccountKey() {
45         return mMetadataParcel.deviceAccountKey;
46     }
47 
48     /**
49      * Get a hash value of device's account key and public bluetooth address without revealing the
50      * public bluetooth address. Sha256 hash value is 32 bytes.
51      *
52      * @return 32-byte Sha256 hash value.
53      * @hide
54      */
55     @Nullable
getSha256DeviceAccountKeyPublicAddress()56     public byte[] getSha256DeviceAccountKeyPublicAddress() {
57         return mMetadataParcel.sha256DeviceAccountKeyPublicAddress;
58     }
59 
60     /**
61      * Get metadata of a Fast Pair device type.
62      *
63      * @hide
64      */
65     @Nullable
getFastPairDeviceMetadata()66     public FastPairDeviceMetadata getFastPairDeviceMetadata() {
67         if (mMetadataParcel.metadata == null) {
68             return null;
69         }
70         return new FastPairDeviceMetadata(mMetadataParcel.metadata);
71     }
72 
73     /**
74      * Get Fast Pair discovery item, which is tied to both the device type and the account.
75      *
76      * @hide
77      */
78     @Nullable
getFastPairDiscoveryItem()79     public FastPairDiscoveryItem getFastPairDiscoveryItem() {
80         if (mMetadataParcel.discoveryItem == null) {
81             return null;
82         }
83         return new FastPairDiscoveryItem(mMetadataParcel.discoveryItem);
84     }
85 
86     /**
87      * Builder used to create FastPairAccountKeyDeviceMetadata.
88      *
89      * @hide
90      */
91     public static final class Builder {
92 
93         private final FastPairAccountKeyDeviceMetadataParcel mBuilderParcel;
94 
95         /**
96          * Default constructor of Builder.
97          *
98          * @hide
99          */
Builder()100         public Builder() {
101             mBuilderParcel = new FastPairAccountKeyDeviceMetadataParcel();
102             mBuilderParcel.deviceAccountKey = null;
103             mBuilderParcel.sha256DeviceAccountKeyPublicAddress = null;
104             mBuilderParcel.metadata = null;
105             mBuilderParcel.discoveryItem = null;
106         }
107 
108         /**
109          * Set Account Key.
110          *
111          * @param deviceAccountKey Fast Pair device account key, which is 16 bytes: first byte is
112          *                         0x04. Next 15 bytes are randomly generated.
113          * @return The builder, to facilitate chaining {@code builder.setXXX(..).setXXX(..)}.
114          * @hide
115          */
116         @NonNull
setDeviceAccountKey(@ullable byte[] deviceAccountKey)117         public Builder setDeviceAccountKey(@Nullable byte[] deviceAccountKey) {
118             mBuilderParcel.deviceAccountKey = deviceAccountKey;
119             return this;
120         }
121 
122         /**
123          * Set sha256 hash value of account key and public bluetooth address.
124          *
125          * @param sha256DeviceAccountKeyPublicAddress 32-byte sha256 hash value of account key and
126          *                                            public bluetooth address.
127          * @return The builder, to facilitate chaining {@code builder.setXXX(..).setXXX(..)}.
128          * @hide
129          */
130         @NonNull
setSha256DeviceAccountKeyPublicAddress( @ullable byte[] sha256DeviceAccountKeyPublicAddress)131         public Builder setSha256DeviceAccountKeyPublicAddress(
132                 @Nullable byte[] sha256DeviceAccountKeyPublicAddress) {
133             mBuilderParcel.sha256DeviceAccountKeyPublicAddress =
134                     sha256DeviceAccountKeyPublicAddress;
135             return this;
136         }
137 
138 
139         /**
140          * Set Fast Pair metadata.
141          *
142          * @param metadata Fast Pair metadata.
143          * @return The builder, to facilitate chaining {@code builder.setXXX(..).setXXX(..)}.
144          * @hide
145          */
146         @NonNull
setFastPairDeviceMetadata(@ullable FastPairDeviceMetadata metadata)147         public Builder setFastPairDeviceMetadata(@Nullable FastPairDeviceMetadata metadata) {
148             if (metadata == null) {
149                 mBuilderParcel.metadata = null;
150             } else {
151                 mBuilderParcel.metadata = metadata.mMetadataParcel;
152             }
153             return this;
154         }
155 
156         /**
157          * Set Fast Pair discovery item.
158          *
159          * @param discoveryItem Fast Pair discovery item.
160          * @return The builder, to facilitate chaining {@code builder.setXXX(..).setXXX(..)}.
161          * @hide
162          */
163         @NonNull
setFastPairDiscoveryItem(@ullable FastPairDiscoveryItem discoveryItem)164         public Builder setFastPairDiscoveryItem(@Nullable FastPairDiscoveryItem discoveryItem) {
165             if (discoveryItem == null) {
166                 mBuilderParcel.discoveryItem = null;
167             } else {
168                 mBuilderParcel.discoveryItem = discoveryItem.mMetadataParcel;
169             }
170             return this;
171         }
172 
173         /**
174          * Build {@link FastPairAccountKeyDeviceMetadata} with the currently set configuration.
175          *
176          * @hide
177          */
178         @NonNull
build()179         public FastPairAccountKeyDeviceMetadata build() {
180             return new FastPairAccountKeyDeviceMetadata(mBuilderParcel);
181         }
182     }
183 }
184