blob: 818ffde44297b743a7a23f823f4719db128644b9 [file] [log] [blame]
Felipe Leme29a5b0d2016-10-25 14:57:11 -07001/*
2 * Copyright (C) 2016 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
17package android.service.autofill;
18
Felipe Leme6d553872016-12-08 17:13:25 -080019import android.annotation.Nullable;
Felipe Leme29a5b0d2016-10-25 14:57:11 -070020import android.app.Activity;
Felipe Leme29a5b0d2016-10-25 14:57:11 -070021import android.os.RemoteException;
Felipe Leme29a5b0d2016-10-25 14:57:11 -070022
Felipe Leme29a5b0d2016-10-25 14:57:11 -070023/**
Laura Davis43e75d92018-08-10 15:46:06 -070024 * <p><code>FillCallback</code> handles autofill requests from the {@link AutofillService} into
25 * the {@link Activity} being autofilled.
26 *
27 * <p>To learn about using Autofill services in your app, read
28 * <a href="/guide/topics/text/autofill-services">Build autofill services</a>.
Felipe Leme29a5b0d2016-10-25 14:57:11 -070029 */
Svet Ganov782043c2017-02-11 00:52:02 +000030public final class FillCallback {
Svet Ganov0f4928f2017-02-02 20:02:51 -080031 private final IFillCallback mCallback;
Svet Ganov013efe12017-04-13 21:56:16 -070032 private final int mRequestId;
Svet Ganov0f4928f2017-02-02 20:02:51 -080033 private boolean mCalled;
Felipe Leme29a5b0d2016-10-25 14:57:11 -070034
35 /** @hide */
Svet Ganov013efe12017-04-13 21:56:16 -070036 public FillCallback(IFillCallback callback, int requestId) {
Felipe Leme6d553872016-12-08 17:13:25 -080037 mCallback = callback;
Svet Ganov013efe12017-04-13 21:56:16 -070038 mRequestId = requestId;
Felipe Leme29a5b0d2016-10-25 14:57:11 -070039 }
40
41 /**
Felipe Lemedeff81b2018-09-19 11:50:52 -070042 * Notifies the Android System that a fill request
43 * ({@link AutofillService#onFillRequest(FillRequest, android.os.CancellationSignal,
44 * FillCallback)}) was successfully fulfilled by the service.
Felipe Leme29a5b0d2016-10-25 14:57:11 -070045 *
Felipe Lemedeff81b2018-09-19 11:50:52 -070046 * <p>This method should always be called, even if the service doesn't have the heuristics to
47 * fulfill the request (in which case it should be called with {@code null}).
48 *
49 * <p>See the main {@link AutofillService} documentation for more details and examples.
50 *
51 * @param response autofill information for that activity, or {@code null} when the service
52 * cannot autofill the activity.
53 *
54 * @throws IllegalStateException if this method or {@link #onFailure(CharSequence)} was already
55 * called.
Felipe Leme29a5b0d2016-10-25 14:57:11 -070056 */
Felipe Leme6d553872016-12-08 17:13:25 -080057 public void onSuccess(@Nullable FillResponse response) {
Svet Ganov0f4928f2017-02-02 20:02:51 -080058 assertNotCalled();
59 mCalled = true;
Philip P. Moltmannc7619632017-04-25 11:57:37 -070060
61 if (response != null) {
62 response.setRequestId(mRequestId);
63 }
64
Svet Ganov0f4928f2017-02-02 20:02:51 -080065 try {
Philip P. Moltmannc7619632017-04-25 11:57:37 -070066 mCallback.onSuccess(response);
Svet Ganov0f4928f2017-02-02 20:02:51 -080067 } catch (RemoteException e) {
68 e.rethrowAsRuntimeException();
Felipe Leme29a5b0d2016-10-25 14:57:11 -070069 }
70 }
71
Felipe Leme1ca634a2016-11-28 17:21:21 -080072 /**
Felipe Lemedeff81b2018-09-19 11:50:52 -070073 * Notifies the Android System that a fill request (
Felipe Lemee5f9c302017-04-18 17:48:49 -070074 * {@link AutofillService#onFillRequest(FillRequest, android.os.CancellationSignal,
Felipe Lemedeff81b2018-09-19 11:50:52 -070075 * FillCallback)}) could not be fulfilled by the service (for example, because the user data was
76 * not available yet), so the request could be retried later.
Felipe Leme1ca634a2016-11-28 17:21:21 -080077 *
Felipe Lemedeff81b2018-09-19 11:50:52 -070078 * <p><b>Note: </b>this method should not be used when the service didn't have the heursitics to
79 * fulfill the request; in this case, the service should call {@link #onSuccess(FillResponse)
80 * onSuccess(null)} instead.
81 *
82 * <p><b>Note: </b>on Android versions up to {@link android.os.Build.VERSION_CODES#P}, this
83 * method is not working as intended, and the service should call
84 * {@link #onSuccess(FillResponse) onSuccess(null)} instead.
85 *
86 * @param message error message to be displayed to the user. <b>Note: </b> this message is
87 * displayed on {@code logcat} logs and should not contain PII (Personally Identifiable
88 * Information, such as username or email address).
89 *
90 * @throws IllegalStateException if this method or {@link #onSuccess(FillResponse)} was already
91 * called.
Felipe Leme1ca634a2016-11-28 17:21:21 -080092 */
Svet Ganov0f4928f2017-02-02 20:02:51 -080093 public void onFailure(@Nullable CharSequence message) {
94 assertNotCalled();
95 mCalled = true;
96 try {
Felipe Lemef61ba5c2018-05-21 11:18:46 -070097 mCallback.onFailure(mRequestId, message);
Svet Ganov0f4928f2017-02-02 20:02:51 -080098 } catch (RemoteException e) {
99 e.rethrowAsRuntimeException();
Felipe Leme29a5b0d2016-10-25 14:57:11 -0700100 }
101 }
102
Svet Ganov0f4928f2017-02-02 20:02:51 -0800103 private void assertNotCalled() {
104 if (mCalled) {
105 throw new IllegalStateException("Already called");
Felipe Leme436ab6a2016-12-15 11:56:15 -0800106 }
107 }
Felipe Leme29a5b0d2016-10-25 14:57:11 -0700108}