1 /* 2 * Copyright (C) 2023 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.server.healthconnect.storage.datatypehelpers; 18 19 import android.annotation.NonNull; 20 21 import com.android.server.healthconnect.migration.PriorityMigrationHelper; 22 import com.android.server.healthconnect.storage.TransactionManager; 23 import com.android.server.healthconnect.storage.request.DeleteTableRequest; 24 25 import java.util.Set; 26 27 /** 28 * Parent class for the database helper classes containing common methods 29 * 30 * @hide 31 */ 32 public abstract class DatabaseHelper { 33 getDatabaseHelpers()34 private static Set<DatabaseHelper> getDatabaseHelpers() { 35 return Set.of( 36 DeviceInfoHelper.getInstance(), 37 AppInfoHelper.getInstance(), 38 new ActivityDateHelper(), 39 new ChangeLogsHelper(), 40 new ChangeLogsRequestHelper(), 41 HealthDataCategoryPriorityHelper.getInstance(), 42 PreferenceHelper.getInstance(), 43 new AccessLogsHelper(), 44 new MigrationEntityHelper(), 45 PriorityMigrationHelper.getInstance()); 46 } 47 48 /** 49 * Deletes all entries from the database for the helper class and clears the cache. This 50 * function is only used for testing, do not use in production. 51 */ clearAllData(@onNull TransactionManager transactionManager)52 public static void clearAllData(@NonNull TransactionManager transactionManager) { 53 for (DatabaseHelper databaseHelper : getDatabaseHelpers()) { 54 databaseHelper.clearData(transactionManager); 55 } 56 clearAllCache(); 57 } 58 clearAllCache()59 public static void clearAllCache() { 60 for (DatabaseHelper databaseHelper : getDatabaseHelpers()) { 61 databaseHelper.clearCache(); 62 } 63 } 64 clearData(@onNull TransactionManager transactionManager)65 protected void clearData(@NonNull TransactionManager transactionManager) { 66 transactionManager.delete(new DeleteTableRequest(getMainTableName())); 67 } 68 clearCache()69 protected void clearCache() {} 70 getMainTableName()71 protected abstract String getMainTableName(); 72 } 73