aboutsummaryrefslogtreecommitdiff
path: root/input/autofill/AutofillFramework/Application/src/main/java/com/example/android/autofillframework/app/CustomVirtualView.java
blob: dc09de53d1de0eaf015dd3a425b598184787a9eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.android.autofillframework.app;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewStructure;
import android.view.autofill.AutofillManager;
import android.view.autofill.AutofillValue;
import android.widget.EditText;
import android.widget.TextView;

import com.example.android.autofillframework.R;

import java.util.ArrayList;
import java.util.Arrays;

import static com.example.android.autofillframework.CommonUtil.bundleToString;


/**
 * Custom View with virtual child views for Username/Password text fields.
 */
public class CustomVirtualView extends View {

    private static final String TAG = "CustomView";

    private static int nextId;

    private final ArrayList<Line> mLines = new ArrayList<>();
    private final SparseArray<Item> mItems = new SparseArray<>();
    private final AutofillManager mAfm;

    private Line mFocusedLine;
    private Paint mTextPaint;
    private int mTextHeight;
    private int mTopMargin;
    private int mLeftMargin;
    private int mVerticalGap;
    private int mLineLength;
    private int mFocusedColor;
    private int mUnfocusedColor;

    private Line mUsernameLine;
    private Line mPasswordLine;

    public CustomVirtualView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mAfm = context.getSystemService(AutofillManager.class);

        mTextPaint = new Paint();

        mUnfocusedColor = Color.BLACK;
        mFocusedColor = Color.RED;
        mTextPaint.setStyle(Style.FILL);
        mTopMargin = 100;
        mLeftMargin = 100;
        mTextHeight = 90;
        mVerticalGap = 10;

        mLineLength = mTextHeight + mVerticalGap;
        mTextPaint.setTextSize(mTextHeight);
        mUsernameLine = addLine("usernameField", context.getString(R.string.username_label),
                new String[]{View.AUTOFILL_HINT_USERNAME}, "         ", true);
        mPasswordLine = addLine("passwordField", context.getString(R.string.password_label),
                new String[]{View.AUTOFILL_HINT_PASSWORD}, "         ", false);

