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 package com.android.server.appsearch.appsindexer;
17 
18 import android.annotation.NonNull;
19 import android.app.appsearch.AppSearchBatchResult;
20 import android.app.appsearch.AppSearchManager;
21 import android.app.appsearch.AppSearchSession;
22 import android.app.appsearch.PutDocumentsRequest;
23 import android.app.appsearch.SearchSpec;
24 import android.app.appsearch.SetSchemaRequest;
25 import android.app.appsearch.SetSchemaResponse;
26 import android.app.appsearch.exceptions.AppSearchException;
27 
28 import java.util.Objects;
29 import java.util.concurrent.Executor;
30 
31 /** SyncAppSearchSessionImpl methods are a super set of SyncGlobalSearchSessionImpl methods. */
32 public class SyncAppSearchSessionImpl extends SyncAppSearchBase implements SyncAppSearchSession {
33     private final AppSearchSession mSession;
34 
SyncAppSearchSessionImpl( @onNull AppSearchManager appSearchManager, @NonNull AppSearchManager.SearchContext searchContext, @NonNull Executor executor)35     public SyncAppSearchSessionImpl(
36             @NonNull AppSearchManager appSearchManager,
37             @NonNull AppSearchManager.SearchContext searchContext,
38             @NonNull Executor executor)
39             throws AppSearchException {
40         super(executor);
41         Objects.requireNonNull(appSearchManager);
42         Objects.requireNonNull(searchContext);
43         Objects.requireNonNull(executor);
44         mSession =
45                 executeAppSearchResultOperation(
46                         resultHandler ->
47                                 appSearchManager.createSearchSession(
48                                         searchContext, executor, resultHandler));
49     }
50 
51     // Not actually asynchronous but added for convenience
52     @Override
53     @NonNull
search(@onNull String query, @NonNull SearchSpec searchSpec)54     public SyncSearchResults search(@NonNull String query, @NonNull SearchSpec searchSpec) {
55         Objects.requireNonNull(query);
56         Objects.requireNonNull(searchSpec);
57         return new SyncSearchResultsImpl(mSession.search(query, searchSpec), mExecutor);
58     }
59 
60     @Override
61     @NonNull
setSchema(@onNull SetSchemaRequest setSchemaRequest)62     public SetSchemaResponse setSchema(@NonNull SetSchemaRequest setSchemaRequest)
63             throws AppSearchException {
64         Objects.requireNonNull(setSchemaRequest);
65         return executeAppSearchResultOperation(
66                 resultHandler ->
67                         mSession.setSchema(setSchemaRequest, mExecutor, mExecutor, resultHandler));
68     }
69 
70     // Put involves an AppSearchBatchResult, so it can't be simplified through
71     // executeAppSearchResultOperation. Instead we use executeAppSearchBatchResultOperation.
72     @Override
73     @NonNull
put(@onNull PutDocumentsRequest request)74     public AppSearchBatchResult<String, Void> put(@NonNull PutDocumentsRequest request)
75             throws AppSearchException {
76         Objects.requireNonNull(request);
77         return executeAppSearchBatchResultOperation(
78                 resultHandler -> mSession.put(request, mExecutor, resultHandler));
79     }
80 
81     // Also not asynchronous but it's necessary to be able to close the session
82     @Override
close()83     public void close() {
84         mSession.close();
85     }
86 }
87