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

Commit 9e3ec9af authored by Nicholas Ambur's avatar Nicholas Ambur Committed by Android (Google) Code Review
Browse files

Merge "add version entry to soundtrigger model database"

parents dcc7e724 d3ec82fb
Loading
Loading
Loading
Loading
+6 −4
Original line number Diff line number Diff line
@@ -4404,10 +4404,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
@@ -303,16 +303,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];
        }

@@ -320,6 +324,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());
@@ -350,6 +355,8 @@ public class SoundTrigger {
                return false;
            if (!Arrays.equals(data, other.data))
                return false;
            if (version != other.version)
                return false;
            return true;
        }
    }
@@ -499,14 +506,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) {
@@ -525,9 +537,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
@@ -546,13 +559,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
@@ -598,10 +614,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
@@ -617,7 +638,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
@@ -630,12 +652,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