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 BatteryState} in the database. */
29 @Dao
30 public interface BatteryStateDao {
31 
32     /** Inserts a {@link BatteryState} data into the database. */
33     @Insert(onConflict = OnConflictStrategy.REPLACE)
insert(BatteryState state)34     void insert(BatteryState state);
35 
36     /** Inserts {@link BatteryState} data into the database. */
37     @Insert(onConflict = OnConflictStrategy.REPLACE)
insertAll(List<BatteryState> states)38     void insertAll(List<BatteryState> states);
39 
40     /** Gets the {@link Cursor} of the latest record timestamp no later than the given timestamp. */
41     @Query("SELECT MAX(timestamp) FROM BatteryState WHERE timestamp <= :timestamp")
getLatestTimestampBefore(long timestamp)42     Cursor getLatestTimestampBefore(long timestamp);
43 
44     /** Lists all recorded battery states after a specific timestamp. */
45     @Query("SELECT * FROM BatteryState WHERE timestamp >= :timestamp ORDER BY timestamp ASC")
getBatteryStatesAfter(long timestamp)46     Cursor getBatteryStatesAfter(long timestamp);
47 
48     /** Lists all recorded data after a specific timestamp. */
49     @Query("SELECT * FROM BatteryState WHERE timestamp > :timestamp ORDER BY timestamp DESC")
getAllAfter(long timestamp)50     List<BatteryState> getAllAfter(long timestamp);
51 
52     /** Get the count of distinct timestamp after a specific timestamp. */
53     @Query("SELECT COUNT(DISTINCT timestamp) FROM BatteryState WHERE timestamp > :timestamp")
getDistinctTimestampCount(long timestamp)54     int getDistinctTimestampCount(long timestamp);
55 
56     /** Lists all distinct timestamps after a specific timestamp. */
57     @Query("SELECT DISTINCT timestamp FROM BatteryState WHERE timestamp > :timestamp")
getDistinctTimestamps(long timestamp)58     List<Long> getDistinctTimestamps(long timestamp);
59 
60     /** Deletes all recorded data before a specific timestamp. */
61     @Query("DELETE FROM BatteryState WHERE timestamp <= :timestamp")
clearAllBefore(long timestamp)62     void clearAllBefore(long timestamp);
63 
64     /** Deletes all recorded data after a specific timestamp. */
65     @Query("DELETE FROM BatteryState WHERE timestamp >= :timestamp")
clearAllAfter(long timestamp)66     void clearAllAfter(long timestamp);
67 
68     /** Clears all recorded data in the database. */
69     @Query("DELETE FROM BatteryState")
clearAll()70     void clearAll();
71 }
72