blob: 4b7f32976d0f7a80c798e2273a38886aa756e430 [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
18import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage;
19
20import android.annotation.CallSuper;
21import android.annotation.NonNull;
22import android.annotation.Nullable;
23import android.annotation.SystemApi;
24import android.app.Service;
25import android.content.ComponentName;
26import android.content.Intent;
Felipe Lemeb96878492018-12-17 12:22:29 -080027import android.content.pm.ParceledListSlice;
28import android.os.Binder;
29import android.os.Bundle;
Felipe Leme749b8892018-12-03 16:30:30 -080030import android.os.Handler;
31import android.os.IBinder;
32import android.os.Looper;
33import android.os.RemoteException;
Felipe Lemebb98ed62018-12-21 09:29:27 -080034import android.service.autofill.AutofillService;
Felipe Lemeb96878492018-12-17 12:22:29 -080035import android.util.ArrayMap;
Felipe Leme749b8892018-12-03 16:30:30 -080036import android.util.Log;
Felipe Lemeb96878492018-12-17 12:22:29 -080037import android.util.Slog;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080038import android.view.contentcapture.ContentCaptureContext;
Felipe Leme749b8892018-12-03 16:30:30 -080039import android.view.contentcapture.ContentCaptureEvent;
Felipe Lemeb96878492018-12-17 12:22:29 -080040import android.view.contentcapture.ContentCaptureManager;
41import android.view.contentcapture.ContentCaptureSession;
Felipe Lemeaa5088e2018-12-10 14:53:58 -080042import android.view.contentcapture.ContentCaptureSessionId;
Felipe Lemeb96878492018-12-17 12:22:29 -080043import android.view.contentcapture.IContentCaptureDirectManager;
Felipe Leme87a9dc92018-12-18 14:28:07 -080044import android.view.contentcapture.MainContentCaptureSession;
Felipe Leme62e45b02019-01-10 10:22:00 -080045import android.view.contentcapture.UserDataRemovalRequest;
Felipe Leme749b8892018-12-03 16:30:30 -080046
Felipe Lemeb96878492018-12-17 12:22:29 -080047import com.android.internal.os.IResultReceiver;
48
49import java.io.FileDescriptor;
50import java.io.PrintWriter;
Felipe Leme749b8892018-12-03 16:30:30 -080051import java.util.List;
52import java.util.Set;
53
54/**
55 * A service used to capture the content of the screen to provide contextual data in other areas of
56 * the system such as Autofill.
57 *
58 * @hide
59 */
60@SystemApi
61public abstract class ContentCaptureService extends Service {
62
63 private static final String TAG = ContentCaptureService.class.getSimpleName();
64
Felipe Lemeaa5088e2018-12-10 14:53:58 -080065 // TODO(b/121044306): STOPSHIP use dynamic value, or change to false
Felipe Leme749b8892018-12-03 16:30:30 -080066 static final boolean DEBUG = true;
67 static final boolean VERBOSE = false;
68
69 /**
70 * The {@link Intent} that must be declared as handled by the service.
71 *
72 * <p>To be supported, the service must also require the
73 * {@link android.Manifest.permission#BIND_CONTENT_CAPTURE_SERVICE} permission so
74 * that other applications can not abuse it.
75 */
76 public static final String SERVICE_INTERFACE =
77 "android.service.contentcapture.ContentCaptureService";
78
79 private Handler mHandler;
80
Felipe Lemeb96878492018-12-17 12:22:29 -080081 /**
82 * Binder that receives calls from the system server.
83 */
84 private final IContentCaptureService mServerInterface = new IContentCaptureService.Stub() {
Felipe Leme749b8892018-12-03 16:30:30 -080085
86 @Override
Felipe Lemebb98ed62018-12-21 09:29:27 -080087 public void onConnectedStateChanged(boolean state) {
88 mHandler.sendMessage(obtainMessage(ContentCaptureService::handleOnConnectedStateChanged,
89 ContentCaptureService.this, state));
90 }
91
92 @Override
Felipe Lemeb96878492018-12-17 12:22:29 -080093 public void onSessionStarted(ContentCaptureContext context, String sessionId, int uid,
94 IResultReceiver clientReceiver) {
95 mHandler.sendMessage(obtainMessage(ContentCaptureService::handleOnCreateSession,
96 ContentCaptureService.this, context, sessionId, uid, clientReceiver));
Felipe Leme749b8892018-12-03 16:30:30 -080097 }
98
99 @Override
100 public void onActivitySnapshot(String sessionId, SnapshotData snapshotData) {
101 mHandler.sendMessage(
102 obtainMessage(ContentCaptureService::handleOnActivitySnapshot,
103 ContentCaptureService.this, sessionId, snapshotData));
104 }
Felipe Lemeb96878492018-12-17 12:22:29 -0800105
106 @Override
107 public void onSessionFinished(String sessionId) {
108 mHandler.sendMessage(obtainMessage(ContentCaptureService::handleFinishSession,
109 ContentCaptureService.this, sessionId));
110 }
Felipe Leme749b8892018-12-03 16:30:30 -0800111 };
112
Felipe Lemeb96878492018-12-17 12:22:29 -0800113 /**
114 * Binder that receives calls from the app.
115 */
116 private final IContentCaptureDirectManager mClientInterface =
117 new IContentCaptureDirectManager.Stub() {
118
119 @Override
Felipe Lemefc24bea2018-12-18 13:19:01 -0800120 public void sendEvents(@SuppressWarnings("rawtypes") ParceledListSlice events) {
Felipe Lemeb96878492018-12-17 12:22:29 -0800121 mHandler.sendMessage(obtainMessage(ContentCaptureService::handleSendEvents,
Felipe Lemefc24bea2018-12-18 13:19:01 -0800122 ContentCaptureService.this, Binder.getCallingUid(), events));
Felipe Lemeb96878492018-12-17 12:22:29 -0800123 }
124 };
125
126 /**
Felipe Lemee127c9a2019-01-04 14:56:38 -0800127 * UIDs associated with each session.
Felipe Lemeb96878492018-12-17 12:22:29 -0800128 *
129 * <p>This map is populated when an session is started, which is called by the system server
130 * and can be trusted. Then subsequent calls made by the app are verified against this map.
131 */
Felipe Lemee127c9a2019-01-04 14:56:38 -0800132 private final ArrayMap<String, Integer> mSessionUids = new ArrayMap<>();
Felipe Lemeb96878492018-12-17 12:22:29 -0800133
Felipe Leme749b8892018-12-03 16:30:30 -0800134 @CallSuper
135 @Override
136 public void onCreate() {
137 super.onCreate();
138 mHandler = new Handler(Looper.getMainLooper(), null, true);
139 }
140
141 /** @hide */
142 @Override
143 public final IBinder onBind(Intent intent) {
144 if (SERVICE_INTERFACE.equals(intent.getAction())) {
Felipe Lemeb96878492018-12-17 12:22:29 -0800145 return mServerInterface.asBinder();
Felipe Leme749b8892018-12-03 16:30:30 -0800146 }
147 Log.w(TAG, "Tried to bind to wrong intent (should be " + SERVICE_INTERFACE + ": " + intent);
148 return null;
149 }
150
151 /**
152 * Explicitly limits content capture to the given packages and activities.
153 *
154 * <p>When the whitelist is set, it overrides the values passed to
155 * {@link #setActivityContentCaptureEnabled(ComponentName, boolean)}
156 * and {@link #setPackageContentCaptureEnabled(String, boolean)}.
157 *
158 * <p>To reset the whitelist, call it passing {@code null} to both arguments.
159 *
160 * <p>Useful when the service wants to restrict content capture to a category of apps, like
161 * chat apps. For example, if the service wants to support view captures on all activities of
162 * app {@code ChatApp1} and just activities {@code act1} and {@code act2} of {@code ChatApp2},
163 * it would call: {@code setContentCaptureWhitelist(Arrays.asList("ChatApp1"),
164 * Arrays.asList(new ComponentName("ChatApp2", "act1"),
165 * new ComponentName("ChatApp2", "act2")));}
166 */
167 public final void setContentCaptureWhitelist(@Nullable List<String> packages,
168 @Nullable List<ComponentName> activities) {
169 //TODO(b/111276913): implement
170 }
171
172 /**
173 * Defines whether content capture should be enabled for activities with such
174 * {@link android.content.ComponentName}.
175 *
176 * <p>Useful to blacklist a particular activity.
177 */
178 public final void setActivityContentCaptureEnabled(@NonNull ComponentName activity,
179 boolean enabled) {
180 //TODO(b/111276913): implement
181 }
182
183 /**
184 * Defines whether content capture should be enabled for activities of the app with such
185 * {@code packageName}.
186 *
187 * <p>Useful to blacklist any activity from a particular app.
188 */
189 public final void setPackageContentCaptureEnabled(@NonNull String packageName,
190 boolean enabled) {
191 //TODO(b/111276913): implement
192 }
193
194 /**
195 * Gets the activities where content capture was disabled by
196 * {@link #setActivityContentCaptureEnabled(ComponentName, boolean)}.
197 */
198 @NonNull
199 public final Set<ComponentName> getContentCaptureDisabledActivities() {
200 //TODO(b/111276913): implement
201 return null;
202 }
203
204 /**
205 * Gets the apps where content capture was disabled by
206 * {@link #setPackageContentCaptureEnabled(String, boolean)}.
207 */
208 @NonNull
209 public final Set<String> getContentCaptureDisabledPackages() {
210 //TODO(b/111276913): implement
211 return null;
212 }
213
214 /**
Felipe Lemebb98ed62018-12-21 09:29:27 -0800215 * Called when the Android system connects to service.
216 *
217 * <p>You should generally do initialization here rather than in {@link #onCreate}.
218 */
219 public void onConnected() {
220 Slog.i(TAG, "bound to " + getClass().getName());
221 }
222
223 /**
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800224 * Creates a new content capture session.
Felipe Leme749b8892018-12-03 16:30:30 -0800225 *
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800226 * @param context content capture context
Felipe Leme749b8892018-12-03 16:30:30 -0800227 * @param sessionId the session's Id
228 */
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800229 public void onCreateContentCaptureSession(@NonNull ContentCaptureContext context,
230 @NonNull ContentCaptureSessionId sessionId) {
Felipe Leme749b8892018-12-03 16:30:30 -0800231 if (VERBOSE) {
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800232 Log.v(TAG, "onCreateContentCaptureSession(id=" + sessionId + ", ctx=" + context + ")");
Felipe Leme749b8892018-12-03 16:30:30 -0800233 }
234 }
235
236 /**
Felipe Leme749b8892018-12-03 16:30:30 -0800237 *
Felipe Lemefc24bea2018-12-18 13:19:01 -0800238 * @deprecated use {@link #onContentCaptureEvent(ContentCaptureSessionId, ContentCaptureEvent)}
239 * instead.
Felipe Leme749b8892018-12-03 16:30:30 -0800240 */
Felipe Lemefc24bea2018-12-18 13:19:01 -0800241 @Deprecated
Felipe Lemeb96878492018-12-17 12:22:29 -0800242 public void onContentCaptureEventsRequest(@NonNull ContentCaptureSessionId sessionId,
243 @NonNull ContentCaptureEventsRequest request) {
244 if (VERBOSE) Log.v(TAG, "onContentCaptureEventsRequest(id=" + sessionId + ")");
245 }
Felipe Leme749b8892018-12-03 16:30:30 -0800246
247 /**
Felipe Lemefc24bea2018-12-18 13:19:01 -0800248 * Notifies the service of {@link ContentCaptureEvent events} associated with a content capture
249 * session.
250 *
251 * @param sessionId the session's Id
252 * @param event the event
253 */
254 public void onContentCaptureEvent(@NonNull ContentCaptureSessionId sessionId,
255 @NonNull ContentCaptureEvent event) {
256 if (VERBOSE) Log.v(TAG, "onContentCaptureEventsRequest(id=" + sessionId + ")");
257 onContentCaptureEventsRequest(sessionId, new ContentCaptureEventsRequest(event));
258 }
Felipe Leme62e45b02019-01-10 10:22:00 -0800259
260 /**
261 * Notifies the service that the app requested to remove data associated with the user.
262 *
263 * @param request the user data requested to be removed
264 */
265 public void onUserDataRemovalRequest(@NonNull UserDataRemovalRequest request) {
266 if (VERBOSE) Log.v(TAG, "onUserDataRemovalRequest()");
267 }
268
Felipe Lemefc24bea2018-12-18 13:19:01 -0800269 /**
Felipe Leme749b8892018-12-03 16:30:30 -0800270 * Notifies the service of {@link SnapshotData snapshot data} associated with a session.
271 *
272 * @param sessionId the session's Id
273 * @param snapshotData the data
274 */
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800275 public void onActivitySnapshot(@NonNull ContentCaptureSessionId sessionId,
Felipe Leme749b8892018-12-03 16:30:30 -0800276 @NonNull SnapshotData snapshotData) {}
277
278 /**
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800279 * Destroys the content capture session.
Felipe Leme749b8892018-12-03 16:30:30 -0800280 *
281 * @param sessionId the id of the session to destroy
Felipe Lemeb96878492018-12-17 12:22:29 -0800282 * */
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800283 public void onDestroyContentCaptureSession(@NonNull ContentCaptureSessionId sessionId) {
Felipe Lemeb96878492018-12-17 12:22:29 -0800284 if (VERBOSE) Log.v(TAG, "onDestroyContentCaptureSession(id=" + sessionId + ")");
285 }
286
Felipe Lemebb98ed62018-12-21 09:29:27 -0800287 /**
288 * Called when the Android system disconnects from the service.
289 *
290 * <p> At this point this service may no longer be an active {@link AutofillService}.
291 */
292 public void onDisconnected() {
293 Slog.i(TAG, "unbinding from " + getClass().getName());
294 }
295
Felipe Lemeb96878492018-12-17 12:22:29 -0800296 @Override
297 @CallSuper
298 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Felipe Lemee127c9a2019-01-04 14:56:38 -0800299 final int size = mSessionUids.size();
Felipe Lemeb96878492018-12-17 12:22:29 -0800300 pw.print("Number sessions: "); pw.println(size);
301 if (size > 0) {
302 final String prefix = " ";
303 for (int i = 0; i < size; i++) {
Felipe Lemee127c9a2019-01-04 14:56:38 -0800304 pw.print(prefix); pw.print(mSessionUids.keyAt(i));
305 pw.print(": uid="); pw.println(mSessionUids.valueAt(i));
Felipe Lemeb96878492018-12-17 12:22:29 -0800306 }
Felipe Leme749b8892018-12-03 16:30:30 -0800307 }
308 }
309
Felipe Lemebb98ed62018-12-21 09:29:27 -0800310 private void handleOnConnectedStateChanged(boolean state) {
311 if (state) {
312 onConnected();
313 } else {
314 onDisconnected();
315 }
316 }
317
Felipe Leme749b8892018-12-03 16:30:30 -0800318 //TODO(b/111276913): consider caching the InteractionSessionId for the lifetime of the session,
319 // so we don't need to create a temporary InteractionSessionId for each event.
320
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800321 private void handleOnCreateSession(@NonNull ContentCaptureContext context,
Felipe Lemeb96878492018-12-17 12:22:29 -0800322 @NonNull String sessionId, int uid, IResultReceiver clientReceiver) {
Felipe Lemee127c9a2019-01-04 14:56:38 -0800323 mSessionUids.put(sessionId, uid);
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800324 onCreateContentCaptureSession(context, new ContentCaptureSessionId(sessionId));
Felipe Lemeb96878492018-12-17 12:22:29 -0800325 setClientState(clientReceiver, ContentCaptureSession.STATE_ACTIVE,
326 mClientInterface.asBinder());
Felipe Leme749b8892018-12-03 16:30:30 -0800327 }
328
Felipe Lemefc24bea2018-12-18 13:19:01 -0800329 private void handleSendEvents(int uid,
330 @NonNull ParceledListSlice<ContentCaptureEvent> parceledEvents) {
331
332 // Most events belong to the same session, so we can keep a reference to the last one
333 // to avoid creating too many ContentCaptureSessionId objects
334 String lastSessionId = null;
335 ContentCaptureSessionId sessionId = null;
336
337 final List<ContentCaptureEvent> events = parceledEvents.getList();
338 for (int i = 0; i < events.size(); i++) {
339 final ContentCaptureEvent event = events.get(i);
Felipe Leme87a9dc92018-12-18 14:28:07 -0800340 if (!handleIsRightCallerFor(event, uid)) continue;
Felipe Lemefc24bea2018-12-18 13:19:01 -0800341 String sessionIdString = event.getSessionId();
342 if (!sessionIdString.equals(lastSessionId)) {
Felipe Lemefc24bea2018-12-18 13:19:01 -0800343 sessionId = new ContentCaptureSessionId(sessionIdString);
344 lastSessionId = sessionIdString;
345 }
Felipe Leme87a9dc92018-12-18 14:28:07 -0800346 switch (event.getType()) {
347 case ContentCaptureEvent.TYPE_SESSION_STARTED:
348 final ContentCaptureContext clientContext = event.getClientContext();
349 clientContext.setParentSessionId(event.getParentSessionId());
Felipe Lemee127c9a2019-01-04 14:56:38 -0800350 mSessionUids.put(sessionIdString, uid);
Felipe Leme87a9dc92018-12-18 14:28:07 -0800351 onCreateContentCaptureSession(clientContext, sessionId);
352 break;
353 case ContentCaptureEvent.TYPE_SESSION_FINISHED:
Felipe Lemee127c9a2019-01-04 14:56:38 -0800354 mSessionUids.remove(sessionIdString);
Felipe Leme87a9dc92018-12-18 14:28:07 -0800355 onDestroyContentCaptureSession(sessionId);
356 break;
357 default:
358 onContentCaptureEvent(sessionId, event);
359 }
Felipe Lemeb96878492018-12-17 12:22:29 -0800360 }
Felipe Leme749b8892018-12-03 16:30:30 -0800361 }
362
363 private void handleOnActivitySnapshot(@NonNull String sessionId,
364 @NonNull SnapshotData snapshotData) {
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800365 onActivitySnapshot(new ContentCaptureSessionId(sessionId), snapshotData);
Felipe Leme749b8892018-12-03 16:30:30 -0800366 }
367
Felipe Lemeb96878492018-12-17 12:22:29 -0800368 private void handleFinishSession(@NonNull String sessionId) {
Felipe Lemee127c9a2019-01-04 14:56:38 -0800369 mSessionUids.remove(sessionId);
Felipe Lemeaa5088e2018-12-10 14:53:58 -0800370 onDestroyContentCaptureSession(new ContentCaptureSessionId(sessionId));
Felipe Leme749b8892018-12-03 16:30:30 -0800371 }
Felipe Lemeb96878492018-12-17 12:22:29 -0800372
373 /**
Felipe Leme87a9dc92018-12-18 14:28:07 -0800374 * Checks if the given {@code uid} owns the session associated with the event.
Felipe Lemeb96878492018-12-17 12:22:29 -0800375 */
Felipe Leme87a9dc92018-12-18 14:28:07 -0800376 private boolean handleIsRightCallerFor(@NonNull ContentCaptureEvent event, int uid) {
377 final String sessionId;
378 switch (event.getType()) {
379 case ContentCaptureEvent.TYPE_SESSION_STARTED:
380 case ContentCaptureEvent.TYPE_SESSION_FINISHED:
381 sessionId = event.getParentSessionId();
382 break;
383 default:
384 sessionId = event.getSessionId();
385 }
Felipe Lemee127c9a2019-01-04 14:56:38 -0800386 final Integer rightUid = mSessionUids.get(sessionId);
Felipe Lemeb96878492018-12-17 12:22:29 -0800387 if (rightUid == null) {
Felipe Lemee127c9a2019-01-04 14:56:38 -0800388 if (DEBUG) {
389 Log.d(TAG, "handleIsRightCallerFor(" + event + "): no session for " + sessionId
390 + ": " + mSessionUids);
391 }
392 // Just ignore, as the session could have been finished already
Felipe Lemeb96878492018-12-17 12:22:29 -0800393 return false;
394 }
395 if (rightUid != uid) {
396 Log.e(TAG, "invalid call from UID " + uid + ": session " + sessionId + " belongs to "
397 + rightUid);
398 //TODO(b/111276913): log metrics as this could be a malicious app forging a sessionId
399 return false;
400 }
401 return true;
402
403 }
404
405 /**
406 * Sends the state of the {@link ContentCaptureManager} in the cleint app.
407 *
408 * @param clientReceiver receiver in the client app.
409 * @param binder handle to the {@code IContentCaptureDirectManager} object that resides in the
410 * service.
411 * @hide
412 */
413 public static void setClientState(@NonNull IResultReceiver clientReceiver,
414 int sessionStatus, @Nullable IBinder binder) {
415 try {
416 final Bundle extras;
417 if (binder != null) {
418 extras = new Bundle();
Felipe Leme87a9dc92018-12-18 14:28:07 -0800419 extras.putBinder(MainContentCaptureSession.EXTRA_BINDER, binder);
Felipe Lemeb96878492018-12-17 12:22:29 -0800420 } else {
421 extras = null;
422 }
423 clientReceiver.send(sessionStatus, extras);
424 } catch (RemoteException e) {
425 Slog.w(TAG, "Error async reporting result to client: " + e);
426 }
427 }
Felipe Leme749b8892018-12-03 16:30:30 -0800428}