From b3f2e4d517db5554d7dedf840c37b21586633951 Mon Sep 17 00:00:00 2001 From: Chaohui Wang Date: Mon, 17 Jun 2024 12:13:18 +0800 Subject: [PATCH] Migrate battery usage DAO to Kotlin To better support null type safety, and fix build warnings. Bug: 332487783 Test: m Settings Change-Id: I00f3add356e3f825ab76aae4f81129eba348eca8 --- .../batteryusage/db/AppUsageEventDao.java | 65 ---------------- .../batteryusage/db/AppUsageEventDao.kt | 59 +++++++++++++++ .../batteryusage/db/BatteryEventDao.java | 75 ------------------- .../batteryusage/db/BatteryEventDao.kt | 73 ++++++++++++++++++ ...atteryStateDao.java => BatteryStateDao.kt} | 52 ++++++------- .../batteryusage/db/BatteryUsageSlotDao.java | 62 --------------- .../batteryusage/db/BatteryUsageSlotDao.kt | 58 ++++++++++++++ 7 files changed, 212 insertions(+), 232 deletions(-) delete mode 100644 src/com/android/settings/fuelgauge/batteryusage/db/AppUsageEventDao.java create mode 100644 src/com/android/settings/fuelgauge/batteryusage/db/AppUsageEventDao.kt delete mode 100644 src/com/android/settings/fuelgauge/batteryusage/db/BatteryEventDao.java create mode 100644 src/com/android/settings/fuelgauge/batteryusage/db/BatteryEventDao.kt rename src/com/android/settings/fuelgauge/batteryusage/db/{BatteryStateDao.java => BatteryStateDao.kt} (57%) delete mode 100644 src/com/android/settings/fuelgauge/batteryusage/db/BatteryUsageSlotDao.java create mode 100644 src/com/android/settings/fuelgauge/batteryusage/db/BatteryUsageSlotDao.kt diff --git a/src/com/android/settings/fuelgauge/batteryusage/db/AppUsageEventDao.java b/src/com/android/settings/fuelgauge/batteryusage/db/AppUsageEventDao.java deleted file mode 100644 index 249780125f2..00000000000 --- a/src/com/android/settings/fuelgauge/batteryusage/db/AppUsageEventDao.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (C) 2022 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings.fuelgauge.batteryusage.db; - -import android.database.Cursor; - -import androidx.room.Dao; -import androidx.room.Insert; -import androidx.room.OnConflictStrategy; -import androidx.room.Query; - -import java.util.List; - -/** Data access object for accessing {@link AppUsageEventEntity} in the database. */ -@Dao -public interface AppUsageEventDao { - - /** Inserts a {@link AppUsageEventEntity} data into the database. */ - @Insert(onConflict = OnConflictStrategy.REPLACE) - void insert(AppUsageEventEntity event); - - /** Inserts {@link AppUsageEventEntity} data into the database. */ - @Insert(onConflict = OnConflictStrategy.REPLACE) - void insertAll(List events); - - /** Lists all recorded data after a specific timestamp. */ - @Query("SELECT * FROM AppUsageEventEntity WHERE timestamp > :timestamp ORDER BY timestamp DESC") - List getAllAfter(long timestamp); - - /** Gets the {@link Cursor} of all recorded data after a specific timestamp of the users. */ - @Query( - "SELECT * FROM AppUsageEventEntity WHERE timestamp >= :timestamp" - + " AND userId IN (:userIds) ORDER BY timestamp ASC") - Cursor getAllForUsersAfter(List userIds, long timestamp); - - /** Gets the {@link Cursor} of the latest timestamp of the specific user. */ - @Query("SELECT MAX(timestamp) as timestamp FROM AppUsageEventEntity WHERE userId = :userId") - Cursor getLatestTimestampOfUser(long userId); - - /** Deletes all recorded data before a specific timestamp. */ - @Query("DELETE FROM AppUsageEventEntity WHERE timestamp <= :timestamp") - void clearAllBefore(long timestamp); - - /** Deletes all recorded data after a specific timestamp. */ - @Query("DELETE FROM AppUsageEventEntity WHERE timestamp >= :timestamp") - void clearAllAfter(long timestamp); - - /** Clears all recorded data in the database. */ - @Query("DELETE FROM AppUsageEventEntity") - void clearAll(); -} diff --git a/src/com/android/settings/fuelgauge/batteryusage/db/AppUsageEventDao.kt b/src/com/android/settings/fuelgauge/batteryusage/db/AppUsageEventDao.kt new file mode 100644 index 00000000000..fa5fbc710ec --- /dev/null +++ b/src/com/android/settings/fuelgauge/batteryusage/db/AppUsageEventDao.kt @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.settings.fuelgauge.batteryusage.db + +import android.database.Cursor +import androidx.room.Dao +import androidx.room.Insert +import androidx.room.OnConflictStrategy +import androidx.room.Query + +/** Data access object for accessing [AppUsageEventEntity] in the database. */ +@Dao +interface AppUsageEventDao { + /** Inserts a [AppUsageEventEntity] data into the database. */ + @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(event: AppUsageEventEntity) + + /** Inserts [AppUsageEventEntity] data into the database. */ + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun insertAll(events: List) + + /** Lists all recorded data after a specific timestamp. */ + @Query("SELECT * FROM AppUsageEventEntity WHERE timestamp > :timestamp ORDER BY timestamp DESC") + fun getAllAfter(timestamp: Long): List + + /** Gets the [Cursor] of all recorded data after a specific timestamp of the users. */ + @Query( + "SELECT * FROM AppUsageEventEntity WHERE timestamp >= :timestamp" + + " AND userId IN (:userIds) ORDER BY timestamp ASC" + ) + fun getAllForUsersAfter(userIds: List, timestamp: Long): Cursor + + /** Gets the [Cursor] of the latest timestamp of the specific user. */ + @Query("SELECT MAX(timestamp) as timestamp FROM AppUsageEventEntity WHERE userId = :userId") + fun getLatestTimestampOfUser(userId: Long): Cursor + + /** Deletes all recorded data before a specific timestamp. */ + @Query("DELETE FROM AppUsageEventEntity WHERE timestamp <= :timestamp") + fun clearAllBefore(timestamp: Long) + + /** Deletes all recorded data after a specific timestamp. */ + @Query("DELETE FROM AppUsageEventEntity WHERE timestamp >= :timestamp") + fun clearAllAfter(timestamp: Long) + + /** Clears all recorded data in the database. */ + @Query("DELETE FROM AppUsageEventEntity") fun clearAll() +} diff --git a/src/com/android/settings/fuelgauge/batteryusage/db/BatteryEventDao.java b/src/com/android/settings/fuelgauge/batteryusage/db/BatteryEventDao.java deleted file mode 100644 index 19d20438afa..00000000000 --- a/src/com/android/settings/fuelgauge/batteryusage/db/BatteryEventDao.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright (C) 2023 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings.fuelgauge.batteryusage.db; - -import android.database.Cursor; - -import androidx.room.Dao; -import androidx.room.Insert; -import androidx.room.OnConflictStrategy; -import androidx.room.Query; - -import java.util.List; - -/** Data access object for accessing {@link BatteryEventEntity} in the database. */ -@Dao -public interface BatteryEventDao { - /** Inserts a {@link BatteryEventEntity} data into the database. */ - @Insert(onConflict = OnConflictStrategy.REPLACE) - void insert(BatteryEventEntity event); - - /** Gets all recorded data. */ - @Query("SELECT * FROM BatteryEventEntity ORDER BY timestamp DESC") - List getAll(); - - /** Gets the {@link Cursor} of the last full charge time . */ - @Query( - "SELECT MAX(timestamp) FROM BatteryEventEntity" - + " WHERE batteryEventType = 3") // BatteryEventType.FULL_CHARGED = 3 - Cursor getLastFullChargeTimestamp(); - - /** Gets the {@link Long} of the last full charge time . */ - @Query( - "SELECT MAX(timestamp) FROM BatteryEventEntity" - + " WHERE batteryEventType = 3") // BatteryEventType.FULL_CHARGED = 3 - Long getLastFullChargeTimestampForLog(); - - /** Gets the {@link Cursor} of all recorded data after a specific timestamp. */ - @Query( - "SELECT * FROM BatteryEventEntity" - + " WHERE timestamp >= :timestamp AND batteryEventType IN (:batteryEventTypes)" - + " ORDER BY timestamp DESC") - Cursor getAllAfter(long timestamp, List batteryEventTypes); - - /** Gets all recorded data after a specific timestamp for log.*/ - @Query( - "SELECT * FROM BatteryEventEntity " - + "WHERE timestamp >= :timestamp ORDER BY timestamp DESC") - List getAllAfterForLog(long timestamp); - - /** Deletes all recorded data before a specific timestamp. */ - @Query("DELETE FROM BatteryEventEntity WHERE timestamp <= :timestamp") - void clearAllBefore(long timestamp); - - /** Deletes all recorded data after a specific timestamp. */ - @Query("DELETE FROM BatteryEventEntity WHERE timestamp >= :timestamp") - void clearAllAfter(long timestamp); - - /** Clears all recorded data in the database. */ - @Query("DELETE FROM BatteryEventEntity") - void clearAll(); -} diff --git a/src/com/android/settings/fuelgauge/batteryusage/db/BatteryEventDao.kt b/src/com/android/settings/fuelgauge/batteryusage/db/BatteryEventDao.kt new file mode 100644 index 00000000000..bac97d065d9 --- /dev/null +++ b/src/com/android/settings/fuelgauge/batteryusage/db/BatteryEventDao.kt @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.settings.fuelgauge.batteryusage.db + +import android.database.Cursor +import androidx.room.Dao +import androidx.room.Insert +import androidx.room.OnConflictStrategy +import androidx.room.Query + +/** Data access object for accessing [BatteryEventEntity] in the database. */ +@Dao +interface BatteryEventDao { + /** Inserts a [BatteryEventEntity] data into the database. */ + @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(event: BatteryEventEntity) + + /** Gets all recorded data. */ + @Query("SELECT * FROM BatteryEventEntity ORDER BY timestamp DESC") + fun getAll(): List + + /** Gets the [Cursor] of the last full charge time. */ + @Query( + "SELECT MAX(timestamp) FROM BatteryEventEntity" + + " WHERE batteryEventType = 3" // BatteryEventType.FULL_CHARGED = 3 + ) + fun getLastFullChargeTimestamp(): Cursor + + /** Gets the [Long] of the last full charge time. */ + @Query( + "SELECT MAX(timestamp) FROM BatteryEventEntity" + + " WHERE batteryEventType = 3" // BatteryEventType.FULL_CHARGED = 3 + ) + fun getLastFullChargeTimestampForLog(): Long? + + /** Gets the [Cursor] of all recorded data after a specific timestamp. */ + @Query( + "SELECT * FROM BatteryEventEntity" + + " WHERE timestamp >= :timestamp AND batteryEventType IN (:batteryEventTypes)" + + " ORDER BY timestamp DESC" + ) + fun getAllAfter(timestamp: Long, batteryEventTypes: List): Cursor + + /** Gets all recorded data after a specific timestamp for log. */ + @Query( + "SELECT * FROM BatteryEventEntity " + + "WHERE timestamp >= :timestamp ORDER BY timestamp DESC" + ) + fun getAllAfterForLog(timestamp: Long): List + + /** Deletes all recorded data before a specific timestamp. */ + @Query("DELETE FROM BatteryEventEntity WHERE timestamp <= :timestamp") + fun clearAllBefore(timestamp: Long) + + /** Deletes all recorded data after a specific timestamp. */ + @Query("DELETE FROM BatteryEventEntity WHERE timestamp >= :timestamp") + fun clearAllAfter(timestamp: Long) + + /** Clears all recorded data in the database. */ + @Query("DELETE FROM BatteryEventEntity") fun clearAll() +} diff --git a/src/com/android/settings/fuelgauge/batteryusage/db/BatteryStateDao.java b/src/com/android/settings/fuelgauge/batteryusage/db/BatteryStateDao.kt similarity index 57% rename from src/com/android/settings/fuelgauge/batteryusage/db/BatteryStateDao.java rename to src/com/android/settings/fuelgauge/batteryusage/db/BatteryStateDao.kt index 049251eb718..6d31e07432a 100644 --- a/src/com/android/settings/fuelgauge/batteryusage/db/BatteryStateDao.java +++ b/src/com/android/settings/fuelgauge/batteryusage/db/BatteryStateDao.kt @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 The Android Open Source Project + * Copyright (C) 2024 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,59 +13,51 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package com.android.settings.fuelgauge.batteryusage.db -package com.android.settings.fuelgauge.batteryusage.db; +import android.database.Cursor +import androidx.room.Dao +import androidx.room.Insert +import androidx.room.OnConflictStrategy +import androidx.room.Query -import android.database.Cursor; - -import androidx.room.Dao; -import androidx.room.Insert; -import androidx.room.OnConflictStrategy; -import androidx.room.Query; - -import java.util.List; - -/** Data access object for accessing {@link BatteryState} in the database. */ +/** Data access object for accessing [BatteryState] in the database. */ @Dao -public interface BatteryStateDao { +interface BatteryStateDao { + /** Inserts a [BatteryState] data into the database. */ + @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(state: BatteryState) - /** Inserts a {@link BatteryState} data into the database. */ - @Insert(onConflict = OnConflictStrategy.REPLACE) - void insert(BatteryState state); + /** Inserts [BatteryState] data into the database. */ + @Insert(onConflict = OnConflictStrategy.REPLACE) fun insertAll(states: List) - /** Inserts {@link BatteryState} data into the database. */ - @Insert(onConflict = OnConflictStrategy.REPLACE) - void insertAll(List states); - - /** Gets the {@link Cursor} of the latest record timestamp no later than the given timestamp. */ + /** Gets the [Cursor] of the latest record timestamp no later than the given timestamp. */ @Query("SELECT MAX(timestamp) FROM BatteryState WHERE timestamp <= :timestamp") - Cursor getLatestTimestampBefore(long timestamp); + fun getLatestTimestampBefore(timestamp: Long): Cursor /** Lists all recorded battery states after a specific timestamp. */ @Query("SELECT * FROM BatteryState WHERE timestamp >= :timestamp ORDER BY timestamp ASC") - Cursor getBatteryStatesAfter(long timestamp); + fun getBatteryStatesAfter(timestamp: Long): Cursor /** Lists all recorded data after a specific timestamp. */ @Query("SELECT * FROM BatteryState WHERE timestamp > :timestamp ORDER BY timestamp DESC") - List getAllAfter(long timestamp); + fun getAllAfter(timestamp: Long): List /** Get the count of distinct timestamp after a specific timestamp. */ @Query("SELECT COUNT(DISTINCT timestamp) FROM BatteryState WHERE timestamp > :timestamp") - int getDistinctTimestampCount(long timestamp); + fun getDistinctTimestampCount(timestamp: Long): Int /** Lists all distinct timestamps after a specific timestamp. */ @Query("SELECT DISTINCT timestamp FROM BatteryState WHERE timestamp > :timestamp") - List getDistinctTimestamps(long timestamp); + fun getDistinctTimestamps(timestamp: Long): List /** Deletes all recorded data before a specific timestamp. */ @Query("DELETE FROM BatteryState WHERE timestamp <= :timestamp") - void clearAllBefore(long timestamp); + fun clearAllBefore(timestamp: Long) /** Deletes all recorded data after a specific timestamp. */ @Query("DELETE FROM BatteryState WHERE timestamp >= :timestamp") - void clearAllAfter(long timestamp); + fun clearAllAfter(timestamp: Long) /** Clears all recorded data in the database. */ - @Query("DELETE FROM BatteryState") - void clearAll(); + @Query("DELETE FROM BatteryState") fun clearAll() } diff --git a/src/com/android/settings/fuelgauge/batteryusage/db/BatteryUsageSlotDao.java b/src/com/android/settings/fuelgauge/batteryusage/db/BatteryUsageSlotDao.java deleted file mode 100644 index d53b0cf2532..00000000000 --- a/src/com/android/settings/fuelgauge/batteryusage/db/BatteryUsageSlotDao.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (C) 2023 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings.fuelgauge.batteryusage.db; - -import android.database.Cursor; - -import androidx.room.Dao; -import androidx.room.Insert; -import androidx.room.OnConflictStrategy; -import androidx.room.Query; - -import java.util.List; - -/** Data access object for accessing {@link BatteryUsageSlotEntity} in the database. */ -@Dao -public interface BatteryUsageSlotDao { - /** Inserts a {@link BatteryUsageSlotEntity} data into the database. */ - @Insert(onConflict = OnConflictStrategy.REPLACE) - void insert(BatteryUsageSlotEntity event); - - /** Gets all recorded data. */ - @Query("SELECT * FROM BatteryUsageSlotEntity ORDER BY timestamp ASC") - List getAll(); - - /** Gets the {@link Cursor} of all recorded data after a specific timestamp. */ - @Query( - "SELECT * FROM BatteryUsageSlotEntity WHERE timestamp >= :timestamp" - + " ORDER BY timestamp ASC") - Cursor getAllAfter(long timestamp); - - /** Gets all recorded data after a specific timestamp for log.*/ - @Query( - "SELECT * FROM BatteryUsageSlotEntity WHERE timestamp >= :timestamp" - + " ORDER BY timestamp DESC") - List getAllAfterForLog(long timestamp); - - /** Deletes all recorded data before a specific timestamp. */ - @Query("DELETE FROM BatteryUsageSlotEntity WHERE timestamp <= :timestamp") - void clearAllBefore(long timestamp); - - /** Deletes all recorded data after a specific timestamp. */ - @Query("DELETE FROM BatteryUsageSlotEntity WHERE timestamp >= :timestamp") - void clearAllAfter(long timestamp); - - /** Clears all recorded data in the database. */ - @Query("DELETE FROM BatteryUsageSlotEntity") - void clearAll(); -} diff --git a/src/com/android/settings/fuelgauge/batteryusage/db/BatteryUsageSlotDao.kt b/src/com/android/settings/fuelgauge/batteryusage/db/BatteryUsageSlotDao.kt new file mode 100644 index 00000000000..434c61a46cd --- /dev/null +++ b/src/com/android/settings/fuelgauge/batteryusage/db/BatteryUsageSlotDao.kt @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.settings.fuelgauge.batteryusage.db + +import android.database.Cursor +import androidx.room.Dao +import androidx.room.Insert +import androidx.room.OnConflictStrategy +import androidx.room.Query + +/** Data access object for accessing [BatteryUsageSlotEntity] in the database. */ +@Dao +interface BatteryUsageSlotDao { + /** Inserts a [BatteryUsageSlotEntity] data into the database. */ + @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(event: BatteryUsageSlotEntity) + + /** Gets all recorded data. */ + @Query("SELECT * FROM BatteryUsageSlotEntity ORDER BY timestamp ASC") + fun getAll(): List + + /** Gets the [Cursor] of all recorded data after a specific timestamp. */ + @Query( + "SELECT * FROM BatteryUsageSlotEntity WHERE timestamp >= :timestamp" + + " ORDER BY timestamp ASC" + ) + fun getAllAfter(timestamp: Long): Cursor + + /** Gets all recorded data after a specific timestamp for log. */ + @Query( + "SELECT * FROM BatteryUsageSlotEntity WHERE timestamp >= :timestamp" + + " ORDER BY timestamp DESC" + ) + fun getAllAfterForLog(timestamp: Long): List + + /** Deletes all recorded data before a specific timestamp. */ + @Query("DELETE FROM BatteryUsageSlotEntity WHERE timestamp <= :timestamp") + fun clearAllBefore(timestamp: Long) + + /** Deletes all recorded data after a specific timestamp. */ + @Query("DELETE FROM BatteryUsageSlotEntity WHERE timestamp >= :timestamp") + fun clearAllAfter(timestamp: Long) + + /** Clears all recorded data in the database. */ + @Query("DELETE FROM BatteryUsageSlotEntity") fun clearAll() +}