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