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; |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 24 | import android.app.appsearch.SearchResult; |
| 25 | import android.app.appsearch.SearchResults; |
Alexander Dorokhine | c9fc960 | 2020-10-06 01:39:50 -0700 | [diff] [blame] | 26 | import android.app.appsearch.SearchSpec; |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 27 | import android.app.appsearch.exceptions.AppSearchException; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 28 | import android.content.Context; |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 29 | import android.os.Binder; |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 30 | import android.os.Bundle; |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 31 | import android.os.UserHandle; |
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; |
| 37 | import com.android.server.appsearch.external.localstorage.converter.GenericDocumentToProtoConverter; |
| 38 | import com.android.server.appsearch.external.localstorage.converter.SchemaToProtoConverter; |
| 39 | import com.android.server.appsearch.external.localstorage.converter.SearchResultToProtoConverter; |
| 40 | import com.android.server.appsearch.external.localstorage.converter.SearchSpecToProtoConverter; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 41 | |
Alexander Dorokhine | cc22345 | 2020-01-19 03:00:28 -0800 | [diff] [blame] | 42 | import com.google.android.icing.proto.DocumentProto; |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 43 | import com.google.android.icing.proto.SchemaProto; |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 44 | import com.google.android.icing.proto.SchemaTypeConfigProto; |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 45 | import com.google.android.icing.proto.SearchResultProto; |
| 46 | import com.google.android.icing.proto.SearchSpecProto; |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 47 | |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 48 | import java.io.IOException; |
Alexander Dorokhine | f660d8f | 2020-10-29 22:37:00 -0700 | [diff] [blame^] | 49 | import java.util.ArrayList; |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 50 | import java.util.List; |
| 51 | |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 52 | /** |
| 53 | * TODO(b/142567528): add comments when implement this class |
| 54 | */ |
| 55 | public class AppSearchManagerService extends SystemService { |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 56 | private static final String TAG = "AppSearchManagerService"; |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 57 | private static final char CALLING_NAME_DATABASE_DELIMITER = '$'; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 58 | |
| 59 | public AppSearchManagerService(Context context) { |
| 60 | super(context); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void onStart() { |
| 65 | publishBinderService(Context.APP_SEARCH_SERVICE, new Stub()); |
| 66 | } |
| 67 | |
| 68 | private class Stub extends IAppSearchManager.Stub { |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 69 | @Override |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 70 | public void setSchema( |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 71 | @NonNull String databaseName, |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 72 | @NonNull List<Bundle> schemaBundles, |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 73 | boolean forceOverride, |
| 74 | @NonNull AndroidFuture<AppSearchResult> callback) { |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 75 | Preconditions.checkNotNull(schemaBundles); |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 76 | Preconditions.checkNotNull(callback); |
| 77 | int callingUid = Binder.getCallingUidOrThrow(); |
| 78 | int callingUserId = UserHandle.getUserId(callingUid); |
Jeff Sharkey | ea5328c | 2020-10-06 11:18:09 -0600 | [diff] [blame] | 79 | final long callingIdentity = Binder.clearCallingIdentity(); |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 80 | try { |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 81 | SchemaProto.Builder schemaProtoBuilder = SchemaProto.newBuilder(); |
| 82 | for (int i = 0; i < schemaBundles.size(); i++) { |
| 83 | AppSearchSchema schema = new AppSearchSchema(schemaBundles.get(i)); |
| 84 | SchemaTypeConfigProto schemaTypeProto = SchemaToProtoConverter.convert(schema); |
| 85 | schemaProtoBuilder.addTypes(schemaTypeProto); |
| 86 | } |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 87 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 88 | databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid); |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 89 | impl.setSchema(databaseName, schemaProtoBuilder.build(), forceOverride); |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 90 | callback.complete(AppSearchResult.newSuccessfulResult(/*result=*/ null)); |
Alexander Dorokhine | 179c8b8 | 2020-01-11 00:17:48 -0800 | [diff] [blame] | 91 | } catch (Throwable t) { |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 92 | callback.complete(throwableToFailedResult(t)); |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 93 | } finally { |
| 94 | Binder.restoreCallingIdentity(callingIdentity); |
Alexander Dorokhine | 179c8b8 | 2020-01-11 00:17:48 -0800 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | @Override |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 99 | public void putDocuments( |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 100 | @NonNull String databaseName, |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 101 | @NonNull List<Bundle> documentBundles, |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 102 | @NonNull AndroidFuture<AppSearchBatchResult> callback) { |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 103 | Preconditions.checkNotNull(documentBundles); |
Alexander Dorokhine | cc22345 | 2020-01-19 03:00:28 -0800 | [diff] [blame] | 104 | Preconditions.checkNotNull(callback); |
| 105 | int callingUid = Binder.getCallingUidOrThrow(); |
| 106 | int callingUserId = UserHandle.getUserId(callingUid); |
Jeff Sharkey | ea5328c | 2020-10-06 11:18:09 -0600 | [diff] [blame] | 107 | final long callingIdentity = Binder.clearCallingIdentity(); |
Alexander Dorokhine | 179c8b8 | 2020-01-11 00:17:48 -0800 | [diff] [blame] | 108 | try { |
Alexander Dorokhine | cc22345 | 2020-01-19 03:00:28 -0800 | [diff] [blame] | 109 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 110 | databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid); |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 111 | AppSearchBatchResult.Builder<String, Void> resultBuilder = |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 112 | new AppSearchBatchResult.Builder<>(); |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 113 | for (int i = 0; i < documentBundles.size(); i++) { |
| 114 | GenericDocument document = new GenericDocument(documentBundles.get(i)); |
| 115 | DocumentProto documentProto = GenericDocumentToProtoConverter.convert(document); |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 116 | try { |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 117 | impl.putDocument(databaseName, documentProto); |
| 118 | resultBuilder.setSuccess(document.getUri(), /*result=*/ null); |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 119 | } catch (Throwable t) { |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 120 | resultBuilder.setResult(document.getUri(), throwableToFailedResult(t)); |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | callback.complete(resultBuilder.build()); |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 124 | } catch (Throwable t) { |
| 125 | callback.completeExceptionally(t); |
Alexander Dorokhine | cc22345 | 2020-01-19 03:00:28 -0800 | [diff] [blame] | 126 | } finally { |
| 127 | Binder.restoreCallingIdentity(callingIdentity); |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 128 | } |
| 129 | } |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 130 | |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 131 | @Override |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 132 | public void getDocuments(@NonNull String databaseName, @NonNull String namespace, |
Alexander Dorokhine | a95f44f | 2020-03-06 13:53:14 -0800 | [diff] [blame] | 133 | @NonNull List<String> uris, @NonNull AndroidFuture<AppSearchBatchResult> callback) { |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 134 | Preconditions.checkNotNull(uris); |
| 135 | Preconditions.checkNotNull(callback); |
| 136 | int callingUid = Binder.getCallingUidOrThrow(); |
| 137 | int callingUserId = UserHandle.getUserId(callingUid); |
Jeff Sharkey | ea5328c | 2020-10-06 11:18:09 -0600 | [diff] [blame] | 138 | final long callingIdentity = Binder.clearCallingIdentity(); |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 139 | try { |
| 140 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 141 | databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid); |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 142 | AppSearchBatchResult.Builder<String, Bundle> resultBuilder = |
Alexander Dorokhine | a95f44f | 2020-03-06 13:53:14 -0800 | [diff] [blame] | 143 | new AppSearchBatchResult.Builder<>(); |
| 144 | for (int i = 0; i < uris.size(); i++) { |
| 145 | String uri = uris.get(i); |
| 146 | try { |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 147 | DocumentProto documentProto = impl.getDocument( |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 148 | databaseName, namespace, uri); |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 149 | if (documentProto == null) { |
Alexander Dorokhine | a95f44f | 2020-03-06 13:53:14 -0800 | [diff] [blame] | 150 | resultBuilder.setFailure( |
| 151 | uri, AppSearchResult.RESULT_NOT_FOUND, /*errorMessage=*/ null); |
| 152 | } else { |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 153 | GenericDocument genericDocument = |
| 154 | GenericDocumentToProtoConverter.convert(documentProto); |
| 155 | resultBuilder.setSuccess(uri, genericDocument.getBundle()); |
Alexander Dorokhine | a95f44f | 2020-03-06 13:53:14 -0800 | [diff] [blame] | 156 | } |
| 157 | } catch (Throwable t) { |
| 158 | resultBuilder.setResult(uri, throwableToFailedResult(t)); |
| 159 | } |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 160 | } |
Alexander Dorokhine | a95f44f | 2020-03-06 13:53:14 -0800 | [diff] [blame] | 161 | callback.complete(resultBuilder.build()); |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 162 | } catch (Throwable t) { |
| 163 | callback.completeExceptionally(t); |
| 164 | } finally { |
| 165 | Binder.restoreCallingIdentity(callingIdentity); |
| 166 | } |
| 167 | } |
| 168 | |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 169 | // TODO(sidchhabra): Do this in a threadpool. |
| 170 | @Override |
Alexander Dorokhine | e708e18 | 2020-03-06 15:30:34 -0800 | [diff] [blame] | 171 | public void query( |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 172 | @NonNull String databaseName, |
Alexander Dorokhine | c9fc960 | 2020-10-06 01:39:50 -0700 | [diff] [blame] | 173 | @NonNull String queryExpression, |
| 174 | @NonNull Bundle searchSpecBundle, |
Alexander Dorokhine | e708e18 | 2020-03-06 15:30:34 -0800 | [diff] [blame] | 175 | @NonNull AndroidFuture<AppSearchResult> callback) { |
Alexander Dorokhine | c9fc960 | 2020-10-06 01:39:50 -0700 | [diff] [blame] | 176 | Preconditions.checkNotNull(queryExpression); |
| 177 | Preconditions.checkNotNull(searchSpecBundle); |
Alexander Dorokhine | e708e18 | 2020-03-06 15:30:34 -0800 | [diff] [blame] | 178 | Preconditions.checkNotNull(callback); |
| 179 | int callingUid = Binder.getCallingUidOrThrow(); |
| 180 | int callingUserId = UserHandle.getUserId(callingUid); |
Jeff Sharkey | ea5328c | 2020-10-06 11:18:09 -0600 | [diff] [blame] | 181 | final long callingIdentity = Binder.clearCallingIdentity(); |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 182 | try { |
Alexander Dorokhine | c9fc960 | 2020-10-06 01:39:50 -0700 | [diff] [blame] | 183 | SearchSpec searchSpec = new SearchSpec(searchSpecBundle); |
| 184 | SearchSpecProto searchSpecProto = |
| 185 | SearchSpecToProtoConverter.toSearchSpecProto(searchSpec); |
| 186 | searchSpecProto = searchSpecProto.toBuilder() |
| 187 | .setQuery(queryExpression).build(); |
Alexander Dorokhine | e708e18 | 2020-03-06 15:30:34 -0800 | [diff] [blame] | 188 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 189 | databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid); |
Alexander Dorokhine | c9fc960 | 2020-10-06 01:39:50 -0700 | [diff] [blame] | 190 | // TODO(adorokhine): handle pagination |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 191 | SearchResultProto searchResultProto = impl.query( |
Alexander Dorokhine | c9fc960 | 2020-10-06 01:39:50 -0700 | [diff] [blame] | 192 | databaseName, |
| 193 | searchSpecProto, |
| 194 | SearchSpecToProtoConverter.toResultSpecProto(searchSpec), |
| 195 | SearchSpecToProtoConverter.toScoringSpecProto(searchSpec)); |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 196 | List<SearchResult> searchResultList = |
Alexander Dorokhine | f660d8f | 2020-10-29 22:37:00 -0700 | [diff] [blame^] | 197 | new ArrayList<>(searchResultProto.getResultsCount()); |
| 198 | for (int i = 0; i < searchResultProto.getResultsCount(); i++) { |
| 199 | SearchResult result = SearchResultToProtoConverter.convertSearchResult( |
| 200 | searchResultProto.getResults(i)); |
| 201 | searchResultList.add(result); |
| 202 | } |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 203 | SearchResults searchResults = |
| 204 | new SearchResults(searchResultList, searchResultProto.getNextPageToken()); |
| 205 | callback.complete(AppSearchResult.newSuccessfulResult(searchResults)); |
Alexander Dorokhine | e708e18 | 2020-03-06 15:30:34 -0800 | [diff] [blame] | 206 | } catch (Throwable t) { |
| 207 | callback.complete(throwableToFailedResult(t)); |
| 208 | } finally { |
| 209 | Binder.restoreCallingIdentity(callingIdentity); |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 210 | } |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 211 | } |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 212 | |
Alexander Dorokhine | f6c66ae | 2020-03-09 14:47:25 -0700 | [diff] [blame] | 213 | @Override |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 214 | public void delete(@NonNull String databaseName, @NonNull String namespace, |
| 215 | List<String> uris, AndroidFuture<AppSearchBatchResult> callback) { |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 216 | Preconditions.checkNotNull(uris); |
| 217 | Preconditions.checkNotNull(callback); |
| 218 | int callingUid = Binder.getCallingUidOrThrow(); |
| 219 | int callingUserId = UserHandle.getUserId(callingUid); |
Jeff Sharkey | ea5328c | 2020-10-06 11:18:09 -0600 | [diff] [blame] | 220 | final long callingIdentity = Binder.clearCallingIdentity(); |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 221 | try { |
| 222 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 223 | databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid); |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 224 | AppSearchBatchResult.Builder<String, Void> resultBuilder = |
| 225 | new AppSearchBatchResult.Builder<>(); |
| 226 | for (int i = 0; i < uris.size(); i++) { |
| 227 | String uri = uris.get(i); |
| 228 | try { |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 229 | impl.remove(databaseName, namespace, uri); |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 230 | resultBuilder.setSuccess(uri, /*result= */null); |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 231 | } catch (Throwable t) { |
| 232 | resultBuilder.setResult(uri, throwableToFailedResult(t)); |
| 233 | } |
| 234 | } |
| 235 | callback.complete(resultBuilder.build()); |
| 236 | } catch (Throwable t) { |
| 237 | callback.completeExceptionally(t); |
| 238 | } finally { |
| 239 | Binder.restoreCallingIdentity(callingIdentity); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | @Override |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 244 | public void deleteByTypes(@NonNull String databaseName, |
Alexander Dorokhine | 7f46d6f | 2020-04-14 14:40:21 -0700 | [diff] [blame] | 245 | List<String> schemaTypes, AndroidFuture<AppSearchBatchResult> callback) { |
| 246 | Preconditions.checkNotNull(schemaTypes); |
| 247 | Preconditions.checkNotNull(callback); |
| 248 | int callingUid = Binder.getCallingUidOrThrow(); |
| 249 | int callingUserId = UserHandle.getUserId(callingUid); |
Jeff Sharkey | ea5328c | 2020-10-06 11:18:09 -0600 | [diff] [blame] | 250 | final long callingIdentity = Binder.clearCallingIdentity(); |
Alexander Dorokhine | 7f46d6f | 2020-04-14 14:40:21 -0700 | [diff] [blame] | 251 | try { |
| 252 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 253 | databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid); |
Alexander Dorokhine | 7f46d6f | 2020-04-14 14:40:21 -0700 | [diff] [blame] | 254 | AppSearchBatchResult.Builder<String, Void> resultBuilder = |
| 255 | new AppSearchBatchResult.Builder<>(); |
| 256 | for (int i = 0; i < schemaTypes.size(); i++) { |
| 257 | String schemaType = schemaTypes.get(i); |
| 258 | try { |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 259 | impl.removeByType(databaseName, schemaType); |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 260 | resultBuilder.setSuccess(schemaType, /*result=*/ null); |
Alexander Dorokhine | 7f46d6f | 2020-04-14 14:40:21 -0700 | [diff] [blame] | 261 | } catch (Throwable t) { |
| 262 | resultBuilder.setResult(schemaType, throwableToFailedResult(t)); |
| 263 | } |
| 264 | } |
| 265 | callback.complete(resultBuilder.build()); |
| 266 | } catch (Throwable t) { |
| 267 | callback.completeExceptionally(t); |
| 268 | } finally { |
| 269 | Binder.restoreCallingIdentity(callingIdentity); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | @Override |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 274 | public void deleteAll(@NonNull String databaseName, |
| 275 | @NonNull AndroidFuture<AppSearchResult> callback) { |
Alexander Dorokhine | f6c66ae | 2020-03-09 14:47:25 -0700 | [diff] [blame] | 276 | Preconditions.checkNotNull(callback); |
| 277 | int callingUid = Binder.getCallingUidOrThrow(); |
| 278 | int callingUserId = UserHandle.getUserId(callingUid); |
Jeff Sharkey | ea5328c | 2020-10-06 11:18:09 -0600 | [diff] [blame] | 279 | final long callingIdentity = Binder.clearCallingIdentity(); |
Alexander Dorokhine | f6c66ae | 2020-03-09 14:47:25 -0700 | [diff] [blame] | 280 | try { |
| 281 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 282 | databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid); |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 283 | impl.removeAll(databaseName); |
Alexander Dorokhine | f6c66ae | 2020-03-09 14:47:25 -0700 | [diff] [blame] | 284 | callback.complete(AppSearchResult.newSuccessfulResult(null)); |
| 285 | } catch (Throwable t) { |
| 286 | callback.complete(throwableToFailedResult(t)); |
| 287 | } finally { |
| 288 | Binder.restoreCallingIdentity(callingIdentity); |
| 289 | } |
| 290 | } |
| 291 | |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 292 | /** |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 293 | * 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] | 294 | * |
| 295 | * <p>The current implementation returns the package name of the app with this uid in a |
| 296 | * format like {@code com.example.package} or {@code com.example.sharedname:5678}. |
| 297 | */ |
| 298 | @NonNull |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 299 | private String rewriteDatabaseNameWithUid(String databaseName, int callingUid) { |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 300 | // For regular apps, this call will return the package name. If callingUid is an |
| 301 | // android:sharedUserId, this value may be another type of name and have a :uid suffix. |
| 302 | String callingUidName = getContext().getPackageManager().getNameForUid(callingUid); |
| 303 | if (callingUidName == null) { |
| 304 | // Not sure how this is possible --- maybe app was uninstalled? |
| 305 | throw new IllegalStateException( |
| 306 | "Failed to look up package name for uid " + callingUid); |
| 307 | } |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 308 | return callingUidName + CALLING_NAME_DATABASE_DELIMITER + databaseName; |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 311 | private <ValueType> AppSearchResult<ValueType> throwableToFailedResult( |
| 312 | @NonNull Throwable t) { |
| 313 | if (t instanceof AppSearchException) { |
| 314 | return ((AppSearchException) t).toAppSearchResult(); |
| 315 | } |
| 316 | |
| 317 | @AppSearchResult.ResultCode int resultCode; |
| 318 | if (t instanceof IllegalStateException) { |
| 319 | resultCode = AppSearchResult.RESULT_INTERNAL_ERROR; |
| 320 | } else if (t instanceof IllegalArgumentException) { |
| 321 | resultCode = AppSearchResult.RESULT_INVALID_ARGUMENT; |
| 322 | } else if (t instanceof IOException) { |
| 323 | resultCode = AppSearchResult.RESULT_IO_ERROR; |
| 324 | } else { |
| 325 | resultCode = AppSearchResult.RESULT_UNKNOWN_ERROR; |
| 326 | } |
| 327 | return AppSearchResult.newFailedResult(resultCode, t.getMessage()); |
| 328 | } |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 329 | } |
| 330 | } |