1 /**
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.soundtrigger;
18 
19 import android.content.ContentValues;
20 import android.content.Context;
21 import android.database.Cursor;
22 import android.database.sqlite.SQLiteDatabase;
23 import android.database.sqlite.SQLiteOpenHelper;
24 import android.hardware.soundtrigger.SoundTrigger;
25 import android.hardware.soundtrigger.SoundTrigger.GenericSoundModel;
26 import android.text.TextUtils;
27 import android.util.Slog;
28 
29 import java.util.Locale;
30 import java.util.UUID;
31 
32 /**
33  * Helper to manage the database of the sound models that have been registered on the device.
34  *
35  * @hide
36  */
37 public class SoundTriggerDbHelper extends SQLiteOpenHelper {
38     static final String TAG = "SoundTriggerDbHelper";
39     static final boolean DBG = false;
40 
41     private static final String NAME = "st_sound_model.db";
42     private static final int VERSION = 1;
43 
44     // Sound trigger-based sound models.
45     public static interface GenericSoundModelContract {
46         public static final String TABLE = "st_sound_model";
47         public static final String KEY_MODEL_UUID = "model_uuid";
48         public static final String KEY_VENDOR_UUID = "vendor_uuid";
49         public static final String KEY_DATA = "data";
50     }
51 
52 
53     // Table Create Statement for the sound trigger table
54     private static final String CREATE_TABLE_ST_SOUND_MODEL = "CREATE TABLE "
55             + GenericSoundModelContract.TABLE + "("
56             + GenericSoundModelContract.KEY_MODEL_UUID + " TEXT PRIMARY KEY,"
57             + GenericSoundModelContract.KEY_VENDOR_UUID + " TEXT,"
58             + GenericSoundModelContract.KEY_DATA + " BLOB" + " )";
59 
60 
SoundTriggerDbHelper(Context context)61     public SoundTriggerDbHelper(Context context) {
62         super(context, NAME, null, VERSION);
63     }
64 
65     @Override
onCreate(SQLiteDatabase db)66     public void onCreate(SQLiteDatabase db) {
67         // creating required tables
68         db.execSQL(CREATE_TABLE_ST_SOUND_MODEL);
69     }
70 
71     @Override
onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)72     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
73         // TODO: For now, drop older tables and recreate new ones.
74         db.execSQL("DROP TABLE IF EXISTS " + GenericSoundModelContract.TABLE);
75         onCreate(db);
76     }
77 
78     /**
79      * Updates the given sound trigger model, adds it, if it doesn't already exist.
80      *
81      */
updateGenericSoundModel(GenericSoundModel soundModel)82     public boolean updateGenericSoundModel(GenericSoundModel soundModel) {
83         synchronized(this) {
84             SQLiteDatabase db = getWritableDatabase();
85             ContentValues values = new ContentValues();
86             values.put(GenericSoundModelContract.KEY_MODEL_UUID, soundModel.uuid.toString());
87             values.put(GenericSoundModelContract.KEY_VENDOR_UUID, soundModel.vendorUuid.toString());
88             values.put(GenericSoundModelContract.KEY_DATA, soundModel.data);
89 
90             try {
91                 return db.insertWithOnConflict(GenericSoundModelContract.TABLE, null, values,
92                         SQLiteDatabase.CONFLICT_REPLACE) != -1;
93             } finally {
94                 db.close();
95             }
96 
97         }
98     }
99 
getGenericSoundModel(UUID model_uuid)100     public GenericSoundModel getGenericSoundModel(UUID model_uuid) {
101         synchronized(this) {
102 
103             // Find the corresponding sound model ID for the keyphrase.
104             String selectQuery = "SELECT  * FROM " + GenericSoundModelContract.TABLE
105                     + " WHERE " + GenericSoundModelContract.KEY_MODEL_UUID + "= '" +
106                     model_uuid + "'";
107             SQLiteDatabase db = getReadableDatabase();
108             Cursor c = db.rawQuery(selectQuery, null);
109             try {
110                 if (c.moveToFirst()) {
111                     do {
112                         byte[] data = c.getBlob(c.getColumnIndex(
113                                 GenericSoundModelContract.KEY_DATA));
114                         String vendor_uuid = c.getString(
115                                 c.getColumnIndex(GenericSoundModelContract.KEY_VENDOR_UUID));
116                         return new GenericSoundModel(model_uuid, UUID.fromString(vendor_uuid),
117                                 data);
118                     } while (c.moveToNext());
119                 }
120             } finally {
121                 c.close();
122                 db.close();
123             }
124         }
125         return null;
126     }
127 
deleteGenericSoundModel(UUID model_uuid)128     public boolean deleteGenericSoundModel(UUID model_uuid) {
129         synchronized(this) {
130             GenericSoundModel soundModel = getGenericSoundModel(model_uuid);
131             if (soundModel == null) {
132                 return false;
133             }
134             // Delete all sound models for the given keyphrase and specified user.
135             SQLiteDatabase db = getWritableDatabase();
136             String soundModelClause = GenericSoundModelContract.KEY_MODEL_UUID
137                     + "='" + soundModel.uuid.toString() + "'";
138             try {
139                 return db.delete(GenericSoundModelContract.TABLE, soundModelClause, null) != 0;
140             } finally {
141                 db.close();
142             }
143         }
144     }
145 }
146