Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package android.service.contentcapture; |
| 17 | |
| 18 | import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage; |
| 19 | |
| 20 | import android.annotation.CallSuper; |
| 21 | import android.annotation.NonNull; |
| 22 | import android.annotation.Nullable; |
| 23 | import android.annotation.SystemApi; |
| 24 | import android.app.Service; |
| 25 | import android.content.ComponentName; |
| 26 | import android.content.Intent; |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 27 | import android.content.pm.ParceledListSlice; |
| 28 | import android.os.Binder; |
| 29 | import android.os.Bundle; |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 30 | import android.os.Handler; |
| 31 | import android.os.IBinder; |
| 32 | import android.os.Looper; |
| 33 | import android.os.RemoteException; |
Felipe Leme | bb98ed6 | 2018-12-21 09:29:27 -0800 | [diff] [blame] | 34 | import android.service.autofill.AutofillService; |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 35 | import android.util.ArrayMap; |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 36 | import android.util.Log; |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 37 | import android.util.Slog; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 38 | import android.view.contentcapture.ContentCaptureContext; |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 39 | import android.view.contentcapture.ContentCaptureEvent; |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 40 | import android.view.contentcapture.ContentCaptureManager; |
| 41 | import android.view.contentcapture.ContentCaptureSession; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 42 | import android.view.contentcapture.ContentCaptureSessionId; |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 43 | import android.view.contentcapture.IContentCaptureDirectManager; |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 44 | import android.view.contentcapture.MainContentCaptureSession; |
Felipe Leme | 62e45b0 | 2019-01-10 10:22:00 -0800 | [diff] [blame^] | 45 | import android.view.contentcapture.UserDataRemovalRequest; |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 46 | |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 47 | import com.android.internal.os.IResultReceiver; |
| 48 | |
| 49 | import java.io.FileDescriptor; |
| 50 | import java.io.PrintWriter; |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 51 | import java.util.List; |
| 52 | import 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 |
| 61 | public abstract class ContentCaptureService extends Service { |
| 62 | |
| 63 | private static final String TAG = ContentCaptureService.class.getSimpleName(); |
| 64 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 65 | // TODO(b/121044306): STOPSHIP use dynamic value, or change to false |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 66 | 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 Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 81 | /** |
| 82 | * Binder that receives calls from the system server. |
| 83 | */ |
| 84 | private final IContentCaptureService mServerInterface = new IContentCaptureService.Stub() { |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 85 | |
| 86 | @Override |
Felipe Leme | bb98ed6 | 2018-12-21 09:29:27 -0800 | [diff] [blame] | 87 | public void onConnectedStateChanged(boolean state) { |
| 88 | mHandler.sendMessage(obtainMessage(ContentCaptureService::handleOnConnectedStateChanged, |
| 89 | ContentCaptureService.this, state)); |
| 90 | } |
| 91 | |
| 92 | @Override |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 93 | 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 Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 97 | } |
| 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 Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 105 | |
| 106 | @Override |
| 107 | public void onSessionFinished(String sessionId) { |
| 108 | mHandler.sendMessage(obtainMessage(ContentCaptureService::handleFinishSession, |
| 109 | ContentCaptureService.this, sessionId)); |
| 110 | } |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 111 | }; |
| 112 | |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 113 | /** |
| 114 | * Binder that receives calls from the app. |
| 115 | */ |
| 116 | private final IContentCaptureDirectManager mClientInterface = |
| 117 | new IContentCaptureDirectManager.Stub() { |
| 118 | |
| 119 | @Override |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 120 | public void sendEvents(@SuppressWarnings("rawtypes") ParceledListSlice events) { |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 121 | mHandler.sendMessage(obtainMessage(ContentCaptureService::handleSendEvents, |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 122 | ContentCaptureService.this, Binder.getCallingUid(), events)); |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 123 | } |
| 124 | }; |
| 125 | |
| 126 | /** |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 127 | * UIDs associated with each session. |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 128 | * |
| 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 Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 132 | private final ArrayMap<String, Integer> mSessionUids = new ArrayMap<>(); |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 133 | |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 134 | @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 Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 145 | return mServerInterface.asBinder(); |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 146 | } |
| 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 Leme | bb98ed6 | 2018-12-21 09:29:27 -0800 | [diff] [blame] | 215 | * 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 Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 224 | * Creates a new content capture session. |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 225 | * |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 226 | * @param context content capture context |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 227 | * @param sessionId the session's Id |
| 228 | */ |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 229 | public void onCreateContentCaptureSession(@NonNull ContentCaptureContext context, |
| 230 | @NonNull ContentCaptureSessionId sessionId) { |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 231 | if (VERBOSE) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 232 | Log.v(TAG, "onCreateContentCaptureSession(id=" + sessionId + ", ctx=" + context + ")"); |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
| 236 | /** |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 237 | * |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 238 | * @deprecated use {@link #onContentCaptureEvent(ContentCaptureSessionId, ContentCaptureEvent)} |
| 239 | * instead. |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 240 | */ |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 241 | @Deprecated |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 242 | public void onContentCaptureEventsRequest(@NonNull ContentCaptureSessionId sessionId, |
| 243 | @NonNull ContentCaptureEventsRequest request) { |
| 244 | if (VERBOSE) Log.v(TAG, "onContentCaptureEventsRequest(id=" + sessionId + ")"); |
| 245 | } |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 246 | |
| 247 | /** |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 248 | * 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 Leme | 62e45b0 | 2019-01-10 10:22:00 -0800 | [diff] [blame^] | 259 | |
| 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 Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 269 | /** |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 270 | * 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 Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 275 | public void onActivitySnapshot(@NonNull ContentCaptureSessionId sessionId, |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 276 | @NonNull SnapshotData snapshotData) {} |
| 277 | |
| 278 | /** |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 279 | * Destroys the content capture session. |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 280 | * |
| 281 | * @param sessionId the id of the session to destroy |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 282 | * */ |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 283 | public void onDestroyContentCaptureSession(@NonNull ContentCaptureSessionId sessionId) { |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 284 | if (VERBOSE) Log.v(TAG, "onDestroyContentCaptureSession(id=" + sessionId + ")"); |
| 285 | } |
| 286 | |
Felipe Leme | bb98ed6 | 2018-12-21 09:29:27 -0800 | [diff] [blame] | 287 | /** |
| 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 Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 296 | @Override |
| 297 | @CallSuper |
| 298 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 299 | final int size = mSessionUids.size(); |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 300 | pw.print("Number sessions: "); pw.println(size); |
| 301 | if (size > 0) { |
| 302 | final String prefix = " "; |
| 303 | for (int i = 0; i < size; i++) { |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 304 | pw.print(prefix); pw.print(mSessionUids.keyAt(i)); |
| 305 | pw.print(": uid="); pw.println(mSessionUids.valueAt(i)); |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 306 | } |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
Felipe Leme | bb98ed6 | 2018-12-21 09:29:27 -0800 | [diff] [blame] | 310 | private void handleOnConnectedStateChanged(boolean state) { |
| 311 | if (state) { |
| 312 | onConnected(); |
| 313 | } else { |
| 314 | onDisconnected(); |
| 315 | } |
| 316 | } |
| 317 | |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 318 | //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 Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 321 | private void handleOnCreateSession(@NonNull ContentCaptureContext context, |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 322 | @NonNull String sessionId, int uid, IResultReceiver clientReceiver) { |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 323 | mSessionUids.put(sessionId, uid); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 324 | onCreateContentCaptureSession(context, new ContentCaptureSessionId(sessionId)); |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 325 | setClientState(clientReceiver, ContentCaptureSession.STATE_ACTIVE, |
| 326 | mClientInterface.asBinder()); |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 329 | 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 Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 340 | if (!handleIsRightCallerFor(event, uid)) continue; |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 341 | String sessionIdString = event.getSessionId(); |
| 342 | if (!sessionIdString.equals(lastSessionId)) { |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 343 | sessionId = new ContentCaptureSessionId(sessionIdString); |
| 344 | lastSessionId = sessionIdString; |
| 345 | } |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 346 | switch (event.getType()) { |
| 347 | case ContentCaptureEvent.TYPE_SESSION_STARTED: |
| 348 | final ContentCaptureContext clientContext = event.getClientContext(); |
| 349 | clientContext.setParentSessionId(event.getParentSessionId()); |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 350 | mSessionUids.put(sessionIdString, uid); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 351 | onCreateContentCaptureSession(clientContext, sessionId); |
| 352 | break; |
| 353 | case ContentCaptureEvent.TYPE_SESSION_FINISHED: |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 354 | mSessionUids.remove(sessionIdString); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 355 | onDestroyContentCaptureSession(sessionId); |
| 356 | break; |
| 357 | default: |
| 358 | onContentCaptureEvent(sessionId, event); |
| 359 | } |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 360 | } |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | private void handleOnActivitySnapshot(@NonNull String sessionId, |
| 364 | @NonNull SnapshotData snapshotData) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 365 | onActivitySnapshot(new ContentCaptureSessionId(sessionId), snapshotData); |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 368 | private void handleFinishSession(@NonNull String sessionId) { |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 369 | mSessionUids.remove(sessionId); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 370 | onDestroyContentCaptureSession(new ContentCaptureSessionId(sessionId)); |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 371 | } |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 372 | |
| 373 | /** |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 374 | * Checks if the given {@code uid} owns the session associated with the event. |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 375 | */ |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 376 | 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 Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 386 | final Integer rightUid = mSessionUids.get(sessionId); |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 387 | if (rightUid == null) { |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 388 | 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 Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 393 | 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 Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 419 | extras.putBinder(MainContentCaptureSession.EXTRA_BINDER, binder); |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 420 | } 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 Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 428 | } |