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