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 | a7de402 | 2019-02-19 17:16:45 -0800 | [diff] [blame] | 19 | import static android.service.autofill.FillRequest.FLAG_AUGMENTED_AUTOFILL_REQUEST; |
felipeal | fd3f8f6 | 2018-02-07 11:49:07 +0100 | [diff] [blame] | 20 | import static android.service.autofill.FillRequest.FLAG_MANUAL_REQUEST; |
| 21 | import static android.view.autofill.Helper.sDebug; |
| 22 | import static android.view.autofill.Helper.sVerbose; |
| 23 | |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 24 | import android.accessibilityservice.AccessibilityServiceInfo; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 25 | import android.annotation.IntDef; |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 26 | import android.annotation.NonNull; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 27 | import android.annotation.Nullable; |
Jeff Sharkey | 98af2e4 | 2018-02-16 10:14:57 -0700 | [diff] [blame] | 28 | import android.annotation.RequiresFeature; |
Felipe Leme | 559e21d | 2019-01-18 17:57:21 -0800 | [diff] [blame] | 29 | import android.annotation.SystemApi; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 30 | import android.annotation.SystemService; |
Felipe Leme | 2cab38c | 2019-01-11 10:39:14 -0800 | [diff] [blame] | 31 | import android.annotation.TestApi; |
Felipe Leme | a4f39cd | 2019-02-19 15:08:59 -0800 | [diff] [blame] | 32 | import android.content.AutofillOptions; |
Felipe Leme | 17292d1 | 2017-10-24 14:03:10 -0700 | [diff] [blame] | 33 | import android.content.ComponentName; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 34 | import android.content.Context; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 35 | import android.content.Intent; |
| 36 | import android.content.IntentSender; |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 37 | import android.content.pm.PackageManager; |
| 38 | import android.content.pm.ResolveInfo; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 39 | import android.graphics.Rect; |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 40 | import android.metrics.LogMaker; |
Felipe Leme | 68b2222 | 2018-07-24 14:57:01 -0700 | [diff] [blame] | 41 | import android.os.Build; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 42 | import android.os.Bundle; |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 43 | import android.os.IBinder; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 44 | import android.os.Parcelable; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 45 | import android.os.RemoteException; |
Philip P. Moltmann | cc684ed | 2017-04-17 14:18:33 -0700 | [diff] [blame] | 46 | import android.service.autofill.AutofillService; |
| 47 | import android.service.autofill.FillEventHistory; |
Felipe Leme | 452886a | 2017-11-27 13:09:13 -0800 | [diff] [blame] | 48 | import android.service.autofill.UserData; |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 49 | import android.util.ArrayMap; |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 50 | import android.util.ArraySet; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 51 | import android.util.Log; |
Felipe Leme | ce40498 | 2018-09-17 09:46:13 -0700 | [diff] [blame] | 52 | import android.util.Slog; |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 53 | import android.util.SparseArray; |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 54 | import android.view.Choreographer; |
Dake Gu | 6a20a19 | 2018-02-08 12:09:30 -0800 | [diff] [blame] | 55 | import android.view.KeyEvent; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 56 | import android.view.View; |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 57 | import android.view.accessibility.AccessibilityEvent; |
| 58 | import android.view.accessibility.AccessibilityManager; |
| 59 | import android.view.accessibility.AccessibilityNodeInfo; |
| 60 | import android.view.accessibility.AccessibilityNodeProvider; |
| 61 | import android.view.accessibility.AccessibilityWindowInfo; |
felipeal | fd3f8f6 | 2018-02-07 11:49:07 +0100 | [diff] [blame] | 62 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 63 | import com.android.internal.annotations.GuardedBy; |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 64 | import com.android.internal.logging.MetricsLogger; |
Felipe Leme | b22d635 | 2017-09-08 20:03:53 -0700 | [diff] [blame] | 65 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Felipe Leme | d4e5228 | 2018-06-18 13:56:38 -0700 | [diff] [blame] | 66 | import com.android.internal.os.IResultReceiver; |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 67 | import com.android.internal.util.ArrayUtils; |
Felipe Leme | 637e05e | 2017-12-06 12:09:37 -0800 | [diff] [blame] | 68 | import com.android.internal.util.Preconditions; |
Felipe Leme | c0c15a3 | 2019-01-08 10:53:33 -0800 | [diff] [blame] | 69 | import com.android.internal.util.SyncResultReceiver; |
felipeal | fd3f8f6 | 2018-02-07 11:49:07 +0100 | [diff] [blame] | 70 | |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 71 | import org.xmlpull.v1.XmlPullParserException; |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 72 | |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 73 | import java.io.IOException; |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 74 | import java.io.PrintWriter; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 75 | import java.lang.annotation.Retention; |
| 76 | import java.lang.annotation.RetentionPolicy; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 77 | import java.lang.ref.WeakReference; |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 78 | import java.util.ArrayList; |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 79 | import java.util.Arrays; |
Felipe Leme | 27f4573 | 2017-12-22 09:05:22 -0800 | [diff] [blame] | 80 | import java.util.Collections; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 81 | import java.util.List; |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 82 | import java.util.Objects; |
Felipe Leme | 7a3c9f5 | 2019-02-13 16:32:49 -0800 | [diff] [blame] | 83 | import java.util.Set; |
Felipe Leme | c0c15a3 | 2019-01-08 10:53:33 -0800 | [diff] [blame] | 84 | |
Felipe Leme | 67e6209 | 2018-02-15 14:47:31 -0800 | [diff] [blame] | 85 | //TODO: use java.lang.ref.Cleaner once Android supports Java 9 |
felipeal | fd3f8f6 | 2018-02-07 11:49:07 +0100 | [diff] [blame] | 86 | import sun.misc.Cleaner; |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 87 | |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 88 | /** |
Laura Davis | 43e75d9 | 2018-08-10 15:46:06 -0700 | [diff] [blame] | 89 | * <p>The {@link AutofillManager} class provides ways for apps and custom views to |
| 90 | * integrate with the Autofill Framework lifecycle. |
| 91 | * |
| 92 | * <p>To learn about using Autofill in your app, read |
| 93 | * the <a href="/guide/topics/text/autofill">Autofill Framework</a> guides. |
| 94 | * |
| 95 | * <h3 id="autofill-lifecycle">Autofill lifecycle</h3> |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 96 | * |
Felipe Leme | 744976e | 2017-07-31 11:34:14 -0700 | [diff] [blame] | 97 | * <p>The autofill lifecycle starts with the creation of an autofill context associated with an |
Laura Davis | 43e75d9 | 2018-08-10 15:46:06 -0700 | [diff] [blame] | 98 | * activity context. The autofill context is created when one of the following methods is called for |
Felipe Leme | 744976e | 2017-07-31 11:34:14 -0700 | [diff] [blame] | 99 | * the first time in an activity context, and the current user has an enabled autofill service: |
| 100 | * |
| 101 | * <ul> |
| 102 | * <li>{@link #notifyViewEntered(View)} |
| 103 | * <li>{@link #notifyViewEntered(View, int, Rect)} |
| 104 | * <li>{@link #requestAutofill(View)} |
| 105 | * </ul> |
| 106 | * |
Laura Davis | 43e75d9 | 2018-08-10 15:46:06 -0700 | [diff] [blame] | 107 | * <p>Typically, the context is automatically created when the first view of the activity is |
Felipe Leme | 744976e | 2017-07-31 11:34:14 -0700 | [diff] [blame] | 108 | * focused because {@code View.onFocusChanged()} indirectly calls |
| 109 | * {@link #notifyViewEntered(View)}. App developers can call {@link #requestAutofill(View)} to |
| 110 | * explicitly create it (for example, a custom view developer could offer a contextual menu action |
| 111 | * in a text-field view to let users manually request autofill). |
| 112 | * |
| 113 | * <p>After the context is created, the Android System creates a {@link android.view.ViewStructure} |
| 114 | * that represents the view hierarchy by calling |
| 115 | * {@link View#dispatchProvideAutofillStructure(android.view.ViewStructure, int)} in the root views |
| 116 | * of all application windows. By default, {@code dispatchProvideAutofillStructure()} results in |
| 117 | * subsequent calls to {@link View#onProvideAutofillStructure(android.view.ViewStructure, int)} and |
| 118 | * {@link View#onProvideAutofillVirtualStructure(android.view.ViewStructure, int)} for each view in |
| 119 | * the hierarchy. |
| 120 | * |
| 121 | * <p>The resulting {@link android.view.ViewStructure} is then passed to the autofill service, which |
| 122 | * parses it looking for views that can be autofilled. If the service finds such views, it returns |
| 123 | * a data structure to the Android System containing the following optional info: |
| 124 | * |
| 125 | * <ul> |
| 126 | * <li>Datasets used to autofill subsets of views in the activity. |
| 127 | * <li>Id of views that the service can save their values for future autofilling. |
| 128 | * </ul> |
| 129 | * |
| 130 | * <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] | 131 | * UI associated with the view, when the view is focused on and is part of a dataset. |
| 132 | * 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] | 133 | * {@link AutofillCallback} through {@link #registerCallback(AutofillCallback)}. When the user |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 134 | * 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] | 135 | * calls to {@link View#autofill(AutofillValue)} or {@link View#autofill(SparseArray)}. |
| 136 | * |
| 137 | * <p>When the service returns ids of savable views, the Android System keeps track of changes |
| 138 | * made to these views, so they can be used to determine if the autofill save UI is shown later. |
| 139 | * |
| 140 | * <p>The context is then finished when one of the following occurs: |
| 141 | * |
| 142 | * <ul> |
| 143 | * <li>{@link #commit()} is called or all savable views are gone. |
| 144 | * <li>{@link #cancel()} is called. |
| 145 | * </ul> |
| 146 | * |
| 147 | * <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] | 148 | * 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] | 149 | * option to Save, the current value of the views is then sent to the autofill service. |
| 150 | * |
Laura Davis | 43e75d9 | 2018-08-10 15:46:06 -0700 | [diff] [blame] | 151 | * <h3 id="additional-notes">Additional notes</h3> |
| 152 | * |
| 153 | * <p>It is safe to call <code>AutofillManager</code> methods from any thread. |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 154 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 155 | @SystemService(Context.AUTOFILL_MANAGER_SERVICE) |
Jeff Sharkey | 98af2e4 | 2018-02-16 10:14:57 -0700 | [diff] [blame] | 156 | @RequiresFeature(PackageManager.FEATURE_AUTOFILL) |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 157 | public final class AutofillManager { |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 158 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 159 | private static final String TAG = "AutofillManager"; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 160 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 161 | /** |
| 162 | * Intent extra: The assist structure which captures the filled screen. |
Felipe Leme | 7320ca9 | 2017-03-29 15:09:54 -0700 | [diff] [blame] | 163 | * |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 164 | * <p> |
| 165 | * Type: {@link android.app.assist.AssistStructure} |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 166 | */ |
| 167 | public static final String EXTRA_ASSIST_STRUCTURE = |
| 168 | "android.view.autofill.extra.ASSIST_STRUCTURE"; |
| 169 | |
| 170 | /** |
| 171 | * Intent extra: The result of an authentication operation. It is |
| 172 | * either a fully populated {@link android.service.autofill.FillResponse} |
| 173 | * or a fully populated {@link android.service.autofill.Dataset} if |
| 174 | * a response or a dataset is being authenticated respectively. |
| 175 | * |
| 176 | * <p> |
| 177 | * Type: {@link android.service.autofill.FillResponse} or a |
| 178 | * {@link android.service.autofill.Dataset} |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 179 | */ |
| 180 | public static final String EXTRA_AUTHENTICATION_RESULT = |
| 181 | "android.view.autofill.extra.AUTHENTICATION_RESULT"; |
| 182 | |
Felipe Leme | 7320ca9 | 2017-03-29 15:09:54 -0700 | [diff] [blame] | 183 | /** |
| 184 | * Intent extra: The optional extras provided by the |
| 185 | * {@link android.service.autofill.AutofillService}. |
| 186 | * |
| 187 | * <p>For example, when the service responds to a {@link |
| 188 | * android.service.autofill.FillCallback#onSuccess(android.service.autofill.FillResponse)} with |
| 189 | * a {@code FillResponse} that requires authentication, the Intent that launches the |
| 190 | * service authentication will contain the Bundle set by |
Felipe Leme | 49e9696 | 2017-04-20 15:46:18 -0700 | [diff] [blame] | 191 | * {@link android.service.autofill.FillResponse.Builder#setClientState(Bundle)} on this extra. |
Felipe Leme | 7320ca9 | 2017-03-29 15:09:54 -0700 | [diff] [blame] | 192 | * |
Felipe Leme | a937238 | 2017-10-09 14:42:36 -0700 | [diff] [blame] | 193 | * <p>On Android {@link android.os.Build.VERSION_CODES#P} and higher, the autofill service |
| 194 | * can also add this bundle to the {@link Intent} set as the |
| 195 | * {@link android.app.Activity#setResult(int, Intent) result} for an authentication request, |
| 196 | * so the bundle can be recovered later on |
| 197 | * {@link android.service.autofill.SaveRequest#getClientState()}. |
| 198 | * |
Felipe Leme | 7320ca9 | 2017-03-29 15:09:54 -0700 | [diff] [blame] | 199 | * <p> |
| 200 | * Type: {@link android.os.Bundle} |
| 201 | */ |
Felipe Leme | 37a440fd | 2017-04-28 10:50:20 -0700 | [diff] [blame] | 202 | public static final String EXTRA_CLIENT_STATE = |
Jeff Sharkey | 000ce80 | 2017-04-29 13:13:27 -0600 | [diff] [blame] | 203 | "android.view.autofill.extra.CLIENT_STATE"; |
Felipe Leme | 7320ca9 | 2017-03-29 15:09:54 -0700 | [diff] [blame] | 204 | |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 205 | /** @hide */ |
| 206 | public static final String EXTRA_RESTORE_SESSION_TOKEN = |
| 207 | "android.view.autofill.extra.RESTORE_SESSION_TOKEN"; |
| 208 | |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 209 | /** |
| 210 | * Internal extra used to pass a binder to the {@link IAugmentedAutofillManagerClient}. |
| 211 | * |
| 212 | * @hide |
| 213 | */ |
| 214 | public static final String EXTRA_AUGMENTED_AUTOFILL_CLIENT = |
| 215 | "android.view.autofill.extra.AUGMENTED_AUTOFILL_CLIENT"; |
| 216 | |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 217 | private static final String SESSION_ID_TAG = "android:sessionId"; |
| 218 | private static final String STATE_TAG = "android:state"; |
| 219 | private static final String LAST_AUTOFILLED_DATA_TAG = "android:lastAutoFilledData"; |
| 220 | |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 221 | /** @hide */ public static final int ACTION_START_SESSION = 1; |
| 222 | /** @hide */ public static final int ACTION_VIEW_ENTERED = 2; |
| 223 | /** @hide */ public static final int ACTION_VIEW_EXITED = 3; |
| 224 | /** @hide */ public static final int ACTION_VALUE_CHANGED = 4; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 225 | |
Felipe Leme | 68b2222 | 2018-07-24 14:57:01 -0700 | [diff] [blame] | 226 | /** @hide */ public static final int NO_LOGGING = 0; |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 227 | /** @hide */ public static final int FLAG_ADD_CLIENT_ENABLED = 0x1; |
| 228 | /** @hide */ public static final int FLAG_ADD_CLIENT_DEBUG = 0x2; |
| 229 | /** @hide */ public static final int FLAG_ADD_CLIENT_VERBOSE = 0x4; |
Felipe Leme | 68b2222 | 2018-07-24 14:57:01 -0700 | [diff] [blame] | 230 | /** @hide */ |
| 231 | public static final int DEFAULT_LOGGING_LEVEL = Build.IS_DEBUGGABLE |
| 232 | ? AutofillManager.FLAG_ADD_CLIENT_DEBUG |
| 233 | : AutofillManager.NO_LOGGING; |
| 234 | |
| 235 | /** @hide */ |
| 236 | public static final int DEFAULT_MAX_PARTITIONS_SIZE = 10; |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 237 | |
Svetoslav Ganov | a9379d0 | 2017-05-09 17:40:24 -0700 | [diff] [blame] | 238 | /** Which bits in an authentication id are used for the dataset id */ |
| 239 | private static final int AUTHENTICATION_ID_DATASET_ID_MASK = 0xFFFF; |
| 240 | /** How many bits in an authentication id are used for the dataset id */ |
| 241 | private static final int AUTHENTICATION_ID_DATASET_ID_SHIFT = 16; |
| 242 | /** @hide The index for an undefined data set */ |
| 243 | public static final int AUTHENTICATION_ID_DATASET_ID_UNDEFINED = 0xFFFF; |
| 244 | |
| 245 | /** |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 246 | * Used on {@link #onPendingSaveUi(int, IBinder)} to cancel the pending UI. |
| 247 | * |
| 248 | * @hide |
| 249 | */ |
| 250 | public static final int PENDING_UI_OPERATION_CANCEL = 1; |
| 251 | |
| 252 | /** |
| 253 | * Used on {@link #onPendingSaveUi(int, IBinder)} to restore the pending UI. |
| 254 | * |
| 255 | * @hide |
| 256 | */ |
| 257 | public static final int PENDING_UI_OPERATION_RESTORE = 2; |
| 258 | |
| 259 | /** |
| 260 | * Initial state of the autofill context, set when there is no session (i.e., when |
| 261 | * {@link #mSessionId} is {@link #NO_SESSION}). |
| 262 | * |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 263 | * <p>In this state, app callbacks (such as {@link #notifyViewEntered(View)}) are notified to |
| 264 | * the server. |
| 265 | * |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 266 | * @hide |
| 267 | */ |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 268 | public static final int STATE_UNKNOWN = 0; |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 269 | |
| 270 | /** |
| 271 | * State where the autofill context hasn't been {@link #commit() finished} nor |
| 272 | * {@link #cancel() canceled} yet. |
| 273 | * |
| 274 | * @hide |
| 275 | */ |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 276 | public static final int STATE_ACTIVE = 1; |
| 277 | |
| 278 | /** |
| 279 | * 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] | 280 | * service could not autofill the activity. |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 281 | * |
| 282 | * <p>In this state, most apps callback (such as {@link #notifyViewEntered(View)}) are ignored, |
| 283 | * exception {@link #requestAutofill(View)} (and {@link #requestAutofill(View, int, Rect)}). |
| 284 | * |
| 285 | * @hide |
| 286 | */ |
| 287 | public static final int STATE_FINISHED = 2; |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 288 | |
| 289 | /** |
| 290 | * State where the autofill context has been {@link #commit() finished} but the server still has |
| 291 | * a session because the Save UI hasn't been dismissed yet. |
| 292 | * |
| 293 | * @hide |
| 294 | */ |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 295 | public static final int STATE_SHOWING_SAVE_UI = 3; |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 296 | |
| 297 | /** |
Felipe Leme | 17292d1 | 2017-10-24 14:03:10 -0700 | [diff] [blame] | 298 | * State where the autofill is disabled because the service cannot autofill the activity at all. |
| 299 | * |
| 300 | * <p>In this state, every call is ignored, even {@link #requestAutofill(View)} |
| 301 | * (and {@link #requestAutofill(View, int, Rect)}). |
| 302 | * |
| 303 | * @hide |
| 304 | */ |
| 305 | public static final int STATE_DISABLED_BY_SERVICE = 4; |
| 306 | |
| 307 | /** |
Felipe Leme | 0c8ce32 | 2018-03-23 13:54:22 -0700 | [diff] [blame] | 308 | * Same as {@link #STATE_UNKNOWN}, but used on |
| 309 | * {@link AutofillManagerClient#setSessionFinished(int)} when the session was finished because |
| 310 | * the URL bar changed on client mode |
| 311 | * |
| 312 | * @hide |
| 313 | */ |
| 314 | public static final int STATE_UNKNOWN_COMPAT_MODE = 5; |
| 315 | |
Felipe Leme | d9dc954 | 2018-09-19 11:54:28 -0700 | [diff] [blame] | 316 | /** |
| 317 | * Same as {@link #STATE_UNKNOWN}, but used on |
| 318 | * {@link AutofillManagerClient#setSessionFinished(int)} when the session was finished because |
| 319 | * the service failed to fullfil a request. |
| 320 | * |
| 321 | * @hide |
| 322 | */ |
| 323 | public static final int STATE_UNKNOWN_FAILED = 6; |
Felipe Leme | 0c8ce32 | 2018-03-23 13:54:22 -0700 | [diff] [blame] | 324 | |
| 325 | /** |
Felipe Leme | bc055b0 | 2018-01-05 17:04:10 -0800 | [diff] [blame] | 326 | * Timeout in ms for calls to the field classification service. |
| 327 | * @hide |
| 328 | */ |
| 329 | public static final int FC_SERVICE_TIMEOUT = 5000; |
| 330 | |
| 331 | /** |
Felipe Leme | c0c15a3 | 2019-01-08 10:53:33 -0800 | [diff] [blame] | 332 | * Timeout for calls to system_server. |
| 333 | */ |
| 334 | private static final int SYNC_CALLS_TIMEOUT_MS = 5000; |
| 335 | |
| 336 | /** |
Felipe Leme | 2cab38c | 2019-01-11 10:39:14 -0800 | [diff] [blame] | 337 | * @hide |
| 338 | */ |
| 339 | @TestApi |
| 340 | public static final int MAX_TEMP_AUGMENTED_SERVICE_DURATION_MS = 1_000 * 60 * 2; // 2 minutes |
| 341 | |
| 342 | /** |
Felipe Leme | cc51027 | 2019-02-07 14:56:27 -0800 | [diff] [blame] | 343 | * Disables Augmented Autofill. |
| 344 | * |
| 345 | * @hide |
| 346 | */ |
| 347 | @TestApi |
| 348 | public static final int FLAG_SMART_SUGGESTION_OFF = 0x0; |
| 349 | |
| 350 | /** |
Felipe Leme | 559e21d | 2019-01-18 17:57:21 -0800 | [diff] [blame] | 351 | * Displays the Augment Autofill window using the same mechanism (such as a popup-window |
| 352 | * attached to the focused view) as the standard autofill. |
| 353 | * |
| 354 | * @hide |
| 355 | */ |
| 356 | @TestApi |
| 357 | public static final int FLAG_SMART_SUGGESTION_SYSTEM = 0x1; |
| 358 | |
Felipe Leme | 559e21d | 2019-01-18 17:57:21 -0800 | [diff] [blame] | 359 | /** @hide */ |
Felipe Leme | cc51027 | 2019-02-07 14:56:27 -0800 | [diff] [blame] | 360 | @IntDef(flag = false, value = { FLAG_SMART_SUGGESTION_OFF, FLAG_SMART_SUGGESTION_SYSTEM }) |
Felipe Leme | 559e21d | 2019-01-18 17:57:21 -0800 | [diff] [blame] | 361 | @Retention(RetentionPolicy.SOURCE) |
| 362 | public @interface SmartSuggestionMode {} |
| 363 | |
| 364 | /** |
Felipe Leme | cc51027 | 2019-02-07 14:56:27 -0800 | [diff] [blame] | 365 | * {@code DeviceConfig} property used to set which Smart Suggestion modes for Augmented Autofill |
| 366 | * are available. |
Felipe Leme | 7841d02 | 2019-02-07 13:02:18 -0800 | [diff] [blame] | 367 | * |
| 368 | * @hide |
| 369 | */ |
| 370 | @TestApi |
| 371 | public static final String DEVICE_CONFIG_AUTOFILL_SMART_SUGGESTION_SUPPORTED_MODES = |
| 372 | "smart_suggestion_supported_modes"; |
| 373 | |
Felipe Leme | a820910 | 2019-02-21 18:01:47 -0800 | [diff] [blame] | 374 | /** |
| 375 | * Sets how long (in ms) the augmented autofill service is bound while idle. |
| 376 | * |
| 377 | * <p>Use {@code 0} to keep it permanently bound. |
| 378 | * |
| 379 | * @hide |
| 380 | */ |
| 381 | public static final String DEVICE_CONFIG_AUGMENTED_SERVICE_IDLE_UNBIND_TIMEOUT = |
| 382 | "augmented_service_idle_unbind_timeout"; |
| 383 | |
| 384 | /** |
| 385 | * Sets how long (in ms) the augmented autofill service request is killed if not replied. |
| 386 | * |
| 387 | * @hide |
| 388 | */ |
| 389 | public static final String DEVICE_CONFIG_AUGMENTED_SERVICE_REQUEST_TIMEOUT = |
| 390 | "augmented_service_request_timeout"; |
| 391 | |
Felipe Leme | 80e7bf1 | 2019-02-14 10:56:52 -0800 | [diff] [blame] | 392 | /** @hide */ |
| 393 | public static final int RESULT_OK = 0; |
| 394 | /** @hide */ |
| 395 | public static final int RESULT_CODE_NOT_SERVICE = -1; |
| 396 | |
Felipe Leme | 7841d02 | 2019-02-07 13:02:18 -0800 | [diff] [blame] | 397 | /** |
Svetoslav Ganov | a9379d0 | 2017-05-09 17:40:24 -0700 | [diff] [blame] | 398 | * Makes an authentication id from a request id and a dataset id. |
| 399 | * |
| 400 | * @param requestId The request id. |
| 401 | * @param datasetId The dataset id. |
| 402 | * @return The authentication id. |
| 403 | * @hide |
| 404 | */ |
| 405 | public static int makeAuthenticationId(int requestId, int datasetId) { |
| 406 | return (requestId << AUTHENTICATION_ID_DATASET_ID_SHIFT) |
| 407 | | (datasetId & AUTHENTICATION_ID_DATASET_ID_MASK); |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Gets the request id from an authentication id. |
| 412 | * |
| 413 | * @param authRequestId The authentication id. |
| 414 | * @return The request id. |
| 415 | * @hide |
| 416 | */ |
| 417 | public static int getRequestIdFromAuthenticationId(int authRequestId) { |
| 418 | return (authRequestId >> AUTHENTICATION_ID_DATASET_ID_SHIFT); |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * Gets the dataset id from an authentication id. |
| 423 | * |
| 424 | * @param authRequestId The authentication id. |
| 425 | * @return The dataset id. |
| 426 | * @hide |
| 427 | */ |
| 428 | public static int getDatasetIdFromAuthenticationId(int authRequestId) { |
| 429 | return (authRequestId & AUTHENTICATION_ID_DATASET_ID_MASK); |
| 430 | } |
| 431 | |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 432 | private final MetricsLogger mMetricsLogger = new MetricsLogger(); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 433 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 434 | /** |
| 435 | * There is currently no session running. |
| 436 | * {@hide} |
| 437 | */ |
| 438 | public static final int NO_SESSION = Integer.MIN_VALUE; |
| 439 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 440 | private final IAutoFillManager mService; |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 441 | |
| 442 | private final Object mLock = new Object(); |
| 443 | |
| 444 | @GuardedBy("mLock") |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 445 | private IAutoFillManagerClient mServiceClient; |
| 446 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 447 | @GuardedBy("mLock") |
Koji Fukui | ccec6a6 | 2017-10-18 17:48:40 +0900 | [diff] [blame] | 448 | private Cleaner mServiceClientCleaner; |
| 449 | |
| 450 | @GuardedBy("mLock") |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 451 | private IAugmentedAutofillManagerClient mAugmentedAutofillServiceClient; |
| 452 | |
| 453 | @GuardedBy("mLock") |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 454 | private AutofillCallback mCallback; |
| 455 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 456 | private final Context mContext; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 457 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 458 | @GuardedBy("mLock") |
| 459 | private int mSessionId = NO_SESSION; |
| 460 | |
| 461 | @GuardedBy("mLock") |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 462 | private int mState = STATE_UNKNOWN; |
| 463 | |
| 464 | @GuardedBy("mLock") |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 465 | private boolean mEnabled; |
| 466 | |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 467 | /** 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] | 468 | @GuardedBy("mLock") |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 469 | @Nullable private ParcelableMap mLastAutofilledData; |
| 470 | |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 471 | /** If view tracking is enabled, contains the tracking state */ |
| 472 | @GuardedBy("mLock") |
| 473 | @Nullable private TrackedViews mTrackedViews; |
| 474 | |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 475 | /** Views that are only tracked because they are fillable and could be anchoring the UI. */ |
| 476 | @GuardedBy("mLock") |
| 477 | @Nullable private ArraySet<AutofillId> mFillableIds; |
| 478 | |
Dake Gu | b0fa378 | 2018-02-26 12:25:14 -0800 | [diff] [blame] | 479 | /** id of last requested autofill ui */ |
| 480 | @Nullable private AutofillId mIdShownFillUi; |
| 481 | |
Felipe Leme | 9a15c0f | 2018-02-14 17:26:46 -0800 | [diff] [blame] | 482 | /** |
| 483 | * Views that were already "entered" - if they're entered again when the session is not active, |
| 484 | * they're ignored |
| 485 | * */ |
| 486 | @GuardedBy("mLock") |
| 487 | @Nullable private ArraySet<AutofillId> mEnteredIds; |
| 488 | |
Felipe Leme | a7de402 | 2019-02-19 17:16:45 -0800 | [diff] [blame] | 489 | /** |
| 490 | * Views that were otherwised not important for autofill but triggered a session because the |
| 491 | * context is whitelisted for augmented autofill. |
| 492 | */ |
| 493 | @GuardedBy("mLock") |
| 494 | @Nullable private Set<AutofillId> mEnteredForAugmentedAutofillIds; |
| 495 | |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 496 | /** If set, session is commited when the field is clicked. */ |
| 497 | @GuardedBy("mLock") |
| 498 | @Nullable private AutofillId mSaveTriggerId; |
| 499 | |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 500 | /** set to true when onInvisibleForAutofill is called, used by onAuthenticationResult */ |
| 501 | @GuardedBy("mLock") |
| 502 | private boolean mOnInvisibleCalled; |
| 503 | |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 504 | /** If set, session is commited when the activity is finished; otherwise session is canceled. */ |
| 505 | @GuardedBy("mLock") |
| 506 | private boolean mSaveOnFinish; |
| 507 | |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 508 | /** If compatibility mode is enabled - this is a bridge to interact with a11y */ |
| 509 | @GuardedBy("mLock") |
| 510 | private CompatibilityBridge mCompatibilityBridge; |
| 511 | |
Felipe Leme | a4f39cd | 2019-02-19 15:08:59 -0800 | [diff] [blame] | 512 | @Nullable |
| 513 | private final AutofillOptions mOptions; |
| 514 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 515 | /** @hide */ |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 516 | public interface AutofillClient { |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 517 | /** |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 518 | * Asks the client to start an authentication flow. |
| 519 | * |
Svetoslav Ganov | a9379d0 | 2017-05-09 17:40:24 -0700 | [diff] [blame] | 520 | * @param authenticationId A unique id of the authentication operation. |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 521 | * @param intent The authentication intent. |
| 522 | * @param fillInIntent The authentication fill-in intent. |
| 523 | */ |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 524 | void autofillClientAuthenticate(int authenticationId, IntentSender intent, |
Svetoslav Ganov | a9379d0 | 2017-05-09 17:40:24 -0700 | [diff] [blame] | 525 | Intent fillInIntent); |
Svet Ganov | 17db9dc | 2017-02-21 19:54:31 -0800 | [diff] [blame] | 526 | |
| 527 | /** |
| 528 | * Tells the client this manager has state to be reset. |
| 529 | */ |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 530 | void autofillClientResetableStateAvailable(); |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 531 | |
| 532 | /** |
| 533 | * Request showing the autofill UI. |
| 534 | * |
| 535 | * @param anchor The real view the UI needs to anchor to. |
| 536 | * @param width The width of the fill UI content. |
| 537 | * @param height The height of the fill UI content. |
| 538 | * @param virtualBounds The bounds of the virtual decendant of the anchor. |
| 539 | * @param presenter The presenter that controls the fill UI window. |
| 540 | * @return Whether the UI was shown. |
| 541 | */ |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 542 | boolean autofillClientRequestShowFillUi(@NonNull View anchor, int width, int height, |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 543 | @Nullable Rect virtualBounds, IAutofillWindowPresenter presenter); |
| 544 | |
| 545 | /** |
Dake Gu | 6a20a19 | 2018-02-08 12:09:30 -0800 | [diff] [blame] | 546 | * Dispatch unhandled keyevent from Autofill window |
| 547 | * @param anchor The real view the UI needs to anchor to. |
| 548 | * @param keyEvent Unhandled KeyEvent from autofill window. |
| 549 | */ |
| 550 | void autofillClientDispatchUnhandledKey(@NonNull View anchor, @NonNull KeyEvent keyEvent); |
| 551 | |
| 552 | /** |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 553 | * Request hiding the autofill UI. |
| 554 | * |
| 555 | * @return Whether the UI was hidden. |
| 556 | */ |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 557 | boolean autofillClientRequestHideFillUi(); |
| 558 | |
| 559 | /** |
| 560 | * Gets whether the fill UI is currenlty being shown. |
| 561 | * |
| 562 | * @return Whether the fill UI is currently being shown |
| 563 | */ |
| 564 | boolean autofillClientIsFillUiShowing(); |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 565 | |
| 566 | /** |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 567 | * Checks if views are currently attached and visible. |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 568 | * |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 569 | * @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] | 570 | */ |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 571 | @NonNull boolean[] autofillClientGetViewVisibility(@NonNull AutofillId[] autofillIds); |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 572 | |
| 573 | /** |
| 574 | * Checks is the client is currently visible as understood by autofill. |
| 575 | * |
| 576 | * @return {@code true} if the client is currently visible |
| 577 | */ |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 578 | boolean autofillClientIsVisibleForAutofill(); |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 579 | |
| 580 | /** |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 581 | * Client might disable enter/exit event e.g. when activity is paused. |
| 582 | */ |
| 583 | boolean isDisablingEnterExitEventForAutofill(); |
| 584 | |
| 585 | /** |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 586 | * Finds views by traversing the hierarchies of the client. |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 587 | * |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 588 | * @param autofillIds The autofill ids of the views to find |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 589 | * |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 590 | * @return And array containing the views (empty if no views found). |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 591 | */ |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 592 | @NonNull View[] autofillClientFindViewsByAutofillIdTraversal( |
| 593 | @NonNull AutofillId[] autofillIds); |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 594 | |
| 595 | /** |
| 596 | * Finds a view by traversing the hierarchies of the client. |
| 597 | * |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 598 | * @param autofillId The autofill id of the views to find |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 599 | * |
| 600 | * @return The view, or {@code null} if not found |
| 601 | */ |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 602 | @Nullable View autofillClientFindViewByAutofillIdTraversal(@NonNull AutofillId autofillId); |
| 603 | |
| 604 | /** |
| 605 | * Finds a view by a11y id in a given client window. |
| 606 | * |
| 607 | * @param viewId The accessibility id of the views to find |
| 608 | * @param windowId The accessibility window id where to search |
| 609 | * |
| 610 | * @return The view, or {@code null} if not found |
| 611 | */ |
| 612 | @Nullable View autofillClientFindViewByAccessibilityIdTraversal(int viewId, int windowId); |
Felipe Leme | 9876a6f | 2017-05-30 15:47:28 -0700 | [diff] [blame] | 613 | |
| 614 | /** |
| 615 | * Runs the specified action on the UI thread. |
| 616 | */ |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 617 | void autofillClientRunOnUiThread(Runnable action); |
Felipe Leme | 17292d1 | 2017-10-24 14:03:10 -0700 | [diff] [blame] | 618 | |
| 619 | /** |
| 620 | * Gets the complete component name of this client. |
| 621 | */ |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 622 | ComponentName autofillClientGetComponentName(); |
| 623 | |
| 624 | /** |
| 625 | * Gets the activity token |
| 626 | */ |
| 627 | @Nullable IBinder autofillClientGetActivityToken(); |
| 628 | |
| 629 | /** |
| 630 | * @return Whether compatibility mode is enabled. |
| 631 | */ |
Felipe Leme | 42b9793 | 2018-02-20 13:04:31 -0800 | [diff] [blame] | 632 | boolean autofillClientIsCompatibilityModeEnabled(); |
| 633 | |
| 634 | /** |
| 635 | * Gets the next unique autofill ID. |
| 636 | * |
| 637 | * <p>Typically used to manage views whose content is recycled - see |
| 638 | * {@link View#setAutofillId(AutofillId)} for more info. |
| 639 | * |
| 640 | * @return An ID that is unique in the activity. |
| 641 | */ |
| 642 | @Nullable AutofillId autofillClientGetNextAutofillId(); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 643 | } |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 644 | |
| 645 | /** |
| 646 | * @hide |
| 647 | */ |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 648 | public AutofillManager(Context context, IAutoFillManager service) { |
Felipe Leme | 637e05e | 2017-12-06 12:09:37 -0800 | [diff] [blame] | 649 | mContext = Preconditions.checkNotNull(context, "context cannot be null"); |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 650 | mService = service; |
Felipe Leme | a4f39cd | 2019-02-19 15:08:59 -0800 | [diff] [blame] | 651 | mOptions = context.getAutofillOptions(); |
| 652 | |
| 653 | if (mOptions != null) { |
| 654 | sDebug = (mOptions.loggingLevel & FLAG_ADD_CLIENT_DEBUG) != 0; |
| 655 | sVerbose = (mOptions.loggingLevel & FLAG_ADD_CLIENT_VERBOSE) != 0; |
| 656 | } |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | /** |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 660 | * @hide |
| 661 | */ |
| 662 | public void enableCompatibilityMode() { |
| 663 | synchronized (mLock) { |
| 664 | // The accessibility manager is a singleton so we may need to plug |
| 665 | // different bridge based on which activity is currently focused |
| 666 | // in the current process. Since compat would be rarely used, just |
| 667 | // create and register a new instance every time. |
Felipe Leme | ce40498 | 2018-09-17 09:46:13 -0700 | [diff] [blame] | 668 | if (sDebug) { |
| 669 | Slog.d(TAG, "creating CompatibilityBridge for " + mContext); |
| 670 | } |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 671 | mCompatibilityBridge = new CompatibilityBridge(); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | /** |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 676 | * Restore state after activity lifecycle |
| 677 | * |
| 678 | * @param savedInstanceState The state to be restored |
| 679 | * |
| 680 | * {@hide} |
| 681 | */ |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 682 | public void onCreate(Bundle savedInstanceState) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 683 | if (!hasAutofillFeature()) { |
| 684 | return; |
| 685 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 686 | synchronized (mLock) { |
| 687 | mLastAutofilledData = savedInstanceState.getParcelable(LAST_AUTOFILLED_DATA_TAG); |
| 688 | |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 689 | if (isActiveLocked()) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 690 | Log.w(TAG, "New session was started before onCreate()"); |
| 691 | return; |
| 692 | } |
| 693 | |
| 694 | mSessionId = savedInstanceState.getInt(SESSION_ID_TAG, NO_SESSION); |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 695 | mState = savedInstanceState.getInt(STATE_TAG, STATE_UNKNOWN); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 696 | |
| 697 | if (mSessionId != NO_SESSION) { |
| 698 | ensureServiceClientAddedIfNeededLocked(); |
| 699 | |
Felipe Leme | 637e05e | 2017-12-06 12:09:37 -0800 | [diff] [blame] | 700 | final AutofillClient client = getClient(); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 701 | if (client != null) { |
Felipe Leme | c0c15a3 | 2019-01-08 10:53:33 -0800 | [diff] [blame] | 702 | final SyncResultReceiver receiver = new SyncResultReceiver( |
| 703 | SYNC_CALLS_TIMEOUT_MS); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 704 | try { |
Felipe Leme | d4e5228 | 2018-06-18 13:56:38 -0700 | [diff] [blame] | 705 | mService.restoreSession(mSessionId, client.autofillClientGetActivityToken(), |
| 706 | mServiceClient.asBinder(), receiver); |
| 707 | final boolean sessionWasRestored = receiver.getIntResult() == 1; |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 708 | |
| 709 | if (!sessionWasRestored) { |
| 710 | Log.w(TAG, "Session " + mSessionId + " could not be restored"); |
| 711 | mSessionId = NO_SESSION; |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 712 | mState = STATE_UNKNOWN; |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 713 | } else { |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 714 | if (sDebug) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 715 | Log.d(TAG, "session " + mSessionId + " was restored"); |
| 716 | } |
| 717 | |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 718 | client.autofillClientResetableStateAvailable(); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 719 | } |
| 720 | } catch (RemoteException e) { |
| 721 | Log.e(TAG, "Could not figure out if there was an autofill session", e); |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | /** |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 729 | * Called once the client becomes visible. |
| 730 | * |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 731 | * @see AutofillClient#autofillClientIsVisibleForAutofill() |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 732 | * |
| 733 | * {@hide} |
| 734 | */ |
| 735 | public void onVisibleForAutofill() { |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 736 | // This gets called when the client just got visible at which point the visibility |
| 737 | // of the tracked views may not have been computed (due to a pending layout, etc). |
| 738 | // While generally we have no way to know when the UI has settled. We will evaluate |
| 739 | // the tracked views state at the end of next frame to guarantee that everything |
| 740 | // that may need to be laid out is laid out. |
| 741 | Choreographer.getInstance().postCallback(Choreographer.CALLBACK_COMMIT, () -> { |
| 742 | synchronized (mLock) { |
| 743 | if (mEnabled && isActiveLocked() && mTrackedViews != null) { |
| 744 | mTrackedViews.onVisibleForAutofillChangedLocked(); |
| 745 | } |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 746 | } |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 747 | }, null); |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | /** |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 751 | * Called once the client becomes invisible. |
| 752 | * |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 753 | * @see AutofillClient#autofillClientIsVisibleForAutofill() |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 754 | * |
| 755 | * {@hide} |
| 756 | */ |
| 757 | public void onInvisibleForAutofill() { |
| 758 | synchronized (mLock) { |
| 759 | mOnInvisibleCalled = true; |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | /** |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 764 | * Save state before activity lifecycle |
| 765 | * |
| 766 | * @param outState Place to store the state |
| 767 | * |
| 768 | * {@hide} |
| 769 | */ |
| 770 | public void onSaveInstanceState(Bundle outState) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 771 | if (!hasAutofillFeature()) { |
| 772 | return; |
| 773 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 774 | synchronized (mLock) { |
| 775 | if (mSessionId != NO_SESSION) { |
| 776 | outState.putInt(SESSION_ID_TAG, mSessionId); |
| 777 | } |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 778 | if (mState != STATE_UNKNOWN) { |
| 779 | outState.putInt(STATE_TAG, mState); |
| 780 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 781 | if (mLastAutofilledData != null) { |
| 782 | outState.putParcelable(LAST_AUTOFILLED_DATA_TAG, mLastAutofilledData); |
| 783 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | |
| 787 | /** |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 788 | * @hide |
| 789 | */ |
Andreas Gampe | 3f24e69 | 2018-02-05 13:24:28 -0800 | [diff] [blame] | 790 | @GuardedBy("mLock") |
felipeal | fd3f8f6 | 2018-02-07 11:49:07 +0100 | [diff] [blame] | 791 | public boolean isCompatibilityModeEnabledLocked() { |
| 792 | return mCompatibilityBridge != null; |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | /** |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 796 | * Checks whether autofill is enabled for the current user. |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 797 | * |
| 798 | * <p>Typically used to determine whether the option to explicitly request autofill should |
| 799 | * be offered - see {@link #requestAutofill(View)}. |
| 800 | * |
| 801 | * @return whether autofill is enabled for the current user. |
| 802 | */ |
| 803 | public boolean isEnabled() { |
Felipe Leme | 3103c63 | 2017-12-18 15:05:14 -0800 | [diff] [blame] | 804 | if (!hasAutofillFeature()) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 805 | return false; |
| 806 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 807 | synchronized (mLock) { |
Felipe Leme | 3103c63 | 2017-12-18 15:05:14 -0800 | [diff] [blame] | 808 | if (isDisabledByServiceLocked()) { |
| 809 | return false; |
| 810 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 811 | ensureServiceClientAddedIfNeededLocked(); |
| 812 | return mEnabled; |
| 813 | } |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | /** |
Philip P. Moltmann | cc684ed | 2017-04-17 14:18:33 -0700 | [diff] [blame] | 817 | * Should always be called from {@link AutofillService#getFillEventHistory()}. |
| 818 | * |
| 819 | * @hide |
| 820 | */ |
| 821 | @Nullable public FillEventHistory getFillEventHistory() { |
| 822 | try { |
Felipe Leme | c0c15a3 | 2019-01-08 10:53:33 -0800 | [diff] [blame] | 823 | final SyncResultReceiver receiver = new SyncResultReceiver(SYNC_CALLS_TIMEOUT_MS); |
Felipe Leme | d4e5228 | 2018-06-18 13:56:38 -0700 | [diff] [blame] | 824 | mService.getFillEventHistory(receiver); |
Felipe Leme | c0c15a3 | 2019-01-08 10:53:33 -0800 | [diff] [blame] | 825 | return receiver.getParcelableResult(); |
Philip P. Moltmann | cc684ed | 2017-04-17 14:18:33 -0700 | [diff] [blame] | 826 | } catch (RemoteException e) { |
| 827 | e.rethrowFromSystemServer(); |
| 828 | return null; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | /** |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 833 | * Explicitly requests a new autofill context. |
| 834 | * |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 835 | * <p>Normally, the autofill context is automatically started if necessary when |
| 836 | * {@link #notifyViewEntered(View)} is called, but this method should be used in the |
| 837 | * cases where it must be explicitly started. For example, when the view offers an AUTOFILL |
| 838 | * option on its contextual overflow menu, and the user selects it. |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 839 | * |
| 840 | * @param view view requesting the new autofill context. |
| 841 | */ |
| 842 | public void requestAutofill(@NonNull View view) { |
Felipe Leme | d114642 | 2017-04-26 10:17:05 -0700 | [diff] [blame] | 843 | notifyViewEntered(view, FLAG_MANUAL_REQUEST); |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | /** |
| 847 | * Explicitly requests a new autofill context for virtual views. |
| 848 | * |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 849 | * <p>Normally, the autofill context is automatically started if necessary when |
| 850 | * {@link #notifyViewEntered(View, int, Rect)} is called, but this method should be used in the |
| 851 | * cases where it must be explicitly started. For example, when the virtual view offers an |
| 852 | * AUTOFILL option on its contextual overflow menu, and the user selects it. |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 853 | * |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 854 | * <p>The virtual view boundaries must be absolute screen coordinates. For example, if the |
| 855 | * parent view uses {@code bounds} to draw the virtual view inside its Canvas, |
| 856 | * the absolute bounds could be calculated by: |
| 857 | * |
| 858 | * <pre class="prettyprint"> |
| 859 | * int offset[] = new int[2]; |
| 860 | * getLocationOnScreen(offset); |
| 861 | * Rect absBounds = new Rect(bounds.left + offset[0], |
| 862 | * bounds.top + offset[1], |
| 863 | * bounds.right + offset[0], bounds.bottom + offset[1]); |
| 864 | * </pre> |
| 865 | * |
| 866 | * @param view the virtual view parent. |
| 867 | * @param virtualId id identifying the virtual child inside the parent view. |
| 868 | * @param absBounds absolute boundaries of the virtual view in the screen. |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 869 | */ |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 870 | public void requestAutofill(@NonNull View view, int virtualId, @NonNull Rect absBounds) { |
| 871 | notifyViewEntered(view, virtualId, absBounds, FLAG_MANUAL_REQUEST); |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 874 | /** |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 875 | * Called when a {@link View} that supports autofill is entered. |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 876 | * |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 877 | * @param view {@link View} that was entered. |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 878 | */ |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 879 | public void notifyViewEntered(@NonNull View view) { |
Felipe Leme | d114642 | 2017-04-26 10:17:05 -0700 | [diff] [blame] | 880 | notifyViewEntered(view, 0); |
| 881 | } |
| 882 | |
Andreas Gampe | 3f24e69 | 2018-02-05 13:24:28 -0800 | [diff] [blame] | 883 | @GuardedBy("mLock") |
Felipe Leme | 9a15c0f | 2018-02-14 17:26:46 -0800 | [diff] [blame] | 884 | private boolean shouldIgnoreViewEnteredLocked(@NonNull AutofillId id, int flags) { |
Felipe Leme | 3103c63 | 2017-12-18 15:05:14 -0800 | [diff] [blame] | 885 | if (isDisabledByServiceLocked()) { |
Felipe Leme | 17292d1 | 2017-10-24 14:03:10 -0700 | [diff] [blame] | 886 | if (sVerbose) { |
Felipe Leme | 9a15c0f | 2018-02-14 17:26:46 -0800 | [diff] [blame] | 887 | Log.v(TAG, "ignoring notifyViewEntered(flags=" + flags + ", view=" + id |
| 888 | + ") on state " + getStateAsStringLocked() + " because disabled by svc"); |
Felipe Leme | 17292d1 | 2017-10-24 14:03:10 -0700 | [diff] [blame] | 889 | } |
| 890 | return true; |
| 891 | } |
Felipe Leme | 9a15c0f | 2018-02-14 17:26:46 -0800 | [diff] [blame] | 892 | if (isFinishedLocked()) { |
| 893 | // Session already finished: ignore if automatic request and view already entered |
| 894 | if ((flags & FLAG_MANUAL_REQUEST) == 0 && mEnteredIds != null |
| 895 | && mEnteredIds.contains(id)) { |
| 896 | if (sVerbose) { |
| 897 | Log.v(TAG, "ignoring notifyViewEntered(flags=" + flags + ", view=" + id |
| 898 | + ") on state " + getStateAsStringLocked() |
| 899 | + " because view was already entered: " + mEnteredIds); |
| 900 | } |
| 901 | return true; |
| 902 | } |
| 903 | } |
Felipe Leme | 17292d1 | 2017-10-24 14:03:10 -0700 | [diff] [blame] | 904 | return false; |
| 905 | } |
| 906 | |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 907 | private boolean isClientVisibleForAutofillLocked() { |
| 908 | final AutofillClient client = getClient(); |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 909 | return client != null && client.autofillClientIsVisibleForAutofill(); |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | private boolean isClientDisablingEnterExitEvent() { |
| 913 | final AutofillClient client = getClient(); |
| 914 | return client != null && client.isDisablingEnterExitEventForAutofill(); |
| 915 | } |
| 916 | |
Felipe Leme | d114642 | 2017-04-26 10:17:05 -0700 | [diff] [blame] | 917 | private void notifyViewEntered(@NonNull View view, int flags) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 918 | if (!hasAutofillFeature()) { |
| 919 | return; |
| 920 | } |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 921 | AutofillCallback callback; |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 922 | synchronized (mLock) { |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 923 | callback = notifyViewEnteredLocked(view, flags); |
| 924 | } |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 925 | |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 926 | if (callback != null) { |
| 927 | mCallback.onAutofillEvent(view, AutofillCallback.EVENT_INPUT_UNAVAILABLE); |
| 928 | } |
| 929 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 930 | |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 931 | /** Returns AutofillCallback if need fire EVENT_INPUT_UNAVAILABLE */ |
Andreas Gampe | 3f24e69 | 2018-02-05 13:24:28 -0800 | [diff] [blame] | 932 | @GuardedBy("mLock") |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 933 | private AutofillCallback notifyViewEnteredLocked(@NonNull View view, int flags) { |
Felipe Leme | 42b9793 | 2018-02-20 13:04:31 -0800 | [diff] [blame] | 934 | final AutofillId id = view.getAutofillId(); |
Felipe Leme | 9a15c0f | 2018-02-14 17:26:46 -0800 | [diff] [blame] | 935 | if (shouldIgnoreViewEnteredLocked(id, flags)) return null; |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 936 | |
| 937 | AutofillCallback callback = null; |
| 938 | |
| 939 | ensureServiceClientAddedIfNeededLocked(); |
| 940 | |
| 941 | if (!mEnabled) { |
Felipe Leme | f3b844b | 2018-10-02 13:50:50 -0700 | [diff] [blame] | 942 | if (sVerbose) { |
| 943 | Log.v(TAG, "ignoring notifyViewEntered(" + id + "): disabled"); |
| 944 | } |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 945 | if (mCallback != null) { |
| 946 | callback = mCallback; |
| 947 | } |
| 948 | } else { |
| 949 | // don't notify entered when Activity is already in background |
| 950 | if (!isClientDisablingEnterExitEvent()) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 951 | final AutofillValue value = view.getAutofillValue(); |
| 952 | |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 953 | if (!isActiveLocked()) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 954 | // Starts new session. |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 955 | startSessionLocked(id, null, value, flags); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 956 | } else { |
| 957 | // Update focus on existing session. |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 958 | updateSessionLocked(id, null, value, ACTION_VIEW_ENTERED, flags); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 959 | } |
Felipe Leme | 9a15c0f | 2018-02-14 17:26:46 -0800 | [diff] [blame] | 960 | addEnteredIdLocked(id); |
Felipe Leme | 24aae15 | 2017-03-15 12:33:01 -0700 | [diff] [blame] | 961 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 962 | } |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 963 | return callback; |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | /** |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 967 | * Called when a {@link View} that supports autofill is exited. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 968 | * |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 969 | * @param view {@link View} that was exited. |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 970 | */ |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 971 | public void notifyViewExited(@NonNull View view) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 972 | if (!hasAutofillFeature()) { |
| 973 | return; |
| 974 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 975 | synchronized (mLock) { |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 976 | notifyViewExitedLocked(view); |
| 977 | } |
| 978 | } |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 979 | |
Andreas Gampe | 3f24e69 | 2018-02-05 13:24:28 -0800 | [diff] [blame] | 980 | @GuardedBy("mLock") |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 981 | void notifyViewExitedLocked(@NonNull View view) { |
| 982 | ensureServiceClientAddedIfNeededLocked(); |
| 983 | |
| 984 | if (mEnabled && isActiveLocked()) { |
| 985 | // dont notify exited when Activity is already in background |
| 986 | if (!isClientDisablingEnterExitEvent()) { |
Felipe Leme | 42b9793 | 2018-02-20 13:04:31 -0800 | [diff] [blame] | 987 | final AutofillId id = view.getAutofillId(); |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 988 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 989 | // Update focus on existing session. |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 990 | updateSessionLocked(id, null, null, ACTION_VIEW_EXITED, 0); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 991 | } |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 992 | } |
| 993 | } |
| 994 | |
| 995 | /** |
Svetoslav Ganov | 4db89ba | 2017-06-28 16:50:37 -0700 | [diff] [blame] | 996 | * Called when a {@link View view's} visibility changed. |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 997 | * |
| 998 | * @param view {@link View} that was exited. |
| 999 | * @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] | 1000 | */ |
Svetoslav Ganov | 4db89ba | 2017-06-28 16:50:37 -0700 | [diff] [blame] | 1001 | public void notifyViewVisibilityChanged(@NonNull View view, boolean isVisible) { |
| 1002 | notifyViewVisibilityChangedInternal(view, 0, isVisible, false); |
| 1003 | } |
| 1004 | |
| 1005 | /** |
| 1006 | * Called when a virtual view's visibility changed. |
| 1007 | * |
| 1008 | * @param view {@link View} that was exited. |
| 1009 | * @param virtualId id identifying the virtual child inside the parent view. |
| 1010 | * @param isVisible visible if the view is visible in the view hierarchy. |
| 1011 | */ |
| 1012 | public void notifyViewVisibilityChanged(@NonNull View view, int virtualId, boolean isVisible) { |
| 1013 | notifyViewVisibilityChangedInternal(view, virtualId, isVisible, true); |
| 1014 | } |
| 1015 | |
| 1016 | /** |
| 1017 | * Called when a view/virtual view's visibility changed. |
| 1018 | * |
| 1019 | * @param view {@link View} that was exited. |
| 1020 | * @param virtualId id identifying the virtual child inside the parent view. |
| 1021 | * @param isVisible visible if the view is visible in the view hierarchy. |
| 1022 | * @param virtual Whether the view is virtual. |
| 1023 | */ |
| 1024 | private void notifyViewVisibilityChangedInternal(@NonNull View view, int virtualId, |
| 1025 | boolean isVisible, boolean virtual) { |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1026 | synchronized (mLock) { |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 1027 | if (mEnabled && isActiveLocked()) { |
Felipe Leme | 305fd40 | 2018-09-11 18:20:42 +0000 | [diff] [blame] | 1028 | final AutofillId id = virtual ? getAutofillId(view, virtualId) |
Svetoslav Ganov | 4db89ba | 2017-06-28 16:50:37 -0700 | [diff] [blame] | 1029 | : view.getAutofillId(); |
Felipe Leme | 42b9793 | 2018-02-20 13:04:31 -0800 | [diff] [blame] | 1030 | if (sVerbose) Log.v(TAG, "visibility changed for " + id + ": " + isVisible); |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 1031 | if (!isVisible && mFillableIds != null) { |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 1032 | if (mFillableIds.contains(id)) { |
| 1033 | if (sDebug) Log.d(TAG, "Hidding UI when view " + id + " became invisible"); |
| 1034 | requestHideFillUi(id, view); |
| 1035 | } |
| 1036 | } |
| 1037 | if (mTrackedViews != null) { |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 1038 | mTrackedViews.notifyViewVisibilityChangedLocked(id, isVisible); |
Felipe Leme | 42b9793 | 2018-02-20 13:04:31 -0800 | [diff] [blame] | 1039 | } else if (sVerbose) { |
| 1040 | Log.v(TAG, "Ignoring visibility change on " + id + ": no tracked views"); |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 1041 | } |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | /** |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 1047 | * Called when a virtual view that supports autofill is entered. |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 1048 | * |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 1049 | * <p>The virtual view boundaries must be absolute screen coordinates. For example, if the |
| 1050 | * parent, non-virtual view uses {@code bounds} to draw the virtual view inside its Canvas, |
| 1051 | * the absolute bounds could be calculated by: |
| 1052 | * |
| 1053 | * <pre class="prettyprint"> |
| 1054 | * int offset[] = new int[2]; |
| 1055 | * getLocationOnScreen(offset); |
| 1056 | * Rect absBounds = new Rect(bounds.left + offset[0], |
| 1057 | * bounds.top + offset[1], |
| 1058 | * bounds.right + offset[0], bounds.bottom + offset[1]); |
| 1059 | * </pre> |
| 1060 | * |
| 1061 | * @param view the virtual view parent. |
| 1062 | * @param virtualId id identifying the virtual child inside the parent view. |
| 1063 | * @param absBounds absolute boundaries of the virtual view in the screen. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 1064 | */ |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 1065 | public void notifyViewEntered(@NonNull View view, int virtualId, @NonNull Rect absBounds) { |
| 1066 | notifyViewEntered(view, virtualId, absBounds, 0); |
Felipe Leme | d114642 | 2017-04-26 10:17:05 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 1069 | private void notifyViewEntered(View view, int virtualId, Rect bounds, int flags) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 1070 | if (!hasAutofillFeature()) { |
| 1071 | return; |
| 1072 | } |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 1073 | AutofillCallback callback; |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1074 | synchronized (mLock) { |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 1075 | callback = notifyViewEnteredLocked(view, virtualId, bounds, flags); |
| 1076 | } |
Felipe Leme | 17292d1 | 2017-10-24 14:03:10 -0700 | [diff] [blame] | 1077 | |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 1078 | if (callback != null) { |
| 1079 | callback.onAutofillEvent(view, virtualId, |
| 1080 | AutofillCallback.EVENT_INPUT_UNAVAILABLE); |
| 1081 | } |
| 1082 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1083 | |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 1084 | /** Returns AutofillCallback if need fire EVENT_INPUT_UNAVAILABLE */ |
Andreas Gampe | 3f24e69 | 2018-02-05 13:24:28 -0800 | [diff] [blame] | 1085 | @GuardedBy("mLock") |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 1086 | private AutofillCallback notifyViewEnteredLocked(View view, int virtualId, Rect bounds, |
| 1087 | int flags) { |
Felipe Leme | 305fd40 | 2018-09-11 18:20:42 +0000 | [diff] [blame] | 1088 | final AutofillId id = getAutofillId(view, virtualId); |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 1089 | AutofillCallback callback = null; |
Felipe Leme | 9a15c0f | 2018-02-14 17:26:46 -0800 | [diff] [blame] | 1090 | if (shouldIgnoreViewEnteredLocked(id, flags)) return callback; |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 1091 | |
| 1092 | ensureServiceClientAddedIfNeededLocked(); |
| 1093 | |
| 1094 | if (!mEnabled) { |
Felipe Leme | f3b844b | 2018-10-02 13:50:50 -0700 | [diff] [blame] | 1095 | if (sVerbose) { |
| 1096 | Log.v(TAG, "ignoring notifyViewEntered(" + id + "): disabled"); |
| 1097 | } |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 1098 | if (mCallback != null) { |
| 1099 | callback = mCallback; |
| 1100 | } |
| 1101 | } else { |
| 1102 | // don't notify entered when Activity is already in background |
| 1103 | if (!isClientDisablingEnterExitEvent()) { |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 1104 | if (!isActiveLocked()) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1105 | // Starts new session. |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1106 | startSessionLocked(id, bounds, null, flags); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1107 | } else { |
| 1108 | // Update focus on existing session. |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 1109 | updateSessionLocked(id, bounds, null, ACTION_VIEW_ENTERED, flags); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1110 | } |
Felipe Leme | 9a15c0f | 2018-02-14 17:26:46 -0800 | [diff] [blame] | 1111 | addEnteredIdLocked(id); |
Felipe Leme | 24aae15 | 2017-03-15 12:33:01 -0700 | [diff] [blame] | 1112 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 1113 | } |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 1114 | return callback; |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 1115 | } |
| 1116 | |
Felipe Leme | 9a15c0f | 2018-02-14 17:26:46 -0800 | [diff] [blame] | 1117 | @GuardedBy("mLock") |
| 1118 | private void addEnteredIdLocked(@NonNull AutofillId id) { |
| 1119 | if (mEnteredIds == null) { |
| 1120 | mEnteredIds = new ArraySet<>(1); |
| 1121 | } |
| 1122 | mEnteredIds.add(id); |
| 1123 | } |
| 1124 | |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 1125 | /** |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 1126 | * Called when a virtual view that supports autofill is exited. |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 1127 | * |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 1128 | * @param view the virtual view parent. |
| 1129 | * @param virtualId id identifying the virtual child inside the parent view. |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 1130 | */ |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 1131 | public void notifyViewExited(@NonNull View view, int virtualId) { |
Felipe Leme | 54cc683 | 2018-03-06 12:54:31 -0800 | [diff] [blame] | 1132 | if (sVerbose) Log.v(TAG, "notifyViewExited(" + view.getAutofillId() + ", " + virtualId); |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 1133 | if (!hasAutofillFeature()) { |
| 1134 | return; |
| 1135 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1136 | synchronized (mLock) { |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 1137 | notifyViewExitedLocked(view, virtualId); |
| 1138 | } |
| 1139 | } |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 1140 | |
Andreas Gampe | 3f24e69 | 2018-02-05 13:24:28 -0800 | [diff] [blame] | 1141 | @GuardedBy("mLock") |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 1142 | private void notifyViewExitedLocked(@NonNull View view, int virtualId) { |
| 1143 | ensureServiceClientAddedIfNeededLocked(); |
| 1144 | |
| 1145 | if (mEnabled && isActiveLocked()) { |
| 1146 | // don't notify exited when Activity is already in background |
| 1147 | if (!isClientDisablingEnterExitEvent()) { |
Felipe Leme | 305fd40 | 2018-09-11 18:20:42 +0000 | [diff] [blame] | 1148 | final AutofillId id = getAutofillId(view, virtualId); |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 1149 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1150 | // Update focus on existing session. |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 1151 | updateSessionLocked(id, null, null, ACTION_VIEW_EXITED, 0); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1152 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1153 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | /** |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 1157 | * Called to indicate the value of an autofillable {@link View} changed. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 1158 | * |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 1159 | * @param view view whose value changed. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 1160 | */ |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 1161 | public void notifyValueChanged(View view) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 1162 | if (!hasAutofillFeature()) { |
| 1163 | return; |
| 1164 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 1165 | AutofillId id = null; |
| 1166 | boolean valueWasRead = false; |
| 1167 | AutofillValue value = null; |
| 1168 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1169 | synchronized (mLock) { |
| 1170 | // If the session is gone some fields might still be highlighted, hence we have to |
| 1171 | // remove the isAutofilled property even if no sessions are active. |
| 1172 | if (mLastAutofilledData == null) { |
| 1173 | view.setAutofilled(false); |
| 1174 | } else { |
Felipe Leme | 42b9793 | 2018-02-20 13:04:31 -0800 | [diff] [blame] | 1175 | id = view.getAutofillId(); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1176 | if (mLastAutofilledData.containsKey(id)) { |
| 1177 | value = view.getAutofillValue(); |
| 1178 | valueWasRead = true; |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 1179 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1180 | if (Objects.equals(mLastAutofilledData.get(id), value)) { |
| 1181 | view.setAutofilled(true); |
| 1182 | } else { |
| 1183 | view.setAutofilled(false); |
| 1184 | mLastAutofilledData.remove(id); |
| 1185 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 1186 | } else { |
| 1187 | view.setAutofilled(false); |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 1188 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 1189 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 1190 | |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 1191 | if (!mEnabled || !isActiveLocked()) { |
Felipe Leme | 9a15c0f | 2018-02-14 17:26:46 -0800 | [diff] [blame] | 1192 | if (sVerbose) { |
| 1193 | Log.v(TAG, "notifyValueChanged(" + view.getAutofillId() |
| 1194 | + "): ignoring on state " + getStateAsStringLocked()); |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 1195 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1196 | return; |
| 1197 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 1198 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1199 | if (id == null) { |
Felipe Leme | 42b9793 | 2018-02-20 13:04:31 -0800 | [diff] [blame] | 1200 | id = view.getAutofillId(); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1201 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 1202 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1203 | if (!valueWasRead) { |
| 1204 | value = view.getAutofillValue(); |
| 1205 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 1206 | |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 1207 | updateSessionLocked(id, null, value, ACTION_VALUE_CHANGED, 0); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1208 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 1209 | } |
| 1210 | |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 1211 | /** |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 1212 | * Called to indicate the value of an autofillable virtual view has changed. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 1213 | * |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 1214 | * @param view the virtual view parent. |
| 1215 | * @param virtualId id identifying the virtual child inside the parent view. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 1216 | * @param value new value of the child. |
| 1217 | */ |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 1218 | public void notifyValueChanged(View view, int virtualId, AutofillValue value) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 1219 | if (!hasAutofillFeature()) { |
| 1220 | return; |
| 1221 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1222 | synchronized (mLock) { |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 1223 | if (!mEnabled || !isActiveLocked()) { |
Felipe Leme | 9a15c0f | 2018-02-14 17:26:46 -0800 | [diff] [blame] | 1224 | if (sVerbose) { |
| 1225 | Log.v(TAG, "notifyValueChanged(" + view.getAutofillId() + ":" + virtualId |
| 1226 | + "): ignoring on state " + getStateAsStringLocked()); |
| 1227 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1228 | return; |
| 1229 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 1230 | |
Felipe Leme | 305fd40 | 2018-09-11 18:20:42 +0000 | [diff] [blame] | 1231 | final AutofillId id = getAutofillId(view, virtualId); |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 1232 | updateSessionLocked(id, null, value, ACTION_VALUE_CHANGED, 0); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1233 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 1234 | } |
| 1235 | |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 1236 | /** |
Felipe Leme | 67e6209 | 2018-02-15 14:47:31 -0800 | [diff] [blame] | 1237 | * Called to indicate a {@link View} is clicked. |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 1238 | * |
Felipe Leme | 67e6209 | 2018-02-15 14:47:31 -0800 | [diff] [blame] | 1239 | * @param view view that has been clicked. |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 1240 | */ |
Felipe Leme | 67e6209 | 2018-02-15 14:47:31 -0800 | [diff] [blame] | 1241 | public void notifyViewClicked(@NonNull View view) { |
| 1242 | notifyViewClicked(view.getAutofillId()); |
| 1243 | } |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 1244 | |
Felipe Leme | 67e6209 | 2018-02-15 14:47:31 -0800 | [diff] [blame] | 1245 | /** |
| 1246 | * Called to indicate a virtual view has been clicked. |
| 1247 | * |
| 1248 | * @param view the virtual view parent. |
| 1249 | * @param virtualId id identifying the virtual child inside the parent view. |
| 1250 | */ |
| 1251 | public void notifyViewClicked(@NonNull View view, int virtualId) { |
Felipe Leme | 305fd40 | 2018-09-11 18:20:42 +0000 | [diff] [blame] | 1252 | notifyViewClicked(getAutofillId(view, virtualId)); |
Felipe Leme | 67e6209 | 2018-02-15 14:47:31 -0800 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | private void notifyViewClicked(AutofillId id) { |
| 1256 | if (!hasAutofillFeature()) { |
| 1257 | return; |
| 1258 | } |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 1259 | if (sVerbose) Log.v(TAG, "notifyViewClicked(): id=" + id + ", trigger=" + mSaveTriggerId); |
| 1260 | |
| 1261 | synchronized (mLock) { |
Felipe Leme | 67e6209 | 2018-02-15 14:47:31 -0800 | [diff] [blame] | 1262 | if (!mEnabled || !isActiveLocked()) { |
| 1263 | return; |
| 1264 | } |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 1265 | if (mSaveTriggerId != null && mSaveTriggerId.equals(id)) { |
| 1266 | if (sDebug) Log.d(TAG, "triggering commit by click of " + id); |
| 1267 | commitLocked(); |
Felipe Leme | 1116652 | 2018-05-07 10:18:47 -0700 | [diff] [blame] | 1268 | mMetricsLogger.write(newLog(MetricsEvent.AUTOFILL_SAVE_EXPLICITLY_TRIGGERED)); |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 1269 | } |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | /** |
| 1274 | * Called by {@link android.app.Activity} to commit or cancel the session on finish. |
| 1275 | * |
| 1276 | * @hide |
| 1277 | */ |
Felipe Leme | 5b32ebe | 2018-02-15 12:52:19 -0800 | [diff] [blame] | 1278 | public void onActivityFinishing() { |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 1279 | if (!hasAutofillFeature()) { |
| 1280 | return; |
| 1281 | } |
| 1282 | synchronized (mLock) { |
| 1283 | if (mSaveOnFinish) { |
Felipe Leme | 5b32ebe | 2018-02-15 12:52:19 -0800 | [diff] [blame] | 1284 | if (sDebug) Log.d(TAG, "onActivityFinishing(): calling commitLocked()"); |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 1285 | commitLocked(); |
| 1286 | } else { |
Felipe Leme | 5b32ebe | 2018-02-15 12:52:19 -0800 | [diff] [blame] | 1287 | if (sDebug) Log.d(TAG, "onActivityFinishing(): calling cancelLocked()"); |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 1288 | cancelLocked(); |
| 1289 | } |
| 1290 | } |
| 1291 | } |
| 1292 | |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 1293 | /** |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 1294 | * Called to indicate the current autofill context should be commited. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 1295 | * |
Felipe Leme | afdfe76 | 2017-07-24 11:25:56 -0700 | [diff] [blame] | 1296 | * <p>This method is typically called by {@link View Views} that manage virtual views; for |
| 1297 | * example, when the view is rendering an {@code HTML} page with a form and virtual views |
| 1298 | * that represent the HTML elements, it should call this method after the form is submitted and |
| 1299 | * another page is rendered. |
| 1300 | * |
| 1301 | * <p><b>Note:</b> This method does not need to be called on regular application lifecycle |
| 1302 | * methods such as {@link android.app.Activity#finish()}. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 1303 | */ |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 1304 | public void commit() { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 1305 | if (!hasAutofillFeature()) { |
| 1306 | return; |
| 1307 | } |
Felipe Leme | 5b32ebe | 2018-02-15 12:52:19 -0800 | [diff] [blame] | 1308 | if (sVerbose) Log.v(TAG, "commit() called by app"); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1309 | synchronized (mLock) { |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 1310 | commitLocked(); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1311 | } |
Felipe Leme | 0200d9e | 2017-01-24 15:10:26 -0800 | [diff] [blame] | 1312 | } |
| 1313 | |
Andreas Gampe | 3f24e69 | 2018-02-05 13:24:28 -0800 | [diff] [blame] | 1314 | @GuardedBy("mLock") |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 1315 | private void commitLocked() { |
| 1316 | if (!mEnabled && !isActiveLocked()) { |
| 1317 | return; |
| 1318 | } |
| 1319 | finishSessionLocked(); |
| 1320 | } |
| 1321 | |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 1322 | /** |
| 1323 | * Called to indicate the current autofill context should be cancelled. |
| 1324 | * |
Felipe Leme | afdfe76 | 2017-07-24 11:25:56 -0700 | [diff] [blame] | 1325 | * <p>This method is typically called by {@link View Views} that manage virtual views; for |
| 1326 | * example, when the view is rendering an {@code HTML} page with a form and virtual views |
| 1327 | * that represent the HTML elements, it should call this method if the user does not post the |
| 1328 | * form but moves to another form in this page. |
| 1329 | * |
| 1330 | * <p><b>Note:</b> This method does not need to be called on regular application lifecycle |
| 1331 | * methods such as {@link android.app.Activity#finish()}. |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 1332 | */ |
| 1333 | public void cancel() { |
Felipe Leme | 601d220 | 2017-11-17 08:21:23 -0800 | [diff] [blame] | 1334 | if (sVerbose) Log.v(TAG, "cancel() called by app"); |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 1335 | if (!hasAutofillFeature()) { |
| 1336 | return; |
| 1337 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1338 | synchronized (mLock) { |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 1339 | cancelLocked(); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1340 | } |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
Andreas Gampe | 3f24e69 | 2018-02-05 13:24:28 -0800 | [diff] [blame] | 1343 | @GuardedBy("mLock") |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 1344 | private void cancelLocked() { |
| 1345 | if (!mEnabled && !isActiveLocked()) { |
| 1346 | return; |
| 1347 | } |
| 1348 | cancelSessionLocked(); |
| 1349 | } |
| 1350 | |
Svet Ganov | f965b87 | 2017-04-28 16:34:02 -0700 | [diff] [blame] | 1351 | /** @hide */ |
| 1352 | public void disableOwnedAutofillServices() { |
| 1353 | disableAutofillServices(); |
| 1354 | } |
| 1355 | |
Svet Ganov | f20a037 | 2017-04-10 17:08:05 -0700 | [diff] [blame] | 1356 | /** |
| 1357 | * If the app calling this API has enabled autofill services they |
| 1358 | * will be disabled. |
| 1359 | */ |
Svet Ganov | f965b87 | 2017-04-28 16:34:02 -0700 | [diff] [blame] | 1360 | public void disableAutofillServices() { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 1361 | if (!hasAutofillFeature()) { |
| 1362 | return; |
| 1363 | } |
Svet Ganov | f20a037 | 2017-04-10 17:08:05 -0700 | [diff] [blame] | 1364 | try { |
| 1365 | mService.disableOwnedAutofillServices(mContext.getUserId()); |
| 1366 | } catch (RemoteException e) { |
| 1367 | throw e.rethrowFromSystemServer(); |
| 1368 | } |
| 1369 | } |
| 1370 | |
Felipe Leme | db04118 | 2017-04-21 17:33:38 -0700 | [diff] [blame] | 1371 | /** |
| 1372 | * Returns {@code true} if the calling application provides a {@link AutofillService} that is |
| 1373 | * enabled for the current user, or {@code false} otherwise. |
| 1374 | */ |
| 1375 | public boolean hasEnabledAutofillServices() { |
| 1376 | if (mService == null) return false; |
| 1377 | |
Felipe Leme | c0c15a3 | 2019-01-08 10:53:33 -0800 | [diff] [blame] | 1378 | final SyncResultReceiver receiver = new SyncResultReceiver(SYNC_CALLS_TIMEOUT_MS); |
Felipe Leme | db04118 | 2017-04-21 17:33:38 -0700 | [diff] [blame] | 1379 | try { |
Felipe Leme | d4e5228 | 2018-06-18 13:56:38 -0700 | [diff] [blame] | 1380 | mService.isServiceEnabled(mContext.getUserId(), mContext.getPackageName(), receiver); |
| 1381 | return receiver.getIntResult() == 1; |
Felipe Leme | db04118 | 2017-04-21 17:33:38 -0700 | [diff] [blame] | 1382 | } catch (RemoteException e) { |
| 1383 | throw e.rethrowFromSystemServer(); |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | /** |
Felipe Leme | 23c75ff | 2017-12-14 13:27:44 -0800 | [diff] [blame] | 1388 | * Returns the component name of the {@link AutofillService} that is enabled for the current |
| 1389 | * user. |
| 1390 | */ |
| 1391 | @Nullable |
| 1392 | public ComponentName getAutofillServiceComponentName() { |
| 1393 | if (mService == null) return null; |
| 1394 | |
Felipe Leme | c0c15a3 | 2019-01-08 10:53:33 -0800 | [diff] [blame] | 1395 | final SyncResultReceiver receiver = new SyncResultReceiver(SYNC_CALLS_TIMEOUT_MS); |
Felipe Leme | 23c75ff | 2017-12-14 13:27:44 -0800 | [diff] [blame] | 1396 | try { |
Felipe Leme | d4e5228 | 2018-06-18 13:56:38 -0700 | [diff] [blame] | 1397 | mService.getAutofillServiceComponentName(receiver); |
Felipe Leme | c0c15a3 | 2019-01-08 10:53:33 -0800 | [diff] [blame] | 1398 | return receiver.getParcelableResult(); |
Felipe Leme | 23c75ff | 2017-12-14 13:27:44 -0800 | [diff] [blame] | 1399 | } catch (RemoteException e) { |
| 1400 | throw e.rethrowFromSystemServer(); |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | /** |
Felipe Leme | f0baef74 | 2018-01-26 14:39:39 -0800 | [diff] [blame] | 1405 | * Gets the id of the {@link UserData} used for |
| 1406 | * <a href="AutofillService.html#FieldClassification">field classification</a>. |
| 1407 | * |
| 1408 | * <p>This method is useful when the service must check the status of the {@link UserData} in |
| 1409 | * the device without fetching the whole object. |
| 1410 | * |
| 1411 | * <p><b>Note:</b> This method should only be called by an app providing an autofill service, |
| 1412 | * and it's ignored if the caller currently doesn't have an enabled autofill service for |
| 1413 | * the user. |
| 1414 | * |
| 1415 | * @return id of the {@link UserData} previously set by {@link #setUserData(UserData)} |
| 1416 | * or {@code null} if it was reset or if the caller currently does not have an enabled autofill |
| 1417 | * service for the user. |
| 1418 | */ |
| 1419 | @Nullable public String getUserDataId() { |
| 1420 | try { |
Felipe Leme | c0c15a3 | 2019-01-08 10:53:33 -0800 | [diff] [blame] | 1421 | final SyncResultReceiver receiver = new SyncResultReceiver(SYNC_CALLS_TIMEOUT_MS); |
Felipe Leme | d4e5228 | 2018-06-18 13:56:38 -0700 | [diff] [blame] | 1422 | mService.getUserDataId(receiver); |
Felipe Leme | c0c15a3 | 2019-01-08 10:53:33 -0800 | [diff] [blame] | 1423 | return receiver.getStringResult(); |
Felipe Leme | f0baef74 | 2018-01-26 14:39:39 -0800 | [diff] [blame] | 1424 | } catch (RemoteException e) { |
| 1425 | e.rethrowFromSystemServer(); |
| 1426 | return null; |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | /** |
Felipe Leme | 78172e7 | 2017-12-08 17:01:15 -0800 | [diff] [blame] | 1431 | * Gets the user data used for |
| 1432 | * <a href="AutofillService.html#FieldClassification">field classification</a>. |
Felipe Leme | 452886a | 2017-11-27 13:09:13 -0800 | [diff] [blame] | 1433 | * |
Felipe Leme | 27f4573 | 2017-12-22 09:05:22 -0800 | [diff] [blame] | 1434 | * <p><b>Note:</b> This method should only be called by an app providing an autofill service, |
| 1435 | * and it's ignored if the caller currently doesn't have an enabled autofill service for |
| 1436 | * the user. |
Felipe Leme | 452886a | 2017-11-27 13:09:13 -0800 | [diff] [blame] | 1437 | * |
Felipe Leme | 452886a | 2017-11-27 13:09:13 -0800 | [diff] [blame] | 1438 | * @return value previously set by {@link #setUserData(UserData)} or {@code null} if it was |
| 1439 | * 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] | 1440 | */ |
Felipe Leme | 452886a | 2017-11-27 13:09:13 -0800 | [diff] [blame] | 1441 | @Nullable public UserData getUserData() { |
| 1442 | try { |
Felipe Leme | c0c15a3 | 2019-01-08 10:53:33 -0800 | [diff] [blame] | 1443 | final SyncResultReceiver receiver = new SyncResultReceiver(SYNC_CALLS_TIMEOUT_MS); |
Felipe Leme | d4e5228 | 2018-06-18 13:56:38 -0700 | [diff] [blame] | 1444 | mService.getUserData(receiver); |
Felipe Leme | c0c15a3 | 2019-01-08 10:53:33 -0800 | [diff] [blame] | 1445 | return receiver.getParcelableResult(); |
Felipe Leme | 452886a | 2017-11-27 13:09:13 -0800 | [diff] [blame] | 1446 | } catch (RemoteException e) { |
| 1447 | e.rethrowFromSystemServer(); |
| 1448 | return null; |
| 1449 | } |
| 1450 | } |
| 1451 | |
| 1452 | /** |
Felipe Leme | f0baef74 | 2018-01-26 14:39:39 -0800 | [diff] [blame] | 1453 | * Sets the {@link UserData} used for |
Felipe Leme | 78172e7 | 2017-12-08 17:01:15 -0800 | [diff] [blame] | 1454 | * <a href="AutofillService.html#FieldClassification">field classification</a> |
Felipe Leme | 452886a | 2017-11-27 13:09:13 -0800 | [diff] [blame] | 1455 | * |
| 1456 | * <p><b>Note:</b> This method should only be called by an app providing an autofill service, |
| 1457 | * and it's ignored if the caller currently doesn't have an enabled autofill service for |
| 1458 | * the user. |
Felipe Leme | 452886a | 2017-11-27 13:09:13 -0800 | [diff] [blame] | 1459 | */ |
Felipe Leme | 452886a | 2017-11-27 13:09:13 -0800 | [diff] [blame] | 1460 | public void setUserData(@Nullable UserData userData) { |
| 1461 | try { |
| 1462 | mService.setUserData(userData); |
| 1463 | } catch (RemoteException e) { |
| 1464 | e.rethrowFromSystemServer(); |
| 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | /** |
Felipe Leme | 78172e7 | 2017-12-08 17:01:15 -0800 | [diff] [blame] | 1469 | * Checks if <a href="AutofillService.html#FieldClassification">field classification</a> is |
| 1470 | * enabled. |
| 1471 | * |
| 1472 | * <p>As field classification is an expensive operation, it could be disabled, either |
| 1473 | * temporarily (for example, because the service exceeded a rate-limit threshold) or |
| 1474 | * permanently (for example, because the device is a low-level device). |
| 1475 | * |
| 1476 | * <p><b>Note:</b> This method should only be called by an app providing an autofill service, |
| 1477 | * and it's ignored if the caller currently doesn't have an enabled autofill service for |
| 1478 | * the user. |
Felipe Leme | 329d040 | 2017-12-06 09:22:43 -0800 | [diff] [blame] | 1479 | */ |
Felipe Leme | 329d040 | 2017-12-06 09:22:43 -0800 | [diff] [blame] | 1480 | public boolean isFieldClassificationEnabled() { |
Felipe Leme | c0c15a3 | 2019-01-08 10:53:33 -0800 | [diff] [blame] | 1481 | final SyncResultReceiver receiver = new SyncResultReceiver(SYNC_CALLS_TIMEOUT_MS); |
Felipe Leme | 329d040 | 2017-12-06 09:22:43 -0800 | [diff] [blame] | 1482 | try { |
Felipe Leme | d4e5228 | 2018-06-18 13:56:38 -0700 | [diff] [blame] | 1483 | mService.isFieldClassificationEnabled(receiver); |
| 1484 | return receiver.getIntResult() == 1; |
Felipe Leme | 329d040 | 2017-12-06 09:22:43 -0800 | [diff] [blame] | 1485 | } catch (RemoteException e) { |
| 1486 | e.rethrowFromSystemServer(); |
| 1487 | return false; |
| 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | /** |
Felipe Leme | 27f4573 | 2017-12-22 09:05:22 -0800 | [diff] [blame] | 1492 | * Gets the name of the default algorithm used for |
| 1493 | * <a href="AutofillService.html#FieldClassification">field classification</a>. |
| 1494 | * |
| 1495 | * <p>The default algorithm is used when the algorithm on {@link UserData} is invalid or not |
| 1496 | * set. |
| 1497 | * |
| 1498 | * <p><b>Note:</b> This method should only be called by an app providing an autofill service, |
| 1499 | * and it's ignored if the caller currently doesn't have an enabled autofill service for |
| 1500 | * the user. |
| 1501 | */ |
| 1502 | @Nullable |
| 1503 | public String getDefaultFieldClassificationAlgorithm() { |
Felipe Leme | c0c15a3 | 2019-01-08 10:53:33 -0800 | [diff] [blame] | 1504 | final SyncResultReceiver receiver = new SyncResultReceiver(SYNC_CALLS_TIMEOUT_MS); |
Felipe Leme | 27f4573 | 2017-12-22 09:05:22 -0800 | [diff] [blame] | 1505 | try { |
Felipe Leme | d4e5228 | 2018-06-18 13:56:38 -0700 | [diff] [blame] | 1506 | mService.getDefaultFieldClassificationAlgorithm(receiver); |
Felipe Leme | c0c15a3 | 2019-01-08 10:53:33 -0800 | [diff] [blame] | 1507 | return receiver.getStringResult(); |
Felipe Leme | 27f4573 | 2017-12-22 09:05:22 -0800 | [diff] [blame] | 1508 | } catch (RemoteException e) { |
| 1509 | e.rethrowFromSystemServer(); |
| 1510 | return null; |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | /** |
| 1515 | * Gets the name of all algorithms currently available for |
| 1516 | * <a href="AutofillService.html#FieldClassification">field classification</a>. |
| 1517 | * |
| 1518 | * <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] | 1519 | * and it returns an empty list if the caller currently doesn't have an enabled autofill service |
| 1520 | * for the user. |
Felipe Leme | 27f4573 | 2017-12-22 09:05:22 -0800 | [diff] [blame] | 1521 | */ |
| 1522 | @NonNull |
| 1523 | public List<String> getAvailableFieldClassificationAlgorithms() { |
Felipe Leme | c0c15a3 | 2019-01-08 10:53:33 -0800 | [diff] [blame] | 1524 | final SyncResultReceiver receiver = new SyncResultReceiver(SYNC_CALLS_TIMEOUT_MS); |
Felipe Leme | 27f4573 | 2017-12-22 09:05:22 -0800 | [diff] [blame] | 1525 | try { |
Felipe Leme | d4e5228 | 2018-06-18 13:56:38 -0700 | [diff] [blame] | 1526 | mService.getAvailableFieldClassificationAlgorithms(receiver); |
Felipe Leme | c0c15a3 | 2019-01-08 10:53:33 -0800 | [diff] [blame] | 1527 | final String[] algorithms = receiver.getStringArrayResult(); |
Felipe Leme | e4ac740 | 2018-01-16 19:37:00 -0800 | [diff] [blame] | 1528 | return algorithms != null ? Arrays.asList(algorithms) : Collections.emptyList(); |
Felipe Leme | 27f4573 | 2017-12-22 09:05:22 -0800 | [diff] [blame] | 1529 | } catch (RemoteException e) { |
| 1530 | e.rethrowFromSystemServer(); |
| 1531 | return null; |
| 1532 | } |
| 1533 | } |
| 1534 | |
| 1535 | /** |
Ricardo Loo | 33d226c | 2017-06-27 14:17:33 -0700 | [diff] [blame] | 1536 | * Returns {@code true} if autofill is supported by the current device and |
| 1537 | * is supported for this user. |
Felipe Leme | db04118 | 2017-04-21 17:33:38 -0700 | [diff] [blame] | 1538 | * |
| 1539 | * <p>Autofill is typically supported, but it could be unsupported in cases like: |
| 1540 | * <ol> |
| 1541 | * <li>Low-end devices. |
| 1542 | * <li>Device policy rules that forbid its usage. |
| 1543 | * </ol> |
| 1544 | */ |
| 1545 | public boolean isAutofillSupported() { |
| 1546 | if (mService == null) return false; |
| 1547 | |
Felipe Leme | c0c15a3 | 2019-01-08 10:53:33 -0800 | [diff] [blame] | 1548 | final SyncResultReceiver receiver = new SyncResultReceiver(SYNC_CALLS_TIMEOUT_MS); |
Felipe Leme | db04118 | 2017-04-21 17:33:38 -0700 | [diff] [blame] | 1549 | try { |
Felipe Leme | d4e5228 | 2018-06-18 13:56:38 -0700 | [diff] [blame] | 1550 | mService.isServiceSupported(mContext.getUserId(), receiver); |
| 1551 | return receiver.getIntResult() == 1; |
Felipe Leme | db04118 | 2017-04-21 17:33:38 -0700 | [diff] [blame] | 1552 | } catch (RemoteException e) { |
| 1553 | throw e.rethrowFromSystemServer(); |
| 1554 | } |
| 1555 | } |
| 1556 | |
Felipe Leme | 637e05e | 2017-12-06 12:09:37 -0800 | [diff] [blame] | 1557 | // Note: don't need to use locked suffix because mContext is final. |
| 1558 | private AutofillClient getClient() { |
Felipe Leme | 686128e | 2017-10-17 14:02:20 -0700 | [diff] [blame] | 1559 | final AutofillClient client = mContext.getAutofillClient(); |
Felipe Leme | 261e75f | 2018-12-17 10:15:08 -0800 | [diff] [blame] | 1560 | if (client == null && sVerbose) { |
| 1561 | Log.v(TAG, "No AutofillClient for " + mContext.getPackageName() + " on context " |
Felipe Leme | 686128e | 2017-10-17 14:02:20 -0700 | [diff] [blame] | 1562 | + mContext); |
| 1563 | } |
| 1564 | return client; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
Dake Gu | d9dbd27 | 2018-03-13 11:38:42 -0700 | [diff] [blame] | 1567 | /** |
| 1568 | * Check if autofill ui is showing, must be called on UI thread. |
| 1569 | * @hide |
| 1570 | */ |
| 1571 | public boolean isAutofillUiShowing() { |
| 1572 | final AutofillClient client = mContext.getAutofillClient(); |
Felipe Leme | cb2e83d | 2018-03-19 11:15:00 -0700 | [diff] [blame] | 1573 | return client != null && client.autofillClientIsFillUiShowing(); |
Dake Gu | d9dbd27 | 2018-03-13 11:38:42 -0700 | [diff] [blame] | 1574 | } |
| 1575 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1576 | /** @hide */ |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 1577 | public void onAuthenticationResult(int authenticationId, Intent data, View focusView) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 1578 | if (!hasAutofillFeature()) { |
| 1579 | return; |
| 1580 | } |
Felipe Leme | 85d1c2d | 2017-04-21 08:56:04 -0700 | [diff] [blame] | 1581 | // 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] | 1582 | // handling the cases where it's not RESULT_OK: it works fine if the service does not |
| 1583 | // set the EXTRA_AUTHENTICATION_RESULT extra, but it could cause weird results if the |
| 1584 | // service set the extra and returned RESULT_CANCELED... |
| 1585 | |
Felipe Leme | 1ad95f0 | 2018-06-15 13:07:34 -0700 | [diff] [blame] | 1586 | if (sDebug) { |
| 1587 | Log.d(TAG, "onAuthenticationResult(): id= " + authenticationId + ", data=" + data); |
| 1588 | } |
Felipe Leme | d633f07 | 2017-02-14 10:17:17 -0800 | [diff] [blame] | 1589 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1590 | synchronized (mLock) { |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 1591 | if (!isActiveLocked()) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1592 | return; |
| 1593 | } |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 1594 | // If authenticate activity closes itself during onCreate(), there is no onStop/onStart |
| 1595 | // of app activity. We enforce enter event to re-show fill ui in such case. |
| 1596 | // CTS example: |
| 1597 | // LoginActivityTest#testDatasetAuthTwoFieldsUserCancelsFirstAttempt |
| 1598 | // LoginActivityTest#testFillResponseAuthBothFieldsUserCancelsFirstAttempt |
| 1599 | if (!mOnInvisibleCalled && focusView != null |
| 1600 | && focusView.canNotifyAutofillEnterExitEvent()) { |
| 1601 | notifyViewExitedLocked(focusView); |
| 1602 | notifyViewEnteredLocked(focusView, 0); |
| 1603 | } |
| 1604 | if (data == null) { |
| 1605 | // data is set to null when result is not RESULT_OK |
| 1606 | return; |
| 1607 | } |
| 1608 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1609 | final Parcelable result = data.getParcelableExtra(EXTRA_AUTHENTICATION_RESULT); |
| 1610 | final Bundle responseData = new Bundle(); |
| 1611 | responseData.putParcelable(EXTRA_AUTHENTICATION_RESULT, result); |
Felipe Leme | a937238 | 2017-10-09 14:42:36 -0700 | [diff] [blame] | 1612 | final Bundle newClientState = data.getBundleExtra(EXTRA_CLIENT_STATE); |
| 1613 | if (newClientState != null) { |
| 1614 | responseData.putBundle(EXTRA_CLIENT_STATE, newClientState); |
| 1615 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1616 | try { |
Svetoslav Ganov | a9379d0 | 2017-05-09 17:40:24 -0700 | [diff] [blame] | 1617 | mService.setAuthenticationResult(responseData, mSessionId, authenticationId, |
| 1618 | mContext.getUserId()); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1619 | } catch (RemoteException e) { |
| 1620 | Log.e(TAG, "Error delivering authentication result", e); |
| 1621 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1622 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 1623 | } |
| 1624 | |
Felipe Leme | 42b9793 | 2018-02-20 13:04:31 -0800 | [diff] [blame] | 1625 | /** |
| 1626 | * Gets the next unique autofill ID for the activity context. |
| 1627 | * |
| 1628 | * <p>Typically used to manage views whose content is recycled - see |
| 1629 | * {@link View#setAutofillId(AutofillId)} for more info. |
| 1630 | * |
| 1631 | * @return An ID that is unique in the activity, or {@code null} if autofill is not supported in |
| 1632 | * the {@link Context} associated with this {@link AutofillManager}. |
| 1633 | */ |
| 1634 | @Nullable |
| 1635 | public AutofillId getNextAutofillId() { |
| 1636 | final AutofillClient client = getClient(); |
| 1637 | if (client == null) return null; |
| 1638 | |
| 1639 | final AutofillId id = client.autofillClientGetNextAutofillId(); |
| 1640 | |
| 1641 | if (id == null && sDebug) { |
| 1642 | Log.d(TAG, "getNextAutofillId(): client " + client + " returned null"); |
| 1643 | } |
| 1644 | |
| 1645 | return id; |
Felipe Leme | 0200d9e | 2017-01-24 15:10:26 -0800 | [diff] [blame] | 1646 | } |
| 1647 | |
Felipe Leme | 305fd40 | 2018-09-11 18:20:42 +0000 | [diff] [blame] | 1648 | private static AutofillId getAutofillId(View parent, int virtualId) { |
| 1649 | return new AutofillId(parent.getAutofillViewId(), virtualId); |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 1650 | } |
| 1651 | |
Andreas Gampe | 3f24e69 | 2018-02-05 13:24:28 -0800 | [diff] [blame] | 1652 | @GuardedBy("mLock") |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1653 | private void startSessionLocked(@NonNull AutofillId id, @NonNull Rect bounds, |
| 1654 | @NonNull AutofillValue value, int flags) { |
Felipe Leme | a7de402 | 2019-02-19 17:16:45 -0800 | [diff] [blame] | 1655 | if (mEnteredForAugmentedAutofillIds != null |
| 1656 | && mEnteredForAugmentedAutofillIds.contains(id)) { |
| 1657 | if (sVerbose) Log.v(TAG, "Starting session for augmented autofill on " + id); |
| 1658 | flags |= FLAG_AUGMENTED_AUTOFILL_REQUEST; |
| 1659 | } |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 1660 | if (sVerbose) { |
| 1661 | Log.v(TAG, "startSessionLocked(): id=" + id + ", bounds=" + bounds + ", value=" + value |
felipeal | fd3f8f6 | 2018-02-07 11:49:07 +0100 | [diff] [blame] | 1662 | + ", flags=" + flags + ", state=" + getStateAsStringLocked() |
Felipe Leme | 9a15c0f | 2018-02-14 17:26:46 -0800 | [diff] [blame] | 1663 | + ", compatMode=" + isCompatibilityModeEnabledLocked() |
| 1664 | + ", enteredIds=" + mEnteredIds); |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 1665 | } |
Felipe Leme | 3103c63 | 2017-12-18 15:05:14 -0800 | [diff] [blame] | 1666 | if (mState != STATE_UNKNOWN && !isFinishedLocked() && (flags & FLAG_MANUAL_REQUEST) == 0) { |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 1667 | if (sVerbose) { |
| 1668 | Log.v(TAG, "not automatically starting session for " + id |
Felipe Leme | 3103c63 | 2017-12-18 15:05:14 -0800 | [diff] [blame] | 1669 | + " on state " + getStateAsStringLocked() + " and flags " + flags); |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 1670 | } |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 1671 | return; |
| 1672 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 1673 | try { |
Felipe Leme | 637e05e | 2017-12-06 12:09:37 -0800 | [diff] [blame] | 1674 | final AutofillClient client = getClient(); |
felipeal | fd3f8f6 | 2018-02-07 11:49:07 +0100 | [diff] [blame] | 1675 | if (client == null) return; // NOTE: getClient() already logged it.. |
Felipe Leme | 637e05e | 2017-12-06 12:09:37 -0800 | [diff] [blame] | 1676 | |
Felipe Leme | c0c15a3 | 2019-01-08 10:53:33 -0800 | [diff] [blame] | 1677 | final SyncResultReceiver receiver = new SyncResultReceiver(SYNC_CALLS_TIMEOUT_MS); |
Felipe Leme | d4e5228 | 2018-06-18 13:56:38 -0700 | [diff] [blame] | 1678 | mService.startSession(client.autofillClientGetActivityToken(), |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1679 | mServiceClient.asBinder(), id, bounds, value, mContext.getUserId(), |
Felipe Leme | 185de72 | 2018-02-13 17:25:44 -0800 | [diff] [blame] | 1680 | mCallback != null, flags, client.autofillClientGetComponentName(), |
Felipe Leme | d4e5228 | 2018-06-18 13:56:38 -0700 | [diff] [blame] | 1681 | isCompatibilityModeEnabledLocked(), receiver); |
| 1682 | mSessionId = receiver.getIntResult(); |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 1683 | if (mSessionId != NO_SESSION) { |
| 1684 | mState = STATE_ACTIVE; |
| 1685 | } |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 1686 | client.autofillClientResetableStateAvailable(); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1687 | } catch (RemoteException e) { |
| 1688 | throw e.rethrowFromSystemServer(); |
| 1689 | } |
| 1690 | } |
| 1691 | |
Andreas Gampe | 3f24e69 | 2018-02-05 13:24:28 -0800 | [diff] [blame] | 1692 | @GuardedBy("mLock") |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1693 | private void finishSessionLocked() { |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 1694 | if (sVerbose) Log.v(TAG, "finishSessionLocked(): " + getStateAsStringLocked()); |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 1695 | |
| 1696 | if (!isActiveLocked()) return; |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1697 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1698 | try { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1699 | mService.finishSession(mSessionId, mContext.getUserId()); |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 1700 | } catch (RemoteException e) { |
| 1701 | throw e.rethrowFromSystemServer(); |
| 1702 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1703 | |
Felipe Leme | 9a15c0f | 2018-02-14 17:26:46 -0800 | [diff] [blame] | 1704 | resetSessionLocked(/* resetEnteredIds= */ true); |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 1705 | } |
| 1706 | |
Andreas Gampe | 3f24e69 | 2018-02-05 13:24:28 -0800 | [diff] [blame] | 1707 | @GuardedBy("mLock") |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1708 | private void cancelSessionLocked() { |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 1709 | if (sVerbose) Log.v(TAG, "cancelSessionLocked(): " + getStateAsStringLocked()); |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 1710 | |
| 1711 | if (!isActiveLocked()) return; |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1712 | |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 1713 | try { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1714 | mService.cancelSession(mSessionId, mContext.getUserId()); |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 1715 | } catch (RemoteException e) { |
| 1716 | throw e.rethrowFromSystemServer(); |
| 1717 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1718 | |
Felipe Leme | 9a15c0f | 2018-02-14 17:26:46 -0800 | [diff] [blame] | 1719 | resetSessionLocked(/* resetEnteredIds= */ true); |
Svet Ganov | 48f10a2 | 2017-04-26 18:49:30 -0700 | [diff] [blame] | 1720 | } |
| 1721 | |
Andreas Gampe | 3f24e69 | 2018-02-05 13:24:28 -0800 | [diff] [blame] | 1722 | @GuardedBy("mLock") |
Felipe Leme | 9a15c0f | 2018-02-14 17:26:46 -0800 | [diff] [blame] | 1723 | private void resetSessionLocked(boolean resetEnteredIds) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1724 | mSessionId = NO_SESSION; |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 1725 | mState = STATE_UNKNOWN; |
Svet Ganov | 48f10a2 | 2017-04-26 18:49:30 -0700 | [diff] [blame] | 1726 | mTrackedViews = null; |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 1727 | mFillableIds = null; |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 1728 | mSaveTriggerId = null; |
Dake Gu | b0fa378 | 2018-02-26 12:25:14 -0800 | [diff] [blame] | 1729 | mIdShownFillUi = null; |
Felipe Leme | 9a15c0f | 2018-02-14 17:26:46 -0800 | [diff] [blame] | 1730 | if (resetEnteredIds) { |
| 1731 | mEnteredIds = null; |
| 1732 | } |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 1733 | } |
| 1734 | |
Andreas Gampe | 3f24e69 | 2018-02-05 13:24:28 -0800 | [diff] [blame] | 1735 | @GuardedBy("mLock") |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 1736 | private void updateSessionLocked(AutofillId id, Rect bounds, AutofillValue value, int action, |
| 1737 | int flags) { |
Felipe Leme | 9a15c0f | 2018-02-14 17:26:46 -0800 | [diff] [blame] | 1738 | if (sVerbose) { |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 1739 | Log.v(TAG, "updateSessionLocked(): id=" + id + ", bounds=" + bounds |
| 1740 | + ", value=" + value + ", action=" + action + ", flags=" + flags); |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 1741 | } |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 1742 | try { |
Felipe Leme | d62b516 | 2018-06-06 15:08:25 -0700 | [diff] [blame] | 1743 | mService.updateSession(mSessionId, id, bounds, value, action, flags, |
| 1744 | mContext.getUserId()); |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 1745 | } catch (RemoteException e) { |
| 1746 | throw e.rethrowFromSystemServer(); |
| 1747 | } |
| 1748 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1749 | |
Andreas Gampe | 3f24e69 | 2018-02-05 13:24:28 -0800 | [diff] [blame] | 1750 | @GuardedBy("mLock") |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1751 | private void ensureServiceClientAddedIfNeededLocked() { |
Felipe Leme | 637e05e | 2017-12-06 12:09:37 -0800 | [diff] [blame] | 1752 | if (getClient() == null) { |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1753 | return; |
| 1754 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1755 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1756 | if (mServiceClient == null) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 1757 | mServiceClient = new AutofillManagerClient(this); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1758 | try { |
Koji Fukui | ccec6a6 | 2017-10-18 17:48:40 +0900 | [diff] [blame] | 1759 | final int userId = mContext.getUserId(); |
Felipe Leme | c0c15a3 | 2019-01-08 10:53:33 -0800 | [diff] [blame] | 1760 | final SyncResultReceiver receiver = new SyncResultReceiver(SYNC_CALLS_TIMEOUT_MS); |
Felipe Leme | d4e5228 | 2018-06-18 13:56:38 -0700 | [diff] [blame] | 1761 | mService.addClient(mServiceClient, userId, receiver); |
| 1762 | final int flags = receiver.getIntResult(); |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 1763 | mEnabled = (flags & FLAG_ADD_CLIENT_ENABLED) != 0; |
| 1764 | sDebug = (flags & FLAG_ADD_CLIENT_DEBUG) != 0; |
| 1765 | sVerbose = (flags & FLAG_ADD_CLIENT_VERBOSE) != 0; |
Koji Fukui | ccec6a6 | 2017-10-18 17:48:40 +0900 | [diff] [blame] | 1766 | final IAutoFillManager service = mService; |
| 1767 | final IAutoFillManagerClient serviceClient = mServiceClient; |
| 1768 | mServiceClientCleaner = Cleaner.create(this, () -> { |
Felipe Leme | 559e21d | 2019-01-18 17:57:21 -0800 | [diff] [blame] | 1769 | // TODO(b/123100811): call service to also remove reference to |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 1770 | // mAugmentedAutofillServiceClient |
Koji Fukui | ccec6a6 | 2017-10-18 17:48:40 +0900 | [diff] [blame] | 1771 | try { |
| 1772 | service.removeClient(serviceClient, userId); |
| 1773 | } catch (RemoteException e) { |
| 1774 | } |
| 1775 | }); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1776 | } catch (RemoteException e) { |
| 1777 | throw e.rethrowFromSystemServer(); |
| 1778 | } |
| 1779 | } |
| 1780 | } |
| 1781 | |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1782 | /** |
| 1783 | * Registers a {@link AutofillCallback} to receive autofill events. |
| 1784 | * |
| 1785 | * @param callback callback to receive events. |
| 1786 | */ |
| 1787 | public void registerCallback(@Nullable AutofillCallback callback) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 1788 | if (!hasAutofillFeature()) { |
| 1789 | return; |
| 1790 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1791 | synchronized (mLock) { |
| 1792 | if (callback == null) return; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1793 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1794 | final boolean hadCallback = mCallback != null; |
| 1795 | mCallback = callback; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1796 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1797 | if (!hadCallback) { |
| 1798 | try { |
| 1799 | mService.setHasCallback(mSessionId, mContext.getUserId(), true); |
| 1800 | } catch (RemoteException e) { |
| 1801 | throw e.rethrowFromSystemServer(); |
| 1802 | } |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1803 | } |
| 1804 | } |
| 1805 | } |
| 1806 | |
| 1807 | /** |
| 1808 | * Unregisters a {@link AutofillCallback} to receive autofill events. |
| 1809 | * |
| 1810 | * @param callback callback to stop receiving events. |
| 1811 | */ |
| 1812 | public void unregisterCallback(@Nullable AutofillCallback callback) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 1813 | if (!hasAutofillFeature()) { |
| 1814 | return; |
| 1815 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1816 | synchronized (mLock) { |
| 1817 | if (callback == null || mCallback == null || callback != mCallback) return; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1818 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1819 | mCallback = null; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1820 | |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1821 | try { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1822 | mService.setHasCallback(mSessionId, mContext.getUserId(), false); |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1823 | } catch (RemoteException e) { |
| 1824 | throw e.rethrowFromSystemServer(); |
| 1825 | } |
| 1826 | } |
| 1827 | } |
| 1828 | |
Felipe Leme | 559e21d | 2019-01-18 17:57:21 -0800 | [diff] [blame] | 1829 | /** |
Felipe Leme | 559e21d | 2019-01-18 17:57:21 -0800 | [diff] [blame] | 1830 | * Explicitly limits augmented autofill to the given packages and activities. |
| 1831 | * |
Felipe Leme | 559e21d | 2019-01-18 17:57:21 -0800 | [diff] [blame] | 1832 | * <p>To reset the whitelist, call it passing {@code null} to both arguments. |
| 1833 | * |
| 1834 | * <p>Useful when the service wants to restrict augmented autofill to a category of apps, like |
| 1835 | * apps that uses addresses. For example, if the service wants to support augmented autofill on |
| 1836 | * all activities of app {@code AddressApp1} and just activities {@code act1} and {@code act2} |
| 1837 | * of {@code AddressApp2}, it would call: |
| 1838 | * {@code setAugmentedAutofillWhitelist(Arrays.asList("AddressApp1"), |
| 1839 | * Arrays.asList(new ComponentName("AddressApp2", "act1"), |
| 1840 | * new ComponentName("AddressApp2", "act2")));} |
| 1841 | * |
| 1842 | * <p><b>Note:</b> This method should only be called by the app providing the augmented autofill |
| 1843 | * service, and it's ignored if the caller isn't it. |
| 1844 | * |
| 1845 | * @hide |
| 1846 | */ |
| 1847 | @SystemApi |
| 1848 | @TestApi |
Felipe Leme | 7a3c9f5 | 2019-02-13 16:32:49 -0800 | [diff] [blame] | 1849 | public void setAugmentedAutofillWhitelist(@Nullable Set<String> packages, |
| 1850 | @Nullable Set<ComponentName> activities) { |
Felipe Leme | 80e7bf1 | 2019-02-14 10:56:52 -0800 | [diff] [blame] | 1851 | if (!hasAutofillFeature()) { |
| 1852 | return; |
| 1853 | } |
| 1854 | |
| 1855 | final SyncResultReceiver resultReceiver = new SyncResultReceiver(SYNC_CALLS_TIMEOUT_MS); |
| 1856 | final int resultCode; |
| 1857 | try { |
| 1858 | mService.setAugmentedAutofillWhitelist(toList(packages), toList(activities), |
| 1859 | resultReceiver); |
| 1860 | resultCode = resultReceiver.getIntResult(); |
| 1861 | } catch (RemoteException e) { |
| 1862 | throw e.rethrowFromSystemServer(); |
| 1863 | } |
| 1864 | switch (resultCode) { |
| 1865 | case RESULT_OK: |
| 1866 | return; |
| 1867 | case RESULT_CODE_NOT_SERVICE: |
| 1868 | throw new SecurityException("caller is not user's Augmented Autofill Service"); |
| 1869 | default: |
| 1870 | Log.wtf(TAG, "setAugmentedAutofillWhitelist(): received invalid result: " |
| 1871 | + resultCode); |
| 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | private <T> ArrayList<T> toList(@Nullable Set<T> set) { |
| 1876 | return set == null ? null : new ArrayList<T>(set); |
Felipe Leme | 559e21d | 2019-01-18 17:57:21 -0800 | [diff] [blame] | 1877 | } |
| 1878 | |
Felipe Leme | a7de402 | 2019-02-19 17:16:45 -0800 | [diff] [blame] | 1879 | /** |
| 1880 | * Notifies that a non-autofillable view was entered because the activity is whitelisted for |
| 1881 | * augmented autofill. |
| 1882 | * |
| 1883 | * <p>This method is necessary to set the right flag on start, so the server-side session |
| 1884 | * doesn't trigger the standard autofill workflow, but the augmented's instead. |
| 1885 | * |
| 1886 | * @hide |
| 1887 | */ |
| 1888 | public void notifyViewEnteredForAugmentedAutofill(@NonNull View view) { |
| 1889 | final AutofillId id = view.getAutofillId(); |
| 1890 | synchronized (mLock) { |
| 1891 | if (mEnteredForAugmentedAutofillIds == null) { |
| 1892 | mEnteredForAugmentedAutofillIds = new ArraySet<>(1); |
| 1893 | } |
| 1894 | mEnteredForAugmentedAutofillIds.add(id); |
| 1895 | } |
| 1896 | } |
| 1897 | |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1898 | private void requestShowFillUi(int sessionId, AutofillId id, int width, int height, |
| 1899 | Rect anchorBounds, IAutofillWindowPresenter presenter) { |
| 1900 | final View anchor = findView(id); |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1901 | if (anchor == null) { |
| 1902 | return; |
| 1903 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1904 | |
| 1905 | AutofillCallback callback = null; |
| 1906 | synchronized (mLock) { |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1907 | if (mSessionId == sessionId) { |
Felipe Leme | 637e05e | 2017-12-06 12:09:37 -0800 | [diff] [blame] | 1908 | AutofillClient client = getClient(); |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1909 | |
| 1910 | if (client != null) { |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 1911 | if (client.autofillClientRequestShowFillUi(anchor, width, height, |
Dake Gu | b0fa378 | 2018-02-26 12:25:14 -0800 | [diff] [blame] | 1912 | anchorBounds, presenter)) { |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1913 | callback = mCallback; |
Dake Gu | b0fa378 | 2018-02-26 12:25:14 -0800 | [diff] [blame] | 1914 | mIdShownFillUi = id; |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1915 | } |
| 1916 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1917 | } |
| 1918 | } |
| 1919 | |
| 1920 | if (callback != null) { |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 1921 | if (id.isVirtualInt()) { |
| 1922 | callback.onAutofillEvent(anchor, id.getVirtualChildIntId(), |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1923 | AutofillCallback.EVENT_INPUT_SHOWN); |
| 1924 | } else { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1925 | callback.onAutofillEvent(anchor, AutofillCallback.EVENT_INPUT_SHOWN); |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1926 | } |
| 1927 | } |
| 1928 | } |
| 1929 | |
Svetoslav Ganov | a9379d0 | 2017-05-09 17:40:24 -0700 | [diff] [blame] | 1930 | private void authenticate(int sessionId, int authenticationId, IntentSender intent, |
| 1931 | Intent fillInIntent) { |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1932 | synchronized (mLock) { |
| 1933 | if (sessionId == mSessionId) { |
Felipe Leme | 637e05e | 2017-12-06 12:09:37 -0800 | [diff] [blame] | 1934 | final AutofillClient client = getClient(); |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1935 | if (client != null) { |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 1936 | // clear mOnInvisibleCalled and we will see if receive onInvisibleForAutofill() |
| 1937 | // before onAuthenticationResult() |
| 1938 | mOnInvisibleCalled = false; |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 1939 | client.autofillClientAuthenticate(authenticationId, intent, fillInIntent); |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1940 | } |
| 1941 | } |
| 1942 | } |
| 1943 | } |
| 1944 | |
Dake Gu | 6a20a19 | 2018-02-08 12:09:30 -0800 | [diff] [blame] | 1945 | private void dispatchUnhandledKey(int sessionId, AutofillId id, KeyEvent keyEvent) { |
| 1946 | final View anchor = findView(id); |
| 1947 | if (anchor == null) { |
| 1948 | return; |
| 1949 | } |
| 1950 | |
Dake Gu | 6a20a19 | 2018-02-08 12:09:30 -0800 | [diff] [blame] | 1951 | synchronized (mLock) { |
| 1952 | if (mSessionId == sessionId) { |
| 1953 | AutofillClient client = getClient(); |
| 1954 | |
| 1955 | if (client != null) { |
| 1956 | client.autofillClientDispatchUnhandledKey(anchor, keyEvent); |
| 1957 | } |
| 1958 | } |
| 1959 | } |
| 1960 | } |
| 1961 | |
Felipe Leme | 51e29da | 2017-10-24 14:03:10 -0700 | [diff] [blame] | 1962 | /** @hide */ |
| 1963 | public static final int SET_STATE_FLAG_ENABLED = 0x01; |
| 1964 | /** @hide */ |
| 1965 | public static final int SET_STATE_FLAG_RESET_SESSION = 0x02; |
| 1966 | /** @hide */ |
| 1967 | public static final int SET_STATE_FLAG_RESET_CLIENT = 0x04; |
| 1968 | /** @hide */ |
| 1969 | public static final int SET_STATE_FLAG_DEBUG = 0x08; |
| 1970 | /** @hide */ |
| 1971 | public static final int SET_STATE_FLAG_VERBOSE = 0x10; |
| 1972 | |
| 1973 | private void setState(int flags) { |
| 1974 | if (sVerbose) Log.v(TAG, "setState(" + flags + ")"); |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1975 | synchronized (mLock) { |
Felipe Leme | 51e29da | 2017-10-24 14:03:10 -0700 | [diff] [blame] | 1976 | mEnabled = (flags & SET_STATE_FLAG_ENABLED) != 0; |
| 1977 | if (!mEnabled || (flags & SET_STATE_FLAG_RESET_SESSION) != 0) { |
Svet Ganov | 48f10a2 | 2017-04-26 18:49:30 -0700 | [diff] [blame] | 1978 | // Reset the session state |
Felipe Leme | 9a15c0f | 2018-02-14 17:26:46 -0800 | [diff] [blame] | 1979 | resetSessionLocked(/* resetEnteredIds= */ true); |
Svet Ganov | 48f10a2 | 2017-04-26 18:49:30 -0700 | [diff] [blame] | 1980 | } |
Felipe Leme | 51e29da | 2017-10-24 14:03:10 -0700 | [diff] [blame] | 1981 | if ((flags & SET_STATE_FLAG_RESET_CLIENT) != 0) { |
Svet Ganov | 48f10a2 | 2017-04-26 18:49:30 -0700 | [diff] [blame] | 1982 | // Reset connection to system |
| 1983 | mServiceClient = null; |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 1984 | mAugmentedAutofillServiceClient = null; |
Koji Fukui | ccec6a6 | 2017-10-18 17:48:40 +0900 | [diff] [blame] | 1985 | if (mServiceClientCleaner != null) { |
| 1986 | mServiceClientCleaner.clean(); |
| 1987 | mServiceClientCleaner = null; |
| 1988 | } |
Svet Ganov | 48f10a2 | 2017-04-26 18:49:30 -0700 | [diff] [blame] | 1989 | } |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1990 | } |
Felipe Leme | 51e29da | 2017-10-24 14:03:10 -0700 | [diff] [blame] | 1991 | sDebug = (flags & SET_STATE_FLAG_DEBUG) != 0; |
| 1992 | sVerbose = (flags & SET_STATE_FLAG_VERBOSE) != 0; |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1993 | } |
| 1994 | |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 1995 | /** |
| 1996 | * Sets a view as autofilled if the current value is the {code targetValue}. |
| 1997 | * |
| 1998 | * @param view The view that is to be autofilled |
| 1999 | * @param targetValue The value we want to fill into view |
| 2000 | */ |
| 2001 | private void setAutofilledIfValuesIs(@NonNull View view, @Nullable AutofillValue targetValue) { |
| 2002 | AutofillValue currentValue = view.getAutofillValue(); |
| 2003 | if (Objects.equals(currentValue, targetValue)) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 2004 | synchronized (mLock) { |
| 2005 | if (mLastAutofilledData == null) { |
| 2006 | mLastAutofilledData = new ParcelableMap(1); |
| 2007 | } |
Felipe Leme | 42b9793 | 2018-02-20 13:04:31 -0800 | [diff] [blame] | 2008 | mLastAutofilledData.put(view.getAutofillId(), targetValue); |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 2009 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 2010 | view.setAutofilled(true); |
| 2011 | } |
| 2012 | } |
| 2013 | |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 2014 | private void autofill(int sessionId, List<AutofillId> ids, List<AutofillValue> values) { |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2015 | synchronized (mLock) { |
| 2016 | if (sessionId != mSessionId) { |
| 2017 | return; |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 2018 | } |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2019 | |
Felipe Leme | 637e05e | 2017-12-06 12:09:37 -0800 | [diff] [blame] | 2020 | final AutofillClient client = getClient(); |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 2021 | if (client == null) { |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2022 | return; |
| 2023 | } |
| 2024 | |
| 2025 | final int itemCount = ids.size(); |
| 2026 | int numApplied = 0; |
| 2027 | ArrayMap<View, SparseArray<AutofillValue>> virtualValues = null; |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 2028 | final View[] views = client.autofillClientFindViewsByAutofillIdTraversal( |
| 2029 | Helper.toArray(ids)); |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2030 | |
Felipe Leme | 49f08ed | 2018-03-26 16:18:45 -0700 | [diff] [blame] | 2031 | ArrayList<AutofillId> failedIds = null; |
| 2032 | |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2033 | for (int i = 0; i < itemCount; i++) { |
| 2034 | final AutofillId id = ids.get(i); |
| 2035 | final AutofillValue value = values.get(i); |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 2036 | final View view = views[i]; |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2037 | if (view == null) { |
Felipe Leme | 49f08ed | 2018-03-26 16:18:45 -0700 | [diff] [blame] | 2038 | // Most likely view has been removed after the initial request was sent to the |
| 2039 | // the service; this is fine, but we need to update the view status in the |
| 2040 | // server side so it can be triggered again. |
| 2041 | Log.d(TAG, "autofill(): no View with id " + id); |
| 2042 | if (failedIds == null) { |
| 2043 | failedIds = new ArrayList<>(); |
| 2044 | } |
| 2045 | failedIds.add(id); |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2046 | continue; |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 2047 | } |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 2048 | if (id.isVirtualInt()) { |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2049 | if (virtualValues == null) { |
| 2050 | // Most likely there will be just one view with virtual children. |
| 2051 | virtualValues = new ArrayMap<>(1); |
| 2052 | } |
| 2053 | SparseArray<AutofillValue> valuesByParent = virtualValues.get(view); |
| 2054 | if (valuesByParent == null) { |
| 2055 | // We don't know the size yet, but usually it will be just a few fields... |
| 2056 | valuesByParent = new SparseArray<>(5); |
| 2057 | virtualValues.put(view, valuesByParent); |
| 2058 | } |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 2059 | valuesByParent.put(id.getVirtualChildIntId(), value); |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2060 | } else { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 2061 | // Mark the view as to be autofilled with 'value' |
| 2062 | if (mLastAutofilledData == null) { |
| 2063 | mLastAutofilledData = new ParcelableMap(itemCount - i); |
| 2064 | } |
| 2065 | mLastAutofilledData.put(id, value); |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2066 | |
| 2067 | view.autofill(value); |
| 2068 | |
| 2069 | // Set as autofilled if the values match now, e.g. when the value was updated |
| 2070 | // synchronously. |
| 2071 | // If autofill happens async, the view is set to autofilled in |
| 2072 | // notifyValueChanged. |
| 2073 | setAutofilledIfValuesIs(view, value); |
| 2074 | |
| 2075 | numApplied++; |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 2076 | } |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 2077 | } |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 2078 | |
Felipe Leme | 49f08ed | 2018-03-26 16:18:45 -0700 | [diff] [blame] | 2079 | if (failedIds != null) { |
| 2080 | if (sVerbose) { |
| 2081 | Log.v(TAG, "autofill(): total failed views: " + failedIds); |
| 2082 | } |
| 2083 | try { |
| 2084 | mService.setAutofillFailure(mSessionId, failedIds, mContext.getUserId()); |
| 2085 | } catch (RemoteException e) { |
| 2086 | // In theory, we could ignore this error since it's not a big deal, but |
| 2087 | // in reality, we rather crash the app anyways, as the failure could be |
| 2088 | // a consequence of something going wrong on the server side... |
| 2089 | e.rethrowFromSystemServer(); |
| 2090 | } |
| 2091 | } |
| 2092 | |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2093 | if (virtualValues != null) { |
| 2094 | for (int i = 0; i < virtualValues.size(); i++) { |
| 2095 | final View parent = virtualValues.keyAt(i); |
| 2096 | final SparseArray<AutofillValue> childrenValues = virtualValues.valueAt(i); |
| 2097 | parent.autofill(childrenValues); |
| 2098 | numApplied += childrenValues.size(); |
Felipe Leme | 49f08ed | 2018-03-26 16:18:45 -0700 | [diff] [blame] | 2099 | // TODO: we should provide a callback so the parent can call failures; something |
| 2100 | // like notifyAutofillFailed(View view, int[] childrenIds); |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2101 | } |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 2102 | } |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 2103 | |
Felipe Leme | 1116652 | 2018-05-07 10:18:47 -0700 | [diff] [blame] | 2104 | mMetricsLogger.write(newLog(MetricsEvent.AUTOFILL_DATASET_APPLIED) |
Felipe Leme | b22d635 | 2017-09-08 20:03:53 -0700 | [diff] [blame] | 2105 | .addTaggedData(MetricsEvent.FIELD_AUTOFILL_NUM_VALUES, itemCount) |
Felipe Leme | 1116652 | 2018-05-07 10:18:47 -0700 | [diff] [blame] | 2106 | .addTaggedData(MetricsEvent.FIELD_AUTOFILL_NUM_VIEWS_FILLED, numApplied)); |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2107 | } |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 2108 | } |
| 2109 | |
Felipe Leme | 1116652 | 2018-05-07 10:18:47 -0700 | [diff] [blame] | 2110 | private LogMaker newLog(int category) { |
Felipe Leme | b838a09 | 2018-05-22 14:56:15 -0700 | [diff] [blame] | 2111 | final LogMaker log = new LogMaker(category) |
Felipe Leme | 833c99b | 2018-05-24 10:41:48 -0700 | [diff] [blame] | 2112 | .addTaggedData(MetricsEvent.FIELD_AUTOFILL_SESSION_ID, mSessionId); |
| 2113 | |
| 2114 | if (isCompatibilityModeEnabledLocked()) { |
| 2115 | log.addTaggedData(MetricsEvent.FIELD_AUTOFILL_COMPAT_MODE, 1); |
| 2116 | } |
Felipe Leme | b838a09 | 2018-05-22 14:56:15 -0700 | [diff] [blame] | 2117 | final AutofillClient client = getClient(); |
| 2118 | if (client == null) { |
| 2119 | // Client should never be null here, but it doesn't hurt to check... |
| 2120 | log.setPackageName(mContext.getPackageName()); |
| 2121 | } else { |
| 2122 | log.setComponentName(client.autofillClientGetComponentName()); |
| 2123 | } |
| 2124 | return log; |
Felipe Leme | 1116652 | 2018-05-07 10:18:47 -0700 | [diff] [blame] | 2125 | } |
| 2126 | |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2127 | /** |
| 2128 | * Set the tracked views. |
| 2129 | * |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 2130 | * @param trackedIds The views to be tracked. |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2131 | * @param saveOnAllViewsInvisible Finish the session once all tracked views are invisible. |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 2132 | * @param saveOnFinish Finish the session once the activity is finished. |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 2133 | * @param fillableIds Views that might anchor FillUI. |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 2134 | * @param saveTriggerId View that when clicked triggers commit(). |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2135 | */ |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 2136 | private void setTrackedViews(int sessionId, @Nullable AutofillId[] trackedIds, |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 2137 | boolean saveOnAllViewsInvisible, boolean saveOnFinish, |
| 2138 | @Nullable AutofillId[] fillableIds, @Nullable AutofillId saveTriggerId) { |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2139 | synchronized (mLock) { |
| 2140 | if (mEnabled && mSessionId == sessionId) { |
| 2141 | if (saveOnAllViewsInvisible) { |
| 2142 | mTrackedViews = new TrackedViews(trackedIds); |
| 2143 | } else { |
| 2144 | mTrackedViews = null; |
| 2145 | } |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 2146 | mSaveOnFinish = saveOnFinish; |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 2147 | if (fillableIds != null) { |
| 2148 | if (mFillableIds == null) { |
| 2149 | mFillableIds = new ArraySet<>(fillableIds.length); |
| 2150 | } |
| 2151 | for (AutofillId id : fillableIds) { |
| 2152 | mFillableIds.add(id); |
| 2153 | } |
| 2154 | if (sVerbose) { |
Felipe Leme | 27e1bf3 | 2018-07-10 15:45:43 -0700 | [diff] [blame] | 2155 | Log.v(TAG, "setTrackedViews(): fillableIds=" + Arrays.toString(fillableIds) |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 2156 | + ", mFillableIds" + mFillableIds); |
| 2157 | } |
| 2158 | } |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 2159 | |
| 2160 | if (mSaveTriggerId != null && !mSaveTriggerId.equals(saveTriggerId)) { |
| 2161 | // Turn off trigger on previous view id. |
| 2162 | setNotifyOnClickLocked(mSaveTriggerId, false); |
| 2163 | } |
| 2164 | |
| 2165 | if (saveTriggerId != null && !saveTriggerId.equals(mSaveTriggerId)) { |
| 2166 | // Turn on trigger on new view id. |
| 2167 | mSaveTriggerId = saveTriggerId; |
| 2168 | setNotifyOnClickLocked(mSaveTriggerId, true); |
| 2169 | } |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2170 | } |
| 2171 | } |
| 2172 | } |
| 2173 | |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 2174 | private void setNotifyOnClickLocked(@NonNull AutofillId id, boolean notify) { |
| 2175 | final View view = findView(id); |
| 2176 | if (view == null) { |
| 2177 | Log.w(TAG, "setNotifyOnClick(): invalid id: " + id); |
| 2178 | return; |
| 2179 | } |
| 2180 | view.setNotifyAutofillManagerOnClick(notify); |
| 2181 | } |
| 2182 | |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 2183 | private void setSaveUiState(int sessionId, boolean shown) { |
| 2184 | if (sDebug) Log.d(TAG, "setSaveUiState(" + sessionId + "): " + shown); |
| 2185 | synchronized (mLock) { |
| 2186 | if (mSessionId != NO_SESSION) { |
| 2187 | // Race condition: app triggered a new session after the previous session was |
| 2188 | // finished but before server called setSaveUiState() - need to cancel the new |
| 2189 | // session to avoid further inconsistent behavior. |
| 2190 | Log.w(TAG, "setSaveUiState(" + sessionId + ", " + shown |
| 2191 | + ") called on existing session " + mSessionId + "; cancelling it"); |
| 2192 | cancelSessionLocked(); |
| 2193 | } |
| 2194 | if (shown) { |
| 2195 | mSessionId = sessionId; |
| 2196 | mState = STATE_SHOWING_SAVE_UI; |
| 2197 | } else { |
| 2198 | mSessionId = NO_SESSION; |
| 2199 | mState = STATE_UNKNOWN; |
| 2200 | } |
| 2201 | } |
| 2202 | } |
| 2203 | |
Felipe Leme | 650f7ab | 2017-09-19 13:08:24 -0700 | [diff] [blame] | 2204 | /** |
| 2205 | * Marks the state of the session as finished. |
| 2206 | * |
| 2207 | * @param newState {@link #STATE_FINISHED} (because the autofill service returned a {@code null} |
Felipe Leme | 0c8ce32 | 2018-03-23 13:54:22 -0700 | [diff] [blame] | 2208 | * FillResponse), {@link #STATE_UNKNOWN} (because the session was removed), |
| 2209 | * {@link #STATE_UNKNOWN_COMPAT_MODE} (beucase the session was finished when the URL bar |
Felipe Leme | d9dc954 | 2018-09-19 11:54:28 -0700 | [diff] [blame] | 2210 | * changed on compat mode), {@link #STATE_UNKNOWN_FAILED} (because the session was finished |
| 2211 | * when the service failed to fullfil the request, or {@link #STATE_DISABLED_BY_SERVICE} |
| 2212 | * (because the autofill service or {@link #STATE_DISABLED_BY_SERVICE} (because the autofill |
| 2213 | * service disabled further autofill requests for the activity). |
Felipe Leme | 650f7ab | 2017-09-19 13:08:24 -0700 | [diff] [blame] | 2214 | */ |
| 2215 | private void setSessionFinished(int newState) { |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 2216 | synchronized (mLock) { |
Felipe Leme | 0c8ce32 | 2018-03-23 13:54:22 -0700 | [diff] [blame] | 2217 | if (sVerbose) { |
| 2218 | Log.v(TAG, "setSessionFinished(): from " + getStateAsStringLocked() + " to " |
| 2219 | + getStateAsString(newState)); |
| 2220 | } |
Felipe Leme | d9dc954 | 2018-09-19 11:54:28 -0700 | [diff] [blame] | 2221 | if (newState == STATE_UNKNOWN_COMPAT_MODE || newState == STATE_UNKNOWN_FAILED) { |
Felipe Leme | 0c8ce32 | 2018-03-23 13:54:22 -0700 | [diff] [blame] | 2222 | resetSessionLocked(/* resetEnteredIds= */ true); |
| 2223 | mState = STATE_UNKNOWN; |
| 2224 | } else { |
| 2225 | resetSessionLocked(/* resetEnteredIds= */ false); |
| 2226 | mState = newState; |
| 2227 | } |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 2228 | } |
| 2229 | } |
| 2230 | |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 2231 | /** |
| 2232 | * Gets a {@link AugmentedAutofillManagerClient} for this {@link AutofillManagerClient}. |
| 2233 | * |
| 2234 | * <p>These are 2 distinct objects because we need to restrict what the Augmented Autofill |
| 2235 | * service can do (which is defined by {@code IAugmentedAutofillManagerClient.aidl}). |
| 2236 | */ |
| 2237 | private void getAugmentedAutofillClient(@NonNull IResultReceiver result) { |
| 2238 | synchronized (mLock) { |
| 2239 | if (mAugmentedAutofillServiceClient == null) { |
| 2240 | mAugmentedAutofillServiceClient = new AugmentedAutofillManagerClient(this); |
| 2241 | } |
| 2242 | final Bundle resultData = new Bundle(); |
| 2243 | resultData.putBinder(EXTRA_AUGMENTED_AUTOFILL_CLIENT, |
| 2244 | mAugmentedAutofillServiceClient.asBinder()); |
| 2245 | |
| 2246 | try { |
| 2247 | result.send(0, resultData); |
| 2248 | } catch (RemoteException e) { |
| 2249 | Log.w(TAG, "Could not send AugmentedAutofillClient back: " + e); |
| 2250 | } |
| 2251 | } |
| 2252 | } |
| 2253 | |
Dake Gu | b0fa378 | 2018-02-26 12:25:14 -0800 | [diff] [blame] | 2254 | /** @hide */ |
| 2255 | public void requestHideFillUi() { |
| 2256 | requestHideFillUi(mIdShownFillUi, true); |
| 2257 | } |
| 2258 | |
| 2259 | private void requestHideFillUi(AutofillId id, boolean force) { |
| 2260 | final View anchor = id == null ? null : findView(id); |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 2261 | if (sVerbose) Log.v(TAG, "requestHideFillUi(" + id + "): anchor = " + anchor); |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 2262 | if (anchor == null) { |
Dake Gu | b0fa378 | 2018-02-26 12:25:14 -0800 | [diff] [blame] | 2263 | if (force) { |
| 2264 | // When user taps outside autofill window, force to close fill ui even id does |
| 2265 | // not match. |
| 2266 | AutofillClient client = getClient(); |
| 2267 | if (client != null) { |
| 2268 | client.autofillClientRequestHideFillUi(); |
| 2269 | } |
| 2270 | } |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 2271 | return; |
| 2272 | } |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 2273 | requestHideFillUi(id, anchor); |
| 2274 | } |
| 2275 | |
| 2276 | private void requestHideFillUi(AutofillId id, View anchor) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 2277 | |
| 2278 | AutofillCallback callback = null; |
| 2279 | synchronized (mLock) { |
Svet Ganov | 48f10a2 | 2017-04-26 18:49:30 -0700 | [diff] [blame] | 2280 | // We do not check the session id for two reasons: |
| 2281 | // 1. If local and remote session id are off sync the UI would be stuck shown |
| 2282 | // 2. There is a race between the user state being destroyed due the fill |
| 2283 | // service being uninstalled and the UI being dismissed. |
Felipe Leme | 637e05e | 2017-12-06 12:09:37 -0800 | [diff] [blame] | 2284 | AutofillClient client = getClient(); |
Svet Ganov | 48f10a2 | 2017-04-26 18:49:30 -0700 | [diff] [blame] | 2285 | if (client != null) { |
Dake Gu | b0fa378 | 2018-02-26 12:25:14 -0800 | [diff] [blame] | 2286 | if (client.autofillClientRequestHideFillUi()) { |
| 2287 | mIdShownFillUi = null; |
Svet Ganov | 48f10a2 | 2017-04-26 18:49:30 -0700 | [diff] [blame] | 2288 | callback = mCallback; |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2289 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 2290 | } |
| 2291 | } |
| 2292 | |
| 2293 | if (callback != null) { |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 2294 | if (id.isVirtualInt()) { |
| 2295 | callback.onAutofillEvent(anchor, id.getVirtualChildIntId(), |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 2296 | AutofillCallback.EVENT_INPUT_HIDDEN); |
| 2297 | } else { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 2298 | callback.onAutofillEvent(anchor, AutofillCallback.EVENT_INPUT_HIDDEN); |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 2299 | } |
| 2300 | } |
| 2301 | } |
| 2302 | |
Felipe Leme | 17292d1 | 2017-10-24 14:03:10 -0700 | [diff] [blame] | 2303 | private void notifyNoFillUi(int sessionId, AutofillId id, int sessionFinishedState) { |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 2304 | if (sVerbose) { |
| 2305 | Log.v(TAG, "notifyNoFillUi(): sessionId=" + sessionId + ", autofillId=" + id |
Felipe Leme | 17292d1 | 2017-10-24 14:03:10 -0700 | [diff] [blame] | 2306 | + ", sessionFinishedState=" + sessionFinishedState); |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 2307 | } |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 2308 | final View anchor = findView(id); |
| 2309 | if (anchor == null) { |
| 2310 | return; |
| 2311 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 2312 | |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2313 | AutofillCallback callback = null; |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 2314 | synchronized (mLock) { |
Felipe Leme | 637e05e | 2017-12-06 12:09:37 -0800 | [diff] [blame] | 2315 | if (mSessionId == sessionId && getClient() != null) { |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2316 | callback = mCallback; |
| 2317 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 2318 | } |
| 2319 | |
| 2320 | if (callback != null) { |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 2321 | if (id.isVirtualInt()) { |
| 2322 | callback.onAutofillEvent(anchor, id.getVirtualChildIntId(), |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 2323 | AutofillCallback.EVENT_INPUT_UNAVAILABLE); |
| 2324 | } else { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 2325 | callback.onAutofillEvent(anchor, AutofillCallback.EVENT_INPUT_UNAVAILABLE); |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 2326 | } |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 2327 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 2328 | |
Felipe Leme | 17292d1 | 2017-10-24 14:03:10 -0700 | [diff] [blame] | 2329 | if (sessionFinishedState != 0) { |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 2330 | // Callback call was "hijacked" to also update the session state. |
Felipe Leme | 17292d1 | 2017-10-24 14:03:10 -0700 | [diff] [blame] | 2331 | setSessionFinished(sessionFinishedState); |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 2332 | } |
| 2333 | } |
| 2334 | |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 2335 | /** |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 2336 | * Find a single view by its id. |
| 2337 | * |
| 2338 | * @param autofillId The autofill id of the view |
| 2339 | * |
| 2340 | * @return The view or {@code null} if view was not found |
| 2341 | */ |
| 2342 | private View findView(@NonNull AutofillId autofillId) { |
Felipe Leme | 637e05e | 2017-12-06 12:09:37 -0800 | [diff] [blame] | 2343 | final AutofillClient client = getClient(); |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 2344 | if (client != null) { |
| 2345 | return client.autofillClientFindViewByAutofillIdTraversal(autofillId); |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 2346 | } |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 2347 | return null; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 2348 | } |
| 2349 | |
Felipe Leme | bb81092 | 2017-04-25 15:54:06 -0700 | [diff] [blame] | 2350 | /** @hide */ |
| 2351 | public boolean hasAutofillFeature() { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 2352 | return mService != null; |
| 2353 | } |
| 2354 | |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 2355 | /** @hide */ |
| 2356 | public void onPendingSaveUi(int operation, IBinder token) { |
| 2357 | if (sVerbose) Log.v(TAG, "onPendingSaveUi(" + operation + "): " + token); |
| 2358 | |
| 2359 | synchronized (mLock) { |
| 2360 | try { |
| 2361 | mService.onPendingSaveUi(operation, token); |
| 2362 | } catch (RemoteException e) { |
| 2363 | e.rethrowFromSystemServer(); |
| 2364 | } |
| 2365 | } |
| 2366 | } |
| 2367 | |
| 2368 | /** @hide */ |
| 2369 | public void dump(String outerPrefix, PrintWriter pw) { |
| 2370 | pw.print(outerPrefix); pw.println("AutofillManager:"); |
| 2371 | final String pfx = outerPrefix + " "; |
| 2372 | pw.print(pfx); pw.print("sessionId: "); pw.println(mSessionId); |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 2373 | pw.print(pfx); pw.print("state: "); pw.println(getStateAsStringLocked()); |
Felipe Leme | 686128e | 2017-10-17 14:02:20 -0700 | [diff] [blame] | 2374 | pw.print(pfx); pw.print("context: "); pw.println(mContext); |
Felipe Leme | b546ca7 | 2018-08-15 08:44:12 -0700 | [diff] [blame] | 2375 | final AutofillClient client = getClient(); |
| 2376 | if (client != null) { |
| 2377 | pw.print(pfx); pw.print("client: "); pw.print(client); |
| 2378 | pw.print(" ("); pw.print(client.autofillClientGetActivityToken()); pw.println(')'); |
| 2379 | } |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 2380 | pw.print(pfx); pw.print("enabled: "); pw.println(mEnabled); |
| 2381 | pw.print(pfx); pw.print("hasService: "); pw.println(mService != null); |
| 2382 | pw.print(pfx); pw.print("hasCallback: "); pw.println(mCallback != null); |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 2383 | pw.print(pfx); pw.print("onInvisibleCalled "); pw.println(mOnInvisibleCalled); |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 2384 | pw.print(pfx); pw.print("last autofilled data: "); pw.println(mLastAutofilledData); |
Felipe Leme | f5e2630 | 2018-06-08 16:26:39 -0700 | [diff] [blame] | 2385 | pw.print(pfx); pw.print("id of last fill UI shown: "); pw.println(mIdShownFillUi); |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 2386 | pw.print(pfx); pw.print("tracked views: "); |
| 2387 | if (mTrackedViews == null) { |
| 2388 | pw.println("null"); |
| 2389 | } else { |
| 2390 | final String pfx2 = pfx + " "; |
| 2391 | pw.println(); |
| 2392 | pw.print(pfx2); pw.print("visible:"); pw.println(mTrackedViews.mVisibleTrackedIds); |
| 2393 | pw.print(pfx2); pw.print("invisible:"); pw.println(mTrackedViews.mInvisibleTrackedIds); |
| 2394 | } |
| 2395 | pw.print(pfx); pw.print("fillable ids: "); pw.println(mFillableIds); |
Felipe Leme | 9a15c0f | 2018-02-14 17:26:46 -0800 | [diff] [blame] | 2396 | pw.print(pfx); pw.print("entered ids: "); pw.println(mEnteredIds); |
Felipe Leme | a7de402 | 2019-02-19 17:16:45 -0800 | [diff] [blame] | 2397 | if (mEnteredForAugmentedAutofillIds != null) { |
| 2398 | pw.print(pfx); pw.print("entered ids for augmented autofill: "); |
| 2399 | pw.println(mEnteredForAugmentedAutofillIds); |
| 2400 | } |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 2401 | pw.print(pfx); pw.print("save trigger id: "); pw.println(mSaveTriggerId); |
| 2402 | pw.print(pfx); pw.print("save on finish(): "); pw.println(mSaveOnFinish); |
Felipe Leme | a4f39cd | 2019-02-19 15:08:59 -0800 | [diff] [blame] | 2403 | if (mOptions != null) { |
| 2404 | pw.print(pfx); pw.print("options: "); mOptions.dumpShort(pw); pw.println(); |
| 2405 | } |
Felipe Leme | b546ca7 | 2018-08-15 08:44:12 -0700 | [diff] [blame] | 2406 | pw.print(pfx); pw.print("compat mode enabled: "); |
| 2407 | synchronized (mLock) { |
| 2408 | if (mCompatibilityBridge != null) { |
| 2409 | final String pfx2 = pfx + " "; |
| 2410 | pw.println("true"); |
| 2411 | pw.print(pfx2); pw.print("windowId: "); |
| 2412 | pw.println(mCompatibilityBridge.mFocusedWindowId); |
| 2413 | pw.print(pfx2); pw.print("nodeId: "); |
| 2414 | pw.println(mCompatibilityBridge.mFocusedNodeId); |
| 2415 | pw.print(pfx2); pw.print("virtualId: "); |
| 2416 | pw.println(AccessibilityNodeInfo |
| 2417 | .getVirtualDescendantId(mCompatibilityBridge.mFocusedNodeId)); |
| 2418 | pw.print(pfx2); pw.print("focusedBounds: "); |
| 2419 | pw.println(mCompatibilityBridge.mFocusedBounds); |
| 2420 | } else { |
| 2421 | pw.println("false"); |
| 2422 | } |
| 2423 | } |
Felipe Leme | 51e29da | 2017-10-24 14:03:10 -0700 | [diff] [blame] | 2424 | pw.print(pfx); pw.print("debug: "); pw.print(sDebug); |
| 2425 | pw.print(" verbose: "); pw.println(sVerbose); |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 2426 | } |
| 2427 | |
Andreas Gampe | 3f24e69 | 2018-02-05 13:24:28 -0800 | [diff] [blame] | 2428 | @GuardedBy("mLock") |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 2429 | private String getStateAsStringLocked() { |
Felipe Leme | 0c8ce32 | 2018-03-23 13:54:22 -0700 | [diff] [blame] | 2430 | return getStateAsString(mState); |
| 2431 | } |
| 2432 | |
| 2433 | @NonNull |
| 2434 | private static String getStateAsString(int state) { |
| 2435 | switch (state) { |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 2436 | case STATE_UNKNOWN: |
Felipe Leme | 0c8ce32 | 2018-03-23 13:54:22 -0700 | [diff] [blame] | 2437 | return "UNKNOWN"; |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 2438 | case STATE_ACTIVE: |
Felipe Leme | 0c8ce32 | 2018-03-23 13:54:22 -0700 | [diff] [blame] | 2439 | return "ACTIVE"; |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 2440 | case STATE_FINISHED: |
Felipe Leme | 0c8ce32 | 2018-03-23 13:54:22 -0700 | [diff] [blame] | 2441 | return "FINISHED"; |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 2442 | case STATE_SHOWING_SAVE_UI: |
Felipe Leme | 0c8ce32 | 2018-03-23 13:54:22 -0700 | [diff] [blame] | 2443 | return "SHOWING_SAVE_UI"; |
Felipe Leme | 17292d1 | 2017-10-24 14:03:10 -0700 | [diff] [blame] | 2444 | case STATE_DISABLED_BY_SERVICE: |
Felipe Leme | 0c8ce32 | 2018-03-23 13:54:22 -0700 | [diff] [blame] | 2445 | return "DISABLED_BY_SERVICE"; |
| 2446 | case STATE_UNKNOWN_COMPAT_MODE: |
| 2447 | return "UNKNOWN_COMPAT_MODE"; |
Felipe Leme | d9dc954 | 2018-09-19 11:54:28 -0700 | [diff] [blame] | 2448 | case STATE_UNKNOWN_FAILED: |
| 2449 | return "UNKNOWN_FAILED"; |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 2450 | default: |
Felipe Leme | 0c8ce32 | 2018-03-23 13:54:22 -0700 | [diff] [blame] | 2451 | return "INVALID:" + state; |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 2452 | } |
| 2453 | } |
| 2454 | |
Felipe Leme | 559e21d | 2019-01-18 17:57:21 -0800 | [diff] [blame] | 2455 | /** @hide */ |
| 2456 | public static String getSmartSuggestionModeToString(@SmartSuggestionMode int flags) { |
Felipe Leme | cc51027 | 2019-02-07 14:56:27 -0800 | [diff] [blame] | 2457 | switch (flags) { |
| 2458 | case FLAG_SMART_SUGGESTION_OFF: |
| 2459 | return "OFF"; |
| 2460 | case FLAG_SMART_SUGGESTION_SYSTEM: |
| 2461 | return "SYSTEM"; |
| 2462 | default: |
| 2463 | return "INVALID:" + flags; |
| 2464 | } |
Felipe Leme | 559e21d | 2019-01-18 17:57:21 -0800 | [diff] [blame] | 2465 | } |
| 2466 | |
Andreas Gampe | 3f24e69 | 2018-02-05 13:24:28 -0800 | [diff] [blame] | 2467 | @GuardedBy("mLock") |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 2468 | private boolean isActiveLocked() { |
| 2469 | return mState == STATE_ACTIVE; |
| 2470 | } |
| 2471 | |
Andreas Gampe | 3f24e69 | 2018-02-05 13:24:28 -0800 | [diff] [blame] | 2472 | @GuardedBy("mLock") |
Felipe Leme | 3103c63 | 2017-12-18 15:05:14 -0800 | [diff] [blame] | 2473 | private boolean isDisabledByServiceLocked() { |
Felipe Leme | 17292d1 | 2017-10-24 14:03:10 -0700 | [diff] [blame] | 2474 | return mState == STATE_DISABLED_BY_SERVICE; |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 2475 | } |
| 2476 | |
Andreas Gampe | 3f24e69 | 2018-02-05 13:24:28 -0800 | [diff] [blame] | 2477 | @GuardedBy("mLock") |
Felipe Leme | 3103c63 | 2017-12-18 15:05:14 -0800 | [diff] [blame] | 2478 | private boolean isFinishedLocked() { |
| 2479 | return mState == STATE_FINISHED; |
| 2480 | } |
| 2481 | |
Felipe Leme | 9876a6f | 2017-05-30 15:47:28 -0700 | [diff] [blame] | 2482 | private void post(Runnable runnable) { |
Felipe Leme | 637e05e | 2017-12-06 12:09:37 -0800 | [diff] [blame] | 2483 | final AutofillClient client = getClient(); |
Felipe Leme | 9876a6f | 2017-05-30 15:47:28 -0700 | [diff] [blame] | 2484 | if (client == null) { |
| 2485 | if (sVerbose) Log.v(TAG, "ignoring post() because client is null"); |
| 2486 | return; |
| 2487 | } |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 2488 | client.autofillClientRunOnUiThread(runnable); |
| 2489 | } |
| 2490 | |
| 2491 | /** |
| 2492 | * Implementation of the accessibility based compatibility. |
| 2493 | */ |
| 2494 | private final class CompatibilityBridge implements AccessibilityManager.AccessibilityPolicy { |
| 2495 | @GuardedBy("mLock") |
| 2496 | private final Rect mFocusedBounds = new Rect(); |
| 2497 | @GuardedBy("mLock") |
| 2498 | private final Rect mTempBounds = new Rect(); |
| 2499 | |
| 2500 | @GuardedBy("mLock") |
| 2501 | private int mFocusedWindowId = AccessibilityWindowInfo.UNDEFINED_WINDOW_ID; |
| 2502 | @GuardedBy("mLock") |
| 2503 | private long mFocusedNodeId = AccessibilityNodeInfo.UNDEFINED_NODE_ID; |
| 2504 | |
| 2505 | // Need to report a fake service in case a11y clients check the service list |
| 2506 | @NonNull |
| 2507 | @GuardedBy("mLock") |
| 2508 | AccessibilityServiceInfo mCompatServiceInfo; |
| 2509 | |
| 2510 | CompatibilityBridge() { |
| 2511 | final AccessibilityManager am = AccessibilityManager.getInstance(mContext); |
| 2512 | am.setAccessibilityPolicy(this); |
| 2513 | } |
| 2514 | |
| 2515 | private AccessibilityServiceInfo getCompatServiceInfo() { |
| 2516 | synchronized (mLock) { |
| 2517 | if (mCompatServiceInfo != null) { |
| 2518 | return mCompatServiceInfo; |
| 2519 | } |
| 2520 | final Intent intent = new Intent(); |
| 2521 | intent.setComponent(new ComponentName("android", |
| 2522 | "com.android.server.autofill.AutofillCompatAccessibilityService")); |
| 2523 | final ResolveInfo resolveInfo = mContext.getPackageManager().resolveService( |
| 2524 | intent, PackageManager.MATCH_SYSTEM_ONLY | PackageManager.GET_META_DATA); |
| 2525 | try { |
| 2526 | mCompatServiceInfo = new AccessibilityServiceInfo(resolveInfo, mContext); |
| 2527 | } catch (XmlPullParserException | IOException e) { |
| 2528 | Log.e(TAG, "Cannot find compat autofill service:" + intent); |
| 2529 | throw new IllegalStateException("Cannot find compat autofill service"); |
| 2530 | } |
| 2531 | return mCompatServiceInfo; |
| 2532 | } |
| 2533 | } |
| 2534 | |
| 2535 | @Override |
| 2536 | public boolean isEnabled(boolean accessibilityEnabled) { |
| 2537 | return true; |
| 2538 | } |
| 2539 | |
| 2540 | @Override |
| 2541 | public int getRelevantEventTypes(int relevantEventTypes) { |
| 2542 | return relevantEventTypes | AccessibilityEvent.TYPE_VIEW_FOCUSED |
| 2543 | | AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED |
Felipe Leme | 54cc683 | 2018-03-06 12:54:31 -0800 | [diff] [blame] | 2544 | | AccessibilityEvent.TYPE_VIEW_CLICKED |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 2545 | | AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED; |
| 2546 | } |
| 2547 | |
| 2548 | @Override |
| 2549 | public List<AccessibilityServiceInfo> getInstalledAccessibilityServiceList( |
| 2550 | List<AccessibilityServiceInfo> installedServices) { |
| 2551 | if (installedServices == null) { |
| 2552 | installedServices = new ArrayList<>(); |
| 2553 | } |
| 2554 | installedServices.add(getCompatServiceInfo()); |
| 2555 | return installedServices; |
| 2556 | } |
| 2557 | |
| 2558 | @Override |
| 2559 | public List<AccessibilityServiceInfo> getEnabledAccessibilityServiceList( |
| 2560 | int feedbackTypeFlags, List<AccessibilityServiceInfo> enabledService) { |
| 2561 | if (enabledService == null) { |
| 2562 | enabledService = new ArrayList<>(); |
| 2563 | } |
| 2564 | enabledService.add(getCompatServiceInfo()); |
| 2565 | return enabledService; |
| 2566 | } |
| 2567 | |
| 2568 | @Override |
| 2569 | public AccessibilityEvent onAccessibilityEvent(AccessibilityEvent event, |
| 2570 | boolean accessibilityEnabled, int relevantEventTypes) { |
Felipe Leme | 5d35d3d | 2018-08-13 18:15:32 -0700 | [diff] [blame] | 2571 | final int type = event.getEventType(); |
| 2572 | if (sVerbose) { |
| 2573 | // NOTE: this is waaay spammy, but that's life. |
| 2574 | Log.v(TAG, "onAccessibilityEvent(" + AccessibilityEvent.eventTypeToString(type) |
Felipe Leme | 7d4461d | 2018-08-16 10:49:03 -0700 | [diff] [blame] | 2575 | + "): virtualId=" |
| 2576 | + AccessibilityNodeInfo.getVirtualDescendantId(event.getSourceNodeId()) |
| 2577 | + ", client=" + getClient()); |
Felipe Leme | 5d35d3d | 2018-08-13 18:15:32 -0700 | [diff] [blame] | 2578 | } |
| 2579 | switch (type) { |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 2580 | case AccessibilityEvent.TYPE_VIEW_FOCUSED: { |
| 2581 | synchronized (mLock) { |
| 2582 | if (mFocusedWindowId == event.getWindowId() |
| 2583 | && mFocusedNodeId == event.getSourceNodeId()) { |
| 2584 | return event; |
| 2585 | } |
| 2586 | if (mFocusedWindowId != AccessibilityWindowInfo.UNDEFINED_WINDOW_ID |
| 2587 | && mFocusedNodeId != AccessibilityNodeInfo.UNDEFINED_NODE_ID) { |
| 2588 | notifyViewExited(mFocusedWindowId, mFocusedNodeId); |
| 2589 | mFocusedWindowId = AccessibilityWindowInfo.UNDEFINED_WINDOW_ID; |
| 2590 | mFocusedNodeId = AccessibilityNodeInfo.UNDEFINED_NODE_ID; |
| 2591 | mFocusedBounds.set(0, 0, 0, 0); |
| 2592 | } |
| 2593 | final int windowId = event.getWindowId(); |
| 2594 | final long nodeId = event.getSourceNodeId(); |
| 2595 | if (notifyViewEntered(windowId, nodeId, mFocusedBounds)) { |
| 2596 | mFocusedWindowId = windowId; |
| 2597 | mFocusedNodeId = nodeId; |
| 2598 | } |
| 2599 | } |
| 2600 | } break; |
| 2601 | |
| 2602 | case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED: { |
| 2603 | synchronized (mLock) { |
| 2604 | if (mFocusedWindowId == event.getWindowId() |
| 2605 | && mFocusedNodeId == event.getSourceNodeId()) { |
| 2606 | notifyValueChanged(event.getWindowId(), event.getSourceNodeId()); |
| 2607 | } |
| 2608 | } |
| 2609 | } break; |
| 2610 | |
Felipe Leme | 54cc683 | 2018-03-06 12:54:31 -0800 | [diff] [blame] | 2611 | case AccessibilityEvent.TYPE_VIEW_CLICKED: { |
| 2612 | synchronized (mLock) { |
| 2613 | notifyViewClicked(event.getWindowId(), event.getSourceNodeId()); |
| 2614 | } |
| 2615 | } break; |
| 2616 | |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 2617 | case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED: { |
| 2618 | final AutofillClient client = getClient(); |
| 2619 | if (client != null) { |
| 2620 | synchronized (mLock) { |
| 2621 | if (client.autofillClientIsFillUiShowing()) { |
| 2622 | notifyViewEntered(mFocusedWindowId, mFocusedNodeId, mFocusedBounds); |
| 2623 | } |
| 2624 | updateTrackedViewsLocked(); |
| 2625 | } |
| 2626 | } |
| 2627 | } break; |
| 2628 | } |
| 2629 | |
| 2630 | return accessibilityEnabled ? event : null; |
| 2631 | } |
| 2632 | |
| 2633 | private boolean notifyViewEntered(int windowId, long nodeId, Rect focusedBounds) { |
| 2634 | final int virtualId = AccessibilityNodeInfo.getVirtualDescendantId(nodeId); |
| 2635 | if (!isVirtualNode(virtualId)) { |
| 2636 | return false; |
| 2637 | } |
| 2638 | final View view = findViewByAccessibilityId(windowId, nodeId); |
| 2639 | if (view == null) { |
| 2640 | return false; |
| 2641 | } |
| 2642 | final AccessibilityNodeInfo node = findVirtualNodeByAccessibilityId(view, virtualId); |
| 2643 | if (node == null) { |
| 2644 | return false; |
| 2645 | } |
| 2646 | if (!node.isEditable()) { |
| 2647 | return false; |
| 2648 | } |
| 2649 | final Rect newBounds = mTempBounds; |
| 2650 | node.getBoundsInScreen(newBounds); |
| 2651 | if (newBounds.equals(focusedBounds)) { |
| 2652 | return false; |
| 2653 | } |
| 2654 | focusedBounds.set(newBounds); |
| 2655 | AutofillManager.this.notifyViewEntered(view, virtualId, newBounds); |
| 2656 | return true; |
| 2657 | } |
| 2658 | |
| 2659 | private void notifyViewExited(int windowId, long nodeId) { |
| 2660 | final int virtualId = AccessibilityNodeInfo.getVirtualDescendantId(nodeId); |
| 2661 | if (!isVirtualNode(virtualId)) { |
| 2662 | return; |
| 2663 | } |
| 2664 | final View view = findViewByAccessibilityId(windowId, nodeId); |
| 2665 | if (view == null) { |
| 2666 | return; |
| 2667 | } |
| 2668 | AutofillManager.this.notifyViewExited(view, virtualId); |
| 2669 | } |
| 2670 | |
| 2671 | private void notifyValueChanged(int windowId, long nodeId) { |
| 2672 | final int virtualId = AccessibilityNodeInfo.getVirtualDescendantId(nodeId); |
| 2673 | if (!isVirtualNode(virtualId)) { |
| 2674 | return; |
| 2675 | } |
| 2676 | final View view = findViewByAccessibilityId(windowId, nodeId); |
| 2677 | if (view == null) { |
| 2678 | return; |
| 2679 | } |
| 2680 | final AccessibilityNodeInfo node = findVirtualNodeByAccessibilityId(view, virtualId); |
| 2681 | if (node == null) { |
| 2682 | return; |
| 2683 | } |
| 2684 | AutofillManager.this.notifyValueChanged(view, virtualId, |
| 2685 | AutofillValue.forText(node.getText())); |
| 2686 | } |
| 2687 | |
Felipe Leme | 54cc683 | 2018-03-06 12:54:31 -0800 | [diff] [blame] | 2688 | private void notifyViewClicked(int windowId, long nodeId) { |
| 2689 | final int virtualId = AccessibilityNodeInfo.getVirtualDescendantId(nodeId); |
| 2690 | if (!isVirtualNode(virtualId)) { |
| 2691 | return; |
| 2692 | } |
| 2693 | final View view = findViewByAccessibilityId(windowId, nodeId); |
| 2694 | if (view == null) { |
| 2695 | return; |
| 2696 | } |
| 2697 | final AccessibilityNodeInfo node = findVirtualNodeByAccessibilityId(view, virtualId); |
| 2698 | if (node == null) { |
| 2699 | return; |
| 2700 | } |
| 2701 | AutofillManager.this.notifyViewClicked(view, virtualId); |
| 2702 | } |
| 2703 | |
Andreas Gampe | 3f24e69 | 2018-02-05 13:24:28 -0800 | [diff] [blame] | 2704 | @GuardedBy("mLock") |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 2705 | private void updateTrackedViewsLocked() { |
| 2706 | if (mTrackedViews != null) { |
| 2707 | mTrackedViews.onVisibleForAutofillChangedLocked(); |
| 2708 | } |
| 2709 | } |
| 2710 | |
| 2711 | private View findViewByAccessibilityId(int windowId, long nodeId) { |
| 2712 | final AutofillClient client = getClient(); |
| 2713 | if (client == null) { |
| 2714 | return null; |
| 2715 | } |
| 2716 | final int viewId = AccessibilityNodeInfo.getAccessibilityViewId(nodeId); |
| 2717 | return client.autofillClientFindViewByAccessibilityIdTraversal(viewId, windowId); |
| 2718 | } |
| 2719 | |
| 2720 | private AccessibilityNodeInfo findVirtualNodeByAccessibilityId(View view, int virtualId) { |
| 2721 | final AccessibilityNodeProvider provider = view.getAccessibilityNodeProvider(); |
| 2722 | if (provider == null) { |
| 2723 | return null; |
| 2724 | } |
| 2725 | return provider.createAccessibilityNodeInfo(virtualId); |
| 2726 | } |
| 2727 | |
| 2728 | private boolean isVirtualNode(int nodeId) { |
| 2729 | return nodeId != AccessibilityNodeProvider.HOST_VIEW_ID |
| 2730 | && nodeId != AccessibilityNodeInfo.UNDEFINED_ITEM_ID; |
| 2731 | } |
Felipe Leme | 9876a6f | 2017-05-30 15:47:28 -0700 | [diff] [blame] | 2732 | } |
| 2733 | |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 2734 | /** |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2735 | * View tracking information. Once all tracked views become invisible the session is finished. |
| 2736 | */ |
| 2737 | private class TrackedViews { |
| 2738 | /** Visible tracked views */ |
| 2739 | @Nullable private ArraySet<AutofillId> mVisibleTrackedIds; |
| 2740 | |
| 2741 | /** Invisible tracked views */ |
| 2742 | @Nullable private ArraySet<AutofillId> mInvisibleTrackedIds; |
| 2743 | |
| 2744 | /** |
| 2745 | * Check if set is null or value is in set. |
| 2746 | * |
| 2747 | * @param set The set or null (== empty set) |
| 2748 | * @param value The value that might be in the set |
| 2749 | * |
| 2750 | * @return {@code true} iff set is not empty and value is in set |
| 2751 | */ |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 2752 | // TODO: move to Helper as static method |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2753 | private <T> boolean isInSet(@Nullable ArraySet<T> set, T value) { |
| 2754 | return set != null && set.contains(value); |
| 2755 | } |
| 2756 | |
| 2757 | /** |
| 2758 | * Add a value to a set. If set is null, create a new set. |
| 2759 | * |
| 2760 | * @param set The set or null (== empty set) |
| 2761 | * @param valueToAdd The value to add |
| 2762 | * |
| 2763 | * @return The set including the new value. If set was {@code null}, a set containing only |
| 2764 | * the new value. |
| 2765 | */ |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 2766 | // TODO: move to Helper as static method |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2767 | @NonNull |
| 2768 | private <T> ArraySet<T> addToSet(@Nullable ArraySet<T> set, T valueToAdd) { |
| 2769 | if (set == null) { |
| 2770 | set = new ArraySet<>(1); |
| 2771 | } |
| 2772 | |
| 2773 | set.add(valueToAdd); |
| 2774 | |
| 2775 | return set; |
| 2776 | } |
| 2777 | |
| 2778 | /** |
| 2779 | * Remove a value from a set. |
| 2780 | * |
| 2781 | * @param set The set or null (== empty set) |
| 2782 | * @param valueToRemove The value to remove |
| 2783 | * |
| 2784 | * @return The set without the removed value. {@code null} if set was null, or is empty |
| 2785 | * after removal. |
| 2786 | */ |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 2787 | // TODO: move to Helper as static method |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2788 | @Nullable |
| 2789 | private <T> ArraySet<T> removeFromSet(@Nullable ArraySet<T> set, T valueToRemove) { |
| 2790 | if (set == null) { |
| 2791 | return null; |
| 2792 | } |
| 2793 | |
| 2794 | set.remove(valueToRemove); |
| 2795 | |
| 2796 | if (set.isEmpty()) { |
| 2797 | return null; |
| 2798 | } |
| 2799 | |
| 2800 | return set; |
| 2801 | } |
| 2802 | |
| 2803 | /** |
| 2804 | * Set the tracked views. |
| 2805 | * |
| 2806 | * @param trackedIds The views to be tracked |
| 2807 | */ |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 2808 | TrackedViews(@Nullable AutofillId[] trackedIds) { |
Felipe Leme | 637e05e | 2017-12-06 12:09:37 -0800 | [diff] [blame] | 2809 | final AutofillClient client = getClient(); |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 2810 | if (!ArrayUtils.isEmpty(trackedIds) && client != null) { |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 2811 | final boolean[] isVisible; |
| 2812 | |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 2813 | if (client.autofillClientIsVisibleForAutofill()) { |
Felipe Leme | 5b32ebe | 2018-02-15 12:52:19 -0800 | [diff] [blame] | 2814 | if (sVerbose) Log.v(TAG, "client is visible, check tracked ids"); |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 2815 | isVisible = client.autofillClientGetViewVisibility(trackedIds); |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 2816 | } else { |
| 2817 | // All false |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 2818 | isVisible = new boolean[trackedIds.length]; |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 2819 | } |
| 2820 | |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 2821 | final int numIds = trackedIds.length; |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2822 | for (int i = 0; i < numIds; i++) { |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 2823 | final AutofillId id = trackedIds[i]; |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2824 | |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 2825 | if (isVisible[i]) { |
Philip P. Moltmann | e0e2871 | 2017-04-20 15:19:06 -0700 | [diff] [blame] | 2826 | mVisibleTrackedIds = addToSet(mVisibleTrackedIds, id); |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2827 | } else { |
| 2828 | mInvisibleTrackedIds = addToSet(mInvisibleTrackedIds, id); |
| 2829 | } |
| 2830 | } |
| 2831 | } |
| 2832 | |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 2833 | if (sVerbose) { |
Felipe Leme | 5b32ebe | 2018-02-15 12:52:19 -0800 | [diff] [blame] | 2834 | Log.v(TAG, "TrackedViews(trackedIds=" + Arrays.toString(trackedIds) + "): " |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2835 | + " mVisibleTrackedIds=" + mVisibleTrackedIds |
| 2836 | + " mInvisibleTrackedIds=" + mInvisibleTrackedIds); |
| 2837 | } |
| 2838 | |
| 2839 | if (mVisibleTrackedIds == null) { |
| 2840 | finishSessionLocked(); |
| 2841 | } |
| 2842 | } |
| 2843 | |
| 2844 | /** |
| 2845 | * Called when a {@link View view's} visibility changes. |
| 2846 | * |
Svetoslav Ganov | 4db89ba | 2017-06-28 16:50:37 -0700 | [diff] [blame] | 2847 | * @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] | 2848 | * @param isVisible visible if the view is visible in the view hierarchy. |
| 2849 | */ |
Andreas Gampe | 3f24e69 | 2018-02-05 13:24:28 -0800 | [diff] [blame] | 2850 | @GuardedBy("mLock") |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 2851 | void notifyViewVisibilityChangedLocked(@NonNull AutofillId id, boolean isVisible) { |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 2852 | if (sDebug) { |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 2853 | Log.d(TAG, "notifyViewVisibilityChangedLocked(): id=" + id + " isVisible=" |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2854 | + isVisible); |
| 2855 | } |
| 2856 | |
Dake Gu | 67decfa | 2017-12-27 11:48:08 -0800 | [diff] [blame] | 2857 | if (isClientVisibleForAutofillLocked()) { |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2858 | if (isVisible) { |
| 2859 | if (isInSet(mInvisibleTrackedIds, id)) { |
| 2860 | mInvisibleTrackedIds = removeFromSet(mInvisibleTrackedIds, id); |
| 2861 | mVisibleTrackedIds = addToSet(mVisibleTrackedIds, id); |
| 2862 | } |
| 2863 | } else { |
| 2864 | if (isInSet(mVisibleTrackedIds, id)) { |
| 2865 | mVisibleTrackedIds = removeFromSet(mVisibleTrackedIds, id); |
| 2866 | mInvisibleTrackedIds = addToSet(mInvisibleTrackedIds, id); |
| 2867 | } |
| 2868 | } |
| 2869 | } |
| 2870 | |
| 2871 | if (mVisibleTrackedIds == null) { |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 2872 | if (sVerbose) { |
| 2873 | Log.v(TAG, "No more visible ids. Invisibile = " + mInvisibleTrackedIds); |
| 2874 | } |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2875 | finishSessionLocked(); |
| 2876 | } |
| 2877 | } |
| 2878 | |
| 2879 | /** |
| 2880 | * Called once the client becomes visible. |
| 2881 | * |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 2882 | * @see AutofillClient#autofillClientIsVisibleForAutofill() |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2883 | */ |
Andreas Gampe | 3f24e69 | 2018-02-05 13:24:28 -0800 | [diff] [blame] | 2884 | @GuardedBy("mLock") |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 2885 | void onVisibleForAutofillChangedLocked() { |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 2886 | // 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] | 2887 | // hence update the visibility state for all views. |
Felipe Leme | 637e05e | 2017-12-06 12:09:37 -0800 | [diff] [blame] | 2888 | AutofillClient client = getClient(); |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2889 | ArraySet<AutofillId> updatedVisibleTrackedIds = null; |
| 2890 | ArraySet<AutofillId> updatedInvisibleTrackedIds = null; |
| 2891 | if (client != null) { |
Felipe Leme | 7008e70 | 2018-03-16 18:02:16 -0700 | [diff] [blame] | 2892 | if (sVerbose) { |
| 2893 | Log.v(TAG, "onVisibleForAutofillChangedLocked(): inv= " + mInvisibleTrackedIds |
| 2894 | + " vis=" + mVisibleTrackedIds); |
| 2895 | } |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2896 | if (mInvisibleTrackedIds != null) { |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 2897 | final ArrayList<AutofillId> orderedInvisibleIds = |
| 2898 | new ArrayList<>(mInvisibleTrackedIds); |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 2899 | final boolean[] isVisible = client.autofillClientGetViewVisibility( |
| 2900 | Helper.toArray(orderedInvisibleIds)); |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 2901 | |
| 2902 | final int numInvisibleTrackedIds = orderedInvisibleIds.size(); |
| 2903 | for (int i = 0; i < numInvisibleTrackedIds; i++) { |
| 2904 | final AutofillId id = orderedInvisibleIds.get(i); |
| 2905 | if (isVisible[i]) { |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2906 | updatedVisibleTrackedIds = addToSet(updatedVisibleTrackedIds, id); |
| 2907 | |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 2908 | if (sDebug) { |
| 2909 | Log.d(TAG, "onVisibleForAutofill() " + id + " became visible"); |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2910 | } |
| 2911 | } else { |
| 2912 | updatedInvisibleTrackedIds = addToSet(updatedInvisibleTrackedIds, id); |
| 2913 | } |
| 2914 | } |
| 2915 | } |
| 2916 | |
| 2917 | if (mVisibleTrackedIds != null) { |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 2918 | final ArrayList<AutofillId> orderedVisibleIds = |
| 2919 | new ArrayList<>(mVisibleTrackedIds); |
Svetoslav Ganov | 24c9045 | 2017-12-27 15:17:14 -0800 | [diff] [blame] | 2920 | final boolean[] isVisible = client.autofillClientGetViewVisibility( |
| 2921 | Helper.toArray(orderedVisibleIds)); |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 2922 | |
| 2923 | final int numVisibleTrackedIds = orderedVisibleIds.size(); |
| 2924 | for (int i = 0; i < numVisibleTrackedIds; i++) { |
| 2925 | final AutofillId id = orderedVisibleIds.get(i); |
| 2926 | |
| 2927 | if (isVisible[i]) { |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2928 | updatedVisibleTrackedIds = addToSet(updatedVisibleTrackedIds, id); |
| 2929 | } else { |
| 2930 | updatedInvisibleTrackedIds = addToSet(updatedInvisibleTrackedIds, id); |
| 2931 | |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 2932 | if (sDebug) { |
| 2933 | Log.d(TAG, "onVisibleForAutofill() " + id + " became invisible"); |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2934 | } |
| 2935 | } |
| 2936 | } |
| 2937 | } |
| 2938 | |
| 2939 | mInvisibleTrackedIds = updatedInvisibleTrackedIds; |
| 2940 | mVisibleTrackedIds = updatedVisibleTrackedIds; |
| 2941 | } |
| 2942 | |
| 2943 | if (mVisibleTrackedIds == null) { |
Felipe Leme | 5b32ebe | 2018-02-15 12:52:19 -0800 | [diff] [blame] | 2944 | if (sVerbose) { |
| 2945 | Log.v(TAG, "onVisibleForAutofillChangedLocked(): no more visible ids"); |
| 2946 | } |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 2947 | finishSessionLocked(); |
| 2948 | } |
| 2949 | } |
| 2950 | } |
| 2951 | |
| 2952 | /** |
Felipe Leme | 744976e | 2017-07-31 11:34:14 -0700 | [diff] [blame] | 2953 | * Callback for autofill related events. |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 2954 | * |
| 2955 | * <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] | 2956 | * enable / disable such views when the autofill UI is shown / hidden. |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 2957 | */ |
| 2958 | public abstract static class AutofillCallback { |
| 2959 | |
| 2960 | /** @hide */ |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 2961 | @IntDef(prefix = { "EVENT_INPUT_" }, value = { |
| 2962 | EVENT_INPUT_SHOWN, |
| 2963 | EVENT_INPUT_HIDDEN, |
| 2964 | EVENT_INPUT_UNAVAILABLE |
| 2965 | }) |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 2966 | @Retention(RetentionPolicy.SOURCE) |
| 2967 | public @interface AutofillEventType {} |
| 2968 | |
| 2969 | /** |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 2970 | * The autofill input UI associated with the view was shown. |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 2971 | * |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 2972 | * <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] | 2973 | * should be hidden upon receiving this event. |
| 2974 | */ |
| 2975 | public static final int EVENT_INPUT_SHOWN = 1; |
| 2976 | |
| 2977 | /** |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 2978 | * The autofill input UI associated with the view was hidden. |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 2979 | * |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 2980 | * <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] | 2981 | * {@link #EVENT_INPUT_SHOWN} event, it could be shown again now. |
| 2982 | */ |
| 2983 | public static final int EVENT_INPUT_HIDDEN = 2; |
| 2984 | |
| 2985 | /** |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 2986 | * The autofill input UI associated with the view isn't shown because |
Felipe Leme | 24aae15 | 2017-03-15 12:33:01 -0700 | [diff] [blame] | 2987 | * autofill is not available. |
| 2988 | * |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 2989 | * <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] | 2990 | * to avoid flickering, it could shown it upon receiving this event. |
| 2991 | */ |
| 2992 | public static final int EVENT_INPUT_UNAVAILABLE = 3; |
| 2993 | |
| 2994 | /** |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 2995 | * Called after a change in the autofill state associated with a view. |
| 2996 | * |
| 2997 | * @param view view associated with the change. |
| 2998 | * |
| 2999 | * @param event currently either {@link #EVENT_INPUT_SHOWN} or {@link #EVENT_INPUT_HIDDEN}. |
| 3000 | */ |
Felipe Leme | 81f01d9 | 2017-03-16 17:13:25 -0700 | [diff] [blame] | 3001 | public void onAutofillEvent(@NonNull View view, @AutofillEventType int event) { |
| 3002 | } |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 3003 | |
| 3004 | /** |
| 3005 | * Called after a change in the autofill state associated with a virtual view. |
| 3006 | * |
| 3007 | * @param view parent view associated with the change. |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 3008 | * @param virtualId id identifying the virtual child inside the parent view. |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 3009 | * |
| 3010 | * @param event currently either {@link #EVENT_INPUT_SHOWN} or {@link #EVENT_INPUT_HIDDEN}. |
| 3011 | */ |
Felipe Leme | 6dcec87 | 2017-05-25 11:24:23 -0700 | [diff] [blame] | 3012 | public void onAutofillEvent(@NonNull View view, int virtualId, |
| 3013 | @AutofillEventType int event) { |
Felipe Leme | 81f01d9 | 2017-03-16 17:13:25 -0700 | [diff] [blame] | 3014 | } |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 3015 | } |
| 3016 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 3017 | private static final class AutofillManagerClient extends IAutoFillManagerClient.Stub { |
| 3018 | private final WeakReference<AutofillManager> mAfm; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 3019 | |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 3020 | private AutofillManagerClient(AutofillManager autofillManager) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 3021 | mAfm = new WeakReference<>(autofillManager); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 3022 | } |
| 3023 | |
| 3024 | @Override |
Felipe Leme | 51e29da | 2017-10-24 14:03:10 -0700 | [diff] [blame] | 3025 | public void setState(int flags) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 3026 | final AutofillManager afm = mAfm.get(); |
| 3027 | if (afm != null) { |
Felipe Leme | 51e29da | 2017-10-24 14:03:10 -0700 | [diff] [blame] | 3028 | afm.post(() -> afm.setState(flags)); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 3029 | } |
| 3030 | } |
| 3031 | |
| 3032 | @Override |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 3033 | public void autofill(int sessionId, List<AutofillId> ids, List<AutofillValue> values) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 3034 | final AutofillManager afm = mAfm.get(); |
| 3035 | if (afm != null) { |
Felipe Leme | 9876a6f | 2017-05-30 15:47:28 -0700 | [diff] [blame] | 3036 | afm.post(() -> afm.autofill(sessionId, ids, values)); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 3037 | } |
| 3038 | } |
| 3039 | |
| 3040 | @Override |
Svetoslav Ganov | a9379d0 | 2017-05-09 17:40:24 -0700 | [diff] [blame] | 3041 | public void authenticate(int sessionId, int authenticationId, IntentSender intent, |
| 3042 | Intent fillInIntent) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 3043 | final AutofillManager afm = mAfm.get(); |
| 3044 | if (afm != null) { |
Felipe Leme | 9876a6f | 2017-05-30 15:47:28 -0700 | [diff] [blame] | 3045 | afm.post(() -> afm.authenticate(sessionId, authenticationId, intent, fillInIntent)); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 3046 | } |
| 3047 | } |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 3048 | |
| 3049 | @Override |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 3050 | public void requestShowFillUi(int sessionId, AutofillId id, int width, int height, |
| 3051 | Rect anchorBounds, IAutofillWindowPresenter presenter) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 3052 | final AutofillManager afm = mAfm.get(); |
| 3053 | if (afm != null) { |
Felipe Leme | 9876a6f | 2017-05-30 15:47:28 -0700 | [diff] [blame] | 3054 | afm.post(() -> afm.requestShowFillUi(sessionId, id, width, height, anchorBounds, |
| 3055 | presenter)); |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 3056 | } |
| 3057 | } |
| 3058 | |
| 3059 | @Override |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 3060 | public void requestHideFillUi(int sessionId, AutofillId id) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 3061 | final AutofillManager afm = mAfm.get(); |
| 3062 | if (afm != null) { |
Dake Gu | b0fa378 | 2018-02-26 12:25:14 -0800 | [diff] [blame] | 3063 | afm.post(() -> afm.requestHideFillUi(id, false)); |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 3064 | } |
| 3065 | } |
| 3066 | |
| 3067 | @Override |
Felipe Leme | 17292d1 | 2017-10-24 14:03:10 -0700 | [diff] [blame] | 3068 | public void notifyNoFillUi(int sessionId, AutofillId id, int sessionFinishedState) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 3069 | final AutofillManager afm = mAfm.get(); |
| 3070 | if (afm != null) { |
Felipe Leme | 17292d1 | 2017-10-24 14:03:10 -0700 | [diff] [blame] | 3071 | afm.post(() -> afm.notifyNoFillUi(sessionId, id, sessionFinishedState)); |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 3072 | } |
| 3073 | } |
Svet Ganov | c3d1c85 | 2017-04-12 22:34:28 -0700 | [diff] [blame] | 3074 | |
| 3075 | @Override |
Dake Gu | 6a20a19 | 2018-02-08 12:09:30 -0800 | [diff] [blame] | 3076 | public void dispatchUnhandledKey(int sessionId, AutofillId id, KeyEvent fullScreen) { |
| 3077 | final AutofillManager afm = mAfm.get(); |
| 3078 | if (afm != null) { |
| 3079 | afm.post(() -> afm.dispatchUnhandledKey(sessionId, id, fullScreen)); |
| 3080 | } |
| 3081 | } |
| 3082 | |
| 3083 | @Override |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 3084 | public void startIntentSender(IntentSender intentSender, Intent intent) { |
Svet Ganov | c3d1c85 | 2017-04-12 22:34:28 -0700 | [diff] [blame] | 3085 | final AutofillManager afm = mAfm.get(); |
| 3086 | if (afm != null) { |
Felipe Leme | 9876a6f | 2017-05-30 15:47:28 -0700 | [diff] [blame] | 3087 | afm.post(() -> { |
Svet Ganov | c3d1c85 | 2017-04-12 22:34:28 -0700 | [diff] [blame] | 3088 | try { |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 3089 | afm.mContext.startIntentSender(intentSender, intent, 0, 0, 0); |
Svet Ganov | c3d1c85 | 2017-04-12 22:34:28 -0700 | [diff] [blame] | 3090 | } catch (IntentSender.SendIntentException e) { |
| 3091 | Log.e(TAG, "startIntentSender() failed for intent:" + intentSender, e); |
| 3092 | } |
| 3093 | }); |
| 3094 | } |
| 3095 | } |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 3096 | |
| 3097 | @Override |
Felipe Leme | 27e2022 | 2017-05-18 15:24:11 -0700 | [diff] [blame] | 3098 | public void setTrackedViews(int sessionId, AutofillId[] ids, |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 3099 | boolean saveOnAllViewsInvisible, boolean saveOnFinish, AutofillId[] fillableIds, |
| 3100 | AutofillId saveTriggerId) { |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 3101 | final AutofillManager afm = mAfm.get(); |
| 3102 | if (afm != null) { |
Felipe Leme | 2fe3ade | 2017-09-28 15:03:36 -0700 | [diff] [blame] | 3103 | afm.post(() -> afm.setTrackedViews(sessionId, ids, saveOnAllViewsInvisible, |
| 3104 | saveOnFinish, fillableIds, saveTriggerId)); |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 3105 | } |
| 3106 | } |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 3107 | |
| 3108 | @Override |
| 3109 | public void setSaveUiState(int sessionId, boolean shown) { |
| 3110 | final AutofillManager afm = mAfm.get(); |
| 3111 | if (afm != null) { |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 3112 | afm.post(() -> afm.setSaveUiState(sessionId, shown)); |
| 3113 | } |
| 3114 | } |
| 3115 | |
| 3116 | @Override |
Felipe Leme | 650f7ab | 2017-09-19 13:08:24 -0700 | [diff] [blame] | 3117 | public void setSessionFinished(int newState) { |
Felipe Leme | c7b4529 | 2017-09-19 09:06:20 -0700 | [diff] [blame] | 3118 | final AutofillManager afm = mAfm.get(); |
| 3119 | if (afm != null) { |
Felipe Leme | 650f7ab | 2017-09-19 13:08:24 -0700 | [diff] [blame] | 3120 | afm.post(() -> afm.setSessionFinished(newState)); |
Felipe Leme | c24a56a | 2017-08-03 14:27:57 -0700 | [diff] [blame] | 3121 | } |
| 3122 | } |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 3123 | |
| 3124 | @Override |
| 3125 | public void getAugmentedAutofillClient(IResultReceiver result) { |
| 3126 | final AutofillManager afm = mAfm.get(); |
| 3127 | if (afm != null) { |
| 3128 | afm.post(() -> afm.getAugmentedAutofillClient(result)); |
| 3129 | } |
| 3130 | } |
| 3131 | } |
| 3132 | |
| 3133 | private static final class AugmentedAutofillManagerClient |
| 3134 | extends IAugmentedAutofillManagerClient.Stub { |
| 3135 | private final WeakReference<AutofillManager> mAfm; |
| 3136 | |
| 3137 | private AugmentedAutofillManagerClient(AutofillManager autofillManager) { |
| 3138 | mAfm = new WeakReference<>(autofillManager); |
| 3139 | } |
| 3140 | |
| 3141 | @Override |
| 3142 | public Rect getViewCoordinates(@NonNull AutofillId id) { |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 3143 | final AutofillManager afm = mAfm.get(); |
| 3144 | if (afm == null) return null; |
| 3145 | |
| 3146 | final View view = afm.getClient().autofillClientFindViewByAutofillIdTraversal(id); |
Feng Cao | 023b84c | 2018-12-14 15:51:17 -0800 | [diff] [blame] | 3147 | final Rect windowVisibleDisplayFrame = new Rect(); |
| 3148 | view.getWindowVisibleDisplayFrame(windowVisibleDisplayFrame); |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 3149 | final int[] location = new int[2]; |
| 3150 | view.getLocationOnScreen(location); |
Feng Cao | 023b84c | 2018-12-14 15:51:17 -0800 | [diff] [blame] | 3151 | final Rect rect = new Rect(location[0], location[1] - windowVisibleDisplayFrame.top, |
| 3152 | location[0] + view.getWidth(), |
| 3153 | location[1] - windowVisibleDisplayFrame.top + view.getHeight()); |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 3154 | if (sVerbose) { |
| 3155 | Log.v(TAG, "Coordinates for " + id + ": " + rect); |
| 3156 | } |
| 3157 | return rect; |
| 3158 | } |
| 3159 | |
| 3160 | @Override |
| 3161 | public void autofill(int sessionId, List<AutofillId> ids, List<AutofillValue> values) { |
| 3162 | final AutofillManager afm = mAfm.get(); |
| 3163 | if (afm != null) { |
| 3164 | afm.post(() -> afm.autofill(sessionId, ids, values)); |
| 3165 | } |
| 3166 | } |
Feng Cao | 158b656 | 2019-01-07 15:42:52 -0800 | [diff] [blame] | 3167 | |
| 3168 | @Override |
| 3169 | public void requestShowFillUi(int sessionId, AutofillId id, int width, int height, |
| 3170 | Rect anchorBounds, IAutofillWindowPresenter presenter) { |
| 3171 | final AutofillManager afm = mAfm.get(); |
| 3172 | if (afm != null) { |
| 3173 | afm.post(() -> afm.requestShowFillUi(sessionId, id, width, height, anchorBounds, |
| 3174 | presenter)); |
| 3175 | } |
| 3176 | } |
| 3177 | |
| 3178 | @Override |
| 3179 | public void requestHideFillUi(int sessionId, AutofillId id) { |
| 3180 | final AutofillManager afm = mAfm.get(); |
| 3181 | if (afm != null) { |
| 3182 | afm.post(() -> afm.requestHideFillUi(id, false)); |
| 3183 | } |
| 3184 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 3185 | } |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 3186 | } |