Use component name instead of package name am: 06ee029b42 am: af3fc07317
am: bcf575a739

Change-Id: If17e512d6781166ea820d1c5a62f1957bcd82128
diff --git a/service/src/com/android/car/CarLocationService.java b/service/src/com/android/car/CarLocationService.java
index 3827c22..2f37e61 100644
--- a/service/src/com/android/car/CarLocationService.java
+++ b/service/src/com/android/car/CarLocationService.java
@@ -216,7 +216,17 @@
                 });
                 break;
             case CarPowerStateListener.SUSPEND_EXIT:
-                deleteCacheFile();
+                if (mCarDrivingStateService != null) {
+                    CarDrivingStateEvent event = mCarDrivingStateService.getCurrentDrivingState();
+                    if (event != null
+                            && event.eventValue == CarDrivingStateEvent.DRIVING_STATE_MOVING) {
+                        deleteCacheFile();
+                    } else {
+                        logd("Registering to receive driving state.");
+                        mCarDrivingStateService.registerDrivingStateChangeListener(
+                                mICarDrivingStateChangeEventListener);
+                    }
+                }
                 if (future != null) {
                     future.complete(null);
                 }
diff --git a/service/src/com/android/car/trust/BLEMessageV1Factory.java b/service/src/com/android/car/trust/BLEMessageV1Factory.java
index afb5c8c..4afe877 100644
--- a/service/src/com/android/car/trust/BLEMessageV1Factory.java
+++ b/service/src/com/android/car/trust/BLEMessageV1Factory.java
@@ -41,14 +41,6 @@
      */
     private static final int FIXED_32_SIZE = 4;
 
-    /**
-     * Additional bytes that are needed during the encoding of the {@code payload} field.
-     *
-     * <p>The {@code payload} field is defined as {@code bytes}, and thus, needs 2 extra bytes to
-     * encode: one to encode the field number and another for length delimiting.
-     */
-    private static final int ADDITIONAL_PAYLOAD_ENCODING_SIZE = 2;
-
     // The size needed to encode a boolean proto field
     private static final int BOOLEAN_FIELD_ENCODING_SIZE = 1;
 
@@ -145,13 +137,14 @@
     public static List<BLEMessage> makeBLEMessages(byte[] payload, OperationType operation,
             int maxSize, boolean isPayloadEncrypted) {
         List<BLEMessage> bleMessages = new ArrayList();
-        int maxPayloadSize = maxSize - getProtoHeaderSize(operation, isPayloadEncrypted);
-        int payloadLength = payload.length;
-        if (payloadLength <= maxPayloadSize) {
+        int payloadSize = payload.length;
+        int maxPayloadSize =
+                maxSize - getProtoHeaderSize(operation, payloadSize, isPayloadEncrypted);
+        if (payloadSize <= maxPayloadSize) {
             bleMessages.add(makeBLEMessage(payload, operation, isPayloadEncrypted));
             return bleMessages;
         }
-        int totalPackets = (int) Math.ceil((double) payloadLength / maxPayloadSize);
+        int totalPackets = (int) Math.ceil((double) payloadSize / maxPayloadSize);
         int start = 0;
         int end = maxPayloadSize;
         for (int i = 0; i < totalPackets; i++) {
@@ -164,7 +157,7 @@
                     .setPayload(ByteString.copyFrom(Arrays.copyOfRange(payload, start, end)))
                     .build());
             start = end;
-            end = Math.min(start + maxPayloadSize, payloadLength);
+            end = Math.min(start + maxPayloadSize, payloadSize);
         }
         return bleMessages;
     }
@@ -174,12 +167,18 @@
      * contain a payload.
      */
     @VisibleForTesting
