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