Felipe Leme | 640f30a | 2017-03-06 15:44:06 -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 | package android.service.autofill; |
| 17 | |
Svet Ganov | ecfa58a | 2017-05-05 19:38:45 -0700 | [diff] [blame] | 18 | import android.annotation.CallSuper; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 21 | import android.annotation.SdkConstant; |
Felipe Leme | 052562c | 2017-07-27 11:45:24 -0700 | [diff] [blame] | 22 | import android.app.Service; |
| 23 | import android.content.Intent; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 24 | import android.os.CancellationSignal; |
| 25 | import android.os.IBinder; |
| 26 | import android.os.ICancellationSignal; |
| 27 | import android.os.Looper; |
Felipe Leme | 052562c | 2017-07-27 11:45:24 -0700 | [diff] [blame] | 28 | import android.os.RemoteException; |
| 29 | import android.provider.Settings; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 30 | import android.util.Log; |
Felipe Leme | 2ef19c1 | 2017-06-05 11:32:32 -0700 | [diff] [blame] | 31 | import android.view.View; |
| 32 | import android.view.ViewStructure; |
| 33 | import android.view.autofill.AutofillId; |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 34 | import android.view.autofill.AutofillManager; |
Felipe Leme | 2ef19c1 | 2017-06-05 11:32:32 -0700 | [diff] [blame] | 35 | import android.view.autofill.AutofillValue; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 36 | |
Felipe Leme | 052562c | 2017-07-27 11:45:24 -0700 | [diff] [blame] | 37 | import com.android.internal.os.HandlerCaller; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 38 | import com.android.internal.os.SomeArgs; |
| 39 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 40 | /** |
Felipe Leme | 2ef19c1 | 2017-06-05 11:32:32 -0700 | [diff] [blame] | 41 | * An {@code AutofillService} is a service used to automatically fill the contents of the screen |
| 42 | * on behalf of a given user - for more information about autofill, read |
| 43 | * <a href="{@docRoot}preview/features/autofill.html">Autofill Framework</a>. |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 44 | * |
Felipe Leme | 2ef19c1 | 2017-06-05 11:32:32 -0700 | [diff] [blame] | 45 | * <p>An {@code AutofillService} is only bound to the Android System for autofill purposes if: |
| 46 | * <ol> |
| 47 | * <li>It requires the {@code android.permission.BIND_AUTOFILL_SERVICE} permission in its |
| 48 | * manifest. |
| 49 | * <li>The user explicitly enables it using Android Settings (the |
| 50 | * {@link Settings#ACTION_REQUEST_SET_AUTOFILL_SERVICE} intent can be used to launch such |
| 51 | * Settings screen). |
| 52 | * </ol> |
| 53 | * |
| 54 | * <h3>Basic usage</h3> |
| 55 | * |
| 56 | * <p>The basic autofill process is defined by the workflow below: |
| 57 | * <ol> |
| 58 | * <li>User focus an editable {@link View}. |
| 59 | * <li>View calls {@link AutofillManager#notifyViewEntered(android.view.View)}. |
| 60 | * <li>A {@link ViewStructure} representing all views in the screen is created. |
| 61 | * <li>The Android System binds to the service and calls {@link #onConnected()}. |
| 62 | * <li>The service receives the view structure through the |
| 63 | * {@link #onFillRequest(FillRequest, CancellationSignal, FillCallback)}. |
| 64 | * <li>The service replies through {@link FillCallback#onSuccess(FillResponse)}. |
| 65 | * <li>The Android System calls {@link #onDisconnected()} and unbinds from the |
| 66 | * {@code AutofillService}. |
| 67 | * <li>The Android System displays an UI affordance with the options sent by the service. |
| 68 | * <li>The user picks an option. |
| 69 | * <li>The proper views are autofilled. |
| 70 | * </ol> |
| 71 | * |
| 72 | * <p>This workflow was designed to minimize the time the Android System is bound to the service; |
| 73 | * for each call, it: binds to service, waits for the reply, and unbinds right away. Furthermore, |
| 74 | * those calls are considered stateless: if the service needs to keep state between calls, it must |
| 75 | * do its own state management (keeping in mind that the service's process might be killed by the |
| 76 | * Android System when unbound; for example, if the device is running low in memory). |
| 77 | * |
| 78 | * <p>Typically, the |
| 79 | * {@link #onFillRequest(FillRequest, CancellationSignal, FillCallback)} will: |
| 80 | * <ol> |
| 81 | * <li>Parse the view structure looking for autofillable views (for example, using |
| 82 | * {@link android.app.assist.AssistStructure.ViewNode#getAutofillHints()}. |
| 83 | * <li>Match the autofillable views with the user's data. |
| 84 | * <li>Create a {@link Dataset} for each set of user's data that match those fields. |
| 85 | * <li>Fill the dataset(s) with the proper {@link AutofillId}s and {@link AutofillValue}s. |
| 86 | * <li>Add the dataset(s) to the {@link FillResponse} passed to |
| 87 | * {@link FillCallback#onSuccess(FillResponse)}. |
| 88 | * </ol> |
| 89 | * |
| 90 | * <p>For example, for a login screen with username and password views where the user only has one |
| 91 | * account in the service, the response could be: |
| 92 | * |
| 93 | * <pre class="prettyprint"> |
| 94 | * new FillResponse.Builder() |
| 95 | * .addDataset(new Dataset.Builder() |
| 96 | * .setValue(id1, AutofillValue.forText("homer"), createPresentation("homer")) |
| 97 | * .setValue(id2, AutofillValue.forText("D'OH!"), createPresentation("password for homer")) |
| 98 | * .build()) |
| 99 | * .build(); |
| 100 | * </pre> |
| 101 | * |
| 102 | * <p>But if the user had 2 accounts instead, the response could be: |
| 103 | * |
| 104 | * <pre class="prettyprint"> |
| 105 | * new FillResponse.Builder() |
| 106 | * .addDataset(new Dataset.Builder() |
| 107 | * .setValue(id1, AutofillValue.forText("homer"), createPresentation("homer")) |
| 108 | * .setValue(id2, AutofillValue.forText("D'OH!"), createPresentation("password for homer")) |
| 109 | * .build()) |
| 110 | * .addDataset(new Dataset.Builder() |
| 111 | * .setValue(id1, AutofillValue.forText("flanders"), createPresentation("flanders")) |
| 112 | * .setValue(id2, AutofillValue.forText("OkelyDokelyDo"), createPresentation("password for flanders")) |
| 113 | * .build()) |
| 114 | * .build(); |
| 115 | * </pre> |
| 116 | * |
| 117 | * <p>If the service does not find any autofillable view in the view structure, it should pass |
| 118 | * {@code null} to {@link FillCallback#onSuccess(FillResponse)}; if the service encountered an error |
| 119 | * processing the request, it should call {@link FillCallback#onFailure(CharSequence)}. For |
| 120 | * performance reasons, it's paramount that the service calls either |
| 121 | * {@link FillCallback#onSuccess(FillResponse)} or {@link FillCallback#onFailure(CharSequence)} for |
| 122 | * each {@link #onFillRequest(FillRequest, CancellationSignal, FillCallback)} received - if it |
| 123 | * doesn't, the request will eventually time out and be discarded by the Android System. |
| 124 | * |
| 125 | * <h3>Saving user data</h3> |
| 126 | * |
| 127 | * <p>If the service is also interested on saving the data filled by the user, it must set a |
| 128 | * {@link SaveInfo} object in the {@link FillResponse}. See {@link SaveInfo} for more details and |
| 129 | * examples. |
| 130 | * |
| 131 | * <h3>User authentication</h3> |
| 132 | * |
| 133 | * <p>The service can provide an extra degree of security by requiring the user to authenticate |
| 134 | * before an app can be autofilled. The authentication is typically required in 2 scenarios: |
| 135 | * <ul> |
| 136 | * <li>To unlock the user data (for example, using a master password or fingerprint |
| 137 | * authentication) - see |
| 138 | * {@link FillResponse.Builder#setAuthentication(AutofillId[], android.content.IntentSender, android.widget.RemoteViews)}. |
| 139 | * <li>To unlock a specific dataset (for example, by providing a CVC for a credit card) - see |
| 140 | * {@link Dataset.Builder#setAuthentication(android.content.IntentSender)}. |
| 141 | * </ul> |
| 142 | * |
| 143 | * <p>When using authentication, it is recommended to encrypt only the sensitive data and leave |
| 144 | * labels unencrypted, so they can be used on presentation views. For example, if the user has a |
| 145 | * home and a work address, the {@code Home} and {@code Work} labels should be stored unencrypted |
| 146 | * (since they don't have any sensitive data) while the address data per se could be stored in an |
| 147 | * encrypted storage. Then when the user chooses the {@code Home} dataset, the platform starts |
| 148 | * the authentication flow, and the service can decrypt the sensitive data. |
| 149 | * |
| 150 | * <p>The authentication mechanism can also be used in scenarios where the service needs multiple |
| 151 | * steps to determine the datasets that can fill a screen. For example, when autofilling a financial |
| 152 | * app where the user has accounts for multiple banks, the workflow could be: |
| 153 | * |
| 154 | * <ol> |
| 155 | * <li>The first {@link FillResponse} contains datasets with the credentials for the financial |
| 156 | * app, plus a "fake" dataset whose presentation says "Tap here for banking apps credentials". |
| 157 | * <li>When the user selects the fake dataset, the service displays a dialog with available |
| 158 | * banking apps. |
| 159 | * <li>When the user select a banking app, the service replies with a new {@link FillResponse} |
| 160 | * containing the datasets for that bank. |
| 161 | * </ol> |
| 162 | * |
| 163 | * <p>Another example of multiple-steps dataset selection is when the service stores the user |
| 164 | * credentials in "vaults": the first response would contain fake datasets with the vault names, |
| 165 | * and the subsequent response would contain the app credentials stored in that vault. |
| 166 | * |
| 167 | * <h3>Data partitioning</h3> |
| 168 | * |
| 169 | * <p>The autofillable views in a screen should be grouped in logical groups called "partitions". |
| 170 | * Typical partitions are: |
| 171 | * <ul> |
| 172 | * <li>Credentials (username/email address, password). |
| 173 | * <li>Address (street, city, state, zip code, etc). |
| 174 | * <li>Payment info (credit card number, expiration date, and verification code). |
| 175 | * </ul> |
| 176 | * <p>For security reasons, when a screen has more than one partition, it's paramount that the |
| 177 | * contents of a dataset do not spawn multiple partitions, specially when one of the partitions |
| 178 | * contains data that is not specific to the application being autofilled. For example, a dataset |
| 179 | * should not contain fields for username, password, and credit card information. The reason for |
| 180 | * this rule is that a malicious app could draft a view structure where the credit card fields |
| 181 | * are not visible, so when the user selects a dataset from the username UI, the credit card info is |
Felipe Leme | 2fb64c0 | 2017-07-31 11:34:14 -0700 | [diff] [blame] | 182 | * released to the application without the user knowledge. Similarly, it's recommended to always |
Felipe Leme | 2ef19c1 | 2017-06-05 11:32:32 -0700 | [diff] [blame] | 183 | * protect a dataset that contains sensitive information by requiring dataset authentication |
Felipe Leme | 2fb64c0 | 2017-07-31 11:34:14 -0700 | [diff] [blame] | 184 | * (see {@link Dataset.Builder#setAuthentication(android.content.IntentSender)}), and to include |
| 185 | * info about the "primary" field of the partition in the custom presentation for "secondary" |
| 186 | * fields — that would prevent a malicious app from getting the "primary" fields without the |
| 187 | * user realizing they're being released (for example, a malicious app could have fields for a |
| 188 | * credit card number, verification code, and expiration date crafted in a way that just the latter |
| 189 | * is visible; by explicitly indicating the expiration date is related to a given credit card |
| 190 | * number, the service would be providing a visual clue for the users to check what would be |
| 191 | * released upon selecting that field). |
Felipe Leme | 2ef19c1 | 2017-06-05 11:32:32 -0700 | [diff] [blame] | 192 | * |
Felipe Leme | 2fb64c0 | 2017-07-31 11:34:14 -0700 | [diff] [blame] | 193 | * <p>When the service detects that a screen has multiple partitions, it should return a |
Felipe Leme | 2ef19c1 | 2017-06-05 11:32:32 -0700 | [diff] [blame] | 194 | * {@link FillResponse} with just the datasets for the partition that originated the request (i.e., |
| 195 | * the partition that has the {@link android.app.assist.AssistStructure.ViewNode} whose |
| 196 | * {@link android.app.assist.AssistStructure.ViewNode#isFocused()} returns {@code true}); then if |
| 197 | * the user selects a field from a different partition, the Android System will make another |
| 198 | * {@link #onFillRequest(FillRequest, CancellationSignal, FillCallback)} call for that partition, |
| 199 | * and so on. |
| 200 | * |
| 201 | * <p>Notice that when the user autofill a partition with the data provided by the service and the |
| 202 | * user did not change these fields, the autofilled value is sent back to the service in the |
| 203 | * subsequent calls (and can be obtained by calling |
| 204 | * {@link android.app.assist.AssistStructure.ViewNode#getAutofillValue()}). This is useful in the |
| 205 | * cases where the service must create datasets for a partition based on the choice made in a |
| 206 | * previous partition. For example, the 1st response for a screen that have credentials and address |
| 207 | * partitions could be: |
| 208 | * |
| 209 | * <pre class="prettyprint"> |
| 210 | * new FillResponse.Builder() |
| 211 | * .addDataset(new Dataset.Builder() // partition 1 (credentials) |
| 212 | * .setValue(id1, AutofillValue.forText("homer"), createPresentation("homer")) |
| 213 | * .setValue(id2, AutofillValue.forText("D'OH!"), createPresentation("password for homer")) |
| 214 | * .build()) |
| 215 | * .addDataset(new Dataset.Builder() // partition 1 (credentials) |
| 216 | * .setValue(id1, AutofillValue.forText("flanders"), createPresentation("flanders")) |
| 217 | * .setValue(id2, AutofillValue.forText("OkelyDokelyDo"), createPresentation("password for flanders")) |
| 218 | * .build()) |
| 219 | * .setSaveInfo(new SaveInfo.Builder(SaveInfo.SAVE_DATA_TYPE_PASSWORD, |
| 220 | * new AutofillId[] { id1, id2 }) |
| 221 | * .build()) |
| 222 | * .build(); |
| 223 | * </pre> |
| 224 | * |
| 225 | * <p>Then if the user selected {@code flanders}, the service would get a new |
| 226 | * {@link #onFillRequest(FillRequest, CancellationSignal, FillCallback)} call, with the values of |
| 227 | * the fields {@code id1} and {@code id2} prepopulated, so the service could then fetch the address |
| 228 | * for the Flanders account and return the following {@link FillResponse} for the address partition: |
| 229 | * |
| 230 | * <pre class="prettyprint"> |
| 231 | * new FillResponse.Builder() |
| 232 | * .addDataset(new Dataset.Builder() // partition 2 (address) |
| 233 | * .setValue(id3, AutofillValue.forText("744 Evergreen Terrace"), createPresentation("744 Evergreen Terrace")) // street |
| 234 | * .setValue(id4, AutofillValue.forText("Springfield"), createPresentation("Springfield")) // city |
| 235 | * .build()) |
| 236 | * .setSaveInfo(new SaveInfo.Builder(SaveInfo.SAVE_DATA_TYPE_PASSWORD | SaveInfo.SAVE_DATA_TYPE_ADDRESS, |
| 237 | * new AutofillId[] { id1, id2 }) // username and password |
| 238 | * .setOptionalIds(new AutofillId[] { id3, id4 }) // state and zipcode |
| 239 | * .build()) |
| 240 | * .build(); |
| 241 | * </pre> |
| 242 | * |
| 243 | * <p>When the service returns multiple {@link FillResponse}, the last one overrides the previous; |
| 244 | * that's why the {@link SaveInfo} in the 2nd request above has the info for both partitions. |
| 245 | * |
Felipe Leme | 2fb64c0 | 2017-07-31 11:34:14 -0700 | [diff] [blame] | 246 | * <h3>Package verification</h3> |
| 247 | * |
| 248 | * <p>When autofilling app-specific data (like username and password), the service must verify |
| 249 | * the authenticity of the request by obtaining all signing certificates of the app being |
| 250 | * autofilled, and only fulfilling the request when they match the values that were |
| 251 | * obtained when the data was first saved — such verification is necessary to avoid phishing |
| 252 | * attempts by apps that were sideloaded in the device with the same package name of another app. |
| 253 | * Here's an example on how to achieve that by hashing the signing certificates: |
| 254 | * |
| 255 | * <pre class="prettyprint"> |
| 256 | * private String getCertificatesHash(String packageName) throws Exception { |
| 257 | * PackageManager pm = mContext.getPackageManager(); |
| 258 | * PackageInfo info = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES); |
| 259 | * ArrayList<String> hashes = new ArrayList<>(info.signatures.length); |
| 260 | * for (Signature sig : info.signatures) { |
| 261 | * byte[] cert = sig.toByteArray(); |
| 262 | * MessageDigest md = MessageDigest.getInstance("SHA-256"); |
| 263 | * md.update(cert); |
| 264 | * hashes.add(toHexString(md.digest())); |
| 265 | * } |
| 266 | * Collections.sort(hashes); |
| 267 | * StringBuilder hash = new StringBuilder(); |
| 268 | * for (int i = 0; i < hashes.size(); i++) { |
| 269 | * hash.append(hashes.get(i)); |
| 270 | * } |
| 271 | * return hash.toString(); |
| 272 | * } |
| 273 | * |
| 274 | * </pre> |
| 275 | * |
Felipe Leme | 2ef19c1 | 2017-06-05 11:32:32 -0700 | [diff] [blame] | 276 | * <h3>Ignoring views</h3> |
| 277 | * |
| 278 | * <p>If the service find views that cannot be autofilled (for example, a text field representing |
| 279 | * the response to a Captcha challenge), it should mark those views as ignored by |
| 280 | * calling {@link FillResponse.Builder#setIgnoredIds(AutofillId...)} so the system does not trigger |
| 281 | * a new {@link #onFillRequest(FillRequest, CancellationSignal, FillCallback)} when these views are |
| 282 | * focused. |
Felipe Leme | 303b609 | 2017-07-31 18:01:15 -0700 | [diff] [blame] | 283 | * |
| 284 | * <h3>Web security</h3> |
| 285 | * |
| 286 | * <p>When handling autofill requests that represent web pages (typically |
| 287 | * view structures whose root's {@link android.app.assist.AssistStructure.ViewNode#getClassName()} |
| 288 | * is a {@link android.webkit.WebView}), the service should take the following steps to verify if |
| 289 | * the structure can be autofilled with the data associated with the app requesting it: |
| 290 | * |
| 291 | * <ol> |
| 292 | * <li>Use the {@link android.app.assist.AssistStructure.ViewNode#getWebDomain()} to get the |
| 293 | * source of the document. |
| 294 | * <li>Get the canonical domain using the |
| 295 | * <a href="https://publicsuffix.org/>Public Suffix List</a> (see example below). |
| 296 | * <li>Use <a href="https://developers.google.com/digital-asset-links/">Digital Asset Links</a> |
| 297 | * to obtain the package name and certificate fingerprint of the package corresponding to |
| 298 | * the canonical domain. |
| 299 | * <li>Make sure the certificate fingerprint matches the value returned by Package Manager |
| 300 | * (see "Package verification" section above). |
| 301 | * </ol> |
| 302 | * |
| 303 | * <p>Here's an example on how to get the canonical domain using |
| 304 | * <a href="https://github.com/google/guava">Guava</a>: |
| 305 | * |
| 306 | * <pre class="prettyprint"> |
| 307 | * private static String getCanonicalDomain(String domain) { |
| 308 | * InternetDomainName idn = InternetDomainName.from(domain); |
| 309 | * while (!idn.isTopPrivateDomain() && idn != null) { |
| 310 | * idn = idn.parent(); |
| 311 | * } |
| 312 | * return idn == null ? null : idn.toString(); |
| 313 | * } |
| 314 | * </pre> |
| 315 | * |
| 316 | * <p>If the association between the web domain and app package cannot be verified through the steps |
| 317 | * above, the service can still autofill the app, but it should warn the user about the potential |
| 318 | * data leakage first, and askfor the user to confirm. For example, the service could: |
| 319 | * |
| 320 | * <ol> |
| 321 | * <li>Create a dataset that requires |
| 322 | * {@link Dataset.Builder#setAuthentication(android.content.IntentSender) authentication} to |
| 323 | * unlock. |
| 324 | * <li>Include the web domain in the custom presentation for the |
| 325 | * {@link Dataset.Builder#setValue(AutofillId, AutofillValue, android.widget.RemoteViews) |
| 326 | * dataset value}. |
| 327 | * <li>When the user select that dataset, show a disclaimer dialog explaining that the app is |
| 328 | * requesting credentials for a web domain, but the service could not verify if the app owns |
| 329 | * that domain. If the user agrees, then the service can unlock the dataset. |
| 330 | * <li>Similarly, when adding a {@link SaveInfo} object for the request, the service should |
| 331 | * include the above disclaimer in the {@link SaveInfo.Builder#setDescription(CharSequence)}. |
| 332 | * </ol> |
| 333 | * |
| 334 | * <p>This same procedure could also be used when the autofillable data is contained inside an |
| 335 | * {@code IFRAME}, in which case the WebView generates a new autofill context when a node inside |
| 336 | * the {@code IFRAME} is focused, which the root node containing the {@code IFRAME}'s {@code src} |
| 337 | * attribute on {@link android.app.assist.AssistStructure.ViewNode#getWebDomain()}. A typical and |
| 338 | * legitimate use case for this scenario is a financial app that allows the user |
| 339 | * to login on different bank accounts. For example, a financial app {@code my_financial_app} could |
| 340 | * use a WebView that loads contents from {@code banklogin.my_financial_app.com}, which contains an |
| 341 | * {@code IFRAME} node whose {@code src} attribute is {@code login.some_bank.com}. When fulfilling |
| 342 | * that request, the service could add an |
| 343 | * {@link Dataset.Builder#setAuthentication(android.content.IntentSender) authenticated dataset} |
| 344 | * whose presentation displays "Username for some_bank.com" and |
| 345 | * "Password for some_bank.com". Then when the user taps one of these options, the service |
| 346 | * shows the disclaimer dialog explaining that selecting that option would release the |
| 347 | * {@code login.some_bank.com} credentials to the {@code my_financial_app}; if the user agrees, |
| 348 | * then the service returns an unlocked dataset with the {@code some_bank.com} credentials. |
| 349 | * |
| 350 | * <p><b>Note:</b> The autofill service could also whitelist well-known browser apps and skip the |
| 351 | * verifications above, as long as the service can verify the authenticity of the browser app by |
| 352 | * checking its signing certificate. |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 353 | */ |
| 354 | public abstract class AutofillService extends Service { |
| 355 | private static final String TAG = "AutofillService"; |
| 356 | |
| 357 | /** |
| 358 | * The {@link Intent} that must be declared as handled by the service. |
| 359 | * To be supported, the service must also require the |
Felipe Leme | decd887 | 2017-04-26 17:42:38 -0700 | [diff] [blame] | 360 | * {@link android.Manifest.permission#BIND_AUTOFILL_SERVICE} permission so |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 361 | * that other applications can not abuse it. |
| 362 | */ |
| 363 | @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION) |
| 364 | public static final String SERVICE_INTERFACE = "android.service.autofill.AutofillService"; |
| 365 | |
| 366 | /** |
| 367 | * Name under which a AutoFillService component publishes information about itself. |
| 368 | * This meta-data should reference an XML resource containing a |
| 369 | * <code><{@link |
Felipe Leme | f78e952 | 2017-04-04 15:07:13 -0700 | [diff] [blame] | 370 | * android.R.styleable#AutofillService autofill-service}></code> tag. |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 371 | * This is a a sample XML file configuring an AutoFillService: |
| 372 | * <pre> <autofill-service |
| 373 | * android:settingsActivity="foo.bar.SettingsActivity" |
| 374 | * . . . |
| 375 | * /></pre> |
| 376 | */ |
| 377 | public static final String SERVICE_META_DATA = "android.autofill"; |
| 378 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 379 | // Handler messages. |
| 380 | private static final int MSG_CONNECT = 1; |
| 381 | private static final int MSG_DISCONNECT = 2; |
| 382 | private static final int MSG_ON_FILL_REQUEST = 3; |
| 383 | private static final int MSG_ON_SAVE_REQUEST = 4; |
| 384 | |
| 385 | private final IAutoFillService mInterface = new IAutoFillService.Stub() { |
| 386 | @Override |
Svet Ganov | f20a037 | 2017-04-10 17:08:05 -0700 | [diff] [blame] | 387 | public void onConnectedStateChanged(boolean connected) { |
| 388 | if (connected) { |
| 389 | mHandlerCaller.obtainMessage(MSG_CONNECT).sendToTarget(); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 390 | } else { |
| 391 | mHandlerCaller.obtainMessage(MSG_DISCONNECT).sendToTarget(); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | @Override |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 396 | public void onFillRequest(FillRequest request, IFillCallback callback) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 397 | ICancellationSignal transport = CancellationSignal.createTransport(); |
| 398 | try { |
| 399 | callback.onCancellable(transport); |
| 400 | } catch (RemoteException e) { |
| 401 | e.rethrowFromSystemServer(); |
| 402 | } |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 403 | mHandlerCaller.obtainMessageOOO(MSG_ON_FILL_REQUEST, request, |
| 404 | CancellationSignal.fromTransport(transport), callback) |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 405 | .sendToTarget(); |
| 406 | } |
| 407 | |
| 408 | @Override |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 409 | public void onSaveRequest(SaveRequest request, ISaveCallback callback) { |
| 410 | mHandlerCaller.obtainMessageOO(MSG_ON_SAVE_REQUEST, request, |
| 411 | callback).sendToTarget(); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 412 | } |
| 413 | }; |
| 414 | |
| 415 | private final HandlerCaller.Callback mHandlerCallback = (msg) -> { |
| 416 | switch (msg.what) { |
| 417 | case MSG_CONNECT: { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 418 | onConnected(); |
| 419 | break; |
| 420 | } case MSG_ON_FILL_REQUEST: { |
| 421 | final SomeArgs args = (SomeArgs) msg.obj; |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 422 | final FillRequest request = (FillRequest) args.arg1; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 423 | final CancellationSignal cancellation = (CancellationSignal) args.arg2; |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 424 | final IFillCallback callback = (IFillCallback) args.arg3; |
| 425 | final FillCallback fillCallback = new FillCallback(callback, request.getId()); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 426 | args.recycle(); |
Felipe Leme | 73fedac | 2017-05-12 09:52:07 -0700 | [diff] [blame] | 427 | onFillRequest(request, cancellation, fillCallback); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 428 | break; |
| 429 | } case MSG_ON_SAVE_REQUEST: { |
| 430 | final SomeArgs args = (SomeArgs) msg.obj; |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 431 | final SaveRequest request = (SaveRequest) args.arg1; |
| 432 | final ISaveCallback callback = (ISaveCallback) args.arg2; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 433 | final SaveCallback saveCallback = new SaveCallback(callback); |
| 434 | args.recycle(); |
Felipe Leme | 73fedac | 2017-05-12 09:52:07 -0700 | [diff] [blame] | 435 | onSaveRequest(request, saveCallback); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 436 | break; |
| 437 | } case MSG_DISCONNECT: { |
| 438 | onDisconnected(); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 439 | break; |
| 440 | } default: { |
| 441 | Log.w(TAG, "MyCallbacks received invalid message type: " + msg); |
| 442 | } |
| 443 | } |
| 444 | }; |
| 445 | |
| 446 | private HandlerCaller mHandlerCaller; |
| 447 | |
Svet Ganov | ecfa58a | 2017-05-05 19:38:45 -0700 | [diff] [blame] | 448 | @CallSuper |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 449 | @Override |
| 450 | public void onCreate() { |
| 451 | super.onCreate(); |
| 452 | mHandlerCaller = new HandlerCaller(null, Looper.getMainLooper(), mHandlerCallback, true); |
| 453 | } |
| 454 | |
| 455 | @Override |
| 456 | public final IBinder onBind(Intent intent) { |
Felipe Leme | 85d1c2d | 2017-04-21 08:56:04 -0700 | [diff] [blame] | 457 | if (SERVICE_INTERFACE.equals(intent.getAction())) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 458 | return mInterface.asBinder(); |
| 459 | } |
| 460 | Log.w(TAG, "Tried to bind to wrong intent: " + intent); |
| 461 | return null; |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Called when the Android system connects to service. |
| 466 | * |
| 467 | * <p>You should generally do initialization here rather than in {@link #onCreate}. |
| 468 | */ |
| 469 | public void onConnected() { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /** |
Felipe Leme | 2ef19c1 | 2017-06-05 11:32:32 -0700 | [diff] [blame] | 473 | * Called by the Android system do decide if a screen can be autofilled by the service. |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 474 | * |
| 475 | * <p>Service must call one of the {@link FillCallback} methods (like |
| 476 | * {@link FillCallback#onSuccess(FillResponse)} |
| 477 | * or {@link FillCallback#onFailure(CharSequence)}) |
| 478 | * to notify the result of the request. |
| 479 | * |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 480 | * @param request the {@link FillRequest request} to handle. |
| 481 | * See {@link FillResponse} for examples of multiple-sections requests. |
| 482 | * @param cancellationSignal signal for observing cancellation requests. The system will use |
| 483 | * this to notify you that the fill result is no longer needed and you should stop |
| 484 | * handling this fill request in order to save resources. |
| 485 | * @param callback object used to notify the result of the request. |
| 486 | */ |
Felipe Leme | 6a77849 | 2017-04-25 09:19:49 -0700 | [diff] [blame] | 487 | public abstract void onFillRequest(@NonNull FillRequest request, |
| 488 | @NonNull CancellationSignal cancellationSignal, @NonNull FillCallback callback); |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 489 | |
| 490 | /** |
Felipe Leme | 2ef19c1 | 2017-06-05 11:32:32 -0700 | [diff] [blame] | 491 | * Called when user requests service to save the fields of a screen. |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 492 | * |
| 493 | * <p>Service must call one of the {@link SaveCallback} methods (like |
| 494 | * {@link SaveCallback#onSuccess()} or {@link SaveCallback#onFailure(CharSequence)}) |
| 495 | * to notify the result of the request. |
| 496 | * |
Felipe Leme | 303b609 | 2017-07-31 18:01:15 -0700 | [diff] [blame] | 497 | * <p><b>Note:</b> To retrieve the actual value of the field, the service should call |
Felipe Leme | 2667523 | 2017-06-19 09:45:48 -0700 | [diff] [blame] | 498 | * {@link android.app.assist.AssistStructure.ViewNode#getAutofillValue()}; if it calls |
| 499 | * {@link android.app.assist.AssistStructure.ViewNode#getText()} or other methods, there is no |
| 500 | * guarantee such method will return the most recent value of the field. |
| 501 | * |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 502 | * @param request the {@link SaveRequest request} to handle. |
| 503 | * See {@link FillResponse} for examples of multiple-sections requests. |
| 504 | * @param callback object used to notify the result of the request. |
| 505 | */ |
Felipe Leme | 6a77849 | 2017-04-25 09:19:49 -0700 | [diff] [blame] | 506 | public abstract void onSaveRequest(@NonNull SaveRequest request, |
| 507 | @NonNull SaveCallback callback); |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 508 | |
| 509 | /** |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 510 | * Called when the Android system disconnects from the service. |
| 511 | * |
| 512 | * <p> At this point this service may no longer be an active {@link AutofillService}. |
| 513 | */ |
| 514 | public void onDisconnected() { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 515 | } |
| 516 | |
Philip P. Moltmann | cc684ed | 2017-04-17 14:18:33 -0700 | [diff] [blame] | 517 | /** |
Felipe Leme | 2e30c6f | 2017-06-20 10:55:01 -0700 | [diff] [blame] | 518 | * Gets the events that happened after the last |
| 519 | * {@link AutofillService#onFillRequest(FillRequest, android.os.CancellationSignal, FillCallback)} |
| 520 | * call. |
Philip P. Moltmann | cc684ed | 2017-04-17 14:18:33 -0700 | [diff] [blame] | 521 | * |
Felipe Leme | 2e30c6f | 2017-06-20 10:55:01 -0700 | [diff] [blame] | 522 | * <p>This method is typically used to keep track of previous user actions to optimize further |
| 523 | * requests. For example, the service might return email addresses in alphabetical order by |
| 524 | * default, but change that order based on the address the user picked on previous requests. |
Philip P. Moltmann | cc684ed | 2017-04-17 14:18:33 -0700 | [diff] [blame] | 525 | * |
Felipe Leme | 2e30c6f | 2017-06-20 10:55:01 -0700 | [diff] [blame] | 526 | * <p>The history is not persisted over reboots, and it's cleared every time the service |
| 527 | * replies to a {@link #onFillRequest(FillRequest, CancellationSignal, FillCallback)} by calling |
| 528 | * {@link FillCallback#onSuccess(FillResponse)} or {@link FillCallback#onFailure(CharSequence)} |
| 529 | * (if the service doesn't call any of these methods, the history will clear out after some |
| 530 | * pre-defined time). Hence, the service should call {@link #getFillEventHistory()} before |
| 531 | * finishing the {@link FillCallback}. |
| 532 | * |
| 533 | * @return The history or {@code null} if there are no events. |
Philip P. Moltmann | cc684ed | 2017-04-17 14:18:33 -0700 | [diff] [blame] | 534 | */ |
| 535 | @Nullable public final FillEventHistory getFillEventHistory() { |
Felipe Leme | 2ef19c1 | 2017-06-05 11:32:32 -0700 | [diff] [blame] | 536 | final AutofillManager afm = getSystemService(AutofillManager.class); |
Philip P. Moltmann | cc684ed | 2017-04-17 14:18:33 -0700 | [diff] [blame] | 537 | |
| 538 | if (afm == null) { |
| 539 | return null; |
| 540 | } else { |
| 541 | return afm.getFillEventHistory(); |
| 542 | } |
| 543 | } |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 544 | } |