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