aboutsummaryrefslogtreecommitdiff
path: root/input/autofill/AutofillFramework/afservice/src/main/java/com/example/android/autofill/service/AutofillHintProperties.java
blob: 3da54c1d9f0540410d64fca1873edf08a73900d6 (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
/*
 * 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.autofill.service;

import android.view.View;

import com.example.android.autofill.service.model.FilledAutofillField;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * Holds the properties associated with an autofill hint in this Autofill Service.
 */
public final class AutofillHintProperties {

    private String mAutofillHint;
    private FakeFieldGenerator mFakeFieldGenerator;
    private Set<Integer> mValidTypes;
    private int mSaveType;
    private int mPartition;

    public AutofillHintProperties(String autofillHint, int saveType, int partitionNumber,
            FakeFieldGenerator fakeFieldGenerator, Integer... validTypes) {
        mAutofillHint = autofillHint;
        mSaveType = saveType;
        mPartition = partitionNumber;
        mFakeFieldGenerator = fakeFieldGenerator;
        mValidTypes = new HashSet<>(Arrays.asList(validTypes));
    }

    /**
     * Generates placeholder autofill field data that is relevant to the autofill hint.
     */
    public FilledAutofillField generateFakeField(int seed, String datasetId) {
        return mFakeFieldGenerator.generate(seed, datasetId);
    }

    /**
     * Returns autofill hint associated with these properties. If you save a field that uses a W3C
     * hint, there is a chance this will return a different but analogous hint, when applicable.
     * For example, W3C has hint 'email' and {@link android.view.View} has hint 'emailAddress', so
     * the W3C hint should map to the hint defined in {@link android.view.View} ('emailAddress').
     */
    public String getAutofillHint() {
        return mAutofillHint;
    }

    /**
     * Returns how this hint maps to a {@link android.service.autofill.SaveInfo} type.
     */
    public int getSaveType() {
        return mSaveType;
    }

    /**
     * Returns which data partition this autofill hint should be a part of. See partitions defined
     * in {@link AutofillHints}.
     */
    public int getPartition() {
        return mPartition;
    }


    /**
     * Sometimes, data for a hint should only be stored as a certain AutofillValue type. For
     * example, it is recommended that data representing a Credit Card Expiration date, annotated
     * with the hint {@link View#AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE}, should
     * only be stored as {@link View#AUTOFILL_TYPE_DATE}.
     */
    public boolean isValidType(int type) {
        return mValidTypes.contains(type);
    }

    public Set<Integer> getTypes() {
        return mValidTypes;
    }
}