SaveCallback.java revision 0200d9ea1509089c0c03b7071aa271e3a9b35c11
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     * SaveCallback)} was successfully fulfilled by the service.
62     *
63     * @throws RuntimeException if an error occurred while calling the Android System.
64     */
65    public void onSuccess() {
66        if (DEBUG) Log.d(TAG, "onSuccess()");
67
68        synchronized (mCallback) {
69            checkNotRepliedYetLocked();
70            try {
71                mCallback.onSaved();
72            } catch (RemoteException e) {
73                e.rethrowAsRuntimeException();
74            } finally {
75                setRepliedLocked();
76            }
77        }
78    }
79
80    /**
81     * Notifies the Android System that an
82     * {@link AutoFillService#onSaveRequest(android.app.assist.AssistStructure, Bundle,
83     * SaveCallback)} could not be fulfilled by the service.
84     *
85     * @param message error message to be displayed to the user.
86     *
87     * @throws RuntimeException if an error occurred while calling the Android System.
88     */
89    public void onFailure(CharSequence message) {
90        if (DEBUG) Log.d(TAG, "onFailure(): message=" + message);
91
92        synchronized (mCallback) {
93            checkNotRepliedYetLocked();
94
95            try {
96                mCallback.showError(message);
97            } catch (RemoteException e) {
98                e.rethrowAsRuntimeException();
99            } finally {
100                setRepliedLocked();
101            }
102        }
103    }
104
105    /** @hide */
106    @Override
107    public void dump(String prefix, PrintWriter pw) {
108        pw.print(prefix); pw.print("SaveCallback: mReplied="); pw.println(mReplied);
109    }
110
111    /** @hide */
112    @Override
113    public void setFinalizer(Finalizer f) {
114        synchronized (mCallback) {
115            mFinalizer = f;
116        }
117    }
118
119    @Override
120    public String toString() {
121        if (!DEBUG) return super.toString();
122
123        return "SaveCallback: [mReplied= " + mReplied + "]";
124    }
125
126    // There can be only one!!
127    private void checkNotRepliedYetLocked() {
128        Preconditions.checkState(!mReplied, "already replied");
129    }
130
131    private void setRepliedLocked() {
132        if (DEBUG) Log.d(TAG, "setReplied()");
133
134        mReplied = true;
135
136        if (mFinalizer != null) {
137            mFinalizer.gone();
138        }
139    }
140}
141