blob: 68a86f3c8bcaba3ffc04a6a0a4c5be14cdf6f334 [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.autofill.augmented;
17
18import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage;
19
20import android.annotation.CallSuper;
21import android.annotation.IntDef;
22import android.annotation.NonNull;
23import android.annotation.Nullable;
24import android.annotation.SystemApi;
25import android.app.Service;
26import android.content.ComponentName;
27import android.content.Intent;
28import android.graphics.Rect;
29import android.os.CancellationSignal;
30import android.os.Handler;
31import android.os.IBinder;
32import android.os.Looper;
33import android.os.RemoteException;
34import android.os.SystemClock;
35import android.service.autofill.augmented.AugmentedAutofillService.AutofillProxy;
36import android.service.autofill.augmented.PresentationParams.SystemPopupPresentationParams;
37import android.util.Log;
38import android.util.Pair;
39import android.util.Slog;
40import android.util.SparseArray;
41import android.util.TimeUtils;
42import android.view.autofill.AutofillId;
43import android.view.autofill.AutofillValue;
44import android.view.autofill.IAugmentedAutofillManagerClient;
45
46import com.android.internal.annotations.GuardedBy;
47
48import java.io.FileDescriptor;
49import java.io.PrintWriter;
50import java.lang.annotation.Retention;
51import java.lang.annotation.RetentionPolicy;
52import java.util.ArrayList;
53import java.util.List;
54
55/**
56 * A service used to augment the Autofill subsystem by potentially providing autofill data when the
57 * "standard" workflow failed (for example, because the standard AutofillService didn't have data).
58 *
59 * @hide
60 */
61@SystemApi
62public abstract class AugmentedAutofillService extends Service {
63
64 private static final String TAG = AugmentedAutofillService.class.getSimpleName();
65
66 // TODO(b/111330312): STOPSHIP use dynamic value, or change to false
67 static final boolean DEBUG = true;
68 static final boolean VERBOSE = false;
69
70 /**
71 * The {@link Intent} that must be declared as handled by the service.
72 * To be supported, the service must also require the
73 * {@link android.Manifest.permission#BIND_AUGMENTED_AUTOFILL_SERVICE} permission so
74 * that other applications can not abuse it.
75 */
76 public static final String SERVICE_INTERFACE =
77 "android.service.autofill.augmented.AugmentedAutofillService";
78
79 private Handler mHandler;
80
81 private SparseArray<AutofillProxy> mAutofillProxies;
82
83 private final IAugmentedAutofillService mInterface = new IAugmentedAutofillService.Stub() {
84
85 @Override
86 public void onFillRequest(int sessionId, IBinder client, int taskId,
87 ComponentName componentName, AutofillId focusedId, AutofillValue focusedValue,
88 long requestTime, IFillCallback callback) {
89 mHandler.sendMessage(obtainMessage(AugmentedAutofillService::handleOnFillRequest,
90 AugmentedAutofillService.this, sessionId, client, taskId, componentName,
91 focusedId, focusedValue, requestTime, callback));
92 }
93
94 @Override
95 public void onDestroyFillWindowRequest(int sessionId) {
96 mHandler.sendMessage(
97 obtainMessage(AugmentedAutofillService::handleOnDestroyFillWindowRequest,
98 AugmentedAutofillService.this, sessionId));
99 }
100 };
101
102 @CallSuper
103 @Override
104 public void onCreate() {
105 super.onCreate();
106 mHandler = new Handler(Looper.getMainLooper(), null, true);
107 }
108
109 /** @hide */
110 @Override
111 public final IBinder onBind(Intent intent) {
112 if (SERVICE_INTERFACE.equals(intent.getAction())) {
113 return mInterface.asBinder();
114 }
115 Log.w(TAG, "Tried to bind to wrong intent (should be " + SERVICE_INTERFACE + ": " + intent);
116 return null;
117 }
118
119 @Override
120 public boolean onUnbind(Intent intent) {
121 mHandler.sendMessage(obtainMessage(AugmentedAutofillService::handleOnUnbind,
122 AugmentedAutofillService.this));
123 return false;
124 }
125
126 // TODO(b/111330312): add methods to disable autofill per app / activity?
127
128 /**
129 * Asks the service to handle an "augmented" autofill request.
130 *
131 * <p>This method is called when the "stantard" autofill service cannot handle a request, which
132 * typically occurs when:
133 * <ul>
134 * <li>Service does not recognize what should be autofilled.
135 * <li>Service does not have data to fill the request.
136 * <li>Service blacklisted that app (or activity) for autofill.
137 * <li>App disabled itself for autofill.
138 * </ul>
139 *
140 * <p>Differently from the standard autofill workflow, on augmented autofill the service is
141 * responsible to generate the autofill UI and request the Android system to autofill the
142 * activity when the user taps an action in that UI (through the
143 * {@link FillController#autofill(List)} method).
144 *
145 * <p>The service <b>MUST</b> call {@link
146 * FillCallback#onSuccess(android.service.autofill.augmented.FillResponse)} as soon as possible,
147 * passing {@code null} when it cannot fulfill the request.
148 * @param request the request to handle.
149 * @param cancellationSignal signal for observing cancellation requests. The system will use
150 * this to notify you that the fill result is no longer needed and you should stop
151 * handling this fill request in order to save resources.
152 * @param controller object used to interact with the autofill system.
153 * @param callback object used to notify the result of the request. Service <b>must</b> call
154 * {@link FillCallback#onSuccess(android.service.autofill.augmented.FillResponse)}.
155 */
156 public void onFillRequest(@NonNull FillRequest request,
157 @NonNull CancellationSignal cancellationSignal, @NonNull FillController controller,
158 @NonNull FillCallback callback) {
159 }
160
161 private void handleOnFillRequest(int sessionId, @NonNull IBinder client, int taskId,
162 @NonNull ComponentName componentName, @NonNull AutofillId focusedId,
163 @Nullable AutofillValue focusedValue, long requestTime,
164 @NonNull IFillCallback callback) {
165 if (mAutofillProxies == null) {
166 mAutofillProxies = new SparseArray<>();
167 }
168 AutofillProxy proxy = mAutofillProxies.get(sessionId);
169 if (proxy == null) {
170 proxy = new AutofillProxy(sessionId, client, taskId, componentName, focusedId,
171 focusedValue, requestTime, callback);
172 mAutofillProxies.put(sessionId, proxy);
173 } else {
174 // TODO(b/111330312): figure out if it's ok to reuse the proxy; add logging
175 if (DEBUG) Log.d(TAG, "Reusing proxy for session " + sessionId);
176 }
177 // TODO(b/111330312): set cancellation signal
178 final CancellationSignal cancellationSignal = null;
179 onFillRequest(new FillRequest(proxy), cancellationSignal, new FillController(proxy),
180 new FillCallback(proxy));
181 }
182
183 private void handleOnDestroyFillWindowRequest(@NonNull int sessionId) {
184 AutofillProxy proxy = null;
185 if (mAutofillProxies != null) {
186 proxy = mAutofillProxies.get(sessionId);
187 }
188 if (proxy == null) {
189 // TODO(b/111330312): this might be fine, in which case we should logv it
190 Log.w(TAG, "No proxy for session " + sessionId);
191 return;
192 }
193 proxy.destroy();
194 mAutofillProxies.remove(sessionId);
195 }
196
197 private void handleOnUnbind() {
198 if (mAutofillProxies == null) {
199 if (DEBUG) Log.d(TAG, "onUnbind(): no proxy to destroy");
200 return;
201 }
202 final int size = mAutofillProxies.size();
203 if (DEBUG) Log.d(TAG, "onUnbind(): destroying " + size + " proxies");
204 for (int i = 0; i < size; i++) {
205 final AutofillProxy proxy = mAutofillProxies.valueAt(i);
206 try {
207 proxy.destroy();
208 } catch (Exception e) {
209 Log.w(TAG, "error destroying " + proxy);
210 }
211 }
212 mAutofillProxies = null;
213 }
214
215 @Override
216 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
217 if (mAutofillProxies != null) {
218 final int size = mAutofillProxies.size();
219 pw.print("Number proxies: "); pw.println(size);
220 for (int i = 0; i < size; i++) {
221 final int sessionId = mAutofillProxies.keyAt(i);
222 final AutofillProxy proxy = mAutofillProxies.valueAt(i);
223 pw.print(i); pw.print(") SessionId="); pw.print(sessionId); pw.println(":");
224 proxy.dump(" ", pw);
225 }
226 }
227 }
228
229 /** @hide */
230 static final class AutofillProxy {
231
232 static final int REPORT_EVENT_ON_SUCCESS = 1;
233 static final int REPORT_EVENT_UI_SHOWN = 2;
234 static final int REPORT_EVENT_UI_DESTROYED = 3;
235
236 @IntDef(prefix = { "REPORT_EVENT_" }, value = {
237 REPORT_EVENT_ON_SUCCESS,
238 REPORT_EVENT_UI_SHOWN,
239 REPORT_EVENT_UI_DESTROYED
240 })
241 @Retention(RetentionPolicy.SOURCE)
242 @interface ReportEvent{}
243
244
245 private final Object mLock = new Object();
246 private final IAugmentedAutofillManagerClient mClient;
247 private final int mSessionId;
248 private final IFillCallback mCallback;
249 public final int taskId;
250 public final ComponentName componentName;
251 public final AutofillId focusedId;
252 public final AutofillValue focusedValue;
253
254 // Objects used to log metrics
255 private final long mRequestTime;
256 private long mOnSuccessTime;
257 private long mUiFirstShownTime;
258 private long mUiFirstDestroyedTime;
259
260 @GuardedBy("mLock")
261 private SystemPopupPresentationParams mSmartSuggestion;
262
263 @GuardedBy("mLock")
264 private FillWindow mFillWindow;
265
266 private AutofillProxy(int sessionId, @NonNull IBinder client, int taskId,
267 @NonNull ComponentName componentName, @NonNull AutofillId focusedId,
268 @Nullable AutofillValue focusedValue, long requestTime,
269 @NonNull IFillCallback callback) {
270 mSessionId = sessionId;
271 mClient = IAugmentedAutofillManagerClient.Stub.asInterface(client);
272 mCallback = callback;
273 this.taskId = taskId;
274 this.componentName = componentName;
275 this.focusedId = focusedId;
276 this.focusedValue = focusedValue;
277 this.mRequestTime = requestTime;
278 // TODO(b/111330312): linkToDeath
279 }
280
281 @NonNull
282 public SystemPopupPresentationParams getSmartSuggestionParams() {
283 synchronized (mLock) {
284 if (mSmartSuggestion != null) {
285 return mSmartSuggestion;
286 }
287 Rect rect;
288 try {
289 rect = mClient.getViewCoordinates(focusedId);
290 } catch (RemoteException e) {
291 Log.w(TAG, "Could not get coordinates for " + focusedId);
292 return null;
293 }
294 if (rect == null) {
295 if (DEBUG) Log.d(TAG, "getViewCoordinates(" + focusedId + ") returned null");
296 return null;
297 }
298 mSmartSuggestion = new SystemPopupPresentationParams(this, rect);
299 return mSmartSuggestion;
300 }
301 }
302
303 public void autofill(@NonNull List<Pair<AutofillId, AutofillValue>> pairs)
304 throws RemoteException {
305 final int size = pairs.size();
306 final List<AutofillId> ids = new ArrayList<>(size);
307 final List<AutofillValue> values = new ArrayList<>(size);
308 for (int i = 0; i < size; i++) {
309 final Pair<AutofillId, AutofillValue> pair = pairs.get(i);
310 ids.add(pair.first);
311 values.add(pair.second);
312 }
313 mClient.autofill(mSessionId, ids, values);
314 }
315
316 public void setFillWindow(@NonNull FillWindow fillWindow) {
317 synchronized (mLock) {
318 mFillWindow = fillWindow;
319 }
320 }
321
322 public FillWindow getFillWindow() {
323 synchronized (mLock) {
324 return mFillWindow;
325 }
326 }
327
328 // Used (mostly) for metrics.
329 public void report(@ReportEvent int event) {
330 switch (event) {
331 case REPORT_EVENT_ON_SUCCESS:
332 if (mOnSuccessTime == 0) {
333 mOnSuccessTime = SystemClock.elapsedRealtime();
334 if (DEBUG) {
335 Slog.d(TAG, "Service responsed in "
336 + TimeUtils.formatDuration(mOnSuccessTime - mRequestTime));
337 }
338 }
339 try {
340 mCallback.onSuccess();
341 } catch (RemoteException e) {
342 Log.e(TAG, "Error reporting success: " + e);
343 }
344 break;
345 case REPORT_EVENT_UI_SHOWN:
346 if (mUiFirstShownTime == 0) {
347 mUiFirstShownTime = SystemClock.elapsedRealtime();
348 if (DEBUG) {
349 Slog.d(TAG, "UI shown in "
350 + TimeUtils.formatDuration(mUiFirstShownTime - mRequestTime));
351 }
352 }
353 break;
354 case REPORT_EVENT_UI_DESTROYED:
355 if (mUiFirstDestroyedTime == 0) {
356 mUiFirstDestroyedTime = SystemClock.elapsedRealtime();
357 if (DEBUG) {
358 Slog.d(TAG, "UI destroyed in "
359 + TimeUtils.formatDuration(
360 mUiFirstDestroyedTime - mRequestTime));
361 }
362 }
363 break;
364 default:
365 Slog.w(TAG, "invalid event reported: " + event);
366 }
367 // TODO(b/111330312): log metrics as well
368 }
369
370 public void dump(@NonNull String prefix, @NonNull PrintWriter pw) {
371 pw.print(prefix); pw.print("sessionId: "); pw.println(mSessionId);
372 pw.print(prefix); pw.print("taskId: "); pw.println(taskId);
373 pw.print(prefix); pw.print("component: ");
374 pw.println(componentName.flattenToShortString());
375 pw.print(prefix); pw.print("focusedId: "); pw.println(focusedId);
376 if (focusedValue != null) {
377 pw.print(prefix); pw.print("focusedValue: "); pw.println(focusedValue);
378 }
379 pw.print(prefix); pw.print("client: "); pw.println(mClient);
380 final String prefix2 = prefix + " ";
381 if (mFillWindow != null) {
382 pw.print(prefix); pw.println("window:");
383 mFillWindow.dump(prefix2, pw);
384 }
385 if (mSmartSuggestion != null) {
386 pw.print(prefix); pw.println("smartSuggestion:");
387 mSmartSuggestion.dump(prefix2, pw);
388 }
389 if (mOnSuccessTime > 0) {
390 final long responseTime = mOnSuccessTime - mRequestTime;
391 pw.print(prefix); pw.print("response time: ");
392 TimeUtils.formatDuration(responseTime, pw); pw.println();
393 }
394
395 if (mUiFirstShownTime > 0) {
396 final long uiRenderingTime = mUiFirstShownTime - mRequestTime;
397 pw.print(prefix); pw.print("UI rendering time: ");
398 TimeUtils.formatDuration(uiRenderingTime, pw); pw.println();
399 }
400
401 if (mUiFirstDestroyedTime > 0) {
402 final long uiTotalTime = mUiFirstDestroyedTime - mRequestTime;
403 pw.print(prefix); pw.print("UI life time: ");
404 TimeUtils.formatDuration(uiTotalTime, pw); pw.println();
405 }
406 }
407
408 private void destroy() {
409 synchronized (mLock) {
410 if (mFillWindow != null) {
411 if (DEBUG) Log.d(TAG, "destroying window");
412 mFillWindow.destroy();
413 }
414 }
415 }
416 }
417}