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 package com.android.settings.fuelgauge.batteryusage.db 17 18 import android.database.Cursor 19 import androidx.room.Dao 20 import androidx.room.Insert 21 import androidx.room.OnConflictStrategy 22 import androidx.room.Query 23 import com.android.settings.fuelgauge.batteryusage.BatteryEventType 24 25 /** Data access object for accessing [BatteryEventEntity] in the database. */ 26 @Dao 27 interface BatteryEventDao { 28 /** Inserts a [BatteryEventEntity] data into the database. */ 29 @Insert(onConflict = OnConflictStrategy.REPLACE) insertnull30 fun insert(event: BatteryEventEntity) 31 32 /** Gets all recorded data. */ 33 @Query("SELECT * FROM BatteryEventEntity ORDER BY timestamp DESC") 34 fun getAll(): List<BatteryEventEntity> 35 36 /** Gets the [Cursor] of the last full charge time. */ 37 @Query( 38 "SELECT MAX(timestamp) FROM BatteryEventEntity" + 39 " WHERE batteryEventType = 3" // BatteryEventType.FULL_CHARGED = 3 40 ) 41 fun getLastFullChargeTimestamp(): Cursor 42 43 /** Gets the [Long] of the last full charge time. */ 44 @Query( 45 "SELECT MAX(timestamp) FROM BatteryEventEntity" + 46 " WHERE batteryEventType = 3" // BatteryEventType.FULL_CHARGED = 3 47 ) 48 fun getLastFullChargeTimestampForLog(): Long? 49 50 /** Gets the [Cursor] of all recorded data after a specific timestamp. */ 51 @Query( 52 "SELECT * FROM BatteryEventEntity" + 53 " WHERE timestamp >= :timestamp AND batteryEventType IN (:batteryEventTypes)" + 54 " ORDER BY timestamp DESC" 55 ) 56 fun getAllAfter(timestamp: Long, batteryEventTypes: List<Int>): Cursor 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" 62 ) 63 fun getAllAfterForLog(timestamp: Long): List<BatteryEventEntity> 64 65 /** Deletes all recorded data before a specific timestamp. */ 66 @Query("DELETE FROM BatteryEventEntity WHERE timestamp <= :timestamp") 67 fun clearAllBefore(timestamp: Long) 68 69 /** Deletes all recorded data after a specific timestamp. */ 70 @Query("DELETE FROM BatteryEventEntity WHERE timestamp >= :timestamp") 71 fun clearAllAfter(timestamp: Long) 72 73 /** Deletes even_hour event data in the database. */ 74 @Query( 75 "DELETE FROM BatteryEventEntity " + 76 "WHERE batteryEventType = 4" // BatteryEventType.EVEN_HOUR = 4 77 ) 78 fun clearEvenHourEvent() 79 80 /** Clears all recorded data in the database. */ 81 @Query("DELETE FROM BatteryEventEntity") 82 fun clearAll() 83 } 84