Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.view.autofill; |
| 18 | |
Felipe Leme | 73fedac | 2017-05-12 09:52:07 -0700 | [diff] [blame] | 19 | import static android.service.autofill.FillRequest.FLAG_MANUAL_REQUEST; |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 20 | import static android.view.autofill.Helper.sDebug; |
| 21 | import static android.view.autofill.Helper.sVerbose; |
Felipe Leme | d633f07 | 2017-02-14 10:17:17 -0800 | [diff] [blame] | 22 | |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 23 | import android.annotation.IntDef; |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 24 | import android.annotation.NonNull; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 25 | import android.annotation.Nullable; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 26 | import android.annotation.SystemService; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 27 | import android.content.Context; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 28 | import android.content.Intent; |
| 29 | import android.content.IntentSender; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 30 | import android.graphics.Rect; |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 31 | import android.metrics.LogMaker; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 32 | import android.os.Bundle; |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 33 | import android.os.IBinder; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 34 | import android.os.Parcelable; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 35 | import android.os.RemoteException; |
Philip P. Moltmann | cc684ed | 2017-04-17 14:18:33 -0700 | [diff] [blame] | 36 | import android.service.autofill.AutofillService; |
| 37 | import android.service.autofill.FillEventHistory; |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 38 | import android.util.ArrayMap; |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 39 | import android.util.ArraySet; |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 40 | import android.util.DebugUtils; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 41 | import android.util.Log; |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 42 | import android.util.SparseArray; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 43 | import android.view.View; |
| 44 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 45 | import com.android.internal.annotations.GuardedBy; |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 46 | import com.android.internal.logging.MetricsLogger; |
Felipe Leme | b22d635 | 2017-09-08 20:03:53 -0700 | [diff] [blame^] | 47 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 48 | |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 49 | import java.io.PrintWriter; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 50 | import java.lang.annotation.Retention; |
| 51 | import java.lang.annotation.RetentionPolicy; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 52 | import java.lang.ref.WeakReference; |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 53 | import java.util.ArrayList; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 54 | import java.util.List; |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 55 | import java.util.Objects; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 56 | |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 57 | /** |
Felipe Leme | 744976e | 2017-07-31 11:34:14 -0700 | [diff] [blame] | 58 | * The {@link AutofillManager} provides ways for apps and custom views to integrate with the |
| 59 | * Autofill Framework lifecycle. |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 60 | * |
Felipe Leme | 744976e | 2017-07-31 11:34:14 -0700 | [diff] [blame] | 61 | * <p>The autofill lifecycle starts with the creation of an autofill context associated with an |
| 62 | * activity context; the autofill context is created when one of the following methods is called for |
| 63 | * the first time in an activity context, and the current user has an enabled autofill service: |
| 64 | * |
| 65 | * <ul> |
| 66 | * <li>{@link #notifyViewEntered(View)} |
| 67 | * <li>{@link #notifyViewEntered(View, int, Rect)} |
| 68 | * <li>{@link #requestAutofill(View)} |
| 69 | * </ul> |
| 70 | * |
| 71 | * <p>Tipically, the context is automatically created when the first view of the activity is |
| 72 | * focused because {@code View.onFocusChanged()} indirectly calls |
| 73 | * {@link #notifyViewEntered(View)}. App developers can call {@link #requestAutofill(View)} to |
| 74 | * explicitly create it (for example, a custom view developer could offer a contextual menu action |
| 75 | * in a text-field view to let users manually request autofill). |
| 76 | * |
| 77 | * <p>After the context is created, the Android System creates a {@link android.view.ViewStructure} |
| 78 | * that represents the view hierarchy by calling |
| 79 | * {@link View#dispatchProvideAutofillStructure(android.view.ViewStructure, int)} in the root views |
| 80 | * of all application windows. By default, {@code dispatchProvideAutofillStructure()} results in |
| 81 | * subsequent calls to {@link View#onProvideAutofillStructure(android.view.ViewStructure, int)} and |
| 82 | * {@link View#onProvideAutofillVirtualStructure(android.view.ViewStructure, int)} for each view in |
| 83 | * the hierarchy. |
| 84 | * |
| 85 | * <p>The resulting {@link android.view.ViewStructure} is then passed to the autofill service, which |
| 86 | * parses it looking for views that can be autofilled. If the service finds such views, it returns |
| 87 | * a data structure to the Android System containing the following optional info: |
| 88 | * |
| 89 | * <ul> |
| 90 | * <li>Datasets used to autofill subsets of views in the activity. |
| 91 | * <li>Id of views that the service can save their values for future autofilling. |
| 92 | * </ul> |
| 93 | * |
| 94 | * <p>When the service returns datasets, the Android System displays an autofill dataset picker |
| 95 | * UI affordance associated with the view, when the view is focused on and is part of a dataset. |
| 96 | * The application can be notified when the affordance is shown by registering an |
| 97 | * {@link AutofillCallback} through {@link #registerCallback(AutofillCallback)}. When the user |
| 98 | * selects a dataset from the affordance, all views present in the dataset are autofilled, through |
| 99 | * calls to {@link View#autofill(AutofillValue)} or {@link View#autofill(SparseArray)}. |
| 100 | * |
| 101 | * <p>When the service returns ids of savable views, the Android System keeps track of changes |
| 102 | * made to these views, so they can be used to determine if the autofill save UI is shown later. |
| 103 | * |
| 104 | * <p>The context is then finished when one of the following occurs: |
| 105 | * |
| 106 | * <ul> |
| 107 | * <li>{@link #commit()} is called or all savable views are gone. |
| 108 | * <li>{@link #cancel()} is called. |
| 109 | * </ul> |
| 110 | * |
| 111 | * <p>Finally, after the autofill context is commited (i.e., not cancelled), the Android System |
| 112 | * shows a save UI affordance if the value of savable views have changed. If the user selects the |
| 113 | * option to Save, the current value of the views is then sent to the autofill service. |
| 114 | * |
| 115 | * <p>It is safe to call into its methods from any thread. |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 116 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 117 | @SystemService(Context.AUTOFILL_MANAGER_SERVICE) |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 118 | public final class AutofillManager { |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 119 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 120 | private static final String TAG = "AutofillManager"; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 121 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 122 | /** |
| 123 | * Intent extra: The assist structure which captures the filled screen. |
Felipe Leme | 7320ca9 | 2017-03-29 15:09:54 -0700 | [diff] [blame] | 124 | * |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 125 | * <p> |
| 126 | * Type: {@link android.app.assist.AssistStructure} |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 127 | */ |
| 128 | public static final String EXTRA_ASSIST_STRUCTURE = |
| 129 | "android.view.autofill.extra.ASSIST_STRUCTURE"; |
| 130 | |
| 131 | /** |
| 132 | * Intent extra: The result of an authentication operation. It is |
| 133 | * either a fully populated {@link android.service.autofill.FillResponse} |
| 134 | * or a fully populated {@link android.service.autofill.Dataset} if |
| 135 | * a response or a dataset is being authenticated respectively. |
| 136 | * |
| 137 | * <p> |
| 138 | * Type: {@link android.service.autofill.FillResponse} or a |
| 139 | * {@link android.service.autofill.Dataset} |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 140 | */ |
| 141 | public static final String EXTRA_AUTHENTICATION_RESULT = |
| 142 | "android.view.autofill.extra.AUTHENTICATION_RESULT"; |
| 143 | |
Felipe Leme | 7320ca9 | 2017-03-29 15:09:54 -0700 | [diff] [blame] | 144 | /** |
| 145 | * Intent extra: The optional extras provided by the |
| 146 | * {@link android.service.autofill.AutofillService}. |
| 147 | * |
| 148 | * <p>For example, when the service responds to a {@link |
| 149 | * android.service.autofill.FillCallback#onSuccess(android.service.autofill.FillResponse)} with |
| 150 | * a {@code FillResponse} that requires authentication, the Intent that launches the |
| 151 | * service authentication will contain the Bundle set by |
Felipe Leme | 49e9696 | 2017-04-20 15:46:18 -0700 | [diff] [blame] | 152 | * {@link android.service.autofill.FillResponse.Builder#setClientState(Bundle)} on this extra. |
Felipe Leme | 7320ca9 | 2017-03-29 15:09:54 -0700 | [diff] [blame] | 153 | * |
| 154 | * <p> |
| 155 | * Type: {@link android.os.Bundle} |
| 156 | */ |
Felipe Leme | 37a440fd | 2017-04-28 10:50:20 -0700 | [diff] [blame] | 157 | public static final String EXTRA_CLIENT_STATE = |
Jeff Sharkey | 000ce80 | 2017-04-29 13:13:27 -0600 | [diff] [blame] | 158 | "android.view.autofill.extra.CLIENT_STATE"; |
Felipe Leme | 7320ca9 | 2017-03-29 15:09:54 -0700 | [diff] [blame] | 159 | |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 160 | |
| 161 | /** @hide */ |
| 162 | public static final String EXTRA_RESTORE_SESSION_TOKEN = |
| 163 | "android.view.autofill.extra.RESTORE_SESSION_TOKEN"; |
| 164 | |
| 165 | private static final String SESSION_ID_TAG = "android:sessionId"; |
| 166 | private static final String STATE_TAG = "android:state"; |
| 167 | private static final String LAST_AUTOFILLED_DATA_TAG = "android:lastAutoFilledData"; |
| 168 | |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 169 | |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 170 | /** @hide */ public static final int ACTION_START_SESSION = 1; |
| 171 | /** @hide */ public static final int ACTION_VIEW_ENTERED = 2; |
| 172 | /** @hide */ public static final int ACTION_VIEW_EXITED = 3; |
| 173 | /** @hide */ public static final int ACTION_VALUE_CHANGED = 4; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 174 | |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 175 | |
| 176 | /** @hide */ public static final int FLAG_ADD_CLIENT_ENABLED = 0x1; |
| 177 | /** @hide */ public static final int FLAG_ADD_CLIENT_DEBUG = 0x2; |
| 178 | /** @hide */ public static final int FLAG_ADD_CLIENT_VERBOSE = 0x4; |
| 179 | |
Svetoslav Ganov | a9379d0 | 2017-05-09 17:40:24 -0700 | [diff] [blame] | 180 | /** Which bits in an authentication id are used for the dataset id */ |
| 181 | private static final int AUTHENTICATION_ID_DATASET_ID_MASK = 0xFFFF; |
| 182 | /** How many bits in an authentication id are used for the dataset id */ |
| 183 | private static final int AUTHENTICATION_ID_DATASET_ID_SHIFT = 16; |
| 184 | /** @hide The index for an undefined data set */ |
| 185 | public static final int AUTHENTICATION_ID_DATASET_ID_UNDEFINED = 0xFFFF; |
| 186 | |
| 187 | /** |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 188 | * Used on {@link #onPendingSaveUi(int, IBinder)} to cancel the pending UI. |
| 189 | * |
| 190 | * @hide |
| 191 | */ |
| 192 | public static final int PENDING_UI_OPERATION_CANCEL = 1; |
| 193 | |
| 194 | /** |
| 195 | * Used on {@link #onPendingSaveUi(int, IBinder)} to restore the pending UI. |
| 196 | * |
| 197 | * @hide |
| 198 | */ |
| 199 | public static final int PENDING_UI_OPERATION_RESTORE = 2; |
| 200 | |
| 201 | /** |
| 202 | * Initial state of the autofill context, set when there is no session (i.e., when |
| 203 | * {@link #mSessionId} is {@link #NO_SESSION}). |
| 204 | * |
| 205 | * @hide |
| 206 | */ |
| 207 | public static final int STATE_UNKNOWN = 1; |
| 208 | |
| 209 | /** |
| 210 | * State where the autofill context hasn't been {@link #commit() finished} nor |
| 211 | * {@link #cancel() canceled} yet. |
| 212 | * |
| 213 | * @hide |
| 214 | */ |
| 215 | public static final int STATE_ACTIVE = 2; |
| 216 | |
| 217 | /** |
| 218 | * State where the autofill context has been {@link #commit() finished} but the server still has |
| 219 | * a session because the Save UI hasn't been dismissed yet. |
| 220 | * |
| 221 | * @hide |
| 222 | */ |
| 223 | public static final int STATE_SHOWING_SAVE_UI = 4; |
| 224 | |
| 225 | /** |
Svetoslav Ganov | a9379d0 | 2017-05-09 17:40:24 -0700 | [diff] [blame] | 226 | * Makes an authentication id from a request id and a dataset id. |
| 227 | * |
| 228 | * @param requestId The request id. |
| 229 | * @param datasetId The dataset id. |
| 230 | * @return The authentication id. |
| 231 | * @hide |
| 232 | */ |
| 233 | public static int makeAuthenticationId(int requestId, int datasetId) { |
| 234 | return (requestId << AUTHENTICATION_ID_DATASET_ID_SHIFT) |
| 235 | | (datasetId & AUTHENTICATION_ID_DATASET_ID_MASK); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Gets the request id from an authentication id. |
| 240 | * |
| 241 | * @param authRequestId The authentication id. |
| 242 | * @return The request id. |
| 243 | * @hide |
| 244 | */ |
| 245 | public static int getRequestIdFromAuthenticationId(int authRequestId) { |
| 246 | return (authRequestId >> AUTHENTICATION_ID_DATASET_ID_SHIFT); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Gets the dataset id from an authentication id. |
| 251 | * |
| 252 | * @param authRequestId The authentication id. |
| 253 | * @return The dataset id. |
| 254 | * @hide |
| 255 | */ |
| 256 | public static int getDatasetIdFromAuthenticationId(int authRequestId) { |
| 257 | return (authRequestId & AUTHENTICATION_ID_DATASET_ID_MASK); |
| 258 | } |
| 259 | |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 260 | private final MetricsLogger mMetricsLogger = new MetricsLogger(); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 261 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 262 | /** |
| 263 | * There is currently no session running. |
| 264 | * {@hide} |
| 265 | */ |
| 266 | public static final int NO_SESSION = Integer.MIN_VALUE; |
| 267 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 268 | private final IAutoFillManager mService; |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 269 | |
| 270 | private final Object mLock = new Object(); |
| 271 | |
| 272 | @GuardedBy("mLock") |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 273 | private IAutoFillManagerClient mServiceClient; |
| 274 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 275 | @GuardedBy("mLock") |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 276 | private AutofillCallback mCallback; |
| 277 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 278 | private final Context mContext; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 279 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 280 | @GuardedBy("mLock") |
| 281 | private int mSessionId = NO_SESSION; |
| 282 | |
| 283 | @GuardedBy("mLock") |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 284 | private int mState = STATE_UNKNOWN; |
| 285 | |
| 286 | @GuardedBy("mLock") |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 287 | private boolean mEnabled; |
| 288 | |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 289 | /** If a view changes to this mapping the autofill operation was successful */ |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 290 | @GuardedBy("mLock") |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 291 | @Nullable private ParcelableMap mLastAutofilledData; |
| 292 | |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 293 | /** If view tracking is enabled, contains the tracking state */ |
| 294 | @GuardedBy("mLock") |
| 295 | @Nullable private TrackedViews mTrackedViews; |
| 296 | |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 297 | /** Views that are only tracked because they are fillable and could be anchoring the UI. */ |
| 298 | @GuardedBy("mLock") |
| 299 | @Nullable private ArraySet<AutofillId> mFillableIds; |
| 300 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 301 | /** @hide */ |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 302 | public interface AutofillClient { |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 303 | /** |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 304 | * Asks the client to start an authentication flow. |
| 305 | * |
Svetoslav Ganov | a9379d0 | 2017-05-09 17:40:24 -0700 | [diff] [blame] | 306 | * @param authenticationId A unique id of the authentication operation. |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 307 | * @param intent The authentication intent. |
| 308 | * @param fillInIntent The authentication fill-in intent. |
| 309 | */ |
Svetoslav Ganov | a9379d0 | 2017-05-09 17:40:24 -0700 | [diff] [blame] | 310 | void autofillCallbackAuthenticate(int authenticationId, IntentSender intent, |
| 311 | Intent fillInIntent); |
Svet Ganov | 17db9dc | 2017-02-21 19:54:31 -0800 | [diff] [blame] | 312 | |
| 313 | /** |
| 314 | * Tells the client this manager has state to be reset. |
| 315 | */ |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 316 | void autofillCallbackResetableStateAvailable(); |
| 317 | |
| 318 | /** |
| 319 | * Request showing the autofill UI. |
| 320 | * |
| 321 | * @param anchor The real view the UI needs to anchor to. |
| 322 | * @param width The width of the fill UI content. |
| 323 | * @param height The height of the fill UI content. |
| 324 | * @param virtualBounds The bounds of the virtual decendant of the anchor. |
| 325 | * @param presenter The presenter that controls the fill UI window. |
| 326 | * @return Whether the UI was shown. |
| 327 | */ |
| 328 | boolean autofillCallbackRequestShowFillUi(@NonNull View anchor, int width, int height, |
| 329 | @Nullable Rect virtualBounds, IAutofillWindowPresenter presenter); |
| 330 | |
| 331 | /** |
| 332 | * Request hiding the autofill UI. |
| 333 | * |
| 334 | * @return Whether the UI was hidden. |
| 335 | */ |
| 336 | boolean autofillCallbackRequestHideFillUi(); |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 337 | |
| 338 | /** |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 339 | * Checks if views are currently attached and visible. |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 340 | * |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 341 | * @return And array with {@code true} iff the view is attached or visible |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 342 | */ |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 343 | @NonNull boolean[] getViewVisibility(@NonNull int[] viewId); |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 344 | |
| 345 | /** |
| 346 | * Checks is the client is currently visible as understood by autofill. |
| 347 | * |
| 348 | * @return {@code true} if the client is currently visible |
| 349 | */ |
| 350 | boolean isVisibleForAutofill(); |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 351 | |
| 352 | /** |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 353 | * Finds views by traversing the hierarchies of the client. |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 354 | * |
Phil Weaver | 846cda93 | 2017-06-15 10:10:06 -0700 | [diff] [blame] | 355 | * @param viewIds The autofill ids of the views to find |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 356 | * |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 357 | * @return And array containing the views (empty if no views found). |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 358 | */ |
Phil Weaver | 846cda93 | 2017-06-15 10:10:06 -0700 | [diff] [blame] | 359 | @NonNull View[] findViewsByAutofillIdTraversal(@NonNull int[] viewIds); |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 360 | |
| 361 | /** |
| 362 | * Finds a view by traversing the hierarchies of the client. |
| 363 | * |
Phil Weaver | 846cda93 | 2017-06-15 10:10:06 -0700 | [diff] [blame] | 364 | * @param viewId The autofill id of the views to find |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 365 | * |
| 366 | * @return The view, or {@code null} if not found |
| 367 | */ |
Phil Weaver | 846cda93 | 2017-06-15 10:10:06 -0700 | [diff] [blame] | 368 | @Nullable View findViewByAutofillIdTraversal(int viewId); |
Felipe Leme | 9876a6f | 2017-05-30 15:47:28 -0700 | [diff] [blame] | 369 | |
| 370 | /** |
| 371 | * Runs the specified action on the UI thread. |
| 372 | */ |
| 373 | void runOnUiThread(Runnable action); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 374 | } |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 375 | |
| 376 | /** |
| 377 | * @hide |
| 378 | */ |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 379 | public AutofillManager(Context context, IAutoFillManager service) { |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 380 | mContext = context; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 381 | mService = service; |
| 382 | } |
| 383 | |
| 384 | /** |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 385 | * Restore state after activity lifecycle |
| 386 | * |
| 387 | * @param savedInstanceState The state to be restored |
| 388 | * |
| 389 | * {@hide} |
| 390 | */ |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 391 | public void onCreate(Bundle savedInstanceState) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 392 | if (!hasAutofillFeature()) { |
| 393 | return; |
| 394 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 395 | synchronized (mLock) { |
| 396 | mLastAutofilledData = savedInstanceState.getParcelable(LAST_AUTOFILLED_DATA_TAG); |
| 397 | |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 398 | if (isActiveLocked()) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 399 | Log.w(TAG, "New session was started before onCreate()"); |
| 400 | return; |
| 401 | } |
| 402 | |
| 403 | mSessionId = savedInstanceState.getInt(SESSION_ID_TAG, NO_SESSION); |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 404 | mState = savedInstanceState.getInt(STATE_TAG, STATE_UNKNOWN); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 405 | |
| 406 | if (mSessionId != NO_SESSION) { |
| 407 | ensureServiceClientAddedIfNeededLocked(); |
| 408 | |
| 409 | final AutofillClient client = getClientLocked(); |
| 410 | if (client != null) { |
| 411 | try { |
| 412 | final boolean sessionWasRestored = mService.restoreSession(mSessionId, |
| 413 | mContext.getActivityToken(), mServiceClient.asBinder()); |
| 414 | |
| 415 | if (!sessionWasRestored) { |
| 416 | Log.w(TAG, "Session " + mSessionId + " could not be restored"); |
| 417 | mSessionId = NO_SESSION; |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 418 | mState = STATE_UNKNOWN; |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 419 | } else { |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 420 | if (sDebug) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 421 | Log.d(TAG, "session " + mSessionId + " was restored"); |
| 422 | } |
| 423 | |
| 424 | client.autofillCallbackResetableStateAvailable(); |
| 425 | } |
| 426 | } catch (RemoteException e) { |
| 427 | Log.e(TAG, "Could not figure out if there was an autofill session", e); |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /** |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 435 | * Called once the client becomes visible. |
| 436 | * |
| 437 | * @see AutofillClient#isVisibleForAutofill() |
| 438 | * |
| 439 | * {@hide} |
| 440 | */ |
| 441 | public void onVisibleForAutofill() { |
| 442 | synchronized (mLock) { |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 443 | if (mEnabled && isActiveLocked() && mTrackedViews != null) { |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 444 | mTrackedViews.onVisibleForAutofillLocked(); |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | /** |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 450 | * Save state before activity lifecycle |
| 451 | * |
| 452 | * @param outState Place to store the state |
| 453 | * |
| 454 | * {@hide} |
| 455 | */ |
| 456 | public void onSaveInstanceState(Bundle outState) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 457 | if (!hasAutofillFeature()) { |
| 458 | return; |
| 459 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 460 | synchronized (mLock) { |
| 461 | if (mSessionId != NO_SESSION) { |
| 462 | outState.putInt(SESSION_ID_TAG, mSessionId); |
| 463 | } |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 464 | if (mState != STATE_UNKNOWN) { |
| 465 | outState.putInt(STATE_TAG, mState); |
| 466 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 467 | if (mLastAutofilledData != null) { |
| 468 | outState.putParcelable(LAST_AUTOFILLED_DATA_TAG, mLastAutofilledData); |
| 469 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Checks whether autofill is enabled for the current user. |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 475 | * |
| 476 | * <p>Typically used to determine whether the option to explicitly request autofill should |
| 477 | * be offered - see {@link #requestAutofill(View)}. |
| 478 | * |
| 479 | * @return whether autofill is enabled for the current user. |
| 480 | */ |
| 481 | public boolean isEnabled() { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 482 | if (!hasAutofillFeature()) { |
| 483 | return false; |
| 484 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 485 | synchronized (mLock) { |
| 486 | ensureServiceClientAddedIfNeededLocked(); |
| 487 | return mEnabled; |
| 488 | } |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | /** |
Philip P. Moltmann | cc684ed | 2017-04-17 14:18:33 -0700 | [diff] [blame] | 492 | * Should always be called from {@link AutofillService#getFillEventHistory()}. |
| 493 | * |
| 494 | * @hide |
| 495 | */ |
| 496 | @Nullable public FillEventHistory getFillEventHistory() { |
| 497 | try { |
| 498 | return mService.getFillEventHistory(); |
| 499 | } catch (RemoteException e) { |
| 500 | e.rethrowFromSystemServer(); |
| 501 | return null; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | /** |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 506 | * Explicitly requests a new autofill context. |
| 507 | * |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 508 | * <p>Normally, the autofill context is automatically started if necessary when |
| 509 | * {@link #notifyViewEntered(View)} is called, but this method should be used in the |
| 510 | * cases where it must be explicitly started. For example, when the view offers an AUTOFILL |
| 511 | * option on its contextual overflow menu, and the user selects it. |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 512 | * |
| 513 | * @param view view requesting the new autofill context. |
| 514 | */ |
| 515 | public void requestAutofill(@NonNull View view) { |
Felipe Leme | d114642 | 2017-04-26 10:17:05 -0700 | [diff] [blame] | 516 | notifyViewEntered(view, FLAG_MANUAL_REQUEST); |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | /** |
| 520 | * Explicitly requests a new autofill context for virtual views. |
| 521 | * |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 522 | * <p>Normally, the autofill context is automatically started if necessary when |
| 523 | * {@link #notifyViewEntered(View, int, Rect)} is called, but this method should be used in the |
| 524 | * cases where it must be explicitly started. For example, when the virtual view offers an |
| 525 | * AUTOFILL option on its contextual overflow menu, and the user selects it. |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 526 | * |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 527 | * <p>The virtual view boundaries must be absolute screen coordinates. For example, if the |
| 528 | * parent view uses {@code bounds} to draw the virtual view inside its Canvas, |
| 529 | * the absolute bounds could be calculated by: |
| 530 | * |
| 531 | * <pre class="prettyprint"> |
| 532 | * int offset[] = new int[2]; |
| 533 | * getLocationOnScreen(offset); |
| 534 | * Rect absBounds = new Rect(bounds.left + offset[0], |
| 535 | * bounds.top + offset[1], |
| 536 | * bounds.right + offset[0], bounds.bottom + offset[1]); |
| 537 | * </pre> |
| 538 | * |
| 539 | * @param view the virtual view parent. |
| 540 | * @param virtualId id identifying the virtual child inside the parent view. |
| 541 | * @param absBounds absolute boundaries of the virtual view in the screen. |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 542 | */ |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 543 | public void requestAutofill(@NonNull View view, int virtualId, @NonNull Rect absBounds) { |
| 544 | notifyViewEntered(view, virtualId, absBounds, FLAG_MANUAL_REQUEST); |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 547 | /** |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 548 | * Called when a {@link View} that supports autofill is entered. |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 549 | * |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 550 | * @param view {@link View} that was entered. |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 551 | */ |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 552 | public void notifyViewEntered(@NonNull View view) { |
Felipe Leme | d114642 | 2017-04-26 10:17:05 -0700 | [diff] [blame] | 553 | notifyViewEntered(view, 0); |
| 554 | } |
| 555 | |
| 556 | private void notifyViewEntered(@NonNull View view, int flags) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 557 | if (!hasAutofillFeature()) { |
| 558 | return; |
| 559 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 560 | AutofillCallback callback = null; |
| 561 | synchronized (mLock) { |
| 562 | ensureServiceClientAddedIfNeededLocked(); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 563 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 564 | if (!mEnabled) { |
| 565 | if (mCallback != null) { |
| 566 | callback = mCallback; |
| 567 | } |
| 568 | } else { |
| 569 | final AutofillId id = getAutofillId(view); |
| 570 | final AutofillValue value = view.getAutofillValue(); |
| 571 | |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 572 | if (!isActiveLocked()) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 573 | // Starts new session. |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 574 | startSessionLocked(id, null, value, flags); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 575 | } else { |
| 576 | // Update focus on existing session. |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 577 | updateSessionLocked(id, null, value, ACTION_VIEW_ENTERED, flags); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 578 | } |
Felipe Leme | 24aae15 | 2017-03-15 12:33:01 -0700 | [diff] [blame] | 579 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 580 | } |
| 581 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 582 | if (callback != null) { |
| 583 | mCallback.onAutofillEvent(view, AutofillCallback.EVENT_INPUT_UNAVAILABLE); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 584 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | /** |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 588 | * Called when a {@link View} that supports autofill is exited. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 589 | * |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 590 | * @param view {@link View} that was exited. |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 591 | */ |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 592 | public void notifyViewExited(@NonNull View view) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 593 | if (!hasAutofillFeature()) { |
| 594 | return; |
| 595 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 596 | synchronized (mLock) { |
| 597 | ensureServiceClientAddedIfNeededLocked(); |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 598 | |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 599 | if (mEnabled && isActiveLocked()) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 600 | final AutofillId id = getAutofillId(view); |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 601 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 602 | // Update focus on existing session. |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 603 | updateSessionLocked(id, null, null, ACTION_VIEW_EXITED, 0); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 604 | } |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
| 608 | /** |
Svetoslav Ganov | 4db89ba | 2017-06-28 16:50:37 -0700 | [diff] [blame] | 609 | * Called when a {@link View view's} visibility changed. |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 610 | * |
| 611 | * @param view {@link View} that was exited. |
| 612 | * @param isVisible visible if the view is visible in the view hierarchy. |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 613 | */ |
Svetoslav Ganov | 4db89ba | 2017-06-28 16:50:37 -0700 | [diff] [blame] | 614 | public void notifyViewVisibilityChanged(@NonNull View view, boolean isVisible) { |
| 615 | notifyViewVisibilityChangedInternal(view, 0, isVisible, false); |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * Called when a virtual view's visibility changed. |
| 620 | * |
| 621 | * @param view {@link View} that was exited. |
| 622 | * @param virtualId id identifying the virtual child inside the parent view. |
| 623 | * @param isVisible visible if the view is visible in the view hierarchy. |
| 624 | */ |
| 625 | public void notifyViewVisibilityChanged(@NonNull View view, int virtualId, boolean isVisible) { |
| 626 | notifyViewVisibilityChangedInternal(view, virtualId, isVisible, true); |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * Called when a view/virtual view's visibility changed. |
| 631 | * |
| 632 | * @param view {@link View} that was exited. |
| 633 | * @param virtualId id identifying the virtual child inside the parent view. |
| 634 | * @param isVisible visible if the view is visible in the view hierarchy. |
| 635 | * @param virtual Whether the view is virtual. |
| 636 | */ |
| 637 | private void notifyViewVisibilityChangedInternal(@NonNull View view, int virtualId, |
| 638 | boolean isVisible, boolean virtual) { |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 639 | synchronized (mLock) { |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 640 | if (mEnabled && isActiveLocked()) { |
Svetoslav Ganov | 4db89ba | 2017-06-28 16:50:37 -0700 | [diff] [blame] | 641 | final AutofillId id = virtual ? getAutofillId(view, virtualId) |
| 642 | : view.getAutofillId(); |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 643 | if (!isVisible && mFillableIds != null) { |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 644 | if (mFillableIds.contains(id)) { |
| 645 | if (sDebug) Log.d(TAG, "Hidding UI when view " + id + " became invisible"); |
| 646 | requestHideFillUi(id, view); |
| 647 | } |
| 648 | } |
| 649 | if (mTrackedViews != null) { |
Svetoslav Ganov | 4db89ba | 2017-06-28 16:50:37 -0700 | [diff] [blame] | 650 | mTrackedViews.notifyViewVisibilityChanged(id, isVisible); |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 651 | } |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | /** |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 657 | * Called when a virtual view that supports autofill is entered. |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 658 | * |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 659 | * <p>The virtual view boundaries must be absolute screen coordinates. For example, if the |
| 660 | * parent, non-virtual view uses {@code bounds} to draw the virtual view inside its Canvas, |
| 661 | * the absolute bounds could be calculated by: |
| 662 | * |
| 663 | * <pre class="prettyprint"> |
| 664 | * int offset[] = new int[2]; |
| 665 | * getLocationOnScreen(offset); |
| 666 | * Rect absBounds = new Rect(bounds.left + offset[0], |
| 667 | * bounds.top + offset[1], |
| 668 | * bounds.right + offset[0], bounds.bottom + offset[1]); |
| 669 | * </pre> |
| 670 | * |
| 671 | * @param view the virtual view parent. |
| 672 | * @param virtualId id identifying the virtual child inside the parent view. |
| 673 | * @param absBounds absolute boundaries of the virtual view in the screen. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 674 | */ |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 675 | public void notifyViewEntered(@NonNull View view, int virtualId, @NonNull Rect absBounds) { |
| 676 | notifyViewEntered(view, virtualId, absBounds, 0); |
Felipe Leme | d114642 | 2017-04-26 10:17:05 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 679 | private void notifyViewEntered(View view, int virtualId, Rect bounds, int flags) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 680 | if (!hasAutofillFeature()) { |
| 681 | return; |
| 682 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 683 | AutofillCallback callback = null; |
| 684 | synchronized (mLock) { |
| 685 | ensureServiceClientAddedIfNeededLocked(); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 686 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 687 | if (!mEnabled) { |
| 688 | if (mCallback != null) { |
| 689 | callback = mCallback; |
| 690 | } |
| 691 | } else { |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 692 | final AutofillId id = getAutofillId(view, virtualId); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 693 | |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 694 | if (!isActiveLocked()) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 695 | // Starts new session. |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 696 | startSessionLocked(id, bounds, null, flags); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 697 | } else { |
| 698 | // Update focus on existing session. |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 699 | updateSessionLocked(id, bounds, null, ACTION_VIEW_ENTERED, flags); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 700 | } |
Felipe Leme | 24aae15 | 2017-03-15 12:33:01 -0700 | [diff] [blame] | 701 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 702 | } |
| 703 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 704 | if (callback != null) { |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 705 | callback.onAutofillEvent(view, virtualId, |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 706 | AutofillCallback.EVENT_INPUT_UNAVAILABLE); |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
| 710 | /** |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 711 | * Called when a virtual view that supports autofill is exited. |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 712 | * |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 713 | * @param view the virtual view parent. |
| 714 | * @param virtualId id identifying the virtual child inside the parent view. |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 715 | */ |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 716 | public void notifyViewExited(@NonNull View view, int virtualId) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 717 | if (!hasAutofillFeature()) { |
| 718 | return; |
| 719 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 720 | synchronized (mLock) { |
| 721 | ensureServiceClientAddedIfNeededLocked(); |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 722 | |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 723 | if (mEnabled && isActiveLocked()) { |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 724 | final AutofillId id = getAutofillId(view, virtualId); |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 725 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 726 | // Update focus on existing session. |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 727 | updateSessionLocked(id, null, null, ACTION_VIEW_EXITED, 0); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 728 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 729 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | /** |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 733 | * Called to indicate the value of an autofillable {@link View} changed. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 734 | * |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 735 | * @param view view whose value changed. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 736 | */ |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 737 | public void notifyValueChanged(View view) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 738 | if (!hasAutofillFeature()) { |
| 739 | return; |
| 740 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 741 | AutofillId id = null; |
| 742 | boolean valueWasRead = false; |
| 743 | AutofillValue value = null; |
| 744 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 745 | synchronized (mLock) { |
| 746 | // If the session is gone some fields might still be highlighted, hence we have to |
| 747 | // remove the isAutofilled property even if no sessions are active. |
| 748 | if (mLastAutofilledData == null) { |
| 749 | view.setAutofilled(false); |
| 750 | } else { |
| 751 | id = getAutofillId(view); |
| 752 | if (mLastAutofilledData.containsKey(id)) { |
| 753 | value = view.getAutofillValue(); |
| 754 | valueWasRead = true; |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 755 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 756 | if (Objects.equals(mLastAutofilledData.get(id), value)) { |
| 757 | view.setAutofilled(true); |
| 758 | } else { |
| 759 | view.setAutofilled(false); |
| 760 | mLastAutofilledData.remove(id); |
| 761 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 762 | } else { |
| 763 | view.setAutofilled(false); |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 764 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 765 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 766 | |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 767 | if (!mEnabled || !isActiveLocked()) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 768 | return; |
| 769 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 770 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 771 | if (id == null) { |
| 772 | id = getAutofillId(view); |
| 773 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 774 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 775 | if (!valueWasRead) { |
| 776 | value = view.getAutofillValue(); |
| 777 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 778 | |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 779 | updateSessionLocked(id, null, value, ACTION_VALUE_CHANGED, 0); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 780 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 781 | } |
| 782 | |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 783 | /** |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 784 | * Called to indicate the value of an autofillable virtual view has changed. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 785 | * |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 786 | * @param view the virtual view parent. |
| 787 | * @param virtualId id identifying the virtual child inside the parent view. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 788 | * @param value new value of the child. |
| 789 | */ |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 790 | public void notifyValueChanged(View view, int virtualId, AutofillValue value) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 791 | if (!hasAutofillFeature()) { |
| 792 | return; |
| 793 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 794 | synchronized (mLock) { |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 795 | if (!mEnabled || !isActiveLocked()) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 796 | return; |
| 797 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 798 | |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 799 | final AutofillId id = getAutofillId(view, virtualId); |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 800 | updateSessionLocked(id, null, value, ACTION_VALUE_CHANGED, 0); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 801 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | /** |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 805 | * Called to indicate the current autofill context should be commited. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 806 | * |
Felipe Leme | afdfe76 | 2017-07-24 11:25:56 -0700 | [diff] [blame] | 807 | * <p>This method is typically called by {@link View Views} that manage virtual views; for |
| 808 | * example, when the view is rendering an {@code HTML} page with a form and virtual views |
| 809 | * that represent the HTML elements, it should call this method after the form is submitted and |
| 810 | * another page is rendered. |
| 811 | * |
| 812 | * <p><b>Note:</b> This method does not need to be called on regular application lifecycle |
| 813 | * methods such as {@link android.app.Activity#finish()}. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 814 | */ |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 815 | public void commit() { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 816 | if (!hasAutofillFeature()) { |
| 817 | return; |
| 818 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 819 | synchronized (mLock) { |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 820 | if (!mEnabled && !isActiveLocked()) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 821 | return; |
| 822 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 823 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 824 | finishSessionLocked(); |
| 825 | } |
Felipe Leme | 0200d9e | 2017-01-24 15:10:26 -0800 | [diff] [blame] | 826 | } |
| 827 | |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 828 | /** |
| 829 | * Called to indicate the current autofill context should be cancelled. |
| 830 | * |
Felipe Leme | afdfe76 | 2017-07-24 11:25:56 -0700 | [diff] [blame] | 831 | * <p>This method is typically called by {@link View Views} that manage virtual views; for |
| 832 | * example, when the view is rendering an {@code HTML} page with a form and virtual views |
| 833 | * that represent the HTML elements, it should call this method if the user does not post the |
| 834 | * form but moves to another form in this page. |
| 835 | * |
| 836 | * <p><b>Note:</b> This method does not need to be called on regular application lifecycle |
| 837 | * methods such as {@link android.app.Activity#finish()}. |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 838 | */ |
| 839 | public void cancel() { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 840 | if (!hasAutofillFeature()) { |
| 841 | return; |
| 842 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 843 | synchronized (mLock) { |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 844 | if (!mEnabled && !isActiveLocked()) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 845 | return; |
| 846 | } |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 847 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 848 | cancelSessionLocked(); |
| 849 | } |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 850 | } |
| 851 | |
Svet Ganov | f965b87 | 2017-04-28 16:34:02 -0700 | [diff] [blame] | 852 | /** @hide */ |
| 853 | public void disableOwnedAutofillServices() { |
| 854 | disableAutofillServices(); |
| 855 | } |
| 856 | |
Svet Ganov | f20a037 | 2017-04-10 17:08:05 -0700 | [diff] [blame] | 857 | /** |
| 858 | * If the app calling this API has enabled autofill services they |
| 859 | * will be disabled. |
| 860 | */ |
Svet Ganov | f965b87 | 2017-04-28 16:34:02 -0700 | [diff] [blame] | 861 | public void disableAutofillServices() { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 862 | if (!hasAutofillFeature()) { |
| 863 | return; |
| 864 | } |
Svet Ganov | f20a037 | 2017-04-10 17:08:05 -0700 | [diff] [blame] | 865 | try { |
| 866 | mService.disableOwnedAutofillServices(mContext.getUserId()); |
| 867 | } catch (RemoteException e) { |
| 868 | throw e.rethrowFromSystemServer(); |
| 869 | } |
| 870 | } |
| 871 | |
Felipe Leme | db04118 | 2017-04-21 17:33:38 -0700 | [diff] [blame] | 872 | /** |
| 873 | * Returns {@code true} if the calling application provides a {@link AutofillService} that is |
| 874 | * enabled for the current user, or {@code false} otherwise. |
| 875 | */ |
| 876 | public boolean hasEnabledAutofillServices() { |
| 877 | if (mService == null) return false; |
| 878 | |
| 879 | try { |
| 880 | return mService.isServiceEnabled(mContext.getUserId(), mContext.getPackageName()); |
| 881 | } catch (RemoteException e) { |
| 882 | throw e.rethrowFromSystemServer(); |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | /** |
Ricardo Loo | 33d226c | 2017-06-27 14:17:33 -0700 | [diff] [blame] | 887 | * Returns {@code true} if autofill is supported by the current device and |
| 888 | * is supported for this user. |
Felipe Leme | db04118 | 2017-04-21 17:33:38 -0700 | [diff] [blame] | 889 | * |
| 890 | * <p>Autofill is typically supported, but it could be unsupported in cases like: |
| 891 | * <ol> |
| 892 | * <li>Low-end devices. |
| 893 | * <li>Device policy rules that forbid its usage. |
| 894 | * </ol> |
| 895 | */ |
| 896 | public boolean isAutofillSupported() { |
| 897 | if (mService == null) return false; |
| 898 | |
| 899 | try { |
| 900 | return mService.isServiceSupported(mContext.getUserId()); |
| 901 | } catch (RemoteException e) { |
| 902 | throw e.rethrowFromSystemServer(); |
| 903 | } |
| 904 | } |
| 905 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 906 | private AutofillClient getClientLocked() { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 907 | if (mContext instanceof AutofillClient) { |
| 908 | return (AutofillClient) mContext; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 909 | } |
Svet Ganov | 17db9dc | 2017-02-21 19:54:31 -0800 | [diff] [blame] | 910 | return null; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | /** @hide */ |
Svetoslav Ganov | a9379d0 | 2017-05-09 17:40:24 -0700 | [diff] [blame] | 914 | public void onAuthenticationResult(int authenticationId, Intent data) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 915 | if (!hasAutofillFeature()) { |
| 916 | return; |
| 917 | } |
Felipe Leme | 85d1c2d | 2017-04-21 08:56:04 -0700 | [diff] [blame] | 918 | // TODO: the result code is being ignored, so this method is not reliably |
Felipe Leme | d633f07 | 2017-02-14 10:17:17 -0800 | [diff] [blame] | 919 | // handling the cases where it's not RESULT_OK: it works fine if the service does not |
| 920 | // set the EXTRA_AUTHENTICATION_RESULT extra, but it could cause weird results if the |
| 921 | // service set the extra and returned RESULT_CANCELED... |
| 922 | |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 923 | if (sDebug) Log.d(TAG, "onAuthenticationResult(): d=" + data); |
Felipe Leme | d633f07 | 2017-02-14 10:17:17 -0800 | [diff] [blame] | 924 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 925 | synchronized (mLock) { |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 926 | if (!isActiveLocked() || data == null) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 927 | return; |
| 928 | } |
| 929 | final Parcelable result = data.getParcelableExtra(EXTRA_AUTHENTICATION_RESULT); |
| 930 | final Bundle responseData = new Bundle(); |
| 931 | responseData.putParcelable(EXTRA_AUTHENTICATION_RESULT, result); |
| 932 | try { |
Svetoslav Ganov | a9379d0 | 2017-05-09 17:40:24 -0700 | [diff] [blame] | 933 | mService.setAuthenticationResult(responseData, mSessionId, authenticationId, |
| 934 | mContext.getUserId()); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 935 | } catch (RemoteException e) { |
| 936 | Log.e(TAG, "Error delivering authentication result", e); |
| 937 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 938 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 939 | } |
| 940 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 941 | private static AutofillId getAutofillId(View view) { |
Phil Weaver | 846cda93 | 2017-06-15 10:10:06 -0700 | [diff] [blame] | 942 | return new AutofillId(view.getAutofillViewId()); |
Felipe Leme | 0200d9e | 2017-01-24 15:10:26 -0800 | [diff] [blame] | 943 | } |
| 944 | |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 945 | private static AutofillId getAutofillId(View parent, int virtualId) { |
Phil Weaver | 846cda93 | 2017-06-15 10:10:06 -0700 | [diff] [blame] | 946 | return new AutofillId(parent.getAutofillViewId(), virtualId); |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 947 | } |
| 948 | |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 949 | private void startSessionLocked(@NonNull AutofillId id, @NonNull Rect bounds, |
| 950 | @NonNull AutofillValue value, int flags) { |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 951 | if (sVerbose) { |
| 952 | Log.v(TAG, "startSessionLocked(): id=" + id + ", bounds=" + bounds + ", value=" + value |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 953 | + ", flags=" + flags + ", state=" + mState); |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 954 | } |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 955 | if (mState != STATE_UNKNOWN) { |
| 956 | if (sDebug) Log.d(TAG, "not starting session for " + id + " on state " + mState); |
| 957 | return; |
| 958 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 959 | try { |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 960 | mSessionId = mService.startSession(mContext.getActivityToken(), |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 961 | mServiceClient.asBinder(), id, bounds, value, mContext.getUserId(), |
Philip P. Moltmann | 7b77116 | 2017-03-03 17:22:57 -0800 | [diff] [blame] | 962 | mCallback != null, flags, mContext.getOpPackageName()); |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 963 | if (mSessionId != NO_SESSION) { |
| 964 | mState = STATE_ACTIVE; |
| 965 | } |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 966 | final AutofillClient client = getClientLocked(); |
Svet Ganov | 17db9dc | 2017-02-21 19:54:31 -0800 | [diff] [blame] | 967 | if (client != null) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 968 | client.autofillCallbackResetableStateAvailable(); |
Svet Ganov | 17db9dc | 2017-02-21 19:54:31 -0800 | [diff] [blame] | 969 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 970 | } catch (RemoteException e) { |
| 971 | throw e.rethrowFromSystemServer(); |
| 972 | } |
| 973 | } |
| 974 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 975 | private void finishSessionLocked() { |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 976 | if (sVerbose) Log.v(TAG, "finishSessionLocked(): " + mState); |
| 977 | |
| 978 | if (!isActiveLocked()) return; |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 979 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 980 | try { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 981 | mService.finishSession(mSessionId, mContext.getUserId()); |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 982 | } catch (RemoteException e) { |
| 983 | throw e.rethrowFromSystemServer(); |
| 984 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 985 | |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 986 | resetSessionLocked(); |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 987 | } |
| 988 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 989 | private void cancelSessionLocked() { |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 990 | if (sVerbose) Log.v(TAG, "cancelSessionLocked(): " + mState); |
| 991 | |
| 992 | if (!isActiveLocked()) return; |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 993 | |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 994 | try { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 995 | mService.cancelSession(mSessionId, mContext.getUserId()); |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 996 | } catch (RemoteException e) { |
| 997 | throw e.rethrowFromSystemServer(); |
| 998 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 999 | |
Svet Ganov | 48f10a2 | 2017-04-26 18:49:30 -0700 | [diff] [blame] | 1000 | resetSessionLocked(); |
| 1001 | } |
| 1002 | |
| 1003 | private void resetSessionLocked() { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1004 | mSessionId = NO_SESSION; |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 1005 | mState = STATE_UNKNOWN; |
Svet Ganov | 48f10a2 | 2017-04-26 18:49:30 -0700 | [diff] [blame] | 1006 | mTrackedViews = null; |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 1007 | mFillableIds = null; |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 1010 | private void updateSessionLocked(AutofillId id, Rect bounds, AutofillValue value, int action, |
| 1011 | int flags) { |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 1012 | if (sVerbose && action != ACTION_VIEW_EXITED) { |
| 1013 | Log.v(TAG, "updateSessionLocked(): id=" + id + ", bounds=" + bounds |
| 1014 | + ", value=" + value + ", action=" + action + ", flags=" + flags); |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 1015 | } |
Felipe Leme | 7f33cd3 | 2017-05-11 10:10:49 -0700 | [diff] [blame] | 1016 | boolean restartIfNecessary = (flags & FLAG_MANUAL_REQUEST) != 0; |
| 1017 | |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 1018 | try { |
Felipe Leme | 7f33cd3 | 2017-05-11 10:10:49 -0700 | [diff] [blame] | 1019 | if (restartIfNecessary) { |
| 1020 | final int newId = mService.updateOrRestartSession(mContext.getActivityToken(), |
| 1021 | mServiceClient.asBinder(), id, bounds, value, mContext.getUserId(), |
| 1022 | mCallback != null, flags, mContext.getOpPackageName(), mSessionId, action); |
| 1023 | if (newId != mSessionId) { |
| 1024 | if (sDebug) Log.d(TAG, "Session restarted: " + mSessionId + "=>" + newId); |
| 1025 | mSessionId = newId; |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 1026 | mState = (mSessionId == NO_SESSION) ? STATE_UNKNOWN : STATE_ACTIVE; |
Felipe Leme | 7f33cd3 | 2017-05-11 10:10:49 -0700 | [diff] [blame] | 1027 | final AutofillClient client = getClientLocked(); |
| 1028 | if (client != null) { |
| 1029 | client.autofillCallbackResetableStateAvailable(); |
| 1030 | } |
| 1031 | } |
| 1032 | } else { |
| 1033 | mService.updateSession(mSessionId, id, bounds, value, action, flags, |
| 1034 | mContext.getUserId()); |
| 1035 | } |
| 1036 | |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 1037 | } catch (RemoteException e) { |
| 1038 | throw e.rethrowFromSystemServer(); |
| 1039 | } |
| 1040 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1041 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1042 | private void ensureServiceClientAddedIfNeededLocked() { |
| 1043 | if (getClientLocked() == null) { |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1044 | return; |
| 1045 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1046 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1047 | if (mServiceClient == null) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 1048 | mServiceClient = new AutofillManagerClient(this); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1049 | try { |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 1050 | final int flags = mService.addClient(mServiceClient, mContext.getUserId()); |
| 1051 | mEnabled = (flags & FLAG_ADD_CLIENT_ENABLED) != 0; |
| 1052 | sDebug = (flags & FLAG_ADD_CLIENT_DEBUG) != 0; |
| 1053 | sVerbose = (flags & FLAG_ADD_CLIENT_VERBOSE) != 0; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1054 | } catch (RemoteException e) { |
| 1055 | throw e.rethrowFromSystemServer(); |
| 1056 | } |
| 1057 | } |
| 1058 | } |
| 1059 | |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1060 | /** |
| 1061 | * Registers a {@link AutofillCallback} to receive autofill events. |
| 1062 | * |
| 1063 | * @param callback callback to receive events. |
| 1064 | */ |
| 1065 | public void registerCallback(@Nullable AutofillCallback callback) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 1066 | if (!hasAutofillFeature()) { |
| 1067 | return; |
| 1068 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1069 | synchronized (mLock) { |
| 1070 | if (callback == null) return; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1071 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1072 | final boolean hadCallback = mCallback != null; |
| 1073 | mCallback = callback; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1074 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1075 | if (!hadCallback) { |
| 1076 | try { |
| 1077 | mService.setHasCallback(mSessionId, mContext.getUserId(), true); |
| 1078 | } catch (RemoteException e) { |
| 1079 | throw e.rethrowFromSystemServer(); |
| 1080 | } |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1081 | } |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | /** |
| 1086 | * Unregisters a {@link AutofillCallback} to receive autofill events. |
| 1087 | * |
| 1088 | * @param callback callback to stop receiving events. |
| 1089 | */ |
| 1090 | public void unregisterCallback(@Nullable AutofillCallback callback) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 1091 | if (!hasAutofillFeature()) { |
| 1092 | return; |
| 1093 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1094 | synchronized (mLock) { |
| 1095 | if (callback == null || mCallback == null || callback != mCallback) return; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1096 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1097 | mCallback = null; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1098 | |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1099 | try { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1100 | mService.setHasCallback(mSessionId, mContext.getUserId(), false); |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1101 | } catch (RemoteException e) { |
| 1102 | throw e.rethrowFromSystemServer(); |
| 1103 | } |
| 1104 | } |
| 1105 | } |
| 1106 | |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1107 | private void requestShowFillUi(int sessionId, AutofillId id, int width, int height, |
| 1108 | Rect anchorBounds, IAutofillWindowPresenter presenter) { |
| 1109 | final View anchor = findView(id); |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1110 | if (anchor == null) { |
| 1111 | return; |
| 1112 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1113 | |
| 1114 | AutofillCallback callback = null; |
| 1115 | synchronized (mLock) { |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1116 | if (mSessionId == sessionId) { |
| 1117 | AutofillClient client = getClientLocked(); |
| 1118 | |
| 1119 | if (client != null) { |
| 1120 | if (client.autofillCallbackRequestShowFillUi(anchor, width, height, |
| 1121 | anchorBounds, presenter) && mCallback != null) { |
| 1122 | callback = mCallback; |
| 1123 | } |
| 1124 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | if (callback != null) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1129 | if (id.isVirtual()) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1130 | callback.onAutofillEvent(anchor, id.getVirtualChildId(), |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1131 | AutofillCallback.EVENT_INPUT_SHOWN); |
| 1132 | } else { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1133 | callback.onAutofillEvent(anchor, AutofillCallback.EVENT_INPUT_SHOWN); |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1134 | } |
| 1135 | } |
| 1136 | } |
| 1137 | |
Svetoslav Ganov | a9379d0 | 2017-05-09 17:40:24 -0700 | [diff] [blame] | 1138 | private void authenticate(int sessionId, int authenticationId, IntentSender intent, |
| 1139 | Intent fillInIntent) { |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1140 | synchronized (mLock) { |
| 1141 | if (sessionId == mSessionId) { |
| 1142 | AutofillClient client = getClientLocked(); |
| 1143 | if (client != null) { |
Svetoslav Ganov | a9379d0 | 2017-05-09 17:40:24 -0700 | [diff] [blame] | 1144 | client.autofillCallbackAuthenticate(authenticationId, intent, fillInIntent); |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1145 | } |
| 1146 | } |
| 1147 | } |
| 1148 | } |
| 1149 | |
Svet Ganov | 48f10a2 | 2017-04-26 18:49:30 -0700 | [diff] [blame] | 1150 | private void setState(boolean enabled, boolean resetSession, boolean resetClient) { |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1151 | synchronized (mLock) { |
| 1152 | mEnabled = enabled; |
Svet Ganov | 48f10a2 | 2017-04-26 18:49:30 -0700 | [diff] [blame] | 1153 | if (!mEnabled || resetSession) { |
| 1154 | // Reset the session state |
| 1155 | resetSessionLocked(); |
| 1156 | } |
| 1157 | if (resetClient) { |
| 1158 | // Reset connection to system |
| 1159 | mServiceClient = null; |
| 1160 | } |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1161 | } |
| 1162 | } |
| 1163 | |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 1164 | /** |
| 1165 | * Sets a view as autofilled if the current value is the {code targetValue}. |
| 1166 | * |
| 1167 | * @param view The view that is to be autofilled |
| 1168 | * @param targetValue The value we want to fill into view |
| 1169 | */ |
| 1170 | private void setAutofilledIfValuesIs(@NonNull View view, @Nullable AutofillValue targetValue) { |
| 1171 | AutofillValue currentValue = view.getAutofillValue(); |
| 1172 | if (Objects.equals(currentValue, targetValue)) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1173 | synchronized (mLock) { |
| 1174 | if (mLastAutofilledData == null) { |
| 1175 | mLastAutofilledData = new ParcelableMap(1); |
| 1176 | } |
| 1177 | mLastAutofilledData.put(getAutofillId(view), targetValue); |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 1178 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 1179 | view.setAutofilled(true); |
| 1180 | } |
| 1181 | } |
| 1182 | |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1183 | private void autofill(int sessionId, List<AutofillId> ids, List<AutofillValue> values) { |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1184 | synchronized (mLock) { |
| 1185 | if (sessionId != mSessionId) { |
| 1186 | return; |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1187 | } |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1188 | |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1189 | final AutofillClient client = getClientLocked(); |
| 1190 | if (client == null) { |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1191 | return; |
| 1192 | } |
| 1193 | |
| 1194 | final int itemCount = ids.size(); |
| 1195 | int numApplied = 0; |
| 1196 | ArrayMap<View, SparseArray<AutofillValue>> virtualValues = null; |
Phil Weaver | 846cda93 | 2017-06-15 10:10:06 -0700 | [diff] [blame] | 1197 | final View[] views = client.findViewsByAutofillIdTraversal(getViewIds(ids)); |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1198 | |
| 1199 | for (int i = 0; i < itemCount; i++) { |
| 1200 | final AutofillId id = ids.get(i); |
| 1201 | final AutofillValue value = values.get(i); |
| 1202 | final int viewId = id.getViewId(); |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1203 | final View view = views[i]; |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1204 | if (view == null) { |
| 1205 | Log.w(TAG, "autofill(): no View with id " + viewId); |
| 1206 | continue; |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1207 | } |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1208 | if (id.isVirtual()) { |
| 1209 | if (virtualValues == null) { |
| 1210 | // Most likely there will be just one view with virtual children. |
| 1211 | virtualValues = new ArrayMap<>(1); |
| 1212 | } |
| 1213 | SparseArray<AutofillValue> valuesByParent = virtualValues.get(view); |
| 1214 | if (valuesByParent == null) { |
| 1215 | // We don't know the size yet, but usually it will be just a few fields... |
| 1216 | valuesByParent = new SparseArray<>(5); |
| 1217 | virtualValues.put(view, valuesByParent); |
| 1218 | } |
| 1219 | valuesByParent.put(id.getVirtualChildId(), value); |
| 1220 | } else { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1221 | // Mark the view as to be autofilled with 'value' |
| 1222 | if (mLastAutofilledData == null) { |
| 1223 | mLastAutofilledData = new ParcelableMap(itemCount - i); |
| 1224 | } |
| 1225 | mLastAutofilledData.put(id, value); |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1226 | |
| 1227 | view.autofill(value); |
| 1228 | |
| 1229 | // Set as autofilled if the values match now, e.g. when the value was updated |
| 1230 | // synchronously. |
| 1231 | // If autofill happens async, the view is set to autofilled in |
| 1232 | // notifyValueChanged. |
| 1233 | setAutofilledIfValuesIs(view, value); |
| 1234 | |
| 1235 | numApplied++; |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 1236 | } |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1237 | } |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1238 | |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1239 | if (virtualValues != null) { |
| 1240 | for (int i = 0; i < virtualValues.size(); i++) { |
| 1241 | final View parent = virtualValues.keyAt(i); |
| 1242 | final SparseArray<AutofillValue> childrenValues = virtualValues.valueAt(i); |
| 1243 | parent.autofill(childrenValues); |
| 1244 | numApplied += childrenValues.size(); |
| 1245 | } |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1246 | } |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1247 | |
Felipe Leme | b22d635 | 2017-09-08 20:03:53 -0700 | [diff] [blame^] | 1248 | final LogMaker log = new LogMaker(MetricsEvent.AUTOFILL_DATASET_APPLIED) |
| 1249 | .setPackageName(mContext.getPackageName()) |
| 1250 | .addTaggedData(MetricsEvent.FIELD_AUTOFILL_NUM_VALUES, itemCount) |
| 1251 | .addTaggedData(MetricsEvent.FIELD_AUTOFILL_NUM_VIEWS_FILLED, numApplied); |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1252 | mMetricsLogger.write(log); |
| 1253 | } |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1254 | } |
| 1255 | |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1256 | /** |
| 1257 | * Set the tracked views. |
| 1258 | * |
| 1259 | * @param trackedIds The views to be tracked |
| 1260 | * @param saveOnAllViewsInvisible Finish the session once all tracked views are invisible. |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 1261 | * @param fillableIds Views that might anchor FillUI. |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1262 | */ |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 1263 | private void setTrackedViews(int sessionId, @Nullable AutofillId[] trackedIds, |
| 1264 | boolean saveOnAllViewsInvisible, @Nullable AutofillId[] fillableIds) { |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1265 | synchronized (mLock) { |
| 1266 | if (mEnabled && mSessionId == sessionId) { |
| 1267 | if (saveOnAllViewsInvisible) { |
| 1268 | mTrackedViews = new TrackedViews(trackedIds); |
| 1269 | } else { |
| 1270 | mTrackedViews = null; |
| 1271 | } |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 1272 | if (fillableIds != null) { |
| 1273 | if (mFillableIds == null) { |
| 1274 | mFillableIds = new ArraySet<>(fillableIds.length); |
| 1275 | } |
| 1276 | for (AutofillId id : fillableIds) { |
| 1277 | mFillableIds.add(id); |
| 1278 | } |
| 1279 | if (sVerbose) { |
| 1280 | Log.v(TAG, "setTrackedViews(): fillableIds=" + fillableIds |
| 1281 | + ", mFillableIds" + mFillableIds); |
| 1282 | } |
| 1283 | } |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1284 | } |
| 1285 | } |
| 1286 | } |
| 1287 | |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 1288 | private void setSaveUiState(int sessionId, boolean shown) { |
| 1289 | if (sDebug) Log.d(TAG, "setSaveUiState(" + sessionId + "): " + shown); |
| 1290 | synchronized (mLock) { |
| 1291 | if (mSessionId != NO_SESSION) { |
| 1292 | // Race condition: app triggered a new session after the previous session was |
| 1293 | // finished but before server called setSaveUiState() - need to cancel the new |
| 1294 | // session to avoid further inconsistent behavior. |
| 1295 | Log.w(TAG, "setSaveUiState(" + sessionId + ", " + shown |
| 1296 | + ") called on existing session " + mSessionId + "; cancelling it"); |
| 1297 | cancelSessionLocked(); |
| 1298 | } |
| 1299 | if (shown) { |
| 1300 | mSessionId = sessionId; |
| 1301 | mState = STATE_SHOWING_SAVE_UI; |
| 1302 | } else { |
| 1303 | mSessionId = NO_SESSION; |
| 1304 | mState = STATE_UNKNOWN; |
| 1305 | } |
| 1306 | } |
| 1307 | } |
| 1308 | |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 1309 | private void requestHideFillUi(AutofillId id) { |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1310 | final View anchor = findView(id); |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 1311 | if (sVerbose) Log.v(TAG, "requestHideFillUi(" + id + "): anchor = " + anchor); |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1312 | if (anchor == null) { |
| 1313 | return; |
| 1314 | } |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 1315 | requestHideFillUi(id, anchor); |
| 1316 | } |
| 1317 | |
| 1318 | private void requestHideFillUi(AutofillId id, View anchor) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1319 | |
| 1320 | AutofillCallback callback = null; |
| 1321 | synchronized (mLock) { |
Svet Ganov | 48f10a2 | 2017-04-26 18:49:30 -0700 | [diff] [blame] | 1322 | // We do not check the session id for two reasons: |
| 1323 | // 1. If local and remote session id are off sync the UI would be stuck shown |
| 1324 | // 2. There is a race between the user state being destroyed due the fill |
| 1325 | // service being uninstalled and the UI being dismissed. |
| 1326 | AutofillClient client = getClientLocked(); |
| 1327 | if (client != null) { |
| 1328 | if (client.autofillCallbackRequestHideFillUi() && mCallback != null) { |
| 1329 | callback = mCallback; |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1330 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | if (callback != null) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1335 | if (id.isVirtual()) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1336 | callback.onAutofillEvent(anchor, id.getVirtualChildId(), |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1337 | AutofillCallback.EVENT_INPUT_HIDDEN); |
| 1338 | } else { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1339 | callback.onAutofillEvent(anchor, AutofillCallback.EVENT_INPUT_HIDDEN); |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1340 | } |
| 1341 | } |
| 1342 | } |
| 1343 | |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1344 | private void notifyNoFillUi(int sessionId, AutofillId id) { |
| 1345 | final View anchor = findView(id); |
| 1346 | if (anchor == null) { |
| 1347 | return; |
| 1348 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1349 | |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1350 | AutofillCallback callback = null; |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1351 | synchronized (mLock) { |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1352 | if (mSessionId == sessionId && getClientLocked() != null) { |
| 1353 | callback = mCallback; |
| 1354 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | if (callback != null) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1358 | if (id.isVirtual()) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1359 | callback.onAutofillEvent(anchor, id.getVirtualChildId(), |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1360 | AutofillCallback.EVENT_INPUT_UNAVAILABLE); |
| 1361 | } else { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1362 | callback.onAutofillEvent(anchor, AutofillCallback.EVENT_INPUT_UNAVAILABLE); |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1363 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1364 | |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1365 | } |
| 1366 | } |
| 1367 | |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1368 | /** |
| 1369 | * Get an array of viewIds from a List of {@link AutofillId}. |
| 1370 | * |
| 1371 | * @param autofillIds The autofill ids to convert |
| 1372 | * |
| 1373 | * @return The array of viewIds. |
| 1374 | */ |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 1375 | // TODO: move to Helper as static method |
| 1376 | @NonNull private int[] getViewIds(@NonNull AutofillId[] autofillIds) { |
| 1377 | final int numIds = autofillIds.length; |
| 1378 | final int[] viewIds = new int[numIds]; |
| 1379 | for (int i = 0; i < numIds; i++) { |
| 1380 | viewIds[i] = autofillIds[i].getViewId(); |
| 1381 | } |
| 1382 | |
| 1383 | return viewIds; |
| 1384 | } |
| 1385 | |
| 1386 | // TODO: move to Helper as static method |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1387 | @NonNull private int[] getViewIds(@NonNull List<AutofillId> autofillIds) { |
| 1388 | final int numIds = autofillIds.size(); |
| 1389 | final int[] viewIds = new int[numIds]; |
| 1390 | for (int i = 0; i < numIds; i++) { |
| 1391 | viewIds[i] = autofillIds.get(i).getViewId(); |
| 1392 | } |
| 1393 | |
| 1394 | return viewIds; |
| 1395 | } |
| 1396 | |
| 1397 | /** |
| 1398 | * Find a single view by its id. |
| 1399 | * |
| 1400 | * @param autofillId The autofill id of the view |
| 1401 | * |
| 1402 | * @return The view or {@code null} if view was not found |
| 1403 | */ |
| 1404 | private View findView(@NonNull AutofillId autofillId) { |
| 1405 | final AutofillClient client = getClientLocked(); |
| 1406 | |
| 1407 | if (client == null) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1408 | return null; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1409 | } |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1410 | |
Phil Weaver | 846cda93 | 2017-06-15 10:10:06 -0700 | [diff] [blame] | 1411 | return client.findViewByAutofillIdTraversal(autofillId.getViewId()); |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1412 | } |
| 1413 | |
Felipe Leme | bb81092 | 2017-04-25 15:54:06 -0700 | [diff] [blame] | 1414 | /** @hide */ |
| 1415 | public boolean hasAutofillFeature() { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 1416 | return mService != null; |
| 1417 | } |
| 1418 | |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 1419 | /** @hide */ |
| 1420 | public void onPendingSaveUi(int operation, IBinder token) { |
| 1421 | if (sVerbose) Log.v(TAG, "onPendingSaveUi(" + operation + "): " + token); |
| 1422 | |
| 1423 | synchronized (mLock) { |
| 1424 | try { |
| 1425 | mService.onPendingSaveUi(operation, token); |
| 1426 | } catch (RemoteException e) { |
| 1427 | e.rethrowFromSystemServer(); |
| 1428 | } |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | /** @hide */ |
| 1433 | public void dump(String outerPrefix, PrintWriter pw) { |
| 1434 | pw.print(outerPrefix); pw.println("AutofillManager:"); |
| 1435 | final String pfx = outerPrefix + " "; |
| 1436 | pw.print(pfx); pw.print("sessionId: "); pw.println(mSessionId); |
| 1437 | pw.print(pfx); pw.print("state: "); pw.println( |
| 1438 | DebugUtils.flagsToString(AutofillManager.class, "STATE_", mState)); |
| 1439 | pw.print(pfx); pw.print("enabled: "); pw.println(mEnabled); |
| 1440 | pw.print(pfx); pw.print("hasService: "); pw.println(mService != null); |
| 1441 | pw.print(pfx); pw.print("hasCallback: "); pw.println(mCallback != null); |
| 1442 | pw.print(pfx); pw.print("last autofilled data: "); pw.println(mLastAutofilledData); |
| 1443 | pw.print(pfx); pw.print("tracked views: "); |
| 1444 | if (mTrackedViews == null) { |
| 1445 | pw.println("null"); |
| 1446 | } else { |
| 1447 | final String pfx2 = pfx + " "; |
| 1448 | pw.println(); |
| 1449 | pw.print(pfx2); pw.print("visible:"); pw.println(mTrackedViews.mVisibleTrackedIds); |
| 1450 | pw.print(pfx2); pw.print("invisible:"); pw.println(mTrackedViews.mInvisibleTrackedIds); |
| 1451 | } |
| 1452 | pw.print(pfx); pw.print("fillable ids: "); pw.println(mFillableIds); |
| 1453 | } |
| 1454 | |
| 1455 | private boolean isActiveLocked() { |
| 1456 | return mState == STATE_ACTIVE; |
| 1457 | } |
| 1458 | |
Felipe Leme | 9876a6f | 2017-05-30 15:47:28 -0700 | [diff] [blame] | 1459 | private void post(Runnable runnable) { |
| 1460 | final AutofillClient client = getClientLocked(); |
| 1461 | if (client == null) { |
| 1462 | if (sVerbose) Log.v(TAG, "ignoring post() because client is null"); |
| 1463 | return; |
| 1464 | } |
| 1465 | client.runOnUiThread(runnable); |
| 1466 | } |
| 1467 | |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1468 | /** |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1469 | * View tracking information. Once all tracked views become invisible the session is finished. |
| 1470 | */ |
| 1471 | private class TrackedViews { |
| 1472 | /** Visible tracked views */ |
| 1473 | @Nullable private ArraySet<AutofillId> mVisibleTrackedIds; |
| 1474 | |
| 1475 | /** Invisible tracked views */ |
| 1476 | @Nullable private ArraySet<AutofillId> mInvisibleTrackedIds; |
| 1477 | |
| 1478 | /** |
| 1479 | * Check if set is null or value is in set. |
| 1480 | * |
| 1481 | * @param set The set or null (== empty set) |
| 1482 | * @param value The value that might be in the set |
| 1483 | * |
| 1484 | * @return {@code true} iff set is not empty and value is in set |
| 1485 | */ |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 1486 | // TODO: move to Helper as static method |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1487 | private <T> boolean isInSet(@Nullable ArraySet<T> set, T value) { |
| 1488 | return set != null && set.contains(value); |
| 1489 | } |
| 1490 | |
| 1491 | /** |
| 1492 | * Add a value to a set. If set is null, create a new set. |
| 1493 | * |
| 1494 | * @param set The set or null (== empty set) |
| 1495 | * @param valueToAdd The value to add |
| 1496 | * |
| 1497 | * @return The set including the new value. If set was {@code null}, a set containing only |
| 1498 | * the new value. |
| 1499 | */ |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 1500 | // TODO: move to Helper as static method |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1501 | @NonNull |
| 1502 | private <T> ArraySet<T> addToSet(@Nullable ArraySet<T> set, T valueToAdd) { |
| 1503 | if (set == null) { |
| 1504 | set = new ArraySet<>(1); |
| 1505 | } |
| 1506 | |
| 1507 | set.add(valueToAdd); |
| 1508 | |
| 1509 | return set; |
| 1510 | } |
| 1511 | |
| 1512 | /** |
| 1513 | * Remove a value from a set. |
| 1514 | * |
| 1515 | * @param set The set or null (== empty set) |
| 1516 | * @param valueToRemove The value to remove |
| 1517 | * |
| 1518 | * @return The set without the removed value. {@code null} if set was null, or is empty |
| 1519 | * after removal. |
| 1520 | */ |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 1521 | // TODO: move to Helper as static method |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1522 | @Nullable |
| 1523 | private <T> ArraySet<T> removeFromSet(@Nullable ArraySet<T> set, T valueToRemove) { |
| 1524 | if (set == null) { |
| 1525 | return null; |
| 1526 | } |
| 1527 | |
| 1528 | set.remove(valueToRemove); |
| 1529 | |
| 1530 | if (set.isEmpty()) { |
| 1531 | return null; |
| 1532 | } |
| 1533 | |
| 1534 | return set; |
| 1535 | } |
| 1536 | |
| 1537 | /** |
| 1538 | * Set the tracked views. |
| 1539 | * |
| 1540 | * @param trackedIds The views to be tracked |
| 1541 | */ |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 1542 | TrackedViews(@Nullable AutofillId[] trackedIds) { |
| 1543 | final AutofillClient client = getClientLocked(); |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1544 | if (trackedIds != null && client != null) { |
| 1545 | final boolean[] isVisible; |
| 1546 | |
| 1547 | if (client.isVisibleForAutofill()) { |
| 1548 | isVisible = client.getViewVisibility(getViewIds(trackedIds)); |
| 1549 | } else { |
| 1550 | // All false |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 1551 | isVisible = new boolean[trackedIds.length]; |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1552 | } |
| 1553 | |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 1554 | final int numIds = trackedIds.length; |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1555 | for (int i = 0; i < numIds; i++) { |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 1556 | final AutofillId id = trackedIds[i]; |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1557 | |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1558 | if (isVisible[i]) { |
Philip P. Moltmann | e0e2871 | 2017-04-20 15:19:06 -0700 | [diff] [blame] | 1559 | mVisibleTrackedIds = addToSet(mVisibleTrackedIds, id); |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1560 | } else { |
| 1561 | mInvisibleTrackedIds = addToSet(mInvisibleTrackedIds, id); |
| 1562 | } |
| 1563 | } |
| 1564 | } |
| 1565 | |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 1566 | if (sVerbose) { |
| 1567 | Log.v(TAG, "TrackedViews(trackedIds=" + trackedIds + "): " |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1568 | + " mVisibleTrackedIds=" + mVisibleTrackedIds |
| 1569 | + " mInvisibleTrackedIds=" + mInvisibleTrackedIds); |
| 1570 | } |
| 1571 | |
| 1572 | if (mVisibleTrackedIds == null) { |
| 1573 | finishSessionLocked(); |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | /** |
| 1578 | * Called when a {@link View view's} visibility changes. |
| 1579 | * |
Svetoslav Ganov | 4db89ba | 2017-06-28 16:50:37 -0700 | [diff] [blame] | 1580 | * @param id the id of the view/virtual view whose visibility changed. |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1581 | * @param isVisible visible if the view is visible in the view hierarchy. |
| 1582 | */ |
Svetoslav Ganov | 4db89ba | 2017-06-28 16:50:37 -0700 | [diff] [blame] | 1583 | void notifyViewVisibilityChanged(@NonNull AutofillId id, boolean isVisible) { |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1584 | AutofillClient client = getClientLocked(); |
| 1585 | |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 1586 | if (sDebug) { |
Svetoslav Ganov | 4db89ba | 2017-06-28 16:50:37 -0700 | [diff] [blame] | 1587 | Log.d(TAG, "notifyViewVisibilityChanged(): id=" + id + " isVisible=" |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1588 | + isVisible); |
| 1589 | } |
| 1590 | |
| 1591 | if (client != null && client.isVisibleForAutofill()) { |
| 1592 | if (isVisible) { |
| 1593 | if (isInSet(mInvisibleTrackedIds, id)) { |
| 1594 | mInvisibleTrackedIds = removeFromSet(mInvisibleTrackedIds, id); |
| 1595 | mVisibleTrackedIds = addToSet(mVisibleTrackedIds, id); |
| 1596 | } |
| 1597 | } else { |
| 1598 | if (isInSet(mVisibleTrackedIds, id)) { |
| 1599 | mVisibleTrackedIds = removeFromSet(mVisibleTrackedIds, id); |
| 1600 | mInvisibleTrackedIds = addToSet(mInvisibleTrackedIds, id); |
| 1601 | } |
| 1602 | } |
| 1603 | } |
| 1604 | |
| 1605 | if (mVisibleTrackedIds == null) { |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 1606 | if (sVerbose) { |
| 1607 | Log.v(TAG, "No more visible ids. Invisibile = " + mInvisibleTrackedIds); |
| 1608 | } |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1609 | finishSessionLocked(); |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | /** |
| 1614 | * Called once the client becomes visible. |
| 1615 | * |
| 1616 | * @see AutofillClient#isVisibleForAutofill() |
| 1617 | */ |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1618 | void onVisibleForAutofillLocked() { |
| 1619 | // The visibility of the views might have changed while the client was not be visible, |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1620 | // hence update the visibility state for all views. |
| 1621 | AutofillClient client = getClientLocked(); |
| 1622 | ArraySet<AutofillId> updatedVisibleTrackedIds = null; |
| 1623 | ArraySet<AutofillId> updatedInvisibleTrackedIds = null; |
| 1624 | if (client != null) { |
| 1625 | if (mInvisibleTrackedIds != null) { |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1626 | final ArrayList<AutofillId> orderedInvisibleIds = |
| 1627 | new ArrayList<>(mInvisibleTrackedIds); |
| 1628 | final boolean[] isVisible = client.getViewVisibility( |
| 1629 | getViewIds(orderedInvisibleIds)); |
| 1630 | |
| 1631 | final int numInvisibleTrackedIds = orderedInvisibleIds.size(); |
| 1632 | for (int i = 0; i < numInvisibleTrackedIds; i++) { |
| 1633 | final AutofillId id = orderedInvisibleIds.get(i); |
| 1634 | if (isVisible[i]) { |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1635 | updatedVisibleTrackedIds = addToSet(updatedVisibleTrackedIds, id); |
| 1636 | |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 1637 | if (sDebug) { |
| 1638 | Log.d(TAG, "onVisibleForAutofill() " + id + " became visible"); |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1639 | } |
| 1640 | } else { |
| 1641 | updatedInvisibleTrackedIds = addToSet(updatedInvisibleTrackedIds, id); |
| 1642 | } |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | if (mVisibleTrackedIds != null) { |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1647 | final ArrayList<AutofillId> orderedVisibleIds = |
| 1648 | new ArrayList<>(mVisibleTrackedIds); |
| 1649 | final boolean[] isVisible = client.getViewVisibility( |
| 1650 | getViewIds(orderedVisibleIds)); |
| 1651 | |
| 1652 | final int numVisibleTrackedIds = orderedVisibleIds.size(); |
| 1653 | for (int i = 0; i < numVisibleTrackedIds; i++) { |
| 1654 | final AutofillId id = orderedVisibleIds.get(i); |
| 1655 | |
| 1656 | if (isVisible[i]) { |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1657 | updatedVisibleTrackedIds = addToSet(updatedVisibleTrackedIds, id); |
| 1658 | } else { |
| 1659 | updatedInvisibleTrackedIds = addToSet(updatedInvisibleTrackedIds, id); |
| 1660 | |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 1661 | if (sDebug) { |
| 1662 | Log.d(TAG, "onVisibleForAutofill() " + id + " became invisible"); |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1663 | } |
| 1664 | } |
| 1665 | } |
| 1666 | } |
| 1667 | |
| 1668 | mInvisibleTrackedIds = updatedInvisibleTrackedIds; |
| 1669 | mVisibleTrackedIds = updatedVisibleTrackedIds; |
| 1670 | } |
| 1671 | |
| 1672 | if (mVisibleTrackedIds == null) { |
| 1673 | finishSessionLocked(); |
| 1674 | } |
| 1675 | } |
| 1676 | } |
| 1677 | |
| 1678 | /** |
Felipe Leme | 744976e | 2017-07-31 11:34:14 -0700 | [diff] [blame] | 1679 | * Callback for autofill related events. |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1680 | * |
| 1681 | * <p>Typically used for applications that display their own "auto-complete" views, so they can |
Felipe Leme | 744976e | 2017-07-31 11:34:14 -0700 | [diff] [blame] | 1682 | * enable / disable such views when the autofill UI affordance is shown / hidden. |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1683 | */ |
| 1684 | public abstract static class AutofillCallback { |
| 1685 | |
| 1686 | /** @hide */ |
| 1687 | @IntDef({EVENT_INPUT_SHOWN, EVENT_INPUT_HIDDEN}) |
| 1688 | @Retention(RetentionPolicy.SOURCE) |
| 1689 | public @interface AutofillEventType {} |
| 1690 | |
| 1691 | /** |
Felipe Leme | 744976e | 2017-07-31 11:34:14 -0700 | [diff] [blame] | 1692 | * The autofill input UI affordance associated with the view was shown. |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1693 | * |
| 1694 | * <p>If the view provides its own auto-complete UI affordance and its currently shown, it |
| 1695 | * should be hidden upon receiving this event. |
| 1696 | */ |
| 1697 | public static final int EVENT_INPUT_SHOWN = 1; |
| 1698 | |
| 1699 | /** |
Felipe Leme | 744976e | 2017-07-31 11:34:14 -0700 | [diff] [blame] | 1700 | * The autofill input UI affordance associated with the view was hidden. |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1701 | * |
| 1702 | * <p>If the view provides its own auto-complete UI affordance that was hidden upon a |
| 1703 | * {@link #EVENT_INPUT_SHOWN} event, it could be shown again now. |
| 1704 | */ |
| 1705 | public static final int EVENT_INPUT_HIDDEN = 2; |
| 1706 | |
| 1707 | /** |
Felipe Leme | 744976e | 2017-07-31 11:34:14 -0700 | [diff] [blame] | 1708 | * The autofill input UI affordance associated with the view isn't shown because |
Felipe Leme | 24aae15 | 2017-03-15 12:33:01 -0700 | [diff] [blame] | 1709 | * autofill is not available. |
| 1710 | * |
| 1711 | * <p>If the view provides its own auto-complete UI affordance but was not displaying it |
| 1712 | * to avoid flickering, it could shown it upon receiving this event. |
| 1713 | */ |
| 1714 | public static final int EVENT_INPUT_UNAVAILABLE = 3; |
| 1715 | |
| 1716 | /** |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1717 | * Called after a change in the autofill state associated with a view. |
| 1718 | * |
| 1719 | * @param view view associated with the change. |
| 1720 | * |
| 1721 | * @param event currently either {@link #EVENT_INPUT_SHOWN} or {@link #EVENT_INPUT_HIDDEN}. |
| 1722 | */ |
Felipe Leme | 81f01d9 | 2017-03-16 17:13:25 -0700 | [diff] [blame] | 1723 | public void onAutofillEvent(@NonNull View view, @AutofillEventType int event) { |
| 1724 | } |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1725 | |
| 1726 | /** |
| 1727 | * Called after a change in the autofill state associated with a virtual view. |
| 1728 | * |
| 1729 | * @param view parent view associated with the change. |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 1730 | * @param virtualId id identifying the virtual child inside the parent view. |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1731 | * |
| 1732 | * @param event currently either {@link #EVENT_INPUT_SHOWN} or {@link #EVENT_INPUT_HIDDEN}. |
| 1733 | */ |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 1734 | public void onAutofillEvent(@NonNull View view, int virtualId, |
| 1735 | @AutofillEventType int event) { |
Felipe Leme | 81f01d9 | 2017-03-16 17:13:25 -0700 | [diff] [blame] | 1736 | } |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1737 | } |
| 1738 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 1739 | private static final class AutofillManagerClient extends IAutoFillManagerClient.Stub { |
| 1740 | private final WeakReference<AutofillManager> mAfm; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1741 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 1742 | AutofillManagerClient(AutofillManager autofillManager) { |
| 1743 | mAfm = new WeakReference<>(autofillManager); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | @Override |
Svet Ganov | 48f10a2 | 2017-04-26 18:49:30 -0700 | [diff] [blame] | 1747 | public void setState(boolean enabled, boolean resetSession, boolean resetClient) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 1748 | final AutofillManager afm = mAfm.get(); |
| 1749 | if (afm != null) { |
Felipe Leme | 9876a6f | 2017-05-30 15:47:28 -0700 | [diff] [blame] | 1750 | afm.post(() -> afm.setState(enabled, resetSession, resetClient)); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | @Override |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1755 | public void autofill(int sessionId, List<AutofillId> ids, List<AutofillValue> values) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 1756 | final AutofillManager afm = mAfm.get(); |
| 1757 | if (afm != null) { |
Felipe Leme | 9876a6f | 2017-05-30 15:47:28 -0700 | [diff] [blame] | 1758 | afm.post(() -> afm.autofill(sessionId, ids, values)); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1759 | } |
| 1760 | } |
| 1761 | |
| 1762 | @Override |
Svetoslav Ganov | a9379d0 | 2017-05-09 17:40:24 -0700 | [diff] [blame] | 1763 | public void authenticate(int sessionId, int authenticationId, IntentSender intent, |
| 1764 | Intent fillInIntent) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 1765 | final AutofillManager afm = mAfm.get(); |
| 1766 | if (afm != null) { |
Felipe Leme | 9876a6f | 2017-05-30 15:47:28 -0700 | [diff] [blame] | 1767 | afm.post(() -> afm.authenticate(sessionId, authenticationId, intent, fillInIntent)); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1768 | } |
| 1769 | } |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1770 | |
| 1771 | @Override |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1772 | public void requestShowFillUi(int sessionId, AutofillId id, int width, int height, |
| 1773 | Rect anchorBounds, IAutofillWindowPresenter presenter) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 1774 | final AutofillManager afm = mAfm.get(); |
| 1775 | if (afm != null) { |
Felipe Leme | 9876a6f | 2017-05-30 15:47:28 -0700 | [diff] [blame] | 1776 | afm.post(() -> afm.requestShowFillUi(sessionId, id, width, height, anchorBounds, |
| 1777 | presenter)); |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1778 | } |
| 1779 | } |
| 1780 | |
| 1781 | @Override |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1782 | public void requestHideFillUi(int sessionId, AutofillId id) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1783 | final AutofillManager afm = mAfm.get(); |
| 1784 | if (afm != null) { |
Felipe Leme | 9876a6f | 2017-05-30 15:47:28 -0700 | [diff] [blame] | 1785 | afm.post(() -> afm.requestHideFillUi(id)); |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1786 | } |
| 1787 | } |
| 1788 | |
| 1789 | @Override |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1790 | public void notifyNoFillUi(int sessionId, AutofillId id) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1791 | final AutofillManager afm = mAfm.get(); |
| 1792 | if (afm != null) { |
Felipe Leme | 9876a6f | 2017-05-30 15:47:28 -0700 | [diff] [blame] | 1793 | afm.post(() -> afm.notifyNoFillUi(sessionId, id)); |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1794 | } |
| 1795 | } |
Svet Ganov | c3d1c85 | 2017-04-12 22:34:28 -0700 | [diff] [blame] | 1796 | |
| 1797 | @Override |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 1798 | public void startIntentSender(IntentSender intentSender, Intent intent) { |
Svet Ganov | c3d1c85 | 2017-04-12 22:34:28 -0700 | [diff] [blame] | 1799 | final AutofillManager afm = mAfm.get(); |
| 1800 | if (afm != null) { |
Felipe Leme | 9876a6f | 2017-05-30 15:47:28 -0700 | [diff] [blame] | 1801 | afm.post(() -> { |
Svet Ganov | c3d1c85 | 2017-04-12 22:34:28 -0700 | [diff] [blame] | 1802 | try { |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 1803 | afm.mContext.startIntentSender(intentSender, intent, 0, 0, 0); |
Svet Ganov | c3d1c85 | 2017-04-12 22:34:28 -0700 | [diff] [blame] | 1804 | } catch (IntentSender.SendIntentException e) { |
| 1805 | Log.e(TAG, "startIntentSender() failed for intent:" + intentSender, e); |
| 1806 | } |
| 1807 | }); |
| 1808 | } |
| 1809 | } |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1810 | |
| 1811 | @Override |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 1812 | public void setTrackedViews(int sessionId, AutofillId[] ids, |
| 1813 | boolean saveOnAllViewsInvisible, AutofillId[] fillableIds) { |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1814 | final AutofillManager afm = mAfm.get(); |
| 1815 | if (afm != null) { |
Felipe Leme | 9876a6f | 2017-05-30 15:47:28 -0700 | [diff] [blame] | 1816 | afm.post(() -> |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 1817 | afm.setTrackedViews(sessionId, ids, saveOnAllViewsInvisible, fillableIds) |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1818 | ); |
| 1819 | } |
| 1820 | } |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 1821 | |
| 1822 | @Override |
| 1823 | public void setSaveUiState(int sessionId, boolean shown) { |
| 1824 | final AutofillManager afm = mAfm.get(); |
| 1825 | if (afm != null) { |
| 1826 | afm.post(() ->afm.setSaveUiState(sessionId, shown)); |
| 1827 | } |
| 1828 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1829 | } |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 1830 | } |