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