Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 1 | /* |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 2 | * Copyright (C) 2020 The Android Open Source Project |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.server.appsearch; |
| 17 | |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 18 | import android.annotation.NonNull; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 19 | import android.app.appsearch.IAppSearchManager; |
| 20 | import android.content.Context; |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 21 | import android.os.Binder; |
| 22 | import android.os.UserHandle; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 23 | |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 24 | import com.android.internal.infra.AndroidFuture; |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 25 | import com.android.internal.util.Preconditions; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 26 | import com.android.server.SystemService; |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 27 | import com.android.server.appsearch.impl.AppSearchImpl; |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 28 | import com.android.server.appsearch.impl.FakeIcing; |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 29 | import com.android.server.appsearch.impl.ImplInstanceManager; |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 30 | |
Alexander Dorokhine | cc22345 | 2020-01-19 03:00:28 -0800 | [diff] [blame^] | 31 | import com.google.android.icing.proto.DocumentProto; |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 32 | import com.google.android.icing.proto.SchemaProto; |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 33 | import com.google.android.icing.proto.SearchResultProto; |
| 34 | import com.google.android.icing.proto.SearchSpecProto; |
| 35 | import com.google.android.icing.protobuf.InvalidProtocolBufferException; |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 36 | |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 37 | /** |
| 38 | * TODO(b/142567528): add comments when implement this class |
| 39 | */ |
| 40 | public class AppSearchManagerService extends SystemService { |
| 41 | |
| 42 | public AppSearchManagerService(Context context) { |
| 43 | super(context); |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 44 | mFakeIcing = new FakeIcing(); |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 45 | } |
| 46 | |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 47 | private final FakeIcing mFakeIcing; |
| 48 | |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 49 | @Override |
| 50 | public void onStart() { |
| 51 | publishBinderService(Context.APP_SEARCH_SERVICE, new Stub()); |
| 52 | } |
| 53 | |
| 54 | private class Stub extends IAppSearchManager.Stub { |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 55 | @Override |
Alexander Dorokhine | 321e4b1 | 2020-01-18 11:15:00 -0800 | [diff] [blame] | 56 | public void setSchema(byte[] schemaBytes, boolean forceOverride, AndroidFuture callback) { |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 57 | Preconditions.checkNotNull(schemaBytes); |
| 58 | Preconditions.checkNotNull(callback); |
| 59 | int callingUid = Binder.getCallingUidOrThrow(); |
| 60 | int callingUserId = UserHandle.getUserId(callingUid); |
| 61 | long callingIdentity = Binder.clearCallingIdentity(); |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 62 | try { |
| 63 | SchemaProto schema = SchemaProto.parseFrom(schemaBytes); |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 64 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Alexander Dorokhine | 321e4b1 | 2020-01-18 11:15:00 -0800 | [diff] [blame] | 65 | impl.setSchema(callingUid, schema, forceOverride); |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 66 | callback.complete(null); |
Alexander Dorokhine | 179c8b8 | 2020-01-11 00:17:48 -0800 | [diff] [blame] | 67 | } catch (Throwable t) { |
| 68 | callback.completeExceptionally(t); |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 69 | } finally { |
| 70 | Binder.restoreCallingIdentity(callingIdentity); |
Alexander Dorokhine | 179c8b8 | 2020-01-11 00:17:48 -0800 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | @Override |
Alexander Dorokhine | cc22345 | 2020-01-19 03:00:28 -0800 | [diff] [blame^] | 75 | public void putDocument(byte[] documentBytes, AndroidFuture callback) { |
| 76 | Preconditions.checkNotNull(documentBytes); |
| 77 | Preconditions.checkNotNull(callback); |
| 78 | int callingUid = Binder.getCallingUidOrThrow(); |
| 79 | int callingUserId = UserHandle.getUserId(callingUid); |
| 80 | long callingIdentity = Binder.clearCallingIdentity(); |
Alexander Dorokhine | 179c8b8 | 2020-01-11 00:17:48 -0800 | [diff] [blame] | 81 | try { |
Alexander Dorokhine | cc22345 | 2020-01-19 03:00:28 -0800 | [diff] [blame^] | 82 | DocumentProto document = DocumentProto.parseFrom(documentBytes); |
| 83 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
| 84 | impl.putDocument(callingUid, document); |
| 85 | callback.complete(null); |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 86 | } catch (Throwable t) { |
| 87 | callback.completeExceptionally(t); |
Alexander Dorokhine | cc22345 | 2020-01-19 03:00:28 -0800 | [diff] [blame^] | 88 | } finally { |
| 89 | Binder.restoreCallingIdentity(callingIdentity); |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 90 | } |
| 91 | } |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 92 | // TODO(sidchhabra):Init FakeIcing properly. |
| 93 | // TODO(sidchhabra): Do this in a threadpool. |
| 94 | @Override |
| 95 | public void query(@NonNull String queryExpression, @NonNull byte[] searchSpec, |
| 96 | AndroidFuture callback) { |
| 97 | Preconditions.checkNotNull(queryExpression); |
| 98 | Preconditions.checkNotNull(searchSpec); |
| 99 | SearchSpecProto searchSpecProto = null; |
| 100 | try { |
| 101 | searchSpecProto = SearchSpecProto.parseFrom(searchSpec); |
| 102 | } catch (InvalidProtocolBufferException e) { |
| 103 | throw new RuntimeException(e); |
| 104 | } |
| 105 | SearchResultProto searchResults = |
| 106 | mFakeIcing.query(queryExpression); |
| 107 | callback.complete(searchResults.toByteArray()); |
| 108 | } |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 109 | } |
| 110 | } |