Debian Codegraph @

core/java/android/service/search/SearchUiService.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 The Android Open Source Project
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 android.service.search;
17
18 import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage;
19
20 import android.annotation.CallSuper;
21 import android.annotation.MainThread;
22 import android.annotation.NonNull;
23 import android.annotation.SystemApi;
24 import android.app.Service;
25 import android.app.search.ISearchCallback;
26 import android.app.search.Query;
27 import android.app.search.SearchContext;
28 import android.app.search.SearchSessionId;
29 import android.app.search.SearchTarget;
30 import android.app.search.SearchTargetEvent;
31 import android.content.Intent;
32 import android.content.pm.ParceledListSlice;
33 import android.os.Handler;
34 import android.os.IBinder;
35 import android.os.Looper;
36 import android.os.RemoteException;
37 import android.service.search.ISearchUiService.Stub;
38 import android.util.Slog;
39
40 import java.util.List;
41 import java.util.function.Consumer;
42
43 /**
44 * A service used to share the lifecycle of search UI (open, close, interaction)
45 * and also to return search result on a query.
46 *
47 * To understand the lifecycle of search session and how a query get issued,
48 * {@see SearchSession}
49 *
50 * @hide
51 */
52 @SystemApi
53 public abstract class SearchUiService extends Service {
54
55 private static final boolean DEBUG = false;
56 private static final String TAG = "SearchUiService";
57
58 /**
59 * The {@link Intent} that must be declared as handled by the service.
60 *
61 * <p>The service must also require the {@link android.permission#MANAGE_SEARCH_UI}
62 * permission.
63 *
64 * @hide
65 */
66 public static final String SERVICE_INTERFACE =
67 "android.service.search.SearchUiService";
68
69 private Handler mHandler;
70
71 private final android.service.search.ISearchUiService mInterface = new Stub() {
72
73 @Override
74 public void onCreateSearchSession(SearchContext context, SearchSessionId sessionId) {
75 mHandler.sendMessage(
76 obtainMessage(SearchUiService::onSearchSessionCreated,
77 SearchUiService.this, context, sessionId));
78 // to be removed
79 mHandler.sendMessage(
80 obtainMessage(SearchUiService::onCreateSearchSession,
81 SearchUiService.this, context, sessionId));
82 }
83
84 @Override
85 public void onQuery(SearchSessionId sessionId, Query input,
86 ISearchCallback callback) {
87 mHandler.sendMessage(
88 obtainMessage(SearchUiService::onQuery,
89 SearchUiService.this, sessionId, input,
90 new CallbackWrapper(callback)));
91 }
92
93 @Override
94 public void onNotifyEvent(SearchSessionId sessionId, Query query, SearchTargetEvent event) {
95 mHandler.sendMessage(
96 obtainMessage(SearchUiService::onNotifyEvent,
97 SearchUiService.this, sessionId, query, event));
98 }
99
100 @Override
101 public void onDestroy(SearchSessionId sessionId) {
102 mHandler.sendMessage(
103 obtainMessage(SearchUiService::doDestroy,
104 SearchUiService.this, sessionId));
105 }
106 };
107
108 @CallSuper
109 @Override
110 public void onCreate() {
111 super.onCreate();
112 mHandler = new Handler(Looper.getMainLooper(), null, true);
113 }
114
115 @Override
116 @NonNull
117 public final IBinder onBind(@NonNull Intent intent) {
118 if (SERVICE_INTERFACE.equals(intent.getAction())) {
119 return mInterface.asBinder();
120 }
121 Slog.w(TAG, "Tried to bind to wrong intent (should be "
122 + SERVICE_INTERFACE + ": " + intent);
123 return null;
124 }
125
126 /**
127 * Creates a new search session.
128 *
129 * @deprecated this is method will be removed as soon as
130 * {@link #onSearchSessionCreated(SearchContext, SearchSessionId)}
131 * is adopted by the service.
132 *
133 * @removed
134 */
135 @Deprecated
136 public void onCreateSearchSession(@NonNull SearchContext context,
137 @NonNull SearchSessionId sessionId) {}
138
139 /**
140 * A new search session is created.
141 */
142 public void onSearchSessionCreated(@NonNull SearchContext context,
143 @NonNull SearchSessionId sessionId) {}
144
145 /**
146 * Called by the client to request search results using a query string.
147 */
148 @MainThread
149 public abstract void onQuery(@NonNull SearchSessionId sessionId,
150 @NonNull Query query,
151 @NonNull Consumer<List<SearchTarget>> callback);
152
153 /**
154 * Called by a client to indicate an interaction (tap, long press, drag, etc) on target(s)
155 * and lifecycle event on the search surface (e.g., visibility change).
156 *
157 * {@see SearchTargetEvent}
158 */
159 @MainThread
160 public abstract void onNotifyEvent(@NonNull SearchSessionId sessionId,
161 @NonNull Query query,
162 @NonNull SearchTargetEvent event);
163
164 private void doDestroy(@NonNull SearchSessionId sessionId) {
165 super.onDestroy();
166 onDestroy(sessionId);
167 }
168
169 /**
170 * Destroys a search session.
171 */
172 @MainThread
173 public abstract void onDestroy(@NonNull SearchSessionId sessionId);
174
175 private static final class CallbackWrapper implements Consumer<List<SearchTarget>> {
176
177 private ISearchCallback mCallback;
178
179 CallbackWrapper(ISearchCallback callback) {
180 mCallback = callback;
181 }
182
183 @Override
184 public void accept(List<SearchTarget> searchTargets) {
185 try {
186 if (mCallback != null) {
187 if (DEBUG) {
188 Slog.d(TAG, "CallbackWrapper.accept searchTargets=" + searchTargets);
189 }
190 mCallback.onResult(new ParceledListSlice(searchTargets));
191 }
192 } catch (RemoteException e) {
193 Slog.e(TAG, "Error sending result:" + e);
194 }
195 }
196 }
197 }