FillCallback.java revision 3461d3c069468e176fab6bc8b8e78c8e6bc81e8b
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 static android.service.autofill.AutoFillService.DEBUG; 20 21import android.annotation.Nullable; 22import android.app.Activity; 23import android.os.Bundle; 24import android.os.RemoteException; 25import android.util.Log; 26import android.view.autofill.FillResponse; 27 28import com.android.internal.util.Preconditions; 29 30/** 31 * Handles auto-fill requests from the {@link AutoFillService} into the {@link Activity} being 32 * auto-filled. 33 */ 34public final class FillCallback { 35 36 private static final String TAG = "FillCallback"; 37 38 private final IAutoFillServerCallback mCallback; 39 40 private boolean mReplied = false; 41 42 /** @hide */ 43 FillCallback(IAutoFillServerCallback callback) { 44 mCallback = callback; 45 } 46 47 /** 48 * Notifies the Android System that an 49 * {@link AutoFillService#onFillRequest(android.app.assist.AssistStructure, Bundle, android.os.CancellationSignal, FillCallback)} 50 * was successfully fulfilled by the service. 51 * 52 * @param response auto-fill information for that activity, or {@code null} when the activity 53 * cannot be auto-filled (for example, if it only contains read-only fields). 54 * 55 * @throws RuntimeException if an error occurred while calling the Android System. 56 */ 57 public void onSuccess(@Nullable FillResponse response) { 58 if (DEBUG) Log.d(TAG, "onSuccess(): respose=" + response); 59 60 checkNotRepliedYet(); 61 62 try { 63 mCallback.showResponse(response); 64 } catch (RemoteException e) { 65 e.rethrowAsRuntimeException(); 66 } 67 } 68 69 /** 70 * Notifies the Android System that an 71 * {@link AutoFillService#onFillRequest(android.app.assist.AssistStructure, Bundle, android.os.CancellationSignal, FillCallback)} 72 * could not be fulfilled by the service. 73 * 74 * @param message error message to be displayed to the user. 75 * 76 * @throws RuntimeException if an error occurred while calling the Android System. 77 */ 78 public void onFailure(CharSequence message) { 79 if (DEBUG) Log.d(TAG, "onFailure(): message=" + message); 80 81 checkNotRepliedYet(); 82 Preconditions.checkArgument(message != null, "message cannot be null"); 83 84 try { 85 mCallback.showError(message.toString()); 86 } catch (RemoteException e) { 87 e.rethrowAsRuntimeException(); 88 } 89 } 90 91 // There can be only one!! 92 private void checkNotRepliedYet() { 93 Preconditions.checkState(!mReplied, "already replied"); 94 mReplied = true; 95 } 96} 97