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