blob: 5d6d3f0a8406a3d7753705e624ae0f83591abc06 [file] [log] [blame]
Terry Wangfebbead2019-10-17 17:05:18 -07001/*
sidchhabraa7c8f8a2020-01-16 18:38:17 -08002 * Copyright (C) 2020 The Android Open Source Project
Terry Wangfebbead2019-10-17 17:05:18 -07003 *
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 */
16package com.android.server.appsearch;
17
sidchhabraa7c8f8a2020-01-16 18:38:17 -080018import android.annotation.NonNull;
Terry Wangfebbead2019-10-17 17:05:18 -070019import android.app.appsearch.IAppSearchManager;
20import android.content.Context;
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080021import android.os.Binder;
22import android.os.UserHandle;
Terry Wangfebbead2019-10-17 17:05:18 -070023
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -080024import com.android.internal.infra.AndroidFuture;
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080025import com.android.internal.util.Preconditions;
Terry Wangfebbead2019-10-17 17:05:18 -070026import com.android.server.SystemService;
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080027import com.android.server.appsearch.impl.AppSearchImpl;
sidchhabraa7c8f8a2020-01-16 18:38:17 -080028import com.android.server.appsearch.impl.FakeIcing;
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080029import com.android.server.appsearch.impl.ImplInstanceManager;
Terry Wangfebbead2019-10-17 17:05:18 -070030
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -080031import com.google.android.icing.proto.SchemaProto;
sidchhabraa7c8f8a2020-01-16 18:38:17 -080032import com.google.android.icing.proto.SearchResultProto;
33import com.google.android.icing.proto.SearchSpecProto;
34import com.google.android.icing.protobuf.InvalidProtocolBufferException;
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -080035
Terry Wangfebbead2019-10-17 17:05:18 -070036/**
37 * TODO(b/142567528): add comments when implement this class
38 */
39public class AppSearchManagerService extends SystemService {
40
41 public AppSearchManagerService(Context context) {
42 super(context);
sidchhabraa7c8f8a2020-01-16 18:38:17 -080043 mFakeIcing = new FakeIcing();
Terry Wangfebbead2019-10-17 17:05:18 -070044 }
45
sidchhabraa7c8f8a2020-01-16 18:38:17 -080046 private final FakeIcing mFakeIcing;
47
Terry Wangfebbead2019-10-17 17:05:18 -070048 @Override
49 public void onStart() {
50 publishBinderService(Context.APP_SEARCH_SERVICE, new Stub());
51 }
52
53 private class Stub extends IAppSearchManager.Stub {
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -080054 @Override
Alexander Dorokhine321e4b12020-01-18 11:15:00 -080055 public void setSchema(byte[] schemaBytes, boolean forceOverride, AndroidFuture callback) {
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080056 Preconditions.checkNotNull(schemaBytes);
57 Preconditions.checkNotNull(callback);
58 int callingUid = Binder.getCallingUidOrThrow();
59 int callingUserId = UserHandle.getUserId(callingUid);
60 long callingIdentity = Binder.clearCallingIdentity();
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -080061 try {
62 SchemaProto schema = SchemaProto.parseFrom(schemaBytes);
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080063 AppSearchImpl impl = ImplInstanceManager.getInstance(getContext(), callingUserId);
Alexander Dorokhine321e4b12020-01-18 11:15:00 -080064 impl.setSchema(callingUid, schema, forceOverride);
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080065 callback.complete(null);
Alexander Dorokhine179c8b82020-01-11 00:17:48 -080066 } catch (Throwable t) {
67 callback.completeExceptionally(t);
Alexander Dorokhine270d4f12020-01-15 17:24:35 -080068 } finally {
69 Binder.restoreCallingIdentity(callingIdentity);
Alexander Dorokhine179c8b82020-01-11 00:17:48 -080070 }
71 }
72
73 @Override
74 public void put(byte[] documentBytes, AndroidFuture callback) {
75 try {
76 throw new UnsupportedOperationException("Put document not yet implemented");
Alexander Dorokhinefd07eba2020-01-13 20:22:20 -080077 } catch (Throwable t) {
78 callback.completeExceptionally(t);
79 }
80 }
sidchhabraa7c8f8a2020-01-16 18:38:17 -080081 // 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 Wangfebbead2019-10-17 17:05:18 -070098 }
99}