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.rkpdapp.database;
18 
19 import android.content.Context;
20 
21 import androidx.room.Database;
22 import androidx.room.Room;
23 import androidx.room.RoomDatabase;
24 import androidx.room.TypeConverters;
25 
26 import com.android.rkpdapp.ThreadPool;
27 
28 /**
29  * Stores the remotely provisioned keys.
30  */
31 @Database(entities = {ProvisionedKey.class}, exportSchema = false, version = 1)
32 @TypeConverters({InstantConverter.class})
33 public abstract class RkpdDatabase extends RoomDatabase {
34     public static final String DB_NAME = "rkpd_database";
35     /**
36      * Provides the DAO object for easy queries.
37      */
provisionedKeyDao()38     public abstract ProvisionedKeyDao provisionedKeyDao();
39 
40     private static volatile RkpdDatabase sInstance;
41 
42     /**
43      * Gets the singleton instance for database.
44      */
getDatabase(final Context context)45     public static RkpdDatabase getDatabase(final Context context) {
46         RkpdDatabase result = sInstance;
47         if (result != null) {
48             return result;
49         }
50         synchronized (RkpdDatabase.class) {
51             if (sInstance == null) {
52                 sInstance = Room.databaseBuilder(context.getApplicationContext(),
53                                 RkpdDatabase.class, DB_NAME)
54                         .setQueryExecutor(ThreadPool.EXECUTOR)
55                         .build();
56             }
57             return sInstance;
58         }
59     }
60 }
61