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 BatteryEventEntity} in the database. */
29 @Dao
30 public interface BatteryEventDao {
31     /** Inserts a {@link BatteryEventEntity} data into the database. */
32     @Insert(onConflict = OnConflictStrategy.REPLACE)
insert(BatteryEventEntity event)33     void insert(BatteryEventEntity event);
34 
35     /** Gets all recorded data. */
36     @Query("SELECT * FROM BatteryEventEntity ORDER BY timestamp DESC")
getAll()37     List<BatteryEventEntity> getAll();
38 
39     /** Gets the {@link Cursor} of the last full charge time . */
40     @Query(
41             "SELECT MAX(timestamp) FROM BatteryEventEntity"
42                     + " WHERE batteryEventType = 3") // BatteryEventType.FULL_CHARGED = 3
getLastFullChargeTimestamp()43     Cursor getLastFullChargeTimestamp();
44 
45     /** Gets the {@link Long} of the last full charge time . */
46     @Query(
47             "SELECT MAX(timestamp) FROM BatteryEventEntity"
48                     + " WHERE batteryEventType = 3") // BatteryEventType.FULL_CHARGED = 3
getLastFullChargeTimestampForLog()49     Long getLastFullChargeTimestampForLog();
50 
51     /** Gets the {@link Cursor} of all recorded data after a specific timestamp. */
52     @Query(
53             "SELECT * FROM BatteryEventEntity"
54                     + " WHERE timestamp >= :timestamp AND batteryEventType IN (:batteryEventTypes)"
55                     + " ORDER BY timestamp DESC")
getAllAfter(long timestamp, List<Integer> batteryEventTypes)56     Cursor getAllAfter(long timestamp, List<Integer> batteryEventTypes);
57 
58     /** Gets all recorded data after a specific timestamp for log.*/
59     @Query(
60             "SELECT * FROM BatteryEventEntity "
61                     + "WHERE timestamp >= :timestamp ORDER BY timestamp DESC")
getAllAfterForLog(long timestamp)62     List<BatteryEventEntity> getAllAfterForLog(long timestamp);
63 
64     /** Deletes all recorded data before a specific timestamp. */
65     @Query("DELETE FROM BatteryEventEntity WHERE timestamp <= :timestamp")
clearAllBefore(long timestamp)66     void clearAllBefore(long timestamp);
67 
68     /** Deletes all recorded data after a specific timestamp. */
69     @Query("DELETE FROM BatteryEventEntity WHERE timestamp >= :timestamp")
clearAllAfter(long timestamp)70     void clearAllAfter(long timestamp);
71 
72     /** Clears all recorded data in the database. */
73     @Query("DELETE FROM BatteryEventEntity")
clearAll()74     void clearAll();
75 }
76