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.settings.fuelgauge.batteryusage.db;
18 
19 import android.database.Cursor;
20 
21 import androidx.room.Dao;
22 import androidx.room.Insert;
23 import androidx.room.OnConflictStrategy;
24 import androidx.room.Query;
25 
26 import java.util.List;
27 
28 /** Data access object for accessing {@link AppUsageEventEntity} in the database. */
29 @Dao
30 public interface AppUsageEventDao {
31 
32     /** Inserts a {@link AppUsageEventEntity} data into the database. */
33     @Insert(onConflict = OnConflictStrategy.REPLACE)
insert(AppUsageEventEntity event)34     void insert(AppUsageEventEntity event);
35 
36     /** Inserts {@link AppUsageEventEntity} data into the database. */
37     @Insert(onConflict = OnConflictStrategy.REPLACE)
insertAll(List<AppUsageEventEntity> events)38     void insertAll(List<AppUsageEventEntity> events);
39 
40     /** Lists all recorded data after a specific timestamp. */
41     @Query("SELECT * FROM AppUsageEventEntity WHERE timestamp > :timestamp ORDER BY timestamp DESC")
getAllAfter(long timestamp)42     List<AppUsageEventEntity> getAllAfter(long timestamp);
43 
44     /** Gets the {@link Cursor} of all recorded data after a specific timestamp of the users. */
45     @Query(
46             "SELECT * FROM AppUsageEventEntity WHERE timestamp >= :timestamp"
47                     + " AND userId IN (:userIds) ORDER BY timestamp ASC")
getAllForUsersAfter(List<Long> userIds, long timestamp)48     Cursor getAllForUsersAfter(List<Long> userIds, long timestamp);
49 
50     /** Gets the {@link Cursor} of the latest timestamp of the specific user. */
51     @Query("SELECT MAX(timestamp) as timestamp FROM AppUsageEventEntity WHERE userId = :userId")
getLatestTimestampOfUser(long userId)52     Cursor getLatestTimestampOfUser(long userId);
53 
54     /** Deletes all recorded data before a specific timestamp. */
55     @Query("DELETE FROM AppUsageEventEntity WHERE timestamp <= :timestamp")
clearAllBefore(long timestamp)56     void clearAllBefore(long timestamp);
57 
58     /** Deletes all recorded data after a specific timestamp. */
59     @Query("DELETE FROM AppUsageEventEntity WHERE timestamp >= :timestamp")
clearAllAfter(long timestamp)60     void clearAllAfter(long timestamp);
61 
62     /** Clears all recorded data in the database. */
63     @Query("DELETE FROM AppUsageEventEntity")
clearAll()64     void clearAll();
65 }
66