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 android.app.appsearch;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.content.Context;
22 import android.os.Environment;
23 import android.os.UserHandle;
24 
25 import java.io.File;
26 import java.util.Objects;
27 import java.util.concurrent.BlockingQueue;
28 import java.util.concurrent.ExecutorService;
29 import java.util.concurrent.Executors;
30 import java.util.concurrent.ThreadPoolExecutor;
31 import java.util.concurrent.TimeUnit;
32 
33 /**
34  * Contains utility methods for Framework implementation of AppSearch.
35  *
36  * @hide
37  */
38 public class FrameworkAppSearchEnvironment implements AppSearchEnvironment {
39 
40     /**
41      * Returns AppSearch directory in the credential encrypted system directory for the given user.
42      *
43      * <p>This folder should only be accessed after unlock.
44      */
45     @Override
getAppSearchDir(@onNull Context unused, @NonNull UserHandle userHandle)46     public File getAppSearchDir(@NonNull Context unused, @NonNull UserHandle userHandle) {
47         // Duplicates the implementation of Environment#getDataSystemCeDirectory
48         // TODO(b/191059409): Unhide Environment#getDataSystemCeDirectory and switch to it.
49         Objects.requireNonNull(userHandle);
50         File systemCeDir = new File(Environment.getDataDirectory(), "system_ce");
51         File systemCeUserDir = new File(systemCeDir, String.valueOf(userHandle.getIdentifier()));
52         return new File(systemCeUserDir, "appsearch");
53     }
54 
55     /** Creates context for the user based on the userHandle. */
56     @Override
createContextAsUser(@onNull Context context, @NonNull UserHandle userHandle)57     public Context createContextAsUser(@NonNull Context context, @NonNull UserHandle userHandle) {
58         Objects.requireNonNull(context);
59         Objects.requireNonNull(userHandle);
60         return context.createContextAsUser(userHandle, /* flags= */ 0);
61     }
62 
63     /** Creates and returns a ThreadPoolExecutor for given parameters. */
64     @Override
createExecutorService( int corePoolSize, int maxConcurrency, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, int priority)65     public ExecutorService createExecutorService(
66             int corePoolSize,
67             int maxConcurrency,
68             long keepAliveTime,
69             TimeUnit unit,
70             BlockingQueue<Runnable> workQueue,
71             int priority) {
72         return new ThreadPoolExecutor(corePoolSize, maxConcurrency, keepAliveTime, unit, workQueue);
73     }
74 
75     /** Createsand returns an ExecutorService with a single thread. */
76     @Override
createSingleThreadExecutor()77     public ExecutorService createSingleThreadExecutor() {
78         return Executors.newSingleThreadExecutor();
79     }
80 
81     /** Creates and returns an Executor with cached thread pools. */
82     @NonNull
83     @Override
createCachedThreadPoolExecutor()84     public 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
92     @Nullable
getCacheDir(@onNull Context context)93     public File getCacheDir(@NonNull Context context) {
94         // Framework/Android does not have app-specific cache directory.
95         return null;
96     }
97 
98     /** Returns if we can log INFO level logs. */
99     @Override
isInfoLoggingEnabled()100     public boolean isInfoLoggingEnabled() {
101         return true;
102     }
103 }
104