SaveCallback.java revision 436ab6a91d64ef6036c67bb361d807e398fb2c4c
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.service.autofill.CallbackHelper.Dumpable;
26import android.service.autofill.CallbackHelper.Finalizer;
27import android.util.Log;
28import android.view.autofill.AutoFillId;
29
30import com.android.internal.annotations.GuardedBy;
31import com.android.internal.util.Preconditions;
32
33import java.io.PrintWriter;
34
35/**
36 * Handles save requests from the {@link AutoFillService} into the {@link Activity} being
37 * auto-filled.
38 *
39 * <p>This class is thread safe.
40 */
41public final class SaveCallback implements Dumpable {
42
43    private static final String TAG = "SaveCallback";
44
45    private final IAutoFillServerCallback mCallback;
46
47    @GuardedBy("mCallback")
48    private boolean mReplied = false;
49
50    @GuardedBy("mCallback")
51    private Finalizer mFinalizer;
52
53    /** @hide */
54    SaveCallback(IAutoFillServerCallback callback) {
55        mCallback = callback;
56    }
57
58    /**
59     * Notifies the Android System that an
60     * {@link AutoFillService#onSaveRequest(android.app.assist.AssistStructure, Bundle,
61     * android.os.CancellationSignal, SaveCallback)} was successfully fulfilled by the service.
62     *
63     * @param ids ids ({@link ViewNode#getAutoFillId()}) of the fields that were saved.
64     *
65     * @throws RuntimeException if an error occurred while calling the Android System.
66     */
67    public void onSuccess(AutoFillId[] ids) {
68        if (DEBUG) Log.d(TAG, "onSuccess(): ids=" + ((ids == null) ? "null" : ids.length));
69
70        Preconditions.checkArgument(ids != null, "ids cannot be null");
71        Preconditions.checkArgument(ids.length > 0, "ids cannot be empty");
72
73        synchronized (mCallback) {
74            checkNotRepliedYetLocked();
75            try {
76                mCallback.highlightSavedFields(ids);
77            } catch (RemoteException e) {
78                e.rethrowAsRuntimeException();
79            } finally {
80                setRepliedLocked();
81            }
82        }
83    }
84
85    /**
86     * Notifies the Android System that an
87     * {@link AutoFillService#onSaveRequest(android.app.assist.AssistStructure, Bundle,
88     * android.os.CancellationSignal, SaveCallback)} could not be fulfilled by the service.
89     *
90     * @param message error message to be displayed to the user.
91     *
92     * @throws RuntimeException if an error occurred while calling the Android System.
93     */
94    public void onFailure(CharSequence message) {
95        if (DEBUG) Log.d(TAG, "onFailure(): message=" + message);
96
97        Preconditions.checkArgument(message != null, "message cannot be null");
98
99        synchronized (mCallback) {
100            checkNotRepliedYetLocked();
101
102            try {
103                mCallback.showError(message);
104            } catch (RemoteException e) {
105                e.rethrowAsRuntimeException();
106            } finally {
107                setRepliedLocked();
108            }
109        }
110    }
111
112    /** @hide */
113    @Override
114    public void dump(String prefix, PrintWriter pw) {
115        pw.print(prefix); pw.print("SaveCallback: mReplied="); pw.println(mReplied);
116    }
117
118    /** @hide */
119    @Override
120    public void setFinalizer(Finalizer f) {
121        synchronized (mCallback) {
122            mFinalizer = f;
123        }
124    }
125
126    @Override
127    public String toString() {
128        if (!DEBUG) return super.toString();
129
130        return "SaveCallback: [mReplied= " + mReplied + "]";
131    }
132
133    // There can be only one!!
134    private void checkNotRepliedYetLocked() {
135        Preconditions.checkState(!mReplied, "already replied");
136    }
137
138    private void setRepliedLocked() {
139        if (DEBUG) Log.d(TAG, "setReplied()");
140
141        mReplied = true;
142
143        if (mFinalizer != null) {
144            mFinalizer.gone();
145        }
146    }
147}
148