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.Nullable; 20 import android.app.Activity; 21 import android.os.RemoteException; 22 23 /** 24 * Handles autofill requests from the {@link AutofillService} into the {@link Activity} being 25 * autofilled. 26 */ 27 public 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 51 if (response != null) { 52 response.setRequestId(mRequestId); 53 } 54 55 try { 56 mCallback.onSuccess(response); 57 } catch (RemoteException e) { 58 e.rethrowAsRuntimeException(); 59 } 60 } 61 62 /** 63 * Notifies the Android System that an 64 * {@link AutofillService#onFillRequest(FillRequest, android.os.CancellationSignal, 65 * FillCallback)} could not be fulfilled by the service. 66 * 67 * @param message error message to be displayed to the user. 68 */ 69 public void onFailure(@Nullable CharSequence message) { 70 assertNotCalled(); 71 mCalled = true; 72 try { 73 mCallback.onFailure(message); 74 } catch (RemoteException e) { 75 e.rethrowAsRuntimeException(); 76 } 77 } 78 79 private void assertNotCalled() { 80 if (mCalled) { 81 throw new IllegalStateException("Already called"); 82 } 83 } 84 } 85