SaveCallback.java revision 1ca634a544a47193dd9e57f9b3eb3e42ab0dbbf1
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.IBinder;
24import android.os.RemoteException;
25import android.util.Log;
26
27import com.android.internal.util.Preconditions;
28
29/**
30 * Handles save requests from the {@link AutoFillService} into the {@link Activity} being
31 * auto-filled.
32 */
33public final class SaveCallback {
34
35    private static final String TAG = "SaveCallback";
36
37    private final IAutoFillCallback mCallback;
38
39    /** @hide */
40    SaveCallback(IBinder binder) {
41        mCallback = IAutoFillCallback.Stub.asInterface(binder);
42    }
43
44    /**
45     * Notifies the {@link Activity} that the save request succeeded.
46     *
47     * @param ids ids ({@link ViewNode#getAutoFillId()}) of the fields that were saved.
48     *
49     * @throws RuntimeException if an error occurred while saving the data.
50     */
51    public void onSuccess(int[] ids) {
52        Preconditions.checkArgument(ids != null, "ids cannot be null");
53
54        Preconditions.checkArgument(ids.length > 0, "ids cannot be empty");
55
56        if (DEBUG) Log.d(TAG, "onSuccess(): ids=" + ids.length);
57
58        // TODO(b/33197203): display which ids were saved
59    }
60
61    /**
62     * Notifies the {@link Activity} that the save request failed.
63     *
64     * @param message error message to be displayed.
65     *
66     * @throws RuntimeException if an error occurred while notifying the activity.
67     */
68    public void onFailure(CharSequence message) {
69        if (DEBUG) Log.d(TAG, "onFailure(): message=" + message);
70
71        Preconditions.checkArgument(message != null, "message cannot be null");
72
73        try {
74            mCallback.showError(message.toString());
75        } catch (RemoteException e) {
76            e.rethrowAsRuntimeException();
77        }
78    }
79}
80