blob: 3505c2900beeb142eeb1968024c1fc28b9a23c28 [file] [log] [blame]
Felipe Leme3461d3c2017-01-19 08:54:55 -08001/*
2 * Copyright (C) 2017 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
17package android.view.autofill;
18
Felipe Leme73fedac2017-05-12 09:52:07 -070019import static android.service.autofill.FillRequest.FLAG_MANUAL_REQUEST;
Felipe Leme9f9ee252017-04-27 13:56:22 -070020import static android.view.autofill.Helper.sDebug;
21import static android.view.autofill.Helper.sVerbose;
Felipe Lemed633f072017-02-14 10:17:17 -080022
Felipe Lemee6010f22017-03-03 11:19:51 -080023import android.annotation.IntDef;
Philip P. Moltmann44611812017-02-23 12:52:46 -080024import android.annotation.NonNull;
Felipe Lemee6010f22017-03-03 11:19:51 -080025import android.annotation.Nullable;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060026import android.annotation.SystemService;
Felipe Leme3461d3c2017-01-19 08:54:55 -080027import android.content.Context;
Svet Ganov782043c2017-02-11 00:52:02 +000028import android.content.Intent;
29import android.content.IntentSender;
Felipe Leme3461d3c2017-01-19 08:54:55 -080030import android.graphics.Rect;
Felipe Leme4753bb02017-03-22 20:24:00 -070031import android.metrics.LogMaker;
Svet Ganov782043c2017-02-11 00:52:02 +000032import android.os.Bundle;
Svet Ganov782043c2017-02-11 00:52:02 +000033import android.os.Parcelable;
Felipe Leme3461d3c2017-01-19 08:54:55 -080034import android.os.RemoteException;
Philip P. Moltmanncc684ed2017-04-17 14:18:33 -070035import android.service.autofill.AutofillService;
36import android.service.autofill.FillEventHistory;
Felipe Leme4753bb02017-03-22 20:24:00 -070037import android.util.ArrayMap;
Philip P. Moltmann494c3f52017-04-11 10:13:33 -070038import android.util.ArraySet;
Felipe Leme3461d3c2017-01-19 08:54:55 -080039import android.util.Log;
Felipe Leme4753bb02017-03-22 20:24:00 -070040import android.util.SparseArray;
Felipe Leme3461d3c2017-01-19 08:54:55 -080041import android.view.View;
42
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -070043import com.android.internal.annotations.GuardedBy;
Felipe Leme4753bb02017-03-22 20:24:00 -070044import com.android.internal.logging.MetricsLogger;
45import com.android.internal.logging.nano.MetricsProto;
46
Felipe Lemee6010f22017-03-03 11:19:51 -080047import java.lang.annotation.Retention;
48import java.lang.annotation.RetentionPolicy;
Svet Ganov782043c2017-02-11 00:52:02 +000049import java.lang.ref.WeakReference;
Philip P. Moltmann134cee22017-05-06 11:28:38 -070050import java.util.ArrayList;
Svet Ganov782043c2017-02-11 00:52:02 +000051import java.util.List;
Philip P. Moltmannb42d1332017-03-27 09:55:30 -070052import java.util.Objects;
Svet Ganov782043c2017-02-11 00:52:02 +000053
Felipe Leme3461d3c2017-01-19 08:54:55 -080054/**
Felipe Leme85d1c2d2017-04-21 08:56:04 -070055 * App entry point to the Autofill Framework.
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -070056 *
57 * <p>It is safe to call into this from any thread.
Felipe Leme3461d3c2017-01-19 08:54:55 -080058 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060059@SystemService(Context.AUTOFILL_MANAGER_SERVICE)
Felipe Leme640f30a2017-03-06 15:44:06 -080060public final class AutofillManager {
Felipe Leme3461d3c2017-01-19 08:54:55 -080061
Felipe Leme640f30a2017-03-06 15:44:06 -080062 private static final String TAG = "AutofillManager";
Felipe Leme3461d3c2017-01-19 08:54:55 -080063
Svet Ganov782043c2017-02-11 00:52:02 +000064 /**
65 * Intent extra: The assist structure which captures the filled screen.
Felipe Leme7320ca92017-03-29 15:09:54 -070066 *
Svet Ganov782043c2017-02-11 00:52:02 +000067 * <p>
68 * Type: {@link android.app.assist.AssistStructure}
Svet Ganov782043c2017-02-11 00:52:02 +000069 */
70 public static final String EXTRA_ASSIST_STRUCTURE =
71 "android.view.autofill.extra.ASSIST_STRUCTURE";
72
73 /**
74 * Intent extra: The result of an authentication operation. It is
75 * either a fully populated {@link android.service.autofill.FillResponse}
76 * or a fully populated {@link android.service.autofill.Dataset} if
77 * a response or a dataset is being authenticated respectively.
78 *
79 * <p>
80 * Type: {@link android.service.autofill.FillResponse} or a
81 * {@link android.service.autofill.Dataset}
Svet Ganov782043c2017-02-11 00:52:02 +000082 */
83 public static final String EXTRA_AUTHENTICATION_RESULT =
84 "android.view.autofill.extra.AUTHENTICATION_RESULT";
85
Felipe Leme7320ca92017-03-29 15:09:54 -070086 /**
87 * Intent extra: The optional extras provided by the
88 * {@link android.service.autofill.AutofillService}.
89 *
90 * <p>For example, when the service responds to a {@link
91 * android.service.autofill.FillCallback#onSuccess(android.service.autofill.FillResponse)} with
92 * a {@code FillResponse} that requires authentication, the Intent that launches the
93 * service authentication will contain the Bundle set by
Felipe Leme49e96962017-04-20 15:46:18 -070094 * {@link android.service.autofill.FillResponse.Builder#setClientState(Bundle)} on this extra.
Felipe Leme7320ca92017-03-29 15:09:54 -070095 *
96 * <p>
97 * Type: {@link android.os.Bundle}
98 */
Felipe Leme37a440fd2017-04-28 10:50:20 -070099 public static final String EXTRA_CLIENT_STATE =
Jeff Sharkey000ce802017-04-29 13:13:27 -0600100 "android.view.autofill.extra.CLIENT_STATE";
Felipe Leme7320ca92017-03-29 15:09:54 -0700101
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700102 static final String SESSION_ID_TAG = "android:sessionId";
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700103 static final String LAST_AUTOFILLED_DATA_TAG = "android:lastAutoFilledData";
104
Felipe Leme0aa4c502017-04-26 12:36:01 -0700105 /** @hide */ public static final int ACTION_START_SESSION = 1;
106 /** @hide */ public static final int ACTION_VIEW_ENTERED = 2;
107 /** @hide */ public static final int ACTION_VIEW_EXITED = 3;
108 /** @hide */ public static final int ACTION_VALUE_CHANGED = 4;
Felipe Leme3461d3c2017-01-19 08:54:55 -0800109
Felipe Leme9f9ee252017-04-27 13:56:22 -0700110
111 /** @hide */ public static final int FLAG_ADD_CLIENT_ENABLED = 0x1;
112 /** @hide */ public static final int FLAG_ADD_CLIENT_DEBUG = 0x2;
113 /** @hide */ public static final int FLAG_ADD_CLIENT_VERBOSE = 0x4;
114
Svetoslav Ganova9379d02017-05-09 17:40:24 -0700115 /** Which bits in an authentication id are used for the dataset id */
116 private static final int AUTHENTICATION_ID_DATASET_ID_MASK = 0xFFFF;
117 /** How many bits in an authentication id are used for the dataset id */
118 private static final int AUTHENTICATION_ID_DATASET_ID_SHIFT = 16;
119 /** @hide The index for an undefined data set */
120 public static final int AUTHENTICATION_ID_DATASET_ID_UNDEFINED = 0xFFFF;
121
122 /**
123 * Makes an authentication id from a request id and a dataset id.
124 *
125 * @param requestId The request id.
126 * @param datasetId The dataset id.
127 * @return The authentication id.
128 * @hide
129 */
130 public static int makeAuthenticationId(int requestId, int datasetId) {
131 return (requestId << AUTHENTICATION_ID_DATASET_ID_SHIFT)
132 | (datasetId & AUTHENTICATION_ID_DATASET_ID_MASK);
133 }
134
135 /**
136 * Gets the request id from an authentication id.
137 *
138 * @param authRequestId The authentication id.
139 * @return The request id.
140 * @hide
141 */
142 public static int getRequestIdFromAuthenticationId(int authRequestId) {
143 return (authRequestId >> AUTHENTICATION_ID_DATASET_ID_SHIFT);
144 }
145
146 /**
147 * Gets the dataset id from an authentication id.
148 *
149 * @param authRequestId The authentication id.
150 * @return The dataset id.
151 * @hide
152 */
153 public static int getDatasetIdFromAuthenticationId(int authRequestId) {
154 return (authRequestId & AUTHENTICATION_ID_DATASET_ID_MASK);
155 }
156
Felipe Leme4753bb02017-03-22 20:24:00 -0700157 private final MetricsLogger mMetricsLogger = new MetricsLogger();
Svet Ganov782043c2017-02-11 00:52:02 +0000158
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700159 /**
160 * There is currently no session running.
161 * {@hide}
162 */
163 public static final int NO_SESSION = Integer.MIN_VALUE;
164
Svet Ganov782043c2017-02-11 00:52:02 +0000165 private final IAutoFillManager mService;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700166
167 private final Object mLock = new Object();
168
169 @GuardedBy("mLock")
Svet Ganov782043c2017-02-11 00:52:02 +0000170 private IAutoFillManagerClient mServiceClient;
171
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700172 @GuardedBy("mLock")
Felipe Lemee6010f22017-03-03 11:19:51 -0800173 private AutofillCallback mCallback;
174
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700175 private final Context mContext;
Svet Ganov782043c2017-02-11 00:52:02 +0000176
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700177 @GuardedBy("mLock")
178 private int mSessionId = NO_SESSION;
179
180 @GuardedBy("mLock")
Svet Ganov782043c2017-02-11 00:52:02 +0000181 private boolean mEnabled;
182
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700183 /** If a view changes to this mapping the autofill operation was successful */
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700184 @GuardedBy("mLock")
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700185 @Nullable private ParcelableMap mLastAutofilledData;
186
Philip P. Moltmann494c3f52017-04-11 10:13:33 -0700187 /** If view tracking is enabled, contains the tracking state */
188 @GuardedBy("mLock")
189 @Nullable private TrackedViews mTrackedViews;
190
Felipe Leme27e20222017-05-18 15:24:11 -0700191 /** Views that are only tracked because they are fillable and could be anchoring the UI. */
192 @GuardedBy("mLock")
193 @Nullable private ArraySet<AutofillId> mFillableIds;
194
Svet Ganov782043c2017-02-11 00:52:02 +0000195 /** @hide */
Felipe Leme640f30a2017-03-06 15:44:06 -0800196 public interface AutofillClient {
Svet Ganov782043c2017-02-11 00:52:02 +0000197 /**
Svet Ganov782043c2017-02-11 00:52:02 +0000198 * Asks the client to start an authentication flow.
199 *
Svetoslav Ganova9379d02017-05-09 17:40:24 -0700200 * @param authenticationId A unique id of the authentication operation.
Svet Ganov782043c2017-02-11 00:52:02 +0000201 * @param intent The authentication intent.
202 * @param fillInIntent The authentication fill-in intent.
203 */
Svetoslav Ganova9379d02017-05-09 17:40:24 -0700204 void autofillCallbackAuthenticate(int authenticationId, IntentSender intent,
205 Intent fillInIntent);
Svet Ganov17db9dc2017-02-21 19:54:31 -0800206
207 /**
208 * Tells the client this manager has state to be reset.
209 */
Felipe Leme4753bb02017-03-22 20:24:00 -0700210 void autofillCallbackResetableStateAvailable();
211
212 /**
213 * Request showing the autofill UI.
214 *
215 * @param anchor The real view the UI needs to anchor to.
216 * @param width The width of the fill UI content.
217 * @param height The height of the fill UI content.
218 * @param virtualBounds The bounds of the virtual decendant of the anchor.
219 * @param presenter The presenter that controls the fill UI window.
220 * @return Whether the UI was shown.
221 */
222 boolean autofillCallbackRequestShowFillUi(@NonNull View anchor, int width, int height,
223 @Nullable Rect virtualBounds, IAutofillWindowPresenter presenter);
224
225 /**
226 * Request hiding the autofill UI.
227 *
228 * @return Whether the UI was hidden.
229 */
230 boolean autofillCallbackRequestHideFillUi();
Philip P. Moltmann494c3f52017-04-11 10:13:33 -0700231
232 /**
Philip P. Moltmann134cee22017-05-06 11:28:38 -0700233 * Checks if views are currently attached and visible.
Philip P. Moltmann494c3f52017-04-11 10:13:33 -0700234 *
Philip P. Moltmann134cee22017-05-06 11:28:38 -0700235 * @return And array with {@code true} iff the view is attached or visible
Philip P. Moltmann494c3f52017-04-11 10:13:33 -0700236 */
Philip P. Moltmann134cee22017-05-06 11:28:38 -0700237 @NonNull boolean[] getViewVisibility(@NonNull int[] viewId);
Philip P. Moltmann494c3f52017-04-11 10:13:33 -0700238
239 /**
240 * Checks is the client is currently visible as understood by autofill.
241 *
242 * @return {@code true} if the client is currently visible
243 */
244 boolean isVisibleForAutofill();
Philip P. Moltmann134cee22017-05-06 11:28:38 -0700245
246 /**
Felipe Leme27e20222017-05-18 15:24:11 -0700247 * Finds views by traversing the hierarchies of the client.
Philip P. Moltmann134cee22017-05-06 11:28:38 -0700248 *
Phil Weaver846cda932017-06-15 10:10:06 -0700249 * @param viewIds The autofill ids of the views to find
Philip P. Moltmann134cee22017-05-06 11:28:38 -0700250 *
Felipe Leme27e20222017-05-18 15:24:11 -0700251 * @return And array containing the views (empty if no views found).
Philip P. Moltmann134cee22017-05-06 11:28:38 -0700252 */
Phil Weaver846cda932017-06-15 10:10:06 -0700253 @NonNull View[] findViewsByAutofillIdTraversal(@NonNull int[] viewIds);
Felipe Leme27e20222017-05-18 15:24:11 -0700254
255 /**
256 * Finds a view by traversing the hierarchies of the client.
257 *
Phil Weaver846cda932017-06-15 10:10:06 -0700258 * @param viewId The autofill id of the views to find
Felipe Leme27e20222017-05-18 15:24:11 -0700259 *
260 * @return The view, or {@code null} if not found
261 */
Phil Weaver846cda932017-06-15 10:10:06 -0700262 @Nullable View findViewByAutofillIdTraversal(int viewId);
Felipe Leme9876a6f2017-05-30 15:47:28 -0700263
264 /**
265 * Runs the specified action on the UI thread.
266 */
267 void runOnUiThread(Runnable action);
Svet Ganov782043c2017-02-11 00:52:02 +0000268 }
Felipe Leme3461d3c2017-01-19 08:54:55 -0800269
270 /**
271 * @hide
272 */
Felipe Leme640f30a2017-03-06 15:44:06 -0800273 public AutofillManager(Context context, IAutoFillManager service) {
Felipe Lemebab851c2017-02-03 18:45:08 -0800274 mContext = context;
Felipe Leme3461d3c2017-01-19 08:54:55 -0800275 mService = service;
276 }
277
278 /**
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700279 * Restore state after activity lifecycle
280 *
281 * @param savedInstanceState The state to be restored
282 *
283 * {@hide}
284 */
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700285 public void onCreate(Bundle savedInstanceState) {
Svet Ganov43574b02017-04-12 09:25:20 -0700286 if (!hasAutofillFeature()) {
287 return;
288 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700289 synchronized (mLock) {
290 mLastAutofilledData = savedInstanceState.getParcelable(LAST_AUTOFILLED_DATA_TAG);
291
292 if (mSessionId != NO_SESSION) {
293 Log.w(TAG, "New session was started before onCreate()");
294 return;
295 }
296
297 mSessionId = savedInstanceState.getInt(SESSION_ID_TAG, NO_SESSION);
298
299 if (mSessionId != NO_SESSION) {
300 ensureServiceClientAddedIfNeededLocked();
301
302 final AutofillClient client = getClientLocked();
303 if (client != null) {
304 try {
305 final boolean sessionWasRestored = mService.restoreSession(mSessionId,
306 mContext.getActivityToken(), mServiceClient.asBinder());
307
308 if (!sessionWasRestored) {
309 Log.w(TAG, "Session " + mSessionId + " could not be restored");
310 mSessionId = NO_SESSION;
311 } else {
Felipe Leme9f9ee252017-04-27 13:56:22 -0700312 if (sDebug) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700313 Log.d(TAG, "session " + mSessionId + " was restored");
314 }
315
316 client.autofillCallbackResetableStateAvailable();
317 }
318 } catch (RemoteException e) {
319 Log.e(TAG, "Could not figure out if there was an autofill session", e);
320 }
321 }
322 }
323 }
324 }
325
326 /**
Philip P. Moltmann494c3f52017-04-11 10:13:33 -0700327 * Called once the client becomes visible.
328 *
329 * @see AutofillClient#isVisibleForAutofill()
330 *
331 * {@hide}
332 */
333 public void onVisibleForAutofill() {
334 synchronized (mLock) {
335 if (mEnabled && mSessionId != NO_SESSION && mTrackedViews != null) {
Philip P. Moltmann134cee22017-05-06 11:28:38 -0700336 mTrackedViews.onVisibleForAutofillLocked();
Philip P. Moltmann494c3f52017-04-11 10:13:33 -0700337 }
338 }
339 }
340
341 /**
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700342 * Save state before activity lifecycle
343 *
344 * @param outState Place to store the state
345 *
346 * {@hide}
347 */
348 public void onSaveInstanceState(Bundle outState) {
Svet Ganov43574b02017-04-12 09:25:20 -0700349 if (!hasAutofillFeature()) {
350 return;
351 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700352 synchronized (mLock) {
353 if (mSessionId != NO_SESSION) {
354 outState.putInt(SESSION_ID_TAG, mSessionId);
355 }
356
357 if (mLastAutofilledData != null) {
358 outState.putParcelable(LAST_AUTOFILLED_DATA_TAG, mLastAutofilledData);
359 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700360 }
361 }
362
363 /**
364 * Checks whether autofill is enabled for the current user.
Felipe Leme2ac463e2017-03-13 14:06:25 -0700365 *
366 * <p>Typically used to determine whether the option to explicitly request autofill should
367 * be offered - see {@link #requestAutofill(View)}.
368 *
369 * @return whether autofill is enabled for the current user.
370 */
371 public boolean isEnabled() {
Svet Ganov43574b02017-04-12 09:25:20 -0700372 if (!hasAutofillFeature()) {
373 return false;
374 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700375 synchronized (mLock) {
376 ensureServiceClientAddedIfNeededLocked();
377 return mEnabled;
378 }
Felipe Leme2ac463e2017-03-13 14:06:25 -0700379 }
380
381 /**
Philip P. Moltmanncc684ed2017-04-17 14:18:33 -0700382 * Should always be called from {@link AutofillService#getFillEventHistory()}.
383 *
384 * @hide
385 */
386 @Nullable public FillEventHistory getFillEventHistory() {
387 try {
388 return mService.getFillEventHistory();
389 } catch (RemoteException e) {
390 e.rethrowFromSystemServer();
391 return null;
392 }
393 }
394
395 /**
Felipe Leme2ac463e2017-03-13 14:06:25 -0700396 * Explicitly requests a new autofill context.
397 *
Felipe Leme6dcec872017-05-25 11:24:23 -0700398 * <p>Normally, the autofill context is automatically started if necessary when
399 * {@link #notifyViewEntered(View)} is called, but this method should be used in the
400 * cases where it must be explicitly started. For example, when the view offers an AUTOFILL
401 * option on its contextual overflow menu, and the user selects it.
Felipe Leme2ac463e2017-03-13 14:06:25 -0700402 *
403 * @param view view requesting the new autofill context.
404 */
405 public void requestAutofill(@NonNull View view) {
Felipe Lemed1146422017-04-26 10:17:05 -0700406 notifyViewEntered(view, FLAG_MANUAL_REQUEST);
Felipe Leme2ac463e2017-03-13 14:06:25 -0700407 }
408
409 /**
410 * Explicitly requests a new autofill context for virtual views.
411 *
Felipe Leme6dcec872017-05-25 11:24:23 -0700412 * <p>Normally, the autofill context is automatically started if necessary when
413 * {@link #notifyViewEntered(View, int, Rect)} is called, but this method should be used in the
414 * cases where it must be explicitly started. For example, when the virtual view offers an
415 * AUTOFILL option on its contextual overflow menu, and the user selects it.
Felipe Leme2ac463e2017-03-13 14:06:25 -0700416 *
Felipe Leme6dcec872017-05-25 11:24:23 -0700417 * <p>The virtual view boundaries must be absolute screen coordinates. For example, if the
418 * parent view uses {@code bounds} to draw the virtual view inside its Canvas,
419 * the absolute bounds could be calculated by:
420 *
421 * <pre class="prettyprint">
422 * int offset[] = new int[2];
423 * getLocationOnScreen(offset);
424 * Rect absBounds = new Rect(bounds.left + offset[0],
425 * bounds.top + offset[1],
426 * bounds.right + offset[0], bounds.bottom + offset[1]);
427 * </pre>
428 *
429 * @param view the virtual view parent.
430 * @param virtualId id identifying the virtual child inside the parent view.
431 * @param absBounds absolute boundaries of the virtual view in the screen.
Felipe Leme2ac463e2017-03-13 14:06:25 -0700432 */
Felipe Leme6dcec872017-05-25 11:24:23 -0700433 public void requestAutofill(@NonNull View view, int virtualId, @NonNull Rect absBounds) {
434 notifyViewEntered(view, virtualId, absBounds, FLAG_MANUAL_REQUEST);
Felipe Leme2ac463e2017-03-13 14:06:25 -0700435 }
436
Felipe Leme2ac463e2017-03-13 14:06:25 -0700437 /**
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700438 * Called when a {@link View} that supports autofill is entered.
Felipe Leme3461d3c2017-01-19 08:54:55 -0800439 *
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700440 * @param view {@link View} that was entered.
Felipe Leme3461d3c2017-01-19 08:54:55 -0800441 */
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700442 public void notifyViewEntered(@NonNull View view) {
Felipe Lemed1146422017-04-26 10:17:05 -0700443 notifyViewEntered(view, 0);
444 }
445
446 private void notifyViewEntered(@NonNull View view, int flags) {
Svet Ganov43574b02017-04-12 09:25:20 -0700447 if (!hasAutofillFeature()) {
448 return;
449 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700450 AutofillCallback callback = null;
451 synchronized (mLock) {
452 ensureServiceClientAddedIfNeededLocked();
Svet Ganov782043c2017-02-11 00:52:02 +0000453
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700454 if (!mEnabled) {
455 if (mCallback != null) {
456 callback = mCallback;
457 }
458 } else {
459 final AutofillId id = getAutofillId(view);
460 final AutofillValue value = view.getAutofillValue();
461
462 if (mSessionId == NO_SESSION) {
463 // Starts new session.
Philip P. Moltmann134cee22017-05-06 11:28:38 -0700464 startSessionLocked(id, null, value, flags);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700465 } else {
466 // Update focus on existing session.
Felipe Leme0aa4c502017-04-26 12:36:01 -0700467 updateSessionLocked(id, null, value, ACTION_VIEW_ENTERED, flags);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700468 }
Felipe Leme24aae152017-03-15 12:33:01 -0700469 }
Felipe Lemebab851c2017-02-03 18:45:08 -0800470 }
471
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700472 if (callback != null) {
473 mCallback.onAutofillEvent(view, AutofillCallback.EVENT_INPUT_UNAVAILABLE);
Svet Ganov782043c2017-02-11 00:52:02 +0000474 }
Felipe Lemebab851c2017-02-03 18:45:08 -0800475 }
476
477 /**
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700478 * Called when a {@link View} that supports autofill is exited.
Felipe Lemebab851c2017-02-03 18:45:08 -0800479 *
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700480 * @param view {@link View} that was exited.
Philip P. Moltmann44611812017-02-23 12:52:46 -0800481 */
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700482 public void notifyViewExited(@NonNull View view) {
Svet Ganov43574b02017-04-12 09:25:20 -0700483 if (!hasAutofillFeature()) {
484 return;
485 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700486 synchronized (mLock) {
487 ensureServiceClientAddedIfNeededLocked();
Philip P. Moltmann44611812017-02-23 12:52:46 -0800488
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700489 if (mEnabled && mSessionId != NO_SESSION) {
490 final AutofillId id = getAutofillId(view);
Philip P. Moltmann44611812017-02-23 12:52:46 -0800491
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700492 // Update focus on existing session.
Felipe Leme0aa4c502017-04-26 12:36:01 -0700493 updateSessionLocked(id, null, null, ACTION_VIEW_EXITED, 0);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700494 }
Philip P. Moltmann44611812017-02-23 12:52:46 -0800495 }
496 }
497
498 /**
Philip P. Moltmann494c3f52017-04-11 10:13:33 -0700499 * Called when a {@link View view's} visibility changes.
500 *
501 * @param view {@link View} that was exited.
502 * @param isVisible visible if the view is visible in the view hierarchy.
503 *
504 * @hide
505 */
506 public void notifyViewVisibilityChange(@NonNull View view, boolean isVisible) {
507 synchronized (mLock) {
Felipe Leme27e20222017-05-18 15:24:11 -0700508 if (mEnabled && mSessionId != NO_SESSION) {
509 if (!isVisible && mFillableIds != null) {
510 final AutofillId id = view.getAutofillId();
511 if (mFillableIds.contains(id)) {
512 if (sDebug) Log.d(TAG, "Hidding UI when view " + id + " became invisible");
513 requestHideFillUi(id, view);
514 }
515 }
516 if (mTrackedViews != null) {
517 mTrackedViews.notifyViewVisibilityChange(view, isVisible);
518 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -0700519 }
520 }
521 }
522
523 /**
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700524 * Called when a virtual view that supports autofill is entered.
Philip P. Moltmann44611812017-02-23 12:52:46 -0800525 *
Felipe Leme6dcec872017-05-25 11:24:23 -0700526 * <p>The virtual view boundaries must be absolute screen coordinates. For example, if the
527 * parent, non-virtual view uses {@code bounds} to draw the virtual view inside its Canvas,
528 * the absolute bounds could be calculated by:
529 *
530 * <pre class="prettyprint">
531 * int offset[] = new int[2];
532 * getLocationOnScreen(offset);
533 * Rect absBounds = new Rect(bounds.left + offset[0],
534 * bounds.top + offset[1],
535 * bounds.right + offset[0], bounds.bottom + offset[1]);
536 * </pre>
537 *
538 * @param view the virtual view parent.
539 * @param virtualId id identifying the virtual child inside the parent view.
540 * @param absBounds absolute boundaries of the virtual view in the screen.
Felipe Lemebab851c2017-02-03 18:45:08 -0800541 */
Felipe Leme6dcec872017-05-25 11:24:23 -0700542 public void notifyViewEntered(@NonNull View view, int virtualId, @NonNull Rect absBounds) {
543 notifyViewEntered(view, virtualId, absBounds, 0);
Felipe Lemed1146422017-04-26 10:17:05 -0700544 }
545
Felipe Leme6dcec872017-05-25 11:24:23 -0700546 private void notifyViewEntered(View view, int virtualId, Rect bounds, int flags) {
Svet Ganov43574b02017-04-12 09:25:20 -0700547 if (!hasAutofillFeature()) {
548 return;
549 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700550 AutofillCallback callback = null;
551 synchronized (mLock) {
552 ensureServiceClientAddedIfNeededLocked();
Svet Ganov782043c2017-02-11 00:52:02 +0000553
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700554 if (!mEnabled) {
555 if (mCallback != null) {
556 callback = mCallback;
557 }
558 } else {
Felipe Leme6dcec872017-05-25 11:24:23 -0700559 final AutofillId id = getAutofillId(view, virtualId);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700560
561 if (mSessionId == NO_SESSION) {
562 // Starts new session.
Philip P. Moltmann134cee22017-05-06 11:28:38 -0700563 startSessionLocked(id, bounds, null, flags);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700564 } else {
565 // Update focus on existing session.
Felipe Leme0aa4c502017-04-26 12:36:01 -0700566 updateSessionLocked(id, bounds, null, ACTION_VIEW_ENTERED, flags);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700567 }
Felipe Leme24aae152017-03-15 12:33:01 -0700568 }
Felipe Lemebab851c2017-02-03 18:45:08 -0800569 }
570
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700571 if (callback != null) {
Felipe Leme6dcec872017-05-25 11:24:23 -0700572 callback.onAutofillEvent(view, virtualId,
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700573 AutofillCallback.EVENT_INPUT_UNAVAILABLE);
Philip P. Moltmann44611812017-02-23 12:52:46 -0800574 }
575 }
576
577 /**
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700578 * Called when a virtual view that supports autofill is exited.
Philip P. Moltmann44611812017-02-23 12:52:46 -0800579 *
Felipe Leme6dcec872017-05-25 11:24:23 -0700580 * @param view the virtual view parent.
581 * @param virtualId id identifying the virtual child inside the parent view.
Philip P. Moltmann44611812017-02-23 12:52:46 -0800582 */
Felipe Leme6dcec872017-05-25 11:24:23 -0700583 public void notifyViewExited(@NonNull View view, int virtualId) {
Svet Ganov43574b02017-04-12 09:25:20 -0700584 if (!hasAutofillFeature()) {
585 return;
586 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700587 synchronized (mLock) {
588 ensureServiceClientAddedIfNeededLocked();
Philip P. Moltmann44611812017-02-23 12:52:46 -0800589
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700590 if (mEnabled && mSessionId != NO_SESSION) {
Felipe Leme6dcec872017-05-25 11:24:23 -0700591 final AutofillId id = getAutofillId(view, virtualId);
Philip P. Moltmann44611812017-02-23 12:52:46 -0800592
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700593 // Update focus on existing session.
Felipe Leme0aa4c502017-04-26 12:36:01 -0700594 updateSessionLocked(id, null, null, ACTION_VIEW_EXITED, 0);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700595 }
Svet Ganov782043c2017-02-11 00:52:02 +0000596 }
Felipe Lemebab851c2017-02-03 18:45:08 -0800597 }
598
599 /**
Felipe Leme640f30a2017-03-06 15:44:06 -0800600 * Called to indicate the value of an autofillable {@link View} changed.
Felipe Lemebab851c2017-02-03 18:45:08 -0800601 *
Philip P. Moltmann44611812017-02-23 12:52:46 -0800602 * @param view view whose value changed.
Felipe Lemebab851c2017-02-03 18:45:08 -0800603 */
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700604 public void notifyValueChanged(View view) {
Svet Ganov43574b02017-04-12 09:25:20 -0700605 if (!hasAutofillFeature()) {
606 return;
607 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700608 AutofillId id = null;
609 boolean valueWasRead = false;
610 AutofillValue value = null;
611
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700612 synchronized (mLock) {
613 // If the session is gone some fields might still be highlighted, hence we have to
614 // remove the isAutofilled property even if no sessions are active.
615 if (mLastAutofilledData == null) {
616 view.setAutofilled(false);
617 } else {
618 id = getAutofillId(view);
619 if (mLastAutofilledData.containsKey(id)) {
620 value = view.getAutofillValue();
621 valueWasRead = true;
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700622
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700623 if (Objects.equals(mLastAutofilledData.get(id), value)) {
624 view.setAutofilled(true);
625 } else {
626 view.setAutofilled(false);
627 mLastAutofilledData.remove(id);
628 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700629 } else {
630 view.setAutofilled(false);
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700631 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700632 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700633
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700634 if (!mEnabled || mSessionId == NO_SESSION) {
635 return;
636 }
Felipe Lemebab851c2017-02-03 18:45:08 -0800637
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700638 if (id == null) {
639 id = getAutofillId(view);
640 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700641
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700642 if (!valueWasRead) {
643 value = view.getAutofillValue();
644 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700645
Felipe Leme0aa4c502017-04-26 12:36:01 -0700646 updateSessionLocked(id, null, value, ACTION_VALUE_CHANGED, 0);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700647 }
Felipe Lemebab851c2017-02-03 18:45:08 -0800648 }
649
Felipe Lemebab851c2017-02-03 18:45:08 -0800650 /**
Felipe Leme6dcec872017-05-25 11:24:23 -0700651 * Called to indicate the value of an autofillable virtual view has changed.
Felipe Lemebab851c2017-02-03 18:45:08 -0800652 *
Felipe Leme6dcec872017-05-25 11:24:23 -0700653 * @param view the virtual view parent.
654 * @param virtualId id identifying the virtual child inside the parent view.
Felipe Lemebab851c2017-02-03 18:45:08 -0800655 * @param value new value of the child.
656 */
Felipe Leme6dcec872017-05-25 11:24:23 -0700657 public void notifyValueChanged(View view, int virtualId, AutofillValue value) {
Svet Ganov43574b02017-04-12 09:25:20 -0700658 if (!hasAutofillFeature()) {
659 return;
660 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700661 synchronized (mLock) {
662 if (!mEnabled || mSessionId == NO_SESSION) {
663 return;
664 }
Felipe Lemebab851c2017-02-03 18:45:08 -0800665
Felipe Leme6dcec872017-05-25 11:24:23 -0700666 final AutofillId id = getAutofillId(view, virtualId);
Felipe Leme0aa4c502017-04-26 12:36:01 -0700667 updateSessionLocked(id, null, value, ACTION_VALUE_CHANGED, 0);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700668 }
Felipe Lemebab851c2017-02-03 18:45:08 -0800669 }
670
671 /**
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700672 * Called to indicate the current autofill context should be commited.
Felipe Lemebab851c2017-02-03 18:45:08 -0800673 *
Felipe Leme33681a92017-07-28 09:14:38 -0700674 * <p>This method is typically called by {@link View Views} that manage virtual views; for
675 * example, when the view is rendering an {@code HTML} page with a form and virtual views
676 * that represent the HTML elements, it should call this method after the form is submitted and
677 * another page is rendered.
678 *
679 * <p><b>Note:</b> This method does not need to be called on regular application lifecycle
680 * methods such as {@link android.app.Activity#finish()}.
Felipe Lemebab851c2017-02-03 18:45:08 -0800681 */
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700682 public void commit() {
Svet Ganov43574b02017-04-12 09:25:20 -0700683 if (!hasAutofillFeature()) {
684 return;
685 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700686 synchronized (mLock) {
687 if (!mEnabled && mSessionId == NO_SESSION) {
688 return;
689 }
Svet Ganov782043c2017-02-11 00:52:02 +0000690
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700691 finishSessionLocked();
692 }
Felipe Leme0200d9e2017-01-24 15:10:26 -0800693 }
694
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700695 /**
696 * Called to indicate the current autofill context should be cancelled.
697 *
Felipe Leme33681a92017-07-28 09:14:38 -0700698 * <p>This method is typically called by {@link View Views} that manage virtual views; for
699 * example, when the view is rendering an {@code HTML} page with a form and virtual views
700 * that represent the HTML elements, it should call this method if the user does not post the
701 * form but moves to another form in this page.
702 *
703 * <p><b>Note:</b> This method does not need to be called on regular application lifecycle
704 * methods such as {@link android.app.Activity#finish()}.
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700705 */
706 public void cancel() {
Svet Ganov43574b02017-04-12 09:25:20 -0700707 if (!hasAutofillFeature()) {
708 return;
709 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700710 synchronized (mLock) {
711 if (!mEnabled && mSessionId == NO_SESSION) {
712 return;
713 }
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700714
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700715 cancelSessionLocked();
716 }
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700717 }
718
Svet Ganovf965b872017-04-28 16:34:02 -0700719 /** @hide */
720 public void disableOwnedAutofillServices() {
721 disableAutofillServices();
722 }
723
Svet Ganovf20a0372017-04-10 17:08:05 -0700724 /**
725 * If the app calling this API has enabled autofill services they
726 * will be disabled.
727 */
Svet Ganovf965b872017-04-28 16:34:02 -0700728 public void disableAutofillServices() {
Svet Ganov43574b02017-04-12 09:25:20 -0700729 if (!hasAutofillFeature()) {
730 return;
731 }
Svet Ganovf20a0372017-04-10 17:08:05 -0700732 try {
733 mService.disableOwnedAutofillServices(mContext.getUserId());
734 } catch (RemoteException e) {
735 throw e.rethrowFromSystemServer();
736 }
737 }
738
Felipe Lemedb041182017-04-21 17:33:38 -0700739 /**
740 * Returns {@code true} if the calling application provides a {@link AutofillService} that is
741 * enabled for the current user, or {@code false} otherwise.
742 */
743 public boolean hasEnabledAutofillServices() {
744 if (mService == null) return false;
745
746 try {
747 return mService.isServiceEnabled(mContext.getUserId(), mContext.getPackageName());
748 } catch (RemoteException e) {
749 throw e.rethrowFromSystemServer();
750 }
751 }
752
753 /**
Ricardo Loo33d226c2017-06-27 14:17:33 -0700754 * Returns {@code true} if autofill is supported by the current device and
755 * is supported for this user.
Felipe Lemedb041182017-04-21 17:33:38 -0700756 *
757 * <p>Autofill is typically supported, but it could be unsupported in cases like:
758 * <ol>
759 * <li>Low-end devices.
760 * <li>Device policy rules that forbid its usage.
761 * </ol>
762 */
763 public boolean isAutofillSupported() {
764 if (mService == null) return false;
765
766 try {
767 return mService.isServiceSupported(mContext.getUserId());
768 } catch (RemoteException e) {
769 throw e.rethrowFromSystemServer();
770 }
771 }
772
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700773 private AutofillClient getClientLocked() {
Felipe Leme640f30a2017-03-06 15:44:06 -0800774 if (mContext instanceof AutofillClient) {
775 return (AutofillClient) mContext;
Svet Ganov782043c2017-02-11 00:52:02 +0000776 }
Svet Ganov17db9dc2017-02-21 19:54:31 -0800777 return null;
Svet Ganov782043c2017-02-11 00:52:02 +0000778 }
779
780 /** @hide */
Svetoslav Ganova9379d02017-05-09 17:40:24 -0700781 public void onAuthenticationResult(int authenticationId, Intent data) {
Svet Ganov43574b02017-04-12 09:25:20 -0700782 if (!hasAutofillFeature()) {
783 return;
784 }
Felipe Leme85d1c2d2017-04-21 08:56:04 -0700785 // TODO: the result code is being ignored, so this method is not reliably
Felipe Lemed633f072017-02-14 10:17:17 -0800786 // handling the cases where it's not RESULT_OK: it works fine if the service does not
787 // set the EXTRA_AUTHENTICATION_RESULT extra, but it could cause weird results if the
788 // service set the extra and returned RESULT_CANCELED...
789
Felipe Leme9f9ee252017-04-27 13:56:22 -0700790 if (sDebug) Log.d(TAG, "onAuthenticationResult(): d=" + data);
Felipe Lemed633f072017-02-14 10:17:17 -0800791
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700792 synchronized (mLock) {
793 if (mSessionId == NO_SESSION || data == null) {
794 return;
795 }
796 final Parcelable result = data.getParcelableExtra(EXTRA_AUTHENTICATION_RESULT);
797 final Bundle responseData = new Bundle();
798 responseData.putParcelable(EXTRA_AUTHENTICATION_RESULT, result);
799 try {
Svetoslav Ganova9379d02017-05-09 17:40:24 -0700800 mService.setAuthenticationResult(responseData, mSessionId, authenticationId,
801 mContext.getUserId());
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700802 } catch (RemoteException e) {
803 Log.e(TAG, "Error delivering authentication result", e);
804 }
Svet Ganov782043c2017-02-11 00:52:02 +0000805 }
Felipe Lemebab851c2017-02-03 18:45:08 -0800806 }
807
Felipe Leme640f30a2017-03-06 15:44:06 -0800808 private static AutofillId getAutofillId(View view) {
Phil Weaver846cda932017-06-15 10:10:06 -0700809 return new AutofillId(view.getAutofillViewId());
Felipe Leme0200d9e2017-01-24 15:10:26 -0800810 }
811
Felipe Leme6dcec872017-05-25 11:24:23 -0700812 private static AutofillId getAutofillId(View parent, int virtualId) {
Phil Weaver846cda932017-06-15 10:10:06 -0700813 return new AutofillId(parent.getAutofillViewId(), virtualId);
Felipe Lemebab851c2017-02-03 18:45:08 -0800814 }
815
Philip P. Moltmann134cee22017-05-06 11:28:38 -0700816 private void startSessionLocked(@NonNull AutofillId id, @NonNull Rect bounds,
817 @NonNull AutofillValue value, int flags) {
Felipe Leme9f9ee252017-04-27 13:56:22 -0700818 if (sVerbose) {
819 Log.v(TAG, "startSessionLocked(): id=" + id + ", bounds=" + bounds + ", value=" + value
Felipe Leme2ac463e2017-03-13 14:06:25 -0700820 + ", flags=" + flags);
Felipe Lemebab851c2017-02-03 18:45:08 -0800821 }
Felipe Lemee6010f22017-03-03 11:19:51 -0800822
Felipe Lemebab851c2017-02-03 18:45:08 -0800823 try {
Philip P. Moltmann134cee22017-05-06 11:28:38 -0700824 mSessionId = mService.startSession(mContext.getActivityToken(),
Felipe Lemee6010f22017-03-03 11:19:51 -0800825 mServiceClient.asBinder(), id, bounds, value, mContext.getUserId(),
Philip P. Moltmann7b771162017-03-03 17:22:57 -0800826 mCallback != null, flags, mContext.getOpPackageName());
Felipe Leme0aa4c502017-04-26 12:36:01 -0700827 final AutofillClient client = getClientLocked();
Svet Ganov17db9dc2017-02-21 19:54:31 -0800828 if (client != null) {
Felipe Leme4753bb02017-03-22 20:24:00 -0700829 client.autofillCallbackResetableStateAvailable();
Svet Ganov17db9dc2017-02-21 19:54:31 -0800830 }
Svet Ganov782043c2017-02-11 00:52:02 +0000831 } catch (RemoteException e) {
832 throw e.rethrowFromSystemServer();
833 }
834 }
835
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700836 private void finishSessionLocked() {
Felipe Leme9f9ee252017-04-27 13:56:22 -0700837 if (sVerbose) Log.v(TAG, "finishSessionLocked()");
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700838
Svet Ganov782043c2017-02-11 00:52:02 +0000839 try {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700840 mService.finishSession(mSessionId, mContext.getUserId());
Felipe Lemebab851c2017-02-03 18:45:08 -0800841 } catch (RemoteException e) {
842 throw e.rethrowFromSystemServer();
843 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700844
Philip P. Moltmann494c3f52017-04-11 10:13:33 -0700845 mTrackedViews = null;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700846 mSessionId = NO_SESSION;
Felipe Lemebab851c2017-02-03 18:45:08 -0800847 }
848
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700849 private void cancelSessionLocked() {
Felipe Leme9f9ee252017-04-27 13:56:22 -0700850 if (sVerbose) Log.v(TAG, "cancelSessionLocked()");
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700851
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700852 try {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700853 mService.cancelSession(mSessionId, mContext.getUserId());
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700854 } catch (RemoteException e) {
855 throw e.rethrowFromSystemServer();
856 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700857
Svet Ganov48f10a22017-04-26 18:49:30 -0700858 resetSessionLocked();
859 }
860
861 private void resetSessionLocked() {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700862 mSessionId = NO_SESSION;
Svet Ganov48f10a22017-04-26 18:49:30 -0700863 mTrackedViews = null;
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700864 }
865
Felipe Leme0aa4c502017-04-26 12:36:01 -0700866 private void updateSessionLocked(AutofillId id, Rect bounds, AutofillValue value, int action,
867 int flags) {
Felipe Leme9f9ee252017-04-27 13:56:22 -0700868 if (sVerbose && action != ACTION_VIEW_EXITED) {
869 Log.v(TAG, "updateSessionLocked(): id=" + id + ", bounds=" + bounds
870 + ", value=" + value + ", action=" + action + ", flags=" + flags);
Felipe Lemebab851c2017-02-03 18:45:08 -0800871 }
Felipe Leme0ab53dc2017-02-23 08:33:18 -0800872
Felipe Leme7f33cd32017-05-11 10:10:49 -0700873 boolean restartIfNecessary = (flags & FLAG_MANUAL_REQUEST) != 0;
874
Felipe Leme3461d3c2017-01-19 08:54:55 -0800875 try {
Felipe Leme7f33cd32017-05-11 10:10:49 -0700876 if (restartIfNecessary) {
877 final int newId = mService.updateOrRestartSession(mContext.getActivityToken(),
878 mServiceClient.asBinder(), id, bounds, value, mContext.getUserId(),
879 mCallback != null, flags, mContext.getOpPackageName(), mSessionId, action);
880 if (newId != mSessionId) {
881 if (sDebug) Log.d(TAG, "Session restarted: " + mSessionId + "=>" + newId);
882 mSessionId = newId;
883 final AutofillClient client = getClientLocked();
884 if (client != null) {
885 client.autofillCallbackResetableStateAvailable();
886 }
887 }
888 } else {
889 mService.updateSession(mSessionId, id, bounds, value, action, flags,
890 mContext.getUserId());
891 }
892
Felipe Leme3461d3c2017-01-19 08:54:55 -0800893 } catch (RemoteException e) {
894 throw e.rethrowFromSystemServer();
895 }
896 }
Svet Ganov782043c2017-02-11 00:52:02 +0000897
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700898 private void ensureServiceClientAddedIfNeededLocked() {
899 if (getClientLocked() == null) {
Svet Ganov782043c2017-02-11 00:52:02 +0000900 return;
901 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700902
Svet Ganov782043c2017-02-11 00:52:02 +0000903 if (mServiceClient == null) {
Felipe Leme640f30a2017-03-06 15:44:06 -0800904 mServiceClient = new AutofillManagerClient(this);
Svet Ganov782043c2017-02-11 00:52:02 +0000905 try {
Felipe Leme9f9ee252017-04-27 13:56:22 -0700906 final int flags = mService.addClient(mServiceClient, mContext.getUserId());
907 mEnabled = (flags & FLAG_ADD_CLIENT_ENABLED) != 0;
908 sDebug = (flags & FLAG_ADD_CLIENT_DEBUG) != 0;
909 sVerbose = (flags & FLAG_ADD_CLIENT_VERBOSE) != 0;
Svet Ganov782043c2017-02-11 00:52:02 +0000910 } catch (RemoteException e) {
911 throw e.rethrowFromSystemServer();
912 }
913 }
914 }
915
Felipe Lemee6010f22017-03-03 11:19:51 -0800916 /**
917 * Registers a {@link AutofillCallback} to receive autofill events.
918 *
919 * @param callback callback to receive events.
920 */
921 public void registerCallback(@Nullable AutofillCallback callback) {
Svet Ganov43574b02017-04-12 09:25:20 -0700922 if (!hasAutofillFeature()) {
923 return;
924 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700925 synchronized (mLock) {
926 if (callback == null) return;
Felipe Lemee6010f22017-03-03 11:19:51 -0800927
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700928 final boolean hadCallback = mCallback != null;
929 mCallback = callback;
Felipe Lemee6010f22017-03-03 11:19:51 -0800930
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700931 if (!hadCallback) {
932 try {
933 mService.setHasCallback(mSessionId, mContext.getUserId(), true);
934 } catch (RemoteException e) {
935 throw e.rethrowFromSystemServer();
936 }
Felipe Lemee6010f22017-03-03 11:19:51 -0800937 }
938 }
939 }
940
941 /**
942 * Unregisters a {@link AutofillCallback} to receive autofill events.
943 *
944 * @param callback callback to stop receiving events.
945 */
946 public void unregisterCallback(@Nullable AutofillCallback callback) {
Svet Ganov43574b02017-04-12 09:25:20 -0700947 if (!hasAutofillFeature()) {
948 return;
949 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700950 synchronized (mLock) {
951 if (callback == null || mCallback == null || callback != mCallback) return;
Felipe Lemee6010f22017-03-03 11:19:51 -0800952
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700953 mCallback = null;
Felipe Lemee6010f22017-03-03 11:19:51 -0800954
Felipe Lemee6010f22017-03-03 11:19:51 -0800955 try {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700956 mService.setHasCallback(mSessionId, mContext.getUserId(), false);
Felipe Lemee6010f22017-03-03 11:19:51 -0800957 } catch (RemoteException e) {
958 throw e.rethrowFromSystemServer();
959 }
960 }
961 }
962
Philip P. Moltmann134cee22017-05-06 11:28:38 -0700963 private void requestShowFillUi(int sessionId, AutofillId id, int width, int height,
964 Rect anchorBounds, IAutofillWindowPresenter presenter) {
965 final View anchor = findView(id);
Felipe Leme4753bb02017-03-22 20:24:00 -0700966 if (anchor == null) {
967 return;
968 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700969
970 AutofillCallback callback = null;
971 synchronized (mLock) {
Philip P. Moltmanne048a652017-04-11 10:13:33 -0700972 if (mSessionId == sessionId) {
973 AutofillClient client = getClientLocked();
974
975 if (client != null) {
976 if (client.autofillCallbackRequestShowFillUi(anchor, width, height,
977 anchorBounds, presenter) && mCallback != null) {
978 callback = mCallback;
979 }
980 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700981 }
982 }
983
984 if (callback != null) {
Felipe Leme4753bb02017-03-22 20:24:00 -0700985 if (id.isVirtual()) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700986 callback.onAutofillEvent(anchor, id.getVirtualChildId(),
Felipe Leme4753bb02017-03-22 20:24:00 -0700987 AutofillCallback.EVENT_INPUT_SHOWN);
988 } else {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700989 callback.onAutofillEvent(anchor, AutofillCallback.EVENT_INPUT_SHOWN);
Felipe Leme4753bb02017-03-22 20:24:00 -0700990 }
991 }
992 }
993
Svetoslav Ganova9379d02017-05-09 17:40:24 -0700994 private void authenticate(int sessionId, int authenticationId, IntentSender intent,
995 Intent fillInIntent) {
Philip P. Moltmanne048a652017-04-11 10:13:33 -0700996 synchronized (mLock) {
997 if (sessionId == mSessionId) {
998 AutofillClient client = getClientLocked();
999 if (client != null) {
Svetoslav Ganova9379d02017-05-09 17:40:24 -07001000 client.autofillCallbackAuthenticate(authenticationId, intent, fillInIntent);
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001001 }
1002 }
1003 }
1004 }
1005
Svet Ganov48f10a22017-04-26 18:49:30 -07001006 private void setState(boolean enabled, boolean resetSession, boolean resetClient) {
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001007 synchronized (mLock) {
1008 mEnabled = enabled;
Svet Ganov48f10a22017-04-26 18:49:30 -07001009 if (!mEnabled || resetSession) {
1010 // Reset the session state
1011 resetSessionLocked();
1012 }
1013 if (resetClient) {
1014 // Reset connection to system
1015 mServiceClient = null;
1016 }
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001017 }
1018 }
1019
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07001020 /**
1021 * Sets a view as autofilled if the current value is the {code targetValue}.
1022 *
1023 * @param view The view that is to be autofilled
1024 * @param targetValue The value we want to fill into view
1025 */
1026 private void setAutofilledIfValuesIs(@NonNull View view, @Nullable AutofillValue targetValue) {
1027 AutofillValue currentValue = view.getAutofillValue();
1028 if (Objects.equals(currentValue, targetValue)) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001029 synchronized (mLock) {
1030 if (mLastAutofilledData == null) {
1031 mLastAutofilledData = new ParcelableMap(1);
1032 }
1033 mLastAutofilledData.put(getAutofillId(view), targetValue);
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07001034 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07001035 view.setAutofilled(true);
1036 }
1037 }
1038
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001039 private void autofill(int sessionId, List<AutofillId> ids, List<AutofillValue> values) {
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001040 synchronized (mLock) {
1041 if (sessionId != mSessionId) {
1042 return;
Felipe Leme4753bb02017-03-22 20:24:00 -07001043 }
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001044
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001045 final AutofillClient client = getClientLocked();
1046 if (client == null) {
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001047 return;
1048 }
1049
1050 final int itemCount = ids.size();
1051 int numApplied = 0;
1052 ArrayMap<View, SparseArray<AutofillValue>> virtualValues = null;
Phil Weaver846cda932017-06-15 10:10:06 -07001053 final View[] views = client.findViewsByAutofillIdTraversal(getViewIds(ids));
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001054
1055 for (int i = 0; i < itemCount; i++) {
1056 final AutofillId id = ids.get(i);
1057 final AutofillValue value = values.get(i);
1058 final int viewId = id.getViewId();
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001059 final View view = views[i];
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001060 if (view == null) {
1061 Log.w(TAG, "autofill(): no View with id " + viewId);
1062 continue;
Felipe Leme4753bb02017-03-22 20:24:00 -07001063 }
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001064 if (id.isVirtual()) {
1065 if (virtualValues == null) {
1066 // Most likely there will be just one view with virtual children.
1067 virtualValues = new ArrayMap<>(1);
1068 }
1069 SparseArray<AutofillValue> valuesByParent = virtualValues.get(view);
1070 if (valuesByParent == null) {
1071 // We don't know the size yet, but usually it will be just a few fields...
1072 valuesByParent = new SparseArray<>(5);
1073 virtualValues.put(view, valuesByParent);
1074 }
1075 valuesByParent.put(id.getVirtualChildId(), value);
1076 } else {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001077 // Mark the view as to be autofilled with 'value'
1078 if (mLastAutofilledData == null) {
1079 mLastAutofilledData = new ParcelableMap(itemCount - i);
1080 }
1081 mLastAutofilledData.put(id, value);
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001082
1083 view.autofill(value);
1084
1085 // Set as autofilled if the values match now, e.g. when the value was updated
1086 // synchronously.
1087 // If autofill happens async, the view is set to autofilled in
1088 // notifyValueChanged.
1089 setAutofilledIfValuesIs(view, value);
1090
1091 numApplied++;
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07001092 }
Felipe Leme4753bb02017-03-22 20:24:00 -07001093 }
Felipe Leme4753bb02017-03-22 20:24:00 -07001094
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001095 if (virtualValues != null) {
1096 for (int i = 0; i < virtualValues.size(); i++) {
1097 final View parent = virtualValues.keyAt(i);
1098 final SparseArray<AutofillValue> childrenValues = virtualValues.valueAt(i);
1099 parent.autofill(childrenValues);
1100 numApplied += childrenValues.size();
1101 }
Felipe Leme4753bb02017-03-22 20:24:00 -07001102 }
Felipe Leme4753bb02017-03-22 20:24:00 -07001103
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001104 final LogMaker log = new LogMaker(MetricsProto.MetricsEvent.AUTOFILL_DATASET_APPLIED);
1105 log.addTaggedData(MetricsProto.MetricsEvent.FIELD_AUTOFILL_NUM_VALUES, itemCount);
1106 log.addTaggedData(MetricsProto.MetricsEvent.FIELD_AUTOFILL_NUM_VIEWS_FILLED,
1107 numApplied);
1108 mMetricsLogger.write(log);
1109 }
Felipe Leme4753bb02017-03-22 20:24:00 -07001110 }
1111
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001112 /**
1113 * Set the tracked views.
1114 *
1115 * @param trackedIds The views to be tracked
1116 * @param saveOnAllViewsInvisible Finish the session once all tracked views are invisible.
Felipe Leme27e20222017-05-18 15:24:11 -07001117 * @param fillableIds Views that might anchor FillUI.
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001118 */
Felipe Leme27e20222017-05-18 15:24:11 -07001119 private void setTrackedViews(int sessionId, @Nullable AutofillId[] trackedIds,
1120 boolean saveOnAllViewsInvisible, @Nullable AutofillId[] fillableIds) {
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001121 synchronized (mLock) {
1122 if (mEnabled && mSessionId == sessionId) {
1123 if (saveOnAllViewsInvisible) {
1124 mTrackedViews = new TrackedViews(trackedIds);
1125 } else {
1126 mTrackedViews = null;
1127 }
Felipe Leme27e20222017-05-18 15:24:11 -07001128 if (fillableIds != null) {
1129 if (mFillableIds == null) {
1130 mFillableIds = new ArraySet<>(fillableIds.length);
1131 }
1132 for (AutofillId id : fillableIds) {
1133 mFillableIds.add(id);
1134 }
1135 if (sVerbose) {
1136 Log.v(TAG, "setTrackedViews(): fillableIds=" + fillableIds
1137 + ", mFillableIds" + mFillableIds);
1138 }
1139 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001140 }
1141 }
1142 }
1143
Felipe Leme27e20222017-05-18 15:24:11 -07001144 private void requestHideFillUi(AutofillId id) {
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001145 final View anchor = findView(id);
Felipe Leme27e20222017-05-18 15:24:11 -07001146 if (sVerbose) Log.v(TAG, "requestHideFillUi(" + id + "): anchor = " + anchor);
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001147 if (anchor == null) {
1148 return;
1149 }
Felipe Leme27e20222017-05-18 15:24:11 -07001150 requestHideFillUi(id, anchor);
1151 }
1152
1153 private void requestHideFillUi(AutofillId id, View anchor) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001154
1155 AutofillCallback callback = null;
1156 synchronized (mLock) {
Svet Ganov48f10a22017-04-26 18:49:30 -07001157 // We do not check the session id for two reasons:
1158 // 1. If local and remote session id are off sync the UI would be stuck shown
1159 // 2. There is a race between the user state being destroyed due the fill
1160 // service being uninstalled and the UI being dismissed.
1161 AutofillClient client = getClientLocked();
1162 if (client != null) {
1163 if (client.autofillCallbackRequestHideFillUi() && mCallback != null) {
1164 callback = mCallback;
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001165 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001166 }
1167 }
1168
1169 if (callback != null) {
Felipe Leme4753bb02017-03-22 20:24:00 -07001170 if (id.isVirtual()) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001171 callback.onAutofillEvent(anchor, id.getVirtualChildId(),
Felipe Leme4753bb02017-03-22 20:24:00 -07001172 AutofillCallback.EVENT_INPUT_HIDDEN);
1173 } else {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001174 callback.onAutofillEvent(anchor, AutofillCallback.EVENT_INPUT_HIDDEN);
Felipe Leme4753bb02017-03-22 20:24:00 -07001175 }
1176 }
1177 }
1178
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001179 private void notifyNoFillUi(int sessionId, AutofillId id) {
1180 final View anchor = findView(id);
1181 if (anchor == null) {
1182 return;
1183 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001184
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001185 AutofillCallback callback = null;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001186 synchronized (mLock) {
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001187 if (mSessionId == sessionId && getClientLocked() != null) {
1188 callback = mCallback;
1189 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001190 }
1191
1192 if (callback != null) {
Felipe Leme4753bb02017-03-22 20:24:00 -07001193 if (id.isVirtual()) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001194 callback.onAutofillEvent(anchor, id.getVirtualChildId(),
Felipe Leme4753bb02017-03-22 20:24:00 -07001195 AutofillCallback.EVENT_INPUT_UNAVAILABLE);
1196 } else {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001197 callback.onAutofillEvent(anchor, AutofillCallback.EVENT_INPUT_UNAVAILABLE);
Felipe Leme4753bb02017-03-22 20:24:00 -07001198 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001199
Felipe Leme4753bb02017-03-22 20:24:00 -07001200 }
1201 }
1202
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001203 /**
1204 * Get an array of viewIds from a List of {@link AutofillId}.
1205 *
1206 * @param autofillIds The autofill ids to convert
1207 *
1208 * @return The array of viewIds.
1209 */
Felipe Leme27e20222017-05-18 15:24:11 -07001210 // TODO: move to Helper as static method
1211 @NonNull private int[] getViewIds(@NonNull AutofillId[] autofillIds) {
1212 final int numIds = autofillIds.length;
1213 final int[] viewIds = new int[numIds];
1214 for (int i = 0; i < numIds; i++) {
1215 viewIds[i] = autofillIds[i].getViewId();
1216 }
1217
1218 return viewIds;
1219 }
1220
1221 // TODO: move to Helper as static method
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001222 @NonNull private int[] getViewIds(@NonNull List<AutofillId> autofillIds) {
1223 final int numIds = autofillIds.size();
1224 final int[] viewIds = new int[numIds];
1225 for (int i = 0; i < numIds; i++) {
1226 viewIds[i] = autofillIds.get(i).getViewId();
1227 }
1228
1229 return viewIds;
1230 }
1231
1232 /**
1233 * Find a single view by its id.
1234 *
1235 * @param autofillId The autofill id of the view
1236 *
1237 * @return The view or {@code null} if view was not found
1238 */
1239 private View findView(@NonNull AutofillId autofillId) {
1240 final AutofillClient client = getClientLocked();
1241
1242 if (client == null) {
Felipe Leme4753bb02017-03-22 20:24:00 -07001243 return null;
Felipe Lemee6010f22017-03-03 11:19:51 -08001244 }
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001245
Phil Weaver846cda932017-06-15 10:10:06 -07001246 return client.findViewByAutofillIdTraversal(autofillId.getViewId());
Felipe Lemee6010f22017-03-03 11:19:51 -08001247 }
1248
Felipe Lemebb810922017-04-25 15:54:06 -07001249 /** @hide */
1250 public boolean hasAutofillFeature() {
Svet Ganov43574b02017-04-12 09:25:20 -07001251 return mService != null;
1252 }
1253
Felipe Leme9876a6f2017-05-30 15:47:28 -07001254 private void post(Runnable runnable) {
1255 final AutofillClient client = getClientLocked();
1256 if (client == null) {
1257 if (sVerbose) Log.v(TAG, "ignoring post() because client is null");
1258 return;
1259 }
1260 client.runOnUiThread(runnable);
1261 }
1262
Felipe Lemee6010f22017-03-03 11:19:51 -08001263 /**
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001264 * View tracking information. Once all tracked views become invisible the session is finished.
1265 */
1266 private class TrackedViews {
1267 /** Visible tracked views */
1268 @Nullable private ArraySet<AutofillId> mVisibleTrackedIds;
1269
1270 /** Invisible tracked views */
1271 @Nullable private ArraySet<AutofillId> mInvisibleTrackedIds;
1272
1273 /**
1274 * Check if set is null or value is in set.
1275 *
1276 * @param set The set or null (== empty set)
1277 * @param value The value that might be in the set
1278 *
1279 * @return {@code true} iff set is not empty and value is in set
1280 */
Felipe Leme27e20222017-05-18 15:24:11 -07001281 // TODO: move to Helper as static method
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001282 private <T> boolean isInSet(@Nullable ArraySet<T> set, T value) {
1283 return set != null && set.contains(value);
1284 }
1285
1286 /**
1287 * Add a value to a set. If set is null, create a new set.
1288 *
1289 * @param set The set or null (== empty set)
1290 * @param valueToAdd The value to add
1291 *
1292 * @return The set including the new value. If set was {@code null}, a set containing only
1293 * the new value.
1294 */
Felipe Leme27e20222017-05-18 15:24:11 -07001295 // TODO: move to Helper as static method
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001296 @NonNull
1297 private <T> ArraySet<T> addToSet(@Nullable ArraySet<T> set, T valueToAdd) {
1298 if (set == null) {
1299 set = new ArraySet<>(1);
1300 }
1301
1302 set.add(valueToAdd);
1303
1304 return set;
1305 }
1306
1307 /**
1308 * Remove a value from a set.
1309 *
1310 * @param set The set or null (== empty set)
1311 * @param valueToRemove The value to remove
1312 *
1313 * @return The set without the removed value. {@code null} if set was null, or is empty
1314 * after removal.
1315 */
Felipe Leme27e20222017-05-18 15:24:11 -07001316 // TODO: move to Helper as static method
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001317 @Nullable
1318 private <T> ArraySet<T> removeFromSet(@Nullable ArraySet<T> set, T valueToRemove) {
1319 if (set == null) {
1320 return null;
1321 }
1322
1323 set.remove(valueToRemove);
1324
1325 if (set.isEmpty()) {
1326 return null;
1327 }
1328
1329 return set;
1330 }
1331
1332 /**
1333 * Set the tracked views.
1334 *
1335 * @param trackedIds The views to be tracked
1336 */
Felipe Leme27e20222017-05-18 15:24:11 -07001337 TrackedViews(@Nullable AutofillId[] trackedIds) {
1338 final AutofillClient client = getClientLocked();
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001339 if (trackedIds != null && client != null) {
1340 final boolean[] isVisible;
1341
1342 if (client.isVisibleForAutofill()) {
1343 isVisible = client.getViewVisibility(getViewIds(trackedIds));
1344 } else {
1345 // All false
Felipe Leme27e20222017-05-18 15:24:11 -07001346 isVisible = new boolean[trackedIds.length];
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001347 }
1348
Felipe Leme27e20222017-05-18 15:24:11 -07001349 final int numIds = trackedIds.length;
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001350 for (int i = 0; i < numIds; i++) {
Felipe Leme27e20222017-05-18 15:24:11 -07001351 final AutofillId id = trackedIds[i];
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001352
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001353 if (isVisible[i]) {
Philip P. Moltmanne0e28712017-04-20 15:19:06 -07001354 mVisibleTrackedIds = addToSet(mVisibleTrackedIds, id);
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001355 } else {
1356 mInvisibleTrackedIds = addToSet(mInvisibleTrackedIds, id);
1357 }
1358 }
1359 }
1360
Felipe Leme9f9ee252017-04-27 13:56:22 -07001361 if (sVerbose) {
1362 Log.v(TAG, "TrackedViews(trackedIds=" + trackedIds + "): "
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001363 + " mVisibleTrackedIds=" + mVisibleTrackedIds
1364 + " mInvisibleTrackedIds=" + mInvisibleTrackedIds);
1365 }
1366
1367 if (mVisibleTrackedIds == null) {
1368 finishSessionLocked();
1369 }
1370 }
1371
1372 /**
1373 * Called when a {@link View view's} visibility changes.
1374 *
1375 * @param view {@link View} that was exited.
1376 * @param isVisible visible if the view is visible in the view hierarchy.
1377 */
1378 void notifyViewVisibilityChange(@NonNull View view, boolean isVisible) {
1379 AutofillId id = getAutofillId(view);
1380 AutofillClient client = getClientLocked();
1381
Felipe Leme9f9ee252017-04-27 13:56:22 -07001382 if (sDebug) {
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001383 Log.d(TAG, "notifyViewVisibilityChange(): id=" + id + " isVisible="
1384 + isVisible);
1385 }
1386
1387 if (client != null && client.isVisibleForAutofill()) {
1388 if (isVisible) {
1389 if (isInSet(mInvisibleTrackedIds, id)) {
1390 mInvisibleTrackedIds = removeFromSet(mInvisibleTrackedIds, id);
1391 mVisibleTrackedIds = addToSet(mVisibleTrackedIds, id);
1392 }
1393 } else {
1394 if (isInSet(mVisibleTrackedIds, id)) {
1395 mVisibleTrackedIds = removeFromSet(mVisibleTrackedIds, id);
1396 mInvisibleTrackedIds = addToSet(mInvisibleTrackedIds, id);
1397 }
1398 }
1399 }
1400
1401 if (mVisibleTrackedIds == null) {
Felipe Leme27e20222017-05-18 15:24:11 -07001402 if (sVerbose) {
1403 Log.v(TAG, "No more visible ids. Invisibile = " + mInvisibleTrackedIds);
1404 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001405 finishSessionLocked();
1406 }
1407 }
1408
1409 /**
1410 * Called once the client becomes visible.
1411 *
1412 * @see AutofillClient#isVisibleForAutofill()
1413 */
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001414 void onVisibleForAutofillLocked() {
1415 // The visibility of the views might have changed while the client was not be visible,
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001416 // hence update the visibility state for all views.
1417 AutofillClient client = getClientLocked();
1418 ArraySet<AutofillId> updatedVisibleTrackedIds = null;
1419 ArraySet<AutofillId> updatedInvisibleTrackedIds = null;
1420 if (client != null) {
1421 if (mInvisibleTrackedIds != null) {
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001422 final ArrayList<AutofillId> orderedInvisibleIds =
1423 new ArrayList<>(mInvisibleTrackedIds);
1424 final boolean[] isVisible = client.getViewVisibility(
1425 getViewIds(orderedInvisibleIds));
1426
1427 final int numInvisibleTrackedIds = orderedInvisibleIds.size();
1428 for (int i = 0; i < numInvisibleTrackedIds; i++) {
1429 final AutofillId id = orderedInvisibleIds.get(i);
1430 if (isVisible[i]) {
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001431 updatedVisibleTrackedIds = addToSet(updatedVisibleTrackedIds, id);
1432
Felipe Leme9f9ee252017-04-27 13:56:22 -07001433 if (sDebug) {
1434 Log.d(TAG, "onVisibleForAutofill() " + id + " became visible");
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001435 }
1436 } else {
1437 updatedInvisibleTrackedIds = addToSet(updatedInvisibleTrackedIds, id);
1438 }
1439 }
1440 }
1441
1442 if (mVisibleTrackedIds != null) {
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001443 final ArrayList<AutofillId> orderedVisibleIds =
1444 new ArrayList<>(mVisibleTrackedIds);
1445 final boolean[] isVisible = client.getViewVisibility(
1446 getViewIds(orderedVisibleIds));
1447
1448 final int numVisibleTrackedIds = orderedVisibleIds.size();
1449 for (int i = 0; i < numVisibleTrackedIds; i++) {
1450 final AutofillId id = orderedVisibleIds.get(i);
1451
1452 if (isVisible[i]) {
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001453 updatedVisibleTrackedIds = addToSet(updatedVisibleTrackedIds, id);
1454 } else {
1455 updatedInvisibleTrackedIds = addToSet(updatedInvisibleTrackedIds, id);
1456
Felipe Leme9f9ee252017-04-27 13:56:22 -07001457 if (sDebug) {
1458 Log.d(TAG, "onVisibleForAutofill() " + id + " became invisible");
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001459 }
1460 }
1461 }
1462 }
1463
1464 mInvisibleTrackedIds = updatedInvisibleTrackedIds;
1465 mVisibleTrackedIds = updatedVisibleTrackedIds;
1466 }
1467
1468 if (mVisibleTrackedIds == null) {
1469 finishSessionLocked();
1470 }
1471 }
1472 }
1473
1474 /**
Felipe Lemee6010f22017-03-03 11:19:51 -08001475 * Callback for auto-fill related events.
1476 *
1477 * <p>Typically used for applications that display their own "auto-complete" views, so they can
1478 * enable / disable such views when the auto-fill UI affordance is shown / hidden.
1479 */
1480 public abstract static class AutofillCallback {
1481
1482 /** @hide */
1483 @IntDef({EVENT_INPUT_SHOWN, EVENT_INPUT_HIDDEN})
1484 @Retention(RetentionPolicy.SOURCE)
1485 public @interface AutofillEventType {}
1486
1487 /**
1488 * The auto-fill input UI affordance associated with the view was shown.
1489 *
1490 * <p>If the view provides its own auto-complete UI affordance and its currently shown, it
1491 * should be hidden upon receiving this event.
1492 */
1493 public static final int EVENT_INPUT_SHOWN = 1;
1494
1495 /**
1496 * The auto-fill input UI affordance associated with the view was hidden.
1497 *
1498 * <p>If the view provides its own auto-complete UI affordance that was hidden upon a
1499 * {@link #EVENT_INPUT_SHOWN} event, it could be shown again now.
1500 */
1501 public static final int EVENT_INPUT_HIDDEN = 2;
1502
1503 /**
Felipe Leme24aae152017-03-15 12:33:01 -07001504 * The auto-fill input UI affordance associated with the view won't be shown because
1505 * autofill is not available.
1506 *
1507 * <p>If the view provides its own auto-complete UI affordance but was not displaying it
1508 * to avoid flickering, it could shown it upon receiving this event.
1509 */
1510 public static final int EVENT_INPUT_UNAVAILABLE = 3;
1511
1512 /**
Felipe Lemee6010f22017-03-03 11:19:51 -08001513 * Called after a change in the autofill state associated with a view.
1514 *
1515 * @param view view associated with the change.
1516 *
1517 * @param event currently either {@link #EVENT_INPUT_SHOWN} or {@link #EVENT_INPUT_HIDDEN}.
1518 */
Felipe Leme81f01d92017-03-16 17:13:25 -07001519 public void onAutofillEvent(@NonNull View view, @AutofillEventType int event) {
1520 }
Felipe Lemee6010f22017-03-03 11:19:51 -08001521
1522 /**
1523 * Called after a change in the autofill state associated with a virtual view.
1524 *
1525 * @param view parent view associated with the change.
Felipe Leme6dcec872017-05-25 11:24:23 -07001526 * @param virtualId id identifying the virtual child inside the parent view.
Felipe Lemee6010f22017-03-03 11:19:51 -08001527 *
1528 * @param event currently either {@link #EVENT_INPUT_SHOWN} or {@link #EVENT_INPUT_HIDDEN}.
1529 */
Felipe Leme6dcec872017-05-25 11:24:23 -07001530 public void onAutofillEvent(@NonNull View view, int virtualId,
1531 @AutofillEventType int event) {
Felipe Leme81f01d92017-03-16 17:13:25 -07001532 }
Felipe Lemee6010f22017-03-03 11:19:51 -08001533 }
1534
Felipe Leme640f30a2017-03-06 15:44:06 -08001535 private static final class AutofillManagerClient extends IAutoFillManagerClient.Stub {
1536 private final WeakReference<AutofillManager> mAfm;
Svet Ganov782043c2017-02-11 00:52:02 +00001537
Felipe Leme640f30a2017-03-06 15:44:06 -08001538 AutofillManagerClient(AutofillManager autofillManager) {
1539 mAfm = new WeakReference<>(autofillManager);
Svet Ganov782043c2017-02-11 00:52:02 +00001540 }
1541
1542 @Override
Svet Ganov48f10a22017-04-26 18:49:30 -07001543 public void setState(boolean enabled, boolean resetSession, boolean resetClient) {
Felipe Leme640f30a2017-03-06 15:44:06 -08001544 final AutofillManager afm = mAfm.get();
1545 if (afm != null) {
Felipe Leme9876a6f2017-05-30 15:47:28 -07001546 afm.post(() -> afm.setState(enabled, resetSession, resetClient));
Svet Ganov782043c2017-02-11 00:52:02 +00001547 }
1548 }
1549
1550 @Override
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001551 public void autofill(int sessionId, List<AutofillId> ids, List<AutofillValue> values) {
Felipe Leme640f30a2017-03-06 15:44:06 -08001552 final AutofillManager afm = mAfm.get();
1553 if (afm != null) {
Felipe Leme9876a6f2017-05-30 15:47:28 -07001554 afm.post(() -> afm.autofill(sessionId, ids, values));
Svet Ganov782043c2017-02-11 00:52:02 +00001555 }
1556 }
1557
1558 @Override
Svetoslav Ganova9379d02017-05-09 17:40:24 -07001559 public void authenticate(int sessionId, int authenticationId, IntentSender intent,
1560 Intent fillInIntent) {
Felipe Leme640f30a2017-03-06 15:44:06 -08001561 final AutofillManager afm = mAfm.get();
1562 if (afm != null) {
Felipe Leme9876a6f2017-05-30 15:47:28 -07001563 afm.post(() -> afm.authenticate(sessionId, authenticationId, intent, fillInIntent));
Svet Ganov782043c2017-02-11 00:52:02 +00001564 }
1565 }
Felipe Lemee6010f22017-03-03 11:19:51 -08001566
1567 @Override
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001568 public void requestShowFillUi(int sessionId, AutofillId id, int width, int height,
1569 Rect anchorBounds, IAutofillWindowPresenter presenter) {
Felipe Leme640f30a2017-03-06 15:44:06 -08001570 final AutofillManager afm = mAfm.get();
1571 if (afm != null) {
Felipe Leme9876a6f2017-05-30 15:47:28 -07001572 afm.post(() -> afm.requestShowFillUi(sessionId, id, width, height, anchorBounds,
1573 presenter));
Felipe Leme4753bb02017-03-22 20:24:00 -07001574 }
1575 }
1576
1577 @Override
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001578 public void requestHideFillUi(int sessionId, AutofillId id) {
Felipe Leme4753bb02017-03-22 20:24:00 -07001579 final AutofillManager afm = mAfm.get();
1580 if (afm != null) {
Felipe Leme9876a6f2017-05-30 15:47:28 -07001581 afm.post(() -> afm.requestHideFillUi(id));
Felipe Leme4753bb02017-03-22 20:24:00 -07001582 }
1583 }
1584
1585 @Override
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001586 public void notifyNoFillUi(int sessionId, AutofillId id) {
Felipe Leme4753bb02017-03-22 20:24:00 -07001587 final AutofillManager afm = mAfm.get();
1588 if (afm != null) {
Felipe Leme9876a6f2017-05-30 15:47:28 -07001589 afm.post(() -> afm.notifyNoFillUi(sessionId, id));
Felipe Lemee6010f22017-03-03 11:19:51 -08001590 }
1591 }
Svet Ganovc3d1c852017-04-12 22:34:28 -07001592
1593 @Override
1594 public void startIntentSender(IntentSender intentSender) {
1595 final AutofillManager afm = mAfm.get();
1596 if (afm != null) {
Felipe Leme9876a6f2017-05-30 15:47:28 -07001597 afm.post(() -> {
Svet Ganovc3d1c852017-04-12 22:34:28 -07001598 try {
1599 afm.mContext.startIntentSender(intentSender, null, 0, 0, 0);
1600 } catch (IntentSender.SendIntentException e) {
1601 Log.e(TAG, "startIntentSender() failed for intent:" + intentSender, e);
1602 }
1603 });
1604 }
1605 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001606
1607 @Override
Felipe Leme27e20222017-05-18 15:24:11 -07001608 public void setTrackedViews(int sessionId, AutofillId[] ids,
1609 boolean saveOnAllViewsInvisible, AutofillId[] fillableIds) {
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001610 final AutofillManager afm = mAfm.get();
1611 if (afm != null) {
Felipe Leme9876a6f2017-05-30 15:47:28 -07001612 afm.post(() ->
Felipe Leme27e20222017-05-18 15:24:11 -07001613 afm.setTrackedViews(sessionId, ids, saveOnAllViewsInvisible, fillableIds)
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001614 );
1615 }
1616 }
Svet Ganov782043c2017-02-11 00:52:02 +00001617 }
Felipe Leme3461d3c2017-01-19 08:54:55 -08001618}