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 17 package android.service.autofill; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.app.Activity; 22 import android.content.IntentSender; 23 import android.os.RemoteException; 24 25 import com.android.internal.util.Preconditions; 26 27 /** 28 * Handles save requests from the {@link AutofillService} into the {@link Activity} being 29 * autofilled. 30 */ 31 public final class SaveCallback { 32 private final ISaveCallback mCallback; 33 private boolean mCalled; 34 35 /** @hide */ 36 SaveCallback(ISaveCallback callback) { 37 mCallback = callback; 38 } 39 40 /** 41 * Notifies the Android System that an 42 * {@link AutofillService#onSaveRequest(SaveRequest, SaveCallback)} was successfully handled 43 * by the service. 44 */ 45 public void onSuccess() { 46 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—for example, launching an 55 * activity asking the user to authenticate first —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) { 67 assertNotCalled(); 68 mCalled = true; 69 try { 70 mCallback.onSuccess(intentSender); 71 } catch (RemoteException e) { 72 e.rethrowAsRuntimeException(); 73 } 74 } 75 76 /** 77 * Notifies the Android System that an 78 * {@link AutofillService#onSaveRequest(SaveRequest, SaveCallback)} could not be handled 79 * by the service. 80 * 81 * <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 86 * you prefer to show your own message, call {@link #onSuccess()} or 87 * {@link #onSuccess(IntentSender)} instead. 88 * 89 * @param message error message to be displayed to the user. 90 */ 91 public void onFailure(CharSequence message) { 92 assertNotCalled(); 93 mCalled = true; 94 try { 95 mCallback.onFailure(message); 96 } catch (RemoteException e) { 97 e.rethrowAsRuntimeException(); 98 } 99 } 100 101 private void assertNotCalled() { 102 if (mCalled) { 103 throw new IllegalStateException("Already called"); 104 } 105 } 106 } 107