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.content.Context; 20 import android.util.Log; 21 22 import androidx.annotation.NonNull; 23 import androidx.room.Database; 24 import androidx.room.Room; 25 import androidx.room.RoomDatabase; 26 27 /** A {@link RoomDatabase} for battery usage states history. */ 28 @Database( 29 entities = { 30 AppUsageEventEntity.class, 31 BatteryEventEntity.class, 32 BatteryState.class, 33 BatteryUsageSlotEntity.class, 34 BatteryReattributeEntity.class 35 }, 36 version = 2) 37 public abstract class BatteryStateDatabase extends RoomDatabase { 38 private static final String TAG = "BatteryStateDatabase"; 39 private static final String DB_FILE_NAME = "battery-usage-db-v10"; 40 41 private static BatteryStateDatabase sBatteryStateDatabase; 42 43 /** Provides DAO for app usage event table. */ appUsageEventDao()44 public abstract AppUsageEventDao appUsageEventDao(); 45 46 /** Provides DAO for battery event table. */ batteryEventDao()47 public abstract BatteryEventDao batteryEventDao(); 48 49 /** Provides DAO for battery state table. */ batteryStateDao()50 public abstract BatteryStateDao batteryStateDao(); 51 52 /** Provides DAO for battery usage slot table. */ batteryUsageSlotDao()53 public abstract BatteryUsageSlotDao batteryUsageSlotDao(); 54 55 /** Provides DAO for battery reattribution table. */ 56 @NonNull batteryReattributeDao()57 public abstract BatteryReattributeDao batteryReattributeDao(); 58 59 /** Gets or creates an instance of {@link RoomDatabase}. */ getInstance(Context context)60 public static BatteryStateDatabase getInstance(Context context) { 61 if (sBatteryStateDatabase == null) { 62 sBatteryStateDatabase = 63 Room.databaseBuilder(context, BatteryStateDatabase.class, DB_FILE_NAME) 64 // Allows accessing data in the main thread for dumping bugreport. 65 .allowMainThreadQueries() 66 .fallbackToDestructiveMigration() 67 .build(); 68 Log.d(TAG, "initialize battery states database"); 69 } 70 return sBatteryStateDatabase; 71 } 72 73 /** Sets the instance of {@link RoomDatabase}. */ setBatteryStateDatabase(BatteryStateDatabase database)74 public static void setBatteryStateDatabase(BatteryStateDatabase database) { 75 BatteryStateDatabase.sBatteryStateDatabase = database; 76 } 77 } 78