-    static int getProtoHeaderSize(OperationType operation, boolean isPayloadEncrypted) {
-        int isPayloadEncryptedFieldSize =
-                isPayloadEncrypted ? (BOOLEAN_FIELD_ENCODING_SIZE + FIELD_NUMBER_ENCODING_SIZE) : 0;
+    static int getProtoHeaderSize(OperationType operation, int payloadSize,
+            boolean isPayloadEncrypted) {
+        int isPayloadEncryptedFieldSize = isPayloadEncrypted
+                ? BOOLEAN_FIELD_ENCODING_SIZE + FIELD_NUMBER_ENCODING_SIZE
+                : 0;
         int operationSize = getEncodedSize(operation.getNumber()) + FIELD_NUMBER_ENCODING_SIZE;
+
+        // The payload size is a varint.
+        int payloadEncodingSize = FIELD_NUMBER_ENCODING_SIZE + getEncodedSize(payloadSize);
+
         return CONSTANT_HEADER_FIELD_SIZE + operationSize + isPayloadEncryptedFieldSize
-                + ADDITIONAL_PAYLOAD_ENCODING_SIZE;
+                + payloadEncodingSize;
     }
 
     /**
diff --git a/service/src/com/android/car/trust/CarTrustAgentBleManager.java b/service/src/com/android/car/trust/CarTrustAgentBleManager.java
index 09c59e6..1e43300 100644
--- a/service/src/com/android/car/trust/CarTrustAgentBleManager.java
+++ b/service/src/com/android/car/trust/CarTrustAgentBleManager.java
@@ -57,7 +57,6 @@
  * the Trusted Device feature.
  */
 class CarTrustAgentBleManager extends BleManager {
-
     private static final String TAG = "CarTrustBLEManager";
 
     /**
@@ -70,6 +69,15 @@
     private static final UUID CLIENT_CHARACTERISTIC_CONFIG =
             UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
 
+    /**
+     * Reserved bytes for an ATT write request payload.
+     *
+     * <p>The attribute protocol uses 3 bytes to encode the command type and attribute ID. These
+     * bytes need to be subtracted from the reported MTU size and the resulting value will
+     * represent the total amount of bytes that can be sent in a write.
+     */
+    private static final int ATT_PAYLOAD_RESERVED_BYTES = 3;
+
     /** @hide */
     @IntDef(prefix = {"TRUSTED_DEVICE_OPERATION_"}, value = {
             TRUSTED_DEVICE_OPERATION_NONE,
@@ -94,7 +102,16 @@
     private String mOriginalBluetoothName;
     private byte[] mUniqueId;
     private String mEnrollmentDeviceName;
-    private int mMtuSize = 20;
+
+    /**
+     * The maximum amount of bytes that can be written over BLE.
+     *
+     * <p>This initial value is 20 because BLE has a default write of 23 bytes. However, 3 bytes
+     * are subtracted due to bytes being reserved for the command type and attribute ID.
+     *
+     * @see #ATT_PAYLOAD_RESERVED_BYTES
+     */
+    private int mMaxWriteSize = 20;
 
     // Enrollment Service and Characteristic UUIDs
     private UUID mEnrollmentServiceUuid;
@@ -169,7 +186,12 @@
 
     @Override
     protected void onMtuSizeChanged(int size) {
-        mMtuSize = size;
+        mMaxWriteSize = size - ATT_PAYLOAD_RESERVED_BYTES;
+
+        if (Log.isLoggable(TAG, Log.DEBUG)) {
+            Log.d(TAG, "MTU size changed to: " + size
+                    + "; setting max payload size to: " + mMaxWriteSize);
+        }
     }
 
     @Override
@@ -347,7 +369,8 @@
         // Characteristic the connected bluetooth device will write to.
         BluetoothGattCharacteristic clientCharacteristic =
                 new BluetoothGattCharacteristic(mEnrollmentClientWriteUuid,
-                        BluetoothGattCharacteristic.PROPERTY_WRITE,
+                        BluetoothGattCharacteristic.PROPERTY_WRITE
+                                | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE,
                         BluetoothGattCharacteristic.PERMISSION_WRITE);
 
         // Characteristic that this manager will write to.
@@ -380,7 +403,8 @@
         // Characteristic the connected bluetooth device will write to.
         BluetoothGattCharacteristic clientCharacteristic = new BluetoothGattCharacteristic(
                 mUnlockClientWriteUuid,
-                BluetoothGattCharacteristic.PROPERTY_WRITE,
+                BluetoothGattCharacteristic.PROPERTY_WRITE
+                        | BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE,
                 BluetoothGattCharacteristic.PERMISSION_WRITE);
 
         // Characteristic that this manager will write to.
@@ -524,7 +548,7 @@
         }
 
         List<BLEMessage> bleMessages = BLEMessageV1Factory.makeBLEMessages(message, operation,
-                mMtuSize, isPayloadEncrypted);
+                mMaxWriteSize, isPayloadEncrypted);
 
         if (Log.isLoggable(TAG, Log.DEBUG)) {
             Log.d(TAG, "sending " + bleMessages.size() + " messages to device");
diff --git a/tests/carservice_unit_test/src/com/android/car/CarLocationServiceTest.java b/tests/carservice_unit_test/src/com/android/car/CarLocationServiceTest.java
index c9e85e0..5384005 100644
--- a/tests/carservice_unit_test/src/com/android/car/CarLocationServiceTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/CarLocationServiceTest.java
@@ -415,13 +415,17 @@
 
     /**
      * Test that the {@link CarLocationService} deletes location_cache.json when the system resumes
-     * from suspend-to-ram.
+     * from suspend-to-ram if moving.
      */
     @Test
-    public void testDeletesCacheFileUponSuspendExit() throws Exception {
+    public void testDeletesCacheFileUponSuspendExitWhileMoving() throws Exception {
         mCarLocationService.init();
         when(mMockLocationManagerProxy.isLocationEnabled()).thenReturn(false);
+        when(mMockCarDrivingStateService.getCurrentDrivingState()).thenReturn(
+                new CarDrivingStateEvent(CarDrivingStateEvent.DRIVING_STATE_MOVING, 0));
+        writeCacheFile("{\"provider\":\"latitude\":16.7666,\"longitude\": \"accuracy\":1.0}");
         CompletableFuture<Void> future = new CompletableFuture<>();
+        assertTrue(getLocationCacheFile().exists());
 
         mCarLocationService.onStateChanged(CarPowerStateListener.SUSPEND_EXIT, future);
 
@@ -431,6 +435,26 @@
     }
 
     /**
+     * Test that the {@link CarLocationService} registers for driving state changes when the system
+     * resumes from suspend-to-ram.
+     */
+    @Test
+    public void testRegistersForDrivingStateChangesUponSuspendExit() throws Exception {
+        mCarLocationService.init();
+        when(mMockLocationManagerProxy.isLocationEnabled()).thenReturn(false);
+        when(mMockCarDrivingStateService.getCurrentDrivingState()).thenReturn(
+                new CarDrivingStateEvent(CarDrivingStateEvent.DRIVING_STATE_PARKED, 0));
+        CompletableFuture<Void> future = new CompletableFuture<>();
+
+        mCarLocationService.onStateChanged(CarPowerStateListener.SUSPEND_EXIT, future);
+
+        assertTrue(future.isDone());
+        verify(mMockLocationManagerProxy, times(0)).isLocationEnabled();
+        // One of the registrations should happen during init and another during onStateChanged.
+        verify(mMockCarDrivingStateService, times(2)).registerDrivingStateChangeListener(any());
+    }
+
+    /**
      * Test that the {@link CarLocationService} deletes location_cache.json when the car enters a
      * moving driving state.
      */
diff --git a/tests/carservice_unit_test/src/com/android/car/trust/BLEMessagePayloadStreamTest.java b/tests/carservice_unit_test/src/com/android/car/trust/BLEMessagePayloadStreamTest.java
index b65bce6..a99b1d7 100644
--- a/tests/carservice_unit_test/src/com/android/car/trust/BLEMessagePayloadStreamTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/trust/BLEMessagePayloadStreamTest.java
@@ -43,7 +43,7 @@
     private static final byte[] TEST_MESSAGE_PAYLOAD = "testMessage".getBytes();
     private static final int TEST_SINGLE_MESSAGE_SIZE =
             TEST_MESSAGE_PAYLOAD.length + BLEMessageV1Factory.getProtoHeaderSize(
-                    OPERATION_TYPE, IS_MESSAGE_ENCRYPTED);
+                    OPERATION_TYPE, TEST_MESSAGE_PAYLOAD.length, IS_MESSAGE_ENCRYPTED);
 
     private BLEMessagePayloadStream mBLEMessagePayloadStream;
     private List<BLEMessage> mBleMessages;