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 |
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); |
| 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 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); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 133 | databaseName = rewriteDatabaseNameWithUid(databaseName, 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. |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 139 | impl.putDocument(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); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 168 | databaseName = rewriteDatabaseNameWithUid(databaseName, 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 { |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 172 | GenericDocument document = impl.getDocument(databaseName, namespace, uri); |
| 173 | resultBuilder.setSuccess(uri, document.getBundle()); |
Alexander Dorokhine | a95f44f | 2020-03-06 13:53:14 -0800 | [diff] [blame] | 174 | } catch (Throwable t) { |
| 175 | resultBuilder.setResult(uri, throwableToFailedResult(t)); |
| 176 | } |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 177 | } |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 178 | invokeCallbackOnResult(callback, resultBuilder.build()); |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 179 | } catch (Throwable t) { |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 180 | invokeCallbackOnError(callback, t); |
Alexander Dorokhine | 69a8d9f | 2020-03-06 10:43:16 -0800 | [diff] [blame] | 181 | } finally { |
| 182 | Binder.restoreCallingIdentity(callingIdentity); |
| 183 | } |
| 184 | } |
| 185 | |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 186 | // TODO(sidchhabra): Do this in a threadpool. |
| 187 | @Override |
Alexander Dorokhine | e708e18 | 2020-03-06 15:30:34 -0800 | [diff] [blame] | 188 | public void query( |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 189 | @NonNull String databaseName, |
Alexander Dorokhine | c9fc960 | 2020-10-06 01:39:50 -0700 | [diff] [blame] | 190 | @NonNull String queryExpression, |
| 191 | @NonNull Bundle searchSpecBundle, |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 192 | @NonNull IAppSearchResultCallback callback) { |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 193 | Preconditions.checkNotNull(databaseName); |
Alexander Dorokhine | c9fc960 | 2020-10-06 01:39:50 -0700 | [diff] [blame] | 194 | Preconditions.checkNotNull(queryExpression); |
| 195 | Preconditions.checkNotNull(searchSpecBundle); |
Terry Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame] | 196 | Preconditions.checkNotNull(callback); |
Alexander Dorokhine | e708e18 | 2020-03-06 15:30:34 -0800 | [diff] [blame] | 197 | int callingUid = Binder.getCallingUidOrThrow(); |
| 198 | int callingUserId = UserHandle.getUserId(callingUid); |
Jeff Sharkey | ea5328c | 2020-10-06 11:18:09 -0600 | [diff] [blame] | 199 | final long callingIdentity = Binder.clearCallingIdentity(); |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 200 | try { |
Terry Wang | 0d39387 | 2020-12-11 22:17:09 +0000 | [diff] [blame] | 201 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 202 | databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid); |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 203 | SearchResultPage searchResultPage = impl.query( |
Alexander Dorokhine | c9fc960 | 2020-10-06 01:39:50 -0700 | [diff] [blame] | 204 | databaseName, |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 205 | queryExpression, |
| 206 | new SearchSpec(searchSpecBundle)); |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 207 | invokeCallbackOnResult(callback, |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 208 | AppSearchResult.newSuccessfulResult(searchResultPage.getBundle())); |
Alexander Dorokhine | e708e18 | 2020-03-06 15:30:34 -0800 | [diff] [blame] | 209 | } catch (Throwable t) { |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 210 | invokeCallbackOnError(callback, t); |
| 211 | } finally { |
| 212 | Binder.restoreCallingIdentity(callingIdentity); |
| 213 | } |
| 214 | } |
| 215 | |
Terry Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame] | 216 | 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 Wang | 0d39387 | 2020-12-11 22:17:09 +0000 | [diff] [blame] | 227 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame] | 228 | 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 Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 240 | @Override |
| 241 | public void getNextPage(long nextPageToken, |
| 242 | @NonNull IAppSearchResultCallback callback) { |
Terry Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame] | 243 | Preconditions.checkNotNull(callback); |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 244 | 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 Wang | 0d39387 | 2020-12-11 22:17:09 +0000 | [diff] [blame] | 250 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 251 | 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 Wang | 0d39387 | 2020-12-11 22:17:09 +0000 | [diff] [blame] | 267 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Alexander Dorokhine | d48f236 | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 268 | impl.invalidateNextPageToken(nextPageToken); |
| 269 | } catch (Throwable t) { |
| 270 | Log.d(TAG, "Unable to invalidate the query page token", t); |
Alexander Dorokhine | e708e18 | 2020-03-06 15:30:34 -0800 | [diff] [blame] | 271 | } finally { |
| 272 | Binder.restoreCallingIdentity(callingIdentity); |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 273 | } |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 274 | } |
Alexander Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 275 | |
Alexander Dorokhine | f6c66ae | 2020-03-09 14:47:25 -0700 | [diff] [blame] | 276 | @Override |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 277 | public void removeByUri(@NonNull String databaseName, @NonNull String namespace, |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 278 | @NonNull List<String> uris, |
| 279 | @NonNull IAppSearchBatchResultCallback callback) { |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 280 | Preconditions.checkNotNull(databaseName); |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 281 | Preconditions.checkNotNull(uris); |
| 282 | Preconditions.checkNotNull(callback); |
| 283 | int callingUid = Binder.getCallingUidOrThrow(); |
| 284 | int callingUserId = UserHandle.getUserId(callingUid); |
Jeff Sharkey | ea5328c | 2020-10-06 11:18:09 -0600 | [diff] [blame] | 285 | final long callingIdentity = Binder.clearCallingIdentity(); |
Terry Wang | 0d39387 | 2020-12-11 22:17:09 +0000 | [diff] [blame] | 286 | AppSearchBatchResult.Builder<String, Void> resultBuilder = |
| 287 | new AppSearchBatchResult.Builder<>(); |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 288 | try { |
Terry Wang | 0d39387 | 2020-12-11 22:17:09 +0000 | [diff] [blame] | 289 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 290 | databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid); |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 291 | for (int i = 0; i < uris.size(); i++) { |
| 292 | String uri = uris.get(i); |
| 293 | try { |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 294 | impl.remove(databaseName, namespace, uri); |
Alexander Dorokhine | c66d67c | 2020-10-08 13:44:04 -0700 | [diff] [blame] | 295 | resultBuilder.setSuccess(uri, /*result= */null); |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 296 | } catch (Throwable t) { |
| 297 | resultBuilder.setResult(uri, throwableToFailedResult(t)); |
| 298 | } |
| 299 | } |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 300 | invokeCallbackOnResult(callback, resultBuilder.build()); |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 301 | } catch (Throwable t) { |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 302 | invokeCallbackOnError(callback, t); |
Alexander Dorokhine | ff82fba | 2020-03-09 16:35:24 -0700 | [diff] [blame] | 303 | } finally { |
| 304 | Binder.restoreCallingIdentity(callingIdentity); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | @Override |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 309 | public void removeByQuery( |
| 310 | @NonNull String databaseName, |
| 311 | @NonNull String queryExpression, |
| 312 | @NonNull Bundle searchSpecBundle, |
Alexander Dorokhine | 178366b | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 313 | @NonNull IAppSearchResultCallback callback) { |
Terry Wang | 26b9e5c | 2020-10-23 02:05:01 -0700 | [diff] [blame] | 314 | Preconditions.checkNotNull(databaseName); |
| 315 | Preconditions.checkNotNull(queryExpression); |
| 316 | Preconditions.checkNotNull(searchSpecBundle); |
Terry Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame] | 317 | Preconditions.checkNotNull(callback); |
Alexander Dorokhine | f6c66ae | 2020-03-09 14:47:25 -0700 | [diff] [blame] | 318 | int callingUid = Binder.getCallingUidOrThrow(); |
| 319 | int callingUserId = UserHandle.getUserId(callingUid); |
Jeff Sharkey | ea5328c | 2020-10-06 11:18:09 -0600 | [diff] [blame] | 320 | final long callingIdentity = Binder.clearCallingIdentity(); |
Alexander Dorokhine | f6c66ae | 2020-03-09 14:47:25 -0700 | [diff] [blame] | 321 | try { |
Terry Wang | 0d39387 | 2020-12-11 22:17:09 +0000 | [diff] [blame] | 322 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 323 | databaseName = rewriteDatabaseNameWithUid(databaseName, callingUid); |
Alexander Dorokhine | 178366b | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 324 | impl.removeByQuery(databaseName, queryExpression, |
| 325 | new SearchSpec(searchSpecBundle)); |
| 326 | invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null)); |
Alexander Dorokhine | f6c66ae | 2020-03-09 14:47:25 -0700 | [diff] [blame] | 327 | } catch (Throwable t) { |
Alexander Dorokhine | 178366b | 2020-10-20 17:40:49 -0700 | [diff] [blame] | 328 | invokeCallbackOnError(callback, t); |
Alexander Dorokhine | f6c66ae | 2020-03-09 14:47:25 -0700 | [diff] [blame] | 329 | } finally { |
| 330 | Binder.restoreCallingIdentity(callingIdentity); |
| 331 | } |
| 332 | } |
| 333 | |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 334 | @Override |
| 335 | public void initialize(@NonNull IAppSearchResultCallback callback) { |
Terry Wang | bfbfcac | 2020-11-06 15:46:44 -0800 | [diff] [blame] | 336 | Preconditions.checkNotNull(callback); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 337 | int callingUid = Binder.getCallingUidOrThrow(); |
| 338 | int callingUserId = UserHandle.getUserId(callingUid); |
| 339 | final long callingIdentity = Binder.clearCallingIdentity(); |
| 340 | try { |
Terry Wang | 0d39387 | 2020-12-11 22:17:09 +0000 | [diff] [blame] | 341 | ImplInstanceManager.getInstance(getContext(), callingUserId); |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 342 | invokeCallbackOnResult(callback, AppSearchResult.newSuccessfulResult(null)); |
| 343 | } catch (Throwable t) { |
| 344 | invokeCallbackOnError(callback, t); |
| 345 | } finally { |
| 346 | Binder.restoreCallingIdentity(callingIdentity); |
| 347 | } |
| 348 | } |
| 349 | |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 350 | /** |
Terry Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 351 | * 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] | 352 | * |
| 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 Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 357 | private String rewriteDatabaseNameWithUid(String databaseName, int callingUid) { |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 358 | // 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 Wang | 6413aee | 2020-10-07 03:04:58 -0700 | [diff] [blame] | 366 | return callingUidName + CALLING_NAME_DATABASE_DELIMITER + databaseName; |
Alexander Dorokhine | ebd3774 | 2020-09-22 15:02:26 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 369 | /** 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 Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 376 | } |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 377 | } |
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 | /** 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 Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 386 | } |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 387 | } |
| 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 Wang | 0d39387 | 2020-12-11 22:17:09 +0000 | [diff] [blame] | 403 | * Invokes the {@link IAppSearchBatchResultCallback} with an throwable. |
Terry Wang | dbd1dca | 2020-11-03 17:03:56 -0800 | [diff] [blame] | 404 | * |
| 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 Dorokhine | 969f446 | 2020-03-05 15:54:19 -0800 | [diff] [blame] | 414 | } |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 415 | } |
| 416 | } |