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 | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 31 | import com.google.android.icing.proto.SchemaProto; |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 32 | import com.google.android.icing.proto.SearchResultProto; |
| 33 | import com.google.android.icing.proto.SearchSpecProto; |
| 34 | import com.google.android.icing.protobuf.InvalidProtocolBufferException; |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 35 | |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 36 | /** |
| 37 | * TODO(b/142567528): add comments when implement this class |
| 38 | */ |
| 39 | public class AppSearchManagerService extends SystemService { |
| 40 | |
| 41 | public AppSearchManagerService(Context context) { |
| 42 | super(context); |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 43 | mFakeIcing = new FakeIcing(); |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 44 | } |
| 45 | |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 46 | private final FakeIcing mFakeIcing; |
| 47 | |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 48 | @Override |
| 49 | public void onStart() { |
| 50 | publishBinderService(Context.APP_SEARCH_SERVICE, new Stub()); |
| 51 | } |
| 52 | |
| 53 | private class Stub extends IAppSearchManager.Stub { |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 54 | @Override |
Alexander Dorokhine | 321e4b1 | 2020-01-18 11:15:00 -0800 | [diff] [blame^] | 55 | public void setSchema(byte[] schemaBytes, boolean forceOverride, AndroidFuture callback) { |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 56 | Preconditions.checkNotNull(schemaBytes); |
| 57 | Preconditions.checkNotNull(callback); |
| 58 | int callingUid = Binder.getCallingUidOrThrow(); |
| 59 | int callingUserId = UserHandle.getUserId(callingUid); |
| 60 | long callingIdentity = Binder.clearCallingIdentity(); |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 61 | try { |
| 62 | SchemaProto schema = SchemaProto.parseFrom(schemaBytes); |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 63 | AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId); |
Alexander Dorokhine | 321e4b1 | 2020-01-18 11:15:00 -0800 | [diff] [blame^] | 64 | impl.setSchema(callingUid, schema, forceOverride); |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 65 | callback.complete(null); |
Alexander Dorokhine | 179c8b8 | 2020-01-11 00:17:48 -0800 | [diff] [blame] | 66 | } catch (Throwable t) { |
| 67 | callback.completeExceptionally(t); |
Alexander Dorokhine | 270d4f1 | 2020-01-15 17:24:35 -0800 | [diff] [blame] | 68 | } finally { |
| 69 | Binder.restoreCallingIdentity(callingIdentity); |
Alexander Dorokhine | 179c8b8 | 2020-01-11 00:17:48 -0800 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void put(byte[] documentBytes, AndroidFuture callback) { |
| 75 | try { |
| 76 | throw new UnsupportedOperationException("Put document not yet implemented"); |
Alexander Dorokhine | fd07eba | 2020-01-13 20:22:20 -0800 | [diff] [blame] | 77 | } catch (Throwable t) { |
| 78 | callback.completeExceptionally(t); |
| 79 | } |
| 80 | } |
sidchhabra | a7c8f8a | 2020-01-16 18:38:17 -0800 | [diff] [blame] | 81 | // TODO(sidchhabra):Init FakeIcing properly. |
| 82 | // TODO(sidchhabra): Do this in a threadpool. |
| 83 | @Override |
| 84 | public void query(@NonNull String queryExpression, @NonNull byte[] searchSpec, |
| 85 | AndroidFuture callback) { |
| 86 | Preconditions.checkNotNull(queryExpression); |
| 87 | Preconditions.checkNotNull(searchSpec); |
| 88 | SearchSpecProto searchSpecProto = null; |
| 89 | try { |
| 90 | searchSpecProto = SearchSpecProto.parseFrom(searchSpec); |
| 91 | } catch (InvalidProtocolBufferException e) { |
| 92 | throw new RuntimeException(e); |
| 93 | } |
| 94 | SearchResultProto searchResults = |
| 95 | mFakeIcing.query(queryExpression); |
| 96 | callback.complete(searchResults.toByteArray()); |
| 97 | } |
Terry Wang | febbead | 2019-10-17 17:05:18 -0700 | [diff] [blame] | 98 | } |
| 99 | } |