FillContext.java revision 013efe173e56612a910ebd8576480ce4ef005e3c
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.NonNull;
20import android.app.assist.AssistStructure;
21import android.os.Bundle;
22import android.os.CancellationSignal;
23import android.os.Parcel;
24import android.os.Parcelable;
25
26/**
27 * This class represents a context for each fill request made via {@link
28 * AutofillService#onFillRequest(FillRequest, CancellationSignal, FillCallback)}.
29 * It contains a snapshot of the UI state, the view ids that were returned by
30 * the {@link AutofillService autofill service} as both required to trigger a save
31 * and optional that can be saved, and the id of the corresponding {@link
32 * FillRequest}.
33 * <p>
34 * This context allows you to inspect the values for the interesting views
35 * in the context they appeared. Also a reference to the corresponding fill
36 * request is useful to store meta-data in the client state bundle passed
37 * to {@link FillResponse.Builder#setClientState(Bundle)} to avoid interpreting
38 * the UI state again while saving.
39 */
40public final class FillContext implements Parcelable {
41    private final int mRequestId;
42    private final @NonNull AssistStructure mStructure;
43
44    /** @hide */
45    public FillContext(int requestId, @NonNull AssistStructure structure) {
46        mRequestId = requestId;
47        mStructure = structure;
48    }
49
50    private FillContext(Parcel parcel) {
51        this(parcel.readInt(), parcel.readParcelable(null));
52    }
53
54    /**
55     * Gets the id of the {@link FillRequest fill request} this context
56     * corresponds to. This is useful to associate your custom client
57     * state with every request to avoid reinterpreting the UI when saving
58     * user data.
59     *
60     * @return The request id.
61     */
62    public int getRequestId() {
63        return mRequestId;
64    }
65
66    /**
67     * @return The screen content.
68     */
69    public AssistStructure getStructure() {
70        return mStructure;
71    }
72
73    @Override
74    public int describeContents() {
75        return 0;
76    }
77
78    @Override
79    public void writeToParcel(Parcel parcel, int flags) {
80        parcel.writeInt(mRequestId);
81        parcel.writeParcelable(mStructure, flags);
82    }
83
84    public static final Parcelable.Creator<FillContext> CREATOR =
85            new Parcelable.Creator<FillContext>() {
86        @Override
87        public FillContext createFromParcel(Parcel parcel) {
88            return new FillContext(parcel);
89        }
90
91        @Override
92        public FillContext[] newArray(int size) {
93            return new FillContext[size];
94        }
95    };
96}
97