1 /*
2  * Copyright 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 androidx.appsearch.app;
18 
19 import android.content.Context;
20 import android.os.UserHandle;
21 
22 import androidx.annotation.IntDef;
23 import androidx.annotation.RestrictTo;
24 
25 import org.jspecify.annotations.NonNull;
26 import org.jspecify.annotations.Nullable;
27 
28 import java.io.File;
29 import java.lang.annotation.Retention;
30 import java.lang.annotation.RetentionPolicy;
31 import java.util.concurrent.BlockingQueue;
32 import java.util.concurrent.ExecutorService;
33 import java.util.concurrent.TimeUnit;
34 
35 /**
36  * An interface which exposes environment specific methods for AppSearch.
37  *
38  * @exportToFramework:hide
39  */
40 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
41 public interface AppSearchEnvironment {
42 
43     /** Returns the directory to initialize appsearch based on the environment. */
getAppSearchDir(@onNull Context context, @Nullable UserHandle userHandle)44     @NonNull File getAppSearchDir(@NonNull Context context, @Nullable UserHandle userHandle);
45 
46     /** Returns the correct context for the user based on the environment. */
createContextAsUser(@onNull Context context, @NonNull UserHandle userHandle)47     @NonNull Context createContextAsUser(@NonNull Context context, @NonNull UserHandle userHandle);
48 
49     /** Returns an ExecutorService based on given parameters. */
createExecutorService( int corePoolSize, int maxConcurrency, long keepAliveTime, @NonNull TimeUnit unit, @NonNull BlockingQueue<Runnable> workQueue, int priority)50     @NonNull ExecutorService createExecutorService(
51             int corePoolSize,
52             int maxConcurrency,
53             long keepAliveTime,
54             @NonNull TimeUnit unit,
55             @NonNull BlockingQueue<Runnable> workQueue,
56             int priority);
57 
58     /** Returns an ExecutorService with a single thread. */
createSingleThreadExecutor()59     @NonNull ExecutorService createSingleThreadExecutor();
60 
61     /** Creates and returns an Executor with cached thread pools. */
createCachedThreadPoolExecutor()62     @NonNull ExecutorService createCachedThreadPoolExecutor();
63 
64     /**
65      * Returns a cache directory for creating temporary files like in case of migrating documents.
66      */
getCacheDir(@onNull Context context)67     @Nullable File getCacheDir(@NonNull Context context);
68 
69     /** Returns if we can log INFO level logs. */
isInfoLoggingEnabled()70     boolean isInfoLoggingEnabled();
71 
72     /**
73      * The different environments that AppSearch code might be built in.
74      */
75     @Retention(RetentionPolicy.SOURCE)
76     @IntDef(value = {
77             JETPACK_ENVIRONMENT,
78             FRAMEWORK_ENVIRONMENT,
79             PLAY_SERVICES_ENVIRONMENT,
80     })
81     @interface EnvironmentType {
82     }
83 
84     /** This code is being built in the Jetpack Environment */
85     int JETPACK_ENVIRONMENT = 1;
86 
87     /** This code is being built in the Android Framework Environment */
88     int FRAMEWORK_ENVIRONMENT = 2;
89 
90     /** This code is being built in the internal environment for Play Services code. */
91     int PLAY_SERVICES_ENVIRONMENT = 3;
92 
93     /** Returns the {@code EnvironmentType} for this environment. */
getEnvironment()94     @EnvironmentType int getEnvironment();
95 }
96