FillCallback.java revision 640f30a7763b0a4b80c767acb84c740aac04768b
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 android.annotation.Nullable; 20import android.app.Activity; 21import android.os.Bundle; 22import android.os.RemoteException; 23 24/** 25 * Handles autofill requests from the {@link AutofillService} into the {@link Activity} being 26 * autofilled. 27 */ 28public final class FillCallback { 29 private final IFillCallback mCallback; 30 private boolean mCalled; 31 32 /** @hide */ 33 public FillCallback(IFillCallback callback) { 34 mCallback = callback; 35 } 36 37 /** 38 * Notifies the Android System that an 39 * {@link AutofillService#onFillRequest(android.app.assist.AssistStructure, Bundle, 40 * android.os.CancellationSignal, FillCallback)} was successfully fulfilled by the service. 41 * 42 * @param response autofill information for that activity, or {@code null} when the activity 43 * cannot be autofilled (for example, if it only contains read-only fields). See 44 * {@link FillResponse} for examples. 45 */ 46 public void onSuccess(@Nullable FillResponse response) { 47 assertNotCalled(); 48 mCalled = true; 49 try { 50 mCallback.onSuccess(response); 51 } catch (RemoteException e) { 52 e.rethrowAsRuntimeException(); 53 } 54 } 55 56 /** 57 * Notifies the Android System that an 58 * {@link AutofillService#onFillRequest(android.app.assist.AssistStructure, 59 * Bundle, android.os.CancellationSignal, FillCallback)} 60 * could not be fulfilled by the service. 61 * 62 * @param message error message to be displayed to the user. 63 */ 64 public void onFailure(@Nullable CharSequence message) { 65 assertNotCalled(); 66 mCalled = true; 67 try { 68 mCallback.onFailure(message); 69 } catch (RemoteException e) { 70 e.rethrowAsRuntimeException(); 71 } 72 } 73 74 private void assertNotCalled() { 75 if (mCalled) { 76 throw new IllegalStateException("Already called"); 77 } 78 } 79} 80