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 android.app.appsearch;
18 
19 import android.annotation.CallbackExecutor;
20 import android.annotation.FlaggedApi;
21 import android.annotation.NonNull;
22 import android.app.appsearch.aidl.AppSearchAttributionSource;
23 import android.app.appsearch.aidl.IAppSearchManager;
24 import android.os.UserHandle;
25 
26 import com.android.appsearch.flags.Flags;
27 
28 import java.util.concurrent.Executor;
29 import java.util.function.Consumer;
30 
31 /**
32  * Provides a connection to the work profile's AppSearch databases that explicitly allow access from
33  * enterprise sessions. Databases may have additional required permissions and restricted fields
34  * when accessed through an enterprise session that they normally would not have.
35  *
36  * <p>EnterpriseGlobalSearchSession will only return results when created from the main user context
37  * and when there is an associated work profile. If the given context is either not the main user or
38  * does not have a work profile, queries will successfully complete with empty results, allowing
39  * clients to query the work profile without having to account for whether it exists or not.
40  */
41 @FlaggedApi(Flags.FLAG_ENABLE_ENTERPRISE_GLOBAL_SEARCH_SESSION)
42 public class EnterpriseGlobalSearchSession extends ReadOnlyGlobalSearchSession {
43 
44     /**
45      * Creates an enterprise search session for the client, defined by the {@code userHandle} and
46      * {@code packageName}.
47      */
createEnterpriseGlobalSearchSession( @onNull IAppSearchManager service, @NonNull UserHandle userHandle, @NonNull AppSearchAttributionSource attributionSource, @NonNull @CallbackExecutor Executor executor, @NonNull Consumer<AppSearchResult<EnterpriseGlobalSearchSession>> callback)48     static void createEnterpriseGlobalSearchSession(
49             @NonNull IAppSearchManager service,
50             @NonNull UserHandle userHandle,
51             @NonNull AppSearchAttributionSource attributionSource,
52             @NonNull @CallbackExecutor Executor executor,
53             @NonNull Consumer<AppSearchResult<EnterpriseGlobalSearchSession>> callback) {
54         EnterpriseGlobalSearchSession enterpriseGlobalSearchSession =
55                 new EnterpriseGlobalSearchSession(service, userHandle, attributionSource);
56         enterpriseGlobalSearchSession.initialize(
57                 executor,
58                 result -> {
59                     if (result.isSuccess()) {
60                         callback.accept(
61                                 AppSearchResult.newSuccessfulResult(enterpriseGlobalSearchSession));
62                     } else {
63                         callback.accept(AppSearchResult.newFailedResult(result));
64                     }
65                 });
66     }
67 
EnterpriseGlobalSearchSession( @onNull IAppSearchManager service, @NonNull UserHandle userHandle, @NonNull AppSearchAttributionSource callerAttributionSource)68     private EnterpriseGlobalSearchSession(
69             @NonNull IAppSearchManager service,
70             @NonNull UserHandle userHandle,
71             @NonNull AppSearchAttributionSource callerAttributionSource) {
72         super(service, userHandle, callerAttributionSource, /* isForEnterprise= */ true);
73     }
74 }
75