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

Commit d3ec82fb authored by Nicholas Ambur's avatar Nicholas Ambur
Browse files

add version entry to soundtrigger model database

Bug:147159435
Test: manual hotword trigger with upgraded database
&& dumpsys voiceinteraction

Change-Id: I45497c1159fe879e1de119a18aac8f7ecc2b0686
parent 2f1b6c41
Loading
Loading
Loading
Loading
+6 −4
Original line number Diff line number Diff line
@@ -4386,10 +4386,12 @@ package android.media.soundtrigger {
  }
  public static class SoundTriggerManager.Model {
    method public static android.media.soundtrigger.SoundTriggerManager.Model create(java.util.UUID, java.util.UUID, byte[]);
    method public byte[] getModelData();
    method public java.util.UUID getModelUuid();
    method public java.util.UUID getVendorUuid();
    method @NonNull public static android.media.soundtrigger.SoundTriggerManager.Model create(@NonNull java.util.UUID, @NonNull java.util.UUID, @Nullable byte[], int);
    method @NonNull public static android.media.soundtrigger.SoundTriggerManager.Model create(@NonNull java.util.UUID, @NonNull java.util.UUID, @Nullable byte[]);
    method @Nullable public byte[] getModelData();
    method @NonNull public java.util.UUID getModelUuid();
    method @NonNull public java.util.UUID getVendorUuid();
    method public int getVersion();
  }
}
+34 −9
Original line number Diff line number Diff line
@@ -265,16 +265,20 @@ public class SoundTrigger {
        @NonNull
        public final UUID vendorUuid;

        /** vendor specific version number of the model */
        public final int version;

        /** Opaque data. For use by vendor implementation and enrollment application */
        @UnsupportedAppUsage
        @NonNull
        public final byte[] data;

        public SoundModel(@NonNull UUID uuid, @Nullable UUID vendorUuid, int type,
                @Nullable byte[] data) {
                @Nullable byte[] data, int version) {
            this.uuid = requireNonNull(uuid);
            this.vendorUuid = vendorUuid != null ? vendorUuid : new UUID(0, 0);
            this.type = type;
            this.version = version;
            this.data = data != null ? data : new byte[0];
        }

@@ -282,6 +286,7 @@ public class SoundTrigger {
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + version;
            result = prime * result + Arrays.hashCode(data);
            result = prime * result + type;
            result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
@@ -312,6 +317,8 @@ public class SoundTrigger {
                return false;
            if (!Arrays.equals(data, other.data))
                return false;
            if (version != other.version)
                return false;
            return true;
        }
    }
@@ -461,14 +468,19 @@ public class SoundTrigger {
        @NonNull
        public final Keyphrase[] keyphrases; // keyword phrases in model

        @UnsupportedAppUsage
        public KeyphraseSoundModel(
                @NonNull UUID uuid, @NonNull UUID vendorUuid, @Nullable byte[] data,
                @Nullable Keyphrase[] keyphrases) {
            super(uuid, vendorUuid, TYPE_KEYPHRASE, data);
                @Nullable Keyphrase[] keyphrases, int version) {
            super(uuid, vendorUuid, TYPE_KEYPHRASE, data, version);
            this.keyphrases = keyphrases != null ? keyphrases : new Keyphrase[0];
        }

        @UnsupportedAppUsage
        public KeyphraseSoundModel(@NonNull UUID uuid, @NonNull UUID vendorUuid,
                @Nullable byte[] data, @Nullable Keyphrase[] keyphrases) {
            this(uuid, vendorUuid, data, keyphrases, -1);
        }

        public static final @android.annotation.NonNull Parcelable.Creator<KeyphraseSoundModel> CREATOR
                = new Parcelable.Creator<KeyphraseSoundModel>() {
            public KeyphraseSoundModel createFromParcel(Parcel in) {
@@ -487,9 +499,10 @@ public class SoundTrigger {
            if (length >= 0) {
                vendorUuid = UUID.fromString(in.readString());
            }
            int version = in.readInt();
            byte[] data = in.readBlob();
            Keyphrase[] keyphrases = in.createTypedArray(Keyphrase.CREATOR);
            return new KeyphraseSoundModel(uuid, vendorUuid, data, keyphrases);
            return new KeyphraseSoundModel(uuid, vendorUuid, data, keyphrases, version);
        }

        @Override
@@ -508,13 +521,16 @@ public class SoundTrigger {
            }
            dest.writeBlob(data);
            dest.writeTypedArray(keyphrases, flags);
            dest.writeInt(version);
        }

        @Override
        public String toString() {
            return "KeyphraseSoundModel [keyphrases=" + Arrays.toString(keyphrases)
                    + ", uuid=" + uuid + ", vendorUuid=" + vendorUuid
                    + ", type=" + type + ", data=" + (data == null ? 0 : data.length) + "]";
                    + ", type=" + type
                    + ", data=" + (data == null ? 0 : data.length)
                    + ", version=" + version + "]";
        }

        @Override
@@ -560,10 +576,15 @@ public class SoundTrigger {
            }
        };

        public GenericSoundModel(@NonNull UUID uuid, @NonNull UUID vendorUuid,
                @Nullable byte[] data, int version) {
            super(uuid, vendorUuid, TYPE_GENERIC_SOUND, data, version);
        }

        @UnsupportedAppUsage
        public GenericSoundModel(@NonNull UUID uuid, @NonNull UUID vendorUuid,
                @Nullable byte[] data) {
            super(uuid, vendorUuid, TYPE_GENERIC_SOUND, data);
            this(uuid, vendorUuid, data, -1);
        }

        @Override
@@ -579,7 +600,8 @@ public class SoundTrigger {
                vendorUuid = UUID.fromString(in.readString());
            }
            byte[] data = in.readBlob();
            return new GenericSoundModel(uuid, vendorUuid, data);
            int version = in.readInt();
            return new GenericSoundModel(uuid, vendorUuid, data, version);
        }

        @Override
@@ -592,12 +614,15 @@ public class SoundTrigger {
                dest.writeString(vendorUuid.toString());
            }
            dest.writeBlob(data);
            dest.writeInt(version);
        }

        @Override
        public String toString() {
            return "GenericSoundModel [uuid=" + uuid + ", vendorUuid=" + vendorUuid
                    + ", type=" + type + ", data=" + (data == null ? 0 : data.length) + "]";
                    + ", type=" + type
                    + ", data=" + (data == null ? 0 : data.length)
                    + ", version=" + version + "]";
        }
    }

