• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.content.Context;
20 
21 import androidx.room.Database;
22 import androidx.room.RoomDatabase;
23 import androidx.room.TypeConverters;
24 
25 import com.android.adservices.data.common.FledgeRoomConverters;
26 import com.android.adservices.service.common.compat.FileCompatUtils;
27 import com.android.adservices.shared.common.ApplicationContextSingleton;
28 
29 import com.google.errorprone.annotations.concurrent.GuardedBy;
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     @GuardedBy("SINGLETON_LOCK")
41     public static volatile KAnonDatabase sSingleton;
42 
43     /** Returns an instance of the KAnonDatabase given a context. */
getInstance()44     public static KAnonDatabase getInstance() {
45         KAnonDatabase singleReadResult = sSingleton;
46         if (singleReadResult != null) {
47             return singleReadResult;
48         }
49 
50         synchronized (SINGLETON_LOCK) {
51             if (sSingleton == null) {
52                 Context context = ApplicationContextSingleton.get();
53                 sSingleton =
54                         FileCompatUtils.roomDatabaseBuilderHelper(
55                                         context, KAnonDatabase.class, DATABASE_NAME)
56                                 .fallbackToDestructiveMigration()
57                                 .build();
58             }
59             return sSingleton;
60         }
61     }
62 
63     /**
64      * @return a Dao to access entities in client_parameters.
65      */
clientParametersDao()66     public abstract ClientParametersDao clientParametersDao();
67 
68     /**
69      * @return a Dao to access entities in server_parameters.
70      */
serverParametersDao()71     public abstract ServerParametersDao serverParametersDao();
72 
73     /**
74      * @return a Dao to access entities in kanon_message table.
75      */
kAnonMessageDao()76     public abstract KAnonMessageDao kAnonMessageDao();
77 }
78