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 | |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 18 | import static android.app.appsearch.AppSearchResult.throwableToFailedResult; |
| 19 | |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 20 | import android.annotation.NonNull; |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 21 | import android.app.appsearch.AppSearchBatchResult; |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 22 | import android.app.appsearch.AppSearchResult; |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 23 | import android.app.appsearch.AppSearchSchema; |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 24 | import android.app.appsearch.GenericDocument; |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 25 | import android.app.appsearch.IAppSearchBatchResultCallback; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 26 | import android.app.appsearch.IAppSearchManager; |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 27 | import android.app.appsearch.IAppSearchResultCallback; |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 28 | import android.app.appsearch.SearchResultPage; |
Alexander Dorokhine | c9fc960 | 2020-10-06 01:39:50 -0700 | [diff] [blame] | 29 | import android.app.appsearch.SearchSpec; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 30 | import android.content.Context; |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 31 | import android.os.Binder; |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 32 | import android.os.Bundle; |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 33 | import android.os.ParcelableException; |
| 34 | import android.os.RemoteException; |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 35 | import android.os.UserHandle; |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 36 | import android.util.Log; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 37 | |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 38 | import com.android.internal.util.Preconditions; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 39 | import com.android.server.SystemService; |
Alexander Dorokhine | f660d8f | 2020-10-29 22:37:00 -0700 | [diff] [blame] | 40 | import com.android.server.appsearch.external.localstorage.AppSearchImpl; |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 41 | |
Alexander Dorokhine | 6a99f94 | 2020-12-04 02:57:22 -0800 | [diff] [blame] | 42 | import java.util.ArrayList; |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 43 | import java.util.List; |
| 44 | |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 45 | /** |
| 46 | * TODO(b/142567528): add comments when implement this class |
| 47 | */ |
| 48 | public class AppSearchManagerService extends SystemService { |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 49 | private static final String TAG = "AppSearchManagerService"; |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 50 | private static final char CALLING_NAME_DATABASE_DELIMITER = '$'; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 51 | |
| 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 Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 62 | @Override |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 63 | public void setSchema( |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 64 | @NonNull String databaseName, |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 65 | @NonNull List<Bundle> schemaBundles, |
Alexander Dorokhine | 6a99f94 | 2020-12-04 02:57:22 -0800 | [diff] [blame] | 66 | @NonNull List<String> schemasNotPlatformSurfaceable, |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 67 | boolean forceOverride, |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 68 | @NonNull IAppSearchResultCallback callback) { |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 69 | Preconditions.checkNotNull(databaseName); |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 70 | Preconditions.checkNotNull(schemaBundles); |
Terry Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame] | 71 | Preconditions.checkNotNull(callback); |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 72 | int callingUid = Binder.getCallingUidOrThrow(); |
| 73 | int callingUserId = UserHandle.getUserId(callingUid); |
Jeff Sharkey | ea5328c | 2020-10-06 11:18:09 -0600 | [diff] [blame] | 74 | final long callingIdentity = Binder.clearCallingIdentity(); |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 75 | try { |
Alexander Dorokhine | 6a99f94 | 2020-12-04 02:57:22 -0800 | [diff] [blame] | 76 | List<AppSearchSchema> schemas = new ArrayList<>(schemaBundles.size()); |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 77 | for (int i = 0; i < schemaBundles.size(); i++) { |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 78 | schemas.add(new AppSearchSchema(schemaBundles.get(i))); |
Alexander Dorokhine | 92ce353 | 2020-10-06 01:39:36 -0700 | [diff] [blame] | 79 | } |
Terry Wang | 0d39387 | 2020-12-11 22:17:09 +0000 | [diff] [blame] | 80 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 81 | databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid); |
Alexander Dorokhine | 6a99f94 | 2020-12-04 02:57:22 -0800 | [diff] [blame] | 82 | impl.setSchema(databaseName, schemas, schemasNotPlatformSurfaceable, forceOverride); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 83 | invokeCallbackOnResult(callback, |
| 84 | AppSearchResult.newSuccessfulResult(/*result=*/ null)); |
Alexander Dorokhine | 179c8b8 | 2020-01-11 00:17:48 -0800 | [diff] [blame] | 85 | } catch (Throwable t) { |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 86 | invokeCallbackOnError(callback, t); |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 87 | } finally { |
| 88 | Binder.restoreCallingIdentity(callingIdentity); |
Alexander Dorokhine | 179c8b8 | 2020-01-11 00:17:48 -0800 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | @Override |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 93 | public void putDocuments( |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 94 | @NonNull String databaseName, |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 95 | @NonNull List<Bundle> documentBundles, |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 96 | @NonNull IAppSearchBatchResultCallback callback) { |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 97 | Preconditions.checkNotNull(databaseName); |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 98 | Preconditions.checkNotNull(documentBundles); |
Alexander Dorokhine | cc22345 | 2020-01-19 03:00:28 -0800 | [diff] [blame] | 99 | Preconditions.checkNotNull(callback); |
| 100 | int callingUid = Binder.getCallingUidOrThrow(); |
| 101 | int callingUserId = UserHandle.getUserId(callingUid); |
Jeff Sharkey | ea5328c | 2020-10-06 11:18:09 -0600 | [diff] [blame] | 102 | final long callingIdentity = Binder.clearCallingIdentity(); |
Alexander Dorokhine | 179c8b8 | 2020-01-11 00:17:48 -0800 | [diff] [blame] | 103 | try { |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 104 | AppSearchBatchResult.Builder<String, Void> resultBuilder = |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 105 | new AppSearchBatchResult.Builder<>(); |
Terry Wang | 0d39387 | 2020-12-11 22:17:09 +0000 | [diff] [blame] | 106 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 107 | databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid); |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 108 | for (int i = 0; i < documentBundles.size(); i++) { |
| 109 | GenericDocument document = new GenericDocument(documentBundles.get(i)); |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 110 | try { |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 111 | // TODO(b/173451571): reduce burden of binder thread by enqueue request onto |
| 112 | // a separate thread. |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 113 | impl.putDocument(databaseName, document); |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 114 | resultBuilder.setSuccess(document.getUri(), /*result=*/ null); |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 115 | } catch (Throwable t) { |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 116 | resultBuilder.setResult(document.getUri(), throwableToFailedResult(t)); |
Alexander Dorokhine | 1846584 | 2020-01-21 01:08:57 -0800 | [diff] [blame] | 117 | } |
| 118 | } |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 119 | invokeCallbackOnResult(callback, resultBuilder.build()); |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 120 | } catch (Throwable t) { |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 121 | invokeCallbackOnError(callback, t); |
Alexander Dorokhine | cc22345 | 2020-01-19 03:00:28 -0800 | [diff] [blame] | 122 | } finally { |
| 123 | Binder.restoreCallingIdentity(callingIdentity); |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 124 | } |
| 125 | } |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 126 | |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 127 | @Override |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 128 | public void getDocuments(@NonNull String databaseName, @NonNull String namespace, |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 129 | @NonNull List<String> uris, |
| 130 | @NonNull IAppSearchBatchResultCallback callback) { |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 131 | Preconditions.checkNotNull(databaseName); |
| 132 | Preconditions.checkNotNull(namespace); |
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 { |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 139 | AppSearchBatchResult.Builder<String, Bundle> resultBuilder = |
Alexander Dorokhine | a95f44f | 2020-03-06 13:53:14 -0800 | [diff] [blame] | 140 | new AppSearchBatchResult.Builder<>(); |
Terry Wang | 0d39387 | 2020-12-11 22:17:09 +0000 | [diff] [blame] | 141 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 142 | databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid); |
Alexander Dorokhine | a95f44f | 2020-03-06 13:53:14 -0800 | [diff] [blame] | 143 | for (int i = 0; i < uris.size(); i++) { |
| 144 | String uri = uris.get(i); |
| 145 | try { |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 146 | GenericDocument document = impl.getDocument(databaseName, namespace, uri); |
| 147 | resultBuilder.setSuccess(uri, document.getBundle()); |
Alexander Dorokhine | a95f44f | 2020-03-06 13:53:14 -0800 | [diff] [blame] | 148 | } catch (Throwable t) { |
| 149 | resultBuilder.setResult(uri, throwableToFailedResult(t)); |
| 150 | } |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 151 | } |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 152 | invokeCallbackOnResult(callback, resultBuilder.build()); |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 153 | } catch (Throwable t) { |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 154 | invokeCallbackOnError(callback, t); |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 155 | } finally { |
| 156 | Binder.restoreCallingIdentity(callingIdentity); |
| 157 | } |
| 158 | } |
| 159 | |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 160 | // TODO(sidchhabra): Do this in a threadpool. |
| 161 | @Override |
Alexander Dorokhine | e708e18 | 2020-03-06 15:30:34 -0800 | [diff] [blame] | 162 | public void query( |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 163 | @NonNull String databaseName, |
Alexander Dorokhine | c9fc960 | 2020-10-06 01:39:50 -0700 | [diff] [blame] | 164 | @NonNull String queryExpression, |
| 165 | @NonNull Bundle searchSpecBundle, |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 166 | @NonNull IAppSearchResultCallback callback) { |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 167 | Preconditions.checkNotNull(databaseName); |
Alexander Dorokhine | c9fc960 | 2020-10-06 01:39:50 -0700 | [diff] [blame] | 168 | Preconditions.checkNotNull(queryExpression); |
| 169 | Preconditions.checkNotNull(searchSpecBundle); |
Terry Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame] | 170 | Preconditions.checkNotNull(callback); |
Alexander Dorokhine | e708e18 | 2020-03-06 15:30:34 -0800 | [diff] [blame] | 171 | int callingUid = Binder.getCallingUidOrThrow(); |
| 172 | int callingUserId = UserHandle.getUserId(callingUid); |
Jeff Sharkey | ea5328c | 2020-10-06 11:18:09 -0600 | [diff] [blame] | 173 | final long callingIdentity = Binder.clearCallingIdentity(); |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 174 | try { |
Terry Wang | 0d39387 | 2020-12-11 22:17:09 +0000 | [diff] [blame] | 175 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 176 | databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid); |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 177 | SearchResultPage searchResultPage = impl.query( |
Alexander Dorokhine | c9fc960 | 2020-10-06 01:39:50 -0700 | [diff] [blame] | 178 | databaseName, |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 179 | queryExpression, |
| 180 | new SearchSpec(searchSpecBundle)); |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 181 | invokeCallbackOnResult(callback, |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 182 | AppSearchResult.newSuccessfulResult(searchResultPage.getBundle())); |
Alexander Dorokhine | e708e18 | 2020-03-06 15:30:34 -0800 | [diff] [blame] | 183 | } catch (Throwable t) { |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 184 | invokeCallbackOnError(callback, t); |
| 185 | } finally { |
| 186 | Binder.restoreCallingIdentity(callingIdentity); |
| 187 | } |
| 188 | } |
| 189 | |
Terry Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame] | 190 | public void globalQuery( |
| 191 | @NonNull String queryExpression, |
| 192 | @NonNull Bundle searchSpecBundle, |
| 193 | @NonNull IAppSearchResultCallback callback) { |
| 194 | Preconditions.checkNotNull(queryExpression); |
| 195 | Preconditions.checkNotNull(searchSpecBundle); |
| 196 | Preconditions.checkNotNull(callback); |
| 197 | int callingUid = Binder.getCallingUidOrThrow(); |
| 198 | int callingUserId = UserHandle.getUserId(callingUid); |
| 199 | final long callingIdentity = Binder.clearCallingIdentity(); |
| 200 | try { |
Terry Wang | 0d39387 | 2020-12-11 22:17:09 +0000 | [diff] [blame] | 201 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame] | 202 | SearchResultPage searchResultPage = impl.globalQuery( |
| 203 | queryExpression, |
| 204 | new SearchSpec(searchSpecBundle)); |
| 205 | invokeCallbackOnResult(callback, |
| 206 | AppSearchResult.newSuccessfulResult(searchResultPage.getBundle())); |
| 207 | } catch (Throwable t) { |
| 208 | invokeCallbackOnError(callback, t); |
| 209 | } finally { |
| 210 | Binder.restoreCallingIdentity(callingIdentity); |
| 211 | } |
| 212 | } |
| 213 | |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 214 | @Override |
| 215 | public void getNextPage(long nextPageToken, |
| 216 | @NonNull IAppSearchResultCallback callback) { |
Terry Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame] | 217 | Preconditions.checkNotNull(callback); |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 218 | int callingUid = Binder.getCallingUidOrThrow(); |
| 219 | int callingUserId = UserHandle.getUserId(callingUid); |
| 220 | final long callingIdentity = Binder.clearCallingIdentity(); |
| 221 | // TODO(b/162450968) check nextPageToken is being advanced by the same uid as originally |
| 222 | // opened it |
| 223 | try { |
Terry Wang | 0d39387 | 2020-12-11 22:17:09 +0000 | [diff] [blame] | 224 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 225 | SearchResultPage searchResultPage = impl.getNextPage(nextPageToken); |
| 226 | invokeCallbackOnResult(callback, |
| 227 | AppSearchResult.newSuccessfulResult(searchResultPage.getBundle())); |
| 228 | } catch (Throwable t) { |
| 229 | invokeCallbackOnError(callback, t); |
| 230 | } finally { |
| 231 | Binder.restoreCallingIdentity(callingIdentity); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public void invalidateNextPageToken(long nextPageToken) { |
| 237 | int callingUid = Binder.getCallingUidOrThrow(); |
| 238 | int callingUserId = UserHandle.getUserId(callingUid); |
| 239 | final long callingIdentity = Binder.clearCallingIdentity(); |
| 240 | try { |
Terry Wang | 0d39387 | 2020-12-11 22:17:09 +0000 | [diff] [blame] | 241 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 242 | impl.invalidateNextPageToken(nextPageToken); |
| 243 | } catch (Throwable t) { |
| 244 | Log.d(TAG, "Unable to invalidate the query page token", t); |
Alexander Dorokhine | e708e18 | 2020-03-06 15:30:34 -0800 | [diff] [blame] | 245 | } finally { |
| 246 | Binder.restoreCallingIdentity(callingIdentity); |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 247 | } |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 248 | } |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 249 | |
Alexander Dorokhine | f6c66ae | 2020-03-09 14:47:25 -0700 | [diff] [blame] | 250 | @Override |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 251 | public void removeByUri(@NonNull String databaseName, @NonNull String namespace, |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 252 | @NonNull List<String> uris, |
| 253 | @NonNull IAppSearchBatchResultCallback callback) { |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 254 | Preconditions.checkNotNull(databaseName); |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 255 | Preconditions.checkNotNull(uris); |
| 256 | Preconditions.checkNotNull(callback); |
| 257 | int callingUid = Binder.getCallingUidOrThrow(); |
| 258 | int callingUserId = UserHandle.getUserId(callingUid); |
Jeff Sharkey | ea5328c | 2020-10-06 11:18:09 -0600 | [diff] [blame] | 259 | final long callingIdentity = Binder.clearCallingIdentity(); |
Terry Wang | 0d39387 | 2020-12-11 22:17:09 +0000 | [diff] [blame] | 260 | AppSearchBatchResult.Builder<String, Void> resultBuilder = |
| 261 | new AppSearchBatchResult.Builder<>(); |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 262 | try { |
Terry Wang | 0d39387 | 2020-12-11 22:17:09 +0000 | [diff] [blame] | 263 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 264 | databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid); |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 265 | for (int i = 0; i < uris.size(); i++) { |
| 266 | String uri = uris.get(i); |
| 267 | try { |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 268 | impl.remove(databaseName, namespace, uri); |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 269 | resultBuilder.setSuccess(uri, /*result= */null); |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 270 | } catch (Throwable t) { |
| 271 | resultBuilder.setResult(uri, throwableToFailedResult(t)); |
| 272 | } |
| 273 | } |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 274 | invokeCallbackOnResult(callback, resultBuilder.build()); |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 275 | } catch (Throwable t) { |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 276 | invokeCallbackOnError(callback, t); |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 277 | } finally { |
| 278 | Binder.restoreCallingIdentity(callingIdentity); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | @Override |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 283 | public void removeByQuery( |
| 284 | @NonNull String databaseName, |
| 285 | @NonNull String queryExpression, |
| 286 | @NonNull Bundle searchSpecBundle, |
Alexander Dorokhine | 178366b | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 287 | @NonNull IAppSearchResultCallback callback) { |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 288 | Preconditions.checkNotNull(databaseName); |
| 289 | Preconditions.checkNotNull(queryExpression); |
| 290 | Preconditions.checkNotNull(searchSpecBundle); |
Terry Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame] | 291 | Preconditions.checkNotNull(callback); |
Alexander Dorokhine | f6c66ae | 2020-03-09 14:47:25 -0700 | [diff] [blame] | 292 | int callingUid = Binder.getCallingUidOrThrow(); |
| 293 | int callingUserId = UserHandle.getUserId(callingUid); |
Jeff Sharkey | ea5328c | 2020-10-06 11:18:09 -0600 | [diff] [blame] | 294 | final long callingIdentity = Binder.clearCallingIdentity(); |
Alexander Dorokhine | f6c66ae | 2020-03-09 14:47:25 -0700 | [diff] [blame] | 295 | try { |
Terry Wang | 0d39387 | 2020-12-11 22:17:09 +0000 | [diff] [blame] | 296 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 297 | databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid); |
Alexander Dorokhine | 178366b | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 298 | impl.removeByQuery(databaseName, queryExpression, |
| 299 | new SearchSpec(searchSpecBundle)); |
| 300 | invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null)); |
Alexander Dorokhine | f6c66ae | 2020-03-09 14:47:25 -0700 | [diff] [blame] | 301 | } catch (Throwable t) { |
Alexander Dorokhine | 178366b | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 302 | invokeCallbackOnError(callback, t); |
Alexander Dorokhine | f6c66ae | 2020-03-09 14:47:25 -0700 | [diff] [blame] | 303 | } finally { |
| 304 | Binder.restoreCallingIdentity(callingIdentity); |
| 305 | } |
| 306 | } |
| 307 | |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 308 | @Override |
| 309 | public void initialize(@NonNull IAppSearchResultCallback callback) { |
Terry Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame] | 310 | Preconditions.checkNotNull(callback); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 311 | int callingUid = Binder.getCallingUidOrThrow(); |
| 312 | int callingUserId = UserHandle.getUserId(callingUid); |
| 313 | final long callingIdentity = Binder.clearCallingIdentity(); |
| 314 | try { |
Terry Wang | 0d39387 | 2020-12-11 22:17:09 +0000 | [diff] [blame] | 315 | ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 316 | invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null)); |
| 317 | } catch (Throwable t) { |
| 318 | invokeCallbackOnError(callback, t); |
| 319 | } finally { |
| 320 | Binder.restoreCallingIdentity(callingIdentity); |
| 321 | } |
| 322 | } |
| 323 | |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 324 | /** |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 325 | * 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] | 326 | * |
| 327 | * <p>The current implementation returns the package name of the app with this uid in a |
| 328 | * format like {@code com.example.package} or {@code com.example.sharedname:5678}. |
| 329 | */ |
| 330 | @NonNull |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 331 | private String rewriteDatabaseNameWithUid(String databaseName, int callingUid) { |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 332 | // For regular apps, this call will return the package name. If callingUid is an |
| 333 | // android:sharedUserId, this value may be another type of name and have a :uid suffix. |
| 334 | String callingUidName = getContext().getPackageManager().getNameForUid(callingUid); |
| 335 | if (callingUidName == null) { |
| 336 | // Not sure how this is possible --- maybe app was uninstalled? |
| 337 | throw new IllegalStateException( |
| 338 | "Failed to look up package name for uid " + callingUid); |
| 339 | } |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 340 | return callingUidName + CALLING_NAME_DATABASE_DELIMITER + databaseName; |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 343 | /** Invokes the {@link IAppSearchResultCallback} with the result. */ |
| 344 | private void invokeCallbackOnResult(IAppSearchResultCallback callback, |
| 345 | AppSearchResult result) { |
| 346 | try { |
| 347 | callback.onResult(result); |
| 348 | } catch (RemoteException e) { |
| 349 | Log.d(TAG, "Unable to send result to the callback", e); |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 350 | } |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 351 | } |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 352 | |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 353 | /** Invokes the {@link IAppSearchBatchResultCallback} with the result. */ |
| 354 | private void invokeCallbackOnResult(IAppSearchBatchResultCallback callback, |
| 355 | AppSearchBatchResult result) { |
| 356 | try { |
| 357 | callback.onResult(result); |
| 358 | } catch (RemoteException e) { |
| 359 | Log.d(TAG, "Unable to send result to the callback", e); |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 360 | } |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Invokes the {@link IAppSearchResultCallback} with an throwable. |
| 365 | * |
| 366 | * <p>The throwable is convert to a {@link AppSearchResult}; |
| 367 | */ |
| 368 | private void invokeCallbackOnError(IAppSearchResultCallback callback, Throwable throwable) { |
| 369 | try { |
| 370 | callback.onResult(throwableToFailedResult(throwable)); |
| 371 | } catch (RemoteException e) { |
| 372 | Log.d(TAG, "Unable to send result to the callback", e); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /** |
Terry Wang | 0d39387 | 2020-12-11 22:17:09 +0000 | [diff] [blame] | 377 | * Invokes the {@link IAppSearchBatchResultCallback} with an throwable. |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 378 | * |
| 379 | * <p>The throwable is converted to {@link ParcelableException}. |
| 380 | */ |
| 381 | private void invokeCallbackOnError(IAppSearchBatchResultCallback callback, |
| 382 | Throwable throwable) { |
| 383 | try { |
| 384 | callback.onSystemError(new ParcelableException(throwable)); |
| 385 | } catch (RemoteException e) { |
| 386 | Log.d(TAG, "Unable to send error to the callback", e); |
| 387 | } |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 388 | } |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 389 | } |
| 390 | } |