FillCallback.java revision e5f9c30688f0277505fb6b50ea385e5df6271ed8
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.RemoteException; 22 23/** 24 * Handles autofill requests from the {@link AutofillService} into the {@link Activity} being 25 * autofilled. 26 */ 27public final class FillCallback { 28 private final IFillCallback mCallback; 29 private final int mRequestId; 30 private boolean mCalled; 31 32 /** @hide */ 33 public FillCallback(IFillCallback callback, int requestId) { 34 mCallback = callback; 35 mRequestId = requestId; 36 } 37 38 /** 39 * Notifies the Android System that an 40 * {@link AutofillService#onFillRequest(FillRequest, android.os.CancellationSignal, 41 * FillCallback)} was successfully fulfilled by the service. 42 * 43 * @param response autofill information for that activity, or {@code null} when the activity 44 * cannot be autofilled (for example, if it only contains read-only fields). See 45 * {@link FillResponse} for examples. 46 */ 47 public void onSuccess(@Nullable FillResponse response) { 48 assertNotCalled(); 49 mCalled = true; 50 try { 51 mCallback.onSuccess(response, mRequestId); 52 } catch (RemoteException e) { 53 e.rethrowAsRuntimeException(); 54 } 55 } 56 57 /** 58 * Notifies the Android System that an 59 * {@link AutofillService#onFillRequest(FillRequest, android.os.CancellationSignal, 60 * FillCallback)} 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