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; |
| 20 | |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 21 | import android.content.Context; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 22 | import android.content.Intent; |
| 23 | import android.content.IntentSender; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 24 | import android.graphics.Rect; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 25 | import android.os.Bundle; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 26 | import android.os.Parcelable; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 27 | import android.os.RemoteException; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 28 | import android.util.Log; |
| 29 | import android.view.View; |
| 30 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 31 | import java.lang.ref.WeakReference; |
| 32 | import java.util.List; |
| 33 | |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 34 | /** |
| 35 | * App entry point to the AutoFill Framework. |
| 36 | */ |
| 37 | // TODO(b/33197203): improve this javadoc |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 38 | //TODO(b/33197203): restrict manager calls to activity |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 39 | public final class AutoFillManager { |
| 40 | |
| 41 | private static final String TAG = "AutoFillManager"; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 42 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 43 | /** |
| 44 | * Intent extra: The assist structure which captures the filled screen. |
| 45 | * <p> |
| 46 | * Type: {@link android.app.assist.AssistStructure} |
| 47 | * </p> |
| 48 | */ |
| 49 | public static final String EXTRA_ASSIST_STRUCTURE = |
| 50 | "android.view.autofill.extra.ASSIST_STRUCTURE"; |
| 51 | |
| 52 | /** |
| 53 | * Intent extra: The result of an authentication operation. It is |
| 54 | * either a fully populated {@link android.service.autofill.FillResponse} |
| 55 | * or a fully populated {@link android.service.autofill.Dataset} if |
| 56 | * a response or a dataset is being authenticated respectively. |
| 57 | * |
| 58 | * <p> |
| 59 | * Type: {@link android.service.autofill.FillResponse} or a |
| 60 | * {@link android.service.autofill.Dataset} |
| 61 | * </p> |
| 62 | */ |
| 63 | public static final String EXTRA_AUTHENTICATION_RESULT = |
| 64 | "android.view.autofill.extra.AUTHENTICATION_RESULT"; |
| 65 | |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 66 | /** @hide */ public static final int FLAG_START_SESSION = 0x1; |
| 67 | /** @hide */ public static final int FLAG_FOCUS_GAINED = 0x2; |
| 68 | /** @hide */ public static final int FLAG_FOCUS_LOST = 0x4; |
| 69 | /** @hide */ public static final int FLAG_VALUE_CHANGED = 0x8; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 70 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 71 | private final Rect mTempRect = new Rect(); |
| 72 | |
| 73 | private final IAutoFillManager mService; |
| 74 | private IAutoFillManagerClient mServiceClient; |
| 75 | |
| 76 | private Context mContext; |
| 77 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 78 | private boolean mHasSession; |
| 79 | private boolean mEnabled; |
| 80 | |
| 81 | /** @hide */ |
| 82 | public interface AutoFillClient { |
| 83 | /** |
| 84 | * Asks the client to perform an auto-fill. |
| 85 | * |
| 86 | * @param ids The values to auto-fill |
| 87 | * @param values The values to auto-fill |
| 88 | */ |
| 89 | void autoFill(List<AutoFillId> ids, List<AutoFillValue> values); |
| 90 | |
| 91 | /** |
| 92 | * Asks the client to start an authentication flow. |
| 93 | * |
| 94 | * @param intent The authentication intent. |
| 95 | * @param fillInIntent The authentication fill-in intent. |
| 96 | */ |
| 97 | void authenticate(IntentSender intent, Intent fillInIntent); |
Svet Ganov | 17db9dc | 2017-02-21 19:54:31 -0800 | [diff] [blame^] | 98 | |
| 99 | /** |
| 100 | * Tells the client this manager has state to be reset. |
| 101 | */ |
| 102 | void resetableStateAvailable(); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 103 | } |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 104 | |
| 105 | /** |
| 106 | * @hide |
| 107 | */ |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 108 | public AutoFillManager(Context context, IAutoFillManager service) { |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 109 | mContext = context; |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 110 | mService = service; |
| 111 | } |
| 112 | |
| 113 | /** |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 114 | * Called to indicate the focus on an auto-fillable {@link View} changed. |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 115 | * |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 116 | * @param view view whose focus changed. |
| 117 | * @param gainFocus whether focus was gained or lost. |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 118 | */ |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 119 | public void focusChanged(View view, boolean gainFocus) { |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 120 | ensureServiceClientAddedIfNeeded(); |
| 121 | |
| 122 | if (!mEnabled) { |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 123 | return; |
| 124 | } |
| 125 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 126 | final Rect bounds = mTempRect; |
Felipe Leme | bd00fef | 2017-01-24 15:10:26 -0800 | [diff] [blame] | 127 | view.getBoundsOnScreen(bounds); |
Felipe Leme | 0200d9e | 2017-01-24 15:10:26 -0800 | [diff] [blame] | 128 | final AutoFillId id = getAutoFillId(view); |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 129 | final AutoFillValue value = view.getAutoFillValue(); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 130 | |
| 131 | if (!mHasSession) { |
| 132 | if (gainFocus) { |
| 133 | // Starts new session. |
| 134 | startSession(id, bounds, value); |
| 135 | } |
| 136 | } else { |
| 137 | // Update focus on existing session. |
| 138 | updateSession(id, bounds, value, gainFocus ? FLAG_FOCUS_GAINED : FLAG_FOCUS_LOST); |
| 139 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Called to indicate the focus on an auto-fillable virtual {@link View} changed. |
| 144 | * |
| 145 | * @param parent parent view whose focus changed. |
| 146 | * @param childId id identifying the virtual child inside the parent view. |
| 147 | * @param bounds child boundaries, relative to the top window. |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 148 | * @param gainFocus whether focus was gained or lost. |
| 149 | */ |
Felipe Leme | 7f60d35 | 2017-02-07 17:14:52 -0800 | [diff] [blame] | 150 | public void virtualFocusChanged(View parent, int childId, Rect bounds, boolean gainFocus) { |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 151 | ensureServiceClientAddedIfNeeded(); |
| 152 | |
| 153 | if (!mEnabled) { |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 154 | return; |
| 155 | } |
| 156 | |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 157 | final AutoFillId id = getAutoFillId(parent, childId); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 158 | |
| 159 | if (!mHasSession) { |
| 160 | if (gainFocus) { |
| 161 | // Starts new session. |
| 162 | startSession(id, bounds, null); |
| 163 | } |
| 164 | } else { |
| 165 | // Update focus on existing session. |
| 166 | updateSession(id, bounds, null, gainFocus ? FLAG_FOCUS_GAINED : FLAG_FOCUS_LOST); |
| 167 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Called to indicate the value of an auto-fillable {@link View} changed. |
| 172 | * |
| 173 | * @param view view whose focus changed. |
| 174 | */ |
| 175 | public void valueChanged(View view) { |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 176 | if (!mEnabled || !mHasSession) { |
| 177 | return; |
| 178 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 179 | |
| 180 | final AutoFillId id = getAutoFillId(view); |
| 181 | final AutoFillValue value = view.getAutoFillValue(); |
| 182 | updateSession(id, null, value, FLAG_VALUE_CHANGED); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | /** |
| 187 | * Called to indicate the value of an auto-fillable virtual {@link View} changed. |
| 188 | * |
| 189 | * @param parent parent view whose value changed. |
| 190 | * @param childId id identifying the virtual child inside the parent view. |
| 191 | * @param value new value of the child. |
| 192 | */ |
| 193 | public void virtualValueChanged(View parent, int childId, AutoFillValue value) { |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 194 | if (!mEnabled || !mHasSession) { |
| 195 | return; |
| 196 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 197 | |
| 198 | final AutoFillId id = getAutoFillId(parent, childId); |
| 199 | updateSession(id, null, value, FLAG_VALUE_CHANGED); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Called to indicate the current auto-fill context should be reset. |
| 204 | * |
| 205 | * <p>For example, when a virtual view is rendering an {@code HTML} page with a form, it should |
| 206 | * call this method after the form is submitted and another page is rendered. |
| 207 | */ |
| 208 | public void reset() { |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 209 | if (!mEnabled && !mHasSession) { |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 210 | return; |
| 211 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 212 | |
| 213 | finishSession(); |
Felipe Leme | 0200d9e | 2017-01-24 15:10:26 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 216 | private AutoFillClient getClient() { |
Svet Ganov | 17db9dc | 2017-02-21 19:54:31 -0800 | [diff] [blame^] | 217 | if (mContext instanceof AutoFillClient) { |
| 218 | return (AutoFillClient) mContext; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 219 | } |
Svet Ganov | 17db9dc | 2017-02-21 19:54:31 -0800 | [diff] [blame^] | 220 | return null; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** @hide */ |
| 224 | public void onAuthenticationResult(Intent data) { |
Felipe Leme | d633f07 | 2017-02-14 10:17:17 -0800 | [diff] [blame] | 225 | // TODO(b/33197203): the result code is being ignored, so this method is not reliably |
| 226 | // handling the cases where it's not RESULT_OK: it works fine if the service does not |
| 227 | // set the EXTRA_AUTHENTICATION_RESULT extra, but it could cause weird results if the |
| 228 | // service set the extra and returned RESULT_CANCELED... |
| 229 | |
| 230 | if (DEBUG) Log.d(TAG, "onAuthenticationResult(): d=" + data); |
| 231 | |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 232 | if (data == null) { |
| 233 | return; |
| 234 | } |
Felipe Leme | d633f07 | 2017-02-14 10:17:17 -0800 | [diff] [blame] | 235 | final Parcelable result = data.getParcelableExtra(EXTRA_AUTHENTICATION_RESULT); |
| 236 | final Bundle responseData = new Bundle(); |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 237 | responseData.putParcelable(EXTRA_AUTHENTICATION_RESULT, result); |
| 238 | try { |
| 239 | mService.setAuthenticationResult(responseData, |
| 240 | mContext.getActivityToken(), mContext.getUserId()); |
| 241 | } catch (RemoteException e) { |
| 242 | Log.e(TAG, "Error delivering authentication result", e); |
| 243 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Felipe Leme | 0200d9e | 2017-01-24 15:10:26 -0800 | [diff] [blame] | 246 | private AutoFillId getAutoFillId(View view) { |
| 247 | return new AutoFillId(view.getAccessibilityViewId()); |
| 248 | } |
| 249 | |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 250 | private AutoFillId getAutoFillId(View parent, int childId) { |
| 251 | return new AutoFillId(parent.getAccessibilityViewId(), childId); |
| 252 | } |
| 253 | |
| 254 | private void startSession(AutoFillId id, Rect bounds, AutoFillValue value) { |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 255 | if (DEBUG) { |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 256 | Log.v(TAG, "startSession(): id=" + id + ", bounds=" + bounds + ", value=" + value); |
| 257 | } |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 258 | try { |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 259 | mService.startSession(mContext.getActivityToken(), mServiceClient.asBinder(), |
| 260 | id, bounds, value, mContext.getUserId()); |
Svet Ganov | 17db9dc | 2017-02-21 19:54:31 -0800 | [diff] [blame^] | 261 | AutoFillClient client = getClient(); |
| 262 | if (client != null) { |
| 263 | client.resetableStateAvailable(); |
| 264 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 265 | mHasSession = true; |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 266 | } catch (RemoteException e) { |
| 267 | throw e.rethrowFromSystemServer(); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | private void finishSession() { |
| 272 | if (DEBUG) { |
| 273 | Log.v(TAG, "finishSession()"); |
| 274 | } |
| 275 | mHasSession = false; |
| 276 | try { |
| 277 | mService.finishSession(mContext.getActivityToken(), mContext.getUserId()); |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 278 | } catch (RemoteException e) { |
| 279 | throw e.rethrowFromSystemServer(); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | private void updateSession(AutoFillId id, Rect bounds, AutoFillValue value, int flags) { |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 284 | if (DEBUG) { |
Felipe Leme | bab851c | 2017-02-03 18:45:08 -0800 | [diff] [blame] | 285 | Log.v(TAG, "updateSession(): id=" + id + ", bounds=" + bounds + ", value=" + value |
| 286 | + ", flags=" + flags); |
| 287 | } |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 288 | try { |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 289 | mService.updateSession(mContext.getActivityToken(), id, bounds, value, flags, |
| 290 | mContext.getUserId()); |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 291 | } catch (RemoteException e) { |
| 292 | throw e.rethrowFromSystemServer(); |
| 293 | } |
| 294 | } |
Svet Ganov | 782043c | 2017-02-11 00:52:02 +0000 | [diff] [blame] | 295 | |
| 296 | private void ensureServiceClientAddedIfNeeded() { |
| 297 | if (getClient() == null) { |
| 298 | return; |
| 299 | } |
| 300 | if (mServiceClient == null) { |
| 301 | mServiceClient = new AutoFillManagerClient(this); |
| 302 | try { |
| 303 | mEnabled = mService.addClient(mServiceClient, mContext.getUserId()); |
| 304 | } catch (RemoteException e) { |
| 305 | throw e.rethrowFromSystemServer(); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | private static final class AutoFillManagerClient extends IAutoFillManagerClient.Stub { |
| 311 | private final WeakReference<AutoFillManager> mAutoFillManager; |
| 312 | |
| 313 | AutoFillManagerClient(AutoFillManager autoFillManager) { |
| 314 | mAutoFillManager = new WeakReference<>(autoFillManager); |
| 315 | } |
| 316 | |
| 317 | @Override |
| 318 | public void setState(boolean enabled) { |
| 319 | final AutoFillManager autoFillManager = mAutoFillManager.get(); |
| 320 | if (autoFillManager != null) { |
| 321 | autoFillManager.mContext.getMainThreadHandler().post(() -> |
| 322 | autoFillManager.mEnabled = enabled); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | @Override |
| 327 | public void autoFill(List<AutoFillId> ids, List<AutoFillValue> values) { |
| 328 | // TODO(b/33197203): must keep the dataset so subsequent calls pass the same |
| 329 | // dataset.extras to service |
| 330 | final AutoFillManager autoFillManager = mAutoFillManager.get(); |
| 331 | if (autoFillManager != null) { |
| 332 | autoFillManager.mContext.getMainThreadHandler().post(() -> { |
| 333 | if (autoFillManager.getClient() != null) { |
| 334 | autoFillManager.getClient().autoFill(ids, values); |
| 335 | } |
| 336 | }); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | @Override |
| 341 | public void authenticate(IntentSender intent, Intent fillInIntent) { |
| 342 | final AutoFillManager autoFillManager = mAutoFillManager.get(); |
| 343 | if (autoFillManager != null) { |
| 344 | autoFillManager.mContext.getMainThreadHandler().post(() -> { |
| 345 | if (autoFillManager.getClient() != null) { |
| 346 | autoFillManager.getClient().authenticate(intent, fillInIntent); |
| 347 | } |
| 348 | }); |
| 349 | } |
| 350 | } |
| 351 | } |
Felipe Leme | 3461d3c | 2017-01-19 08:54:55 -0800 | [diff] [blame] | 352 | } |