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 
17 package android.service.autofill;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.os.Bundle;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Objects;
28 
29 /**
30  * This class represents a request to an {@link AutofillService
31  * autofill provider} to save applicable data entered by the user.
32  *
33  * @see AutofillService#onSaveRequest(SaveRequest, SaveCallback)
34  */
35 public final class SaveRequest implements Parcelable {
36     private final @NonNull ArrayList<FillContext> mFillContexts;
37     private final @Nullable Bundle mClientState;
38     private final @Nullable ArrayList<String> mDatasetIds;
39 
40     /** @hide */
SaveRequest(@onNull ArrayList<FillContext> fillContexts, @Nullable Bundle clientState, @Nullable ArrayList<String> datasetIds)41     public SaveRequest(@NonNull ArrayList<FillContext> fillContexts,
42             @Nullable Bundle clientState, @Nullable ArrayList<String> datasetIds) {
43         mFillContexts = Objects.requireNonNull(fillContexts, "fillContexts");
44         mClientState = clientState;
45         mDatasetIds = datasetIds;
46     }
47 
SaveRequest(@onNull Parcel parcel)48     private SaveRequest(@NonNull Parcel parcel) {
49         this(parcel.createTypedArrayList(FillContext.CREATOR),
50                 parcel.readBundle(), parcel.createStringArrayList());
51     }
52 
53     /**
54      * Gets the contexts associated with each previous fill request.
55      *
56      * <p><b>Note:</b> Starting on Android {@link android.os.Build.VERSION_CODES#Q}, it could also
57      * include contexts from requests whose {@link SaveInfo} had the
58      * {@link SaveInfo#FLAG_DELAY_SAVE} flag.
59      *
60      * @return The contexts associated with each previous fill request.
61      */
getFillContexts()62     public @NonNull List<FillContext> getFillContexts() {
63         return mFillContexts;
64     }
65 
66     /**
67      * Gets the latest client state bundle set by the service in a
68      * {@link FillResponse.Builder#setClientState(Bundle) fill response}.
69      *
70      * <p><b>Note:</b> Prior to Android {@link android.os.Build.VERSION_CODES#P}, only client state
71      * bundles set by {@link FillResponse.Builder#setClientState(Bundle)} were considered. On
72      * Android {@link android.os.Build.VERSION_CODES#P} and higher, bundles set in the result of
73      * an authenticated request through the
74      * {@link android.view.autofill.AutofillManager#EXTRA_CLIENT_STATE} extra are
75      * also considered (and take precedence when set).
76      *
77      * @return The client state.
78      */
getClientState()79     public @Nullable Bundle getClientState() {
80         return mClientState;
81     }
82 
83     /**
84      * Gets the ids of the datasets selected by the user, in the order in which they were selected.
85      */
86     @Nullable
getDatasetIds()87     public List<String> getDatasetIds() {
88         return mDatasetIds;
89     }
90 
91     @Override
describeContents()92     public int describeContents() {
93         return 0;
94     }
95 
96     @Override
writeToParcel(Parcel parcel, int flags)97     public void writeToParcel(Parcel parcel, int flags) {
98         parcel.writeTypedList(mFillContexts, flags);
99         parcel.writeBundle(mClientState);
100         parcel.writeStringList(mDatasetIds);
101     }
102 
103     public static final @android.annotation.NonNull Creator<SaveRequest> CREATOR =
104             new Creator<SaveRequest>() {
105         @Override
106         public SaveRequest createFromParcel(Parcel parcel) {
107             return new SaveRequest(parcel);
108         }
109 
110         @Override
111         public SaveRequest[] newArray(int size) {
112             return new SaveRequest[size];
113         }
114     };
115 }
116