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.AppSearchManager;
20 import android.app.appsearch.GlobalSearchSession;
21 import android.app.appsearch.SearchSpec;
22 import android.app.appsearch.exceptions.AppSearchException;
23 
24 import java.util.Objects;
25 import java.util.concurrent.Executor;
26 
27 public class SyncGlobalSearchSessionImpl extends SyncAppSearchBase
28         implements SyncGlobalSearchSession {
29 
30     private final GlobalSearchSession mGlobalSession;
31 
SyncGlobalSearchSessionImpl( @onNull AppSearchManager appSearchManager, @NonNull Executor executor)32     public SyncGlobalSearchSessionImpl(
33             @NonNull AppSearchManager appSearchManager, @NonNull Executor executor)
34             throws AppSearchException {
35         super(executor);
36         Objects.requireNonNull(appSearchManager);
37         Objects.requireNonNull(executor);
38 
39         mGlobalSession =
40                 executeAppSearchResultOperation(
41                         resultHandler ->
42                                 appSearchManager.createGlobalSearchSession(
43                                         executor, resultHandler));
44     }
45 
46     // Not actually asynchronous but added for convenience
47     @Override
48     @NonNull
search(@onNull String query, @NonNull SearchSpec searchSpec)49     public SyncSearchResults search(@NonNull String query, @NonNull SearchSpec searchSpec) {
50         Objects.requireNonNull(query);
51         Objects.requireNonNull(searchSpec);
52         return new SyncSearchResultsImpl(mGlobalSession.search(query, searchSpec), mExecutor);
53     }
54 
55     // Also not asynchronous but it's necessary to be able to close the session
56     @Override
close()57     public void close() {
58         mGlobalSession.close();
59     }
60 }
61