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; |
| 21 | import android.os.RemoteException; |
Felipe Leme | 2ef19c1 | 2017-06-05 11:32:32 -0700 | [diff] [blame] | 22 | import android.provider.Settings; |
| 23 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 24 | import com.android.internal.os.HandlerCaller; |
| 25 | import android.annotation.SdkConstant; |
Felipe Leme | 2ef19c1 | 2017-06-05 11:32:32 -0700 | [diff] [blame] | 26 | import android.app.Service;import android.content.Intent; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 27 | import android.os.CancellationSignal; |
| 28 | import android.os.IBinder; |
| 29 | import android.os.ICancellationSignal; |
| 30 | import android.os.Looper; |
| 31 | import android.util.Log; |
Felipe Leme | 2ef19c1 | 2017-06-05 11:32:32 -0700 | [diff] [blame] | 32 | import android.view.View; |
| 33 | import android.view.ViewStructure; |
| 34 | import android.view.autofill.AutofillId; |
Felipe Leme | 2ac463e | 2017-03-13 14:06:25 -0700 | [diff] [blame] | 35 | import android.view.autofill.AutofillManager; |
Felipe Leme | 2ef19c1 | 2017-06-05 11:32:32 -0700 | [diff] [blame] | 36 | import android.view.autofill.AutofillValue; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 37 | |
| 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 | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 283 | */ |
| 284 | public abstract class AutofillService extends Service { |
| 285 | private static final String TAG = "AutofillService"; |
| 286 | |
| 287 | /** |
| 288 | * The {@link Intent} that must be declared as handled by the service. |
| 289 | * To be supported, the service must also require the |
Felipe Leme | decd887 | 2017-04-26 17:42:38 -0700 | [diff] [blame] | 290 | * {@link android.Manifest.permission#BIND_AUTOFILL_SERVICE} permission so |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 291 | * that other applications can not abuse it. |
| 292 | */ |
| 293 | @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION) |
| 294 | public static final String SERVICE_INTERFACE = "android.service.autofill.AutofillService"; |
| 295 | |
| 296 | /** |
| 297 | * Name under which a AutoFillService component publishes information about itself. |
| 298 | * This meta-data should reference an XML resource containing a |
| 299 | * <code><{@link |
Felipe Leme | f78e952 | 2017-04-04 15:07:13 -0700 | [diff] [blame] | 300 | * android.R.styleable#AutofillService autofill-service}></code> tag. |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 301 | * This is a a sample XML file configuring an AutoFillService: |
| 302 | * <pre> <autofill-service |
| 303 | * android:settingsActivity="foo.bar.SettingsActivity" |
| 304 | * . . . |
| 305 | * /></pre> |
| 306 | */ |
| 307 | public static final String SERVICE_META_DATA = "android.autofill"; |
| 308 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 309 | // Handler messages. |
| 310 | private static final int MSG_CONNECT = 1; |
| 311 | private static final int MSG_DISCONNECT = 2; |
| 312 | private static final int MSG_ON_FILL_REQUEST = 3; |
| 313 | private static final int MSG_ON_SAVE_REQUEST = 4; |
| 314 | |
| 315 | private final IAutoFillService mInterface = new IAutoFillService.Stub() { |
| 316 | @Override |
Svet Ganov | f20a037 | 2017-04-10 17:08:05 -0700 | [diff] [blame] | 317 | public void onConnectedStateChanged(boolean connected) { |
| 318 | if (connected) { |
| 319 | mHandlerCaller.obtainMessage(MSG_CONNECT).sendToTarget(); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 320 | } else { |
| 321 | mHandlerCaller.obtainMessage(MSG_DISCONNECT).sendToTarget(); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | @Override |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 326 | public void onFillRequest(FillRequest request, IFillCallback callback) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 327 | ICancellationSignal transport = CancellationSignal.createTransport(); |
| 328 | try { |
| 329 | callback.onCancellable(transport); |
| 330 | } catch (RemoteException e) { |
| 331 | e.rethrowFromSystemServer(); |
| 332 | } |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 333 | mHandlerCaller.obtainMessageOOO(MSG_ON_FILL_REQUEST, request, |
| 334 | CancellationSignal.fromTransport(transport), callback) |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 335 | .sendToTarget(); |
| 336 | } |
| 337 | |
| 338 | @Override |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 339 | public void onSaveRequest(SaveRequest request, ISaveCallback callback) { |
| 340 | mHandlerCaller.obtainMessageOO(MSG_ON_SAVE_REQUEST, request, |
| 341 | callback).sendToTarget(); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 342 | } |
| 343 | }; |
| 344 | |
| 345 | private final HandlerCaller.Callback mHandlerCallback = (msg) -> { |
| 346 | switch (msg.what) { |
| 347 | case MSG_CONNECT: { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 348 | onConnected(); |
| 349 | break; |
| 350 | } case MSG_ON_FILL_REQUEST: { |
| 351 | final SomeArgs args = (SomeArgs) msg.obj; |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 352 | final FillRequest request = (FillRequest) args.arg1; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 353 | final CancellationSignal cancellation = (CancellationSignal) args.arg2; |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 354 | final IFillCallback callback = (IFillCallback) args.arg3; |
| 355 | final FillCallback fillCallback = new FillCallback(callback, request.getId()); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 356 | args.recycle(); |
Felipe Leme | 73fedac | 2017-05-12 09:52:07 -0700 | [diff] [blame] | 357 | onFillRequest(request, cancellation, fillCallback); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 358 | break; |
| 359 | } case MSG_ON_SAVE_REQUEST: { |
| 360 | final SomeArgs args = (SomeArgs) msg.obj; |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 361 | final SaveRequest request = (SaveRequest) args.arg1; |
| 362 | final ISaveCallback callback = (ISaveCallback) args.arg2; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 363 | final SaveCallback saveCallback = new SaveCallback(callback); |
| 364 | args.recycle(); |
Felipe Leme | 73fedac | 2017-05-12 09:52:07 -0700 | [diff] [blame] | 365 | onSaveRequest(request, saveCallback); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 366 | break; |
| 367 | } case MSG_DISCONNECT: { |
| 368 | onDisconnected(); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 369 | break; |
| 370 | } default: { |
| 371 | Log.w(TAG, "MyCallbacks received invalid message type: " + msg); |
| 372 | } |
| 373 | } |
| 374 | }; |
| 375 | |
| 376 | private HandlerCaller mHandlerCaller; |
| 377 | |
Svet Ganov | ecfa58a | 2017-05-05 19:38:45 -0700 | [diff] [blame] | 378 | @CallSuper |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 379 | @Override |
| 380 | public void onCreate() { |
| 381 | super.onCreate(); |
| 382 | mHandlerCaller = new HandlerCaller(null, Looper.getMainLooper(), mHandlerCallback, true); |
| 383 | } |
| 384 | |
| 385 | @Override |
| 386 | public final IBinder onBind(Intent intent) { |
Felipe Leme | 85d1c2d | 2017-04-21 08:56:04 -0700 | [diff] [blame] | 387 | if (SERVICE_INTERFACE.equals(intent.getAction())) { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 388 | return mInterface.asBinder(); |
| 389 | } |
| 390 | Log.w(TAG, "Tried to bind to wrong intent: " + intent); |
| 391 | return null; |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Called when the Android system connects to service. |
| 396 | * |
| 397 | * <p>You should generally do initialization here rather than in {@link #onCreate}. |
| 398 | */ |
| 399 | public void onConnected() { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /** |
Felipe Leme | 2ef19c1 | 2017-06-05 11:32:32 -0700 | [diff] [blame] | 403 | * 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] | 404 | * |
| 405 | * <p>Service must call one of the {@link FillCallback} methods (like |
| 406 | * {@link FillCallback#onSuccess(FillResponse)} |
| 407 | * or {@link FillCallback#onFailure(CharSequence)}) |
| 408 | * to notify the result of the request. |
| 409 | * |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 410 | * @param request the {@link FillRequest request} to handle. |
| 411 | * See {@link FillResponse} for examples of multiple-sections requests. |
| 412 | * @param cancellationSignal signal for observing cancellation requests. The system will use |
| 413 | * this to notify you that the fill result is no longer needed and you should stop |
| 414 | * handling this fill request in order to save resources. |
| 415 | * @param callback object used to notify the result of the request. |
| 416 | */ |
Felipe Leme | 6a77849 | 2017-04-25 09:19:49 -0700 | [diff] [blame] | 417 | public abstract void onFillRequest(@NonNull FillRequest request, |
| 418 | @NonNull CancellationSignal cancellationSignal, @NonNull FillCallback callback); |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 419 | |
| 420 | /** |
Felipe Leme | 2ef19c1 | 2017-06-05 11:32:32 -0700 | [diff] [blame] | 421 | * Called when user requests service to save the fields of a screen. |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 422 | * |
| 423 | * <p>Service must call one of the {@link SaveCallback} methods (like |
| 424 | * {@link SaveCallback#onSuccess()} or {@link SaveCallback#onFailure(CharSequence)}) |
| 425 | * to notify the result of the request. |
| 426 | * |
Felipe Leme | 2667523 | 2017-06-19 09:45:48 -0700 | [diff] [blame] | 427 | * <p><b>NOTE: </b>to retrieve the actual value of the field, the service should call |
| 428 | * {@link android.app.assist.AssistStructure.ViewNode#getAutofillValue()}; if it calls |
| 429 | * {@link android.app.assist.AssistStructure.ViewNode#getText()} or other methods, there is no |
| 430 | * guarantee such method will return the most recent value of the field. |
| 431 | * |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 432 | * @param request the {@link SaveRequest request} to handle. |
| 433 | * See {@link FillResponse} for examples of multiple-sections requests. |
| 434 | * @param callback object used to notify the result of the request. |
| 435 | */ |
Felipe Leme | 6a77849 | 2017-04-25 09:19:49 -0700 | [diff] [blame] | 436 | public abstract void onSaveRequest(@NonNull SaveRequest request, |
| 437 | @NonNull SaveCallback callback); |
Svet Ganov | 013efe1 | 2017-04-13 21:56:16 -0700 | [diff] [blame] | 438 | |
| 439 | /** |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 440 | * Called when the Android system disconnects from the service. |
| 441 | * |
| 442 | * <p> At this point this service may no longer be an active {@link AutofillService}. |
| 443 | */ |
| 444 | public void onDisconnected() { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 445 | } |
| 446 | |
Philip P. Moltmann | cc684ed | 2017-04-17 14:18:33 -0700 | [diff] [blame] | 447 | /** |
Felipe Leme | 2e30c6f | 2017-06-20 10:55:01 -0700 | [diff] [blame] | 448 | * Gets the events that happened after the last |
| 449 | * {@link AutofillService#onFillRequest(FillRequest, android.os.CancellationSignal, FillCallback)} |
| 450 | * call. |
Philip P. Moltmann | cc684ed | 2017-04-17 14:18:33 -0700 | [diff] [blame] | 451 | * |
Felipe Leme | 2e30c6f | 2017-06-20 10:55:01 -0700 | [diff] [blame] | 452 | * <p>This method is typically used to keep track of previous user actions to optimize further |
| 453 | * requests. For example, the service might return email addresses in alphabetical order by |
| 454 | * 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] | 455 | * |
Felipe Leme | 2e30c6f | 2017-06-20 10:55:01 -0700 | [diff] [blame] | 456 | * <p>The history is not persisted over reboots, and it's cleared every time the service |
| 457 | * replies to a {@link #onFillRequest(FillRequest, CancellationSignal, FillCallback)} by calling |
| 458 | * {@link FillCallback#onSuccess(FillResponse)} or {@link FillCallback#onFailure(CharSequence)} |
| 459 | * (if the service doesn't call any of these methods, the history will clear out after some |
| 460 | * pre-defined time). Hence, the service should call {@link #getFillEventHistory()} before |
| 461 | * finishing the {@link FillCallback}. |
| 462 | * |
| 463 | * @return The history or {@code null} if there are no events. |
Philip P. Moltmann | cc684ed | 2017-04-17 14:18:33 -0700 | [diff] [blame] | 464 | */ |
| 465 | @Nullable public final FillEventHistory getFillEventHistory() { |
Felipe Leme | 2ef19c1 | 2017-06-05 11:32:32 -0700 | [diff] [blame] | 466 | final AutofillManager afm = getSystemService(AutofillManager.class); |
Philip P. Moltmann | cc684ed | 2017-04-17 14:18:33 -0700 | [diff] [blame] | 467 | |
| 468 | if (afm == null) { |
| 469 | return null; |
| 470 | } else { |
| 471 | return afm.getFillEventHistory(); |
| 472 | } |
| 473 | } |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 474 | } |