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

Commit ecf3381a authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Upgrade metadata database for Fast Pair devices"

parents 546e5e25 2dc759c8
Loading
Loading
Loading
Loading
+22 −1
Original line number Diff line number Diff line
@@ -37,6 +37,13 @@ class CustomizedMetadataEntity {
    public byte[] untethered_right_charging;
    public byte[] untethered_case_charging;
    public byte[] enhanced_settings_ui_uri;
    public byte[] device_type;
    public byte[] main_battery;
    public byte[] main_charging;
    public byte[] main_low_battery_threshold;
    public byte[] untethered_left_low_battery_threshold;
    public byte[] untethered_right_low_battery_threshold;
    public byte[] untethered_case_low_battery_threshold;

    public String toString() {
        StringBuilder builder = new StringBuilder();
@@ -73,7 +80,21 @@ class CustomizedMetadataEntity {
                .append("|untethered_case_charging=")
                .append(metadataToString(untethered_case_charging))
                .append("|enhanced_settings_ui_uri=")
                .append(metadataToString(enhanced_settings_ui_uri));
                .append(metadataToString(enhanced_settings_ui_uri))
                .append("|device_type=")
                .append(metadataToString(device_type))
                .append("|main_battery=")
                .append(metadataToString(main_battery))
                .append("|main_charging=")
                .append(metadataToString(main_charging))
                .append("|main_low_battery_threshold=")
                .append(metadataToString(main_low_battery_threshold))
                .append("|untethered_left_low_battery_threshold=")
                .append(metadataToString(untethered_left_low_battery_threshold))
                .append("|untethered_right_low_battery_threshold=")
                .append(metadataToString(untethered_right_low_battery_threshold))
                .append("|untethered_case_low_battery_threshold=")
                .append(metadataToString(untethered_case_low_battery_threshold));

        return builder.toString();
    }
+2 −19
Original line number Diff line number Diff line
@@ -214,24 +214,7 @@ public class DatabaseManager {
    }

    boolean isValidMetaKey(int key) {
        switch (key) {
            case BluetoothDevice.METADATA_MANUFACTURER_NAME:
            case BluetoothDevice.METADATA_MODEL_NAME:
            case BluetoothDevice.METADATA_SOFTWARE_VERSION:
            case BluetoothDevice.METADATA_HARDWARE_VERSION:
            case BluetoothDevice.METADATA_COMPANION_APP:
            case BluetoothDevice.METADATA_MAIN_ICON:
            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:
        if (key >= 0 && key <= BluetoothDevice.getMaxMetadataKey()) {
            return true;
        }
        Log.w(TAG, "Invalid metadata key " + key);
+46 −50
Original line number Diff line number Diff line
@@ -198,6 +198,27 @@ class Metadata {
            case BluetoothDevice.METADATA_ENHANCED_SETTINGS_UI_URI:
                publicMetadata.enhanced_settings_ui_uri = value;
                break;
            case BluetoothDevice.METADATA_DEVICE_TYPE:
                publicMetadata.device_type = value;
                break;
            case BluetoothDevice.METADATA_MAIN_BATTERY:
                publicMetadata.main_battery = value;
                break;
            case BluetoothDevice.METADATA_MAIN_CHARGING:
                publicMetadata.main_charging = value;
                break;
            case BluetoothDevice.METADATA_MAIN_LOW_BATTERY_THRESHOLD:
                publicMetadata.main_low_battery_threshold = value;
                break;
            case BluetoothDevice.METADATA_UNTETHERED_LEFT_LOW_BATTERY_THRESHOLD:
                publicMetadata.untethered_left_low_battery_threshold = value;
                break;
            case BluetoothDevice.METADATA_UNTETHERED_RIGHT_LOW_BATTERY_THRESHOLD:
                publicMetadata.untethered_right_low_battery_threshold = value;
                break;
            case BluetoothDevice.METADATA_UNTETHERED_CASE_LOW_BATTERY_THRESHOLD:
                publicMetadata.untethered_case_low_battery_threshold = value;
                break;
        }
    }

@@ -255,62 +276,37 @@ class Metadata {
            case BluetoothDevice.METADATA_ENHANCED_SETTINGS_UI_URI:
                value = publicMetadata.enhanced_settings_ui_uri;
                break;
            case BluetoothDevice.METADATA_DEVICE_TYPE:
                value = publicMetadata.device_type;
                break;
            case BluetoothDevice.METADATA_MAIN_BATTERY:
                value = publicMetadata.main_battery;
                break;
            case BluetoothDevice.METADATA_MAIN_CHARGING:
                value = publicMetadata.main_charging;
                break;
            case BluetoothDevice.METADATA_MAIN_LOW_BATTERY_THRESHOLD:
                value = publicMetadata.main_low_battery_threshold;
                break;
            case BluetoothDevice.METADATA_UNTETHERED_LEFT_LOW_BATTERY_THRESHOLD:
                value = publicMetadata.untethered_left_low_battery_threshold;
                break;
            case BluetoothDevice.METADATA_UNTETHERED_RIGHT_LOW_BATTERY_THRESHOLD:
                value = publicMetadata.untethered_right_low_battery_threshold;
                break;
            case BluetoothDevice.METADATA_UNTETHERED_CASE_LOW_BATTERY_THRESHOLD:
                value = publicMetadata.untethered_case_low_battery_threshold;
                break;
        }
        return value;
    }

    List<Integer> getChangedCustomizedMeta() {
        List<Integer> list = new ArrayList<>();
        if (publicMetadata.manufacturer_name != null) {
            list.add(BluetoothDevice.METADATA_MANUFACTURER_NAME);
        }
        if (publicMetadata.model_name != null) {
            list.add(BluetoothDevice.METADATA_MODEL_NAME);
        }
        if (publicMetadata.software_version != null) {
            list.add(BluetoothDevice.METADATA_SOFTWARE_VERSION);
        }
        if (publicMetadata.hardware_version != null) {
            list.add(BluetoothDevice.METADATA_HARDWARE_VERSION);
        }
        if (publicMetadata.companion_app != null) {
            list.add(BluetoothDevice.METADATA_COMPANION_APP);
        }
        if (publicMetadata.main_icon != null) {
            list.add(BluetoothDevice.METADATA_MAIN_ICON);
        }
        if (publicMetadata.is_untethered_headset != null) {
            list.add(BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET);
        }
        if (publicMetadata.untethered_left_icon != null) {
            list.add(BluetoothDevice.METADATA_UNTETHERED_LEFT_ICON);
        }
        if (publicMetadata.untethered_right_icon != null) {
            list.add(BluetoothDevice.METADATA_UNTETHERED_RIGHT_ICON);
        }
        if (publicMetadata.untethered_case_icon != null) {
            list.add(BluetoothDevice.METADATA_UNTETHERED_CASE_ICON);
        }
        if (publicMetadata.untethered_left_battery != null) {
            list.add(BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY);
        }
        if (publicMetadata.untethered_right_battery != null) {
            list.add(BluetoothDevice.METADATA_UNTETHERED_RIGHT_BATTERY);
        }
        if (publicMetadata.untethered_case_battery != null) {
            list.add(BluetoothDevice.METADATA_UNTETHERED_CASE_BATTERY);
        }
        if (publicMetadata.untethered_left_charging != null) {
            list.add(BluetoothDevice.METADATA_UNTETHERED_LEFT_CHARGING);
        }
        if (publicMetadata.untethered_right_charging != null) {
            list.add(BluetoothDevice.METADATA_UNTETHERED_RIGHT_CHARGING);
        }
        if (publicMetadata.untethered_case_charging != null) {
            list.add(BluetoothDevice.METADATA_UNTETHERED_CASE_CHARGING);
        for (int key = 0; key <= BluetoothDevice.getMaxMetadataKey(); key++) {
            if (getCustomizedMeta(key) != null) {
                list.add(key);
            }
        if (publicMetadata.enhanced_settings_ui_uri != null) {
            list.add(BluetoothDevice.METADATA_ENHANCED_SETTINGS_UI_URI);
        }
        return list;
    }
+28 −1
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ import java.util.List;
/**
 * MetadataDatabase is a Room database stores Bluetooth persistence data
 */
@Database(entities = {Metadata.class}, version = 104)
@Database(entities = {Metadata.class}, version = 105)
public abstract class MetadataDatabase extends RoomDatabase {
    /**
     * The metadata database file name
@@ -57,6 +57,7 @@ public abstract class MetadataDatabase extends RoomDatabase {
                .addMigrations(MIGRATION_101_102)
                .addMigrations(MIGRATION_102_103)
                .addMigrations(MIGRATION_103_104)
                .addMigrations(MIGRATION_104_105)
                .allowMainThreadQueries()
                .build();
    }
@@ -303,4 +304,30 @@ public abstract class MetadataDatabase extends RoomDatabase {
            }
        }
    };

    @VisibleForTesting
    static final Migration MIGRATION_104_105 = new Migration(104, 105) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            try {
                database.execSQL("ALTER TABLE metadata ADD COLUMN `device_type` BLOB");
                database.execSQL("ALTER TABLE metadata ADD COLUMN `main_battery` BLOB");
                database.execSQL("ALTER TABLE metadata ADD COLUMN `main_charging` BLOB");
                database.execSQL("ALTER TABLE metadata ADD COLUMN "
                        + "`main_low_battery_threshold` BLOB");
                database.execSQL("ALTER TABLE metadata ADD COLUMN "
                        + "`untethered_left_low_battery_threshold` BLOB");
                database.execSQL("ALTER TABLE metadata ADD COLUMN "
                        + "`untethered_right_low_battery_threshold` BLOB");
                database.execSQL("ALTER TABLE metadata ADD COLUMN "
                        + "`untethered_case_low_battery_threshold` BLOB");
            } catch (SQLException ex) {
                // Check if user has new schema, but is just missing the version update
                Cursor cursor = database.query("SELECT * FROM metadata");
                if (cursor == null || cursor.getColumnIndex("device_type") == -1) {
                    throw ex;
                }
            }
        }
    };
}
+72 −0
Original line number Diff line number Diff line
@@ -364,6 +364,23 @@ public final class DatabaseManagerTest {
                value, true);
        testSetGetCustomMetaCase(false, BluetoothDevice.METADATA_ENHANCED_SETTINGS_UI_URI,
                value, true);
        testSetGetCustomMetaCase(false, BluetoothDevice.METADATA_DEVICE_TYPE,
                value, true);
        testSetGetCustomMetaCase(false, BluetoothDevice.METADATA_MAIN_BATTERY,
                value, true);
        testSetGetCustomMetaCase(false, BluetoothDevice.METADATA_MAIN_CHARGING,
                value, true);
        testSetGetCustomMetaCase(false, BluetoothDevice.METADATA_MAIN_LOW_BATTERY_THRESHOLD,
                value, true);
        testSetGetCustomMetaCase(false,
                BluetoothDevice.METADATA_UNTETHERED_LEFT_LOW_BATTERY_THRESHOLD,
                value, true);
        testSetGetCustomMetaCase(false,
                BluetoothDevice.METADATA_UNTETHERED_RIGHT_LOW_BATTERY_THRESHOLD,
                value, true);
        testSetGetCustomMetaCase(false,
                BluetoothDevice.METADATA_UNTETHERED_CASE_LOW_BATTERY_THRESHOLD,
                value, true);
        testSetGetCustomMetaCase(false, badKey, value, false);

        // Device is in database
@@ -401,6 +418,23 @@ public final class DatabaseManagerTest {
                value, true);
        testSetGetCustomMetaCase(true, BluetoothDevice.METADATA_ENHANCED_SETTINGS_UI_URI,
                value, true);
        testSetGetCustomMetaCase(true, BluetoothDevice.METADATA_DEVICE_TYPE,
                value, true);
        testSetGetCustomMetaCase(true, BluetoothDevice.METADATA_MAIN_BATTERY,
                value, true);
        testSetGetCustomMetaCase(true, BluetoothDevice.METADATA_MAIN_CHARGING,
                value, true);
        testSetGetCustomMetaCase(true, BluetoothDevice.METADATA_MAIN_LOW_BATTERY_THRESHOLD,
                value, true);
        testSetGetCustomMetaCase(true,
                BluetoothDevice.METADATA_UNTETHERED_LEFT_LOW_BATTERY_THRESHOLD,
                value, true);
        testSetGetCustomMetaCase(true,
                BluetoothDevice.METADATA_UNTETHERED_RIGHT_LOW_BATTERY_THRESHOLD,
                value, true);
        testSetGetCustomMetaCase(true,
                BluetoothDevice.METADATA_UNTETHERED_CASE_LOW_BATTERY_THRESHOLD,
                value, true);
    }

    @Test
@@ -922,6 +956,44 @@ public final class DatabaseManagerTest {
        }
    }

    @Test
    public void testDatabaseMigration_104_105() throws IOException {
        // Create a database with version 104
        SupportSQLiteDatabase db = testHelper.createDatabase(DB_NAME, 104);

        // insert a device to the database
        ContentValues device = new ContentValues();
        device.put("address", TEST_BT_ADDR);

        // Migrate database from 104 to 105
        db.close();
        db = testHelper.runMigrationsAndValidate(DB_NAME, 105, true,
                MetadataDatabase.MIGRATION_104_105);
        Cursor cursor = db.query("SELECT * FROM metadata");

        assertHasColumn(cursor, "device_type", true);
        assertHasColumn(cursor, "main_battery", true);
        assertHasColumn(cursor, "main_charging", true);
        assertHasColumn(cursor, "main_low_battery_threshold", true);
        assertHasColumn(cursor, "untethered_right_low_battery_threshold", true);
        assertHasColumn(cursor, "untethered_left_low_battery_threshold", true);
        assertHasColumn(cursor, "untethered_case_low_battery_threshold", true);

        while (cursor.moveToNext()) {
            // Check the old column have the original value
            assertColumnBlobData(cursor, "address", TEST_BT_ADDR.getBytes());

            // Check the new columns were added with their default values
            assertColumnBlobData(cursor, "device_type", null);
            assertColumnBlobData(cursor, "main_battery", null);
            assertColumnBlobData(cursor, "main_charging", null);
            assertColumnBlobData(cursor, "main_low_battery_threshold", null);
            assertColumnBlobData(cursor, "untethered_right_low_battery_threshold", null);
            assertColumnBlobData(cursor, "untethered_left_low_battery_threshold", null);
            assertColumnBlobData(cursor, "untethered_case_low_battery_threshold", null);
        }
    }

    /**
     * Helper function to check whether the database has the expected column
     */
Loading