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