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 | d633f07 | 2017-02-14 10:17:17 -0800 | [diff] [blame] | 19 | import static android.view.autofill.Helper.DEBUG; |
Felipe Leme | 0ab53dc | 2017-02-23 08:33:18 -0800 | [diff] [blame] | 20 | import static android.view.autofill.Helper.VERBOSE; |
Felipe Leme | d633f07 | 2017-02-14 10:17:17 -0800 | [diff] [blame] | 21 | |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 22 | import android.annotation.IntDef; |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 23 | import android.annotation.NonNull; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 24 | import android.annotation.Nullable; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 25 | import android.content.Context; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 26 | import android.content.Intent; |
| 27 | import android.content.IntentSender; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 28 | import android.graphics.Rect; |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 29 | import android.metrics.LogMaker; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 30 | import android.os.Bundle; |
Svet Ganov | 28a2c7e | 2017-02-21 10:16:16 -0800 | [diff] [blame] | 31 | import android.os.IBinder; |
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; |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 34 | import android.util.ArrayMap; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 35 | import android.util.Log; |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 36 | import android.util.SparseArray; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 37 | import android.view.View; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 38 | import android.view.WindowManagerGlobal; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 39 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 40 | import com.android.internal.annotations.GuardedBy; |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 41 | import com.android.internal.logging.MetricsLogger; |
| 42 | import com.android.internal.logging.nano.MetricsProto; |
| 43 | |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 44 | import java.lang.annotation.Retention; |
| 45 | import java.lang.annotation.RetentionPolicy; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 46 | import java.lang.ref.WeakReference; |
| 47 | import java.util.List; |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 48 | import java.util.Objects; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 49 | |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 50 | /** |
| 51 | * App entry point to the AutoFill Framework. |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 52 | * |
| 53 | * <p>It is safe to call into this from any thread. |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 54 | */ |
| 55 | // TODO(b/33197203): improve this javadoc |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 56 | //TODO(b/33197203): restrict manager calls to activity |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 57 | public final class AutofillManager { |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 58 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 59 | private static final String TAG = "AutofillManager"; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 60 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 61 | /** |
| 62 | * Intent extra: The assist structure which captures the filled screen. |
Felipe Leme | 7320ca9 | 2017-03-29 15:09:54 -0700 | [diff] [blame] | 63 | * |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 64 | * <p> |
| 65 | * Type: {@link android.app.assist.AssistStructure} |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 66 | */ |
| 67 | public static final String EXTRA_ASSIST_STRUCTURE = |
| 68 | "android.view.autofill.extra.ASSIST_STRUCTURE"; |
| 69 | |
| 70 | /** |
| 71 | * Intent extra: The result of an authentication operation. It is |
| 72 | * either a fully populated {@link android.service.autofill.FillResponse} |
| 73 | * or a fully populated {@link android.service.autofill.Dataset} if |
| 74 | * a response or a dataset is being authenticated respectively. |
| 75 | * |
| 76 | * <p> |
| 77 | * Type: {@link android.service.autofill.FillResponse} or a |
| 78 | * {@link android.service.autofill.Dataset} |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 79 | */ |
| 80 | public static final String EXTRA_AUTHENTICATION_RESULT = |
| 81 | "android.view.autofill.extra.AUTHENTICATION_RESULT"; |
| 82 | |
Felipe Leme | 7320ca9 | 2017-03-29 15:09:54 -0700 | [diff] [blame] | 83 | /** |
| 84 | * Intent extra: The optional extras provided by the |
| 85 | * {@link android.service.autofill.AutofillService}. |
| 86 | * |
| 87 | * <p>For example, when the service responds to a {@link |
| 88 | * android.service.autofill.FillCallback#onSuccess(android.service.autofill.FillResponse)} with |
| 89 | * a {@code FillResponse} that requires authentication, the Intent that launches the |
| 90 | * service authentication will contain the Bundle set by |
| 91 | * {@link android.service.autofill.FillResponse.Builder#setExtras(Bundle)} on this extra. |
| 92 | * |
| 93 | * <p> |
| 94 | * Type: {@link android.os.Bundle} |
| 95 | */ |
| 96 | public static final String EXTRA_DATA_EXTRAS = "android.view.autofill.extra.DATA_EXTRAS"; |
| 97 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 98 | static final String SESSION_ID_TAG = "android:sessionId"; |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 99 | static final String LAST_AUTOFILLED_DATA_TAG = "android:lastAutoFilledData"; |
| 100 | |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 101 | // Public flags start from the lowest bit |
| 102 | /** |
| 103 | * Indicates autofill was explicitly requested by the user. |
| 104 | */ |
| 105 | public static final int FLAG_MANUAL_REQUEST = 0x1; |
| 106 | |
| 107 | // Private flags start from the highest bit |
| 108 | /** @hide */ public static final int FLAG_START_SESSION = 0x80000000; |
| 109 | /** @hide */ public static final int FLAG_VIEW_ENTERED = 0x40000000; |
| 110 | /** @hide */ public static final int FLAG_VIEW_EXITED = 0x20000000; |
| 111 | /** @hide */ public static final int FLAG_VALUE_CHANGED = 0x10000000; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 112 | |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 113 | private final MetricsLogger mMetricsLogger = new MetricsLogger(); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 114 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 115 | /** |
| 116 | * There is currently no session running. |
| 117 | * {@hide} |
| 118 | */ |
| 119 | public static final int NO_SESSION = Integer.MIN_VALUE; |
| 120 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 121 | private final IAutoFillManager mService; |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 122 | |
| 123 | private final Object mLock = new Object(); |
| 124 | |
| 125 | @GuardedBy("mLock") |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 126 | private IAutoFillManagerClient mServiceClient; |
| 127 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 128 | @GuardedBy("mLock") |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 129 | private AutofillCallback mCallback; |
| 130 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 131 | private final Context mContext; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 132 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 133 | @GuardedBy("mLock") |
| 134 | private int mSessionId = NO_SESSION; |
| 135 | |
| 136 | @GuardedBy("mLock") |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 137 | private boolean mEnabled; |
| 138 | |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 139 | /** 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] | 140 | @GuardedBy("mLock") |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 141 | @Nullable private ParcelableMap mLastAutofilledData; |
| 142 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 143 | /** @hide */ |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 144 | public interface AutofillClient { |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 145 | /** |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 146 | * Asks the client to start an authentication flow. |
| 147 | * |
| 148 | * @param intent The authentication intent. |
| 149 | * @param fillInIntent The authentication fill-in intent. |
| 150 | */ |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 151 | void autofillCallbackAuthenticate(IntentSender intent, Intent fillInIntent); |
Svet Ganov | 17db9dc | 2017-02-21 19:54:31 -0800 | [diff] [blame] | 152 | |
| 153 | /** |
| 154 | * Tells the client this manager has state to be reset. |
| 155 | */ |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 156 | void autofillCallbackResetableStateAvailable(); |
| 157 | |
| 158 | /** |
| 159 | * Request showing the autofill UI. |
| 160 | * |
| 161 | * @param anchor The real view the UI needs to anchor to. |
| 162 | * @param width The width of the fill UI content. |
| 163 | * @param height The height of the fill UI content. |
| 164 | * @param virtualBounds The bounds of the virtual decendant of the anchor. |
| 165 | * @param presenter The presenter that controls the fill UI window. |
| 166 | * @return Whether the UI was shown. |
| 167 | */ |
| 168 | boolean autofillCallbackRequestShowFillUi(@NonNull View anchor, int width, int height, |
| 169 | @Nullable Rect virtualBounds, IAutofillWindowPresenter presenter); |
| 170 | |
| 171 | /** |
| 172 | * Request hiding the autofill UI. |
| 173 | * |
| 174 | * @return Whether the UI was hidden. |
| 175 | */ |
| 176 | boolean autofillCallbackRequestHideFillUi(); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 177 | } |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 178 | |
| 179 | /** |
| 180 | * @hide |
| 181 | */ |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 182 | public AutofillManager(Context context, IAutoFillManager service) { |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 183 | mContext = context; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 184 | mService = service; |
| 185 | } |
| 186 | |
| 187 | /** |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 188 | * Restore state after activity lifecycle |
| 189 | * |
| 190 | * @param savedInstanceState The state to be restored |
| 191 | * |
| 192 | * {@hide} |
| 193 | */ |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 194 | public void onCreate(Bundle savedInstanceState) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame^] | 195 | if (!hasAutofillFeature()) { |
| 196 | return; |
| 197 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 198 | synchronized (mLock) { |
| 199 | mLastAutofilledData = savedInstanceState.getParcelable(LAST_AUTOFILLED_DATA_TAG); |
| 200 | |
| 201 | if (mSessionId != NO_SESSION) { |
| 202 | Log.w(TAG, "New session was started before onCreate()"); |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | mSessionId = savedInstanceState.getInt(SESSION_ID_TAG, NO_SESSION); |
| 207 | |
| 208 | if (mSessionId != NO_SESSION) { |
| 209 | ensureServiceClientAddedIfNeededLocked(); |
| 210 | |
| 211 | final AutofillClient client = getClientLocked(); |
| 212 | if (client != null) { |
| 213 | try { |
| 214 | final boolean sessionWasRestored = mService.restoreSession(mSessionId, |
| 215 | mContext.getActivityToken(), mServiceClient.asBinder()); |
| 216 | |
| 217 | if (!sessionWasRestored) { |
| 218 | Log.w(TAG, "Session " + mSessionId + " could not be restored"); |
| 219 | mSessionId = NO_SESSION; |
| 220 | } else { |
| 221 | if (DEBUG) { |
| 222 | Log.d(TAG, "session " + mSessionId + " was restored"); |
| 223 | } |
| 224 | |
| 225 | client.autofillCallbackResetableStateAvailable(); |
| 226 | } |
| 227 | } catch (RemoteException e) { |
| 228 | Log.e(TAG, "Could not figure out if there was an autofill session", e); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Set window future popup windows should be attached to. |
| 237 | * |
| 238 | * @param windowToken The window the popup windows should be attached to |
| 239 | * |
| 240 | * {@hide} |
| 241 | */ |
| 242 | public void onAttachedToWindow(@NonNull IBinder windowToken) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame^] | 243 | if (!hasAutofillFeature()) { |
| 244 | return; |
| 245 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 246 | synchronized (mLock) { |
| 247 | if (mSessionId == NO_SESSION) { |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | try { |
| 252 | mService.setWindow(mSessionId, windowToken); |
| 253 | } catch (RemoteException e) { |
| 254 | Log.e(TAG, "Could not attach window to session " + mSessionId); |
| 255 | } |
| 256 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Save state before activity lifecycle |
| 261 | * |
| 262 | * @param outState Place to store the state |
| 263 | * |
| 264 | * {@hide} |
| 265 | */ |
| 266 | public void onSaveInstanceState(Bundle outState) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame^] | 267 | if (!hasAutofillFeature()) { |
| 268 | return; |
| 269 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 270 | synchronized (mLock) { |
| 271 | if (mSessionId != NO_SESSION) { |
| 272 | outState.putInt(SESSION_ID_TAG, mSessionId); |
| 273 | } |
| 274 | |
| 275 | if (mLastAutofilledData != null) { |
| 276 | outState.putParcelable(LAST_AUTOFILLED_DATA_TAG, mLastAutofilledData); |
| 277 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Checks whether autofill is enabled for the current user. |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 283 | * |
| 284 | * <p>Typically used to determine whether the option to explicitly request autofill should |
| 285 | * be offered - see {@link #requestAutofill(View)}. |
| 286 | * |
| 287 | * @return whether autofill is enabled for the current user. |
| 288 | */ |
| 289 | public boolean isEnabled() { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame^] | 290 | if (!hasAutofillFeature()) { |
| 291 | return false; |
| 292 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 293 | synchronized (mLock) { |
| 294 | ensureServiceClientAddedIfNeededLocked(); |
| 295 | return mEnabled; |
| 296 | } |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Explicitly requests a new autofill context. |
| 301 | * |
| 302 | * <p>Normally, the autofill context is automatically started when autofillable views are |
| 303 | * focused, but this method should be used in the cases where it must be explicitly requested, |
| 304 | * like a view that provides a contextual menu allowing users to autofill the activity. |
| 305 | * |
| 306 | * @param view view requesting the new autofill context. |
| 307 | */ |
| 308 | public void requestAutofill(@NonNull View view) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame^] | 309 | if (!hasAutofillFeature()) { |
| 310 | return; |
| 311 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 312 | synchronized (mLock) { |
| 313 | ensureServiceClientAddedIfNeededLocked(); |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 314 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 315 | if (!mEnabled) { |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | final AutofillId id = getAutofillId(view); |
| 320 | final AutofillValue value = view.getAutofillValue(); |
| 321 | |
| 322 | startSessionLocked(id, view.getWindowToken(), null, value, FLAG_MANUAL_REQUEST); |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 323 | } |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Explicitly requests a new autofill context for virtual views. |
| 328 | * |
| 329 | * <p>Normally, the autofill context is automatically started when autofillable views are |
| 330 | * focused, but this method should be used in the cases where it must be explicitly requested, |
| 331 | * like a virtual view that provides a contextual menu allowing users to autofill the activity. |
| 332 | * |
| 333 | * @param view the {@link View} whose descendant is the virtual view. |
| 334 | * @param childId id identifying the virtual child inside the view. |
| 335 | * @param bounds child boundaries, relative to the top window. |
| 336 | */ |
| 337 | public void requestAutofill(@NonNull View view, int childId, @NonNull Rect bounds) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame^] | 338 | if (!hasAutofillFeature()) { |
| 339 | return; |
| 340 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 341 | synchronized (mLock) { |
| 342 | ensureServiceClientAddedIfNeededLocked(); |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 343 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 344 | if (!mEnabled) { |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | final AutofillId id = getAutofillId(view, childId); |
| 349 | startSessionLocked(id, view.getWindowToken(), bounds, null, FLAG_MANUAL_REQUEST); |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 350 | } |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | |
| 354 | /** |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 355 | * Called when a {@link View} that supports autofill is entered. |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 356 | * |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 357 | * @param view {@link View} that was entered. |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 358 | */ |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 359 | public void notifyViewEntered(@NonNull View view) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame^] | 360 | if (!hasAutofillFeature()) { |
| 361 | return; |
| 362 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 363 | AutofillCallback callback = null; |
| 364 | synchronized (mLock) { |
| 365 | ensureServiceClientAddedIfNeededLocked(); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 366 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 367 | if (!mEnabled) { |
| 368 | if (mCallback != null) { |
| 369 | callback = mCallback; |
| 370 | } |
| 371 | } else { |
| 372 | final AutofillId id = getAutofillId(view); |
| 373 | final AutofillValue value = view.getAutofillValue(); |
| 374 | |
| 375 | if (mSessionId == NO_SESSION) { |
| 376 | // Starts new session. |
| 377 | startSessionLocked(id, view.getWindowToken(), null, value, 0); |
| 378 | } else { |
| 379 | // Update focus on existing session. |
| 380 | updateSessionLocked(id, null, value, FLAG_VIEW_ENTERED); |
| 381 | } |
Felipe Leme | 24aae15 | 2017-03-15 12:33:01 -0700 | [diff] [blame] | 382 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 383 | } |
| 384 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 385 | if (callback != null) { |
| 386 | mCallback.onAutofillEvent(view, AutofillCallback.EVENT_INPUT_UNAVAILABLE); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 387 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /** |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 391 | * Called when a {@link View} that supports autofill is exited. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 392 | * |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 393 | * @param view {@link View} that was exited. |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 394 | */ |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 395 | public void notifyViewExited(@NonNull View view) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame^] | 396 | if (!hasAutofillFeature()) { |
| 397 | return; |
| 398 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 399 | synchronized (mLock) { |
| 400 | ensureServiceClientAddedIfNeededLocked(); |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 401 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 402 | if (mEnabled && mSessionId != NO_SESSION) { |
| 403 | final AutofillId id = getAutofillId(view); |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 404 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 405 | // Update focus on existing session. |
| 406 | updateSessionLocked(id, null, null, FLAG_VIEW_EXITED); |
| 407 | } |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
| 411 | /** |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 412 | * Called when a virtual view that supports autofill is entered. |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 413 | * |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 414 | * @param view the {@link View} whose descendant is the virtual view. |
| 415 | * @param childId id identifying the virtual child inside the view. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 416 | * @param bounds child boundaries, relative to the top window. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 417 | */ |
Felipe Leme | 81f01d9 | 2017-03-16 17:13:25 -0700 | [diff] [blame] | 418 | public void notifyViewEntered(@NonNull View view, int childId, @NonNull Rect bounds) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame^] | 419 | if (!hasAutofillFeature()) { |
| 420 | return; |
| 421 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 422 | AutofillCallback callback = null; |
| 423 | synchronized (mLock) { |
| 424 | ensureServiceClientAddedIfNeededLocked(); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 425 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 426 | if (!mEnabled) { |
| 427 | if (mCallback != null) { |
| 428 | callback = mCallback; |
| 429 | } |
| 430 | } else { |
| 431 | final AutofillId id = getAutofillId(view, childId); |
| 432 | |
| 433 | if (mSessionId == NO_SESSION) { |
| 434 | // Starts new session. |
| 435 | startSessionLocked(id, view.getWindowToken(), bounds, null, 0); |
| 436 | } else { |
| 437 | // Update focus on existing session. |
| 438 | updateSessionLocked(id, bounds, null, FLAG_VIEW_ENTERED); |
| 439 | } |
Felipe Leme | 24aae15 | 2017-03-15 12:33:01 -0700 | [diff] [blame] | 440 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 441 | } |
| 442 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 443 | if (callback != null) { |
| 444 | callback.onAutofillEvent(view, childId, |
| 445 | AutofillCallback.EVENT_INPUT_UNAVAILABLE); |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
| 449 | /** |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 450 | * Called when a virtual view that supports autofill is exited. |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 451 | * |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 452 | * @param view the {@link View} whose descendant is the virtual view. |
| 453 | * @param childId id identifying the virtual child inside the view. |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 454 | */ |
Felipe Leme | 81f01d9 | 2017-03-16 17:13:25 -0700 | [diff] [blame] | 455 | public void notifyViewExited(@NonNull View view, int childId) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame^] | 456 | if (!hasAutofillFeature()) { |
| 457 | return; |
| 458 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 459 | synchronized (mLock) { |
| 460 | ensureServiceClientAddedIfNeededLocked(); |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 461 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 462 | if (mEnabled && mSessionId != NO_SESSION) { |
| 463 | final AutofillId id = getAutofillId(view, childId); |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 464 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 465 | // Update focus on existing session. |
| 466 | updateSessionLocked(id, null, null, FLAG_VIEW_EXITED); |
| 467 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 468 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /** |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 472 | * Called to indicate the value of an autofillable {@link View} changed. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 473 | * |
Philip P. Moltmann | 4461181 | 2017-02-23 12:52:46 -0800 | [diff] [blame] | 474 | * @param view view whose value changed. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 475 | */ |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 476 | public void notifyValueChanged(View view) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame^] | 477 | if (!hasAutofillFeature()) { |
| 478 | return; |
| 479 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 480 | AutofillId id = null; |
| 481 | boolean valueWasRead = false; |
| 482 | AutofillValue value = null; |
| 483 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 484 | synchronized (mLock) { |
| 485 | // If the session is gone some fields might still be highlighted, hence we have to |
| 486 | // remove the isAutofilled property even if no sessions are active. |
| 487 | if (mLastAutofilledData == null) { |
| 488 | view.setAutofilled(false); |
| 489 | } else { |
| 490 | id = getAutofillId(view); |
| 491 | if (mLastAutofilledData.containsKey(id)) { |
| 492 | value = view.getAutofillValue(); |
| 493 | valueWasRead = true; |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 494 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 495 | if (Objects.equals(mLastAutofilledData.get(id), value)) { |
| 496 | view.setAutofilled(true); |
| 497 | } else { |
| 498 | view.setAutofilled(false); |
| 499 | mLastAutofilledData.remove(id); |
| 500 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 501 | } else { |
| 502 | view.setAutofilled(false); |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 503 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 504 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 505 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 506 | if (!mEnabled || mSessionId == NO_SESSION) { |
| 507 | return; |
| 508 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 509 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 510 | if (id == null) { |
| 511 | id = getAutofillId(view); |
| 512 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 513 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 514 | if (!valueWasRead) { |
| 515 | value = view.getAutofillValue(); |
| 516 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 517 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 518 | updateSessionLocked(id, null, value, FLAG_VALUE_CHANGED); |
| 519 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 520 | } |
| 521 | |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 522 | /** |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 523 | * Called to indicate the value of an autofillable virtual {@link View} changed. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 524 | * |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 525 | * @param view the {@link View} whose descendant is the virtual view. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 526 | * @param childId id identifying the virtual child inside the parent view. |
| 527 | * @param value new value of the child. |
| 528 | */ |
Felipe Leme | 81f01d9 | 2017-03-16 17:13:25 -0700 | [diff] [blame] | 529 | public void notifyValueChanged(View view, int childId, AutofillValue value) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame^] | 530 | if (!hasAutofillFeature()) { |
| 531 | return; |
| 532 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 533 | synchronized (mLock) { |
| 534 | if (!mEnabled || mSessionId == NO_SESSION) { |
| 535 | return; |
| 536 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 537 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 538 | final AutofillId id = getAutofillId(view, childId); |
| 539 | updateSessionLocked(id, null, value, FLAG_VALUE_CHANGED); |
| 540 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /** |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 544 | * Called to indicate the current autofill context should be commited. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 545 | * |
| 546 | * <p>For example, when a virtual view is rendering an {@code HTML} page with a form, it should |
| 547 | * call this method after the form is submitted and another page is rendered. |
| 548 | */ |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 549 | public void commit() { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame^] | 550 | if (!hasAutofillFeature()) { |
| 551 | return; |
| 552 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 553 | synchronized (mLock) { |
| 554 | if (!mEnabled && mSessionId == NO_SESSION) { |
| 555 | return; |
| 556 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 557 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 558 | finishSessionLocked(); |
| 559 | } |
Felipe Leme | 0200d9e | 2017-01-24 15:10:26 -0800 | [diff] [blame] | 560 | } |
| 561 | |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 562 | /** |
| 563 | * Called to indicate the current autofill context should be cancelled. |
| 564 | * |
| 565 | * <p>For example, when a virtual view is rendering an {@code HTML} page with a form, it should |
| 566 | * call this method if the user does not post the form but moves to another form in this page. |
| 567 | */ |
| 568 | public void cancel() { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame^] | 569 | if (!hasAutofillFeature()) { |
| 570 | return; |
| 571 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 572 | synchronized (mLock) { |
| 573 | if (!mEnabled && mSessionId == NO_SESSION) { |
| 574 | return; |
| 575 | } |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 576 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 577 | cancelSessionLocked(); |
| 578 | } |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Svet Ganov | f20a037 | 2017-04-10 17:08:05 -0700 | [diff] [blame] | 581 | /** |
| 582 | * If the app calling this API has enabled autofill services they |
| 583 | * will be disabled. |
| 584 | */ |
| 585 | public void disableOwnedAutofillServices() { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame^] | 586 | if (!hasAutofillFeature()) { |
| 587 | return; |
| 588 | } |
Svet Ganov | f20a037 | 2017-04-10 17:08:05 -0700 | [diff] [blame] | 589 | try { |
| 590 | mService.disableOwnedAutofillServices(mContext.getUserId()); |
| 591 | } catch (RemoteException e) { |
| 592 | throw e.rethrowFromSystemServer(); |
| 593 | } |
| 594 | } |
| 595 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 596 | private AutofillClient getClientLocked() { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 597 | if (mContext instanceof AutofillClient) { |
| 598 | return (AutofillClient) mContext; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 599 | } |
Svet Ganov | 17db9dc | 2017-02-21 19:54:31 -0800 | [diff] [blame] | 600 | return null; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /** @hide */ |
| 604 | public void onAuthenticationResult(Intent data) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame^] | 605 | if (!hasAutofillFeature()) { |
| 606 | return; |
| 607 | } |
Felipe Leme | d633f07 | 2017-02-14 10:17:17 -0800 | [diff] [blame] | 608 | // TODO(b/33197203): the result code is being ignored, so this method is not reliably |
| 609 | // handling the cases where it's not RESULT_OK: it works fine if the service does not |
| 610 | // set the EXTRA_AUTHENTICATION_RESULT extra, but it could cause weird results if the |
| 611 | // service set the extra and returned RESULT_CANCELED... |
| 612 | |
| 613 | if (DEBUG) Log.d(TAG, "onAuthenticationResult(): d=" + data); |
| 614 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 615 | synchronized (mLock) { |
| 616 | if (mSessionId == NO_SESSION || data == null) { |
| 617 | return; |
| 618 | } |
| 619 | final Parcelable result = data.getParcelableExtra(EXTRA_AUTHENTICATION_RESULT); |
| 620 | final Bundle responseData = new Bundle(); |
| 621 | responseData.putParcelable(EXTRA_AUTHENTICATION_RESULT, result); |
| 622 | try { |
| 623 | mService.setAuthenticationResult(responseData, mSessionId, mContext.getUserId()); |
| 624 | } catch (RemoteException e) { |
| 625 | Log.e(TAG, "Error delivering authentication result", e); |
| 626 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 627 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 628 | } |
| 629 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 630 | private static AutofillId getAutofillId(View view) { |
| 631 | return new AutofillId(view.getAccessibilityViewId()); |
Felipe Leme | 0200d9e | 2017-01-24 15:10:26 -0800 | [diff] [blame] | 632 | } |
| 633 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 634 | private static AutofillId getAutofillId(View parent, int childId) { |
| 635 | return new AutofillId(parent.getAccessibilityViewId(), childId); |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 636 | } |
| 637 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 638 | private void startSessionLocked(@NonNull AutofillId id, @NonNull IBinder windowToken, |
Philip P. Moltmann | 7b77116 | 2017-03-03 17:22:57 -0800 | [diff] [blame] | 639 | @NonNull Rect bounds, @NonNull AutofillValue value, int flags) { |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 640 | if (DEBUG) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 641 | Log.d(TAG, "startSessionLocked(): id=" + id + ", bounds=" + bounds + ", value=" + value |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 642 | + ", flags=" + flags); |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 643 | } |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 644 | |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 645 | try { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 646 | mSessionId = mService.startSession(mContext.getActivityToken(), windowToken, |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 647 | mServiceClient.asBinder(), id, bounds, value, mContext.getUserId(), |
Philip P. Moltmann | 7b77116 | 2017-03-03 17:22:57 -0800 | [diff] [blame] | 648 | mCallback != null, flags, mContext.getOpPackageName()); |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 649 | AutofillClient client = getClientLocked(); |
Svet Ganov | 17db9dc | 2017-02-21 19:54:31 -0800 | [diff] [blame] | 650 | if (client != null) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 651 | client.autofillCallbackResetableStateAvailable(); |
Svet Ganov | 17db9dc | 2017-02-21 19:54:31 -0800 | [diff] [blame] | 652 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 653 | } catch (RemoteException e) { |
| 654 | throw e.rethrowFromSystemServer(); |
| 655 | } |
| 656 | } |
| 657 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 658 | private void finishSessionLocked() { |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 659 | if (DEBUG) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 660 | Log.d(TAG, "finishSessionLocked()"); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 661 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 662 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 663 | try { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 664 | mService.finishSession(mSessionId, mContext.getUserId()); |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 665 | } catch (RemoteException e) { |
| 666 | throw e.rethrowFromSystemServer(); |
| 667 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 668 | |
| 669 | mSessionId = NO_SESSION; |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 670 | } |
| 671 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 672 | private void cancelSessionLocked() { |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 673 | if (DEBUG) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 674 | Log.d(TAG, "cancelSessionLocked()"); |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 675 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 676 | |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 677 | try { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 678 | mService.cancelSession(mSessionId, mContext.getUserId()); |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 679 | } catch (RemoteException e) { |
| 680 | throw e.rethrowFromSystemServer(); |
| 681 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 682 | |
| 683 | mSessionId = NO_SESSION; |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 686 | private void updateSessionLocked(AutofillId id, Rect bounds, AutofillValue value, int flags) { |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 687 | if (DEBUG) { |
Svet Ganov | 2f8fb1f | 2017-03-13 00:21:04 -0700 | [diff] [blame] | 688 | if (VERBOSE || (flags & FLAG_VIEW_EXITED) != 0) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 689 | Log.d(TAG, "updateSessionLocked(): id=" + id + ", bounds=" + bounds |
| 690 | + ", value=" + value + ", flags=" + flags); |
Felipe Leme | 0ab53dc | 2017-02-23 08:33:18 -0800 | [diff] [blame] | 691 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 692 | } |
Felipe Leme | 0ab53dc | 2017-02-23 08:33:18 -0800 | [diff] [blame] | 693 | |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 694 | try { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 695 | mService.updateSession(mSessionId, id, bounds, value, flags, mContext.getUserId()); |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 696 | } catch (RemoteException e) { |
| 697 | throw e.rethrowFromSystemServer(); |
| 698 | } |
| 699 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 700 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 701 | private void ensureServiceClientAddedIfNeededLocked() { |
| 702 | if (getClientLocked() == null) { |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 703 | return; |
| 704 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 705 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 706 | if (mServiceClient == null) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 707 | mServiceClient = new AutofillManagerClient(this); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 708 | try { |
| 709 | mEnabled = mService.addClient(mServiceClient, mContext.getUserId()); |
| 710 | } catch (RemoteException e) { |
| 711 | throw e.rethrowFromSystemServer(); |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 716 | /** |
| 717 | * Registers a {@link AutofillCallback} to receive autofill events. |
| 718 | * |
| 719 | * @param callback callback to receive events. |
| 720 | */ |
| 721 | public void registerCallback(@Nullable AutofillCallback callback) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame^] | 722 | if (!hasAutofillFeature()) { |
| 723 | return; |
| 724 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 725 | synchronized (mLock) { |
| 726 | if (callback == null) return; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 727 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 728 | final boolean hadCallback = mCallback != null; |
| 729 | mCallback = callback; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 730 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 731 | if (!hadCallback) { |
| 732 | try { |
| 733 | mService.setHasCallback(mSessionId, mContext.getUserId(), true); |
| 734 | } catch (RemoteException e) { |
| 735 | throw e.rethrowFromSystemServer(); |
| 736 | } |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | /** |
| 742 | * Unregisters a {@link AutofillCallback} to receive autofill events. |
| 743 | * |
| 744 | * @param callback callback to stop receiving events. |
| 745 | */ |
| 746 | public void unregisterCallback(@Nullable AutofillCallback callback) { |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame^] | 747 | if (!hasAutofillFeature()) { |
| 748 | return; |
| 749 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 750 | synchronized (mLock) { |
| 751 | if (callback == null || mCallback == null || callback != mCallback) return; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 752 | |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 753 | mCallback = null; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 754 | |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 755 | try { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 756 | mService.setHasCallback(mSessionId, mContext.getUserId(), false); |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 757 | } catch (RemoteException e) { |
| 758 | throw e.rethrowFromSystemServer(); |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 763 | private void requestShowFillUi(IBinder windowToken, AutofillId id, int width, int height, |
| 764 | Rect anchorBounds, IAutofillWindowPresenter presenter) { |
| 765 | final View anchor = findAchorView(windowToken, id); |
| 766 | if (anchor == null) { |
| 767 | return; |
| 768 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 769 | |
| 770 | AutofillCallback callback = null; |
| 771 | synchronized (mLock) { |
| 772 | if (getClientLocked().autofillCallbackRequestShowFillUi(anchor, width, height, |
| 773 | anchorBounds, presenter) && mCallback != null) { |
| 774 | callback = mCallback; |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | if (callback != null) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 779 | if (id.isVirtual()) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 780 | callback.onAutofillEvent(anchor, id.getVirtualChildId(), |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 781 | AutofillCallback.EVENT_INPUT_SHOWN); |
| 782 | } else { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 783 | callback.onAutofillEvent(anchor, AutofillCallback.EVENT_INPUT_SHOWN); |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 784 | } |
| 785 | } |
| 786 | } |
| 787 | |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 788 | /** |
| 789 | * Sets a view as autofilled if the current value is the {code targetValue}. |
| 790 | * |
| 791 | * @param view The view that is to be autofilled |
| 792 | * @param targetValue The value we want to fill into view |
| 793 | */ |
| 794 | private void setAutofilledIfValuesIs(@NonNull View view, @Nullable AutofillValue targetValue) { |
| 795 | AutofillValue currentValue = view.getAutofillValue(); |
| 796 | if (Objects.equals(currentValue, targetValue)) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 797 | synchronized (mLock) { |
| 798 | if (mLastAutofilledData == null) { |
| 799 | mLastAutofilledData = new ParcelableMap(1); |
| 800 | } |
| 801 | mLastAutofilledData.put(getAutofillId(view), targetValue); |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 802 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 803 | view.setAutofilled(true); |
| 804 | } |
| 805 | } |
| 806 | |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 807 | private void handleAutofill(IBinder windowToken, List<AutofillId> ids, |
| 808 | List<AutofillValue> values) { |
| 809 | final View root = WindowManagerGlobal.getInstance().getWindowView(windowToken); |
| 810 | if (root == null) { |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 811 | return; |
| 812 | } |
| 813 | |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 814 | final int itemCount = ids.size(); |
| 815 | int numApplied = 0; |
| 816 | ArrayMap<View, SparseArray<AutofillValue>> virtualValues = null; |
| 817 | |
| 818 | for (int i = 0; i < itemCount; i++) { |
| 819 | final AutofillId id = ids.get(i); |
| 820 | final AutofillValue value = values.get(i); |
| 821 | final int viewId = id.getViewId(); |
| 822 | final View view = root.findViewByAccessibilityIdTraversal(viewId); |
| 823 | if (view == null) { |
| 824 | Log.w(TAG, "autofill(): no View with id " + viewId); |
| 825 | continue; |
| 826 | } |
| 827 | if (id.isVirtual()) { |
| 828 | if (virtualValues == null) { |
| 829 | // Most likely there will be just one view with virtual children. |
| 830 | virtualValues = new ArrayMap<>(1); |
| 831 | } |
| 832 | SparseArray<AutofillValue> valuesByParent = virtualValues.get(view); |
| 833 | if (valuesByParent == null) { |
| 834 | // We don't know the size yet, but usually it will be just a few fields... |
| 835 | valuesByParent = new SparseArray<>(5); |
| 836 | virtualValues.put(view, valuesByParent); |
| 837 | } |
| 838 | valuesByParent.put(id.getVirtualChildId(), value); |
| 839 | } else { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 840 | synchronized (mLock) { |
| 841 | // Mark the view as to be autofilled with 'value' |
| 842 | if (mLastAutofilledData == null) { |
| 843 | mLastAutofilledData = new ParcelableMap(itemCount - i); |
| 844 | } |
| 845 | mLastAutofilledData.put(id, value); |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 846 | } |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 847 | |
Felipe Leme | 955e252 | 2017-03-29 17:47:58 -0700 | [diff] [blame] | 848 | view.autofill(value); |
Philip P. Moltmann | b42d133 | 2017-03-27 09:55:30 -0700 | [diff] [blame] | 849 | |
| 850 | // Set as autofilled if the values match now, e.g. when the value was updated |
| 851 | // synchronously. |
| 852 | // If autofill happens async, the view is set to autofilled in notifyValueChanged. |
| 853 | setAutofilledIfValuesIs(view, value); |
| 854 | |
Felipe Leme | 955e252 | 2017-03-29 17:47:58 -0700 | [diff] [blame] | 855 | numApplied++; |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | |
| 859 | if (virtualValues != null) { |
| 860 | for (int i = 0; i < virtualValues.size(); i++) { |
| 861 | final View parent = virtualValues.keyAt(i); |
| 862 | final SparseArray<AutofillValue> childrenValues = virtualValues.valueAt(i); |
Felipe Leme | 955e252 | 2017-03-29 17:47:58 -0700 | [diff] [blame] | 863 | parent.autofill(childrenValues); |
| 864 | numApplied += childrenValues.size(); |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 865 | } |
| 866 | } |
| 867 | |
| 868 | final LogMaker log = new LogMaker(MetricsProto.MetricsEvent.AUTOFILL_DATASET_APPLIED); |
| 869 | log.addTaggedData(MetricsProto.MetricsEvent.FIELD_AUTOFILL_NUM_VALUES, itemCount); |
| 870 | log.addTaggedData(MetricsProto.MetricsEvent.FIELD_AUTOFILL_NUM_VIEWS_FILLED, numApplied); |
| 871 | mMetricsLogger.write(log); |
| 872 | } |
| 873 | |
| 874 | private void requestHideFillUi(IBinder windowToken, AutofillId id) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 875 | final View anchor = findAchorView(windowToken, id); |
| 876 | |
| 877 | AutofillCallback callback = null; |
| 878 | synchronized (mLock) { |
| 879 | if (getClientLocked().autofillCallbackRequestHideFillUi() && mCallback != null) { |
| 880 | callback = mCallback; |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | if (callback != null) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 885 | if (id.isVirtual()) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 886 | callback.onAutofillEvent(anchor, id.getVirtualChildId(), |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 887 | AutofillCallback.EVENT_INPUT_HIDDEN); |
| 888 | } else { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 889 | callback.onAutofillEvent(anchor, AutofillCallback.EVENT_INPUT_HIDDEN); |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 890 | } |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | private void notifyNoFillUi(IBinder windowToken, AutofillId id) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 895 | final View anchor = findAchorView(windowToken, id); |
| 896 | |
| 897 | AutofillCallback callback; |
| 898 | synchronized (mLock) { |
| 899 | callback = mCallback; |
| 900 | } |
| 901 | |
| 902 | if (callback != null) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 903 | if (id.isVirtual()) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 904 | callback.onAutofillEvent(anchor, id.getVirtualChildId(), |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 905 | AutofillCallback.EVENT_INPUT_UNAVAILABLE); |
| 906 | } else { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 907 | callback.onAutofillEvent(anchor, AutofillCallback.EVENT_INPUT_UNAVAILABLE); |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 908 | } |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 909 | |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 910 | } |
| 911 | } |
| 912 | |
| 913 | private View findAchorView(IBinder windowToken, AutofillId id) { |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 914 | final View root = WindowManagerGlobal.getInstance().getWindowView(windowToken); |
| 915 | if (root == null) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 916 | Log.w(TAG, "no window with token " + windowToken); |
| 917 | return null; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 918 | } |
| 919 | final View view = root.findViewByAccessibilityIdTraversal(id.getViewId()); |
| 920 | if (view == null) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 921 | Log.w(TAG, "no view with id " + id); |
| 922 | return null; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 923 | } |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 924 | return view; |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 925 | } |
| 926 | |
Svet Ganov | 43574b0 | 2017-04-12 09:25:20 -0700 | [diff] [blame^] | 927 | private boolean hasAutofillFeature() { |
| 928 | return mService != null; |
| 929 | } |
| 930 | |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 931 | /** |
| 932 | * Callback for auto-fill related events. |
| 933 | * |
| 934 | * <p>Typically used for applications that display their own "auto-complete" views, so they can |
| 935 | * enable / disable such views when the auto-fill UI affordance is shown / hidden. |
| 936 | */ |
| 937 | public abstract static class AutofillCallback { |
| 938 | |
| 939 | /** @hide */ |
| 940 | @IntDef({EVENT_INPUT_SHOWN, EVENT_INPUT_HIDDEN}) |
| 941 | @Retention(RetentionPolicy.SOURCE) |
| 942 | public @interface AutofillEventType {} |
| 943 | |
| 944 | /** |
| 945 | * The auto-fill input UI affordance associated with the view was shown. |
| 946 | * |
| 947 | * <p>If the view provides its own auto-complete UI affordance and its currently shown, it |
| 948 | * should be hidden upon receiving this event. |
| 949 | */ |
| 950 | public static final int EVENT_INPUT_SHOWN = 1; |
| 951 | |
| 952 | /** |
| 953 | * The auto-fill input UI affordance associated with the view was hidden. |
| 954 | * |
| 955 | * <p>If the view provides its own auto-complete UI affordance that was hidden upon a |
| 956 | * {@link #EVENT_INPUT_SHOWN} event, it could be shown again now. |
| 957 | */ |
| 958 | public static final int EVENT_INPUT_HIDDEN = 2; |
| 959 | |
| 960 | /** |
Felipe Leme | 24aae15 | 2017-03-15 12:33:01 -0700 | [diff] [blame] | 961 | * The auto-fill input UI affordance associated with the view won't be shown because |
| 962 | * autofill is not available. |
| 963 | * |
| 964 | * <p>If the view provides its own auto-complete UI affordance but was not displaying it |
| 965 | * to avoid flickering, it could shown it upon receiving this event. |
| 966 | */ |
| 967 | public static final int EVENT_INPUT_UNAVAILABLE = 3; |
| 968 | |
| 969 | /** |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 970 | * Called after a change in the autofill state associated with a view. |
| 971 | * |
| 972 | * @param view view associated with the change. |
| 973 | * |
| 974 | * @param event currently either {@link #EVENT_INPUT_SHOWN} or {@link #EVENT_INPUT_HIDDEN}. |
| 975 | */ |
Felipe Leme | 81f01d9 | 2017-03-16 17:13:25 -0700 | [diff] [blame] | 976 | public void onAutofillEvent(@NonNull View view, @AutofillEventType int event) { |
| 977 | } |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 978 | |
| 979 | /** |
| 980 | * Called after a change in the autofill state associated with a virtual view. |
| 981 | * |
| 982 | * @param view parent view associated with the change. |
| 983 | * @param childId id identifying the virtual child inside the parent view. |
| 984 | * |
| 985 | * @param event currently either {@link #EVENT_INPUT_SHOWN} or {@link #EVENT_INPUT_HIDDEN}. |
| 986 | */ |
Felipe Leme | 81f01d9 | 2017-03-16 17:13:25 -0700 | [diff] [blame] | 987 | public void onAutofillEvent(@NonNull View view, int childId, @AutofillEventType int event) { |
| 988 | } |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 989 | } |
| 990 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 991 | private static final class AutofillManagerClient extends IAutoFillManagerClient.Stub { |
| 992 | private final WeakReference<AutofillManager> mAfm; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 993 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 994 | AutofillManagerClient(AutofillManager autofillManager) { |
| 995 | mAfm = new WeakReference<>(autofillManager); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | @Override |
| 999 | public void setState(boolean enabled) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 1000 | final AutofillManager afm = mAfm.get(); |
| 1001 | if (afm != null) { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1002 | afm.mContext.getMainThreadHandler().post(() -> { |
| 1003 | synchronized (afm.mLock) { |
| 1004 | afm.mEnabled = enabled; |
| 1005 | } |
| 1006 | }); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | @Override |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1011 | public void autofill(IBinder windowToken, List<AutofillId> ids, |
| 1012 | List<AutofillValue> values) { |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1013 | // TODO(b/33197203): must keep the dataset so subsequent calls pass the same |
| 1014 | // dataset.extras to service |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 1015 | final AutofillManager afm = mAfm.get(); |
| 1016 | if (afm != null) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1017 | afm.mContext.getMainThreadHandler().post(() -> |
| 1018 | afm.handleAutofill(windowToken, ids, values)); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | @Override |
| 1023 | public void authenticate(IntentSender intent, Intent fillInIntent) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 1024 | final AutofillManager afm = mAfm.get(); |
| 1025 | if (afm != null) { |
| 1026 | afm.mContext.getMainThreadHandler().post(() -> { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1027 | if (afm.getClientLocked() != null) { |
| 1028 | afm.getClientLocked().autofillCallbackAuthenticate(intent, fillInIntent); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1029 | } |
| 1030 | }); |
| 1031 | } |
| 1032 | } |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1033 | |
| 1034 | @Override |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1035 | public void requestShowFillUi(IBinder windowToken, AutofillId id, |
| 1036 | int width, int height, Rect anchorBounds, IAutofillWindowPresenter presenter) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 1037 | final AutofillManager afm = mAfm.get(); |
| 1038 | if (afm != null) { |
| 1039 | afm.mContext.getMainThreadHandler().post(() -> { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1040 | if (afm.getClientLocked() != null) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1041 | afm.requestShowFillUi(windowToken, id, width, |
| 1042 | height, anchorBounds, presenter); |
| 1043 | } |
| 1044 | }); |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | @Override |
| 1049 | public void requestHideFillUi(IBinder windowToken, AutofillId id) { |
| 1050 | final AutofillManager afm = mAfm.get(); |
| 1051 | if (afm != null) { |
| 1052 | afm.mContext.getMainThreadHandler().post(() -> { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1053 | if (afm.getClientLocked() != null) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1054 | afm.requestHideFillUi(windowToken, id); |
| 1055 | } |
| 1056 | }); |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | @Override |
| 1061 | public void notifyNoFillUi(IBinder windowToken, AutofillId id) { |
| 1062 | final AutofillManager afm = mAfm.get(); |
| 1063 | if (afm != null) { |
| 1064 | afm.mContext.getMainThreadHandler().post(() -> { |
Philip P. Moltmann | eab62ba | 2017-03-20 10:55:43 -0700 | [diff] [blame] | 1065 | if (afm.getClientLocked() != null) { |
Felipe Leme | 4753bb0 | 2017-03-22 20:24:00 -0700 | [diff] [blame] | 1066 | afm.notifyNoFillUi(windowToken, id); |
Felipe Leme | e6010f2 | 2017-03-03 11:19:51 -0800 | [diff] [blame] | 1067 | } |
| 1068 | }); |
| 1069 | } |
| 1070 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 1071 | } |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 1072 | } |