From c84feef5290bcabaababc23e86c59cc14a5af357 Mon Sep 17 00:00:00 2001 From: Douglas Sigelbaum Date: Wed, 7 Jun 2017 14:39:08 -0400 Subject: Renamed autofill Field classes. Also moved AutofillMetadata-related classes out of the model package. Bug: 38182790 Test: manual Change-Id: Ia86bad151a64c0487215622546a0ea7c67de2dcb --- .../multidatasetservice/AutofillFieldMetadata.kt | 82 ++++++++++++++++ .../AutofillFieldMetadataCollection.kt | 49 ++++++++++ .../multidatasetservice/AutofillHelper.kt | 27 +++--- .../multidatasetservice/MyAutofillService.kt | 2 +- .../multidatasetservice/StructureParser.kt | 18 ++-- .../datasource/AutofillRepository.kt | 8 +- .../datasource/SharedPrefsAutofillRepository.kt | 16 ++-- .../multidatasetservice/model/AutofillField.kt | 82 ---------------- .../model/AutofillFieldsCollection.kt | 51 ---------- .../multidatasetservice/model/ClientFormData.kt | 103 -------------------- .../model/FilledAutofillField.kt | 52 +++++++++++ .../model/FilledAutofillFieldCollection.kt | 104 +++++++++++++++++++++ .../model/SavableAutofillData.kt | 52 ----------- 13 files changed, 321 insertions(+), 325 deletions(-) create mode 100644 input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/AutofillFieldMetadata.kt create mode 100644 input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/AutofillFieldMetadataCollection.kt delete mode 100644 input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/AutofillField.kt delete mode 100644 input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/AutofillFieldsCollection.kt delete mode 100644 input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/ClientFormData.kt create mode 100644 input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/FilledAutofillField.kt create mode 100644 input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/FilledAutofillFieldCollection.kt delete mode 100644 input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/SavableAutofillData.kt (limited to 'input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework') diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/AutofillFieldMetadata.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/AutofillFieldMetadata.kt new file mode 100644 index 00000000..47539dfd --- /dev/null +++ b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/AutofillFieldMetadata.kt @@ -0,0 +1,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.multidatasetservice + +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 AutofillFieldMetadata(view: ViewNode) { + var saveType = 0 + private set + + val autofillHints: Array = view.autofillHints + val autofillId: AutofillId = view.autofillId + val autofillType: Int = view.autofillType + val autofillOptions: Array? = 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: CharSequence): 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 + } + } + } + } +} diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/AutofillFieldMetadataCollection.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/AutofillFieldMetadataCollection.kt new file mode 100644 index 00000000..4aa3e1fd --- /dev/null +++ b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/AutofillFieldMetadataCollection.kt @@ -0,0 +1,49 @@ +/* + * 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.view.autofill.AutofillId + +/** + * Data structure that stores a collection of `AutofillFieldMetadata`s. Contains all of the client's `View` + * hierarchy autofill-relevant metadata. + */ +data class AutofillFieldMetadataCollection(val autofillIds: java.util.ArrayList = java.util.ArrayList(), + val allAutofillHints: java.util.ArrayList = java.util.ArrayList(), + val focusedAutofillHints: java.util.ArrayList = java.util.ArrayList()) { + + private val autofillHintsToFieldsMap = java.util.HashMap>() + var saveType = 0 + private set + + fun add(autofillFieldMetadata: AutofillFieldMetadata) { + saveType = saveType or autofillFieldMetadata.saveType + autofillIds.add(autofillFieldMetadata.autofillId) + val hintsList = autofillFieldMetadata.autofillHints + allAutofillHints.addAll(hintsList) + if (autofillFieldMetadata.isFocused) { + focusedAutofillHints.addAll(hintsList) + } + autofillFieldMetadata.autofillHints.forEach { autofillHint -> + autofillHintsToFieldsMap[autofillHint] = autofillHintsToFieldsMap[autofillHint] ?: java.util.ArrayList() + autofillHintsToFieldsMap[autofillHint]?.add(autofillFieldMetadata) + } + } + + fun getFieldsForHint(hint: String): MutableList? { + return autofillHintsToFieldsMap[hint] + } +} diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/AutofillHelper.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/AutofillHelper.kt index 4d67d5bc..394f620f 100644 --- a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/AutofillHelper.kt +++ b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/AutofillHelper.kt @@ -23,8 +23,7 @@ import android.util.Log import android.widget.RemoteViews import com.example.android.autofillframework.CommonUtil.TAG import com.example.android.autofillframework.R -import com.example.android.autofillframework.multidatasetservice.model.AutofillFieldsCollection -import com.example.android.autofillframework.multidatasetservice.model.ClientFormData +import com.example.android.autofillframework.multidatasetservice.model.FilledAutofillFieldCollection import java.util.HashMap /** @@ -36,11 +35,11 @@ object AutofillHelper { * Wraps autofill data in a [Dataset] object which can then be sent back to the * client View. */ - fun newDataset(context: Context, autofillFields: AutofillFieldsCollection, - clientFormData: ClientFormData, datasetAuth: Boolean): Dataset? { - clientFormData.datasetName?.let { datasetName -> + fun newDataset(context: Context, autofillFieldMetadata: AutofillFieldMetadataCollection, + filledAutofillFieldCollection: FilledAutofillFieldCollection, datasetAuth: Boolean): Dataset? { + filledAutofillFieldCollection.datasetName?.let { datasetName -> val datasetBuilder = Dataset.Builder(newRemoteViews(context.packageName, datasetName)) - val setValueAtLeastOnce = clientFormData.applyToFields(autofillFields, datasetBuilder) + val setValueAtLeastOnce = filledAutofillFieldCollection.applyToFields(autofillFieldMetadata, datasetBuilder) if (datasetAuth) { val sender = AuthActivity.getAuthIntentSenderForDataset(context, datasetName) datasetBuilder.setAuthentication(sender) @@ -63,20 +62,20 @@ object AutofillHelper { * then be sent back to the client View. */ fun newResponse(context: Context, - datasetAuth: Boolean, autofillFields: AutofillFieldsCollection, - clientFormDataMap: HashMap?): FillResponse? { + datasetAuth: Boolean, autofillFieldMetadata: AutofillFieldMetadataCollection, + filledAutofillFieldCollectionMap: HashMap?): FillResponse? { val responseBuilder = FillResponse.Builder() - clientFormDataMap?.keys?.let { datasetNames -> + filledAutofillFieldCollectionMap?.keys?.let { datasetNames -> for (datasetName in datasetNames) { - clientFormDataMap[datasetName]?.let { clientFormData -> - val dataset = newDataset(context, autofillFields, clientFormData, datasetAuth) + filledAutofillFieldCollectionMap[datasetName]?.let { clientFormData -> + val dataset = newDataset(context, autofillFieldMetadata, clientFormData, datasetAuth) dataset?.let(responseBuilder::addDataset) } } } - if (autofillFields.saveType != 0) { - val autofillIds = autofillFields.autofillIds - responseBuilder.setSaveInfo(SaveInfo.Builder(autofillFields.saveType, + if (autofillFieldMetadata.saveType != 0) { + val autofillIds = autofillFieldMetadata.autofillIds + responseBuilder.setSaveInfo(SaveInfo.Builder(autofillFieldMetadata.saveType, autofillIds.toTypedArray()).build()) return responseBuilder.build() } else { diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/MyAutofillService.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/MyAutofillService.kt index f9169029..fa069241 100644 --- a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/MyAutofillService.kt +++ b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/MyAutofillService.kt @@ -78,7 +78,7 @@ class MyAutofillService : AutofillService() { Log.d(TAG, "onSaveRequest(): data=" + bundleToString(data)) val parser = StructureParser(structure) parser.parseForSave() - SharedPrefsAutofillRepository.saveClientFormData(this, parser.clientFormData) + SharedPrefsAutofillRepository.saveClientFormData(this, parser.filledAutofillFieldCollection) } override fun onConnected() { diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/StructureParser.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/StructureParser.kt index 78cab5ee..31b59c8a 100644 --- a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/StructureParser.kt +++ b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/StructureParser.kt @@ -19,10 +19,8 @@ import android.app.assist.AssistStructure import android.app.assist.AssistStructure.ViewNode import android.util.Log import com.example.android.autofillframework.CommonUtil.TAG -import com.example.android.autofillframework.multidatasetservice.model.AutofillField -import com.example.android.autofillframework.multidatasetservice.model.AutofillFieldsCollection -import com.example.android.autofillframework.multidatasetservice.model.ClientFormData -import com.example.android.autofillframework.multidatasetservice.model.SavableAutofillData +import com.example.android.autofillframework.multidatasetservice.model.FilledAutofillFieldCollection +import com.example.android.autofillframework.multidatasetservice.model.FilledAutofillField /** * Parser for an AssistStructure object. This is invoked when the Autofill Service receives an @@ -30,8 +28,8 @@ import com.example.android.autofillframework.multidatasetservice.model.SavableAu * parses the hierarchy and collects autofill metadata from {@link ViewNode}s along the way. */ internal class StructureParser(private val mStructure: AssistStructure) { - val autofillFields = AutofillFieldsCollection() - var clientFormData: ClientFormData = ClientFormData() + val autofillFields = AutofillFieldMetadataCollection() + var filledAutofillFieldCollection: FilledAutofillFieldCollection = FilledAutofillFieldCollection() private set @@ -49,7 +47,7 @@ internal class StructureParser(private val mStructure: AssistStructure) { private fun parse(forFill: Boolean) { Log.d(TAG, "Parsing structure for " + mStructure.activityComponent) val nodes = mStructure.windowNodeCount - clientFormData = ClientFormData() + filledAutofillFieldCollection = FilledAutofillFieldCollection() for (i in 0..nodes - 1) { val node = mStructure.getWindowNodeAt(i) val view = node.rootViewNode @@ -61,10 +59,10 @@ internal class StructureParser(private val mStructure: AssistStructure) { viewNode.autofillHints?.let { autofillHints -> if (autofillHints.isNotEmpty()) { if (forFill) { - autofillFields.add(AutofillField(viewNode)) + autofillFields.add(AutofillFieldMetadata(viewNode)) } else { - clientFormData.setAutofillValuesForHints(viewNode.autofillHints, - SavableAutofillData(viewNode)) + filledAutofillFieldCollection.setAutofillValuesForHints(viewNode.autofillHints, + FilledAutofillField(viewNode)) } } } diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/datasource/AutofillRepository.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/datasource/AutofillRepository.kt index 9e4d6e20..510d759f 100644 --- a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/datasource/AutofillRepository.kt +++ b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/datasource/AutofillRepository.kt @@ -16,22 +16,22 @@ package com.example.android.autofillframework.multidatasetservice.datasource import android.content.Context -import com.example.android.autofillframework.multidatasetservice.model.ClientFormData +import com.example.android.autofillframework.multidatasetservice.model.FilledAutofillFieldCollection import java.util.HashMap interface AutofillRepository { /** - * Gets saved ClientFormData that contains some objects that can autofill fields with these + * Gets saved FilledAutofillFieldCollection that contains some objects that can autofill fields with these * `autofillHints`. */ fun getClientFormData(context: Context, focusedAutofillHints: List, - allAutofillHints: List): HashMap? + allAutofillHints: List): HashMap? /** * Saves LoginCredential under this datasetName. */ - fun saveClientFormData(context: Context, clientFormData: ClientFormData) + fun saveClientFormData(context: Context, filledAutofillFieldCollection: FilledAutofillFieldCollection) /** * Clears all data. diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/datasource/SharedPrefsAutofillRepository.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/datasource/SharedPrefsAutofillRepository.kt index 8aeac0df..e2171708 100644 --- a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/datasource/SharedPrefsAutofillRepository.kt +++ b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/datasource/SharedPrefsAutofillRepository.kt @@ -18,7 +18,7 @@ package com.example.android.autofillframework.multidatasetservice.datasource import android.content.Context import android.content.SharedPreferences import android.util.ArraySet -import com.example.android.autofillframework.multidatasetservice.model.ClientFormData +import com.example.android.autofillframework.multidatasetservice.model.FilledAutofillFieldCollection import com.google.gson.Gson import com.google.gson.reflect.TypeToken @@ -38,13 +38,13 @@ object SharedPrefsAutofillRepository : AutofillRepository { } override fun getClientFormData(context: Context, focusedAutofillHints: List, - allAutofillHints: List): HashMap? { + allAutofillHints: List): HashMap? { var hasDataForFocusedAutofillHints = false - val clientFormDataMap = HashMap() + val clientFormDataMap = HashMap() val clientFormDataStringSet = getAllAutofillDataStringSet(context) for (clientFormDataString in clientFormDataStringSet) { - val type = object : TypeToken() {}.type - Gson().fromJson(clientFormDataString, type)?.let { clientFormData -> + val type = object : TypeToken() {}.type + Gson().fromJson(clientFormDataString, type)?.let { clientFormData -> if (clientFormData.helpsWithHints(focusedAutofillHints)) { // Saved data has data relevant to at least 1 of the hints associated with the // View in focus. @@ -66,11 +66,11 @@ object SharedPrefsAutofillRepository : AutofillRepository { } } - override fun saveClientFormData(context: Context, clientFormData: ClientFormData) { + override fun saveClientFormData(context: Context, filledAutofillFieldCollection: FilledAutofillFieldCollection) { val datasetName = "dataset-" + getDatasetNumber(context) - clientFormData.datasetName = datasetName + filledAutofillFieldCollection.datasetName = datasetName val allAutofillData = getAllAutofillDataStringSet(context) - allAutofillData.add(Gson().toJson(clientFormData).toString()) + allAutofillData.add(Gson().toJson(filledAutofillFieldCollection).toString()) saveAllAutofillDataStringSet(context, allAutofillData) incrementDatasetNumber(context) } diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/AutofillField.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/AutofillField.kt deleted file mode 100644 index 474454a3..00000000 --- a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/AutofillField.kt +++ /dev/null @@ -1,82 +0,0 @@ -/* - * 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.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 = view.autofillHints - val autofillId: AutofillId = view.autofillId - val autofillType: Int = view.autofillType - val autofillOptions: Array? = 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: CharSequence): 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 - } - } - } - } -} diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/AutofillFieldsCollection.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/AutofillFieldsCollection.kt deleted file mode 100644 index c62ea327..00000000 --- a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/AutofillFieldsCollection.kt +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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.model - -import android.view.autofill.AutofillId -import java.util.ArrayList -import java.util.HashMap - -/** - * Data structure that stores a collection of `AutofillField`s. Contains all of the client's `View` - * hierarchy autofill-relevant metadata. - */ -data class AutofillFieldsCollection(val autofillIds: ArrayList = ArrayList(), - val allAutofillHints: ArrayList = ArrayList(), - val focusedAutofillHints: ArrayList = ArrayList()) { - - private val autofillHintsToFieldsMap = HashMap>() - var saveType = 0 - private set - - fun add(autofillField: AutofillField) { - saveType = saveType or autofillField.saveType - autofillIds.add(autofillField.autofillId) - val hintsList = autofillField.autofillHints - allAutofillHints.addAll(hintsList) - if (autofillField.isFocused) { - focusedAutofillHints.addAll(hintsList) - } - autofillField.autofillHints.forEach { autofillHint -> - autofillHintsToFieldsMap[autofillHint] = autofillHintsToFieldsMap[autofillHint] ?: ArrayList() - autofillHintsToFieldsMap[autofillHint]?.add(autofillField) - } - } - - fun getFieldsForHint(hint: String): MutableList? { - return autofillHintsToFieldsMap[hint] - } -} diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/ClientFormData.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/ClientFormData.kt deleted file mode 100644 index 8f434abe..00000000 --- a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/ClientFormData.kt +++ /dev/null @@ -1,103 +0,0 @@ -/* - * 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.model - -import android.service.autofill.Dataset -import android.util.Log -import android.view.View -import android.view.autofill.AutofillId -import android.view.autofill.AutofillValue -import java.util.HashMap - - -/** - * ClientFormData is the model that represents all of the form data on a client app's page, plus the - * dataset name associated with it. - */ -class ClientFormData constructor(var datasetName: String? = null, - private val hintMap: HashMap = HashMap()) { - - private val TAG = "ClientFormData" - - /** - * Sets values for a list of autofillHints. - */ - fun setAutofillValuesForHints(autofillHints: Array, autofillData: SavableAutofillData) { - autofillHints.forEach { hint -> - hintMap[hint] = autofillData - } - } - - /** - * Populates a [Dataset.Builder] with appropriate values for each [AutofillId] - * in a `AutofillFieldsCollection`. - */ - fun applyToFields(autofillFieldsCollection: AutofillFieldsCollection, - datasetBuilder: Dataset.Builder): Boolean { - var setValueAtLeastOnce = false - for (hint in autofillFieldsCollection.allAutofillHints) { - val autofillFields = autofillFieldsCollection.getFieldsForHint(hint) ?: continue - for (autofillField in autofillFields) { - val autofillId = autofillField.autofillId - val autofillType = autofillField.autofillType - val savedAutofillValue = hintMap[hint] - when (autofillType) { - View.AUTOFILL_TYPE_LIST -> { - savedAutofillValue?.textValue?.let(autofillField::getAutofillOptionIndex)?.let { index -> - datasetBuilder.setValue(autofillId, AutofillValue.forList(index)) - setValueAtLeastOnce = true - } - } - View.AUTOFILL_TYPE_DATE -> { - savedAutofillValue?.dateValue?.let { date -> - datasetBuilder.setValue(autofillId, AutofillValue.forDate(date)) - setValueAtLeastOnce = true - } - } - View.AUTOFILL_TYPE_TEXT -> { - savedAutofillValue?.textValue?.let { text -> - datasetBuilder.setValue(autofillId, AutofillValue.forText(text)) - setValueAtLeastOnce = true - } - } - View.AUTOFILL_TYPE_TOGGLE -> { - savedAutofillValue?.toggleValue?.let { toggle -> - datasetBuilder.setValue(autofillId, AutofillValue.forToggle(toggle)) - setValueAtLeastOnce = true - } - } - else -> Log.w(TAG, "Invalid autofill type - " + autofillType) - } - } - } - return setValueAtLeastOnce - } - - /** - * Returns whether this model contains autofill data that is relevant to any of the - * autofillHints that are passed in. - */ - fun helpsWithHints(autofillHints: List): Boolean { - for (autofillHint in autofillHints) { - hintMap[autofillHint]?.let { savedAutofillValue -> - if (!savedAutofillValue.isNull()) { - return true - } - } - } - return false - } -} diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/FilledAutofillField.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/FilledAutofillField.kt new file mode 100644 index 00000000..fd2e1769 --- /dev/null +++ b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/FilledAutofillField.kt @@ -0,0 +1,52 @@ +/* + * 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.model + +import android.app.assist.AssistStructure +import android.view.autofill.AutofillValue + +/** + * JSON serializable data class containing the same data as an [AutofillValue]. + */ +class FilledAutofillField(viewNode: AssistStructure.ViewNode) { + var textValue: CharSequence? = null + var dateValue: Long? = null + var toggleValue: Boolean? = null + + init { + viewNode.autofillValue?.let { autofillValue -> + if (autofillValue.isList) { + val index = autofillValue.listValue + viewNode.autofillOptions?.let { autofillOptions -> + if (autofillOptions.size > index) { + textValue = autofillOptions[index] + } + } + } else if (autofillValue.isDate) { + dateValue = autofillValue.dateValue + } else if (autofillValue.isText) { + // Using toString of AutofillValue.getTextValue in order to save it to + // SharedPreferences. + textValue = autofillValue.textValue.toString() + } else { + } + } + } + + fun isNull(): Boolean { + return textValue == null && dateValue == null && toggleValue == null + } +} diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/FilledAutofillFieldCollection.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/FilledAutofillFieldCollection.kt new file mode 100644 index 00000000..1adf1dcc --- /dev/null +++ b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/FilledAutofillFieldCollection.kt @@ -0,0 +1,104 @@ +/* + * 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.model + +import android.service.autofill.Dataset +import android.util.Log +import android.view.View +import android.view.autofill.AutofillId +import android.view.autofill.AutofillValue +import com.example.android.autofillframework.multidatasetservice.AutofillFieldMetadataCollection +import java.util.HashMap + + +/** + * FilledAutofillFieldCollection is the model that represents all of the form data on a client app's page, plus the + * dataset name associated with it. + */ +class FilledAutofillFieldCollection constructor(var datasetName: String? = null, + private val hintMap: HashMap = HashMap()) { + + private val TAG = "FilledAutofillFieldCollection" + + /** + * Sets values for a list of autofillHints. + */ + fun setAutofillValuesForHints(autofillHints: Array, autofillField: FilledAutofillField) { + autofillHints.forEach { hint -> + hintMap[hint] = autofillField + } + } + + /** + * Populates a [Dataset.Builder] with appropriate values for each [AutofillId] + * in a `AutofillFieldMetadataCollection`. + */ + fun applyToFields(autofillFieldMetadataCollection: AutofillFieldMetadataCollection, + datasetBuilder: Dataset.Builder): Boolean { + var setValueAtLeastOnce = false + for (hint in autofillFieldMetadataCollection.allAutofillHints) { + val autofillFields = autofillFieldMetadataCollection.getFieldsForHint(hint) ?: continue + for (autofillField in autofillFields) { + val autofillId = autofillField.autofillId + val autofillType = autofillField.autofillType + val savedAutofillValue = hintMap[hint] + when (autofillType) { + View.AUTOFILL_TYPE_LIST -> { + savedAutofillValue?.textValue?.let(autofillField::getAutofillOptionIndex)?.let { index -> + datasetBuilder.setValue(autofillId, AutofillValue.forList(index)) + setValueAtLeastOnce = true + } + } + View.AUTOFILL_TYPE_DATE -> { + savedAutofillValue?.dateValue?.let { date -> + datasetBuilder.setValue(autofillId, AutofillValue.forDate(date)) + setValueAtLeastOnce = true + } + } + View.AUTOFILL_TYPE_TEXT -> { + savedAutofillValue?.textValue?.let { text -> + datasetBuilder.setValue(autofillId, AutofillValue.forText(text)) + setValueAtLeastOnce = true + } + } + View.AUTOFILL_TYPE_TOGGLE -> { + savedAutofillValue?.toggleValue?.let { toggle -> + datasetBuilder.setValue(autofillId, AutofillValue.forToggle(toggle)) + setValueAtLeastOnce = true + } + } + else -> Log.w(TAG, "Invalid autofill type - " + autofillType) + } + } + } + return setValueAtLeastOnce + } + + /** + * Returns whether this model contains autofill data that is relevant to any of the + * autofillHints that are passed in. + */ + fun helpsWithHints(autofillHints: List): Boolean { + for (autofillHint in autofillHints) { + hintMap[autofillHint]?.let { savedAutofillValue -> + if (!savedAutofillValue.isNull()) { + return true + } + } + } + return false + } +} diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/SavableAutofillData.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/SavableAutofillData.kt deleted file mode 100644 index 3c50d339..00000000 --- a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/SavableAutofillData.kt +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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.model - -import android.app.assist.AssistStructure -import android.view.autofill.AutofillValue - -/** - * JSON serializable data class containing the same data as an [AutofillValue]. - */ -class SavableAutofillData(viewNode: AssistStructure.ViewNode) { - var textValue: CharSequence? = null - var dateValue: Long? = null - var toggleValue: Boolean? = null - - init { - viewNode.autofillValue?.let { autofillValue -> - if (autofillValue.isList) { - val index = autofillValue.listValue - viewNode.autofillOptions?.let { autofillOptions -> - if (autofillOptions.size > index) { - textValue = autofillOptions[index] - } - } - } else if (autofillValue.isDate) { - dateValue = autofillValue.dateValue - } else if (autofillValue.isText) { - // Using toString of AutofillValue.getTextValue in order to save it to - // SharedPreferences. - textValue = autofillValue.textValue.toString() - } else { - } - } - } - - fun isNull(): Boolean { - return textValue == null && dateValue == null && toggleValue == null - } -} -- cgit v1.2.3