aboutsummaryrefslogtreecommitdiff
path: root/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android
diff options
context:
space:
mode:
authorDouglas Sigelbaum <sigelbaum@google.com>2017-06-14 14:35:49 -0700
committerDouglas Sigelbaum <sigelbaum@google.com>2017-06-22 10:54:38 -0700
commitdd108ebb984500ab79aff58710c6936def8b0af9 (patch)
treef46ce8fc3bd7bb69e9801175d3f27e729a38cd2d /input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android
parentbb55ff972cb9708b875bebcbd3ceef4f283c2ae6 (diff)
downloadandroid-dd108ebb984500ab79aff58710c6936def8b0af9.tar.gz
Autofill sample: added icons, fixed an auth bug, added more comments.
* Added icons for autofill popups. * Fixed RTE in the service due to autofill being enabled on the autofill login page. * Added more comments to other unrelated functions. * Added helper functions for ensuring only supported hints are supported. * Added string resources. * Changed some method/variable names to match newish class names. Bug: 38182790 Test: manual Change-Id: I1874c7be12ee1b50f5cc94542561d00820ba1874
Diffstat (limited to 'input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android')
-rw-r--r--input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/AuthActivity.kt2
-rw-r--r--input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/AutofillFieldMetadata.kt2
-rw-r--r--input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/AutofillHelper.kt20
-rw-r--r--input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/MyAutofillService.kt15
-rw-r--r--input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/StructureParser.kt3
-rw-r--r--input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/datasource/AutofillRepository.kt4
-rw-r--r--input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/datasource/SharedPrefsAutofillRepository.kt11
-rw-r--r--input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/FilledAutofillField.kt14
-rw-r--r--input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/FilledAutofillFieldCollection.kt19
9 files changed, 55 insertions, 35 deletions
diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/AuthActivity.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/AuthActivity.kt
index 4c1b5b72..eea17992 100644
--- a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/AuthActivity.kt
+++ b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/AuthActivity.kt
@@ -92,7 +92,7 @@ class AuthActivity : AppCompatActivity() {
val autofillFields = parser.autofillFields
mReplyIntent = Intent()
val clientFormDataMap = SharedPrefsAutofillRepository
- .getClientFormData(this, autofillFields.focusedAutofillHints, autofillFields.allAutofillHints)
+ .getFilledAutofillFieldCollection(this, autofillFields.focusedAutofillHints, autofillFields.allAutofillHints)
if (forResponse) {
AutofillHelper.newResponse(this, false, autofillFields, clientFormDataMap)?.let(this::setResponseIntent)
} else {
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
index 47539dfd..f894a18b 100644
--- 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
@@ -28,7 +28,7 @@ class AutofillFieldMetadata(view: ViewNode) {
var saveType = 0
private set
- val autofillHints: Array<String> = view.autofillHints
+ val autofillHints: Array<String> = view.autofillHints.filter(AutofillHelper::isValidHint).toTypedArray()
val autofillId: AutofillId = view.autofillId
val autofillType: Int = view.autofillType
val autofillOptions: Array<CharSequence>? = view.autofillOptions
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 e8b5fb1e..027d0f31 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
@@ -19,6 +19,7 @@ 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
@@ -52,14 +53,21 @@ object AutofillHelper {
* client View.
*/
fun newDataset(context: Context, autofillFieldMetadata: AutofillFieldMetadataCollection,
- filledAutofillFieldCollection: FilledAutofillFieldCollection, datasetAuth: Boolean): Dataset? {
+ filledAutofillFieldCollection: FilledAutofillFieldCollection,
+ datasetAuth: Boolean): Dataset? {
filledAutofillFieldCollection.datasetName?.let { datasetName ->
- val datasetBuilder = Dataset.Builder(newRemoteViews(context.packageName, datasetName))
- val setValueAtLeastOnce = filledAutofillFieldCollection.applyToFields(autofillFieldMetadata, datasetBuilder)
+ 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()
}
@@ -67,9 +75,11 @@ object AutofillHelper {
return null
}
- fun newRemoteViews(packageName: String, remoteViewsText: String): RemoteViews {
+ fun newRemoteViews(packageName: String, remoteViewsText: String,
+ @DrawableRes drawableId: Int): RemoteViews {
val presentation = RemoteViews(packageName, R.layout.multidataset_service_list_item)
- presentation.setTextViewText(R.id.text1, remoteViewsText)
+ presentation.setTextViewText(R.id.text, remoteViewsText)
+ presentation.setImageViewResource(R.id.icon, drawableId)
return presentation
}
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 fa069241..ce2ce0d7 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
@@ -36,14 +36,6 @@ class MyAutofillService : AutofillService() {
val structure = request.getFillContexts().get(request.getFillContexts().size - 1).structure
val data = request.clientState
Log.d(TAG, "onFillRequest(): data=" + bundleToString(data))
-
- // Temporary hack for disabling autofill for components in this autofill service.
- // i.e. we don't want to autofill components in AuthActivity.
- if (structure.activityComponent.toShortString()
- .contains("com.example.android.autofillframework.service")) {
- callback.onSuccess(null)
- return
- }
cancellationSignal.setOnCancelListener { Log.w(TAG, "Cancel autofill not implemented in this sample.") }
// Parse AutoFill data in Activity
val parser = StructureParser(structure)
@@ -58,13 +50,13 @@ class MyAutofillService : AutofillService() {
// to generate Response.
val sender = AuthActivity.getAuthIntentSenderForResponse(this)
val presentation = AutofillHelper
- .newRemoteViews(packageName, getString(R.string.autofill_sign_in_prompt))
+ .newRemoteViews(packageName, getString(R.string.autofill_sign_in_prompt), R.drawable.ic_lock_black_24dp)
responseBuilder
.setAuthentication(autofillFields.autofillIds.toTypedArray(), sender, presentation)
callback.onSuccess(responseBuilder.build())
} else {
val datasetAuth = MyPreferences.isDatasetAuth(this)
- val clientFormDataMap = SharedPrefsAutofillRepository.getClientFormData(this,
+ val clientFormDataMap = SharedPrefsAutofillRepository.getFilledAutofillFieldCollection(this,
autofillFields.focusedAutofillHints, autofillFields.allAutofillHints)
val response = AutofillHelper.newResponse(this, datasetAuth, autofillFields, clientFormDataMap)
callback.onSuccess(response)
@@ -78,7 +70,8 @@ class MyAutofillService : AutofillService() {
Log.d(TAG, "onSaveRequest(): data=" + bundleToString(data))
val parser = StructureParser(structure)
parser.parseForSave()
- SharedPrefsAutofillRepository.saveClientFormData(this, parser.filledAutofillFieldCollection)
+ SharedPrefsAutofillRepository.saveFilledAutofillFieldCollection(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 31b59c8a..d1bbc9c1 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
@@ -61,8 +61,7 @@ internal class StructureParser(private val mStructure: AssistStructure) {
if (forFill) {
autofillFields.add(AutofillFieldMetadata(viewNode))
} else {
- filledAutofillFieldCollection.setAutofillValuesForHints(viewNode.autofillHints,
- FilledAutofillField(viewNode))
+ filledAutofillFieldCollection.add(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 510d759f..00a7d8df 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
@@ -25,13 +25,13 @@ interface AutofillRepository {
* Gets saved FilledAutofillFieldCollection that contains some objects that can autofill fields with these
* `autofillHints`.
*/
- fun getClientFormData(context: Context, focusedAutofillHints: List<String>,
+ fun getFilledAutofillFieldCollection(context: Context, focusedAutofillHints: List<String>,
allAutofillHints: List<String>): HashMap<String, FilledAutofillFieldCollection>?
/**
* Saves LoginCredential under this datasetName.
*/
- fun saveClientFormData(context: Context, filledAutofillFieldCollection: FilledAutofillFieldCollection)
+ fun saveFilledAutofillFieldCollection(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 e2171708..98119152 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
@@ -20,6 +20,7 @@ import android.content.SharedPreferences
import android.util.ArraySet
import com.example.android.autofillframework.multidatasetservice.model.FilledAutofillFieldCollection
import com.google.gson.Gson
+import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
@@ -37,14 +38,15 @@ object SharedPrefsAutofillRepository : AutofillRepository {
return context.applicationContext.getSharedPreferences(SHARED_PREF_KEY, Context.MODE_PRIVATE)
}
- override fun getClientFormData(context: Context, focusedAutofillHints: List<String>,
+ override fun getFilledAutofillFieldCollection(context: Context, focusedAutofillHints: List<String>,
allAutofillHints: List<String>): HashMap<String, FilledAutofillFieldCollection>? {
var hasDataForFocusedAutofillHints = false
val clientFormDataMap = HashMap<String, FilledAutofillFieldCollection>()
val clientFormDataStringSet = getAllAutofillDataStringSet(context)
for (clientFormDataString in clientFormDataStringSet) {
val type = object : TypeToken<FilledAutofillFieldCollection>() {}.type
- Gson().fromJson<FilledAutofillFieldCollection>(clientFormDataString, type)?.let { clientFormData ->
+ val gson: Gson = GsonBuilder().excludeFieldsWithoutExposeAnnotation().setPrettyPrinting().create()
+ gson.fromJson<FilledAutofillFieldCollection>(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 +68,12 @@ object SharedPrefsAutofillRepository : AutofillRepository {
}
}
- override fun saveClientFormData(context: Context, filledAutofillFieldCollection: FilledAutofillFieldCollection) {
+ override fun saveFilledAutofillFieldCollection(context: Context, filledAutofillFieldCollection: FilledAutofillFieldCollection) {
val datasetName = "dataset-" + getDatasetNumber(context)
filledAutofillFieldCollection.datasetName = datasetName
val allAutofillData = getAllAutofillDataStringSet(context)
- allAutofillData.add(Gson().toJson(filledAutofillFieldCollection).toString())
+ val gson: Gson = GsonBuilder().excludeFieldsWithoutExposeAnnotation().setPrettyPrinting().create()
+ 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/FilledAutofillField.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/multidatasetservice/model/FilledAutofillField.kt
index fd2e1769..64617ccf 100644
--- 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
@@ -17,22 +17,32 @@ package com.example.android.autofillframework.multidatasetservice.model
import android.app.assist.AssistStructure
import android.view.autofill.AutofillValue
+import com.example.android.autofillframework.multidatasetservice.AutofillHelper
+import com.google.gson.annotations.Expose
/**
* JSON serializable data class containing the same data as an [AutofillValue].
*/
class FilledAutofillField(viewNode: AssistStructure.ViewNode) {
- var textValue: CharSequence? = null
+ @Expose
+ var textValue: String? = null
+
+ @Expose
var dateValue: Long? = null
+
+ @Expose
var toggleValue: Boolean? = null
+ val autofillHints: Array<String> =
+ viewNode.autofillHints.filter(AutofillHelper::isValidHint).toTypedArray()
+
init {
viewNode.autofillValue?.let { autofillValue ->
if (autofillValue.isList) {
val index = autofillValue.listValue
viewNode.autofillOptions?.let { autofillOptions ->
if (autofillOptions.size > index) {
- textValue = autofillOptions[index]
+ textValue = autofillOptions[index].toString()
}
}
} else if (autofillValue.isDate) {
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
index 03e13ef5..032e1251 100644
--- 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
@@ -23,6 +23,7 @@ import android.view.autofill.AutofillValue
import com.example.android.autofillframework.CommonUtil.TAG
import com.example.android.autofillframework.multidatasetservice.AutofillFieldMetadataCollection
import com.example.android.autofillframework.multidatasetservice.AutofillHelper
+import com.google.gson.annotations.Expose
import java.util.HashMap
@@ -30,21 +31,24 @@ 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<String, FilledAutofillField> = HashMap<String, FilledAutofillField>()) {
+class FilledAutofillFieldCollection constructor(@Expose var datasetName: String? = null,
+ @Expose private val hintMap: HashMap<String, FilledAutofillField> = HashMap<String, FilledAutofillField>()) {
/**
* Sets values for a list of autofillHints.
*/
- fun setAutofillValuesForHints(autofillHints: Array<String>, autofillField: FilledAutofillField) {
- autofillHints.filter(AutofillHelper::isValidHint).forEach { autofillHint ->
+ fun add(autofillField: FilledAutofillField) {
+ autofillField.autofillHints.forEach { autofillHint ->
hintMap[autofillHint] = autofillField
}
}
/**
* Populates a [Dataset.Builder] with appropriate values for each [AutofillId]
- * in a `AutofillFieldMetadataCollection`.
+ * in a `AutofillFieldMetadataCollection`. In other words, it builds an Autofill dataset
+ * by applying saved values (from this `FilledAutofillFieldCollection`) to Views specified
+ * in a `AutofillFieldMetadataCollection`, which represents the current page the user is
+ * on.
*/
fun applyToFields(autofillFieldMetadataCollection: AutofillFieldMetadataCollection,
datasetBuilder: Dataset.Builder): Boolean {
@@ -88,8 +92,9 @@ class FilledAutofillFieldCollection constructor(var datasetName: String? = null,
}
/**
- * Returns whether this model contains autofill data that is relevant to any of the
- * autofillHints that are passed in.
+ * @param autofillHints List of autofill hints, usually associated with a View or set of Views.
+ * @return whether any of the filled fields on the page have at least 1 autofillHint that is
+ * in the provided autofillHints.
*/
fun helpsWithHints(autofillHints: List<String>): Boolean {
for (autofillHint in autofillHints) {