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.adservices.data.kanon;
18 
19 import android.annotation.NonNull;
20 import android.content.Context;
21 
22 import androidx.room.Database;
23 import androidx.room.RoomDatabase;
24 import androidx.room.TypeConverters;
25 
26 import com.android.adservices.data.common.FledgeRoomConverters;
27 import com.android.adservices.service.common.compat.FileCompatUtils;
28 
29 import java.util.Objects;
30 
31 @Database(
32         entities = {DBServerParameters.class, DBClientParameters.class, DBKAnonMessage.class},
33         version = KAnonDatabase.DATABASE_VERSION)
34 @TypeConverters({FledgeRoomConverters.class})
35 public abstract class KAnonDatabase extends RoomDatabase {
36     private static final Object SINGLETON_LOCK = new Object();
37     public static final int DATABASE_VERSION = 1;
38     public static final String DATABASE_NAME = FileCompatUtils.getAdservicesFilename("kanon.db");
39 
40     public static volatile KAnonDatabase sSingleton = null;
41 
42     /** Returns an instance of the KAnonDatabase given a context. */
getInstance(@onNull Context context)43     public static KAnonDatabase getInstance(@NonNull Context context) {
44         Objects.requireNonNull(context, "Context must be provided.");
45         KAnonDatabase singleReadResult = sSingleton;
46         if (singleReadResult != null) {
47             return singleReadResult;
48         }
49 
50         synchronized (SINGLETON_LOCK) {
51             if (sSingleton == null) {
52                 sSingleton =
53                         FileCompatUtils.roomDatabaseBuilderHelper(
54                                         context, KAnonDatabase.class, DATABASE_NAME)
55                                 .fallbackToDestructiveMigration()
56                                 .build();
57             }
58             return sSingleton;
59         }
60     }
61 
62     /**
63      * @return a Dao to access entities in client_parameters.
64      */
clientParametersDao()65     public abstract ClientParametersDao clientParametersDao();
66 
67     /**
68      * @return a Dao to access entities in server_parameters.
69      */
serverParametersDao()70     public abstract ServerParametersDao serverParametersDao();
71 
72     /**
73      * @return a Dao to access entities in kanon_message table.
74      */
kAnonMessageDao()75     public abstract KAnonMessageDao kAnonMessageDao();
76 }
77