• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.room.Database;
23 import androidx.room.Room;
24 import androidx.room.RoomDatabase;
25 
26 /** A {@link RoomDatabase} for battery usage states history. */
27 @Database(
28         entities = {AppUsageEventEntity.class, BatteryEventEntity.class, BatteryState.class,
29                 BatteryUsageSlotEntity.class},
30         version = 1)
31 public abstract class BatteryStateDatabase extends RoomDatabase {
32     private static final String TAG = "BatteryStateDatabase";
33 
34     private static BatteryStateDatabase sBatteryStateDatabase;
35 
36     /** Provides DAO for app usage event table. */
appUsageEventDao()37     public abstract AppUsageEventDao appUsageEventDao();
38     /** Provides DAO for battery event table. */
batteryEventDao()39     public abstract BatteryEventDao batteryEventDao();
40     /** Provides DAO for battery state table. */
batteryStateDao()41     public abstract BatteryStateDao batteryStateDao();
42     /** Provides DAO for battery usage slot table. */
batteryUsageSlotDao()43     public abstract BatteryUsageSlotDao batteryUsageSlotDao();
44 
45     /** Gets or creates an instance of {@link RoomDatabase}. */
getInstance(Context context)46     public static BatteryStateDatabase getInstance(Context context) {
47         if (sBatteryStateDatabase == null) {
48             sBatteryStateDatabase =
49                     Room.databaseBuilder(
50                                     context, BatteryStateDatabase.class, "battery-usage-db-v9")
51                             // Allows accessing data in the main thread for dumping bugreport.
52                             .allowMainThreadQueries()
53                             .fallbackToDestructiveMigration()
54                             .build();
55             Log.d(TAG, "initialize battery states database");
56         }
57         return sBatteryStateDatabase;
58     }
59 
60     /** Sets the instance of {@link RoomDatabase}. */
setBatteryStateDatabase(BatteryStateDatabase database)61     public static void setBatteryStateDatabase(BatteryStateDatabase database) {
62         BatteryStateDatabase.sBatteryStateDatabase = database;
63     }
64 }
65