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 | |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 18 | import static android.view.contentcapture.ContentCaptureHelper.sDebug; |
| 19 | import static android.view.contentcapture.ContentCaptureHelper.sVerbose; |
Felipe Leme | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame^] | 20 | import static android.view.contentcapture.ContentCaptureSession.NO_SESSION_ID; |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 21 | |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 22 | import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage; |
| 23 | |
| 24 | import android.annotation.CallSuper; |
| 25 | import android.annotation.NonNull; |
| 26 | import android.annotation.Nullable; |
| 27 | import android.annotation.SystemApi; |
Felipe Leme | 19652c0 | 2019-02-04 13:01:29 -0800 | [diff] [blame] | 28 | import android.annotation.TestApi; |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 29 | import android.app.Service; |
| 30 | import android.content.ComponentName; |
| 31 | import android.content.Intent; |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 32 | import android.content.pm.ParceledListSlice; |
| 33 | import android.os.Binder; |
| 34 | import android.os.Bundle; |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 35 | import android.os.Handler; |
| 36 | import android.os.IBinder; |
| 37 | import android.os.Looper; |
| 38 | import android.os.RemoteException; |
Felipe Leme | bb98ed6 | 2018-12-21 09:29:27 -0800 | [diff] [blame] | 39 | import android.service.autofill.AutofillService; |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 40 | import android.util.Log; |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 41 | import android.util.Slog; |
Felipe Leme | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame^] | 42 | import android.util.SparseIntArray; |
Felipe Leme | 790be04 | 2019-03-25 09:52:19 -0700 | [diff] [blame] | 43 | import android.view.contentcapture.ContentCaptureCondition; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 44 | import android.view.contentcapture.ContentCaptureContext; |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 45 | import android.view.contentcapture.ContentCaptureEvent; |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 46 | import android.view.contentcapture.ContentCaptureManager; |
| 47 | import android.view.contentcapture.ContentCaptureSession; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 48 | import android.view.contentcapture.ContentCaptureSessionId; |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 49 | import android.view.contentcapture.IContentCaptureDirectManager; |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 50 | import android.view.contentcapture.MainContentCaptureSession; |
Felipe Leme | 62e45b0 | 2019-01-10 10:22:00 -0800 | [diff] [blame] | 51 | import android.view.contentcapture.UserDataRemovalRequest; |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 52 | |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 53 | import com.android.internal.os.IResultReceiver; |
| 54 | |
| 55 | import java.io.FileDescriptor; |
| 56 | import java.io.PrintWriter; |
Felipe Leme | 7a3c9f5 | 2019-02-13 16:32:49 -0800 | [diff] [blame] | 57 | import java.util.ArrayList; |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 58 | import java.util.List; |
Felipe Leme | 7a3c9f5 | 2019-02-13 16:32:49 -0800 | [diff] [blame] | 59 | import java.util.Set; |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * A service used to capture the content of the screen to provide contextual data in other areas of |
| 63 | * the system such as Autofill. |
| 64 | * |
| 65 | * @hide |
| 66 | */ |
| 67 | @SystemApi |
Felipe Leme | 19652c0 | 2019-02-04 13:01:29 -0800 | [diff] [blame] | 68 | @TestApi |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 69 | public abstract class ContentCaptureService extends Service { |
| 70 | |
| 71 | private static final String TAG = ContentCaptureService.class.getSimpleName(); |
| 72 | |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 73 | /** |
| 74 | * The {@link Intent} that must be declared as handled by the service. |
| 75 | * |
| 76 | * <p>To be supported, the service must also require the |
| 77 | * {@link android.Manifest.permission#BIND_CONTENT_CAPTURE_SERVICE} permission so |
| 78 | * that other applications can not abuse it. |
| 79 | */ |
| 80 | public static final String SERVICE_INTERFACE = |
| 81 | "android.service.contentcapture.ContentCaptureService"; |
| 82 | |
Felipe Leme | a5d5e2d | 2019-03-19 16:54:55 -0700 | [diff] [blame] | 83 | /** |
| 84 | * Name under which a ContentCaptureService component publishes information about itself. |
| 85 | * |
| 86 | * <p>This meta-data should reference an XML resource containing a |
| 87 | * <code><{@link |
| 88 | * android.R.styleable#ContentCaptureService content-capture-service}></code> tag. |
| 89 | * |
| 90 | * <p>This is a a sample XML file configuring a ContentCaptureService: |
| 91 | * <pre> <content-capture-service |
| 92 | * android:settingsActivity="foo.bar.SettingsActivity" |
| 93 | * . . . |
| 94 | * /></pre> |
| 95 | */ |
| 96 | public static final String SERVICE_META_DATA = "android.content_capture"; |
| 97 | |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 98 | private Handler mHandler; |
Felipe Leme | af4e4fd | 2019-01-15 08:32:42 -0800 | [diff] [blame] | 99 | private IContentCaptureServiceCallback mCallback; |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 100 | |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 101 | /** |
| 102 | * Binder that receives calls from the system server. |
| 103 | */ |
| 104 | private final IContentCaptureService mServerInterface = new IContentCaptureService.Stub() { |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 105 | |
| 106 | @Override |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 107 | public void onConnected(IBinder callback, boolean verbose, boolean debug) { |
| 108 | sVerbose = verbose; |
| 109 | sDebug = debug; |
Felipe Leme | af4e4fd | 2019-01-15 08:32:42 -0800 | [diff] [blame] | 110 | mHandler.sendMessage(obtainMessage(ContentCaptureService::handleOnConnected, |
| 111 | ContentCaptureService.this, callback)); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public void onDisconnected() { |
| 116 | mHandler.sendMessage(obtainMessage(ContentCaptureService::handleOnDisconnected, |
| 117 | ContentCaptureService.this)); |
Felipe Leme | bb98ed6 | 2018-12-21 09:29:27 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Override |
Felipe Leme | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame^] | 121 | public void onSessionStarted(ContentCaptureContext context, int sessionId, int uid, |
Felipe Leme | 518fb62 | 2019-03-08 15:35:04 -0800 | [diff] [blame] | 122 | IResultReceiver clientReceiver, int initialState) { |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 123 | mHandler.sendMessage(obtainMessage(ContentCaptureService::handleOnCreateSession, |
Felipe Leme | 518fb62 | 2019-03-08 15:35:04 -0800 | [diff] [blame] | 124 | ContentCaptureService.this, context, sessionId, uid, clientReceiver, |
| 125 | initialState)); |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
Felipe Leme | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame^] | 129 | public void onActivitySnapshot(int sessionId, SnapshotData snapshotData) { |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 130 | mHandler.sendMessage( |
| 131 | obtainMessage(ContentCaptureService::handleOnActivitySnapshot, |
| 132 | ContentCaptureService.this, sessionId, snapshotData)); |
| 133 | } |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 134 | |
| 135 | @Override |
Felipe Leme | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame^] | 136 | public void onSessionFinished(int sessionId) { |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 137 | mHandler.sendMessage(obtainMessage(ContentCaptureService::handleFinishSession, |
| 138 | ContentCaptureService.this, sessionId)); |
| 139 | } |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 140 | |
| 141 | @Override |
| 142 | public void onUserDataRemovalRequest(UserDataRemovalRequest request) { |
| 143 | mHandler.sendMessage( |
| 144 | obtainMessage(ContentCaptureService::handleOnUserDataRemovalRequest, |
| 145 | ContentCaptureService.this, request)); |
| 146 | } |
Felipe Leme | 141864d | 2019-02-27 17:01:51 -0800 | [diff] [blame] | 147 | |
| 148 | @Override |
| 149 | public void onActivityEvent(ActivityEvent event) { |
| 150 | mHandler.sendMessage(obtainMessage(ContentCaptureService::handleOnActivityEvent, |
| 151 | ContentCaptureService.this, event)); |
| 152 | |
| 153 | } |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 154 | }; |
| 155 | |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 156 | /** |
| 157 | * Binder that receives calls from the app. |
| 158 | */ |
| 159 | private final IContentCaptureDirectManager mClientInterface = |
| 160 | new IContentCaptureDirectManager.Stub() { |
| 161 | |
| 162 | @Override |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 163 | public void sendEvents(@SuppressWarnings("rawtypes") ParceledListSlice events) { |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 164 | mHandler.sendMessage(obtainMessage(ContentCaptureService::handleSendEvents, |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 165 | ContentCaptureService.this, Binder.getCallingUid(), events)); |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 166 | } |
| 167 | }; |
| 168 | |
| 169 | /** |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 170 | * UIDs associated with each session. |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 171 | * |
| 172 | * <p>This map is populated when an session is started, which is called by the system server |
| 173 | * and can be trusted. Then subsequent calls made by the app are verified against this map. |
| 174 | */ |
Felipe Leme | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame^] | 175 | private final SparseIntArray mSessionUids = new SparseIntArray(); |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 176 | |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 177 | @CallSuper |
| 178 | @Override |
| 179 | public void onCreate() { |
| 180 | super.onCreate(); |
| 181 | mHandler = new Handler(Looper.getMainLooper(), null, true); |
| 182 | } |
| 183 | |
| 184 | /** @hide */ |
| 185 | @Override |
| 186 | public final IBinder onBind(Intent intent) { |
| 187 | if (SERVICE_INTERFACE.equals(intent.getAction())) { |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 188 | return mServerInterface.asBinder(); |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 189 | } |
| 190 | Log.w(TAG, "Tried to bind to wrong intent (should be " + SERVICE_INTERFACE + ": " + intent); |
| 191 | return null; |
| 192 | } |
| 193 | |
| 194 | /** |
Felipe Leme | 7a3c9f5 | 2019-02-13 16:32:49 -0800 | [diff] [blame] | 195 | * Explicitly limits content capture to the given packages and activities. |
| 196 | * |
| 197 | * <p>To reset the whitelist, call it passing {@code null} to both arguments. |
| 198 | * |
| 199 | * <p>Useful when the service wants to restrict content capture to a category of apps, like |
| 200 | * chat apps. For example, if the service wants to support view captures on all activities of |
| 201 | * app {@code ChatApp1} and just activities {@code act1} and {@code act2} of {@code ChatApp2}, |
| 202 | * it would call: {@code setContentCaptureWhitelist(Sets.newArraySet("ChatApp1"), |
| 203 | * Sets.newArraySet(new ComponentName("ChatApp2", "act1"), |
| 204 | * new ComponentName("ChatApp2", "act2")));} |
| 205 | */ |
| 206 | public final void setContentCaptureWhitelist(@Nullable Set<String> packages, |
| 207 | @Nullable Set<ComponentName> activities) { |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 208 | final IContentCaptureServiceCallback callback = mCallback; |
| 209 | if (callback == null) { |
| 210 | Log.w(TAG, "setContentCaptureWhitelist(): no server callback"); |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | try { |
| 215 | callback.setContentCaptureWhitelist(toList(packages), toList(activities)); |
| 216 | } catch (RemoteException e) { |
| 217 | e.rethrowFromSystemServer(); |
| 218 | } |
Felipe Leme | 7a3c9f5 | 2019-02-13 16:32:49 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Felipe Leme | 790be04 | 2019-03-25 09:52:19 -0700 | [diff] [blame] | 221 | /** |
| 222 | * Explicitly sets the conditions for which content capture should be available by an app. |
| 223 | * |
| 224 | * <p>Typically used to restrict content capture to a few websites on browser apps. Example: |
| 225 | * |
| 226 | * <code> |
| 227 | * ArraySet<ContentCaptureCondition> conditions = new ArraySet<>(1); |
| 228 | * conditions.add(new ContentCaptureCondition(new LocusId("^https://.*\\.example\\.com$"), |
| 229 | * ContentCaptureCondition.FLAG_IS_REGEX)); |
| 230 | * service.setContentCaptureConditions("com.example.browser_app", conditions); |
| 231 | * |
| 232 | * </code> |
| 233 | * |
| 234 | * <p>NOTE: </p> this method doesn't automatically disable content capture for the given |
| 235 | * conditions; it's up to the {@code packageName} implementation to call |
| 236 | * {@link ContentCaptureManager#getContentCaptureConditions()} and disable it accordingly. |
| 237 | * |
| 238 | * @param packageName name of the packages where the restrictions are set. |
| 239 | * @param conditions list of conditions, or {@code null} to reset the conditions for the |
| 240 | * package. |
| 241 | */ |
| 242 | public final void setContentCaptureConditions(@NonNull String packageName, |
| 243 | @Nullable Set<ContentCaptureCondition> conditions) { |
| 244 | // TODO(b/129267994): implement |
| 245 | } |
| 246 | |
Felipe Leme | 7a3c9f5 | 2019-02-13 16:32:49 -0800 | [diff] [blame] | 247 | private <T> ArrayList<T> toList(@Nullable Set<T> set) { |
| 248 | return set == null ? null : new ArrayList<T>(set); |
| 249 | } |
| 250 | |
| 251 | /** |
Felipe Leme | bb98ed6 | 2018-12-21 09:29:27 -0800 | [diff] [blame] | 252 | * Called when the Android system connects to service. |
| 253 | * |
| 254 | * <p>You should generally do initialization here rather than in {@link #onCreate}. |
| 255 | */ |
| 256 | public void onConnected() { |
| 257 | Slog.i(TAG, "bound to " + getClass().getName()); |
| 258 | } |
| 259 | |
| 260 | /** |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 261 | * Creates a new content capture session. |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 262 | * |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 263 | * @param context content capture context |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 264 | * @param sessionId the session's Id |
| 265 | */ |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 266 | public void onCreateContentCaptureSession(@NonNull ContentCaptureContext context, |
| 267 | @NonNull ContentCaptureSessionId sessionId) { |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 268 | if (sVerbose) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 269 | Log.v(TAG, "onCreateContentCaptureSession(id=" + sessionId + ", ctx=" + context + ")"); |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
| 273 | /** |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 274 | * Notifies the service of {@link ContentCaptureEvent events} associated with a content capture |
| 275 | * session. |
| 276 | * |
| 277 | * @param sessionId the session's Id |
| 278 | * @param event the event |
| 279 | */ |
| 280 | public void onContentCaptureEvent(@NonNull ContentCaptureSessionId sessionId, |
| 281 | @NonNull ContentCaptureEvent event) { |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 282 | if (sVerbose) Log.v(TAG, "onContentCaptureEventsRequest(id=" + sessionId + ")"); |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 283 | } |
Felipe Leme | 62e45b0 | 2019-01-10 10:22:00 -0800 | [diff] [blame] | 284 | |
| 285 | /** |
| 286 | * Notifies the service that the app requested to remove data associated with the user. |
| 287 | * |
| 288 | * @param request the user data requested to be removed |
| 289 | */ |
| 290 | public void onUserDataRemovalRequest(@NonNull UserDataRemovalRequest request) { |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 291 | if (sVerbose) Log.v(TAG, "onUserDataRemovalRequest()"); |
Felipe Leme | 62e45b0 | 2019-01-10 10:22:00 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 294 | /** |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 295 | * Notifies the service of {@link SnapshotData snapshot data} associated with a session. |
| 296 | * |
| 297 | * @param sessionId the session's Id |
| 298 | * @param snapshotData the data |
| 299 | */ |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 300 | public void onActivitySnapshot(@NonNull ContentCaptureSessionId sessionId, |
Felipe Leme | 141864d | 2019-02-27 17:01:51 -0800 | [diff] [blame] | 301 | @NonNull SnapshotData snapshotData) { |
| 302 | if (sVerbose) Log.v(TAG, "onActivitySnapshot(id=" + sessionId + ")"); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Notifies the service of an activity-level event that is not associated with a session. |
| 307 | * |
| 308 | * <p>This method can be used to track some high-level events for all activities, even those |
| 309 | * that are not whitelisted for Content Capture. |
| 310 | * |
| 311 | * @param event high-level activity event |
| 312 | */ |
| 313 | public void onActivityEvent(@NonNull ActivityEvent event) { |
| 314 | if (sVerbose) Log.v(TAG, "onActivityEvent(): " + event); |
| 315 | } |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 316 | |
| 317 | /** |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 318 | * Destroys the content capture session. |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 319 | * |
| 320 | * @param sessionId the id of the session to destroy |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 321 | * */ |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 322 | public void onDestroyContentCaptureSession(@NonNull ContentCaptureSessionId sessionId) { |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 323 | if (sVerbose) Log.v(TAG, "onDestroyContentCaptureSession(id=" + sessionId + ")"); |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 324 | } |
| 325 | |
Felipe Leme | bb98ed6 | 2018-12-21 09:29:27 -0800 | [diff] [blame] | 326 | /** |
Felipe Leme | 72e83d8 | 2019-02-13 14:51:17 -0800 | [diff] [blame] | 327 | * Disables the Content Capture service for the given user. |
| 328 | */ |
Felipe Leme | bedcd61 | 2019-03-22 10:29:30 -0700 | [diff] [blame] | 329 | public final void disableSelf() { |
| 330 | if (sDebug) Log.d(TAG, "disableSelf()"); |
Felipe Leme | 72e83d8 | 2019-02-13 14:51:17 -0800 | [diff] [blame] | 331 | |
| 332 | final IContentCaptureServiceCallback callback = mCallback; |
| 333 | if (callback == null) { |
Felipe Leme | bedcd61 | 2019-03-22 10:29:30 -0700 | [diff] [blame] | 334 | Log.w(TAG, "disableSelf(): no server callback"); |
Felipe Leme | 72e83d8 | 2019-02-13 14:51:17 -0800 | [diff] [blame] | 335 | return; |
| 336 | } |
| 337 | try { |
| 338 | callback.disableSelf(); |
| 339 | } catch (RemoteException e) { |
| 340 | e.rethrowFromSystemServer(); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | /** |
Felipe Leme | bb98ed6 | 2018-12-21 09:29:27 -0800 | [diff] [blame] | 345 | * Called when the Android system disconnects from the service. |
| 346 | * |
| 347 | * <p> At this point this service may no longer be an active {@link AutofillService}. |
| 348 | */ |
| 349 | public void onDisconnected() { |
| 350 | Slog.i(TAG, "unbinding from " + getClass().getName()); |
| 351 | } |
| 352 | |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 353 | @Override |
| 354 | @CallSuper |
| 355 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 356 | pw.print("Debug: "); pw.print(sDebug); pw.print(" Verbose: "); pw.println(sVerbose); |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 357 | final int size = mSessionUids.size(); |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 358 | pw.print("Number sessions: "); pw.println(size); |
| 359 | if (size > 0) { |
| 360 | final String prefix = " "; |
| 361 | for (int i = 0; i < size; i++) { |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 362 | pw.print(prefix); pw.print(mSessionUids.keyAt(i)); |
| 363 | pw.print(": uid="); pw.println(mSessionUids.valueAt(i)); |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 364 | } |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
Felipe Leme | af4e4fd | 2019-01-15 08:32:42 -0800 | [diff] [blame] | 368 | private void handleOnConnected(@NonNull IBinder callback) { |
| 369 | mCallback = IContentCaptureServiceCallback.Stub.asInterface(callback); |
| 370 | onConnected(); |
| 371 | } |
| 372 | |
| 373 | private void handleOnDisconnected() { |
| 374 | onDisconnected(); |
| 375 | mCallback = null; |
Felipe Leme | bb98ed6 | 2018-12-21 09:29:27 -0800 | [diff] [blame] | 376 | } |
| 377 | |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 378 | //TODO(b/111276913): consider caching the InteractionSessionId for the lifetime of the session, |
| 379 | // so we don't need to create a temporary InteractionSessionId for each event. |
| 380 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 381 | private void handleOnCreateSession(@NonNull ContentCaptureContext context, |
Felipe Leme | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame^] | 382 | int sessionId, int uid, IResultReceiver clientReceiver, int initialState) { |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 383 | mSessionUids.put(sessionId, uid); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 384 | onCreateContentCaptureSession(context, new ContentCaptureSessionId(sessionId)); |
Adam He | 328c0e3 | 2019-01-03 15:19:22 -0800 | [diff] [blame] | 385 | |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 386 | final int clientFlags = context.getFlags(); |
| 387 | int stateFlags = 0; |
| 388 | if ((clientFlags & ContentCaptureContext.FLAG_DISABLED_BY_FLAG_SECURE) != 0) { |
| 389 | stateFlags |= ContentCaptureSession.STATE_FLAG_SECURE; |
Adam He | 328c0e3 | 2019-01-03 15:19:22 -0800 | [diff] [blame] | 390 | } |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 391 | if ((clientFlags & ContentCaptureContext.FLAG_DISABLED_BY_APP) != 0) { |
| 392 | stateFlags |= ContentCaptureSession.STATE_BY_APP; |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 393 | } |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 394 | if (stateFlags == 0) { |
Felipe Leme | 518fb62 | 2019-03-08 15:35:04 -0800 | [diff] [blame] | 395 | stateFlags = initialState; |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 396 | } else { |
| 397 | stateFlags |= ContentCaptureSession.STATE_DISABLED; |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 398 | } |
| 399 | setClientState(clientReceiver, stateFlags, mClientInterface.asBinder()); |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 400 | } |
| 401 | |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 402 | private void handleSendEvents(int uid, |
| 403 | @NonNull ParceledListSlice<ContentCaptureEvent> parceledEvents) { |
| 404 | |
| 405 | // Most events belong to the same session, so we can keep a reference to the last one |
| 406 | // to avoid creating too many ContentCaptureSessionId objects |
Felipe Leme | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame^] | 407 | int lastSessionId = NO_SESSION_ID; |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 408 | ContentCaptureSessionId sessionId = null; |
| 409 | |
| 410 | final List<ContentCaptureEvent> events = parceledEvents.getList(); |
| 411 | for (int i = 0; i < events.size(); i++) { |
| 412 | final ContentCaptureEvent event = events.get(i); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 413 | if (!handleIsRightCallerFor(event, uid)) continue; |
Felipe Leme | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame^] | 414 | int sessionIdInt = event.getSessionId(); |
| 415 | if (sessionIdInt != lastSessionId) { |
| 416 | sessionId = new ContentCaptureSessionId(sessionIdInt); |
| 417 | lastSessionId = sessionIdInt; |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 418 | } |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 419 | switch (event.getType()) { |
| 420 | case ContentCaptureEvent.TYPE_SESSION_STARTED: |
Felipe Leme | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame] | 421 | final ContentCaptureContext clientContext = event.getContentCaptureContext(); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 422 | clientContext.setParentSessionId(event.getParentSessionId()); |
Felipe Leme | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame^] | 423 | mSessionUids.put(sessionIdInt, uid); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 424 | onCreateContentCaptureSession(clientContext, sessionId); |
| 425 | break; |
| 426 | case ContentCaptureEvent.TYPE_SESSION_FINISHED: |
Felipe Leme | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame^] | 427 | mSessionUids.delete(sessionIdInt); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 428 | onDestroyContentCaptureSession(sessionId); |
| 429 | break; |
| 430 | default: |
| 431 | onContentCaptureEvent(sessionId, event); |
| 432 | } |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 433 | } |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 434 | } |
| 435 | |
Felipe Leme | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame^] | 436 | private void handleOnActivitySnapshot(int sessionId, @NonNull SnapshotData snapshotData) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 437 | onActivitySnapshot(new ContentCaptureSessionId(sessionId), snapshotData); |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 438 | } |
| 439 | |
Felipe Leme | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame^] | 440 | private void handleFinishSession(int sessionId) { |
| 441 | mSessionUids.delete(sessionId); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 442 | onDestroyContentCaptureSession(new ContentCaptureSessionId(sessionId)); |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 443 | } |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 444 | |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 445 | private void handleOnUserDataRemovalRequest(@NonNull UserDataRemovalRequest request) { |
| 446 | onUserDataRemovalRequest(request); |
| 447 | } |
| 448 | |
Felipe Leme | 141864d | 2019-02-27 17:01:51 -0800 | [diff] [blame] | 449 | private void handleOnActivityEvent(@NonNull ActivityEvent event) { |
| 450 | onActivityEvent(event); |
| 451 | } |
| 452 | |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 453 | /** |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 454 | * 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] | 455 | */ |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 456 | private boolean handleIsRightCallerFor(@NonNull ContentCaptureEvent event, int uid) { |
Felipe Leme | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame^] | 457 | final int sessionId; |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 458 | switch (event.getType()) { |
| 459 | case ContentCaptureEvent.TYPE_SESSION_STARTED: |
| 460 | case ContentCaptureEvent.TYPE_SESSION_FINISHED: |
| 461 | sessionId = event.getParentSessionId(); |
| 462 | break; |
| 463 | default: |
| 464 | sessionId = event.getSessionId(); |
| 465 | } |
Felipe Leme | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame^] | 466 | if (mSessionUids.indexOfKey(sessionId) < 0) { |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 467 | if (sVerbose) { |
Felipe Leme | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame] | 468 | Log.v(TAG, "handleIsRightCallerFor(" + event + "): no session for " + sessionId |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 469 | + ": " + mSessionUids); |
| 470 | } |
| 471 | // Just ignore, as the session could have been finished already |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 472 | return false; |
| 473 | } |
Felipe Leme | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame^] | 474 | final int rightUid = mSessionUids.get(sessionId); |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 475 | if (rightUid != uid) { |
| 476 | Log.e(TAG, "invalid call from UID " + uid + ": session " + sessionId + " belongs to " |
| 477 | + rightUid); |
| 478 | //TODO(b/111276913): log metrics as this could be a malicious app forging a sessionId |
| 479 | return false; |
| 480 | } |
| 481 | return true; |
| 482 | |
| 483 | } |
| 484 | |
| 485 | /** |
Adam He | 328c0e3 | 2019-01-03 15:19:22 -0800 | [diff] [blame] | 486 | * Sends the state of the {@link ContentCaptureManager} in the client app. |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 487 | * |
| 488 | * @param clientReceiver receiver in the client app. |
Adam He | 328c0e3 | 2019-01-03 15:19:22 -0800 | [diff] [blame] | 489 | * @param sessionState state of the session |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 490 | * @param binder handle to the {@code IContentCaptureDirectManager} object that resides in the |
| 491 | * service. |
| 492 | * @hide |
| 493 | */ |
| 494 | public static void setClientState(@NonNull IResultReceiver clientReceiver, |
Adam He | 328c0e3 | 2019-01-03 15:19:22 -0800 | [diff] [blame] | 495 | int sessionState, @Nullable IBinder binder) { |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 496 | try { |
| 497 | final Bundle extras; |
| 498 | if (binder != null) { |
| 499 | extras = new Bundle(); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 500 | extras.putBinder(MainContentCaptureSession.EXTRA_BINDER, binder); |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 501 | } else { |
| 502 | extras = null; |
| 503 | } |
Adam He | 328c0e3 | 2019-01-03 15:19:22 -0800 | [diff] [blame] | 504 | clientReceiver.send(sessionState, extras); |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 505 | } catch (RemoteException e) { |
| 506 | Slog.w(TAG, "Error async reporting result to client: " + e); |
| 507 | } |
| 508 | } |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 509 | } |