SaveCallback.java revision 3461d3c069468e176fab6bc8b8e78c8e6bc81e8b
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 static android.service.autofill.AutoFillService.DEBUG;
20
21import android.app.Activity;
22import android.app.assist.AssistStructure.ViewNode;
23import android.os.Bundle;
24import android.os.RemoteException;
25import android.util.Log;
26import android.view.autofill.AutoFillId;
27
28import com.android.internal.util.Preconditions;
29
30/**
31 * Handles save requests from the {@link AutoFillService} into the {@link Activity} being
32 * auto-filled.
33 */
34public final class SaveCallback {
35
36    private static final String TAG = "SaveCallback";
37
38    private final IAutoFillServerCallback mCallback;
39
40    private boolean mReplied = false;
41
42    /** @hide */
43    SaveCallback(IAutoFillServerCallback callback) {
44        mCallback = callback;
45    }
46
47    /**
48     * Notifies the Android System that an
49     * {@link AutoFillService#onSaveRequest(android.app.assist.AssistStructure, Bundle, android.os.CancellationSignal, SaveCallback)}
50     * was successfully fulfilled by the service.
51     *
52     * @param ids ids ({@link ViewNode#getAutoFillId()}) of the fields that were saved.
53     *
54     * @throws RuntimeException if an error occurred while calling the Android System.
55     */
56    public void onSuccess(AutoFillId[] ids) {
57        if (DEBUG) Log.d(TAG, "onSuccess(): ids=" + ((ids == null) ? "null" : ids.length));
58
59        Preconditions.checkArgument(ids != null, "ids cannot be null");
60        checkNotRepliedYet();
61
62        Preconditions.checkArgument(ids.length > 0, "ids cannot be empty");
63
64        try {
65            mCallback.highlightSavedFields(ids);
66        } catch (RemoteException e) {
67            e.rethrowAsRuntimeException();
68        }
69    }
70
71    /**
72     * Notifies the Android System that an
73     * {@link AutoFillService#onSaveRequest(android.app.assist.AssistStructure, Bundle, android.os.CancellationSignal, SaveCallback)}
74     * could not be fulfilled by the service.
75     *
76     * @param message error message to be displayed to the user.
77     *
78     * @throws RuntimeException if an error occurred while calling the Android System.
79     */
80    public void onFailure(CharSequence message) {
81        if (DEBUG) Log.d(TAG, "onFailure(): message=" + message);
82
83        Preconditions.checkArgument(message != null, "message cannot be null");
84        checkNotRepliedYet();
85
86        try {
87            mCallback.showError(message.toString());
88        } catch (RemoteException e) {
89            e.rethrowAsRuntimeException();
90        }
91    }
92
93    // There can be only one!!
94    private void checkNotRepliedYet() {
95        Preconditions.checkState(!mReplied, "already replied");
96        mReplied = true;
97    }
98}
99