blob: 192abd5912ab175a58c8b957c90117ff28e1152e [file] [log] [blame]
Felipe Leme749b8892018-12-03 16:30:30 -08001/*
2 * Copyright (C) 2018 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 */
16package android.service.contentcapture;
17
Felipe Leme326f15a2019-02-19 09:42:24 -080018import static android.view.contentcapture.ContentCaptureHelper.sDebug;
19import static android.view.contentcapture.ContentCaptureHelper.sVerbose;
20
Felipe Leme749b8892018-12-03 16:30:30 -080021import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage;
22
23import android.annotation.CallSuper;
24import android.annotation.NonNull;
25import android.annotation.Nullable;
26import android.annotation.SystemApi;
Felipe Leme19652c02019-02-04 13:01:29 -080027import android.annotation.TestApi;
Felipe Leme749b8892018-12-03 16:30:30 -080028import android.app.Service;
29import android.content.ComponentName;
30import android.content.Intent;
Felipe Lemeb96878492018-12-17 12:22:29 -080031import android.content.pm.ParceledListSlice;
32import android.os.Binder;
33import android.os.Bundle;
Felipe Leme749b8892018-12-03 16:30:30 -080034import android.os.Handler;
35import android.os.IBinder;
36import android.os.Looper;
37import android.os.RemoteException;
Felipe Lemebb98ed62018-12-21 09:29:27 -080038import android.service.autofill.AutofillService;
Felipe Lemeb96878492018-12-17 12:22:29 -080039import android.util.ArrayMap;
Felipe Leme749b8892018-12-03 16:30:30 -080040import android.util.Log;
Felipe Lemeb96878492018-12-17 12:22:29 -080041import android.util.Slog;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080042import android.view.contentcapture.ContentCaptureContext;
Felipe Leme749b8892018-12-03 16:30:30 -080043import android.view.contentcapture.ContentCaptureEvent;
Felipe Lemeb96878492018-12-17 12:22:29 -080044import android.view.contentcapture.ContentCaptureManager;
45import android.view.contentcapture.ContentCaptureSession;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080046import android.view.contentcapture.ContentCaptureSessionId;
Felipe Lemeb96878492018-12-17 12:22:29 -080047import android.view.contentcapture.IContentCaptureDirectManager;
Felipe Leme87a9dc92018-12-18 14:28:07 -080048import android.view.contentcapture.MainContentCaptureSession;
Felipe Leme62e45b02019-01-10 10:22:00 -080049import android.view.contentcapture.UserDataRemovalRequest;
Felipe Leme749b8892018-12-03 16:30:30 -080050
Felipe Lemeb96878492018-12-17 12:22:29 -080051import com.android.internal.os.IResultReceiver;
52
53import java.io.FileDescriptor;
54import java.io.PrintWriter;
Felipe Leme7a3c9f52019-02-13 16:32:49 -080055import java.util.ArrayList;
Felipe Leme749b8892018-12-03 16:30:30 -080056import java.util.List;
Felipe Leme7a3c9f52019-02-13 16:32:49 -080057import java.util.Set;
Felipe Leme749b8892018-12-03 16:30:30 -080058
59/**
60 * A service used to capture the content of the screen to provide contextual data in other areas of
61 * the system such as Autofill.
62 *
63 * @hide
64 */
65@SystemApi
Felipe Leme19652c02019-02-04 13:01:29 -080066@TestApi
Felipe Leme749b8892018-12-03 16:30:30 -080067public abstract class ContentCaptureService extends Service {
68
69 private static final String TAG = ContentCaptureService.class.getSimpleName();
70
Felipe Leme749b8892018-12-03 16:30:30 -080071 /**
72 * The {@link Intent} that must be declared as handled by the service.
73 *
74 * <p>To be supported, the service must also require the
75 * {@link android.Manifest.permission#BIND_CONTENT_CAPTURE_SERVICE} permission so
76 * that other applications can not abuse it.
77 */
78 public static final String SERVICE_INTERFACE =
79 "android.service.contentcapture.ContentCaptureService";
80
81 private Handler mHandler;
Felipe Lemeaf4e4fd2019-01-15 08:32:42 -080082 private IContentCaptureServiceCallback mCallback;
Felipe Leme749b8892018-12-03 16:30:30 -080083
Felipe Lemeb96878492018-12-17 12:22:29 -080084 /**
85 * Binder that receives calls from the system server.
86 */
87 private final IContentCaptureService mServerInterface = new IContentCaptureService.Stub() {
Felipe Leme749b8892018-12-03 16:30:30 -080088
89 @Override
Felipe Leme326f15a2019-02-19 09:42:24 -080090 public void onConnected(IBinder callback, boolean verbose, boolean debug) {
91 sVerbose = verbose;
92 sDebug = debug;
Felipe Lemeaf4e4fd2019-01-15 08:32:42 -080093 mHandler.sendMessage(obtainMessage(ContentCaptureService::handleOnConnected,
94 ContentCaptureService.this, callback));
95 }
96
97 @Override
98 public void onDisconnected() {
99 mHandler.sendMessage(obtainMessage(ContentCaptureService::handleOnDisconnected,
100 ContentCaptureService.this));
Felipe Lemebb98ed62018-12-21 09:29:27 -0800101 }
102
103 @Override
Felipe Lemeb96878492018-12-17 12:22:29 -0800104 public void onSessionStarted(ContentCaptureContext context, String sessionId, int uid,
105 IResultReceiver clientReceiver) {
106 mHandler.sendMessage(obtainMessage(ContentCaptureService::handleOnCreateSession,
107 ContentCaptureService.this, context, sessionId, uid, clientReceiver));
Felipe Leme749b8892018-12-03 16:30:30 -0800108 }
109
110 @Override
111 public void onActivitySnapshot(String sessionId, SnapshotData snapshotData) {
112 mHandler.sendMessage(
113 obtainMessage(ContentCaptureService::handleOnActivitySnapshot,
114 ContentCaptureService.this, sessionId, snapshotData));
115 }
Felipe Lemeb96878492018-12-17 12:22:29 -0800116
117 @Override
118 public void onSessionFinished(String sessionId) {
119 mHandler.sendMessage(obtainMessage(ContentCaptureService::handleFinishSession,
120 ContentCaptureService.this, sessionId));
121 }
Adam He3d0409b2019-01-15 14:22:04 -0800122
123 @Override
124 public void onUserDataRemovalRequest(UserDataRemovalRequest request) {
125 mHandler.sendMessage(
126 obtainMessage(ContentCaptureService::handleOnUserDataRemovalRequest,
127 ContentCaptureService.this, request));
128 }
Felipe Leme141864d2019-02-27 17:01:51 -0800129
130 @Override
131 public void onActivityEvent(ActivityEvent event) {
132 mHandler.sendMessage(obtainMessage(ContentCaptureService::handleOnActivityEvent,
133 ContentCaptureService.this, event));
134
135 }
Felipe Leme749b8892018-12-03 16:30:30 -0800136 };
137
Felipe Lemeb96878492018-12-17 12:22:29 -0800138 /**
139 * Binder that receives calls from the app.
140 */
141 private final IContentCaptureDirectManager mClientInterface =
142 new IContentCaptureDirectManager.Stub() {
143
144 @Override
Felipe Lemefc24bea2018-12-18 13:19:01 -0800145 public void sendEvents(@SuppressWarnings("rawtypes") ParceledListSlice events) {
Felipe Lemeb96878492018-12-17 12:22:29 -0800146 mHandler.sendMessage(obtainMessage(ContentCaptureService::handleSendEvents,
Felipe Lemefc24bea2018-12-18 13:19:01 -0800147 ContentCaptureService.this, Binder.getCallingUid(), events));
Felipe Lemeb96878492018-12-17 12:22:29 -0800148 }
149 };
150
151 /**
Felipe Lemee127c9a2019-01-04 14:56:38 -0800152 * UIDs associated with each session.
Felipe Lemeb96878492018-12-17 12:22:29 -0800153 *
154 * <p>This map is populated when an session is started, which is called by the system server
155 * and can be trusted. Then subsequent calls made by the app are verified against this map.
156 */
Felipe Lemee127c9a2019-01-04 14:56:38 -0800157 private final ArrayMap<String, Integer> mSessionUids = new ArrayMap<>();
Felipe Lemeb96878492018-12-17 12:22:29 -0800158
Felipe Leme749b8892018-12-03 16:30:30 -0800159 @CallSuper
160 @Override
161 public void onCreate() {
162 super.onCreate();
163 mHandler = new Handler(Looper.getMainLooper(), null, true);
164 }
165
166 /** @hide */
167 @Override
168 public final IBinder onBind(Intent intent) {
169 if (SERVICE_INTERFACE.equals(intent.getAction())) {
Felipe Lemeb96878492018-12-17 12:22:29 -0800170 return mServerInterface.asBinder();
Felipe Leme749b8892018-12-03 16:30:30 -0800171 }
172 Log.w(TAG, "Tried to bind to wrong intent (should be " + SERVICE_INTERFACE + ": " + intent);
173 return null;
174 }
175
176 /**
Felipe Leme7a3c9f52019-02-13 16:32:49 -0800177 * Explicitly limits content capture to the given packages and activities.
178 *
179 * <p>To reset the whitelist, call it passing {@code null} to both arguments.
180 *
181 * <p>Useful when the service wants to restrict content capture to a category of apps, like
182 * chat apps. For example, if the service wants to support view captures on all activities of
183 * app {@code ChatApp1} and just activities {@code act1} and {@code act2} of {@code ChatApp2},
184 * it would call: {@code setContentCaptureWhitelist(Sets.newArraySet("ChatApp1"),
185 * Sets.newArraySet(new ComponentName("ChatApp2", "act1"),
186 * new ComponentName("ChatApp2", "act2")));}
187 */
188 public final void setContentCaptureWhitelist(@Nullable Set<String> packages,
189 @Nullable Set<ComponentName> activities) {
Felipe Lemee8eae9d42019-02-13 15:41:55 -0800190 final IContentCaptureServiceCallback callback = mCallback;
191 if (callback == null) {
192 Log.w(TAG, "setContentCaptureWhitelist(): no server callback");
193 return;
194 }
195
196 try {
197 callback.setContentCaptureWhitelist(toList(packages), toList(activities));
198 } catch (RemoteException e) {
199 e.rethrowFromSystemServer();
200 }
Felipe Leme7a3c9f52019-02-13 16:32:49 -0800201 }
202
203 private <T> ArrayList<T> toList(@Nullable Set<T> set) {
204 return set == null ? null : new ArrayList<T>(set);
205 }
206
207 /**
Felipe Lemebb98ed62018-12-21 09:29:27 -0800208 * Called when the Android system connects to service.
209 *
210 * <p>You should generally do initialization here rather than in {@link #onCreate}.
211 */
212 public void onConnected() {
213 Slog.i(TAG, "bound to " + getClass().getName());
214 }
215
216 /**
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800217 * Creates a new content capture session.
Felipe Leme749b8892018-12-03 16:30:30 -0800218 *
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800219 * @param context content capture context
Felipe Leme749b8892018-12-03 16:30:30 -0800220 * @param sessionId the session's Id
221 */
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800222 public void onCreateContentCaptureSession(@NonNull ContentCaptureContext context,
223 @NonNull ContentCaptureSessionId sessionId) {
Felipe Leme326f15a2019-02-19 09:42:24 -0800224 if (sVerbose) {
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800225 Log.v(TAG, "onCreateContentCaptureSession(id=" + sessionId + ", ctx=" + context + ")");
Felipe Leme749b8892018-12-03 16:30:30 -0800226 }
227 }
228
229 /**
Felipe Lemefc24bea2018-12-18 13:19:01 -0800230 * Notifies the service of {@link ContentCaptureEvent events} associated with a content capture
231 * session.
232 *
233 * @param sessionId the session's Id
234 * @param event the event
235 */
236 public void onContentCaptureEvent(@NonNull ContentCaptureSessionId sessionId,
237 @NonNull ContentCaptureEvent event) {
Felipe Leme326f15a2019-02-19 09:42:24 -0800238 if (sVerbose) Log.v(TAG, "onContentCaptureEventsRequest(id=" + sessionId + ")");
Felipe Lemefc24bea2018-12-18 13:19:01 -0800239 }
Felipe Leme62e45b02019-01-10 10:22:00 -0800240
241 /**
242 * Notifies the service that the app requested to remove data associated with the user.
243 *
244 * @param request the user data requested to be removed
245 */
246 public void onUserDataRemovalRequest(@NonNull UserDataRemovalRequest request) {
Felipe Leme326f15a2019-02-19 09:42:24 -0800247 if (sVerbose) Log.v(TAG, "onUserDataRemovalRequest()");
Felipe Leme62e45b02019-01-10 10:22:00 -0800248 }
249
Felipe Lemefc24bea2018-12-18 13:19:01 -0800250 /**
Felipe Leme749b8892018-12-03 16:30:30 -0800251 * Notifies the service of {@link SnapshotData snapshot data} associated with a session.
252 *
253 * @param sessionId the session's Id
254 * @param snapshotData the data
255 */
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800256 public void onActivitySnapshot(@NonNull ContentCaptureSessionId sessionId,
Felipe Leme141864d2019-02-27 17:01:51 -0800257 @NonNull SnapshotData snapshotData) {
258 if (sVerbose) Log.v(TAG, "onActivitySnapshot(id=" + sessionId + ")");
259 }
260
261 /**
262 * Notifies the service of an activity-level event that is not associated with a session.
263 *
264 * <p>This method can be used to track some high-level events for all activities, even those
265 * that are not whitelisted for Content Capture.
266 *
267 * @param event high-level activity event
268 */
269 public void onActivityEvent(@NonNull ActivityEvent event) {
270 if (sVerbose) Log.v(TAG, "onActivityEvent(): " + event);
271 }
Felipe Leme749b8892018-12-03 16:30:30 -0800272
273 /**
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800274 * Destroys the content capture session.
Felipe Leme749b8892018-12-03 16:30:30 -0800275 *
276 * @param sessionId the id of the session to destroy
Felipe Lemeb96878492018-12-17 12:22:29 -0800277 * */
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800278 public void onDestroyContentCaptureSession(@NonNull ContentCaptureSessionId sessionId) {
Felipe Leme326f15a2019-02-19 09:42:24 -0800279 if (sVerbose) Log.v(TAG, "onDestroyContentCaptureSession(id=" + sessionId + ")");
Felipe Lemeb96878492018-12-17 12:22:29 -0800280 }
281
Felipe Lemebb98ed62018-12-21 09:29:27 -0800282 /**
Felipe Leme72e83d82019-02-13 14:51:17 -0800283 * Disables the Content Capture service for the given user.
284 */
285 public final void disableContentCaptureServices() {
Felipe Leme326f15a2019-02-19 09:42:24 -0800286 if (sDebug) Log.d(TAG, "disableContentCaptureServices()");
Felipe Leme72e83d82019-02-13 14:51:17 -0800287
288 final IContentCaptureServiceCallback callback = mCallback;
289 if (callback == null) {
290 Log.w(TAG, "disableContentCaptureServices(): no server callback");
291 return;
292 }
293 try {
294 callback.disableSelf();
295 } catch (RemoteException e) {
296 e.rethrowFromSystemServer();
297 }
298 }
299
300 /**
Felipe Lemebb98ed62018-12-21 09:29:27 -0800301 * Called when the Android system disconnects from the service.
302 *
303 * <p> At this point this service may no longer be an active {@link AutofillService}.
304 */
305 public void onDisconnected() {
306 Slog.i(TAG, "unbinding from " + getClass().getName());
307 }
308
Felipe Lemeb96878492018-12-17 12:22:29 -0800309 @Override
310 @CallSuper
311 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Felipe Leme326f15a2019-02-19 09:42:24 -0800312 pw.print("Debug: "); pw.print(sDebug); pw.print(" Verbose: "); pw.println(sVerbose);
Felipe Lemee127c9a2019-01-04 14:56:38 -0800313 final int size = mSessionUids.size();
Felipe Lemeb96878492018-12-17 12:22:29 -0800314 pw.print("Number sessions: "); pw.println(size);
315 if (size > 0) {
316 final String prefix = " ";
317 for (int i = 0; i < size; i++) {
Felipe Lemee127c9a2019-01-04 14:56:38 -0800318 pw.print(prefix); pw.print(mSessionUids.keyAt(i));
319 pw.print(": uid="); pw.println(mSessionUids.valueAt(i));
Felipe Lemeb96878492018-12-17 12:22:29 -0800320 }
Felipe Leme749b8892018-12-03 16:30:30 -0800321 }
322 }
323
Felipe Lemeaf4e4fd2019-01-15 08:32:42 -0800324 private void handleOnConnected(@NonNull IBinder callback) {
325 mCallback = IContentCaptureServiceCallback.Stub.asInterface(callback);
326 onConnected();
327 }
328
329 private void handleOnDisconnected() {
330 onDisconnected();
331 mCallback = null;
Felipe Lemebb98ed62018-12-21 09:29:27 -0800332 }
333
Felipe Leme749b8892018-12-03 16:30:30 -0800334 //TODO(b/111276913): consider caching the InteractionSessionId for the lifetime of the session,
335 // so we don't need to create a temporary InteractionSessionId for each event.
336
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800337 private void handleOnCreateSession(@NonNull ContentCaptureContext context,
Felipe Lemeb96878492018-12-17 12:22:29 -0800338 @NonNull String sessionId, int uid, IResultReceiver clientReceiver) {
Felipe Lemee127c9a2019-01-04 14:56:38 -0800339 mSessionUids.put(sessionId, uid);
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800340 onCreateContentCaptureSession(context, new ContentCaptureSessionId(sessionId));
Adam He328c0e32019-01-03 15:19:22 -0800341
Felipe Leme01b87492019-01-15 13:26:52 -0800342 final int clientFlags = context.getFlags();
343 int stateFlags = 0;
344 if ((clientFlags & ContentCaptureContext.FLAG_DISABLED_BY_FLAG_SECURE) != 0) {
345 stateFlags |= ContentCaptureSession.STATE_FLAG_SECURE;
Adam He328c0e32019-01-03 15:19:22 -0800346 }
Felipe Leme01b87492019-01-15 13:26:52 -0800347 if ((clientFlags & ContentCaptureContext.FLAG_DISABLED_BY_APP) != 0) {
348 stateFlags |= ContentCaptureSession.STATE_BY_APP;
Adam He6079d152019-01-10 11:37:17 -0800349 }
Felipe Leme01b87492019-01-15 13:26:52 -0800350 if (stateFlags == 0) {
351 stateFlags = ContentCaptureSession.STATE_ACTIVE;
352 } else {
353 stateFlags |= ContentCaptureSession.STATE_DISABLED;
Adam He328c0e32019-01-03 15:19:22 -0800354
Felipe Leme01b87492019-01-15 13:26:52 -0800355 }
356 setClientState(clientReceiver, stateFlags, mClientInterface.asBinder());
Felipe Leme749b8892018-12-03 16:30:30 -0800357 }
358
Felipe Lemefc24bea2018-12-18 13:19:01 -0800359 private void handleSendEvents(int uid,
360 @NonNull ParceledListSlice<ContentCaptureEvent> parceledEvents) {
361
362 // Most events belong to the same session, so we can keep a reference to the last one
363 // to avoid creating too many ContentCaptureSessionId objects
364 String lastSessionId = null;
365 ContentCaptureSessionId sessionId = null;
366
367 final List<ContentCaptureEvent> events = parceledEvents.getList();
368 for (int i = 0; i < events.size(); i++) {
369 final ContentCaptureEvent event = events.get(i);
Felipe Leme87a9dc92018-12-18 14:28:07 -0800370 if (!handleIsRightCallerFor(event, uid)) continue;
Felipe Lemefc24bea2018-12-18 13:19:01 -0800371 String sessionIdString = event.getSessionId();
372 if (!sessionIdString.equals(lastSessionId)) {
Felipe Lemefc24bea2018-12-18 13:19:01 -0800373 sessionId = new ContentCaptureSessionId(sessionIdString);
374 lastSessionId = sessionIdString;
375 }
Felipe Leme87a9dc92018-12-18 14:28:07 -0800376 switch (event.getType()) {
377 case ContentCaptureEvent.TYPE_SESSION_STARTED:
Felipe Leme4eecbe62019-02-11 17:50:17 -0800378 final ContentCaptureContext clientContext = event.getContentCaptureContext();
Felipe Leme87a9dc92018-12-18 14:28:07 -0800379 clientContext.setParentSessionId(event.getParentSessionId());
Felipe Lemee127c9a2019-01-04 14:56:38 -0800380 mSessionUids.put(sessionIdString, uid);
Felipe Leme87a9dc92018-12-18 14:28:07 -0800381 onCreateContentCaptureSession(clientContext, sessionId);
382 break;
383 case ContentCaptureEvent.TYPE_SESSION_FINISHED:
Felipe Lemee127c9a2019-01-04 14:56:38 -0800384 mSessionUids.remove(sessionIdString);
Felipe Leme87a9dc92018-12-18 14:28:07 -0800385 onDestroyContentCaptureSession(sessionId);
386 break;
387 default:
388 onContentCaptureEvent(sessionId, event);
389 }
Felipe Lemeb96878492018-12-17 12:22:29 -0800390 }
Felipe Leme749b8892018-12-03 16:30:30 -0800391 }
392
393 private void handleOnActivitySnapshot(@NonNull String sessionId,
394 @NonNull SnapshotData snapshotData) {
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800395 onActivitySnapshot(new ContentCaptureSessionId(sessionId), snapshotData);
Felipe Leme749b8892018-12-03 16:30:30 -0800396 }
397
Felipe Lemeb96878492018-12-17 12:22:29 -0800398 private void handleFinishSession(@NonNull String sessionId) {
Felipe Lemee127c9a2019-01-04 14:56:38 -0800399 mSessionUids.remove(sessionId);
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800400 onDestroyContentCaptureSession(new ContentCaptureSessionId(sessionId));
Felipe Leme749b8892018-12-03 16:30:30 -0800401 }
Felipe Lemeb96878492018-12-17 12:22:29 -0800402
Adam He3d0409b2019-01-15 14:22:04 -0800403 private void handleOnUserDataRemovalRequest(@NonNull UserDataRemovalRequest request) {
404 onUserDataRemovalRequest(request);
405 }
406
Felipe Leme141864d2019-02-27 17:01:51 -0800407 private void handleOnActivityEvent(@NonNull ActivityEvent event) {
408 onActivityEvent(event);
409 }
410
Felipe Lemeb96878492018-12-17 12:22:29 -0800411 /**
Felipe Leme87a9dc92018-12-18 14:28:07 -0800412 * Checks if the given {@code uid} owns the session associated with the event.
Felipe Lemeb96878492018-12-17 12:22:29 -0800413 */
Felipe Leme87a9dc92018-12-18 14:28:07 -0800414 private boolean handleIsRightCallerFor(@NonNull ContentCaptureEvent event, int uid) {
415 final String sessionId;
416 switch (event.getType()) {
417 case ContentCaptureEvent.TYPE_SESSION_STARTED:
418 case ContentCaptureEvent.TYPE_SESSION_FINISHED:
419 sessionId = event.getParentSessionId();
420 break;
421 default:
422 sessionId = event.getSessionId();
423 }
Felipe Lemee127c9a2019-01-04 14:56:38 -0800424 final Integer rightUid = mSessionUids.get(sessionId);
Felipe Lemeb96878492018-12-17 12:22:29 -0800425 if (rightUid == null) {
Felipe Leme326f15a2019-02-19 09:42:24 -0800426 if (sVerbose) {
Felipe Leme4eecbe62019-02-11 17:50:17 -0800427 Log.v(TAG, "handleIsRightCallerFor(" + event + "): no session for " + sessionId
Felipe Lemee127c9a2019-01-04 14:56:38 -0800428 + ": " + mSessionUids);
429 }
430 // Just ignore, as the session could have been finished already
Felipe Lemeb96878492018-12-17 12:22:29 -0800431 return false;
432 }
433 if (rightUid != uid) {
434 Log.e(TAG, "invalid call from UID " + uid + ": session " + sessionId + " belongs to "
435 + rightUid);
436 //TODO(b/111276913): log metrics as this could be a malicious app forging a sessionId
437 return false;
438 }
439 return true;
440
441 }
442
443 /**
Adam He328c0e32019-01-03 15:19:22 -0800444 * Sends the state of the {@link ContentCaptureManager} in the client app.
Felipe Lemeb96878492018-12-17 12:22:29 -0800445 *
446 * @param clientReceiver receiver in the client app.
Adam He328c0e32019-01-03 15:19:22 -0800447 * @param sessionState state of the session
Felipe Lemeb96878492018-12-17 12:22:29 -0800448 * @param binder handle to the {@code IContentCaptureDirectManager} object that resides in the
449 * service.
450 * @hide
451 */
452 public static void setClientState(@NonNull IResultReceiver clientReceiver,
Adam He328c0e32019-01-03 15:19:22 -0800453 int sessionState, @Nullable IBinder binder) {
Felipe Lemeb96878492018-12-17 12:22:29 -0800454 try {
455 final Bundle extras;
456 if (binder != null) {
457 extras = new Bundle();
Felipe Leme87a9dc92018-12-18 14:28:07 -0800458 extras.putBinder(MainContentCaptureSession.EXTRA_BINDER, binder);
Felipe Lemeb96878492018-12-17 12:22:29 -0800459 } else {
460 extras = null;
461 }
Adam He328c0e32019-01-03 15:19:22 -0800462 clientReceiver.send(sessionState, extras);
Felipe Lemeb96878492018-12-17 12:22:29 -0800463 } catch (RemoteException e) {
464 Slog.w(TAG, "Error async reporting result to client: " + e);
465 }
466 }
Felipe Leme749b8892018-12-03 16:30:30 -0800467}