blob: 1695c1306824c76fa23ee865b3fa705bde5e53e5 [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 Leme6d553872016-12-08 17:13:25 -080042 * Notifies the Android System that an
Felipe Lemee5f9c302017-04-18 17:48:49 -070043 * {@link AutofillService#onFillRequest(FillRequest, android.os.CancellationSignal,
44 * FillCallback)} was successfully fulfilled by the service.
Felipe Leme29a5b0d2016-10-25 14:57:11 -070045 *
Felipe Leme640f30a2017-03-06 15:44:06 -080046 * @param response autofill information for that activity, or {@code null} when the activity
47 * cannot be autofilled (for example, if it only contains read-only fields). See
Felipe Leme436ab6a2016-12-15 11:56:15 -080048 * {@link FillResponse} for examples.
Felipe Leme29a5b0d2016-10-25 14:57:11 -070049 */
Felipe Leme6d553872016-12-08 17:13:25 -080050 public void onSuccess(@Nullable FillResponse response) {
Svet Ganov0f4928f2017-02-02 20:02:51 -080051 assertNotCalled();
52 mCalled = true;
Philip P. Moltmannc7619632017-04-25 11:57:37 -070053
54 if (response != null) {
55 response.setRequestId(mRequestId);
56 }
57
Svet Ganov0f4928f2017-02-02 20:02:51 -080058 try {
Philip P. Moltmannc7619632017-04-25 11:57:37 -070059 mCallback.onSuccess(response);
Svet Ganov0f4928f2017-02-02 20:02:51 -080060 } catch (RemoteException e) {
61 e.rethrowAsRuntimeException();
Felipe Leme29a5b0d2016-10-25 14:57:11 -070062 }
63 }
64
Felipe Leme1ca634a2016-11-28 17:21:21 -080065 /**
Felipe Leme6d553872016-12-08 17:13:25 -080066 * Notifies the Android System that an
Felipe Lemee5f9c302017-04-18 17:48:49 -070067 * {@link AutofillService#onFillRequest(FillRequest, android.os.CancellationSignal,
68 * FillCallback)} could not be fulfilled by the service.
Felipe Leme1ca634a2016-11-28 17:21:21 -080069 *
Felipe Leme6d553872016-12-08 17:13:25 -080070 * @param message error message to be displayed to the user.
Felipe Leme1ca634a2016-11-28 17:21:21 -080071 */
Svet Ganov0f4928f2017-02-02 20:02:51 -080072 public void onFailure(@Nullable CharSequence message) {
73 assertNotCalled();
74 mCalled = true;
75 try {
Felipe Lemef61ba5c2018-05-21 11:18:46 -070076 mCallback.onFailure(mRequestId, message);
Svet Ganov0f4928f2017-02-02 20:02:51 -080077 } catch (RemoteException e) {
78 e.rethrowAsRuntimeException();
Felipe Leme29a5b0d2016-10-25 14:57:11 -070079 }
80 }
81
Svet Ganov0f4928f2017-02-02 20:02:51 -080082 private void assertNotCalled() {
83 if (mCalled) {
84 throw new IllegalStateException("Already called");
Felipe Leme436ab6a2016-12-15 11:56:15 -080085 }
86 }
Felipe Leme29a5b0d2016-10-25 14:57:11 -070087}