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 // @exportToFramework:skipFile() 17 package androidx.appsearch.app; 18 19 import android.content.Context; 20 import android.os.UserHandle; 21 22 import androidx.annotation.RestrictTo; 23 24 import org.jspecify.annotations.NonNull; 25 import org.jspecify.annotations.Nullable; 26 27 import java.io.File; 28 import java.util.concurrent.BlockingQueue; 29 import java.util.concurrent.ExecutorService; 30 import java.util.concurrent.Executors; 31 import java.util.concurrent.ThreadPoolExecutor; 32 import java.util.concurrent.TimeUnit; 33 34 /** 35 * Contains utility methods for Framework implementation of AppSearch. 36 * 37 * @exportToFramework:hide 38 */ 39 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) 40 public class JetpackAppSearchEnvironment implements AppSearchEnvironment { 41 42 /** 43 * Returns AppSearch directory in the credential encrypted system directory for the given user. 44 * 45 * <p>This folder should only be accessed after unlock. 46 */ 47 @Override getAppSearchDir(@onNull Context context, @Nullable UserHandle unused)48 public @NonNull File getAppSearchDir(@NonNull Context context, @Nullable UserHandle unused) { 49 return new File(context.getFilesDir(), "appsearch"); 50 } 51 52 /** Creates context for the user based on the userHandle. */ 53 @Override createContextAsUser(@onNull Context context, @NonNull UserHandle userHandle)54 public @NonNull Context createContextAsUser(@NonNull Context context, 55 @NonNull UserHandle userHandle) { 56 return context; 57 } 58 59 /** Creates and returns a ThreadPoolExecutor for given parameters. */ 60 @Override createExecutorService( int corePoolSize, int maxConcurrency, long keepAliveTime, @NonNull TimeUnit unit, @NonNull BlockingQueue<Runnable> workQueue, int priority)61 public @NonNull ExecutorService createExecutorService( 62 int corePoolSize, 63 int maxConcurrency, 64 long keepAliveTime, 65 @NonNull TimeUnit unit, 66 @NonNull BlockingQueue<Runnable> workQueue, 67 int priority) { 68 return new ThreadPoolExecutor( 69 corePoolSize, 70 maxConcurrency, 71 keepAliveTime, 72 unit, 73 workQueue); 74 } 75 76 /** Creates and returns an ExecutorService with a single thread. */ 77 @Override createSingleThreadExecutor()78 public @NonNull ExecutorService createSingleThreadExecutor() { 79 return Executors.newSingleThreadExecutor(); 80 } 81 82 /** Creates and returns an Executor with cached thread pools. */ 83 @Override createCachedThreadPoolExecutor()84 public @NonNull ExecutorService createCachedThreadPoolExecutor() { 85 return Executors.newCachedThreadPool(); 86 } 87 88 /** 89 * Returns a cache directory for creating temporary files like in case of migrating documents. 90 */ 91 @Override getCacheDir(@onNull Context context)92 public @Nullable File getCacheDir(@NonNull Context context) { 93 return context.getCacheDir(); 94 } 95 96 @Override isInfoLoggingEnabled()97 public boolean isInfoLoggingEnabled() { 98 // INFO logging is enabled by default in Jetpack AppSearch. 99 return true; 100 } 101 102 @Override 103 @EnvironmentType getEnvironment()104 public int getEnvironment() { 105 return AppSearchEnvironment.JETPACK_ENVIRONMENT; 106 } 107 } 108