FillCallback.java revision 29a5b0d0f1cc7fd6cbfe97c816b8a687d9e438cc
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 /** 66 * Notifies the activity that the auto-fill request failed. 67 */ 68 public void onFailure(CharSequence message) { 69 if (DEBUG) Log.d(TAG, "onFailure(): message=" + message); 70 71 Preconditions.checkArgument(message != null, "message cannot be null"); 72 73 try { 74 mCallback.showError(message.toString()); 75 } catch (RemoteException e) { 76 e.rethrowAsRuntimeException(); 77 } 78 } 79 80 /** 81 * Data used to fill the fields of an {@link Activity}. 82 * 83 * <p>This class is immutable. 84 */ 85 public static final class FillData { 86 87 private final List<FillableInputField> mList; 88 89 private FillData(Builder builder) { 90 final int size = builder.mFields.size(); 91 final List<FillableInputField> list = new ArrayList<>(size); 92 for (int i = 0; i < size; i++) { 93 list.add(builder.mFields.valueAt(i)); 94 } 95 mList = Collections.unmodifiableList(list); 96 // TODO: use FastImmutableArraySet or a similar structure instead? 97 } 98 99 /** 100 * Gets the response as a {@code List} so it can be used in a binder call. 101 */ 102 List<FillableInputField> asList() { 103 return mList; 104 } 105 106 @Override 107 public String toString() { 108 return "[AutoFillResponse: " + mList + "]"; 109 } 110 111 /** 112 * Builder for {@link FillData} objects. 113 * 114 * <p>Typical usage: 115 * 116 * <pre class="prettyprint"> 117 * FillCallback.FillData data = new FillCallback.FillData.Builder() 118 * .setTextField(id1, "value 1") 119 * .setTextField(id2, "value 2") 120 * .build() 121 * </pre> 122 */ 123 public static class Builder { 124 private final SparseArray<FillableInputField> mFields = new SparseArray<>(); 125 126 /** 127 * Auto-fills a text field. 128 * 129 * @param id view id as returned by {@link ViewNode#getAutoFillId()}. 130 * @param text text to be auto-filled. 131 * @return same builder so it can be chained. 132 */ 133 public Builder setTextField(int id, String text) { 134 mFields.put(id, FillableInputField.forText(id, text)); 135 return this; 136 } 137 138 /** 139 * Builds a new {@link FillData} instance. 140 */ 141 public FillData build() { 142 return new FillData(this); 143 } 144 } 145 } 146} 147