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