FillRequest.java revision 2f517c26d23393d4f0d6b3352de6e4c92c9e107e
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.app.assist.AssistStructure;
23import android.os.Bundle;
24import android.os.CancellationSignal;
25import android.os.Parcel;
26import android.os.Parcelable;
27import com.android.internal.util.Preconditions;
28
29import java.lang.annotation.Retention;
30import java.lang.annotation.RetentionPolicy;
31import java.util.concurrent.atomic.AtomicInteger;
32
33/**
34 * This class represents a request to an {@link AutofillService autofill provider}
35 * to interpret the screen and provide information to the system which views are
36 * interesting for saving and what are the possible ways to fill the inputs on
37 * the screen if applicable.
38 *
39 * @see AutofillService#onFillRequest(FillRequest, CancellationSignal, FillCallback)
40 */
41public final class FillRequest implements Parcelable {
42    /**
43     * Indicates autofill was explicitly requested by the user.
44     */
45    public static final int FLAG_MANUAL_REQUEST = 0x1;
46
47    /** @hide */
48    @IntDef(
49        flag = true,
50        value = {FLAG_MANUAL_REQUEST})
51    @Retention(RetentionPolicy.SOURCE)
52    @interface RequestFlags{}
53
54    private final int mId;
55    private final @RequestFlags int mFlags;
56    private final @NonNull AssistStructure mStructure;
57    private final @Nullable Bundle mClientState;
58
59    private FillRequest(@NonNull Parcel parcel) {
60        mId = parcel.readInt();
61        mStructure = parcel.readParcelable(null);
62        mClientState = parcel.readBundle();
63        mFlags = parcel.readInt();
64    }
65
66    /** @hide */
67    public FillRequest(int id, @NonNull AssistStructure structure,
68            @Nullable Bundle clientState, @RequestFlags int flags) {
69        mId = id;
70        mFlags = Preconditions.checkFlagsArgument(flags, FLAG_MANUAL_REQUEST);
71        mStructure = Preconditions.checkNotNull(structure, "structure");
72        mClientState = clientState;
73    }
74
75    /**
76     * @return The unique id of this request.
77     */
78    public int getId() {
79        return mId;
80    }
81
82    /**
83     * @return The flags associated with this request.
84     *
85     * @see #FLAG_MANUAL_REQUEST
86     */
87    public @RequestFlags int getFlags() {
88        return mFlags;
89    }
90
91    /**
92     * @return The structure capturing the UI state.
93     */
94    public @NonNull AssistStructure getStructure() {
95        return mStructure;
96    }
97
98    /**
99     * Gets the extra client state returned from the last {@link
100     * AutofillService#onFillRequest(FillRequest, CancellationSignal, FillCallback)
101     * fill request}.
102     * <p>
103     * Once a {@link AutofillService#onSaveRequest(SaveRequest, SaveCallback)
104     * save request} is made the client state is cleared.
105     *
106     * @return The client state.
107     */
108    public @Nullable Bundle getClientState() {
109        return mClientState;
110    }
111
112    @Override
113    public int describeContents() {
114        return 0;
115    }
116
117    @Override
118    public void writeToParcel(Parcel parcel, int flags) {
119        parcel.writeInt(mId);
120        parcel.writeParcelable(mStructure, flags);
121        parcel.writeBundle(mClientState);
122        parcel.writeInt(mFlags);
123    }
124
125    public static final Parcelable.Creator<FillRequest> CREATOR =
126            new Parcelable.Creator<FillRequest>() {
127        @Override
128        public FillRequest createFromParcel(Parcel parcel) {
129            return new FillRequest(parcel);
130        }
131
132        @Override
133        public FillRequest[] newArray(int size) {
134            return new FillRequest[size];
135        }
136    };
137}
138