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