Terry Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | |
| 20 | import android.annotation.CallbackExecutor; |
| 21 | import android.annotation.NonNull; |
| 22 | import android.os.RemoteException; |
| 23 | |
| 24 | import java.util.Objects; |
| 25 | import java.util.concurrent.Executor; |
| 26 | import java.util.function.Consumer; |
| 27 | |
| 28 | /** |
| 29 | * This class provides global access to the centralized AppSearch index maintained by the system. |
| 30 | * |
| 31 | * <p>Apps can retrieve indexed documents through the query API. |
| 32 | * @hide |
| 33 | */ |
| 34 | public class GlobalSearchSession { |
| 35 | |
| 36 | private final IAppSearchManager mService; |
| 37 | |
| 38 | static void createGlobalSearchSession( |
| 39 | @NonNull IAppSearchManager service, |
| 40 | @NonNull @CallbackExecutor Executor executor, |
| 41 | @NonNull Consumer<AppSearchResult<GlobalSearchSession>> callback) { |
| 42 | GlobalSearchSession globalSearchSession = new GlobalSearchSession(service); |
| 43 | globalSearchSession.initialize(executor, callback); |
| 44 | } |
| 45 | |
| 46 | // NOTE: No instance of this class should be created or returned except via initialize(). |
| 47 | // Once the callback.accept has been called here, the class is ready to use. |
| 48 | private void initialize( |
| 49 | @NonNull @CallbackExecutor Executor executor, |
| 50 | @NonNull Consumer<AppSearchResult<GlobalSearchSession>> callback) { |
| 51 | try { |
| 52 | mService.initialize(new IAppSearchResultCallback.Stub() { |
| 53 | public void onResult(AppSearchResult result) { |
| 54 | executor.execute(() -> { |
| 55 | if (result.isSuccess()) { |
| 56 | callback.accept( |
| 57 | AppSearchResult.newSuccessfulResult(GlobalSearchSession.this)); |
| 58 | } else { |
| 59 | callback.accept(result); |
| 60 | } |
| 61 | }); |
| 62 | } |
| 63 | }); |
| 64 | } catch (RemoteException e) { |
| 65 | throw e.rethrowFromSystemServer(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | private GlobalSearchSession(@NonNull IAppSearchManager service) { |
| 70 | mService = service; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Searches across all documents in the storage based on a given query string. |
| 75 | * |
| 76 | * <p>Currently we support following features in the raw query format: |
| 77 | * <ul> |
| 78 | * <li>AND |
| 79 | * <p>AND joins (e.g. “match documents that have both the terms ‘dog’ and |
| 80 | * ‘cat’”). |
| 81 | * Example: hello world matches documents that have both ‘hello’ and ‘world’ |
| 82 | * <li>OR |
| 83 | * <p>OR joins (e.g. “match documents that have either the term ‘dog’ or |
| 84 | * ‘cat’”). |
| 85 | * Example: dog OR puppy |
| 86 | * <li>Exclusion |
| 87 | * <p>Exclude a term (e.g. “match documents that do |
| 88 | * not have the term ‘dog’”). |
| 89 | * Example: -dog excludes the term ‘dog’ |
| 90 | * <li>Grouping terms |
| 91 | * <p>Allow for conceptual grouping of subqueries to enable hierarchical structures (e.g. |
| 92 | * “match documents that have either ‘dog’ or ‘puppy’, and either ‘cat’ or ‘kitten’”). |
| 93 | * Example: (dog puppy) (cat kitten) two one group containing two terms. |
| 94 | * <li>Property restricts |
| 95 | * <p> Specifies which properties of a document to specifically match terms in (e.g. |
| 96 | * “match documents where the ‘subject’ property contains ‘important’”). |
| 97 | * Example: subject:important matches documents with the term ‘important’ in the |
| 98 | * ‘subject’ property |
| 99 | * <li>Schema type restricts |
| 100 | * <p>This is similar to property restricts, but allows for restricts on top-level document |
| 101 | * fields, such as schema_type. Clients should be able to limit their query to documents of |
| 102 | * a certain schema_type (e.g. “match documents that are of the ‘Email’ schema_type”). |
| 103 | * Example: { schema_type_filters: “Email”, “Video”,query: “dog” } will match documents |
| 104 | * that contain the query term ‘dog’ and are of either the ‘Email’ schema type or the |
| 105 | * ‘Video’ schema type. |
| 106 | * </ul> |
| 107 | * |
| 108 | * <p> This method is lightweight. The heavy work will be done in |
| 109 | * {@link SearchResults#getNextPage}. |
| 110 | * |
| 111 | * @param queryExpression Query String to search. |
| 112 | * @param searchSpec Spec for setting filters, raw query etc. |
| 113 | * @param executor Executor on which to invoke the callback of the following request |
| 114 | * {@link SearchResults#getNextPage}. |
| 115 | * @return The search result of performing this operation. |
| 116 | */ |
| 117 | @NonNull |
| 118 | public SearchResults globalQuery( |
| 119 | @NonNull String queryExpression, |
| 120 | @NonNull SearchSpec searchSpec, |
| 121 | @NonNull @CallbackExecutor Executor executor) { |
| 122 | Objects.requireNonNull(queryExpression); |
| 123 | Objects.requireNonNull(searchSpec); |
| 124 | Objects.requireNonNull(executor); |
| 125 | return new SearchResults(mService, /*databaseName=*/null, queryExpression, |
| 126 | searchSpec, executor); |
| 127 | } |
| 128 | } |