SaveCallback.java revision 782043caf81055aa1c331e9cc15b24a10e1bf17a
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.app.Activity;
20import android.os.Bundle;
21import android.os.RemoteException;
22
23/**
24 * Handles save requests from the {@link AutoFillService} into the {@link Activity} being
25 * auto-filled.
26 */
27public final class SaveCallback {
28    private final ISaveCallback mCallback;
29    private boolean mCalled;
30
31    /** @hide */
32    SaveCallback(ISaveCallback callback) {
33        mCallback = callback;
34    }
35
36    /**
37     * Notifies the Android System that an
38     * {@link AutoFillService#onSaveRequest (android.app.assist.AssistStructure, Bundle,
39     * SaveCallback)} was successfully fulfilled by the service.
40     *
41     * @throws RuntimeException if an error occurred while calling the Android System.
42     */
43    public void onSuccess() {
44        assertNotCalled();
45        mCalled = true;
46        try {
47            mCallback.onSuccess();
48        } catch (RemoteException e) {
49            e.rethrowAsRuntimeException();
50        }
51    }
52
53    /**
54     * Notifies the Android System that an
55     * {@link AutoFillService#onSaveRequest(android.app.assist.AssistStructure, Bundle,
56     * SaveCallback)} could not be fulfilled by the service.
57     *
58     * @param message error message to be displayed to the user.
59     *
60     * @throws RuntimeException if an error occurred while calling the Android System.
61     */
62    public void onFailure(CharSequence message) {
63        assertNotCalled();
64        mCalled = true;
65        try {
66            mCallback.onFailure(message);
67        } catch (RemoteException e) {
68            e.rethrowAsRuntimeException();
69        }
70    }
71
72    private void assertNotCalled() {
73        if (mCalled) {
74            throw new IllegalStateException("Already called");
75        }
76    }
77}
78