+25 −3
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ import com.android.internal.app.ISoundTriggerService;
import com.android.internal.util.Preconditions;

import java.util.HashMap;
import java.util.Objects;
import java.util.UUID;

/**
@@ -175,19 +176,40 @@ public final class SoundTriggerManager {
         * Factory constructor to create a SoundModel instance for use with methods in this
         * class.
         */
        public static Model create(UUID modelUuid, UUID vendorUuid, byte[] data) {
            return new Model(new SoundTrigger.GenericSoundModel(modelUuid,
                        vendorUuid, data));
        @NonNull
        public static Model create(@NonNull UUID modelUuid, @NonNull UUID vendorUuid,
                @Nullable byte[] data, int version) {
            Objects.requireNonNull(modelUuid);
            Objects.requireNonNull(vendorUuid);
            return new Model(new SoundTrigger.GenericSoundModel(modelUuid, vendorUuid, data,
                    version));
        }

        /**
         * Factory constructor to create a SoundModel instance for use with methods in this
         * class.
         */
        @NonNull
        public static Model create(@NonNull UUID modelUuid, @NonNull UUID vendorUuid,
                @Nullable byte[] data) {
            return create(modelUuid, vendorUuid, data, -1);
        }

        @NonNull
        public UUID getModelUuid() {
            return mGenericSoundModel.uuid;
        }

        @NonNull
        public UUID getVendorUuid() {
            return mGenericSoundModel.vendorUuid;
        }

        public int getVersion() {
            return mGenericSoundModel.version;
        }

        @Nullable
        public byte[] getModelData() {
            return mGenericSoundModel.data;
        }
+60 −10
Original line number Diff line number Diff line
@@ -21,12 +21,10 @@ import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.hardware.soundtrigger.SoundTrigger;
import android.hardware.soundtrigger.SoundTrigger.GenericSoundModel;
import android.text.TextUtils;
import android.util.Slog;

import java.util.Locale;
import java.io.PrintWriter;
import java.util.UUID;

/**
@@ -39,7 +37,7 @@ public class SoundTriggerDbHelper extends SQLiteOpenHelper {
    static final boolean DBG = false;

    private static final String NAME = "st_sound_model.db";
    private static final int VERSION = 1;
    private static final int VERSION = 2;

    // Sound trigger-based sound models.
    public static interface GenericSoundModelContract {
@@ -47,15 +45,16 @@ public class SoundTriggerDbHelper extends SQLiteOpenHelper {
        public static final String KEY_MODEL_UUID = "model_uuid";
        public static final String KEY_VENDOR_UUID = "vendor_uuid";
        public static final String KEY_DATA = "data";
        public static final String KEY_MODEL_VERSION = "model_version";
    }


    // Table Create Statement for the sound trigger table
    private static final String CREATE_TABLE_ST_SOUND_MODEL = "CREATE TABLE "
            + GenericSoundModelContract.TABLE + "("
            + GenericSoundModelContract.KEY_MODEL_UUID + " TEXT PRIMARY KEY,"
            + GenericSoundModelContract.KEY_VENDOR_UUID + " TEXT,"
            + GenericSoundModelContract.KEY_DATA + " BLOB" + " )";
            + GenericSoundModelContract.KEY_DATA + " BLOB,"
            + GenericSoundModelContract.KEY_MODEL_VERSION + " INTEGER" + " )";


    public SoundTriggerDbHelper(Context context) {
@@ -70,9 +69,13 @@ public class SoundTriggerDbHelper extends SQLiteOpenHelper {

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO: For now, drop older tables and recreate new ones.
        db.execSQL("DROP TABLE IF EXISTS " + GenericSoundModelContract.TABLE);
        onCreate(db);
        if (oldVersion == 1) {
            // In version 2, a model version number was added.
            Slog.d(TAG, "Adding model version column");
            db.execSQL("ALTER TABLE " + GenericSoundModelContract.TABLE + " ADD COLUMN "
                    + GenericSoundModelContract.KEY_MODEL_VERSION + " INTEGER DEFAULT -1");
            oldVersion++;
        }
    }

    /**
@@ -86,6 +89,7 @@ public class SoundTriggerDbHelper extends SQLiteOpenHelper {
            values.put(GenericSoundModelContract.KEY_MODEL_UUID, soundModel.uuid.toString());
            values.put(GenericSoundModelContract.KEY_VENDOR_UUID, soundModel.vendorUuid.toString());
            values.put(GenericSoundModelContract.KEY_DATA, soundModel.data);
            values.put(GenericSoundModelContract.KEY_MODEL_VERSION, soundModel.version);

            try {
                return db.insertWithOnConflict(GenericSoundModelContract.TABLE, null, values,
@@ -113,8 +117,10 @@ public class SoundTriggerDbHelper extends SQLiteOpenHelper {
                                GenericSoundModelContract.KEY_DATA));
                        String vendor_uuid = c.getString(
                                c.getColumnIndex(GenericSoundModelContract.KEY_VENDOR_UUID));
                        int version = c.getInt(
                                c.getColumnIndex(GenericSoundModelContract.KEY_MODEL_VERSION));
                        return new GenericSoundModel(model_uuid, UUID.fromString(vendor_uuid),
                                data);
                                data, version);
                    } while (c.moveToNext());
                }
            } finally {
@@ -142,4 +148,48 @@ public class SoundTriggerDbHelper extends SQLiteOpenHelper {
            }
        }
    }

    public void dump(PrintWriter pw) {
        synchronized(this) {
            String selectQuery = "SELECT  * FROM " + GenericSoundModelContract.TABLE;
            SQLiteDatabase db = getReadableDatabase();
            Cursor c = db.rawQuery(selectQuery, null);
            try {
                pw.println("  Enrolled GenericSoundModels:");
                if (c.moveToFirst()) {
                    String[] columnNames = c.getColumnNames();
                    do {
                        for (String name : columnNames) {
                            int colNameIndex = c.getColumnIndex(name);
                            int type = c.getType(colNameIndex);
                            switch (type) {
                                case Cursor.FIELD_TYPE_STRING:
                                    pw.printf("    %s: %s\n", name,
                                            c.getString(colNameIndex));
                                    break;
                                case Cursor.FIELD_TYPE_BLOB:
                                    pw.printf("    %s: data blob\n", name);
                                    break;
                                case Cursor.FIELD_TYPE_INTEGER:
                                    pw.printf("    %s: %d\n", name,
                                            c.getInt(colNameIndex));
                                    break;
                                case Cursor.FIELD_TYPE_FLOAT:
                                    pw.printf("    %s: %f\n", name,
                                            c.getFloat(colNameIndex));
                                    break;
                                case Cursor.FIELD_TYPE_NULL:
                                    pw.printf("    %s: null\n", name);
                                    break;
                            }
                        }
                        pw.println();
                    } while (c.moveToNext());
                }
            } finally {
                c.close();
                db.close();
            }
        }
    }
}
+3 −0
Original line number Diff line number Diff line
@@ -1495,6 +1495,9 @@ public class SoundTriggerService extends SystemService {
            // log
            sEventLogger.dump(pw);

            // enrolled models
            mDbHelper.dump(pw);

            // stats
            mSoundModelStatTracker.dump(pw);
        }
Loading