blob: 15128253b7651a115a6d30305f465e3bdfb53c00 [file] [log] [blame]
Terry Wangfebbead2019-10-17 17:05:18 -07001/*
sidchhabraa7c8f8a2020-01-16 18:38:17 -08002 * Copyright (C) 2020 The Android Open Source Project
Terry Wangfebbead2019-10-17 17:05:18 -07003 *
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 */
16package com.android.server.appsearch;
17
Terry Wangdbd1dca2020-11-03 17:03:56 -080018import static android.app.appsearch.AppSearchResult.throwableToFailedResult;
19
sidchhabraa7c8f8a2020-01-16 18:38:17 -080020import android.annotation.NonNull;
Alexander Dorokhine18465842020-01-21 01:08:57 -080021import android.app.appsearch.AppSearchBatchResult;
Alexander Dorokhine969f4462020-03-05 15:54:19 -080022import android.app.appsearch.AppSearchResult;
Alexander Dorokhine92ce3532020-10-06 01:39:36 -070023import android.app.appsearch.AppSearchSchema;
Alexander Dorokhinec66d67c2020-10-08 13:44:04 -070024import android.app.appsearch.GenericDocument;
Terry Wangdbd1dca2020-11-03 17:03:56 -080025import android.app.appsearch.IAppSearchBatchResultCallback;
Terry Wangfebbead2019-10-17 17:05:18 -070026import android.app.appsearch.IAppSearchManager;
Terry Wangdbd1dca2020-11-03 17:03:56 -080027import android.app.appsearch.IAppSearchResultCallback;
Terry Wang26b9e5c2020-10-23 02:05:01 -070028import android.app.appsearch.SearchResultPage;
Alexander Dorokhinec9fc9602020-10-06 01:39:50 -070029import android.app.appsearch.SearchSpec;
Terry Wangfebbead2019-10-17 17:05:18 -070030import android.content.Context;
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080031import android.os.Binder;
Alexander Dorokhine92ce3532020-10-06 01:39:36 -070032import android.os.Bundle;
Terry Wangdbd1dca2020-11-03 17:03:56 -080033import android.os.ParcelableException;
34import android.os.RemoteException;
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080035import android.os.UserHandle;
Terry Wangdbd1dca2020-11-03 17:03:56 -080036import android.util.Log;
Terry Wangfebbead2019-10-17 17:05:18 -070037
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080038import com.android.internal.util.Preconditions;
Terry Wangfebbead2019-10-17 17:05:18 -070039import com.android.server.SystemService;
Alexander Dorokhinef660d8f2020-10-29 22:37:00 -070040import com.android.server.appsearch.external.localstorage.AppSearchImpl;
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -080041
Alexander Dorokhine6a99f942020-12-04 02:57:22 -080042import java.util.ArrayList;
Alexander Dorokhine18465842020-01-21 01:08:57 -080043import java.util.List;
44
Terry Wangfebbead2019-10-17 17:05:18 -070045/**
46 * TODO(b/142567528): add comments when implement this class
47 */
48public class AppSearchManagerService extends SystemService {
Alexander Dorokhineebd37742020-09-22 15:02:26 -070049 private static final String TAG = "AppSearchManagerService";
Terry Wang6413aee2020-10-07 03:04:58 -070050 private static final char CALLING_NAME_DATABASE_DELIMITER = '$';
Terry Wangfebbead2019-10-17 17:05:18 -070051
52 public AppSearchManagerService(Context context) {
53 super(context);
54 }
55
56 @Override
57 public void onStart() {
58 publishBinderService(Context.APP_SEARCH_SERVICE, new Stub());
59 }
60
61 private class Stub extends IAppSearchManager.Stub {
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -080062 @Override
Alexander Dorokhine969f4462020-03-05 15:54:19 -080063 public void setSchema(
Terry Wang6413aee2020-10-07 03:04:58 -070064 @NonNull String databaseName,
Alexander Dorokhine92ce3532020-10-06 01:39:36 -070065 @NonNull List<Bundle> schemaBundles,
Alexander Dorokhine6a99f942020-12-04 02:57:22 -080066 @NonNull List<String> schemasNotPlatformSurfaceable,
Alexander Dorokhine969f4462020-03-05 15:54:19 -080067 boolean forceOverride,
Terry Wangdbd1dca2020-11-03 17:03:56 -080068 @NonNull IAppSearchResultCallback callback) {
Terry Wang26b9e5c2020-10-23 02:05:01 -070069 Preconditions.checkNotNull(databaseName);
Alexander Dorokhine92ce3532020-10-06 01:39:36 -070070 Preconditions.checkNotNull(schemaBundles);
Terry Wangbfbfcac2020-11-06 15:46:44 -080071 Preconditions.checkNotNull(callback);
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080072 int callingUid = Binder.getCallingUidOrThrow();
73 int callingUserId = UserHandle.getUserId(callingUid);
Jeff Sharkeyea5328c2020-10-06 11:18:09 -060074 final long callingIdentity = Binder.clearCallingIdentity();
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -080075 try {
Alexander Dorokhine6a99f942020-12-04 02:57:22 -080076 List<AppSearchSchema> schemas = new ArrayList<>(schemaBundles.size());
Alexander Dorokhine92ce3532020-10-06 01:39:36 -070077 for (int i = 0; i < schemaBundles.size(); i++) {
Terry Wang26b9e5c2020-10-23 02:05:01 -070078 schemas.add(new AppSearchSchema(schemaBundles.get(i)));
Alexander Dorokhine92ce3532020-10-06 01:39:36 -070079 }
Terry Wang0d393872020-12-11 22:17:09 +000080 AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
Terry Wang6413aee2020-10-07 03:04:58 -070081 databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid);
Alexander Dorokhine6a99f942020-12-04 02:57:22 -080082 impl.setSchema(databaseName, schemas, schemasNotPlatformSurfaceable, forceOverride);
Terry Wangdbd1dca2020-11-03 17:03:56 -080083 invokeCallbackOnResult(callback,
84 AppSearchResult.newSuccessfulResult(/*result=*/ null));
Alexander Dorokhine179c8b82020-01-11 00:17:48 -080085 } catch (Throwable t) {
Terry Wangdbd1dca2020-11-03 17:03:56 -080086 invokeCallbackOnError(callback, t);
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080087 } finally {
88 Binder.restoreCallingIdentity(callingIdentity);
Alexander Dorokhine179c8b82020-01-11 00:17:48 -080089 }
90 }
91
92 @Override
Terry Wang83a24932020-12-09 21:00:18 -080093 public void getSchema(
94 @NonNull String databaseName,
95 @NonNull IAppSearchResultCallback callback) {
96 Preconditions.checkNotNull(databaseName);
97 Preconditions.checkNotNull(callback);
98 int callingUid = Binder.getCallingUidOrThrow();
99 int callingUserId = UserHandle.getUserId(callingUid);
100 final long callingIdentity = Binder.clearCallingIdentity();
101 try {
102 AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
103 databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid);
104 List<AppSearchSchema> schemas = impl.getSchema(databaseName);
105 List<Bundle> schemaBundles = new ArrayList<>(schemas.size());
106 for (int i = 0; i < schemas.size(); i++) {
107 schemaBundles.add(schemas.get(i).getBundle());
108 }
109 invokeCallbackOnResult(callback,
110 AppSearchResult.newSuccessfulResult(schemaBundles));
111 } catch (Throwable t) {
112 invokeCallbackOnError(callback, t);
113 } finally {
114 Binder.restoreCallingIdentity(callingIdentity);
115 }
116 }
117
118 @Override
Alexander Dorokhine18465842020-01-21 01:08:57 -0800119 public void putDocuments(
Terry Wang6413aee2020-10-07 03:04:58 -0700120 @NonNull String databaseName,
Alexander Dorokhinec66d67c2020-10-08 13:44:04 -0700121 @NonNull List<Bundle> documentBundles,
Terry Wangdbd1dca2020-11-03 17:03:56 -0800122 @NonNull IAppSearchBatchResultCallback callback) {
Terry Wang26b9e5c2020-10-23 02:05:01 -0700123 Preconditions.checkNotNull(databaseName);
Alexander Dorokhinec66d67c2020-10-08 13:44:04 -0700124 Preconditions.checkNotNull(documentBundles);
Alexander Dorokhinecc223452020-01-19 03:00:28 -0800125 Preconditions.checkNotNull(callback);
126 int callingUid = Binder.getCallingUidOrThrow();
127 int callingUserId = UserHandle.getUserId(callingUid);
Jeff Sharkeyea5328c2020-10-06 11:18:09 -0600128 final long callingIdentity = Binder.clearCallingIdentity();
Alexander Dorokhine179c8b82020-01-11 00:17:48 -0800129 try {
Alexander Dorokhine18465842020-01-21 01:08:57 -0800130 AppSearchBatchResult.Builder<String, Void> resultBuilder =
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800131 new AppSearchBatchResult.Builder<>();
Terry Wang0d393872020-12-11 22:17:09 +0000132 AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
Terry Wangdbd1dca2020-11-03 17:03:56 -0800133 databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid);
Alexander Dorokhinec66d67c2020-10-08 13:44:04 -0700134 for (int i = 0; i < documentBundles.size(); i++) {
135 GenericDocument document = new GenericDocument(documentBundles.get(i));
Alexander Dorokhine18465842020-01-21 01:08:57 -0800136 try {
Terry Wangdbd1dca2020-11-03 17:03:56 -0800137 // TODO(b/173451571): reduce burden of binder thread by enqueue request onto
138 // a separate thread.
Terry Wang26b9e5c2020-10-23 02:05:01 -0700139 impl.putDocument(databaseName, document);
Alexander Dorokhinec66d67c2020-10-08 13:44:04 -0700140 resultBuilder.setSuccess(document.getUri(), /*result=*/ null);
Alexander Dorokhine18465842020-01-21 01:08:57 -0800141 } catch (Throwable t) {
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800142 resultBuilder.setResult(document.getUri(), throwableToFailedResult(t));
Alexander Dorokhine18465842020-01-21 01:08:57 -0800143 }
144 }
Terry Wangdbd1dca2020-11-03 17:03:56 -0800145 invokeCallbackOnResult(callback, resultBuilder.build());
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -0800146 } catch (Throwable t) {
Terry Wangdbd1dca2020-11-03 17:03:56 -0800147 invokeCallbackOnError(callback, t);
Alexander Dorokhinecc223452020-01-19 03:00:28 -0800148 } finally {
149 Binder.restoreCallingIdentity(callingIdentity);
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -0800150 }
151 }
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800152
Alexander Dorokhine69a8d9f2020-03-06 10:43:16 -0800153 @Override
Terry Wang6413aee2020-10-07 03:04:58 -0700154 public void getDocuments(@NonNull String databaseName, @NonNull String namespace,
Terry Wangdbd1dca2020-11-03 17:03:56 -0800155 @NonNull List<String> uris,
156 @NonNull IAppSearchBatchResultCallback callback) {
Terry Wang26b9e5c2020-10-23 02:05:01 -0700157 Preconditions.checkNotNull(databaseName);
158 Preconditions.checkNotNull(namespace);
Alexander Dorokhine69a8d9f2020-03-06 10:43:16 -0800159 Preconditions.checkNotNull(uris);
160 Preconditions.checkNotNull(callback);
161 int callingUid = Binder.getCallingUidOrThrow();
162 int callingUserId = UserHandle.getUserId(callingUid);
Jeff Sharkeyea5328c2020-10-06 11:18:09 -0600163 final long callingIdentity = Binder.clearCallingIdentity();
Alexander Dorokhine69a8d9f2020-03-06 10:43:16 -0800164 try {
Alexander Dorokhinec66d67c2020-10-08 13:44:04 -0700165 AppSearchBatchResult.Builder<String, Bundle> resultBuilder =
Alexander Dorokhinea95f44f2020-03-06 13:53:14 -0800166 new AppSearchBatchResult.Builder<>();
Terry Wang0d393872020-12-11 22:17:09 +0000167 AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
Terry Wangdbd1dca2020-11-03 17:03:56 -0800168 databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid);
Alexander Dorokhinea95f44f2020-03-06 13:53:14 -0800169 for (int i = 0; i < uris.size(); i++) {
170 String uri = uris.get(i);
171 try {
Terry Wang26b9e5c2020-10-23 02:05:01 -0700172 GenericDocument document = impl.getDocument(databaseName, namespace, uri);
173 resultBuilder.setSuccess(uri, document.getBundle());
Alexander Dorokhinea95f44f2020-03-06 13:53:14 -0800174 } catch (Throwable t) {
175 resultBuilder.setResult(uri, throwableToFailedResult(t));
176 }
Alexander Dorokhine69a8d9f2020-03-06 10:43:16 -0800177 }
Terry Wangdbd1dca2020-11-03 17:03:56 -0800178 invokeCallbackOnResult(callback, resultBuilder.build());
Alexander Dorokhine69a8d9f2020-03-06 10:43:16 -0800179 } catch (Throwable t) {
Terry Wangdbd1dca2020-11-03 17:03:56 -0800180 invokeCallbackOnError(callback, t);
Alexander Dorokhine69a8d9f2020-03-06 10:43:16 -0800181 } finally {
182 Binder.restoreCallingIdentity(callingIdentity);
183 }
184 }
185
sidchhabraa7c8f8a2020-01-16 18:38:17 -0800186 // TODO(sidchhabra): Do this in a threadpool.
187 @Override
Alexander Dorokhinee708e182020-03-06 15:30:34 -0800188 public void query(
Terry Wang6413aee2020-10-07 03:04:58 -0700189 @NonNull String databaseName,
Alexander Dorokhinec9fc9602020-10-06 01:39:50 -0700190 @NonNull String queryExpression,
191 @NonNull Bundle searchSpecBundle,
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700192 @NonNull IAppSearchResultCallback callback) {
Terry Wang26b9e5c2020-10-23 02:05:01 -0700193 Preconditions.checkNotNull(databaseName);
Alexander Dorokhinec9fc9602020-10-06 01:39:50 -0700194 Preconditions.checkNotNull(queryExpression);
195 Preconditions.checkNotNull(searchSpecBundle);
Terry Wangbfbfcac2020-11-06 15:46:44 -0800196 Preconditions.checkNotNull(callback);
Alexander Dorokhinee708e182020-03-06 15:30:34 -0800197 int callingUid = Binder.getCallingUidOrThrow();
198 int callingUserId = UserHandle.getUserId(callingUid);
Jeff Sharkeyea5328c2020-10-06 11:18:09 -0600199 final long callingIdentity = Binder.clearCallingIdentity();
sidchhabraa7c8f8a2020-01-16 18:38:17 -0800200 try {
Terry Wang0d393872020-12-11 22:17:09 +0000201 AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
Terry Wang6413aee2020-10-07 03:04:58 -0700202 databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid);
Terry Wang26b9e5c2020-10-23 02:05:01 -0700203 SearchResultPage searchResultPage = impl.query(
Alexander Dorokhinec9fc9602020-10-06 01:39:50 -0700204 databaseName,
Terry Wang26b9e5c2020-10-23 02:05:01 -0700205 queryExpression,
206 new SearchSpec(searchSpecBundle));
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700207 invokeCallbackOnResult(callback,
Terry Wang26b9e5c2020-10-23 02:05:01 -0700208 AppSearchResult.newSuccessfulResult(searchResultPage.getBundle()));
Alexander Dorokhinee708e182020-03-06 15:30:34 -0800209 } catch (Throwable t) {
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700210 invokeCallbackOnError(callback, t);
211 } finally {
212 Binder.restoreCallingIdentity(callingIdentity);
213 }
214 }
215
Terry Wangbfbfcac2020-11-06 15:46:44 -0800216 public void globalQuery(
217 @NonNull String queryExpression,
218 @NonNull Bundle searchSpecBundle,
219 @NonNull IAppSearchResultCallback callback) {
220 Preconditions.checkNotNull(queryExpression);
221 Preconditions.checkNotNull(searchSpecBundle);
222 Preconditions.checkNotNull(callback);
223 int callingUid = Binder.getCallingUidOrThrow();
224 int callingUserId = UserHandle.getUserId(callingUid);
225 final long callingIdentity = Binder.clearCallingIdentity();
226 try {
Terry Wang0d393872020-12-11 22:17:09 +0000227 AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
Terry Wangbfbfcac2020-11-06 15:46:44 -0800228 SearchResultPage searchResultPage = impl.globalQuery(
229 queryExpression,
230 new SearchSpec(searchSpecBundle));
231 invokeCallbackOnResult(callback,
232 AppSearchResult.newSuccessfulResult(searchResultPage.getBundle()));
233 } catch (Throwable t) {
234 invokeCallbackOnError(callback, t);
235 } finally {
236 Binder.restoreCallingIdentity(callingIdentity);
237 }
238 }
239
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700240 @Override
241 public void getNextPage(long nextPageToken,
242 @NonNull IAppSearchResultCallback callback) {
Terry Wangbfbfcac2020-11-06 15:46:44 -0800243 Preconditions.checkNotNull(callback);
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700244 int callingUid = Binder.getCallingUidOrThrow();
245 int callingUserId = UserHandle.getUserId(callingUid);
246 final long callingIdentity = Binder.clearCallingIdentity();
247 // TODO(b/162450968) check nextPageToken is being advanced by the same uid as originally
248 // opened it
249 try {
Terry Wang0d393872020-12-11 22:17:09 +0000250 AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700251 SearchResultPage searchResultPage = impl.getNextPage(nextPageToken);
252 invokeCallbackOnResult(callback,
253 AppSearchResult.newSuccessfulResult(searchResultPage.getBundle()));
254 } catch (Throwable t) {
255 invokeCallbackOnError(callback, t);
256 } finally {
257 Binder.restoreCallingIdentity(callingIdentity);
258 }
259 }
260
261 @Override
262 public void invalidateNextPageToken(long nextPageToken) {
263 int callingUid = Binder.getCallingUidOrThrow();
264 int callingUserId = UserHandle.getUserId(callingUid);
265 final long callingIdentity = Binder.clearCallingIdentity();
266 try {
Terry Wang0d393872020-12-11 22:17:09 +0000267 AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
Alexander Dorokhined48f2362020-10-20 17:40:49 -0700268 impl.invalidateNextPageToken(nextPageToken);
269 } catch (Throwable t) {
270 Log.d(TAG, "Unable to invalidate the query page token", t);
Alexander Dorokhinee708e182020-03-06 15:30:34 -0800271 } finally {
272 Binder.restoreCallingIdentity(callingIdentity);
sidchhabraa7c8f8a2020-01-16 18:38:17 -0800273 }
sidchhabraa7c8f8a2020-01-16 18:38:17 -0800274 }
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800275
Alexander Dorokhinef6c66ae2020-03-09 14:47:25 -0700276 @Override
Terry Wang26b9e5c2020-10-23 02:05:01 -0700277 public void removeByUri(@NonNull String databaseName, @NonNull String namespace,
Terry Wangdbd1dca2020-11-03 17:03:56 -0800278 @NonNull List<String> uris,
279 @NonNull IAppSearchBatchResultCallback callback) {
Terry Wang26b9e5c2020-10-23 02:05:01 -0700280 Preconditions.checkNotNull(databaseName);
Alexander Dorokhineff82fba2020-03-09 16:35:24 -0700281 Preconditions.checkNotNull(uris);
282 Preconditions.checkNotNull(callback);
283 int callingUid = Binder.getCallingUidOrThrow();
284 int callingUserId = UserHandle.getUserId(callingUid);
Jeff Sharkeyea5328c2020-10-06 11:18:09 -0600285 final long callingIdentity = Binder.clearCallingIdentity();
Terry Wang0d393872020-12-11 22:17:09 +0000286 AppSearchBatchResult.Builder<String, Void> resultBuilder =
287 new AppSearchBatchResult.Builder<>();
Alexander Dorokhineff82fba2020-03-09 16:35:24 -0700288 try {
Terry Wang0d393872020-12-11 22:17:09 +0000289 AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
Terry Wang6413aee2020-10-07 03:04:58 -0700290 databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid);
Alexander Dorokhineff82fba2020-03-09 16:35:24 -0700291 for (int i = 0; i < uris.size(); i++) {
292 String uri = uris.get(i);
293 try {
Terry Wang6413aee2020-10-07 03:04:58 -0700294 impl.remove(databaseName, namespace, uri);
Alexander Dorokhinec66d67c2020-10-08 13:44:04 -0700295 resultBuilder.setSuccess(uri, /*result= */null);
Alexander Dorokhineff82fba2020-03-09 16:35:24 -0700296 } catch (Throwable t) {
297 resultBuilder.setResult(uri, throwableToFailedResult(t));
298 }
299 }
Terry Wangdbd1dca2020-11-03 17:03:56 -0800300 invokeCallbackOnResult(callback, resultBuilder.build());
Alexander Dorokhineff82fba2020-03-09 16:35:24 -0700301 } catch (Throwable t) {
Terry Wangdbd1dca2020-11-03 17:03:56 -0800302 invokeCallbackOnError(callback, t);
Alexander Dorokhineff82fba2020-03-09 16:35:24 -0700303 } finally {
304 Binder.restoreCallingIdentity(callingIdentity);
305 }
306 }
307
308 @Override
Terry Wang26b9e5c2020-10-23 02:05:01 -0700309 public void removeByQuery(
310 @NonNull String databaseName,
311 @NonNull String queryExpression,
312 @NonNull Bundle searchSpecBundle,
Alexander Dorokhine178366b2020-10-20 17:40:49 -0700313 @NonNull IAppSearchResultCallback callback) {
Terry Wang26b9e5c2020-10-23 02:05:01 -0700314 Preconditions.checkNotNull(databaseName);
315 Preconditions.checkNotNull(queryExpression);
316 Preconditions.checkNotNull(searchSpecBundle);
Terry Wangbfbfcac2020-11-06 15:46:44 -0800317 Preconditions.checkNotNull(callback);
Alexander Dorokhinef6c66ae2020-03-09 14:47:25 -0700318 int callingUid = Binder.getCallingUidOrThrow();
319 int callingUserId = UserHandle.getUserId(callingUid);
Jeff Sharkeyea5328c2020-10-06 11:18:09 -0600320 final long callingIdentity = Binder.clearCallingIdentity();
Alexander Dorokhinef6c66ae2020-03-09 14:47:25 -0700321 try {
Terry Wang0d393872020-12-11 22:17:09 +0000322 AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
Terry Wang6413aee2020-10-07 03:04:58 -0700323 databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid);
Alexander Dorokhine178366b2020-10-20 17:40:49 -0700324 impl.removeByQuery(databaseName, queryExpression,
325 new SearchSpec(searchSpecBundle));
326 invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null));
Alexander Dorokhinef6c66ae2020-03-09 14:47:25 -0700327 } catch (Throwable t) {
Alexander Dorokhine178366b2020-10-20 17:40:49 -0700328 invokeCallbackOnError(callback, t);
Alexander Dorokhinef6c66ae2020-03-09 14:47:25 -0700329 } finally {
330 Binder.restoreCallingIdentity(callingIdentity);
331 }
332 }
333
Terry Wangdbd1dca2020-11-03 17:03:56 -0800334 @Override
335 public void initialize(@NonNull IAppSearchResultCallback callback) {
Terry Wangbfbfcac2020-11-06 15:46:44 -0800336 Preconditions.checkNotNull(callback);
Terry Wangdbd1dca2020-11-03 17:03:56 -0800337 int callingUid = Binder.getCallingUidOrThrow();
338 int callingUserId = UserHandle.getUserId(callingUid);
339 final long callingIdentity = Binder.clearCallingIdentity();
340 try {
Terry Wang0d393872020-12-11 22:17:09 +0000341 ImplInstanceManager.getInstance(getContext(), callingUserId);
Terry Wangdbd1dca2020-11-03 17:03:56 -0800342 invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null));
343 } catch (Throwable t) {
344 invokeCallbackOnError(callback, t);
345 } finally {
346 Binder.restoreCallingIdentity(callingIdentity);
347 }
348 }
349
Alexander Dorokhineebd37742020-09-22 15:02:26 -0700350 /**
Terry Wang6413aee2020-10-07 03:04:58 -0700351 * Rewrites the database name by adding a prefix of unique name for the given uid.
Alexander Dorokhineebd37742020-09-22 15:02:26 -0700352 *
353 * <p>The current implementation returns the package name of the app with this uid in a
354 * format like {@code com.example.package} or {@code com.example.sharedname:5678}.
355 */
356 @NonNull
Terry Wang6413aee2020-10-07 03:04:58 -0700357 private String rewriteDatabaseNameWithUid(String databaseName, int callingUid) {
Alexander Dorokhineebd37742020-09-22 15:02:26 -0700358 // For regular apps, this call will return the package name. If callingUid is an
359 // android:sharedUserId, this value may be another type of name and have a :uid suffix.
360 String callingUidName = getContext().getPackageManager().getNameForUid(callingUid);
361 if (callingUidName == null) {
362 // Not sure how this is possible --- maybe app was uninstalled?
363 throw new IllegalStateException(
364 "Failed to look up package name for uid " + callingUid);
365 }
Terry Wang6413aee2020-10-07 03:04:58 -0700366 return callingUidName + CALLING_NAME_DATABASE_DELIMITER + databaseName;
Alexander Dorokhineebd37742020-09-22 15:02:26 -0700367 }
368
Terry Wangdbd1dca2020-11-03 17:03:56 -0800369 /** Invokes the {@link IAppSearchResultCallback} with the result. */
370 private void invokeCallbackOnResult(IAppSearchResultCallback callback,
371 AppSearchResult result) {
372 try {
373 callback.onResult(result);
374 } catch (RemoteException e) {
375 Log.d(TAG, "Unable to send result to the callback", e);
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800376 }
Terry Wangdbd1dca2020-11-03 17:03:56 -0800377 }
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800378
Terry Wangdbd1dca2020-11-03 17:03:56 -0800379 /** Invokes the {@link IAppSearchBatchResultCallback} with the result. */
380 private void invokeCallbackOnResult(IAppSearchBatchResultCallback callback,
381 AppSearchBatchResult result) {
382 try {
383 callback.onResult(result);
384 } catch (RemoteException e) {
385 Log.d(TAG, "Unable to send result to the callback", e);
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800386 }
Terry Wangdbd1dca2020-11-03 17:03:56 -0800387 }
388
389 /**
390 * Invokes the {@link IAppSearchResultCallback} with an throwable.
391 *
392 * <p>The throwable is convert to a {@link AppSearchResult};
393 */
394 private void invokeCallbackOnError(IAppSearchResultCallback callback, Throwable throwable) {
395 try {
396 callback.onResult(throwableToFailedResult(throwable));
397 } catch (RemoteException e) {
398 Log.d(TAG, "Unable to send result to the callback", e);
399 }
400 }
401
402 /**
Terry Wang0d393872020-12-11 22:17:09 +0000403 * Invokes the {@link IAppSearchBatchResultCallback} with an throwable.
Terry Wangdbd1dca2020-11-03 17:03:56 -0800404 *
405 * <p>The throwable is converted to {@link ParcelableException}.
406 */
407 private void invokeCallbackOnError(IAppSearchBatchResultCallback callback,
408 Throwable throwable) {
409 try {
410 callback.onSystemError(new ParcelableException(throwable));
411 } catch (RemoteException e) {
412 Log.d(TAG, "Unable to send error to the callback", e);
413 }
Alexander Dorokhine969f4462020-03-05 15:54:19 -0800414 }
Terry Wangfebbead2019-10-17 17:05:18 -0700415 }
416}