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.adservices.cobalt; 18 19 import android.content.Context; 20 21 import com.android.adservices.service.common.compat.FileCompatUtils; 22 import com.android.cobalt.data.CobaltDatabase; 23 import com.android.cobalt.data.DataService; 24 import com.android.cobalt.logging.CobaltOperationLogger; 25 26 import java.util.Objects; 27 import java.util.concurrent.ExecutorService; 28 29 /** 30 * Static data and functions related to the Cobalt data service. 31 * 32 * <p>This may makes sense to have in the Cobalt library itself if manual migrations are needed, but 33 * it will be owned by AdServices for now to make the database name and destructive migration 34 * explicit. 35 */ 36 final class CobaltDataServiceFactory { 37 private static final String DB_NAME = FileCompatUtils.getAdservicesFilename("cobalt_db"); 38 39 @SuppressWarnings("AvoidStaticContext") // Factory method createDataService( Context context, ExecutorService executorService, CobaltOperationLogger operationLogger)40 static DataService createDataService( 41 Context context, 42 ExecutorService executorService, 43 CobaltOperationLogger operationLogger) { 44 Objects.requireNonNull(context); 45 Objects.requireNonNull(executorService); 46 Objects.requireNonNull(operationLogger); 47 48 CobaltDatabase cobaltDatabase = 49 FileCompatUtils.roomDatabaseBuilderHelper(context, CobaltDatabase.class, DB_NAME) 50 .fallbackToDestructiveMigration() 51 .build(); 52 return new DataService(executorService, cobaltDatabase, operationLogger); 53 } 54 } 55