diff options
author | Douglas Sigelbaum <sigelbaum@google.com> | 2017-05-31 00:30:47 -0700 |
---|---|---|
committer | Douglas Sigelbaum <sigelbaum@google.com> | 2017-05-31 11:45:41 -0700 |
commit | 389ba153ecf015cbff36764e9c07f12ac3a0353d (patch) | |
tree | 284aa0a3355b15801ab5a1e171ff541e1ee850ba /input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework | |
parent | da2273924314181bb5e7e50fc52effc2f00aff1b (diff) | |
download | android-389ba153ecf015cbff36764e9c07f12ac3a0353d.tar.gz |
Changes to get Kotlin and Java autofill samples in sync.
Also fixed a crash when using datasetAuth. Impacted both samples.
Bug: 38182790
Test: manual
Change-Id: I1bfb00e3a22708d7bcbaade2aff3b00789499fe7
Diffstat (limited to 'input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework')
9 files changed, 44 insertions, 39 deletions
diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/app/CustomVirtualView.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/app/CustomVirtualView.kt index 86dc9b25..bef16afe 100644 --- a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/app/CustomVirtualView.kt +++ b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/app/CustomVirtualView.kt @@ -70,20 +70,21 @@ class CustomVirtualView(context: Context, attrs: AttributeSet) : View(context, a } override fun autofill(values: SparseArray<AutofillValue>) { - // User has just selected a Dataset from the list of Autofill suggestions and the Dataset's - // AutofillValue gets passed into this method. + // User has just selected a Dataset from the list of autofill suggestions. + // The Dataset is comprised of a list of AutofillValues, with each AutofillValue meant + // to fill a specific autofillable view. Now we have to update the UI based on the + // AutofillValues in the list. Log.d(TAG, "autofill(): " + values) for (i in 0..values.size() - 1) { val id = values.keyAt(i) val value = values.valueAt(i) - - mItems[id]?.let { - if (!it.editable) { - Log.w(TAG, "Item for autofillId $id is not editable: $it") - return@autofill + mItems[id]?.let { item -> + if (item.editable) { + // Set the item's text to the text wrapped in the AutofillValue. + item.text = value.textValue + } else { + Log.w(TAG, "Item for autofillId $id is not editable: $item") } - // Set the item's text to the text wrapped in the AutofillValue. - it.text = value.textValue } } postInvalidate() diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/app/VirtualLoginActivity.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/app/VirtualLoginActivity.kt index c6eb721f..52081afd 100644 --- a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/app/VirtualLoginActivity.kt +++ b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/app/VirtualLoginActivity.kt @@ -51,6 +51,7 @@ class VirtualLoginActivity : AppCompatActivity() { if (valid) { val intent = WelcomeActivity.getStartActivityIntent(this@VirtualLoginActivity) startActivity(intent) + finish() } else { Toast.makeText(this, "Authentication failed.", Toast.LENGTH_SHORT).show() } diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/AuthActivity.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/AuthActivity.kt index 12be45bb..d102a7e9 100644 --- a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/AuthActivity.kt +++ b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/AuthActivity.kt @@ -97,8 +97,8 @@ class AuthActivity : Activity() { } else { val datasetName = intent.getStringExtra(EXTRA_DATASET_NAME) clientFormDataMap?.let { - it[datasetName]?.let { - AutofillHelper.newDataset(this, autofillFields, it)?.let(this::setDatasetIntent) + it[datasetName]?.let { clientFormData -> + AutofillHelper.newDataset(this, autofillFields, clientFormData, false)?.let(this::setDatasetIntent) } } } @@ -115,7 +115,7 @@ class AuthActivity : Activity() { companion object { // Unique autofillId for dataset intents. - private var sDatasetPendingIntentId = 0 + private var datasetPendingIntentId = 0 internal fun getAuthIntentSenderForResponse(context: Context): IntentSender { val intent = Intent(context, AuthActivity::class.java) @@ -127,7 +127,7 @@ class AuthActivity : Activity() { val intent = Intent(context, AuthActivity::class.java) intent.putExtra(EXTRA_DATASET_NAME, datasetName) intent.putExtra(EXTRA_FOR_RESPONSE, false) - return PendingIntent.getActivity(context, ++sDatasetPendingIntentId, intent, + return PendingIntent.getActivity(context, ++datasetPendingIntentId, intent, PendingIntent.FLAG_CANCEL_CURRENT).intentSender } } diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/AutofillHelper.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/AutofillHelper.kt index b7470d7b..58666ef4 100644 --- a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/AutofillHelper.kt +++ b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/AutofillHelper.kt @@ -37,10 +37,14 @@ object AutofillHelper { * client View. */ fun newDataset(context: Context, autofillFields: AutofillFieldsCollection, - clientFormData: ClientFormData): Dataset? { + clientFormData: ClientFormData, datasetAuth: Boolean): Dataset? { clientFormData.datasetName?.let { datasetName -> val datasetBuilder = Dataset.Builder(newRemoteViews(context.packageName, datasetName)) val setValueAtLeastOnce = clientFormData.applyToFields(autofillFields, datasetBuilder) + if (datasetAuth) { + val sender = AuthActivity.getAuthIntentSenderForDataset(context, datasetName) + datasetBuilder.setAuthentication(sender) + } if (setValueAtLeastOnce) { return datasetBuilder.build() } @@ -65,18 +69,8 @@ object AutofillHelper { clientFormDataMap?.keys?.let { datasetNames -> for (datasetName in datasetNames) { clientFormDataMap[datasetName]?.let { clientFormData -> - if (datasetAuth) { - clientFormData.datasetName?.let { - val datasetBuilder = Dataset.Builder(newRemoteViews(context.packageName, it)) - val sender = AuthActivity - .getAuthIntentSenderForDataset(context, it) - datasetBuilder.setAuthentication(sender) - responseBuilder.addDataset(datasetBuilder.build()) - } - } else { - val dataset = newDataset(context, autofillFields, clientFormData) - dataset?.let(responseBuilder::addDataset) - } + val dataset = newDataset(context, autofillFields, clientFormData, datasetAuth) + dataset?.let(responseBuilder::addDataset) } } } diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/MyAutofillService.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/MyAutofillService.kt index 9bc2a8c2..41712bd1 100644 --- a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/MyAutofillService.kt +++ b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/MyAutofillService.kt @@ -76,7 +76,8 @@ class MyAutofillService : AutofillService() { callback.onSuccess(responseBuilder.build()) } else { val datasetAuth = MyPreferences.isDatasetAuth(this) - val clientFormDataMap = SharedPrefsAutofillRepository.getClientFormData(this, autofillFields.focusedAutofillHints, autofillFields.allAutofillHints) + val clientFormDataMap = SharedPrefsAutofillRepository.getClientFormData(this, + autofillFields.focusedAutofillHints, autofillFields.allAutofillHints) val response = AutofillHelper.newResponse(this, datasetAuth, autofillFields, clientFormDataMap) callback.onSuccess(response) } diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/StructureParser.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/StructureParser.kt index e05a2d51..2cb37eea 100644 --- a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/StructureParser.kt +++ b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/StructureParser.kt @@ -22,12 +22,12 @@ import com.example.android.autofillframework.CommonUtil.TAG import com.example.android.autofillframework.service.model.AutofillField import com.example.android.autofillframework.service.model.AutofillFieldsCollection import com.example.android.autofillframework.service.model.ClientFormData -import com.example.android.autofillframework.service.model.MutableAutofillValue +import com.example.android.autofillframework.service.model.SavableAutofillData /** * Parser for an AssistStructure object. This is invoked when the Autofill Service receives an - * AssistStructure from the client Activity, representing its View hierarchy. In this - * sample, it parses the hierarchy and records + * AssistStructure from the client Activity, representing its View hierarchy. In this sample, it + * parses the hierarchy and collects autofill metadata from {@link ViewNode}s along the way. */ internal class StructureParser(private val mStructure: AssistStructure) { val autofillFields = AutofillFieldsCollection() @@ -64,7 +64,7 @@ internal class StructureParser(private val mStructure: AssistStructure) { autofillFields.add(AutofillField(viewNode)) } else { clientFormData.setAutofillValuesForHints(viewNode.autofillHints, - MutableAutofillValue(viewNode)) + SavableAutofillData(viewNode)) } } } diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/datasource/SharedPrefsAutofillRepository.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/datasource/SharedPrefsAutofillRepository.kt index a7eadc96..e2fb8708 100644 --- a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/datasource/SharedPrefsAutofillRepository.kt +++ b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/datasource/SharedPrefsAutofillRepository.kt @@ -24,9 +24,9 @@ import com.google.gson.reflect.TypeToken /** - * Singleton autofill data repository, that stores autofill fields to SharedPreferences. - * DISCLAIMER, you should not store sensitive fields like user data unencrypted. This is only done - * here for simplicity and learning purposes. + * Singleton autofill data repository that stores autofill fields to SharedPreferences. + * Disclaimer: you should not store sensitive fields like user data unencrypted. This is done + * here only for simplicity and learning purposes. */ object SharedPrefsAutofillRepository : AutofillRepository { private val SHARED_PREF_KEY = "com.example.android.autofillframework.service" @@ -46,9 +46,13 @@ object SharedPrefsAutofillRepository : AutofillRepository { val type = object : TypeToken<ClientFormData>() {}.type Gson().fromJson<ClientFormData>(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. hasDataForFocusedAutofillHints = true clientFormData.datasetName?.let { datasetName -> if (clientFormData.helpsWithHints(allAutofillHints)) { + // Saved data has data relevant to at least 1 of these hints associated with any + // of the Views in the hierarchy. clientFormDataMap.put(datasetName, clientFormData) } } diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/model/ClientFormData.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/model/ClientFormData.kt index 78980aaa..2dc844ae 100644 --- a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/model/ClientFormData.kt +++ b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/model/ClientFormData.kt @@ -28,16 +28,16 @@ import java.util.HashMap * dataset name associated with it. */ class ClientFormData constructor(var datasetName: String? = null, - private val hintMap: HashMap<String, MutableAutofillValue> = HashMap<String, MutableAutofillValue>()) { + private val hintMap: HashMap<String, SavableAutofillData> = HashMap<String, SavableAutofillData>()) { private val TAG = "ClientFormData" /** * Sets values for a list of autofillHints. */ - fun setAutofillValuesForHints(autofillHints: Array<String>, autofillValue: MutableAutofillValue) { + fun setAutofillValuesForHints(autofillHints: Array<String>, autofillData: SavableAutofillData) { autofillHints.forEach { hint -> - hintMap[hint] = autofillValue + hintMap[hint] = autofillData } } @@ -86,6 +86,10 @@ class ClientFormData constructor(var datasetName: String? = null, 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<String>): Boolean { for (autofillHint in autofillHints) { hintMap[autofillHint]?.let { savedAutofillValue -> diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/model/MutableAutofillValue.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/model/SavableAutofillData.kt index 702a1358..44baebfa 100644 --- a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/model/MutableAutofillValue.kt +++ b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/service/model/SavableAutofillData.kt @@ -19,9 +19,9 @@ import android.app.assist.AssistStructure import android.view.autofill.AutofillValue /** - * Mutable, JSON serializable data class containing the same data as an [AutofillValue]. + * JSON serializable data class containing the same data as an [AutofillValue]. */ -class MutableAutofillValue(viewNode: AssistStructure.ViewNode) { +class SavableAutofillData(viewNode: AssistStructure.ViewNode) { var textValue: String? = null var dateValue: Long? = null var toggleValue: Boolean? = null |