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 AppUsageEventEntity} in the database. */ 29 @Dao 30 public interface AppUsageEventDao { 31 32 /** Inserts a {@link AppUsageEventEntity} data into the database. */ 33 @Insert(onConflict = OnConflictStrategy.REPLACE) insert(AppUsageEventEntity event)34 void insert(AppUsageEventEntity event); 35 36 /** Inserts {@link AppUsageEventEntity} data into the database. */ 37 @Insert(onConflict = OnConflictStrategy.REPLACE) insertAll(List<AppUsageEventEntity> events)38 void insertAll(List<AppUsageEventEntity> events); 39 40 /** Lists all recorded data after a specific timestamp. */ 41 @Query("SELECT * FROM AppUsageEventEntity WHERE timestamp > :timestamp ORDER BY timestamp DESC") getAllAfter(long timestamp)42 List<AppUsageEventEntity> getAllAfter(long timestamp); 43 44 /** Gets the {@link Cursor} of all recorded data after a specific timestamp of the users. */ 45 @Query("SELECT * FROM AppUsageEventEntity WHERE timestamp >= :timestamp" 46 + " AND userId IN (:userIds) ORDER BY timestamp ASC") getAllForUsersAfter(List<Long> userIds, long timestamp)47 Cursor getAllForUsersAfter(List<Long> userIds, long timestamp); 48 49 /** Gets the {@link Cursor} of the latest timestamp of the specific user. */ 50 @Query("SELECT MAX(timestamp) as timestamp FROM AppUsageEventEntity WHERE userId = :userId") getLatestTimestampOfUser(long userId)51 Cursor getLatestTimestampOfUser(long userId); 52 53 /** Deletes all recorded data before a specific timestamp. */ 54 @Query("DELETE FROM AppUsageEventEntity WHERE timestamp <= :timestamp") clearAllBefore(long timestamp)55 void clearAllBefore(long timestamp); 56 57 /** Clears all recorded data in the database. */ 58 @Query("DELETE FROM AppUsageEventEntity") clearAll()59 void clearAll(); 60 } 61