FillCallback.java revision 013efe173e56612a910ebd8576480ce4ef005e3c
1/*
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
19import android.annotation.Nullable;
20import android.app.Activity;
21import android.os.Bundle;
22import android.os.RemoteException;
23
24/**
25 * Handles autofill requests from the {@link AutofillService} into the {@link Activity} being
26 * autofilled.
27 */
28public final class FillCallback {
29    private final IFillCallback mCallback;
30    private final int mRequestId;
31    private boolean mCalled;
32
33    /** @hide */
34    public FillCallback(IFillCallback callback, int requestId) {
35        mCallback = callback;
36        mRequestId = requestId;
37    }
38
39    /**
40     * Notifies the Android System that an
41     * {@link AutofillService#onFillRequest(android.app.assist.AssistStructure, Bundle,
42     * int, android.os.CancellationSignal, FillCallback)} was successfully fulfilled by the service.
43     *
44     * @param response autofill information for that activity, or {@code null} when the activity
45     * cannot be autofilled (for example, if it only contains read-only fields). See
46     * {@link FillResponse} for examples.
47     */
48    public void onSuccess(@Nullable FillResponse response) {
49        assertNotCalled();
50        mCalled = true;
51        try {
52            mCallback.onSuccess(response, mRequestId);
53        } catch (RemoteException e) {
54            e.rethrowAsRuntimeException();
55        }
56    }
57
58    /**
59     * Notifies the Android System that an
60     * {@link AutofillService#onFillRequest(android.app.assist.AssistStructure,
61     * Bundle, int, android.os.CancellationSignal, FillCallback)}
62     * could not be fulfilled by the service.
63     *
64     * @param message error message to be displayed to the user.
65     */
66    public void onFailure(@Nullable CharSequence message) {
67        assertNotCalled();
68        mCalled = true;
69        try {
70            mCallback.onFailure(message);
71        } catch (RemoteException e) {
72            e.rethrowAsRuntimeException();
73        }
74    }
75
76    private void assertNotCalled() {
77        if (mCalled) {
78            throw new IllegalStateException("Already called");
79        }
80    }
81}
82