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