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.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 BatteryUsageSlotEntity} in the database. */
29 @Dao
30 public interface BatteryUsageSlotDao {
31     /** Inserts a {@link BatteryUsageSlotEntity} data into the database. */
32     @Insert(onConflict = OnConflictStrategy.REPLACE)
insert(BatteryUsageSlotEntity event)33     void insert(BatteryUsageSlotEntity event);
34 
35     /** Gets all recorded data. */
36     @Query("SELECT * FROM BatteryUsageSlotEntity ORDER BY timestamp ASC")
getAll()37     List<BatteryUsageSlotEntity> getAll();
38 
39     /** Gets the {@link Cursor} of all recorded data after a specific timestamp. */
40     @Query(
41             "SELECT * FROM BatteryUsageSlotEntity WHERE timestamp >= :timestamp"
42                     + " ORDER BY timestamp ASC")
getAllAfter(long timestamp)43     Cursor getAllAfter(long timestamp);
44 
45     /** Gets all recorded data after a specific timestamp for log.*/
46     @Query(
47             "SELECT * FROM BatteryUsageSlotEntity WHERE timestamp >= :timestamp"
48                     + " ORDER BY timestamp DESC")
getAllAfterForLog(long timestamp)49     List<BatteryUsageSlotEntity> getAllAfterForLog(long timestamp);
50 
51     /** Deletes all recorded data before a specific timestamp. */
52     @Query("DELETE FROM BatteryUsageSlotEntity WHERE timestamp <= :timestamp")
clearAllBefore(long timestamp)53     void clearAllBefore(long timestamp);
54 
55     /** Deletes all recorded data after a specific timestamp. */
56     @Query("DELETE FROM BatteryUsageSlotEntity WHERE timestamp >= :timestamp")
clearAllAfter(long timestamp)57     void clearAllAfter(long timestamp);
58 
59     /** Clears all recorded data in the database. */
60     @Query("DELETE FROM BatteryUsageSlotEntity")
clearAll()61     void clearAll();
62 }
63