Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 1 | /* |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 2 | * Copyright (C) 2020 The Android Open Source Project |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 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; |
| 17 | |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 18 | import android.annotation.NonNull; |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 19 | import android.app.appsearch.AppSearchBatchResult; |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 20 | import android.app.appsearch.AppSearchResult; |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 21 | import android.app.appsearch.AppSearchSchema; |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 22 | import android.app.appsearch.GenericDocument; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 23 | import android.app.appsearch.IAppSearchManager; |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame^] | 24 | import android.app.appsearch.SearchResultPage; |
Alexander Dorokhine | c9fc960 | 2020-10-06 01:39:50 -0700 | [diff] [blame] | 25 | import android.app.appsearch.SearchSpec; |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 26 | import android.app.appsearch.exceptions.AppSearchException; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 27 | import android.content.Context; |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 28 | import android.os.Binder; |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 29 | import android.os.Bundle; |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 30 | import android.os.UserHandle; |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame^] | 31 | import android.util.ArraySet; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 32 | |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 33 | import com.android.internal.infra.AndroidFuture; |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 34 | import com.android.internal.util.Preconditions; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 35 | import com.android.server.SystemService; |
Alexander Dorokhine | f660d8f | 2020-10-29 22:37:00 -0700 | [diff] [blame] | 36 | import com.android.server.appsearch.external.localstorage.AppSearchImpl; |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 37 | |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 38 | import java.io.IOException; |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 39 | import java.util.List; |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame^] | 40 | import java.util.Set; |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 41 | |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 42 | /** |
| 43 | * TODO(b/142567528): add comments when implement this class |
| 44 | */ |
| 45 | public class AppSearchManagerService extends SystemService { |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 46 | private static final String TAG = "AppSearchManagerService"; |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 47 | private static final char CALLING_NAME_DATABASE_DELIMITER = '$'; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 48 | |
| 49 | public AppSearchManagerService(Context context) { |
| 50 | super(context); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public void onStart() { |
| 55 | publishBinderService(Context.APP_SEARCH_SERVICE, new Stub()); |
| 56 | } |
| 57 | |
| 58 | private class Stub extends IAppSearchManager.Stub { |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 59 | @Override |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 60 | public void setSchema( |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 61 | @NonNull String databaseName, |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 62 | @NonNull List<Bundle> schemaBundles, |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 63 | boolean forceOverride, |
| 64 | @NonNull AndroidFuture<AppSearchResult> callback) { |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame^] | 65 | Preconditions.checkNotNull(databaseName); |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 66 | Preconditions.checkNotNull(schemaBundles); |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 67 | Preconditions.checkNotNull(callback); |
| 68 | int callingUid = Binder.getCallingUidOrThrow(); |
| 69 | int callingUserId = UserHandle.getUserId(callingUid); |
Jeff Sharkey | ea5328c | 2020-10-06 11:18:09 -0600 | [diff] [blame] | 70 | final long callingIdentity = Binder.clearCallingIdentity(); |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 71 | try { |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame^] | 72 | Set<AppSearchSchema> schemas = new ArraySet<>(schemaBundles.size()); |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 73 | for (int i = 0; i < schemaBundles.size(); i++) { |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame^] | 74 | schemas.add(new AppSearchSchema(schemaBundles.get(i))); |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 75 | } |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 76 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 77 | databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid); |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame^] | 78 | impl.setSchema(databaseName, schemas, forceOverride); |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 79 | callback.complete(AppSearchResult.newSuccessfulResult(/*result=*/ null)); |
Alexander Dorokhine | 179c8b8 | 2020-01-11 00:17:48 -0800 | [diff] [blame] | 80 | } catch (Throwable t) { |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 81 | callback.complete(throwableToFailedResult(t)); |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 82 | } finally { |
| 83 | Binder.restoreCallingIdentity(callingIdentity); |
Alexander Dorokhine | 179c8b8 | 2020-01-11 00:17:48 -0800 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | @Override |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 88 | public void putDocuments( |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 89 | @NonNull String databaseName, |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 90 | @NonNull List<Bundle> documentBundles, |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 91 | @NonNull AndroidFuture<AppSearchBatchResult> callback) { |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame^] | 92 | Preconditions.checkNotNull(databaseName); |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 93 | Preconditions.checkNotNull(documentBundles); |
Alexander Dorokhine | cc22345 | 2020-01-19 03:00:28 -0800 | [diff] [blame] | 94 | Preconditions.checkNotNull(callback); |
| 95 | int callingUid = Binder.getCallingUidOrThrow(); |
| 96 | int callingUserId = UserHandle.getUserId(callingUid); |
Jeff Sharkey | ea5328c | 2020-10-06 11:18:09 -0600 | [diff] [blame] | 97 | final long callingIdentity = Binder.clearCallingIdentity(); |
Alexander Dorokhine | 179c8b8 | 2020-01-11 00:17:48 -0800 | [diff] [blame] | 98 | try { |
Alexander Dorokhine | cc22345 | 2020-01-19 03:00:28 -0800 | [diff] [blame] | 99 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 100 | databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid); |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 101 | AppSearchBatchResult.Builder<String, Void> resultBuilder = |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 102 | new AppSearchBatchResult.Builder<>(); |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 103 | for (int i = 0; i < documentBundles.size(); i++) { |
| 104 | GenericDocument document = new GenericDocument(documentBundles.get(i)); |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 105 | try { |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame^] | 106 | impl.putDocument(databaseName, document); |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 107 | resultBuilder.setSuccess(document.getUri(), /*result=*/ null); |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 108 | } catch (Throwable t) { |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 109 | resultBuilder.setResult(document.getUri(), throwableToFailedResult(t)); |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | callback.complete(resultBuilder.build()); |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 113 | } catch (Throwable t) { |
| 114 | callback.completeExceptionally(t); |
Alexander Dorokhine | cc22345 | 2020-01-19 03:00:28 -0800 | [diff] [blame] | 115 | } finally { |
| 116 | Binder.restoreCallingIdentity(callingIdentity); |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 117 | } |
| 118 | } |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 119 | |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 120 | @Override |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 121 | public void getDocuments(@NonNull String databaseName, @NonNull String namespace, |
Alexander Dorokhine | a95f44f | 2020-03-06 13:53:14 -0800 | [diff] [blame] | 122 | @NonNull List<String> uris, @NonNull AndroidFuture<AppSearchBatchResult> callback) { |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame^] | 123 | Preconditions.checkNotNull(databaseName); |
| 124 | Preconditions.checkNotNull(namespace); |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 125 | Preconditions.checkNotNull(uris); |
| 126 | Preconditions.checkNotNull(callback); |
| 127 | int callingUid = Binder.getCallingUidOrThrow(); |
| 128 | int callingUserId = UserHandle.getUserId(callingUid); |
Jeff Sharkey | ea5328c | 2020-10-06 11:18:09 -0600 | [diff] [blame] | 129 | final long callingIdentity = Binder.clearCallingIdentity(); |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 130 | try { |
| 131 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 132 | databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid); |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 133 | AppSearchBatchResult.Builder<String, Bundle> resultBuilder = |
Alexander Dorokhine | a95f44f | 2020-03-06 13:53:14 -0800 | [diff] [blame] | 134 | new AppSearchBatchResult.Builder<>(); |
| 135 | for (int i = 0; i < uris.size(); i++) { |
| 136 | String uri = uris.get(i); |
| 137 | try { |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame^] | 138 | GenericDocument document = impl.getDocument(databaseName, namespace, uri); |
| 139 | resultBuilder.setSuccess(uri, document.getBundle()); |
Alexander Dorokhine | a95f44f | 2020-03-06 13:53:14 -0800 | [diff] [blame] | 140 | } catch (Throwable t) { |
| 141 | resultBuilder.setResult(uri, throwableToFailedResult(t)); |
| 142 | } |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 143 | } |
Alexander Dorokhine | a95f44f | 2020-03-06 13:53:14 -0800 | [diff] [blame] | 144 | callback.complete(resultBuilder.build()); |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 145 | } catch (Throwable t) { |
| 146 | callback.completeExceptionally(t); |
| 147 | } finally { |
| 148 | Binder.restoreCallingIdentity(callingIdentity); |
| 149 | } |
| 150 | } |
| 151 | |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 152 | // TODO(sidchhabra): Do this in a threadpool. |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame^] | 153 | // TODO(b/162450968) handle pagination after getNextPage and SearchResults is ready. |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 154 | @Override |
Alexander Dorokhine | e708e18 | 2020-03-06 15:30:34 -0800 | [diff] [blame] | 155 | public void query( |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 156 | @NonNull String databaseName, |
Alexander Dorokhine | c9fc960 | 2020-10-06 01:39:50 -0700 | [diff] [blame] | 157 | @NonNull String queryExpression, |
| 158 | @NonNull Bundle searchSpecBundle, |
Alexander Dorokhine | e708e18 | 2020-03-06 15:30:34 -0800 | [diff] [blame] | 159 | @NonNull AndroidFuture<AppSearchResult> callback) { |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame^] | 160 | Preconditions.checkNotNull(databaseName); |
Alexander Dorokhine | c9fc960 | 2020-10-06 01:39:50 -0700 | [diff] [blame] | 161 | Preconditions.checkNotNull(queryExpression); |
| 162 | Preconditions.checkNotNull(searchSpecBundle); |
Alexander Dorokhine | e708e18 | 2020-03-06 15:30:34 -0800 | [diff] [blame] | 163 | Preconditions.checkNotNull(callback); |
| 164 | int callingUid = Binder.getCallingUidOrThrow(); |
| 165 | int callingUserId = UserHandle.getUserId(callingUid); |
Jeff Sharkey | ea5328c | 2020-10-06 11:18:09 -0600 | [diff] [blame] | 166 | final long callingIdentity = Binder.clearCallingIdentity(); |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 167 | try { |
Alexander Dorokhine | e708e18 | 2020-03-06 15:30:34 -0800 | [diff] [blame] | 168 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 169 | databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid); |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame^] | 170 | SearchResultPage searchResultPage = impl.query( |
Alexander Dorokhine | c9fc960 | 2020-10-06 01:39:50 -0700 | [diff] [blame] | 171 | databaseName, |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame^] | 172 | queryExpression, |
| 173 | new SearchSpec(searchSpecBundle)); |
| 174 | callback.complete( |
| 175 | AppSearchResult.newSuccessfulResult(searchResultPage.getBundle())); |
Alexander Dorokhine | e708e18 | 2020-03-06 15:30:34 -0800 | [diff] [blame] | 176 | } catch (Throwable t) { |
| 177 | callback.complete(throwableToFailedResult(t)); |
| 178 | } finally { |
| 179 | Binder.restoreCallingIdentity(callingIdentity); |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 180 | } |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 181 | } |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 182 | |
Alexander Dorokhine | f6c66ae | 2020-03-09 14:47:25 -0700 | [diff] [blame] | 183 | @Override |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame^] | 184 | public void removeByUri(@NonNull String databaseName, @NonNull String namespace, |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 185 | List<String> uris, AndroidFuture<AppSearchBatchResult> callback) { |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame^] | 186 | Preconditions.checkNotNull(databaseName); |
| 187 | Preconditions.checkNotNull(namespace); |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 188 | Preconditions.checkNotNull(uris); |
| 189 | Preconditions.checkNotNull(callback); |
| 190 | int callingUid = Binder.getCallingUidOrThrow(); |
| 191 | int callingUserId = UserHandle.getUserId(callingUid); |
Jeff Sharkey | ea5328c | 2020-10-06 11:18:09 -0600 | [diff] [blame] | 192 | final long callingIdentity = Binder.clearCallingIdentity(); |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 193 | try { |
| 194 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 195 | databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid); |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 196 | AppSearchBatchResult.Builder<String, Void> resultBuilder = |
| 197 | new AppSearchBatchResult.Builder<>(); |
| 198 | for (int i = 0; i < uris.size(); i++) { |
| 199 | String uri = uris.get(i); |
| 200 | try { |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 201 | impl.remove(databaseName, namespace, uri); |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 202 | resultBuilder.setSuccess(uri, /*result= */null); |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 203 | } catch (Throwable t) { |
| 204 | resultBuilder.setResult(uri, throwableToFailedResult(t)); |
| 205 | } |
| 206 | } |
| 207 | callback.complete(resultBuilder.build()); |
| 208 | } catch (Throwable t) { |
| 209 | callback.completeExceptionally(t); |
| 210 | } finally { |
| 211 | Binder.restoreCallingIdentity(callingIdentity); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | @Override |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame^] | 216 | public void removeByQuery( |
| 217 | @NonNull String databaseName, |
| 218 | @NonNull String queryExpression, |
| 219 | @NonNull Bundle searchSpecBundle, |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 220 | @NonNull AndroidFuture<AppSearchResult> callback) { |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame^] | 221 | Preconditions.checkNotNull(databaseName); |
| 222 | Preconditions.checkNotNull(queryExpression); |
| 223 | Preconditions.checkNotNull(searchSpecBundle); |
Alexander Dorokhine | f6c66ae | 2020-03-09 14:47:25 -0700 | [diff] [blame] | 224 | Preconditions.checkNotNull(callback); |
| 225 | int callingUid = Binder.getCallingUidOrThrow(); |
| 226 | int callingUserId = UserHandle.getUserId(callingUid); |
Jeff Sharkey | ea5328c | 2020-10-06 11:18:09 -0600 | [diff] [blame] | 227 | final long callingIdentity = Binder.clearCallingIdentity(); |
Alexander Dorokhine | f6c66ae | 2020-03-09 14:47:25 -0700 | [diff] [blame] | 228 | try { |
| 229 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 230 | databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid); |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame^] | 231 | impl.removeByQuery(databaseName, queryExpression, new SearchSpec(searchSpecBundle)); |
| 232 | callback.complete(AppSearchResult.newSuccessfulResult(/*result= */null)); |
Alexander Dorokhine | f6c66ae | 2020-03-09 14:47:25 -0700 | [diff] [blame] | 233 | } catch (Throwable t) { |
| 234 | callback.complete(throwableToFailedResult(t)); |
| 235 | } finally { |
| 236 | Binder.restoreCallingIdentity(callingIdentity); |
| 237 | } |
| 238 | } |
| 239 | |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 240 | /** |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 241 | * Rewrites the database name by adding a prefix of unique name for the given uid. |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 242 | * |
| 243 | * <p>The current implementation returns the package name of the app with this uid in a |
| 244 | * format like {@code com.example.package} or {@code com.example.sharedname:5678}. |
| 245 | */ |
| 246 | @NonNull |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 247 | private String rewriteDatabaseNameWithUid(String databaseName, int callingUid) { |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 248 | // For regular apps, this call will return the package name. If callingUid is an |
| 249 | // android:sharedUserId, this value may be another type of name and have a :uid suffix. |
| 250 | String callingUidName = getContext().getPackageManager().getNameForUid(callingUid); |
| 251 | if (callingUidName == null) { |
| 252 | // Not sure how this is possible --- maybe app was uninstalled? |
| 253 | throw new IllegalStateException( |
| 254 | "Failed to look up package name for uid " + callingUid); |
| 255 | } |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 256 | return callingUidName + CALLING_NAME_DATABASE_DELIMITER + databaseName; |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 259 | private <ValueType> AppSearchResult<ValueType> throwableToFailedResult( |
| 260 | @NonNull Throwable t) { |
| 261 | if (t instanceof AppSearchException) { |
| 262 | return ((AppSearchException) t).toAppSearchResult(); |
| 263 | } |
| 264 | |
| 265 | @AppSearchResult.ResultCode int resultCode; |
| 266 | if (t instanceof IllegalStateException) { |
| 267 | resultCode = AppSearchResult.RESULT_INTERNAL_ERROR; |
| 268 | } else if (t instanceof IllegalArgumentException) { |
| 269 | resultCode = AppSearchResult.RESULT_INVALID_ARGUMENT; |
| 270 | } else if (t instanceof IOException) { |
| 271 | resultCode = AppSearchResult.RESULT_IO_ERROR; |
| 272 | } else { |
| 273 | resultCode = AppSearchResult.RESULT_UNKNOWN_ERROR; |
| 274 | } |
| 275 | return AppSearchResult.newFailedResult(resultCode, t.getMessage()); |
| 276 | } |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 277 | } |
| 278 | } |