1 /*
2  * Copyright (C) 2023 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.deviceconfig.db;
18 
19 import android.content.Context;
20 import android.database.sqlite.SQLiteDatabase;
21 import android.database.sqlite.SQLiteOpenHelper;
22 import android.provider.BaseColumns;
23 
24 /**
25  * @hide
26  */
27 public class DeviceConfigDbHelper extends SQLiteOpenHelper {
28     public static final int DATABASE_VERSION = 1;
29     public static final String DATABASE_NAME = "config_infrastructure.db";
30 
31     /**
32      * TODO(b/265948914) / to consider:
33      *
34      * - enforce uniqueness of (namespace, key) pairs
35      * - synchronize calls that modify the db (maybe reads too?)
36      * - probably use a read/write lock
37      * - per-process caching of results so we don't go to the db every time
38      * - test the sql commands to make sure they work well (e.g. where clauses are
39      * written properly)
40      * - check the performance of the sql commands and look for optimizations
41      * - write a test for adapter.setProperties that has some but not all
42      * preexisting properties
43      * - Settings.Config has a concept "makeDefault" which is not implemented here
44      * - ensure that any sql exceptions are not thrown to the callers (where methods
45      * can return
46      * false)
47      * - see what happens if a caller starts observing changes before the database
48      * is loaded/ready (early in the boot process)
49      * - I've seen strict mode alerts about doing I/O in the main thread after a
50      * device boots. Maybe we can't avoid it but double check.
51      * - finish API implementation in DatabaseDataStore
52      */
53 
54     interface Contract {
55         class DeviceConfigEntry implements BaseColumns {
56             public static final String TABLE_NAME = "config";
57             public static final String COLUMN_NAME_NAMESPACE = "namespace";
58             public static final String COLUMN_NAME_KEY = "config_key";
59             public static final String COLUMN_NAME_VALUE = "config_value";
60         }
61     }
62 
63     private static final String SQL_CREATE_ENTRIES =
64             "CREATE TABLE " + Contract.DeviceConfigEntry.TABLE_NAME + " (" +
65                     Contract.DeviceConfigEntry._ID + " INTEGER PRIMARY KEY," +
66                     Contract.DeviceConfigEntry.COLUMN_NAME_NAMESPACE + " TEXT," +
67                     Contract.DeviceConfigEntry.COLUMN_NAME_KEY + " TEXT," +
68                     Contract.DeviceConfigEntry.COLUMN_NAME_VALUE + " TEXT)";
69 
DeviceConfigDbHelper(Context context)70     public DeviceConfigDbHelper(Context context) {
71         super(context, DATABASE_NAME, null, DATABASE_VERSION);
72     }
73 
74     @Override
onCreate(SQLiteDatabase db)75     public void onCreate(SQLiteDatabase db) {
76         db.execSQL(SQL_CREATE_ENTRIES);
77     }
78 
79     @Override
onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)80     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
81         // no op for now
82     }
83 
84 }
85