FillCallback.java revision dfa7fbc8702fae62e6b3f78c4d9245995baee04e
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; 20import static android.service.autofill.AutoFillService.TAG; 21 22import android.app.Activity; 23import android.app.assist.AssistStructure.ViewNode; 24import android.os.IBinder; 25import android.os.RemoteException; 26import android.util.Log; 27import android.util.SparseArray; 28 29import com.android.internal.util.Preconditions; 30 31import java.util.ArrayList; 32import java.util.Collections; 33import java.util.List; 34 35/** 36 * Handles auto-fill requests from the {@link AutoFillService} into the {@link Activity} being 37 * auto-filled. 38 */ 39public final class FillCallback { 40 41 private final IAutoFillCallback mCallback; 42 43 /** @hide */ 44 FillCallback(IBinder binder) { 45 mCallback = IAutoFillCallback.Stub.asInterface(binder); 46 } 47 48 /** 49 * Auto-fills the {@link Activity}. 50 * 51 * @throws RuntimeException if an error occurred while auto-filling it. 52 */ 53 public void onSuccess(FillData data) { 54 if (DEBUG) Log.d(TAG, "onSuccess(): data=" + data); 55 56 Preconditions.checkArgument(data != null, "data cannot be null"); 57 58 try { 59 mCallback.autofill(data.asList()); 60 } catch (RemoteException e) { 61 e.rethrowAsRuntimeException(); 62 } 63 } 64 65 public void onFailure(CharSequence message) { 66 if (DEBUG) Log.d(TAG, "onFailure(): message=" + message); 67 68 Preconditions.checkArgument(message != null, "message cannot be null"); 69 70 try { 71 mCallback.showError(message.toString()); 72 } catch (RemoteException e) { 73 e.rethrowAsRuntimeException(); 74 } 75 } 76 77 /** 78 * Data used to fill the fields of an {@link Activity}. 79 * 80 * <p>This class is immutable. 81 */ 82 public static final class FillData { 83 84 private final List<FillableInputField> mList; 85 86 private FillData(Builder builder) { 87 final int size = builder.mFields.size(); 88 final List<FillableInputField> list = new ArrayList<>(size); 89 for (int i = 0; i < size; i++) { 90 list.add(builder.mFields.valueAt(i)); 91 } 92 mList = Collections.unmodifiableList(list); 93 // TODO: use FastImmutableArraySet or a similar structure instead? 94 } 95 96 /** 97 * Gets the response as a {@code List} so it can be used in a binder call. 98 */ 99 List<FillableInputField> asList() { 100 return mList; 101 } 102 103 @Override 104 public String toString() { 105 return "[AutoFillResponse: " + mList + "]"; 106 } 107 108 /** 109 * Builder for {@link FillData} objects. 110 * 111 * <p>Typical usage: 112 * 113 * <pre class="prettyprint"> 114 * FillCallback.FillData data = new FillCallback.FillData.Builder() 115 * .setTextField(id1, "value 1") 116 * .setTextField(id2, "value 2") 117 * .build() 118 * </pre> 119 */ 120 public static class Builder { 121 private final SparseArray<FillableInputField> mFields = new SparseArray<>(); 122 123 /** 124 * Auto-fills a text field. 125 * 126 * @param id view id as returned by {@link ViewNode#getAutoFillId()}. 127 * @param text text to be auto-filled. 128 * @return same builder so it can be chained. 129 */ 130 public Builder setTextField(int id, String text) { 131 mFields.put(id, FillableInputField.forText(id, text)); 132 return this; 133 } 134 135 /** 136 * Builds a new {@link FillData} instance. 137 */ 138 public FillData build() { 139 return new FillData(this); 140 } 141 } 142 } 143} 144