Merge "Migrate battery usage DAO to Kotlin" into main

This commit is contained in:
Xinyi Mao
2024-06-17 08:47:06 +00:00
committed by Android (Google) Code Review
7 changed files with 212 additions and 232 deletions

View File

@@ -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<AppUsageEventEntity> events);
/** Lists all recorded data after a specific timestamp. */
@Query("SELECT * FROM AppUsageEventEntity WHERE timestamp > :timestamp ORDER BY timestamp DESC")
List<AppUsageEventEntity> 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<Long> 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();
}

View File

@@ -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<AppUsageEventEntity>)
/** Lists all recorded data after a specific timestamp. */
@Query("SELECT * FROM AppUsageEventEntity WHERE timestamp > :timestamp ORDER BY timestamp DESC")
fun getAllAfter(timestamp: Long): List<AppUsageEventEntity>
/** 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<Long>, 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()
}

View File

@@ -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<BatteryEventEntity> 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<Integer> batteryEventTypes);
/** Gets all recorded data after a specific timestamp for log.*/
@Query(
"SELECT * FROM BatteryEventEntity "
+ "WHERE timestamp >= :timestamp ORDER BY timestamp DESC")
List<BatteryEventEntity> 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();
}

View File

@@ -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<BatteryEventEntity>
/** 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<Int>): 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<BatteryEventEntity>
/** 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()
}

View File

@@ -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<BatteryState>)
/** Inserts {@link BatteryState} data into the database. */
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAll(List<BatteryState> 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<BatteryState> getAllAfter(long timestamp);
fun getAllAfter(timestamp: Long): List<BatteryState>
/** 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<Long> getDistinctTimestamps(long timestamp);
fun getDistinctTimestamps(timestamp: Long): List<Long>
/** 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()
}

View File

@@ -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<BatteryUsageSlotEntity> 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<BatteryUsageSlotEntity> 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();
}

View File

@@ -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<BatteryUsageSlotEntity>
/** 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<BatteryUsageSlotEntity>
/** 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()
}