Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3f4815df authored by Ugo Yu's avatar Ugo Yu
Browse files

Align with metadata API changes

- Fix typo on custom metadata keys.
- Upgrade Bluetooth database version to 102 and change
  custom metadata data type from string to byte array.

Bug: 124448651
Test: build pass, runtest bluetooth
Change-Id: Ic3946ac8890a90d4a65e8a333d6a3c93b3bfcf7c
parent 22857048
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
@@ -39,6 +39,8 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@@ -110,6 +112,24 @@ public final class Utils {
        return sb.toString();
    }

    /**
     * A parser to transfer a byte array to a UTF8 string
     *
     * @param valueBuf the byte array to transfer
     * @return the transferred UTF8 string
     */
    public static String byteArrayToUtf8String(byte[] valueBuf) {
        CharsetDecoder decoder = Charset.forName("UTF8").newDecoder();
        ByteBuffer byteBuffer = ByteBuffer.wrap(valueBuf);
        String valueStr = "";
        try {
            valueStr = decoder.decode(byteBuffer).toString();
        } catch (Exception ex) {
            Log.e(TAG, "Error when parsing byte array to UTF8 String. " + ex);
        }
        return valueStr;
    }

    public static byte[] intToByteArray(int value) {
        ByteBuffer converter = ByteBuffer.allocate(4);
        converter.order(ByteOrder.nativeOrder());
+7 −7
Original line number Diff line number Diff line
@@ -1694,7 +1694,7 @@ public class AdapterService extends Service {
        }

        @Override
        public boolean setMetadata(BluetoothDevice device, int key, String value) {
        public boolean setMetadata(BluetoothDevice device, int key, byte[] value) {
            AdapterService service = getService();
            if (service == null) {
                return false;
@@ -1703,7 +1703,7 @@ public class AdapterService extends Service {
        }

        @Override
        public String getMetadata(BluetoothDevice device, int key) {
        public byte[] getMetadata(BluetoothDevice device, int key) {
            AdapterService service = getService();
            if (service == null) {
                return null;
@@ -2718,15 +2718,15 @@ public class AdapterService extends Service {
        return true;
    }

    boolean setMetadata(BluetoothDevice device, int key, String value) {
        if (value.length() > BluetoothDevice.METADATA_MAX_LENGTH) {
            Log.e(TAG, "setMetadata: value length too long " + value.length());
    boolean setMetadata(BluetoothDevice device, int key, byte[] value) {
        if (value.length > BluetoothDevice.METADATA_MAX_LENGTH) {
            Log.e(TAG, "setMetadata: value length too long " + value.length);
            return false;
        }
        return mDatabaseManager.setCustomMeta(device, key, value);
    }

    String getMetadata(BluetoothDevice device, int key) {
    byte[] getMetadata(BluetoothDevice device, int key) {
        return mDatabaseManager.getCustomMeta(device, key);
    }

@@ -2734,7 +2734,7 @@ public class AdapterService extends Service {
     * Update metadata change to registered listeners
     */
    @VisibleForTesting
    public void metadataChanged(String address, int key, String value) {
    public void metadataChanged(String address, int key, byte[] value) {
        BluetoothDevice device = mRemoteDevices.getDevice(Utils.getBytesFromAddress(address));
        if (mMetadataListeners.containsKey(device)) {
            ArrayList<IBluetoothMetadataListener> list = mMetadataListeners.get(device);
+17 −17
Original line number Diff line number Diff line
@@ -20,21 +20,21 @@ import androidx.room.Entity;

@Entity
class CustomizedMetadataEntity {
    public String manufacturer_name;
    public String model_name;
    public String software_version;
    public String hardware_version;
    public String companion_app;
    public String main_icon;
    public String is_unthethered_headset;
    public String unthethered_left_icon;
    public String unthethered_right_icon;
    public String unthethered_case_icon;
    public String unthethered_left_battery;
    public String unthethered_right_battery;
    public String unthethered_case_battery;
    public String unthethered_left_charging;
    public String unthethered_right_charging;
    public String unthethered_case_charging;
    public String enhanced_settings_ui_uri;
    public byte[] manufacturer_name;
    public byte[] model_name;
    public byte[] software_version;
    public byte[] hardware_version;
    public byte[] companion_app;
    public byte[] main_icon;
    public byte[] is_untethered_headset;
    public byte[] untethered_left_icon;
    public byte[] untethered_right_icon;
    public byte[] untethered_case_icon;
    public byte[] untethered_left_battery;
    public byte[] untethered_right_battery;
    public byte[] untethered_case_battery;
    public byte[] untethered_left_charging;
    public byte[] untethered_right_charging;
    public byte[] untethered_case_charging;
    public byte[] enhanced_settings_ui_uri;
}
+18 −17
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import android.provider.Settings;
import android.util.Log;
import android.util.StatsLog;

import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.internal.annotations.VisibleForTesting;

@@ -184,16 +185,16 @@ public class DatabaseManager {
            case BluetoothDevice.METADATA_HARDWARE_VERSION:
            case BluetoothDevice.METADATA_COMPANION_APP:
            case BluetoothDevice.METADATA_MAIN_ICON:
            case BluetoothDevice.METADATA_IS_UNTHETHERED_HEADSET:
            case BluetoothDevice.METADATA_UNTHETHERED_LEFT_ICON:
            case BluetoothDevice.METADATA_UNTHETHERED_RIGHT_ICON:
            case BluetoothDevice.METADATA_UNTHETHERED_CASE_ICON:
            case BluetoothDevice.METADATA_UNTHETHERED_LEFT_BATTERY:
            case BluetoothDevice.METADATA_UNTHETHERED_RIGHT_BATTERY:
            case BluetoothDevice.METADATA_UNTHETHERED_CASE_BATTERY:
            case BluetoothDevice.METADATA_UNTHETHERED_LEFT_CHARGING:
            case BluetoothDevice.METADATA_UNTHETHERED_RIGHT_CHARGING:
            case BluetoothDevice.METADATA_UNTHETHERED_CASE_CHARGING:
            case BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET:
            case BluetoothDevice.METADATA_UNTETHERED_LEFT_ICON:
            case BluetoothDevice.METADATA_UNTETHERED_RIGHT_ICON:
            case BluetoothDevice.METADATA_UNTETHERED_CASE_ICON:
            case BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY:
            case BluetoothDevice.METADATA_UNTETHERED_RIGHT_BATTERY:
            case BluetoothDevice.METADATA_UNTETHERED_CASE_BATTERY:
            case BluetoothDevice.METADATA_UNTETHERED_LEFT_CHARGING:
            case BluetoothDevice.METADATA_UNTETHERED_RIGHT_CHARGING:
            case BluetoothDevice.METADATA_UNTETHERED_CASE_CHARGING:
            case BluetoothDevice.METADATA_ENHANCED_SETTINGS_UI_URI:
                return true;
        }
@@ -205,7 +206,7 @@ public class DatabaseManager {
     * Set customized metadata to database with requested key
     */
    @VisibleForTesting
    public boolean setCustomMeta(BluetoothDevice device, int key, String newValue) {
    public boolean setCustomMeta(BluetoothDevice device, int key, byte[] newValue) {
        synchronized (mMetadataCache) {
            if (device == null) {
                Log.e(TAG, "setCustomMeta: device is null");
@@ -224,8 +225,8 @@ public class DatabaseManager {
                createMetadata(address);
            }
            Metadata data = mMetadataCache.get(address);
            String oldValue = data.getCustomizedMeta(key);
            if (oldValue != null && oldValue.equals(newValue)) {
            byte[] oldValue = data.getCustomizedMeta(key);
            if (oldValue != null && Arrays.equals(oldValue, newValue)) {
                if (VERBOSE) {
                    Log.d(TAG, "setCustomMeta: metadata not changed.");
                }
@@ -244,7 +245,7 @@ public class DatabaseManager {
     * Get customized metadata from database with requested key
     */
    @VisibleForTesting
    public String getCustomMeta(BluetoothDevice device, int key) {
    public byte[] getCustomMeta(BluetoothDevice device, int key) {
        synchronized (mMetadataCache) {
            if (device == null) {
                Log.e(TAG, "getCustomMeta: device is null");
@@ -263,8 +264,7 @@ public class DatabaseManager {
            }

            Metadata data = mMetadataCache.get(address);
            String value = data.getCustomizedMeta(key);
            return value;
            return data.getCustomizedMeta(key);
        }
    }

@@ -763,13 +763,14 @@ public class DatabaseManager {
        mHandler.sendMessage(message);
    }

    private void logManufacturerInfo(BluetoothDevice device, int key, String value) {
    private void logManufacturerInfo(BluetoothDevice device, int key, byte[] bytesValue) {
        String callingApp = mAdapterService.getPackageManager().getNameForUid(
                Binder.getCallingUid());
        String manufacturerName = "";
        String modelName = "";
        String hardwareVersion = "";
        String softwareVersion = "";
        String value = Utils.byteArrayToUtf8String(bytesValue);
        switch (key) {
            case BluetoothDevice.METADATA_MANUFACTURER_NAME:
                manufacturerName = value;
+63 −63
Original line number Diff line number Diff line
@@ -132,7 +132,7 @@ class Metadata {
        return BluetoothProfile.PRIORITY_UNDEFINED;
    }

    void setCustomizedMeta(int key, String value) {
    void setCustomizedMeta(int key, byte[] value) {
        switch (key) {
            case BluetoothDevice.METADATA_MANUFACTURER_NAME:
                publicMetadata.manufacturer_name = value;
@@ -152,35 +152,35 @@ class Metadata {
            case BluetoothDevice.METADATA_MAIN_ICON:
                publicMetadata.main_icon = value;
                break;
            case BluetoothDevice.METADATA_IS_UNTHETHERED_HEADSET:
                publicMetadata.is_unthethered_headset = value;
            case BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET:
                publicMetadata.is_untethered_headset = value;
                break;
            case BluetoothDevice.METADATA_UNTHETHERED_LEFT_ICON:
                publicMetadata.unthethered_left_icon = value;
            case BluetoothDevice.METADATA_UNTETHERED_LEFT_ICON:
                publicMetadata.untethered_left_icon = value;
                break;
            case BluetoothDevice.METADATA_UNTHETHERED_RIGHT_ICON:
                publicMetadata.unthethered_right_icon = value;
            case BluetoothDevice.METADATA_UNTETHERED_RIGHT_ICON:
                publicMetadata.untethered_right_icon = value;
                break;
            case BluetoothDevice.METADATA_UNTHETHERED_CASE_ICON:
                publicMetadata.unthethered_case_icon = value;
            case BluetoothDevice.METADATA_UNTETHERED_CASE_ICON:
                publicMetadata.untethered_case_icon = value;
                break;
            case BluetoothDevice.METADATA_UNTHETHERED_LEFT_BATTERY:
                publicMetadata.unthethered_left_battery = value;
            case BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY:
                publicMetadata.untethered_left_battery = value;
                break;
            case BluetoothDevice.METADATA_UNTHETHERED_RIGHT_BATTERY:
                publicMetadata.unthethered_right_battery = value;
            case BluetoothDevice.METADATA_UNTETHERED_RIGHT_BATTERY:
                publicMetadata.untethered_right_battery = value;
                break;
            case BluetoothDevice.METADATA_UNTHETHERED_CASE_BATTERY:
                publicMetadata.unthethered_case_battery = value;
            case BluetoothDevice.METADATA_UNTETHERED_CASE_BATTERY:
                publicMetadata.untethered_case_battery = value;
                break;
            case BluetoothDevice.METADATA_UNTHETHERED_LEFT_CHARGING:
                publicMetadata.unthethered_left_charging = value;
            case BluetoothDevice.METADATA_UNTETHERED_LEFT_CHARGING:
                publicMetadata.untethered_left_charging = value;
                break;
            case BluetoothDevice.METADATA_UNTHETHERED_RIGHT_CHARGING:
                publicMetadata.unthethered_right_charging = value;
            case BluetoothDevice.METADATA_UNTETHERED_RIGHT_CHARGING:
                publicMetadata.untethered_right_charging = value;
                break;
            case BluetoothDevice.METADATA_UNTHETHERED_CASE_CHARGING:
                publicMetadata.unthethered_case_charging = value;
            case BluetoothDevice.METADATA_UNTETHERED_CASE_CHARGING:
                publicMetadata.untethered_case_charging = value;
                break;
            case BluetoothDevice.METADATA_ENHANCED_SETTINGS_UI_URI:
                publicMetadata.enhanced_settings_ui_uri = value;
@@ -188,8 +188,8 @@ class Metadata {
        }
    }

    String getCustomizedMeta(int key) {
        String value = null;
    byte[] getCustomizedMeta(int key) {
        byte[] value = null;
        switch (key) {
            case BluetoothDevice.METADATA_MANUFACTURER_NAME:
                value = publicMetadata.manufacturer_name;
@@ -209,35 +209,35 @@ class Metadata {
            case BluetoothDevice.METADATA_MAIN_ICON:
                value = publicMetadata.main_icon;
                break;
            case BluetoothDevice.METADATA_IS_UNTHETHERED_HEADSET:
                value = publicMetadata.is_unthethered_headset;
            case BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET:
                value = publicMetadata.is_untethered_headset;
                break;
            case BluetoothDevice.METADATA_UNTHETHERED_LEFT_ICON:
                value = publicMetadata.unthethered_left_icon;
            case BluetoothDevice.METADATA_UNTETHERED_LEFT_ICON:
                value = publicMetadata.untethered_left_icon;
                break;
            case BluetoothDevice.METADATA_UNTHETHERED_RIGHT_ICON:
                value = publicMetadata.unthethered_right_icon;
            case BluetoothDevice.METADATA_UNTETHERED_RIGHT_ICON:
                value = publicMetadata.untethered_right_icon;
                break;
            case BluetoothDevice.METADATA_UNTHETHERED_CASE_ICON:
                value = publicMetadata.unthethered_case_icon;
            case BluetoothDevice.METADATA_UNTETHERED_CASE_ICON:
                value = publicMetadata.untethered_case_icon;
                break;
            case BluetoothDevice.METADATA_UNTHETHERED_LEFT_BATTERY:
                value = publicMetadata.unthethered_left_battery;
            case BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY:
                value = publicMetadata.untethered_left_battery;
                break;
            case BluetoothDevice.METADATA_UNTHETHERED_RIGHT_BATTERY:
                value = publicMetadata.unthethered_right_battery;
            case BluetoothDevice.METADATA_UNTETHERED_RIGHT_BATTERY:
                value = publicMetadata.untethered_right_battery;
                break;
            case BluetoothDevice.METADATA_UNTHETHERED_CASE_BATTERY:
                value = publicMetadata.unthethered_case_battery;
            case BluetoothDevice.METADATA_UNTETHERED_CASE_BATTERY:
                value = publicMetadata.untethered_case_battery;
                break;
            case BluetoothDevice.METADATA_UNTHETHERED_LEFT_CHARGING:
                value = publicMetadata.unthethered_left_charging;
            case BluetoothDevice.METADATA_UNTETHERED_LEFT_CHARGING:
                value = publicMetadata.untethered_left_charging;
                break;
            case BluetoothDevice.METADATA_UNTHETHERED_RIGHT_CHARGING:
                value = publicMetadata.unthethered_right_charging;
            case BluetoothDevice.METADATA_UNTETHERED_RIGHT_CHARGING:
                value = publicMetadata.untethered_right_charging;
                break;
            case BluetoothDevice.METADATA_UNTHETHERED_CASE_CHARGING:
                value = publicMetadata.unthethered_case_charging;
            case BluetoothDevice.METADATA_UNTETHERED_CASE_CHARGING:
                value = publicMetadata.untethered_case_charging;
                break;
            case BluetoothDevice.METADATA_ENHANCED_SETTINGS_UI_URI:
                value = publicMetadata.enhanced_settings_ui_uri;
@@ -266,35 +266,35 @@ class Metadata {
        if (publicMetadata.main_icon != null) {
            list.add(BluetoothDevice.METADATA_MAIN_ICON);
        }
        if (publicMetadata.is_unthethered_headset != null) {
            list.add(BluetoothDevice.METADATA_IS_UNTHETHERED_HEADSET);
        if (publicMetadata.is_untethered_headset != null) {
            list.add(BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET);
        }
        if (publicMetadata.unthethered_left_icon != null) {
            list.add(BluetoothDevice.METADATA_UNTHETHERED_LEFT_ICON);
        if (publicMetadata.untethered_left_icon != null) {
            list.add(BluetoothDevice.METADATA_UNTETHERED_LEFT_ICON);
        }
        if (publicMetadata.unthethered_right_icon != null) {
            list.add(BluetoothDevice.METADATA_UNTHETHERED_RIGHT_ICON);
        if (publicMetadata.untethered_right_icon != null) {
            list.add(BluetoothDevice.METADATA_UNTETHERED_RIGHT_ICON);
        }
        if (publicMetadata.unthethered_case_icon != null) {
            list.add(BluetoothDevice.METADATA_UNTHETHERED_CASE_ICON);
        if (publicMetadata.untethered_case_icon != null) {
            list.add(BluetoothDevice.METADATA_UNTETHERED_CASE_ICON);
        }
        if (publicMetadata.unthethered_left_battery != null) {
            list.add(BluetoothDevice.METADATA_UNTHETHERED_LEFT_BATTERY);
        if (publicMetadata.untethered_left_battery != null) {
            list.add(BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY);
        }
        if (publicMetadata.unthethered_right_battery != null) {
            list.add(BluetoothDevice.METADATA_UNTHETHERED_RIGHT_BATTERY);
        if (publicMetadata.untethered_right_battery != null) {
            list.add(BluetoothDevice.METADATA_UNTETHERED_RIGHT_BATTERY);
        }
        if (publicMetadata.unthethered_case_battery != null) {
            list.add(BluetoothDevice.METADATA_UNTHETHERED_CASE_BATTERY);
        if (publicMetadata.untethered_case_battery != null) {
            list.add(BluetoothDevice.METADATA_UNTETHERED_CASE_BATTERY);
        }
        if (publicMetadata.unthethered_left_charging != null) {
            list.add(BluetoothDevice.METADATA_UNTHETHERED_LEFT_CHARGING);
        if (publicMetadata.untethered_left_charging != null) {
            list.add(BluetoothDevice.METADATA_UNTETHERED_LEFT_CHARGING);
        }
        if (publicMetadata.unthethered_right_charging != null) {
            list.add(BluetoothDevice.METADATA_UNTHETHERED_RIGHT_CHARGING);
        if (publicMetadata.untethered_right_charging != null) {
            list.add(BluetoothDevice.METADATA_UNTETHERED_RIGHT_CHARGING);
        }
        if (publicMetadata.unthethered_case_charging != null) {
            list.add(BluetoothDevice.METADATA_UNTHETHERED_CASE_CHARGING);
        if (publicMetadata.untethered_case_charging != null) {
            list.add(BluetoothDevice.METADATA_UNTETHERED_CASE_CHARGING);
        }
        if (publicMetadata.enhanced_settings_ui_uri != null) {
            list.add(BluetoothDevice.METADATA_ENHANCED_SETTINGS_UI_URI);
Loading