blob: 6d84aa009c75e391642fe0b3ca1d45b15c4b2bf0 [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 Leme452886a2017-11-27 13:09:13 -080027import android.annotation.TestApi;
Felipe Leme17292d12017-10-24 14:03:10 -070028import android.content.ComponentName;
Felipe Leme3461d3c2017-01-19 08:54:55 -080029import android.content.Context;
Svet Ganov782043c2017-02-11 00:52:02 +000030import android.content.Intent;
31import android.content.IntentSender;
Felipe Leme3461d3c2017-01-19 08:54:55 -080032import android.graphics.Rect;
Felipe Leme4753bb02017-03-22 20:24:00 -070033import android.metrics.LogMaker;
Svet Ganov782043c2017-02-11 00:52:02 +000034import android.os.Bundle;
Felipe Lemec24a56a2017-08-03 14:27:57 -070035import android.os.IBinder;
Svet Ganov782043c2017-02-11 00:52:02 +000036import android.os.Parcelable;
Felipe Leme3461d3c2017-01-19 08:54:55 -080037import android.os.RemoteException;
Philip P. Moltmanncc684ed2017-04-17 14:18:33 -070038import android.service.autofill.AutofillService;
39import android.service.autofill.FillEventHistory;
Felipe Leme452886a2017-11-27 13:09:13 -080040import android.service.autofill.UserData;
Felipe Leme4753bb02017-03-22 20:24:00 -070041import android.util.ArrayMap;
Philip P. Moltmann494c3f52017-04-11 10:13:33 -070042import android.util.ArraySet;
Felipe Leme3461d3c2017-01-19 08:54:55 -080043import android.util.Log;
Felipe Leme4753bb02017-03-22 20:24:00 -070044import android.util.SparseArray;
Felipe Leme3461d3c2017-01-19 08:54:55 -080045import android.view.View;
46
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -070047import com.android.internal.annotations.GuardedBy;
Felipe Leme4753bb02017-03-22 20:24:00 -070048import com.android.internal.logging.MetricsLogger;
Felipe Lemeb22d6352017-09-08 20:03:53 -070049import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Felipe Leme637e05e2017-12-06 12:09:37 -080050import com.android.internal.util.Preconditions;
Felipe Leme4753bb02017-03-22 20:24:00 -070051
Felipe Lemec24a56a2017-08-03 14:27:57 -070052import java.io.PrintWriter;
Felipe Lemee6010f22017-03-03 11:19:51 -080053import java.lang.annotation.Retention;
54import java.lang.annotation.RetentionPolicy;
Svet Ganov782043c2017-02-11 00:52:02 +000055import java.lang.ref.WeakReference;
Philip P. Moltmann134cee22017-05-06 11:28:38 -070056import java.util.ArrayList;
Svet Ganov782043c2017-02-11 00:52:02 +000057import java.util.List;
Philip P. Moltmannb42d1332017-03-27 09:55:30 -070058import java.util.Objects;
Svet Ganov782043c2017-02-11 00:52:02 +000059
Koji Fukuiccec6a62017-10-18 17:48:40 +090060// TODO: use java.lang.ref.Cleaner once Android supports Java 9
61import sun.misc.Cleaner;
62
Felipe Leme3461d3c2017-01-19 08:54:55 -080063/**
Felipe Leme744976e2017-07-31 11:34:14 -070064 * The {@link AutofillManager} provides ways for apps and custom views to integrate with the
65 * Autofill Framework lifecycle.
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -070066 *
Felipe Leme744976e2017-07-31 11:34:14 -070067 * <p>The autofill lifecycle starts with the creation of an autofill context associated with an
68 * activity context; the autofill context is created when one of the following methods is called for
69 * the first time in an activity context, and the current user has an enabled autofill service:
70 *
71 * <ul>
72 * <li>{@link #notifyViewEntered(View)}
73 * <li>{@link #notifyViewEntered(View, int, Rect)}
74 * <li>{@link #requestAutofill(View)}
75 * </ul>
76 *
77 * <p>Tipically, the context is automatically created when the first view of the activity is
78 * focused because {@code View.onFocusChanged()} indirectly calls
79 * {@link #notifyViewEntered(View)}. App developers can call {@link #requestAutofill(View)} to
80 * explicitly create it (for example, a custom view developer could offer a contextual menu action
81 * in a text-field view to let users manually request autofill).
82 *
83 * <p>After the context is created, the Android System creates a {@link android.view.ViewStructure}
84 * that represents the view hierarchy by calling
85 * {@link View#dispatchProvideAutofillStructure(android.view.ViewStructure, int)} in the root views
86 * of all application windows. By default, {@code dispatchProvideAutofillStructure()} results in
87 * subsequent calls to {@link View#onProvideAutofillStructure(android.view.ViewStructure, int)} and
88 * {@link View#onProvideAutofillVirtualStructure(android.view.ViewStructure, int)} for each view in
89 * the hierarchy.
90 *
91 * <p>The resulting {@link android.view.ViewStructure} is then passed to the autofill service, which
92 * parses it looking for views that can be autofilled. If the service finds such views, it returns
93 * a data structure to the Android System containing the following optional info:
94 *
95 * <ul>
96 * <li>Datasets used to autofill subsets of views in the activity.
97 * <li>Id of views that the service can save their values for future autofilling.
98 * </ul>
99 *
100 * <p>When the service returns datasets, the Android System displays an autofill dataset picker
Felipe Leme2fe3ade2017-09-28 15:03:36 -0700101 * UI associated with the view, when the view is focused on and is part of a dataset.
102 * The application can be notified when the UI is shown by registering an
Felipe Leme744976e2017-07-31 11:34:14 -0700103 * {@link AutofillCallback} through {@link #registerCallback(AutofillCallback)}. When the user
Felipe Leme2fe3ade2017-09-28 15:03:36 -0700104 * selects a dataset from the UI, all views present in the dataset are autofilled, through
Felipe Leme744976e2017-07-31 11:34:14 -0700105 * calls to {@link View#autofill(AutofillValue)} or {@link View#autofill(SparseArray)}.
106 *
107 * <p>When the service returns ids of savable views, the Android System keeps track of changes
108 * made to these views, so they can be used to determine if the autofill save UI is shown later.
109 *
110 * <p>The context is then finished when one of the following occurs:
111 *
112 * <ul>
113 * <li>{@link #commit()} is called or all savable views are gone.
114 * <li>{@link #cancel()} is called.
115 * </ul>
116 *
117 * <p>Finally, after the autofill context is commited (i.e., not cancelled), the Android System
Felipe Leme2fe3ade2017-09-28 15:03:36 -0700118 * shows an autofill save UI if the value of savable views have changed. If the user selects the
Felipe Leme744976e2017-07-31 11:34:14 -0700119 * option to Save, the current value of the views is then sent to the autofill service.
120 *
121 * <p>It is safe to call into its methods from any thread.
Felipe Leme3461d3c2017-01-19 08:54:55 -0800122 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -0600123@SystemService(Context.AUTOFILL_MANAGER_SERVICE)
Felipe Leme640f30a2017-03-06 15:44:06 -0800124public final class AutofillManager {
Felipe Leme3461d3c2017-01-19 08:54:55 -0800125
Felipe Leme640f30a2017-03-06 15:44:06 -0800126 private static final String TAG = "AutofillManager";
Felipe Leme3461d3c2017-01-19 08:54:55 -0800127
Svet Ganov782043c2017-02-11 00:52:02 +0000128 /**
129 * Intent extra: The assist structure which captures the filled screen.
Felipe Leme7320ca92017-03-29 15:09:54 -0700130 *
Svet Ganov782043c2017-02-11 00:52:02 +0000131 * <p>
132 * Type: {@link android.app.assist.AssistStructure}
Svet Ganov782043c2017-02-11 00:52:02 +0000133 */
134 public static final String EXTRA_ASSIST_STRUCTURE =
135 "android.view.autofill.extra.ASSIST_STRUCTURE";
136
137 /**
138 * Intent extra: The result of an authentication operation. It is
139 * either a fully populated {@link android.service.autofill.FillResponse}
140 * or a fully populated {@link android.service.autofill.Dataset} if
141 * a response or a dataset is being authenticated respectively.
142 *
143 * <p>
144 * Type: {@link android.service.autofill.FillResponse} or a
145 * {@link android.service.autofill.Dataset}
Svet Ganov782043c2017-02-11 00:52:02 +0000146 */
147 public static final String EXTRA_AUTHENTICATION_RESULT =
148 "android.view.autofill.extra.AUTHENTICATION_RESULT";
149
Felipe Leme7320ca92017-03-29 15:09:54 -0700150 /**
151 * Intent extra: The optional extras provided by the
152 * {@link android.service.autofill.AutofillService}.
153 *
154 * <p>For example, when the service responds to a {@link
155 * android.service.autofill.FillCallback#onSuccess(android.service.autofill.FillResponse)} with
156 * a {@code FillResponse} that requires authentication, the Intent that launches the
157 * service authentication will contain the Bundle set by
Felipe Leme49e96962017-04-20 15:46:18 -0700158 * {@link android.service.autofill.FillResponse.Builder#setClientState(Bundle)} on this extra.
Felipe Leme7320ca92017-03-29 15:09:54 -0700159 *
Felipe Lemea9372382017-10-09 14:42:36 -0700160 * <p>On Android {@link android.os.Build.VERSION_CODES#P} and higher, the autofill service
161 * can also add this bundle to the {@link Intent} set as the
162 * {@link android.app.Activity#setResult(int, Intent) result} for an authentication request,
163 * so the bundle can be recovered later on
164 * {@link android.service.autofill.SaveRequest#getClientState()}.
165 *
Felipe Leme7320ca92017-03-29 15:09:54 -0700166 * <p>
167 * Type: {@link android.os.Bundle}
168 */
Felipe Leme37a440fd2017-04-28 10:50:20 -0700169 public static final String EXTRA_CLIENT_STATE =
Jeff Sharkey000ce802017-04-29 13:13:27 -0600170 "android.view.autofill.extra.CLIENT_STATE";
Felipe Leme7320ca92017-03-29 15:09:54 -0700171
Felipe Lemec24a56a2017-08-03 14:27:57 -0700172
173 /** @hide */
174 public static final String EXTRA_RESTORE_SESSION_TOKEN =
175 "android.view.autofill.extra.RESTORE_SESSION_TOKEN";
176
177 private static final String SESSION_ID_TAG = "android:sessionId";
178 private static final String STATE_TAG = "android:state";
179 private static final String LAST_AUTOFILLED_DATA_TAG = "android:lastAutoFilledData";
180
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700181
Felipe Leme0aa4c502017-04-26 12:36:01 -0700182 /** @hide */ public static final int ACTION_START_SESSION = 1;
183 /** @hide */ public static final int ACTION_VIEW_ENTERED = 2;
184 /** @hide */ public static final int ACTION_VIEW_EXITED = 3;
185 /** @hide */ public static final int ACTION_VALUE_CHANGED = 4;
Felipe Leme3461d3c2017-01-19 08:54:55 -0800186
Felipe Leme9f9ee252017-04-27 13:56:22 -0700187
188 /** @hide */ public static final int FLAG_ADD_CLIENT_ENABLED = 0x1;
189 /** @hide */ public static final int FLAG_ADD_CLIENT_DEBUG = 0x2;
190 /** @hide */ public static final int FLAG_ADD_CLIENT_VERBOSE = 0x4;
191
Svetoslav Ganova9379d02017-05-09 17:40:24 -0700192 /** Which bits in an authentication id are used for the dataset id */
193 private static final int AUTHENTICATION_ID_DATASET_ID_MASK = 0xFFFF;
194 /** How many bits in an authentication id are used for the dataset id */
195 private static final int AUTHENTICATION_ID_DATASET_ID_SHIFT = 16;
196 /** @hide The index for an undefined data set */
197 public static final int AUTHENTICATION_ID_DATASET_ID_UNDEFINED = 0xFFFF;
198
199 /**
Felipe Lemec24a56a2017-08-03 14:27:57 -0700200 * Used on {@link #onPendingSaveUi(int, IBinder)} to cancel the pending UI.
201 *
202 * @hide
203 */
204 public static final int PENDING_UI_OPERATION_CANCEL = 1;
205
206 /**
207 * Used on {@link #onPendingSaveUi(int, IBinder)} to restore the pending UI.
208 *
209 * @hide
210 */
211 public static final int PENDING_UI_OPERATION_RESTORE = 2;
212
213 /**
214 * Initial state of the autofill context, set when there is no session (i.e., when
215 * {@link #mSessionId} is {@link #NO_SESSION}).
216 *
Felipe Lemec7b45292017-09-19 09:06:20 -0700217 * <p>In this state, app callbacks (such as {@link #notifyViewEntered(View)}) are notified to
218 * the server.
219 *
Felipe Lemec24a56a2017-08-03 14:27:57 -0700220 * @hide
221 */
Felipe Lemec7b45292017-09-19 09:06:20 -0700222 public static final int STATE_UNKNOWN = 0;
Felipe Lemec24a56a2017-08-03 14:27:57 -0700223
224 /**
225 * State where the autofill context hasn't been {@link #commit() finished} nor
226 * {@link #cancel() canceled} yet.
227 *
228 * @hide
229 */
Felipe Lemec7b45292017-09-19 09:06:20 -0700230 public static final int STATE_ACTIVE = 1;
231
232 /**
233 * State where the autofill context was finished by the server because the autofill
Felipe Leme17292d12017-10-24 14:03:10 -0700234 * service could not autofill the activity.
Felipe Lemec7b45292017-09-19 09:06:20 -0700235 *
236 * <p>In this state, most apps callback (such as {@link #notifyViewEntered(View)}) are ignored,
237 * exception {@link #requestAutofill(View)} (and {@link #requestAutofill(View, int, Rect)}).
238 *
239 * @hide
240 */
241 public static final int STATE_FINISHED = 2;
Felipe Lemec24a56a2017-08-03 14:27:57 -0700242
243 /**
244 * State where the autofill context has been {@link #commit() finished} but the server still has
245 * a session because the Save UI hasn't been dismissed yet.
246 *
247 * @hide
248 */
Felipe Lemec7b45292017-09-19 09:06:20 -0700249 public static final int STATE_SHOWING_SAVE_UI = 3;
Felipe Lemec24a56a2017-08-03 14:27:57 -0700250
251 /**
Felipe Leme17292d12017-10-24 14:03:10 -0700252 * State where the autofill is disabled because the service cannot autofill the activity at all.
253 *
254 * <p>In this state, every call is ignored, even {@link #requestAutofill(View)}
255 * (and {@link #requestAutofill(View, int, Rect)}).
256 *
257 * @hide
258 */
259 public static final int STATE_DISABLED_BY_SERVICE = 4;
260
261 /**
Svetoslav Ganova9379d02017-05-09 17:40:24 -0700262 * Makes an authentication id from a request id and a dataset id.
263 *
264 * @param requestId The request id.
265 * @param datasetId The dataset id.
266 * @return The authentication id.
267 * @hide
268 */
269 public static int makeAuthenticationId(int requestId, int datasetId) {
270 return (requestId << AUTHENTICATION_ID_DATASET_ID_SHIFT)
271 | (datasetId & AUTHENTICATION_ID_DATASET_ID_MASK);
272 }
273
274 /**
275 * Gets the request id from an authentication id.
276 *
277 * @param authRequestId The authentication id.
278 * @return The request id.
279 * @hide
280 */
281 public static int getRequestIdFromAuthenticationId(int authRequestId) {
282 return (authRequestId >> AUTHENTICATION_ID_DATASET_ID_SHIFT);
283 }
284
285 /**
286 * Gets the dataset id from an authentication id.
287 *
288 * @param authRequestId The authentication id.
289 * @return The dataset id.
290 * @hide
291 */
292 public static int getDatasetIdFromAuthenticationId(int authRequestId) {
293 return (authRequestId & AUTHENTICATION_ID_DATASET_ID_MASK);
294 }
295
Felipe Leme4753bb02017-03-22 20:24:00 -0700296 private final MetricsLogger mMetricsLogger = new MetricsLogger();
Svet Ganov782043c2017-02-11 00:52:02 +0000297
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700298 /**
299 * There is currently no session running.
300 * {@hide}
301 */
302 public static final int NO_SESSION = Integer.MIN_VALUE;
303
Svet Ganov782043c2017-02-11 00:52:02 +0000304 private final IAutoFillManager mService;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700305
306 private final Object mLock = new Object();
307
308 @GuardedBy("mLock")
Svet Ganov782043c2017-02-11 00:52:02 +0000309 private IAutoFillManagerClient mServiceClient;
310
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700311 @GuardedBy("mLock")
Koji Fukuiccec6a62017-10-18 17:48:40 +0900312 private Cleaner mServiceClientCleaner;
313
314 @GuardedBy("mLock")
Felipe Lemee6010f22017-03-03 11:19:51 -0800315 private AutofillCallback mCallback;
316
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700317 private final Context mContext;
Svet Ganov782043c2017-02-11 00:52:02 +0000318
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700319 @GuardedBy("mLock")
320 private int mSessionId = NO_SESSION;
321
322 @GuardedBy("mLock")
Felipe Lemec24a56a2017-08-03 14:27:57 -0700323 private int mState = STATE_UNKNOWN;
324
325 @GuardedBy("mLock")
Svet Ganov782043c2017-02-11 00:52:02 +0000326 private boolean mEnabled;
327
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700328 /** If a view changes to this mapping the autofill operation was successful */
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700329 @GuardedBy("mLock")
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700330 @Nullable private ParcelableMap mLastAutofilledData;
331
Philip P. Moltmann494c3f52017-04-11 10:13:33 -0700332 /** If view tracking is enabled, contains the tracking state */
333 @GuardedBy("mLock")
334 @Nullable private TrackedViews mTrackedViews;
335
Felipe Leme27e20222017-05-18 15:24:11 -0700336 /** Views that are only tracked because they are fillable and could be anchoring the UI. */
337 @GuardedBy("mLock")
338 @Nullable private ArraySet<AutofillId> mFillableIds;
339
Felipe Leme2fe3ade2017-09-28 15:03:36 -0700340 /** If set, session is commited when the field is clicked. */
341 @GuardedBy("mLock")
342 @Nullable private AutofillId mSaveTriggerId;
343
344 /** If set, session is commited when the activity is finished; otherwise session is canceled. */
345 @GuardedBy("mLock")
346 private boolean mSaveOnFinish;
347
Svet Ganov782043c2017-02-11 00:52:02 +0000348 /** @hide */
Felipe Leme640f30a2017-03-06 15:44:06 -0800349 public interface AutofillClient {
Svet Ganov782043c2017-02-11 00:52:02 +0000350 /**
Svet Ganov782043c2017-02-11 00:52:02 +0000351 * Asks the client to start an authentication flow.
352 *
Svetoslav Ganova9379d02017-05-09 17:40:24 -0700353 * @param authenticationId A unique id of the authentication operation.
Svet Ganov782043c2017-02-11 00:52:02 +0000354 * @param intent The authentication intent.
355 * @param fillInIntent The authentication fill-in intent.
356 */
Svetoslav Ganova9379d02017-05-09 17:40:24 -0700357 void autofillCallbackAuthenticate(int authenticationId, IntentSender intent,
358 Intent fillInIntent);
Svet Ganov17db9dc2017-02-21 19:54:31 -0800359
360 /**
361 * Tells the client this manager has state to be reset.
362 */
Felipe Leme4753bb02017-03-22 20:24:00 -0700363 void autofillCallbackResetableStateAvailable();
364
365 /**
366 * Request showing the autofill UI.
367 *
368 * @param anchor The real view the UI needs to anchor to.
369 * @param width The width of the fill UI content.
370 * @param height The height of the fill UI content.
371 * @param virtualBounds The bounds of the virtual decendant of the anchor.
372 * @param presenter The presenter that controls the fill UI window.
373 * @return Whether the UI was shown.
374 */
375 boolean autofillCallbackRequestShowFillUi(@NonNull View anchor, int width, int height,
376 @Nullable Rect virtualBounds, IAutofillWindowPresenter presenter);
377
378 /**
379 * Request hiding the autofill UI.
380 *
381 * @return Whether the UI was hidden.
382 */
383 boolean autofillCallbackRequestHideFillUi();
Philip P. Moltmann494c3f52017-04-11 10:13:33 -0700384
385 /**
Philip P. Moltmann134cee22017-05-06 11:28:38 -0700386 * Checks if views are currently attached and visible.
Philip P. Moltmann494c3f52017-04-11 10:13:33 -0700387 *
Philip P. Moltmann134cee22017-05-06 11:28:38 -0700388 * @return And array with {@code true} iff the view is attached or visible
Philip P. Moltmann494c3f52017-04-11 10:13:33 -0700389 */
Philip P. Moltmann134cee22017-05-06 11:28:38 -0700390 @NonNull boolean[] getViewVisibility(@NonNull int[] viewId);
Philip P. Moltmann494c3f52017-04-11 10:13:33 -0700391
392 /**
393 * Checks is the client is currently visible as understood by autofill.
394 *
395 * @return {@code true} if the client is currently visible
396 */
397 boolean isVisibleForAutofill();
Philip P. Moltmann134cee22017-05-06 11:28:38 -0700398
399 /**
Felipe Leme27e20222017-05-18 15:24:11 -0700400 * Finds views by traversing the hierarchies of the client.
Philip P. Moltmann134cee22017-05-06 11:28:38 -0700401 *
Phil Weaver846cda932017-06-15 10:10:06 -0700402 * @param viewIds The autofill ids of the views to find
Philip P. Moltmann134cee22017-05-06 11:28:38 -0700403 *
Felipe Leme27e20222017-05-18 15:24:11 -0700404 * @return And array containing the views (empty if no views found).
Philip P. Moltmann134cee22017-05-06 11:28:38 -0700405 */
Phil Weaver846cda932017-06-15 10:10:06 -0700406 @NonNull View[] findViewsByAutofillIdTraversal(@NonNull int[] viewIds);
Felipe Leme27e20222017-05-18 15:24:11 -0700407
408 /**
409 * Finds a view by traversing the hierarchies of the client.
410 *
Phil Weaver846cda932017-06-15 10:10:06 -0700411 * @param viewId The autofill id of the views to find
Felipe Leme27e20222017-05-18 15:24:11 -0700412 *
413 * @return The view, or {@code null} if not found
414 */
Phil Weaver846cda932017-06-15 10:10:06 -0700415 @Nullable View findViewByAutofillIdTraversal(int viewId);
Felipe Leme9876a6f2017-05-30 15:47:28 -0700416
417 /**
418 * Runs the specified action on the UI thread.
419 */
420 void runOnUiThread(Runnable action);
Felipe Leme17292d12017-10-24 14:03:10 -0700421
422 /**
423 * Gets the complete component name of this client.
424 */
425 ComponentName getComponentName();
Svet Ganov782043c2017-02-11 00:52:02 +0000426 }
Felipe Leme3461d3c2017-01-19 08:54:55 -0800427
428 /**
429 * @hide
430 */
Felipe Leme640f30a2017-03-06 15:44:06 -0800431 public AutofillManager(Context context, IAutoFillManager service) {
Felipe Leme637e05e2017-12-06 12:09:37 -0800432 mContext = Preconditions.checkNotNull(context, "context cannot be null");
Felipe Leme3461d3c2017-01-19 08:54:55 -0800433 mService = service;
434 }
435
436 /**
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700437 * Restore state after activity lifecycle
438 *
439 * @param savedInstanceState The state to be restored
440 *
441 * {@hide}
442 */
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700443 public void onCreate(Bundle savedInstanceState) {
Svet Ganov43574b02017-04-12 09:25:20 -0700444 if (!hasAutofillFeature()) {
445 return;
446 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700447 synchronized (mLock) {
448 mLastAutofilledData = savedInstanceState.getParcelable(LAST_AUTOFILLED_DATA_TAG);
449
Felipe Lemec24a56a2017-08-03 14:27:57 -0700450 if (isActiveLocked()) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700451 Log.w(TAG, "New session was started before onCreate()");
452 return;
453 }
454
455 mSessionId = savedInstanceState.getInt(SESSION_ID_TAG, NO_SESSION);
Felipe Lemec24a56a2017-08-03 14:27:57 -0700456 mState = savedInstanceState.getInt(STATE_TAG, STATE_UNKNOWN);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700457
458 if (mSessionId != NO_SESSION) {
459 ensureServiceClientAddedIfNeededLocked();
460
Felipe Leme637e05e2017-12-06 12:09:37 -0800461 final AutofillClient client = getClient();
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700462 if (client != null) {
463 try {
464 final boolean sessionWasRestored = mService.restoreSession(mSessionId,
465 mContext.getActivityToken(), mServiceClient.asBinder());
466
467 if (!sessionWasRestored) {
468 Log.w(TAG, "Session " + mSessionId + " could not be restored");
469 mSessionId = NO_SESSION;
Felipe Lemec24a56a2017-08-03 14:27:57 -0700470 mState = STATE_UNKNOWN;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700471 } else {
Felipe Leme9f9ee252017-04-27 13:56:22 -0700472 if (sDebug) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700473 Log.d(TAG, "session " + mSessionId + " was restored");
474 }
475
476 client.autofillCallbackResetableStateAvailable();
477 }
478 } catch (RemoteException e) {
479 Log.e(TAG, "Could not figure out if there was an autofill session", e);
480 }
481 }
482 }
483 }
484 }
485
486 /**
Philip P. Moltmann494c3f52017-04-11 10:13:33 -0700487 * Called once the client becomes visible.
488 *
489 * @see AutofillClient#isVisibleForAutofill()
490 *
491 * {@hide}
492 */
493 public void onVisibleForAutofill() {
494 synchronized (mLock) {
Felipe Lemec24a56a2017-08-03 14:27:57 -0700495 if (mEnabled && isActiveLocked() && mTrackedViews != null) {
Philip P. Moltmann134cee22017-05-06 11:28:38 -0700496 mTrackedViews.onVisibleForAutofillLocked();
Philip P. Moltmann494c3f52017-04-11 10:13:33 -0700497 }
498 }
499 }
500
501 /**
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700502 * Save state before activity lifecycle
503 *
504 * @param outState Place to store the state
505 *
506 * {@hide}
507 */
508 public void onSaveInstanceState(Bundle outState) {
Svet Ganov43574b02017-04-12 09:25:20 -0700509 if (!hasAutofillFeature()) {
510 return;
511 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700512 synchronized (mLock) {
513 if (mSessionId != NO_SESSION) {
514 outState.putInt(SESSION_ID_TAG, mSessionId);
515 }
Felipe Lemec24a56a2017-08-03 14:27:57 -0700516 if (mState != STATE_UNKNOWN) {
517 outState.putInt(STATE_TAG, mState);
518 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700519 if (mLastAutofilledData != null) {
520 outState.putParcelable(LAST_AUTOFILLED_DATA_TAG, mLastAutofilledData);
521 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700522 }
523 }
524
525 /**
526 * Checks whether autofill is enabled for the current user.
Felipe Leme2ac463e2017-03-13 14:06:25 -0700527 *
528 * <p>Typically used to determine whether the option to explicitly request autofill should
529 * be offered - see {@link #requestAutofill(View)}.
530 *
531 * @return whether autofill is enabled for the current user.
532 */
533 public boolean isEnabled() {
Felipe Leme17292d12017-10-24 14:03:10 -0700534 if (!hasAutofillFeature() || isDisabledByService()) {
Svet Ganov43574b02017-04-12 09:25:20 -0700535 return false;
536 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700537 synchronized (mLock) {
538 ensureServiceClientAddedIfNeededLocked();
539 return mEnabled;
540 }
Felipe Leme2ac463e2017-03-13 14:06:25 -0700541 }
542
543 /**
Philip P. Moltmanncc684ed2017-04-17 14:18:33 -0700544 * Should always be called from {@link AutofillService#getFillEventHistory()}.
545 *
546 * @hide
547 */
548 @Nullable public FillEventHistory getFillEventHistory() {
549 try {
550 return mService.getFillEventHistory();
551 } catch (RemoteException e) {
552 e.rethrowFromSystemServer();
553 return null;
554 }
555 }
556
557 /**
Felipe Leme2ac463e2017-03-13 14:06:25 -0700558 * Explicitly requests a new autofill context.
559 *
Felipe Leme6dcec872017-05-25 11:24:23 -0700560 * <p>Normally, the autofill context is automatically started if necessary when
561 * {@link #notifyViewEntered(View)} is called, but this method should be used in the
562 * cases where it must be explicitly started. For example, when the view offers an AUTOFILL
563 * option on its contextual overflow menu, and the user selects it.
Felipe Leme2ac463e2017-03-13 14:06:25 -0700564 *
565 * @param view view requesting the new autofill context.
566 */
567 public void requestAutofill(@NonNull View view) {
Felipe Lemed1146422017-04-26 10:17:05 -0700568 notifyViewEntered(view, FLAG_MANUAL_REQUEST);
Felipe Leme2ac463e2017-03-13 14:06:25 -0700569 }
570
571 /**
572 * Explicitly requests a new autofill context for virtual views.
573 *
Felipe Leme6dcec872017-05-25 11:24:23 -0700574 * <p>Normally, the autofill context is automatically started if necessary when
575 * {@link #notifyViewEntered(View, int, Rect)} is called, but this method should be used in the
576 * cases where it must be explicitly started. For example, when the virtual view offers an
577 * AUTOFILL option on its contextual overflow menu, and the user selects it.
Felipe Leme2ac463e2017-03-13 14:06:25 -0700578 *
Felipe Leme6dcec872017-05-25 11:24:23 -0700579 * <p>The virtual view boundaries must be absolute screen coordinates. For example, if the
580 * parent view uses {@code bounds} to draw the virtual view inside its Canvas,
581 * the absolute bounds could be calculated by:
582 *
583 * <pre class="prettyprint">
584 * int offset[] = new int[2];
585 * getLocationOnScreen(offset);
586 * Rect absBounds = new Rect(bounds.left + offset[0],
587 * bounds.top + offset[1],
588 * bounds.right + offset[0], bounds.bottom + offset[1]);
589 * </pre>
590 *
591 * @param view the virtual view parent.
592 * @param virtualId id identifying the virtual child inside the parent view.
593 * @param absBounds absolute boundaries of the virtual view in the screen.
Felipe Leme2ac463e2017-03-13 14:06:25 -0700594 */
Felipe Leme6dcec872017-05-25 11:24:23 -0700595 public void requestAutofill(@NonNull View view, int virtualId, @NonNull Rect absBounds) {
596 notifyViewEntered(view, virtualId, absBounds, FLAG_MANUAL_REQUEST);
Felipe Leme2ac463e2017-03-13 14:06:25 -0700597 }
598
Felipe Leme2ac463e2017-03-13 14:06:25 -0700599 /**
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700600 * Called when a {@link View} that supports autofill is entered.
Felipe Leme3461d3c2017-01-19 08:54:55 -0800601 *
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700602 * @param view {@link View} that was entered.
Felipe Leme3461d3c2017-01-19 08:54:55 -0800603 */
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700604 public void notifyViewEntered(@NonNull View view) {
Felipe Lemed1146422017-04-26 10:17:05 -0700605 notifyViewEntered(view, 0);
606 }
607
Felipe Leme17292d12017-10-24 14:03:10 -0700608 private boolean shouldIgnoreViewEnteredLocked(@NonNull View view, int flags) {
609 if (isDisabledByService()) {
610 if (sVerbose) {
611 Log.v(TAG, "ignoring notifyViewEntered(flags=" + flags + ", view=" + view
612 + ") on state " + getStateAsStringLocked());
613 }
614 return true;
615 }
616 if (mState == STATE_FINISHED && (flags & FLAG_MANUAL_REQUEST) == 0) {
617 if (sVerbose) {
618 Log.v(TAG, "ignoring notifyViewEntered(flags=" + flags + ", view=" + view
619 + ") on state " + getStateAsStringLocked());
620 }
621 return true;
622 }
623 return false;
624 }
625
Felipe Lemed1146422017-04-26 10:17:05 -0700626 private void notifyViewEntered(@NonNull View view, int flags) {
Svet Ganov43574b02017-04-12 09:25:20 -0700627 if (!hasAutofillFeature()) {
628 return;
629 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700630 AutofillCallback callback = null;
631 synchronized (mLock) {
Felipe Leme17292d12017-10-24 14:03:10 -0700632 if (shouldIgnoreViewEnteredLocked(view, flags)) return;
Felipe Lemec7b45292017-09-19 09:06:20 -0700633
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700634 ensureServiceClientAddedIfNeededLocked();
Svet Ganov782043c2017-02-11 00:52:02 +0000635
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700636 if (!mEnabled) {
637 if (mCallback != null) {
638 callback = mCallback;
639 }
640 } else {
641 final AutofillId id = getAutofillId(view);
642 final AutofillValue value = view.getAutofillValue();
643
Felipe Lemec24a56a2017-08-03 14:27:57 -0700644 if (!isActiveLocked()) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700645 // Starts new session.
Philip P. Moltmann134cee22017-05-06 11:28:38 -0700646 startSessionLocked(id, null, value, flags);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700647 } else {
648 // Update focus on existing session.
Felipe Leme0aa4c502017-04-26 12:36:01 -0700649 updateSessionLocked(id, null, value, ACTION_VIEW_ENTERED, flags);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700650 }
Felipe Leme24aae152017-03-15 12:33:01 -0700651 }
Felipe Lemebab851c2017-02-03 18:45:08 -0800652 }
653
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700654 if (callback != null) {
655 mCallback.onAutofillEvent(view, AutofillCallback.EVENT_INPUT_UNAVAILABLE);
Svet Ganov782043c2017-02-11 00:52:02 +0000656 }
Felipe Lemebab851c2017-02-03 18:45:08 -0800657 }
658
659 /**
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700660 * Called when a {@link View} that supports autofill is exited.
Felipe Lemebab851c2017-02-03 18:45:08 -0800661 *
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700662 * @param view {@link View} that was exited.
Philip P. Moltmann44611812017-02-23 12:52:46 -0800663 */
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700664 public void notifyViewExited(@NonNull View view) {
Svet Ganov43574b02017-04-12 09:25:20 -0700665 if (!hasAutofillFeature()) {
666 return;
667 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700668 synchronized (mLock) {
669 ensureServiceClientAddedIfNeededLocked();
Philip P. Moltmann44611812017-02-23 12:52:46 -0800670
Felipe Lemec24a56a2017-08-03 14:27:57 -0700671 if (mEnabled && isActiveLocked()) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700672 final AutofillId id = getAutofillId(view);
Philip P. Moltmann44611812017-02-23 12:52:46 -0800673
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700674 // Update focus on existing session.
Felipe Leme0aa4c502017-04-26 12:36:01 -0700675 updateSessionLocked(id, null, null, ACTION_VIEW_EXITED, 0);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700676 }
Philip P. Moltmann44611812017-02-23 12:52:46 -0800677 }
678 }
679
680 /**
Svetoslav Ganov4db89ba2017-06-28 16:50:37 -0700681 * Called when a {@link View view's} visibility changed.
Philip P. Moltmann494c3f52017-04-11 10:13:33 -0700682 *
683 * @param view {@link View} that was exited.
684 * @param isVisible visible if the view is visible in the view hierarchy.
Philip P. Moltmann494c3f52017-04-11 10:13:33 -0700685 */
Svetoslav Ganov4db89ba2017-06-28 16:50:37 -0700686 public void notifyViewVisibilityChanged(@NonNull View view, boolean isVisible) {
687 notifyViewVisibilityChangedInternal(view, 0, isVisible, false);
688 }
689
690 /**
691 * Called when a virtual view's visibility changed.
692 *
693 * @param view {@link View} that was exited.
694 * @param virtualId id identifying the virtual child inside the parent view.
695 * @param isVisible visible if the view is visible in the view hierarchy.
696 */
697 public void notifyViewVisibilityChanged(@NonNull View view, int virtualId, boolean isVisible) {
698 notifyViewVisibilityChangedInternal(view, virtualId, isVisible, true);
699 }
700
701 /**
702 * Called when a view/virtual view's visibility changed.
703 *
704 * @param view {@link View} that was exited.
705 * @param virtualId id identifying the virtual child inside the parent view.
706 * @param isVisible visible if the view is visible in the view hierarchy.
707 * @param virtual Whether the view is virtual.
708 */
709 private void notifyViewVisibilityChangedInternal(@NonNull View view, int virtualId,
710 boolean isVisible, boolean virtual) {
Philip P. Moltmann494c3f52017-04-11 10:13:33 -0700711 synchronized (mLock) {
Felipe Lemec24a56a2017-08-03 14:27:57 -0700712 if (mEnabled && isActiveLocked()) {
Svetoslav Ganov4db89ba2017-06-28 16:50:37 -0700713 final AutofillId id = virtual ? getAutofillId(view, virtualId)
714 : view.getAutofillId();
Felipe Leme27e20222017-05-18 15:24:11 -0700715 if (!isVisible && mFillableIds != null) {
Felipe Leme27e20222017-05-18 15:24:11 -0700716 if (mFillableIds.contains(id)) {
717 if (sDebug) Log.d(TAG, "Hidding UI when view " + id + " became invisible");
718 requestHideFillUi(id, view);
719 }
720 }
721 if (mTrackedViews != null) {
Svetoslav Ganov4db89ba2017-06-28 16:50:37 -0700722 mTrackedViews.notifyViewVisibilityChanged(id, isVisible);
Felipe Leme27e20222017-05-18 15:24:11 -0700723 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -0700724 }
725 }
726 }
727
728 /**
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700729 * Called when a virtual view that supports autofill is entered.
Philip P. Moltmann44611812017-02-23 12:52:46 -0800730 *
Felipe Leme6dcec872017-05-25 11:24:23 -0700731 * <p>The virtual view boundaries must be absolute screen coordinates. For example, if the
732 * parent, non-virtual view uses {@code bounds} to draw the virtual view inside its Canvas,
733 * the absolute bounds could be calculated by:
734 *
735 * <pre class="prettyprint">
736 * int offset[] = new int[2];
737 * getLocationOnScreen(offset);
738 * Rect absBounds = new Rect(bounds.left + offset[0],
739 * bounds.top + offset[1],
740 * bounds.right + offset[0], bounds.bottom + offset[1]);
741 * </pre>
742 *
743 * @param view the virtual view parent.
744 * @param virtualId id identifying the virtual child inside the parent view.
745 * @param absBounds absolute boundaries of the virtual view in the screen.
Felipe Lemebab851c2017-02-03 18:45:08 -0800746 */
Felipe Leme6dcec872017-05-25 11:24:23 -0700747 public void notifyViewEntered(@NonNull View view, int virtualId, @NonNull Rect absBounds) {
748 notifyViewEntered(view, virtualId, absBounds, 0);
Felipe Lemed1146422017-04-26 10:17:05 -0700749 }
750
Felipe Leme6dcec872017-05-25 11:24:23 -0700751 private void notifyViewEntered(View view, int virtualId, Rect bounds, int flags) {
Svet Ganov43574b02017-04-12 09:25:20 -0700752 if (!hasAutofillFeature()) {
753 return;
754 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700755 AutofillCallback callback = null;
756 synchronized (mLock) {
Felipe Leme17292d12017-10-24 14:03:10 -0700757 if (shouldIgnoreViewEnteredLocked(view, flags)) return;
758
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700759 ensureServiceClientAddedIfNeededLocked();
Svet Ganov782043c2017-02-11 00:52:02 +0000760
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700761 if (!mEnabled) {
762 if (mCallback != null) {
763 callback = mCallback;
764 }
765 } else {
Felipe Leme6dcec872017-05-25 11:24:23 -0700766 final AutofillId id = getAutofillId(view, virtualId);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700767
Felipe Lemec24a56a2017-08-03 14:27:57 -0700768 if (!isActiveLocked()) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700769 // Starts new session.
Philip P. Moltmann134cee22017-05-06 11:28:38 -0700770 startSessionLocked(id, bounds, null, flags);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700771 } else {
772 // Update focus on existing session.
Felipe Leme0aa4c502017-04-26 12:36:01 -0700773 updateSessionLocked(id, bounds, null, ACTION_VIEW_ENTERED, flags);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700774 }
Felipe Leme24aae152017-03-15 12:33:01 -0700775 }
Felipe Lemebab851c2017-02-03 18:45:08 -0800776 }
777
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700778 if (callback != null) {
Felipe Leme6dcec872017-05-25 11:24:23 -0700779 callback.onAutofillEvent(view, virtualId,
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700780 AutofillCallback.EVENT_INPUT_UNAVAILABLE);
Philip P. Moltmann44611812017-02-23 12:52:46 -0800781 }
782 }
783
784 /**
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700785 * Called when a virtual view that supports autofill is exited.
Philip P. Moltmann44611812017-02-23 12:52:46 -0800786 *
Felipe Leme6dcec872017-05-25 11:24:23 -0700787 * @param view the virtual view parent.
788 * @param virtualId id identifying the virtual child inside the parent view.
Philip P. Moltmann44611812017-02-23 12:52:46 -0800789 */
Felipe Leme6dcec872017-05-25 11:24:23 -0700790 public void notifyViewExited(@NonNull View view, int virtualId) {
Svet Ganov43574b02017-04-12 09:25:20 -0700791 if (!hasAutofillFeature()) {
792 return;
793 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700794 synchronized (mLock) {
795 ensureServiceClientAddedIfNeededLocked();
Philip P. Moltmann44611812017-02-23 12:52:46 -0800796
Felipe Lemec24a56a2017-08-03 14:27:57 -0700797 if (mEnabled && isActiveLocked()) {
Felipe Leme6dcec872017-05-25 11:24:23 -0700798 final AutofillId id = getAutofillId(view, virtualId);
Philip P. Moltmann44611812017-02-23 12:52:46 -0800799
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700800 // Update focus on existing session.
Felipe Leme0aa4c502017-04-26 12:36:01 -0700801 updateSessionLocked(id, null, null, ACTION_VIEW_EXITED, 0);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700802 }
Svet Ganov782043c2017-02-11 00:52:02 +0000803 }
Felipe Lemebab851c2017-02-03 18:45:08 -0800804 }
805
806 /**
Felipe Leme640f30a2017-03-06 15:44:06 -0800807 * Called to indicate the value of an autofillable {@link View} changed.
Felipe Lemebab851c2017-02-03 18:45:08 -0800808 *
Philip P. Moltmann44611812017-02-23 12:52:46 -0800809 * @param view view whose value changed.
Felipe Lemebab851c2017-02-03 18:45:08 -0800810 */
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700811 public void notifyValueChanged(View view) {
Svet Ganov43574b02017-04-12 09:25:20 -0700812 if (!hasAutofillFeature()) {
813 return;
814 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700815 AutofillId id = null;
816 boolean valueWasRead = false;
817 AutofillValue value = null;
818
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700819 synchronized (mLock) {
820 // If the session is gone some fields might still be highlighted, hence we have to
821 // remove the isAutofilled property even if no sessions are active.
822 if (mLastAutofilledData == null) {
823 view.setAutofilled(false);
824 } else {
825 id = getAutofillId(view);
826 if (mLastAutofilledData.containsKey(id)) {
827 value = view.getAutofillValue();
828 valueWasRead = true;
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700829
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700830 if (Objects.equals(mLastAutofilledData.get(id), value)) {
831 view.setAutofilled(true);
832 } else {
833 view.setAutofilled(false);
834 mLastAutofilledData.remove(id);
835 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700836 } else {
837 view.setAutofilled(false);
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700838 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700839 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700840
Felipe Lemec24a56a2017-08-03 14:27:57 -0700841 if (!mEnabled || !isActiveLocked()) {
Felipe Lemec7b45292017-09-19 09:06:20 -0700842 if (sVerbose && mEnabled) {
843 Log.v(TAG, "notifyValueChanged(" + view + "): ignoring on state "
844 + getStateAsStringLocked());
845 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700846 return;
847 }
Felipe Lemebab851c2017-02-03 18:45:08 -0800848
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700849 if (id == null) {
850 id = getAutofillId(view);
851 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700852
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700853 if (!valueWasRead) {
854 value = view.getAutofillValue();
855 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -0700856
Felipe Leme0aa4c502017-04-26 12:36:01 -0700857 updateSessionLocked(id, null, value, ACTION_VALUE_CHANGED, 0);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700858 }
Felipe Lemebab851c2017-02-03 18:45:08 -0800859 }
860
Felipe Lemebab851c2017-02-03 18:45:08 -0800861 /**
Felipe Leme6dcec872017-05-25 11:24:23 -0700862 * Called to indicate the value of an autofillable virtual view has changed.
Felipe Lemebab851c2017-02-03 18:45:08 -0800863 *
Felipe Leme6dcec872017-05-25 11:24:23 -0700864 * @param view the virtual view parent.
865 * @param virtualId id identifying the virtual child inside the parent view.
Felipe Lemebab851c2017-02-03 18:45:08 -0800866 * @param value new value of the child.
867 */
Felipe Leme6dcec872017-05-25 11:24:23 -0700868 public void notifyValueChanged(View view, int virtualId, AutofillValue value) {
Svet Ganov43574b02017-04-12 09:25:20 -0700869 if (!hasAutofillFeature()) {
870 return;
871 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700872 synchronized (mLock) {
Felipe Lemec24a56a2017-08-03 14:27:57 -0700873 if (!mEnabled || !isActiveLocked()) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700874 return;
875 }
Felipe Lemebab851c2017-02-03 18:45:08 -0800876
Felipe Leme6dcec872017-05-25 11:24:23 -0700877 final AutofillId id = getAutofillId(view, virtualId);
Felipe Leme0aa4c502017-04-26 12:36:01 -0700878 updateSessionLocked(id, null, value, ACTION_VALUE_CHANGED, 0);
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700879 }
Felipe Lemebab851c2017-02-03 18:45:08 -0800880 }
881
Felipe Leme2fe3ade2017-09-28 15:03:36 -0700882
883 /**
884 * Called when a {@link View} is clicked. Currently only used by views that should trigger save.
885 *
886 * @hide
887 */
888 public void notifyViewClicked(View view) {
889 final AutofillId id = view.getAutofillId();
890
891 if (sVerbose) Log.v(TAG, "notifyViewClicked(): id=" + id + ", trigger=" + mSaveTriggerId);
892
893 synchronized (mLock) {
894 if (mSaveTriggerId != null && mSaveTriggerId.equals(id)) {
895 if (sDebug) Log.d(TAG, "triggering commit by click of " + id);
896 commitLocked();
897 mMetricsLogger.action(MetricsEvent.AUTOFILL_SAVE_EXPLICITLY_TRIGGERED,
898 mContext.getPackageName());
899 }
900 }
901 }
902
903 /**
904 * Called by {@link android.app.Activity} to commit or cancel the session on finish.
905 *
906 * @hide
907 */
908 public void onActivityFinished() {
909 if (!hasAutofillFeature()) {
910 return;
911 }
912 synchronized (mLock) {
913 if (mSaveOnFinish) {
Felipe Leme601d2202017-11-17 08:21:23 -0800914 if (sDebug) Log.d(TAG, "Committing session on finish() as requested by service");
Felipe Leme2fe3ade2017-09-28 15:03:36 -0700915 commitLocked();
916 } else {
917 if (sDebug) Log.d(TAG, "Cancelling session on finish() as requested by service");
918 cancelLocked();
919 }
920 }
921 }
922
Felipe Lemebab851c2017-02-03 18:45:08 -0800923 /**
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700924 * Called to indicate the current autofill context should be commited.
Felipe Lemebab851c2017-02-03 18:45:08 -0800925 *
Felipe Lemeafdfe762017-07-24 11:25:56 -0700926 * <p>This method is typically called by {@link View Views} that manage virtual views; for
927 * example, when the view is rendering an {@code HTML} page with a form and virtual views
928 * that represent the HTML elements, it should call this method after the form is submitted and
929 * another page is rendered.
930 *
931 * <p><b>Note:</b> This method does not need to be called on regular application lifecycle
932 * methods such as {@link android.app.Activity#finish()}.
Felipe Lemebab851c2017-02-03 18:45:08 -0800933 */
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700934 public void commit() {
Svet Ganov43574b02017-04-12 09:25:20 -0700935 if (!hasAutofillFeature()) {
936 return;
937 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700938 synchronized (mLock) {
Felipe Leme2fe3ade2017-09-28 15:03:36 -0700939 commitLocked();
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700940 }
Felipe Leme0200d9e2017-01-24 15:10:26 -0800941 }
942
Felipe Leme2fe3ade2017-09-28 15:03:36 -0700943 private void commitLocked() {
944 if (!mEnabled && !isActiveLocked()) {
945 return;
946 }
947 finishSessionLocked();
948 }
949
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700950 /**
951 * Called to indicate the current autofill context should be cancelled.
952 *
Felipe Lemeafdfe762017-07-24 11:25:56 -0700953 * <p>This method is typically called by {@link View Views} that manage virtual views; for
954 * example, when the view is rendering an {@code HTML} page with a form and virtual views
955 * that represent the HTML elements, it should call this method if the user does not post the
956 * form but moves to another form in this page.
957 *
958 * <p><b>Note:</b> This method does not need to be called on regular application lifecycle
959 * methods such as {@link android.app.Activity#finish()}.
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700960 */
961 public void cancel() {
Felipe Leme601d2202017-11-17 08:21:23 -0800962 if (sVerbose) Log.v(TAG, "cancel() called by app");
Svet Ganov43574b02017-04-12 09:25:20 -0700963 if (!hasAutofillFeature()) {
964 return;
965 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700966 synchronized (mLock) {
Felipe Leme2fe3ade2017-09-28 15:03:36 -0700967 cancelLocked();
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -0700968 }
Svet Ganov2f8fb1f2017-03-13 00:21:04 -0700969 }
970
Felipe Leme2fe3ade2017-09-28 15:03:36 -0700971 private void cancelLocked() {
972 if (!mEnabled && !isActiveLocked()) {
973 return;
974 }
975 cancelSessionLocked();
976 }
977
Svet Ganovf965b872017-04-28 16:34:02 -0700978 /** @hide */
979 public void disableOwnedAutofillServices() {
980 disableAutofillServices();
981 }
982
Svet Ganovf20a0372017-04-10 17:08:05 -0700983 /**
984 * If the app calling this API has enabled autofill services they
985 * will be disabled.
986 */
Svet Ganovf965b872017-04-28 16:34:02 -0700987 public void disableAutofillServices() {
Svet Ganov43574b02017-04-12 09:25:20 -0700988 if (!hasAutofillFeature()) {
989 return;
990 }
Svet Ganovf20a0372017-04-10 17:08:05 -0700991 try {
992 mService.disableOwnedAutofillServices(mContext.getUserId());
993 } catch (RemoteException e) {
994 throw e.rethrowFromSystemServer();
995 }
996 }
997
Felipe Lemedb041182017-04-21 17:33:38 -0700998 /**
999 * Returns {@code true} if the calling application provides a {@link AutofillService} that is
1000 * enabled for the current user, or {@code false} otherwise.
1001 */
1002 public boolean hasEnabledAutofillServices() {
1003 if (mService == null) return false;
1004
1005 try {
1006 return mService.isServiceEnabled(mContext.getUserId(), mContext.getPackageName());
1007 } catch (RemoteException e) {
1008 throw e.rethrowFromSystemServer();
1009 }
1010 }
1011
1012 /**
Felipe Leme452886a2017-11-27 13:09:13 -08001013 * Gets the user data used for <a href="#FieldsClassification">fields classification</a>.
1014 *
1015 * <p><b>Note:</b> This method should only be called by an app providing an autofill service.
1016 *
1017 * TODO(b/67867469):
1018 * - proper javadoc
1019 * - unhide / remove testApi
1020 *
1021 * @return value previously set by {@link #setUserData(UserData)} or {@code null} if it was
1022 * reset or if the caller currently does not have an enabled autofill service for the user.
1023 *
1024 * @hide
1025 */
1026 @TestApi
1027 @Nullable public UserData getUserData() {
1028 try {
1029 return mService.getUserData();
1030 } catch (RemoteException e) {
1031 e.rethrowFromSystemServer();
1032 return null;
1033 }
1034 }
1035
1036 /**
1037 * Sets the user data used for <a href="#FieldsClassification">fields classification</a>.
1038 *
1039 * <p><b>Note:</b> This method should only be called by an app providing an autofill service,
1040 * and it's ignored if the caller currently doesn't have an enabled autofill service for
1041 * the user.
1042 *
1043 * TODO(b/67867469):
1044 * - proper javadoc
1045 * - unhide / remove testApi
1046 * - add unit tests:
1047 * - call set / get / verify
1048 *
1049 * @hide
1050 */
1051 @TestApi
1052 public void setUserData(@Nullable UserData userData) {
1053 try {
1054 mService.setUserData(userData);
1055 } catch (RemoteException e) {
1056 e.rethrowFromSystemServer();
1057 }
1058 }
1059
1060 /**
Felipe Leme329d0402017-12-06 09:22:43 -08001061 * TODO(b/67867469):
1062 * - proper javadoc
1063 * - mention this method in other places
1064 * - unhide / remove testApi
1065 * @hide
1066 */
1067 @TestApi
1068 public boolean isFieldClassificationEnabled() {
1069 try {
1070 return mService.isFieldClassificationEnabled();
1071 } catch (RemoteException e) {
1072 e.rethrowFromSystemServer();
1073 return false;
1074 }
1075 }
1076
1077 /**
Ricardo Loo33d226c2017-06-27 14:17:33 -07001078 * Returns {@code true} if autofill is supported by the current device and
1079 * is supported for this user.
Felipe Lemedb041182017-04-21 17:33:38 -07001080 *
1081 * <p>Autofill is typically supported, but it could be unsupported in cases like:
1082 * <ol>
1083 * <li>Low-end devices.
1084 * <li>Device policy rules that forbid its usage.
1085 * </ol>
1086 */
1087 public boolean isAutofillSupported() {
1088 if (mService == null) return false;
1089
1090 try {
1091 return mService.isServiceSupported(mContext.getUserId());
1092 } catch (RemoteException e) {
1093 throw e.rethrowFromSystemServer();
1094 }
1095 }
1096
Felipe Leme637e05e2017-12-06 12:09:37 -08001097 // Note: don't need to use locked suffix because mContext is final.
1098 private AutofillClient getClient() {
Felipe Leme686128e2017-10-17 14:02:20 -07001099 final AutofillClient client = mContext.getAutofillClient();
1100 if (client == null && sDebug) {
1101 Log.d(TAG, "No AutofillClient for " + mContext.getPackageName() + " on context "
1102 + mContext);
1103 }
1104 return client;
Svet Ganov782043c2017-02-11 00:52:02 +00001105 }
1106
1107 /** @hide */
Svetoslav Ganova9379d02017-05-09 17:40:24 -07001108 public void onAuthenticationResult(int authenticationId, Intent data) {
Svet Ganov43574b02017-04-12 09:25:20 -07001109 if (!hasAutofillFeature()) {
1110 return;
1111 }
Felipe Leme85d1c2d2017-04-21 08:56:04 -07001112 // TODO: the result code is being ignored, so this method is not reliably
Felipe Lemed633f072017-02-14 10:17:17 -08001113 // handling the cases where it's not RESULT_OK: it works fine if the service does not
1114 // set the EXTRA_AUTHENTICATION_RESULT extra, but it could cause weird results if the
1115 // service set the extra and returned RESULT_CANCELED...
1116
Felipe Leme9f9ee252017-04-27 13:56:22 -07001117 if (sDebug) Log.d(TAG, "onAuthenticationResult(): d=" + data);
Felipe Lemed633f072017-02-14 10:17:17 -08001118
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001119 synchronized (mLock) {
Felipe Lemec24a56a2017-08-03 14:27:57 -07001120 if (!isActiveLocked() || data == null) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001121 return;
1122 }
1123 final Parcelable result = data.getParcelableExtra(EXTRA_AUTHENTICATION_RESULT);
1124 final Bundle responseData = new Bundle();
1125 responseData.putParcelable(EXTRA_AUTHENTICATION_RESULT, result);
Felipe Lemea9372382017-10-09 14:42:36 -07001126 final Bundle newClientState = data.getBundleExtra(EXTRA_CLIENT_STATE);
1127 if (newClientState != null) {
1128 responseData.putBundle(EXTRA_CLIENT_STATE, newClientState);
1129 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001130 try {
Svetoslav Ganova9379d02017-05-09 17:40:24 -07001131 mService.setAuthenticationResult(responseData, mSessionId, authenticationId,
1132 mContext.getUserId());
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001133 } catch (RemoteException e) {
1134 Log.e(TAG, "Error delivering authentication result", e);
1135 }
Svet Ganov782043c2017-02-11 00:52:02 +00001136 }
Felipe Lemebab851c2017-02-03 18:45:08 -08001137 }
1138
Felipe Leme640f30a2017-03-06 15:44:06 -08001139 private static AutofillId getAutofillId(View view) {
Phil Weaver846cda932017-06-15 10:10:06 -07001140 return new AutofillId(view.getAutofillViewId());
Felipe Leme0200d9e2017-01-24 15:10:26 -08001141 }
1142
Felipe Leme6dcec872017-05-25 11:24:23 -07001143 private static AutofillId getAutofillId(View parent, int virtualId) {
Phil Weaver846cda932017-06-15 10:10:06 -07001144 return new AutofillId(parent.getAutofillViewId(), virtualId);
Felipe Lemebab851c2017-02-03 18:45:08 -08001145 }
1146
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001147 private void startSessionLocked(@NonNull AutofillId id, @NonNull Rect bounds,
1148 @NonNull AutofillValue value, int flags) {
Felipe Leme9f9ee252017-04-27 13:56:22 -07001149 if (sVerbose) {
1150 Log.v(TAG, "startSessionLocked(): id=" + id + ", bounds=" + bounds + ", value=" + value
Felipe Lemec7b45292017-09-19 09:06:20 -07001151 + ", flags=" + flags + ", state=" + getStateAsStringLocked());
Felipe Lemebab851c2017-02-03 18:45:08 -08001152 }
Felipe Lemec7b45292017-09-19 09:06:20 -07001153 if (mState != STATE_UNKNOWN && (flags & FLAG_MANUAL_REQUEST) == 0) {
1154 if (sVerbose) {
1155 Log.v(TAG, "not automatically starting session for " + id
1156 + " on state " + getStateAsStringLocked());
1157 }
Felipe Lemec24a56a2017-08-03 14:27:57 -07001158 return;
1159 }
Felipe Lemebab851c2017-02-03 18:45:08 -08001160 try {
Felipe Leme637e05e2017-12-06 12:09:37 -08001161 final AutofillClient client = getClient();
1162 if (client == null) return; // NOTE: getClient() already logd it..
1163
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001164 mSessionId = mService.startSession(mContext.getActivityToken(),
Felipe Lemee6010f22017-03-03 11:19:51 -08001165 mServiceClient.asBinder(), id, bounds, value, mContext.getUserId(),
Felipe Leme17292d12017-10-24 14:03:10 -07001166 mCallback != null, flags, client.getComponentName());
Felipe Lemec24a56a2017-08-03 14:27:57 -07001167 if (mSessionId != NO_SESSION) {
1168 mState = STATE_ACTIVE;
1169 }
Felipe Leme637e05e2017-12-06 12:09:37 -08001170 client.autofillCallbackResetableStateAvailable();
Svet Ganov782043c2017-02-11 00:52:02 +00001171 } catch (RemoteException e) {
1172 throw e.rethrowFromSystemServer();
1173 }
1174 }
1175
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001176 private void finishSessionLocked() {
Felipe Lemec7b45292017-09-19 09:06:20 -07001177 if (sVerbose) Log.v(TAG, "finishSessionLocked(): " + getStateAsStringLocked());
Felipe Lemec24a56a2017-08-03 14:27:57 -07001178
1179 if (!isActiveLocked()) return;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001180
Svet Ganov782043c2017-02-11 00:52:02 +00001181 try {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001182 mService.finishSession(mSessionId, mContext.getUserId());
Felipe Lemebab851c2017-02-03 18:45:08 -08001183 } catch (RemoteException e) {
1184 throw e.rethrowFromSystemServer();
1185 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001186
Felipe Lemec24a56a2017-08-03 14:27:57 -07001187 resetSessionLocked();
Felipe Lemebab851c2017-02-03 18:45:08 -08001188 }
1189
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001190 private void cancelSessionLocked() {
Felipe Lemec7b45292017-09-19 09:06:20 -07001191 if (sVerbose) Log.v(TAG, "cancelSessionLocked(): " + getStateAsStringLocked());
Felipe Lemec24a56a2017-08-03 14:27:57 -07001192
1193 if (!isActiveLocked()) return;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001194
Svet Ganov2f8fb1f2017-03-13 00:21:04 -07001195 try {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001196 mService.cancelSession(mSessionId, mContext.getUserId());
Svet Ganov2f8fb1f2017-03-13 00:21:04 -07001197 } catch (RemoteException e) {
1198 throw e.rethrowFromSystemServer();
1199 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001200
Svet Ganov48f10a22017-04-26 18:49:30 -07001201 resetSessionLocked();
1202 }
1203
1204 private void resetSessionLocked() {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001205 mSessionId = NO_SESSION;
Felipe Lemec24a56a2017-08-03 14:27:57 -07001206 mState = STATE_UNKNOWN;
Svet Ganov48f10a22017-04-26 18:49:30 -07001207 mTrackedViews = null;
Felipe Lemec24a56a2017-08-03 14:27:57 -07001208 mFillableIds = null;
Felipe Leme2fe3ade2017-09-28 15:03:36 -07001209 mSaveTriggerId = null;
Svet Ganov2f8fb1f2017-03-13 00:21:04 -07001210 }
1211
Felipe Leme0aa4c502017-04-26 12:36:01 -07001212 private void updateSessionLocked(AutofillId id, Rect bounds, AutofillValue value, int action,
1213 int flags) {
Felipe Leme9f9ee252017-04-27 13:56:22 -07001214 if (sVerbose && action != ACTION_VIEW_EXITED) {
1215 Log.v(TAG, "updateSessionLocked(): id=" + id + ", bounds=" + bounds
1216 + ", value=" + value + ", action=" + action + ", flags=" + flags);
Felipe Lemebab851c2017-02-03 18:45:08 -08001217 }
Felipe Leme7f33cd32017-05-11 10:10:49 -07001218 boolean restartIfNecessary = (flags & FLAG_MANUAL_REQUEST) != 0;
1219
Felipe Leme3461d3c2017-01-19 08:54:55 -08001220 try {
Felipe Leme7f33cd32017-05-11 10:10:49 -07001221 if (restartIfNecessary) {
Felipe Leme637e05e2017-12-06 12:09:37 -08001222 final AutofillClient client = getClient();
1223 if (client == null) return; // NOTE: getClient() already logd it..
1224
Felipe Leme7f33cd32017-05-11 10:10:49 -07001225 final int newId = mService.updateOrRestartSession(mContext.getActivityToken(),
1226 mServiceClient.asBinder(), id, bounds, value, mContext.getUserId(),
Felipe Leme17292d12017-10-24 14:03:10 -07001227 mCallback != null, flags, client.getComponentName(), mSessionId, action);
Felipe Leme7f33cd32017-05-11 10:10:49 -07001228 if (newId != mSessionId) {
1229 if (sDebug) Log.d(TAG, "Session restarted: " + mSessionId + "=>" + newId);
1230 mSessionId = newId;
Felipe Lemec24a56a2017-08-03 14:27:57 -07001231 mState = (mSessionId == NO_SESSION) ? STATE_UNKNOWN : STATE_ACTIVE;
Felipe Leme637e05e2017-12-06 12:09:37 -08001232 client.autofillCallbackResetableStateAvailable();
Felipe Leme7f33cd32017-05-11 10:10:49 -07001233 }
1234 } else {
1235 mService.updateSession(mSessionId, id, bounds, value, action, flags,
1236 mContext.getUserId());
1237 }
1238
Felipe Leme3461d3c2017-01-19 08:54:55 -08001239 } catch (RemoteException e) {
1240 throw e.rethrowFromSystemServer();
1241 }
1242 }
Svet Ganov782043c2017-02-11 00:52:02 +00001243
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001244 private void ensureServiceClientAddedIfNeededLocked() {
Felipe Leme637e05e2017-12-06 12:09:37 -08001245 if (getClient() == null) {
Svet Ganov782043c2017-02-11 00:52:02 +00001246 return;
1247 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001248
Svet Ganov782043c2017-02-11 00:52:02 +00001249 if (mServiceClient == null) {
Felipe Leme640f30a2017-03-06 15:44:06 -08001250 mServiceClient = new AutofillManagerClient(this);
Svet Ganov782043c2017-02-11 00:52:02 +00001251 try {
Koji Fukuiccec6a62017-10-18 17:48:40 +09001252 final int userId = mContext.getUserId();
1253 final int flags = mService.addClient(mServiceClient, userId);
Felipe Leme9f9ee252017-04-27 13:56:22 -07001254 mEnabled = (flags & FLAG_ADD_CLIENT_ENABLED) != 0;
1255 sDebug = (flags & FLAG_ADD_CLIENT_DEBUG) != 0;
1256 sVerbose = (flags & FLAG_ADD_CLIENT_VERBOSE) != 0;
Koji Fukuiccec6a62017-10-18 17:48:40 +09001257 final IAutoFillManager service = mService;
1258 final IAutoFillManagerClient serviceClient = mServiceClient;
1259 mServiceClientCleaner = Cleaner.create(this, () -> {
1260 try {
1261 service.removeClient(serviceClient, userId);
1262 } catch (RemoteException e) {
1263 }
1264 });
Svet Ganov782043c2017-02-11 00:52:02 +00001265 } catch (RemoteException e) {
1266 throw e.rethrowFromSystemServer();
1267 }
1268 }
1269 }
1270
Felipe Lemee6010f22017-03-03 11:19:51 -08001271 /**
1272 * Registers a {@link AutofillCallback} to receive autofill events.
1273 *
1274 * @param callback callback to receive events.
1275 */
1276 public void registerCallback(@Nullable AutofillCallback callback) {
Svet Ganov43574b02017-04-12 09:25:20 -07001277 if (!hasAutofillFeature()) {
1278 return;
1279 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001280 synchronized (mLock) {
1281 if (callback == null) return;
Felipe Lemee6010f22017-03-03 11:19:51 -08001282
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001283 final boolean hadCallback = mCallback != null;
1284 mCallback = callback;
Felipe Lemee6010f22017-03-03 11:19:51 -08001285
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001286 if (!hadCallback) {
1287 try {
1288 mService.setHasCallback(mSessionId, mContext.getUserId(), true);
1289 } catch (RemoteException e) {
1290 throw e.rethrowFromSystemServer();
1291 }
Felipe Lemee6010f22017-03-03 11:19:51 -08001292 }
1293 }
1294 }
1295
1296 /**
1297 * Unregisters a {@link AutofillCallback} to receive autofill events.
1298 *
1299 * @param callback callback to stop receiving events.
1300 */
1301 public void unregisterCallback(@Nullable AutofillCallback callback) {
Svet Ganov43574b02017-04-12 09:25:20 -07001302 if (!hasAutofillFeature()) {
1303 return;
1304 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001305 synchronized (mLock) {
1306 if (callback == null || mCallback == null || callback != mCallback) return;
Felipe Lemee6010f22017-03-03 11:19:51 -08001307
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001308 mCallback = null;
Felipe Lemee6010f22017-03-03 11:19:51 -08001309
Felipe Lemee6010f22017-03-03 11:19:51 -08001310 try {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001311 mService.setHasCallback(mSessionId, mContext.getUserId(), false);
Felipe Lemee6010f22017-03-03 11:19:51 -08001312 } catch (RemoteException e) {
1313 throw e.rethrowFromSystemServer();
1314 }
1315 }
1316 }
1317
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001318 private void requestShowFillUi(int sessionId, AutofillId id, int width, int height,
1319 Rect anchorBounds, IAutofillWindowPresenter presenter) {
1320 final View anchor = findView(id);
Felipe Leme4753bb02017-03-22 20:24:00 -07001321 if (anchor == null) {
1322 return;
1323 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001324
1325 AutofillCallback callback = null;
1326 synchronized (mLock) {
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001327 if (mSessionId == sessionId) {
Felipe Leme637e05e2017-12-06 12:09:37 -08001328 AutofillClient client = getClient();
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001329
1330 if (client != null) {
1331 if (client.autofillCallbackRequestShowFillUi(anchor, width, height,
1332 anchorBounds, presenter) && mCallback != null) {
1333 callback = mCallback;
1334 }
1335 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001336 }
1337 }
1338
1339 if (callback != null) {
Felipe Leme4753bb02017-03-22 20:24:00 -07001340 if (id.isVirtual()) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001341 callback.onAutofillEvent(anchor, id.getVirtualChildId(),
Felipe Leme4753bb02017-03-22 20:24:00 -07001342 AutofillCallback.EVENT_INPUT_SHOWN);
1343 } else {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001344 callback.onAutofillEvent(anchor, AutofillCallback.EVENT_INPUT_SHOWN);
Felipe Leme4753bb02017-03-22 20:24:00 -07001345 }
1346 }
1347 }
1348
Svetoslav Ganova9379d02017-05-09 17:40:24 -07001349 private void authenticate(int sessionId, int authenticationId, IntentSender intent,
1350 Intent fillInIntent) {
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001351 synchronized (mLock) {
1352 if (sessionId == mSessionId) {
Felipe Leme637e05e2017-12-06 12:09:37 -08001353 final AutofillClient client = getClient();
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001354 if (client != null) {
Svetoslav Ganova9379d02017-05-09 17:40:24 -07001355 client.autofillCallbackAuthenticate(authenticationId, intent, fillInIntent);
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001356 }
1357 }
1358 }
1359 }
1360
Felipe Leme51e29da2017-10-24 14:03:10 -07001361 /** @hide */
1362 public static final int SET_STATE_FLAG_ENABLED = 0x01;
1363 /** @hide */
1364 public static final int SET_STATE_FLAG_RESET_SESSION = 0x02;
1365 /** @hide */
1366 public static final int SET_STATE_FLAG_RESET_CLIENT = 0x04;
1367 /** @hide */
1368 public static final int SET_STATE_FLAG_DEBUG = 0x08;
1369 /** @hide */
1370 public static final int SET_STATE_FLAG_VERBOSE = 0x10;
1371
1372 private void setState(int flags) {
1373 if (sVerbose) Log.v(TAG, "setState(" + flags + ")");
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001374 synchronized (mLock) {
Felipe Leme51e29da2017-10-24 14:03:10 -07001375 mEnabled = (flags & SET_STATE_FLAG_ENABLED) != 0;
1376 if (!mEnabled || (flags & SET_STATE_FLAG_RESET_SESSION) != 0) {
Svet Ganov48f10a22017-04-26 18:49:30 -07001377 // Reset the session state
1378 resetSessionLocked();
1379 }
Felipe Leme51e29da2017-10-24 14:03:10 -07001380 if ((flags & SET_STATE_FLAG_RESET_CLIENT) != 0) {
Svet Ganov48f10a22017-04-26 18:49:30 -07001381 // Reset connection to system
1382 mServiceClient = null;
Koji Fukuiccec6a62017-10-18 17:48:40 +09001383 if (mServiceClientCleaner != null) {
1384 mServiceClientCleaner.clean();
1385 mServiceClientCleaner = null;
1386 }
Svet Ganov48f10a22017-04-26 18:49:30 -07001387 }
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001388 }
Felipe Leme51e29da2017-10-24 14:03:10 -07001389 sDebug = (flags & SET_STATE_FLAG_DEBUG) != 0;
1390 sVerbose = (flags & SET_STATE_FLAG_VERBOSE) != 0;
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001391 }
1392
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07001393 /**
1394 * Sets a view as autofilled if the current value is the {code targetValue}.
1395 *
1396 * @param view The view that is to be autofilled
1397 * @param targetValue The value we want to fill into view
1398 */
1399 private void setAutofilledIfValuesIs(@NonNull View view, @Nullable AutofillValue targetValue) {
1400 AutofillValue currentValue = view.getAutofillValue();
1401 if (Objects.equals(currentValue, targetValue)) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001402 synchronized (mLock) {
1403 if (mLastAutofilledData == null) {
1404 mLastAutofilledData = new ParcelableMap(1);
1405 }
1406 mLastAutofilledData.put(getAutofillId(view), targetValue);
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07001407 }
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07001408 view.setAutofilled(true);
1409 }
1410 }
1411
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001412 private void autofill(int sessionId, List<AutofillId> ids, List<AutofillValue> values) {
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001413 synchronized (mLock) {
1414 if (sessionId != mSessionId) {
1415 return;
Felipe Leme4753bb02017-03-22 20:24:00 -07001416 }
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001417
Felipe Leme637e05e2017-12-06 12:09:37 -08001418 final AutofillClient client = getClient();
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001419 if (client == null) {
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001420 return;
1421 }
1422
1423 final int itemCount = ids.size();
1424 int numApplied = 0;
1425 ArrayMap<View, SparseArray<AutofillValue>> virtualValues = null;
Phil Weaver846cda932017-06-15 10:10:06 -07001426 final View[] views = client.findViewsByAutofillIdTraversal(getViewIds(ids));
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001427
1428 for (int i = 0; i < itemCount; i++) {
1429 final AutofillId id = ids.get(i);
1430 final AutofillValue value = values.get(i);
1431 final int viewId = id.getViewId();
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001432 final View view = views[i];
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001433 if (view == null) {
1434 Log.w(TAG, "autofill(): no View with id " + viewId);
1435 continue;
Felipe Leme4753bb02017-03-22 20:24:00 -07001436 }
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001437 if (id.isVirtual()) {
1438 if (virtualValues == null) {
1439 // Most likely there will be just one view with virtual children.
1440 virtualValues = new ArrayMap<>(1);
1441 }
1442 SparseArray<AutofillValue> valuesByParent = virtualValues.get(view);
1443 if (valuesByParent == null) {
1444 // We don't know the size yet, but usually it will be just a few fields...
1445 valuesByParent = new SparseArray<>(5);
1446 virtualValues.put(view, valuesByParent);
1447 }
1448 valuesByParent.put(id.getVirtualChildId(), value);
1449 } else {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001450 // Mark the view as to be autofilled with 'value'
1451 if (mLastAutofilledData == null) {
1452 mLastAutofilledData = new ParcelableMap(itemCount - i);
1453 }
1454 mLastAutofilledData.put(id, value);
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001455
1456 view.autofill(value);
1457
1458 // Set as autofilled if the values match now, e.g. when the value was updated
1459 // synchronously.
1460 // If autofill happens async, the view is set to autofilled in
1461 // notifyValueChanged.
1462 setAutofilledIfValuesIs(view, value);
1463
1464 numApplied++;
Philip P. Moltmannb42d1332017-03-27 09:55:30 -07001465 }
Felipe Leme4753bb02017-03-22 20:24:00 -07001466 }
Felipe Leme4753bb02017-03-22 20:24:00 -07001467
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001468 if (virtualValues != null) {
1469 for (int i = 0; i < virtualValues.size(); i++) {
1470 final View parent = virtualValues.keyAt(i);
1471 final SparseArray<AutofillValue> childrenValues = virtualValues.valueAt(i);
1472 parent.autofill(childrenValues);
1473 numApplied += childrenValues.size();
1474 }
Felipe Leme4753bb02017-03-22 20:24:00 -07001475 }
Felipe Leme4753bb02017-03-22 20:24:00 -07001476
Felipe Lemeb22d6352017-09-08 20:03:53 -07001477 final LogMaker log = new LogMaker(MetricsEvent.AUTOFILL_DATASET_APPLIED)
1478 .setPackageName(mContext.getPackageName())
1479 .addTaggedData(MetricsEvent.FIELD_AUTOFILL_NUM_VALUES, itemCount)
1480 .addTaggedData(MetricsEvent.FIELD_AUTOFILL_NUM_VIEWS_FILLED, numApplied);
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001481 mMetricsLogger.write(log);
1482 }
Felipe Leme4753bb02017-03-22 20:24:00 -07001483 }
1484
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001485 /**
1486 * Set the tracked views.
1487 *
Felipe Leme2fe3ade2017-09-28 15:03:36 -07001488 * @param trackedIds The views to be tracked.
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001489 * @param saveOnAllViewsInvisible Finish the session once all tracked views are invisible.
Felipe Leme2fe3ade2017-09-28 15:03:36 -07001490 * @param saveOnFinish Finish the session once the activity is finished.
Felipe Leme27e20222017-05-18 15:24:11 -07001491 * @param fillableIds Views that might anchor FillUI.
Felipe Leme2fe3ade2017-09-28 15:03:36 -07001492 * @param saveTriggerId View that when clicked triggers commit().
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001493 */
Felipe Leme27e20222017-05-18 15:24:11 -07001494 private void setTrackedViews(int sessionId, @Nullable AutofillId[] trackedIds,
Felipe Leme2fe3ade2017-09-28 15:03:36 -07001495 boolean saveOnAllViewsInvisible, boolean saveOnFinish,
1496 @Nullable AutofillId[] fillableIds, @Nullable AutofillId saveTriggerId) {
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001497 synchronized (mLock) {
1498 if (mEnabled && mSessionId == sessionId) {
1499 if (saveOnAllViewsInvisible) {
1500 mTrackedViews = new TrackedViews(trackedIds);
1501 } else {
1502 mTrackedViews = null;
1503 }
Felipe Leme2fe3ade2017-09-28 15:03:36 -07001504 mSaveOnFinish = saveOnFinish;
Felipe Leme27e20222017-05-18 15:24:11 -07001505 if (fillableIds != null) {
1506 if (mFillableIds == null) {
1507 mFillableIds = new ArraySet<>(fillableIds.length);
1508 }
1509 for (AutofillId id : fillableIds) {
1510 mFillableIds.add(id);
1511 }
1512 if (sVerbose) {
1513 Log.v(TAG, "setTrackedViews(): fillableIds=" + fillableIds
1514 + ", mFillableIds" + mFillableIds);
1515 }
1516 }
Felipe Leme2fe3ade2017-09-28 15:03:36 -07001517
1518 if (mSaveTriggerId != null && !mSaveTriggerId.equals(saveTriggerId)) {
1519 // Turn off trigger on previous view id.
1520 setNotifyOnClickLocked(mSaveTriggerId, false);
1521 }
1522
1523 if (saveTriggerId != null && !saveTriggerId.equals(mSaveTriggerId)) {
1524 // Turn on trigger on new view id.
1525 mSaveTriggerId = saveTriggerId;
1526 setNotifyOnClickLocked(mSaveTriggerId, true);
1527 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001528 }
1529 }
1530 }
1531
Felipe Leme2fe3ade2017-09-28 15:03:36 -07001532 private void setNotifyOnClickLocked(@NonNull AutofillId id, boolean notify) {
1533 final View view = findView(id);
1534 if (view == null) {
1535 Log.w(TAG, "setNotifyOnClick(): invalid id: " + id);
1536 return;
1537 }
1538 view.setNotifyAutofillManagerOnClick(notify);
1539 }
1540
Felipe Lemec24a56a2017-08-03 14:27:57 -07001541 private void setSaveUiState(int sessionId, boolean shown) {
1542 if (sDebug) Log.d(TAG, "setSaveUiState(" + sessionId + "): " + shown);
1543 synchronized (mLock) {
1544 if (mSessionId != NO_SESSION) {
1545 // Race condition: app triggered a new session after the previous session was
1546 // finished but before server called setSaveUiState() - need to cancel the new
1547 // session to avoid further inconsistent behavior.
1548 Log.w(TAG, "setSaveUiState(" + sessionId + ", " + shown
1549 + ") called on existing session " + mSessionId + "; cancelling it");
1550 cancelSessionLocked();
1551 }
1552 if (shown) {
1553 mSessionId = sessionId;
1554 mState = STATE_SHOWING_SAVE_UI;
1555 } else {
1556 mSessionId = NO_SESSION;
1557 mState = STATE_UNKNOWN;
1558 }
1559 }
1560 }
1561
Felipe Leme650f7ab2017-09-19 13:08:24 -07001562 /**
1563 * Marks the state of the session as finished.
1564 *
1565 * @param newState {@link #STATE_FINISHED} (because the autofill service returned a {@code null}
Felipe Leme17292d12017-10-24 14:03:10 -07001566 * FillResponse), {@link #STATE_UNKNOWN} (because the session was removed), or
1567 * {@link #STATE_DISABLED_BY_SERVICE} (because the autofill service disabled further autofill
1568 * requests for the activity).
Felipe Leme650f7ab2017-09-19 13:08:24 -07001569 */
1570 private void setSessionFinished(int newState) {
Felipe Lemec7b45292017-09-19 09:06:20 -07001571 synchronized (mLock) {
Felipe Leme650f7ab2017-09-19 13:08:24 -07001572 if (sVerbose) Log.v(TAG, "setSessionFinished(): from " + mState + " to " + newState);
Felipe Lemec7b45292017-09-19 09:06:20 -07001573 resetSessionLocked();
Felipe Leme650f7ab2017-09-19 13:08:24 -07001574 mState = newState;
Felipe Lemec7b45292017-09-19 09:06:20 -07001575 }
1576 }
1577
Felipe Leme27e20222017-05-18 15:24:11 -07001578 private void requestHideFillUi(AutofillId id) {
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001579 final View anchor = findView(id);
Felipe Leme27e20222017-05-18 15:24:11 -07001580 if (sVerbose) Log.v(TAG, "requestHideFillUi(" + id + "): anchor = " + anchor);
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001581 if (anchor == null) {
1582 return;
1583 }
Felipe Leme27e20222017-05-18 15:24:11 -07001584 requestHideFillUi(id, anchor);
1585 }
1586
1587 private void requestHideFillUi(AutofillId id, View anchor) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001588
1589 AutofillCallback callback = null;
1590 synchronized (mLock) {
Svet Ganov48f10a22017-04-26 18:49:30 -07001591 // We do not check the session id for two reasons:
1592 // 1. If local and remote session id are off sync the UI would be stuck shown
1593 // 2. There is a race between the user state being destroyed due the fill
1594 // service being uninstalled and the UI being dismissed.
Felipe Leme637e05e2017-12-06 12:09:37 -08001595 AutofillClient client = getClient();
Svet Ganov48f10a22017-04-26 18:49:30 -07001596 if (client != null) {
1597 if (client.autofillCallbackRequestHideFillUi() && mCallback != null) {
1598 callback = mCallback;
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001599 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001600 }
1601 }
1602
1603 if (callback != null) {
Felipe Leme4753bb02017-03-22 20:24:00 -07001604 if (id.isVirtual()) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001605 callback.onAutofillEvent(anchor, id.getVirtualChildId(),
Felipe Leme4753bb02017-03-22 20:24:00 -07001606 AutofillCallback.EVENT_INPUT_HIDDEN);
1607 } else {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001608 callback.onAutofillEvent(anchor, AutofillCallback.EVENT_INPUT_HIDDEN);
Felipe Leme4753bb02017-03-22 20:24:00 -07001609 }
1610 }
1611 }
1612
Felipe Leme17292d12017-10-24 14:03:10 -07001613 private void notifyNoFillUi(int sessionId, AutofillId id, int sessionFinishedState) {
Felipe Lemec7b45292017-09-19 09:06:20 -07001614 if (sVerbose) {
1615 Log.v(TAG, "notifyNoFillUi(): sessionId=" + sessionId + ", autofillId=" + id
Felipe Leme17292d12017-10-24 14:03:10 -07001616 + ", sessionFinishedState=" + sessionFinishedState);
Felipe Lemec7b45292017-09-19 09:06:20 -07001617 }
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001618 final View anchor = findView(id);
1619 if (anchor == null) {
1620 return;
1621 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001622
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001623 AutofillCallback callback = null;
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001624 synchronized (mLock) {
Felipe Leme637e05e2017-12-06 12:09:37 -08001625 if (mSessionId == sessionId && getClient() != null) {
Philip P. Moltmanne048a652017-04-11 10:13:33 -07001626 callback = mCallback;
1627 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001628 }
1629
1630 if (callback != null) {
Felipe Leme4753bb02017-03-22 20:24:00 -07001631 if (id.isVirtual()) {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001632 callback.onAutofillEvent(anchor, id.getVirtualChildId(),
Felipe Leme4753bb02017-03-22 20:24:00 -07001633 AutofillCallback.EVENT_INPUT_UNAVAILABLE);
1634 } else {
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001635 callback.onAutofillEvent(anchor, AutofillCallback.EVENT_INPUT_UNAVAILABLE);
Felipe Leme4753bb02017-03-22 20:24:00 -07001636 }
Felipe Lemec7b45292017-09-19 09:06:20 -07001637 }
Philip P. Moltmanneab62ba2017-03-20 10:55:43 -07001638
Felipe Leme17292d12017-10-24 14:03:10 -07001639 if (sessionFinishedState != 0) {
Felipe Lemec7b45292017-09-19 09:06:20 -07001640 // Callback call was "hijacked" to also update the session state.
Felipe Leme17292d12017-10-24 14:03:10 -07001641 setSessionFinished(sessionFinishedState);
Felipe Leme4753bb02017-03-22 20:24:00 -07001642 }
1643 }
1644
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001645 /**
1646 * Get an array of viewIds from a List of {@link AutofillId}.
1647 *
1648 * @param autofillIds The autofill ids to convert
1649 *
1650 * @return The array of viewIds.
1651 */
Felipe Leme27e20222017-05-18 15:24:11 -07001652 // TODO: move to Helper as static method
1653 @NonNull private int[] getViewIds(@NonNull AutofillId[] autofillIds) {
1654 final int numIds = autofillIds.length;
1655 final int[] viewIds = new int[numIds];
1656 for (int i = 0; i < numIds; i++) {
1657 viewIds[i] = autofillIds[i].getViewId();
1658 }
1659
1660 return viewIds;
1661 }
1662
1663 // TODO: move to Helper as static method
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001664 @NonNull private int[] getViewIds(@NonNull List<AutofillId> autofillIds) {
1665 final int numIds = autofillIds.size();
1666 final int[] viewIds = new int[numIds];
1667 for (int i = 0; i < numIds; i++) {
1668 viewIds[i] = autofillIds.get(i).getViewId();
1669 }
1670
1671 return viewIds;
1672 }
1673
1674 /**
1675 * Find a single view by its id.
1676 *
1677 * @param autofillId The autofill id of the view
1678 *
1679 * @return The view or {@code null} if view was not found
1680 */
1681 private View findView(@NonNull AutofillId autofillId) {
Felipe Leme637e05e2017-12-06 12:09:37 -08001682 final AutofillClient client = getClient();
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001683
1684 if (client == null) {
Felipe Leme4753bb02017-03-22 20:24:00 -07001685 return null;
Felipe Lemee6010f22017-03-03 11:19:51 -08001686 }
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001687
Phil Weaver846cda932017-06-15 10:10:06 -07001688 return client.findViewByAutofillIdTraversal(autofillId.getViewId());
Felipe Lemee6010f22017-03-03 11:19:51 -08001689 }
1690
Felipe Lemebb810922017-04-25 15:54:06 -07001691 /** @hide */
1692 public boolean hasAutofillFeature() {
Svet Ganov43574b02017-04-12 09:25:20 -07001693 return mService != null;
1694 }
1695
Felipe Lemec24a56a2017-08-03 14:27:57 -07001696 /** @hide */
1697 public void onPendingSaveUi(int operation, IBinder token) {
1698 if (sVerbose) Log.v(TAG, "onPendingSaveUi(" + operation + "): " + token);
1699
1700 synchronized (mLock) {
1701 try {
1702 mService.onPendingSaveUi(operation, token);
1703 } catch (RemoteException e) {
1704 e.rethrowFromSystemServer();
1705 }
1706 }
1707 }
1708
1709 /** @hide */
1710 public void dump(String outerPrefix, PrintWriter pw) {
1711 pw.print(outerPrefix); pw.println("AutofillManager:");
1712 final String pfx = outerPrefix + " ";
1713 pw.print(pfx); pw.print("sessionId: "); pw.println(mSessionId);
Felipe Lemec7b45292017-09-19 09:06:20 -07001714 pw.print(pfx); pw.print("state: "); pw.println(getStateAsStringLocked());
Felipe Leme686128e2017-10-17 14:02:20 -07001715 pw.print(pfx); pw.print("context: "); pw.println(mContext);
Felipe Leme637e05e2017-12-06 12:09:37 -08001716 pw.print(pfx); pw.print("client: "); pw.println(getClient());
Felipe Lemec24a56a2017-08-03 14:27:57 -07001717 pw.print(pfx); pw.print("enabled: "); pw.println(mEnabled);
1718 pw.print(pfx); pw.print("hasService: "); pw.println(mService != null);
1719 pw.print(pfx); pw.print("hasCallback: "); pw.println(mCallback != null);
1720 pw.print(pfx); pw.print("last autofilled data: "); pw.println(mLastAutofilledData);
1721 pw.print(pfx); pw.print("tracked views: ");
1722 if (mTrackedViews == null) {
1723 pw.println("null");
1724 } else {
1725 final String pfx2 = pfx + " ";
1726 pw.println();
1727 pw.print(pfx2); pw.print("visible:"); pw.println(mTrackedViews.mVisibleTrackedIds);
1728 pw.print(pfx2); pw.print("invisible:"); pw.println(mTrackedViews.mInvisibleTrackedIds);
1729 }
1730 pw.print(pfx); pw.print("fillable ids: "); pw.println(mFillableIds);
Felipe Leme2fe3ade2017-09-28 15:03:36 -07001731 pw.print(pfx); pw.print("save trigger id: "); pw.println(mSaveTriggerId);
1732 pw.print(pfx); pw.print("save on finish(): "); pw.println(mSaveOnFinish);
Felipe Leme51e29da2017-10-24 14:03:10 -07001733 pw.print(pfx); pw.print("debug: "); pw.print(sDebug);
1734 pw.print(" verbose: "); pw.println(sVerbose);
Felipe Lemec24a56a2017-08-03 14:27:57 -07001735 }
1736
Felipe Lemec7b45292017-09-19 09:06:20 -07001737 private String getStateAsStringLocked() {
1738 switch (mState) {
1739 case STATE_UNKNOWN:
1740 return "STATE_UNKNOWN";
1741 case STATE_ACTIVE:
1742 return "STATE_ACTIVE";
1743 case STATE_FINISHED:
1744 return "STATE_FINISHED";
1745 case STATE_SHOWING_SAVE_UI:
1746 return "STATE_SHOWING_SAVE_UI";
Felipe Leme17292d12017-10-24 14:03:10 -07001747 case STATE_DISABLED_BY_SERVICE:
1748 return "STATE_DISABLED_BY_SERVICE";
Felipe Lemec7b45292017-09-19 09:06:20 -07001749 default:
1750 return "INVALID:" + mState;
1751 }
1752 }
1753
Felipe Lemec24a56a2017-08-03 14:27:57 -07001754 private boolean isActiveLocked() {
1755 return mState == STATE_ACTIVE;
1756 }
1757
Felipe Leme17292d12017-10-24 14:03:10 -07001758 private boolean isDisabledByService() {
1759 return mState == STATE_DISABLED_BY_SERVICE;
Felipe Lemec7b45292017-09-19 09:06:20 -07001760 }
1761
Felipe Leme9876a6f2017-05-30 15:47:28 -07001762 private void post(Runnable runnable) {
Felipe Leme637e05e2017-12-06 12:09:37 -08001763 final AutofillClient client = getClient();
Felipe Leme9876a6f2017-05-30 15:47:28 -07001764 if (client == null) {
1765 if (sVerbose) Log.v(TAG, "ignoring post() because client is null");
1766 return;
1767 }
1768 client.runOnUiThread(runnable);
1769 }
1770
Felipe Lemee6010f22017-03-03 11:19:51 -08001771 /**
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001772 * View tracking information. Once all tracked views become invisible the session is finished.
1773 */
1774 private class TrackedViews {
1775 /** Visible tracked views */
1776 @Nullable private ArraySet<AutofillId> mVisibleTrackedIds;
1777
1778 /** Invisible tracked views */
1779 @Nullable private ArraySet<AutofillId> mInvisibleTrackedIds;
1780
1781 /**
1782 * Check if set is null or value is in set.
1783 *
1784 * @param set The set or null (== empty set)
1785 * @param value The value that might be in the set
1786 *
1787 * @return {@code true} iff set is not empty and value is in set
1788 */
Felipe Leme27e20222017-05-18 15:24:11 -07001789 // TODO: move to Helper as static method
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001790 private <T> boolean isInSet(@Nullable ArraySet<T> set, T value) {
1791 return set != null && set.contains(value);
1792 }
1793
1794 /**
1795 * Add a value to a set. If set is null, create a new set.
1796 *
1797 * @param set The set or null (== empty set)
1798 * @param valueToAdd The value to add
1799 *
1800 * @return The set including the new value. If set was {@code null}, a set containing only
1801 * the new value.
1802 */
Felipe Leme27e20222017-05-18 15:24:11 -07001803 // TODO: move to Helper as static method
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001804 @NonNull
1805 private <T> ArraySet<T> addToSet(@Nullable ArraySet<T> set, T valueToAdd) {
1806 if (set == null) {
1807 set = new ArraySet<>(1);
1808 }
1809
1810 set.add(valueToAdd);
1811
1812 return set;
1813 }
1814
1815 /**
1816 * Remove a value from a set.
1817 *
1818 * @param set The set or null (== empty set)
1819 * @param valueToRemove The value to remove
1820 *
1821 * @return The set without the removed value. {@code null} if set was null, or is empty
1822 * after removal.
1823 */
Felipe Leme27e20222017-05-18 15:24:11 -07001824 // TODO: move to Helper as static method
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001825 @Nullable
1826 private <T> ArraySet<T> removeFromSet(@Nullable ArraySet<T> set, T valueToRemove) {
1827 if (set == null) {
1828 return null;
1829 }
1830
1831 set.remove(valueToRemove);
1832
1833 if (set.isEmpty()) {
1834 return null;
1835 }
1836
1837 return set;
1838 }
1839
1840 /**
1841 * Set the tracked views.
1842 *
1843 * @param trackedIds The views to be tracked
1844 */
Felipe Leme27e20222017-05-18 15:24:11 -07001845 TrackedViews(@Nullable AutofillId[] trackedIds) {
Felipe Leme637e05e2017-12-06 12:09:37 -08001846 final AutofillClient client = getClient();
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001847 if (trackedIds != null && client != null) {
1848 final boolean[] isVisible;
1849
1850 if (client.isVisibleForAutofill()) {
1851 isVisible = client.getViewVisibility(getViewIds(trackedIds));
1852 } else {
1853 // All false
Felipe Leme27e20222017-05-18 15:24:11 -07001854 isVisible = new boolean[trackedIds.length];
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001855 }
1856
Felipe Leme27e20222017-05-18 15:24:11 -07001857 final int numIds = trackedIds.length;
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001858 for (int i = 0; i < numIds; i++) {
Felipe Leme27e20222017-05-18 15:24:11 -07001859 final AutofillId id = trackedIds[i];
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001860
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001861 if (isVisible[i]) {
Philip P. Moltmanne0e28712017-04-20 15:19:06 -07001862 mVisibleTrackedIds = addToSet(mVisibleTrackedIds, id);
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001863 } else {
1864 mInvisibleTrackedIds = addToSet(mInvisibleTrackedIds, id);
1865 }
1866 }
1867 }
1868
Felipe Leme9f9ee252017-04-27 13:56:22 -07001869 if (sVerbose) {
1870 Log.v(TAG, "TrackedViews(trackedIds=" + trackedIds + "): "
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001871 + " mVisibleTrackedIds=" + mVisibleTrackedIds
1872 + " mInvisibleTrackedIds=" + mInvisibleTrackedIds);
1873 }
1874
1875 if (mVisibleTrackedIds == null) {
1876 finishSessionLocked();
1877 }
1878 }
1879
1880 /**
1881 * Called when a {@link View view's} visibility changes.
1882 *
Svetoslav Ganov4db89ba2017-06-28 16:50:37 -07001883 * @param id the id of the view/virtual view whose visibility changed.
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001884 * @param isVisible visible if the view is visible in the view hierarchy.
1885 */
Svetoslav Ganov4db89ba2017-06-28 16:50:37 -07001886 void notifyViewVisibilityChanged(@NonNull AutofillId id, boolean isVisible) {
Felipe Leme637e05e2017-12-06 12:09:37 -08001887 AutofillClient client = getClient();
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001888
Felipe Leme9f9ee252017-04-27 13:56:22 -07001889 if (sDebug) {
Svetoslav Ganov4db89ba2017-06-28 16:50:37 -07001890 Log.d(TAG, "notifyViewVisibilityChanged(): id=" + id + " isVisible="
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001891 + isVisible);
1892 }
1893
1894 if (client != null && client.isVisibleForAutofill()) {
1895 if (isVisible) {
1896 if (isInSet(mInvisibleTrackedIds, id)) {
1897 mInvisibleTrackedIds = removeFromSet(mInvisibleTrackedIds, id);
1898 mVisibleTrackedIds = addToSet(mVisibleTrackedIds, id);
1899 }
1900 } else {
1901 if (isInSet(mVisibleTrackedIds, id)) {
1902 mVisibleTrackedIds = removeFromSet(mVisibleTrackedIds, id);
1903 mInvisibleTrackedIds = addToSet(mInvisibleTrackedIds, id);
1904 }
1905 }
1906 }
1907
1908 if (mVisibleTrackedIds == null) {
Felipe Leme27e20222017-05-18 15:24:11 -07001909 if (sVerbose) {
1910 Log.v(TAG, "No more visible ids. Invisibile = " + mInvisibleTrackedIds);
1911 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001912 finishSessionLocked();
1913 }
1914 }
1915
1916 /**
1917 * Called once the client becomes visible.
1918 *
1919 * @see AutofillClient#isVisibleForAutofill()
1920 */
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001921 void onVisibleForAutofillLocked() {
1922 // The visibility of the views might have changed while the client was not be visible,
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001923 // hence update the visibility state for all views.
Felipe Leme637e05e2017-12-06 12:09:37 -08001924 AutofillClient client = getClient();
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001925 ArraySet<AutofillId> updatedVisibleTrackedIds = null;
1926 ArraySet<AutofillId> updatedInvisibleTrackedIds = null;
1927 if (client != null) {
1928 if (mInvisibleTrackedIds != null) {
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001929 final ArrayList<AutofillId> orderedInvisibleIds =
1930 new ArrayList<>(mInvisibleTrackedIds);
1931 final boolean[] isVisible = client.getViewVisibility(
1932 getViewIds(orderedInvisibleIds));
1933
1934 final int numInvisibleTrackedIds = orderedInvisibleIds.size();
1935 for (int i = 0; i < numInvisibleTrackedIds; i++) {
1936 final AutofillId id = orderedInvisibleIds.get(i);
1937 if (isVisible[i]) {
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001938 updatedVisibleTrackedIds = addToSet(updatedVisibleTrackedIds, id);
1939
Felipe Leme9f9ee252017-04-27 13:56:22 -07001940 if (sDebug) {
1941 Log.d(TAG, "onVisibleForAutofill() " + id + " became visible");
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001942 }
1943 } else {
1944 updatedInvisibleTrackedIds = addToSet(updatedInvisibleTrackedIds, id);
1945 }
1946 }
1947 }
1948
1949 if (mVisibleTrackedIds != null) {
Philip P. Moltmann134cee22017-05-06 11:28:38 -07001950 final ArrayList<AutofillId> orderedVisibleIds =
1951 new ArrayList<>(mVisibleTrackedIds);
1952 final boolean[] isVisible = client.getViewVisibility(
1953 getViewIds(orderedVisibleIds));
1954
1955 final int numVisibleTrackedIds = orderedVisibleIds.size();
1956 for (int i = 0; i < numVisibleTrackedIds; i++) {
1957 final AutofillId id = orderedVisibleIds.get(i);
1958
1959 if (isVisible[i]) {
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001960 updatedVisibleTrackedIds = addToSet(updatedVisibleTrackedIds, id);
1961 } else {
1962 updatedInvisibleTrackedIds = addToSet(updatedInvisibleTrackedIds, id);
1963
Felipe Leme9f9ee252017-04-27 13:56:22 -07001964 if (sDebug) {
1965 Log.d(TAG, "onVisibleForAutofill() " + id + " became invisible");
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07001966 }
1967 }
1968 }
1969 }
1970
1971 mInvisibleTrackedIds = updatedInvisibleTrackedIds;
1972 mVisibleTrackedIds = updatedVisibleTrackedIds;
1973 }
1974
1975 if (mVisibleTrackedIds == null) {
1976 finishSessionLocked();
1977 }
1978 }
1979 }
1980
1981 /**
Felipe Leme744976e2017-07-31 11:34:14 -07001982 * Callback for autofill related events.
Felipe Lemee6010f22017-03-03 11:19:51 -08001983 *
1984 * <p>Typically used for applications that display their own "auto-complete" views, so they can
Felipe Leme2fe3ade2017-09-28 15:03:36 -07001985 * enable / disable such views when the autofill UI is shown / hidden.
Felipe Lemee6010f22017-03-03 11:19:51 -08001986 */
1987 public abstract static class AutofillCallback {
1988
1989 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -07001990 @IntDef(prefix = { "EVENT_INPUT_" }, value = {
1991 EVENT_INPUT_SHOWN,
1992 EVENT_INPUT_HIDDEN,
1993 EVENT_INPUT_UNAVAILABLE
1994 })
Felipe Lemee6010f22017-03-03 11:19:51 -08001995 @Retention(RetentionPolicy.SOURCE)
1996 public @interface AutofillEventType {}
1997
1998 /**
Felipe Leme2fe3ade2017-09-28 15:03:36 -07001999 * The autofill input UI associated with the view was shown.
Felipe Lemee6010f22017-03-03 11:19:51 -08002000 *
Felipe Leme2fe3ade2017-09-28 15:03:36 -07002001 * <p>If the view provides its own auto-complete UI and its currently shown, it
Felipe Lemee6010f22017-03-03 11:19:51 -08002002 * should be hidden upon receiving this event.
2003 */
2004 public static final int EVENT_INPUT_SHOWN = 1;
2005
2006 /**
Felipe Leme2fe3ade2017-09-28 15:03:36 -07002007 * The autofill input UI associated with the view was hidden.
Felipe Lemee6010f22017-03-03 11:19:51 -08002008 *
Felipe Leme2fe3ade2017-09-28 15:03:36 -07002009 * <p>If the view provides its own auto-complete UI that was hidden upon a
Felipe Lemee6010f22017-03-03 11:19:51 -08002010 * {@link #EVENT_INPUT_SHOWN} event, it could be shown again now.
2011 */
2012 public static final int EVENT_INPUT_HIDDEN = 2;
2013
2014 /**
Felipe Leme2fe3ade2017-09-28 15:03:36 -07002015 * The autofill input UI associated with the view isn't shown because
Felipe Leme24aae152017-03-15 12:33:01 -07002016 * autofill is not available.
2017 *
Felipe Leme2fe3ade2017-09-28 15:03:36 -07002018 * <p>If the view provides its own auto-complete UI but was not displaying it
Felipe Leme24aae152017-03-15 12:33:01 -07002019 * to avoid flickering, it could shown it upon receiving this event.
2020 */
2021 public static final int EVENT_INPUT_UNAVAILABLE = 3;
2022
2023 /**
Felipe Lemee6010f22017-03-03 11:19:51 -08002024 * Called after a change in the autofill state associated with a view.
2025 *
2026 * @param view view associated with the change.
2027 *
2028 * @param event currently either {@link #EVENT_INPUT_SHOWN} or {@link #EVENT_INPUT_HIDDEN}.
2029 */
Felipe Leme81f01d92017-03-16 17:13:25 -07002030 public void onAutofillEvent(@NonNull View view, @AutofillEventType int event) {
2031 }
Felipe Lemee6010f22017-03-03 11:19:51 -08002032
2033 /**
2034 * Called after a change in the autofill state associated with a virtual view.
2035 *
2036 * @param view parent view associated with the change.
Felipe Leme6dcec872017-05-25 11:24:23 -07002037 * @param virtualId id identifying the virtual child inside the parent view.
Felipe Lemee6010f22017-03-03 11:19:51 -08002038 *
2039 * @param event currently either {@link #EVENT_INPUT_SHOWN} or {@link #EVENT_INPUT_HIDDEN}.
2040 */
Felipe Leme6dcec872017-05-25 11:24:23 -07002041 public void onAutofillEvent(@NonNull View view, int virtualId,
2042 @AutofillEventType int event) {
Felipe Leme81f01d92017-03-16 17:13:25 -07002043 }
Felipe Lemee6010f22017-03-03 11:19:51 -08002044 }
2045
Felipe Leme640f30a2017-03-06 15:44:06 -08002046 private static final class AutofillManagerClient extends IAutoFillManagerClient.Stub {
2047 private final WeakReference<AutofillManager> mAfm;
Svet Ganov782043c2017-02-11 00:52:02 +00002048
Felipe Leme640f30a2017-03-06 15:44:06 -08002049 AutofillManagerClient(AutofillManager autofillManager) {
2050 mAfm = new WeakReference<>(autofillManager);
Svet Ganov782043c2017-02-11 00:52:02 +00002051 }
2052
2053 @Override
Felipe Leme51e29da2017-10-24 14:03:10 -07002054 public void setState(int flags) {
Felipe Leme640f30a2017-03-06 15:44:06 -08002055 final AutofillManager afm = mAfm.get();
2056 if (afm != null) {
Felipe Leme51e29da2017-10-24 14:03:10 -07002057 afm.post(() -> afm.setState(flags));
Svet Ganov782043c2017-02-11 00:52:02 +00002058 }
2059 }
2060
2061 @Override
Philip P. Moltmann134cee22017-05-06 11:28:38 -07002062 public void autofill(int sessionId, List<AutofillId> ids, List<AutofillValue> values) {
Felipe Leme640f30a2017-03-06 15:44:06 -08002063 final AutofillManager afm = mAfm.get();
2064 if (afm != null) {
Felipe Leme9876a6f2017-05-30 15:47:28 -07002065 afm.post(() -> afm.autofill(sessionId, ids, values));
Svet Ganov782043c2017-02-11 00:52:02 +00002066 }
2067 }
2068
2069 @Override
Svetoslav Ganova9379d02017-05-09 17:40:24 -07002070 public void authenticate(int sessionId, int authenticationId, IntentSender intent,
2071 Intent fillInIntent) {
Felipe Leme640f30a2017-03-06 15:44:06 -08002072 final AutofillManager afm = mAfm.get();
2073 if (afm != null) {
Felipe Leme9876a6f2017-05-30 15:47:28 -07002074 afm.post(() -> afm.authenticate(sessionId, authenticationId, intent, fillInIntent));
Svet Ganov782043c2017-02-11 00:52:02 +00002075 }
2076 }
Felipe Lemee6010f22017-03-03 11:19:51 -08002077
2078 @Override
Philip P. Moltmann134cee22017-05-06 11:28:38 -07002079 public void requestShowFillUi(int sessionId, AutofillId id, int width, int height,
2080 Rect anchorBounds, IAutofillWindowPresenter presenter) {
Felipe Leme640f30a2017-03-06 15:44:06 -08002081 final AutofillManager afm = mAfm.get();
2082 if (afm != null) {
Felipe Leme9876a6f2017-05-30 15:47:28 -07002083 afm.post(() -> afm.requestShowFillUi(sessionId, id, width, height, anchorBounds,
2084 presenter));
Felipe Leme4753bb02017-03-22 20:24:00 -07002085 }
2086 }
2087
2088 @Override
Philip P. Moltmann134cee22017-05-06 11:28:38 -07002089 public void requestHideFillUi(int sessionId, AutofillId id) {
Felipe Leme4753bb02017-03-22 20:24:00 -07002090 final AutofillManager afm = mAfm.get();
2091 if (afm != null) {
Felipe Leme9876a6f2017-05-30 15:47:28 -07002092 afm.post(() -> afm.requestHideFillUi(id));
Felipe Leme4753bb02017-03-22 20:24:00 -07002093 }
2094 }
2095
2096 @Override
Felipe Leme17292d12017-10-24 14:03:10 -07002097 public void notifyNoFillUi(int sessionId, AutofillId id, int sessionFinishedState) {
Felipe Leme4753bb02017-03-22 20:24:00 -07002098 final AutofillManager afm = mAfm.get();
2099 if (afm != null) {
Felipe Leme17292d12017-10-24 14:03:10 -07002100 afm.post(() -> afm.notifyNoFillUi(sessionId, id, sessionFinishedState));
Felipe Lemee6010f22017-03-03 11:19:51 -08002101 }
2102 }
Svet Ganovc3d1c852017-04-12 22:34:28 -07002103
2104 @Override
Felipe Lemec24a56a2017-08-03 14:27:57 -07002105 public void startIntentSender(IntentSender intentSender, Intent intent) {
Svet Ganovc3d1c852017-04-12 22:34:28 -07002106 final AutofillManager afm = mAfm.get();
2107 if (afm != null) {
Felipe Leme9876a6f2017-05-30 15:47:28 -07002108 afm.post(() -> {
Svet Ganovc3d1c852017-04-12 22:34:28 -07002109 try {
Felipe Lemec24a56a2017-08-03 14:27:57 -07002110 afm.mContext.startIntentSender(intentSender, intent, 0, 0, 0);
Svet Ganovc3d1c852017-04-12 22:34:28 -07002111 } catch (IntentSender.SendIntentException e) {
2112 Log.e(TAG, "startIntentSender() failed for intent:" + intentSender, e);
2113 }
2114 });
2115 }
2116 }
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07002117
2118 @Override
Felipe Leme27e20222017-05-18 15:24:11 -07002119 public void setTrackedViews(int sessionId, AutofillId[] ids,
Felipe Leme2fe3ade2017-09-28 15:03:36 -07002120 boolean saveOnAllViewsInvisible, boolean saveOnFinish, AutofillId[] fillableIds,
2121 AutofillId saveTriggerId) {
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07002122 final AutofillManager afm = mAfm.get();
2123 if (afm != null) {
Felipe Leme2fe3ade2017-09-28 15:03:36 -07002124 afm.post(() -> afm.setTrackedViews(sessionId, ids, saveOnAllViewsInvisible,
2125 saveOnFinish, fillableIds, saveTriggerId));
Philip P. Moltmann494c3f52017-04-11 10:13:33 -07002126 }
2127 }
Felipe Lemec24a56a2017-08-03 14:27:57 -07002128
2129 @Override
2130 public void setSaveUiState(int sessionId, boolean shown) {
2131 final AutofillManager afm = mAfm.get();
2132 if (afm != null) {
Felipe Lemec7b45292017-09-19 09:06:20 -07002133 afm.post(() -> afm.setSaveUiState(sessionId, shown));
2134 }
2135 }
2136
2137 @Override
Felipe Leme650f7ab2017-09-19 13:08:24 -07002138 public void setSessionFinished(int newState) {
Felipe Lemec7b45292017-09-19 09:06:20 -07002139 final AutofillManager afm = mAfm.get();
2140 if (afm != null) {
Felipe Leme650f7ab2017-09-19 13:08:24 -07002141 afm.post(() -> afm.setSessionFinished(newState));
Felipe Lemec24a56a2017-08-03 14:27:57 -07002142 }
2143 }
Svet Ganov782043c2017-02-11 00:52:02 +00002144 }
Felipe Leme3461d3c2017-01-19 08:54:55 -08002145}