blob: a98a2492f6946082017be04fa80fe5caa354f9a2 [file] [log] [blame]
Felipe Leme1ca634a2016-11-28 17:21:21 -08001/*
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
Felipe Leme4bcb01a2017-11-21 16:47:13 -080019import android.annotation.NonNull;
20import android.annotation.Nullable;
Felipe Leme1ca634a2016-11-28 17:21:21 -080021import android.app.Activity;
Felipe Leme4bcb01a2017-11-21 16:47:13 -080022import android.content.IntentSender;
Felipe Leme1ca634a2016-11-28 17:21:21 -080023import android.os.RemoteException;
Felipe Leme436ab6a2016-12-15 11:56:15 -080024
Felipe Leme4bcb01a2017-11-21 16:47:13 -080025import com.android.internal.util.Preconditions;
26
Felipe Leme1ca634a2016-11-28 17:21:21 -080027/**
Felipe Leme640f30a2017-03-06 15:44:06 -080028 * Handles save requests from the {@link AutofillService} into the {@link Activity} being
29 * autofilled.
Felipe Leme1ca634a2016-11-28 17:21:21 -080030 */
Svet Ganov0f4928f2017-02-02 20:02:51 -080031public final class SaveCallback {
32 private final ISaveCallback mCallback;
33 private boolean mCalled;
Felipe Leme436ab6a2016-12-15 11:56:15 -080034
Felipe Leme1ca634a2016-11-28 17:21:21 -080035 /** @hide */
Svet Ganov0f4928f2017-02-02 20:02:51 -080036 SaveCallback(ISaveCallback callback) {
Felipe Leme6d553872016-12-08 17:13:25 -080037 mCallback = callback;
Felipe Leme1ca634a2016-11-28 17:21:21 -080038 }
39
40 /**
Felipe Leme6d553872016-12-08 17:13:25 -080041 * Notifies the Android System that an
Felipe Leme30040fa2017-10-03 11:41:52 -070042 * {@link AutofillService#onSaveRequest(SaveRequest, SaveCallback)} was successfully handled
Felipe Lemee5f9c302017-04-18 17:48:49 -070043 * by the service.
Felipe Lemedeff81b2018-09-19 11:50:52 -070044 *
45 * @throws IllegalStateException if this method, {@link #onSuccess(IntentSender)}, or
46 * {@link #onFailure(CharSequence)} was already called.
Felipe Leme1ca634a2016-11-28 17:21:21 -080047 */
Felipe Leme0200d9e2017-01-24 15:10:26 -080048 public void onSuccess() {
Felipe Leme4bcb01a2017-11-21 16:47:13 -080049 onSuccessInternal(null);
50 }
51
52 /**
53 * Notifies the Android System that an
54 * {@link AutofillService#onSaveRequest(SaveRequest, SaveCallback)} was successfully handled
55 * by the service.
56 *
57 * <p>This method is useful when the service requires extra work&mdash;for example, launching an
58 * activity asking the user to authenticate first &mdash;before it can process the request,
59 * as the intent will be launched from the context of the activity being autofilled and hence
60 * will be part of that activity's stack.
61 *
62 * @param intentSender intent that will be launched from the context of activity being
63 * autofilled.
Felipe Lemedeff81b2018-09-19 11:50:52 -070064 *
65 * @throws IllegalStateException if this method, {@link #onSuccess()},
66 * or {@link #onFailure(CharSequence)} was already called.
Felipe Leme4bcb01a2017-11-21 16:47:13 -080067 */
68 public void onSuccess(@NonNull IntentSender intentSender) {
69 onSuccessInternal(Preconditions.checkNotNull(intentSender));
70 }
71
72 private void onSuccessInternal(@Nullable IntentSender intentSender) {
Svet Ganov0f4928f2017-02-02 20:02:51 -080073 assertNotCalled();
74 mCalled = true;
75 try {
Felipe Leme4bcb01a2017-11-21 16:47:13 -080076 mCallback.onSuccess(intentSender);
Svet Ganov0f4928f2017-02-02 20:02:51 -080077 } catch (RemoteException e) {
78 e.rethrowAsRuntimeException();
Felipe Leme6d553872016-12-08 17:13:25 -080079 }
Felipe Leme1ca634a2016-11-28 17:21:21 -080080 }
81
82 /**
Felipe Leme6d553872016-12-08 17:13:25 -080083 * Notifies the Android System that an
Felipe Leme30040fa2017-10-03 11:41:52 -070084 * {@link AutofillService#onSaveRequest(SaveRequest, SaveCallback)} could not be handled
Felipe Lemee5f9c302017-04-18 17:48:49 -070085 * by the service.
Felipe Leme1ca634a2016-11-28 17:21:21 -080086 *
Felipe Leme30040fa2017-10-03 11:41:52 -070087 * <p>This method should only be called when the service could not handle the request right away
88 * and could not recover or retry it. If the service could retry or recover, it could keep
89 * the {@link SaveRequest} and call {@link #onSuccess()} instead.
90 *
91 * <p><b>Note:</b> The Android System displays an UI with the supplied error message; if
Felipe Leme4bcb01a2017-11-21 16:47:13 -080092 * you prefer to show your own message, call {@link #onSuccess()} or
93 * {@link #onSuccess(IntentSender)} instead.
Felipe Leme30040fa2017-10-03 11:41:52 -070094 *
Felipe Lemedeff81b2018-09-19 11:50:52 -070095 * @param message error message to be displayed to the user. <b>Note: </b> this message is
96 * displayed on {@code logcat} logs and should not contain PII (Personally Identifiable
97 * Information, such as username or email address).
98 *
99 * @throws IllegalStateException if this method, {@link #onSuccess()},
100 * or {@link #onSuccess(IntentSender)} was already called.
Felipe Leme1ca634a2016-11-28 17:21:21 -0800101 */
102 public void onFailure(CharSequence message) {
Svet Ganov0f4928f2017-02-02 20:02:51 -0800103 assertNotCalled();
104 mCalled = true;
105 try {
106 mCallback.onFailure(message);
107 } catch (RemoteException e) {
108 e.rethrowAsRuntimeException();
Felipe Leme1ca634a2016-11-28 17:21:21 -0800109 }
110 }
Felipe Leme6d553872016-12-08 17:13:25 -0800111
Svet Ganov0f4928f2017-02-02 20:02:51 -0800112 private void assertNotCalled() {
113 if (mCalled) {
114 throw new IllegalStateException("Already called");
Felipe Leme436ab6a2016-12-15 11:56:15 -0800115 }
Felipe Leme6d553872016-12-08 17:13:25 -0800116 }
Felipe Leme1ca634a2016-11-28 17:21:21 -0800117}