        Log.d(TAG, "Text height: " + mTextHeight);
    }

    @Override
    public void autofill(SparseArray<AutofillValue> values) {
        // User has just selected a Dataset from the list of autofill suggestions.
        // The Dataset is comprised of a list of AutofillValues, with each AutofillValue meant
        // to fill a specific autofillable view. Now we have to update the UI based on the
        // AutofillValues in the list.
        Log.d(TAG, "autoFill(): " + values);
        for (int i = 0; i < values.size(); i++) {
            final int id = values.keyAt(i);
            final AutofillValue value = values.valueAt(i);
            final Item item = mItems.get(id);
            if (item == null) {
                Log.w(TAG, "No item for id " + id);
                return;
            }
            if (!item.editable) {
                Log.w(TAG, "Item for id " + id + " is not editable: " + item);
                return;
            }
            // Set the item's text to the text wrapped in the AutofillValue.
            item.text = value.getTextValue();
        }
        postInvalidate();
    }


    @Override
    public void onProvideAutofillVirtualStructure(ViewStructure structure, int flags) {
        // Build a ViewStructure that will get passed to the AutofillService by the framework
        // when it is time to find autofill suggestions.
        structure.setClassName(getClass().getName());
        int childrenSize = mItems.size();
        Log.d(TAG, "onProvideAutofillVirtualStructure(): flags = " + flags + ", items = "
                + childrenSize + ", extras: " + bundleToString(structure.getExtras()));
        int index = structure.addChildCount(childrenSize);
        // Traverse through the view hierarchy, including virtual child views. For each view, we
        // need to set the relevant autofill metadata and add it to the ViewStructure.
        for (int i = 0; i < childrenSize; i++) {
            Item item = mItems.valueAt(i);
            Log.d(TAG, "Adding new child at index " + index + ": " + item);
            ViewStructure child = structure.newChild(index);
            child.setAutofillId(structure, item.id);
            child.setAutofillHints(item.hints);
            child.setAutofillType(item.type);
            child.setDataIsSensitive(!item.sanitized);
            child.setText(item.text);
            child.setAutofillValue(AutofillValue.forText(item.text));
            child.setFocused(item.focused);
            child.setId(item.id, getContext().getPackageName(), null, item.line.idEntry);
            child.setClassName(item.getClassName());
            index++;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Log.d(TAG, "onDraw: " + mLines.size() + " lines; canvas:" + canvas);
        float x;
        float y = mTopMargin + mLineLength;
        for (int i = 0; i < mLines.size(); i++) {
            x = mLeftMargin;
            Line line = mLines.get(i);
            Log.v(TAG, "Drawing '" + line + "' at " + x + "x" + y);
            mTextPaint.setColor(line.fieldTextItem.focused ? mFocusedColor : mUnfocusedColor);
            String readOnlyText = line.labelItem.text + ":  [";
            String writeText = line.fieldTextItem.text + "]";
            // Paints the label first...
            canvas.drawText(readOnlyText, x, y, mTextPaint);
            // ...then paints the edit text and sets the proper boundary
            float deltaX = mTextPaint.measureText(readOnlyText);
            x += deltaX;
            line.bounds.set((int) x, (int) (y - mLineLength),
                    (int) (x + mTextPaint.measureText(writeText)), (int) y);
            Log.d(TAG, "setBounds(" + x + ", " + y + "): " + line.bounds);
            canvas.drawText(writeText, x, y, mTextPaint);
            y += mLineLength;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int y = (int) event.getY();
        Log.d(TAG, "Touched: y=" + y + ", range=" + mLineLength + ", top=" + mTopMargin);
        int lowerY = mTopMargin;
        int upperY = -1;
        for (int i = 0; i < mLines.size(); i++) {
            upperY = lowerY + mLineLength;
            Line line = mLines.get(i);
            Log.d(TAG, "Line " + i + " ranges from " + lowerY + " to " + upperY);
            if (lowerY <= y && y <= upperY) {
                if (mFocusedLine != null) {
                    Log.d(TAG, "Removing focus from " + mFocusedLine);
                    mFocusedLine.changeFocus(false);
                }
                Log.d(TAG, "Changing focus to " + line);
                mFocusedLine = line;
                mFocusedLine.changeFocus(true);
                invalidate();
                break;
            }
            lowerY += mLineLength;
        }
        return super.onTouchEvent(event);
    }

    public CharSequence getUsernameText() {
        return mUsernameLine.fieldTextItem.text;
    }

    public CharSequence getPasswordText() {
        return mPasswordLine.fieldTextItem.text;
    }

    public void resetFields() {
        mUsernameLine.reset();
        mPasswordLine.reset();
        postInvalidate();
    }

    private Line addLine(String idEntry, String label, String[] hints, String text, boolean sanitized) {
        Line line = new Line(idEntry, label, hints, text, sanitized);
        mLines.add(line);
        mItems.put(line.labelItem.id, line.labelItem);
        mItems.put(line.fieldTextItem.id, line.fieldTextItem);
        return line;
    }

    private static final class Item {
        private final Line line;
        private final int id;
        private final boolean editable;
        private final boolean sanitized;
        private final String[] hints;
        private final int type;
        private CharSequence text;
        private boolean focused = false;

        Item(Line line, int id, String[] hints, int type, CharSequence text, boolean editable, boolean sanitized) {
            this.line = line;
            this.id = id;
            this.text = text;
            this.editable = editable;
            this.sanitized = sanitized;
            this.hints = hints;
            this.type = type;
        }

        @Override
        public String toString() {
            return id + ": " + text + (editable ? " (editable)" : " (read-only)"
                    + (sanitized ? " (sanitized)" : " (sensitive"));
        }

        public String getClassName() {
            return editable ? EditText.class.getName() : TextView.class.getName();
        }
    }

    private final class Line {

        // Boundaries of the text field, relative to the CustomView
        final Rect bounds = new Rect();
        private Item labelItem;
        private Item fieldTextItem;
        private String idEntry;

        private Line(String idEntry, String label, String[] hints, String text, boolean sanitized) {
            this.idEntry = idEntry;
            this.labelItem = new Item(this, ++nextId, null, AUTOFILL_TYPE_NONE, label, false, true);
            this.fieldTextItem = new Item(this, ++nextId, hints, AUTOFILL_TYPE_TEXT, text, true, sanitized);
        }

        void changeFocus(boolean focused) {
            fieldTextItem.focused = focused;
            if (focused) {
                final Rect absBounds = getAbsCoordinates();
                Log.d(TAG, "focus gained on " + fieldTextItem.id + "; absBounds=" + absBounds);
                mAfm.notifyViewEntered(CustomVirtualView.this, fieldTextItem.id, absBounds);
            } else {
                Log.d(TAG, "focus lost on " + fieldTextItem.id);
                mAfm.notifyViewExited(CustomVirtualView.this, fieldTextItem.id);
            }
        }

        private Rect getAbsCoordinates() {
            // Must offset the boundaries so they're relative to the CustomView.
            final int offset[] = new int[2];
            getLocationOnScreen(offset);
            final Rect absBounds = new Rect(bounds.left + offset[0],
                    bounds.top + offset[1],
                    bounds.right + offset[0], bounds.bottom + offset[1]);
            Log.v(TAG, "getAbsCoordinates() for " + fieldTextItem.id + ": bounds=" + bounds
                    + " offset: " + Arrays.toString(offset) + " absBounds: " + absBounds);
            return absBounds;
        }

        public void reset() {
            fieldTextItem.text = "        ";
        }

        @Override
        public String toString() {
            return "Label: " + labelItem + " Text: " + fieldTextItem + " Focused: " +
                    fieldTextItem.focused;
        }
    }
}