FillRequest.java revision 67f9d5070a74a0bf34f0335899a96dedcac26c96
1/*
2 * Copyright (C) 2017 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 android.annotation.IntDef;
20import android.annotation.NonNull;
21import android.annotation.Nullable;
22import android.os.Bundle;
23import android.os.CancellationSignal;
24import android.os.Parcel;
25import android.os.Parcelable;
26import android.view.View;
27
28import com.android.internal.util.Preconditions;
29
30import java.lang.annotation.Retention;
31import java.lang.annotation.RetentionPolicy;
32import java.util.ArrayList;
33import java.util.List;
34
35/**
36 * This class represents a request to an autofill service
37 * to interpret the screen and provide information to the system which views are
38 * interesting for saving and what are the possible ways to fill the inputs on
39 * the screen if applicable.
40 *
41 * @see AutofillService#onFillRequest(FillRequest, CancellationSignal, FillCallback)
42 */
43public final class FillRequest implements Parcelable {
44
45    /**
46     * Indicates autofill was explicitly requested by the user.
47     *
48     * <p>Users typically make an explicit request to autofill a screen in two situations:
49     * <ul>
50     *   <li>The app disabled autofill (using {@link View#setImportantForAutofill(int)}.
51     *   <li>The service could not figure out how to autofill a screen (but the user knows the
52     *       service has data for that app).
53     * </ul>
54     *
55     * <p>This flag is particularly useful for the second case. For example, the service could offer
56     * a complex UI where the user can map which screen views belong to each user data, or it could
57     * offer a simpler UI where the user picks the data for just the view used to trigger the
58     * request (that would be the view whose
59     * {@link android.app.assist.AssistStructure.ViewNode#isFocused()} method returns {@code true}).
60     *
61     * <p>An explicit autofill request is triggered when the
62     * {@link android.view.autofill.AutofillManager#requestAutofill(View)} or
63     * {@link android.view.autofill.AutofillManager#requestAutofill(View, int, android.graphics.Rect)}
64     * is called. For example, standard {@link android.widget.TextView} views show an
65     * {@code AUTOFILL} option in the overflow menu that triggers such request.
66     */
67    public static final int FLAG_MANUAL_REQUEST = 0x1;
68
69    /** @hide */
70    public static final int INVALID_REQUEST_ID = Integer.MIN_VALUE;
71
72    /** @hide */
73    @IntDef(
74        flag = true,
75        value = {FLAG_MANUAL_REQUEST})
76    @Retention(RetentionPolicy.SOURCE)
77    @interface RequestFlags{}
78
79    private final int mId;
80    private final @RequestFlags int mFlags;
81    private final @NonNull ArrayList<FillContext> mContexts;
82    private final @Nullable Bundle mClientState;
83
84    private FillRequest(@NonNull Parcel parcel) {
85        mId = parcel.readInt();
86        mContexts = new ArrayList<>();
87        parcel.readParcelableList(mContexts, null);
88
89        mClientState = parcel.readBundle();
90        mFlags = parcel.readInt();
91    }
92
93    /** @hide */
94    public FillRequest(int id, @NonNull ArrayList<FillContext> contexts,
95            @Nullable Bundle clientState, @RequestFlags int flags) {
96        mId = id;
97        mFlags = Preconditions.checkFlagsArgument(flags, FLAG_MANUAL_REQUEST);
98        mContexts = Preconditions.checkCollectionElementsNotNull(contexts, "contexts");
99        mClientState = clientState;
100    }
101
102    /**
103     * Gets the unique id of this request.
104     */
105    public int getId() {
106        return mId;
107    }
108
109    /**
110     * Gets the flags associated with this request.
111     *
112     * @see #FLAG_MANUAL_REQUEST
113     */
114    public @RequestFlags int getFlags() {
115        return mFlags;
116    }
117
118    /**
119     * Gets the contexts associated with each previous fill request.
120     */
121    public @NonNull List<FillContext> getFillContexts() {
122        return mContexts;
123    }
124
125    /**
126     * Gets the extra client state returned from the last {@link
127     * AutofillService#onFillRequest(FillRequest, CancellationSignal, FillCallback)
128     * fill request}, so the service can use it for state management.
129     *
130     * <p>Once a {@link AutofillService#onSaveRequest(SaveRequest, SaveCallback)
131     * save request} is made, the client state is cleared.
132     *
133     * @return The client state.
134     */
135    public @Nullable Bundle getClientState() {
136        return mClientState;
137    }
138
139    @Override
140    public int describeContents() {
141        return 0;
142    }
143
144    @Override
145    public void writeToParcel(Parcel parcel, int flags) {
146        parcel.writeInt(mId);
147        parcel.writeParcelableList(mContexts, flags);
148        parcel.writeBundle(mClientState);
149        parcel.writeInt(mFlags);
150    }
151
152    public static final Parcelable.Creator<FillRequest> CREATOR =
153            new Parcelable.Creator<FillRequest>() {
154        @Override
155        public FillRequest createFromParcel(Parcel parcel) {
156            return new FillRequest(parcel);
157        }
158
159        @Override
160        public FillRequest[] newArray(int size) {
161            return new FillRequest[size];
162        }
163    };
164}
165