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