blob: cbd0cd94c0e39eb4deee150727a7eef051245888 [file] [log] [blame]
Svet Ganov013efe12017-04-13 21:56:16 -07001/*
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;
Svet Ganov013efe12017-04-13 21:56:16 -070022import android.os.Bundle;
Svet Ganov013efe12017-04-13 21:56:16 -070023import android.os.Parcel;
24import android.os.Parcelable;
Felipe Leme2ef19c12017-06-05 11:32:32 -070025import android.view.View;
Philip P. Moltmann121e5262017-04-25 13:33:41 -070026
Svet Ganov013efe12017-04-13 21:56:16 -070027import com.android.internal.util.Preconditions;
28
29import java.lang.annotation.Retention;
30import java.lang.annotation.RetentionPolicy;
Philip P. Moltmann121e5262017-04-25 13:33:41 -070031import java.util.ArrayList;
Jeff Sharkey000ce802017-04-29 13:13:27 -060032import java.util.List;
Svet Ganov013efe12017-04-13 21:56:16 -070033
34/**
Felipe Leme2ef19c12017-06-05 11:32:32 -070035 * This class represents a request to an autofill service
Svet Ganov013efe12017-04-13 21:56:16 -070036 * to interpret the screen and provide information to the system which views are
37 * interesting for saving and what are the possible ways to fill the inputs on
38 * the screen if applicable.
39 *
Felipe Lemea012d7e2017-09-06 15:19:11 -070040 * @see AutofillService#onFillRequest(FillRequest, android.os.CancellationSignal, FillCallback)
Svet Ganov013efe12017-04-13 21:56:16 -070041 */
42public final class FillRequest implements Parcelable {
Felipe Leme2ef19c12017-06-05 11:32:32 -070043
Svet Ganov013efe12017-04-13 21:56:16 -070044 /**
45 * Indicates autofill was explicitly requested by the user.
Felipe Leme2ef19c12017-06-05 11:32:32 -070046 *
47 * <p>Users typically make an explicit request to autofill a screen in two situations:
48 * <ul>
49 * <li>The app disabled autofill (using {@link View#setImportantForAutofill(int)}.
50 * <li>The service could not figure out how to autofill a screen (but the user knows the
51 * service has data for that app).
52 * </ul>
53 *
54 * <p>This flag is particularly useful for the second case. For example, the service could offer
55 * a complex UI where the user can map which screen views belong to each user data, or it could
56 * offer a simpler UI where the user picks the data for just the view used to trigger the
57 * request (that would be the view whose
58 * {@link android.app.assist.AssistStructure.ViewNode#isFocused()} method returns {@code true}).
59 *
60 * <p>An explicit autofill request is triggered when the
61 * {@link android.view.autofill.AutofillManager#requestAutofill(View)} or
62 * {@link android.view.autofill.AutofillManager#requestAutofill(View, int, android.graphics.Rect)}
Jeff Sharkey67f9d502017-08-05 13:49:13 -060063 * is called. For example, standard {@link android.widget.TextView} views show an
64 * {@code AUTOFILL} option in the overflow menu that triggers such request.
Svet Ganov013efe12017-04-13 21:56:16 -070065 */
66 public static final int FLAG_MANUAL_REQUEST = 0x1;
67
Felipe Lemeeacd74d2018-08-02 10:36:34 -070068 /**
69 * Indicates this request was made using
70 * <a href="AutofillService.html#CompatibilityMode">compatibility mode</a>.
71 */
72 public static final int FLAG_COMPATIBILITY_MODE_REQUEST = 0x2;
73
Felipe Lemea7de4022019-02-19 17:16:45 -080074 // Private flags below start from the highest-significative bit (0x80000000)
75 /**
76 * Request was only triggered for augmented autofill.
77 *
78 * @hide
79 */
80 public static final int FLAG_AUGMENTED_AUTOFILL_REQUEST = 0x80000000;
81
Svet Ganov013efe12017-04-13 21:56:16 -070082 /** @hide */
Philip P. Moltmannc7619632017-04-25 11:57:37 -070083 public static final int INVALID_REQUEST_ID = Integer.MIN_VALUE;
84
85 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -070086 @IntDef(flag = true, prefix = { "FLAG_" }, value = {
Felipe Lemeeacd74d2018-08-02 10:36:34 -070087 FLAG_MANUAL_REQUEST, FLAG_COMPATIBILITY_MODE_REQUEST
Jeff Sharkeyce8db992017-12-13 20:05:05 -070088 })
Svet Ganov013efe12017-04-13 21:56:16 -070089 @Retention(RetentionPolicy.SOURCE)
90 @interface RequestFlags{}
91
92 private final int mId;
93 private final @RequestFlags int mFlags;
Philip P. Moltmann121e5262017-04-25 13:33:41 -070094 private final @NonNull ArrayList<FillContext> mContexts;
Svet Ganov013efe12017-04-13 21:56:16 -070095 private final @Nullable Bundle mClientState;
96
Svet Ganov013efe12017-04-13 21:56:16 -070097 private FillRequest(@NonNull Parcel parcel) {
98 mId = parcel.readInt();
Philip P. Moltmann121e5262017-04-25 13:33:41 -070099 mContexts = new ArrayList<>();
100 parcel.readParcelableList(mContexts, null);
101
Svet Ganov013efe12017-04-13 21:56:16 -0700102 mClientState = parcel.readBundle();
103 mFlags = parcel.readInt();
104 }
105
Philip P. Moltmann2f517c22017-04-21 16:33:52 -0700106 /** @hide */
Philip P. Moltmann121e5262017-04-25 13:33:41 -0700107 public FillRequest(int id, @NonNull ArrayList<FillContext> contexts,
Svet Ganov013efe12017-04-13 21:56:16 -0700108 @Nullable Bundle clientState, @RequestFlags int flags) {
109 mId = id;
Felipe Lemeeacd74d2018-08-02 10:36:34 -0700110 mFlags = Preconditions.checkFlagsArgument(flags,
111 FLAG_MANUAL_REQUEST | FLAG_COMPATIBILITY_MODE_REQUEST);
Philip P. Moltmann121e5262017-04-25 13:33:41 -0700112 mContexts = Preconditions.checkCollectionElementsNotNull(contexts, "contexts");
Svet Ganov013efe12017-04-13 21:56:16 -0700113 mClientState = clientState;
114 }
115
116 /**
Felipe Leme2ef19c12017-06-05 11:32:32 -0700117 * Gets the unique id of this request.
Svet Ganov013efe12017-04-13 21:56:16 -0700118 */
119 public int getId() {
120 return mId;
121 }
122
123 /**
Felipe Leme2ef19c12017-06-05 11:32:32 -0700124 * Gets the flags associated with this request.
Svet Ganov013efe12017-04-13 21:56:16 -0700125 *
Felipe Lemea7de4022019-02-19 17:16:45 -0800126 * @return any combination of {@link #FLAG_MANUAL_REQUEST} and
127 * {@link #FLAG_COMPATIBILITY_MODE_REQUEST}.
Svet Ganov013efe12017-04-13 21:56:16 -0700128 */
129 public @RequestFlags int getFlags() {
130 return mFlags;
131 }
132
133 /**
Felipe Leme2ef19c12017-06-05 11:32:32 -0700134 * Gets the contexts associated with each previous fill request.
Felipe Lemec7ee7af2018-08-27 12:36:16 -0700135 *
136 * <p><b>Note:</b> Starting on Android {@link android.os.Build.VERSION_CODES#Q}, it could also
137 * include contexts from requests whose {@link SaveInfo} had the
138 * {@link SaveInfo#FLAG_DELAY_SAVE} flag.
Svet Ganov013efe12017-04-13 21:56:16 -0700139 */
Jeff Sharkey000ce802017-04-29 13:13:27 -0600140 public @NonNull List<FillContext> getFillContexts() {
Philip P. Moltmann121e5262017-04-25 13:33:41 -0700141 return mContexts;
Svet Ganov013efe12017-04-13 21:56:16 -0700142 }
143
Felipe Lemea012d7e2017-09-06 15:19:11 -0700144 @Override
145 public String toString() {
146 return "FillRequest: [id=" + mId + ", flags=" + mFlags + ", ctxts= " + mContexts + "]";
147 }
148
Svet Ganov013efe12017-04-13 21:56:16 -0700149 /**
Felipe Lemed37f53e2017-12-07 10:41:10 -0800150 * Gets the latest client state bundle set by the service in a
151 * {@link FillResponse.Builder#setClientState(Bundle) fill response}.
Felipe Leme2ef19c12017-06-05 11:32:32 -0700152 *
Felipe Lemed37f53e2017-12-07 10:41:10 -0800153 * <p><b>Note:</b> Prior to Android {@link android.os.Build.VERSION_CODES#P}, only client state
154 * bundles set by {@link FillResponse.Builder#setClientState(Bundle)} were considered. On
155 * Android {@link android.os.Build.VERSION_CODES#P} and higher, bundles set in the result of
156 * an authenticated request through the
157 * {@link android.view.autofill.AutofillManager#EXTRA_CLIENT_STATE} extra are
158 * also considered (and take precedence when set).
Svet Ganov013efe12017-04-13 21:56:16 -0700159 *
160 * @return The client state.
161 */
162 public @Nullable Bundle getClientState() {
163 return mClientState;
164 }
165
166 @Override
167 public int describeContents() {
168 return 0;
169 }
170
171 @Override
172 public void writeToParcel(Parcel parcel, int flags) {
173 parcel.writeInt(mId);
Philip P. Moltmann121e5262017-04-25 13:33:41 -0700174 parcel.writeParcelableList(mContexts, flags);
Svet Ganov013efe12017-04-13 21:56:16 -0700175 parcel.writeBundle(mClientState);
176 parcel.writeInt(mFlags);
177 }
178
179 public static final Parcelable.Creator<FillRequest> CREATOR =
180 new Parcelable.Creator<FillRequest>() {
181 @Override
182 public FillRequest createFromParcel(Parcel parcel) {
183 return new FillRequest(parcel);
184 }
185
186 @Override
187 public FillRequest[] newArray(int size) {
188 return new FillRequest[size];
189 }
190 };
191}