1 /*
2  * Copyright (C) 2024 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.appsearch;
18 
19 import android.annotation.NonNull;
20 import android.content.Context;
21 
22 import com.android.internal.annotations.VisibleForTesting;
23 import com.android.server.appsearch.external.localstorage.visibilitystore.VisibilityChecker;
24 import com.android.server.appsearch.stats.PlatformLogger;
25 import com.android.server.appsearch.visibilitystore.VisibilityCheckerImpl;
26 
27 import java.util.concurrent.Executor;
28 
29 /** This is a factory class for implementations needed based on environment for service code. */
30 public final class AppSearchComponentFactory {
31     private static volatile ServiceAppSearchConfig sConfigInstance;
32 
33     /** Gets an instance of ServiceAppSearchConfig for the given executor. */
getConfigInstance(@onNull Executor executor)34     public static ServiceAppSearchConfig getConfigInstance(@NonNull Executor executor) {
35         ServiceAppSearchConfig localRef = sConfigInstance;
36         if (localRef == null) {
37             synchronized (AppSearchComponentFactory.class) {
38                 localRef = sConfigInstance;
39                 if (localRef == null) {
40                     sConfigInstance =
41                             localRef = FrameworkServiceAppSearchConfig.getInstance(executor);
42                 }
43             }
44         }
45         return localRef;
46     }
47 
48     @VisibleForTesting
setConfigInstanceForTest(@onNull ServiceAppSearchConfig appSearchConfig)49     static void setConfigInstanceForTest(@NonNull ServiceAppSearchConfig appSearchConfig) {
50         synchronized (AppSearchComponentFactory.class) {
51             sConfigInstance = appSearchConfig;
52         }
53     }
54 
createLoggerInstance( @onNull Context context, @NonNull ServiceAppSearchConfig config)55     public static InternalAppSearchLogger createLoggerInstance(
56             @NonNull Context context, @NonNull ServiceAppSearchConfig config) {
57         return new PlatformLogger(context, config);
58     }
59 
createVisibilityCheckerInstance(@onNull Context context)60     public static VisibilityChecker createVisibilityCheckerInstance(@NonNull Context context) {
61         return new VisibilityCheckerImpl(context);
62     }
63 
AppSearchComponentFactory()64     private AppSearchComponentFactory() {}
65 }
66