1 /*
2  * Copyright (C) 2024 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.adselection;
18 
19 import android.annotation.NonNull;
20 
21 import androidx.annotation.Nullable;
22 import androidx.room.Dao;
23 import androidx.room.Insert;
24 import androidx.room.OnConflictStrategy;
25 import androidx.room.Query;
26 import androidx.room.Transaction;
27 
28 import java.time.Instant;
29 import java.util.List;
30 
31 /** Data Access Object interface for access to the local AdSelectionDebugReport data storage. */
32 @Dao
33 public abstract class ConsentedDebugConfigurationDao {
34 
35     /**
36      * Persists consented debug configuration in {@link DBConsentedDebugConfiguration.TABLE_NAME}
37      *
38      * @param dbConsentedDebugConfiguration is {@link DBConsentedDebugConfiguration} to add to the
39      *     table consented_debug_configuration.
40      */
41     @Insert(onConflict = OnConflictStrategy.REPLACE)
persistConsentedDebugConfiguration( @onNull DBConsentedDebugConfiguration dbConsentedDebugConfiguration)42     public abstract void persistConsentedDebugConfiguration(
43             @NonNull DBConsentedDebugConfiguration dbConsentedDebugConfiguration);
44 
45     /**
46      * Fetch the most recently created consented debug configurations whose expiry timestamp is
47      * after the given time.
48      *
49      * @param currentTime to compare against consented debug configuration expiry time
50      * @param limit to specify how many consented debug configurations should be selected from DB.
51      * @return All the consented debug configurations
52      */
53     @Query(
54             "SELECT * FROM consented_debug_configuration "
55                     + "WHERE expiry_timestamp > (:currentTime) "
56                     + "AND is_consent_provided = 1 "
57                     + "ORDER BY creation_timestamp desc "
58                     + "LIMIT (:limit);")
59     @Nullable
getAllActiveConsentedDebugConfigurations( @onNull Instant currentTime, int limit)60     public abstract List<DBConsentedDebugConfiguration> getAllActiveConsentedDebugConfigurations(
61             @NonNull Instant currentTime, int limit);
62 
63     /** deletes all consented debug configurations. */
64     @Query("DELETE FROM consented_debug_configuration")
deleteAllConsentedDebugConfigurations()65     public abstract void deleteAllConsentedDebugConfigurations();
66 
67     /**
68      * Deletes all existing {@link DBConsentedDebugConfiguration} and persist {@link
69      * DBConsentedDebugConfiguration}
70      *
71      * @param dbConsentedDebugConfiguration is {@link DBConsentedDebugConfiguration} to add to the
72      *     table consented_debug_configuration.
73      */
74     @Transaction
deleteExistingConsentedDebugConfigurationsAndPersist( @onNull DBConsentedDebugConfiguration dbConsentedDebugConfiguration)75     public void deleteExistingConsentedDebugConfigurationsAndPersist(
76             @NonNull DBConsentedDebugConfiguration dbConsentedDebugConfiguration) {
77         deleteAllConsentedDebugConfigurations();
78         persistConsentedDebugConfiguration(dbConsentedDebugConfiguration);
79     }
80 }
81