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 com.android.server.autofill;
18
19import static android.service.autofill.FillRequest.FLAG_MANUAL_REQUEST;
20import static com.android.server.autofill.Helper.sDebug;
21import static com.android.server.autofill.Helper.sVerbose;
22
23import android.annotation.Nullable;
24import android.graphics.Rect;
25import android.service.autofill.FillResponse;
26import android.util.DebugUtils;
27import android.util.Slog;
28import android.view.autofill.AutofillId;
29import android.view.autofill.AutofillValue;
30
31import java.io.PrintWriter;
32
33/**
34 * State for a given view with a AutofillId.
35 *
36 * <p>This class holds state about a view and calls its listener when the fill UI is ready to
37 * be displayed for the view.
38 */
39final class ViewState {
40    interface Listener {
41        /**
42         * Called when the fill UI is ready to be shown for this view.
43         */
44        void onFillReady(FillResponse fillResponse, AutofillId focusedId,
45                @Nullable AutofillValue value);
46    }
47
48    private static final String TAG = "ViewState";
49
50    // NOTE: state constants must be public because of flagstoString().
51    public static final int STATE_UNKNOWN = 0x000;
52    /** Initial state. */
53    public static final int STATE_INITIAL = 0x001;
54    /** View id is present in a dataset returned by the service. */
55    public static final int STATE_FILLABLE = 0x002;
56    /** View was autofilled after user selected a dataset. */
57    public static final int STATE_AUTOFILLED = 0x004;
58    /** View value was changed, but not by the service. */
59    public static final int STATE_CHANGED = 0x008;
60    /** Set only in the View that started a session. */
61    public static final int STATE_STARTED_SESSION = 0x010;
62    /** View that started a new partition when focused on. */
63    public static final int STATE_STARTED_PARTITION = 0x020;
64    /** User select a dataset in this view, but service must authenticate first. */
65    public static final int STATE_WAITING_DATASET_AUTH = 0x040;
66    /** Service does not care about this view. */
67    public static final int STATE_IGNORED = 0x080;
68    /** User manually request autofill in this view, after it was already autofilled. */
69    public static final int STATE_RESTARTED_SESSION = 0x100;
70
71    public final AutofillId id;
72
73    private final Listener mListener;
74    private final Session mSession;
75
76    private FillResponse mResponse;
77    private AutofillValue mCurrentValue;
78    private AutofillValue mAutofilledValue;
79    private Rect mVirtualBounds;
80    private int mState;
81
82    ViewState(Session session, AutofillId id, Listener listener, int state) {
83        mSession = session;
84        this.id = id;
85        mListener = listener;
86        mState = state;
87    }
88
89    /**
90     * Gets the boundaries of the virtual view, or {@code null} if the the view is not virtual.
91     */
92    @Nullable
93    Rect getVirtualBounds() {
94        return mVirtualBounds;
95    }
96
97    /**
98     * Gets the current value of the view.
99     */
100    @Nullable
101    AutofillValue getCurrentValue() {
102        return mCurrentValue;
103    }
104
105    void setCurrentValue(AutofillValue value) {
106        mCurrentValue = value;
107    }
108
109    @Nullable
110    AutofillValue getAutofilledValue() {
111        return mAutofilledValue;
112    }
113
114    void setAutofilledValue(@Nullable AutofillValue value) {
115        mAutofilledValue = value;
116    }
117
118    @Nullable
119    FillResponse getResponse() {
120        return mResponse;
121    }
122
123    void setResponse(FillResponse response) {
124        mResponse = response;
125    }
126
127    CharSequence getServiceName() {
128        return mSession.getServiceName();
129    }
130
131    int getState() {
132        return mState;
133    }
134
135    String getStateAsString() {
136        return DebugUtils.flagsToString(ViewState.class, "STATE_", mState);
137    }
138
139    void setState(int state) {
140        if (mState == STATE_INITIAL) {
141            mState = state;
142        } else {
143            mState |= state;
144        }
145    }
146
147    void resetState(int state) {
148        mState &= ~state;
149    }
150
151    // TODO: refactor / rename / document this method (and maybeCallOnFillReady) to make it clear
152    // that it can change the value and update the UI; similarly, should replace code that
153    // directly sets mAutofillValue to use encapsulation.
154    void update(@Nullable AutofillValue autofillValue, @Nullable Rect virtualBounds, int flags) {
155        if (autofillValue != null) {
156            mCurrentValue = autofillValue;
157        }
158        if (virtualBounds != null) {
159            mVirtualBounds = virtualBounds;
160        }
161
162        maybeCallOnFillReady(flags);
163    }
164
165    /**
166     * Calls {@link
167     * Listener#onFillReady(FillResponse, AutofillId, AutofillValue)} if the
168     * fill UI is ready to be displayed (i.e. when response and bounds are set).
169     */
170    void maybeCallOnFillReady(int flags) {
171        if ((mState & STATE_AUTOFILLED) != 0 && (flags & FLAG_MANUAL_REQUEST) == 0) {
172            if (sDebug) Slog.d(TAG, "Ignoring UI for " + id + " on " + getStateAsString());
173            return;
174        }
175        // First try the current response associated with this View.
176        if (mResponse != null) {
177            if (mResponse.getDatasets() != null || mResponse.getAuthentication() != null) {
178                mListener.onFillReady(mResponse, this.id, mCurrentValue);
179            }
180        }
181    }
182
183    @Override
184    public String toString() {
185        return "ViewState: [id=" + id + ", currentValue=" + mCurrentValue
186                + ", autofilledValue=" + mAutofilledValue
187                + ", bounds=" + mVirtualBounds + ", state=" + getStateAsString() + "]";
188    }
189
190    void dump(String prefix, PrintWriter pw) {
191        pw.print(prefix); pw.print("id:" ); pw.println(this.id);
192        pw.print(prefix); pw.print("state:" ); pw.println(getStateAsString());
193        pw.print(prefix); pw.print("response:");
194        if (mResponse == null) {
195            pw.println("N/A");
196        } else {
197            if (sVerbose) {
198                pw.println(mResponse);
199            } else {
200                pw.println(mResponse.getRequestId());
201            }
202        }
203        pw.print(prefix); pw.print("currentValue:" ); pw.println(mCurrentValue);
204        pw.print(prefix); pw.print("autofilledValue:" ); pw.println(mAutofilledValue);
205        pw.print(prefix); pw.print("virtualBounds:" ); pw.println(mVirtualBounds);
206    }
207}