aboutsummaryrefslogtreecommitdiff
path: root/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/AutofillHelper.kt
blob: dc62d12bbafa99a017ed2871e2d53e0614a19c37 (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
/*
 * 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.multidatasetservice

import android.content.Context
import android.service.autofill.Dataset
import android.service.autofill.FillResponse
import android.service.autofill.SaveInfo
import android.support.annotation.DrawableRes
import android.util.Log
import android.view.View
import android.widget.RemoteViews
import com.example.android.autofillframework.CommonUtil.TAG
import com.example.android.autofillframework.R
import com.example.android.autofillframework.multidatasetservice.model.FilledAutofillFieldCollection
import java.util.HashMap


/**
 * This is a class containing helper methods for building Autofill Datasets and Responses.
 */
object AutofillHelper {

    /**
     * Wraps autofill data in a [Dataset] object which can then be sent back to the
     * client View.
     */
    fun newDataset(context: Context, autofillFieldMetadata: AutofillFieldMetadataCollection,
            filledAutofillFieldCollection: FilledAutofillFieldCollection,
            datasetAuth: Boolean): Dataset? {
        filledAutofillFieldCollection.datasetName?.let { datasetName ->
            val datasetBuilder: Dataset.Builder
            if (datasetAuth) {
                datasetBuilder = Dataset.Builder(newRemoteViews(context.packageName, datasetName,
                        R.drawable.ic_lock_black_24dp))
                val sender = AuthActivity.getAuthIntentSenderForDataset(context, datasetName)
                datasetBuilder.setAuthentication(sender)
            } else {
                datasetBuilder = Dataset.Builder(newRemoteViews(context.packageName, datasetName,
                        R.drawable.ic_person_black_24dp))
            }
            val setValueAtLeastOnce = filledAutofillFieldCollection
                    .applyToFields(autofillFieldMetadata, datasetBuilder)
            if (setValueAtLeastOnce) {
                return datasetBuilder.build()
            }
        }
        return null
    }

    fun newRemoteViews(packageName: String, remoteViewsText: String,
            @DrawableRes drawableId: Int): RemoteViews {
        val presentation = RemoteViews(packageName, R.layout.multidataset_service_list_item)
        presentation.setTextViewText(R.id.text, remoteViewsText)
        presentation.setImageViewResource(R.id.icon, drawableId)
        return presentation
    }

    /**
     * Wraps autofill data in a [FillResponse] object (essentially a series of Datasets) which can
     * then be sent back to the client View.
     */
    fun newResponse(context: Context,
            datasetAuth: Boolean, autofillFieldMetadata: AutofillFieldMetadataCollection,
            filledAutofillFieldCollectionMap: HashMap<String, FilledAutofillFieldCollection>?): FillResponse? {
        val responseBuilder = FillResponse.Builder()
        filledAutofillFieldCollectionMap?.keys?.let { datasetNames ->
            for (datasetName in datasetNames) {
                filledAutofillFieldCollectionMap[datasetName]?.let { clientFormData ->
                    val dataset = newDataset(context, autofillFieldMetadata, clientFormData, datasetAuth)
                    dataset?.let(responseBuilder::addDataset)
                }
            }
        }
        if (autofillFieldMetadata.saveType != 0) {
            val autofillIds = autofillFieldMetadata.autofillIds
            responseBuilder.setSaveInfo(SaveInfo.Builder(autofillFieldMetadata.saveType,
                    autofillIds.toTypedArray()).build())
            return responseBuilder.build()
        } else {
            Log.d(TAG, "These fields are not meant to be saved by autofill.")
            return null
        }
    }

    fun isValidHint(hint: String): Boolean {
        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,
            View.AUTOFILL_HINT_EMAIL_ADDRESS,
            View.AUTOFILL_HINT_PHONE,
            View.AUTOFILL_HINT_NAME,
            View.AUTOFILL_HINT_PASSWORD,
            View.AUTOFILL_HINT_POSTAL_ADDRESS,
            View.AUTOFILL_HINT_POSTAL_CODE,
            View.AUTOFILL_HINT_USERNAME ->
                return true
            else ->
                return false
        }
    }
}