aboutsummaryrefslogtreecommitdiff
path: root/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com
diff options
context:
space:
mode:
authorDouglas Sigelbaum <sigelbaum@google.com>2017-06-21 18:20:53 -0700
committerDouglas Sigelbaum <sigelbaum@google.com>2017-08-31 21:05:35 +0000
commit313fb2327ba92aa3771f0d2a807be7149425bb39 (patch)
treee536f754e32a6035ee828a95def20f1d14f22000 /input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com
parent71556a6f6969b258fbcfd5bbc01ac32bb03d9674 (diff)
downloadandroid-313fb2327ba92aa3771f0d2a807be7149425bb39.tar.gz
Autofill sample: add credit card expiration compound view example.
Bug: 38182790 Test: manual Change-Id: Ib611578b811715103666233f84720ef2bb184c00
Diffstat (limited to 'input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com')
-rw-r--r--input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/app/CreditCardCompoundViewActivity.kt56
-rw-r--r--input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/app/CreditCardExpirationDateView.kt107
-rw-r--r--input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/app/MainActivity.kt54
3 files changed, 182 insertions, 35 deletions
diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/app/CreditCardCompoundViewActivity.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/app/CreditCardCompoundViewActivity.kt
new file mode 100644
index 00000000..09f83ac4
--- /dev/null
+++ b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/app/CreditCardCompoundViewActivity.kt
@@ -0,0 +1,56 @@
+/*
+ * 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.app
+
+import android.content.Context
+import android.content.Intent
+import android.os.Bundle
+import android.support.v7.app.AppCompatActivity
+
+import com.example.android.autofillframework.R
+import kotlinx.android.synthetic.main.credit_card_compound_view_activity.*
+
+class CreditCardCompoundViewActivity : AppCompatActivity() {
+
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+ setContentView(R.layout.credit_card_compound_view_activity)
+ submitButton.setOnClickListener { submit() }
+ clearButton.setOnClickListener { resetFields() }
+ }
+
+ private fun resetFields() {
+ creditCardExpirationView.reset()
+ creditCardNumberField.setText("")
+ }
+
+ /**
+ * Launches new Activity and finishes, triggering an autofill save request if the user entered
+ * any new data.
+ */
+ private fun submit() {
+ val intent = WelcomeActivity.getStartActivityIntent(this)
+ startActivity(intent)
+ finish()
+ }
+
+ companion object {
+ fun getStartActivityIntent(context: Context): Intent {
+ val intent = Intent(context, CreditCardCompoundViewActivity::class.java)
+ return intent
+ }
+ }
+}
diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/app/CreditCardExpirationDateView.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/app/CreditCardExpirationDateView.kt
new file mode 100644
index 00000000..609522b3
--- /dev/null
+++ b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/app/CreditCardExpirationDateView.kt
@@ -0,0 +1,107 @@
+/*
+ * 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.app
+
+import android.content.Context
+import android.util.AttributeSet
+import android.view.LayoutInflater
+import android.view.View
+import android.view.autofill.AutofillManager
+import android.view.autofill.AutofillValue
+import android.widget.AdapterView
+import android.widget.ArrayAdapter
+import android.widget.FrameLayout
+import com.example.android.autofillframework.R
+import kotlinx.android.synthetic.main.cc_exp_date.view.*
+import java.util.Calendar
+
+class CreditCardExpirationDateView @JvmOverloads constructor(
+ context: Context,
+ attrs: AttributeSet? = null,
+ defStyleAttr: Int = 0,
+ defStyleRes: Int = 0
+) : FrameLayout(context, attrs, defStyleAttr, defStyleRes) {
+
+ private val startYear = Calendar.getInstance().get(Calendar.YEAR)
+
+ init {
+ LayoutInflater.from(context).inflate(R.layout.cc_exp_date, this)
+ importantForAutofill = View.IMPORTANT_FOR_AUTOFILL_YES_EXCLUDE_DESCENDANTS
+ val spinnerOnItemSelectedListener = object : AdapterView.OnItemSelectedListener {
+ override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int,
+ id: Long) {
+ context.getSystemService(AutofillManager::class.java)
+ .notifyValueChanged(this@CreditCardExpirationDateView)
+ }
+
+ override fun onNothingSelected(parent: AdapterView<*>) = Unit
+ }
+ val years = arrayOfNulls<String>(CC_EXP_YEARS_COUNT)
+ for (i in 0 until years.size) {
+ years[i] = Integer.toString(startYear + i)
+ }
+
+ with(ccExpMonth) {
+ adapter = ArrayAdapter.createFromResource(context, R.array.month_array,
+ android.R.layout.simple_spinner_item).apply {
+ setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
+ }
+ onItemSelectedListener = spinnerOnItemSelectedListener
+ }
+
+ with(ccExpYear) {
+ adapter = ArrayAdapter<String>(context,
+ android.R.layout.simple_spinner_item, years)
+ onItemSelectedListener = spinnerOnItemSelectedListener
+ }
+ }
+
+ override fun getAutofillValue() =
+ AutofillValue.forDate(Calendar.getInstance().apply {
+ // clear() sets hours, minutes, seconds, and millis to 0 to ensure that after
+ // autofill() is called, getAutofillValue() == the value that was originally passed
+ // into autofill(). Without clear(), the view will not turn yellow when updated.
+ clear()
+ val year = Integer.parseInt(ccExpYear.selectedItem.toString())
+ val month = ccExpMonth.selectedItemPosition
+ set(Calendar.YEAR, year)
+ set(Calendar.MONTH, month)
+ }.timeInMillis)
+
+ override fun autofill(value: AutofillValue) {
+ if (!value.isDate) {
+ return
+ }
+ val calendar = Calendar.getInstance().apply {
+ timeInMillis = value.dateValue
+ }
+ val month = calendar.get(Calendar.MONTH)
+ val year = calendar.get(Calendar.YEAR)
+ ccExpMonth.setSelection(month)
+ ccExpYear.setSelection(year - startYear)
+ }
+
+ override fun getAutofillType() = View.AUTOFILL_TYPE_DATE
+
+ fun reset() {
+ ccExpMonth.setSelection(0)
+ ccExpYear.setSelection(0)
+ }
+
+ companion object {
+ private const val CC_EXP_YEARS_COUNT = 5
+ }
+} \ No newline at end of file
diff --git a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/app/MainActivity.kt b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/app/MainActivity.kt
index d251369d..43a25220 100644
--- a/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/app/MainActivity.kt
+++ b/input/autofill/AutofillFramework/kotlinApp/Application/src/main/java/com/example/android/autofillframework/app/MainActivity.kt
@@ -19,11 +19,7 @@ import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import com.example.android.autofillframework.R
-import kotlinx.android.synthetic.main.activity_main.creditCardCheckoutButton
-import kotlinx.android.synthetic.main.activity_main.emailComposeButton
-import kotlinx.android.synthetic.main.activity_main.standardLoginWithAutoCompleteButton
-import kotlinx.android.synthetic.main.activity_main.standardViewSignInButton
-import kotlinx.android.synthetic.main.activity_main.virtualViewSignInButton
+import kotlinx.android.synthetic.main.activity_main.*
/**
* This is used to launch sample activities that showcase autofill.
@@ -33,35 +29,23 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
- standardViewSignInButton.setNavigationButtonClickListener(View.OnClickListener { standardViewSignIn() })
- virtualViewSignInButton.setNavigationButtonClickListener(View.OnClickListener { virtualViewSignIn() })
- creditCardCheckoutButton.setNavigationButtonClickListener(View.OnClickListener { creditCardCheckout() })
- standardLoginWithAutoCompleteButton.setNavigationButtonClickListener(View.OnClickListener { standardAutoCompleteSignIn() })
- emailComposeButton.setNavigationButtonClickListener(View.OnClickListener { emailCompose() })
- }
-
- private fun creditCardCheckout() {
- val intent = CreditCardActivity.getStartActivityIntent(this)
- startActivity(intent)
- }
-
- private fun standardViewSignIn() {
- val intent = StandardSignInActivity.getStartActivityIntent(this)
- startActivity(intent)
- }
-
- private fun standardAutoCompleteSignIn() {
- val intent = StandardAutoCompleteSignInActivity.getStartActivityIntent(this)
- startActivity(intent)
- }
-
- private fun virtualViewSignIn() {
- val intent = VirtualSignInActivity.getStartActivityIntent(this)
- startActivity(intent)
- }
-
- private fun emailCompose() {
- val intent = EmailComposeActivity.getStartActivityIntent(this)
- startActivity(intent)
+ standardViewSignInButton.setNavigationButtonClickListener(View.OnClickListener {
+ startActivity(StandardSignInActivity.getStartActivityIntent(this@MainActivity))
+ })
+ virtualViewSignInButton.setNavigationButtonClickListener(View.OnClickListener {
+ startActivity(VirtualSignInActivity.getStartActivityIntent(this@MainActivity))
+ })
+ creditCardCheckoutButton.setNavigationButtonClickListener(View.OnClickListener {
+ startActivity(CreditCardActivity.getStartActivityIntent(this@MainActivity))
+ })
+ standardLoginWithAutoCompleteButton.setNavigationButtonClickListener(View.OnClickListener {
+ startActivity(StandardAutoCompleteSignInActivity.getStartActivityIntent(this@MainActivity))
+ })
+ emailComposeButton.setNavigationButtonClickListener(View.OnClickListener {
+ startActivity(EmailComposeActivity.getStartActivityIntent(this@MainActivity))
+ })
+ creditCardCompoundViewButton.setNavigationButtonClickListener(View.OnClickListener {
+ startActivity(CreditCardCompoundViewActivity.getStartActivityIntent(this@MainActivity))
+ })
}
} \ No newline at end of file