aboutsummaryrefslogtreecommitdiff
path: root/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/model/AutofillField.kt
blob: 315299bec79a8bdd0a7da1657a5460087e67bed0 (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
/*
 * 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.service.model

import android.app.assist.AssistStructure.ViewNode;
import android.service.autofill.SaveInfo
import android.view.View
import android.view.autofill.AutofillId

/**
 * A stripped down version of a [ViewNode] that contains only autofill-relevant metadata. It also
 * contains a `saveType` flag that is calculated based on the [ViewNode]'s autofill hints.
 */
class AutofillField(view: ViewNode) {
    var saveType = 0
        private set

    val autofillHints: Array<String> = view.autofillHints
    val autofillId: AutofillId = view.autofillId
    val autofillType: Int = view.autofillType
    val autofillOptions: Array<String>? = view.autofillOptions
    val isFocused: Boolean = view.isFocused

    init {
        updateSaveTypeFromHints()
    }

    /**
     * When the [ViewNode] is a list that the user needs to choose a string from (i.e. a spinner),
     * this is called to return the index of a specific item in the list.
     */
    fun getAutofillOptionIndex(value: String): Int? {
        return autofillOptions?.indexOf(value)
    }

    private fun updateSaveTypeFromHints() {
        saveType = 0
        for (hint in autofillHints) {
            when (hint) {
                View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE,
                View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DAY,
                View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH,
                View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR,
                View.AUTOFILL_HINT_CREDIT_CARD_NUMBER,
                View.AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE -> {
                    saveType = saveType or SaveInfo.SAVE_DATA_TYPE_CREDIT_CARD
                }
                View.AUTOFILL_HINT_EMAIL_ADDRESS -> {
                    saveType = saveType or SaveInfo.SAVE_DATA_TYPE_EMAIL_ADDRESS
                }
                View.AUTOFILL_HINT_PHONE, View.AUTOFILL_HINT_NAME -> {
                    saveType = saveType or SaveInfo.SAVE_DATA_TYPE_GENERIC
                }
                View.AUTOFILL_HINT_PASSWORD -> {
                    saveType = saveType or SaveInfo.SAVE_DATA_TYPE_PASSWORD
                    saveType = saveType and SaveInfo.SAVE_DATA_TYPE_EMAIL_ADDRESS.inv()
                    saveType = saveType and SaveInfo.SAVE_DATA_TYPE_USERNAME.inv()
                }
                View.AUTOFILL_HINT_POSTAL_ADDRESS,
                View.AUTOFILL_HINT_POSTAL_CODE -> {
                    saveType = saveType or SaveInfo.SAVE_DATA_TYPE_ADDRESS
                }
                View.AUTOFILL_HINT_USERNAME -> {
                    saveType = saveType or SaveInfo.SAVE_DATA_TYPE_USERNAME
                }
            }
        }
    }
}