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