Remove Java enum uage in Constants.java
Test: exist unit tests.
Bug: 201673262
Change-Id: Ie97bd62156e406fef733541c2b56f4b9bc9ea1e6
diff --git a/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/Constants.java b/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/Constants.java
index d3eb388..cfecd2f 100644
--- a/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/Constants.java
+++ b/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/Constants.java
@@ -28,6 +28,8 @@
import android.bluetooth.BluetoothHeadset;
import android.util.Log;
+import androidx.annotation.IntDef;
+
import com.android.server.nearby.common.bluetooth.BluetoothException;
import com.android.server.nearby.common.bluetooth.gatt.BluetoothGattConnection;
@@ -35,6 +37,8 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.primitives.Shorts;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
import java.nio.ByteBuffer;
import java.security.GeneralSecurityException;
import java.util.Random;
@@ -199,67 +203,57 @@
/**
* Enumerates all flags of key-based pairing request.
*/
- // TODO(b/201673262): Convert enum to byte.
- public enum KeyBasedPairingRequestFlag {
+ @Retention(RetentionPolicy.SOURCE)
+ @IntDef(
+ value = {
+ KeyBasedPairingRequestFlag.REQUEST_DISCOVERABLE,
+ KeyBasedPairingRequestFlag.PROVIDER_INITIATES_BONDING,
+ KeyBasedPairingRequestFlag.REQUEST_DEVICE_NAME,
+ KeyBasedPairingRequestFlag.REQUEST_RETROACTIVE_PAIR,
+ })
+ public @interface KeyBasedPairingRequestFlag {
/**
* The bit indicating that the Fast Pair device should temporarily become
* discoverable.
*/
- REQUEST_DISCOVERABLE((byte) (1 << 7)),
+ int REQUEST_DISCOVERABLE = (byte) (1 << 7);
/**
* The bit indicating that the requester (Seeker) has included their public address
* in bytes [7,12] of the request, and the Provider should initiate bonding to that
* address.
*/
- PROVIDER_INITIATES_BONDING((byte) (1 << 6)),
+ int PROVIDER_INITIATES_BONDING = (byte) (1 << 6);
/**
* The bit indicating that Seeker requests Provider shall return the existing name.
*/
- REQUEST_DEVICE_NAME((byte) (1 << 5)),
+ int REQUEST_DEVICE_NAME = (byte) (1 << 5);
/**
* The bit indicating that the Seeker request retroactive pairing.
*/
- REQUEST_RETROACTIVE_PAIR((byte) (1 << 4));
-
- private final byte mValue;
-
- KeyBasedPairingRequestFlag(byte flag) {
- this.mValue = flag;
- }
-
- /** Returns value. */
- public byte getValue() {
- return mValue;
- }
+ int REQUEST_RETROACTIVE_PAIR = (byte) (1 << 4);
}
/**
* Enumerates all flags of action over BLE request, see Fast Pair spec for details.
*/
- // TODO(b/201673262): Convert enum to byte.
- public enum ActionOverBleFlag {
+ @IntDef(
+ value = {
+ ActionOverBleFlag.DEVICE_ACTION,
+ ActionOverBleFlag.ADDITIONAL_DATA_CHARACTERISTIC,
+ })
+ public @interface ActionOverBleFlag {
/**
* The bit indicating that the handshaking is for Device Action.
*/
- DEVICE_ACTION((byte) (1 << 7)),
+ int DEVICE_ACTION = (byte) (1 << 7);
/**
* The bit indicating that this handshake will be followed by Additional Data
* characteristic.
*/
- ADDITIONAL_DATA_CHARACTERISTIC((byte) (1 << 6));
-
- private final byte mValue;
-
- ActionOverBleFlag(byte value) {
- this.mValue = value;
- }
-
- /** Retunns value. */
- public byte getValue() {
- return mValue;
- }
+ int ADDITIONAL_DATA_CHARACTERISTIC = (byte) (1 << 6);
}
+
/**
* Constants related to the decrypted response sent back in a notify.
*/
@@ -308,28 +302,26 @@
/**
* The type of the Passkey Block message.
*/
- // TODO(b/201673262): Convert enum to byte.
- public enum Type {
+ @IntDef(
+ value = {
+ Type.SEEKER,
+ Type.PROVIDER,
+ })
+ public @interface Type {
/**
* Seeker's Passkey.
*/
- SEEKER((byte) 0x02),
+ int SEEKER = (byte) 0x02;
/**
* Provider's Passkey.
*/
- PROVIDER((byte) 0x03);
-
- private final byte mValue;
-
- Type(byte value) {
- this.mValue = value;
- }
+ int PROVIDER = (byte) 0x03;
}
/**
* Constructs the encrypted value to write to the characteristic.
*/
- public static byte[] encrypt(Type type, byte[] secret, int passkey)
+ public static byte[] encrypt(@Type int type, byte[] secret, int passkey)
throws GeneralSecurityException {
Preconditions.checkArgument(
0 < passkey && passkey < /*2^24=*/ 16777216,
@@ -342,19 +334,20 @@
- passkeyBytes.length];
new Random().nextBytes(salt);
return AesEcbSingleBlockEncryption.encrypt(
- secret, concat(new byte[]{type.mValue}, passkeyBytes, salt));
+ secret, concat(new byte[]{(byte) type}, passkeyBytes, salt));
}
/**
* Extracts the passkey from the encrypted characteristic value.
*/
- public static int decrypt(Type type, byte[] secret, byte[] passkeyCharacteristicValue)
+ public static int decrypt(@Type int type, byte[] secret,
+ byte[] passkeyCharacteristicValue)
throws GeneralSecurityException {
byte[] decrypted = AesEcbSingleBlockEncryption
.decrypt(secret, passkeyCharacteristicValue);
- if (decrypted[0] != type.mValue) {
+ if (decrypted[0] != (byte) type) {
throw new GeneralSecurityException(
- "Wrong Passkey Block type (expected " + type.mValue + ", got "
+ "Wrong Passkey Block type (expected " + type + ", got "
+ decrypted[0] + ")");
}
return ByteBuffer.allocate(4)
@@ -470,36 +463,18 @@
/**
* Enumerates all types of additional data.
*/
- // TODO(b/201673262): Convert enum to byte.
- public enum AdditionalDataType {
+ @Retention(RetentionPolicy.SOURCE)
+ @IntDef(
+ value = {
+ AdditionalDataType.PERSONALIZED_NAME,
+ AdditionalDataType.UNKNOWN,
+ })
+ public @interface AdditionalDataType {
/**
* The value indicating that the type is for personalized name.
*/
- PERSONALIZED_NAME((byte) 0x01),
- UNKNOWN((byte) 0x00); // and all others.
-
- private final byte mValue;
-
- AdditionalDataType(byte value) {
- this.mValue = value;
- }
-
- public byte getValue() {
- return mValue;
- }
-
- /** Converts byte to enum. */
- public static AdditionalDataType valueOf(byte value) {
- for (AdditionalDataType type : AdditionalDataType.values()) {
- if (type.getValue() == value) {
- return type;
- }
- }
- return UNKNOWN;
- }
- }
-
- private AdditionalDataCharacteristic() {
+ int PERSONALIZED_NAME = (byte) 0x01;
+ int UNKNOWN = (byte) 0x00; // and all others.
}
}
@@ -533,45 +508,49 @@
/**
* Enumerates all types of beacon actions.
*/
- // TODO(b/201673262): Convert enum to byte.
- public enum BeaconActionType {
- /**
- * The value indicating that the type is for personalized name.
- */
- READ_BEACON_PARAMETERS((byte) 0x00),
- READ_PROVISIONING_STATE((byte) 0x01),
- SET_EPHEMERAL_IDENTITY_KEY((byte) 0x02),
- CLEAR_EPHEMERAL_IDENTITY_KEY((byte) 0x03),
- READ_EPHEMERAL_IDENTITY_KEY((byte) 0x04),
- RING((byte) 0x05),
- READ_RINGING_STATE((byte) 0x06),
- UNKNOWN((byte) 0xFF); // and all others.
-
- private final byte mValue;
-
- BeaconActionType(byte value) {
- this.mValue = value;
- }
-
- public byte getValue() {
- return mValue;
- }
-
- /** Converts value to enum. */
- public static BeaconActionType valueOf(byte value) {
- for (BeaconActionType type : BeaconActionType.values()) {
- if (type.getValue() == value) {
- return type;
- }
- }
- return UNKNOWN;
- }
+ /** Fast Pair Bond State. */
+ @Retention(RetentionPolicy.SOURCE)
+ @IntDef(
+ value = {
+ BeaconActionType.READ_BEACON_PARAMETERS,
+ BeaconActionType.READ_PROVISIONING_STATE,
+ BeaconActionType.SET_EPHEMERAL_IDENTITY_KEY,
+ BeaconActionType.CLEAR_EPHEMERAL_IDENTITY_KEY,
+ BeaconActionType.READ_EPHEMERAL_IDENTITY_KEY,
+ BeaconActionType.RING,
+ BeaconActionType.READ_RINGING_STATE,
+ BeaconActionType.UNKNOWN,
+ })
+ public @interface BeaconActionType {
+ int READ_BEACON_PARAMETERS = (byte) 0x00;
+ int READ_PROVISIONING_STATE = (byte) 0x01;
+ int SET_EPHEMERAL_IDENTITY_KEY = (byte) 0x02;
+ int CLEAR_EPHEMERAL_IDENTITY_KEY = (byte) 0x03;
+ int READ_EPHEMERAL_IDENTITY_KEY = (byte) 0x04;
+ int RING = (byte) 0x05;
+ int READ_RINGING_STATE = (byte) 0x06;
+ int UNKNOWN = (byte) 0xFF; // and all others
}
- private BeaconActionsCharacteristic() {
+ /** Converts value to enum. */
+ public static @BeaconActionType int valueOf(byte value) {
+ switch(value) {
+ case BeaconActionType.READ_BEACON_PARAMETERS:
+ case BeaconActionType.READ_PROVISIONING_STATE:
+ case BeaconActionType.SET_EPHEMERAL_IDENTITY_KEY:
+ case BeaconActionType.CLEAR_EPHEMERAL_IDENTITY_KEY:
+ case BeaconActionType.READ_EPHEMERAL_IDENTITY_KEY:
+ case BeaconActionType.RING:
+ case BeaconActionType.READ_RINGING_STATE:
+ case BeaconActionType.UNKNOWN:
+ return value;
+ default:
+ return BeaconActionType.UNKNOWN;
+ }
}
}
+
/**
* Characteristic to read for checking firmware version. 0X2A26 is assigned number from
* bluetooth SIG website.
diff --git a/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/FastPairDualConnection.java b/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/FastPairDualConnection.java
index b3be779..b3570f9 100644
--- a/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/FastPairDualConnection.java
+++ b/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/FastPairDualConnection.java
@@ -1492,7 +1492,7 @@
* secret. The given key should be the account key.
*/
private SharedSecret handshakeForActionOverBle(byte[] key,
- AdditionalDataType additionalDataType)
+ @AdditionalDataType int additionalDataType)
throws InterruptedException, ExecutionException, TimeoutException, BluetoothException,
GeneralSecurityException, PairingException {
HandshakeHandler handshakeHandler = prepareForHandshake();
diff --git a/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/HandshakeHandler.java b/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/HandshakeHandler.java
index b1918b5..984133b 100644
--- a/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/HandshakeHandler.java
+++ b/nearby/service/java/com/android/server/nearby/common/bluetooth/fastpair/HandshakeHandler.java
@@ -393,8 +393,8 @@
/**
* Adds flags without changing other flags.
*/
- public Builder addFlag(KeyBasedPairingRequestFlag flag) {
- this.mFlags |= flag.getValue();
+ public Builder addFlag(@KeyBasedPairingRequestFlag int flag) {
+ this.mFlags |= (byte) flag;
return this;
}
@@ -448,7 +448,7 @@
String.format(
"type (%02X), flag (%02X)", rawMessage[TYPE_INDEX],
rawMessage[FLAGS_INDEX]));
- if ((mFlags & DEVICE_ACTION.getValue()) != 0) {
+ if ((mFlags & (byte) DEVICE_ACTION) != 0) {
rawMessage[EVENT_GROUP_INDEX] = mEventGroup;
rawMessage[EVENT_CODE_INDEX] = mEventCode;
@@ -470,7 +470,7 @@
rawMessage[EVENT_CODE_INDEX],
rawMessage[EVENT_ADDITIONAL_DATA_LENGTH_INDEX]));
}
- if ((mFlags & ADDITIONAL_DATA_CHARACTERISTIC.getValue()) != 0) {
+ if ((mFlags & (byte) ADDITIONAL_DATA_CHARACTERISTIC) != 0) {
rawMessage[ADDITIONAL_DATA_TYPE_INDEX] = mAdditionalDataType;
stringBuilder.append(
String.format(", data id(%02X)", rawMessage[ADDITIONAL_DATA_TYPE_INDEX]));
@@ -496,8 +496,8 @@
/**
* Adds flag without changing other flags.
*/
- public Builder addFlag(ActionOverBleFlag flag) {
- this.mFlags |= flag.getValue();
+ public Builder addFlag(@ActionOverBleFlag int flag) {
+ this.mFlags |= (byte) flag;
return this;
}
@@ -505,7 +505,7 @@
* Set event group and event code.
*/
public Builder setEvent(int eventGroup, int eventCode) {
- this.mFlags |= DEVICE_ACTION.getValue();
+ this.mFlags |= (byte) DEVICE_ACTION;
this.mEventGroup = (byte) (eventGroup & 0xFF);
this.mEventCode = (byte) (eventCode & 0xFF);
return this;
@@ -522,9 +522,9 @@
/**
* Set event additional data type.
*/
- public Builder setAdditionalDataType(AdditionalDataType additionalDataType) {
- this.mFlags |= ADDITIONAL_DATA_CHARACTERISTIC.getValue();
- this.mAdditionalDataType = additionalDataType.getValue();
+ public Builder setAdditionalDataType(@AdditionalDataType int additionalDataType) {
+ this.mFlags |= (byte) ADDITIONAL_DATA_CHARACTERISTIC;
+ this.mAdditionalDataType = (byte) additionalDataType;
return this;
}