• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.ondevicepersonalization.services.data;
18 
19 import android.content.Context;
20 import android.database.sqlite.SQLiteDatabase;
21 import android.database.sqlite.SQLiteOpenHelper;
22 import android.util.Log;
23 
24 import com.android.internal.annotations.VisibleForTesting;
25 import com.android.ondevicepersonalization.services.data.events.EventsContract;
26 import com.android.ondevicepersonalization.services.data.events.QueriesContract;
27 import com.android.ondevicepersonalization.services.data.user.UserDataTables;
28 import com.android.ondevicepersonalization.services.data.vendor.VendorSettingsContract;
29 
30 /**
31  * Helper to manage the OnDevicePersonalization database.
32  */
33 public class OnDevicePersonalizationDbHelper extends SQLiteOpenHelper {
34 
35     private static final String TAG = "OnDevicePersonalizationDbHelper";
36 
37     private static final int DATABASE_VERSION = 1;
38     private static final String DATABASE_NAME = "ondevicepersonalization.db";
39 
40     private static OnDevicePersonalizationDbHelper sSingleton = null;
41 
OnDevicePersonalizationDbHelper(Context context, String dbName)42     private OnDevicePersonalizationDbHelper(Context context, String dbName) {
43         super(context, dbName, null, DATABASE_VERSION);
44     }
45 
46     /** Returns an instance of the OnDevicePersonalizationDbHelper given a context. */
getInstance(Context context)47     public static OnDevicePersonalizationDbHelper getInstance(Context context) {
48         synchronized (OnDevicePersonalizationDbHelper.class) {
49             if (sSingleton == null) {
50                 sSingleton = new OnDevicePersonalizationDbHelper(context, DATABASE_NAME);
51             }
52             return sSingleton;
53         }
54     }
55 
56     /**
57      * Returns an instance of the OnDevicePersonalizationDbHelper given a context. This is used
58      * for testing only.
59      */
60     @VisibleForTesting
getInstanceForTest(Context context)61     public static OnDevicePersonalizationDbHelper getInstanceForTest(Context context) {
62         synchronized (OnDevicePersonalizationDbHelper.class) {
63             if (sSingleton == null) {
64                 // Use null database name to make it in-memory
65                 sSingleton = new OnDevicePersonalizationDbHelper(context, null);
66             }
67             return sSingleton;
68         }
69     }
70 
71     @Override
onCreate(SQLiteDatabase db)72     public void onCreate(SQLiteDatabase db) {
73         db.execSQL(VendorSettingsContract.VendorSettingsEntry.CREATE_TABLE_STATEMENT);
74 
75         // Queries and events tables.
76         db.execSQL(QueriesContract.QueriesEntry.CREATE_TABLE_STATEMENT);
77         db.execSQL(EventsContract.EventsEntry.CREATE_TABLE_STATEMENT);
78 
79         // User data tables and indexes.
80         db.execSQL(UserDataTables.LocationHistory.CREATE_TABLE_STATEMENT);
81         db.execSQL(UserDataTables.LocationHistory.CREATE_INDEXES_STATEMENT);
82         db.execSQL(UserDataTables.AppUsageHistory.CREATE_TABLE_STATEMENT);
83         db.execSQL(UserDataTables.AppUsageHistory.CREATE_STARTING_TIME_SEC_INDEX_STATEMENT);
84         db.execSQL(UserDataTables.AppUsageHistory.CREATE_ENDING_TIME_SEC_INDEX_STATEMENT);
85         db.execSQL(UserDataTables.AppUsageHistory.CREATE_TOTAL_TIME_USED_SEC_INDEX_STATEMENT);
86     }
87 
88     @Override
onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)89     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
90         // TODO: handle upgrade when the db schema is changed.
91         Log.d(TAG, "DB upgrade from " + oldVersion + " to " + newVersion);
92         throw new UnsupportedOperationException(
93                 "Database upgrade for OnDevicePersonalization is unsupported");
94     }
95 
96     @Override
onConfigure(SQLiteDatabase db)97     public void onConfigure(SQLiteDatabase db) {
98         db.setForeignKeyConstraintsEnabled(true);
99         db.enableWriteAheadLogging();
100     }
101 }
102