blob: 06e2896a7f8ad26a2840811898d96aa93d737877 [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 Leme3ebb3592018-09-18 14:59:30 -070022import android.util.Log;
Felipe Leme29a5b0d2016-10-25 14:57:11 -070023
Felipe Leme29a5b0d2016-10-25 14:57:11 -070024/**
Laura Davis43e75d92018-08-10 15:46:06 -070025 * <p><code>FillCallback</code> handles autofill requests from the {@link AutofillService} into
26 * the {@link Activity} being autofilled.
27 *
28 * <p>To learn about using Autofill services in your app, read
29 * <a href="/guide/topics/text/autofill-services">Build autofill services</a>.
Felipe Leme29a5b0d2016-10-25 14:57:11 -070030 */
Svet Ganov782043c2017-02-11 00:52:02 +000031public final class FillCallback {
Felipe Leme3ebb3592018-09-18 14:59:30 -070032
33 private static final String TAG = "FillCallback";
34
Svet Ganov0f4928f2017-02-02 20:02:51 -080035 private final IFillCallback mCallback;
Svet Ganov013efe12017-04-13 21:56:16 -070036 private final int mRequestId;
Svet Ganov0f4928f2017-02-02 20:02:51 -080037 private boolean mCalled;
Felipe Leme29a5b0d2016-10-25 14:57:11 -070038
39 /** @hide */
Svet Ganov013efe12017-04-13 21:56:16 -070040 public FillCallback(IFillCallback callback, int requestId) {
Felipe Leme6d553872016-12-08 17:13:25 -080041 mCallback = callback;
Svet Ganov013efe12017-04-13 21:56:16 -070042 mRequestId = requestId;
Felipe Leme29a5b0d2016-10-25 14:57:11 -070043 }
44
45 /**
Felipe Lemedeff81b2018-09-19 11:50:52 -070046 * Notifies the Android System that a fill request
47 * ({@link AutofillService#onFillRequest(FillRequest, android.os.CancellationSignal,
48 * FillCallback)}) was successfully fulfilled by the service.
Felipe Leme29a5b0d2016-10-25 14:57:11 -070049 *
Felipe Lemedeff81b2018-09-19 11:50:52 -070050 * <p>This method should always be called, even if the service doesn't have the heuristics to
51 * fulfill the request (in which case it should be called with {@code null}).
52 *
53 * <p>See the main {@link AutofillService} documentation for more details and examples.
54 *
55 * @param response autofill information for that activity, or {@code null} when the service
56 * cannot autofill the activity.
57 *
58 * @throws IllegalStateException if this method or {@link #onFailure(CharSequence)} was already
59 * called.
Felipe Leme29a5b0d2016-10-25 14:57:11 -070060 */
Felipe Leme6d553872016-12-08 17:13:25 -080061 public void onSuccess(@Nullable FillResponse response) {
Svet Ganov0f4928f2017-02-02 20:02:51 -080062 assertNotCalled();
63 mCalled = true;
Philip P. Moltmannc7619632017-04-25 11:57:37 -070064
65 if (response != null) {
66 response.setRequestId(mRequestId);
67 }
68
Svet Ganov0f4928f2017-02-02 20:02:51 -080069 try {
Philip P. Moltmannc7619632017-04-25 11:57:37 -070070 mCallback.onSuccess(response);
Svet Ganov0f4928f2017-02-02 20:02:51 -080071 } catch (RemoteException e) {
72 e.rethrowAsRuntimeException();
Felipe Leme29a5b0d2016-10-25 14:57:11 -070073 }
74 }
75
Felipe Leme1ca634a2016-11-28 17:21:21 -080076 /**
Felipe Lemedeff81b2018-09-19 11:50:52 -070077 * Notifies the Android System that a fill request (
Felipe Lemee5f9c302017-04-18 17:48:49 -070078 * {@link AutofillService#onFillRequest(FillRequest, android.os.CancellationSignal,
Felipe Lemedeff81b2018-09-19 11:50:52 -070079 * FillCallback)}) could not be fulfilled by the service (for example, because the user data was
80 * not available yet), so the request could be retried later.
Felipe Leme1ca634a2016-11-28 17:21:21 -080081 *
Felipe Lemedeff81b2018-09-19 11:50:52 -070082 * <p><b>Note: </b>this method should not be used when the service didn't have the heursitics to
83 * fulfill the request; in this case, the service should call {@link #onSuccess(FillResponse)
84 * onSuccess(null)} instead.
85 *
Felipe Lemed9dc9542018-09-19 11:54:28 -070086 * <p><b>Note: </b>prior to {@link android.os.Build.VERSION_CODES#Q}, this
87 * method was not working as intended and the service should always call
Felipe Lemedeff81b2018-09-19 11:50:52 -070088 * {@link #onSuccess(FillResponse) onSuccess(null)} instead.
89 *
Felipe Lemed9dc9542018-09-19 11:54:28 -070090 * <p><b>Note: </b>for apps targeting {@link android.os.Build.VERSION_CODES#Q} or higher, this
91 * method just logs the message on {@code logcat}; for apps targetting older SDKs, it also
92 * displays the message to user using a {@link android.widget.Toast}. Generally speaking, you
93 * should not display an error to the user if the request failed, unless the request had the
94 * {@link FillRequest#FLAG_MANUAL_REQUEST} flag.
95 *
96 * @param message error message. <b>Note: </b> this message should <b>not</b> contain PII
97 * (Personally Identifiable Information, such as username or email address).
Felipe Lemedeff81b2018-09-19 11:50:52 -070098 *
99 * @throws IllegalStateException if this method or {@link #onSuccess(FillResponse)} was already
100 * called.
Felipe Leme1ca634a2016-11-28 17:21:21 -0800101 */
Svet Ganov0f4928f2017-02-02 20:02:51 -0800102 public void onFailure(@Nullable CharSequence message) {
Felipe Lemed9dc9542018-09-19 11:54:28 -0700103 Log.w(TAG, "onFailure(): " + message);
Svet Ganov0f4928f2017-02-02 20:02:51 -0800104 assertNotCalled();
105 mCalled = true;
106 try {
Felipe Lemef61ba5c2018-05-21 11:18:46 -0700107 mCallback.onFailure(mRequestId, message);
Svet Ganov0f4928f2017-02-02 20:02:51 -0800108 } catch (RemoteException e) {
109 e.rethrowAsRuntimeException();
Felipe Leme29a5b0d2016-10-25 14:57:11 -0700110 }
111 }
112
Svet Ganov0f4928f2017-02-02 20:02:51 -0800113 private void assertNotCalled() {
114 if (mCalled) {
115 throw new IllegalStateException("Already called");
Felipe Leme436ab6a2016-12-15 11:56:15 -0800116 }
117 }
Felipe Leme29a5b0d2016-10-25 14:57:11 -0700118}