• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.SystemApi;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 
26 import com.android.internal.util.Preconditions;
27 
28 import java.util.Arrays;
29 import java.util.Objects;
30 
31 /**
32  * Represents a data element in Nearby Presence.
33  *
34  * @hide
35  */
36 @SystemApi
37 public final class DataElement implements Parcelable {
38 
39     private final int mKey;
40     private final byte[] mValue;
41 
42     /** @hide */
43     @IntDef({
44             DataType.BLE_SERVICE_DATA,
45             DataType.BLE_ADDRESS,
46             DataType.SALT,
47             DataType.PRIVATE_IDENTITY,
48             DataType.TRUSTED_IDENTITY,
49             DataType.PUBLIC_IDENTITY,
50             DataType.PROVISIONED_IDENTITY,
51             DataType.TX_POWER,
52             DataType.ACTION,
53             DataType.MODEL_ID,
54             DataType.EDDYSTONE_EPHEMERAL_IDENTIFIER,
55             DataType.ACCOUNT_KEY_DATA,
56             DataType.CONNECTION_STATUS,
57             DataType.BATTERY,
58             DataType.SCAN_MODE,
59             DataType.TEST_DE_BEGIN,
60             DataType.TEST_DE_END
61     })
62     public @interface DataType {
63         int BLE_SERVICE_DATA = 100;
64         int BLE_ADDRESS = 101;
65         // This is to indicate if the scan is offload only
66         int SCAN_MODE = 102;
67         int SALT = 0;
68         int PRIVATE_IDENTITY = 1;
69         int TRUSTED_IDENTITY = 2;
70         int PUBLIC_IDENTITY = 3;
71         int PROVISIONED_IDENTITY = 4;
72         int TX_POWER = 5;
73         int ACTION = 6;
74         int MODEL_ID = 7;
75         int EDDYSTONE_EPHEMERAL_IDENTIFIER = 8;
76         int ACCOUNT_KEY_DATA = 9;
77         int CONNECTION_STATUS = 10;
78         int BATTERY = 11;
79         // Reserves test DE ranges from {@link DataElement.DataType#TEST_DE_BEGIN}
80         // to {@link DataElement.DataType#TEST_DE_END}, inclusive.
81         // Reserves 128 Test DEs.
82         int TEST_DE_BEGIN = Integer.MAX_VALUE - 127; // 2147483520
83         int TEST_DE_END = Integer.MAX_VALUE; // 2147483647
84     }
85 
86     /**
87      * @hide
88      */
isValidType(int type)89     public static boolean isValidType(int type) {
90         return type == DataType.BLE_SERVICE_DATA
91                 || type == DataType.ACCOUNT_KEY_DATA
92                 || type == DataType.BLE_ADDRESS
93                 || type == DataType.SCAN_MODE
94                 || type == DataType.SALT
95                 || type == DataType.PRIVATE_IDENTITY
96                 || type == DataType.TRUSTED_IDENTITY
97                 || type == DataType.PUBLIC_IDENTITY
98                 || type == DataType.PROVISIONED_IDENTITY
99                 || type == DataType.TX_POWER
100                 || type == DataType.ACTION
101                 || type == DataType.MODEL_ID
102                 || type == DataType.EDDYSTONE_EPHEMERAL_IDENTIFIER
103                 || type == DataType.CONNECTION_STATUS
104                 || type == DataType.BATTERY;
105     }
106 
107     /**
108      * @return {@code true} if this is identity type.
109      * @hide
110      */
isIdentityDataType()111     public boolean isIdentityDataType() {
112         return mKey == DataType.PRIVATE_IDENTITY
113                 || mKey == DataType.TRUSTED_IDENTITY
114                 || mKey == DataType.PUBLIC_IDENTITY
115                 || mKey == DataType.PROVISIONED_IDENTITY;
116     }
117 
118     /**
119      * @return {@code true} if this is test data element type.
120      * @hide
121      */
isTestDeType(int type)122     public static boolean isTestDeType(int type) {
123         return type >= DataType.TEST_DE_BEGIN && type <= DataType.TEST_DE_END;
124     }
125 
126     /**
127      * Constructs a {@link DataElement}.
128      */
DataElement(int key, @NonNull byte[] value)129     public DataElement(int key, @NonNull byte[] value) {
130         Preconditions.checkArgument(value != null, "value cannot be null");
131         mKey = key;
132         mValue = value;
133     }
134 
135     @NonNull
136     public static final Creator<DataElement> CREATOR = new Creator<DataElement>() {
137         @Override
138         public DataElement createFromParcel(Parcel in) {
139             int key = in.readInt();
140             byte[] value = new byte[in.readInt()];
141             in.readByteArray(value);
142             return new DataElement(key, value);
143         }
144 
145         @Override
146         public DataElement[] newArray(int size) {
147             return new DataElement[size];
148         }
149     };
150 
151     @Override
equals(@ullable Object obj)152     public boolean equals(@Nullable Object obj) {
153         if (obj instanceof DataElement) {
154             return mKey == ((DataElement) obj).mKey
155                     && Arrays.equals(mValue, ((DataElement) obj).mValue);
156         }
157         return false;
158     }
159 
160     @Override
hashCode()161     public int hashCode() {
162         return Objects.hash(mKey, Arrays.hashCode(mValue));
163     }
164 
165     @Override
describeContents()166     public int describeContents() {
167         return 0;
168     }
169 
170     @Override
writeToParcel(@onNull Parcel dest, int flags)171     public void writeToParcel(@NonNull Parcel dest, int flags) {
172         dest.writeInt(mKey);
173         dest.writeInt(mValue.length);
174         dest.writeByteArray(mValue);
175     }
176 
177     /**
178      * Returns the key of the data element, as defined in the nearby presence specification.
179      */
getKey()180     public int getKey() {
181         return mKey;
182     }
183 
184     /**
185      * Returns the value of the data element.
186      */
187     @NonNull
getValue()188     public byte[] getValue() {
189         return mValue;
190     }
191 }
192