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 | |
Felipe Leme | 73fedac | 2017-05-12 09:52:07 -0700 | [diff] [blame] | 19 | import static android.service.autofill.FillRequest.FLAG_MANUAL_REQUEST; |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 20 | import static android.view.autofill.Helper.sDebug; |
| 21 | import static android.view.autofill.Helper.sVerbose; |
Felipe Leme | d633f07 | 2017-02-14 10:17:17 -0800 | [diff] [blame] | 22 | |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 23 | import android.annotation.IntDef; |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 24 | import android.annotation.NonNull; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 25 | import android.annotation.Nullable; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 26 | import android.content.Context; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 27 | import android.content.Intent; |
| 28 | import android.content.IntentSender; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 29 | import android.graphics.Rect; |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 30 | import android.metrics.LogMaker; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 31 | import android.os.Bundle; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 32 | import android.os.Parcelable; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 33 | import android.os.RemoteException; |
Philip P. Moltmann | cc684ed | 2017-04-17 14:18:33 -0700 | [diff] [blame] | 34 | import android.service.autofill.AutofillService; |
| 35 | import android.service.autofill.FillEventHistory; |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 36 | import android.util.ArrayMap; |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 37 | import android.util.ArraySet; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 38 | import android.util.Log; |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 39 | import android.util.SparseArray; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 40 | import android.view.View; |
| 41 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 42 | import com.android.internal.annotations.GuardedBy; |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 43 | import com.android.internal.logging.MetricsLogger; |
| 44 | import com.android.internal.logging.nano.MetricsProto; |
| 45 | |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 46 | import java.lang.annotation.Retention; |
| 47 | import java.lang.annotation.RetentionPolicy; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 48 | import java.lang.ref.WeakReference; |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 49 | import java.util.ArrayList; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 50 | import java.util.List; |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 51 | import java.util.Objects; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 52 | |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 53 | /** |
Felipe Leme | 85d1c2d | 2017-04-21 08:56:04 -0700 | [diff] [blame] | 54 | * App entry point to the Autofill Framework. |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 55 | * |
| 56 | * <p>It is safe to call into this from any thread. |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 57 | */ |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 58 | public final class AutofillManager { |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 59 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 60 | private static final String TAG = "AutofillManager"; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 61 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 62 | /** |
| 63 | * Intent extra: The assist structure which captures the filled screen. |
Felipe Leme | 7320ca9 | 2017-03-29 15:09:54 -0700 | [diff] [blame] | 64 | * |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 65 | * <p> |
| 66 | * Type: {@link android.app.assist.AssistStructure} |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 67 | */ |
| 68 | public static final String EXTRA_ASSIST_STRUCTURE = |
| 69 | "android.view.autofill.extra.ASSIST_STRUCTURE"; |
| 70 | |
| 71 | /** |
| 72 | * Intent extra: The result of an authentication operation. It is |
| 73 | * either a fully populated {@link android.service.autofill.FillResponse} |
| 74 | * or a fully populated {@link android.service.autofill.Dataset} if |
| 75 | * a response or a dataset is being authenticated respectively. |
| 76 | * |
| 77 | * <p> |
| 78 | * Type: {@link android.service.autofill.FillResponse} or a |
| 79 | * {@link android.service.autofill.Dataset} |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 80 | */ |
| 81 | public static final String EXTRA_AUTHENTICATION_RESULT = |
| 82 | "android.view.autofill.extra.AUTHENTICATION_RESULT"; |
| 83 | |
Felipe Leme | 7320ca9 | 2017-03-29 15:09:54 -0700 | [diff] [blame] | 84 | /** |
| 85 | * Intent extra: The optional extras provided by the |
| 86 | * {@link android.service.autofill.AutofillService}. |
| 87 | * |
| 88 | * <p>For example, when the service responds to a {@link |
| 89 | * android.service.autofill.FillCallback#onSuccess(android.service.autofill.FillResponse)} with |
| 90 | * a {@code FillResponse} that requires authentication, the Intent that launches the |
| 91 | * service authentication will contain the Bundle set by |
Felipe Leme | 49e9696 | 2017-04-20 15:46:18 -0700 | [diff] [blame] | 92 | * {@link android.service.autofill.FillResponse.Builder#setClientState(Bundle)} on this extra. |
Felipe Leme | 7320ca9 | 2017-03-29 15:09:54 -0700 | [diff] [blame] | 93 | * |
| 94 | * <p> |
| 95 | * Type: {@link android.os.Bundle} |
| 96 | */ |
Felipe Leme | 37a440fd | 2017-04-28 10:50:20 -0700 | [diff] [blame] | 97 | public static final String EXTRA_CLIENT_STATE = |
Jeff Sharkey | 000ce80 | 2017-04-29 13:13:27 -0600 | [diff] [blame] | 98 | "android.view.autofill.extra.CLIENT_STATE"; |
Felipe Leme | 7320ca9 | 2017-03-29 15:09:54 -0700 | [diff] [blame] | 99 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 100 | static final String SESSION_ID_TAG = "android:sessionId"; |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 101 | static final String LAST_AUTOFILLED_DATA_TAG = "android:lastAutoFilledData"; |
| 102 | |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 103 | /** @hide */ public static final int ACTION_START_SESSION = 1; |
| 104 | /** @hide */ public static final int ACTION_VIEW_ENTERED = 2; |
| 105 | /** @hide */ public static final int ACTION_VIEW_EXITED = 3; |
| 106 | /** @hide */ public static final int ACTION_VALUE_CHANGED = 4; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 107 | |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 108 | |
| 109 | /** @hide */ public static final int FLAG_ADD_CLIENT_ENABLED = 0x1; |
| 110 | /** @hide */ public static final int FLAG_ADD_CLIENT_DEBUG = 0x2; |
| 111 | /** @hide */ public static final int FLAG_ADD_CLIENT_VERBOSE = 0x4; |
| 112 | |
Svetoslav Ganov | a9379d0 | 2017-05-09 17:40:24 -0700 | [diff] [blame] | 113 | /** Which bits in an authentication id are used for the dataset id */ |
| 114 | private static final int AUTHENTICATION_ID_DATASET_ID_MASK = 0xFFFF; |
| 115 | /** How many bits in an authentication id are used for the dataset id */ |
| 116 | private static final int AUTHENTICATION_ID_DATASET_ID_SHIFT = 16; |
| 117 | /** @hide The index for an undefined data set */ |
| 118 | public static final int AUTHENTICATION_ID_DATASET_ID_UNDEFINED = 0xFFFF; |
| 119 | |
| 120 | /** |
| 121 | * Makes an authentication id from a request id and a dataset id. |
| 122 | * |
| 123 | * @param requestId The request id. |
| 124 | * @param datasetId The dataset id. |
| 125 | * @return The authentication id. |
| 126 | * @hide |
| 127 | */ |
| 128 | public static int makeAuthenticationId(int requestId, int datasetId) { |
| 129 | return (requestId << AUTHENTICATION_ID_DATASET_ID_SHIFT) |
| 130 | | (datasetId & AUTHENTICATION_ID_DATASET_ID_MASK); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Gets the request id from an authentication id. |
| 135 | * |
| 136 | * @param authRequestId The authentication id. |
| 137 | * @return The request id. |
| 138 | * @hide |
| 139 | */ |
| 140 | public static int getRequestIdFromAuthenticationId(int authRequestId) { |
| 141 | return (authRequestId >> AUTHENTICATION_ID_DATASET_ID_SHIFT); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Gets the dataset id from an authentication id. |
| 146 | * |
| 147 | * @param authRequestId The authentication id. |
| 148 | * @return The dataset id. |
| 149 | * @hide |
| 150 | */ |
| 151 | public static int getDatasetIdFromAuthenticationId(int authRequestId) { |
| 152 | return (authRequestId & AUTHENTICATION_ID_DATASET_ID_MASK); |
| 153 | } |
| 154 | |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 155 | private final MetricsLogger mMetricsLogger = new MetricsLogger(); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 156 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 157 | /** |
| 158 | * There is currently no session running. |
| 159 | * {@hide} |
| 160 | */ |
| 161 | public static final int NO_SESSION = Integer.MIN_VALUE; |
| 162 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 163 | private final IAutoFillManager mService; |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 164 | |
| 165 | private final Object mLock = new Object(); |
| 166 | |
| 167 | @GuardedBy("mLock") |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 168 | private IAutoFillManagerClient mServiceClient; |
| 169 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 170 | @GuardedBy("mLock") |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 171 | private AutofillCallback mCallback; |
| 172 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 173 | private final Context mContext; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 174 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 175 | @GuardedBy("mLock") |
| 176 | private int mSessionId = NO_SESSION; |
| 177 | |
| 178 | @GuardedBy("mLock") |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 179 | private boolean mEnabled; |
| 180 | |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 181 | /** 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] | 182 | @GuardedBy("mLock") |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 183 | @Nullable private ParcelableMap mLastAutofilledData; |
| 184 | |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 185 | /** If view tracking is enabled, contains the tracking state */ |
| 186 | @GuardedBy("mLock") |
| 187 | @Nullable private TrackedViews mTrackedViews; |
| 188 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 189 | /** @hide */ |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 190 | public interface AutofillClient { |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 191 | /** |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 192 | * Asks the client to start an authentication flow. |
| 193 | * |
Svetoslav Ganov | a9379d0 | 2017-05-09 17:40:24 -0700 | [diff] [blame] | 194 | * @param authenticationId A unique id of the authentication operation. |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 195 | * @param intent The authentication intent. |
| 196 | * @param fillInIntent The authentication fill-in intent. |
| 197 | */ |
Svetoslav Ganov | a9379d0 | 2017-05-09 17:40:24 -0700 | [diff] [blame] | 198 | void autofillCallbackAuthenticate(int authenticationId, IntentSender intent, |
| 199 | Intent fillInIntent); |
Svet Ganov | 17db9dc | 2017-02-21 19:54:31 -0800 | [diff] [blame] | 200 | |
| 201 | /** |
| 202 | * Tells the client this manager has state to be reset. |
| 203 | */ |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 204 | void autofillCallbackResetableStateAvailable(); |
| 205 | |
| 206 | /** |
| 207 | * Request showing the autofill UI. |
| 208 | * |
| 209 | * @param anchor The real view the UI needs to anchor to. |
| 210 | * @param width The width of the fill UI content. |
| 211 | * @param height The height of the fill UI content. |
| 212 | * @param virtualBounds The bounds of the virtual decendant of the anchor. |
| 213 | * @param presenter The presenter that controls the fill UI window. |
| 214 | * @return Whether the UI was shown. |
| 215 | */ |
| 216 | boolean autofillCallbackRequestShowFillUi(@NonNull View anchor, int width, int height, |
| 217 | @Nullable Rect virtualBounds, IAutofillWindowPresenter presenter); |
| 218 | |
| 219 | /** |
| 220 | * Request hiding the autofill UI. |
| 221 | * |
| 222 | * @return Whether the UI was hidden. |
| 223 | */ |
| 224 | boolean autofillCallbackRequestHideFillUi(); |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 225 | |
| 226 | /** |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 227 | * Checks if views are currently attached and visible. |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 228 | * |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 229 | * @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] | 230 | */ |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 231 | @NonNull boolean[] getViewVisibility(@NonNull int[] viewId); |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 232 | |
| 233 | /** |
| 234 | * Checks is the client is currently visible as understood by autofill. |
| 235 | * |
| 236 | * @return {@code true} if the client is currently visible |
| 237 | */ |
| 238 | boolean isVisibleForAutofill(); |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 239 | |
| 240 | /** |
| 241 | * Find views by traversing the hierarchies of the client. |
| 242 | * |
| 243 | * @param viewIds The accessibility ids of the views to find |
| 244 | * |
| 245 | * @return And array containing the views, or {@code null} if not found |
| 246 | */ |
| 247 | @NonNull View[] findViewsByAccessibilityIdTraversal(@NonNull int[] viewIds); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 248 | } |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 249 | |
| 250 | /** |
| 251 | * @hide |
| 252 | */ |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 253 | public AutofillManager(Context context, IAutoFillManager service) { |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 254 | mContext = context; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 255 | mService = service; |
| 256 | } |
| 257 | |
| 258 | /** |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 259 | * Restore state after activity lifecycle |
| 260 | * |
| 261 | * @param savedInstanceState The state to be restored |
| 262 | * |
| 263 | * {@hide} |
| 264 | */ |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 265 | public void onCreate(Bundle savedInstanceState) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 266 | if (!hasAutofillFeature()) { |
| 267 | return; |
| 268 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 269 | synchronized (mLock) { |
| 270 | mLastAutofilledData = savedInstanceState.getParcelable(LAST_AUTOFILLED_DATA_TAG); |
| 271 | |
| 272 | if (mSessionId != NO_SESSION) { |
| 273 | Log.w(TAG, "New session was started before onCreate()"); |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | mSessionId = savedInstanceState.getInt(SESSION_ID_TAG, NO_SESSION); |
| 278 | |
| 279 | if (mSessionId != NO_SESSION) { |
| 280 | ensureServiceClientAddedIfNeededLocked(); |
| 281 | |
| 282 | final AutofillClient client = getClientLocked(); |
| 283 | if (client != null) { |
| 284 | try { |
| 285 | final boolean sessionWasRestored = mService.restoreSession(mSessionId, |
| 286 | mContext.getActivityToken(), mServiceClient.asBinder()); |
| 287 | |
| 288 | if (!sessionWasRestored) { |
| 289 | Log.w(TAG, "Session " + mSessionId + " could not be restored"); |
| 290 | mSessionId = NO_SESSION; |
| 291 | } else { |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 292 | if (sDebug) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 293 | Log.d(TAG, "session " + mSessionId + " was restored"); |
| 294 | } |
| 295 | |
| 296 | client.autofillCallbackResetableStateAvailable(); |
| 297 | } |
| 298 | } catch (RemoteException e) { |
| 299 | Log.e(TAG, "Could not figure out if there was an autofill session", e); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | /** |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 307 | * Called once the client becomes visible. |
| 308 | * |
| 309 | * @see AutofillClient#isVisibleForAutofill() |
| 310 | * |
| 311 | * {@hide} |
| 312 | */ |
| 313 | public void onVisibleForAutofill() { |
| 314 | synchronized (mLock) { |
| 315 | if (mEnabled && mSessionId != NO_SESSION && mTrackedViews != null) { |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 316 | mTrackedViews.onVisibleForAutofillLocked(); |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | /** |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 322 | * Save state before activity lifecycle |
| 323 | * |
| 324 | * @param outState Place to store the state |
| 325 | * |
| 326 | * {@hide} |
| 327 | */ |
| 328 | public void onSaveInstanceState(Bundle outState) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 329 | if (!hasAutofillFeature()) { |
| 330 | return; |
| 331 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 332 | synchronized (mLock) { |
| 333 | if (mSessionId != NO_SESSION) { |
| 334 | outState.putInt(SESSION_ID_TAG, mSessionId); |
| 335 | } |
| 336 | |
| 337 | if (mLastAutofilledData != null) { |
| 338 | outState.putParcelable(LAST_AUTOFILLED_DATA_TAG, mLastAutofilledData); |
| 339 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Checks whether autofill is enabled for the current user. |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 345 | * |
| 346 | * <p>Typically used to determine whether the option to explicitly request autofill should |
| 347 | * be offered - see {@link #requestAutofill(View)}. |
| 348 | * |
| 349 | * @return whether autofill is enabled for the current user. |
| 350 | */ |
| 351 | public boolean isEnabled() { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 352 | if (!hasAutofillFeature()) { |
| 353 | return false; |
| 354 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 355 | synchronized (mLock) { |
| 356 | ensureServiceClientAddedIfNeededLocked(); |
| 357 | return mEnabled; |
| 358 | } |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /** |
Philip P. Moltmann | cc684ed | 2017-04-17 14:18:33 -0700 | [diff] [blame] | 362 | * Should always be called from {@link AutofillService#getFillEventHistory()}. |
| 363 | * |
| 364 | * @hide |
| 365 | */ |
| 366 | @Nullable public FillEventHistory getFillEventHistory() { |
| 367 | try { |
| 368 | return mService.getFillEventHistory(); |
| 369 | } catch (RemoteException e) { |
| 370 | e.rethrowFromSystemServer(); |
| 371 | return null; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | /** |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 376 | * Explicitly requests a new autofill context. |
| 377 | * |
| 378 | * <p>Normally, the autofill context is automatically started when autofillable views are |
| 379 | * focused, but this method should be used in the cases where it must be explicitly requested, |
| 380 | * like a view that provides a contextual menu allowing users to autofill the activity. |
| 381 | * |
| 382 | * @param view view requesting the new autofill context. |
| 383 | */ |
| 384 | public void requestAutofill(@NonNull View view) { |
Felipe Leme | d114642 | 2017-04-26 10:17:05 -0700 | [diff] [blame] | 385 | notifyViewEntered(view, FLAG_MANUAL_REQUEST); |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Explicitly requests a new autofill context for virtual views. |
| 390 | * |
| 391 | * <p>Normally, the autofill context is automatically started when autofillable views are |
| 392 | * focused, but this method should be used in the cases where it must be explicitly requested, |
| 393 | * like a virtual view that provides a contextual menu allowing users to autofill the activity. |
| 394 | * |
| 395 | * @param view the {@link View} whose descendant is the virtual view. |
| 396 | * @param childId id identifying the virtual child inside the view. |
| 397 | * @param bounds child boundaries, relative to the top window. |
| 398 | */ |
| 399 | public void requestAutofill(@NonNull View view, int childId, @NonNull Rect bounds) { |
Felipe Leme | d114642 | 2017-04-26 10:17:05 -0700 | [diff] [blame] | 400 | notifyViewEntered(view, childId, bounds, FLAG_MANUAL_REQUEST); |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 403 | /** |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 404 | * Called when a {@link View} that supports autofill is entered. |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 405 | * |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 406 | * @param view {@link View} that was entered. |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 407 | */ |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 408 | public void notifyViewEntered(@NonNull View view) { |
Felipe Leme | d114642 | 2017-04-26 10:17:05 -0700 | [diff] [blame] | 409 | notifyViewEntered(view, 0); |
| 410 | } |
| 411 | |
| 412 | private void notifyViewEntered(@NonNull View view, int flags) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 413 | if (!hasAutofillFeature()) { |
| 414 | return; |
| 415 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 416 | AutofillCallback callback = null; |
| 417 | synchronized (mLock) { |
| 418 | ensureServiceClientAddedIfNeededLocked(); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 419 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 420 | if (!mEnabled) { |
| 421 | if (mCallback != null) { |
| 422 | callback = mCallback; |
| 423 | } |
| 424 | } else { |
| 425 | final AutofillId id = getAutofillId(view); |
| 426 | final AutofillValue value = view.getAutofillValue(); |
| 427 | |
| 428 | if (mSessionId == NO_SESSION) { |
| 429 | // Starts new session. |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 430 | startSessionLocked(id, null, value, flags); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 431 | } else { |
| 432 | // Update focus on existing session. |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 433 | updateSessionLocked(id, null, value, ACTION_VIEW_ENTERED, flags); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 434 | } |
Felipe Leme | 24aae15 | 2017-03-15 12:33:01 -0700 | [diff] [blame] | 435 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 436 | } |
| 437 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 438 | if (callback != null) { |
| 439 | mCallback.onAutofillEvent(view, AutofillCallback.EVENT_INPUT_UNAVAILABLE); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 440 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /** |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 444 | * Called when a {@link View} that supports autofill is exited. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 445 | * |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 446 | * @param view {@link View} that was exited. |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 447 | */ |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 448 | public void notifyViewExited(@NonNull View view) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 449 | if (!hasAutofillFeature()) { |
| 450 | return; |
| 451 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 452 | synchronized (mLock) { |
| 453 | ensureServiceClientAddedIfNeededLocked(); |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 454 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 455 | if (mEnabled && mSessionId != NO_SESSION) { |
| 456 | final AutofillId id = getAutofillId(view); |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 457 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 458 | // Update focus on existing session. |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 459 | updateSessionLocked(id, null, null, ACTION_VIEW_EXITED, 0); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 460 | } |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
| 464 | /** |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 465 | * Called when a {@link View view's} visibility changes. |
| 466 | * |
| 467 | * @param view {@link View} that was exited. |
| 468 | * @param isVisible visible if the view is visible in the view hierarchy. |
| 469 | * |
| 470 | * @hide |
| 471 | */ |
| 472 | public void notifyViewVisibilityChange(@NonNull View view, boolean isVisible) { |
| 473 | synchronized (mLock) { |
| 474 | if (mEnabled && mSessionId != NO_SESSION && mTrackedViews != null) { |
| 475 | mTrackedViews.notifyViewVisibilityChange(view, isVisible); |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | /** |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 481 | * Called when a virtual view that supports autofill is entered. |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 482 | * |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 483 | * @param view the {@link View} whose descendant is the virtual view. |
| 484 | * @param childId id identifying the virtual child inside the view. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 485 | * @param bounds child boundaries, relative to the top window. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 486 | */ |
Felipe Leme | 81f01d9 | 2017-03-16 17:13:25 -0700 | [diff] [blame] | 487 | public void notifyViewEntered(@NonNull View view, int childId, @NonNull Rect bounds) { |
Felipe Leme | d114642 | 2017-04-26 10:17:05 -0700 | [diff] [blame] | 488 | notifyViewEntered(view, childId, bounds, 0); |
| 489 | } |
| 490 | |
| 491 | private void notifyViewEntered(View view, int childId, Rect bounds, int flags) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 492 | if (!hasAutofillFeature()) { |
| 493 | return; |
| 494 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 495 | AutofillCallback callback = null; |
| 496 | synchronized (mLock) { |
| 497 | ensureServiceClientAddedIfNeededLocked(); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 498 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 499 | if (!mEnabled) { |
| 500 | if (mCallback != null) { |
| 501 | callback = mCallback; |
| 502 | } |
| 503 | } else { |
| 504 | final AutofillId id = getAutofillId(view, childId); |
| 505 | |
| 506 | if (mSessionId == NO_SESSION) { |
| 507 | // Starts new session. |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 508 | startSessionLocked(id, bounds, null, flags); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 509 | } else { |
| 510 | // Update focus on existing session. |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 511 | updateSessionLocked(id, bounds, null, ACTION_VIEW_ENTERED, flags); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 512 | } |
Felipe Leme | 24aae15 | 2017-03-15 12:33:01 -0700 | [diff] [blame] | 513 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 514 | } |
| 515 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 516 | if (callback != null) { |
| 517 | callback.onAutofillEvent(view, childId, |
| 518 | AutofillCallback.EVENT_INPUT_UNAVAILABLE); |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
| 522 | /** |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 523 | * Called when a virtual view that supports autofill is exited. |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 524 | * |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 525 | * @param view the {@link View} whose descendant is the virtual view. |
| 526 | * @param childId id identifying the virtual child inside the view. |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 527 | */ |
Felipe Leme | 81f01d9 | 2017-03-16 17:13:25 -0700 | [diff] [blame] | 528 | public void notifyViewExited(@NonNull View view, int childId) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 529 | if (!hasAutofillFeature()) { |
| 530 | return; |
| 531 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 532 | synchronized (mLock) { |
| 533 | ensureServiceClientAddedIfNeededLocked(); |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 534 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 535 | if (mEnabled && mSessionId != NO_SESSION) { |
| 536 | final AutofillId id = getAutofillId(view, childId); |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 537 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 538 | // Update focus on existing session. |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 539 | updateSessionLocked(id, null, null, ACTION_VIEW_EXITED, 0); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 540 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 541 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /** |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 545 | * Called to indicate the value of an autofillable {@link View} changed. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 546 | * |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 547 | * @param view view whose value changed. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 548 | */ |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 549 | public void notifyValueChanged(View view) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 550 | if (!hasAutofillFeature()) { |
| 551 | return; |
| 552 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 553 | AutofillId id = null; |
| 554 | boolean valueWasRead = false; |
| 555 | AutofillValue value = null; |
| 556 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 557 | synchronized (mLock) { |
| 558 | // If the session is gone some fields might still be highlighted, hence we have to |
| 559 | // remove the isAutofilled property even if no sessions are active. |
| 560 | if (mLastAutofilledData == null) { |
| 561 | view.setAutofilled(false); |
| 562 | } else { |
| 563 | id = getAutofillId(view); |
| 564 | if (mLastAutofilledData.containsKey(id)) { |
| 565 | value = view.getAutofillValue(); |
| 566 | valueWasRead = true; |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 567 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 568 | if (Objects.equals(mLastAutofilledData.get(id), value)) { |
| 569 | view.setAutofilled(true); |
| 570 | } else { |
| 571 | view.setAutofilled(false); |
| 572 | mLastAutofilledData.remove(id); |
| 573 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 574 | } else { |
| 575 | view.setAutofilled(false); |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 576 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 577 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 578 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 579 | if (!mEnabled || mSessionId == NO_SESSION) { |
| 580 | return; |
| 581 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 582 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 583 | if (id == null) { |
| 584 | id = getAutofillId(view); |
| 585 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 586 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 587 | if (!valueWasRead) { |
| 588 | value = view.getAutofillValue(); |
| 589 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 590 | |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 591 | updateSessionLocked(id, null, value, ACTION_VALUE_CHANGED, 0); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 592 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 593 | } |
| 594 | |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 595 | /** |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 596 | * Called to indicate the value of an autofillable virtual {@link View} changed. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 597 | * |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 598 | * @param view the {@link View} whose descendant is the virtual view. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 599 | * @param childId id identifying the virtual child inside the parent view. |
| 600 | * @param value new value of the child. |
| 601 | */ |
Felipe Leme | 81f01d9 | 2017-03-16 17:13:25 -0700 | [diff] [blame] | 602 | public void notifyValueChanged(View view, int childId, AutofillValue value) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 603 | if (!hasAutofillFeature()) { |
| 604 | return; |
| 605 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 606 | synchronized (mLock) { |
| 607 | if (!mEnabled || mSessionId == NO_SESSION) { |
| 608 | return; |
| 609 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 610 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 611 | final AutofillId id = getAutofillId(view, childId); |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 612 | updateSessionLocked(id, null, value, ACTION_VALUE_CHANGED, 0); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 613 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | /** |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 617 | * Called to indicate the current autofill context should be commited. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 618 | * |
| 619 | * <p>For example, when a virtual view is rendering an {@code HTML} page with a form, it should |
| 620 | * call this method after the form is submitted and another page is rendered. |
| 621 | */ |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 622 | public void commit() { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 623 | if (!hasAutofillFeature()) { |
| 624 | return; |
| 625 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 626 | synchronized (mLock) { |
| 627 | if (!mEnabled && mSessionId == NO_SESSION) { |
| 628 | return; |
| 629 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 630 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 631 | finishSessionLocked(); |
| 632 | } |
Felipe Leme | 0200d9e | 2017-01-24 15:10:26 -0800 | [diff] [blame] | 633 | } |
| 634 | |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 635 | /** |
| 636 | * Called to indicate the current autofill context should be cancelled. |
| 637 | * |
| 638 | * <p>For example, when a virtual view is rendering an {@code HTML} page with a form, it should |
| 639 | * call this method if the user does not post the form but moves to another form in this page. |
| 640 | */ |
| 641 | public void cancel() { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 642 | if (!hasAutofillFeature()) { |
| 643 | return; |
| 644 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 645 | synchronized (mLock) { |
| 646 | if (!mEnabled && mSessionId == NO_SESSION) { |
| 647 | return; |
| 648 | } |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 649 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 650 | cancelSessionLocked(); |
| 651 | } |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Svet Ganov | f965b87 | 2017-04-28 16:34:02 -0700 | [diff] [blame] | 654 | /** @hide */ |
| 655 | public void disableOwnedAutofillServices() { |
| 656 | disableAutofillServices(); |
| 657 | } |
| 658 | |
Svet Ganov | f20a037 | 2017-04-10 17:08:05 -0700 | [diff] [blame] | 659 | /** |
| 660 | * If the app calling this API has enabled autofill services they |
| 661 | * will be disabled. |
| 662 | */ |
Svet Ganov | f965b87 | 2017-04-28 16:34:02 -0700 | [diff] [blame] | 663 | public void disableAutofillServices() { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 664 | if (!hasAutofillFeature()) { |
| 665 | return; |
| 666 | } |
Svet Ganov | f20a037 | 2017-04-10 17:08:05 -0700 | [diff] [blame] | 667 | try { |
| 668 | mService.disableOwnedAutofillServices(mContext.getUserId()); |
| 669 | } catch (RemoteException e) { |
| 670 | throw e.rethrowFromSystemServer(); |
| 671 | } |
| 672 | } |
| 673 | |
Felipe Leme | db04118 | 2017-04-21 17:33:38 -0700 | [diff] [blame] | 674 | /** |
| 675 | * Returns {@code true} if the calling application provides a {@link AutofillService} that is |
| 676 | * enabled for the current user, or {@code false} otherwise. |
| 677 | */ |
| 678 | public boolean hasEnabledAutofillServices() { |
| 679 | if (mService == null) return false; |
| 680 | |
| 681 | try { |
| 682 | return mService.isServiceEnabled(mContext.getUserId(), mContext.getPackageName()); |
| 683 | } catch (RemoteException e) { |
| 684 | throw e.rethrowFromSystemServer(); |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | /** |
| 689 | * Returns {@code true} if Autofill is supported for this user. |
| 690 | * |
| 691 | * <p>Autofill is typically supported, but it could be unsupported in cases like: |
| 692 | * <ol> |
| 693 | * <li>Low-end devices. |
| 694 | * <li>Device policy rules that forbid its usage. |
| 695 | * </ol> |
| 696 | */ |
| 697 | public boolean isAutofillSupported() { |
| 698 | if (mService == null) return false; |
| 699 | |
| 700 | try { |
| 701 | return mService.isServiceSupported(mContext.getUserId()); |
| 702 | } catch (RemoteException e) { |
| 703 | throw e.rethrowFromSystemServer(); |
| 704 | } |
| 705 | } |
| 706 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 707 | private AutofillClient getClientLocked() { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 708 | if (mContext instanceof AutofillClient) { |
| 709 | return (AutofillClient) mContext; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 710 | } |
Svet Ganov | 17db9dc | 2017-02-21 19:54:31 -0800 | [diff] [blame] | 711 | return null; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | /** @hide */ |
Svetoslav Ganov | a9379d0 | 2017-05-09 17:40:24 -0700 | [diff] [blame] | 715 | public void onAuthenticationResult(int authenticationId, Intent data) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 716 | if (!hasAutofillFeature()) { |
| 717 | return; |
| 718 | } |
Felipe Leme | 85d1c2d | 2017-04-21 08:56:04 -0700 | [diff] [blame] | 719 | // 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] | 720 | // handling the cases where it's not RESULT_OK: it works fine if the service does not |
| 721 | // set the EXTRA_AUTHENTICATION_RESULT extra, but it could cause weird results if the |
| 722 | // service set the extra and returned RESULT_CANCELED... |
| 723 | |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 724 | if (sDebug) Log.d(TAG, "onAuthenticationResult(): d=" + data); |
Felipe Leme | d633f07 | 2017-02-14 10:17:17 -0800 | [diff] [blame] | 725 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 726 | synchronized (mLock) { |
| 727 | if (mSessionId == NO_SESSION || data == null) { |
| 728 | return; |
| 729 | } |
| 730 | final Parcelable result = data.getParcelableExtra(EXTRA_AUTHENTICATION_RESULT); |
| 731 | final Bundle responseData = new Bundle(); |
| 732 | responseData.putParcelable(EXTRA_AUTHENTICATION_RESULT, result); |
| 733 | try { |
Svetoslav Ganov | a9379d0 | 2017-05-09 17:40:24 -0700 | [diff] [blame] | 734 | mService.setAuthenticationResult(responseData, mSessionId, authenticationId, |
| 735 | mContext.getUserId()); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 736 | } catch (RemoteException e) { |
| 737 | Log.e(TAG, "Error delivering authentication result", e); |
| 738 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 739 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 740 | } |
| 741 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 742 | private static AutofillId getAutofillId(View view) { |
| 743 | return new AutofillId(view.getAccessibilityViewId()); |
Felipe Leme | 0200d9e | 2017-01-24 15:10:26 -0800 | [diff] [blame] | 744 | } |
| 745 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 746 | private static AutofillId getAutofillId(View parent, int childId) { |
| 747 | return new AutofillId(parent.getAccessibilityViewId(), childId); |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 748 | } |
| 749 | |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 750 | private void startSessionLocked(@NonNull AutofillId id, @NonNull Rect bounds, |
| 751 | @NonNull AutofillValue value, int flags) { |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 752 | if (sVerbose) { |
| 753 | Log.v(TAG, "startSessionLocked(): id=" + id + ", bounds=" + bounds + ", value=" + value |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 754 | + ", flags=" + flags); |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 755 | } |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 756 | |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 757 | try { |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 758 | mSessionId = mService.startSession(mContext.getActivityToken(), |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 759 | mServiceClient.asBinder(), id, bounds, value, mContext.getUserId(), |
Philip P. Moltmann | 7b77116 | 2017-03-03 17:22:57 -0800 | [diff] [blame] | 760 | mCallback != null, flags, mContext.getOpPackageName()); |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 761 | final AutofillClient client = getClientLocked(); |
Svet Ganov | 17db9dc | 2017-02-21 19:54:31 -0800 | [diff] [blame] | 762 | if (client != null) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 763 | client.autofillCallbackResetableStateAvailable(); |
Svet Ganov | 17db9dc | 2017-02-21 19:54:31 -0800 | [diff] [blame] | 764 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 765 | } catch (RemoteException e) { |
| 766 | throw e.rethrowFromSystemServer(); |
| 767 | } |
| 768 | } |
| 769 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 770 | private void finishSessionLocked() { |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 771 | if (sVerbose) Log.v(TAG, "finishSessionLocked()"); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 772 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 773 | try { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 774 | mService.finishSession(mSessionId, mContext.getUserId()); |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 775 | } catch (RemoteException e) { |
| 776 | throw e.rethrowFromSystemServer(); |
| 777 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 778 | |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 779 | mTrackedViews = null; |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 780 | mSessionId = NO_SESSION; |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 781 | } |
| 782 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 783 | private void cancelSessionLocked() { |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 784 | if (sVerbose) Log.v(TAG, "cancelSessionLocked()"); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 785 | |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 786 | try { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 787 | mService.cancelSession(mSessionId, mContext.getUserId()); |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 788 | } catch (RemoteException e) { |
| 789 | throw e.rethrowFromSystemServer(); |
| 790 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 791 | |
Svet Ganov | 48f10a2 | 2017-04-26 18:49:30 -0700 | [diff] [blame] | 792 | resetSessionLocked(); |
| 793 | } |
| 794 | |
| 795 | private void resetSessionLocked() { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 796 | mSessionId = NO_SESSION; |
Svet Ganov | 48f10a2 | 2017-04-26 18:49:30 -0700 | [diff] [blame] | 797 | mTrackedViews = null; |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 798 | } |
| 799 | |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 800 | private void updateSessionLocked(AutofillId id, Rect bounds, AutofillValue value, int action, |
| 801 | int flags) { |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 802 | if (sVerbose && action != ACTION_VIEW_EXITED) { |
| 803 | Log.v(TAG, "updateSessionLocked(): id=" + id + ", bounds=" + bounds |
| 804 | + ", value=" + value + ", action=" + action + ", flags=" + flags); |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 805 | } |
Felipe Leme | 0ab53dc | 2017-02-23 08:33:18 -0800 | [diff] [blame] | 806 | |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 807 | try { |
Felipe Leme | 0aa4c50 | 2017-04-26 12:36:01 -0700 | [diff] [blame] | 808 | mService.updateSession(mSessionId, id, bounds, value, action, flags, |
| 809 | mContext.getUserId()); |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 810 | } catch (RemoteException e) { |
| 811 | throw e.rethrowFromSystemServer(); |
| 812 | } |
| 813 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 814 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 815 | private void ensureServiceClientAddedIfNeededLocked() { |
| 816 | if (getClientLocked() == null) { |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 817 | return; |
| 818 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 819 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 820 | if (mServiceClient == null) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 821 | mServiceClient = new AutofillManagerClient(this); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 822 | try { |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 823 | final int flags = mService.addClient(mServiceClient, mContext.getUserId()); |
| 824 | mEnabled = (flags & FLAG_ADD_CLIENT_ENABLED) != 0; |
| 825 | sDebug = (flags & FLAG_ADD_CLIENT_DEBUG) != 0; |
| 826 | sVerbose = (flags & FLAG_ADD_CLIENT_VERBOSE) != 0; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 827 | } catch (RemoteException e) { |
| 828 | throw e.rethrowFromSystemServer(); |
| 829 | } |
| 830 | } |
| 831 | } |
| 832 | |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 833 | /** |
| 834 | * Registers a {@link AutofillCallback} to receive autofill events. |
| 835 | * |
| 836 | * @param callback callback to receive events. |
| 837 | */ |
| 838 | public void registerCallback(@Nullable AutofillCallback callback) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 839 | if (!hasAutofillFeature()) { |
| 840 | return; |
| 841 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 842 | synchronized (mLock) { |
| 843 | if (callback == null) return; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 844 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 845 | final boolean hadCallback = mCallback != null; |
| 846 | mCallback = callback; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 847 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 848 | if (!hadCallback) { |
| 849 | try { |
| 850 | mService.setHasCallback(mSessionId, mContext.getUserId(), true); |
| 851 | } catch (RemoteException e) { |
| 852 | throw e.rethrowFromSystemServer(); |
| 853 | } |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 854 | } |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | /** |
| 859 | * Unregisters a {@link AutofillCallback} to receive autofill events. |
| 860 | * |
| 861 | * @param callback callback to stop receiving events. |
| 862 | */ |
| 863 | public void unregisterCallback(@Nullable AutofillCallback callback) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 864 | if (!hasAutofillFeature()) { |
| 865 | return; |
| 866 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 867 | synchronized (mLock) { |
| 868 | if (callback == null || mCallback == null || callback != mCallback) return; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 869 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 870 | mCallback = null; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 871 | |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 872 | try { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 873 | mService.setHasCallback(mSessionId, mContext.getUserId(), false); |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 874 | } catch (RemoteException e) { |
| 875 | throw e.rethrowFromSystemServer(); |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 880 | private void requestShowFillUi(int sessionId, AutofillId id, int width, int height, |
| 881 | Rect anchorBounds, IAutofillWindowPresenter presenter) { |
| 882 | final View anchor = findView(id); |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 883 | if (anchor == null) { |
| 884 | return; |
| 885 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 886 | |
| 887 | AutofillCallback callback = null; |
| 888 | synchronized (mLock) { |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 889 | if (mSessionId == sessionId) { |
| 890 | AutofillClient client = getClientLocked(); |
| 891 | |
| 892 | if (client != null) { |
| 893 | if (client.autofillCallbackRequestShowFillUi(anchor, width, height, |
| 894 | anchorBounds, presenter) && mCallback != null) { |
| 895 | callback = mCallback; |
| 896 | } |
| 897 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | |
| 901 | if (callback != null) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 902 | if (id.isVirtual()) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 903 | callback.onAutofillEvent(anchor, id.getVirtualChildId(), |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 904 | AutofillCallback.EVENT_INPUT_SHOWN); |
| 905 | } else { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 906 | callback.onAutofillEvent(anchor, AutofillCallback.EVENT_INPUT_SHOWN); |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | } |
| 910 | |
Svetoslav Ganov | a9379d0 | 2017-05-09 17:40:24 -0700 | [diff] [blame] | 911 | private void authenticate(int sessionId, int authenticationId, IntentSender intent, |
| 912 | Intent fillInIntent) { |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 913 | synchronized (mLock) { |
| 914 | if (sessionId == mSessionId) { |
| 915 | AutofillClient client = getClientLocked(); |
| 916 | if (client != null) { |
Svetoslav Ganov | a9379d0 | 2017-05-09 17:40:24 -0700 | [diff] [blame] | 917 | client.autofillCallbackAuthenticate(authenticationId, intent, fillInIntent); |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | } |
| 921 | } |
| 922 | |
Svet Ganov | 48f10a2 | 2017-04-26 18:49:30 -0700 | [diff] [blame] | 923 | private void setState(boolean enabled, boolean resetSession, boolean resetClient) { |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 924 | synchronized (mLock) { |
| 925 | mEnabled = enabled; |
Svet Ganov | 48f10a2 | 2017-04-26 18:49:30 -0700 | [diff] [blame] | 926 | if (!mEnabled || resetSession) { |
| 927 | // Reset the session state |
| 928 | resetSessionLocked(); |
| 929 | } |
| 930 | if (resetClient) { |
| 931 | // Reset connection to system |
| 932 | mServiceClient = null; |
| 933 | } |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 937 | /** |
| 938 | * Sets a view as autofilled if the current value is the {code targetValue}. |
| 939 | * |
| 940 | * @param view The view that is to be autofilled |
| 941 | * @param targetValue The value we want to fill into view |
| 942 | */ |
| 943 | private void setAutofilledIfValuesIs(@NonNull View view, @Nullable AutofillValue targetValue) { |
| 944 | AutofillValue currentValue = view.getAutofillValue(); |
| 945 | if (Objects.equals(currentValue, targetValue)) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 946 | synchronized (mLock) { |
| 947 | if (mLastAutofilledData == null) { |
| 948 | mLastAutofilledData = new ParcelableMap(1); |
| 949 | } |
| 950 | mLastAutofilledData.put(getAutofillId(view), targetValue); |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 951 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 952 | view.setAutofilled(true); |
| 953 | } |
| 954 | } |
| 955 | |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 956 | private void autofill(int sessionId, List<AutofillId> ids, List<AutofillValue> values) { |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 957 | synchronized (mLock) { |
| 958 | if (sessionId != mSessionId) { |
| 959 | return; |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 960 | } |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 961 | |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 962 | final AutofillClient client = getClientLocked(); |
| 963 | if (client == null) { |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 964 | return; |
| 965 | } |
| 966 | |
| 967 | final int itemCount = ids.size(); |
| 968 | int numApplied = 0; |
| 969 | ArrayMap<View, SparseArray<AutofillValue>> virtualValues = null; |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 970 | final View[] views = client.findViewsByAccessibilityIdTraversal(getViewIds(ids)); |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 971 | |
| 972 | for (int i = 0; i < itemCount; i++) { |
| 973 | final AutofillId id = ids.get(i); |
| 974 | final AutofillValue value = values.get(i); |
| 975 | final int viewId = id.getViewId(); |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 976 | final View view = views[i]; |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 977 | if (view == null) { |
| 978 | Log.w(TAG, "autofill(): no View with id " + viewId); |
| 979 | continue; |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 980 | } |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 981 | if (id.isVirtual()) { |
| 982 | if (virtualValues == null) { |
| 983 | // Most likely there will be just one view with virtual children. |
| 984 | virtualValues = new ArrayMap<>(1); |
| 985 | } |
| 986 | SparseArray<AutofillValue> valuesByParent = virtualValues.get(view); |
| 987 | if (valuesByParent == null) { |
| 988 | // We don't know the size yet, but usually it will be just a few fields... |
| 989 | valuesByParent = new SparseArray<>(5); |
| 990 | virtualValues.put(view, valuesByParent); |
| 991 | } |
| 992 | valuesByParent.put(id.getVirtualChildId(), value); |
| 993 | } else { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 994 | // Mark the view as to be autofilled with 'value' |
| 995 | if (mLastAutofilledData == null) { |
| 996 | mLastAutofilledData = new ParcelableMap(itemCount - i); |
| 997 | } |
| 998 | mLastAutofilledData.put(id, value); |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 999 | |
| 1000 | view.autofill(value); |
| 1001 | |
| 1002 | // Set as autofilled if the values match now, e.g. when the value was updated |
| 1003 | // synchronously. |
| 1004 | // If autofill happens async, the view is set to autofilled in |
| 1005 | // notifyValueChanged. |
| 1006 | setAutofilledIfValuesIs(view, value); |
| 1007 | |
| 1008 | numApplied++; |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 1009 | } |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1010 | } |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1011 | |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1012 | if (virtualValues != null) { |
| 1013 | for (int i = 0; i < virtualValues.size(); i++) { |
| 1014 | final View parent = virtualValues.keyAt(i); |
| 1015 | final SparseArray<AutofillValue> childrenValues = virtualValues.valueAt(i); |
| 1016 | parent.autofill(childrenValues); |
| 1017 | numApplied += childrenValues.size(); |
| 1018 | } |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1019 | } |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1020 | |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1021 | final LogMaker log = new LogMaker(MetricsProto.MetricsEvent.AUTOFILL_DATASET_APPLIED); |
| 1022 | log.addTaggedData(MetricsProto.MetricsEvent.FIELD_AUTOFILL_NUM_VALUES, itemCount); |
| 1023 | log.addTaggedData(MetricsProto.MetricsEvent.FIELD_AUTOFILL_NUM_VIEWS_FILLED, |
| 1024 | numApplied); |
| 1025 | mMetricsLogger.write(log); |
| 1026 | } |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1029 | /** |
| 1030 | * Set the tracked views. |
| 1031 | * |
| 1032 | * @param trackedIds The views to be tracked |
| 1033 | * @param saveOnAllViewsInvisible Finish the session once all tracked views are invisible. |
| 1034 | */ |
| 1035 | private void setTrackedViews(int sessionId, List<AutofillId> trackedIds, |
| 1036 | boolean saveOnAllViewsInvisible) { |
| 1037 | synchronized (mLock) { |
| 1038 | if (mEnabled && mSessionId == sessionId) { |
| 1039 | if (saveOnAllViewsInvisible) { |
| 1040 | mTrackedViews = new TrackedViews(trackedIds); |
| 1041 | } else { |
| 1042 | mTrackedViews = null; |
| 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | } |
| 1047 | |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1048 | private void requestHideFillUi(int sessionId, AutofillId id) { |
| 1049 | final View anchor = findView(id); |
| 1050 | if (anchor == null) { |
| 1051 | return; |
| 1052 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1053 | |
| 1054 | AutofillCallback callback = null; |
| 1055 | synchronized (mLock) { |
Svet Ganov | 48f10a2 | 2017-04-26 18:49:30 -0700 | [diff] [blame] | 1056 | // We do not check the session id for two reasons: |
| 1057 | // 1. If local and remote session id are off sync the UI would be stuck shown |
| 1058 | // 2. There is a race between the user state being destroyed due the fill |
| 1059 | // service being uninstalled and the UI being dismissed. |
| 1060 | AutofillClient client = getClientLocked(); |
| 1061 | if (client != null) { |
| 1062 | if (client.autofillCallbackRequestHideFillUi() && mCallback != null) { |
| 1063 | callback = mCallback; |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1064 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | if (callback != null) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1069 | if (id.isVirtual()) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1070 | callback.onAutofillEvent(anchor, id.getVirtualChildId(), |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1071 | AutofillCallback.EVENT_INPUT_HIDDEN); |
| 1072 | } else { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1073 | callback.onAutofillEvent(anchor, AutofillCallback.EVENT_INPUT_HIDDEN); |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1074 | } |
| 1075 | } |
| 1076 | } |
| 1077 | |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1078 | private void notifyNoFillUi(int sessionId, AutofillId id) { |
| 1079 | final View anchor = findView(id); |
| 1080 | if (anchor == null) { |
| 1081 | return; |
| 1082 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1083 | |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1084 | AutofillCallback callback = null; |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1085 | synchronized (mLock) { |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1086 | if (mSessionId == sessionId && getClientLocked() != null) { |
| 1087 | callback = mCallback; |
| 1088 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | if (callback != null) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1092 | if (id.isVirtual()) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1093 | callback.onAutofillEvent(anchor, id.getVirtualChildId(), |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1094 | AutofillCallback.EVENT_INPUT_UNAVAILABLE); |
| 1095 | } else { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1096 | callback.onAutofillEvent(anchor, AutofillCallback.EVENT_INPUT_UNAVAILABLE); |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1097 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1098 | |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1099 | } |
| 1100 | } |
| 1101 | |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1102 | /** |
| 1103 | * Get an array of viewIds from a List of {@link AutofillId}. |
| 1104 | * |
| 1105 | * @param autofillIds The autofill ids to convert |
| 1106 | * |
| 1107 | * @return The array of viewIds. |
| 1108 | */ |
| 1109 | @NonNull private int[] getViewIds(@NonNull List<AutofillId> autofillIds) { |
| 1110 | final int numIds = autofillIds.size(); |
| 1111 | final int[] viewIds = new int[numIds]; |
| 1112 | for (int i = 0; i < numIds; i++) { |
| 1113 | viewIds[i] = autofillIds.get(i).getViewId(); |
| 1114 | } |
| 1115 | |
| 1116 | return viewIds; |
| 1117 | } |
| 1118 | |
| 1119 | /** |
| 1120 | * Find a single view by its id. |
| 1121 | * |
| 1122 | * @param autofillId The autofill id of the view |
| 1123 | * |
| 1124 | * @return The view or {@code null} if view was not found |
| 1125 | */ |
| 1126 | private View findView(@NonNull AutofillId autofillId) { |
| 1127 | final AutofillClient client = getClientLocked(); |
| 1128 | |
| 1129 | if (client == null) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1130 | return null; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1131 | } |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1132 | |
| 1133 | return client.findViewsByAccessibilityIdTraversal(new int[]{autofillId.getViewId()})[0]; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1134 | } |
| 1135 | |
Felipe Leme | bb81092 | 2017-04-25 15:54:06 -0700 | [diff] [blame] | 1136 | /** @hide */ |
| 1137 | public boolean hasAutofillFeature() { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame] | 1138 | return mService != null; |
| 1139 | } |
| 1140 | |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1141 | /** |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1142 | * View tracking information. Once all tracked views become invisible the session is finished. |
| 1143 | */ |
| 1144 | private class TrackedViews { |
| 1145 | /** Visible tracked views */ |
| 1146 | @Nullable private ArraySet<AutofillId> mVisibleTrackedIds; |
| 1147 | |
| 1148 | /** Invisible tracked views */ |
| 1149 | @Nullable private ArraySet<AutofillId> mInvisibleTrackedIds; |
| 1150 | |
| 1151 | /** |
| 1152 | * Check if set is null or value is in set. |
| 1153 | * |
| 1154 | * @param set The set or null (== empty set) |
| 1155 | * @param value The value that might be in the set |
| 1156 | * |
| 1157 | * @return {@code true} iff set is not empty and value is in set |
| 1158 | */ |
| 1159 | private <T> boolean isInSet(@Nullable ArraySet<T> set, T value) { |
| 1160 | return set != null && set.contains(value); |
| 1161 | } |
| 1162 | |
| 1163 | /** |
| 1164 | * Add a value to a set. If set is null, create a new set. |
| 1165 | * |
| 1166 | * @param set The set or null (== empty set) |
| 1167 | * @param valueToAdd The value to add |
| 1168 | * |
| 1169 | * @return The set including the new value. If set was {@code null}, a set containing only |
| 1170 | * the new value. |
| 1171 | */ |
| 1172 | @NonNull |
| 1173 | private <T> ArraySet<T> addToSet(@Nullable ArraySet<T> set, T valueToAdd) { |
| 1174 | if (set == null) { |
| 1175 | set = new ArraySet<>(1); |
| 1176 | } |
| 1177 | |
| 1178 | set.add(valueToAdd); |
| 1179 | |
| 1180 | return set; |
| 1181 | } |
| 1182 | |
| 1183 | /** |
| 1184 | * Remove a value from a set. |
| 1185 | * |
| 1186 | * @param set The set or null (== empty set) |
| 1187 | * @param valueToRemove The value to remove |
| 1188 | * |
| 1189 | * @return The set without the removed value. {@code null} if set was null, or is empty |
| 1190 | * after removal. |
| 1191 | */ |
| 1192 | @Nullable |
| 1193 | private <T> ArraySet<T> removeFromSet(@Nullable ArraySet<T> set, T valueToRemove) { |
| 1194 | if (set == null) { |
| 1195 | return null; |
| 1196 | } |
| 1197 | |
| 1198 | set.remove(valueToRemove); |
| 1199 | |
| 1200 | if (set.isEmpty()) { |
| 1201 | return null; |
| 1202 | } |
| 1203 | |
| 1204 | return set; |
| 1205 | } |
| 1206 | |
| 1207 | /** |
| 1208 | * Set the tracked views. |
| 1209 | * |
| 1210 | * @param trackedIds The views to be tracked |
| 1211 | */ |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1212 | TrackedViews(List<AutofillId> trackedIds) { |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1213 | mVisibleTrackedIds = null; |
| 1214 | mInvisibleTrackedIds = null; |
| 1215 | |
| 1216 | AutofillClient client = getClientLocked(); |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1217 | if (trackedIds != null && client != null) { |
| 1218 | final boolean[] isVisible; |
| 1219 | |
| 1220 | if (client.isVisibleForAutofill()) { |
| 1221 | isVisible = client.getViewVisibility(getViewIds(trackedIds)); |
| 1222 | } else { |
| 1223 | // All false |
| 1224 | isVisible = new boolean[trackedIds.size()]; |
| 1225 | } |
| 1226 | |
| 1227 | final int numIds = trackedIds.size(); |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1228 | for (int i = 0; i < numIds; i++) { |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1229 | final AutofillId id = trackedIds.get(i); |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1230 | |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1231 | if (isVisible[i]) { |
Philip P. Moltmann | e0e2871 | 2017-04-20 15:19:06 -0700 | [diff] [blame] | 1232 | mVisibleTrackedIds = addToSet(mVisibleTrackedIds, id); |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1233 | } else { |
| 1234 | mInvisibleTrackedIds = addToSet(mInvisibleTrackedIds, id); |
| 1235 | } |
| 1236 | } |
| 1237 | } |
| 1238 | |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 1239 | if (sVerbose) { |
| 1240 | Log.v(TAG, "TrackedViews(trackedIds=" + trackedIds + "): " |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1241 | + " mVisibleTrackedIds=" + mVisibleTrackedIds |
| 1242 | + " mInvisibleTrackedIds=" + mInvisibleTrackedIds); |
| 1243 | } |
| 1244 | |
| 1245 | if (mVisibleTrackedIds == null) { |
| 1246 | finishSessionLocked(); |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | /** |
| 1251 | * Called when a {@link View view's} visibility changes. |
| 1252 | * |
| 1253 | * @param view {@link View} that was exited. |
| 1254 | * @param isVisible visible if the view is visible in the view hierarchy. |
| 1255 | */ |
| 1256 | void notifyViewVisibilityChange(@NonNull View view, boolean isVisible) { |
| 1257 | AutofillId id = getAutofillId(view); |
| 1258 | AutofillClient client = getClientLocked(); |
| 1259 | |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 1260 | if (sDebug) { |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1261 | Log.d(TAG, "notifyViewVisibilityChange(): id=" + id + " isVisible=" |
| 1262 | + isVisible); |
| 1263 | } |
| 1264 | |
| 1265 | if (client != null && client.isVisibleForAutofill()) { |
| 1266 | if (isVisible) { |
| 1267 | if (isInSet(mInvisibleTrackedIds, id)) { |
| 1268 | mInvisibleTrackedIds = removeFromSet(mInvisibleTrackedIds, id); |
| 1269 | mVisibleTrackedIds = addToSet(mVisibleTrackedIds, id); |
| 1270 | } |
| 1271 | } else { |
| 1272 | if (isInSet(mVisibleTrackedIds, id)) { |
| 1273 | mVisibleTrackedIds = removeFromSet(mVisibleTrackedIds, id); |
| 1274 | mInvisibleTrackedIds = addToSet(mInvisibleTrackedIds, id); |
| 1275 | } |
| 1276 | } |
| 1277 | } |
| 1278 | |
| 1279 | if (mVisibleTrackedIds == null) { |
| 1280 | finishSessionLocked(); |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | /** |
| 1285 | * Called once the client becomes visible. |
| 1286 | * |
| 1287 | * @see AutofillClient#isVisibleForAutofill() |
| 1288 | */ |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1289 | void onVisibleForAutofillLocked() { |
| 1290 | // 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] | 1291 | // hence update the visibility state for all views. |
| 1292 | AutofillClient client = getClientLocked(); |
| 1293 | ArraySet<AutofillId> updatedVisibleTrackedIds = null; |
| 1294 | ArraySet<AutofillId> updatedInvisibleTrackedIds = null; |
| 1295 | if (client != null) { |
| 1296 | if (mInvisibleTrackedIds != null) { |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1297 | final ArrayList<AutofillId> orderedInvisibleIds = |
| 1298 | new ArrayList<>(mInvisibleTrackedIds); |
| 1299 | final boolean[] isVisible = client.getViewVisibility( |
| 1300 | getViewIds(orderedInvisibleIds)); |
| 1301 | |
| 1302 | final int numInvisibleTrackedIds = orderedInvisibleIds.size(); |
| 1303 | for (int i = 0; i < numInvisibleTrackedIds; i++) { |
| 1304 | final AutofillId id = orderedInvisibleIds.get(i); |
| 1305 | if (isVisible[i]) { |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1306 | updatedVisibleTrackedIds = addToSet(updatedVisibleTrackedIds, id); |
| 1307 | |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 1308 | if (sDebug) { |
| 1309 | Log.d(TAG, "onVisibleForAutofill() " + id + " became visible"); |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1310 | } |
| 1311 | } else { |
| 1312 | updatedInvisibleTrackedIds = addToSet(updatedInvisibleTrackedIds, id); |
| 1313 | } |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | if (mVisibleTrackedIds != null) { |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1318 | final ArrayList<AutofillId> orderedVisibleIds = |
| 1319 | new ArrayList<>(mVisibleTrackedIds); |
| 1320 | final boolean[] isVisible = client.getViewVisibility( |
| 1321 | getViewIds(orderedVisibleIds)); |
| 1322 | |
| 1323 | final int numVisibleTrackedIds = orderedVisibleIds.size(); |
| 1324 | for (int i = 0; i < numVisibleTrackedIds; i++) { |
| 1325 | final AutofillId id = orderedVisibleIds.get(i); |
| 1326 | |
| 1327 | if (isVisible[i]) { |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1328 | updatedVisibleTrackedIds = addToSet(updatedVisibleTrackedIds, id); |
| 1329 | } else { |
| 1330 | updatedInvisibleTrackedIds = addToSet(updatedInvisibleTrackedIds, id); |
| 1331 | |
Felipe Leme | 9f9ee25 | 2017-04-27 13:56:22 -0700 | [diff] [blame] | 1332 | if (sDebug) { |
| 1333 | Log.d(TAG, "onVisibleForAutofill() " + id + " became invisible"); |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1334 | } |
| 1335 | } |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | mInvisibleTrackedIds = updatedInvisibleTrackedIds; |
| 1340 | mVisibleTrackedIds = updatedVisibleTrackedIds; |
| 1341 | } |
| 1342 | |
| 1343 | if (mVisibleTrackedIds == null) { |
| 1344 | finishSessionLocked(); |
| 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | /** |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1350 | * Callback for auto-fill related events. |
| 1351 | * |
| 1352 | * <p>Typically used for applications that display their own "auto-complete" views, so they can |
| 1353 | * enable / disable such views when the auto-fill UI affordance is shown / hidden. |
| 1354 | */ |
| 1355 | public abstract static class AutofillCallback { |
| 1356 | |
| 1357 | /** @hide */ |
| 1358 | @IntDef({EVENT_INPUT_SHOWN, EVENT_INPUT_HIDDEN}) |
| 1359 | @Retention(RetentionPolicy.SOURCE) |
| 1360 | public @interface AutofillEventType {} |
| 1361 | |
| 1362 | /** |
| 1363 | * The auto-fill input UI affordance associated with the view was shown. |
| 1364 | * |
| 1365 | * <p>If the view provides its own auto-complete UI affordance and its currently shown, it |
| 1366 | * should be hidden upon receiving this event. |
| 1367 | */ |
| 1368 | public static final int EVENT_INPUT_SHOWN = 1; |
| 1369 | |
| 1370 | /** |
| 1371 | * The auto-fill input UI affordance associated with the view was hidden. |
| 1372 | * |
| 1373 | * <p>If the view provides its own auto-complete UI affordance that was hidden upon a |
| 1374 | * {@link #EVENT_INPUT_SHOWN} event, it could be shown again now. |
| 1375 | */ |
| 1376 | public static final int EVENT_INPUT_HIDDEN = 2; |
| 1377 | |
| 1378 | /** |
Felipe Leme | 24aae15 | 2017-03-15 12:33:01 -0700 | [diff] [blame] | 1379 | * The auto-fill input UI affordance associated with the view won't be shown because |
| 1380 | * autofill is not available. |
| 1381 | * |
| 1382 | * <p>If the view provides its own auto-complete UI affordance but was not displaying it |
| 1383 | * to avoid flickering, it could shown it upon receiving this event. |
| 1384 | */ |
| 1385 | public static final int EVENT_INPUT_UNAVAILABLE = 3; |
| 1386 | |
| 1387 | /** |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1388 | * Called after a change in the autofill state associated with a view. |
| 1389 | * |
| 1390 | * @param view view associated with the change. |
| 1391 | * |
| 1392 | * @param event currently either {@link #EVENT_INPUT_SHOWN} or {@link #EVENT_INPUT_HIDDEN}. |
| 1393 | */ |
Felipe Leme | 81f01d9 | 2017-03-16 17:13:25 -0700 | [diff] [blame] | 1394 | public void onAutofillEvent(@NonNull View view, @AutofillEventType int event) { |
| 1395 | } |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1396 | |
| 1397 | /** |
| 1398 | * Called after a change in the autofill state associated with a virtual view. |
| 1399 | * |
| 1400 | * @param view parent view associated with the change. |
| 1401 | * @param childId id identifying the virtual child inside the parent view. |
| 1402 | * |
| 1403 | * @param event currently either {@link #EVENT_INPUT_SHOWN} or {@link #EVENT_INPUT_HIDDEN}. |
| 1404 | */ |
Felipe Leme | 81f01d9 | 2017-03-16 17:13:25 -0700 | [diff] [blame] | 1405 | public void onAutofillEvent(@NonNull View view, int childId, @AutofillEventType int event) { |
| 1406 | } |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1407 | } |
| 1408 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 1409 | private static final class AutofillManagerClient extends IAutoFillManagerClient.Stub { |
| 1410 | private final WeakReference<AutofillManager> mAfm; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1411 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 1412 | AutofillManagerClient(AutofillManager autofillManager) { |
| 1413 | mAfm = new WeakReference<>(autofillManager); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | @Override |
Svet Ganov | 48f10a2 | 2017-04-26 18:49:30 -0700 | [diff] [blame] | 1417 | public void setState(boolean enabled, boolean resetSession, boolean resetClient) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 1418 | final AutofillManager afm = mAfm.get(); |
| 1419 | if (afm != null) { |
Svet Ganov | 48f10a2 | 2017-04-26 18:49:30 -0700 | [diff] [blame] | 1420 | afm.mContext.getMainThreadHandler().post( |
| 1421 | () -> afm.setState(enabled, resetSession, resetClient)); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | @Override |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1426 | public void autofill(int sessionId, List<AutofillId> ids, List<AutofillValue> values) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 1427 | final AutofillManager afm = mAfm.get(); |
| 1428 | if (afm != null) { |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1429 | afm.mContext.getMainThreadHandler().post( |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1430 | () -> afm.autofill(sessionId, ids, values)); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | @Override |
Svetoslav Ganov | a9379d0 | 2017-05-09 17:40:24 -0700 | [diff] [blame] | 1435 | public void authenticate(int sessionId, int authenticationId, IntentSender intent, |
| 1436 | Intent fillInIntent) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 1437 | final AutofillManager afm = mAfm.get(); |
| 1438 | if (afm != null) { |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1439 | afm.mContext.getMainThreadHandler().post( |
Svetoslav Ganov | a9379d0 | 2017-05-09 17:40:24 -0700 | [diff] [blame] | 1440 | () -> afm.authenticate(sessionId, authenticationId, intent, fillInIntent)); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1441 | } |
| 1442 | } |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1443 | |
| 1444 | @Override |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1445 | public void requestShowFillUi(int sessionId, AutofillId id, int width, int height, |
| 1446 | Rect anchorBounds, IAutofillWindowPresenter presenter) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 1447 | final AutofillManager afm = mAfm.get(); |
| 1448 | if (afm != null) { |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1449 | afm.mContext.getMainThreadHandler().post( |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1450 | () -> afm.requestShowFillUi(sessionId, id, width, height, anchorBounds, |
| 1451 | presenter)); |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1452 | } |
| 1453 | } |
| 1454 | |
| 1455 | @Override |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1456 | public void requestHideFillUi(int sessionId, AutofillId id) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1457 | final AutofillManager afm = mAfm.get(); |
| 1458 | if (afm != null) { |
Philip P. Moltmann | e048a65 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1459 | afm.mContext.getMainThreadHandler().post( |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1460 | () -> afm.requestHideFillUi(sessionId, id)); |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1461 | } |
| 1462 | } |
| 1463 | |
| 1464 | @Override |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1465 | public void notifyNoFillUi(int sessionId, AutofillId id) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1466 | final AutofillManager afm = mAfm.get(); |
| 1467 | if (afm != null) { |
Philip P. Moltmann | 134cee2 | 2017-05-06 11:28:38 -0700 | [diff] [blame] | 1468 | afm.mContext.getMainThreadHandler().post(() -> afm.notifyNoFillUi(sessionId, id)); |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1469 | } |
| 1470 | } |
Svet Ganov | c3d1c85 | 2017-04-12 22:34:28 -0700 | [diff] [blame] | 1471 | |
| 1472 | @Override |
| 1473 | public void startIntentSender(IntentSender intentSender) { |
| 1474 | final AutofillManager afm = mAfm.get(); |
| 1475 | if (afm != null) { |
| 1476 | afm.mContext.getMainThreadHandler().post(() -> { |
| 1477 | try { |
| 1478 | afm.mContext.startIntentSender(intentSender, null, 0, 0, 0); |
| 1479 | } catch (IntentSender.SendIntentException e) { |
| 1480 | Log.e(TAG, "startIntentSender() failed for intent:" + intentSender, e); |
| 1481 | } |
| 1482 | }); |
| 1483 | } |
| 1484 | } |
Philip P. Moltmann | 494c3f5 | 2017-04-11 10:13:33 -0700 | [diff] [blame] | 1485 | |
| 1486 | @Override |
| 1487 | public void setTrackedViews(int sessionId, List<AutofillId> ids, |
| 1488 | boolean saveOnAllViewsInvisible) { |
| 1489 | final AutofillManager afm = mAfm.get(); |
| 1490 | if (afm != null) { |
| 1491 | afm.mContext.getMainThreadHandler().post( |
| 1492 | () -> afm.setTrackedViews(sessionId, ids, saveOnAllViewsInvisible) |
| 1493 | ); |
| 1494 | } |
| 1495 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1496 | } |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 1497 | } |