blob: 855981a544fd7a482c15911e0e9c0a43ae25ac20 [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 Leme1ca634a2016-11-28 17:21:21 -080044 */
Felipe Leme0200d9e2017-01-24 15:10:26 -080045 public void onSuccess() {
Felipe Leme4bcb01a2017-11-21 16:47:13 -080046 onSuccessInternal(null);
47 }
48
49 /**
50 * Notifies the Android System that an
51 * {@link AutofillService#onSaveRequest(SaveRequest, SaveCallback)} was successfully handled
52 * by the service.
53 *
54 * <p>This method is useful when the service requires extra work&mdash;for example, launching an
55 * activity asking the user to authenticate first &mdash;before it can process the request,
56 * as the intent will be launched from the context of the activity being autofilled and hence
57 * will be part of that activity's stack.
58 *
59 * @param intentSender intent that will be launched from the context of activity being
60 * autofilled.
61 */
62 public void onSuccess(@NonNull IntentSender intentSender) {
63 onSuccessInternal(Preconditions.checkNotNull(intentSender));
64 }
65
66 private void onSuccessInternal(@Nullable IntentSender intentSender) {
Svet Ganov0f4928f2017-02-02 20:02:51 -080067 assertNotCalled();
68 mCalled = true;
69 try {
Felipe Leme4bcb01a2017-11-21 16:47:13 -080070 mCallback.onSuccess(intentSender);
Svet Ganov0f4928f2017-02-02 20:02:51 -080071 } catch (RemoteException e) {
72 e.rethrowAsRuntimeException();
Felipe Leme6d553872016-12-08 17:13:25 -080073 }
Felipe Leme1ca634a2016-11-28 17:21:21 -080074 }
75
76 /**
Felipe Leme6d553872016-12-08 17:13:25 -080077 * Notifies the Android System that an
Felipe Leme30040fa2017-10-03 11:41:52 -070078 * {@link AutofillService#onSaveRequest(SaveRequest, SaveCallback)} could not be handled
Felipe Lemee5f9c302017-04-18 17:48:49 -070079 * by the service.
Felipe Leme1ca634a2016-11-28 17:21:21 -080080 *
Felipe Leme30040fa2017-10-03 11:41:52 -070081 * <p>This method should only be called when the service could not handle the request right away
82 * and could not recover or retry it. If the service could retry or recover, it could keep
83 * the {@link SaveRequest} and call {@link #onSuccess()} instead.
84 *
85 * <p><b>Note:</b> The Android System displays an UI with the supplied error message; if
Felipe Leme4bcb01a2017-11-21 16:47:13 -080086 * you prefer to show your own message, call {@link #onSuccess()} or
87 * {@link #onSuccess(IntentSender)} instead.
Felipe Leme30040fa2017-10-03 11:41:52 -070088 *
Felipe Leme6d553872016-12-08 17:13:25 -080089 * @param message error message to be displayed to the user.
Felipe Leme1ca634a2016-11-28 17:21:21 -080090 */
91 public void onFailure(CharSequence message) {
Svet Ganov0f4928f2017-02-02 20:02:51 -080092 assertNotCalled();
93 mCalled = true;
94 try {
95 mCallback.onFailure(message);
96 } catch (RemoteException e) {
97 e.rethrowAsRuntimeException();
Felipe Leme1ca634a2016-11-28 17:21:21 -080098 }
99 }
Felipe Leme6d553872016-12-08 17:13:25 -0800100
Svet Ganov0f4928f2017-02-02 20:02:51 -0800101 private void assertNotCalled() {
102 if (mCalled) {
103 throw new IllegalStateException("Already called");
Felipe Leme436ab6a2016-12-15 11:56:15 -0800104 }
Felipe Leme6d553872016-12-08 17:13:25 -0800105 }
Felipe Leme1ca634a2016-11-28 17:21:21 -0800106}