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